Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_9 / gcc / cgraph.c
blob532d09b2870af9ce70f71acb1e161c5af93a5b3f
1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 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 "varasm.h"
32 #include "calls.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "hashtab.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "debug.h"
40 #include "target.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "tree-eh.h"
46 #include "gimple-expr.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "timevar.h"
50 #include "dumpfile.h"
51 #include "gimple-ssa.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "tree-ssa.h"
55 #include "value-prof.h"
56 #include "except.h"
57 #include "diagnostic-core.h"
58 #include "rtl.h"
59 #include "ipa-utils.h"
60 #include "lto-streamer.h"
61 #include "l-ipo.h"
62 #include "ipa-inline.h"
63 #include "cfgloop.h"
64 #include "gimple-pretty-print.h"
65 #include "expr.h"
66 #include "tree-dfa.h"
67 #include "opts.h"
69 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
70 #include "tree-pass.h"
72 static void cgraph_node_remove_callers (struct cgraph_node *node);
73 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
74 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
76 /* Queue of cgraph nodes scheduled to be lowered. */
77 symtab_node *x_cgraph_nodes_queue;
78 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
80 /* Number of nodes in existence. */
81 int cgraph_n_nodes;
83 /* Maximal uid used in cgraph nodes. */
84 int cgraph_max_uid;
86 /* Maximal uid used in cgraph edges. */
87 int cgraph_edge_max_uid;
89 /* Set when whole unit has been analyzed so we can access global info. */
90 bool cgraph_global_info_ready = false;
92 /* What state callgraph is in right now. */
93 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
95 /* Set when the cgraph is fully build and the basic flags are computed. */
96 bool cgraph_function_flags_ready = false;
98 /* List of hooks triggered on cgraph_edge events. */
99 struct cgraph_edge_hook_list {
100 cgraph_edge_hook hook;
101 void *data;
102 struct cgraph_edge_hook_list *next;
105 /* List of hooks triggered on cgraph_node events. */
106 struct cgraph_node_hook_list {
107 cgraph_node_hook hook;
108 void *data;
109 struct cgraph_node_hook_list *next;
112 /* List of hooks triggered on events involving two cgraph_edges. */
113 struct cgraph_2edge_hook_list {
114 cgraph_2edge_hook hook;
115 void *data;
116 struct cgraph_2edge_hook_list *next;
119 /* List of hooks triggered on events involving two cgraph_nodes. */
120 struct cgraph_2node_hook_list {
121 cgraph_2node_hook hook;
122 void *data;
123 struct cgraph_2node_hook_list *next;
126 /* List of hooks triggered when an edge is removed. */
127 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
128 /* List of hooks triggered when a node is removed. */
129 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
130 /* List of hooks triggered when an edge is duplicated. */
131 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
132 /* List of hooks triggered when a node is duplicated. */
133 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
134 /* List of hooks triggered when an function is inserted. */
135 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
137 /* Head of a linked list of unused (freed) call graph nodes.
138 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
139 static GTY(()) struct cgraph_node *free_nodes;
140 /* Head of a linked list of unused (freed) call graph edges.
141 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
142 static GTY(()) struct cgraph_edge *free_edges;
144 /* Did procss_same_body_aliases run? */
145 bool cpp_implicit_aliases_done;
147 /* Map a cgraph_node to cgraph_function_version_info using this htab.
148 The cgraph_function_version_info has a THIS_NODE field that is the
149 corresponding cgraph_node.. */
151 static GTY((param_is (struct cgraph_function_version_info))) htab_t
152 cgraph_fnver_htab = NULL;
154 /* Hash function for cgraph_fnver_htab. */
155 static hashval_t
156 cgraph_fnver_htab_hash (const void *ptr)
158 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
159 return (hashval_t)(uid);
162 /* eq function for cgraph_fnver_htab. */
163 static int
164 cgraph_fnver_htab_eq (const void *p1, const void *p2)
166 const struct cgraph_function_version_info *n1
167 = (const struct cgraph_function_version_info *)p1;
168 const struct cgraph_function_version_info *n2
169 = (const struct cgraph_function_version_info *)p2;
171 return n1->this_node->uid == n2->this_node->uid;
174 /* Mark as GC root all allocated nodes. */
175 static GTY(()) struct cgraph_function_version_info *
176 version_info_node = NULL;
178 /* Get the cgraph_function_version_info node corresponding to node. */
179 struct cgraph_function_version_info *
180 get_cgraph_node_version (struct cgraph_node *node)
182 struct cgraph_function_version_info *ret;
183 struct cgraph_function_version_info key;
184 key.this_node = node;
186 if (cgraph_fnver_htab == NULL)
187 return NULL;
189 ret = (struct cgraph_function_version_info *)
190 htab_find (cgraph_fnver_htab, &key);
192 return ret;
195 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
196 corresponding to cgraph_node NODE. */
197 struct cgraph_function_version_info *
198 insert_new_cgraph_node_version (struct cgraph_node *node)
200 void **slot;
202 version_info_node = NULL;
203 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
204 version_info_node->this_node = node;
206 if (cgraph_fnver_htab == NULL)
207 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
208 cgraph_fnver_htab_eq, NULL);
210 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
211 gcc_assert (slot != NULL);
212 *slot = version_info_node;
213 return version_info_node;
216 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
217 DECL is a duplicate declaration. */
218 void
219 delete_function_version (tree decl)
221 struct cgraph_node *decl_node = cgraph_get_node (decl);
222 struct cgraph_function_version_info *decl_v = NULL;
224 if (decl_node == NULL)
225 return;
227 decl_v = get_cgraph_node_version (decl_node);
229 if (decl_v == NULL)
230 return;
232 if (decl_v->prev != NULL)
233 decl_v->prev->next = decl_v->next;
235 if (decl_v->next != NULL)
236 decl_v->next->prev = decl_v->prev;
238 if (cgraph_fnver_htab != NULL)
239 htab_remove_elt (cgraph_fnver_htab, decl_v);
241 cgraph_remove_node (decl_node);
244 /* Record that DECL1 and DECL2 are semantically identical function
245 versions. */
246 void
247 record_function_versions (tree decl1, tree decl2)
249 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
250 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
251 struct cgraph_function_version_info *decl1_v = NULL;
252 struct cgraph_function_version_info *decl2_v = NULL;
253 struct cgraph_function_version_info *before;
254 struct cgraph_function_version_info *after;
256 gcc_assert (decl1_node != NULL && decl2_node != NULL);
257 decl1_v = get_cgraph_node_version (decl1_node);
258 decl2_v = get_cgraph_node_version (decl2_node);
260 if (decl1_v != NULL && decl2_v != NULL)
261 return;
263 if (decl1_v == NULL)
264 decl1_v = insert_new_cgraph_node_version (decl1_node);
266 if (decl2_v == NULL)
267 decl2_v = insert_new_cgraph_node_version (decl2_node);
269 /* Chain decl2_v and decl1_v. All semantically identical versions
270 will be chained together. */
272 before = decl1_v;
273 after = decl2_v;
275 while (before->next != NULL)
276 before = before->next;
278 while (after->prev != NULL)
279 after= after->prev;
281 before->next = after;
282 after->prev = before;
285 /* Macros to access the next item in the list of free cgraph nodes and
286 edges. */
287 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->next)
288 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
289 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
291 /* Register HOOK to be called with DATA on each removed edge. */
292 struct cgraph_edge_hook_list *
293 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
295 struct cgraph_edge_hook_list *entry;
296 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
298 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
299 entry->hook = hook;
300 entry->data = data;
301 entry->next = NULL;
302 while (*ptr)
303 ptr = &(*ptr)->next;
304 *ptr = entry;
305 return entry;
308 /* Remove ENTRY from the list of hooks called on removing edges. */
309 void
310 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
312 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
314 while (*ptr != entry)
315 ptr = &(*ptr)->next;
316 *ptr = entry->next;
317 free (entry);
320 /* Call all edge removal hooks. */
321 static void
322 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
324 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
325 while (entry)
327 entry->hook (e, entry->data);
328 entry = entry->next;
332 /* Register HOOK to be called with DATA on each removed node. */
333 struct cgraph_node_hook_list *
334 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
336 struct cgraph_node_hook_list *entry;
337 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
339 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
340 entry->hook = hook;
341 entry->data = data;
342 entry->next = NULL;
343 while (*ptr)
344 ptr = &(*ptr)->next;
345 *ptr = entry;
346 return entry;
349 /* Remove ENTRY from the list of hooks called on removing nodes. */
350 void
351 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
353 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
355 while (*ptr != entry)
356 ptr = &(*ptr)->next;
357 *ptr = entry->next;
358 free (entry);
361 /* Call all node removal hooks. */
362 static void
363 cgraph_call_node_removal_hooks (struct cgraph_node *node)
365 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
366 while (entry)
368 entry->hook (node, entry->data);
369 entry = entry->next;
373 /* Register HOOK to be called with DATA on each inserted node. */
374 struct cgraph_node_hook_list *
375 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
377 struct cgraph_node_hook_list *entry;
378 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
380 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
381 entry->hook = hook;
382 entry->data = data;
383 entry->next = NULL;
384 while (*ptr)
385 ptr = &(*ptr)->next;
386 *ptr = entry;
387 return entry;
390 /* Remove ENTRY from the list of hooks called on inserted nodes. */
391 void
392 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
394 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
396 while (*ptr != entry)
397 ptr = &(*ptr)->next;
398 *ptr = entry->next;
399 free (entry);
402 /* Call all node insertion hooks. */
403 void
404 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
406 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
407 while (entry)
409 entry->hook (node, entry->data);
410 entry = entry->next;
414 /* Register HOOK to be called with DATA on each duplicated edge. */
415 struct cgraph_2edge_hook_list *
416 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
418 struct cgraph_2edge_hook_list *entry;
419 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
421 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
422 entry->hook = hook;
423 entry->data = data;
424 entry->next = NULL;
425 while (*ptr)
426 ptr = &(*ptr)->next;
427 *ptr = entry;
428 return entry;
431 /* Remove ENTRY from the list of hooks called on duplicating edges. */
432 void
433 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
435 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
437 while (*ptr != entry)
438 ptr = &(*ptr)->next;
439 *ptr = entry->next;
440 free (entry);
443 /* Call all edge duplication hooks. */
444 void
445 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
446 struct cgraph_edge *cs2)
448 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
449 while (entry)
451 entry->hook (cs1, cs2, entry->data);
452 entry = entry->next;
456 /* Register HOOK to be called with DATA on each duplicated node. */
457 struct cgraph_2node_hook_list *
458 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
460 struct cgraph_2node_hook_list *entry;
461 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
463 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
464 entry->hook = hook;
465 entry->data = data;
466 entry->next = NULL;
467 while (*ptr)
468 ptr = &(*ptr)->next;
469 *ptr = entry;
470 return entry;
473 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
474 void
475 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
477 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
479 while (*ptr != entry)
480 ptr = &(*ptr)->next;
481 *ptr = entry->next;
482 free (entry);
485 /* Call all node duplication hooks. */
486 void
487 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
488 struct cgraph_node *node2)
490 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
491 while (entry)
493 entry->hook (node1, node2, entry->data);
494 entry = entry->next;
498 /* Allocate new callgraph node. */
500 static inline struct cgraph_node *
501 cgraph_allocate_node (void)
503 struct cgraph_node *node;
505 if (free_nodes)
507 node = free_nodes;
508 free_nodes = NEXT_FREE_NODE (node);
510 else
512 node = ggc_alloc_cleared_cgraph_node ();
513 node->uid = cgraph_max_uid++;
516 return node;
519 /* Allocate new callgraph node and insert it into basic data structures. */
521 struct cgraph_node *
522 cgraph_create_empty_node (void)
524 struct cgraph_node *node = cgraph_allocate_node ();
526 node->type = SYMTAB_FUNCTION;
527 node->frequency = NODE_FREQUENCY_NORMAL;
528 node->count_materialization_scale = REG_BR_PROB_BASE;
529 cgraph_n_nodes++;
530 return node;
533 /* Return cgraph node assigned to DECL. Create new one when needed. */
535 struct cgraph_node *
536 cgraph_create_node (tree decl)
538 struct cgraph_node *node = cgraph_create_empty_node ();
539 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
541 node->decl = decl;
542 symtab_register_node (node);
544 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
546 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
547 node->next_nested = node->origin->nested;
548 node->origin->nested = node;
550 pattern_match_function_attributes (decl);
551 return node;
554 /* Try to find a call graph node for declaration DECL and if it does not exist
555 or if it corresponds to an inline clone, create a new one. */
557 struct cgraph_node *
558 cgraph_get_create_node (tree decl)
560 struct cgraph_node *first_clone = cgraph_get_node (decl);
562 if (first_clone && !first_clone->global.inlined_to)
563 return first_clone;
565 struct cgraph_node *node = cgraph_create_node (decl);
566 if (first_clone)
568 first_clone->clone_of = node;
569 node->clones = first_clone;
570 symtab_prevail_in_asm_name_hash (node);
571 symtab_insert_node_to_hashtable (node);
572 if (dump_file)
573 fprintf (dump_file, "Introduced new external node "
574 "(%s/%i) and turned into root of the clone tree.\n",
575 xstrdup (node->name ()), node->order);
577 else if (dump_file)
578 fprintf (dump_file, "Introduced new external node "
579 "(%s/%i).\n", xstrdup (node->name ()),
580 node->order);
581 return node;
584 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
585 the function body is associated with (not necessarily cgraph_node (DECL). */
587 struct cgraph_node *
588 cgraph_create_function_alias (tree alias, tree target)
590 struct cgraph_node *alias_node;
592 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
593 || TREE_CODE (target) == IDENTIFIER_NODE);
594 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
595 alias_node = cgraph_get_create_node (alias);
596 gcc_assert (!alias_node->definition);
597 alias_node->alias_target = target;
598 alias_node->definition = true;
599 alias_node->alias = true;
600 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
601 alias_node->weakref = true;
602 return alias_node;
605 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
606 and NULL otherwise.
607 Same body aliases are output whenever the body of DECL is output,
608 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
610 struct cgraph_node *
611 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
613 struct cgraph_node *n;
614 #ifndef ASM_OUTPUT_DEF
615 /* If aliases aren't supported by the assembler, fail. */
616 return NULL;
617 #endif
618 /* Langhooks can create same body aliases of symbols not defined.
619 Those are useless. Drop them on the floor. */
620 if (cgraph_global_info_ready)
621 return NULL;
623 n = cgraph_create_function_alias (alias, decl);
624 n->cpp_implicit_alias = true;
625 if (cpp_implicit_aliases_done)
626 symtab_resolve_alias (n,
627 cgraph_get_node (decl));
628 return n;
631 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
632 aliases DECL with an adjustments made into the first parameter.
633 See comments in thunk_adjust for detail on the parameters. */
635 struct cgraph_node *
636 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
637 tree alias, tree decl ATTRIBUTE_UNUSED,
638 bool this_adjusting,
639 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
640 tree virtual_offset,
641 tree real_alias)
643 struct cgraph_node *node;
645 node = cgraph_get_node (alias);
646 if (node)
648 gcc_assert (node->definition);
649 gcc_assert (!node->alias);
650 gcc_assert (!node->thunk.thunk_p);
651 cgraph_remove_node (node);
654 node = cgraph_create_node (alias);
655 gcc_checking_assert (!virtual_offset
656 || tree_to_double_int (virtual_offset) ==
657 double_int::from_shwi (virtual_value));
658 node->thunk.fixed_offset = fixed_offset;
659 node->thunk.this_adjusting = this_adjusting;
660 node->thunk.virtual_value = virtual_value;
661 node->thunk.virtual_offset_p = virtual_offset != NULL;
662 node->thunk.alias = real_alias;
663 node->thunk.thunk_p = true;
664 node->definition = true;
666 return node;
669 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
670 Return NULL if there's no such node. */
672 struct cgraph_node *
673 cgraph_node_for_asm (tree asmname)
675 /* We do not want to look at inline clones. */
676 for (symtab_node *node = symtab_node_for_asm (asmname);
677 node;
678 node = node->next_sharing_asm_name)
680 cgraph_node *cn = dyn_cast <cgraph_node> (node);
681 if (cn && !cn->global.inlined_to)
683 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
684 return cgraph_lipo_get_resolved_node (cn->decl);
685 return cn;
688 return NULL;
691 /* Returns a hash value for X (which really is a cgraph_edge). */
693 static hashval_t
694 edge_hash (const void *x)
696 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
699 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
701 static int
702 edge_eq (const void *x, const void *y)
704 return ((const struct cgraph_edge *) x)->call_stmt == y;
707 /* Add call graph edge E to call site hash of its caller. */
709 static inline void
710 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
712 void **slot;
713 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
714 e->call_stmt,
715 htab_hash_pointer (e->call_stmt),
716 INSERT);
717 *slot = e;
720 /* Add call graph edge E to call site hash of its caller. */
722 static inline void
723 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
725 void **slot;
726 /* There are two speculative edges for every statement (one direct,
727 one indirect); always hash the direct one. */
728 if (e->speculative && e->indirect_unknown_callee)
729 return;
730 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
731 e->call_stmt,
732 htab_hash_pointer (e->call_stmt),
733 INSERT);
734 if (*slot)
736 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
737 if (e->callee)
738 *slot = e;
739 return;
741 gcc_assert (!*slot || e->speculative);
742 *slot = e;
745 /* Return the callgraph edge representing the GIMPLE_CALL statement
746 CALL_STMT. */
748 struct cgraph_edge *
749 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
751 struct cgraph_edge *e, *e2;
752 int n = 0;
754 if (node->call_site_hash)
755 return (struct cgraph_edge *)
756 htab_find_with_hash (node->call_site_hash, call_stmt,
757 htab_hash_pointer (call_stmt));
759 /* This loop may turn out to be performance problem. In such case adding
760 hashtables into call nodes with very many edges is probably best
761 solution. It is not good idea to add pointer into CALL_EXPR itself
762 because we want to make possible having multiple cgraph nodes representing
763 different clones of the same body before the body is actually cloned. */
764 for (e = node->callees; e; e = e->next_callee)
766 if (e->call_stmt == call_stmt)
767 break;
768 n++;
771 if (!e)
772 for (e = node->indirect_calls; e; e = e->next_callee)
774 if (e->call_stmt == call_stmt)
775 break;
776 n++;
779 if (n > 100)
781 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
782 for (e2 = node->callees; e2; e2 = e2->next_callee)
783 /* Skip fake edges. */
784 if (e2->call_stmt)
785 cgraph_add_edge_to_call_site_hash (e2);
786 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
787 cgraph_add_edge_to_call_site_hash (e2);
790 return e;
794 /* Change field call_stmt of edge E to NEW_STMT.
795 If UPDATE_SPECULATIVE and E is any component of speculative
796 edge, then update all components. */
798 void
799 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
800 bool update_speculative)
802 tree decl;
804 /* Speculative edges has three component, update all of them
805 when asked to. */
806 if (update_speculative && e->speculative)
808 struct cgraph_edge *direct, *indirect;
809 struct ipa_ref *ref;
811 cgraph_speculative_call_info (e, direct, indirect, ref);
812 cgraph_set_call_stmt (direct, new_stmt, false);
813 cgraph_set_call_stmt (indirect, new_stmt, false);
814 ref->stmt = new_stmt;
815 return;
818 /* Only direct speculative edges go to call_site_hash. */
819 if (e->caller->call_site_hash
820 && (!e->speculative || !e->indirect_unknown_callee))
822 htab_remove_elt_with_hash (e->caller->call_site_hash,
823 e->call_stmt,
824 htab_hash_pointer (e->call_stmt));
827 e->call_stmt = new_stmt;
828 if (e->indirect_unknown_callee
829 && (decl = gimple_call_fndecl (new_stmt)))
831 /* Constant propagation (and possibly also inlining?) can turn an
832 indirect call into a direct one. */
833 struct cgraph_node *new_callee = cgraph_get_node (decl);
834 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
835 new_callee = cgraph_lipo_get_resolved_node (decl);
837 gcc_checking_assert (new_callee);
838 e = cgraph_make_edge_direct (e, new_callee);
841 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
842 e->can_throw_external = stmt_can_throw_external (new_stmt);
843 pop_cfun ();
844 if (e->caller->call_site_hash)
845 cgraph_add_edge_to_call_site_hash (e);
848 /* Allocate a cgraph_edge structure and fill it with data according to the
849 parameters of which only CALLEE can be NULL (when creating an indirect call
850 edge). */
852 static struct cgraph_edge *
853 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
854 gimple call_stmt, gcov_type count, int freq,
855 bool indir_unknown_callee)
857 struct cgraph_edge *edge;
859 /* LTO does not actually have access to the call_stmt since these
860 have not been loaded yet. */
861 if (call_stmt)
863 /* This is a rather expensive check possibly triggering
864 construction of call stmt hashtable. */
865 #ifdef ENABLE_CHECKING
866 struct cgraph_edge *e;
867 gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
868 #endif
870 gcc_assert (is_gimple_call (call_stmt));
873 if (free_edges)
875 edge = free_edges;
876 free_edges = NEXT_FREE_EDGE (edge);
878 else
880 edge = ggc_alloc_cgraph_edge ();
881 edge->uid = cgraph_edge_max_uid++;
884 edge->aux = NULL;
885 edge->caller = caller;
886 edge->callee = callee;
887 edge->prev_caller = NULL;
888 edge->next_caller = NULL;
889 edge->prev_callee = NULL;
890 edge->next_callee = NULL;
891 edge->lto_stmt_uid = 0;
893 edge->count = count;
894 gcc_assert (count >= 0);
895 edge->frequency = freq;
896 gcc_assert (freq >= 0);
897 gcc_assert (freq <= CGRAPH_FREQ_MAX);
899 edge->call_stmt = call_stmt;
900 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
901 edge->can_throw_external
902 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
903 pop_cfun ();
904 if (call_stmt
905 && callee && callee->decl
906 && !gimple_check_call_matching_types (call_stmt, callee->decl,
907 false))
908 edge->call_stmt_cannot_inline_p = true;
909 else
910 edge->call_stmt_cannot_inline_p = false;
912 edge->indirect_info = NULL;
913 edge->indirect_inlining_edge = 0;
914 edge->speculative = false;
915 edge->indirect_unknown_callee = indir_unknown_callee;
916 if (call_stmt && caller->call_site_hash)
917 cgraph_add_edge_to_call_site_hash (edge);
919 return edge;
922 /* Create edge from CALLER to CALLEE in the cgraph. */
924 struct cgraph_edge *
925 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
926 gimple call_stmt, gcov_type count, int freq)
928 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
929 count, freq, false);
931 initialize_inline_failed (edge);
933 edge->next_caller = callee->callers;
934 if (callee->callers)
935 callee->callers->prev_caller = edge;
936 edge->next_callee = caller->callees;
937 if (caller->callees)
938 caller->callees->prev_callee = edge;
939 caller->callees = edge;
940 callee->callers = edge;
942 return edge;
945 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
947 struct cgraph_indirect_call_info *
948 cgraph_allocate_init_indirect_info (void)
950 struct cgraph_indirect_call_info *ii;
952 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
953 ii->param_index = -1;
954 return ii;
957 /* Create an indirect edge with a yet-undetermined callee where the call
958 statement destination is a formal parameter of the caller with index
959 PARAM_INDEX. */
961 struct cgraph_edge *
962 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
963 int ecf_flags,
964 gcov_type count, int freq)
966 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
967 count, freq, true);
968 tree target;
970 initialize_inline_failed (edge);
972 edge->indirect_info = cgraph_allocate_init_indirect_info ();
973 edge->indirect_info->ecf_flags = ecf_flags;
975 /* Record polymorphic call info. */
976 if (call_stmt
977 && (target = gimple_call_fn (call_stmt))
978 && virtual_method_call_p (target))
980 tree otr_type;
981 HOST_WIDE_INT otr_token;
982 ipa_polymorphic_call_context context;
984 get_polymorphic_call_info (caller->decl,
985 target,
986 &otr_type, &otr_token,
987 &context);
989 /* Only record types can have virtual calls. */
990 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
991 edge->indirect_info->polymorphic = true;
992 edge->indirect_info->param_index = -1;
993 edge->indirect_info->otr_token = otr_token;
994 edge->indirect_info->otr_type = otr_type;
995 edge->indirect_info->outer_type = context.outer_type;
996 edge->indirect_info->offset = context.offset;
997 edge->indirect_info->maybe_in_construction
998 = context.maybe_in_construction;
999 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
1002 edge->next_callee = caller->indirect_calls;
1003 if (caller->indirect_calls)
1004 caller->indirect_calls->prev_callee = edge;
1005 caller->indirect_calls = edge;
1007 return edge;
1010 /* Remove the edge E from the list of the callers of the callee. */
1012 static inline void
1013 cgraph_edge_remove_callee (struct cgraph_edge *e)
1015 gcc_assert (!e->indirect_unknown_callee);
1016 if (e->prev_caller)
1017 e->prev_caller->next_caller = e->next_caller;
1018 if (e->next_caller)
1019 e->next_caller->prev_caller = e->prev_caller;
1020 if (!e->prev_caller)
1021 e->callee->callers = e->next_caller;
1024 /* Remove the edge E from the list of the callees of the caller. */
1026 static inline void
1027 cgraph_edge_remove_caller (struct cgraph_edge *e)
1029 if (e->prev_callee)
1030 e->prev_callee->next_callee = e->next_callee;
1031 if (e->next_callee)
1032 e->next_callee->prev_callee = e->prev_callee;
1033 if (!e->prev_callee)
1035 if (e->indirect_unknown_callee)
1036 e->caller->indirect_calls = e->next_callee;
1037 else
1038 e->caller->callees = e->next_callee;
1040 if (e->caller->call_site_hash && e->call_stmt)
1041 htab_remove_elt_with_hash (e->caller->call_site_hash,
1042 e->call_stmt,
1043 htab_hash_pointer (e->call_stmt));
1046 /* Put the edge onto the free list. */
1048 static void
1049 cgraph_free_edge (struct cgraph_edge *e)
1051 int uid = e->uid;
1053 if (e->indirect_info)
1054 ggc_free (e->indirect_info);
1056 /* Clear out the edge so we do not dangle pointers. */
1057 memset (e, 0, sizeof (*e));
1058 e->uid = uid;
1059 NEXT_FREE_EDGE (e) = free_edges;
1060 free_edges = e;
1063 /* Remove the edge E in the cgraph. */
1065 void
1066 cgraph_remove_edge (struct cgraph_edge *e)
1068 /* Call all edge removal hooks. */
1069 cgraph_call_edge_removal_hooks (e);
1071 if (!e->indirect_unknown_callee)
1072 /* Remove from callers list of the callee. */
1073 cgraph_edge_remove_callee (e);
1075 /* Remove from callees list of the callers. */
1076 cgraph_edge_remove_caller (e);
1078 /* Put the edge onto the free list. */
1079 cgraph_free_edge (e);
1082 /* Remove fake cgraph edges for indirect calls. NODE is the callee
1083 of the edges. */
1085 void
1086 cgraph_remove_fake_indirect_call_in_edges (struct cgraph_node *node)
1088 struct cgraph_edge *f, *e;
1090 if (!L_IPO_COMP_MODE)
1091 return;
1093 for (e = node->callers; e; e = f)
1095 f = e->next_caller;
1096 if (!e->call_stmt)
1097 cgraph_remove_edge (e);
1102 /* Set callee of call graph edge E and add it to the corresponding set of
1103 callers. */
1105 static void
1106 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1108 e->prev_caller = NULL;
1109 if (n->callers)
1110 n->callers->prev_caller = e;
1111 e->next_caller = n->callers;
1112 n->callers = e;
1113 e->callee = n;
1116 /* Turn edge E into speculative call calling N2. Update
1117 the profile so the direct call is taken COUNT times
1118 with FREQUENCY.
1120 At clone materialization time, the indirect call E will
1121 be expanded as:
1123 if (call_dest == N2)
1124 n2 ();
1125 else
1126 call call_dest
1128 At this time the function just creates the direct call,
1129 the referencd representing the if conditional and attaches
1130 them all to the orginal indirect call statement.
1132 Return direct edge created. */
1134 struct cgraph_edge *
1135 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1136 struct cgraph_node *n2,
1137 gcov_type direct_count,
1138 int direct_frequency)
1140 struct cgraph_node *n = e->caller;
1141 struct ipa_ref *ref;
1142 struct cgraph_edge *e2;
1144 if (dump_file)
1146 fprintf (dump_file, "Indirect call -> speculative call"
1147 " %s/%i => %s/%i\n",
1148 xstrdup (n->name ()), n->order,
1149 xstrdup (n2->name ()), n2->order);
1151 e->speculative = true;
1152 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1153 initialize_inline_failed (e2);
1154 e2->speculative = true;
1155 if (TREE_NOTHROW (n2->decl))
1156 e2->can_throw_external = false;
1157 else
1158 e2->can_throw_external = e->can_throw_external;
1159 e2->lto_stmt_uid = e->lto_stmt_uid;
1160 e->count -= e2->count;
1161 e->frequency -= e2->frequency;
1162 cgraph_call_edge_duplication_hooks (e, e2);
1163 ref = ipa_record_reference (n, n2,
1164 IPA_REF_ADDR, e->call_stmt);
1165 ref->lto_stmt_uid = e->lto_stmt_uid;
1166 ref->speculative = e->speculative;
1167 cgraph_mark_address_taken_node (n2);
1168 return e2;
1171 /* Speculative call consist of three components:
1172 1) an indirect edge representing the original call
1173 2) an direct edge representing the new call
1174 3) ADDR_EXPR reference representing the speculative check.
1175 All three components are attached to single statement (the indirect
1176 call) and if one of them exists, all of them must exist.
1178 Given speculative call edge E, return all three components.
1181 void
1182 cgraph_speculative_call_info (struct cgraph_edge *e,
1183 struct cgraph_edge *&direct,
1184 struct cgraph_edge *&indirect,
1185 struct ipa_ref *&reference)
1187 struct ipa_ref *ref;
1188 int i;
1189 struct cgraph_edge *e2;
1191 if (!e->indirect_unknown_callee)
1192 for (e2 = e->caller->indirect_calls;
1193 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1194 e2 = e2->next_callee)
1196 else
1198 e2 = e;
1199 /* We can take advantage of the call stmt hash. */
1200 if (e2->call_stmt)
1202 e = cgraph_edge (e->caller, e2->call_stmt);
1203 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1205 else
1206 for (e = e->caller->callees;
1207 e2->call_stmt != e->call_stmt
1208 || e2->lto_stmt_uid != e->lto_stmt_uid;
1209 e = e->next_callee)
1212 gcc_assert (e->speculative && e2->speculative);
1213 direct = e;
1214 indirect = e2;
1216 reference = NULL;
1217 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->ref_list,
1218 i, ref); i++)
1219 if (ref->speculative
1220 && ((ref->stmt && ref->stmt == e->call_stmt)
1221 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1223 reference = ref;
1224 break;
1227 /* Speculative edge always consist of all three components - direct edge,
1228 indirect and reference. */
1230 gcc_assert (e && e2 && ref);
1233 /* Redirect callee of E to N. The function does not update underlying
1234 call expression. */
1236 void
1237 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1239 /* Remove from callers list of the current callee. */
1240 cgraph_edge_remove_callee (e);
1242 /* Insert to callers list of the new callee. */
1243 cgraph_set_edge_callee (e, n);
1246 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1247 Remove the speculative call sequence and return edge representing the call.
1248 It is up to caller to redirect the call as appropriate. */
1250 struct cgraph_edge *
1251 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1253 struct cgraph_edge *e2;
1254 struct ipa_ref *ref;
1256 gcc_assert (edge->speculative);
1257 cgraph_speculative_call_info (edge, e2, edge, ref);
1258 if (!callee_decl
1259 || !symtab_semantically_equivalent_p (ref->referred,
1260 symtab_get_node (callee_decl)))
1262 if (dump_file)
1264 if (callee_decl)
1266 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1267 "turned out to have contradicting known target ",
1268 xstrdup (edge->caller->name ()), edge->caller->order,
1269 xstrdup (e2->callee->name ()), e2->callee->order);
1270 print_generic_expr (dump_file, callee_decl, 0);
1271 fprintf (dump_file, "\n");
1273 else
1275 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1276 xstrdup (edge->caller->name ()), edge->caller->order,
1277 xstrdup (e2->callee->name ()), e2->callee->order);
1281 else
1283 struct cgraph_edge *tmp = edge;
1284 if (dump_file)
1285 fprintf (dump_file, "Speculative call turned into direct call.\n");
1286 edge = e2;
1287 e2 = tmp;
1288 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1289 in the functions inlined through it. */
1291 edge->count += e2->count;
1292 edge->frequency += e2->frequency;
1293 if (edge->frequency > CGRAPH_FREQ_MAX)
1294 edge->frequency = CGRAPH_FREQ_MAX;
1295 edge->speculative = false;
1296 e2->speculative = false;
1297 ipa_remove_reference (ref);
1298 if (e2->indirect_unknown_callee || e2->inline_failed)
1299 cgraph_remove_edge (e2);
1300 else
1301 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1302 if (edge->caller->call_site_hash)
1303 cgraph_update_edge_in_call_site_hash (edge);
1304 return edge;
1307 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1308 CALLEE. DELTA is an integer constant that is to be added to the this
1309 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1311 struct cgraph_edge *
1312 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1314 gcc_assert (edge->indirect_unknown_callee);
1316 /* If we are redirecting speculative call, make it non-speculative. */
1317 if (edge->indirect_unknown_callee && edge->speculative)
1319 edge = cgraph_resolve_speculation (edge, callee->decl);
1321 /* On successful speculation just return the pre existing direct edge. */
1322 if (!edge->indirect_unknown_callee)
1323 return edge;
1326 edge->indirect_unknown_callee = 0;
1327 ggc_free (edge->indirect_info);
1328 edge->indirect_info = NULL;
1330 /* Get the edge out of the indirect edge list. */
1331 if (edge->prev_callee)
1332 edge->prev_callee->next_callee = edge->next_callee;
1333 if (edge->next_callee)
1334 edge->next_callee->prev_callee = edge->prev_callee;
1335 if (!edge->prev_callee)
1336 edge->caller->indirect_calls = edge->next_callee;
1338 /* Put it into the normal callee list */
1339 edge->prev_callee = NULL;
1340 edge->next_callee = edge->caller->callees;
1341 if (edge->caller->callees)
1342 edge->caller->callees->prev_callee = edge;
1343 edge->caller->callees = edge;
1345 /* Insert to callers list of the new callee. */
1346 cgraph_set_edge_callee (edge, callee);
1348 if (edge->call_stmt)
1349 edge->call_stmt_cannot_inline_p
1350 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1351 false);
1353 /* We need to re-determine the inlining status of the edge. */
1354 initialize_inline_failed (edge);
1355 return edge;
1358 /* If necessary, change the function declaration in the call statement
1359 associated with E so that it corresponds to the edge callee. */
1361 gimple
1362 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1364 tree decl = gimple_call_fndecl (e->call_stmt);
1365 tree lhs = gimple_call_lhs (e->call_stmt);
1366 gimple new_stmt;
1367 gimple_stmt_iterator gsi;
1368 #ifdef ENABLE_CHECKING
1369 struct cgraph_node *node;
1370 #endif
1372 if (e->speculative)
1374 struct cgraph_edge *e2;
1375 gimple new_stmt;
1376 struct ipa_ref *ref;
1378 cgraph_speculative_call_info (e, e, e2, ref);
1379 /* If there already is an direct call (i.e. as a result of inliner's
1380 substitution), forget about speculating. */
1381 if (decl)
1382 e = cgraph_resolve_speculation (e, decl);
1383 /* If types do not match, speculation was likely wrong.
1384 The direct edge was posisbly redirected to the clone with a different
1385 signature. We did not update the call statement yet, so compare it
1386 with the reference that still points to the proper type. */
1387 else if (!gimple_check_call_matching_types (e->call_stmt,
1388 ref->referred->decl,
1389 true))
1391 if (dump_file)
1392 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1393 "Type mismatch.\n",
1394 xstrdup (e->caller->name ()),
1395 e->caller->order,
1396 xstrdup (e->callee->name ()),
1397 e->callee->order);
1398 e = cgraph_resolve_speculation (e, NULL);
1399 /* We are producing the final function body and will throw away the
1400 callgraph edges really soon. Reset the counts/frequencies to
1401 keep verifier happy in the case of roundoff errors. */
1402 e->count = gimple_bb (e->call_stmt)->count;
1403 e->frequency = compute_call_stmt_bb_frequency
1404 (e->caller->decl, gimple_bb (e->call_stmt));
1406 /* Expand speculation into GIMPLE code. */
1407 else
1409 if (dump_file)
1410 fprintf (dump_file,
1411 "Expanding speculative call of %s/%i -> %s/%i count:"
1412 HOST_WIDEST_INT_PRINT_DEC"\n",
1413 xstrdup (e->caller->name ()),
1414 e->caller->order,
1415 xstrdup (e->callee->name ()),
1416 e->callee->order,
1417 (HOST_WIDEST_INT)e->count);
1418 gcc_assert (e2->speculative);
1419 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1420 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1421 e->count || e2->count
1422 ? RDIV (e->count * REG_BR_PROB_BASE,
1423 e->count + e2->count)
1424 : e->frequency || e2->frequency
1425 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1426 e->frequency + e2->frequency)
1427 : REG_BR_PROB_BASE / 2,
1428 e->count, e->count + e2->count);
1429 e->speculative = false;
1430 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1431 new_stmt, false);
1432 e->frequency = compute_call_stmt_bb_frequency
1433 (e->caller->decl, gimple_bb (e->call_stmt));
1434 e2->frequency = compute_call_stmt_bb_frequency
1435 (e2->caller->decl, gimple_bb (e2->call_stmt));
1436 e2->speculative = false;
1437 ref->speculative = false;
1438 ref->stmt = NULL;
1439 /* Indirect edges are not both in the call site hash.
1440 get it updated. */
1441 if (e->caller->call_site_hash)
1442 cgraph_update_edge_in_call_site_hash (e2);
1443 pop_cfun ();
1444 /* Continue redirecting E to proper target. */
1448 if (e->indirect_unknown_callee
1449 || decl == e->callee->decl)
1450 return e->call_stmt;
1452 #ifdef ENABLE_CHECKING
1453 if (decl)
1455 node = cgraph_get_node (decl);
1456 gcc_assert (!node || !node->clone.combined_args_to_skip);
1458 #endif
1460 if (cgraph_dump_file)
1462 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1463 xstrdup (e->caller->name ()), e->caller->order,
1464 xstrdup (e->callee->name ()), e->callee->order);
1465 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1466 if (e->callee->clone.combined_args_to_skip)
1468 fprintf (cgraph_dump_file, " combined args to skip: ");
1469 dump_bitmap (cgraph_dump_file,
1470 e->callee->clone.combined_args_to_skip);
1474 if (e->callee->clone.combined_args_to_skip)
1476 int lp_nr;
1478 new_stmt
1479 = gimple_call_copy_skip_args (e->call_stmt,
1480 e->callee->clone.combined_args_to_skip);
1481 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1482 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1484 if (gimple_vdef (new_stmt)
1485 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1486 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1488 gsi = gsi_for_stmt (e->call_stmt);
1489 gsi_replace (&gsi, new_stmt, false);
1490 /* We need to defer cleaning EH info on the new statement to
1491 fixup-cfg. We may not have dominator information at this point
1492 and thus would end up with unreachable blocks and have no way
1493 to communicate that we need to run CFG cleanup then. */
1494 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1495 if (lp_nr != 0)
1497 remove_stmt_from_eh_lp (e->call_stmt);
1498 add_stmt_to_eh_lp (new_stmt, lp_nr);
1501 else
1503 new_stmt = e->call_stmt;
1504 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1505 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1506 if (L_IPO_COMP_MODE)
1508 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1509 if (lp_nr != 0 && !stmt_could_throw_p (e->call_stmt))
1510 remove_stmt_from_eh_lp (e->call_stmt);
1514 /* If the call becomes noreturn, remove the lhs. */
1515 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1517 if (TREE_CODE (lhs) == SSA_NAME)
1519 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1520 TREE_TYPE (lhs), NULL);
1521 var = get_or_create_ssa_default_def
1522 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1523 gimple set_stmt = gimple_build_assign (lhs, var);
1524 gsi = gsi_for_stmt (new_stmt);
1525 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1526 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1528 gimple_call_set_lhs (new_stmt, NULL_TREE);
1529 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1532 /* If new callee has no static chain, remove it. */
1533 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1535 gimple_call_set_chain (new_stmt, NULL);
1536 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1539 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1541 if (cgraph_dump_file)
1543 fprintf (cgraph_dump_file, " updated to:");
1544 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1546 return new_stmt;
1549 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1550 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1551 of OLD_STMT if it was previously call statement.
1552 If NEW_STMT is NULL, the call has been dropped without any
1553 replacement. */
1555 static void
1556 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1557 gimple old_stmt, tree old_call,
1558 gimple new_stmt)
1560 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1561 ? gimple_call_fndecl (new_stmt) : 0;
1563 /* We are seeing indirect calls, then there is nothing to update. */
1564 if (!new_call && !old_call)
1565 return;
1566 /* See if we turned indirect call into direct call or folded call to one builtin
1567 into different builtin. */
1568 if (old_call != new_call)
1570 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1571 struct cgraph_edge *ne = NULL;
1572 gcov_type count;
1573 int frequency;
1575 if (e)
1577 /* See if the edge is already there and has the correct callee. It
1578 might be so because of indirect inlining has already updated
1579 it. We also might've cloned and redirected the edge. */
1580 if (new_call && e->callee)
1582 struct cgraph_node *callee = e->callee;
1583 while (callee)
1585 if (callee->decl == new_call
1586 || callee->former_clone_of == new_call)
1588 cgraph_set_call_stmt (e, new_stmt);
1589 return;
1591 callee = callee->clone_of;
1595 /* Otherwise remove edge and create new one; we can't simply redirect
1596 since function has changed, so inline plan and other information
1597 attached to edge is invalid. */
1598 count = e->count;
1599 frequency = e->frequency;
1600 if (e->indirect_unknown_callee || e->inline_failed)
1601 cgraph_remove_edge (e);
1602 else
1603 cgraph_remove_node_and_inline_clones (e->callee, NULL);
1605 else if (new_call)
1607 /* We are seeing new direct call; compute profile info based on BB. */
1608 basic_block bb = gimple_bb (new_stmt);
1609 count = bb->count;
1610 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1611 bb);
1614 if (new_call)
1616 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1617 new_stmt, count, frequency);
1618 gcc_assert (ne->inline_failed);
1621 /* We only updated the call stmt; update pointer in cgraph edge.. */
1622 else if (old_stmt != new_stmt)
1623 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1626 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1627 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1628 of OLD_STMT before it was updated (updating can happen inplace). */
1630 void
1631 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1633 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1634 struct cgraph_node *node;
1636 gcc_checking_assert (orig);
1637 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1638 if (orig->clones)
1639 for (node = orig->clones; node != orig;)
1641 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1642 if (node->clones)
1643 node = node->clones;
1644 else if (node->next_sibling_clone)
1645 node = node->next_sibling_clone;
1646 else
1648 while (node != orig && !node->next_sibling_clone)
1649 node = node->clone_of;
1650 if (node != orig)
1651 node = node->next_sibling_clone;
1657 /* Remove all callees from the node. */
1659 void
1660 cgraph_node_remove_callees (struct cgraph_node *node)
1662 struct cgraph_edge *e, *f;
1664 /* It is sufficient to remove the edges from the lists of callers of
1665 the callees. The callee list of the node can be zapped with one
1666 assignment. */
1667 for (e = node->callees; e; e = f)
1669 f = e->next_callee;
1670 cgraph_call_edge_removal_hooks (e);
1671 if (!e->indirect_unknown_callee)
1672 cgraph_edge_remove_callee (e);
1673 cgraph_free_edge (e);
1675 for (e = node->indirect_calls; e; e = f)
1677 f = e->next_callee;
1678 cgraph_call_edge_removal_hooks (e);
1679 if (!e->indirect_unknown_callee)
1680 cgraph_edge_remove_callee (e);
1681 cgraph_free_edge (e);
1683 node->indirect_calls = NULL;
1684 node->callees = NULL;
1685 if (node->call_site_hash)
1687 htab_delete (node->call_site_hash);
1688 node->call_site_hash = NULL;
1692 /* Remove all callers from the node. */
1694 static void
1695 cgraph_node_remove_callers (struct cgraph_node *node)
1697 struct cgraph_edge *e, *f;
1699 /* It is sufficient to remove the edges from the lists of callees of
1700 the callers. The caller list of the node can be zapped with one
1701 assignment. */
1702 for (e = node->callers; e; e = f)
1704 f = e->next_caller;
1705 cgraph_call_edge_removal_hooks (e);
1706 cgraph_edge_remove_caller (e);
1707 cgraph_free_edge (e);
1709 node->callers = NULL;
1712 /* Helper function for cgraph_release_function_body and free_lang_data.
1713 It releases body from function DECL without having to inspect its
1714 possibly non-existent symtab node. */
1716 void
1717 release_function_body (tree decl)
1719 if (cgraph_get_node (decl)
1720 && cgraph_is_aux_decl_external (cgraph_get_node (decl)))
1721 DECL_EXTERNAL (decl) = 1;
1723 if (DECL_STRUCT_FUNCTION (decl))
1725 push_cfun (DECL_STRUCT_FUNCTION (decl));
1726 if (cfun->cfg
1727 && current_loops)
1729 cfun->curr_properties &= ~PROP_loops;
1730 loop_optimizer_finalize ();
1732 if (cfun->gimple_df)
1734 delete_tree_ssa ();
1735 delete_tree_cfg_annotations ();
1736 cfun->eh = NULL;
1738 if (cfun->cfg)
1740 gcc_assert (dom_computed[0] == DOM_NONE);
1741 gcc_assert (dom_computed[1] == DOM_NONE);
1742 clear_edges ();
1743 cfun->cfg = NULL;
1745 if (cfun->value_histograms)
1746 free_histograms ();
1747 pop_cfun ();
1748 gimple_set_body (decl, NULL);
1749 /* Struct function hangs a lot of data that would leak if we didn't
1750 removed all pointers to it. */
1751 ggc_free (DECL_STRUCT_FUNCTION (decl));
1752 DECL_STRUCT_FUNCTION (decl) = NULL;
1754 DECL_SAVED_TREE (decl) = NULL;
1757 /* Release memory used to represent body of function NODE.
1758 Use this only for functions that are released before being translated to
1759 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1760 are free'd in final.c via free_after_compilation(). */
1762 void
1763 cgraph_release_function_body (struct cgraph_node *node)
1765 node->ipa_transforms_to_apply.release ();
1766 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1768 DECL_RESULT (node->decl) = NULL;
1769 DECL_ARGUMENTS (node->decl) = NULL;
1771 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1772 of its associated function function declaration because it's
1773 needed to emit debug info later. */
1774 if (!node->used_as_abstract_origin && DECL_INITIAL (node->decl))
1775 DECL_INITIAL (node->decl) = error_mark_node;
1776 release_function_body (node->decl);
1777 if (node->lto_file_data)
1778 lto_free_function_in_decl_state_for_node (node);
1781 /* Remove the node from cgraph. */
1783 void
1784 cgraph_remove_node (struct cgraph_node *node)
1786 struct cgraph_node *n;
1787 int uid = node->uid;
1789 cgraph_call_node_removal_hooks (node);
1790 cgraph_node_remove_callers (node);
1791 cgraph_node_remove_callees (node);
1792 node->ipa_transforms_to_apply.release ();
1794 /* Incremental inlining access removed nodes stored in the postorder list.
1796 node->force_output = false;
1797 node->forced_by_abi = false;
1798 for (n = node->nested; n; n = n->next_nested)
1799 n->origin = NULL;
1800 node->nested = NULL;
1801 if (node->origin)
1803 struct cgraph_node **node2 = &node->origin->nested;
1805 while (*node2 != node)
1806 node2 = &(*node2)->next_nested;
1807 *node2 = node->next_nested;
1809 symtab_unregister_node (node);
1810 if (node->prev_sibling_clone)
1811 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1812 else if (node->clone_of)
1813 node->clone_of->clones = node->next_sibling_clone;
1814 if (node->next_sibling_clone)
1815 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1816 if (node->clones)
1818 struct cgraph_node *n, *next;
1820 if (node->clone_of)
1822 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1823 n->clone_of = node->clone_of;
1824 n->clone_of = node->clone_of;
1825 n->next_sibling_clone = node->clone_of->clones;
1826 if (node->clone_of->clones)
1827 node->clone_of->clones->prev_sibling_clone = n;
1828 node->clone_of->clones = node->clones;
1830 else
1832 /* We are removing node with clones. This makes clones inconsistent,
1833 but assume they will be removed subsequently and just keep clone
1834 tree intact. This can happen in unreachable function removal since
1835 we remove unreachable functions in random order, not by bottom-up
1836 walk of clone trees. */
1837 for (n = node->clones; n; n = next)
1839 next = n->next_sibling_clone;
1840 n->next_sibling_clone = NULL;
1841 n->prev_sibling_clone = NULL;
1842 n->clone_of = NULL;
1847 /* While all the clones are removed after being proceeded, the function
1848 itself is kept in the cgraph even after it is compiled. Check whether
1849 we are done with this body and reclaim it proactively if this is the case.
1851 if (cgraph_state != CGRAPH_LTO_STREAMING)
1853 n = cgraph_get_node (node->decl);
1854 if (!n
1855 || (!n->clones && !n->clone_of && !n->global.inlined_to
1856 && (cgraph_global_info_ready
1857 && (TREE_ASM_WRITTEN (n->decl)
1858 || DECL_EXTERNAL (n->decl)
1859 || !n->analyzed
1860 || (!flag_wpa && n->in_other_partition)))))
1861 cgraph_release_function_body (node);
1864 cgraph_remove_link_node (node);
1865 node->decl = NULL;
1867 if (node->call_site_hash)
1869 htab_delete (node->call_site_hash);
1870 node->call_site_hash = NULL;
1872 cgraph_n_nodes--;
1874 /* Clear out the node to NULL all pointers and add the node to the free
1875 list. */
1876 memset (node, 0, sizeof (*node));
1877 node->type = SYMTAB_FUNCTION;
1878 node->uid = uid;
1879 SET_NEXT_FREE_NODE (node, free_nodes);
1880 free_nodes = node;
1884 /* Likewise indicate that a node is having address taken. */
1886 void
1887 cgraph_mark_address_taken_node (struct cgraph_node *node)
1889 /* Indirect inlining can figure out that all uses of the address are
1890 inlined. */
1891 if (node->global.inlined_to)
1893 gcc_assert (cfun->after_inlining);
1894 gcc_assert (node->callers->indirect_inlining_edge);
1895 return;
1897 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1898 IPA_REF_ADDR reference exists (and thus it should be set on node
1899 representing alias we take address of) and as a test whether address
1900 of the object was taken (and thus it should be set on node alias is
1901 referring to). We should remove the first use and the remove the
1902 following set. */
1903 node->address_taken = 1;
1904 node = cgraph_function_or_thunk_node (node, NULL);
1905 node->address_taken = 1;
1908 /* Return local info for the compiled function. */
1910 struct cgraph_local_info *
1911 cgraph_local_info (tree decl)
1913 struct cgraph_node *node;
1915 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1916 node = cgraph_get_node (decl);
1917 if (!node)
1918 return NULL;
1919 return &node->local;
1922 /* Return local info for the compiled function. */
1924 struct cgraph_global_info *
1925 cgraph_global_info (tree decl)
1927 struct cgraph_node *node;
1929 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1930 node = cgraph_get_node (decl);
1931 if (!node)
1932 return NULL;
1933 return &node->global;
1936 /* Return local info for the compiled function. */
1938 struct cgraph_rtl_info *
1939 cgraph_rtl_info (tree decl)
1941 struct cgraph_node *node;
1943 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1944 node = cgraph_get_node (decl);
1945 if (!node
1946 || (decl != current_function_decl
1947 && !TREE_ASM_WRITTEN (node->decl)))
1948 return NULL;
1949 return &node->rtl;
1952 /* Return a string describing the failure REASON. */
1954 const char*
1955 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1957 #undef DEFCIFCODE
1958 #define DEFCIFCODE(code, type, string) string,
1960 static const char *cif_string_table[CIF_N_REASONS] = {
1961 #include "cif-code.def"
1964 /* Signedness of an enum type is implementation defined, so cast it
1965 to unsigned before testing. */
1966 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1967 return cif_string_table[reason];
1970 /* Return a type describing the failure REASON. */
1972 cgraph_inline_failed_type_t
1973 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1975 #undef DEFCIFCODE
1976 #define DEFCIFCODE(code, type, string) type,
1978 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1979 #include "cif-code.def"
1982 /* Signedness of an enum type is implementation defined, so cast it
1983 to unsigned before testing. */
1984 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1985 return cif_type_table[reason];
1988 /* Names used to print out the availability enum. */
1989 const char * const cgraph_availability_names[] =
1990 {"unset", "not_available", "overwritable", "available", "local"};
1993 /* Dump call graph node NODE to file F. */
1995 void
1996 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1998 struct cgraph_edge *edge;
1999 int indirect_calls_count = 0;
2001 dump_symtab_base (f, node);
2003 if (node->global.inlined_to)
2004 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
2005 xstrdup (node->name ()),
2006 node->order,
2007 xstrdup (node->global.inlined_to->name ()),
2008 node->global.inlined_to->order);
2009 if (node->clone_of)
2010 fprintf (f, " Clone of %s/%i\n",
2011 node->clone_of->asm_name (),
2012 node->clone_of->order);
2013 if (cgraph_function_flags_ready)
2014 fprintf (f, " Availability: %s\n",
2015 cgraph_availability_names [cgraph_function_body_availability (node)]);
2017 if (node->profile_id)
2018 fprintf (f, " Profile id: %i\n",
2019 node->profile_id);
2020 fprintf (f, " First run: %i\n", node->tp_first_run);
2021 fprintf (f, " Function flags:");
2022 if (node->count)
2023 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
2024 (HOST_WIDEST_INT)node->count);
2025 if (node->max_bb_count)
2026 fprintf (f, " hottest bb executed "HOST_WIDEST_INT_PRINT_DEC"x",
2027 (HOST_WIDEST_INT)node->max_bb_count);
2028 if (node->origin)
2029 fprintf (f, " nested in: %s", node->origin->asm_name ());
2030 if (gimple_has_body_p (node->decl))
2031 fprintf (f, " body");
2032 if (node->process)
2033 fprintf (f, " process");
2034 if (node->local.local)
2035 fprintf (f, " local");
2036 if (node->local.redefined_extern_inline)
2037 fprintf (f, " redefined_extern_inline");
2038 if (node->only_called_at_startup)
2039 fprintf (f, " only_called_at_startup");
2040 if (node->only_called_at_exit)
2041 fprintf (f, " only_called_at_exit");
2042 if (node->tm_clone)
2043 fprintf (f, " tm_clone");
2045 fprintf (f, "\n");
2047 if (node->thunk.thunk_p)
2049 fprintf (f, " Thunk");
2050 if (node->thunk.alias)
2051 fprintf (f, " of %s (asm: %s)",
2052 lang_hooks.decl_printable_name (node->thunk.alias, 2),
2053 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
2054 fprintf (f, " fixed offset %i virtual value %i has "
2055 "virtual offset %i)\n",
2056 (int)node->thunk.fixed_offset,
2057 (int)node->thunk.virtual_value,
2058 (int)node->thunk.virtual_offset_p);
2060 if (node->alias && node->thunk.alias
2061 && DECL_P (node->thunk.alias))
2063 fprintf (f, " Alias of %s",
2064 lang_hooks.decl_printable_name (node->thunk.alias, 2));
2065 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
2066 fprintf (f, " (asm: %s)",
2067 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
2068 fprintf (f, "\n");
2071 fprintf (f, " Called by: ");
2073 for (edge = node->callers; edge; edge = edge->next_caller)
2075 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2076 edge->caller->order);
2077 if (edge->count)
2078 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
2079 (HOST_WIDEST_INT)edge->count);
2080 if (edge->frequency)
2081 fprintf (f, "(%.2f per call) ",
2082 edge->frequency / (double)CGRAPH_FREQ_BASE);
2083 if (edge->speculative)
2084 fprintf (f, "(speculative) ");
2085 if (!edge->inline_failed)
2086 fprintf (f, "(inlined) ");
2087 if (edge->indirect_inlining_edge)
2088 fprintf (f, "(indirect_inlining) ");
2089 if (edge->can_throw_external)
2090 fprintf (f, "(can throw external) ");
2093 fprintf (f, "\n Calls: ");
2094 for (edge = node->callees; edge; edge = edge->next_callee)
2096 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2097 edge->callee->order);
2098 if (edge->speculative)
2099 fprintf (f, "(speculative) ");
2100 if (!edge->inline_failed)
2101 fprintf (f, "(inlined) ");
2102 if (edge->indirect_inlining_edge)
2103 fprintf (f, "(indirect_inlining) ");
2104 if (edge->count)
2105 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
2106 (HOST_WIDEST_INT)edge->count);
2107 if (edge->frequency)
2108 fprintf (f, "(%.2f per call) ",
2109 edge->frequency / (double)CGRAPH_FREQ_BASE);
2110 if (edge->can_throw_external)
2111 fprintf (f, "(can throw external) ");
2113 fprintf (f, "\n");
2115 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2116 indirect_calls_count++;
2117 if (indirect_calls_count)
2118 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2119 indirect_calls_count);
2123 /* Dump call graph node NODE to stderr. */
2125 DEBUG_FUNCTION void
2126 debug_cgraph_node (struct cgraph_node *node)
2128 dump_cgraph_node (stderr, node);
2132 /* Dump the callgraph to file F. */
2134 void
2135 dump_cgraph (FILE *f)
2137 struct cgraph_node *node;
2139 fprintf (f, "callgraph:\n\n");
2140 FOR_EACH_FUNCTION (node)
2141 dump_cgraph_node (f, node);
2145 /* Dump the call graph to stderr. */
2147 DEBUG_FUNCTION void
2148 debug_cgraph (void)
2150 dump_cgraph (stderr);
2153 /* Return true when the DECL can possibly be inlined. */
2154 bool
2155 cgraph_function_possibly_inlined_p (tree decl)
2157 if (!cgraph_global_info_ready)
2158 return !DECL_UNINLINABLE (decl);
2159 return DECL_POSSIBLY_INLINED (decl);
2162 /* NODE is no longer nested function; update cgraph accordingly. */
2163 void
2164 cgraph_unnest_node (struct cgraph_node *node)
2166 struct cgraph_node **node2 = &node->origin->nested;
2167 gcc_assert (node->origin);
2169 while (*node2 != node)
2170 node2 = &(*node2)->next_nested;
2171 *node2 = node->next_nested;
2172 node->origin = NULL;
2175 /* Return function availability. See cgraph.h for description of individual
2176 return values. */
2177 enum availability
2178 cgraph_function_body_availability (struct cgraph_node *node)
2180 enum availability avail;
2181 if (!node->analyzed)
2182 avail = AVAIL_NOT_AVAILABLE;
2183 else if (node->local.local)
2184 avail = AVAIL_LOCAL;
2185 else if (node->alias && node->weakref)
2186 cgraph_function_or_thunk_node (node, &avail);
2187 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->decl)))
2188 avail = AVAIL_OVERWRITABLE;
2189 else if (!node->externally_visible)
2190 avail = AVAIL_AVAILABLE;
2191 /* Inline functions are safe to be analyzed even if their symbol can
2192 be overwritten at runtime. It is not meaningful to enforce any sane
2193 behaviour on replacing inline function by different body. */
2194 else if (DECL_DECLARED_INLINE_P (node->decl))
2195 avail = AVAIL_AVAILABLE;
2197 /* If the function can be overwritten, return OVERWRITABLE. Take
2198 care at least of two notable extensions - the COMDAT functions
2199 used to share template instantiations in C++ (this is symmetric
2200 to code cp_cannot_inline_tree_fn and probably shall be shared and
2201 the inlinability hooks completely eliminated).
2203 ??? Does the C++ one definition rule allow us to always return
2204 AVAIL_AVAILABLE here? That would be good reason to preserve this
2205 bit. */
2207 else if (decl_replaceable_p (node->decl)
2208 && !DECL_EXTERNAL (node->decl))
2209 avail = AVAIL_OVERWRITABLE;
2210 else avail = AVAIL_AVAILABLE;
2212 return avail;
2215 /* Worker for cgraph_node_can_be_local_p. */
2216 static bool
2217 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2218 void *data ATTRIBUTE_UNUSED)
2220 return !(!node->force_output
2221 && ((DECL_COMDAT (node->decl)
2222 && !node->forced_by_abi
2223 && !symtab_used_from_object_file_p (node)
2224 && !node->same_comdat_group)
2225 || !node->externally_visible));
2228 /* Return true if NODE can be made local for API change.
2229 Extern inline functions and C++ COMDAT functions can be made local
2230 at the expense of possible code size growth if function is used in multiple
2231 compilation units. */
2232 bool
2233 cgraph_node_can_be_local_p (struct cgraph_node *node)
2235 return (!node->address_taken
2236 && !cgraph_for_node_and_aliases (node,
2237 cgraph_node_cannot_be_local_p_1,
2238 NULL, true));
2241 /* Call calback on NODE, thunks and aliases associated to NODE.
2242 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2243 skipped. */
2245 bool
2246 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2247 bool (*callback) (struct cgraph_node *, void *),
2248 void *data,
2249 bool include_overwritable)
2251 struct cgraph_edge *e;
2252 int i;
2253 struct ipa_ref *ref;
2255 if (callback (node, data))
2256 return true;
2257 for (e = node->callers; e; e = e->next_caller)
2258 if (e->caller->thunk.thunk_p
2259 && (include_overwritable
2260 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2261 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2262 include_overwritable))
2263 return true;
2264 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2265 if (ref->use == IPA_REF_ALIAS)
2267 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2268 if (include_overwritable
2269 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2270 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2271 include_overwritable))
2272 return true;
2274 return false;
2277 /* Call calback on NODE and aliases associated to NODE.
2278 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2279 skipped. */
2281 bool
2282 cgraph_for_node_and_aliases (struct cgraph_node *node,
2283 bool (*callback) (struct cgraph_node *, void *),
2284 void *data,
2285 bool include_overwritable)
2287 int i;
2288 struct ipa_ref *ref;
2290 if (callback (node, data))
2291 return true;
2292 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2293 if (ref->use == IPA_REF_ALIAS)
2295 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2296 if (include_overwritable
2297 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2298 if (cgraph_for_node_and_aliases (alias, callback, data,
2299 include_overwritable))
2300 return true;
2302 return false;
2305 /* Worker to bring NODE local. */
2307 static bool
2308 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2310 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2311 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2313 symtab_make_decl_local (node->decl);
2315 node->externally_visible = false;
2316 node->forced_by_abi = false;
2317 node->local.local = true;
2318 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2319 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2320 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2321 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2323 return false;
2326 /* Bring NODE local. */
2328 void
2329 cgraph_make_node_local (struct cgraph_node *node)
2331 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2332 NULL, true);
2335 /* Worker to set nothrow flag. */
2337 static bool
2338 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2340 struct cgraph_edge *e;
2342 TREE_NOTHROW (node->decl) = data != NULL;
2344 if (data != NULL)
2345 for (e = node->callers; e; e = e->next_caller)
2346 e->can_throw_external = false;
2347 return false;
2350 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2351 if any to NOTHROW. */
2353 void
2354 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2356 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2357 (void *)(size_t)nothrow, false);
2360 /* Worker to set const flag. */
2362 static bool
2363 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2365 /* Static constructors and destructors without a side effect can be
2366 optimized out. */
2367 if (data && !((size_t)data & 2))
2369 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2370 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2371 if (DECL_STATIC_DESTRUCTOR (node->decl))
2372 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2374 TREE_READONLY (node->decl) = data != NULL;
2375 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2376 return false;
2379 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2380 if any to READONLY. */
2382 void
2383 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2385 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2386 (void *)(size_t)(readonly + (int)looping * 2),
2387 false);
2390 /* Worker to set pure flag. */
2392 static bool
2393 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2395 /* Static constructors and destructors without a side effect can be
2396 optimized out. */
2397 if (data && !((size_t)data & 2))
2399 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2400 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2401 if (DECL_STATIC_DESTRUCTOR (node->decl))
2402 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2404 DECL_PURE_P (node->decl) = data != NULL;
2405 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2406 return false;
2409 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2410 if any to PURE. */
2412 void
2413 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2415 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2416 (void *)(size_t)(pure + (int)looping * 2),
2417 false);
2420 /* Return true when NODE can not return or throw and thus
2421 it is safe to ignore its side effects for IPA analysis. */
2423 bool
2424 cgraph_node_cannot_return (struct cgraph_node *node)
2426 int flags = flags_from_decl_or_type (node->decl);
2427 if (!flag_exceptions)
2428 return (flags & ECF_NORETURN) != 0;
2429 else
2430 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2431 == (ECF_NORETURN | ECF_NOTHROW));
2434 /* Return true when call of E can not lead to return from caller
2435 and thus it is safe to ignore its side effects for IPA analysis
2436 when computing side effects of the caller.
2437 FIXME: We could actually mark all edges that have no reaching
2438 patch to the exit block or throw to get better results. */
2439 bool
2440 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2442 if (cgraph_node_cannot_return (e->caller))
2443 return true;
2444 if (e->indirect_unknown_callee)
2446 int flags = e->indirect_info->ecf_flags;
2447 if (!flag_exceptions)
2448 return (flags & ECF_NORETURN) != 0;
2449 else
2450 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2451 == (ECF_NORETURN | ECF_NOTHROW));
2453 else
2454 return cgraph_node_cannot_return (e->callee);
2457 /* Return true when function NODE can be removed from callgraph
2458 if all direct calls are eliminated. */
2460 bool
2461 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2463 gcc_assert (!node->global.inlined_to);
2464 /* Extern inlines can always go, we will use the external definition. */
2465 if (DECL_EXTERNAL (node->decl))
2466 return true;
2467 /* Aux functions are safe to remove, but only once static promotion is
2468 complete since they may affect promoted names if they are the context
2469 for any static variables. */
2470 if (cgraph_pre_profiling_inlining_done && cgraph_is_aux_decl_external (node))
2471 return true;
2472 /* When function is needed, we can not remove it. */
2473 if (node->force_output || node->used_from_other_partition)
2474 return false;
2475 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2476 || DECL_STATIC_DESTRUCTOR (node->decl))
2477 return false;
2478 /* Only COMDAT functions can be removed if externally visible. */
2479 if (node->externally_visible
2480 && (!DECL_COMDAT (node->decl)
2481 || node->forced_by_abi
2482 || symtab_used_from_object_file_p (node)))
2483 return false;
2484 return true;
2487 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2489 static bool
2490 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2492 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2495 /* Return true when function NODE and its aliases can be removed from callgraph
2496 if all direct calls are eliminated. */
2498 bool
2499 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2501 /* Extern inlines can always go, we will use the external definition. */
2502 if (DECL_EXTERNAL (node->decl))
2503 return true;
2504 if (node->address_taken)
2505 return false;
2506 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2509 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2511 static bool
2512 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2514 return symtab_used_from_object_file_p (node);
2517 /* Return true when function NODE can be expected to be removed
2518 from program when direct calls in this compilation unit are removed.
2520 As a special case COMDAT functions are
2521 cgraph_can_remove_if_no_direct_calls_p while the are not
2522 cgraph_only_called_directly_p (it is possible they are called from other
2523 unit)
2525 This function behaves as cgraph_only_called_directly_p because eliminating
2526 all uses of COMDAT function does not make it necessarily disappear from
2527 the program unless we are compiling whole program or we do LTO. In this
2528 case we know we win since dynamic linking will not really discard the
2529 linkonce section. */
2531 bool
2532 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2534 gcc_assert (!node->global.inlined_to);
2535 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2536 return false;
2537 if (!in_lto_p && !flag_whole_program)
2538 return cgraph_only_called_directly_p (node);
2539 else
2541 if (DECL_EXTERNAL (node->decl))
2542 return true;
2543 return cgraph_can_remove_if_no_direct_calls_p (node);
2548 /* Worker for cgraph_only_called_directly_p. */
2550 static bool
2551 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2553 return !cgraph_only_called_directly_or_aliased_p (node);
2556 /* Return true when function NODE and all its aliases are only called
2557 directly.
2558 i.e. it is not externally visible, address was not taken and
2559 it is not used in any other non-standard way. */
2561 bool
2562 cgraph_only_called_directly_p (struct cgraph_node *node)
2564 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2565 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2566 NULL, true);
2570 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2572 static bool
2573 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2575 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2576 struct cgraph_edge *cs;
2577 enum availability avail;
2578 cgraph_function_or_thunk_node (node, &avail);
2580 if (avail > AVAIL_OVERWRITABLE)
2581 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2582 if (!cs->indirect_inlining_edge)
2583 redirect_callers->safe_push (cs);
2584 return false;
2587 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2588 (i.e. are not overwritable). */
2590 vec<cgraph_edge_p>
2591 collect_callers_of_node (struct cgraph_node *node)
2593 vec<cgraph_edge_p> redirect_callers = vNULL;
2594 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2595 &redirect_callers, false);
2596 return redirect_callers;
2599 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2601 static bool
2602 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2604 bool skipped_thunk = false;
2605 node = cgraph_function_or_thunk_node (node, NULL);
2606 node2 = cgraph_function_or_thunk_node (node2, NULL);
2608 /* There are no virtual clones of thunks so check former_clone_of or if we
2609 might have skipped thunks because this adjustments are no longer
2610 necessary. */
2611 while (node->thunk.thunk_p)
2613 if (node2->former_clone_of == node->decl)
2614 return true;
2615 if (!node->thunk.this_adjusting)
2616 return false;
2617 node = cgraph_function_or_thunk_node (node->callees->callee, NULL);
2618 skipped_thunk = true;
2621 if (skipped_thunk)
2623 if (!node2->clone.args_to_skip
2624 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2625 return false;
2626 if (node2->former_clone_of == node->decl)
2627 return true;
2628 else if (!node2->clone_of)
2629 return false;
2632 while (node != node2 && node2)
2633 node2 = node2->clone_of;
2634 return node2 != NULL;
2637 /* Verify edge E count and frequency. */
2639 static bool
2640 verify_edge_count_and_frequency (struct cgraph_edge *e)
2642 bool error_found = false;
2643 if (e->count < 0)
2645 error ("caller edge count is negative");
2646 error_found = true;
2648 if (e->frequency < 0)
2650 error ("caller edge frequency is negative");
2651 error_found = true;
2653 if (e->frequency > CGRAPH_FREQ_MAX)
2655 error ("caller edge frequency is too large");
2656 error_found = true;
2658 if (gimple_has_body_p (e->caller->decl)
2659 && e->call_stmt
2660 && !e->caller->global.inlined_to
2661 && !e->speculative
2662 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2663 Remove this once edges are actually removed from the function at that time. */
2664 && (e->frequency
2665 || (inline_edge_summary_vec.exists ()
2666 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2667 || !inline_edge_summary (e)->predicate)))
2668 && (e->frequency
2669 != compute_call_stmt_bb_frequency (e->caller->decl,
2670 gimple_bb (e->call_stmt))))
2672 error ("caller edge frequency %i does not match BB frequency %i",
2673 e->frequency,
2674 compute_call_stmt_bb_frequency (e->caller->decl,
2675 gimple_bb (e->call_stmt)));
2676 error_found = true;
2678 return error_found;
2681 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2682 static void
2683 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2685 bool fndecl_was_null = false;
2686 /* debug_gimple_stmt needs correct cfun */
2687 if (cfun != this_cfun)
2688 set_cfun (this_cfun);
2689 /* ...and an actual current_function_decl */
2690 if (!current_function_decl)
2692 current_function_decl = this_cfun->decl;
2693 fndecl_was_null = true;
2695 debug_gimple_stmt (stmt);
2696 if (fndecl_was_null)
2697 current_function_decl = NULL;
2700 /* Verify that call graph edge E corresponds to DECL from the associated
2701 statement. Return true if the verification should fail. */
2703 static bool
2704 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2706 struct cgraph_node *node;
2708 if (!decl || e->callee->global.inlined_to)
2709 return false;
2710 if (cgraph_state == CGRAPH_LTO_STREAMING)
2711 return false;
2712 node = cgraph_get_node (decl);
2714 /* We do not know if a node from a different partition is an alias or what it
2715 aliases and therefore cannot do the former_clone_of check reliably. When
2716 body_removed is set, we have lost all information about what was alias or
2717 thunk of and also cannot proceed. */
2718 if (!node
2719 || node->body_removed
2720 || node->in_other_partition
2721 || e->callee->in_other_partition)
2722 return false;
2724 /* Optimizers can redirect unreachable calls or calls triggering undefined
2725 behaviour to builtin_unreachable. */
2726 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2727 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2728 return false;
2729 node = cgraph_function_or_thunk_node (node, NULL);
2731 if (e->callee->former_clone_of != node->decl
2732 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2733 && !clone_of_p (node, e->callee))
2734 return true;
2735 else
2736 return false;
2739 /* Verify cgraph nodes of given cgraph node. */
2740 DEBUG_FUNCTION void
2741 verify_cgraph_node (struct cgraph_node *node)
2743 struct cgraph_edge *e;
2744 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
2745 basic_block this_block;
2746 gimple_stmt_iterator gsi;
2747 bool error_found = false;
2749 if (seen_error ())
2750 return;
2752 timevar_push (TV_CGRAPH_VERIFY);
2753 error_found |= verify_symtab_base (node);
2754 for (e = node->callees; e; e = e->next_callee)
2755 if (e->aux)
2757 error ("aux field set for edge %s->%s",
2758 identifier_to_locale (e->caller->name ()),
2759 identifier_to_locale (e->callee->name ()));
2760 error_found = true;
2762 if (node->count < 0)
2764 error ("execution count is negative");
2765 error_found = true;
2767 if (node->global.inlined_to && node->same_comdat_group)
2769 error ("inline clone in same comdat group list");
2770 error_found = true;
2772 if (!node->definition && !node->in_other_partition && node->local.local)
2774 error ("local symbols must be defined");
2775 error_found = true;
2777 if (node->global.inlined_to && node->externally_visible)
2779 error ("externally visible inline clone");
2780 error_found = true;
2782 if (node->global.inlined_to && node->address_taken)
2784 error ("inline clone with address taken");
2785 error_found = true;
2787 if (node->global.inlined_to && node->force_output)
2789 error ("inline clone is forced to output");
2790 error_found = true;
2792 for (e = node->indirect_calls; e; e = e->next_callee)
2794 if (e->aux)
2796 error ("aux field set for indirect edge from %s",
2797 identifier_to_locale (e->caller->name ()));
2798 error_found = true;
2800 if (!e->indirect_unknown_callee
2801 || !e->indirect_info)
2803 error ("An indirect edge from %s is not marked as indirect or has "
2804 "associated indirect_info, the corresponding statement is: ",
2805 identifier_to_locale (e->caller->name ()));
2806 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2807 error_found = true;
2810 bool check_comdat = symtab_comdat_local_p (node);
2811 for (e = node->callers; e; e = e->next_caller)
2813 if (verify_edge_count_and_frequency (e))
2814 error_found = true;
2815 if (check_comdat
2816 && !symtab_in_same_comdat_p (e->caller, node))
2818 error ("comdat-local function called by %s outside its comdat",
2819 identifier_to_locale (e->caller->name ()));
2820 error_found = true;
2822 if (!e->inline_failed)
2824 if (node->global.inlined_to
2825 != (e->caller->global.inlined_to
2826 ? e->caller->global.inlined_to : e->caller))
2828 error ("inlined_to pointer is wrong");
2829 error_found = true;
2831 if (node->callers->next_caller)
2833 error ("multiple inline callers");
2834 error_found = true;
2837 else
2838 if (node->global.inlined_to)
2840 error ("inlined_to pointer set for noninline callers");
2841 error_found = true;
2844 for (e = node->indirect_calls; e; e = e->next_callee)
2845 if (verify_edge_count_and_frequency (e))
2846 error_found = true;
2847 if (!node->callers && node->global.inlined_to)
2849 error ("inlined_to pointer is set but no predecessors found");
2850 error_found = true;
2852 if (node->global.inlined_to == node)
2854 error ("inlined_to pointer refers to itself");
2855 error_found = true;
2858 if (node->clone_of)
2860 struct cgraph_node *n;
2861 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2862 if (n == node)
2863 break;
2864 if (!n)
2866 error ("node has wrong clone_of");
2867 error_found = true;
2870 if (node->clones)
2872 struct cgraph_node *n;
2873 for (n = node->clones; n; n = n->next_sibling_clone)
2874 if (n->clone_of != node)
2875 break;
2876 if (n)
2878 error ("node has wrong clone list");
2879 error_found = true;
2882 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2884 error ("node is in clone list but it is not clone");
2885 error_found = true;
2887 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2889 error ("node has wrong prev_clone pointer");
2890 error_found = true;
2892 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2894 error ("double linked list of clones corrupted");
2895 error_found = true;
2898 if (node->analyzed && node->alias)
2900 bool ref_found = false;
2901 int i;
2902 struct ipa_ref *ref;
2904 if (node->callees)
2906 error ("Alias has call edges");
2907 error_found = true;
2909 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
2910 i, ref); i++)
2911 if (ref->use != IPA_REF_ALIAS)
2913 error ("Alias has non-alias reference");
2914 error_found = true;
2916 else if (ref_found
2917 /* in LIPO mode, the alias can refer to the real target also */
2918 && !L_IPO_COMP_MODE)
2920 error ("Alias has more than one alias reference");
2921 error_found = true;
2923 else
2924 ref_found = true;
2925 if (!ref_found)
2927 error ("Analyzed alias has no reference");
2928 error_found = true;
2931 if (node->analyzed && node->thunk.thunk_p)
2933 if (!node->callees)
2935 error ("No edge out of thunk node");
2936 error_found = true;
2938 else if (node->callees->next_callee)
2940 error ("More than one edge out of thunk node");
2941 error_found = true;
2943 if (gimple_has_body_p (node->decl))
2945 error ("Thunk is not supposed to have body");
2946 error_found = true;
2949 else if (node->analyzed && gimple_has_body_p (node->decl)
2950 && !TREE_ASM_WRITTEN (node->decl)
2951 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
2952 && !flag_wpa)
2954 if (this_cfun->cfg)
2956 pointer_set_t *stmts = pointer_set_create ();
2957 int i;
2958 struct ipa_ref *ref;
2960 /* Reach the trees by walking over the CFG, and note the
2961 enclosing basic-blocks in the call edges. */
2962 FOR_EACH_BB_FN (this_block, this_cfun)
2964 for (gsi = gsi_start_phis (this_block);
2965 !gsi_end_p (gsi); gsi_next (&gsi))
2966 pointer_set_insert (stmts, gsi_stmt (gsi));
2967 for (gsi = gsi_start_bb (this_block);
2968 !gsi_end_p (gsi);
2969 gsi_next (&gsi))
2971 gimple stmt = gsi_stmt (gsi);
2972 pointer_set_insert (stmts, stmt);
2973 if (is_gimple_call (stmt))
2975 struct cgraph_edge *e = cgraph_edge (node, stmt);
2976 tree decl = gimple_call_fndecl (stmt);
2977 if (e)
2979 if (e->aux)
2981 error ("shared call_stmt:");
2982 cgraph_debug_gimple_stmt (this_cfun, stmt);
2983 error_found = true;
2985 if (!e->indirect_unknown_callee)
2987 if (verify_edge_corresponds_to_fndecl (e, decl))
2989 error ("edge points to wrong declaration:");
2990 debug_tree (e->callee->decl);
2991 fprintf (stderr," Instead of:");
2992 debug_tree (decl);
2993 error_found = true;
2996 else if (decl)
2998 error ("an indirect edge with unknown callee "
2999 "corresponding to a call_stmt with "
3000 "a known declaration:");
3001 error_found = true;
3002 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3004 e->aux = (void *)1;
3006 else if (decl)
3008 error ("missing callgraph edge for call stmt:");
3009 cgraph_debug_gimple_stmt (this_cfun, stmt);
3010 error_found = true;
3015 for (i = 0;
3016 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
3017 i++)
3018 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
3020 error ("reference to dead statement");
3021 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3022 error_found = true;
3024 pointer_set_destroy (stmts);
3026 else
3027 /* No CFG available?! */
3028 gcc_unreachable ();
3030 for (e = node->callees; e; e = e->next_callee)
3032 if (!e->aux && e->call_stmt)
3034 error ("edge %s->%s has no corresponding call_stmt",
3035 identifier_to_locale (e->caller->name ()),
3036 identifier_to_locale (e->callee->name ()));
3037 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3038 error_found = true;
3040 e->aux = 0;
3042 for (e = node->indirect_calls; e; e = e->next_callee)
3044 if (!e->aux && !e->speculative)
3046 error ("an indirect edge from %s has no corresponding call_stmt",
3047 identifier_to_locale (e->caller->name ()));
3048 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3049 error_found = true;
3051 e->aux = 0;
3054 if (error_found)
3056 dump_cgraph_node (stderr, node);
3057 internal_error ("verify_cgraph_node failed");
3059 timevar_pop (TV_CGRAPH_VERIFY);
3062 /* Verify whole cgraph structure. */
3063 DEBUG_FUNCTION void
3064 verify_cgraph (void)
3066 struct cgraph_node *node;
3068 if (seen_error ())
3069 return;
3071 FOR_EACH_FUNCTION (node)
3072 verify_cgraph_node (node);
3075 /* Given NODE, walk the alias chain to return the function NODE is alias of.
3076 Walk through thunk, too.
3077 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3079 struct cgraph_node *
3080 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
3084 node = cgraph_function_or_thunk_node (node, availability);
3085 if (node->thunk.thunk_p)
3087 node = node->callees->callee;
3088 if (availability)
3090 enum availability a;
3091 a = cgraph_function_body_availability (node);
3092 if (a < *availability)
3093 *availability = a;
3095 node = cgraph_function_or_thunk_node (node, availability);
3097 } while (node && node->thunk.thunk_p);
3098 return node;
3101 /* When doing LTO, read NODE's body from disk if it is not already present. */
3103 bool
3104 cgraph_get_body (struct cgraph_node *node)
3106 struct lto_file_decl_data *file_data;
3107 const char *data, *name;
3108 size_t len;
3109 tree decl = node->decl;
3111 if (DECL_RESULT (decl))
3112 return false;
3114 gcc_assert (in_lto_p);
3116 file_data = node->lto_file_data;
3117 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3119 /* We may have renamed the declaration, e.g., a static function. */
3120 name = lto_get_decl_name_mapping (file_data, name);
3122 data = lto_get_section_data (file_data, LTO_section_function_body,
3123 name, &len);
3124 if (!data)
3126 dump_cgraph_node (stderr, node);
3127 fatal_error ("%s: section %s is missing",
3128 file_data->file_name,
3129 name);
3132 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3134 lto_input_function_body (file_data, node, data);
3135 lto_stats.num_function_bodies++;
3136 lto_free_section_data (file_data, LTO_section_function_body, name,
3137 data, len);
3138 lto_free_function_in_decl_state_for_node (node);
3139 return true;
3142 /* Verify if the type of the argument matches that of the function
3143 declaration. If we cannot verify this or there is a mismatch,
3144 return false. */
3146 static bool
3147 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3149 tree parms, p;
3150 unsigned int i, nargs;
3152 /* Calls to internal functions always match their signature. */
3153 if (gimple_call_internal_p (stmt))
3154 return true;
3156 nargs = gimple_call_num_args (stmt);
3158 /* Get argument types for verification. */
3159 if (fndecl)
3160 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3161 else
3162 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3164 /* Verify if the type of the argument matches that of the function
3165 declaration. If we cannot verify this or there is a mismatch,
3166 return false. */
3167 if (fndecl && DECL_ARGUMENTS (fndecl))
3169 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3170 i < nargs;
3171 i++, p = DECL_CHAIN (p))
3173 tree arg;
3174 /* We cannot distinguish a varargs function from the case
3175 of excess parameters, still deferring the inlining decision
3176 to the callee is possible. */
3177 if (!p)
3178 break;
3179 arg = gimple_call_arg (stmt, i);
3180 if (p == error_mark_node
3181 || DECL_ARG_TYPE (p) == error_mark_node
3182 || arg == error_mark_node
3183 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3184 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3185 return false;
3187 if (args_count_match && p)
3188 return false;
3190 else if (parms)
3192 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3194 tree arg;
3195 /* If this is a varargs function defer inlining decision
3196 to callee. */
3197 if (!p)
3198 break;
3199 arg = gimple_call_arg (stmt, i);
3200 if (TREE_VALUE (p) == error_mark_node
3201 || arg == error_mark_node
3202 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3203 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3204 && !fold_convertible_p (TREE_VALUE (p), arg)))
3205 return false;
3208 else
3210 if (nargs != 0)
3211 return false;
3213 return true;
3216 /* Verify if the type of the argument and lhs of CALL_STMT matches
3217 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3218 true, the arg count needs to be the same.
3219 If we cannot verify this or there is a mismatch, return false. */
3221 bool
3222 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3223 bool args_count_match)
3225 tree lhs;
3227 if ((DECL_RESULT (callee)
3228 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3229 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3230 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3231 TREE_TYPE (lhs))
3232 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3233 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3234 return false;
3235 return true;
3238 #include "gt-cgraph.h"