* cgraph.c (cgraph_create_indirect_edge): Use get_polymorphic_call_info.
[official-gcc.git] / gcc / cgraph.c
blob996f1b660900ad47f6a65ce670522b6311e531c8
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "tree-inline.h"
32 #include "langhooks.h"
33 #include "hashtab.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "debug.h"
38 #include "target.h"
39 #include "basic-block.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "timevar.h"
45 #include "dumpfile.h"
46 #include "gimple-ssa.h"
47 #include "cgraph.h"
48 #include "tree-cfg.h"
49 #include "tree-ssa.h"
50 #include "value-prof.h"
51 #include "except.h"
52 #include "diagnostic-core.h"
53 #include "rtl.h"
54 #include "ipa-utils.h"
55 #include "lto-streamer.h"
56 #include "ipa-inline.h"
57 #include "cfgloop.h"
58 #include "gimple-pretty-print.h"
60 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
61 #include "tree-pass.h"
63 static void cgraph_node_remove_callers (struct cgraph_node *node);
64 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
65 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
67 /* Queue of cgraph nodes scheduled to be lowered. */
68 symtab_node *x_cgraph_nodes_queue;
69 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
71 /* Number of nodes in existence. */
72 int cgraph_n_nodes;
74 /* Maximal uid used in cgraph nodes. */
75 int cgraph_max_uid;
77 /* Maximal uid used in cgraph edges. */
78 int cgraph_edge_max_uid;
80 /* Set when whole unit has been analyzed so we can access global info. */
81 bool cgraph_global_info_ready = false;
83 /* What state callgraph is in right now. */
84 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
86 /* Set when the cgraph is fully build and the basic flags are computed. */
87 bool cgraph_function_flags_ready = false;
89 /* List of hooks triggered on cgraph_edge events. */
90 struct cgraph_edge_hook_list {
91 cgraph_edge_hook hook;
92 void *data;
93 struct cgraph_edge_hook_list *next;
96 /* List of hooks triggered on cgraph_node events. */
97 struct cgraph_node_hook_list {
98 cgraph_node_hook hook;
99 void *data;
100 struct cgraph_node_hook_list *next;
103 /* List of hooks triggered on events involving two cgraph_edges. */
104 struct cgraph_2edge_hook_list {
105 cgraph_2edge_hook hook;
106 void *data;
107 struct cgraph_2edge_hook_list *next;
110 /* List of hooks triggered on events involving two cgraph_nodes. */
111 struct cgraph_2node_hook_list {
112 cgraph_2node_hook hook;
113 void *data;
114 struct cgraph_2node_hook_list *next;
117 /* List of hooks triggered when an edge is removed. */
118 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
119 /* List of hooks triggered when a node is removed. */
120 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
121 /* List of hooks triggered when an edge is duplicated. */
122 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
123 /* List of hooks triggered when a node is duplicated. */
124 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
125 /* List of hooks triggered when an function is inserted. */
126 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
128 /* Head of a linked list of unused (freed) call graph nodes.
129 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
130 static GTY(()) struct cgraph_node *free_nodes;
131 /* Head of a linked list of unused (freed) call graph edges.
132 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
133 static GTY(()) struct cgraph_edge *free_edges;
135 /* Did procss_same_body_aliases run? */
136 bool cpp_implicit_aliases_done;
138 /* Map a cgraph_node to cgraph_function_version_info using this htab.
139 The cgraph_function_version_info has a THIS_NODE field that is the
140 corresponding cgraph_node.. */
142 static GTY((param_is (struct cgraph_function_version_info))) htab_t
143 cgraph_fnver_htab = NULL;
145 /* Hash function for cgraph_fnver_htab. */
146 static hashval_t
147 cgraph_fnver_htab_hash (const void *ptr)
149 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
150 return (hashval_t)(uid);
153 /* eq function for cgraph_fnver_htab. */
154 static int
155 cgraph_fnver_htab_eq (const void *p1, const void *p2)
157 const struct cgraph_function_version_info *n1
158 = (const struct cgraph_function_version_info *)p1;
159 const struct cgraph_function_version_info *n2
160 = (const struct cgraph_function_version_info *)p2;
162 return n1->this_node->uid == n2->this_node->uid;
165 /* Mark as GC root all allocated nodes. */
166 static GTY(()) struct cgraph_function_version_info *
167 version_info_node = NULL;
169 /* Get the cgraph_function_version_info node corresponding to node. */
170 struct cgraph_function_version_info *
171 get_cgraph_node_version (struct cgraph_node *node)
173 struct cgraph_function_version_info *ret;
174 struct cgraph_function_version_info key;
175 key.this_node = node;
177 if (cgraph_fnver_htab == NULL)
178 return NULL;
180 ret = (struct cgraph_function_version_info *)
181 htab_find (cgraph_fnver_htab, &key);
183 return ret;
186 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
187 corresponding to cgraph_node NODE. */
188 struct cgraph_function_version_info *
189 insert_new_cgraph_node_version (struct cgraph_node *node)
191 void **slot;
193 version_info_node = NULL;
194 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
195 version_info_node->this_node = node;
197 if (cgraph_fnver_htab == NULL)
198 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
199 cgraph_fnver_htab_eq, NULL);
201 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
202 gcc_assert (slot != NULL);
203 *slot = version_info_node;
204 return version_info_node;
207 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
208 DECL is a duplicate declaration. */
209 void
210 delete_function_version (tree decl)
212 struct cgraph_node *decl_node = cgraph_get_node (decl);
213 struct cgraph_function_version_info *decl_v = NULL;
215 if (decl_node == NULL)
216 return;
218 decl_v = get_cgraph_node_version (decl_node);
220 if (decl_v == NULL)
221 return;
223 if (decl_v->prev != NULL)
224 decl_v->prev->next = decl_v->next;
226 if (decl_v->next != NULL)
227 decl_v->next->prev = decl_v->prev;
229 if (cgraph_fnver_htab != NULL)
230 htab_remove_elt (cgraph_fnver_htab, decl_v);
232 cgraph_remove_node (decl_node);
235 /* Record that DECL1 and DECL2 are semantically identical function
236 versions. */
237 void
238 record_function_versions (tree decl1, tree decl2)
240 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
241 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
242 struct cgraph_function_version_info *decl1_v = NULL;
243 struct cgraph_function_version_info *decl2_v = NULL;
244 struct cgraph_function_version_info *before;
245 struct cgraph_function_version_info *after;
247 gcc_assert (decl1_node != NULL && decl2_node != NULL);
248 decl1_v = get_cgraph_node_version (decl1_node);
249 decl2_v = get_cgraph_node_version (decl2_node);
251 if (decl1_v != NULL && decl2_v != NULL)
252 return;
254 if (decl1_v == NULL)
255 decl1_v = insert_new_cgraph_node_version (decl1_node);
257 if (decl2_v == NULL)
258 decl2_v = insert_new_cgraph_node_version (decl2_node);
260 /* Chain decl2_v and decl1_v. All semantically identical versions
261 will be chained together. */
263 before = decl1_v;
264 after = decl2_v;
266 while (before->next != NULL)
267 before = before->next;
269 while (after->prev != NULL)
270 after= after->prev;
272 before->next = after;
273 after->prev = before;
276 /* Macros to access the next item in the list of free cgraph nodes and
277 edges. */
278 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->next)
279 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
280 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
282 /* Register HOOK to be called with DATA on each removed edge. */
283 struct cgraph_edge_hook_list *
284 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
286 struct cgraph_edge_hook_list *entry;
287 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
289 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
290 entry->hook = hook;
291 entry->data = data;
292 entry->next = NULL;
293 while (*ptr)
294 ptr = &(*ptr)->next;
295 *ptr = entry;
296 return entry;
299 /* Remove ENTRY from the list of hooks called on removing edges. */
300 void
301 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
303 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
305 while (*ptr != entry)
306 ptr = &(*ptr)->next;
307 *ptr = entry->next;
308 free (entry);
311 /* Call all edge removal hooks. */
312 static void
313 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
315 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
316 while (entry)
318 entry->hook (e, entry->data);
319 entry = entry->next;
323 /* Register HOOK to be called with DATA on each removed node. */
324 struct cgraph_node_hook_list *
325 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
327 struct cgraph_node_hook_list *entry;
328 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
330 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
331 entry->hook = hook;
332 entry->data = data;
333 entry->next = NULL;
334 while (*ptr)
335 ptr = &(*ptr)->next;
336 *ptr = entry;
337 return entry;
340 /* Remove ENTRY from the list of hooks called on removing nodes. */
341 void
342 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
344 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
346 while (*ptr != entry)
347 ptr = &(*ptr)->next;
348 *ptr = entry->next;
349 free (entry);
352 /* Call all node removal hooks. */
353 static void
354 cgraph_call_node_removal_hooks (struct cgraph_node *node)
356 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
357 while (entry)
359 entry->hook (node, entry->data);
360 entry = entry->next;
364 /* Register HOOK to be called with DATA on each inserted node. */
365 struct cgraph_node_hook_list *
366 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
368 struct cgraph_node_hook_list *entry;
369 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
371 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
372 entry->hook = hook;
373 entry->data = data;
374 entry->next = NULL;
375 while (*ptr)
376 ptr = &(*ptr)->next;
377 *ptr = entry;
378 return entry;
381 /* Remove ENTRY from the list of hooks called on inserted nodes. */
382 void
383 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
385 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
387 while (*ptr != entry)
388 ptr = &(*ptr)->next;
389 *ptr = entry->next;
390 free (entry);
393 /* Call all node insertion hooks. */
394 void
395 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
397 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
398 while (entry)
400 entry->hook (node, entry->data);
401 entry = entry->next;
405 /* Register HOOK to be called with DATA on each duplicated edge. */
406 struct cgraph_2edge_hook_list *
407 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
409 struct cgraph_2edge_hook_list *entry;
410 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
412 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
413 entry->hook = hook;
414 entry->data = data;
415 entry->next = NULL;
416 while (*ptr)
417 ptr = &(*ptr)->next;
418 *ptr = entry;
419 return entry;
422 /* Remove ENTRY from the list of hooks called on duplicating edges. */
423 void
424 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
426 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
428 while (*ptr != entry)
429 ptr = &(*ptr)->next;
430 *ptr = entry->next;
431 free (entry);
434 /* Call all edge duplication hooks. */
435 void
436 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
437 struct cgraph_edge *cs2)
439 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
440 while (entry)
442 entry->hook (cs1, cs2, entry->data);
443 entry = entry->next;
447 /* Register HOOK to be called with DATA on each duplicated node. */
448 struct cgraph_2node_hook_list *
449 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
451 struct cgraph_2node_hook_list *entry;
452 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
454 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
455 entry->hook = hook;
456 entry->data = data;
457 entry->next = NULL;
458 while (*ptr)
459 ptr = &(*ptr)->next;
460 *ptr = entry;
461 return entry;
464 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
465 void
466 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
468 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
470 while (*ptr != entry)
471 ptr = &(*ptr)->next;
472 *ptr = entry->next;
473 free (entry);
476 /* Call all node duplication hooks. */
477 void
478 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
479 struct cgraph_node *node2)
481 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
482 while (entry)
484 entry->hook (node1, node2, entry->data);
485 entry = entry->next;
489 /* Allocate new callgraph node. */
491 static inline struct cgraph_node *
492 cgraph_allocate_node (void)
494 struct cgraph_node *node;
496 if (free_nodes)
498 node = free_nodes;
499 free_nodes = NEXT_FREE_NODE (node);
501 else
503 node = ggc_alloc_cleared_cgraph_node ();
504 node->uid = cgraph_max_uid++;
507 return node;
510 /* Allocate new callgraph node and insert it into basic data structures. */
512 struct cgraph_node *
513 cgraph_create_empty_node (void)
515 struct cgraph_node *node = cgraph_allocate_node ();
517 node->type = SYMTAB_FUNCTION;
518 node->frequency = NODE_FREQUENCY_NORMAL;
519 node->count_materialization_scale = REG_BR_PROB_BASE;
520 cgraph_n_nodes++;
521 return node;
524 /* Return cgraph node assigned to DECL. Create new one when needed. */
526 struct cgraph_node *
527 cgraph_create_node (tree decl)
529 struct cgraph_node *node = cgraph_create_empty_node ();
530 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
532 node->decl = decl;
533 symtab_register_node (node);
535 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
537 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
538 node->next_nested = node->origin->nested;
539 node->origin->nested = node;
541 return node;
544 /* Try to find a call graph node for declaration DECL and if it does not exist
545 or if it corresponds to an inline clone, create a new one. */
547 struct cgraph_node *
548 cgraph_get_create_node (tree decl)
550 struct cgraph_node *first_clone = cgraph_get_node (decl);
552 if (first_clone && !first_clone->global.inlined_to)
553 return first_clone;
555 struct cgraph_node *node = cgraph_create_node (decl);
556 if (first_clone)
558 first_clone->clone_of = node;
559 node->clones = first_clone;
560 symtab_prevail_in_asm_name_hash (node);
561 symtab_insert_node_to_hashtable (node);
562 if (dump_file)
563 fprintf (dump_file, "Introduced new external node "
564 "(%s/%i) and turned into root of the clone tree.\n",
565 xstrdup (node->name ()), node->order);
567 else if (dump_file)
568 fprintf (dump_file, "Introduced new external node "
569 "(%s/%i).\n", xstrdup (node->name ()),
570 node->order);
571 return node;
574 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
575 the function body is associated with (not necessarily cgraph_node (DECL). */
577 struct cgraph_node *
578 cgraph_create_function_alias (tree alias, tree target)
580 struct cgraph_node *alias_node;
582 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
583 || TREE_CODE (target) == IDENTIFIER_NODE);
584 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
585 alias_node = cgraph_get_create_node (alias);
586 gcc_assert (!alias_node->definition);
587 alias_node->alias_target = target;
588 alias_node->definition = true;
589 alias_node->alias = true;
590 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
591 alias_node->weakref = true;
592 return alias_node;
595 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
596 and NULL otherwise.
597 Same body aliases are output whenever the body of DECL is output,
598 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
600 struct cgraph_node *
601 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
603 struct cgraph_node *n;
604 #ifndef ASM_OUTPUT_DEF
605 /* If aliases aren't supported by the assembler, fail. */
606 return NULL;
607 #endif
608 /* Langhooks can create same body aliases of symbols not defined.
609 Those are useless. Drop them on the floor. */
610 if (cgraph_global_info_ready)
611 return NULL;
613 n = cgraph_create_function_alias (alias, decl);
614 n->cpp_implicit_alias = true;
615 if (cpp_implicit_aliases_done)
616 symtab_resolve_alias (n,
617 cgraph_get_node (decl));
618 return n;
621 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
622 aliases DECL with an adjustments made into the first parameter.
623 See comments in thunk_adjust for detail on the parameters. */
625 struct cgraph_node *
626 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
627 tree alias, tree decl ATTRIBUTE_UNUSED,
628 bool this_adjusting,
629 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
630 tree virtual_offset,
631 tree real_alias)
633 struct cgraph_node *node;
635 node = cgraph_get_node (alias);
636 if (node)
638 gcc_assert (node->definition);
639 gcc_assert (!node->alias);
640 gcc_assert (!node->thunk.thunk_p);
641 cgraph_remove_node (node);
644 node = cgraph_create_node (alias);
645 gcc_checking_assert (!virtual_offset
646 || tree_to_double_int (virtual_offset) ==
647 double_int::from_shwi (virtual_value));
648 node->thunk.fixed_offset = fixed_offset;
649 node->thunk.this_adjusting = this_adjusting;
650 node->thunk.virtual_value = virtual_value;
651 node->thunk.virtual_offset_p = virtual_offset != NULL;
652 node->thunk.alias = real_alias;
653 node->thunk.thunk_p = true;
654 node->definition = true;
656 return node;
659 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
660 Return NULL if there's no such node. */
662 struct cgraph_node *
663 cgraph_node_for_asm (tree asmname)
665 /* We do not want to look at inline clones. */
666 for (symtab_node *node = symtab_node_for_asm (asmname);
667 node;
668 node = node->next_sharing_asm_name)
670 cgraph_node *cn = dyn_cast <cgraph_node> (node);
671 if (cn && !cn->global.inlined_to)
672 return cn;
674 return NULL;
677 /* Returns a hash value for X (which really is a cgraph_edge). */
679 static hashval_t
680 edge_hash (const void *x)
682 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
685 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
687 static int
688 edge_eq (const void *x, const void *y)
690 return ((const struct cgraph_edge *) x)->call_stmt == y;
693 /* Add call graph edge E to call site hash of its caller. */
695 static inline void
696 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
698 void **slot;
699 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
700 e->call_stmt,
701 htab_hash_pointer (e->call_stmt),
702 INSERT);
703 *slot = e;
706 /* Add call graph edge E to call site hash of its caller. */
708 static inline void
709 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
711 void **slot;
712 /* There are two speculative edges for every statement (one direct,
713 one indirect); always hash the direct one. */
714 if (e->speculative && e->indirect_unknown_callee)
715 return;
716 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
717 e->call_stmt,
718 htab_hash_pointer (e->call_stmt),
719 INSERT);
720 if (*slot)
722 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
723 if (e->callee)
724 *slot = e;
725 return;
727 gcc_assert (!*slot || e->speculative);
728 *slot = e;
731 /* Return the callgraph edge representing the GIMPLE_CALL statement
732 CALL_STMT. */
734 struct cgraph_edge *
735 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
737 struct cgraph_edge *e, *e2;
738 int n = 0;
740 if (node->call_site_hash)
741 return (struct cgraph_edge *)
742 htab_find_with_hash (node->call_site_hash, call_stmt,
743 htab_hash_pointer (call_stmt));
745 /* This loop may turn out to be performance problem. In such case adding
746 hashtables into call nodes with very many edges is probably best
747 solution. It is not good idea to add pointer into CALL_EXPR itself
748 because we want to make possible having multiple cgraph nodes representing
749 different clones of the same body before the body is actually cloned. */
750 for (e = node->callees; e; e = e->next_callee)
752 if (e->call_stmt == call_stmt)
753 break;
754 n++;
757 if (!e)
758 for (e = node->indirect_calls; e; e = e->next_callee)
760 if (e->call_stmt == call_stmt)
761 break;
762 n++;
765 if (n > 100)
767 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
768 for (e2 = node->callees; e2; e2 = e2->next_callee)
769 cgraph_add_edge_to_call_site_hash (e2);
770 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
771 cgraph_add_edge_to_call_site_hash (e2);
774 return e;
778 /* Change field call_stmt of edge E to NEW_STMT.
779 If UPDATE_SPECULATIVE and E is any component of speculative
780 edge, then update all components. */
782 void
783 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
784 bool update_speculative)
786 tree decl;
788 /* Speculative edges has three component, update all of them
789 when asked to. */
790 if (update_speculative && e->speculative)
792 struct cgraph_edge *direct, *indirect;
793 struct ipa_ref *ref;
795 cgraph_speculative_call_info (e, direct, indirect, ref);
796 cgraph_set_call_stmt (direct, new_stmt, false);
797 cgraph_set_call_stmt (indirect, new_stmt, false);
798 ref->stmt = new_stmt;
799 return;
802 /* Only direct speculative edges go to call_site_hash. */
803 if (e->caller->call_site_hash
804 && (!e->speculative || !e->indirect_unknown_callee))
806 htab_remove_elt_with_hash (e->caller->call_site_hash,
807 e->call_stmt,
808 htab_hash_pointer (e->call_stmt));
811 e->call_stmt = new_stmt;
812 if (e->indirect_unknown_callee
813 && (decl = gimple_call_fndecl (new_stmt)))
815 /* Constant propagation (and possibly also inlining?) can turn an
816 indirect call into a direct one. */
817 struct cgraph_node *new_callee = cgraph_get_node (decl);
819 gcc_checking_assert (new_callee);
820 e = cgraph_make_edge_direct (e, new_callee);
823 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
824 e->can_throw_external = stmt_can_throw_external (new_stmt);
825 pop_cfun ();
826 if (e->caller->call_site_hash)
827 cgraph_add_edge_to_call_site_hash (e);
830 /* Allocate a cgraph_edge structure and fill it with data according to the
831 parameters of which only CALLEE can be NULL (when creating an indirect call
832 edge). */
834 static struct cgraph_edge *
835 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
836 gimple call_stmt, gcov_type count, int freq,
837 bool indir_unknown_callee)
839 struct cgraph_edge *edge;
841 /* LTO does not actually have access to the call_stmt since these
842 have not been loaded yet. */
843 if (call_stmt)
845 /* This is a rather expensive check possibly triggering
846 construction of call stmt hashtable. */
847 #ifdef ENABLE_CHECKING
848 struct cgraph_edge *e;
849 gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
850 #endif
852 gcc_assert (is_gimple_call (call_stmt));
855 if (free_edges)
857 edge = free_edges;
858 free_edges = NEXT_FREE_EDGE (edge);
860 else
862 edge = ggc_alloc_cgraph_edge ();
863 edge->uid = cgraph_edge_max_uid++;
866 edge->aux = NULL;
867 edge->caller = caller;
868 edge->callee = callee;
869 edge->prev_caller = NULL;
870 edge->next_caller = NULL;
871 edge->prev_callee = NULL;
872 edge->next_callee = NULL;
873 edge->lto_stmt_uid = 0;
875 edge->count = count;
876 gcc_assert (count >= 0);
877 edge->frequency = freq;
878 gcc_assert (freq >= 0);
879 gcc_assert (freq <= CGRAPH_FREQ_MAX);
881 edge->call_stmt = call_stmt;
882 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
883 edge->can_throw_external
884 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
885 pop_cfun ();
886 if (call_stmt
887 && callee && callee->decl
888 && !gimple_check_call_matching_types (call_stmt, callee->decl,
889 false))
890 edge->call_stmt_cannot_inline_p = true;
891 else
892 edge->call_stmt_cannot_inline_p = false;
894 edge->indirect_info = NULL;
895 edge->indirect_inlining_edge = 0;
896 edge->speculative = false;
897 edge->indirect_unknown_callee = indir_unknown_callee;
898 if (call_stmt && caller->call_site_hash)
899 cgraph_add_edge_to_call_site_hash (edge);
901 return edge;
904 /* Create edge from CALLER to CALLEE in the cgraph. */
906 struct cgraph_edge *
907 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
908 gimple call_stmt, gcov_type count, int freq)
910 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
911 count, freq, false);
913 initialize_inline_failed (edge);
915 edge->next_caller = callee->callers;
916 if (callee->callers)
917 callee->callers->prev_caller = edge;
918 edge->next_callee = caller->callees;
919 if (caller->callees)
920 caller->callees->prev_callee = edge;
921 caller->callees = edge;
922 callee->callers = edge;
924 return edge;
927 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
929 struct cgraph_indirect_call_info *
930 cgraph_allocate_init_indirect_info (void)
932 struct cgraph_indirect_call_info *ii;
934 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
935 ii->param_index = -1;
936 return ii;
939 /* Create an indirect edge with a yet-undetermined callee where the call
940 statement destination is a formal parameter of the caller with index
941 PARAM_INDEX. */
943 struct cgraph_edge *
944 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
945 int ecf_flags,
946 gcov_type count, int freq)
948 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
949 count, freq, true);
950 tree target;
952 initialize_inline_failed (edge);
954 edge->indirect_info = cgraph_allocate_init_indirect_info ();
955 edge->indirect_info->ecf_flags = ecf_flags;
957 /* Record polymorphic call info. */
958 if (call_stmt
959 && (target = gimple_call_fn (call_stmt))
960 && virtual_method_call_p (target))
962 tree otr_type;
963 HOST_WIDE_INT otr_token;
964 ipa_polymorphic_call_context context;
966 get_polymorphic_call_info (caller->decl,
967 target,
968 &otr_type, &otr_token,
969 &context);
971 /* Only record types can have virtual calls. */
972 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
973 edge->indirect_info->polymorphic = true;
974 edge->indirect_info->param_index = -1;
975 edge->indirect_info->otr_token = otr_token;
976 edge->indirect_info->otr_type = otr_type;
977 edge->indirect_info->outer_type = context.outer_type;
978 edge->indirect_info->offset = context.offset;
979 edge->indirect_info->maybe_in_construction
980 = context.maybe_in_construction;
981 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
984 edge->next_callee = caller->indirect_calls;
985 if (caller->indirect_calls)
986 caller->indirect_calls->prev_callee = edge;
987 caller->indirect_calls = edge;
989 return edge;
992 /* Remove the edge E from the list of the callers of the callee. */
994 static inline void
995 cgraph_edge_remove_callee (struct cgraph_edge *e)
997 gcc_assert (!e->indirect_unknown_callee);
998 if (e->prev_caller)
999 e->prev_caller->next_caller = e->next_caller;
1000 if (e->next_caller)
1001 e->next_caller->prev_caller = e->prev_caller;
1002 if (!e->prev_caller)
1003 e->callee->callers = e->next_caller;
1006 /* Remove the edge E from the list of the callees of the caller. */
1008 static inline void
1009 cgraph_edge_remove_caller (struct cgraph_edge *e)
1011 if (e->prev_callee)
1012 e->prev_callee->next_callee = e->next_callee;
1013 if (e->next_callee)
1014 e->next_callee->prev_callee = e->prev_callee;
1015 if (!e->prev_callee)
1017 if (e->indirect_unknown_callee)
1018 e->caller->indirect_calls = e->next_callee;
1019 else
1020 e->caller->callees = e->next_callee;
1022 if (e->caller->call_site_hash)
1023 htab_remove_elt_with_hash (e->caller->call_site_hash,
1024 e->call_stmt,
1025 htab_hash_pointer (e->call_stmt));
1028 /* Put the edge onto the free list. */
1030 static void
1031 cgraph_free_edge (struct cgraph_edge *e)
1033 int uid = e->uid;
1035 if (e->indirect_info)
1036 ggc_free (e->indirect_info);
1038 /* Clear out the edge so we do not dangle pointers. */
1039 memset (e, 0, sizeof (*e));
1040 e->uid = uid;
1041 NEXT_FREE_EDGE (e) = free_edges;
1042 free_edges = e;
1045 /* Remove the edge E in the cgraph. */
1047 void
1048 cgraph_remove_edge (struct cgraph_edge *e)
1050 /* Call all edge removal hooks. */
1051 cgraph_call_edge_removal_hooks (e);
1053 if (!e->indirect_unknown_callee)
1054 /* Remove from callers list of the callee. */
1055 cgraph_edge_remove_callee (e);
1057 /* Remove from callees list of the callers. */
1058 cgraph_edge_remove_caller (e);
1060 /* Put the edge onto the free list. */
1061 cgraph_free_edge (e);
1064 /* Set callee of call graph edge E and add it to the corresponding set of
1065 callers. */
1067 static void
1068 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1070 e->prev_caller = NULL;
1071 if (n->callers)
1072 n->callers->prev_caller = e;
1073 e->next_caller = n->callers;
1074 n->callers = e;
1075 e->callee = n;
1078 /* Turn edge E into speculative call calling N2. Update
1079 the profile so the direct call is taken COUNT times
1080 with FREQUENCY.
1082 At clone materialization time, the indirect call E will
1083 be expanded as:
1085 if (call_dest == N2)
1086 n2 ();
1087 else
1088 call call_dest
1090 At this time the function just creates the direct call,
1091 the referencd representing the if conditional and attaches
1092 them all to the orginal indirect call statement.
1094 Return direct edge created. */
1096 struct cgraph_edge *
1097 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1098 struct cgraph_node *n2,
1099 gcov_type direct_count,
1100 int direct_frequency)
1102 struct cgraph_node *n = e->caller;
1103 struct ipa_ref *ref;
1104 struct cgraph_edge *e2;
1106 if (dump_file)
1108 fprintf (dump_file, "Indirect call -> speculative call"
1109 " %s/%i => %s/%i\n",
1110 xstrdup (n->name ()), n->order,
1111 xstrdup (n2->name ()), n2->order);
1113 e->speculative = true;
1114 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1115 initialize_inline_failed (e2);
1116 e2->speculative = true;
1117 if (TREE_NOTHROW (n2->decl))
1118 e2->can_throw_external = false;
1119 else
1120 e2->can_throw_external = e->can_throw_external;
1121 e2->lto_stmt_uid = e->lto_stmt_uid;
1122 e->count -= e2->count;
1123 e->frequency -= e2->frequency;
1124 cgraph_call_edge_duplication_hooks (e, e2);
1125 ref = ipa_record_reference (n, n2,
1126 IPA_REF_ADDR, e->call_stmt);
1127 ref->lto_stmt_uid = e->lto_stmt_uid;
1128 ref->speculative = e->speculative;
1129 cgraph_mark_address_taken_node (n2);
1130 return e2;
1133 /* Speculative call consist of three components:
1134 1) an indirect edge representing the original call
1135 2) an direct edge representing the new call
1136 3) ADDR_EXPR reference representing the speculative check.
1137 All three components are attached to single statement (the indirect
1138 call) and if one of them exists, all of them must exist.
1140 Given speculative call edge E, return all three components.
1143 void
1144 cgraph_speculative_call_info (struct cgraph_edge *e,
1145 struct cgraph_edge *&direct,
1146 struct cgraph_edge *&indirect,
1147 struct ipa_ref *&reference)
1149 struct ipa_ref *ref;
1150 int i;
1151 struct cgraph_edge *e2;
1153 if (!e->indirect_unknown_callee)
1154 for (e2 = e->caller->indirect_calls;
1155 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1156 e2 = e2->next_callee)
1158 else
1160 e2 = e;
1161 /* We can take advantage of the call stmt hash. */
1162 if (e2->call_stmt)
1164 e = cgraph_edge (e->caller, e2->call_stmt);
1165 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1167 else
1168 for (e = e->caller->callees;
1169 e2->call_stmt != e->call_stmt
1170 || e2->lto_stmt_uid != e->lto_stmt_uid;
1171 e = e->next_callee)
1174 gcc_assert (e->speculative && e2->speculative);
1175 direct = e;
1176 indirect = e2;
1178 reference = NULL;
1179 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->ref_list,
1180 i, ref); i++)
1181 if (ref->speculative
1182 && ((ref->stmt && ref->stmt == e->call_stmt)
1183 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1185 reference = ref;
1186 break;
1189 /* Speculative edge always consist of all three components - direct edge,
1190 indirect and reference. */
1192 gcc_assert (e && e2 && ref);
1195 /* Redirect callee of E to N. The function does not update underlying
1196 call expression. */
1198 void
1199 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1201 /* Remove from callers list of the current callee. */
1202 cgraph_edge_remove_callee (e);
1204 /* Insert to callers list of the new callee. */
1205 cgraph_set_edge_callee (e, n);
1208 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1209 Remove the speculative call sequence and return edge representing the call.
1210 It is up to caller to redirect the call as appropriate. */
1212 struct cgraph_edge *
1213 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1215 struct cgraph_edge *e2;
1216 struct ipa_ref *ref;
1218 gcc_assert (edge->speculative);
1219 cgraph_speculative_call_info (edge, e2, edge, ref);
1220 if (!callee_decl
1221 || !symtab_semantically_equivalent_p (ref->referred,
1222 symtab_get_node (callee_decl)))
1224 if (dump_file)
1226 if (callee_decl)
1228 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1229 "turned out to have contradicting known target ",
1230 xstrdup (edge->caller->name ()), edge->caller->order,
1231 xstrdup (e2->callee->name ()), e2->callee->order);
1232 print_generic_expr (dump_file, callee_decl, 0);
1233 fprintf (dump_file, "\n");
1235 else
1237 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1238 xstrdup (edge->caller->name ()), edge->caller->order,
1239 xstrdup (e2->callee->name ()), e2->callee->order);
1243 else
1245 struct cgraph_edge *tmp = edge;
1246 if (dump_file)
1247 fprintf (dump_file, "Speculative call turned into direct call.\n");
1248 edge = e2;
1249 e2 = tmp;
1250 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1251 in the functions inlined through it. */
1253 edge->count += e2->count;
1254 edge->frequency += e2->frequency;
1255 if (edge->frequency > CGRAPH_FREQ_MAX)
1256 edge->frequency = CGRAPH_FREQ_MAX;
1257 edge->speculative = false;
1258 e2->speculative = false;
1259 ipa_remove_reference (ref);
1260 if (e2->indirect_unknown_callee || e2->inline_failed)
1261 cgraph_remove_edge (e2);
1262 else
1263 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1264 if (edge->caller->call_site_hash)
1265 cgraph_update_edge_in_call_site_hash (edge);
1266 return edge;
1269 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1270 CALLEE. DELTA is an integer constant that is to be added to the this
1271 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1273 struct cgraph_edge *
1274 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1276 gcc_assert (edge->indirect_unknown_callee);
1278 /* If we are redirecting speculative call, make it non-speculative. */
1279 if (edge->indirect_unknown_callee && edge->speculative)
1281 edge = cgraph_resolve_speculation (edge, callee->decl);
1283 /* On successful speculation just return the pre existing direct edge. */
1284 if (!edge->indirect_unknown_callee)
1285 return edge;
1288 edge->indirect_unknown_callee = 0;
1289 ggc_free (edge->indirect_info);
1290 edge->indirect_info = NULL;
1292 /* Get the edge out of the indirect edge list. */
1293 if (edge->prev_callee)
1294 edge->prev_callee->next_callee = edge->next_callee;
1295 if (edge->next_callee)
1296 edge->next_callee->prev_callee = edge->prev_callee;
1297 if (!edge->prev_callee)
1298 edge->caller->indirect_calls = edge->next_callee;
1300 /* Put it into the normal callee list */
1301 edge->prev_callee = NULL;
1302 edge->next_callee = edge->caller->callees;
1303 if (edge->caller->callees)
1304 edge->caller->callees->prev_callee = edge;
1305 edge->caller->callees = edge;
1307 /* Insert to callers list of the new callee. */
1308 cgraph_set_edge_callee (edge, callee);
1310 if (edge->call_stmt)
1311 edge->call_stmt_cannot_inline_p
1312 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1313 false);
1315 /* We need to re-determine the inlining status of the edge. */
1316 initialize_inline_failed (edge);
1317 return edge;
1320 /* If necessary, change the function declaration in the call statement
1321 associated with E so that it corresponds to the edge callee. */
1323 gimple
1324 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1326 tree decl = gimple_call_fndecl (e->call_stmt);
1327 gimple new_stmt;
1328 gimple_stmt_iterator gsi;
1329 #ifdef ENABLE_CHECKING
1330 struct cgraph_node *node;
1331 #endif
1333 if (e->speculative)
1335 struct cgraph_edge *e2;
1336 gimple new_stmt;
1337 struct ipa_ref *ref;
1339 cgraph_speculative_call_info (e, e, e2, ref);
1340 /* If there already is an direct call (i.e. as a result of inliner's
1341 substitution), forget about speculating. */
1342 if (decl)
1343 e = cgraph_resolve_speculation (e, decl);
1344 /* If types do not match, speculation was likely wrong.
1345 The direct edge was posisbly redirected to the clone with a different
1346 signature. We did not update the call statement yet, so compare it
1347 with the reference that still points to the proper type. */
1348 else if (!gimple_check_call_matching_types (e->call_stmt,
1349 ref->referred->decl,
1350 true))
1352 if (dump_file)
1353 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1354 "Type mismatch.\n",
1355 xstrdup (e->caller->name ()),
1356 e->caller->order,
1357 xstrdup (e->callee->name ()),
1358 e->callee->order);
1359 e = cgraph_resolve_speculation (e, NULL);
1360 /* We are producing the final function body and will throw away the
1361 callgraph edges really soon. Reset the counts/frequencies to
1362 keep verifier happy in the case of roundoff errors. */
1363 e->count = gimple_bb (e->call_stmt)->count;
1364 e->frequency = compute_call_stmt_bb_frequency
1365 (e->caller->decl, gimple_bb (e->call_stmt));
1367 /* Expand speculation into GIMPLE code. */
1368 else
1370 if (dump_file)
1371 fprintf (dump_file,
1372 "Expanding speculative call of %s/%i -> %s/%i count:"
1373 HOST_WIDEST_INT_PRINT_DEC"\n",
1374 xstrdup (e->caller->name ()),
1375 e->caller->order,
1376 xstrdup (e->callee->name ()),
1377 e->callee->order,
1378 (HOST_WIDEST_INT)e->count);
1379 gcc_assert (e2->speculative);
1380 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1381 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1382 e->count || e2->count
1383 ? RDIV (e->count * REG_BR_PROB_BASE,
1384 e->count + e2->count)
1385 : e->frequency || e2->frequency
1386 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1387 e->frequency + e2->frequency)
1388 : REG_BR_PROB_BASE / 2,
1389 e->count, e->count + e2->count);
1390 e->speculative = false;
1391 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1392 new_stmt, false);
1393 e->frequency = compute_call_stmt_bb_frequency
1394 (e->caller->decl, gimple_bb (e->call_stmt));
1395 e2->frequency = compute_call_stmt_bb_frequency
1396 (e2->caller->decl, gimple_bb (e2->call_stmt));
1397 e2->speculative = false;
1398 ref->speculative = false;
1399 ref->stmt = NULL;
1400 /* Indirect edges are not both in the call site hash.
1401 get it updated. */
1402 if (e->caller->call_site_hash)
1403 cgraph_update_edge_in_call_site_hash (e2);
1404 pop_cfun ();
1405 /* Continue redirecting E to proper target. */
1409 if (e->indirect_unknown_callee
1410 || decl == e->callee->decl)
1411 return e->call_stmt;
1413 #ifdef ENABLE_CHECKING
1414 if (decl)
1416 node = cgraph_get_node (decl);
1417 gcc_assert (!node || !node->clone.combined_args_to_skip);
1419 #endif
1421 if (cgraph_dump_file)
1423 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1424 xstrdup (e->caller->name ()), e->caller->order,
1425 xstrdup (e->callee->name ()), e->callee->order);
1426 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1427 if (e->callee->clone.combined_args_to_skip)
1429 fprintf (cgraph_dump_file, " combined args to skip: ");
1430 dump_bitmap (cgraph_dump_file,
1431 e->callee->clone.combined_args_to_skip);
1435 if (e->callee->clone.combined_args_to_skip)
1437 int lp_nr;
1439 new_stmt
1440 = gimple_call_copy_skip_args (e->call_stmt,
1441 e->callee->clone.combined_args_to_skip);
1442 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1443 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1445 if (gimple_vdef (new_stmt)
1446 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1447 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1449 gsi = gsi_for_stmt (e->call_stmt);
1450 gsi_replace (&gsi, new_stmt, false);
1451 /* We need to defer cleaning EH info on the new statement to
1452 fixup-cfg. We may not have dominator information at this point
1453 and thus would end up with unreachable blocks and have no way
1454 to communicate that we need to run CFG cleanup then. */
1455 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1456 if (lp_nr != 0)
1458 remove_stmt_from_eh_lp (e->call_stmt);
1459 add_stmt_to_eh_lp (new_stmt, lp_nr);
1462 else
1464 new_stmt = e->call_stmt;
1465 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1466 update_stmt (new_stmt);
1469 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1471 if (cgraph_dump_file)
1473 fprintf (cgraph_dump_file, " updated to:");
1474 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1476 return new_stmt;
1479 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1480 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1481 of OLD_STMT if it was previously call statement.
1482 If NEW_STMT is NULL, the call has been dropped without any
1483 replacement. */
1485 static void
1486 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1487 gimple old_stmt, tree old_call,
1488 gimple new_stmt)
1490 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1491 ? gimple_call_fndecl (new_stmt) : 0;
1493 /* We are seeing indirect calls, then there is nothing to update. */
1494 if (!new_call && !old_call)
1495 return;
1496 /* See if we turned indirect call into direct call or folded call to one builtin
1497 into different builtin. */
1498 if (old_call != new_call)
1500 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1501 struct cgraph_edge *ne = NULL;
1502 gcov_type count;
1503 int frequency;
1505 if (e)
1507 /* See if the edge is already there and has the correct callee. It
1508 might be so because of indirect inlining has already updated
1509 it. We also might've cloned and redirected the edge. */
1510 if (new_call && e->callee)
1512 struct cgraph_node *callee = e->callee;
1513 while (callee)
1515 if (callee->decl == new_call
1516 || callee->former_clone_of == new_call)
1517 return;
1518 callee = callee->clone_of;
1522 /* Otherwise remove edge and create new one; we can't simply redirect
1523 since function has changed, so inline plan and other information
1524 attached to edge is invalid. */
1525 count = e->count;
1526 frequency = e->frequency;
1527 cgraph_remove_edge (e);
1529 else if (new_call)
1531 /* We are seeing new direct call; compute profile info based on BB. */
1532 basic_block bb = gimple_bb (new_stmt);
1533 count = bb->count;
1534 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1535 bb);
1538 if (new_call)
1540 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1541 new_stmt, count, frequency);
1542 gcc_assert (ne->inline_failed);
1545 /* We only updated the call stmt; update pointer in cgraph edge.. */
1546 else if (old_stmt != new_stmt)
1547 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1550 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1551 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1552 of OLD_STMT before it was updated (updating can happen inplace). */
1554 void
1555 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1557 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1558 struct cgraph_node *node;
1560 gcc_checking_assert (orig);
1561 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1562 if (orig->clones)
1563 for (node = orig->clones; node != orig;)
1565 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1566 if (node->clones)
1567 node = node->clones;
1568 else if (node->next_sibling_clone)
1569 node = node->next_sibling_clone;
1570 else
1572 while (node != orig && !node->next_sibling_clone)
1573 node = node->clone_of;
1574 if (node != orig)
1575 node = node->next_sibling_clone;
1581 /* Remove all callees from the node. */
1583 void
1584 cgraph_node_remove_callees (struct cgraph_node *node)
1586 struct cgraph_edge *e, *f;
1588 /* It is sufficient to remove the edges from the lists of callers of
1589 the callees. The callee list of the node can be zapped with one
1590 assignment. */
1591 for (e = node->callees; e; e = f)
1593 f = e->next_callee;
1594 cgraph_call_edge_removal_hooks (e);
1595 if (!e->indirect_unknown_callee)
1596 cgraph_edge_remove_callee (e);
1597 cgraph_free_edge (e);
1599 for (e = node->indirect_calls; e; e = f)
1601 f = e->next_callee;
1602 cgraph_call_edge_removal_hooks (e);
1603 if (!e->indirect_unknown_callee)
1604 cgraph_edge_remove_callee (e);
1605 cgraph_free_edge (e);
1607 node->indirect_calls = NULL;
1608 node->callees = NULL;
1609 if (node->call_site_hash)
1611 htab_delete (node->call_site_hash);
1612 node->call_site_hash = NULL;
1616 /* Remove all callers from the node. */
1618 static void
1619 cgraph_node_remove_callers (struct cgraph_node *node)
1621 struct cgraph_edge *e, *f;
1623 /* It is sufficient to remove the edges from the lists of callees of
1624 the callers. The caller list of the node can be zapped with one
1625 assignment. */
1626 for (e = node->callers; e; e = f)
1628 f = e->next_caller;
1629 cgraph_call_edge_removal_hooks (e);
1630 cgraph_edge_remove_caller (e);
1631 cgraph_free_edge (e);
1633 node->callers = NULL;
1636 /* Helper function for cgraph_release_function_body and free_lang_data.
1637 It releases body from function DECL without having to inspect its
1638 possibly non-existent symtab node. */
1640 void
1641 release_function_body (tree decl)
1643 if (DECL_STRUCT_FUNCTION (decl))
1645 push_cfun (DECL_STRUCT_FUNCTION (decl));
1646 if (cfun->cfg
1647 && current_loops)
1649 cfun->curr_properties &= ~PROP_loops;
1650 loop_optimizer_finalize ();
1652 if (cfun->gimple_df)
1654 delete_tree_ssa ();
1655 delete_tree_cfg_annotations ();
1656 cfun->eh = NULL;
1658 if (cfun->cfg)
1660 gcc_assert (dom_computed[0] == DOM_NONE);
1661 gcc_assert (dom_computed[1] == DOM_NONE);
1662 clear_edges ();
1663 cfun->cfg = NULL;
1665 if (cfun->value_histograms)
1666 free_histograms ();
1667 pop_cfun ();
1668 gimple_set_body (decl, NULL);
1669 /* Struct function hangs a lot of data that would leak if we didn't
1670 removed all pointers to it. */
1671 ggc_free (DECL_STRUCT_FUNCTION (decl));
1672 DECL_STRUCT_FUNCTION (decl) = NULL;
1674 DECL_SAVED_TREE (decl) = NULL;
1677 /* Release memory used to represent body of function NODE.
1678 Use this only for functions that are released before being translated to
1679 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1680 are free'd in final.c via free_after_compilation(). */
1682 void
1683 cgraph_release_function_body (struct cgraph_node *node)
1685 node->ipa_transforms_to_apply.release ();
1686 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1688 DECL_RESULT (node->decl) = NULL;
1689 DECL_ARGUMENTS (node->decl) = NULL;
1691 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1692 of its associated function function declaration because it's
1693 needed to emit debug info later. */
1694 if (!node->used_as_abstract_origin && DECL_INITIAL (node->decl))
1695 DECL_INITIAL (node->decl) = error_mark_node;
1696 release_function_body (node->decl);
1697 if (node->lto_file_data)
1698 lto_free_function_in_decl_state_for_node (node);
1701 /* Remove the node from cgraph. */
1703 void
1704 cgraph_remove_node (struct cgraph_node *node)
1706 struct cgraph_node *n;
1707 int uid = node->uid;
1709 cgraph_call_node_removal_hooks (node);
1710 cgraph_node_remove_callers (node);
1711 cgraph_node_remove_callees (node);
1712 node->ipa_transforms_to_apply.release ();
1714 /* Incremental inlining access removed nodes stored in the postorder list.
1716 node->force_output = false;
1717 node->forced_by_abi = false;
1718 for (n = node->nested; n; n = n->next_nested)
1719 n->origin = NULL;
1720 node->nested = NULL;
1721 if (node->origin)
1723 struct cgraph_node **node2 = &node->origin->nested;
1725 while (*node2 != node)
1726 node2 = &(*node2)->next_nested;
1727 *node2 = node->next_nested;
1729 symtab_unregister_node (node);
1730 if (node->prev_sibling_clone)
1731 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1732 else if (node->clone_of)
1733 node->clone_of->clones = node->next_sibling_clone;
1734 if (node->next_sibling_clone)
1735 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1736 if (node->clones)
1738 struct cgraph_node *n, *next;
1740 if (node->clone_of)
1742 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1743 n->clone_of = node->clone_of;
1744 n->clone_of = node->clone_of;
1745 n->next_sibling_clone = node->clone_of->clones;
1746 if (node->clone_of->clones)
1747 node->clone_of->clones->prev_sibling_clone = n;
1748 node->clone_of->clones = node->clones;
1750 else
1752 /* We are removing node with clones. This makes clones inconsistent,
1753 but assume they will be removed subsequently and just keep clone
1754 tree intact. This can happen in unreachable function removal since
1755 we remove unreachable functions in random order, not by bottom-up
1756 walk of clone trees. */
1757 for (n = node->clones; n; n = next)
1759 next = n->next_sibling_clone;
1760 n->next_sibling_clone = NULL;
1761 n->prev_sibling_clone = NULL;
1762 n->clone_of = NULL;
1767 /* While all the clones are removed after being proceeded, the function
1768 itself is kept in the cgraph even after it is compiled. Check whether
1769 we are done with this body and reclaim it proactively if this is the case.
1771 if (cgraph_state != CGRAPH_LTO_STREAMING)
1773 n = cgraph_get_node (node->decl);
1774 if (!n
1775 || (!n->clones && !n->clone_of && !n->global.inlined_to
1776 && (cgraph_global_info_ready
1777 && (TREE_ASM_WRITTEN (n->decl)
1778 || DECL_EXTERNAL (n->decl)
1779 || !n->analyzed
1780 || (!flag_wpa && n->in_other_partition)))))
1781 cgraph_release_function_body (node);
1784 node->decl = NULL;
1785 if (node->call_site_hash)
1787 htab_delete (node->call_site_hash);
1788 node->call_site_hash = NULL;
1790 cgraph_n_nodes--;
1792 /* Clear out the node to NULL all pointers and add the node to the free
1793 list. */
1794 memset (node, 0, sizeof (*node));
1795 node->type = SYMTAB_FUNCTION;
1796 node->uid = uid;
1797 SET_NEXT_FREE_NODE (node, free_nodes);
1798 free_nodes = node;
1801 /* Likewise indicate that a node is having address taken. */
1803 void
1804 cgraph_mark_address_taken_node (struct cgraph_node *node)
1806 /* Indirect inlining can figure out that all uses of the address are
1807 inlined. */
1808 if (node->global.inlined_to)
1810 gcc_assert (cfun->after_inlining);
1811 gcc_assert (node->callers->indirect_inlining_edge);
1812 return;
1814 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1815 IPA_REF_ADDR reference exists (and thus it should be set on node
1816 representing alias we take address of) and as a test whether address
1817 of the object was taken (and thus it should be set on node alias is
1818 referring to). We should remove the first use and the remove the
1819 following set. */
1820 node->address_taken = 1;
1821 node = cgraph_function_or_thunk_node (node, NULL);
1822 node->address_taken = 1;
1825 /* Return local info for the compiled function. */
1827 struct cgraph_local_info *
1828 cgraph_local_info (tree decl)
1830 struct cgraph_node *node;
1832 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1833 node = cgraph_get_node (decl);
1834 if (!node)
1835 return NULL;
1836 return &node->local;
1839 /* Return local info for the compiled function. */
1841 struct cgraph_global_info *
1842 cgraph_global_info (tree decl)
1844 struct cgraph_node *node;
1846 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1847 node = cgraph_get_node (decl);
1848 if (!node)
1849 return NULL;
1850 return &node->global;
1853 /* Return local info for the compiled function. */
1855 struct cgraph_rtl_info *
1856 cgraph_rtl_info (tree decl)
1858 struct cgraph_node *node;
1860 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1861 node = cgraph_get_node (decl);
1862 if (!node
1863 || (decl != current_function_decl
1864 && !TREE_ASM_WRITTEN (node->decl)))
1865 return NULL;
1866 return &node->rtl;
1869 /* Return a string describing the failure REASON. */
1871 const char*
1872 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1874 #undef DEFCIFCODE
1875 #define DEFCIFCODE(code, string) string,
1877 static const char *cif_string_table[CIF_N_REASONS] = {
1878 #include "cif-code.def"
1881 /* Signedness of an enum type is implementation defined, so cast it
1882 to unsigned before testing. */
1883 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1884 return cif_string_table[reason];
1887 /* Names used to print out the availability enum. */
1888 const char * const cgraph_availability_names[] =
1889 {"unset", "not_available", "overwritable", "available", "local"};
1892 /* Dump call graph node NODE to file F. */
1894 void
1895 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1897 struct cgraph_edge *edge;
1898 int indirect_calls_count = 0;
1900 dump_symtab_base (f, node);
1902 if (node->global.inlined_to)
1903 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1904 xstrdup (node->name ()),
1905 node->order,
1906 xstrdup (node->global.inlined_to->name ()),
1907 node->global.inlined_to->order);
1908 if (node->clone_of)
1909 fprintf (f, " Clone of %s/%i\n",
1910 node->clone_of->asm_name (),
1911 node->clone_of->order);
1912 if (cgraph_function_flags_ready)
1913 fprintf (f, " Availability: %s\n",
1914 cgraph_availability_names [cgraph_function_body_availability (node)]);
1916 if (node->profile_id)
1917 fprintf (f, " Profile id: %i\n",
1918 node->profile_id);
1919 fprintf (f, " First run: %i\n", node->tp_first_run);
1920 fprintf (f, " Function flags:");
1921 if (node->count)
1922 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1923 (HOST_WIDEST_INT)node->count);
1924 if (node->origin)
1925 fprintf (f, " nested in: %s", node->origin->asm_name ());
1926 if (gimple_has_body_p (node->decl))
1927 fprintf (f, " body");
1928 if (node->process)
1929 fprintf (f, " process");
1930 if (node->local.local)
1931 fprintf (f, " local");
1932 if (node->local.redefined_extern_inline)
1933 fprintf (f, " redefined_extern_inline");
1934 if (node->only_called_at_startup)
1935 fprintf (f, " only_called_at_startup");
1936 if (node->only_called_at_exit)
1937 fprintf (f, " only_called_at_exit");
1938 if (node->tm_clone)
1939 fprintf (f, " tm_clone");
1941 fprintf (f, "\n");
1943 if (node->thunk.thunk_p)
1945 fprintf (f, " Thunk");
1946 if (node->thunk.alias)
1947 fprintf (f, " of %s (asm: %s)",
1948 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1949 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1950 fprintf (f, " fixed offset %i virtual value %i has "
1951 "virtual offset %i)\n",
1952 (int)node->thunk.fixed_offset,
1953 (int)node->thunk.virtual_value,
1954 (int)node->thunk.virtual_offset_p);
1956 if (node->alias && node->thunk.alias
1957 && DECL_P (node->thunk.alias))
1959 fprintf (f, " Alias of %s",
1960 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1961 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1962 fprintf (f, " (asm: %s)",
1963 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1964 fprintf (f, "\n");
1967 fprintf (f, " Called by: ");
1969 for (edge = node->callers; edge; edge = edge->next_caller)
1971 fprintf (f, "%s/%i ", edge->caller->asm_name (),
1972 edge->caller->order);
1973 if (edge->count)
1974 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1975 (HOST_WIDEST_INT)edge->count);
1976 if (edge->frequency)
1977 fprintf (f, "(%.2f per call) ",
1978 edge->frequency / (double)CGRAPH_FREQ_BASE);
1979 if (edge->speculative)
1980 fprintf (f, "(speculative) ");
1981 if (!edge->inline_failed)
1982 fprintf (f, "(inlined) ");
1983 if (edge->indirect_inlining_edge)
1984 fprintf (f, "(indirect_inlining) ");
1985 if (edge->can_throw_external)
1986 fprintf (f, "(can throw external) ");
1989 fprintf (f, "\n Calls: ");
1990 for (edge = node->callees; edge; edge = edge->next_callee)
1992 fprintf (f, "%s/%i ", edge->callee->asm_name (),
1993 edge->callee->order);
1994 if (edge->speculative)
1995 fprintf (f, "(speculative) ");
1996 if (!edge->inline_failed)
1997 fprintf (f, "(inlined) ");
1998 if (edge->indirect_inlining_edge)
1999 fprintf (f, "(indirect_inlining) ");
2000 if (edge->count)
2001 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
2002 (HOST_WIDEST_INT)edge->count);
2003 if (edge->frequency)
2004 fprintf (f, "(%.2f per call) ",
2005 edge->frequency / (double)CGRAPH_FREQ_BASE);
2006 if (edge->can_throw_external)
2007 fprintf (f, "(can throw external) ");
2009 fprintf (f, "\n");
2011 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2012 indirect_calls_count++;
2013 if (indirect_calls_count)
2014 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2015 indirect_calls_count);
2019 /* Dump call graph node NODE to stderr. */
2021 DEBUG_FUNCTION void
2022 debug_cgraph_node (struct cgraph_node *node)
2024 dump_cgraph_node (stderr, node);
2028 /* Dump the callgraph to file F. */
2030 void
2031 dump_cgraph (FILE *f)
2033 struct cgraph_node *node;
2035 fprintf (f, "callgraph:\n\n");
2036 FOR_EACH_FUNCTION (node)
2037 dump_cgraph_node (f, node);
2041 /* Dump the call graph to stderr. */
2043 DEBUG_FUNCTION void
2044 debug_cgraph (void)
2046 dump_cgraph (stderr);
2049 /* Return true when the DECL can possibly be inlined. */
2050 bool
2051 cgraph_function_possibly_inlined_p (tree decl)
2053 if (!cgraph_global_info_ready)
2054 return !DECL_UNINLINABLE (decl);
2055 return DECL_POSSIBLY_INLINED (decl);
2058 /* NODE is no longer nested function; update cgraph accordingly. */
2059 void
2060 cgraph_unnest_node (struct cgraph_node *node)
2062 struct cgraph_node **node2 = &node->origin->nested;
2063 gcc_assert (node->origin);
2065 while (*node2 != node)
2066 node2 = &(*node2)->next_nested;
2067 *node2 = node->next_nested;
2068 node->origin = NULL;
2071 /* Return function availability. See cgraph.h for description of individual
2072 return values. */
2073 enum availability
2074 cgraph_function_body_availability (struct cgraph_node *node)
2076 enum availability avail;
2077 if (!node->analyzed)
2078 avail = AVAIL_NOT_AVAILABLE;
2079 else if (node->local.local)
2080 avail = AVAIL_LOCAL;
2081 else if (node->alias && node->weakref)
2082 cgraph_function_or_thunk_node (node, &avail);
2083 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->decl)))
2084 avail = AVAIL_OVERWRITABLE;
2085 else if (!node->externally_visible)
2086 avail = AVAIL_AVAILABLE;
2087 /* Inline functions are safe to be analyzed even if their symbol can
2088 be overwritten at runtime. It is not meaningful to enforce any sane
2089 behaviour on replacing inline function by different body. */
2090 else if (DECL_DECLARED_INLINE_P (node->decl))
2091 avail = AVAIL_AVAILABLE;
2093 /* If the function can be overwritten, return OVERWRITABLE. Take
2094 care at least of two notable extensions - the COMDAT functions
2095 used to share template instantiations in C++ (this is symmetric
2096 to code cp_cannot_inline_tree_fn and probably shall be shared and
2097 the inlinability hooks completely eliminated).
2099 ??? Does the C++ one definition rule allow us to always return
2100 AVAIL_AVAILABLE here? That would be good reason to preserve this
2101 bit. */
2103 else if (decl_replaceable_p (node->decl)
2104 && !DECL_EXTERNAL (node->decl))
2105 avail = AVAIL_OVERWRITABLE;
2106 else avail = AVAIL_AVAILABLE;
2108 return avail;
2111 /* Worker for cgraph_node_can_be_local_p. */
2112 static bool
2113 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2114 void *data ATTRIBUTE_UNUSED)
2116 return !(!node->force_output
2117 && ((DECL_COMDAT (node->decl)
2118 && !node->forced_by_abi
2119 && !symtab_used_from_object_file_p (node)
2120 && !node->same_comdat_group)
2121 || !node->externally_visible));
2124 /* Return true if NODE can be made local for API change.
2125 Extern inline functions and C++ COMDAT functions can be made local
2126 at the expense of possible code size growth if function is used in multiple
2127 compilation units. */
2128 bool
2129 cgraph_node_can_be_local_p (struct cgraph_node *node)
2131 return (!node->address_taken
2132 && !cgraph_for_node_and_aliases (node,
2133 cgraph_node_cannot_be_local_p_1,
2134 NULL, true));
2137 /* Call calback on NODE, thunks and aliases associated to NODE.
2138 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2139 skipped. */
2141 bool
2142 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2143 bool (*callback) (struct cgraph_node *, void *),
2144 void *data,
2145 bool include_overwritable)
2147 struct cgraph_edge *e;
2148 int i;
2149 struct ipa_ref *ref;
2151 if (callback (node, data))
2152 return true;
2153 for (e = node->callers; e; e = e->next_caller)
2154 if (e->caller->thunk.thunk_p
2155 && (include_overwritable
2156 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2157 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2158 include_overwritable))
2159 return true;
2160 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2161 if (ref->use == IPA_REF_ALIAS)
2163 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2164 if (include_overwritable
2165 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2166 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2167 include_overwritable))
2168 return true;
2170 return false;
2173 /* Call calback on NODE and aliases associated to NODE.
2174 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2175 skipped. */
2177 bool
2178 cgraph_for_node_and_aliases (struct cgraph_node *node,
2179 bool (*callback) (struct cgraph_node *, void *),
2180 void *data,
2181 bool include_overwritable)
2183 int i;
2184 struct ipa_ref *ref;
2186 if (callback (node, data))
2187 return true;
2188 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2189 if (ref->use == IPA_REF_ALIAS)
2191 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2192 if (include_overwritable
2193 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2194 if (cgraph_for_node_and_aliases (alias, callback, data,
2195 include_overwritable))
2196 return true;
2198 return false;
2201 /* Worker to bring NODE local. */
2203 static bool
2204 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2206 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2207 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2209 symtab_make_decl_local (node->decl);
2211 node->externally_visible = false;
2212 node->forced_by_abi = false;
2213 node->local.local = true;
2214 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2215 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2216 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2217 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2219 return false;
2222 /* Bring NODE local. */
2224 void
2225 cgraph_make_node_local (struct cgraph_node *node)
2227 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2228 NULL, true);
2231 /* Worker to set nothrow flag. */
2233 static bool
2234 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2236 struct cgraph_edge *e;
2238 TREE_NOTHROW (node->decl) = data != NULL;
2240 if (data != NULL)
2241 for (e = node->callers; e; e = e->next_caller)
2242 e->can_throw_external = false;
2243 return false;
2246 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2247 if any to NOTHROW. */
2249 void
2250 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2252 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2253 (void *)(size_t)nothrow, false);
2256 /* Worker to set const flag. */
2258 static bool
2259 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2261 /* Static constructors and destructors without a side effect can be
2262 optimized out. */
2263 if (data && !((size_t)data & 2))
2265 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2266 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2267 if (DECL_STATIC_DESTRUCTOR (node->decl))
2268 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2270 TREE_READONLY (node->decl) = data != NULL;
2271 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2272 return false;
2275 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2276 if any to READONLY. */
2278 void
2279 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2281 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2282 (void *)(size_t)(readonly + (int)looping * 2),
2283 false);
2286 /* Worker to set pure flag. */
2288 static bool
2289 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2291 /* Static constructors and destructors without a side effect can be
2292 optimized out. */
2293 if (data && !((size_t)data & 2))
2295 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2296 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2297 if (DECL_STATIC_DESTRUCTOR (node->decl))
2298 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2300 DECL_PURE_P (node->decl) = data != NULL;
2301 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2302 return false;
2305 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2306 if any to PURE. */
2308 void
2309 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2311 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2312 (void *)(size_t)(pure + (int)looping * 2),
2313 false);
2316 /* Return true when NODE can not return or throw and thus
2317 it is safe to ignore its side effects for IPA analysis. */
2319 bool
2320 cgraph_node_cannot_return (struct cgraph_node *node)
2322 int flags = flags_from_decl_or_type (node->decl);
2323 if (!flag_exceptions)
2324 return (flags & ECF_NORETURN) != 0;
2325 else
2326 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2327 == (ECF_NORETURN | ECF_NOTHROW));
2330 /* Return true when call of E can not lead to return from caller
2331 and thus it is safe to ignore its side effects for IPA analysis
2332 when computing side effects of the caller.
2333 FIXME: We could actually mark all edges that have no reaching
2334 patch to EXIT_BLOCK_PTR or throw to get better results. */
2335 bool
2336 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2338 if (cgraph_node_cannot_return (e->caller))
2339 return true;
2340 if (e->indirect_unknown_callee)
2342 int flags = e->indirect_info->ecf_flags;
2343 if (!flag_exceptions)
2344 return (flags & ECF_NORETURN) != 0;
2345 else
2346 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2347 == (ECF_NORETURN | ECF_NOTHROW));
2349 else
2350 return cgraph_node_cannot_return (e->callee);
2353 /* Return true when function NODE can be removed from callgraph
2354 if all direct calls are eliminated. */
2356 bool
2357 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2359 gcc_assert (!node->global.inlined_to);
2360 /* Extern inlines can always go, we will use the external definition. */
2361 if (DECL_EXTERNAL (node->decl))
2362 return true;
2363 /* When function is needed, we can not remove it. */
2364 if (node->force_output || node->used_from_other_partition)
2365 return false;
2366 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2367 || DECL_STATIC_DESTRUCTOR (node->decl))
2368 return false;
2369 /* Only COMDAT functions can be removed if externally visible. */
2370 if (node->externally_visible
2371 && (!DECL_COMDAT (node->decl)
2372 || node->forced_by_abi
2373 || symtab_used_from_object_file_p (node)))
2374 return false;
2375 return true;
2378 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2380 static bool
2381 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2383 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2386 /* Return true when function NODE and its aliases can be removed from callgraph
2387 if all direct calls are eliminated. */
2389 bool
2390 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2392 /* Extern inlines can always go, we will use the external definition. */
2393 if (DECL_EXTERNAL (node->decl))
2394 return true;
2395 if (node->address_taken)
2396 return false;
2397 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2400 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2402 static bool
2403 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2405 return symtab_used_from_object_file_p (node);
2408 /* Return true when function NODE can be expected to be removed
2409 from program when direct calls in this compilation unit are removed.
2411 As a special case COMDAT functions are
2412 cgraph_can_remove_if_no_direct_calls_p while the are not
2413 cgraph_only_called_directly_p (it is possible they are called from other
2414 unit)
2416 This function behaves as cgraph_only_called_directly_p because eliminating
2417 all uses of COMDAT function does not make it necessarily disappear from
2418 the program unless we are compiling whole program or we do LTO. In this
2419 case we know we win since dynamic linking will not really discard the
2420 linkonce section. */
2422 bool
2423 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2425 gcc_assert (!node->global.inlined_to);
2426 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2427 return false;
2428 if (!in_lto_p && !flag_whole_program)
2429 return cgraph_only_called_directly_p (node);
2430 else
2432 if (DECL_EXTERNAL (node->decl))
2433 return true;
2434 return cgraph_can_remove_if_no_direct_calls_p (node);
2439 /* Worker for cgraph_only_called_directly_p. */
2441 static bool
2442 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2444 return !cgraph_only_called_directly_or_aliased_p (node);
2447 /* Return true when function NODE and all its aliases are only called
2448 directly.
2449 i.e. it is not externally visible, address was not taken and
2450 it is not used in any other non-standard way. */
2452 bool
2453 cgraph_only_called_directly_p (struct cgraph_node *node)
2455 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2456 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2457 NULL, true);
2461 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2463 static bool
2464 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2466 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2467 struct cgraph_edge *cs;
2468 enum availability avail;
2469 cgraph_function_or_thunk_node (node, &avail);
2471 if (avail > AVAIL_OVERWRITABLE)
2472 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2473 if (!cs->indirect_inlining_edge)
2474 redirect_callers->safe_push (cs);
2475 return false;
2478 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2479 (i.e. are not overwritable). */
2481 vec<cgraph_edge_p>
2482 collect_callers_of_node (struct cgraph_node *node)
2484 vec<cgraph_edge_p> redirect_callers = vNULL;
2485 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2486 &redirect_callers, false);
2487 return redirect_callers;
2490 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2491 static bool
2492 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2494 node = cgraph_function_or_thunk_node (node, NULL);
2495 node2 = cgraph_function_or_thunk_node (node2, NULL);
2496 while (node != node2 && node2)
2497 node2 = node2->clone_of;
2498 return node2 != NULL;
2501 /* Verify edge E count and frequency. */
2503 static bool
2504 verify_edge_count_and_frequency (struct cgraph_edge *e)
2506 bool error_found = false;
2507 if (e->count < 0)
2509 error ("caller edge count is negative");
2510 error_found = true;
2512 if (e->frequency < 0)
2514 error ("caller edge frequency is negative");
2515 error_found = true;
2517 if (e->frequency > CGRAPH_FREQ_MAX)
2519 error ("caller edge frequency is too large");
2520 error_found = true;
2522 if (gimple_has_body_p (e->caller->decl)
2523 && !e->caller->global.inlined_to
2524 && !e->speculative
2525 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2526 Remove this once edges are actually removed from the function at that time. */
2527 && (e->frequency
2528 || (inline_edge_summary_vec.exists ()
2529 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2530 || !inline_edge_summary (e)->predicate)))
2531 && (e->frequency
2532 != compute_call_stmt_bb_frequency (e->caller->decl,
2533 gimple_bb (e->call_stmt))))
2535 error ("caller edge frequency %i does not match BB frequency %i",
2536 e->frequency,
2537 compute_call_stmt_bb_frequency (e->caller->decl,
2538 gimple_bb (e->call_stmt)));
2539 error_found = true;
2541 return error_found;
2544 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2545 static void
2546 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2548 bool fndecl_was_null = false;
2549 /* debug_gimple_stmt needs correct cfun */
2550 if (cfun != this_cfun)
2551 set_cfun (this_cfun);
2552 /* ...and an actual current_function_decl */
2553 if (!current_function_decl)
2555 current_function_decl = this_cfun->decl;
2556 fndecl_was_null = true;
2558 debug_gimple_stmt (stmt);
2559 if (fndecl_was_null)
2560 current_function_decl = NULL;
2563 /* Verify that call graph edge E corresponds to DECL from the associated
2564 statement. Return true if the verification should fail. */
2566 static bool
2567 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2569 struct cgraph_node *node;
2571 if (!decl || e->callee->global.inlined_to)
2572 return false;
2573 if (cgraph_state == CGRAPH_LTO_STREAMING)
2574 return false;
2575 node = cgraph_get_node (decl);
2577 /* We do not know if a node from a different partition is an alias or what it
2578 aliases and therefore cannot do the former_clone_of check reliably. */
2579 if (!node || node->in_other_partition || e->callee->in_other_partition)
2580 return false;
2581 node = cgraph_function_or_thunk_node (node, NULL);
2583 if (e->callee->former_clone_of != node->decl
2584 /* IPA-CP sometimes redirect edge to clone and then back to the former
2585 function. This ping-pong has to go, eventually. */
2586 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2587 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2588 return true;
2589 else
2590 return false;
2593 /* Verify cgraph nodes of given cgraph node. */
2594 DEBUG_FUNCTION void
2595 verify_cgraph_node (struct cgraph_node *node)
2597 struct cgraph_edge *e;
2598 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
2599 basic_block this_block;
2600 gimple_stmt_iterator gsi;
2601 bool error_found = false;
2603 if (seen_error ())
2604 return;
2606 timevar_push (TV_CGRAPH_VERIFY);
2607 error_found |= verify_symtab_base (node);
2608 for (e = node->callees; e; e = e->next_callee)
2609 if (e->aux)
2611 error ("aux field set for edge %s->%s",
2612 identifier_to_locale (e->caller->name ()),
2613 identifier_to_locale (e->callee->name ()));
2614 error_found = true;
2616 if (node->count < 0)
2618 error ("execution count is negative");
2619 error_found = true;
2621 if (node->global.inlined_to && node->same_comdat_group)
2623 error ("inline clone in same comdat group list");
2624 error_found = true;
2626 if (!node->definition && !node->in_other_partition && node->local.local)
2628 error ("local symbols must be defined");
2629 error_found = true;
2631 if (node->global.inlined_to && node->externally_visible)
2633 error ("externally visible inline clone");
2634 error_found = true;
2636 if (node->global.inlined_to && node->address_taken)
2638 error ("inline clone with address taken");
2639 error_found = true;
2641 if (node->global.inlined_to && node->force_output)
2643 error ("inline clone is forced to output");
2644 error_found = true;
2646 for (e = node->indirect_calls; e; e = e->next_callee)
2648 if (e->aux)
2650 error ("aux field set for indirect edge from %s",
2651 identifier_to_locale (e->caller->name ()));
2652 error_found = true;
2654 if (!e->indirect_unknown_callee
2655 || !e->indirect_info)
2657 error ("An indirect edge from %s is not marked as indirect or has "
2658 "associated indirect_info, the corresponding statement is: ",
2659 identifier_to_locale (e->caller->name ()));
2660 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2661 error_found = true;
2664 for (e = node->callers; e; e = e->next_caller)
2666 if (verify_edge_count_and_frequency (e))
2667 error_found = true;
2668 if (!e->inline_failed)
2670 if (node->global.inlined_to
2671 != (e->caller->global.inlined_to
2672 ? e->caller->global.inlined_to : e->caller))
2674 error ("inlined_to pointer is wrong");
2675 error_found = true;
2677 if (node->callers->next_caller)
2679 error ("multiple inline callers");
2680 error_found = true;
2683 else
2684 if (node->global.inlined_to)
2686 error ("inlined_to pointer set for noninline callers");
2687 error_found = true;
2690 for (e = node->indirect_calls; e; e = e->next_callee)
2691 if (verify_edge_count_and_frequency (e))
2692 error_found = true;
2693 if (!node->callers && node->global.inlined_to)
2695 error ("inlined_to pointer is set but no predecessors found");
2696 error_found = true;
2698 if (node->global.inlined_to == node)
2700 error ("inlined_to pointer refers to itself");
2701 error_found = true;
2704 if (node->clone_of)
2706 struct cgraph_node *n;
2707 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2708 if (n == node)
2709 break;
2710 if (!n)
2712 error ("node has wrong clone_of");
2713 error_found = true;
2716 if (node->clones)
2718 struct cgraph_node *n;
2719 for (n = node->clones; n; n = n->next_sibling_clone)
2720 if (n->clone_of != node)
2721 break;
2722 if (n)
2724 error ("node has wrong clone list");
2725 error_found = true;
2728 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2730 error ("node is in clone list but it is not clone");
2731 error_found = true;
2733 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2735 error ("node has wrong prev_clone pointer");
2736 error_found = true;
2738 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2740 error ("double linked list of clones corrupted");
2741 error_found = true;
2744 if (node->analyzed && node->alias)
2746 bool ref_found = false;
2747 int i;
2748 struct ipa_ref *ref;
2750 if (node->callees)
2752 error ("Alias has call edges");
2753 error_found = true;
2755 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
2756 i, ref); i++)
2757 if (ref->use != IPA_REF_ALIAS)
2759 error ("Alias has non-alias reference");
2760 error_found = true;
2762 else if (ref_found)
2764 error ("Alias has more than one alias reference");
2765 error_found = true;
2767 else
2768 ref_found = true;
2769 if (!ref_found)
2771 error ("Analyzed alias has no reference");
2772 error_found = true;
2775 if (node->analyzed && node->thunk.thunk_p)
2777 if (!node->callees)
2779 error ("No edge out of thunk node");
2780 error_found = true;
2782 else if (node->callees->next_callee)
2784 error ("More than one edge out of thunk node");
2785 error_found = true;
2787 if (gimple_has_body_p (node->decl))
2789 error ("Thunk is not supposed to have body");
2790 error_found = true;
2793 else if (node->analyzed && gimple_has_body_p (node->decl)
2794 && !TREE_ASM_WRITTEN (node->decl)
2795 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
2796 && !flag_wpa)
2798 if (this_cfun->cfg)
2800 pointer_set_t *stmts = pointer_set_create ();
2801 int i;
2802 struct ipa_ref *ref;
2804 /* Reach the trees by walking over the CFG, and note the
2805 enclosing basic-blocks in the call edges. */
2806 FOR_EACH_BB_FN (this_block, this_cfun)
2808 for (gsi = gsi_start_phis (this_block);
2809 !gsi_end_p (gsi); gsi_next (&gsi))
2810 pointer_set_insert (stmts, gsi_stmt (gsi));
2811 for (gsi = gsi_start_bb (this_block);
2812 !gsi_end_p (gsi);
2813 gsi_next (&gsi))
2815 gimple stmt = gsi_stmt (gsi);
2816 pointer_set_insert (stmts, stmt);
2817 if (is_gimple_call (stmt))
2819 struct cgraph_edge *e = cgraph_edge (node, stmt);
2820 tree decl = gimple_call_fndecl (stmt);
2821 if (e)
2823 if (e->aux)
2825 error ("shared call_stmt:");
2826 cgraph_debug_gimple_stmt (this_cfun, stmt);
2827 error_found = true;
2829 if (!e->indirect_unknown_callee)
2831 if (verify_edge_corresponds_to_fndecl (e, decl))
2833 error ("edge points to wrong declaration:");
2834 debug_tree (e->callee->decl);
2835 fprintf (stderr," Instead of:");
2836 debug_tree (decl);
2837 error_found = true;
2840 else if (decl)
2842 error ("an indirect edge with unknown callee "
2843 "corresponding to a call_stmt with "
2844 "a known declaration:");
2845 error_found = true;
2846 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2848 e->aux = (void *)1;
2850 else if (decl)
2852 error ("missing callgraph edge for call stmt:");
2853 cgraph_debug_gimple_stmt (this_cfun, stmt);
2854 error_found = true;
2859 for (i = 0;
2860 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
2861 i++)
2862 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2864 error ("reference to dead statement");
2865 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2866 error_found = true;
2868 pointer_set_destroy (stmts);
2870 else
2871 /* No CFG available?! */
2872 gcc_unreachable ();
2874 for (e = node->callees; e; e = e->next_callee)
2876 if (!e->aux)
2878 error ("edge %s->%s has no corresponding call_stmt",
2879 identifier_to_locale (e->caller->name ()),
2880 identifier_to_locale (e->callee->name ()));
2881 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2882 error_found = true;
2884 e->aux = 0;
2886 for (e = node->indirect_calls; e; e = e->next_callee)
2888 if (!e->aux && !e->speculative)
2890 error ("an indirect edge from %s has no corresponding call_stmt",
2891 identifier_to_locale (e->caller->name ()));
2892 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2893 error_found = true;
2895 e->aux = 0;
2898 if (error_found)
2900 dump_cgraph_node (stderr, node);
2901 internal_error ("verify_cgraph_node failed");
2903 timevar_pop (TV_CGRAPH_VERIFY);
2906 /* Verify whole cgraph structure. */
2907 DEBUG_FUNCTION void
2908 verify_cgraph (void)
2910 struct cgraph_node *node;
2912 if (seen_error ())
2913 return;
2915 FOR_EACH_FUNCTION (node)
2916 verify_cgraph_node (node);
2919 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2920 Walk through thunk, too.
2921 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2923 struct cgraph_node *
2924 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2928 node = cgraph_function_or_thunk_node (node, availability);
2929 if (node->thunk.thunk_p)
2931 node = node->callees->callee;
2932 if (availability)
2934 enum availability a;
2935 a = cgraph_function_body_availability (node);
2936 if (a < *availability)
2937 *availability = a;
2939 node = cgraph_function_or_thunk_node (node, availability);
2941 } while (node && node->thunk.thunk_p);
2942 return node;
2945 /* When doing LTO, read NODE's body from disk if it is not already present. */
2947 bool
2948 cgraph_get_body (struct cgraph_node *node)
2950 struct lto_file_decl_data *file_data;
2951 const char *data, *name;
2952 size_t len;
2953 tree decl = node->decl;
2955 if (DECL_RESULT (decl))
2956 return false;
2958 gcc_assert (in_lto_p);
2960 file_data = node->lto_file_data;
2961 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2963 /* We may have renamed the declaration, e.g., a static function. */
2964 name = lto_get_decl_name_mapping (file_data, name);
2966 data = lto_get_section_data (file_data, LTO_section_function_body,
2967 name, &len);
2968 if (!data)
2970 dump_cgraph_node (stderr, node);
2971 fatal_error ("%s: section %s is missing",
2972 file_data->file_name,
2973 name);
2976 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2978 lto_input_function_body (file_data, node, data);
2979 lto_stats.num_function_bodies++;
2980 lto_free_section_data (file_data, LTO_section_function_body, name,
2981 data, len);
2982 lto_free_function_in_decl_state_for_node (node);
2983 return true;
2986 /* Verify if the type of the argument matches that of the function
2987 declaration. If we cannot verify this or there is a mismatch,
2988 return false. */
2990 static bool
2991 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
2993 tree parms, p;
2994 unsigned int i, nargs;
2996 /* Calls to internal functions always match their signature. */
2997 if (gimple_call_internal_p (stmt))
2998 return true;
3000 nargs = gimple_call_num_args (stmt);
3002 /* Get argument types for verification. */
3003 if (fndecl)
3004 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3005 else
3006 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3008 /* Verify if the type of the argument matches that of the function
3009 declaration. If we cannot verify this or there is a mismatch,
3010 return false. */
3011 if (fndecl && DECL_ARGUMENTS (fndecl))
3013 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3014 i < nargs;
3015 i++, p = DECL_CHAIN (p))
3017 tree arg;
3018 /* We cannot distinguish a varargs function from the case
3019 of excess parameters, still deferring the inlining decision
3020 to the callee is possible. */
3021 if (!p)
3022 break;
3023 arg = gimple_call_arg (stmt, i);
3024 if (p == error_mark_node
3025 || arg == error_mark_node
3026 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3027 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3028 return false;
3030 if (args_count_match && p)
3031 return false;
3033 else if (parms)
3035 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3037 tree arg;
3038 /* If this is a varargs function defer inlining decision
3039 to callee. */
3040 if (!p)
3041 break;
3042 arg = gimple_call_arg (stmt, i);
3043 if (TREE_VALUE (p) == error_mark_node
3044 || arg == error_mark_node
3045 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3046 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3047 && !fold_convertible_p (TREE_VALUE (p), arg)))
3048 return false;
3051 else
3053 if (nargs != 0)
3054 return false;
3056 return true;
3059 /* Verify if the type of the argument and lhs of CALL_STMT matches
3060 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3061 true, the arg count needs to be the same.
3062 If we cannot verify this or there is a mismatch, return false. */
3064 bool
3065 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3066 bool args_count_match)
3068 tree lhs;
3070 if ((DECL_RESULT (callee)
3071 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3072 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3073 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3074 TREE_TYPE (lhs))
3075 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3076 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3077 return false;
3078 return true;
3081 #include "gt-cgraph.h"