2013-07-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cgraph.c
blobc6ade31041884cebe775565efe3e8c26d179672e
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "tree-inline.h"
32 #include "langhooks.h"
33 #include "hashtab.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "debug.h"
38 #include "target.h"
39 #include "basic-block.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "gimple.h"
43 #include "timevar.h"
44 #include "dumpfile.h"
45 #include "tree-flow.h"
46 #include "value-prof.h"
47 #include "except.h"
48 #include "diagnostic-core.h"
49 #include "rtl.h"
50 #include "ipa-utils.h"
51 #include "lto-streamer.h"
52 #include "ipa-inline.h"
53 #include "cfgloop.h"
54 #include "gimple-pretty-print.h"
56 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
57 #include "tree-pass.h"
59 static void cgraph_node_remove_callers (struct cgraph_node *node);
60 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
61 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
63 /* Queue of cgraph nodes scheduled to be lowered. */
64 symtab_node x_cgraph_nodes_queue;
65 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
67 /* Number of nodes in existence. */
68 int cgraph_n_nodes;
70 /* Maximal uid used in cgraph nodes. */
71 int cgraph_max_uid;
73 /* Maximal uid used in cgraph edges. */
74 int cgraph_edge_max_uid;
76 /* Set when whole unit has been analyzed so we can access global info. */
77 bool cgraph_global_info_ready = false;
79 /* What state callgraph is in right now. */
80 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
82 /* Set when the cgraph is fully build and the basic flags are computed. */
83 bool cgraph_function_flags_ready = false;
85 /* List of hooks triggered on cgraph_edge events. */
86 struct cgraph_edge_hook_list {
87 cgraph_edge_hook hook;
88 void *data;
89 struct cgraph_edge_hook_list *next;
92 /* List of hooks triggered on cgraph_node events. */
93 struct cgraph_node_hook_list {
94 cgraph_node_hook hook;
95 void *data;
96 struct cgraph_node_hook_list *next;
99 /* List of hooks triggered on events involving two cgraph_edges. */
100 struct cgraph_2edge_hook_list {
101 cgraph_2edge_hook hook;
102 void *data;
103 struct cgraph_2edge_hook_list *next;
106 /* List of hooks triggered on events involving two cgraph_nodes. */
107 struct cgraph_2node_hook_list {
108 cgraph_2node_hook hook;
109 void *data;
110 struct cgraph_2node_hook_list *next;
113 /* List of hooks triggered when an edge is removed. */
114 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
115 /* List of hooks triggered when a node is removed. */
116 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
117 /* List of hooks triggered when an edge is duplicated. */
118 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
119 /* List of hooks triggered when a node is duplicated. */
120 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
121 /* List of hooks triggered when an function is inserted. */
122 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
124 /* Head of a linked list of unused (freed) call graph nodes.
125 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
126 static GTY(()) struct cgraph_node *free_nodes;
127 /* Head of a linked list of unused (freed) call graph edges.
128 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
129 static GTY(()) struct cgraph_edge *free_edges;
131 /* Did procss_same_body_aliases run? */
132 bool cpp_implicit_aliases_done;
134 /* Map a cgraph_node to cgraph_function_version_info using this htab.
135 The cgraph_function_version_info has a THIS_NODE field that is the
136 corresponding cgraph_node.. */
138 static htab_t GTY((param_is (struct cgraph_function_version_info *)))
139 cgraph_fnver_htab = NULL;
141 /* Hash function for cgraph_fnver_htab. */
142 static hashval_t
143 cgraph_fnver_htab_hash (const void *ptr)
145 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
146 return (hashval_t)(uid);
149 /* eq function for cgraph_fnver_htab. */
150 static int
151 cgraph_fnver_htab_eq (const void *p1, const void *p2)
153 const struct cgraph_function_version_info *n1
154 = (const struct cgraph_function_version_info *)p1;
155 const struct cgraph_function_version_info *n2
156 = (const struct cgraph_function_version_info *)p2;
158 return n1->this_node->uid == n2->this_node->uid;
161 /* Mark as GC root all allocated nodes. */
162 static GTY(()) struct cgraph_function_version_info *
163 version_info_node = NULL;
165 /* Get the cgraph_function_version_info node corresponding to node. */
166 struct cgraph_function_version_info *
167 get_cgraph_node_version (struct cgraph_node *node)
169 struct cgraph_function_version_info *ret;
170 struct cgraph_function_version_info key;
171 key.this_node = node;
173 if (cgraph_fnver_htab == NULL)
174 return NULL;
176 ret = (struct cgraph_function_version_info *)
177 htab_find (cgraph_fnver_htab, &key);
179 return ret;
182 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
183 corresponding to cgraph_node NODE. */
184 struct cgraph_function_version_info *
185 insert_new_cgraph_node_version (struct cgraph_node *node)
187 void **slot;
189 version_info_node = NULL;
190 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
191 version_info_node->this_node = node;
193 if (cgraph_fnver_htab == NULL)
194 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
195 cgraph_fnver_htab_eq, NULL);
197 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
198 gcc_assert (slot != NULL);
199 *slot = version_info_node;
200 return version_info_node;
203 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
204 DECL is a duplicate declaration. */
205 void
206 delete_function_version (tree decl)
208 struct cgraph_node *decl_node = cgraph_get_node (decl);
209 struct cgraph_function_version_info *decl_v = NULL;
211 if (decl_node == NULL)
212 return;
214 decl_v = get_cgraph_node_version (decl_node);
216 if (decl_v == NULL)
217 return;
219 if (decl_v->prev != NULL)
220 decl_v->prev->next = decl_v->next;
222 if (decl_v->next != NULL)
223 decl_v->next->prev = decl_v->prev;
225 if (cgraph_fnver_htab != NULL)
226 htab_remove_elt (cgraph_fnver_htab, decl_v);
228 cgraph_remove_node (decl_node);
231 /* Record that DECL1 and DECL2 are semantically identical function
232 versions. */
233 void
234 record_function_versions (tree decl1, tree decl2)
236 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
237 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
238 struct cgraph_function_version_info *decl1_v = NULL;
239 struct cgraph_function_version_info *decl2_v = NULL;
240 struct cgraph_function_version_info *before;
241 struct cgraph_function_version_info *after;
243 gcc_assert (decl1_node != NULL && decl2_node != NULL);
244 decl1_v = get_cgraph_node_version (decl1_node);
245 decl2_v = get_cgraph_node_version (decl2_node);
247 if (decl1_v != NULL && decl2_v != NULL)
248 return;
250 if (decl1_v == NULL)
251 decl1_v = insert_new_cgraph_node_version (decl1_node);
253 if (decl2_v == NULL)
254 decl2_v = insert_new_cgraph_node_version (decl2_node);
256 /* Chain decl2_v and decl1_v. All semantically identical versions
257 will be chained together. */
259 before = decl1_v;
260 after = decl2_v;
262 while (before->next != NULL)
263 before = before->next;
265 while (after->prev != NULL)
266 after= after->prev;
268 before->next = after;
269 after->prev = before;
272 /* Macros to access the next item in the list of free cgraph nodes and
273 edges. */
274 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
275 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
276 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
278 /* Register HOOK to be called with DATA on each removed edge. */
279 struct cgraph_edge_hook_list *
280 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
282 struct cgraph_edge_hook_list *entry;
283 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
285 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
286 entry->hook = hook;
287 entry->data = data;
288 entry->next = NULL;
289 while (*ptr)
290 ptr = &(*ptr)->next;
291 *ptr = entry;
292 return entry;
295 /* Remove ENTRY from the list of hooks called on removing edges. */
296 void
297 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
299 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
301 while (*ptr != entry)
302 ptr = &(*ptr)->next;
303 *ptr = entry->next;
304 free (entry);
307 /* Call all edge removal hooks. */
308 static void
309 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
311 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
312 while (entry)
314 entry->hook (e, entry->data);
315 entry = entry->next;
319 /* Register HOOK to be called with DATA on each removed node. */
320 struct cgraph_node_hook_list *
321 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
323 struct cgraph_node_hook_list *entry;
324 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
326 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
327 entry->hook = hook;
328 entry->data = data;
329 entry->next = NULL;
330 while (*ptr)
331 ptr = &(*ptr)->next;
332 *ptr = entry;
333 return entry;
336 /* Remove ENTRY from the list of hooks called on removing nodes. */
337 void
338 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
340 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
342 while (*ptr != entry)
343 ptr = &(*ptr)->next;
344 *ptr = entry->next;
345 free (entry);
348 /* Call all node removal hooks. */
349 static void
350 cgraph_call_node_removal_hooks (struct cgraph_node *node)
352 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
353 while (entry)
355 entry->hook (node, entry->data);
356 entry = entry->next;
360 /* Register HOOK to be called with DATA on each inserted node. */
361 struct cgraph_node_hook_list *
362 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
364 struct cgraph_node_hook_list *entry;
365 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
367 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
368 entry->hook = hook;
369 entry->data = data;
370 entry->next = NULL;
371 while (*ptr)
372 ptr = &(*ptr)->next;
373 *ptr = entry;
374 return entry;
377 /* Remove ENTRY from the list of hooks called on inserted nodes. */
378 void
379 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
381 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
383 while (*ptr != entry)
384 ptr = &(*ptr)->next;
385 *ptr = entry->next;
386 free (entry);
389 /* Call all node insertion hooks. */
390 void
391 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
393 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
394 while (entry)
396 entry->hook (node, entry->data);
397 entry = entry->next;
401 /* Register HOOK to be called with DATA on each duplicated edge. */
402 struct cgraph_2edge_hook_list *
403 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
405 struct cgraph_2edge_hook_list *entry;
406 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
408 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
409 entry->hook = hook;
410 entry->data = data;
411 entry->next = NULL;
412 while (*ptr)
413 ptr = &(*ptr)->next;
414 *ptr = entry;
415 return entry;
418 /* Remove ENTRY from the list of hooks called on duplicating edges. */
419 void
420 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
422 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
424 while (*ptr != entry)
425 ptr = &(*ptr)->next;
426 *ptr = entry->next;
427 free (entry);
430 /* Call all edge duplication hooks. */
431 void
432 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
433 struct cgraph_edge *cs2)
435 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
436 while (entry)
438 entry->hook (cs1, cs2, entry->data);
439 entry = entry->next;
443 /* Register HOOK to be called with DATA on each duplicated node. */
444 struct cgraph_2node_hook_list *
445 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
447 struct cgraph_2node_hook_list *entry;
448 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
450 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
451 entry->hook = hook;
452 entry->data = data;
453 entry->next = NULL;
454 while (*ptr)
455 ptr = &(*ptr)->next;
456 *ptr = entry;
457 return entry;
460 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
461 void
462 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
464 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
466 while (*ptr != entry)
467 ptr = &(*ptr)->next;
468 *ptr = entry->next;
469 free (entry);
472 /* Call all node duplication hooks. */
473 void
474 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
475 struct cgraph_node *node2)
477 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
478 while (entry)
480 entry->hook (node1, node2, entry->data);
481 entry = entry->next;
485 /* Allocate new callgraph node. */
487 static inline struct cgraph_node *
488 cgraph_allocate_node (void)
490 struct cgraph_node *node;
492 if (free_nodes)
494 node = free_nodes;
495 free_nodes = NEXT_FREE_NODE (node);
497 else
499 node = ggc_alloc_cleared_cgraph_node ();
500 node->uid = cgraph_max_uid++;
503 return node;
506 /* Allocate new callgraph node and insert it into basic data structures. */
508 struct cgraph_node *
509 cgraph_create_empty_node (void)
511 struct cgraph_node *node = cgraph_allocate_node ();
513 node->symbol.type = SYMTAB_FUNCTION;
514 node->frequency = NODE_FREQUENCY_NORMAL;
515 node->count_materialization_scale = REG_BR_PROB_BASE;
516 cgraph_n_nodes++;
517 return node;
520 /* Return cgraph node assigned to DECL. Create new one when needed. */
522 struct cgraph_node *
523 cgraph_create_node (tree decl)
525 struct cgraph_node *node = cgraph_create_empty_node ();
526 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
528 node->symbol.decl = decl;
529 symtab_register_node ((symtab_node) node);
531 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
533 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
534 node->next_nested = node->origin->nested;
535 node->origin->nested = node;
537 return node;
540 /* Try to find a call graph node for declaration DECL and if it does not exist,
541 create it. */
543 struct cgraph_node *
544 cgraph_get_create_node (tree decl)
546 struct cgraph_node *node;
548 node = cgraph_get_node (decl);
549 if (node)
550 return node;
552 return cgraph_create_node (decl);
555 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
556 the function body is associated with (not necessarily cgraph_node (DECL). */
558 struct cgraph_node *
559 cgraph_create_function_alias (tree alias, tree target)
561 struct cgraph_node *alias_node;
563 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
564 || TREE_CODE (target) == IDENTIFIER_NODE);
565 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
566 alias_node = cgraph_get_create_node (alias);
567 gcc_assert (!alias_node->symbol.definition);
568 alias_node->symbol.alias_target = target;
569 alias_node->symbol.definition = true;
570 alias_node->symbol.alias = true;
571 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
572 alias_node->symbol.weakref = true;
573 return alias_node;
576 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
577 and NULL otherwise.
578 Same body aliases are output whenever the body of DECL is output,
579 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
581 struct cgraph_node *
582 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
584 struct cgraph_node *n;
585 #ifndef ASM_OUTPUT_DEF
586 /* If aliases aren't supported by the assembler, fail. */
587 return NULL;
588 #endif
589 /* Langhooks can create same body aliases of symbols not defined.
590 Those are useless. Drop them on the floor. */
591 if (cgraph_global_info_ready)
592 return NULL;
594 n = cgraph_create_function_alias (alias, decl);
595 n->symbol.cpp_implicit_alias = true;
596 if (cpp_implicit_aliases_done)
597 symtab_resolve_alias ((symtab_node)n,
598 (symtab_node)cgraph_get_node (decl));
599 return n;
602 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
603 aliases DECL with an adjustments made into the first parameter.
604 See comments in thunk_adjust for detail on the parameters. */
606 struct cgraph_node *
607 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
608 tree alias, tree decl ATTRIBUTE_UNUSED,
609 bool this_adjusting,
610 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
611 tree virtual_offset,
612 tree real_alias)
614 struct cgraph_node *node;
616 node = cgraph_get_node (alias);
617 if (node)
619 gcc_assert (node->symbol.definition);
620 gcc_assert (!node->symbol.alias);
621 gcc_assert (!node->thunk.thunk_p);
622 cgraph_remove_node (node);
625 node = cgraph_create_node (alias);
626 gcc_checking_assert (!virtual_offset
627 || tree_to_double_int (virtual_offset) ==
628 double_int::from_shwi (virtual_value));
629 node->thunk.fixed_offset = fixed_offset;
630 node->thunk.this_adjusting = this_adjusting;
631 node->thunk.virtual_value = virtual_value;
632 node->thunk.virtual_offset_p = virtual_offset != NULL;
633 node->thunk.alias = real_alias;
634 node->thunk.thunk_p = true;
635 node->symbol.definition = true;
637 return node;
640 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
641 Return NULL if there's no such node. */
643 struct cgraph_node *
644 cgraph_node_for_asm (tree asmname)
646 /* We do not want to look at inline clones. */
647 for (symtab_node node = symtab_node_for_asm (asmname);
648 node;
649 node = node->symbol.next_sharing_asm_name)
651 cgraph_node *cn = dyn_cast <cgraph_node> (node);
652 if (cn && !cn->global.inlined_to)
653 return cn;
655 return NULL;
658 /* Returns a hash value for X (which really is a cgraph_edge). */
660 static hashval_t
661 edge_hash (const void *x)
663 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
666 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
668 static int
669 edge_eq (const void *x, const void *y)
671 return ((const struct cgraph_edge *) x)->call_stmt == y;
674 /* Add call graph edge E to call site hash of its caller. */
676 static inline void
677 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
679 void **slot;
680 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
681 e->call_stmt,
682 htab_hash_pointer (e->call_stmt),
683 INSERT);
684 gcc_assert (!*slot);
685 *slot = e;
688 /* Return the callgraph edge representing the GIMPLE_CALL statement
689 CALL_STMT. */
691 struct cgraph_edge *
692 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
694 struct cgraph_edge *e, *e2;
695 int n = 0;
697 if (node->call_site_hash)
698 return (struct cgraph_edge *)
699 htab_find_with_hash (node->call_site_hash, call_stmt,
700 htab_hash_pointer (call_stmt));
702 /* This loop may turn out to be performance problem. In such case adding
703 hashtables into call nodes with very many edges is probably best
704 solution. It is not good idea to add pointer into CALL_EXPR itself
705 because we want to make possible having multiple cgraph nodes representing
706 different clones of the same body before the body is actually cloned. */
707 for (e = node->callees; e; e = e->next_callee)
709 if (e->call_stmt == call_stmt)
710 break;
711 n++;
714 if (!e)
715 for (e = node->indirect_calls; e; e = e->next_callee)
717 if (e->call_stmt == call_stmt)
718 break;
719 n++;
722 if (n > 100)
724 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
725 for (e2 = node->callees; e2; e2 = e2->next_callee)
726 cgraph_add_edge_to_call_site_hash (e2);
727 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
728 cgraph_add_edge_to_call_site_hash (e2);
731 return e;
735 /* Change field call_stmt of edge E to NEW_STMT. */
737 void
738 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
740 tree decl;
742 if (e->caller->call_site_hash)
744 htab_remove_elt_with_hash (e->caller->call_site_hash,
745 e->call_stmt,
746 htab_hash_pointer (e->call_stmt));
749 e->call_stmt = new_stmt;
750 if (e->indirect_unknown_callee
751 && (decl = gimple_call_fndecl (new_stmt)))
753 /* Constant propagation (and possibly also inlining?) can turn an
754 indirect call into a direct one. */
755 struct cgraph_node *new_callee = cgraph_get_node (decl);
757 gcc_checking_assert (new_callee);
758 cgraph_make_edge_direct (e, new_callee);
761 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
762 e->can_throw_external = stmt_can_throw_external (new_stmt);
763 pop_cfun ();
764 if (e->caller->call_site_hash)
765 cgraph_add_edge_to_call_site_hash (e);
768 /* Allocate a cgraph_edge structure and fill it with data according to the
769 parameters of which only CALLEE can be NULL (when creating an indirect call
770 edge). */
772 static struct cgraph_edge *
773 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
774 gimple call_stmt, gcov_type count, int freq)
776 struct cgraph_edge *edge;
778 /* LTO does not actually have access to the call_stmt since these
779 have not been loaded yet. */
780 if (call_stmt)
782 /* This is a rather expensive check possibly triggering
783 construction of call stmt hashtable. */
784 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
786 gcc_assert (is_gimple_call (call_stmt));
789 if (free_edges)
791 edge = free_edges;
792 free_edges = NEXT_FREE_EDGE (edge);
794 else
796 edge = ggc_alloc_cgraph_edge ();
797 edge->uid = cgraph_edge_max_uid++;
800 edge->aux = NULL;
801 edge->caller = caller;
802 edge->callee = callee;
803 edge->prev_caller = NULL;
804 edge->next_caller = NULL;
805 edge->prev_callee = NULL;
806 edge->next_callee = NULL;
808 edge->count = count;
809 gcc_assert (count >= 0);
810 edge->frequency = freq;
811 gcc_assert (freq >= 0);
812 gcc_assert (freq <= CGRAPH_FREQ_MAX);
814 edge->call_stmt = call_stmt;
815 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
816 edge->can_throw_external
817 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
818 pop_cfun ();
819 if (call_stmt
820 && callee && callee->symbol.decl
821 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl,
822 false))
823 edge->call_stmt_cannot_inline_p = true;
824 else
825 edge->call_stmt_cannot_inline_p = false;
826 if (call_stmt && caller->call_site_hash)
827 cgraph_add_edge_to_call_site_hash (edge);
829 edge->indirect_info = NULL;
830 edge->indirect_inlining_edge = 0;
832 return edge;
835 /* Create edge from CALLER to CALLEE in the cgraph. */
837 struct cgraph_edge *
838 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
839 gimple call_stmt, gcov_type count, int freq)
841 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
842 count, freq);
844 edge->indirect_unknown_callee = 0;
845 initialize_inline_failed (edge);
847 edge->next_caller = callee->callers;
848 if (callee->callers)
849 callee->callers->prev_caller = edge;
850 edge->next_callee = caller->callees;
851 if (caller->callees)
852 caller->callees->prev_callee = edge;
853 caller->callees = edge;
854 callee->callers = edge;
856 return edge;
859 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
861 struct cgraph_indirect_call_info *
862 cgraph_allocate_init_indirect_info (void)
864 struct cgraph_indirect_call_info *ii;
866 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
867 ii->param_index = -1;
868 return ii;
871 /* Create an indirect edge with a yet-undetermined callee where the call
872 statement destination is a formal parameter of the caller with index
873 PARAM_INDEX. */
875 struct cgraph_edge *
876 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
877 int ecf_flags,
878 gcov_type count, int freq)
880 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
881 count, freq);
883 edge->indirect_unknown_callee = 1;
884 initialize_inline_failed (edge);
886 edge->indirect_info = cgraph_allocate_init_indirect_info ();
887 edge->indirect_info->ecf_flags = ecf_flags;
889 edge->next_callee = caller->indirect_calls;
890 if (caller->indirect_calls)
891 caller->indirect_calls->prev_callee = edge;
892 caller->indirect_calls = edge;
894 return edge;
897 /* Remove the edge E from the list of the callers of the callee. */
899 static inline void
900 cgraph_edge_remove_callee (struct cgraph_edge *e)
902 gcc_assert (!e->indirect_unknown_callee);
903 if (e->prev_caller)
904 e->prev_caller->next_caller = e->next_caller;
905 if (e->next_caller)
906 e->next_caller->prev_caller = e->prev_caller;
907 if (!e->prev_caller)
908 e->callee->callers = e->next_caller;
911 /* Remove the edge E from the list of the callees of the caller. */
913 static inline void
914 cgraph_edge_remove_caller (struct cgraph_edge *e)
916 if (e->prev_callee)
917 e->prev_callee->next_callee = e->next_callee;
918 if (e->next_callee)
919 e->next_callee->prev_callee = e->prev_callee;
920 if (!e->prev_callee)
922 if (e->indirect_unknown_callee)
923 e->caller->indirect_calls = e->next_callee;
924 else
925 e->caller->callees = e->next_callee;
927 if (e->caller->call_site_hash)
928 htab_remove_elt_with_hash (e->caller->call_site_hash,
929 e->call_stmt,
930 htab_hash_pointer (e->call_stmt));
933 /* Put the edge onto the free list. */
935 static void
936 cgraph_free_edge (struct cgraph_edge *e)
938 int uid = e->uid;
940 /* Clear out the edge so we do not dangle pointers. */
941 memset (e, 0, sizeof (*e));
942 e->uid = uid;
943 NEXT_FREE_EDGE (e) = free_edges;
944 free_edges = e;
947 /* Remove the edge E in the cgraph. */
949 void
950 cgraph_remove_edge (struct cgraph_edge *e)
952 /* Call all edge removal hooks. */
953 cgraph_call_edge_removal_hooks (e);
955 if (!e->indirect_unknown_callee)
956 /* Remove from callers list of the callee. */
957 cgraph_edge_remove_callee (e);
959 /* Remove from callees list of the callers. */
960 cgraph_edge_remove_caller (e);
962 /* Put the edge onto the free list. */
963 cgraph_free_edge (e);
966 /* Set callee of call graph edge E and add it to the corresponding set of
967 callers. */
969 static void
970 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
972 e->prev_caller = NULL;
973 if (n->callers)
974 n->callers->prev_caller = e;
975 e->next_caller = n->callers;
976 n->callers = e;
977 e->callee = n;
980 /* Redirect callee of E to N. The function does not update underlying
981 call expression. */
983 void
984 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
986 /* Remove from callers list of the current callee. */
987 cgraph_edge_remove_callee (e);
989 /* Insert to callers list of the new callee. */
990 cgraph_set_edge_callee (e, n);
993 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
994 CALLEE. DELTA is an integer constant that is to be added to the this
995 pointer (first parameter) to compensate for skipping a thunk adjustment. */
997 void
998 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1000 edge->indirect_unknown_callee = 0;
1002 /* Get the edge out of the indirect edge list. */
1003 if (edge->prev_callee)
1004 edge->prev_callee->next_callee = edge->next_callee;
1005 if (edge->next_callee)
1006 edge->next_callee->prev_callee = edge->prev_callee;
1007 if (!edge->prev_callee)
1008 edge->caller->indirect_calls = edge->next_callee;
1010 /* Put it into the normal callee list */
1011 edge->prev_callee = NULL;
1012 edge->next_callee = edge->caller->callees;
1013 if (edge->caller->callees)
1014 edge->caller->callees->prev_callee = edge;
1015 edge->caller->callees = edge;
1017 /* Insert to callers list of the new callee. */
1018 cgraph_set_edge_callee (edge, callee);
1020 if (edge->call_stmt)
1021 edge->call_stmt_cannot_inline_p
1022 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl,
1023 false);
1025 /* We need to re-determine the inlining status of the edge. */
1026 initialize_inline_failed (edge);
1029 /* If necessary, change the function declaration in the call statement
1030 associated with E so that it corresponds to the edge callee. */
1032 gimple
1033 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1035 tree decl = gimple_call_fndecl (e->call_stmt);
1036 gimple new_stmt;
1037 gimple_stmt_iterator gsi;
1038 #ifdef ENABLE_CHECKING
1039 struct cgraph_node *node;
1040 #endif
1042 if (e->indirect_unknown_callee
1043 || decl == e->callee->symbol.decl)
1044 return e->call_stmt;
1046 #ifdef ENABLE_CHECKING
1047 if (decl)
1049 node = cgraph_get_node (decl);
1050 gcc_assert (!node || !node->clone.combined_args_to_skip);
1052 #endif
1054 if (cgraph_dump_file)
1056 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1057 xstrdup (cgraph_node_name (e->caller)), e->caller->symbol.order,
1058 xstrdup (cgraph_node_name (e->callee)), e->callee->symbol.order);
1059 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1060 if (e->callee->clone.combined_args_to_skip)
1062 fprintf (cgraph_dump_file, " combined args to skip: ");
1063 dump_bitmap (cgraph_dump_file,
1064 e->callee->clone.combined_args_to_skip);
1068 if (e->callee->clone.combined_args_to_skip)
1070 int lp_nr;
1072 new_stmt
1073 = gimple_call_copy_skip_args (e->call_stmt,
1074 e->callee->clone.combined_args_to_skip);
1075 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1076 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1078 if (gimple_vdef (new_stmt)
1079 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1080 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1082 gsi = gsi_for_stmt (e->call_stmt);
1083 gsi_replace (&gsi, new_stmt, false);
1084 /* We need to defer cleaning EH info on the new statement to
1085 fixup-cfg. We may not have dominator information at this point
1086 and thus would end up with unreachable blocks and have no way
1087 to communicate that we need to run CFG cleanup then. */
1088 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1089 if (lp_nr != 0)
1091 remove_stmt_from_eh_lp (e->call_stmt);
1092 add_stmt_to_eh_lp (new_stmt, lp_nr);
1095 else
1097 new_stmt = e->call_stmt;
1098 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1099 update_stmt (new_stmt);
1102 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
1104 if (cgraph_dump_file)
1106 fprintf (cgraph_dump_file, " updated to:");
1107 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1109 return new_stmt;
1112 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1113 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1114 of OLD_STMT if it was previously call statement.
1115 If NEW_STMT is NULL, the call has been dropped without any
1116 replacement. */
1118 static void
1119 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1120 gimple old_stmt, tree old_call,
1121 gimple new_stmt)
1123 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1124 ? gimple_call_fndecl (new_stmt) : 0;
1126 /* We are seeing indirect calls, then there is nothing to update. */
1127 if (!new_call && !old_call)
1128 return;
1129 /* See if we turned indirect call into direct call or folded call to one builtin
1130 into different builtin. */
1131 if (old_call != new_call)
1133 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1134 struct cgraph_edge *ne = NULL;
1135 gcov_type count;
1136 int frequency;
1138 if (e)
1140 /* See if the edge is already there and has the correct callee. It
1141 might be so because of indirect inlining has already updated
1142 it. We also might've cloned and redirected the edge. */
1143 if (new_call && e->callee)
1145 struct cgraph_node *callee = e->callee;
1146 while (callee)
1148 if (callee->symbol.decl == new_call
1149 || callee->former_clone_of == new_call)
1150 return;
1151 callee = callee->clone_of;
1155 /* Otherwise remove edge and create new one; we can't simply redirect
1156 since function has changed, so inline plan and other information
1157 attached to edge is invalid. */
1158 count = e->count;
1159 frequency = e->frequency;
1160 cgraph_remove_edge (e);
1162 else if (new_call)
1164 /* We are seeing new direct call; compute profile info based on BB. */
1165 basic_block bb = gimple_bb (new_stmt);
1166 count = bb->count;
1167 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1168 bb);
1171 if (new_call)
1173 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1174 new_stmt, count, frequency);
1175 gcc_assert (ne->inline_failed);
1178 /* We only updated the call stmt; update pointer in cgraph edge.. */
1179 else if (old_stmt != new_stmt)
1180 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1183 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1184 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1185 of OLD_STMT before it was updated (updating can happen inplace). */
1187 void
1188 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1190 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1191 struct cgraph_node *node;
1193 gcc_checking_assert (orig);
1194 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1195 if (orig->clones)
1196 for (node = orig->clones; node != orig;)
1198 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1199 if (node->clones)
1200 node = node->clones;
1201 else if (node->next_sibling_clone)
1202 node = node->next_sibling_clone;
1203 else
1205 while (node != orig && !node->next_sibling_clone)
1206 node = node->clone_of;
1207 if (node != orig)
1208 node = node->next_sibling_clone;
1214 /* Remove all callees from the node. */
1216 void
1217 cgraph_node_remove_callees (struct cgraph_node *node)
1219 struct cgraph_edge *e, *f;
1221 /* It is sufficient to remove the edges from the lists of callers of
1222 the callees. The callee list of the node can be zapped with one
1223 assignment. */
1224 for (e = node->callees; e; e = f)
1226 f = e->next_callee;
1227 cgraph_call_edge_removal_hooks (e);
1228 if (!e->indirect_unknown_callee)
1229 cgraph_edge_remove_callee (e);
1230 cgraph_free_edge (e);
1232 for (e = node->indirect_calls; e; e = f)
1234 f = e->next_callee;
1235 cgraph_call_edge_removal_hooks (e);
1236 if (!e->indirect_unknown_callee)
1237 cgraph_edge_remove_callee (e);
1238 cgraph_free_edge (e);
1240 node->indirect_calls = NULL;
1241 node->callees = NULL;
1242 if (node->call_site_hash)
1244 htab_delete (node->call_site_hash);
1245 node->call_site_hash = NULL;
1249 /* Remove all callers from the node. */
1251 static void
1252 cgraph_node_remove_callers (struct cgraph_node *node)
1254 struct cgraph_edge *e, *f;
1256 /* It is sufficient to remove the edges from the lists of callees of
1257 the callers. The caller list of the node can be zapped with one
1258 assignment. */
1259 for (e = node->callers; e; e = f)
1261 f = e->next_caller;
1262 cgraph_call_edge_removal_hooks (e);
1263 cgraph_edge_remove_caller (e);
1264 cgraph_free_edge (e);
1266 node->callers = NULL;
1269 /* Release memory used to represent body of function NODE.
1270 Use this only for functions that are released before being translated to
1271 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1272 are free'd in final.c via free_after_compilation(). */
1274 void
1275 cgraph_release_function_body (struct cgraph_node *node)
1277 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1279 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1280 if (cfun->cfg
1281 && current_loops)
1283 cfun->curr_properties &= ~PROP_loops;
1284 loop_optimizer_finalize ();
1286 if (cfun->gimple_df)
1288 delete_tree_ssa ();
1289 delete_tree_cfg_annotations ();
1290 cfun->eh = NULL;
1292 if (cfun->cfg)
1294 gcc_assert (dom_computed[0] == DOM_NONE);
1295 gcc_assert (dom_computed[1] == DOM_NONE);
1296 clear_edges ();
1297 cfun->cfg = NULL;
1299 if (cfun->value_histograms)
1300 free_histograms ();
1301 pop_cfun();
1302 gimple_set_body (node->symbol.decl, NULL);
1303 node->ipa_transforms_to_apply.release ();
1304 /* Struct function hangs a lot of data that would leak if we didn't
1305 removed all pointers to it. */
1306 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1307 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1309 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1310 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1311 of its associated function function declaration because it's
1312 needed to emit debug info later. */
1313 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1314 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1317 /* Remove the node from cgraph. */
1319 void
1320 cgraph_remove_node (struct cgraph_node *node)
1322 struct cgraph_node *n;
1323 int uid = node->uid;
1325 cgraph_call_node_removal_hooks (node);
1326 cgraph_node_remove_callers (node);
1327 cgraph_node_remove_callees (node);
1328 node->ipa_transforms_to_apply.release ();
1330 /* Incremental inlining access removed nodes stored in the postorder list.
1332 node->symbol.force_output = false;
1333 node->symbol.forced_by_abi = false;
1334 for (n = node->nested; n; n = n->next_nested)
1335 n->origin = NULL;
1336 node->nested = NULL;
1337 if (node->origin)
1339 struct cgraph_node **node2 = &node->origin->nested;
1341 while (*node2 != node)
1342 node2 = &(*node2)->next_nested;
1343 *node2 = node->next_nested;
1345 symtab_unregister_node ((symtab_node)node);
1346 if (node->prev_sibling_clone)
1347 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1348 else if (node->clone_of)
1349 node->clone_of->clones = node->next_sibling_clone;
1350 if (node->next_sibling_clone)
1351 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1352 if (node->clones)
1354 struct cgraph_node *n, *next;
1356 if (node->clone_of)
1358 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1359 n->clone_of = node->clone_of;
1360 n->clone_of = node->clone_of;
1361 n->next_sibling_clone = node->clone_of->clones;
1362 if (node->clone_of->clones)
1363 node->clone_of->clones->prev_sibling_clone = n;
1364 node->clone_of->clones = node->clones;
1366 else
1368 /* We are removing node with clones. This makes clones inconsistent,
1369 but assume they will be removed subsequently and just keep clone
1370 tree intact. This can happen in unreachable function removal since
1371 we remove unreachable functions in random order, not by bottom-up
1372 walk of clone trees. */
1373 for (n = node->clones; n; n = next)
1375 next = n->next_sibling_clone;
1376 n->next_sibling_clone = NULL;
1377 n->prev_sibling_clone = NULL;
1378 n->clone_of = NULL;
1383 /* While all the clones are removed after being proceeded, the function
1384 itself is kept in the cgraph even after it is compiled. Check whether
1385 we are done with this body and reclaim it proactively if this is the case.
1387 if (cgraph_state != CGRAPH_LTO_STREAMING)
1389 n = cgraph_get_node (node->symbol.decl);
1390 if (!n
1391 || (!n->clones && !n->clone_of && !n->global.inlined_to
1392 && (cgraph_global_info_ready
1393 && (TREE_ASM_WRITTEN (n->symbol.decl)
1394 || DECL_EXTERNAL (n->symbol.decl)
1395 || !n->symbol.analyzed
1396 || (!flag_wpa && n->symbol.in_other_partition)))))
1397 cgraph_release_function_body (node);
1400 node->symbol.decl = NULL;
1401 if (node->call_site_hash)
1403 htab_delete (node->call_site_hash);
1404 node->call_site_hash = NULL;
1406 cgraph_n_nodes--;
1408 /* Clear out the node to NULL all pointers and add the node to the free
1409 list. */
1410 memset (node, 0, sizeof(*node));
1411 node->symbol.type = SYMTAB_FUNCTION;
1412 node->uid = uid;
1413 SET_NEXT_FREE_NODE (node, free_nodes);
1414 free_nodes = node;
1417 /* Likewise indicate that a node is having address taken. */
1419 void
1420 cgraph_mark_address_taken_node (struct cgraph_node *node)
1422 /* Indirect inlining can figure out that all uses of the address are
1423 inlined. */
1424 if (node->global.inlined_to)
1426 gcc_assert (cfun->after_inlining);
1427 gcc_assert (node->callers->indirect_inlining_edge);
1428 return;
1430 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1431 IPA_REF_ADDR reference exists (and thus it should be set on node
1432 representing alias we take address of) and as a test whether address
1433 of the object was taken (and thus it should be set on node alias is
1434 referring to). We should remove the first use and the remove the
1435 following set. */
1436 node->symbol.address_taken = 1;
1437 node = cgraph_function_or_thunk_node (node, NULL);
1438 node->symbol.address_taken = 1;
1441 /* Return local info for the compiled function. */
1443 struct cgraph_local_info *
1444 cgraph_local_info (tree decl)
1446 struct cgraph_node *node;
1448 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1449 node = cgraph_get_node (decl);
1450 if (!node)
1451 return NULL;
1452 return &node->local;
1455 /* Return local info for the compiled function. */
1457 struct cgraph_global_info *
1458 cgraph_global_info (tree decl)
1460 struct cgraph_node *node;
1462 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1463 node = cgraph_get_node (decl);
1464 if (!node)
1465 return NULL;
1466 return &node->global;
1469 /* Return local info for the compiled function. */
1471 struct cgraph_rtl_info *
1472 cgraph_rtl_info (tree decl)
1474 struct cgraph_node *node;
1476 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1477 node = cgraph_get_node (decl);
1478 if (!node
1479 || (decl != current_function_decl
1480 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1481 return NULL;
1482 return &node->rtl;
1485 /* Return a string describing the failure REASON. */
1487 const char*
1488 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1490 #undef DEFCIFCODE
1491 #define DEFCIFCODE(code, string) string,
1493 static const char *cif_string_table[CIF_N_REASONS] = {
1494 #include "cif-code.def"
1497 /* Signedness of an enum type is implementation defined, so cast it
1498 to unsigned before testing. */
1499 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1500 return cif_string_table[reason];
1503 /* Names used to print out the availability enum. */
1504 const char * const cgraph_availability_names[] =
1505 {"unset", "not_available", "overwritable", "available", "local"};
1508 /* Dump call graph node NODE to file F. */
1510 void
1511 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1513 struct cgraph_edge *edge;
1514 int indirect_calls_count = 0;
1516 dump_symtab_base (f, (symtab_node) node);
1518 if (node->global.inlined_to)
1519 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1520 xstrdup (cgraph_node_name (node)),
1521 node->symbol.order,
1522 xstrdup (cgraph_node_name (node->global.inlined_to)),
1523 node->global.inlined_to->symbol.order);
1524 if (node->clone_of)
1525 fprintf (f, " Clone of %s/%i\n",
1526 cgraph_node_asm_name (node->clone_of),
1527 node->clone_of->symbol.order);
1528 if (cgraph_function_flags_ready)
1529 fprintf (f, " Availability: %s\n",
1530 cgraph_availability_names [cgraph_function_body_availability (node)]);
1532 fprintf (f, " Function flags:");
1533 if (node->count)
1534 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1535 (HOST_WIDEST_INT)node->count);
1536 if (node->origin)
1537 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1538 if (gimple_has_body_p (node->symbol.decl))
1539 fprintf (f, " body");
1540 if (node->process)
1541 fprintf (f, " process");
1542 if (node->local.local)
1543 fprintf (f, " local");
1544 if (node->local.redefined_extern_inline)
1545 fprintf (f, " redefined_extern_inline");
1546 if (node->only_called_at_startup)
1547 fprintf (f, " only_called_at_startup");
1548 if (node->only_called_at_exit)
1549 fprintf (f, " only_called_at_exit");
1550 if (node->tm_clone)
1551 fprintf (f, " tm_clone");
1553 fprintf (f, "\n");
1555 if (node->thunk.thunk_p)
1557 fprintf (f, " Thunk");
1558 if (node->thunk.alias)
1559 fprintf (f, " of %s (asm: %s)",
1560 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1561 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1562 fprintf (f, " fixed offset %i virtual value %i has "
1563 "virtual offset %i)\n",
1564 (int)node->thunk.fixed_offset,
1565 (int)node->thunk.virtual_value,
1566 (int)node->thunk.virtual_offset_p);
1568 if (node->symbol.alias && node->thunk.alias
1569 && DECL_P (node->thunk.alias))
1571 fprintf (f, " Alias of %s",
1572 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1573 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1574 fprintf (f, " (asm: %s)",
1575 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1576 fprintf (f, "\n");
1579 fprintf (f, " Called by: ");
1581 for (edge = node->callers; edge; edge = edge->next_caller)
1583 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1584 edge->caller->symbol.order);
1585 if (edge->count)
1586 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1587 (HOST_WIDEST_INT)edge->count);
1588 if (edge->frequency)
1589 fprintf (f, "(%.2f per call) ",
1590 edge->frequency / (double)CGRAPH_FREQ_BASE);
1591 if (!edge->inline_failed)
1592 fprintf(f, "(inlined) ");
1593 if (edge->indirect_inlining_edge)
1594 fprintf(f, "(indirect_inlining) ");
1595 if (edge->can_throw_external)
1596 fprintf(f, "(can throw external) ");
1599 fprintf (f, "\n Calls: ");
1600 for (edge = node->callees; edge; edge = edge->next_callee)
1602 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1603 edge->callee->symbol.order);
1604 if (!edge->inline_failed)
1605 fprintf(f, "(inlined) ");
1606 if (edge->indirect_inlining_edge)
1607 fprintf(f, "(indirect_inlining) ");
1608 if (edge->count)
1609 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1610 (HOST_WIDEST_INT)edge->count);
1611 if (edge->frequency)
1612 fprintf (f, "(%.2f per call) ",
1613 edge->frequency / (double)CGRAPH_FREQ_BASE);
1614 if (edge->can_throw_external)
1615 fprintf(f, "(can throw external) ");
1617 fprintf (f, "\n");
1619 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1620 indirect_calls_count++;
1621 if (indirect_calls_count)
1622 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1623 indirect_calls_count);
1627 /* Dump call graph node NODE to stderr. */
1629 DEBUG_FUNCTION void
1630 debug_cgraph_node (struct cgraph_node *node)
1632 dump_cgraph_node (stderr, node);
1636 /* Dump the callgraph to file F. */
1638 void
1639 dump_cgraph (FILE *f)
1641 struct cgraph_node *node;
1643 fprintf (f, "callgraph:\n\n");
1644 FOR_EACH_FUNCTION (node)
1645 dump_cgraph_node (f, node);
1649 /* Dump the call graph to stderr. */
1651 DEBUG_FUNCTION void
1652 debug_cgraph (void)
1654 dump_cgraph (stderr);
1657 /* Return true when the DECL can possibly be inlined. */
1658 bool
1659 cgraph_function_possibly_inlined_p (tree decl)
1661 if (!cgraph_global_info_ready)
1662 return !DECL_UNINLINABLE (decl);
1663 return DECL_POSSIBLY_INLINED (decl);
1666 /* NODE is no longer nested function; update cgraph accordingly. */
1667 void
1668 cgraph_unnest_node (struct cgraph_node *node)
1670 struct cgraph_node **node2 = &node->origin->nested;
1671 gcc_assert (node->origin);
1673 while (*node2 != node)
1674 node2 = &(*node2)->next_nested;
1675 *node2 = node->next_nested;
1676 node->origin = NULL;
1679 /* Return function availability. See cgraph.h for description of individual
1680 return values. */
1681 enum availability
1682 cgraph_function_body_availability (struct cgraph_node *node)
1684 enum availability avail;
1685 gcc_assert (cgraph_function_flags_ready);
1686 if (!node->symbol.analyzed)
1687 avail = AVAIL_NOT_AVAILABLE;
1688 else if (node->local.local)
1689 avail = AVAIL_LOCAL;
1690 else if (!node->symbol.externally_visible)
1691 avail = AVAIL_AVAILABLE;
1692 /* Inline functions are safe to be analyzed even if their symbol can
1693 be overwritten at runtime. It is not meaningful to enforce any sane
1694 behaviour on replacing inline function by different body. */
1695 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1696 avail = AVAIL_AVAILABLE;
1698 /* If the function can be overwritten, return OVERWRITABLE. Take
1699 care at least of two notable extensions - the COMDAT functions
1700 used to share template instantiations in C++ (this is symmetric
1701 to code cp_cannot_inline_tree_fn and probably shall be shared and
1702 the inlinability hooks completely eliminated).
1704 ??? Does the C++ one definition rule allow us to always return
1705 AVAIL_AVAILABLE here? That would be good reason to preserve this
1706 bit. */
1708 else if (decl_replaceable_p (node->symbol.decl)
1709 && !DECL_EXTERNAL (node->symbol.decl))
1710 avail = AVAIL_OVERWRITABLE;
1711 else avail = AVAIL_AVAILABLE;
1713 return avail;
1716 /* Worker for cgraph_node_can_be_local_p. */
1717 static bool
1718 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1719 void *data ATTRIBUTE_UNUSED)
1721 return !(!node->symbol.force_output
1722 && ((DECL_COMDAT (node->symbol.decl)
1723 && !node->symbol.forced_by_abi
1724 && !symtab_used_from_object_file_p ((symtab_node) node)
1725 && !node->symbol.same_comdat_group)
1726 || !node->symbol.externally_visible));
1729 /* Return true if NODE can be made local for API change.
1730 Extern inline functions and C++ COMDAT functions can be made local
1731 at the expense of possible code size growth if function is used in multiple
1732 compilation units. */
1733 bool
1734 cgraph_node_can_be_local_p (struct cgraph_node *node)
1736 return (!node->symbol.address_taken
1737 && !cgraph_for_node_and_aliases (node,
1738 cgraph_node_cannot_be_local_p_1,
1739 NULL, true));
1742 /* Call calback on NODE, thunks and aliases associated to NODE.
1743 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1744 skipped. */
1746 bool
1747 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1748 bool (*callback) (struct cgraph_node *, void *),
1749 void *data,
1750 bool include_overwritable)
1752 struct cgraph_edge *e;
1753 int i;
1754 struct ipa_ref *ref;
1756 if (callback (node, data))
1757 return true;
1758 for (e = node->callers; e; e = e->next_caller)
1759 if (e->caller->thunk.thunk_p
1760 && (include_overwritable
1761 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1762 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1763 include_overwritable))
1764 return true;
1765 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1766 if (ref->use == IPA_REF_ALIAS)
1768 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1769 if (include_overwritable
1770 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1771 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1772 include_overwritable))
1773 return true;
1775 return false;
1778 /* Call calback on NODE and aliases associated to NODE.
1779 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1780 skipped. */
1782 bool
1783 cgraph_for_node_and_aliases (struct cgraph_node *node,
1784 bool (*callback) (struct cgraph_node *, void *),
1785 void *data,
1786 bool include_overwritable)
1788 int i;
1789 struct ipa_ref *ref;
1791 if (callback (node, data))
1792 return true;
1793 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1794 if (ref->use == IPA_REF_ALIAS)
1796 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1797 if (include_overwritable
1798 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1799 if (cgraph_for_node_and_aliases (alias, callback, data,
1800 include_overwritable))
1801 return true;
1803 return false;
1806 /* Worker to bring NODE local. */
1808 static bool
1809 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1811 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1812 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1814 symtab_make_decl_local (node->symbol.decl);
1816 node->symbol.externally_visible = false;
1817 node->symbol.forced_by_abi = false;
1818 node->local.local = true;
1819 node->symbol.unique_name = (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
1820 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
1821 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1822 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1824 return false;
1827 /* Bring NODE local. */
1829 void
1830 cgraph_make_node_local (struct cgraph_node *node)
1832 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1833 NULL, true);
1836 /* Worker to set nothrow flag. */
1838 static bool
1839 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1841 struct cgraph_edge *e;
1843 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1845 if (data != NULL)
1846 for (e = node->callers; e; e = e->next_caller)
1847 e->can_throw_external = false;
1848 return false;
1851 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1852 if any to NOTHROW. */
1854 void
1855 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1857 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1858 (void *)(size_t)nothrow, false);
1861 /* Worker to set const flag. */
1863 static bool
1864 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1866 /* Static constructors and destructors without a side effect can be
1867 optimized out. */
1868 if (data && !((size_t)data & 2))
1870 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1871 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1872 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1873 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1875 TREE_READONLY (node->symbol.decl) = data != NULL;
1876 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1877 return false;
1880 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1881 if any to READONLY. */
1883 void
1884 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1886 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1887 (void *)(size_t)(readonly + (int)looping * 2),
1888 false);
1891 /* Worker to set pure flag. */
1893 static bool
1894 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1896 /* Static constructors and destructors without a side effect can be
1897 optimized out. */
1898 if (data && !((size_t)data & 2))
1900 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1901 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1902 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1903 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1905 DECL_PURE_P (node->symbol.decl) = data != NULL;
1906 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1907 return false;
1910 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1911 if any to PURE. */
1913 void
1914 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1916 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1917 (void *)(size_t)(pure + (int)looping * 2),
1918 false);
1921 /* Data used by cgraph_propagate_frequency. */
1923 struct cgraph_propagate_frequency_data
1925 bool maybe_unlikely_executed;
1926 bool maybe_executed_once;
1927 bool only_called_at_startup;
1928 bool only_called_at_exit;
1931 /* Worker for cgraph_propagate_frequency_1. */
1933 static bool
1934 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1936 struct cgraph_propagate_frequency_data *d;
1937 struct cgraph_edge *edge;
1939 d = (struct cgraph_propagate_frequency_data *)data;
1940 for (edge = node->callers;
1941 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1942 || d->only_called_at_startup || d->only_called_at_exit);
1943 edge = edge->next_caller)
1945 if (edge->caller != node)
1947 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1948 /* It makes sense to put main() together with the static constructors.
1949 It will be executed for sure, but rest of functions called from
1950 main are definitely not at startup only. */
1951 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1952 d->only_called_at_startup = 0;
1953 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1955 if (!edge->frequency)
1956 continue;
1957 switch (edge->caller->frequency)
1959 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1960 break;
1961 case NODE_FREQUENCY_EXECUTED_ONCE:
1962 if (dump_file && (dump_flags & TDF_DETAILS))
1963 fprintf (dump_file, " Called by %s that is executed once\n",
1964 cgraph_node_name (edge->caller));
1965 d->maybe_unlikely_executed = false;
1966 if (inline_edge_summary (edge)->loop_depth)
1968 d->maybe_executed_once = false;
1969 if (dump_file && (dump_flags & TDF_DETAILS))
1970 fprintf (dump_file, " Called in loop\n");
1972 break;
1973 case NODE_FREQUENCY_HOT:
1974 case NODE_FREQUENCY_NORMAL:
1975 if (dump_file && (dump_flags & TDF_DETAILS))
1976 fprintf (dump_file, " Called by %s that is normal or hot\n",
1977 cgraph_node_name (edge->caller));
1978 d->maybe_unlikely_executed = false;
1979 d->maybe_executed_once = false;
1980 break;
1983 return edge != NULL;
1986 /* See if the frequency of NODE can be updated based on frequencies of its
1987 callers. */
1988 bool
1989 cgraph_propagate_frequency (struct cgraph_node *node)
1991 struct cgraph_propagate_frequency_data d = {true, true, true, true};
1992 bool changed = false;
1994 if (!node->local.local)
1995 return false;
1996 gcc_assert (node->symbol.analyzed);
1997 if (dump_file && (dump_flags & TDF_DETAILS))
1998 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2000 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2002 if ((d.only_called_at_startup && !d.only_called_at_exit)
2003 && !node->only_called_at_startup)
2005 node->only_called_at_startup = true;
2006 if (dump_file)
2007 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2008 cgraph_node_name (node));
2009 changed = true;
2011 if ((d.only_called_at_exit && !d.only_called_at_startup)
2012 && !node->only_called_at_exit)
2014 node->only_called_at_exit = true;
2015 if (dump_file)
2016 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2017 cgraph_node_name (node));
2018 changed = true;
2020 /* These come either from profile or user hints; never update them. */
2021 if (node->frequency == NODE_FREQUENCY_HOT
2022 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2023 return changed;
2024 if (d.maybe_unlikely_executed)
2026 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2027 if (dump_file)
2028 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2029 cgraph_node_name (node));
2030 changed = true;
2032 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2034 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2035 if (dump_file)
2036 fprintf (dump_file, "Node %s promoted to executed once.\n",
2037 cgraph_node_name (node));
2038 changed = true;
2040 return changed;
2043 /* Return true when NODE can not return or throw and thus
2044 it is safe to ignore its side effects for IPA analysis. */
2046 bool
2047 cgraph_node_cannot_return (struct cgraph_node *node)
2049 int flags = flags_from_decl_or_type (node->symbol.decl);
2050 if (!flag_exceptions)
2051 return (flags & ECF_NORETURN) != 0;
2052 else
2053 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2054 == (ECF_NORETURN | ECF_NOTHROW));
2057 /* Return true when call of E can not lead to return from caller
2058 and thus it is safe to ignore its side effects for IPA analysis
2059 when computing side effects of the caller.
2060 FIXME: We could actually mark all edges that have no reaching
2061 patch to EXIT_BLOCK_PTR or throw to get better results. */
2062 bool
2063 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2065 if (cgraph_node_cannot_return (e->caller))
2066 return true;
2067 if (e->indirect_unknown_callee)
2069 int flags = e->indirect_info->ecf_flags;
2070 if (!flag_exceptions)
2071 return (flags & ECF_NORETURN) != 0;
2072 else
2073 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2074 == (ECF_NORETURN | ECF_NOTHROW));
2076 else
2077 return cgraph_node_cannot_return (e->callee);
2080 /* Return true when function NODE can be removed from callgraph
2081 if all direct calls are eliminated. */
2083 bool
2084 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2086 gcc_assert (!node->global.inlined_to);
2087 /* Extern inlines can always go, we will use the external definition. */
2088 if (DECL_EXTERNAL (node->symbol.decl))
2089 return true;
2090 /* When function is needed, we can not remove it. */
2091 if (node->symbol.force_output || node->symbol.used_from_other_partition)
2092 return false;
2093 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
2094 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2095 return false;
2096 /* Only COMDAT functions can be removed if externally visible. */
2097 if (node->symbol.externally_visible
2098 && (!DECL_COMDAT (node->symbol.decl)
2099 || node->symbol.forced_by_abi
2100 || symtab_used_from_object_file_p ((symtab_node) node)))
2101 return false;
2102 return true;
2105 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2107 static bool
2108 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2110 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2113 /* Return true when function NODE and its aliases can be removed from callgraph
2114 if all direct calls are eliminated. */
2116 bool
2117 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2119 /* Extern inlines can always go, we will use the external definition. */
2120 if (DECL_EXTERNAL (node->symbol.decl))
2121 return true;
2122 if (node->symbol.address_taken)
2123 return false;
2124 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2127 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2129 static bool
2130 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2132 return symtab_used_from_object_file_p ((symtab_node) node);
2135 /* Return true when function NODE can be expected to be removed
2136 from program when direct calls in this compilation unit are removed.
2138 As a special case COMDAT functions are
2139 cgraph_can_remove_if_no_direct_calls_p while the are not
2140 cgraph_only_called_directly_p (it is possible they are called from other
2141 unit)
2143 This function behaves as cgraph_only_called_directly_p because eliminating
2144 all uses of COMDAT function does not make it necessarily disappear from
2145 the program unless we are compiling whole program or we do LTO. In this
2146 case we know we win since dynamic linking will not really discard the
2147 linkonce section. */
2149 bool
2150 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2152 gcc_assert (!node->global.inlined_to);
2153 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2154 return false;
2155 if (!in_lto_p && !flag_whole_program)
2156 return cgraph_only_called_directly_p (node);
2157 else
2159 if (DECL_EXTERNAL (node->symbol.decl))
2160 return true;
2161 return cgraph_can_remove_if_no_direct_calls_p (node);
2166 /* Worker for cgraph_only_called_directly_p. */
2168 static bool
2169 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2171 return !cgraph_only_called_directly_or_aliased_p (node);
2174 /* Return true when function NODE and all its aliases are only called
2175 directly.
2176 i.e. it is not externally visible, address was not taken and
2177 it is not used in any other non-standard way. */
2179 bool
2180 cgraph_only_called_directly_p (struct cgraph_node *node)
2182 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2183 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2184 NULL, true);
2188 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2190 static bool
2191 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2193 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2194 struct cgraph_edge *cs;
2195 enum availability avail;
2196 cgraph_function_or_thunk_node (node, &avail);
2198 if (avail > AVAIL_OVERWRITABLE)
2199 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2200 if (!cs->indirect_inlining_edge)
2201 redirect_callers->safe_push (cs);
2202 return false;
2205 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2206 (i.e. are not overwritable). */
2208 vec<cgraph_edge_p>
2209 collect_callers_of_node (struct cgraph_node *node)
2211 vec<cgraph_edge_p> redirect_callers = vNULL;
2212 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2213 &redirect_callers, false);
2214 return redirect_callers;
2217 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2218 static bool
2219 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2221 node = cgraph_function_or_thunk_node (node, NULL);
2222 node2 = cgraph_function_or_thunk_node (node2, NULL);
2223 while (node != node2 && node2)
2224 node2 = node2->clone_of;
2225 return node2 != NULL;
2228 /* Verify edge E count and frequency. */
2230 static bool
2231 verify_edge_count_and_frequency (struct cgraph_edge *e)
2233 bool error_found = false;
2234 if (e->count < 0)
2236 error ("caller edge count is negative");
2237 error_found = true;
2239 if (e->frequency < 0)
2241 error ("caller edge frequency is negative");
2242 error_found = true;
2244 if (e->frequency > CGRAPH_FREQ_MAX)
2246 error ("caller edge frequency is too large");
2247 error_found = true;
2249 if (gimple_has_body_p (e->caller->symbol.decl)
2250 && !e->caller->global.inlined_to
2251 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2252 Remove this once edges are actually removed from the function at that time. */
2253 && (e->frequency
2254 || (inline_edge_summary_vec.exists ()
2255 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2256 || !inline_edge_summary (e)->predicate)))
2257 && (e->frequency
2258 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2259 gimple_bb (e->call_stmt))))
2261 error ("caller edge frequency %i does not match BB frequency %i",
2262 e->frequency,
2263 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2264 gimple_bb (e->call_stmt)));
2265 error_found = true;
2267 return error_found;
2270 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2271 static void
2272 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2274 bool fndecl_was_null = false;
2275 /* debug_gimple_stmt needs correct cfun */
2276 if (cfun != this_cfun)
2277 set_cfun (this_cfun);
2278 /* ...and an actual current_function_decl */
2279 if (!current_function_decl)
2281 current_function_decl = this_cfun->decl;
2282 fndecl_was_null = true;
2284 debug_gimple_stmt (stmt);
2285 if (fndecl_was_null)
2286 current_function_decl = NULL;
2289 /* Verify that call graph edge E corresponds to DECL from the associated
2290 statement. Return true if the verification should fail. */
2292 static bool
2293 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2295 struct cgraph_node *node;
2297 if (!decl || e->callee->global.inlined_to)
2298 return false;
2299 if (cgraph_state == CGRAPH_LTO_STREAMING)
2300 return false;
2301 node = cgraph_get_node (decl);
2303 /* We do not know if a node from a different partition is an alias or what it
2304 aliases and therefore cannot do the former_clone_of check reliably. */
2305 if (!node || node->symbol.in_other_partition)
2306 return false;
2307 node = cgraph_function_or_thunk_node (node, NULL);
2309 if (e->callee->former_clone_of != node->symbol.decl
2310 /* IPA-CP sometimes redirect edge to clone and then back to the former
2311 function. This ping-pong has to go, eventually. */
2312 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2313 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2314 return true;
2315 else
2316 return false;
2319 /* Verify cgraph nodes of given cgraph node. */
2320 DEBUG_FUNCTION void
2321 verify_cgraph_node (struct cgraph_node *node)
2323 struct cgraph_edge *e;
2324 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2325 basic_block this_block;
2326 gimple_stmt_iterator gsi;
2327 bool error_found = false;
2329 if (seen_error ())
2330 return;
2332 timevar_push (TV_CGRAPH_VERIFY);
2333 error_found |= verify_symtab_base ((symtab_node) node);
2334 for (e = node->callees; e; e = e->next_callee)
2335 if (e->aux)
2337 error ("aux field set for edge %s->%s",
2338 identifier_to_locale (cgraph_node_name (e->caller)),
2339 identifier_to_locale (cgraph_node_name (e->callee)));
2340 error_found = true;
2342 if (node->count < 0)
2344 error ("execution count is negative");
2345 error_found = true;
2347 if (node->global.inlined_to && node->symbol.same_comdat_group)
2349 error ("inline clone in same comdat group list");
2350 error_found = true;
2352 if (!node->symbol.definition && node->local.local)
2354 error ("local symbols must be defined");
2355 error_found = true;
2357 if (node->global.inlined_to && node->symbol.externally_visible)
2359 error ("externally visible inline clone");
2360 error_found = true;
2362 if (node->global.inlined_to && node->symbol.address_taken)
2364 error ("inline clone with address taken");
2365 error_found = true;
2367 if (node->global.inlined_to && node->symbol.force_output)
2369 error ("inline clone is forced to output");
2370 error_found = true;
2372 for (e = node->indirect_calls; e; e = e->next_callee)
2374 if (e->aux)
2376 error ("aux field set for indirect edge from %s",
2377 identifier_to_locale (cgraph_node_name (e->caller)));
2378 error_found = true;
2380 if (!e->indirect_unknown_callee
2381 || !e->indirect_info)
2383 error ("An indirect edge from %s is not marked as indirect or has "
2384 "associated indirect_info, the corresponding statement is: ",
2385 identifier_to_locale (cgraph_node_name (e->caller)));
2386 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2387 error_found = true;
2390 for (e = node->callers; e; e = e->next_caller)
2392 if (verify_edge_count_and_frequency (e))
2393 error_found = true;
2394 if (!e->inline_failed)
2396 if (node->global.inlined_to
2397 != (e->caller->global.inlined_to
2398 ? e->caller->global.inlined_to : e->caller))
2400 error ("inlined_to pointer is wrong");
2401 error_found = true;
2403 if (node->callers->next_caller)
2405 error ("multiple inline callers");
2406 error_found = true;
2409 else
2410 if (node->global.inlined_to)
2412 error ("inlined_to pointer set for noninline callers");
2413 error_found = true;
2416 for (e = node->indirect_calls; e; e = e->next_callee)
2417 if (verify_edge_count_and_frequency (e))
2418 error_found = true;
2419 if (!node->callers && node->global.inlined_to)
2421 error ("inlined_to pointer is set but no predecessors found");
2422 error_found = true;
2424 if (node->global.inlined_to == node)
2426 error ("inlined_to pointer refers to itself");
2427 error_found = true;
2430 if (node->clone_of)
2432 struct cgraph_node *n;
2433 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2434 if (n == node)
2435 break;
2436 if (!n)
2438 error ("node has wrong clone_of");
2439 error_found = true;
2442 if (node->clones)
2444 struct cgraph_node *n;
2445 for (n = node->clones; n; n = n->next_sibling_clone)
2446 if (n->clone_of != node)
2447 break;
2448 if (n)
2450 error ("node has wrong clone list");
2451 error_found = true;
2454 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2456 error ("node is in clone list but it is not clone");
2457 error_found = true;
2459 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2461 error ("node has wrong prev_clone pointer");
2462 error_found = true;
2464 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2466 error ("double linked list of clones corrupted");
2467 error_found = true;
2470 if (node->symbol.analyzed && node->symbol.alias)
2472 bool ref_found = false;
2473 int i;
2474 struct ipa_ref *ref;
2476 if (node->callees)
2478 error ("Alias has call edges");
2479 error_found = true;
2481 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2482 i, ref); i++)
2483 if (ref->use != IPA_REF_ALIAS)
2485 error ("Alias has non-alias reference");
2486 error_found = true;
2488 else if (ref_found)
2490 error ("Alias has more than one alias reference");
2491 error_found = true;
2493 else
2494 ref_found = true;
2495 if (!ref_found)
2497 error ("Analyzed alias has no reference");
2498 error_found = true;
2501 if (node->symbol.analyzed && node->thunk.thunk_p)
2503 if (!node->callees)
2505 error ("No edge out of thunk node");
2506 error_found = true;
2508 else if (node->callees->next_callee)
2510 error ("More than one edge out of thunk node");
2511 error_found = true;
2513 if (gimple_has_body_p (node->symbol.decl))
2515 error ("Thunk is not supposed to have body");
2516 error_found = true;
2519 else if (node->symbol.analyzed && gimple_has_body_p (node->symbol.decl)
2520 && !TREE_ASM_WRITTEN (node->symbol.decl)
2521 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2522 && !flag_wpa)
2524 if (this_cfun->cfg)
2526 /* Reach the trees by walking over the CFG, and note the
2527 enclosing basic-blocks in the call edges. */
2528 FOR_EACH_BB_FN (this_block, this_cfun)
2529 for (gsi = gsi_start_bb (this_block);
2530 !gsi_end_p (gsi);
2531 gsi_next (&gsi))
2533 gimple stmt = gsi_stmt (gsi);
2534 if (is_gimple_call (stmt))
2536 struct cgraph_edge *e = cgraph_edge (node, stmt);
2537 tree decl = gimple_call_fndecl (stmt);
2538 if (e)
2540 if (e->aux)
2542 error ("shared call_stmt:");
2543 cgraph_debug_gimple_stmt (this_cfun, stmt);
2544 error_found = true;
2546 if (!e->indirect_unknown_callee)
2548 if (verify_edge_corresponds_to_fndecl (e, decl))
2550 error ("edge points to wrong declaration:");
2551 debug_tree (e->callee->symbol.decl);
2552 fprintf (stderr," Instead of:");
2553 debug_tree (decl);
2554 error_found = true;
2557 else if (decl)
2559 error ("an indirect edge with unknown callee "
2560 "corresponding to a call_stmt with "
2561 "a known declaration:");
2562 error_found = true;
2563 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2565 e->aux = (void *)1;
2567 else if (decl)
2569 error ("missing callgraph edge for call stmt:");
2570 cgraph_debug_gimple_stmt (this_cfun, stmt);
2571 error_found = true;
2576 else
2577 /* No CFG available?! */
2578 gcc_unreachable ();
2580 for (e = node->callees; e; e = e->next_callee)
2582 if (!e->aux)
2584 error ("edge %s->%s has no corresponding call_stmt",
2585 identifier_to_locale (cgraph_node_name (e->caller)),
2586 identifier_to_locale (cgraph_node_name (e->callee)));
2587 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2588 error_found = true;
2590 e->aux = 0;
2592 for (e = node->indirect_calls; e; e = e->next_callee)
2594 if (!e->aux)
2596 error ("an indirect edge from %s has no corresponding call_stmt",
2597 identifier_to_locale (cgraph_node_name (e->caller)));
2598 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2599 error_found = true;
2601 e->aux = 0;
2604 if (error_found)
2606 dump_cgraph_node (stderr, node);
2607 internal_error ("verify_cgraph_node failed");
2609 timevar_pop (TV_CGRAPH_VERIFY);
2612 /* Verify whole cgraph structure. */
2613 DEBUG_FUNCTION void
2614 verify_cgraph (void)
2616 struct cgraph_node *node;
2618 if (seen_error ())
2619 return;
2621 FOR_EACH_FUNCTION (node)
2622 verify_cgraph_node (node);
2625 /* Create external decl node for DECL.
2626 The difference i nbetween cgraph_get_create_node and
2627 cgraph_get_create_real_symbol_node is that cgraph_get_create_node
2628 may return inline clone, while cgraph_get_create_real_symbol_node
2629 will create a new node in this case.
2630 FIXME: This function should be removed once clones are put out of decl
2631 hash. */
2633 struct cgraph_node *
2634 cgraph_get_create_real_symbol_node (tree decl)
2636 struct cgraph_node *first_clone = cgraph_get_node (decl);
2637 struct cgraph_node *node;
2638 /* create symbol table node. even if inline clone exists, we can not take
2639 it as a target of non-inlined call. */
2640 node = cgraph_get_node (decl);
2641 if (node && !node->global.inlined_to)
2642 return node;
2644 node = cgraph_create_node (decl);
2646 /* ok, we previously inlined the function, then removed the offline copy and
2647 now we want it back for external call. this can happen when devirtualizing
2648 while inlining function called once that happens after extern inlined and
2649 virtuals are already removed. in this case introduce the external node
2650 and make it available for call. */
2651 if (first_clone)
2653 first_clone->clone_of = node;
2654 node->clones = first_clone;
2655 symtab_prevail_in_asm_name_hash ((symtab_node) node);
2656 symtab_insert_node_to_hashtable ((symtab_node) node);
2657 if (dump_file)
2658 fprintf (dump_file, "Introduced new external node "
2659 "(%s/%i) and turned into root of the clone tree.\n",
2660 xstrdup (cgraph_node_name (node)), node->symbol.order);
2662 else if (dump_file)
2663 fprintf (dump_file, "Introduced new external node "
2664 "(%s/%i).\n", xstrdup (cgraph_node_name (node)),
2665 node->symbol.order);
2666 return node;
2670 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2671 Walk through thunk, too.
2672 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2674 struct cgraph_node *
2675 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2679 node = cgraph_function_or_thunk_node (node, availability);
2680 if (node->thunk.thunk_p)
2682 node = node->callees->callee;
2683 if (availability)
2685 enum availability a;
2686 a = cgraph_function_body_availability (node);
2687 if (a < *availability)
2688 *availability = a;
2690 node = cgraph_function_or_thunk_node (node, availability);
2692 } while (node && node->thunk.thunk_p);
2693 return node;
2696 #include "gt-cgraph.h"