PR target/50465
[official-gcc.git] / gcc / cgraph.c
blob14e7a3b0f0818e0729b475a9e99a322dc188c411
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"
101 #include "ipa-inline.h"
103 const char * const ld_plugin_symbol_resolution_names[]=
106 "undef",
107 "prevailing_def",
108 "prevailing_def_ironly",
109 "preempted_reg",
110 "preempted_ir",
111 "resolved_ir",
112 "resolved_exec",
113 "resolved_dyn"
116 static void cgraph_node_remove_callers (struct cgraph_node *node);
117 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
118 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
120 /* Hash table used to convert declarations into nodes. */
121 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
122 /* Hash table used to convert assembler names into nodes. */
123 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
125 /* The linked list of cgraph nodes. */
126 struct cgraph_node *cgraph_nodes;
128 /* Queue of cgraph nodes scheduled to be lowered. */
129 struct cgraph_node *cgraph_nodes_queue;
131 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
132 secondary queue used during optimization to accommodate passes that
133 may generate new functions that need to be optimized and expanded. */
134 struct cgraph_node *cgraph_new_nodes;
136 /* Number of nodes in existence. */
137 int cgraph_n_nodes;
139 /* Maximal uid used in cgraph nodes. */
140 int cgraph_max_uid;
142 /* Maximal uid used in cgraph edges. */
143 int cgraph_edge_max_uid;
145 /* Set when whole unit has been analyzed so we can access global info. */
146 bool cgraph_global_info_ready = false;
148 /* What state callgraph is in right now. */
149 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
151 /* Set when the cgraph is fully build and the basic flags are computed. */
152 bool cgraph_function_flags_ready = false;
154 /* Linked list of cgraph asm nodes. */
155 struct cgraph_asm_node *cgraph_asm_nodes;
157 /* Last node in cgraph_asm_nodes. */
158 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
160 /* The order index of the next cgraph node to be created. This is
161 used so that we can sort the cgraph nodes in order by when we saw
162 them, to support -fno-toplevel-reorder. */
163 int cgraph_order;
165 /* List of hooks triggered on cgraph_edge events. */
166 struct cgraph_edge_hook_list {
167 cgraph_edge_hook hook;
168 void *data;
169 struct cgraph_edge_hook_list *next;
172 /* List of hooks triggered on cgraph_node events. */
173 struct cgraph_node_hook_list {
174 cgraph_node_hook hook;
175 void *data;
176 struct cgraph_node_hook_list *next;
179 /* List of hooks triggered on events involving two cgraph_edges. */
180 struct cgraph_2edge_hook_list {
181 cgraph_2edge_hook hook;
182 void *data;
183 struct cgraph_2edge_hook_list *next;
186 /* List of hooks triggered on events involving two cgraph_nodes. */
187 struct cgraph_2node_hook_list {
188 cgraph_2node_hook hook;
189 void *data;
190 struct cgraph_2node_hook_list *next;
193 /* List of hooks triggered when an edge is removed. */
194 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
195 /* List of hooks triggered when a node is removed. */
196 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
197 /* List of hooks triggered when an edge is duplicated. */
198 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
199 /* List of hooks triggered when a node is duplicated. */
200 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
201 /* List of hooks triggered when an function is inserted. */
202 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
204 /* Head of a linked list of unused (freed) call graph nodes.
205 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
206 static GTY(()) struct cgraph_node *free_nodes;
207 /* Head of a linked list of unused (freed) call graph edges.
208 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
209 static GTY(()) struct cgraph_edge *free_edges;
211 /* Did procss_same_body_aliases run? */
212 bool same_body_aliases_done;
214 /* Macros to access the next item in the list of free cgraph nodes and
215 edges. */
216 #define NEXT_FREE_NODE(NODE) (NODE)->next
217 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
219 /* Register HOOK to be called with DATA on each removed edge. */
220 struct cgraph_edge_hook_list *
221 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
223 struct cgraph_edge_hook_list *entry;
224 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
226 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
227 entry->hook = hook;
228 entry->data = data;
229 entry->next = NULL;
230 while (*ptr)
231 ptr = &(*ptr)->next;
232 *ptr = entry;
233 return entry;
236 /* Remove ENTRY from the list of hooks called on removing edges. */
237 void
238 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
240 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
242 while (*ptr != entry)
243 ptr = &(*ptr)->next;
244 *ptr = entry->next;
245 free (entry);
248 /* Call all edge removal hooks. */
249 static void
250 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
252 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
253 while (entry)
255 entry->hook (e, entry->data);
256 entry = entry->next;
260 /* Register HOOK to be called with DATA on each removed node. */
261 struct cgraph_node_hook_list *
262 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
264 struct cgraph_node_hook_list *entry;
265 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
267 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
268 entry->hook = hook;
269 entry->data = data;
270 entry->next = NULL;
271 while (*ptr)
272 ptr = &(*ptr)->next;
273 *ptr = entry;
274 return entry;
277 /* Remove ENTRY from the list of hooks called on removing nodes. */
278 void
279 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
281 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
283 while (*ptr != entry)
284 ptr = &(*ptr)->next;
285 *ptr = entry->next;
286 free (entry);
289 /* Call all node removal hooks. */
290 static void
291 cgraph_call_node_removal_hooks (struct cgraph_node *node)
293 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
294 while (entry)
296 entry->hook (node, entry->data);
297 entry = entry->next;
301 /* Register HOOK to be called with DATA on each inserted node. */
302 struct cgraph_node_hook_list *
303 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
305 struct cgraph_node_hook_list *entry;
306 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
308 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
309 entry->hook = hook;
310 entry->data = data;
311 entry->next = NULL;
312 while (*ptr)
313 ptr = &(*ptr)->next;
314 *ptr = entry;
315 return entry;
318 /* Remove ENTRY from the list of hooks called on inserted nodes. */
319 void
320 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
322 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
324 while (*ptr != entry)
325 ptr = &(*ptr)->next;
326 *ptr = entry->next;
327 free (entry);
330 /* Call all node insertion hooks. */
331 void
332 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
334 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
335 while (entry)
337 entry->hook (node, entry->data);
338 entry = entry->next;
342 /* Register HOOK to be called with DATA on each duplicated edge. */
343 struct cgraph_2edge_hook_list *
344 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
346 struct cgraph_2edge_hook_list *entry;
347 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
349 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
350 entry->hook = hook;
351 entry->data = data;
352 entry->next = NULL;
353 while (*ptr)
354 ptr = &(*ptr)->next;
355 *ptr = entry;
356 return entry;
359 /* Remove ENTRY from the list of hooks called on duplicating edges. */
360 void
361 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
363 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
365 while (*ptr != entry)
366 ptr = &(*ptr)->next;
367 *ptr = entry->next;
368 free (entry);
371 /* Call all edge duplication hooks. */
372 static void
373 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
374 struct cgraph_edge *cs2)
376 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
377 while (entry)
379 entry->hook (cs1, cs2, entry->data);
380 entry = entry->next;
384 /* Register HOOK to be called with DATA on each duplicated node. */
385 struct cgraph_2node_hook_list *
386 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
388 struct cgraph_2node_hook_list *entry;
389 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
391 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
392 entry->hook = hook;
393 entry->data = data;
394 entry->next = NULL;
395 while (*ptr)
396 ptr = &(*ptr)->next;
397 *ptr = entry;
398 return entry;
401 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
402 void
403 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
405 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
407 while (*ptr != entry)
408 ptr = &(*ptr)->next;
409 *ptr = entry->next;
410 free (entry);
413 /* Call all node duplication hooks. */
414 static void
415 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
416 struct cgraph_node *node2)
418 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
419 while (entry)
421 entry->hook (node1, node2, entry->data);
422 entry = entry->next;
426 /* Returns a hash code for P. */
428 static hashval_t
429 hash_node (const void *p)
431 const struct cgraph_node *n = (const struct cgraph_node *) p;
432 return (hashval_t) DECL_UID (n->decl);
436 /* Returns nonzero if P1 and P2 are equal. */
438 static int
439 eq_node (const void *p1, const void *p2)
441 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
442 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
443 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
446 /* Allocate new callgraph node. */
448 static inline struct cgraph_node *
449 cgraph_allocate_node (void)
451 struct cgraph_node *node;
453 if (free_nodes)
455 node = free_nodes;
456 free_nodes = NEXT_FREE_NODE (node);
458 else
460 node = ggc_alloc_cleared_cgraph_node ();
461 node->uid = cgraph_max_uid++;
464 return node;
467 /* Allocate new callgraph node and insert it into basic data structures. */
469 static struct cgraph_node *
470 cgraph_create_node_1 (void)
472 struct cgraph_node *node = cgraph_allocate_node ();
474 node->next = cgraph_nodes;
475 node->order = cgraph_order++;
476 if (cgraph_nodes)
477 cgraph_nodes->previous = node;
478 node->previous = NULL;
479 node->frequency = NODE_FREQUENCY_NORMAL;
480 node->count_materialization_scale = REG_BR_PROB_BASE;
481 ipa_empty_ref_list (&node->ref_list);
482 cgraph_nodes = node;
483 cgraph_n_nodes++;
484 return node;
487 /* Return cgraph node assigned to DECL. Create new one when needed. */
489 struct cgraph_node *
490 cgraph_create_node (tree decl)
492 struct cgraph_node key, *node, **slot;
494 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
496 if (!cgraph_hash)
497 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
499 key.decl = decl;
500 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
501 gcc_assert (!*slot);
503 node = cgraph_create_node_1 ();
504 node->decl = decl;
505 *slot = node;
506 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
508 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
509 node->next_nested = node->origin->nested;
510 node->origin->nested = node;
512 if (assembler_name_hash)
514 void **aslot;
515 tree name = DECL_ASSEMBLER_NAME (decl);
517 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
518 decl_assembler_name_hash (name),
519 INSERT);
520 /* We can have multiple declarations with same assembler name. For C++
521 it is __builtin_strlen and strlen, for instance. Do we need to
522 record them all? Original implementation marked just first one
523 so lets hope for the best. */
524 if (*aslot == NULL)
525 *aslot = node;
527 return node;
530 /* Try to find a call graph node for declaration DECL and if it does not exist,
531 create it. */
533 struct cgraph_node *
534 cgraph_get_create_node (tree decl)
536 struct cgraph_node *node;
538 node = cgraph_get_node (decl);
539 if (node)
540 return node;
542 return cgraph_create_node (decl);
545 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
546 the function body is associated with (not neccesarily cgraph_node (DECL). */
548 struct cgraph_node *
549 cgraph_create_function_alias (tree alias, tree decl)
551 struct cgraph_node *alias_node;
553 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
554 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
555 alias_node = cgraph_get_create_node (alias);
556 gcc_assert (!alias_node->local.finalized);
557 alias_node->thunk.alias = decl;
558 alias_node->local.finalized = true;
559 alias_node->alias = 1;
561 if ((TREE_PUBLIC (alias) && !DECL_COMDAT (alias) && !DECL_EXTERNAL (alias))
562 || (DECL_VIRTUAL_P (alias)
563 && (DECL_COMDAT (alias) || DECL_EXTERNAL (alias))))
564 cgraph_mark_reachable_node (alias_node);
565 return alias_node;
568 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
569 and NULL otherwise.
570 Same body aliases are output whenever the body of DECL is output,
571 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
573 struct cgraph_node *
574 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
576 struct cgraph_node *n;
577 #ifndef ASM_OUTPUT_DEF
578 /* If aliases aren't supported by the assembler, fail. */
579 return NULL;
580 #endif
581 /* Langhooks can create same body aliases of symbols not defined.
582 Those are useless. Drop them on the floor. */
583 if (cgraph_global_info_ready)
584 return NULL;
586 n = cgraph_create_function_alias (alias, decl);
587 n->same_body_alias = true;
588 if (same_body_aliases_done)
589 ipa_record_reference (n, NULL, cgraph_get_node (decl), NULL, IPA_REF_ALIAS,
590 NULL);
591 return n;
594 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
595 aliases DECL with an adjustments made into the first parameter.
596 See comments in thunk_adjust for detail on the parameters. */
598 struct cgraph_node *
599 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
600 tree alias, tree decl,
601 bool this_adjusting,
602 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
603 tree virtual_offset,
604 tree real_alias)
606 struct cgraph_node *node;
608 node = cgraph_get_node (alias);
609 if (node)
611 gcc_assert (node->local.finalized);
612 gcc_assert (!node->alias);
613 gcc_assert (!node->thunk.thunk_p);
614 cgraph_remove_node (node);
617 node = cgraph_create_node (alias);
618 gcc_checking_assert (!virtual_offset
619 || double_int_equal_p
620 (tree_to_double_int (virtual_offset),
621 shwi_to_double_int (virtual_value)));
622 node->thunk.fixed_offset = fixed_offset;
623 node->thunk.this_adjusting = this_adjusting;
624 node->thunk.virtual_value = virtual_value;
625 node->thunk.virtual_offset_p = virtual_offset != NULL;
626 node->thunk.alias = real_alias;
627 node->thunk.thunk_p = true;
628 node->local.finalized = true;
630 if (cgraph_decide_is_function_needed (node, decl))
631 cgraph_mark_needed_node (node);
633 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
634 || (DECL_VIRTUAL_P (decl)
635 && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
636 cgraph_mark_reachable_node (node);
638 return node;
641 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
642 is assigned. */
644 struct cgraph_node *
645 cgraph_get_node (const_tree decl)
647 struct cgraph_node key, *node = NULL, **slot;
649 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
651 if (!cgraph_hash)
652 return NULL;
654 key.decl = CONST_CAST2 (tree, const_tree, decl);
656 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
657 NO_INSERT);
659 if (slot && *slot)
660 node = *slot;
661 return node;
664 /* Insert already constructed node into hashtable. */
666 void
667 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
669 struct cgraph_node **slot;
671 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
673 gcc_assert (!*slot);
674 *slot = node;
677 /* Returns a hash code for P. */
679 static hashval_t
680 hash_node_by_assembler_name (const void *p)
682 const struct cgraph_node *n = (const struct cgraph_node *) p;
683 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
686 /* Returns nonzero if P1 and P2 are equal. */
688 static int
689 eq_assembler_name (const void *p1, const void *p2)
691 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
692 const_tree name = (const_tree)p2;
693 return (decl_assembler_name_equal (n1->decl, name));
696 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
697 Return NULL if there's no such node. */
699 struct cgraph_node *
700 cgraph_node_for_asm (tree asmname)
702 struct cgraph_node *node;
703 void **slot;
705 if (!assembler_name_hash)
707 assembler_name_hash =
708 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
709 NULL);
710 for (node = cgraph_nodes; node; node = node->next)
711 if (!node->global.inlined_to)
713 tree name = DECL_ASSEMBLER_NAME (node->decl);
714 slot = htab_find_slot_with_hash (assembler_name_hash, name,
715 decl_assembler_name_hash (name),
716 INSERT);
717 /* We can have multiple declarations with same assembler name. For C++
718 it is __builtin_strlen and strlen, for instance. Do we need to
719 record them all? Original implementation marked just first one
720 so lets hope for the best. */
721 if (!*slot)
722 *slot = node;
726 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
727 decl_assembler_name_hash (asmname),
728 NO_INSERT);
730 if (slot)
732 node = (struct cgraph_node *) *slot;
733 return node;
735 return NULL;
738 /* Returns a hash value for X (which really is a die_struct). */
740 static hashval_t
741 edge_hash (const void *x)
743 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
746 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
748 static int
749 edge_eq (const void *x, const void *y)
751 return ((const struct cgraph_edge *) x)->call_stmt == y;
754 /* Add call graph edge E to call site hash of its caller. */
756 static inline void
757 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
759 void **slot;
760 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
761 e->call_stmt,
762 htab_hash_pointer (e->call_stmt),
763 INSERT);
764 gcc_assert (!*slot);
765 *slot = e;
768 /* Return the callgraph edge representing the GIMPLE_CALL statement
769 CALL_STMT. */
771 struct cgraph_edge *
772 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
774 struct cgraph_edge *e, *e2;
775 int n = 0;
777 if (node->call_site_hash)
778 return (struct cgraph_edge *)
779 htab_find_with_hash (node->call_site_hash, call_stmt,
780 htab_hash_pointer (call_stmt));
782 /* This loop may turn out to be performance problem. In such case adding
783 hashtables into call nodes with very many edges is probably best
784 solution. It is not good idea to add pointer into CALL_EXPR itself
785 because we want to make possible having multiple cgraph nodes representing
786 different clones of the same body before the body is actually cloned. */
787 for (e = node->callees; e; e = e->next_callee)
789 if (e->call_stmt == call_stmt)
790 break;
791 n++;
794 if (!e)
795 for (e = node->indirect_calls; e; e = e->next_callee)
797 if (e->call_stmt == call_stmt)
798 break;
799 n++;
802 if (n > 100)
804 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
805 for (e2 = node->callees; e2; e2 = e2->next_callee)
806 cgraph_add_edge_to_call_site_hash (e2);
807 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
808 cgraph_add_edge_to_call_site_hash (e2);
811 return e;
815 /* Change field call_stmt of edge E to NEW_STMT. */
817 void
818 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
820 tree decl;
822 if (e->caller->call_site_hash)
824 htab_remove_elt_with_hash (e->caller->call_site_hash,
825 e->call_stmt,
826 htab_hash_pointer (e->call_stmt));
829 e->call_stmt = new_stmt;
830 if (e->indirect_unknown_callee
831 && (decl = gimple_call_fndecl (new_stmt)))
833 /* Constant propagation (and possibly also inlining?) can turn an
834 indirect call into a direct one. */
835 struct cgraph_node *new_callee = cgraph_get_node (decl);
837 gcc_checking_assert (new_callee);
838 cgraph_make_edge_direct (e, new_callee);
841 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
842 e->can_throw_external = stmt_can_throw_external (new_stmt);
843 pop_cfun ();
844 if (e->caller->call_site_hash)
845 cgraph_add_edge_to_call_site_hash (e);
848 /* Like cgraph_set_call_stmt but walk the clone tree and update all
849 clones sharing the same function body. */
851 void
852 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
853 gimple old_stmt, gimple new_stmt)
855 struct cgraph_node *node;
856 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
858 if (edge)
859 cgraph_set_call_stmt (edge, new_stmt);
861 node = orig->clones;
862 if (node)
863 while (node != orig)
865 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
866 if (edge)
867 cgraph_set_call_stmt (edge, new_stmt);
868 if (node->clones)
869 node = node->clones;
870 else if (node->next_sibling_clone)
871 node = node->next_sibling_clone;
872 else
874 while (node != orig && !node->next_sibling_clone)
875 node = node->clone_of;
876 if (node != orig)
877 node = node->next_sibling_clone;
882 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
883 same function body. If clones already have edge for OLD_STMT; only
884 update the edge same way as cgraph_set_call_stmt_including_clones does.
886 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
887 frequencies of the clones. */
889 void
890 cgraph_create_edge_including_clones (struct cgraph_node *orig,
891 struct cgraph_node *callee,
892 gimple old_stmt,
893 gimple stmt, gcov_type count,
894 int freq,
895 cgraph_inline_failed_t reason)
897 struct cgraph_node *node;
898 struct cgraph_edge *edge;
900 if (!cgraph_edge (orig, stmt))
902 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
903 edge->inline_failed = reason;
906 node = orig->clones;
907 if (node)
908 while (node != orig)
910 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
912 /* It is possible that clones already contain the edge while
913 master didn't. Either we promoted indirect call into direct
914 call in the clone or we are processing clones of unreachable
915 master where edges has been removed. */
916 if (edge)
917 cgraph_set_call_stmt (edge, stmt);
918 else if (!cgraph_edge (node, stmt))
920 edge = cgraph_create_edge (node, callee, stmt, count,
921 freq);
922 edge->inline_failed = reason;
925 if (node->clones)
926 node = node->clones;
927 else if (node->next_sibling_clone)
928 node = node->next_sibling_clone;
929 else
931 while (node != orig && !node->next_sibling_clone)
932 node = node->clone_of;
933 if (node != orig)
934 node = node->next_sibling_clone;
939 /* Allocate a cgraph_edge structure and fill it with data according to the
940 parameters of which only CALLEE can be NULL (when creating an indirect call
941 edge). */
943 static struct cgraph_edge *
944 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
945 gimple call_stmt, gcov_type count, int freq)
947 struct cgraph_edge *edge;
949 /* LTO does not actually have access to the call_stmt since these
950 have not been loaded yet. */
951 if (call_stmt)
953 /* This is a rather expensive check possibly triggering
954 construction of call stmt hashtable. */
955 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
957 gcc_assert (is_gimple_call (call_stmt));
960 if (free_edges)
962 edge = free_edges;
963 free_edges = NEXT_FREE_EDGE (edge);
965 else
967 edge = ggc_alloc_cgraph_edge ();
968 edge->uid = cgraph_edge_max_uid++;
971 edge->aux = NULL;
972 edge->caller = caller;
973 edge->callee = callee;
974 edge->prev_caller = NULL;
975 edge->next_caller = NULL;
976 edge->prev_callee = NULL;
977 edge->next_callee = NULL;
979 edge->count = count;
980 gcc_assert (count >= 0);
981 edge->frequency = freq;
982 gcc_assert (freq >= 0);
983 gcc_assert (freq <= CGRAPH_FREQ_MAX);
985 edge->call_stmt = call_stmt;
986 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
987 edge->can_throw_external
988 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
989 pop_cfun ();
990 edge->call_stmt_cannot_inline_p =
991 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
992 if (call_stmt && caller->call_site_hash)
993 cgraph_add_edge_to_call_site_hash (edge);
995 edge->indirect_info = NULL;
996 edge->indirect_inlining_edge = 0;
998 return edge;
1001 /* Create edge from CALLER to CALLEE in the cgraph. */
1003 struct cgraph_edge *
1004 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1005 gimple call_stmt, gcov_type count, int freq)
1007 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1008 count, freq);
1010 edge->indirect_unknown_callee = 0;
1011 initialize_inline_failed (edge);
1013 edge->next_caller = callee->callers;
1014 if (callee->callers)
1015 callee->callers->prev_caller = edge;
1016 edge->next_callee = caller->callees;
1017 if (caller->callees)
1018 caller->callees->prev_callee = edge;
1019 caller->callees = edge;
1020 callee->callers = edge;
1022 return edge;
1025 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1027 struct cgraph_indirect_call_info *
1028 cgraph_allocate_init_indirect_info (void)
1030 struct cgraph_indirect_call_info *ii;
1032 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1033 ii->param_index = -1;
1034 return ii;
1037 /* Create an indirect edge with a yet-undetermined callee where the call
1038 statement destination is a formal parameter of the caller with index
1039 PARAM_INDEX. */
1041 struct cgraph_edge *
1042 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1043 int ecf_flags,
1044 gcov_type count, int freq)
1046 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1047 count, freq);
1049 edge->indirect_unknown_callee = 1;
1050 initialize_inline_failed (edge);
1052 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1053 edge->indirect_info->ecf_flags = ecf_flags;
1055 edge->next_callee = caller->indirect_calls;
1056 if (caller->indirect_calls)
1057 caller->indirect_calls->prev_callee = edge;
1058 caller->indirect_calls = edge;
1060 return edge;
1063 /* Remove the edge E from the list of the callers of the callee. */
1065 static inline void
1066 cgraph_edge_remove_callee (struct cgraph_edge *e)
1068 gcc_assert (!e->indirect_unknown_callee);
1069 if (e->prev_caller)
1070 e->prev_caller->next_caller = e->next_caller;
1071 if (e->next_caller)
1072 e->next_caller->prev_caller = e->prev_caller;
1073 if (!e->prev_caller)
1074 e->callee->callers = e->next_caller;
1077 /* Remove the edge E from the list of the callees of the caller. */
1079 static inline void
1080 cgraph_edge_remove_caller (struct cgraph_edge *e)
1082 if (e->prev_callee)
1083 e->prev_callee->next_callee = e->next_callee;
1084 if (e->next_callee)
1085 e->next_callee->prev_callee = e->prev_callee;
1086 if (!e->prev_callee)
1088 if (e->indirect_unknown_callee)
1089 e->caller->indirect_calls = e->next_callee;
1090 else
1091 e->caller->callees = e->next_callee;
1093 if (e->caller->call_site_hash)
1094 htab_remove_elt_with_hash (e->caller->call_site_hash,
1095 e->call_stmt,
1096 htab_hash_pointer (e->call_stmt));
1099 /* Put the edge onto the free list. */
1101 static void
1102 cgraph_free_edge (struct cgraph_edge *e)
1104 int uid = e->uid;
1106 /* Clear out the edge so we do not dangle pointers. */
1107 memset (e, 0, sizeof (*e));
1108 e->uid = uid;
1109 NEXT_FREE_EDGE (e) = free_edges;
1110 free_edges = e;
1113 /* Remove the edge E in the cgraph. */
1115 void
1116 cgraph_remove_edge (struct cgraph_edge *e)
1118 /* Call all edge removal hooks. */
1119 cgraph_call_edge_removal_hooks (e);
1121 if (!e->indirect_unknown_callee)
1122 /* Remove from callers list of the callee. */
1123 cgraph_edge_remove_callee (e);
1125 /* Remove from callees list of the callers. */
1126 cgraph_edge_remove_caller (e);
1128 /* Put the edge onto the free list. */
1129 cgraph_free_edge (e);
1132 /* Set callee of call graph edge E and add it to the corresponding set of
1133 callers. */
1135 static void
1136 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1138 e->prev_caller = NULL;
1139 if (n->callers)
1140 n->callers->prev_caller = e;
1141 e->next_caller = n->callers;
1142 n->callers = e;
1143 e->callee = n;
1146 /* Redirect callee of E to N. The function does not update underlying
1147 call expression. */
1149 void
1150 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1152 /* Remove from callers list of the current callee. */
1153 cgraph_edge_remove_callee (e);
1155 /* Insert to callers list of the new callee. */
1156 cgraph_set_edge_callee (e, n);
1159 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1160 CALLEE. DELTA is an integer constant that is to be added to the this
1161 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1163 void
1164 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1166 edge->indirect_unknown_callee = 0;
1168 /* Get the edge out of the indirect edge list. */
1169 if (edge->prev_callee)
1170 edge->prev_callee->next_callee = edge->next_callee;
1171 if (edge->next_callee)
1172 edge->next_callee->prev_callee = edge->prev_callee;
1173 if (!edge->prev_callee)
1174 edge->caller->indirect_calls = edge->next_callee;
1176 /* Put it into the normal callee list */
1177 edge->prev_callee = NULL;
1178 edge->next_callee = edge->caller->callees;
1179 if (edge->caller->callees)
1180 edge->caller->callees->prev_callee = edge;
1181 edge->caller->callees = edge;
1183 /* Insert to callers list of the new callee. */
1184 cgraph_set_edge_callee (edge, callee);
1186 /* We need to re-determine the inlining status of the edge. */
1187 initialize_inline_failed (edge);
1191 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1192 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1193 of OLD_STMT if it was previously call statement.
1194 If NEW_STMT is NULL, the call has been dropped without any
1195 replacement. */
1197 static void
1198 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1199 gimple old_stmt, tree old_call,
1200 gimple new_stmt)
1202 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1203 ? gimple_call_fndecl (new_stmt) : 0;
1205 /* We are seeing indirect calls, then there is nothing to update. */
1206 if (!new_call && !old_call)
1207 return;
1208 /* See if we turned indirect call into direct call or folded call to one builtin
1209 into different builtin. */
1210 if (old_call != new_call)
1212 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1213 struct cgraph_edge *ne = NULL;
1214 gcov_type count;
1215 int frequency;
1217 if (e)
1219 /* See if the edge is already there and has the correct callee. It
1220 might be so because of indirect inlining has already updated
1221 it. We also might've cloned and redirected the edge. */
1222 if (new_call && e->callee)
1224 struct cgraph_node *callee = e->callee;
1225 while (callee)
1227 if (callee->decl == new_call
1228 || callee->former_clone_of == new_call)
1229 return;
1230 callee = callee->clone_of;
1234 /* Otherwise remove edge and create new one; we can't simply redirect
1235 since function has changed, so inline plan and other information
1236 attached to edge is invalid. */
1237 count = e->count;
1238 frequency = e->frequency;
1239 cgraph_remove_edge (e);
1241 else if (new_call)
1243 /* We are seeing new direct call; compute profile info based on BB. */
1244 basic_block bb = gimple_bb (new_stmt);
1245 count = bb->count;
1246 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1247 bb);
1250 if (new_call)
1252 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1253 new_stmt, count, frequency);
1254 gcc_assert (ne->inline_failed);
1257 /* We only updated the call stmt; update pointer in cgraph edge.. */
1258 else if (old_stmt != new_stmt)
1259 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1262 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1263 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1264 of OLD_STMT before it was updated (updating can happen inplace). */
1266 void
1267 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1269 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1270 struct cgraph_node *node;
1272 gcc_checking_assert (orig);
1273 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1274 if (orig->clones)
1275 for (node = orig->clones; node != orig;)
1277 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1278 if (node->clones)
1279 node = node->clones;
1280 else if (node->next_sibling_clone)
1281 node = node->next_sibling_clone;
1282 else
1284 while (node != orig && !node->next_sibling_clone)
1285 node = node->clone_of;
1286 if (node != orig)
1287 node = node->next_sibling_clone;
1293 /* Remove all callees from the node. */
1295 void
1296 cgraph_node_remove_callees (struct cgraph_node *node)
1298 struct cgraph_edge *e, *f;
1300 /* It is sufficient to remove the edges from the lists of callers of
1301 the callees. The callee list of the node can be zapped with one
1302 assignment. */
1303 for (e = node->callees; e; e = f)
1305 f = e->next_callee;
1306 cgraph_call_edge_removal_hooks (e);
1307 if (!e->indirect_unknown_callee)
1308 cgraph_edge_remove_callee (e);
1309 cgraph_free_edge (e);
1311 for (e = node->indirect_calls; e; e = f)
1313 f = e->next_callee;
1314 cgraph_call_edge_removal_hooks (e);
1315 if (!e->indirect_unknown_callee)
1316 cgraph_edge_remove_callee (e);
1317 cgraph_free_edge (e);
1319 node->indirect_calls = NULL;
1320 node->callees = NULL;
1321 if (node->call_site_hash)
1323 htab_delete (node->call_site_hash);
1324 node->call_site_hash = NULL;
1328 /* Remove all callers from the node. */
1330 static void
1331 cgraph_node_remove_callers (struct cgraph_node *node)
1333 struct cgraph_edge *e, *f;
1335 /* It is sufficient to remove the edges from the lists of callees of
1336 the callers. The caller list of the node can be zapped with one
1337 assignment. */
1338 for (e = node->callers; e; e = f)
1340 f = e->next_caller;
1341 cgraph_call_edge_removal_hooks (e);
1342 cgraph_edge_remove_caller (e);
1343 cgraph_free_edge (e);
1345 node->callers = NULL;
1348 /* Release memory used to represent body of function NODE. */
1350 void
1351 cgraph_release_function_body (struct cgraph_node *node)
1353 if (DECL_STRUCT_FUNCTION (node->decl))
1355 tree old_decl = current_function_decl;
1356 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1357 if (cfun->gimple_df)
1359 current_function_decl = node->decl;
1360 delete_tree_ssa ();
1361 delete_tree_cfg_annotations ();
1362 cfun->eh = NULL;
1363 current_function_decl = old_decl;
1365 if (cfun->cfg)
1367 gcc_assert (dom_computed[0] == DOM_NONE);
1368 gcc_assert (dom_computed[1] == DOM_NONE);
1369 clear_edges ();
1371 if (cfun->value_histograms)
1372 free_histograms ();
1373 gcc_assert (!current_loops);
1374 pop_cfun();
1375 gimple_set_body (node->decl, NULL);
1376 VEC_free (ipa_opt_pass, heap,
1377 node->ipa_transforms_to_apply);
1378 /* Struct function hangs a lot of data that would leak if we didn't
1379 removed all pointers to it. */
1380 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1381 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1383 DECL_SAVED_TREE (node->decl) = NULL;
1384 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1385 of its associated function function declaration because it's
1386 needed to emit debug info later. */
1387 if (!node->abstract_and_needed)
1388 DECL_INITIAL (node->decl) = error_mark_node;
1391 /* Remove the node from cgraph. */
1393 void
1394 cgraph_remove_node (struct cgraph_node *node)
1396 void **slot;
1397 bool kill_body = false;
1398 struct cgraph_node *n;
1399 int uid = node->uid;
1401 cgraph_call_node_removal_hooks (node);
1402 cgraph_node_remove_callers (node);
1403 cgraph_node_remove_callees (node);
1404 ipa_remove_all_references (&node->ref_list);
1405 ipa_remove_all_refering (&node->ref_list);
1406 VEC_free (ipa_opt_pass, heap,
1407 node->ipa_transforms_to_apply);
1409 /* Incremental inlining access removed nodes stored in the postorder list.
1411 node->needed = node->reachable = false;
1412 for (n = node->nested; n; n = n->next_nested)
1413 n->origin = NULL;
1414 node->nested = NULL;
1415 if (node->origin)
1417 struct cgraph_node **node2 = &node->origin->nested;
1419 while (*node2 != node)
1420 node2 = &(*node2)->next_nested;
1421 *node2 = node->next_nested;
1423 if (node->previous)
1424 node->previous->next = node->next;
1425 else
1426 cgraph_nodes = node->next;
1427 if (node->next)
1428 node->next->previous = node->previous;
1429 node->next = NULL;
1430 node->previous = NULL;
1431 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1432 if (*slot == node)
1434 struct cgraph_node *next_inline_clone;
1436 for (next_inline_clone = node->clones;
1437 next_inline_clone && next_inline_clone->decl != node->decl;
1438 next_inline_clone = next_inline_clone->next_sibling_clone)
1441 /* If there is inline clone of the node being removed, we need
1442 to put it into the position of removed node and reorganize all
1443 other clones to be based on it. */
1444 if (next_inline_clone)
1446 struct cgraph_node *n;
1447 struct cgraph_node *new_clones;
1449 *slot = next_inline_clone;
1451 /* Unlink inline clone from the list of clones of removed node. */
1452 if (next_inline_clone->next_sibling_clone)
1453 next_inline_clone->next_sibling_clone->prev_sibling_clone
1454 = next_inline_clone->prev_sibling_clone;
1455 if (next_inline_clone->prev_sibling_clone)
1457 gcc_assert (node->clones != next_inline_clone);
1458 next_inline_clone->prev_sibling_clone->next_sibling_clone
1459 = next_inline_clone->next_sibling_clone;
1461 else
1463 gcc_assert (node->clones == next_inline_clone);
1464 node->clones = next_inline_clone->next_sibling_clone;
1467 new_clones = node->clones;
1468 node->clones = NULL;
1470 /* Copy clone info. */
1471 next_inline_clone->clone = node->clone;
1473 /* Now place it into clone tree at same level at NODE. */
1474 next_inline_clone->clone_of = node->clone_of;
1475 next_inline_clone->prev_sibling_clone = NULL;
1476 next_inline_clone->next_sibling_clone = NULL;
1477 if (node->clone_of)
1479 if (node->clone_of->clones)
1480 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1481 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1482 node->clone_of->clones = next_inline_clone;
1485 /* Merge the clone list. */
1486 if (new_clones)
1488 if (!next_inline_clone->clones)
1489 next_inline_clone->clones = new_clones;
1490 else
1492 n = next_inline_clone->clones;
1493 while (n->next_sibling_clone)
1494 n = n->next_sibling_clone;
1495 n->next_sibling_clone = new_clones;
1496 new_clones->prev_sibling_clone = n;
1500 /* Update clone_of pointers. */
1501 n = new_clones;
1502 while (n)
1504 n->clone_of = next_inline_clone;
1505 n = n->next_sibling_clone;
1508 else
1510 htab_clear_slot (cgraph_hash, slot);
1511 kill_body = true;
1515 if (node->prev_sibling_clone)
1516 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1517 else if (node->clone_of)
1518 node->clone_of->clones = node->next_sibling_clone;
1519 if (node->next_sibling_clone)
1520 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1521 if (node->clones)
1523 struct cgraph_node *n, *next;
1525 if (node->clone_of)
1527 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1528 n->clone_of = node->clone_of;
1529 n->clone_of = node->clone_of;
1530 n->next_sibling_clone = node->clone_of->clones;
1531 if (node->clone_of->clones)
1532 node->clone_of->clones->prev_sibling_clone = n;
1533 node->clone_of->clones = node->clones;
1535 else
1537 /* We are removing node with clones. this makes clones inconsistent,
1538 but assume they will be removed subsequently and just keep clone
1539 tree intact. This can happen in unreachable function removal since
1540 we remove unreachable functions in random order, not by bottom-up
1541 walk of clone trees. */
1542 for (n = node->clones; n; n = next)
1544 next = n->next_sibling_clone;
1545 n->next_sibling_clone = NULL;
1546 n->prev_sibling_clone = NULL;
1547 n->clone_of = NULL;
1552 if (node->same_comdat_group)
1554 struct cgraph_node *prev;
1555 for (prev = node->same_comdat_group;
1556 prev->same_comdat_group != node;
1557 prev = prev->same_comdat_group)
1559 if (node->same_comdat_group == prev)
1560 prev->same_comdat_group = NULL;
1561 else
1562 prev->same_comdat_group = node->same_comdat_group;
1563 node->same_comdat_group = NULL;
1566 /* While all the clones are removed after being proceeded, the function
1567 itself is kept in the cgraph even after it is compiled. Check whether
1568 we are done with this body and reclaim it proactively if this is the case.
1570 if (!kill_body && *slot)
1572 struct cgraph_node *n = (struct cgraph_node *) *slot;
1573 if (!n->clones && !n->clone_of && !n->global.inlined_to
1574 && (cgraph_global_info_ready
1575 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1576 || n->in_other_partition)))
1577 kill_body = true;
1579 if (assembler_name_hash)
1581 tree name = DECL_ASSEMBLER_NAME (node->decl);
1582 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1583 decl_assembler_name_hash (name),
1584 NO_INSERT);
1585 /* Inline clones are not hashed. */
1586 if (slot && *slot == node)
1587 htab_clear_slot (assembler_name_hash, slot);
1590 if (kill_body)
1591 cgraph_release_function_body (node);
1592 node->decl = NULL;
1593 if (node->call_site_hash)
1595 htab_delete (node->call_site_hash);
1596 node->call_site_hash = NULL;
1598 cgraph_n_nodes--;
1600 /* Clear out the node to NULL all pointers and add the node to the free
1601 list. */
1602 memset (node, 0, sizeof(*node));
1603 node->uid = uid;
1604 NEXT_FREE_NODE (node) = free_nodes;
1605 free_nodes = node;
1608 /* Add NEW_ to the same comdat group that OLD is in. */
1610 void
1611 cgraph_add_to_same_comdat_group (struct cgraph_node *new_,
1612 struct cgraph_node *old)
1614 gcc_assert (DECL_ONE_ONLY (old->decl));
1615 gcc_assert (!new_->same_comdat_group);
1616 gcc_assert (new_ != old);
1618 DECL_COMDAT_GROUP (new_->decl) = DECL_COMDAT_GROUP (old->decl);
1619 new_->same_comdat_group = old;
1620 if (!old->same_comdat_group)
1621 old->same_comdat_group = new_;
1622 else
1624 struct cgraph_node *n;
1625 for (n = old->same_comdat_group;
1626 n->same_comdat_group != old;
1627 n = n->same_comdat_group)
1629 n->same_comdat_group = new_;
1633 /* Remove the node from cgraph. */
1635 void
1636 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1638 struct cgraph_edge *e, *next;
1639 for (e = node->callees; e; e = next)
1641 next = e->next_callee;
1642 if (!e->inline_failed)
1643 cgraph_remove_node_and_inline_clones (e->callee);
1645 cgraph_remove_node (node);
1648 /* Notify finalize_compilation_unit that given node is reachable. */
1650 void
1651 cgraph_mark_reachable_node (struct cgraph_node *node)
1653 if (!node->reachable && node->local.finalized)
1655 if (cgraph_global_info_ready)
1657 /* Verify that function does not appear to be needed out of blue
1658 during the optimization process. This can happen for extern
1659 inlines when bodies was removed after inlining. */
1660 gcc_assert ((node->analyzed || node->in_other_partition
1661 || DECL_EXTERNAL (node->decl)));
1663 else
1664 notice_global_symbol (node->decl);
1665 node->reachable = 1;
1667 node->next_needed = cgraph_nodes_queue;
1668 cgraph_nodes_queue = node;
1672 /* Likewise indicate that a node is needed, i.e. reachable via some
1673 external means. */
1675 void
1676 cgraph_mark_needed_node (struct cgraph_node *node)
1678 node->needed = 1;
1679 gcc_assert (!node->global.inlined_to);
1680 cgraph_mark_reachable_node (node);
1683 /* Likewise indicate that a node is having address taken. */
1685 void
1686 cgraph_mark_address_taken_node (struct cgraph_node *node)
1688 gcc_assert (!node->global.inlined_to);
1689 cgraph_mark_reachable_node (node);
1690 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1691 IPA_REF_ADDR reference exists (and thus it should be set on node
1692 representing alias we take address of) and as a test whether address
1693 of the object was taken (and thus it should be set on node alias is
1694 referring to). We should remove the first use and the remove the
1695 following set. */
1696 node->address_taken = 1;
1697 node = cgraph_function_or_thunk_node (node, NULL);
1698 node->address_taken = 1;
1701 /* Return local info for the compiled function. */
1703 struct cgraph_local_info *
1704 cgraph_local_info (tree decl)
1706 struct cgraph_node *node;
1708 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1709 node = cgraph_get_node (decl);
1710 if (!node)
1711 return NULL;
1712 return &node->local;
1715 /* Return local info for the compiled function. */
1717 struct cgraph_global_info *
1718 cgraph_global_info (tree decl)
1720 struct cgraph_node *node;
1722 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1723 node = cgraph_get_node (decl);
1724 if (!node)
1725 return NULL;
1726 return &node->global;
1729 /* Return local info for the compiled function. */
1731 struct cgraph_rtl_info *
1732 cgraph_rtl_info (tree decl)
1734 struct cgraph_node *node;
1736 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1737 node = cgraph_get_node (decl);
1738 if (!node
1739 || (decl != current_function_decl
1740 && !TREE_ASM_WRITTEN (node->decl)))
1741 return NULL;
1742 return &node->rtl;
1745 /* Return a string describing the failure REASON. */
1747 const char*
1748 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1750 #undef DEFCIFCODE
1751 #define DEFCIFCODE(code, string) string,
1753 static const char *cif_string_table[CIF_N_REASONS] = {
1754 #include "cif-code.def"
1757 /* Signedness of an enum type is implementation defined, so cast it
1758 to unsigned before testing. */
1759 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1760 return cif_string_table[reason];
1763 /* Return name of the node used in debug output. */
1764 const char *
1765 cgraph_node_name (struct cgraph_node *node)
1767 return lang_hooks.decl_printable_name (node->decl, 2);
1770 /* Names used to print out the availability enum. */
1771 const char * const cgraph_availability_names[] =
1772 {"unset", "not_available", "overwritable", "available", "local"};
1775 /* Dump call graph node NODE to file F. */
1777 void
1778 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1780 struct cgraph_edge *edge;
1781 int indirect_calls_count = 0;
1783 fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
1784 dump_addr (f, " @", (void *)node);
1785 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1786 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1787 if (node->global.inlined_to)
1788 fprintf (f, " (inline copy in %s/%i)",
1789 cgraph_node_name (node->global.inlined_to),
1790 node->global.inlined_to->uid);
1791 if (node->same_comdat_group)
1792 fprintf (f, " (same comdat group as %s/%i)",
1793 cgraph_node_name (node->same_comdat_group),
1794 node->same_comdat_group->uid);
1795 if (node->clone_of)
1796 fprintf (f, " (clone of %s/%i)",
1797 cgraph_node_name (node->clone_of),
1798 node->clone_of->uid);
1799 if (cgraph_function_flags_ready)
1800 fprintf (f, " availability:%s",
1801 cgraph_availability_names [cgraph_function_body_availability (node)]);
1802 if (node->analyzed)
1803 fprintf (f, " analyzed");
1804 if (node->in_other_partition)
1805 fprintf (f, " in_other_partition");
1806 if (node->count)
1807 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1808 (HOST_WIDEST_INT)node->count);
1809 if (node->origin)
1810 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1811 if (node->needed)
1812 fprintf (f, " needed");
1813 if (node->address_taken)
1814 fprintf (f, " address_taken");
1815 else if (node->reachable)
1816 fprintf (f, " reachable");
1817 else if (node->reachable_from_other_partition)
1818 fprintf (f, " reachable_from_other_partition");
1819 if (gimple_has_body_p (node->decl))
1820 fprintf (f, " body");
1821 if (node->process)
1822 fprintf (f, " process");
1823 if (node->local.local)
1824 fprintf (f, " local");
1825 if (node->local.externally_visible)
1826 fprintf (f, " externally_visible");
1827 if (node->resolution != LDPR_UNKNOWN)
1828 fprintf (f, " %s",
1829 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1830 if (node->local.finalized)
1831 fprintf (f, " finalized");
1832 if (node->local.redefined_extern_inline)
1833 fprintf (f, " redefined_extern_inline");
1834 if (TREE_ASM_WRITTEN (node->decl))
1835 fprintf (f, " asm_written");
1836 if (node->only_called_at_startup)
1837 fprintf (f, " only_called_at_startup");
1838 if (node->only_called_at_exit)
1839 fprintf (f, " only_called_at_exit");
1841 fprintf (f, "\n");
1843 if (node->thunk.thunk_p)
1845 fprintf (f, " thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1846 "virtual offset %i)\n",
1847 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1848 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1849 (int)node->thunk.fixed_offset,
1850 (int)node->thunk.virtual_value,
1851 (int)node->thunk.virtual_offset_p);
1853 if (node->alias && node->thunk.alias)
1855 fprintf (f, " alias of %s",
1856 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1857 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1858 fprintf (f, " (asm: %s)",
1859 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1860 fprintf (f, "\n");
1863 fprintf (f, " called by: ");
1865 for (edge = node->callers; edge; edge = edge->next_caller)
1867 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1868 edge->caller->uid);
1869 if (edge->count)
1870 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1871 (HOST_WIDEST_INT)edge->count);
1872 if (edge->frequency)
1873 fprintf (f, "(%.2f per call) ",
1874 edge->frequency / (double)CGRAPH_FREQ_BASE);
1875 if (!edge->inline_failed)
1876 fprintf(f, "(inlined) ");
1877 if (edge->indirect_inlining_edge)
1878 fprintf(f, "(indirect_inlining) ");
1879 if (edge->can_throw_external)
1880 fprintf(f, "(can throw external) ");
1883 fprintf (f, "\n calls: ");
1884 for (edge = node->callees; edge; edge = edge->next_callee)
1886 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1887 edge->callee->uid);
1888 if (!edge->inline_failed)
1889 fprintf(f, "(inlined) ");
1890 if (edge->indirect_inlining_edge)
1891 fprintf(f, "(indirect_inlining) ");
1892 if (edge->count)
1893 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1894 (HOST_WIDEST_INT)edge->count);
1895 if (edge->frequency)
1896 fprintf (f, "(%.2f per call) ",
1897 edge->frequency / (double)CGRAPH_FREQ_BASE);
1898 if (edge->can_throw_external)
1899 fprintf(f, "(can throw external) ");
1901 fprintf (f, "\n");
1902 fprintf (f, " References: ");
1903 ipa_dump_references (f, &node->ref_list);
1904 fprintf (f, " Refering this function: ");
1905 ipa_dump_refering (f, &node->ref_list);
1907 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1908 indirect_calls_count++;
1909 if (indirect_calls_count)
1910 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1911 indirect_calls_count);
1915 /* Dump call graph node NODE to stderr. */
1917 DEBUG_FUNCTION void
1918 debug_cgraph_node (struct cgraph_node *node)
1920 dump_cgraph_node (stderr, node);
1924 /* Dump the callgraph to file F. */
1926 void
1927 dump_cgraph (FILE *f)
1929 struct cgraph_node *node;
1931 fprintf (f, "callgraph:\n\n");
1932 for (node = cgraph_nodes; node; node = node->next)
1933 dump_cgraph_node (f, node);
1937 /* Dump the call graph to stderr. */
1939 DEBUG_FUNCTION void
1940 debug_cgraph (void)
1942 dump_cgraph (stderr);
1946 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
1948 void
1949 change_decl_assembler_name (tree decl, tree name)
1951 struct cgraph_node *node;
1952 void **slot;
1953 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1954 SET_DECL_ASSEMBLER_NAME (decl, name);
1955 else
1957 if (name == DECL_ASSEMBLER_NAME (decl))
1958 return;
1960 if (assembler_name_hash
1961 && TREE_CODE (decl) == FUNCTION_DECL
1962 && (node = cgraph_get_node (decl)) != NULL)
1964 tree old_name = DECL_ASSEMBLER_NAME (decl);
1965 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
1966 decl_assembler_name_hash (old_name),
1967 NO_INSERT);
1968 /* Inline clones are not hashed. */
1969 if (slot && *slot == node)
1970 htab_clear_slot (assembler_name_hash, slot);
1972 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1973 && DECL_RTL_SET_P (decl))
1974 warning (0, "%D renamed after being referenced in assembly", decl);
1976 SET_DECL_ASSEMBLER_NAME (decl, name);
1978 if (assembler_name_hash
1979 && TREE_CODE (decl) == FUNCTION_DECL
1980 && (node = cgraph_get_node (decl)) != NULL)
1982 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1983 decl_assembler_name_hash (name),
1984 INSERT);
1985 gcc_assert (!*slot);
1986 *slot = node;
1990 /* Add a top-level asm statement to the list. */
1992 struct cgraph_asm_node *
1993 cgraph_add_asm_node (tree asm_str)
1995 struct cgraph_asm_node *node;
1997 node = ggc_alloc_cleared_cgraph_asm_node ();
1998 node->asm_str = asm_str;
1999 node->order = cgraph_order++;
2000 node->next = NULL;
2001 if (cgraph_asm_nodes == NULL)
2002 cgraph_asm_nodes = node;
2003 else
2004 cgraph_asm_last_node->next = node;
2005 cgraph_asm_last_node = node;
2006 return node;
2009 /* Return true when the DECL can possibly be inlined. */
2010 bool
2011 cgraph_function_possibly_inlined_p (tree decl)
2013 if (!cgraph_global_info_ready)
2014 return !DECL_UNINLINABLE (decl);
2015 return DECL_POSSIBLY_INLINED (decl);
2018 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2019 struct cgraph_edge *
2020 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2021 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2022 int freq_scale, bool update_original)
2024 struct cgraph_edge *new_edge;
2025 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2026 gcov_type freq;
2028 /* We do not want to ignore loop nest after frequency drops to 0. */
2029 if (!freq_scale)
2030 freq_scale = 1;
2031 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2032 if (freq > CGRAPH_FREQ_MAX)
2033 freq = CGRAPH_FREQ_MAX;
2035 if (e->indirect_unknown_callee)
2037 tree decl;
2039 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2041 struct cgraph_node *callee = cgraph_get_node (decl);
2042 gcc_checking_assert (callee);
2043 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
2045 else
2047 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2048 e->indirect_info->ecf_flags,
2049 count, freq);
2050 *new_edge->indirect_info = *e->indirect_info;
2053 else
2055 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
2056 if (e->indirect_info)
2058 new_edge->indirect_info
2059 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2060 *new_edge->indirect_info = *e->indirect_info;
2064 new_edge->inline_failed = e->inline_failed;
2065 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2066 new_edge->lto_stmt_uid = stmt_uid;
2067 /* Clone flags that depend on call_stmt availability manually. */
2068 new_edge->can_throw_external = e->can_throw_external;
2069 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2070 if (update_original)
2072 e->count -= new_edge->count;
2073 if (e->count < 0)
2074 e->count = 0;
2076 cgraph_call_edge_duplication_hooks (e, new_edge);
2077 return new_edge;
2081 /* Create node representing clone of N executed COUNT times. Decrease
2082 the execution counts from original node too.
2083 The new clone will have decl set to DECL that may or may not be the same
2084 as decl of N.
2086 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2087 function's profile to reflect the fact that part of execution is handled
2088 by node.
2089 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
2090 the new clone. Otherwise the caller is responsible for doing so later. */
2092 struct cgraph_node *
2093 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2094 bool update_original,
2095 VEC(cgraph_edge_p,heap) *redirect_callers,
2096 bool call_duplication_hook)
2098 struct cgraph_node *new_node = cgraph_create_node_1 ();
2099 struct cgraph_edge *e;
2100 gcov_type count_scale;
2101 unsigned i;
2103 new_node->decl = decl;
2104 new_node->origin = n->origin;
2105 if (new_node->origin)
2107 new_node->next_nested = new_node->origin->nested;
2108 new_node->origin->nested = new_node;
2110 new_node->analyzed = n->analyzed;
2111 new_node->local = n->local;
2112 new_node->local.externally_visible = false;
2113 new_node->local.local = true;
2114 new_node->global = n->global;
2115 new_node->rtl = n->rtl;
2116 new_node->count = count;
2117 new_node->frequency = n->frequency;
2118 new_node->clone = n->clone;
2119 new_node->clone.tree_map = 0;
2120 if (n->count)
2122 if (new_node->count > n->count)
2123 count_scale = REG_BR_PROB_BASE;
2124 else
2125 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2127 else
2128 count_scale = 0;
2129 if (update_original)
2131 n->count -= count;
2132 if (n->count < 0)
2133 n->count = 0;
2136 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2138 /* Redirect calls to the old version node to point to its new
2139 version. */
2140 cgraph_redirect_edge_callee (e, new_node);
2144 for (e = n->callees;e; e=e->next_callee)
2145 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2146 count_scale, freq, update_original);
2148 for (e = n->indirect_calls; e; e = e->next_callee)
2149 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2150 count_scale, freq, update_original);
2151 ipa_clone_references (new_node, NULL, &n->ref_list);
2153 new_node->next_sibling_clone = n->clones;
2154 if (n->clones)
2155 n->clones->prev_sibling_clone = new_node;
2156 n->clones = new_node;
2157 new_node->clone_of = n;
2159 if (n->decl != decl)
2161 struct cgraph_node **slot;
2162 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2163 gcc_assert (!*slot);
2164 *slot = new_node;
2165 if (assembler_name_hash)
2167 void **aslot;
2168 tree name = DECL_ASSEMBLER_NAME (decl);
2170 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2171 decl_assembler_name_hash (name),
2172 INSERT);
2173 gcc_assert (!*aslot);
2174 *aslot = new_node;
2178 if (call_duplication_hook)
2179 cgraph_call_node_duplication_hooks (n, new_node);
2180 return new_node;
2183 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2185 static GTY(()) unsigned int clone_fn_id_num;
2187 tree
2188 clone_function_name (tree decl, const char *suffix)
2190 tree name = DECL_ASSEMBLER_NAME (decl);
2191 size_t len = IDENTIFIER_LENGTH (name);
2192 char *tmp_name, *prefix;
2194 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2195 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2196 strcpy (prefix + len + 1, suffix);
2197 #ifndef NO_DOT_IN_LABEL
2198 prefix[len] = '.';
2199 #elif !defined NO_DOLLAR_IN_LABEL
2200 prefix[len] = '$';
2201 #else
2202 prefix[len] = '_';
2203 #endif
2204 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2205 return get_identifier (tmp_name);
2208 /* Create callgraph node clone with new declaration. The actual body will
2209 be copied later at compilation stage.
2211 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2212 bitmap interface.
2214 struct cgraph_node *
2215 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2216 VEC(cgraph_edge_p,heap) *redirect_callers,
2217 VEC(ipa_replace_map_p,gc) *tree_map,
2218 bitmap args_to_skip,
2219 const char * suffix)
2221 tree old_decl = old_node->decl;
2222 struct cgraph_node *new_node = NULL;
2223 tree new_decl;
2224 size_t i;
2225 struct ipa_replace_map *map;
2227 if (!flag_wpa)
2228 gcc_checking_assert (tree_versionable_function_p (old_decl));
2230 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2232 /* Make a new FUNCTION_DECL tree node */
2233 if (!args_to_skip)
2234 new_decl = copy_node (old_decl);
2235 else
2236 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2237 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2239 /* Generate a new name for the new version. */
2240 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2241 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2242 SET_DECL_RTL (new_decl, NULL);
2244 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2245 CGRAPH_FREQ_BASE, false,
2246 redirect_callers, false);
2247 /* Update the properties.
2248 Make clone visible only within this translation unit. Make sure
2249 that is not weak also.
2250 ??? We cannot use COMDAT linkage because there is no
2251 ABI support for this. */
2252 DECL_EXTERNAL (new_node->decl) = 0;
2253 if (DECL_ONE_ONLY (old_decl))
2254 DECL_SECTION_NAME (new_node->decl) = NULL;
2255 DECL_COMDAT_GROUP (new_node->decl) = 0;
2256 TREE_PUBLIC (new_node->decl) = 0;
2257 DECL_COMDAT (new_node->decl) = 0;
2258 DECL_WEAK (new_node->decl) = 0;
2259 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
2260 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
2261 new_node->clone.tree_map = tree_map;
2262 new_node->clone.args_to_skip = args_to_skip;
2263 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2265 tree var = map->new_tree;
2267 STRIP_NOPS (var);
2268 if (TREE_CODE (var) != ADDR_EXPR)
2269 continue;
2270 var = get_base_var (var);
2271 if (!var)
2272 continue;
2274 /* Record references of the future statement initializing the constant
2275 argument. */
2276 if (TREE_CODE (var) == FUNCTION_DECL)
2278 struct cgraph_node *ref_node = cgraph_get_node (var);
2279 gcc_checking_assert (ref_node);
2280 ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2281 NULL);
2283 else if (TREE_CODE (var) == VAR_DECL)
2284 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2285 IPA_REF_ADDR, NULL);
2287 if (!args_to_skip)
2288 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2289 else if (old_node->clone.combined_args_to_skip)
2291 int newi = 0, oldi = 0;
2292 tree arg;
2293 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2294 struct cgraph_node *orig_node;
2295 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2297 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2299 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2301 bitmap_set_bit (new_args_to_skip, oldi);
2302 continue;
2304 if (bitmap_bit_p (args_to_skip, newi))
2305 bitmap_set_bit (new_args_to_skip, oldi);
2306 newi++;
2308 new_node->clone.combined_args_to_skip = new_args_to_skip;
2310 else
2311 new_node->clone.combined_args_to_skip = args_to_skip;
2312 new_node->local.externally_visible = 0;
2313 new_node->local.local = 1;
2314 new_node->lowered = true;
2315 new_node->reachable = true;
2317 cgraph_call_node_duplication_hooks (old_node, new_node);
2320 return new_node;
2323 /* NODE is no longer nested function; update cgraph accordingly. */
2324 void
2325 cgraph_unnest_node (struct cgraph_node *node)
2327 struct cgraph_node **node2 = &node->origin->nested;
2328 gcc_assert (node->origin);
2330 while (*node2 != node)
2331 node2 = &(*node2)->next_nested;
2332 *node2 = node->next_nested;
2333 node->origin = NULL;
2336 /* Return function availability. See cgraph.h for description of individual
2337 return values. */
2338 enum availability
2339 cgraph_function_body_availability (struct cgraph_node *node)
2341 enum availability avail;
2342 gcc_assert (cgraph_function_flags_ready);
2343 if (!node->analyzed)
2344 avail = AVAIL_NOT_AVAILABLE;
2345 else if (node->local.local)
2346 avail = AVAIL_LOCAL;
2347 else if (!node->local.externally_visible)
2348 avail = AVAIL_AVAILABLE;
2349 /* Inline functions are safe to be analyzed even if their symbol can
2350 be overwritten at runtime. It is not meaningful to enforce any sane
2351 behaviour on replacing inline function by different body. */
2352 else if (DECL_DECLARED_INLINE_P (node->decl))
2353 avail = AVAIL_AVAILABLE;
2355 /* If the function can be overwritten, return OVERWRITABLE. Take
2356 care at least of two notable extensions - the COMDAT functions
2357 used to share template instantiations in C++ (this is symmetric
2358 to code cp_cannot_inline_tree_fn and probably shall be shared and
2359 the inlinability hooks completely eliminated).
2361 ??? Does the C++ one definition rule allow us to always return
2362 AVAIL_AVAILABLE here? That would be good reason to preserve this
2363 bit. */
2365 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2366 avail = AVAIL_OVERWRITABLE;
2367 else avail = AVAIL_AVAILABLE;
2369 return avail;
2372 /* Add the function FNDECL to the call graph.
2373 Unlike cgraph_finalize_function, this function is intended to be used
2374 by middle end and allows insertion of new function at arbitrary point
2375 of compilation. The function can be either in high, low or SSA form
2376 GIMPLE.
2378 The function is assumed to be reachable and have address taken (so no
2379 API breaking optimizations are performed on it).
2381 Main work done by this function is to enqueue the function for later
2382 processing to avoid need the passes to be re-entrant. */
2384 void
2385 cgraph_add_new_function (tree fndecl, bool lowered)
2387 struct cgraph_node *node;
2388 switch (cgraph_state)
2390 case CGRAPH_STATE_CONSTRUCTION:
2391 /* Just enqueue function to be processed at nearest occurrence. */
2392 node = cgraph_create_node (fndecl);
2393 node->next_needed = cgraph_new_nodes;
2394 if (lowered)
2395 node->lowered = true;
2396 cgraph_new_nodes = node;
2397 break;
2399 case CGRAPH_STATE_IPA:
2400 case CGRAPH_STATE_IPA_SSA:
2401 case CGRAPH_STATE_EXPANSION:
2402 /* Bring the function into finalized state and enqueue for later
2403 analyzing and compilation. */
2404 node = cgraph_get_create_node (fndecl);
2405 node->local.local = false;
2406 node->local.finalized = true;
2407 node->reachable = node->needed = true;
2408 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2410 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2411 current_function_decl = fndecl;
2412 gimple_register_cfg_hooks ();
2413 tree_lowering_passes (fndecl);
2414 bitmap_obstack_initialize (NULL);
2415 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2416 execute_pass_list (pass_early_local_passes.pass.sub);
2417 bitmap_obstack_release (NULL);
2418 pop_cfun ();
2419 current_function_decl = NULL;
2421 lowered = true;
2423 if (lowered)
2424 node->lowered = true;
2425 node->next_needed = cgraph_new_nodes;
2426 cgraph_new_nodes = node;
2427 break;
2429 case CGRAPH_STATE_FINISHED:
2430 /* At the very end of compilation we have to do all the work up
2431 to expansion. */
2432 node = cgraph_create_node (fndecl);
2433 if (lowered)
2434 node->lowered = true;
2435 cgraph_analyze_function (node);
2436 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2437 current_function_decl = fndecl;
2438 gimple_register_cfg_hooks ();
2439 bitmap_obstack_initialize (NULL);
2440 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2441 execute_pass_list (pass_early_local_passes.pass.sub);
2442 bitmap_obstack_release (NULL);
2443 tree_rest_of_compilation (fndecl);
2444 pop_cfun ();
2445 current_function_decl = NULL;
2446 break;
2449 /* Set a personality if required and we already passed EH lowering. */
2450 if (lowered
2451 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2452 == eh_personality_lang))
2453 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2456 /* Worker for cgraph_node_can_be_local_p. */
2457 static bool
2458 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2459 void *data ATTRIBUTE_UNUSED)
2461 return !(!node->needed
2462 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2463 || !node->local.externally_visible));
2466 /* Return true if NODE can be made local for API change.
2467 Extern inline functions and C++ COMDAT functions can be made local
2468 at the expense of possible code size growth if function is used in multiple
2469 compilation units. */
2470 bool
2471 cgraph_node_can_be_local_p (struct cgraph_node *node)
2473 return (!node->address_taken
2474 && !cgraph_for_node_and_aliases (node,
2475 cgraph_node_cannot_be_local_p_1,
2476 NULL, true));
2479 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2480 but other code such as notice_global_symbol generates rtl. */
2481 void
2482 cgraph_make_decl_local (tree decl)
2484 rtx rtl, symbol;
2486 if (TREE_CODE (decl) == VAR_DECL)
2487 DECL_COMMON (decl) = 0;
2488 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2490 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
2492 /* It is possible that we are linking against library defining same COMDAT
2493 function. To avoid conflict we need to rename our local name of the
2494 function just in the case WHOPR partitioning decide to make it hidden
2495 to avoid cross partition references. */
2496 if (flag_wpa)
2498 const char *old_name;
2500 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2501 if (TREE_CODE (decl) == FUNCTION_DECL)
2503 struct cgraph_node *node = cgraph_get_node (decl);
2504 change_decl_assembler_name (decl,
2505 clone_function_name (decl, "local"));
2506 if (node->local.lto_file_data)
2507 lto_record_renamed_decl (node->local.lto_file_data,
2508 old_name,
2509 IDENTIFIER_POINTER
2510 (DECL_ASSEMBLER_NAME (decl)));
2512 else if (TREE_CODE (decl) == VAR_DECL)
2514 struct varpool_node *vnode = varpool_get_node (decl);
2515 /* change_decl_assembler_name will warn here on vtables because
2516 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2517 SET_DECL_ASSEMBLER_NAME (decl,
2518 clone_function_name (decl, "local"));
2519 if (vnode->lto_file_data)
2520 lto_record_renamed_decl (vnode->lto_file_data,
2521 old_name,
2522 IDENTIFIER_POINTER
2523 (DECL_ASSEMBLER_NAME (decl)));
2526 DECL_SECTION_NAME (decl) = 0;
2527 DECL_COMDAT (decl) = 0;
2529 DECL_COMDAT_GROUP (decl) = 0;
2530 DECL_WEAK (decl) = 0;
2531 DECL_EXTERNAL (decl) = 0;
2532 TREE_PUBLIC (decl) = 0;
2533 if (!DECL_RTL_SET_P (decl))
2534 return;
2536 /* Update rtl flags. */
2537 make_decl_rtl (decl);
2539 rtl = DECL_RTL (decl);
2540 if (!MEM_P (rtl))
2541 return;
2543 symbol = XEXP (rtl, 0);
2544 if (GET_CODE (symbol) != SYMBOL_REF)
2545 return;
2547 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2550 /* Call calback on NODE, thunks and aliases asociated to NODE.
2551 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2552 skipped. */
2554 bool
2555 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2556 bool (*callback) (struct cgraph_node *, void *),
2557 void *data,
2558 bool include_overwritable)
2560 struct cgraph_edge *e;
2561 int i;
2562 struct ipa_ref *ref;
2564 if (callback (node, data))
2565 return true;
2566 for (e = node->callers; e; e = e->next_caller)
2567 if (e->caller->thunk.thunk_p
2568 && (include_overwritable
2569 || cgraph_function_body_availability (e->caller)))
2570 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2571 include_overwritable))
2572 return true;
2573 for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
2574 if (ref->use == IPA_REF_ALIAS)
2576 struct cgraph_node *alias = ipa_ref_refering_node (ref);
2577 if (include_overwritable
2578 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2579 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2580 include_overwritable))
2581 return true;
2583 return false;
2586 /* Call calback on NODE and aliases asociated to NODE.
2587 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2588 skipped. */
2590 bool
2591 cgraph_for_node_and_aliases (struct cgraph_node *node,
2592 bool (*callback) (struct cgraph_node *, void *),
2593 void *data,
2594 bool include_overwritable)
2596 int i;
2597 struct ipa_ref *ref;
2599 if (callback (node, data))
2600 return true;
2601 for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
2602 if (ref->use == IPA_REF_ALIAS)
2604 struct cgraph_node *alias = ipa_ref_refering_node (ref);
2605 if (include_overwritable
2606 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2607 if (cgraph_for_node_and_aliases (alias, callback, data,
2608 include_overwritable))
2609 return true;
2611 return false;
2614 /* Worker to bring NODE local. */
2616 static bool
2617 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2619 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2620 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2622 cgraph_make_decl_local (node->decl);
2624 node->local.externally_visible = false;
2625 node->local.local = true;
2626 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2627 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2629 return false;
2632 /* Bring NODE local. */
2634 void
2635 cgraph_make_node_local (struct cgraph_node *node)
2637 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2638 NULL, true);
2641 /* Worker to set nothrow flag. */
2643 static bool
2644 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2646 struct cgraph_edge *e;
2648 TREE_NOTHROW (node->decl) = data != NULL;
2650 if (data != NULL)
2651 for (e = node->callers; e; e = e->next_caller)
2652 e->can_throw_external = false;
2653 return false;
2656 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2657 if any to NOTHROW. */
2659 void
2660 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2662 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2663 (void *)(size_t)nothrow, false);
2666 /* Worker to set const flag. */
2668 static bool
2669 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2671 /* Static constructors and destructors without a side effect can be
2672 optimized out. */
2673 if (data && !((size_t)data & 2))
2675 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2676 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2677 if (DECL_STATIC_DESTRUCTOR (node->decl))
2678 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2680 TREE_READONLY (node->decl) = data != NULL;
2681 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2682 return false;
2685 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2686 if any to READONLY. */
2688 void
2689 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2691 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2692 (void *)(size_t)(readonly + (int)looping * 2),
2693 false);
2696 /* Worker to set pure flag. */
2698 static bool
2699 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2701 /* Static pureructors and destructors without a side effect can be
2702 optimized out. */
2703 if (data && !((size_t)data & 2))
2705 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2706 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2707 if (DECL_STATIC_DESTRUCTOR (node->decl))
2708 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2710 DECL_PURE_P (node->decl) = data != NULL;
2711 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2712 return false;
2715 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2716 if any to PURE. */
2718 void
2719 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2721 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2722 (void *)(size_t)(pure + (int)looping * 2),
2723 false);
2726 /* Data used by cgraph_propagate_frequency. */
2728 struct cgraph_propagate_frequency_data
2730 bool maybe_unlikely_executed;
2731 bool maybe_executed_once;
2732 bool only_called_at_startup;
2733 bool only_called_at_exit;
2736 /* Worker for cgraph_propagate_frequency_1. */
2738 static bool
2739 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
2741 struct cgraph_propagate_frequency_data *d;
2742 struct cgraph_edge *edge;
2744 d = (struct cgraph_propagate_frequency_data *)data;
2745 for (edge = node->callers;
2746 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
2747 || d->only_called_at_startup || d->only_called_at_exit);
2748 edge = edge->next_caller)
2750 if (edge->caller != node)
2752 d->only_called_at_startup &= edge->caller->only_called_at_startup;
2753 /* It makes sense to put main() together with the static constructors.
2754 It will be executed for sure, but rest of functions called from
2755 main are definitely not at startup only. */
2756 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2757 d->only_called_at_startup = 0;
2758 d->only_called_at_exit &= edge->caller->only_called_at_exit;
2760 if (!edge->frequency)
2761 continue;
2762 switch (edge->caller->frequency)
2764 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2765 break;
2766 case NODE_FREQUENCY_EXECUTED_ONCE:
2767 if (dump_file && (dump_flags & TDF_DETAILS))
2768 fprintf (dump_file, " Called by %s that is executed once\n",
2769 cgraph_node_name (edge->caller));
2770 d->maybe_unlikely_executed = false;
2771 if (inline_edge_summary (edge)->loop_depth)
2773 d->maybe_executed_once = false;
2774 if (dump_file && (dump_flags & TDF_DETAILS))
2775 fprintf (dump_file, " Called in loop\n");
2777 break;
2778 case NODE_FREQUENCY_HOT:
2779 case NODE_FREQUENCY_NORMAL:
2780 if (dump_file && (dump_flags & TDF_DETAILS))
2781 fprintf (dump_file, " Called by %s that is normal or hot\n",
2782 cgraph_node_name (edge->caller));
2783 d->maybe_unlikely_executed = false;
2784 d->maybe_executed_once = false;
2785 break;
2788 return edge != NULL;
2791 /* See if the frequency of NODE can be updated based on frequencies of its
2792 callers. */
2793 bool
2794 cgraph_propagate_frequency (struct cgraph_node *node)
2796 struct cgraph_propagate_frequency_data d = {true, true, true, true};
2797 bool changed = false;
2799 if (!node->local.local)
2800 return false;
2801 gcc_assert (node->analyzed);
2802 if (dump_file && (dump_flags & TDF_DETAILS))
2803 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2805 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2807 if ((d.only_called_at_startup && !d.only_called_at_exit)
2808 && !node->only_called_at_startup)
2810 node->only_called_at_startup = true;
2811 if (dump_file)
2812 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2813 cgraph_node_name (node));
2814 changed = true;
2816 if ((d.only_called_at_exit && !d.only_called_at_startup)
2817 && !node->only_called_at_exit)
2819 node->only_called_at_exit = true;
2820 if (dump_file)
2821 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2822 cgraph_node_name (node));
2823 changed = true;
2825 /* These come either from profile or user hints; never update them. */
2826 if (node->frequency == NODE_FREQUENCY_HOT
2827 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2828 return changed;
2829 if (d.maybe_unlikely_executed)
2831 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2832 if (dump_file)
2833 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2834 cgraph_node_name (node));
2835 changed = true;
2837 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2839 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2840 if (dump_file)
2841 fprintf (dump_file, "Node %s promoted to executed once.\n",
2842 cgraph_node_name (node));
2843 changed = true;
2845 return changed;
2848 /* Return true when NODE can not return or throw and thus
2849 it is safe to ignore its side effects for IPA analysis. */
2851 bool
2852 cgraph_node_cannot_return (struct cgraph_node *node)
2854 int flags = flags_from_decl_or_type (node->decl);
2855 if (!flag_exceptions)
2856 return (flags & ECF_NORETURN) != 0;
2857 else
2858 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2859 == (ECF_NORETURN | ECF_NOTHROW));
2862 /* Return true when call of E can not lead to return from caller
2863 and thus it is safe to ignore its side effects for IPA analysis
2864 when computing side effects of the caller.
2865 FIXME: We could actually mark all edges that have no reaching
2866 patch to EXIT_BLOCK_PTR or throw to get better results. */
2867 bool
2868 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2870 if (cgraph_node_cannot_return (e->caller))
2871 return true;
2872 if (e->indirect_unknown_callee)
2874 int flags = e->indirect_info->ecf_flags;
2875 if (!flag_exceptions)
2876 return (flags & ECF_NORETURN) != 0;
2877 else
2878 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2879 == (ECF_NORETURN | ECF_NOTHROW));
2881 else
2882 return cgraph_node_cannot_return (e->callee);
2885 /* Return true when function NODE can be removed from callgraph
2886 if all direct calls are eliminated. */
2888 bool
2889 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2891 gcc_assert (!node->global.inlined_to);
2892 /* Extern inlines can always go, we will use the external definition. */
2893 if (DECL_EXTERNAL (node->decl))
2894 return true;
2895 /* When function is needed, we can not remove it. */
2896 if (node->needed || node->reachable_from_other_partition)
2897 return false;
2898 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2899 || DECL_STATIC_DESTRUCTOR (node->decl))
2900 return false;
2901 /* Only COMDAT functions can be removed if externally visible. */
2902 if (node->local.externally_visible
2903 && (!DECL_COMDAT (node->decl)
2904 || cgraph_used_from_object_file_p (node)))
2905 return false;
2906 return true;
2909 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2911 static bool
2912 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2914 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2917 /* Return true when function NODE and its aliases can be removed from callgraph
2918 if all direct calls are eliminated. */
2920 bool
2921 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2923 /* Extern inlines can always go, we will use the external definition. */
2924 if (DECL_EXTERNAL (node->decl))
2925 return true;
2926 if (node->address_taken)
2927 return false;
2928 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2931 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2933 static bool
2934 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2936 return cgraph_used_from_object_file_p (node);
2939 /* Return true when function NODE can be expected to be removed
2940 from program when direct calls in this compilation unit are removed.
2942 As a special case COMDAT functions are
2943 cgraph_can_remove_if_no_direct_calls_p while the are not
2944 cgraph_only_called_directly_p (it is possible they are called from other
2945 unit)
2947 This function behaves as cgraph_only_called_directly_p because eliminating
2948 all uses of COMDAT function does not make it necessarily disappear from
2949 the program unless we are compiling whole program or we do LTO. In this
2950 case we know we win since dynamic linking will not really discard the
2951 linkonce section. */
2953 bool
2954 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2956 gcc_assert (!node->global.inlined_to);
2957 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2958 return false;
2959 if (!in_lto_p && !flag_whole_program)
2960 return cgraph_only_called_directly_p (node);
2961 else
2963 if (DECL_EXTERNAL (node->decl))
2964 return true;
2965 return cgraph_can_remove_if_no_direct_calls_p (node);
2969 /* Return true when RESOLUTION indicate that linker will use
2970 the symbol from non-LTO object files. */
2972 bool
2973 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2975 return (resolution == LDPR_PREVAILING_DEF
2976 || resolution == LDPR_PREEMPTED_REG
2977 || resolution == LDPR_RESOLVED_EXEC
2978 || resolution == LDPR_RESOLVED_DYN);
2982 /* Return true when NODE is known to be used from other (non-LTO) object file.
2983 Known only when doing LTO via linker plugin. */
2985 bool
2986 cgraph_used_from_object_file_p (struct cgraph_node *node)
2988 gcc_assert (!node->global.inlined_to);
2989 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2990 return false;
2991 if (resolution_used_from_other_file_p (node->resolution))
2992 return true;
2993 return false;
2996 /* Worker for cgraph_only_called_directly_p. */
2998 static bool
2999 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3001 return !cgraph_only_called_directly_or_aliased_p (node);
3004 /* Return true when function NODE and all its aliases are only called
3005 directly.
3006 i.e. it is not externally visible, address was not taken and
3007 it is not used in any other non-standard way. */
3009 bool
3010 cgraph_only_called_directly_p (struct cgraph_node *node)
3012 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
3013 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
3014 NULL, true);
3018 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3020 static bool
3021 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
3023 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
3024 struct cgraph_edge *cs;
3025 enum availability avail;
3026 cgraph_function_or_thunk_node (node, &avail);
3028 if (avail > AVAIL_OVERWRITABLE)
3029 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3030 if (!cs->indirect_inlining_edge)
3031 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
3032 return false;
3035 /* Collect all callers of NODE and its aliases that are known to lead to NODE
3036 (i.e. are not overwritable). */
3038 VEC (cgraph_edge_p, heap) *
3039 collect_callers_of_node (struct cgraph_node *node)
3041 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
3042 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
3043 &redirect_callers, false);
3044 return redirect_callers;
3047 #include "gt-cgraph.h"