* cgraph.c (cgraph_speculative_call_info): Fix parameter order and formating;
[official-gcc.git] / gcc / cgraph.c
blobdc4e7b994b6f390efb7dafdba9f2fa2f455dd5af
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_update_edge_in_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 *slot = e;
687 /* Add call graph edge E to call site hash of its caller. */
689 static inline void
690 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
692 void **slot;
693 /* There are two speculative edges for every statement (one direct,
694 one indirect); always hash the direct one. */
695 if (e->speculative && e->indirect_unknown_callee)
696 return;
697 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
698 e->call_stmt,
699 htab_hash_pointer (e->call_stmt),
700 INSERT);
701 if (*slot)
703 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
704 if (e->callee)
705 *slot = e;
706 return;
708 gcc_assert (!*slot || e->speculative);
709 *slot = e;
712 /* Return the callgraph edge representing the GIMPLE_CALL statement
713 CALL_STMT. */
715 struct cgraph_edge *
716 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
718 struct cgraph_edge *e, *e2;
719 int n = 0;
721 if (node->call_site_hash)
722 return (struct cgraph_edge *)
723 htab_find_with_hash (node->call_site_hash, call_stmt,
724 htab_hash_pointer (call_stmt));
726 /* This loop may turn out to be performance problem. In such case adding
727 hashtables into call nodes with very many edges is probably best
728 solution. It is not good idea to add pointer into CALL_EXPR itself
729 because we want to make possible having multiple cgraph nodes representing
730 different clones of the same body before the body is actually cloned. */
731 for (e = node->callees; e; e = e->next_callee)
733 if (e->call_stmt == call_stmt)
734 break;
735 n++;
738 if (!e)
739 for (e = node->indirect_calls; e; e = e->next_callee)
741 if (e->call_stmt == call_stmt)
742 break;
743 n++;
746 if (n > 100)
748 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
749 for (e2 = node->callees; e2; e2 = e2->next_callee)
750 cgraph_add_edge_to_call_site_hash (e2);
751 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
752 cgraph_add_edge_to_call_site_hash (e2);
755 return e;
759 /* Change field call_stmt of edge E to NEW_STMT.
760 If UPDATE_SPECULATIVE and E is any component of speculative
761 edge, then update all components. */
763 void
764 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
765 bool update_speculative)
767 tree decl;
769 /* Speculative edges has three component, update all of them
770 when asked to. */
771 if (update_speculative && e->speculative)
773 struct cgraph_edge *direct, *indirect;
774 struct ipa_ref *ref;
776 cgraph_speculative_call_info (e, direct, indirect, ref);
777 cgraph_set_call_stmt (direct, new_stmt, false);
778 cgraph_set_call_stmt (indirect, new_stmt, false);
779 ref->stmt = new_stmt;
780 return;
783 /* Only direct speculative edges go to call_site_hash. */
784 if (e->caller->call_site_hash
785 && (!e->speculative || !e->indirect_unknown_callee))
787 htab_remove_elt_with_hash (e->caller->call_site_hash,
788 e->call_stmt,
789 htab_hash_pointer (e->call_stmt));
792 e->call_stmt = new_stmt;
793 if (e->indirect_unknown_callee
794 && (decl = gimple_call_fndecl (new_stmt)))
796 /* Constant propagation (and possibly also inlining?) can turn an
797 indirect call into a direct one. */
798 struct cgraph_node *new_callee = cgraph_get_node (decl);
800 gcc_checking_assert (new_callee);
801 e = cgraph_make_edge_direct (e, new_callee);
804 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
805 e->can_throw_external = stmt_can_throw_external (new_stmt);
806 pop_cfun ();
807 if (e->caller->call_site_hash)
808 cgraph_add_edge_to_call_site_hash (e);
811 /* Allocate a cgraph_edge structure and fill it with data according to the
812 parameters of which only CALLEE can be NULL (when creating an indirect call
813 edge). */
815 static struct cgraph_edge *
816 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
817 gimple call_stmt, gcov_type count, int freq)
819 struct cgraph_edge *edge;
821 /* LTO does not actually have access to the call_stmt since these
822 have not been loaded yet. */
823 if (call_stmt)
825 /* This is a rather expensive check possibly triggering
826 construction of call stmt hashtable. */
827 #ifdef ENABLE_CHECKING
828 struct cgraph_edge *e;
829 gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
830 #endif
832 gcc_assert (is_gimple_call (call_stmt));
835 if (free_edges)
837 edge = free_edges;
838 free_edges = NEXT_FREE_EDGE (edge);
840 else
842 edge = ggc_alloc_cgraph_edge ();
843 edge->uid = cgraph_edge_max_uid++;
846 edge->aux = NULL;
847 edge->caller = caller;
848 edge->callee = callee;
849 edge->prev_caller = NULL;
850 edge->next_caller = NULL;
851 edge->prev_callee = NULL;
852 edge->next_callee = NULL;
853 edge->lto_stmt_uid = 0;
855 edge->count = count;
856 gcc_assert (count >= 0);
857 edge->frequency = freq;
858 gcc_assert (freq >= 0);
859 gcc_assert (freq <= CGRAPH_FREQ_MAX);
861 edge->call_stmt = call_stmt;
862 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
863 edge->can_throw_external
864 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
865 pop_cfun ();
866 if (call_stmt
867 && callee && callee->symbol.decl
868 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl,
869 false))
870 edge->call_stmt_cannot_inline_p = true;
871 else
872 edge->call_stmt_cannot_inline_p = false;
873 if (call_stmt && caller->call_site_hash)
874 cgraph_add_edge_to_call_site_hash (edge);
876 edge->indirect_info = NULL;
877 edge->indirect_inlining_edge = 0;
878 edge->speculative = false;
880 return edge;
883 /* Create edge from CALLER to CALLEE in the cgraph. */
885 struct cgraph_edge *
886 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
887 gimple call_stmt, gcov_type count, int freq)
889 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
890 count, freq);
892 edge->indirect_unknown_callee = 0;
893 initialize_inline_failed (edge);
895 edge->next_caller = callee->callers;
896 if (callee->callers)
897 callee->callers->prev_caller = edge;
898 edge->next_callee = caller->callees;
899 if (caller->callees)
900 caller->callees->prev_callee = edge;
901 caller->callees = edge;
902 callee->callers = edge;
904 return edge;
907 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
909 struct cgraph_indirect_call_info *
910 cgraph_allocate_init_indirect_info (void)
912 struct cgraph_indirect_call_info *ii;
914 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
915 ii->param_index = -1;
916 return ii;
919 /* Create an indirect edge with a yet-undetermined callee where the call
920 statement destination is a formal parameter of the caller with index
921 PARAM_INDEX. */
923 struct cgraph_edge *
924 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
925 int ecf_flags,
926 gcov_type count, int freq)
928 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
929 count, freq);
930 tree target;
932 edge->indirect_unknown_callee = 1;
933 initialize_inline_failed (edge);
935 edge->indirect_info = cgraph_allocate_init_indirect_info ();
936 edge->indirect_info->ecf_flags = ecf_flags;
938 /* Record polymorphic call info. */
939 if (call_stmt
940 && (target = gimple_call_fn (call_stmt))
941 && virtual_method_call_p (target))
943 tree type = obj_type_ref_class (target);
946 /* Only record types can have virtual calls. */
947 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
948 edge->indirect_info->param_index = -1;
949 edge->indirect_info->otr_token
950 = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
951 edge->indirect_info->otr_type = type;
952 edge->indirect_info->polymorphic = 1;
955 edge->next_callee = caller->indirect_calls;
956 if (caller->indirect_calls)
957 caller->indirect_calls->prev_callee = edge;
958 caller->indirect_calls = edge;
960 return edge;
963 /* Remove the edge E from the list of the callers of the callee. */
965 static inline void
966 cgraph_edge_remove_callee (struct cgraph_edge *e)
968 gcc_assert (!e->indirect_unknown_callee);
969 if (e->prev_caller)
970 e->prev_caller->next_caller = e->next_caller;
971 if (e->next_caller)
972 e->next_caller->prev_caller = e->prev_caller;
973 if (!e->prev_caller)
974 e->callee->callers = e->next_caller;
977 /* Remove the edge E from the list of the callees of the caller. */
979 static inline void
980 cgraph_edge_remove_caller (struct cgraph_edge *e)
982 if (e->prev_callee)
983 e->prev_callee->next_callee = e->next_callee;
984 if (e->next_callee)
985 e->next_callee->prev_callee = e->prev_callee;
986 if (!e->prev_callee)
988 if (e->indirect_unknown_callee)
989 e->caller->indirect_calls = e->next_callee;
990 else
991 e->caller->callees = e->next_callee;
993 if (e->caller->call_site_hash)
994 htab_remove_elt_with_hash (e->caller->call_site_hash,
995 e->call_stmt,
996 htab_hash_pointer (e->call_stmt));
999 /* Put the edge onto the free list. */
1001 static void
1002 cgraph_free_edge (struct cgraph_edge *e)
1004 int uid = e->uid;
1006 if (e->indirect_info)
1007 ggc_free (e->indirect_info);
1009 /* Clear out the edge so we do not dangle pointers. */
1010 memset (e, 0, sizeof (*e));
1011 e->uid = uid;
1012 NEXT_FREE_EDGE (e) = free_edges;
1013 free_edges = e;
1016 /* Remove the edge E in the cgraph. */
1018 void
1019 cgraph_remove_edge (struct cgraph_edge *e)
1021 /* Call all edge removal hooks. */
1022 cgraph_call_edge_removal_hooks (e);
1024 if (!e->indirect_unknown_callee)
1025 /* Remove from callers list of the callee. */
1026 cgraph_edge_remove_callee (e);
1028 /* Remove from callees list of the callers. */
1029 cgraph_edge_remove_caller (e);
1031 /* Put the edge onto the free list. */
1032 cgraph_free_edge (e);
1035 /* Set callee of call graph edge E and add it to the corresponding set of
1036 callers. */
1038 static void
1039 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1041 e->prev_caller = NULL;
1042 if (n->callers)
1043 n->callers->prev_caller = e;
1044 e->next_caller = n->callers;
1045 n->callers = e;
1046 e->callee = n;
1049 /* Turn edge E into speculative call calling N2. Update
1050 the profile so the direct call is taken COUNT times
1051 with FREQUENCY.
1053 At clone materialization time, the indirect call E will
1054 be expanded as:
1056 if (call_dest == N2)
1057 n2 ();
1058 else
1059 call call_dest
1061 At this time the function just creates the direct call,
1062 the referencd representing the if conditional and attaches
1063 them all to the orginal indirect call statement.
1065 Return direct edge created. */
1067 struct cgraph_edge *
1068 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1069 struct cgraph_node *n2,
1070 gcov_type direct_count,
1071 int direct_frequency)
1073 struct cgraph_node *n = e->caller;
1074 struct ipa_ref *ref;
1075 struct cgraph_edge *e2;
1077 if (dump_file)
1079 fprintf (dump_file, "Indirect call -> speculative call"
1080 " %s/%i => %s/%i\n",
1081 xstrdup (cgraph_node_name (n)), n->symbol.order,
1082 xstrdup (cgraph_node_name (n2)), n2->symbol.order);
1084 e->speculative = true;
1085 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1086 initialize_inline_failed (e2);
1087 e2->speculative = true;
1088 if (TREE_NOTHROW (n2->symbol.decl))
1089 e2->can_throw_external = false;
1090 else
1091 e2->can_throw_external = e->can_throw_external;
1092 e2->lto_stmt_uid = e->lto_stmt_uid;
1093 e->count -= e2->count;
1094 e->frequency -= e2->frequency;
1095 cgraph_call_edge_duplication_hooks (e, e2);
1096 ref = ipa_record_reference ((symtab_node)n, (symtab_node)n2,
1097 IPA_REF_ADDR, e->call_stmt);
1098 ref->lto_stmt_uid = e->lto_stmt_uid;
1099 ref->speculative = e->speculative;
1100 cgraph_mark_address_taken_node (n2);
1101 return e2;
1104 /* Speculative call consist of three components:
1105 1) an indirect edge representing the original call
1106 2) an direct edge representing the new call
1107 3) ADDR_EXPR reference representing the speculative check.
1108 All three components are attached to single statement (the indirect
1109 call) and if one of them exists, all of them must exist.
1111 Given speculative call edge E, return all three components.
1114 void
1115 cgraph_speculative_call_info (struct cgraph_edge *e,
1116 struct cgraph_edge *&direct,
1117 struct cgraph_edge *&indirect,
1118 struct ipa_ref *&reference)
1120 struct ipa_ref *ref;
1121 int i;
1122 struct cgraph_edge *e2;
1124 if (!e->indirect_unknown_callee)
1125 for (e2 = e->caller->indirect_calls;
1126 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1127 e2 = e2->next_callee)
1129 else
1131 e2 = e;
1132 /* We can take advantage of the call stmt hash. */
1133 if (e2->call_stmt)
1135 e = cgraph_edge (e->caller, e2->call_stmt);
1136 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1138 else
1139 for (e = e->caller->callees;
1140 e2->call_stmt != e->call_stmt
1141 || e2->lto_stmt_uid != e->lto_stmt_uid;
1142 e = e->next_callee)
1145 gcc_assert (e->speculative && e2->speculative);
1146 direct = e;
1147 indirect = e2;
1149 reference = NULL;
1150 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->symbol.ref_list,
1151 i, ref); i++)
1152 if (ref->speculative
1153 && ((ref->stmt && ref->stmt == e->call_stmt)
1154 || (ref->lto_stmt_uid == e->lto_stmt_uid)))
1156 reference = ref;
1157 break;
1160 /* Speculative edge always consist of all three components - direct edge,
1161 indirect and reference. */
1163 gcc_assert (e && e2 && ref);
1166 /* Redirect callee of E to N. The function does not update underlying
1167 call expression. */
1169 void
1170 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1172 /* Remove from callers list of the current callee. */
1173 cgraph_edge_remove_callee (e);
1175 /* Insert to callers list of the new callee. */
1176 cgraph_set_edge_callee (e, n);
1179 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1180 Remove the speculative call sequence and return edge representing the call.
1181 It is up to caller to redirect the call as appropriate. */
1183 struct cgraph_edge *
1184 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1186 struct cgraph_edge *e2;
1187 struct ipa_ref *ref;
1189 gcc_assert (edge->speculative);
1190 cgraph_speculative_call_info (edge, e2, edge, ref);
1191 if (ref->referred->symbol.decl != callee_decl)
1193 if (dump_file)
1195 if (callee_decl)
1197 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1198 "turned out to have contradicting known target ",
1199 xstrdup (cgraph_node_name (edge->caller)), edge->caller->symbol.order,
1200 xstrdup (cgraph_node_name (e2->callee)), e2->callee->symbol.order);
1201 print_generic_expr (dump_file, callee_decl, 0);
1202 fprintf (dump_file, "\n");
1204 else
1206 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1207 xstrdup (cgraph_node_name (edge->caller)), edge->caller->symbol.order,
1208 xstrdup (cgraph_node_name (e2->callee)), e2->callee->symbol.order);
1212 else
1214 struct cgraph_edge *tmp = edge;
1215 if (dump_file)
1216 fprintf (dump_file, "Speculative call turned into direct call.\n");
1217 edge = e2;
1218 e2 = tmp;
1219 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1220 in the functions inlined through it. */
1222 edge->count += e2->count;
1223 edge->frequency += e2->frequency;
1224 if (edge->frequency > CGRAPH_FREQ_MAX)
1225 edge->frequency = CGRAPH_FREQ_MAX;
1226 edge->speculative = false;
1227 e2->speculative = false;
1228 if (e2->indirect_unknown_callee || e2->inline_failed)
1229 cgraph_remove_edge (e2);
1230 else
1231 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1232 if (edge->caller->call_site_hash)
1233 cgraph_update_edge_in_call_site_hash (edge);
1234 ipa_remove_reference (ref);
1235 return edge;
1238 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1239 CALLEE. DELTA is an integer constant that is to be added to the this
1240 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1242 struct cgraph_edge *
1243 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1245 gcc_assert (edge->indirect_unknown_callee);
1247 /* If we are redirecting speculative call, make it non-speculative. */
1248 if (edge->indirect_unknown_callee && edge->speculative)
1250 edge = cgraph_resolve_speculation (edge, callee->symbol.decl);
1252 /* On successful speculation just return the pre existing direct edge. */
1253 if (!edge->indirect_unknown_callee)
1254 return edge;
1257 edge->indirect_unknown_callee = 0;
1258 ggc_free (edge->indirect_info);
1259 edge->indirect_info = NULL;
1261 /* Get the edge out of the indirect edge list. */
1262 if (edge->prev_callee)
1263 edge->prev_callee->next_callee = edge->next_callee;
1264 if (edge->next_callee)
1265 edge->next_callee->prev_callee = edge->prev_callee;
1266 if (!edge->prev_callee)
1267 edge->caller->indirect_calls = edge->next_callee;
1269 /* Put it into the normal callee list */
1270 edge->prev_callee = NULL;
1271 edge->next_callee = edge->caller->callees;
1272 if (edge->caller->callees)
1273 edge->caller->callees->prev_callee = edge;
1274 edge->caller->callees = edge;
1276 /* Insert to callers list of the new callee. */
1277 cgraph_set_edge_callee (edge, callee);
1279 if (edge->call_stmt)
1280 edge->call_stmt_cannot_inline_p
1281 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl,
1282 false);
1284 /* We need to re-determine the inlining status of the edge. */
1285 initialize_inline_failed (edge);
1286 return edge;
1289 /* If necessary, change the function declaration in the call statement
1290 associated with E so that it corresponds to the edge callee. */
1292 gimple
1293 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1295 tree decl = gimple_call_fndecl (e->call_stmt);
1296 gimple new_stmt;
1297 gimple_stmt_iterator gsi;
1298 #ifdef ENABLE_CHECKING
1299 struct cgraph_node *node;
1300 #endif
1302 if (e->speculative)
1304 struct cgraph_edge *e2;
1305 gimple new_stmt;
1306 struct ipa_ref *ref;
1308 cgraph_speculative_call_info (e, e, e2, ref);
1309 /* If there already is an direct call (i.e. as a result of inliner's substitution),
1310 forget about speculating. */
1311 if (decl)
1312 e = cgraph_resolve_speculation (e, decl);
1313 /* If types do not match, speculation was likely wrong. */
1314 else if (!gimple_check_call_matching_types (e->call_stmt, e->callee->symbol.decl,
1315 true))
1317 if (dump_file)
1318 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1319 "Type mismatch.\n",
1320 xstrdup (cgraph_node_name (e->caller)), e->caller->symbol.order,
1321 xstrdup (cgraph_node_name (e->callee)), e->callee->symbol.order);
1322 e = cgraph_resolve_speculation (e, NULL);
1324 /* Expand speculation into GIMPLE code. */
1325 else
1327 if (dump_file)
1328 fprintf (dump_file, "Expanding speculative call of %s/%i -> %s/%i count:"
1329 HOST_WIDEST_INT_PRINT_DEC"\n",
1330 xstrdup (cgraph_node_name (e->caller)), e->caller->symbol.order,
1331 xstrdup (cgraph_node_name (e->callee)), e->callee->symbol.order,
1332 (HOST_WIDEST_INT)e->count);
1333 gcc_assert (e2->speculative);
1334 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
1335 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1336 e->count || e2->count
1337 ? RDIV (e->count * REG_BR_PROB_BASE,
1338 e->count + e2->count)
1339 : e->frequency || e2->frequency
1340 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1341 e->frequency + e2->frequency)
1342 : REG_BR_PROB_BASE / 2,
1343 e->count, e->count + e2->count);
1344 e->speculative = false;
1345 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1346 e->frequency = compute_call_stmt_bb_frequency (e->caller->symbol.decl,
1347 gimple_bb (e->call_stmt));
1348 e2->frequency = compute_call_stmt_bb_frequency (e2->caller->symbol.decl,
1349 gimple_bb (e2->call_stmt));
1350 e2->speculative = false;
1351 ref->speculative = false;
1352 ref->stmt = NULL;
1353 /* Indirect edges are not both in the call site hash.
1354 get it updated. */
1355 if (e->caller->call_site_hash)
1356 cgraph_update_edge_in_call_site_hash (e2);
1357 pop_cfun ();
1358 /* Continue redirecting E to proper target. */
1362 if (e->indirect_unknown_callee
1363 || decl == e->callee->symbol.decl)
1364 return e->call_stmt;
1366 #ifdef ENABLE_CHECKING
1367 if (decl)
1369 node = cgraph_get_node (decl);
1370 gcc_assert (!node || !node->clone.combined_args_to_skip);
1372 #endif
1374 if (cgraph_dump_file)
1376 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1377 xstrdup (cgraph_node_name (e->caller)), e->caller->symbol.order,
1378 xstrdup (cgraph_node_name (e->callee)), e->callee->symbol.order);
1379 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1380 if (e->callee->clone.combined_args_to_skip)
1382 fprintf (cgraph_dump_file, " combined args to skip: ");
1383 dump_bitmap (cgraph_dump_file,
1384 e->callee->clone.combined_args_to_skip);
1388 if (e->callee->clone.combined_args_to_skip)
1390 int lp_nr;
1392 new_stmt
1393 = gimple_call_copy_skip_args (e->call_stmt,
1394 e->callee->clone.combined_args_to_skip);
1395 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1396 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1398 if (gimple_vdef (new_stmt)
1399 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1400 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1402 gsi = gsi_for_stmt (e->call_stmt);
1403 gsi_replace (&gsi, new_stmt, false);
1404 /* We need to defer cleaning EH info on the new statement to
1405 fixup-cfg. We may not have dominator information at this point
1406 and thus would end up with unreachable blocks and have no way
1407 to communicate that we need to run CFG cleanup then. */
1408 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1409 if (lp_nr != 0)
1411 remove_stmt_from_eh_lp (e->call_stmt);
1412 add_stmt_to_eh_lp (new_stmt, lp_nr);
1415 else
1417 new_stmt = e->call_stmt;
1418 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1419 update_stmt (new_stmt);
1422 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1424 if (cgraph_dump_file)
1426 fprintf (cgraph_dump_file, " updated to:");
1427 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1429 return new_stmt;
1432 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1433 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1434 of OLD_STMT if it was previously call statement.
1435 If NEW_STMT is NULL, the call has been dropped without any
1436 replacement. */
1438 static void
1439 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1440 gimple old_stmt, tree old_call,
1441 gimple new_stmt)
1443 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1444 ? gimple_call_fndecl (new_stmt) : 0;
1446 /* We are seeing indirect calls, then there is nothing to update. */
1447 if (!new_call && !old_call)
1448 return;
1449 /* See if we turned indirect call into direct call or folded call to one builtin
1450 into different builtin. */
1451 if (old_call != new_call)
1453 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1454 struct cgraph_edge *ne = NULL;
1455 gcov_type count;
1456 int frequency;
1458 if (e)
1460 /* See if the edge is already there and has the correct callee. It
1461 might be so because of indirect inlining has already updated
1462 it. We also might've cloned and redirected the edge. */
1463 if (new_call && e->callee)
1465 struct cgraph_node *callee = e->callee;
1466 while (callee)
1468 if (callee->symbol.decl == new_call
1469 || callee->former_clone_of == new_call)
1470 return;
1471 callee = callee->clone_of;
1475 /* Otherwise remove edge and create new one; we can't simply redirect
1476 since function has changed, so inline plan and other information
1477 attached to edge is invalid. */
1478 count = e->count;
1479 frequency = e->frequency;
1480 cgraph_remove_edge (e);
1482 else if (new_call)
1484 /* We are seeing new direct call; compute profile info based on BB. */
1485 basic_block bb = gimple_bb (new_stmt);
1486 count = bb->count;
1487 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1488 bb);
1491 if (new_call)
1493 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1494 new_stmt, count, frequency);
1495 gcc_assert (ne->inline_failed);
1498 /* We only updated the call stmt; update pointer in cgraph edge.. */
1499 else if (old_stmt != new_stmt)
1500 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1503 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1504 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1505 of OLD_STMT before it was updated (updating can happen inplace). */
1507 void
1508 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1510 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1511 struct cgraph_node *node;
1513 gcc_checking_assert (orig);
1514 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1515 if (orig->clones)
1516 for (node = orig->clones; node != orig;)
1518 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1519 if (node->clones)
1520 node = node->clones;
1521 else if (node->next_sibling_clone)
1522 node = node->next_sibling_clone;
1523 else
1525 while (node != orig && !node->next_sibling_clone)
1526 node = node->clone_of;
1527 if (node != orig)
1528 node = node->next_sibling_clone;
1534 /* Remove all callees from the node. */
1536 void
1537 cgraph_node_remove_callees (struct cgraph_node *node)
1539 struct cgraph_edge *e, *f;
1541 /* It is sufficient to remove the edges from the lists of callers of
1542 the callees. The callee list of the node can be zapped with one
1543 assignment. */
1544 for (e = node->callees; e; e = f)
1546 f = e->next_callee;
1547 cgraph_call_edge_removal_hooks (e);
1548 if (!e->indirect_unknown_callee)
1549 cgraph_edge_remove_callee (e);
1550 cgraph_free_edge (e);
1552 for (e = node->indirect_calls; e; e = f)
1554 f = e->next_callee;
1555 cgraph_call_edge_removal_hooks (e);
1556 if (!e->indirect_unknown_callee)
1557 cgraph_edge_remove_callee (e);
1558 cgraph_free_edge (e);
1560 node->indirect_calls = NULL;
1561 node->callees = NULL;
1562 if (node->call_site_hash)
1564 htab_delete (node->call_site_hash);
1565 node->call_site_hash = NULL;
1569 /* Remove all callers from the node. */
1571 static void
1572 cgraph_node_remove_callers (struct cgraph_node *node)
1574 struct cgraph_edge *e, *f;
1576 /* It is sufficient to remove the edges from the lists of callees of
1577 the callers. The caller list of the node can be zapped with one
1578 assignment. */
1579 for (e = node->callers; e; e = f)
1581 f = e->next_caller;
1582 cgraph_call_edge_removal_hooks (e);
1583 cgraph_edge_remove_caller (e);
1584 cgraph_free_edge (e);
1586 node->callers = NULL;
1589 /* Helper function for cgraph_release_function_body and free_lang_data.
1590 It releases body from function DECL without having to inspect its
1591 possibly non-existent symtab node. */
1593 void
1594 release_function_body (tree decl)
1596 if (DECL_STRUCT_FUNCTION (decl))
1598 push_cfun (DECL_STRUCT_FUNCTION (decl));
1599 if (cfun->cfg
1600 && current_loops)
1602 cfun->curr_properties &= ~PROP_loops;
1603 loop_optimizer_finalize ();
1605 if (cfun->gimple_df)
1607 delete_tree_ssa ();
1608 delete_tree_cfg_annotations ();
1609 cfun->eh = NULL;
1611 if (cfun->cfg)
1613 gcc_assert (dom_computed[0] == DOM_NONE);
1614 gcc_assert (dom_computed[1] == DOM_NONE);
1615 clear_edges ();
1616 cfun->cfg = NULL;
1618 if (cfun->value_histograms)
1619 free_histograms ();
1620 pop_cfun();
1621 gimple_set_body (decl, NULL);
1622 /* Struct function hangs a lot of data that would leak if we didn't
1623 removed all pointers to it. */
1624 ggc_free (DECL_STRUCT_FUNCTION (decl));
1625 DECL_STRUCT_FUNCTION (decl) = NULL;
1627 DECL_SAVED_TREE (decl) = NULL;
1630 /* Release memory used to represent body of function NODE.
1631 Use this only for functions that are released before being translated to
1632 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1633 are free'd in final.c via free_after_compilation(). */
1635 void
1636 cgraph_release_function_body (struct cgraph_node *node)
1638 node->ipa_transforms_to_apply.release ();
1639 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1641 DECL_RESULT (node->symbol.decl) = NULL;
1642 DECL_ARGUMENTS (node->symbol.decl) = NULL;
1644 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1645 of its associated function function declaration because it's
1646 needed to emit debug info later. */
1647 if (!node->used_as_abstract_origin && DECL_INITIAL (node->symbol.decl))
1648 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1649 release_function_body (node->symbol.decl);
1652 /* Remove the node from cgraph. */
1654 void
1655 cgraph_remove_node (struct cgraph_node *node)
1657 struct cgraph_node *n;
1658 int uid = node->uid;
1660 cgraph_call_node_removal_hooks (node);
1661 cgraph_node_remove_callers (node);
1662 cgraph_node_remove_callees (node);
1663 node->ipa_transforms_to_apply.release ();
1665 /* Incremental inlining access removed nodes stored in the postorder list.
1667 node->symbol.force_output = false;
1668 node->symbol.forced_by_abi = false;
1669 for (n = node->nested; n; n = n->next_nested)
1670 n->origin = NULL;
1671 node->nested = NULL;
1672 if (node->origin)
1674 struct cgraph_node **node2 = &node->origin->nested;
1676 while (*node2 != node)
1677 node2 = &(*node2)->next_nested;
1678 *node2 = node->next_nested;
1680 symtab_unregister_node ((symtab_node)node);
1681 if (node->prev_sibling_clone)
1682 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1683 else if (node->clone_of)
1684 node->clone_of->clones = node->next_sibling_clone;
1685 if (node->next_sibling_clone)
1686 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1687 if (node->clones)
1689 struct cgraph_node *n, *next;
1691 if (node->clone_of)
1693 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1694 n->clone_of = node->clone_of;
1695 n->clone_of = node->clone_of;
1696 n->next_sibling_clone = node->clone_of->clones;
1697 if (node->clone_of->clones)
1698 node->clone_of->clones->prev_sibling_clone = n;
1699 node->clone_of->clones = node->clones;
1701 else
1703 /* We are removing node with clones. This makes clones inconsistent,
1704 but assume they will be removed subsequently and just keep clone
1705 tree intact. This can happen in unreachable function removal since
1706 we remove unreachable functions in random order, not by bottom-up
1707 walk of clone trees. */
1708 for (n = node->clones; n; n = next)
1710 next = n->next_sibling_clone;
1711 n->next_sibling_clone = NULL;
1712 n->prev_sibling_clone = NULL;
1713 n->clone_of = NULL;
1718 /* While all the clones are removed after being proceeded, the function
1719 itself is kept in the cgraph even after it is compiled. Check whether
1720 we are done with this body and reclaim it proactively if this is the case.
1722 if (cgraph_state != CGRAPH_LTO_STREAMING)
1724 n = cgraph_get_node (node->symbol.decl);
1725 if (!n
1726 || (!n->clones && !n->clone_of && !n->global.inlined_to
1727 && (cgraph_global_info_ready
1728 && (TREE_ASM_WRITTEN (n->symbol.decl)
1729 || DECL_EXTERNAL (n->symbol.decl)
1730 || !n->symbol.analyzed
1731 || (!flag_wpa && n->symbol.in_other_partition)))))
1732 cgraph_release_function_body (node);
1735 node->symbol.decl = NULL;
1736 if (node->call_site_hash)
1738 htab_delete (node->call_site_hash);
1739 node->call_site_hash = NULL;
1741 cgraph_n_nodes--;
1743 /* Clear out the node to NULL all pointers and add the node to the free
1744 list. */
1745 memset (node, 0, sizeof(*node));
1746 node->symbol.type = SYMTAB_FUNCTION;
1747 node->uid = uid;
1748 SET_NEXT_FREE_NODE (node, free_nodes);
1749 free_nodes = node;
1752 /* Likewise indicate that a node is having address taken. */
1754 void
1755 cgraph_mark_address_taken_node (struct cgraph_node *node)
1757 /* Indirect inlining can figure out that all uses of the address are
1758 inlined. */
1759 if (node->global.inlined_to)
1761 gcc_assert (cfun->after_inlining);
1762 gcc_assert (node->callers->indirect_inlining_edge);
1763 return;
1765 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1766 IPA_REF_ADDR reference exists (and thus it should be set on node
1767 representing alias we take address of) and as a test whether address
1768 of the object was taken (and thus it should be set on node alias is
1769 referring to). We should remove the first use and the remove the
1770 following set. */
1771 node->symbol.address_taken = 1;
1772 node = cgraph_function_or_thunk_node (node, NULL);
1773 node->symbol.address_taken = 1;
1776 /* Return local info for the compiled function. */
1778 struct cgraph_local_info *
1779 cgraph_local_info (tree decl)
1781 struct cgraph_node *node;
1783 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1784 node = cgraph_get_node (decl);
1785 if (!node)
1786 return NULL;
1787 return &node->local;
1790 /* Return local info for the compiled function. */
1792 struct cgraph_global_info *
1793 cgraph_global_info (tree decl)
1795 struct cgraph_node *node;
1797 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1798 node = cgraph_get_node (decl);
1799 if (!node)
1800 return NULL;
1801 return &node->global;
1804 /* Return local info for the compiled function. */
1806 struct cgraph_rtl_info *
1807 cgraph_rtl_info (tree decl)
1809 struct cgraph_node *node;
1811 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1812 node = cgraph_get_node (decl);
1813 if (!node
1814 || (decl != current_function_decl
1815 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1816 return NULL;
1817 return &node->rtl;
1820 /* Return a string describing the failure REASON. */
1822 const char*
1823 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1825 #undef DEFCIFCODE
1826 #define DEFCIFCODE(code, string) string,
1828 static const char *cif_string_table[CIF_N_REASONS] = {
1829 #include "cif-code.def"
1832 /* Signedness of an enum type is implementation defined, so cast it
1833 to unsigned before testing. */
1834 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1835 return cif_string_table[reason];
1838 /* Names used to print out the availability enum. */
1839 const char * const cgraph_availability_names[] =
1840 {"unset", "not_available", "overwritable", "available", "local"};
1843 /* Dump call graph node NODE to file F. */
1845 void
1846 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1848 struct cgraph_edge *edge;
1849 int indirect_calls_count = 0;
1851 dump_symtab_base (f, (symtab_node) node);
1853 if (node->global.inlined_to)
1854 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1855 xstrdup (cgraph_node_name (node)),
1856 node->symbol.order,
1857 xstrdup (cgraph_node_name (node->global.inlined_to)),
1858 node->global.inlined_to->symbol.order);
1859 if (node->clone_of)
1860 fprintf (f, " Clone of %s/%i\n",
1861 cgraph_node_asm_name (node->clone_of),
1862 node->clone_of->symbol.order);
1863 if (cgraph_function_flags_ready)
1864 fprintf (f, " Availability: %s\n",
1865 cgraph_availability_names [cgraph_function_body_availability (node)]);
1867 if (node->profile_id)
1868 fprintf (f, " Profile id: %i\n",
1869 node->profile_id);
1870 fprintf (f, " Function flags:");
1871 if (node->count)
1872 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1873 (HOST_WIDEST_INT)node->count);
1874 if (node->origin)
1875 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1876 if (gimple_has_body_p (node->symbol.decl))
1877 fprintf (f, " body");
1878 if (node->process)
1879 fprintf (f, " process");
1880 if (node->local.local)
1881 fprintf (f, " local");
1882 if (node->local.redefined_extern_inline)
1883 fprintf (f, " redefined_extern_inline");
1884 if (node->only_called_at_startup)
1885 fprintf (f, " only_called_at_startup");
1886 if (node->only_called_at_exit)
1887 fprintf (f, " only_called_at_exit");
1888 if (node->tm_clone)
1889 fprintf (f, " tm_clone");
1891 fprintf (f, "\n");
1893 if (node->thunk.thunk_p)
1895 fprintf (f, " Thunk");
1896 if (node->thunk.alias)
1897 fprintf (f, " of %s (asm: %s)",
1898 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1899 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1900 fprintf (f, " fixed offset %i virtual value %i has "
1901 "virtual offset %i)\n",
1902 (int)node->thunk.fixed_offset,
1903 (int)node->thunk.virtual_value,
1904 (int)node->thunk.virtual_offset_p);
1906 if (node->symbol.alias && node->thunk.alias
1907 && DECL_P (node->thunk.alias))
1909 fprintf (f, " Alias of %s",
1910 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1911 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1912 fprintf (f, " (asm: %s)",
1913 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1914 fprintf (f, "\n");
1917 fprintf (f, " Called by: ");
1919 for (edge = node->callers; edge; edge = edge->next_caller)
1921 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1922 edge->caller->symbol.order);
1923 if (edge->count)
1924 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1925 (HOST_WIDEST_INT)edge->count);
1926 if (edge->frequency)
1927 fprintf (f, "(%.2f per call) ",
1928 edge->frequency / (double)CGRAPH_FREQ_BASE);
1929 if (edge->speculative)
1930 fprintf(f, "(speculative) ");
1931 if (!edge->inline_failed)
1932 fprintf(f, "(inlined) ");
1933 if (edge->indirect_inlining_edge)
1934 fprintf(f, "(indirect_inlining) ");
1935 if (edge->can_throw_external)
1936 fprintf(f, "(can throw external) ");
1939 fprintf (f, "\n Calls: ");
1940 for (edge = node->callees; edge; edge = edge->next_callee)
1942 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1943 edge->callee->symbol.order);
1944 if (edge->speculative)
1945 fprintf(f, "(speculative) ");
1946 if (!edge->inline_failed)
1947 fprintf(f, "(inlined) ");
1948 if (edge->indirect_inlining_edge)
1949 fprintf(f, "(indirect_inlining) ");
1950 if (edge->count)
1951 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1952 (HOST_WIDEST_INT)edge->count);
1953 if (edge->frequency)
1954 fprintf (f, "(%.2f per call) ",
1955 edge->frequency / (double)CGRAPH_FREQ_BASE);
1956 if (edge->can_throw_external)
1957 fprintf(f, "(can throw external) ");
1959 fprintf (f, "\n");
1961 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1962 indirect_calls_count++;
1963 if (indirect_calls_count)
1964 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1965 indirect_calls_count);
1969 /* Dump call graph node NODE to stderr. */
1971 DEBUG_FUNCTION void
1972 debug_cgraph_node (struct cgraph_node *node)
1974 dump_cgraph_node (stderr, node);
1978 /* Dump the callgraph to file F. */
1980 void
1981 dump_cgraph (FILE *f)
1983 struct cgraph_node *node;
1985 fprintf (f, "callgraph:\n\n");
1986 FOR_EACH_FUNCTION (node)
1987 dump_cgraph_node (f, node);
1991 /* Dump the call graph to stderr. */
1993 DEBUG_FUNCTION void
1994 debug_cgraph (void)
1996 dump_cgraph (stderr);
1999 /* Return true when the DECL can possibly be inlined. */
2000 bool
2001 cgraph_function_possibly_inlined_p (tree decl)
2003 if (!cgraph_global_info_ready)
2004 return !DECL_UNINLINABLE (decl);
2005 return DECL_POSSIBLY_INLINED (decl);
2008 /* NODE is no longer nested function; update cgraph accordingly. */
2009 void
2010 cgraph_unnest_node (struct cgraph_node *node)
2012 struct cgraph_node **node2 = &node->origin->nested;
2013 gcc_assert (node->origin);
2015 while (*node2 != node)
2016 node2 = &(*node2)->next_nested;
2017 *node2 = node->next_nested;
2018 node->origin = NULL;
2021 /* Return function availability. See cgraph.h for description of individual
2022 return values. */
2023 enum availability
2024 cgraph_function_body_availability (struct cgraph_node *node)
2026 enum availability avail;
2027 if (!node->symbol.analyzed)
2028 avail = AVAIL_NOT_AVAILABLE;
2029 else if (node->local.local)
2030 avail = AVAIL_LOCAL;
2031 else if (!node->symbol.externally_visible)
2032 avail = AVAIL_AVAILABLE;
2033 /* Inline functions are safe to be analyzed even if their symbol can
2034 be overwritten at runtime. It is not meaningful to enforce any sane
2035 behaviour on replacing inline function by different body. */
2036 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
2037 avail = AVAIL_AVAILABLE;
2039 /* If the function can be overwritten, return OVERWRITABLE. Take
2040 care at least of two notable extensions - the COMDAT functions
2041 used to share template instantiations in C++ (this is symmetric
2042 to code cp_cannot_inline_tree_fn and probably shall be shared and
2043 the inlinability hooks completely eliminated).
2045 ??? Does the C++ one definition rule allow us to always return
2046 AVAIL_AVAILABLE here? That would be good reason to preserve this
2047 bit. */
2049 else if (decl_replaceable_p (node->symbol.decl)
2050 && !DECL_EXTERNAL (node->symbol.decl))
2051 avail = AVAIL_OVERWRITABLE;
2052 else avail = AVAIL_AVAILABLE;
2054 return avail;
2057 /* Worker for cgraph_node_can_be_local_p. */
2058 static bool
2059 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2060 void *data ATTRIBUTE_UNUSED)
2062 return !(!node->symbol.force_output
2063 && ((DECL_COMDAT (node->symbol.decl)
2064 && !node->symbol.forced_by_abi
2065 && !symtab_used_from_object_file_p ((symtab_node) node)
2066 && !node->symbol.same_comdat_group)
2067 || !node->symbol.externally_visible));
2070 /* Return true if NODE can be made local for API change.
2071 Extern inline functions and C++ COMDAT functions can be made local
2072 at the expense of possible code size growth if function is used in multiple
2073 compilation units. */
2074 bool
2075 cgraph_node_can_be_local_p (struct cgraph_node *node)
2077 return (!node->symbol.address_taken
2078 && !cgraph_for_node_and_aliases (node,
2079 cgraph_node_cannot_be_local_p_1,
2080 NULL, true));
2083 /* Call calback on NODE, thunks and aliases associated to NODE.
2084 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2085 skipped. */
2087 bool
2088 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2089 bool (*callback) (struct cgraph_node *, void *),
2090 void *data,
2091 bool include_overwritable)
2093 struct cgraph_edge *e;
2094 int i;
2095 struct ipa_ref *ref;
2097 if (callback (node, data))
2098 return true;
2099 for (e = node->callers; e; e = e->next_caller)
2100 if (e->caller->thunk.thunk_p
2101 && (include_overwritable
2102 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2103 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2104 include_overwritable))
2105 return true;
2106 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
2107 if (ref->use == IPA_REF_ALIAS)
2109 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2110 if (include_overwritable
2111 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2112 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2113 include_overwritable))
2114 return true;
2116 return false;
2119 /* Call calback on NODE and aliases associated to NODE.
2120 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2121 skipped. */
2123 bool
2124 cgraph_for_node_and_aliases (struct cgraph_node *node,
2125 bool (*callback) (struct cgraph_node *, void *),
2126 void *data,
2127 bool include_overwritable)
2129 int i;
2130 struct ipa_ref *ref;
2132 if (callback (node, data))
2133 return true;
2134 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
2135 if (ref->use == IPA_REF_ALIAS)
2137 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2138 if (include_overwritable
2139 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2140 if (cgraph_for_node_and_aliases (alias, callback, data,
2141 include_overwritable))
2142 return true;
2144 return false;
2147 /* Worker to bring NODE local. */
2149 static bool
2150 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2152 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2153 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
2155 symtab_make_decl_local (node->symbol.decl);
2157 node->symbol.externally_visible = false;
2158 node->symbol.forced_by_abi = false;
2159 node->local.local = true;
2160 node->symbol.unique_name = (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
2161 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2162 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
2163 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2165 return false;
2168 /* Bring NODE local. */
2170 void
2171 cgraph_make_node_local (struct cgraph_node *node)
2173 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2174 NULL, true);
2177 /* Worker to set nothrow flag. */
2179 static bool
2180 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2182 struct cgraph_edge *e;
2184 TREE_NOTHROW (node->symbol.decl) = data != NULL;
2186 if (data != NULL)
2187 for (e = node->callers; e; e = e->next_caller)
2188 e->can_throw_external = false;
2189 return false;
2192 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2193 if any to NOTHROW. */
2195 void
2196 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2198 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2199 (void *)(size_t)nothrow, false);
2202 /* Worker to set const flag. */
2204 static bool
2205 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2207 /* Static constructors and destructors without a side effect can be
2208 optimized out. */
2209 if (data && !((size_t)data & 2))
2211 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
2212 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
2213 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2214 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
2216 TREE_READONLY (node->symbol.decl) = data != NULL;
2217 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
2218 return false;
2221 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2222 if any to READONLY. */
2224 void
2225 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2227 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2228 (void *)(size_t)(readonly + (int)looping * 2),
2229 false);
2232 /* Worker to set pure flag. */
2234 static bool
2235 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2237 /* Static constructors and destructors without a side effect can be
2238 optimized out. */
2239 if (data && !((size_t)data & 2))
2241 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
2242 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
2243 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2244 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
2246 DECL_PURE_P (node->symbol.decl) = data != NULL;
2247 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
2248 return false;
2251 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2252 if any to PURE. */
2254 void
2255 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2257 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2258 (void *)(size_t)(pure + (int)looping * 2),
2259 false);
2262 /* Data used by cgraph_propagate_frequency. */
2264 struct cgraph_propagate_frequency_data
2266 bool maybe_unlikely_executed;
2267 bool maybe_executed_once;
2268 bool only_called_at_startup;
2269 bool only_called_at_exit;
2272 /* Worker for cgraph_propagate_frequency_1. */
2274 static bool
2275 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
2277 struct cgraph_propagate_frequency_data *d;
2278 struct cgraph_edge *edge;
2280 d = (struct cgraph_propagate_frequency_data *)data;
2281 for (edge = node->callers;
2282 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
2283 || d->only_called_at_startup || d->only_called_at_exit);
2284 edge = edge->next_caller)
2286 if (edge->caller != node)
2288 d->only_called_at_startup &= edge->caller->only_called_at_startup;
2289 /* It makes sense to put main() together with the static constructors.
2290 It will be executed for sure, but rest of functions called from
2291 main are definitely not at startup only. */
2292 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
2293 d->only_called_at_startup = 0;
2294 d->only_called_at_exit &= edge->caller->only_called_at_exit;
2296 if (!edge->frequency)
2297 continue;
2298 switch (edge->caller->frequency)
2300 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2301 break;
2302 case NODE_FREQUENCY_EXECUTED_ONCE:
2303 if (dump_file && (dump_flags & TDF_DETAILS))
2304 fprintf (dump_file, " Called by %s that is executed once\n",
2305 cgraph_node_name (edge->caller));
2306 d->maybe_unlikely_executed = false;
2307 if (inline_edge_summary (edge)->loop_depth)
2309 d->maybe_executed_once = false;
2310 if (dump_file && (dump_flags & TDF_DETAILS))
2311 fprintf (dump_file, " Called in loop\n");
2313 break;
2314 case NODE_FREQUENCY_HOT:
2315 case NODE_FREQUENCY_NORMAL:
2316 if (dump_file && (dump_flags & TDF_DETAILS))
2317 fprintf (dump_file, " Called by %s that is normal or hot\n",
2318 cgraph_node_name (edge->caller));
2319 d->maybe_unlikely_executed = false;
2320 d->maybe_executed_once = false;
2321 break;
2324 return edge != NULL;
2327 /* See if the frequency of NODE can be updated based on frequencies of its
2328 callers. */
2329 bool
2330 cgraph_propagate_frequency (struct cgraph_node *node)
2332 struct cgraph_propagate_frequency_data d = {true, true, true, true};
2333 bool changed = false;
2335 if (!node->local.local)
2336 return false;
2337 gcc_assert (node->symbol.analyzed);
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2341 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2343 if ((d.only_called_at_startup && !d.only_called_at_exit)
2344 && !node->only_called_at_startup)
2346 node->only_called_at_startup = true;
2347 if (dump_file)
2348 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2349 cgraph_node_name (node));
2350 changed = true;
2352 if ((d.only_called_at_exit && !d.only_called_at_startup)
2353 && !node->only_called_at_exit)
2355 node->only_called_at_exit = true;
2356 if (dump_file)
2357 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2358 cgraph_node_name (node));
2359 changed = true;
2361 /* These come either from profile or user hints; never update them. */
2362 if (node->frequency == NODE_FREQUENCY_HOT
2363 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2364 return changed;
2365 if (d.maybe_unlikely_executed)
2367 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2368 if (dump_file)
2369 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2370 cgraph_node_name (node));
2371 changed = true;
2373 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2375 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2376 if (dump_file)
2377 fprintf (dump_file, "Node %s promoted to executed once.\n",
2378 cgraph_node_name (node));
2379 changed = true;
2381 return changed;
2384 /* Return true when NODE can not return or throw and thus
2385 it is safe to ignore its side effects for IPA analysis. */
2387 bool
2388 cgraph_node_cannot_return (struct cgraph_node *node)
2390 int flags = flags_from_decl_or_type (node->symbol.decl);
2391 if (!flag_exceptions)
2392 return (flags & ECF_NORETURN) != 0;
2393 else
2394 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2395 == (ECF_NORETURN | ECF_NOTHROW));
2398 /* Return true when call of E can not lead to return from caller
2399 and thus it is safe to ignore its side effects for IPA analysis
2400 when computing side effects of the caller.
2401 FIXME: We could actually mark all edges that have no reaching
2402 patch to EXIT_BLOCK_PTR or throw to get better results. */
2403 bool
2404 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2406 if (cgraph_node_cannot_return (e->caller))
2407 return true;
2408 if (e->indirect_unknown_callee)
2410 int flags = e->indirect_info->ecf_flags;
2411 if (!flag_exceptions)
2412 return (flags & ECF_NORETURN) != 0;
2413 else
2414 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2415 == (ECF_NORETURN | ECF_NOTHROW));
2417 else
2418 return cgraph_node_cannot_return (e->callee);
2421 /* Return true when function NODE can be removed from callgraph
2422 if all direct calls are eliminated. */
2424 bool
2425 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2427 gcc_assert (!node->global.inlined_to);
2428 /* Extern inlines can always go, we will use the external definition. */
2429 if (DECL_EXTERNAL (node->symbol.decl))
2430 return true;
2431 /* When function is needed, we can not remove it. */
2432 if (node->symbol.force_output || node->symbol.used_from_other_partition)
2433 return false;
2434 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
2435 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2436 return false;
2437 /* Only COMDAT functions can be removed if externally visible. */
2438 if (node->symbol.externally_visible
2439 && (!DECL_COMDAT (node->symbol.decl)
2440 || node->symbol.forced_by_abi
2441 || symtab_used_from_object_file_p ((symtab_node) node)))
2442 return false;
2443 return true;
2446 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2448 static bool
2449 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2451 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2454 /* Return true when function NODE and its aliases can be removed from callgraph
2455 if all direct calls are eliminated. */
2457 bool
2458 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2460 /* Extern inlines can always go, we will use the external definition. */
2461 if (DECL_EXTERNAL (node->symbol.decl))
2462 return true;
2463 if (node->symbol.address_taken)
2464 return false;
2465 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2468 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2470 static bool
2471 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2473 return symtab_used_from_object_file_p ((symtab_node) node);
2476 /* Return true when function NODE can be expected to be removed
2477 from program when direct calls in this compilation unit are removed.
2479 As a special case COMDAT functions are
2480 cgraph_can_remove_if_no_direct_calls_p while the are not
2481 cgraph_only_called_directly_p (it is possible they are called from other
2482 unit)
2484 This function behaves as cgraph_only_called_directly_p because eliminating
2485 all uses of COMDAT function does not make it necessarily disappear from
2486 the program unless we are compiling whole program or we do LTO. In this
2487 case we know we win since dynamic linking will not really discard the
2488 linkonce section. */
2490 bool
2491 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2493 gcc_assert (!node->global.inlined_to);
2494 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2495 return false;
2496 if (!in_lto_p && !flag_whole_program)
2497 return cgraph_only_called_directly_p (node);
2498 else
2500 if (DECL_EXTERNAL (node->symbol.decl))
2501 return true;
2502 return cgraph_can_remove_if_no_direct_calls_p (node);
2507 /* Worker for cgraph_only_called_directly_p. */
2509 static bool
2510 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2512 return !cgraph_only_called_directly_or_aliased_p (node);
2515 /* Return true when function NODE and all its aliases are only called
2516 directly.
2517 i.e. it is not externally visible, address was not taken and
2518 it is not used in any other non-standard way. */
2520 bool
2521 cgraph_only_called_directly_p (struct cgraph_node *node)
2523 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2524 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2525 NULL, true);
2529 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2531 static bool
2532 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2534 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2535 struct cgraph_edge *cs;
2536 enum availability avail;
2537 cgraph_function_or_thunk_node (node, &avail);
2539 if (avail > AVAIL_OVERWRITABLE)
2540 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2541 if (!cs->indirect_inlining_edge)
2542 redirect_callers->safe_push (cs);
2543 return false;
2546 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2547 (i.e. are not overwritable). */
2549 vec<cgraph_edge_p>
2550 collect_callers_of_node (struct cgraph_node *node)
2552 vec<cgraph_edge_p> redirect_callers = vNULL;
2553 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2554 &redirect_callers, false);
2555 return redirect_callers;
2558 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2559 static bool
2560 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2562 node = cgraph_function_or_thunk_node (node, NULL);
2563 node2 = cgraph_function_or_thunk_node (node2, NULL);
2564 while (node != node2 && node2)
2565 node2 = node2->clone_of;
2566 return node2 != NULL;
2569 /* Verify edge E count and frequency. */
2571 static bool
2572 verify_edge_count_and_frequency (struct cgraph_edge *e)
2574 bool error_found = false;
2575 if (e->count < 0)
2577 error ("caller edge count is negative");
2578 error_found = true;
2580 if (e->frequency < 0)
2582 error ("caller edge frequency is negative");
2583 error_found = true;
2585 if (e->frequency > CGRAPH_FREQ_MAX)
2587 error ("caller edge frequency is too large");
2588 error_found = true;
2590 if (gimple_has_body_p (e->caller->symbol.decl)
2591 && !e->caller->global.inlined_to
2592 && !e->speculative
2593 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2594 Remove this once edges are actually removed from the function at that time. */
2595 && (e->frequency
2596 || (inline_edge_summary_vec.exists ()
2597 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2598 || !inline_edge_summary (e)->predicate)))
2599 && (e->frequency
2600 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2601 gimple_bb (e->call_stmt))))
2603 error ("caller edge frequency %i does not match BB frequency %i",
2604 e->frequency,
2605 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2606 gimple_bb (e->call_stmt)));
2607 error_found = true;
2609 return error_found;
2612 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2613 static void
2614 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2616 bool fndecl_was_null = false;
2617 /* debug_gimple_stmt needs correct cfun */
2618 if (cfun != this_cfun)
2619 set_cfun (this_cfun);
2620 /* ...and an actual current_function_decl */
2621 if (!current_function_decl)
2623 current_function_decl = this_cfun->decl;
2624 fndecl_was_null = true;
2626 debug_gimple_stmt (stmt);
2627 if (fndecl_was_null)
2628 current_function_decl = NULL;
2631 /* Verify that call graph edge E corresponds to DECL from the associated
2632 statement. Return true if the verification should fail. */
2634 static bool
2635 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2637 struct cgraph_node *node;
2639 if (!decl || e->callee->global.inlined_to)
2640 return false;
2641 if (cgraph_state == CGRAPH_LTO_STREAMING)
2642 return false;
2643 node = cgraph_get_node (decl);
2645 /* We do not know if a node from a different partition is an alias or what it
2646 aliases and therefore cannot do the former_clone_of check reliably. */
2647 if (!node || node->symbol.in_other_partition || e->callee->symbol.in_other_partition)
2648 return false;
2649 node = cgraph_function_or_thunk_node (node, NULL);
2651 if (e->callee->former_clone_of != node->symbol.decl
2652 /* IPA-CP sometimes redirect edge to clone and then back to the former
2653 function. This ping-pong has to go, eventually. */
2654 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2655 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2656 return true;
2657 else
2658 return false;
2661 /* Verify cgraph nodes of given cgraph node. */
2662 DEBUG_FUNCTION void
2663 verify_cgraph_node (struct cgraph_node *node)
2665 struct cgraph_edge *e;
2666 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2667 basic_block this_block;
2668 gimple_stmt_iterator gsi;
2669 bool error_found = false;
2671 if (seen_error ())
2672 return;
2674 timevar_push (TV_CGRAPH_VERIFY);
2675 error_found |= verify_symtab_base ((symtab_node) node);
2676 for (e = node->callees; e; e = e->next_callee)
2677 if (e->aux)
2679 error ("aux field set for edge %s->%s",
2680 identifier_to_locale (cgraph_node_name (e->caller)),
2681 identifier_to_locale (cgraph_node_name (e->callee)));
2682 error_found = true;
2684 if (node->count < 0)
2686 error ("execution count is negative");
2687 error_found = true;
2689 if (node->global.inlined_to && node->symbol.same_comdat_group)
2691 error ("inline clone in same comdat group list");
2692 error_found = true;
2694 if (!node->symbol.definition && !node->symbol.in_other_partition && node->local.local)
2696 error ("local symbols must be defined");
2697 error_found = true;
2699 if (node->global.inlined_to && node->symbol.externally_visible)
2701 error ("externally visible inline clone");
2702 error_found = true;
2704 if (node->global.inlined_to && node->symbol.address_taken)
2706 error ("inline clone with address taken");
2707 error_found = true;
2709 if (node->global.inlined_to && node->symbol.force_output)
2711 error ("inline clone is forced to output");
2712 error_found = true;
2714 for (e = node->indirect_calls; e; e = e->next_callee)
2716 if (e->aux)
2718 error ("aux field set for indirect edge from %s",
2719 identifier_to_locale (cgraph_node_name (e->caller)));
2720 error_found = true;
2722 if (!e->indirect_unknown_callee
2723 || !e->indirect_info)
2725 error ("An indirect edge from %s is not marked as indirect or has "
2726 "associated indirect_info, the corresponding statement is: ",
2727 identifier_to_locale (cgraph_node_name (e->caller)));
2728 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2729 error_found = true;
2732 for (e = node->callers; e; e = e->next_caller)
2734 if (verify_edge_count_and_frequency (e))
2735 error_found = true;
2736 if (!e->inline_failed)
2738 if (node->global.inlined_to
2739 != (e->caller->global.inlined_to
2740 ? e->caller->global.inlined_to : e->caller))
2742 error ("inlined_to pointer is wrong");
2743 error_found = true;
2745 if (node->callers->next_caller)
2747 error ("multiple inline callers");
2748 error_found = true;
2751 else
2752 if (node->global.inlined_to)
2754 error ("inlined_to pointer set for noninline callers");
2755 error_found = true;
2758 for (e = node->indirect_calls; e; e = e->next_callee)
2759 if (verify_edge_count_and_frequency (e))
2760 error_found = true;
2761 if (!node->callers && node->global.inlined_to)
2763 error ("inlined_to pointer is set but no predecessors found");
2764 error_found = true;
2766 if (node->global.inlined_to == node)
2768 error ("inlined_to pointer refers to itself");
2769 error_found = true;
2772 if (node->clone_of)
2774 struct cgraph_node *n;
2775 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2776 if (n == node)
2777 break;
2778 if (!n)
2780 error ("node has wrong clone_of");
2781 error_found = true;
2784 if (node->clones)
2786 struct cgraph_node *n;
2787 for (n = node->clones; n; n = n->next_sibling_clone)
2788 if (n->clone_of != node)
2789 break;
2790 if (n)
2792 error ("node has wrong clone list");
2793 error_found = true;
2796 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2798 error ("node is in clone list but it is not clone");
2799 error_found = true;
2801 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2803 error ("node has wrong prev_clone pointer");
2804 error_found = true;
2806 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2808 error ("double linked list of clones corrupted");
2809 error_found = true;
2812 if (node->symbol.analyzed && node->symbol.alias)
2814 bool ref_found = false;
2815 int i;
2816 struct ipa_ref *ref;
2818 if (node->callees)
2820 error ("Alias has call edges");
2821 error_found = true;
2823 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2824 i, ref); i++)
2825 if (ref->use != IPA_REF_ALIAS)
2827 error ("Alias has non-alias reference");
2828 error_found = true;
2830 else if (ref_found)
2832 error ("Alias has more than one alias reference");
2833 error_found = true;
2835 else
2836 ref_found = true;
2837 if (!ref_found)
2839 error ("Analyzed alias has no reference");
2840 error_found = true;
2843 if (node->symbol.analyzed && node->thunk.thunk_p)
2845 if (!node->callees)
2847 error ("No edge out of thunk node");
2848 error_found = true;
2850 else if (node->callees->next_callee)
2852 error ("More than one edge out of thunk node");
2853 error_found = true;
2855 if (gimple_has_body_p (node->symbol.decl))
2857 error ("Thunk is not supposed to have body");
2858 error_found = true;
2861 else if (node->symbol.analyzed && gimple_has_body_p (node->symbol.decl)
2862 && !TREE_ASM_WRITTEN (node->symbol.decl)
2863 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2864 && !flag_wpa)
2866 if (this_cfun->cfg)
2868 pointer_set_t *stmts = pointer_set_create ();
2869 int i;
2870 struct ipa_ref *ref;
2872 /* Reach the trees by walking over the CFG, and note the
2873 enclosing basic-blocks in the call edges. */
2874 FOR_EACH_BB_FN (this_block, this_cfun)
2876 for (gsi = gsi_start_phis (this_block);
2877 !gsi_end_p (gsi); gsi_next (&gsi))
2878 pointer_set_insert (stmts, gsi_stmt (gsi));
2879 for (gsi = gsi_start_bb (this_block);
2880 !gsi_end_p (gsi);
2881 gsi_next (&gsi))
2883 gimple stmt = gsi_stmt (gsi);
2884 pointer_set_insert (stmts, stmt);
2885 if (is_gimple_call (stmt))
2887 struct cgraph_edge *e = cgraph_edge (node, stmt);
2888 tree decl = gimple_call_fndecl (stmt);
2889 if (e)
2891 if (e->aux)
2893 error ("shared call_stmt:");
2894 cgraph_debug_gimple_stmt (this_cfun, stmt);
2895 error_found = true;
2897 if (!e->indirect_unknown_callee)
2899 if (verify_edge_corresponds_to_fndecl (e, decl))
2901 error ("edge points to wrong declaration:");
2902 debug_tree (e->callee->symbol.decl);
2903 fprintf (stderr," Instead of:");
2904 debug_tree (decl);
2905 error_found = true;
2908 else if (decl)
2910 error ("an indirect edge with unknown callee "
2911 "corresponding to a call_stmt with "
2912 "a known declaration:");
2913 error_found = true;
2914 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2916 e->aux = (void *)1;
2918 else if (decl)
2920 error ("missing callgraph edge for call stmt:");
2921 cgraph_debug_gimple_stmt (this_cfun, stmt);
2922 error_found = true;
2927 for (i = 0;
2928 ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref);
2929 i++)
2930 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2932 error ("reference to dead statement");
2933 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2934 error_found = true;
2936 pointer_set_destroy (stmts);
2938 else
2939 /* No CFG available?! */
2940 gcc_unreachable ();
2942 for (e = node->callees; e; e = e->next_callee)
2944 if (!e->aux)
2946 error ("edge %s->%s has no corresponding call_stmt",
2947 identifier_to_locale (cgraph_node_name (e->caller)),
2948 identifier_to_locale (cgraph_node_name (e->callee)));
2949 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2950 error_found = true;
2952 e->aux = 0;
2954 for (e = node->indirect_calls; e; e = e->next_callee)
2956 if (!e->aux && !e->speculative)
2958 error ("an indirect edge from %s has no corresponding call_stmt",
2959 identifier_to_locale (cgraph_node_name (e->caller)));
2960 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2961 error_found = true;
2963 e->aux = 0;
2966 if (error_found)
2968 dump_cgraph_node (stderr, node);
2969 internal_error ("verify_cgraph_node failed");
2971 timevar_pop (TV_CGRAPH_VERIFY);
2974 /* Verify whole cgraph structure. */
2975 DEBUG_FUNCTION void
2976 verify_cgraph (void)
2978 struct cgraph_node *node;
2980 if (seen_error ())
2981 return;
2983 FOR_EACH_FUNCTION (node)
2984 verify_cgraph_node (node);
2987 /* Create external decl node for DECL.
2988 The difference i nbetween cgraph_get_create_node and
2989 cgraph_get_create_real_symbol_node is that cgraph_get_create_node
2990 may return inline clone, while cgraph_get_create_real_symbol_node
2991 will create a new node in this case.
2992 FIXME: This function should be removed once clones are put out of decl
2993 hash. */
2995 struct cgraph_node *
2996 cgraph_get_create_real_symbol_node (tree decl)
2998 struct cgraph_node *first_clone = cgraph_get_node (decl);
2999 struct cgraph_node *node;
3000 /* create symbol table node. even if inline clone exists, we can not take
3001 it as a target of non-inlined call. */
3002 node = cgraph_get_node (decl);
3003 if (node && !node->global.inlined_to)
3004 return node;
3006 node = cgraph_create_node (decl);
3008 /* ok, we previously inlined the function, then removed the offline copy and
3009 now we want it back for external call. this can happen when devirtualizing
3010 while inlining function called once that happens after extern inlined and
3011 virtuals are already removed. in this case introduce the external node
3012 and make it available for call. */
3013 if (first_clone)
3015 first_clone->clone_of = node;
3016 node->clones = first_clone;
3017 symtab_prevail_in_asm_name_hash ((symtab_node) node);
3018 symtab_insert_node_to_hashtable ((symtab_node) node);
3019 if (dump_file)
3020 fprintf (dump_file, "Introduced new external node "
3021 "(%s/%i) and turned into root of the clone tree.\n",
3022 xstrdup (cgraph_node_name (node)), node->symbol.order);
3024 else if (dump_file)
3025 fprintf (dump_file, "Introduced new external node "
3026 "(%s/%i).\n", xstrdup (cgraph_node_name (node)),
3027 node->symbol.order);
3028 return node;
3032 /* Given NODE, walk the alias chain to return the function NODE is alias of.
3033 Walk through thunk, too.
3034 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3036 struct cgraph_node *
3037 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
3041 node = cgraph_function_or_thunk_node (node, availability);
3042 if (node->thunk.thunk_p)
3044 node = node->callees->callee;
3045 if (availability)
3047 enum availability a;
3048 a = cgraph_function_body_availability (node);
3049 if (a < *availability)
3050 *availability = a;
3052 node = cgraph_function_or_thunk_node (node, availability);
3054 } while (node && node->thunk.thunk_p);
3055 return node;
3058 /* When doing LTO, read NODE's body from disk if it is not already present. */
3060 bool
3061 cgraph_get_body (struct cgraph_node *node)
3063 struct lto_file_decl_data *file_data;
3064 const char *data, *name;
3065 size_t len;
3066 tree decl = node->symbol.decl;
3068 if (DECL_RESULT (decl))
3069 return false;
3071 gcc_assert (in_lto_p);
3073 file_data = node->symbol.lto_file_data;
3074 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3076 /* We may have renamed the declaration, e.g., a static function. */
3077 name = lto_get_decl_name_mapping (file_data, name);
3079 data = lto_get_section_data (file_data, LTO_section_function_body,
3080 name, &len);
3081 if (!data)
3083 dump_cgraph_node (stderr, node);
3084 fatal_error ("%s: section %s is missing",
3085 file_data->file_name,
3086 name);
3089 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3091 lto_input_function_body (file_data, decl, data);
3092 lto_stats.num_function_bodies++;
3093 lto_free_section_data (file_data, LTO_section_function_body, name,
3094 data, len);
3095 return true;
3098 #include "gt-cgraph.h"