gcc/c-family/
[official-gcc.git] / gcc / cgraph.c
blob1ada64b120d66b54a296a9939a64d970c6b675b1
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 type = obj_type_ref_class (target);
965 /* Only record types can have virtual calls. */
966 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
967 edge->indirect_info->param_index = -1;
968 edge->indirect_info->otr_token
969 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
970 edge->indirect_info->otr_type = type;
971 edge->indirect_info->polymorphic = 1;
974 edge->next_callee = caller->indirect_calls;
975 if (caller->indirect_calls)
976 caller->indirect_calls->prev_callee = edge;
977 caller->indirect_calls = edge;
979 return edge;
982 /* Remove the edge E from the list of the callers of the callee. */
984 static inline void
985 cgraph_edge_remove_callee (struct cgraph_edge *e)
987 gcc_assert (!e->indirect_unknown_callee);
988 if (e->prev_caller)
989 e->prev_caller->next_caller = e->next_caller;
990 if (e->next_caller)
991 e->next_caller->prev_caller = e->prev_caller;
992 if (!e->prev_caller)
993 e->callee->callers = e->next_caller;
996 /* Remove the edge E from the list of the callees of the caller. */
998 static inline void
999 cgraph_edge_remove_caller (struct cgraph_edge *e)
1001 if (e->prev_callee)
1002 e->prev_callee->next_callee = e->next_callee;
1003 if (e->next_callee)
1004 e->next_callee->prev_callee = e->prev_callee;
1005 if (!e->prev_callee)
1007 if (e->indirect_unknown_callee)
1008 e->caller->indirect_calls = e->next_callee;
1009 else
1010 e->caller->callees = e->next_callee;
1012 if (e->caller->call_site_hash)
1013 htab_remove_elt_with_hash (e->caller->call_site_hash,
1014 e->call_stmt,
1015 htab_hash_pointer (e->call_stmt));
1018 /* Put the edge onto the free list. */
1020 static void
1021 cgraph_free_edge (struct cgraph_edge *e)
1023 int uid = e->uid;
1025 if (e->indirect_info)
1026 ggc_free (e->indirect_info);
1028 /* Clear out the edge so we do not dangle pointers. */
1029 memset (e, 0, sizeof (*e));
1030 e->uid = uid;
1031 NEXT_FREE_EDGE (e) = free_edges;
1032 free_edges = e;
1035 /* Remove the edge E in the cgraph. */
1037 void
1038 cgraph_remove_edge (struct cgraph_edge *e)
1040 /* Call all edge removal hooks. */
1041 cgraph_call_edge_removal_hooks (e);
1043 if (!e->indirect_unknown_callee)
1044 /* Remove from callers list of the callee. */
1045 cgraph_edge_remove_callee (e);
1047 /* Remove from callees list of the callers. */
1048 cgraph_edge_remove_caller (e);
1050 /* Put the edge onto the free list. */
1051 cgraph_free_edge (e);
1054 /* Set callee of call graph edge E and add it to the corresponding set of
1055 callers. */
1057 static void
1058 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1060 e->prev_caller = NULL;
1061 if (n->callers)
1062 n->callers->prev_caller = e;
1063 e->next_caller = n->callers;
1064 n->callers = e;
1065 e->callee = n;
1068 /* Turn edge E into speculative call calling N2. Update
1069 the profile so the direct call is taken COUNT times
1070 with FREQUENCY.
1072 At clone materialization time, the indirect call E will
1073 be expanded as:
1075 if (call_dest == N2)
1076 n2 ();
1077 else
1078 call call_dest
1080 At this time the function just creates the direct call,
1081 the referencd representing the if conditional and attaches
1082 them all to the orginal indirect call statement.
1084 Return direct edge created. */
1086 struct cgraph_edge *
1087 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1088 struct cgraph_node *n2,
1089 gcov_type direct_count,
1090 int direct_frequency)
1092 struct cgraph_node *n = e->caller;
1093 struct ipa_ref *ref;
1094 struct cgraph_edge *e2;
1096 if (dump_file)
1098 fprintf (dump_file, "Indirect call -> speculative call"
1099 " %s/%i => %s/%i\n",
1100 xstrdup (n->name ()), n->order,
1101 xstrdup (n2->name ()), n2->order);
1103 e->speculative = true;
1104 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1105 initialize_inline_failed (e2);
1106 e2->speculative = true;
1107 if (TREE_NOTHROW (n2->decl))
1108 e2->can_throw_external = false;
1109 else
1110 e2->can_throw_external = e->can_throw_external;
1111 e2->lto_stmt_uid = e->lto_stmt_uid;
1112 e->count -= e2->count;
1113 e->frequency -= e2->frequency;
1114 cgraph_call_edge_duplication_hooks (e, e2);
1115 ref = ipa_record_reference (n, n2,
1116 IPA_REF_ADDR, e->call_stmt);
1117 ref->lto_stmt_uid = e->lto_stmt_uid;
1118 ref->speculative = e->speculative;
1119 cgraph_mark_address_taken_node (n2);
1120 return e2;
1123 /* Speculative call consist of three components:
1124 1) an indirect edge representing the original call
1125 2) an direct edge representing the new call
1126 3) ADDR_EXPR reference representing the speculative check.
1127 All three components are attached to single statement (the indirect
1128 call) and if one of them exists, all of them must exist.
1130 Given speculative call edge E, return all three components.
1133 void
1134 cgraph_speculative_call_info (struct cgraph_edge *e,
1135 struct cgraph_edge *&direct,
1136 struct cgraph_edge *&indirect,
1137 struct ipa_ref *&reference)
1139 struct ipa_ref *ref;
1140 int i;
1141 struct cgraph_edge *e2;
1143 if (!e->indirect_unknown_callee)
1144 for (e2 = e->caller->indirect_calls;
1145 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1146 e2 = e2->next_callee)
1148 else
1150 e2 = e;
1151 /* We can take advantage of the call stmt hash. */
1152 if (e2->call_stmt)
1154 e = cgraph_edge (e->caller, e2->call_stmt);
1155 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1157 else
1158 for (e = e->caller->callees;
1159 e2->call_stmt != e->call_stmt
1160 || e2->lto_stmt_uid != e->lto_stmt_uid;
1161 e = e->next_callee)
1164 gcc_assert (e->speculative && e2->speculative);
1165 direct = e;
1166 indirect = e2;
1168 reference = NULL;
1169 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->ref_list,
1170 i, ref); i++)
1171 if (ref->speculative
1172 && ((ref->stmt && ref->stmt == e->call_stmt)
1173 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1175 reference = ref;
1176 break;
1179 /* Speculative edge always consist of all three components - direct edge,
1180 indirect and reference. */
1182 gcc_assert (e && e2 && ref);
1185 /* Redirect callee of E to N. The function does not update underlying
1186 call expression. */
1188 void
1189 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1191 /* Remove from callers list of the current callee. */
1192 cgraph_edge_remove_callee (e);
1194 /* Insert to callers list of the new callee. */
1195 cgraph_set_edge_callee (e, n);
1198 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1199 Remove the speculative call sequence and return edge representing the call.
1200 It is up to caller to redirect the call as appropriate. */
1202 struct cgraph_edge *
1203 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1205 struct cgraph_edge *e2;
1206 struct ipa_ref *ref;
1208 gcc_assert (edge->speculative);
1209 cgraph_speculative_call_info (edge, e2, edge, ref);
1210 if (!callee_decl
1211 || !symtab_semantically_equivalent_p (ref->referred,
1212 symtab_get_node (callee_decl)))
1214 if (dump_file)
1216 if (callee_decl)
1218 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1219 "turned out to have contradicting known target ",
1220 xstrdup (edge->caller->name ()), edge->caller->order,
1221 xstrdup (e2->callee->name ()), e2->callee->order);
1222 print_generic_expr (dump_file, callee_decl, 0);
1223 fprintf (dump_file, "\n");
1225 else
1227 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1228 xstrdup (edge->caller->name ()), edge->caller->order,
1229 xstrdup (e2->callee->name ()), e2->callee->order);
1233 else
1235 struct cgraph_edge *tmp = edge;
1236 if (dump_file)
1237 fprintf (dump_file, "Speculative call turned into direct call.\n");
1238 edge = e2;
1239 e2 = tmp;
1240 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1241 in the functions inlined through it. */
1243 edge->count += e2->count;
1244 edge->frequency += e2->frequency;
1245 if (edge->frequency > CGRAPH_FREQ_MAX)
1246 edge->frequency = CGRAPH_FREQ_MAX;
1247 edge->speculative = false;
1248 e2->speculative = false;
1249 ipa_remove_reference (ref);
1250 if (e2->indirect_unknown_callee || e2->inline_failed)
1251 cgraph_remove_edge (e2);
1252 else
1253 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1254 if (edge->caller->call_site_hash)
1255 cgraph_update_edge_in_call_site_hash (edge);
1256 return edge;
1259 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1260 CALLEE. DELTA is an integer constant that is to be added to the this
1261 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1263 struct cgraph_edge *
1264 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1266 gcc_assert (edge->indirect_unknown_callee);
1268 /* If we are redirecting speculative call, make it non-speculative. */
1269 if (edge->indirect_unknown_callee && edge->speculative)
1271 edge = cgraph_resolve_speculation (edge, callee->decl);
1273 /* On successful speculation just return the pre existing direct edge. */
1274 if (!edge->indirect_unknown_callee)
1275 return edge;
1278 edge->indirect_unknown_callee = 0;
1279 ggc_free (edge->indirect_info);
1280 edge->indirect_info = NULL;
1282 /* Get the edge out of the indirect edge list. */
1283 if (edge->prev_callee)
1284 edge->prev_callee->next_callee = edge->next_callee;
1285 if (edge->next_callee)
1286 edge->next_callee->prev_callee = edge->prev_callee;
1287 if (!edge->prev_callee)
1288 edge->caller->indirect_calls = edge->next_callee;
1290 /* Put it into the normal callee list */
1291 edge->prev_callee = NULL;
1292 edge->next_callee = edge->caller->callees;
1293 if (edge->caller->callees)
1294 edge->caller->callees->prev_callee = edge;
1295 edge->caller->callees = edge;
1297 /* Insert to callers list of the new callee. */
1298 cgraph_set_edge_callee (edge, callee);
1300 if (edge->call_stmt)
1301 edge->call_stmt_cannot_inline_p
1302 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1303 false);
1305 /* We need to re-determine the inlining status of the edge. */
1306 initialize_inline_failed (edge);
1307 return edge;
1310 /* If necessary, change the function declaration in the call statement
1311 associated with E so that it corresponds to the edge callee. */
1313 gimple
1314 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1316 tree decl = gimple_call_fndecl (e->call_stmt);
1317 gimple new_stmt;
1318 gimple_stmt_iterator gsi;
1319 #ifdef ENABLE_CHECKING
1320 struct cgraph_node *node;
1321 #endif
1323 if (e->speculative)
1325 struct cgraph_edge *e2;
1326 gimple new_stmt;
1327 struct ipa_ref *ref;
1329 cgraph_speculative_call_info (e, e, e2, ref);
1330 /* If there already is an direct call (i.e. as a result of inliner's
1331 substitution), forget about speculating. */
1332 if (decl)
1333 e = cgraph_resolve_speculation (e, decl);
1334 /* If types do not match, speculation was likely wrong.
1335 The direct edge was posisbly redirected to the clone with a different
1336 signature. We did not update the call statement yet, so compare it
1337 with the reference that still points to the proper type. */
1338 else if (!gimple_check_call_matching_types (e->call_stmt,
1339 ref->referred->decl,
1340 true))
1342 if (dump_file)
1343 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1344 "Type mismatch.\n",
1345 xstrdup (e->caller->name ()),
1346 e->caller->order,
1347 xstrdup (e->callee->name ()),
1348 e->callee->order);
1349 e = cgraph_resolve_speculation (e, NULL);
1350 /* We are producing the final function body and will throw away the
1351 callgraph edges really soon. Reset the counts/frequencies to
1352 keep verifier happy in the case of roundoff errors. */
1353 e->count = gimple_bb (e->call_stmt)->count;
1354 e->frequency = compute_call_stmt_bb_frequency
1355 (e->caller->decl, gimple_bb (e->call_stmt));
1357 /* Expand speculation into GIMPLE code. */
1358 else
1360 if (dump_file)
1361 fprintf (dump_file,
1362 "Expanding speculative call of %s/%i -> %s/%i count:"
1363 HOST_WIDEST_INT_PRINT_DEC"\n",
1364 xstrdup (e->caller->name ()),
1365 e->caller->order,
1366 xstrdup (e->callee->name ()),
1367 e->callee->order,
1368 (HOST_WIDEST_INT)e->count);
1369 gcc_assert (e2->speculative);
1370 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1371 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1372 e->count || e2->count
1373 ? RDIV (e->count * REG_BR_PROB_BASE,
1374 e->count + e2->count)
1375 : e->frequency || e2->frequency
1376 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1377 e->frequency + e2->frequency)
1378 : REG_BR_PROB_BASE / 2,
1379 e->count, e->count + e2->count);
1380 e->speculative = false;
1381 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1382 new_stmt, false);
1383 e->frequency = compute_call_stmt_bb_frequency
1384 (e->caller->decl, gimple_bb (e->call_stmt));
1385 e2->frequency = compute_call_stmt_bb_frequency
1386 (e2->caller->decl, gimple_bb (e2->call_stmt));
1387 e2->speculative = false;
1388 ref->speculative = false;
1389 ref->stmt = NULL;
1390 /* Indirect edges are not both in the call site hash.
1391 get it updated. */
1392 if (e->caller->call_site_hash)
1393 cgraph_update_edge_in_call_site_hash (e2);
1394 pop_cfun ();
1395 /* Continue redirecting E to proper target. */
1399 if (e->indirect_unknown_callee
1400 || decl == e->callee->decl)
1401 return e->call_stmt;
1403 #ifdef ENABLE_CHECKING
1404 if (decl)
1406 node = cgraph_get_node (decl);
1407 gcc_assert (!node || !node->clone.combined_args_to_skip);
1409 #endif
1411 if (cgraph_dump_file)
1413 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1414 xstrdup (e->caller->name ()), e->caller->order,
1415 xstrdup (e->callee->name ()), e->callee->order);
1416 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1417 if (e->callee->clone.combined_args_to_skip)
1419 fprintf (cgraph_dump_file, " combined args to skip: ");
1420 dump_bitmap (cgraph_dump_file,
1421 e->callee->clone.combined_args_to_skip);
1425 if (e->callee->clone.combined_args_to_skip)
1427 int lp_nr;
1429 new_stmt
1430 = gimple_call_copy_skip_args (e->call_stmt,
1431 e->callee->clone.combined_args_to_skip);
1432 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1433 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1435 if (gimple_vdef (new_stmt)
1436 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1437 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1439 gsi = gsi_for_stmt (e->call_stmt);
1440 gsi_replace (&gsi, new_stmt, false);
1441 /* We need to defer cleaning EH info on the new statement to
1442 fixup-cfg. We may not have dominator information at this point
1443 and thus would end up with unreachable blocks and have no way
1444 to communicate that we need to run CFG cleanup then. */
1445 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1446 if (lp_nr != 0)
1448 remove_stmt_from_eh_lp (e->call_stmt);
1449 add_stmt_to_eh_lp (new_stmt, lp_nr);
1452 else
1454 new_stmt = e->call_stmt;
1455 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1456 update_stmt (new_stmt);
1459 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1461 if (cgraph_dump_file)
1463 fprintf (cgraph_dump_file, " updated to:");
1464 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1466 return new_stmt;
1469 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1470 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1471 of OLD_STMT if it was previously call statement.
1472 If NEW_STMT is NULL, the call has been dropped without any
1473 replacement. */
1475 static void
1476 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1477 gimple old_stmt, tree old_call,
1478 gimple new_stmt)
1480 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1481 ? gimple_call_fndecl (new_stmt) : 0;
1483 /* We are seeing indirect calls, then there is nothing to update. */
1484 if (!new_call && !old_call)
1485 return;
1486 /* See if we turned indirect call into direct call or folded call to one builtin
1487 into different builtin. */
1488 if (old_call != new_call)
1490 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1491 struct cgraph_edge *ne = NULL;
1492 gcov_type count;
1493 int frequency;
1495 if (e)
1497 /* See if the edge is already there and has the correct callee. It
1498 might be so because of indirect inlining has already updated
1499 it. We also might've cloned and redirected the edge. */
1500 if (new_call && e->callee)
1502 struct cgraph_node *callee = e->callee;
1503 while (callee)
1505 if (callee->decl == new_call
1506 || callee->former_clone_of == new_call)
1507 return;
1508 callee = callee->clone_of;
1512 /* Otherwise remove edge and create new one; we can't simply redirect
1513 since function has changed, so inline plan and other information
1514 attached to edge is invalid. */
1515 count = e->count;
1516 frequency = e->frequency;
1517 cgraph_remove_edge (e);
1519 else if (new_call)
1521 /* We are seeing new direct call; compute profile info based on BB. */
1522 basic_block bb = gimple_bb (new_stmt);
1523 count = bb->count;
1524 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1525 bb);
1528 if (new_call)
1530 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1531 new_stmt, count, frequency);
1532 gcc_assert (ne->inline_failed);
1535 /* We only updated the call stmt; update pointer in cgraph edge.. */
1536 else if (old_stmt != new_stmt)
1537 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1540 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1541 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1542 of OLD_STMT before it was updated (updating can happen inplace). */
1544 void
1545 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1547 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1548 struct cgraph_node *node;
1550 gcc_checking_assert (orig);
1551 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1552 if (orig->clones)
1553 for (node = orig->clones; node != orig;)
1555 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1556 if (node->clones)
1557 node = node->clones;
1558 else if (node->next_sibling_clone)
1559 node = node->next_sibling_clone;
1560 else
1562 while (node != orig && !node->next_sibling_clone)
1563 node = node->clone_of;
1564 if (node != orig)
1565 node = node->next_sibling_clone;
1571 /* Remove all callees from the node. */
1573 void
1574 cgraph_node_remove_callees (struct cgraph_node *node)
1576 struct cgraph_edge *e, *f;
1578 /* It is sufficient to remove the edges from the lists of callers of
1579 the callees. The callee list of the node can be zapped with one
1580 assignment. */
1581 for (e = node->callees; e; e = f)
1583 f = e->next_callee;
1584 cgraph_call_edge_removal_hooks (e);
1585 if (!e->indirect_unknown_callee)
1586 cgraph_edge_remove_callee (e);
1587 cgraph_free_edge (e);
1589 for (e = node->indirect_calls; e; e = f)
1591 f = e->next_callee;
1592 cgraph_call_edge_removal_hooks (e);
1593 if (!e->indirect_unknown_callee)
1594 cgraph_edge_remove_callee (e);
1595 cgraph_free_edge (e);
1597 node->indirect_calls = NULL;
1598 node->callees = NULL;
1599 if (node->call_site_hash)
1601 htab_delete (node->call_site_hash);
1602 node->call_site_hash = NULL;
1606 /* Remove all callers from the node. */
1608 static void
1609 cgraph_node_remove_callers (struct cgraph_node *node)
1611 struct cgraph_edge *e, *f;
1613 /* It is sufficient to remove the edges from the lists of callees of
1614 the callers. The caller list of the node can be zapped with one
1615 assignment. */
1616 for (e = node->callers; e; e = f)
1618 f = e->next_caller;
1619 cgraph_call_edge_removal_hooks (e);
1620 cgraph_edge_remove_caller (e);
1621 cgraph_free_edge (e);
1623 node->callers = NULL;
1626 /* Helper function for cgraph_release_function_body and free_lang_data.
1627 It releases body from function DECL without having to inspect its
1628 possibly non-existent symtab node. */
1630 void
1631 release_function_body (tree decl)
1633 if (DECL_STRUCT_FUNCTION (decl))
1635 push_cfun (DECL_STRUCT_FUNCTION (decl));
1636 if (cfun->cfg
1637 && current_loops)
1639 cfun->curr_properties &= ~PROP_loops;
1640 loop_optimizer_finalize ();
1642 if (cfun->gimple_df)
1644 delete_tree_ssa ();
1645 delete_tree_cfg_annotations ();
1646 cfun->eh = NULL;
1648 if (cfun->cfg)
1650 gcc_assert (dom_computed[0] == DOM_NONE);
1651 gcc_assert (dom_computed[1] == DOM_NONE);
1652 clear_edges ();
1653 cfun->cfg = NULL;
1655 if (cfun->value_histograms)
1656 free_histograms ();
1657 pop_cfun ();
1658 gimple_set_body (decl, NULL);
1659 /* Struct function hangs a lot of data that would leak if we didn't
1660 removed all pointers to it. */
1661 ggc_free (DECL_STRUCT_FUNCTION (decl));
1662 DECL_STRUCT_FUNCTION (decl) = NULL;
1664 DECL_SAVED_TREE (decl) = NULL;
1667 /* Release memory used to represent body of function NODE.
1668 Use this only for functions that are released before being translated to
1669 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1670 are free'd in final.c via free_after_compilation(). */
1672 void
1673 cgraph_release_function_body (struct cgraph_node *node)
1675 node->ipa_transforms_to_apply.release ();
1676 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1678 DECL_RESULT (node->decl) = NULL;
1679 DECL_ARGUMENTS (node->decl) = NULL;
1681 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1682 of its associated function function declaration because it's
1683 needed to emit debug info later. */
1684 if (!node->used_as_abstract_origin && DECL_INITIAL (node->decl))
1685 DECL_INITIAL (node->decl) = error_mark_node;
1686 release_function_body (node->decl);
1687 if (node->lto_file_data)
1688 lto_free_function_in_decl_state_for_node (node);
1691 /* Remove the node from cgraph. */
1693 void
1694 cgraph_remove_node (struct cgraph_node *node)
1696 struct cgraph_node *n;
1697 int uid = node->uid;
1699 cgraph_call_node_removal_hooks (node);
1700 cgraph_node_remove_callers (node);
1701 cgraph_node_remove_callees (node);
1702 node->ipa_transforms_to_apply.release ();
1704 /* Incremental inlining access removed nodes stored in the postorder list.
1706 node->force_output = false;
1707 node->forced_by_abi = false;
1708 for (n = node->nested; n; n = n->next_nested)
1709 n->origin = NULL;
1710 node->nested = NULL;
1711 if (node->origin)
1713 struct cgraph_node **node2 = &node->origin->nested;
1715 while (*node2 != node)
1716 node2 = &(*node2)->next_nested;
1717 *node2 = node->next_nested;
1719 symtab_unregister_node (node);
1720 if (node->prev_sibling_clone)
1721 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1722 else if (node->clone_of)
1723 node->clone_of->clones = node->next_sibling_clone;
1724 if (node->next_sibling_clone)
1725 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1726 if (node->clones)
1728 struct cgraph_node *n, *next;
1730 if (node->clone_of)
1732 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1733 n->clone_of = node->clone_of;
1734 n->clone_of = node->clone_of;
1735 n->next_sibling_clone = node->clone_of->clones;
1736 if (node->clone_of->clones)
1737 node->clone_of->clones->prev_sibling_clone = n;
1738 node->clone_of->clones = node->clones;
1740 else
1742 /* We are removing node with clones. This makes clones inconsistent,
1743 but assume they will be removed subsequently and just keep clone
1744 tree intact. This can happen in unreachable function removal since
1745 we remove unreachable functions in random order, not by bottom-up
1746 walk of clone trees. */
1747 for (n = node->clones; n; n = next)
1749 next = n->next_sibling_clone;
1750 n->next_sibling_clone = NULL;
1751 n->prev_sibling_clone = NULL;
1752 n->clone_of = NULL;
1757 /* While all the clones are removed after being proceeded, the function
1758 itself is kept in the cgraph even after it is compiled. Check whether
1759 we are done with this body and reclaim it proactively if this is the case.
1761 if (cgraph_state != CGRAPH_LTO_STREAMING)
1763 n = cgraph_get_node (node->decl);
1764 if (!n
1765 || (!n->clones && !n->clone_of && !n->global.inlined_to
1766 && (cgraph_global_info_ready
1767 && (TREE_ASM_WRITTEN (n->decl)
1768 || DECL_EXTERNAL (n->decl)
1769 || !n->analyzed
1770 || (!flag_wpa && n->in_other_partition)))))
1771 cgraph_release_function_body (node);
1774 node->decl = NULL;
1775 if (node->call_site_hash)
1777 htab_delete (node->call_site_hash);
1778 node->call_site_hash = NULL;
1780 cgraph_n_nodes--;
1782 /* Clear out the node to NULL all pointers and add the node to the free
1783 list. */
1784 memset (node, 0, sizeof (*node));
1785 node->type = SYMTAB_FUNCTION;
1786 node->uid = uid;
1787 SET_NEXT_FREE_NODE (node, free_nodes);
1788 free_nodes = node;
1791 /* Likewise indicate that a node is having address taken. */
1793 void
1794 cgraph_mark_address_taken_node (struct cgraph_node *node)
1796 /* Indirect inlining can figure out that all uses of the address are
1797 inlined. */
1798 if (node->global.inlined_to)
1800 gcc_assert (cfun->after_inlining);
1801 gcc_assert (node->callers->indirect_inlining_edge);
1802 return;
1804 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1805 IPA_REF_ADDR reference exists (and thus it should be set on node
1806 representing alias we take address of) and as a test whether address
1807 of the object was taken (and thus it should be set on node alias is
1808 referring to). We should remove the first use and the remove the
1809 following set. */
1810 node->address_taken = 1;
1811 node = cgraph_function_or_thunk_node (node, NULL);
1812 node->address_taken = 1;
1815 /* Return local info for the compiled function. */
1817 struct cgraph_local_info *
1818 cgraph_local_info (tree decl)
1820 struct cgraph_node *node;
1822 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1823 node = cgraph_get_node (decl);
1824 if (!node)
1825 return NULL;
1826 return &node->local;
1829 /* Return local info for the compiled function. */
1831 struct cgraph_global_info *
1832 cgraph_global_info (tree decl)
1834 struct cgraph_node *node;
1836 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1837 node = cgraph_get_node (decl);
1838 if (!node)
1839 return NULL;
1840 return &node->global;
1843 /* Return local info for the compiled function. */
1845 struct cgraph_rtl_info *
1846 cgraph_rtl_info (tree decl)
1848 struct cgraph_node *node;
1850 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1851 node = cgraph_get_node (decl);
1852 if (!node
1853 || (decl != current_function_decl
1854 && !TREE_ASM_WRITTEN (node->decl)))
1855 return NULL;
1856 return &node->rtl;
1859 /* Return a string describing the failure REASON. */
1861 const char*
1862 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1864 #undef DEFCIFCODE
1865 #define DEFCIFCODE(code, string) string,
1867 static const char *cif_string_table[CIF_N_REASONS] = {
1868 #include "cif-code.def"
1871 /* Signedness of an enum type is implementation defined, so cast it
1872 to unsigned before testing. */
1873 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1874 return cif_string_table[reason];
1877 /* Names used to print out the availability enum. */
1878 const char * const cgraph_availability_names[] =
1879 {"unset", "not_available", "overwritable", "available", "local"};
1882 /* Dump call graph node NODE to file F. */
1884 void
1885 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1887 struct cgraph_edge *edge;
1888 int indirect_calls_count = 0;
1890 dump_symtab_base (f, node);
1892 if (node->global.inlined_to)
1893 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1894 xstrdup (node->name ()),
1895 node->order,
1896 xstrdup (node->global.inlined_to->name ()),
1897 node->global.inlined_to->order);
1898 if (node->clone_of)
1899 fprintf (f, " Clone of %s/%i\n",
1900 node->clone_of->asm_name (),
1901 node->clone_of->order);
1902 if (cgraph_function_flags_ready)
1903 fprintf (f, " Availability: %s\n",
1904 cgraph_availability_names [cgraph_function_body_availability (node)]);
1906 if (node->profile_id)
1907 fprintf (f, " Profile id: %i\n",
1908 node->profile_id);
1909 fprintf (f, " First run: %i\n", node->tp_first_run);
1910 fprintf (f, " Function flags:");
1911 if (node->count)
1912 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1913 (HOST_WIDEST_INT)node->count);
1914 if (node->origin)
1915 fprintf (f, " nested in: %s", node->origin->asm_name ());
1916 if (gimple_has_body_p (node->decl))
1917 fprintf (f, " body");
1918 if (node->process)
1919 fprintf (f, " process");
1920 if (node->local.local)
1921 fprintf (f, " local");
1922 if (node->local.redefined_extern_inline)
1923 fprintf (f, " redefined_extern_inline");
1924 if (node->only_called_at_startup)
1925 fprintf (f, " only_called_at_startup");
1926 if (node->only_called_at_exit)
1927 fprintf (f, " only_called_at_exit");
1928 if (node->tm_clone)
1929 fprintf (f, " tm_clone");
1931 fprintf (f, "\n");
1933 if (node->thunk.thunk_p)
1935 fprintf (f, " Thunk");
1936 if (node->thunk.alias)
1937 fprintf (f, " of %s (asm: %s)",
1938 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1939 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1940 fprintf (f, " fixed offset %i virtual value %i has "
1941 "virtual offset %i)\n",
1942 (int)node->thunk.fixed_offset,
1943 (int)node->thunk.virtual_value,
1944 (int)node->thunk.virtual_offset_p);
1946 if (node->alias && node->thunk.alias
1947 && DECL_P (node->thunk.alias))
1949 fprintf (f, " Alias of %s",
1950 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1951 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1952 fprintf (f, " (asm: %s)",
1953 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1954 fprintf (f, "\n");
1957 fprintf (f, " Called by: ");
1959 for (edge = node->callers; edge; edge = edge->next_caller)
1961 fprintf (f, "%s/%i ", edge->caller->asm_name (),
1962 edge->caller->order);
1963 if (edge->count)
1964 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1965 (HOST_WIDEST_INT)edge->count);
1966 if (edge->frequency)
1967 fprintf (f, "(%.2f per call) ",
1968 edge->frequency / (double)CGRAPH_FREQ_BASE);
1969 if (edge->speculative)
1970 fprintf (f, "(speculative) ");
1971 if (!edge->inline_failed)
1972 fprintf (f, "(inlined) ");
1973 if (edge->indirect_inlining_edge)
1974 fprintf (f, "(indirect_inlining) ");
1975 if (edge->can_throw_external)
1976 fprintf (f, "(can throw external) ");
1979 fprintf (f, "\n Calls: ");
1980 for (edge = node->callees; edge; edge = edge->next_callee)
1982 fprintf (f, "%s/%i ", edge->callee->asm_name (),
1983 edge->callee->order);
1984 if (edge->speculative)
1985 fprintf (f, "(speculative) ");
1986 if (!edge->inline_failed)
1987 fprintf (f, "(inlined) ");
1988 if (edge->indirect_inlining_edge)
1989 fprintf (f, "(indirect_inlining) ");
1990 if (edge->count)
1991 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1992 (HOST_WIDEST_INT)edge->count);
1993 if (edge->frequency)
1994 fprintf (f, "(%.2f per call) ",
1995 edge->frequency / (double)CGRAPH_FREQ_BASE);
1996 if (edge->can_throw_external)
1997 fprintf (f, "(can throw external) ");
1999 fprintf (f, "\n");
2001 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2002 indirect_calls_count++;
2003 if (indirect_calls_count)
2004 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2005 indirect_calls_count);
2009 /* Dump call graph node NODE to stderr. */
2011 DEBUG_FUNCTION void
2012 debug_cgraph_node (struct cgraph_node *node)
2014 dump_cgraph_node (stderr, node);
2018 /* Dump the callgraph to file F. */
2020 void
2021 dump_cgraph (FILE *f)
2023 struct cgraph_node *node;
2025 fprintf (f, "callgraph:\n\n");
2026 FOR_EACH_FUNCTION (node)
2027 dump_cgraph_node (f, node);
2031 /* Dump the call graph to stderr. */
2033 DEBUG_FUNCTION void
2034 debug_cgraph (void)
2036 dump_cgraph (stderr);
2039 /* Return true when the DECL can possibly be inlined. */
2040 bool
2041 cgraph_function_possibly_inlined_p (tree decl)
2043 if (!cgraph_global_info_ready)
2044 return !DECL_UNINLINABLE (decl);
2045 return DECL_POSSIBLY_INLINED (decl);
2048 /* NODE is no longer nested function; update cgraph accordingly. */
2049 void
2050 cgraph_unnest_node (struct cgraph_node *node)
2052 struct cgraph_node **node2 = &node->origin->nested;
2053 gcc_assert (node->origin);
2055 while (*node2 != node)
2056 node2 = &(*node2)->next_nested;
2057 *node2 = node->next_nested;
2058 node->origin = NULL;
2061 /* Return function availability. See cgraph.h for description of individual
2062 return values. */
2063 enum availability
2064 cgraph_function_body_availability (struct cgraph_node *node)
2066 enum availability avail;
2067 if (!node->analyzed)
2068 avail = AVAIL_NOT_AVAILABLE;
2069 else if (node->local.local)
2070 avail = AVAIL_LOCAL;
2071 else if (node->alias && node->weakref)
2072 cgraph_function_or_thunk_node (node, &avail);
2073 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->decl)))
2074 avail = AVAIL_OVERWRITABLE;
2075 else if (!node->externally_visible)
2076 avail = AVAIL_AVAILABLE;
2077 /* Inline functions are safe to be analyzed even if their symbol can
2078 be overwritten at runtime. It is not meaningful to enforce any sane
2079 behaviour on replacing inline function by different body. */
2080 else if (DECL_DECLARED_INLINE_P (node->decl))
2081 avail = AVAIL_AVAILABLE;
2083 /* If the function can be overwritten, return OVERWRITABLE. Take
2084 care at least of two notable extensions - the COMDAT functions
2085 used to share template instantiations in C++ (this is symmetric
2086 to code cp_cannot_inline_tree_fn and probably shall be shared and
2087 the inlinability hooks completely eliminated).
2089 ??? Does the C++ one definition rule allow us to always return
2090 AVAIL_AVAILABLE here? That would be good reason to preserve this
2091 bit. */
2093 else if (decl_replaceable_p (node->decl)
2094 && !DECL_EXTERNAL (node->decl))
2095 avail = AVAIL_OVERWRITABLE;
2096 else avail = AVAIL_AVAILABLE;
2098 return avail;
2101 /* Worker for cgraph_node_can_be_local_p. */
2102 static bool
2103 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2104 void *data ATTRIBUTE_UNUSED)
2106 return !(!node->force_output
2107 && ((DECL_COMDAT (node->decl)
2108 && !node->forced_by_abi
2109 && !symtab_used_from_object_file_p (node)
2110 && !node->same_comdat_group)
2111 || !node->externally_visible));
2114 /* Return true if NODE can be made local for API change.
2115 Extern inline functions and C++ COMDAT functions can be made local
2116 at the expense of possible code size growth if function is used in multiple
2117 compilation units. */
2118 bool
2119 cgraph_node_can_be_local_p (struct cgraph_node *node)
2121 return (!node->address_taken
2122 && !cgraph_for_node_and_aliases (node,
2123 cgraph_node_cannot_be_local_p_1,
2124 NULL, true));
2127 /* Call calback on NODE, thunks and aliases associated to NODE.
2128 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2129 skipped. */
2131 bool
2132 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2133 bool (*callback) (struct cgraph_node *, void *),
2134 void *data,
2135 bool include_overwritable)
2137 struct cgraph_edge *e;
2138 int i;
2139 struct ipa_ref *ref;
2141 if (callback (node, data))
2142 return true;
2143 for (e = node->callers; e; e = e->next_caller)
2144 if (e->caller->thunk.thunk_p
2145 && (include_overwritable
2146 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2147 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2148 include_overwritable))
2149 return true;
2150 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2151 if (ref->use == IPA_REF_ALIAS)
2153 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2154 if (include_overwritable
2155 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2156 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2157 include_overwritable))
2158 return true;
2160 return false;
2163 /* Call calback on NODE and aliases associated to NODE.
2164 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2165 skipped. */
2167 bool
2168 cgraph_for_node_and_aliases (struct cgraph_node *node,
2169 bool (*callback) (struct cgraph_node *, void *),
2170 void *data,
2171 bool include_overwritable)
2173 int i;
2174 struct ipa_ref *ref;
2176 if (callback (node, data))
2177 return true;
2178 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2179 if (ref->use == IPA_REF_ALIAS)
2181 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2182 if (include_overwritable
2183 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2184 if (cgraph_for_node_and_aliases (alias, callback, data,
2185 include_overwritable))
2186 return true;
2188 return false;
2191 /* Worker to bring NODE local. */
2193 static bool
2194 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2196 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2197 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2199 symtab_make_decl_local (node->decl);
2201 node->externally_visible = false;
2202 node->forced_by_abi = false;
2203 node->local.local = true;
2204 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2205 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2206 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2207 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2209 return false;
2212 /* Bring NODE local. */
2214 void
2215 cgraph_make_node_local (struct cgraph_node *node)
2217 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2218 NULL, true);
2221 /* Worker to set nothrow flag. */
2223 static bool
2224 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2226 struct cgraph_edge *e;
2228 TREE_NOTHROW (node->decl) = data != NULL;
2230 if (data != NULL)
2231 for (e = node->callers; e; e = e->next_caller)
2232 e->can_throw_external = false;
2233 return false;
2236 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2237 if any to NOTHROW. */
2239 void
2240 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2242 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2243 (void *)(size_t)nothrow, false);
2246 /* Worker to set const flag. */
2248 static bool
2249 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2251 /* Static constructors and destructors without a side effect can be
2252 optimized out. */
2253 if (data && !((size_t)data & 2))
2255 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2256 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2257 if (DECL_STATIC_DESTRUCTOR (node->decl))
2258 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2260 TREE_READONLY (node->decl) = data != NULL;
2261 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2262 return false;
2265 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2266 if any to READONLY. */
2268 void
2269 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2271 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2272 (void *)(size_t)(readonly + (int)looping * 2),
2273 false);
2276 /* Worker to set pure flag. */
2278 static bool
2279 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2281 /* Static constructors and destructors without a side effect can be
2282 optimized out. */
2283 if (data && !((size_t)data & 2))
2285 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2286 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2287 if (DECL_STATIC_DESTRUCTOR (node->decl))
2288 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2290 DECL_PURE_P (node->decl) = data != NULL;
2291 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2292 return false;
2295 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2296 if any to PURE. */
2298 void
2299 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2301 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2302 (void *)(size_t)(pure + (int)looping * 2),
2303 false);
2306 /* Return true when NODE can not return or throw and thus
2307 it is safe to ignore its side effects for IPA analysis. */
2309 bool
2310 cgraph_node_cannot_return (struct cgraph_node *node)
2312 int flags = flags_from_decl_or_type (node->decl);
2313 if (!flag_exceptions)
2314 return (flags & ECF_NORETURN) != 0;
2315 else
2316 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2317 == (ECF_NORETURN | ECF_NOTHROW));
2320 /* Return true when call of E can not lead to return from caller
2321 and thus it is safe to ignore its side effects for IPA analysis
2322 when computing side effects of the caller.
2323 FIXME: We could actually mark all edges that have no reaching
2324 patch to EXIT_BLOCK_PTR or throw to get better results. */
2325 bool
2326 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2328 if (cgraph_node_cannot_return (e->caller))
2329 return true;
2330 if (e->indirect_unknown_callee)
2332 int flags = e->indirect_info->ecf_flags;
2333 if (!flag_exceptions)
2334 return (flags & ECF_NORETURN) != 0;
2335 else
2336 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2337 == (ECF_NORETURN | ECF_NOTHROW));
2339 else
2340 return cgraph_node_cannot_return (e->callee);
2343 /* Return true when function NODE can be removed from callgraph
2344 if all direct calls are eliminated. */
2346 bool
2347 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2349 gcc_assert (!node->global.inlined_to);
2350 /* Extern inlines can always go, we will use the external definition. */
2351 if (DECL_EXTERNAL (node->decl))
2352 return true;
2353 /* When function is needed, we can not remove it. */
2354 if (node->force_output || node->used_from_other_partition)
2355 return false;
2356 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2357 || DECL_STATIC_DESTRUCTOR (node->decl))
2358 return false;
2359 /* Only COMDAT functions can be removed if externally visible. */
2360 if (node->externally_visible
2361 && (!DECL_COMDAT (node->decl)
2362 || node->forced_by_abi
2363 || symtab_used_from_object_file_p (node)))
2364 return false;
2365 return true;
2368 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2370 static bool
2371 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2373 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2376 /* Return true when function NODE and its aliases can be removed from callgraph
2377 if all direct calls are eliminated. */
2379 bool
2380 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2382 /* Extern inlines can always go, we will use the external definition. */
2383 if (DECL_EXTERNAL (node->decl))
2384 return true;
2385 if (node->address_taken)
2386 return false;
2387 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2390 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2392 static bool
2393 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2395 return symtab_used_from_object_file_p (node);
2398 /* Return true when function NODE can be expected to be removed
2399 from program when direct calls in this compilation unit are removed.
2401 As a special case COMDAT functions are
2402 cgraph_can_remove_if_no_direct_calls_p while the are not
2403 cgraph_only_called_directly_p (it is possible they are called from other
2404 unit)
2406 This function behaves as cgraph_only_called_directly_p because eliminating
2407 all uses of COMDAT function does not make it necessarily disappear from
2408 the program unless we are compiling whole program or we do LTO. In this
2409 case we know we win since dynamic linking will not really discard the
2410 linkonce section. */
2412 bool
2413 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2415 gcc_assert (!node->global.inlined_to);
2416 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2417 return false;
2418 if (!in_lto_p && !flag_whole_program)
2419 return cgraph_only_called_directly_p (node);
2420 else
2422 if (DECL_EXTERNAL (node->decl))
2423 return true;
2424 return cgraph_can_remove_if_no_direct_calls_p (node);
2429 /* Worker for cgraph_only_called_directly_p. */
2431 static bool
2432 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2434 return !cgraph_only_called_directly_or_aliased_p (node);
2437 /* Return true when function NODE and all its aliases are only called
2438 directly.
2439 i.e. it is not externally visible, address was not taken and
2440 it is not used in any other non-standard way. */
2442 bool
2443 cgraph_only_called_directly_p (struct cgraph_node *node)
2445 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2446 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2447 NULL, true);
2451 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2453 static bool
2454 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2456 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2457 struct cgraph_edge *cs;
2458 enum availability avail;
2459 cgraph_function_or_thunk_node (node, &avail);
2461 if (avail > AVAIL_OVERWRITABLE)
2462 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2463 if (!cs->indirect_inlining_edge)
2464 redirect_callers->safe_push (cs);
2465 return false;
2468 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2469 (i.e. are not overwritable). */
2471 vec<cgraph_edge_p>
2472 collect_callers_of_node (struct cgraph_node *node)
2474 vec<cgraph_edge_p> redirect_callers = vNULL;
2475 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2476 &redirect_callers, false);
2477 return redirect_callers;
2480 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2481 static bool
2482 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2484 node = cgraph_function_or_thunk_node (node, NULL);
2485 node2 = cgraph_function_or_thunk_node (node2, NULL);
2486 while (node != node2 && node2)
2487 node2 = node2->clone_of;
2488 return node2 != NULL;
2491 /* Verify edge E count and frequency. */
2493 static bool
2494 verify_edge_count_and_frequency (struct cgraph_edge *e)
2496 bool error_found = false;
2497 if (e->count < 0)
2499 error ("caller edge count is negative");
2500 error_found = true;
2502 if (e->frequency < 0)
2504 error ("caller edge frequency is negative");
2505 error_found = true;
2507 if (e->frequency > CGRAPH_FREQ_MAX)
2509 error ("caller edge frequency is too large");
2510 error_found = true;
2512 if (gimple_has_body_p (e->caller->decl)
2513 && !e->caller->global.inlined_to
2514 && !e->speculative
2515 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2516 Remove this once edges are actually removed from the function at that time. */
2517 && (e->frequency
2518 || (inline_edge_summary_vec.exists ()
2519 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2520 || !inline_edge_summary (e)->predicate)))
2521 && (e->frequency
2522 != compute_call_stmt_bb_frequency (e->caller->decl,
2523 gimple_bb (e->call_stmt))))
2525 error ("caller edge frequency %i does not match BB frequency %i",
2526 e->frequency,
2527 compute_call_stmt_bb_frequency (e->caller->decl,
2528 gimple_bb (e->call_stmt)));
2529 error_found = true;
2531 return error_found;
2534 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2535 static void
2536 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2538 bool fndecl_was_null = false;
2539 /* debug_gimple_stmt needs correct cfun */
2540 if (cfun != this_cfun)
2541 set_cfun (this_cfun);
2542 /* ...and an actual current_function_decl */
2543 if (!current_function_decl)
2545 current_function_decl = this_cfun->decl;
2546 fndecl_was_null = true;
2548 debug_gimple_stmt (stmt);
2549 if (fndecl_was_null)
2550 current_function_decl = NULL;
2553 /* Verify that call graph edge E corresponds to DECL from the associated
2554 statement. Return true if the verification should fail. */
2556 static bool
2557 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2559 struct cgraph_node *node;
2561 if (!decl || e->callee->global.inlined_to)
2562 return false;
2563 if (cgraph_state == CGRAPH_LTO_STREAMING)
2564 return false;
2565 node = cgraph_get_node (decl);
2567 /* We do not know if a node from a different partition is an alias or what it
2568 aliases and therefore cannot do the former_clone_of check reliably. */
2569 if (!node || node->in_other_partition || e->callee->in_other_partition)
2570 return false;
2571 node = cgraph_function_or_thunk_node (node, NULL);
2573 if (e->callee->former_clone_of != node->decl
2574 /* IPA-CP sometimes redirect edge to clone and then back to the former
2575 function. This ping-pong has to go, eventually. */
2576 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2577 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2578 return true;
2579 else
2580 return false;
2583 /* Verify cgraph nodes of given cgraph node. */
2584 DEBUG_FUNCTION void
2585 verify_cgraph_node (struct cgraph_node *node)
2587 struct cgraph_edge *e;
2588 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
2589 basic_block this_block;
2590 gimple_stmt_iterator gsi;
2591 bool error_found = false;
2593 if (seen_error ())
2594 return;
2596 timevar_push (TV_CGRAPH_VERIFY);
2597 error_found |= verify_symtab_base (node);
2598 for (e = node->callees; e; e = e->next_callee)
2599 if (e->aux)
2601 error ("aux field set for edge %s->%s",
2602 identifier_to_locale (e->caller->name ()),
2603 identifier_to_locale (e->callee->name ()));
2604 error_found = true;
2606 if (node->count < 0)
2608 error ("execution count is negative");
2609 error_found = true;
2611 if (node->global.inlined_to && node->same_comdat_group)
2613 error ("inline clone in same comdat group list");
2614 error_found = true;
2616 if (!node->definition && !node->in_other_partition && node->local.local)
2618 error ("local symbols must be defined");
2619 error_found = true;
2621 if (node->global.inlined_to && node->externally_visible)
2623 error ("externally visible inline clone");
2624 error_found = true;
2626 if (node->global.inlined_to && node->address_taken)
2628 error ("inline clone with address taken");
2629 error_found = true;
2631 if (node->global.inlined_to && node->force_output)
2633 error ("inline clone is forced to output");
2634 error_found = true;
2636 for (e = node->indirect_calls; e; e = e->next_callee)
2638 if (e->aux)
2640 error ("aux field set for indirect edge from %s",
2641 identifier_to_locale (e->caller->name ()));
2642 error_found = true;
2644 if (!e->indirect_unknown_callee
2645 || !e->indirect_info)
2647 error ("An indirect edge from %s is not marked as indirect or has "
2648 "associated indirect_info, the corresponding statement is: ",
2649 identifier_to_locale (e->caller->name ()));
2650 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2651 error_found = true;
2654 for (e = node->callers; e; e = e->next_caller)
2656 if (verify_edge_count_and_frequency (e))
2657 error_found = true;
2658 if (!e->inline_failed)
2660 if (node->global.inlined_to
2661 != (e->caller->global.inlined_to
2662 ? e->caller->global.inlined_to : e->caller))
2664 error ("inlined_to pointer is wrong");
2665 error_found = true;
2667 if (node->callers->next_caller)
2669 error ("multiple inline callers");
2670 error_found = true;
2673 else
2674 if (node->global.inlined_to)
2676 error ("inlined_to pointer set for noninline callers");
2677 error_found = true;
2680 for (e = node->indirect_calls; e; e = e->next_callee)
2681 if (verify_edge_count_and_frequency (e))
2682 error_found = true;
2683 if (!node->callers && node->global.inlined_to)
2685 error ("inlined_to pointer is set but no predecessors found");
2686 error_found = true;
2688 if (node->global.inlined_to == node)
2690 error ("inlined_to pointer refers to itself");
2691 error_found = true;
2694 if (node->clone_of)
2696 struct cgraph_node *n;
2697 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2698 if (n == node)
2699 break;
2700 if (!n)
2702 error ("node has wrong clone_of");
2703 error_found = true;
2706 if (node->clones)
2708 struct cgraph_node *n;
2709 for (n = node->clones; n; n = n->next_sibling_clone)
2710 if (n->clone_of != node)
2711 break;
2712 if (n)
2714 error ("node has wrong clone list");
2715 error_found = true;
2718 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2720 error ("node is in clone list but it is not clone");
2721 error_found = true;
2723 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2725 error ("node has wrong prev_clone pointer");
2726 error_found = true;
2728 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2730 error ("double linked list of clones corrupted");
2731 error_found = true;
2734 if (node->analyzed && node->alias)
2736 bool ref_found = false;
2737 int i;
2738 struct ipa_ref *ref;
2740 if (node->callees)
2742 error ("Alias has call edges");
2743 error_found = true;
2745 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
2746 i, ref); i++)
2747 if (ref->use != IPA_REF_ALIAS)
2749 error ("Alias has non-alias reference");
2750 error_found = true;
2752 else if (ref_found)
2754 error ("Alias has more than one alias reference");
2755 error_found = true;
2757 else
2758 ref_found = true;
2759 if (!ref_found)
2761 error ("Analyzed alias has no reference");
2762 error_found = true;
2765 if (node->analyzed && node->thunk.thunk_p)
2767 if (!node->callees)
2769 error ("No edge out of thunk node");
2770 error_found = true;
2772 else if (node->callees->next_callee)
2774 error ("More than one edge out of thunk node");
2775 error_found = true;
2777 if (gimple_has_body_p (node->decl))
2779 error ("Thunk is not supposed to have body");
2780 error_found = true;
2783 else if (node->analyzed && gimple_has_body_p (node->decl)
2784 && !TREE_ASM_WRITTEN (node->decl)
2785 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
2786 && !flag_wpa)
2788 if (this_cfun->cfg)
2790 pointer_set_t *stmts = pointer_set_create ();
2791 int i;
2792 struct ipa_ref *ref;
2794 /* Reach the trees by walking over the CFG, and note the
2795 enclosing basic-blocks in the call edges. */
2796 FOR_EACH_BB_FN (this_block, this_cfun)
2798 for (gsi = gsi_start_phis (this_block);
2799 !gsi_end_p (gsi); gsi_next (&gsi))
2800 pointer_set_insert (stmts, gsi_stmt (gsi));
2801 for (gsi = gsi_start_bb (this_block);
2802 !gsi_end_p (gsi);
2803 gsi_next (&gsi))
2805 gimple stmt = gsi_stmt (gsi);
2806 pointer_set_insert (stmts, stmt);
2807 if (is_gimple_call (stmt))
2809 struct cgraph_edge *e = cgraph_edge (node, stmt);
2810 tree decl = gimple_call_fndecl (stmt);
2811 if (e)
2813 if (e->aux)
2815 error ("shared call_stmt:");
2816 cgraph_debug_gimple_stmt (this_cfun, stmt);
2817 error_found = true;
2819 if (!e->indirect_unknown_callee)
2821 if (verify_edge_corresponds_to_fndecl (e, decl))
2823 error ("edge points to wrong declaration:");
2824 debug_tree (e->callee->decl);
2825 fprintf (stderr," Instead of:");
2826 debug_tree (decl);
2827 error_found = true;
2830 else if (decl)
2832 error ("an indirect edge with unknown callee "
2833 "corresponding to a call_stmt with "
2834 "a known declaration:");
2835 error_found = true;
2836 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2838 e->aux = (void *)1;
2840 else if (decl)
2842 error ("missing callgraph edge for call stmt:");
2843 cgraph_debug_gimple_stmt (this_cfun, stmt);
2844 error_found = true;
2849 for (i = 0;
2850 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
2851 i++)
2852 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2854 error ("reference to dead statement");
2855 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2856 error_found = true;
2858 pointer_set_destroy (stmts);
2860 else
2861 /* No CFG available?! */
2862 gcc_unreachable ();
2864 for (e = node->callees; e; e = e->next_callee)
2866 if (!e->aux)
2868 error ("edge %s->%s has no corresponding call_stmt",
2869 identifier_to_locale (e->caller->name ()),
2870 identifier_to_locale (e->callee->name ()));
2871 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2872 error_found = true;
2874 e->aux = 0;
2876 for (e = node->indirect_calls; e; e = e->next_callee)
2878 if (!e->aux && !e->speculative)
2880 error ("an indirect edge from %s has no corresponding call_stmt",
2881 identifier_to_locale (e->caller->name ()));
2882 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2883 error_found = true;
2885 e->aux = 0;
2888 if (error_found)
2890 dump_cgraph_node (stderr, node);
2891 internal_error ("verify_cgraph_node failed");
2893 timevar_pop (TV_CGRAPH_VERIFY);
2896 /* Verify whole cgraph structure. */
2897 DEBUG_FUNCTION void
2898 verify_cgraph (void)
2900 struct cgraph_node *node;
2902 if (seen_error ())
2903 return;
2905 FOR_EACH_FUNCTION (node)
2906 verify_cgraph_node (node);
2909 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2910 Walk through thunk, too.
2911 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2913 struct cgraph_node *
2914 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2918 node = cgraph_function_or_thunk_node (node, availability);
2919 if (node->thunk.thunk_p)
2921 node = node->callees->callee;
2922 if (availability)
2924 enum availability a;
2925 a = cgraph_function_body_availability (node);
2926 if (a < *availability)
2927 *availability = a;
2929 node = cgraph_function_or_thunk_node (node, availability);
2931 } while (node && node->thunk.thunk_p);
2932 return node;
2935 /* When doing LTO, read NODE's body from disk if it is not already present. */
2937 bool
2938 cgraph_get_body (struct cgraph_node *node)
2940 struct lto_file_decl_data *file_data;
2941 const char *data, *name;
2942 size_t len;
2943 tree decl = node->decl;
2945 if (DECL_RESULT (decl))
2946 return false;
2948 gcc_assert (in_lto_p);
2950 file_data = node->lto_file_data;
2951 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2953 /* We may have renamed the declaration, e.g., a static function. */
2954 name = lto_get_decl_name_mapping (file_data, name);
2956 data = lto_get_section_data (file_data, LTO_section_function_body,
2957 name, &len);
2958 if (!data)
2960 dump_cgraph_node (stderr, node);
2961 fatal_error ("%s: section %s is missing",
2962 file_data->file_name,
2963 name);
2966 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2968 lto_input_function_body (file_data, node, data);
2969 lto_stats.num_function_bodies++;
2970 lto_free_section_data (file_data, LTO_section_function_body, name,
2971 data, len);
2972 lto_free_function_in_decl_state_for_node (node);
2973 return true;
2976 /* Verify if the type of the argument matches that of the function
2977 declaration. If we cannot verify this or there is a mismatch,
2978 return false. */
2980 static bool
2981 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
2983 tree parms, p;
2984 unsigned int i, nargs;
2986 /* Calls to internal functions always match their signature. */
2987 if (gimple_call_internal_p (stmt))
2988 return true;
2990 nargs = gimple_call_num_args (stmt);
2992 /* Get argument types for verification. */
2993 if (fndecl)
2994 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2995 else
2996 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
2998 /* Verify if the type of the argument matches that of the function
2999 declaration. If we cannot verify this or there is a mismatch,
3000 return false. */
3001 if (fndecl && DECL_ARGUMENTS (fndecl))
3003 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3004 i < nargs;
3005 i++, p = DECL_CHAIN (p))
3007 tree arg;
3008 /* We cannot distinguish a varargs function from the case
3009 of excess parameters, still deferring the inlining decision
3010 to the callee is possible. */
3011 if (!p)
3012 break;
3013 arg = gimple_call_arg (stmt, i);
3014 if (p == error_mark_node
3015 || arg == error_mark_node
3016 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3017 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3018 return false;
3020 if (args_count_match && p)
3021 return false;
3023 else if (parms)
3025 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3027 tree arg;
3028 /* If this is a varargs function defer inlining decision
3029 to callee. */
3030 if (!p)
3031 break;
3032 arg = gimple_call_arg (stmt, i);
3033 if (TREE_VALUE (p) == error_mark_node
3034 || arg == error_mark_node
3035 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3036 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3037 && !fold_convertible_p (TREE_VALUE (p), arg)))
3038 return false;
3041 else
3043 if (nargs != 0)
3044 return false;
3046 return true;
3049 /* Verify if the type of the argument and lhs of CALL_STMT matches
3050 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3051 true, the arg count needs to be the same.
3052 If we cannot verify this or there is a mismatch, return false. */
3054 bool
3055 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3056 bool args_count_match)
3058 tree lhs;
3060 if ((DECL_RESULT (callee)
3061 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3062 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3063 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3064 TREE_TYPE (lhs))
3065 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3066 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3067 return false;
3068 return true;
3071 #include "gt-cgraph.h"