PR tree-optimization/48377
[official-gcc.git] / gcc / cgraph.c
bloba6217e9af35bc6e26ac79a26547a246541bfbbad
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 edge->call_stmt_size = 0;
1036 edge->call_stmt_time = 0;
1037 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1038 edge->can_throw_external
1039 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1040 pop_cfun ();
1041 edge->call_stmt_cannot_inline_p =
1042 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1043 if (call_stmt && caller->call_site_hash)
1044 cgraph_add_edge_to_call_site_hash (edge);
1046 edge->indirect_info = NULL;
1047 edge->indirect_inlining_edge = 0;
1049 return edge;
1052 /* Create edge from CALLER to CALLEE in the cgraph. */
1054 struct cgraph_edge *
1055 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1056 gimple call_stmt, gcov_type count, int freq, int nest)
1058 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1059 count, freq, nest);
1061 edge->indirect_unknown_callee = 0;
1062 initialize_inline_failed (edge);
1064 edge->next_caller = callee->callers;
1065 if (callee->callers)
1066 callee->callers->prev_caller = edge;
1067 edge->next_callee = caller->callees;
1068 if (caller->callees)
1069 caller->callees->prev_callee = edge;
1070 caller->callees = edge;
1071 callee->callers = edge;
1073 return edge;
1076 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1078 struct cgraph_indirect_call_info *
1079 cgraph_allocate_init_indirect_info (void)
1081 struct cgraph_indirect_call_info *ii;
1083 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1084 ii->param_index = -1;
1085 return ii;
1088 /* Create an indirect edge with a yet-undetermined callee where the call
1089 statement destination is a formal parameter of the caller with index
1090 PARAM_INDEX. */
1092 struct cgraph_edge *
1093 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1094 int ecf_flags,
1095 gcov_type count, int freq, int nest)
1097 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1098 count, freq, nest);
1100 edge->indirect_unknown_callee = 1;
1101 initialize_inline_failed (edge);
1103 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1104 edge->indirect_info->ecf_flags = ecf_flags;
1106 edge->next_callee = caller->indirect_calls;
1107 if (caller->indirect_calls)
1108 caller->indirect_calls->prev_callee = edge;
1109 caller->indirect_calls = edge;
1111 return edge;
1114 /* Remove the edge E from the list of the callers of the callee. */
1116 static inline void
1117 cgraph_edge_remove_callee (struct cgraph_edge *e)
1119 gcc_assert (!e->indirect_unknown_callee);
1120 if (e->prev_caller)
1121 e->prev_caller->next_caller = e->next_caller;
1122 if (e->next_caller)
1123 e->next_caller->prev_caller = e->prev_caller;
1124 if (!e->prev_caller)
1125 e->callee->callers = e->next_caller;
1128 /* Remove the edge E from the list of the callees of the caller. */
1130 static inline void
1131 cgraph_edge_remove_caller (struct cgraph_edge *e)
1133 if (e->prev_callee)
1134 e->prev_callee->next_callee = e->next_callee;
1135 if (e->next_callee)
1136 e->next_callee->prev_callee = e->prev_callee;
1137 if (!e->prev_callee)
1139 if (e->indirect_unknown_callee)
1140 e->caller->indirect_calls = e->next_callee;
1141 else
1142 e->caller->callees = e->next_callee;
1144 if (e->caller->call_site_hash)
1145 htab_remove_elt_with_hash (e->caller->call_site_hash,
1146 e->call_stmt,
1147 htab_hash_pointer (e->call_stmt));
1150 /* Put the edge onto the free list. */
1152 static void
1153 cgraph_free_edge (struct cgraph_edge *e)
1155 int uid = e->uid;
1157 /* Clear out the edge so we do not dangle pointers. */
1158 memset (e, 0, sizeof (*e));
1159 e->uid = uid;
1160 NEXT_FREE_EDGE (e) = free_edges;
1161 free_edges = e;
1164 /* Remove the edge E in the cgraph. */
1166 void
1167 cgraph_remove_edge (struct cgraph_edge *e)
1169 /* Call all edge removal hooks. */
1170 cgraph_call_edge_removal_hooks (e);
1172 if (!e->indirect_unknown_callee)
1173 /* Remove from callers list of the callee. */
1174 cgraph_edge_remove_callee (e);
1176 /* Remove from callees list of the callers. */
1177 cgraph_edge_remove_caller (e);
1179 /* Put the edge onto the free list. */
1180 cgraph_free_edge (e);
1183 /* Set callee of call graph edge E and add it to the corresponding set of
1184 callers. */
1186 static void
1187 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1189 e->prev_caller = NULL;
1190 if (n->callers)
1191 n->callers->prev_caller = e;
1192 e->next_caller = n->callers;
1193 n->callers = e;
1194 e->callee = n;
1197 /* Redirect callee of E to N. The function does not update underlying
1198 call expression. */
1200 void
1201 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1203 /* Remove from callers list of the current callee. */
1204 cgraph_edge_remove_callee (e);
1206 /* Insert to callers list of the new callee. */
1207 cgraph_set_edge_callee (e, n);
1210 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1211 CALLEE. DELTA is an integer constant that is to be added to the this
1212 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1214 void
1215 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1216 HOST_WIDE_INT delta)
1218 edge->indirect_unknown_callee = 0;
1219 edge->indirect_info->thunk_delta = delta;
1221 /* Get the edge out of the indirect edge list. */
1222 if (edge->prev_callee)
1223 edge->prev_callee->next_callee = edge->next_callee;
1224 if (edge->next_callee)
1225 edge->next_callee->prev_callee = edge->prev_callee;
1226 if (!edge->prev_callee)
1227 edge->caller->indirect_calls = edge->next_callee;
1229 /* Put it into the normal callee list */
1230 edge->prev_callee = NULL;
1231 edge->next_callee = edge->caller->callees;
1232 if (edge->caller->callees)
1233 edge->caller->callees->prev_callee = edge;
1234 edge->caller->callees = edge;
1236 /* Insert to callers list of the new callee. */
1237 cgraph_set_edge_callee (edge, callee);
1239 /* We need to re-determine the inlining status of the edge. */
1240 initialize_inline_failed (edge);
1244 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1245 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1246 of OLD_STMT if it was previously call statement. */
1248 static void
1249 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1250 gimple old_stmt, tree old_call, gimple new_stmt)
1252 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1254 /* We are seeing indirect calls, then there is nothing to update. */
1255 if (!new_call && !old_call)
1256 return;
1257 /* See if we turned indirect call into direct call or folded call to one builtin
1258 into different builtin. */
1259 if (old_call != new_call)
1261 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1262 struct cgraph_edge *ne = NULL;
1263 gcov_type count;
1264 int frequency;
1265 int loop_nest;
1267 if (e)
1269 /* See if the edge is already there and has the correct callee. It
1270 might be so because of indirect inlining has already updated
1271 it. We also might've cloned and redirected the edge. */
1272 if (new_call && e->callee)
1274 struct cgraph_node *callee = e->callee;
1275 while (callee)
1277 if (callee->decl == new_call
1278 || callee->former_clone_of == new_call)
1279 return;
1280 callee = callee->clone_of;
1284 /* Otherwise remove edge and create new one; we can't simply redirect
1285 since function has changed, so inline plan and other information
1286 attached to edge is invalid. */
1287 count = e->count;
1288 frequency = e->frequency;
1289 loop_nest = e->loop_nest;
1290 cgraph_remove_edge (e);
1292 else
1294 /* We are seeing new direct call; compute profile info based on BB. */
1295 basic_block bb = gimple_bb (new_stmt);
1296 count = bb->count;
1297 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1298 bb);
1299 loop_nest = bb->loop_depth;
1302 if (new_call)
1304 ne = cgraph_create_edge (node, cgraph_node (new_call),
1305 new_stmt, count, frequency,
1306 loop_nest);
1307 gcc_assert (ne->inline_failed);
1310 /* We only updated the call stmt; update pointer in cgraph edge.. */
1311 else if (old_stmt != new_stmt)
1312 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1315 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1316 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1317 of OLD_STMT before it was updated (updating can happen inplace). */
1319 void
1320 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1322 struct cgraph_node *orig = cgraph_node (cfun->decl);
1323 struct cgraph_node *node;
1325 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1326 if (orig->clones)
1327 for (node = orig->clones; node != orig;)
1329 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1330 if (node->clones)
1331 node = node->clones;
1332 else if (node->next_sibling_clone)
1333 node = node->next_sibling_clone;
1334 else
1336 while (node != orig && !node->next_sibling_clone)
1337 node = node->clone_of;
1338 if (node != orig)
1339 node = node->next_sibling_clone;
1345 /* Remove all callees from the node. */
1347 void
1348 cgraph_node_remove_callees (struct cgraph_node *node)
1350 struct cgraph_edge *e, *f;
1352 /* It is sufficient to remove the edges from the lists of callers of
1353 the callees. The callee list of the node can be zapped with one
1354 assignment. */
1355 for (e = node->callees; e; e = f)
1357 f = e->next_callee;
1358 cgraph_call_edge_removal_hooks (e);
1359 if (!e->indirect_unknown_callee)
1360 cgraph_edge_remove_callee (e);
1361 cgraph_free_edge (e);
1363 for (e = node->indirect_calls; e; e = f)
1365 f = e->next_callee;
1366 cgraph_call_edge_removal_hooks (e);
1367 if (!e->indirect_unknown_callee)
1368 cgraph_edge_remove_callee (e);
1369 cgraph_free_edge (e);
1371 node->indirect_calls = NULL;
1372 node->callees = NULL;
1373 if (node->call_site_hash)
1375 htab_delete (node->call_site_hash);
1376 node->call_site_hash = NULL;
1380 /* Remove all callers from the node. */
1382 static void
1383 cgraph_node_remove_callers (struct cgraph_node *node)
1385 struct cgraph_edge *e, *f;
1387 /* It is sufficient to remove the edges from the lists of callees of
1388 the callers. The caller list of the node can be zapped with one
1389 assignment. */
1390 for (e = node->callers; e; e = f)
1392 f = e->next_caller;
1393 cgraph_call_edge_removal_hooks (e);
1394 cgraph_edge_remove_caller (e);
1395 cgraph_free_edge (e);
1397 node->callers = NULL;
1400 /* Release memory used to represent body of function NODE. */
1402 void
1403 cgraph_release_function_body (struct cgraph_node *node)
1405 if (DECL_STRUCT_FUNCTION (node->decl))
1407 tree old_decl = current_function_decl;
1408 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1409 if (cfun->gimple_df)
1411 current_function_decl = node->decl;
1412 delete_tree_ssa ();
1413 delete_tree_cfg_annotations ();
1414 cfun->eh = NULL;
1415 current_function_decl = old_decl;
1417 if (cfun->cfg)
1419 gcc_assert (dom_computed[0] == DOM_NONE);
1420 gcc_assert (dom_computed[1] == DOM_NONE);
1421 clear_edges ();
1423 if (cfun->value_histograms)
1424 free_histograms ();
1425 gcc_assert (!current_loops);
1426 pop_cfun();
1427 gimple_set_body (node->decl, NULL);
1428 VEC_free (ipa_opt_pass, heap,
1429 node->ipa_transforms_to_apply);
1430 /* Struct function hangs a lot of data that would leak if we didn't
1431 removed all pointers to it. */
1432 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1433 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1435 DECL_SAVED_TREE (node->decl) = NULL;
1436 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1437 of its associated function function declaration because it's
1438 needed to emit debug info later. */
1439 if (!node->abstract_and_needed)
1440 DECL_INITIAL (node->decl) = error_mark_node;
1443 /* Remove same body alias node. */
1445 void
1446 cgraph_remove_same_body_alias (struct cgraph_node *node)
1448 void **slot;
1449 int uid = node->uid;
1451 gcc_assert (node->same_body_alias);
1452 if (node->previous)
1453 node->previous->next = node->next;
1454 else
1455 node->same_body->same_body = node->next;
1456 if (node->next)
1457 node->next->previous = node->previous;
1458 node->next = NULL;
1459 node->previous = NULL;
1460 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1461 if (*slot == node)
1462 htab_clear_slot (cgraph_hash, slot);
1463 if (assembler_name_hash)
1465 tree name = DECL_ASSEMBLER_NAME (node->decl);
1466 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1467 decl_assembler_name_hash (name),
1468 NO_INSERT);
1469 if (slot && *slot == node)
1470 htab_clear_slot (assembler_name_hash, slot);
1473 /* Clear out the node to NULL all pointers and add the node to the free
1474 list. */
1475 memset (node, 0, sizeof(*node));
1476 node->uid = uid;
1477 NEXT_FREE_NODE (node) = free_nodes;
1478 free_nodes = node;
1481 /* Remove the node from cgraph. */
1483 void
1484 cgraph_remove_node (struct cgraph_node *node)
1486 void **slot;
1487 bool kill_body = false;
1488 struct cgraph_node *n;
1489 int uid = node->uid;
1491 cgraph_call_node_removal_hooks (node);
1492 cgraph_node_remove_callers (node);
1493 cgraph_node_remove_callees (node);
1494 ipa_remove_all_references (&node->ref_list);
1495 ipa_remove_all_refering (&node->ref_list);
1496 VEC_free (ipa_opt_pass, heap,
1497 node->ipa_transforms_to_apply);
1499 /* Incremental inlining access removed nodes stored in the postorder list.
1501 node->needed = node->reachable = false;
1502 for (n = node->nested; n; n = n->next_nested)
1503 n->origin = NULL;
1504 node->nested = NULL;
1505 if (node->origin)
1507 struct cgraph_node **node2 = &node->origin->nested;
1509 while (*node2 != node)
1510 node2 = &(*node2)->next_nested;
1511 *node2 = node->next_nested;
1513 if (node->previous)
1514 node->previous->next = node->next;
1515 else
1516 cgraph_nodes = node->next;
1517 if (node->next)
1518 node->next->previous = node->previous;
1519 node->next = NULL;
1520 node->previous = NULL;
1521 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1522 if (*slot == node)
1524 struct cgraph_node *next_inline_clone;
1526 for (next_inline_clone = node->clones;
1527 next_inline_clone && next_inline_clone->decl != node->decl;
1528 next_inline_clone = next_inline_clone->next_sibling_clone)
1531 /* If there is inline clone of the node being removed, we need
1532 to put it into the position of removed node and reorganize all
1533 other clones to be based on it. */
1534 if (next_inline_clone)
1536 struct cgraph_node *n;
1537 struct cgraph_node *new_clones;
1539 *slot = next_inline_clone;
1541 /* Unlink inline clone from the list of clones of removed node. */
1542 if (next_inline_clone->next_sibling_clone)
1543 next_inline_clone->next_sibling_clone->prev_sibling_clone
1544 = next_inline_clone->prev_sibling_clone;
1545 if (next_inline_clone->prev_sibling_clone)
1547 gcc_assert (node->clones != next_inline_clone);
1548 next_inline_clone->prev_sibling_clone->next_sibling_clone
1549 = next_inline_clone->next_sibling_clone;
1551 else
1553 gcc_assert (node->clones == next_inline_clone);
1554 node->clones = next_inline_clone->next_sibling_clone;
1557 new_clones = node->clones;
1558 node->clones = NULL;
1560 /* Copy clone info. */
1561 next_inline_clone->clone = node->clone;
1563 /* Now place it into clone tree at same level at NODE. */
1564 next_inline_clone->clone_of = node->clone_of;
1565 next_inline_clone->prev_sibling_clone = NULL;
1566 next_inline_clone->next_sibling_clone = NULL;
1567 if (node->clone_of)
1569 if (node->clone_of->clones)
1570 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1571 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1572 node->clone_of->clones = next_inline_clone;
1575 /* Merge the clone list. */
1576 if (new_clones)
1578 if (!next_inline_clone->clones)
1579 next_inline_clone->clones = new_clones;
1580 else
1582 n = next_inline_clone->clones;
1583 while (n->next_sibling_clone)
1584 n = n->next_sibling_clone;
1585 n->next_sibling_clone = new_clones;
1586 new_clones->prev_sibling_clone = n;
1590 /* Update clone_of pointers. */
1591 n = new_clones;
1592 while (n)
1594 n->clone_of = next_inline_clone;
1595 n = n->next_sibling_clone;
1598 else
1600 htab_clear_slot (cgraph_hash, slot);
1601 kill_body = true;
1605 if (node->prev_sibling_clone)
1606 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1607 else if (node->clone_of)
1608 node->clone_of->clones = node->next_sibling_clone;
1609 if (node->next_sibling_clone)
1610 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1611 if (node->clones)
1613 struct cgraph_node *n, *next;
1615 if (node->clone_of)
1617 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1618 n->clone_of = node->clone_of;
1619 n->clone_of = node->clone_of;
1620 n->next_sibling_clone = node->clone_of->clones;
1621 if (node->clone_of->clones)
1622 node->clone_of->clones->prev_sibling_clone = n;
1623 node->clone_of->clones = node->clones;
1625 else
1627 /* We are removing node with clones. this makes clones inconsistent,
1628 but assume they will be removed subsequently and just keep clone
1629 tree intact. This can happen in unreachable function removal since
1630 we remove unreachable functions in random order, not by bottom-up
1631 walk of clone trees. */
1632 for (n = node->clones; n; n = next)
1634 next = n->next_sibling_clone;
1635 n->next_sibling_clone = NULL;
1636 n->prev_sibling_clone = NULL;
1637 n->clone_of = NULL;
1642 while (node->same_body)
1643 cgraph_remove_same_body_alias (node->same_body);
1645 if (node->same_comdat_group)
1647 struct cgraph_node *prev;
1648 for (prev = node->same_comdat_group;
1649 prev->same_comdat_group != node;
1650 prev = prev->same_comdat_group)
1652 if (node->same_comdat_group == prev)
1653 prev->same_comdat_group = NULL;
1654 else
1655 prev->same_comdat_group = node->same_comdat_group;
1656 node->same_comdat_group = NULL;
1659 /* While all the clones are removed after being proceeded, the function
1660 itself is kept in the cgraph even after it is compiled. Check whether
1661 we are done with this body and reclaim it proactively if this is the case.
1663 if (!kill_body && *slot)
1665 struct cgraph_node *n = (struct cgraph_node *) *slot;
1666 if (!n->clones && !n->clone_of && !n->global.inlined_to
1667 && (cgraph_global_info_ready
1668 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1669 || n->in_other_partition)))
1670 kill_body = true;
1672 if (assembler_name_hash)
1674 tree name = DECL_ASSEMBLER_NAME (node->decl);
1675 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1676 decl_assembler_name_hash (name),
1677 NO_INSERT);
1678 /* Inline clones are not hashed. */
1679 if (slot && *slot == node)
1680 htab_clear_slot (assembler_name_hash, slot);
1683 if (kill_body)
1684 cgraph_release_function_body (node);
1685 node->decl = NULL;
1686 if (node->call_site_hash)
1688 htab_delete (node->call_site_hash);
1689 node->call_site_hash = NULL;
1691 cgraph_n_nodes--;
1693 /* Clear out the node to NULL all pointers and add the node to the free
1694 list. */
1695 memset (node, 0, sizeof(*node));
1696 node->uid = uid;
1697 NEXT_FREE_NODE (node) = free_nodes;
1698 free_nodes = node;
1701 /* Remove the node from cgraph. */
1703 void
1704 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1706 struct cgraph_edge *e, *next;
1707 for (e = node->callees; e; e = next)
1709 next = e->next_callee;
1710 if (!e->inline_failed)
1711 cgraph_remove_node_and_inline_clones (e->callee);
1713 cgraph_remove_node (node);
1716 /* Notify finalize_compilation_unit that given node is reachable. */
1718 void
1719 cgraph_mark_reachable_node (struct cgraph_node *node)
1721 if (!node->reachable && node->local.finalized)
1723 if (cgraph_global_info_ready)
1725 /* Verify that function does not appear to be needed out of blue
1726 during the optimization process. This can happen for extern
1727 inlines when bodies was removed after inlining. */
1728 gcc_assert ((node->analyzed || node->in_other_partition
1729 || DECL_EXTERNAL (node->decl)));
1731 else
1732 notice_global_symbol (node->decl);
1733 node->reachable = 1;
1735 node->next_needed = cgraph_nodes_queue;
1736 cgraph_nodes_queue = node;
1740 /* Likewise indicate that a node is needed, i.e. reachable via some
1741 external means. */
1743 void
1744 cgraph_mark_needed_node (struct cgraph_node *node)
1746 node->needed = 1;
1747 gcc_assert (!node->global.inlined_to);
1748 cgraph_mark_reachable_node (node);
1751 /* Likewise indicate that a node is having address taken. */
1753 void
1754 cgraph_mark_address_taken_node (struct cgraph_node *node)
1756 gcc_assert (!node->global.inlined_to);
1757 cgraph_mark_reachable_node (node);
1758 node->address_taken = 1;
1761 /* Return local info for the compiled function. */
1763 struct cgraph_local_info *
1764 cgraph_local_info (tree decl)
1766 struct cgraph_node *node;
1768 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1769 node = cgraph_node (decl);
1770 return &node->local;
1773 /* Return local info for the compiled function. */
1775 struct cgraph_global_info *
1776 cgraph_global_info (tree decl)
1778 struct cgraph_node *node;
1780 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1781 node = cgraph_node (decl);
1782 return &node->global;
1785 /* Return local info for the compiled function. */
1787 struct cgraph_rtl_info *
1788 cgraph_rtl_info (tree decl)
1790 struct cgraph_node *node;
1792 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1793 node = cgraph_node (decl);
1794 if (decl != current_function_decl
1795 && !TREE_ASM_WRITTEN (node->decl))
1796 return NULL;
1797 return &node->rtl;
1800 /* Return a string describing the failure REASON. */
1802 const char*
1803 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1805 #undef DEFCIFCODE
1806 #define DEFCIFCODE(code, string) string,
1808 static const char *cif_string_table[CIF_N_REASONS] = {
1809 #include "cif-code.def"
1812 /* Signedness of an enum type is implementation defined, so cast it
1813 to unsigned before testing. */
1814 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1815 return cif_string_table[reason];
1818 /* Return name of the node used in debug output. */
1819 const char *
1820 cgraph_node_name (struct cgraph_node *node)
1822 return lang_hooks.decl_printable_name (node->decl, 2);
1825 /* Names used to print out the availability enum. */
1826 const char * const cgraph_availability_names[] =
1827 {"unset", "not_available", "overwritable", "available", "local"};
1830 /* Dump call graph node NODE to file F. */
1832 void
1833 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1835 struct cgraph_edge *edge;
1836 int indirect_calls_count = 0;
1838 fprintf (f, "%s/%i(%i)", cgraph_node_name (node), node->uid,
1839 node->pid);
1840 dump_addr (f, " @", (void *)node);
1841 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1842 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1843 if (node->global.inlined_to)
1844 fprintf (f, " (inline copy in %s/%i)",
1845 cgraph_node_name (node->global.inlined_to),
1846 node->global.inlined_to->uid);
1847 if (node->same_comdat_group)
1848 fprintf (f, " (same comdat group as %s/%i)",
1849 cgraph_node_name (node->same_comdat_group),
1850 node->same_comdat_group->uid);
1851 if (node->clone_of)
1852 fprintf (f, " (clone of %s/%i)",
1853 cgraph_node_name (node->clone_of),
1854 node->clone_of->uid);
1855 if (cgraph_function_flags_ready)
1856 fprintf (f, " availability:%s",
1857 cgraph_availability_names [cgraph_function_body_availability (node)]);
1858 if (node->analyzed)
1859 fprintf (f, " analyzed");
1860 if (node->in_other_partition)
1861 fprintf (f, " in_other_partition");
1862 if (node->count)
1863 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1864 (HOST_WIDEST_INT)node->count);
1865 if (node->local.inline_summary.self_time)
1866 fprintf (f, " %i time, %i benefit", node->local.inline_summary.self_time,
1867 node->local.inline_summary.time_inlining_benefit);
1868 if (node->global.time && node->global.time
1869 != node->local.inline_summary.self_time)
1870 fprintf (f, " (%i after inlining)", node->global.time);
1871 if (node->local.inline_summary.self_size)
1872 fprintf (f, " %i size, %i benefit", node->local.inline_summary.self_size,
1873 node->local.inline_summary.size_inlining_benefit);
1874 if (node->global.size && node->global.size
1875 != node->local.inline_summary.self_size)
1876 fprintf (f, " (%i after inlining)", node->global.size);
1877 if (node->local.inline_summary.estimated_self_stack_size)
1878 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1879 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
1880 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
1881 if (node->origin)
1882 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1883 if (node->needed)
1884 fprintf (f, " needed");
1885 if (node->address_taken)
1886 fprintf (f, " address_taken");
1887 else if (node->reachable)
1888 fprintf (f, " reachable");
1889 else if (node->reachable_from_other_partition)
1890 fprintf (f, " reachable_from_other_partition");
1891 if (gimple_has_body_p (node->decl))
1892 fprintf (f, " body");
1893 if (node->process)
1894 fprintf (f, " process");
1895 if (node->local.local)
1896 fprintf (f, " local");
1897 if (node->local.externally_visible)
1898 fprintf (f, " externally_visible");
1899 if (node->resolution != LDPR_UNKNOWN)
1900 fprintf (f, " %s",
1901 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1902 if (node->local.finalized)
1903 fprintf (f, " finalized");
1904 if (node->local.disregard_inline_limits)
1905 fprintf (f, " always_inline");
1906 else if (node->local.inlinable)
1907 fprintf (f, " inlinable");
1908 else if (node->local.versionable)
1909 fprintf (f, " versionable");
1910 if (node->local.redefined_extern_inline)
1911 fprintf (f, " redefined_extern_inline");
1912 if (TREE_ASM_WRITTEN (node->decl))
1913 fprintf (f, " asm_written");
1914 if (node->only_called_at_startup)
1915 fprintf (f, " only_called_at_startup");
1916 if (node->only_called_at_exit)
1917 fprintf (f, " only_called_at_exit");
1919 fprintf (f, "\n called by: ");
1920 for (edge = node->callers; edge; edge = edge->next_caller)
1922 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1923 edge->caller->uid);
1924 if (edge->count)
1925 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1926 (HOST_WIDEST_INT)edge->count);
1927 if (edge->frequency)
1928 fprintf (f, "(%.2f per call) ",
1929 edge->frequency / (double)CGRAPH_FREQ_BASE);
1930 if (!edge->inline_failed)
1931 fprintf(f, "(inlined) ");
1932 if (edge->indirect_inlining_edge)
1933 fprintf(f, "(indirect_inlining) ");
1934 if (edge->can_throw_external)
1935 fprintf(f, "(can throw external) ");
1938 fprintf (f, "\n calls: ");
1939 for (edge = node->callees; edge; edge = edge->next_callee)
1941 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1942 edge->callee->uid);
1943 if (!edge->inline_failed)
1944 fprintf(f, "(inlined) ");
1945 if (edge->indirect_inlining_edge)
1946 fprintf(f, "(indirect_inlining) ");
1947 if (edge->count)
1948 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1949 (HOST_WIDEST_INT)edge->count);
1950 if (edge->frequency)
1951 fprintf (f, "(%.2f per call) ",
1952 edge->frequency / (double)CGRAPH_FREQ_BASE);
1953 if (edge->loop_nest)
1954 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1955 if (edge->can_throw_external)
1956 fprintf(f, "(can throw external) ");
1958 fprintf (f, "\n");
1959 fprintf (f, " References: ");
1960 ipa_dump_references (f, &node->ref_list);
1961 fprintf (f, " Refering this function: ");
1962 ipa_dump_refering (f, &node->ref_list);
1964 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1965 indirect_calls_count++;
1966 if (indirect_calls_count)
1967 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1968 indirect_calls_count);
1970 if (node->same_body)
1972 struct cgraph_node *n;
1973 fprintf (f, " aliases & thunks:");
1974 for (n = node->same_body; n; n = n->next)
1976 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1977 if (n->thunk.thunk_p)
1979 fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1980 "virtual offset %i",
1981 lang_hooks.decl_printable_name (n->thunk.alias, 2),
1982 (int)n->thunk.fixed_offset,
1983 (int)n->thunk.virtual_value,
1984 (int)n->thunk.virtual_offset_p);
1985 fprintf (f, ")");
1987 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1988 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1990 fprintf (f, "\n");
1995 /* Dump call graph node NODE to stderr. */
1997 DEBUG_FUNCTION void
1998 debug_cgraph_node (struct cgraph_node *node)
2000 dump_cgraph_node (stderr, node);
2004 /* Dump the callgraph to file F. */
2006 void
2007 dump_cgraph (FILE *f)
2009 struct cgraph_node *node;
2011 fprintf (f, "callgraph:\n\n");
2012 for (node = cgraph_nodes; node; node = node->next)
2013 dump_cgraph_node (f, node);
2017 /* Dump the call graph to stderr. */
2019 DEBUG_FUNCTION void
2020 debug_cgraph (void)
2022 dump_cgraph (stderr);
2026 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
2028 void
2029 change_decl_assembler_name (tree decl, tree name)
2031 struct cgraph_node *node;
2032 void **slot;
2033 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
2034 SET_DECL_ASSEMBLER_NAME (decl, name);
2035 else
2037 if (name == DECL_ASSEMBLER_NAME (decl))
2038 return;
2040 if (assembler_name_hash
2041 && TREE_CODE (decl) == FUNCTION_DECL
2042 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2044 tree old_name = DECL_ASSEMBLER_NAME (decl);
2045 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2046 decl_assembler_name_hash (old_name),
2047 NO_INSERT);
2048 /* Inline clones are not hashed. */
2049 if (slot && *slot == node)
2050 htab_clear_slot (assembler_name_hash, slot);
2052 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2053 && DECL_RTL_SET_P (decl))
2054 warning (0, "%D renamed after being referenced in assembly", decl);
2056 SET_DECL_ASSEMBLER_NAME (decl, name);
2058 if (assembler_name_hash
2059 && TREE_CODE (decl) == FUNCTION_DECL
2060 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2062 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2063 decl_assembler_name_hash (name),
2064 INSERT);
2065 gcc_assert (!*slot);
2066 *slot = node;
2070 /* Add a top-level asm statement to the list. */
2072 struct cgraph_asm_node *
2073 cgraph_add_asm_node (tree asm_str)
2075 struct cgraph_asm_node *node;
2077 node = ggc_alloc_cleared_cgraph_asm_node ();
2078 node->asm_str = asm_str;
2079 node->order = cgraph_order++;
2080 node->next = NULL;
2081 if (cgraph_asm_nodes == NULL)
2082 cgraph_asm_nodes = node;
2083 else
2084 cgraph_asm_last_node->next = node;
2085 cgraph_asm_last_node = node;
2086 return node;
2089 /* Return true when the DECL can possibly be inlined. */
2090 bool
2091 cgraph_function_possibly_inlined_p (tree decl)
2093 if (!cgraph_global_info_ready)
2094 return !DECL_UNINLINABLE (decl);
2095 return DECL_POSSIBLY_INLINED (decl);
2098 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2099 struct cgraph_edge *
2100 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2101 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2102 int freq_scale, int loop_nest, bool update_original)
2104 struct cgraph_edge *new_edge;
2105 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2106 gcov_type freq;
2108 /* We do not want to ignore loop nest after frequency drops to 0. */
2109 if (!freq_scale)
2110 freq_scale = 1;
2111 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2112 if (freq > CGRAPH_FREQ_MAX)
2113 freq = CGRAPH_FREQ_MAX;
2115 if (e->indirect_unknown_callee)
2117 tree decl;
2119 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2121 struct cgraph_node *callee = cgraph_node (decl);
2122 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq,
2123 e->loop_nest + loop_nest);
2125 else
2127 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2128 e->indirect_info->ecf_flags,
2129 count, freq,
2130 e->loop_nest + loop_nest);
2131 *new_edge->indirect_info = *e->indirect_info;
2134 else
2136 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
2137 e->loop_nest + loop_nest);
2138 if (e->indirect_info)
2140 new_edge->indirect_info
2141 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2142 *new_edge->indirect_info = *e->indirect_info;
2146 new_edge->call_stmt_size = e->call_stmt_size;
2147 new_edge->call_stmt_time = e->call_stmt_time;
2148 new_edge->inline_failed = e->inline_failed;
2149 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2150 new_edge->lto_stmt_uid = stmt_uid;
2151 /* Clone flags that depend on call_stmt availability manually. */
2152 new_edge->can_throw_external = e->can_throw_external;
2153 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2154 if (update_original)
2156 e->count -= new_edge->count;
2157 if (e->count < 0)
2158 e->count = 0;
2160 cgraph_call_edge_duplication_hooks (e, new_edge);
2161 return new_edge;
2164 /* Create node representing clone of N executed COUNT times. Decrease
2165 the execution counts from original node too.
2166 The new clone will have decl set to DECL that may or may not be the same
2167 as decl of N.
2169 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2170 function's profile to reflect the fact that part of execution is handled
2171 by node. */
2172 struct cgraph_node *
2173 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2174 int loop_nest, bool update_original,
2175 VEC(cgraph_edge_p,heap) *redirect_callers)
2177 struct cgraph_node *new_node = cgraph_create_node ();
2178 struct cgraph_edge *e;
2179 gcov_type count_scale;
2180 unsigned i;
2182 new_node->decl = decl;
2183 new_node->origin = n->origin;
2184 if (new_node->origin)
2186 new_node->next_nested = new_node->origin->nested;
2187 new_node->origin->nested = new_node;
2189 new_node->analyzed = n->analyzed;
2190 new_node->local = n->local;
2191 new_node->local.externally_visible = false;
2192 new_node->local.local = true;
2193 new_node->local.vtable_method = false;
2194 new_node->global = n->global;
2195 new_node->rtl = n->rtl;
2196 new_node->count = count;
2197 new_node->frequency = n->frequency;
2198 new_node->clone = n->clone;
2199 new_node->clone.tree_map = 0;
2200 if (n->count)
2202 if (new_node->count > n->count)
2203 count_scale = REG_BR_PROB_BASE;
2204 else
2205 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2207 else
2208 count_scale = 0;
2209 if (update_original)
2211 n->count -= count;
2212 if (n->count < 0)
2213 n->count = 0;
2216 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2218 /* Redirect calls to the old version node to point to its new
2219 version. */
2220 cgraph_redirect_edge_callee (e, new_node);
2224 for (e = n->callees;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);
2228 for (e = n->indirect_calls; e; e = e->next_callee)
2229 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2230 count_scale, freq, loop_nest, update_original);
2231 ipa_clone_references (new_node, NULL, &n->ref_list);
2233 new_node->next_sibling_clone = n->clones;
2234 if (n->clones)
2235 n->clones->prev_sibling_clone = new_node;
2236 n->clones = new_node;
2237 new_node->clone_of = n;
2239 cgraph_call_node_duplication_hooks (n, new_node);
2240 if (n->decl != decl)
2242 struct cgraph_node **slot;
2243 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2244 gcc_assert (!*slot);
2245 *slot = new_node;
2246 if (assembler_name_hash)
2248 void **aslot;
2249 tree name = DECL_ASSEMBLER_NAME (decl);
2251 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2252 decl_assembler_name_hash (name),
2253 INSERT);
2254 gcc_assert (!*aslot);
2255 *aslot = new_node;
2258 return new_node;
2261 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2263 static GTY(()) unsigned int clone_fn_id_num;
2265 tree
2266 clone_function_name (tree decl, const char *suffix)
2268 tree name = DECL_ASSEMBLER_NAME (decl);
2269 size_t len = IDENTIFIER_LENGTH (name);
2270 char *tmp_name, *prefix;
2272 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2273 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2274 strcpy (prefix + len + 1, suffix);
2275 #ifndef NO_DOT_IN_LABEL
2276 prefix[len] = '.';
2277 #elif !defined NO_DOLLAR_IN_LABEL
2278 prefix[len] = '$';
2279 #else
2280 prefix[len] = '_';
2281 #endif
2282 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2283 return get_identifier (tmp_name);
2286 /* Create callgraph node clone with new declaration. The actual body will
2287 be copied later at compilation stage.
2289 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2290 bitmap interface.
2292 struct cgraph_node *
2293 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2294 VEC(cgraph_edge_p,heap) *redirect_callers,
2295 VEC(ipa_replace_map_p,gc) *tree_map,
2296 bitmap args_to_skip,
2297 const char * suffix)
2299 tree old_decl = old_node->decl;
2300 struct cgraph_node *new_node = NULL;
2301 tree new_decl;
2302 size_t i;
2303 struct ipa_replace_map *map;
2305 if (!flag_wpa)
2306 gcc_checking_assert (tree_versionable_function_p (old_decl));
2308 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2310 /* Make a new FUNCTION_DECL tree node */
2311 if (!args_to_skip)
2312 new_decl = copy_node (old_decl);
2313 else
2314 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2315 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2317 /* Generate a new name for the new version. */
2318 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2319 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2320 SET_DECL_RTL (new_decl, NULL);
2322 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2323 CGRAPH_FREQ_BASE, 0, false,
2324 redirect_callers);
2325 /* Update the properties.
2326 Make clone visible only within this translation unit. Make sure
2327 that is not weak also.
2328 ??? We cannot use COMDAT linkage because there is no
2329 ABI support for this. */
2330 DECL_EXTERNAL (new_node->decl) = 0;
2331 if (DECL_ONE_ONLY (old_decl))
2332 DECL_SECTION_NAME (new_node->decl) = NULL;
2333 DECL_COMDAT_GROUP (new_node->decl) = 0;
2334 TREE_PUBLIC (new_node->decl) = 0;
2335 DECL_COMDAT (new_node->decl) = 0;
2336 DECL_WEAK (new_node->decl) = 0;
2337 new_node->clone.tree_map = tree_map;
2338 new_node->clone.args_to_skip = args_to_skip;
2339 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2341 tree var = map->new_tree;
2343 STRIP_NOPS (var);
2344 if (TREE_CODE (var) != ADDR_EXPR)
2345 continue;
2346 var = get_base_var (var);
2347 if (!var)
2348 continue;
2350 /* Record references of the future statement initializing the constant
2351 argument. */
2352 if (TREE_CODE (var) == FUNCTION_DECL)
2353 ipa_record_reference (new_node, NULL, cgraph_node (var),
2354 NULL, IPA_REF_ADDR, NULL);
2355 else if (TREE_CODE (var) == VAR_DECL)
2356 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2357 IPA_REF_ADDR, NULL);
2359 if (!args_to_skip)
2360 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2361 else if (old_node->clone.combined_args_to_skip)
2363 int newi = 0, oldi = 0;
2364 tree arg;
2365 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2366 struct cgraph_node *orig_node;
2367 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2369 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2371 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2373 bitmap_set_bit (new_args_to_skip, oldi);
2374 continue;
2376 if (bitmap_bit_p (args_to_skip, newi))
2377 bitmap_set_bit (new_args_to_skip, oldi);
2378 newi++;
2380 new_node->clone.combined_args_to_skip = new_args_to_skip;
2382 else
2383 new_node->clone.combined_args_to_skip = args_to_skip;
2384 new_node->local.externally_visible = 0;
2385 new_node->local.local = 1;
2386 new_node->lowered = true;
2387 new_node->reachable = true;
2390 return new_node;
2393 /* NODE is no longer nested function; update cgraph accordingly. */
2394 void
2395 cgraph_unnest_node (struct cgraph_node *node)
2397 struct cgraph_node **node2 = &node->origin->nested;
2398 gcc_assert (node->origin);
2400 while (*node2 != node)
2401 node2 = &(*node2)->next_nested;
2402 *node2 = node->next_nested;
2403 node->origin = NULL;
2406 /* Return function availability. See cgraph.h for description of individual
2407 return values. */
2408 enum availability
2409 cgraph_function_body_availability (struct cgraph_node *node)
2411 enum availability avail;
2412 gcc_assert (cgraph_function_flags_ready);
2413 if (!node->analyzed)
2414 avail = AVAIL_NOT_AVAILABLE;
2415 else if (node->local.local)
2416 avail = AVAIL_LOCAL;
2417 else if (!node->local.externally_visible)
2418 avail = AVAIL_AVAILABLE;
2419 /* Inline functions are safe to be analyzed even if their symbol can
2420 be overwritten at runtime. It is not meaningful to enforce any sane
2421 behaviour on replacing inline function by different body. */
2422 else if (DECL_DECLARED_INLINE_P (node->decl))
2423 avail = AVAIL_AVAILABLE;
2425 /* If the function can be overwritten, return OVERWRITABLE. Take
2426 care at least of two notable extensions - the COMDAT functions
2427 used to share template instantiations in C++ (this is symmetric
2428 to code cp_cannot_inline_tree_fn and probably shall be shared and
2429 the inlinability hooks completely eliminated).
2431 ??? Does the C++ one definition rule allow us to always return
2432 AVAIL_AVAILABLE here? That would be good reason to preserve this
2433 bit. */
2435 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2436 avail = AVAIL_OVERWRITABLE;
2437 else avail = AVAIL_AVAILABLE;
2439 return avail;
2442 /* Add the function FNDECL to the call graph.
2443 Unlike cgraph_finalize_function, this function is intended to be used
2444 by middle end and allows insertion of new function at arbitrary point
2445 of compilation. The function can be either in high, low or SSA form
2446 GIMPLE.
2448 The function is assumed to be reachable and have address taken (so no
2449 API breaking optimizations are performed on it).
2451 Main work done by this function is to enqueue the function for later
2452 processing to avoid need the passes to be re-entrant. */
2454 void
2455 cgraph_add_new_function (tree fndecl, bool lowered)
2457 struct cgraph_node *node;
2458 switch (cgraph_state)
2460 case CGRAPH_STATE_CONSTRUCTION:
2461 /* Just enqueue function to be processed at nearest occurrence. */
2462 node = cgraph_node (fndecl);
2463 node->next_needed = cgraph_new_nodes;
2464 if (lowered)
2465 node->lowered = true;
2466 cgraph_new_nodes = node;
2467 break;
2469 case CGRAPH_STATE_IPA:
2470 case CGRAPH_STATE_IPA_SSA:
2471 case CGRAPH_STATE_EXPANSION:
2472 /* Bring the function into finalized state and enqueue for later
2473 analyzing and compilation. */
2474 node = cgraph_node (fndecl);
2475 node->local.local = false;
2476 node->local.finalized = true;
2477 node->reachable = node->needed = true;
2478 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2480 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2481 current_function_decl = fndecl;
2482 gimple_register_cfg_hooks ();
2483 tree_lowering_passes (fndecl);
2484 bitmap_obstack_initialize (NULL);
2485 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2486 execute_pass_list (pass_early_local_passes.pass.sub);
2487 bitmap_obstack_release (NULL);
2488 pop_cfun ();
2489 current_function_decl = NULL;
2491 lowered = true;
2493 if (lowered)
2494 node->lowered = true;
2495 node->next_needed = cgraph_new_nodes;
2496 cgraph_new_nodes = node;
2497 break;
2499 case CGRAPH_STATE_FINISHED:
2500 /* At the very end of compilation we have to do all the work up
2501 to expansion. */
2502 node = cgraph_node (fndecl);
2503 if (lowered)
2504 node->lowered = true;
2505 cgraph_analyze_function (node);
2506 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2507 current_function_decl = fndecl;
2508 gimple_register_cfg_hooks ();
2509 bitmap_obstack_initialize (NULL);
2510 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2511 execute_pass_list (pass_early_local_passes.pass.sub);
2512 bitmap_obstack_release (NULL);
2513 tree_rest_of_compilation (fndecl);
2514 pop_cfun ();
2515 current_function_decl = NULL;
2516 break;
2519 /* Set a personality if required and we already passed EH lowering. */
2520 if (lowered
2521 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2522 == eh_personality_lang))
2523 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2526 /* Return true if NODE can be made local for API change.
2527 Extern inline functions and C++ COMDAT functions can be made local
2528 at the expense of possible code size growth if function is used in multiple
2529 compilation units. */
2530 bool
2531 cgraph_node_can_be_local_p (struct cgraph_node *node)
2533 return (!node->needed && !node->address_taken
2534 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2535 || !node->local.externally_visible));
2538 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2539 but other code such as notice_global_symbol generates rtl. */
2540 void
2541 cgraph_make_decl_local (tree decl)
2543 rtx rtl, symbol;
2545 if (TREE_CODE (decl) == VAR_DECL)
2546 DECL_COMMON (decl) = 0;
2547 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2549 if (DECL_COMDAT (decl))
2551 /* It is possible that we are linking against library defining same COMDAT
2552 function. To avoid conflict we need to rename our local name of the
2553 function just in the case WHOPR partitioning decide to make it hidden
2554 to avoid cross partition references. */
2555 if (flag_wpa)
2557 const char *old_name;
2559 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2560 if (TREE_CODE (decl) == FUNCTION_DECL)
2562 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2563 change_decl_assembler_name (decl,
2564 clone_function_name (decl, "local"));
2565 if (node->local.lto_file_data)
2566 lto_record_renamed_decl (node->local.lto_file_data,
2567 old_name,
2568 IDENTIFIER_POINTER
2569 (DECL_ASSEMBLER_NAME (decl)));
2571 else if (TREE_CODE (decl) == VAR_DECL)
2573 struct varpool_node *vnode = varpool_get_node (decl);
2574 /* change_decl_assembler_name will warn here on vtables because
2575 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2576 SET_DECL_ASSEMBLER_NAME (decl,
2577 clone_function_name (decl, "local"));
2578 if (vnode->lto_file_data)
2579 lto_record_renamed_decl (vnode->lto_file_data,
2580 old_name,
2581 IDENTIFIER_POINTER
2582 (DECL_ASSEMBLER_NAME (decl)));
2585 DECL_SECTION_NAME (decl) = 0;
2586 DECL_COMDAT (decl) = 0;
2588 DECL_COMDAT_GROUP (decl) = 0;
2589 DECL_WEAK (decl) = 0;
2590 DECL_EXTERNAL (decl) = 0;
2591 TREE_PUBLIC (decl) = 0;
2592 if (!DECL_RTL_SET_P (decl))
2593 return;
2595 /* Update rtl flags. */
2596 make_decl_rtl (decl);
2598 rtl = DECL_RTL (decl);
2599 if (!MEM_P (rtl))
2600 return;
2602 symbol = XEXP (rtl, 0);
2603 if (GET_CODE (symbol) != SYMBOL_REF)
2604 return;
2606 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2609 /* Bring NODE local. */
2610 void
2611 cgraph_make_node_local (struct cgraph_node *node)
2613 gcc_assert (cgraph_node_can_be_local_p (node));
2614 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2616 struct cgraph_node *alias;
2617 cgraph_make_decl_local (node->decl);
2619 for (alias = node->same_body; alias; alias = alias->next)
2620 cgraph_make_decl_local (alias->decl);
2622 node->local.externally_visible = false;
2623 node->local.local = true;
2624 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2625 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2629 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2630 if any to NOTHROW. */
2632 void
2633 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2635 struct cgraph_node *alias;
2636 TREE_NOTHROW (node->decl) = nothrow;
2637 for (alias = node->same_body; alias; alias = alias->next)
2638 TREE_NOTHROW (alias->decl) = nothrow;
2641 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2642 if any to READONLY. */
2644 void
2645 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2647 struct cgraph_node *alias;
2648 /* Static constructors and destructors without a side effect can be
2649 optimized out. */
2650 if (!looping && readonly)
2652 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2653 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2654 if (DECL_STATIC_DESTRUCTOR (node->decl))
2655 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2657 TREE_READONLY (node->decl) = readonly;
2658 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2659 for (alias = node->same_body; alias; alias = alias->next)
2661 TREE_READONLY (alias->decl) = readonly;
2662 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2666 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2667 if any to PURE. */
2669 void
2670 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2672 struct cgraph_node *alias;
2673 /* Static constructors and destructors without a side effect can be
2674 optimized out. */
2675 if (!looping && pure)
2677 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2678 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2679 if (DECL_STATIC_DESTRUCTOR (node->decl))
2680 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2682 DECL_PURE_P (node->decl) = pure;
2683 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2684 for (alias = node->same_body; alias; alias = alias->next)
2686 DECL_PURE_P (alias->decl) = pure;
2687 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2691 /* See if the frequency of NODE can be updated based on frequencies of its
2692 callers. */
2693 bool
2694 cgraph_propagate_frequency (struct cgraph_node *node)
2696 bool maybe_unlikely_executed = true, maybe_executed_once = true;
2697 bool only_called_at_startup = true;
2698 bool only_called_at_exit = true;
2699 bool changed = false;
2700 struct cgraph_edge *edge;
2702 if (!node->local.local)
2703 return false;
2704 gcc_assert (node->analyzed);
2705 if (dump_file && (dump_flags & TDF_DETAILS))
2706 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2708 for (edge = node->callers;
2709 edge && (maybe_unlikely_executed || maybe_executed_once
2710 || only_called_at_startup || only_called_at_exit);
2711 edge = edge->next_caller)
2713 if (edge->caller != node)
2715 only_called_at_startup &= edge->caller->only_called_at_startup;
2716 /* It makes sense to put main() together with the static constructors.
2717 It will be executed for sure, but rest of functions called from
2718 main are definitely not at startup only. */
2719 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2720 only_called_at_startup = 0;
2721 only_called_at_exit &= edge->caller->only_called_at_exit;
2723 if (!edge->frequency)
2724 continue;
2725 switch (edge->caller->frequency)
2727 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2728 break;
2729 case NODE_FREQUENCY_EXECUTED_ONCE:
2730 if (dump_file && (dump_flags & TDF_DETAILS))
2731 fprintf (dump_file, " Called by %s that is executed once\n",
2732 cgraph_node_name (edge->caller));
2733 maybe_unlikely_executed = false;
2734 if (edge->loop_nest)
2736 maybe_executed_once = false;
2737 if (dump_file && (dump_flags & TDF_DETAILS))
2738 fprintf (dump_file, " Called in loop\n");
2740 break;
2741 case NODE_FREQUENCY_HOT:
2742 case NODE_FREQUENCY_NORMAL:
2743 if (dump_file && (dump_flags & TDF_DETAILS))
2744 fprintf (dump_file, " Called by %s that is normal or hot\n",
2745 cgraph_node_name (edge->caller));
2746 maybe_unlikely_executed = false;
2747 maybe_executed_once = false;
2748 break;
2751 if ((only_called_at_startup && !only_called_at_exit)
2752 && !node->only_called_at_startup)
2754 node->only_called_at_startup = true;
2755 if (dump_file)
2756 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2757 cgraph_node_name (node));
2758 changed = true;
2760 if ((only_called_at_exit && !only_called_at_startup)
2761 && !node->only_called_at_exit)
2763 node->only_called_at_exit = true;
2764 if (dump_file)
2765 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2766 cgraph_node_name (node));
2767 changed = true;
2769 /* These come either from profile or user hints; never update them. */
2770 if (node->frequency == NODE_FREQUENCY_HOT
2771 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2772 return changed;
2773 if (maybe_unlikely_executed)
2775 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2776 if (dump_file)
2777 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2778 cgraph_node_name (node));
2779 changed = true;
2781 else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2783 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2784 if (dump_file)
2785 fprintf (dump_file, "Node %s promoted to executed once.\n",
2786 cgraph_node_name (node));
2787 changed = true;
2789 return changed;
2792 /* Return true when NODE can not return or throw and thus
2793 it is safe to ignore its side effects for IPA analysis. */
2795 bool
2796 cgraph_node_cannot_return (struct cgraph_node *node)
2798 int flags = flags_from_decl_or_type (node->decl);
2799 if (!flag_exceptions)
2800 return (flags & ECF_NORETURN) != 0;
2801 else
2802 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2803 == (ECF_NORETURN | ECF_NOTHROW));
2806 /* Return true when call of E can not lead to return from caller
2807 and thus it is safe to ignore its side effects for IPA analysis
2808 when computing side effects of the caller.
2809 FIXME: We could actually mark all edges that have no reaching
2810 patch to EXIT_BLOCK_PTR or throw to get better results. */
2811 bool
2812 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2814 if (cgraph_node_cannot_return (e->caller))
2815 return true;
2816 if (e->indirect_unknown_callee)
2818 int flags = e->indirect_info->ecf_flags;
2819 if (!flag_exceptions)
2820 return (flags & ECF_NORETURN) != 0;
2821 else
2822 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2823 == (ECF_NORETURN | ECF_NOTHROW));
2825 else
2826 return cgraph_node_cannot_return (e->callee);
2829 /* Return true when function NODE can be removed from callgraph
2830 if all direct calls are eliminated. */
2832 bool
2833 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2835 gcc_assert (!node->global.inlined_to);
2836 /* Extern inlines can always go, we will use the external definition. */
2837 if (DECL_EXTERNAL (node->decl))
2838 return true;
2839 /* When function is needed, we can not remove it. */
2840 if (node->needed || node->reachable_from_other_partition)
2841 return false;
2842 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2843 || DECL_STATIC_DESTRUCTOR (node->decl))
2844 return false;
2845 /* Only COMDAT functions can be removed if externally visible. */
2846 if (node->local.externally_visible
2847 && (!DECL_COMDAT (node->decl)
2848 || cgraph_used_from_object_file_p (node)))
2849 return false;
2850 return true;
2853 /* Return true when function NODE can be expected to be removed
2854 from program when direct calls in this compilation unit are removed.
2856 As a special case COMDAT functions are
2857 cgraph_can_remove_if_no_direct_calls_p while the are not
2858 cgraph_only_called_directly_p (it is possible they are called from other
2859 unit)
2861 This function behaves as cgraph_only_called_directly_p because eliminating
2862 all uses of COMDAT function does not make it necessarily disappear from
2863 the program unless we are compiling whole program or we do LTO. In this
2864 case we know we win since dynamic linking will not really discard the
2865 linkonce section. */
2867 bool
2868 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2870 gcc_assert (!node->global.inlined_to);
2871 if (cgraph_used_from_object_file_p (node))
2872 return false;
2873 if (!in_lto_p && !flag_whole_program)
2874 return cgraph_only_called_directly_p (node);
2875 else
2877 if (DECL_EXTERNAL (node->decl))
2878 return true;
2879 return cgraph_can_remove_if_no_direct_calls_p (node);
2883 /* Return true when RESOLUTION indicate that linker will use
2884 the symbol from non-LTO object files. */
2886 bool
2887 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2889 return (resolution == LDPR_PREVAILING_DEF
2890 || resolution == LDPR_PREEMPTED_REG
2891 || resolution == LDPR_RESOLVED_EXEC
2892 || resolution == LDPR_RESOLVED_DYN);
2895 /* Return true when NODE is known to be used from other (non-LTO) object file.
2896 Known only when doing LTO via linker plugin. */
2898 bool
2899 cgraph_used_from_object_file_p (struct cgraph_node *node)
2901 struct cgraph_node *alias;
2903 gcc_assert (!node->global.inlined_to);
2904 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2905 return false;
2906 if (resolution_used_from_other_file_p (node->resolution))
2907 return true;
2908 for (alias = node->same_body; alias; alias = alias->next)
2909 if (TREE_PUBLIC (alias->decl)
2910 && resolution_used_from_other_file_p (alias->resolution))
2911 return true;
2912 return false;
2915 #include "gt-cgraph.h"