PR c/61077
[official-gcc.git] / gcc / cgraph.c
bloba5d07496f0a7a6fb7ec574cc7c2a94d760570db7
1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "varasm.h"
32 #include "calls.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "hashtab.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "debug.h"
40 #include "target.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "tree-eh.h"
46 #include "gimple-expr.h"
47 #include "gimple.h"
48 #include "gimple-iterator.h"
49 #include "timevar.h"
50 #include "dumpfile.h"
51 #include "gimple-ssa.h"
52 #include "cgraph.h"
53 #include "tree-cfg.h"
54 #include "tree-ssa.h"
55 #include "value-prof.h"
56 #include "except.h"
57 #include "diagnostic-core.h"
58 #include "rtl.h"
59 #include "ipa-utils.h"
60 #include "lto-streamer.h"
61 #include "ipa-inline.h"
62 #include "cfgloop.h"
63 #include "gimple-pretty-print.h"
64 #include "expr.h"
65 #include "tree-dfa.h"
67 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
68 #include "tree-pass.h"
70 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
71 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
73 /* Queue of cgraph nodes scheduled to be lowered. */
74 symtab_node *x_cgraph_nodes_queue;
75 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
77 /* Number of nodes in existence. */
78 int cgraph_n_nodes;
80 /* Maximal uid used in cgraph nodes. */
81 int cgraph_max_uid;
83 /* Maximal uid used in cgraph edges. */
84 int cgraph_edge_max_uid;
86 /* Set when whole unit has been analyzed so we can access global info. */
87 bool cgraph_global_info_ready = false;
89 /* What state callgraph is in right now. */
90 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
92 /* Set when the cgraph is fully build and the basic flags are computed. */
93 bool cgraph_function_flags_ready = false;
95 /* List of hooks triggered on cgraph_edge events. */
96 struct cgraph_edge_hook_list {
97 cgraph_edge_hook hook;
98 void *data;
99 struct cgraph_edge_hook_list *next;
102 /* List of hooks triggered on cgraph_node events. */
103 struct cgraph_node_hook_list {
104 cgraph_node_hook hook;
105 void *data;
106 struct cgraph_node_hook_list *next;
109 /* List of hooks triggered on events involving two cgraph_edges. */
110 struct cgraph_2edge_hook_list {
111 cgraph_2edge_hook hook;
112 void *data;
113 struct cgraph_2edge_hook_list *next;
116 /* List of hooks triggered on events involving two cgraph_nodes. */
117 struct cgraph_2node_hook_list {
118 cgraph_2node_hook hook;
119 void *data;
120 struct cgraph_2node_hook_list *next;
123 /* List of hooks triggered when an edge is removed. */
124 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
125 /* List of hooks triggered when a node is removed. */
126 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
127 /* List of hooks triggered when an edge is duplicated. */
128 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
129 /* List of hooks triggered when a node is duplicated. */
130 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
131 /* List of hooks triggered when an function is inserted. */
132 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
134 /* Head of a linked list of unused (freed) call graph nodes.
135 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
136 static GTY(()) struct cgraph_node *free_nodes;
137 /* Head of a linked list of unused (freed) call graph edges.
138 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
139 static GTY(()) struct cgraph_edge *free_edges;
141 /* Did procss_same_body_aliases run? */
142 bool cpp_implicit_aliases_done;
144 /* Map a cgraph_node to cgraph_function_version_info using this htab.
145 The cgraph_function_version_info has a THIS_NODE field that is the
146 corresponding cgraph_node.. */
148 static GTY((param_is (struct cgraph_function_version_info))) htab_t
149 cgraph_fnver_htab = NULL;
151 /* Hash function for cgraph_fnver_htab. */
152 static hashval_t
153 cgraph_fnver_htab_hash (const void *ptr)
155 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
156 return (hashval_t)(uid);
159 /* eq function for cgraph_fnver_htab. */
160 static int
161 cgraph_fnver_htab_eq (const void *p1, const void *p2)
163 const struct cgraph_function_version_info *n1
164 = (const struct cgraph_function_version_info *)p1;
165 const struct cgraph_function_version_info *n2
166 = (const struct cgraph_function_version_info *)p2;
168 return n1->this_node->uid == n2->this_node->uid;
171 /* Mark as GC root all allocated nodes. */
172 static GTY(()) struct cgraph_function_version_info *
173 version_info_node = NULL;
175 /* Get the cgraph_function_version_info node corresponding to node. */
176 struct cgraph_function_version_info *
177 cgraph_node::function_version (void)
179 struct cgraph_function_version_info *ret;
180 struct cgraph_function_version_info key;
181 key.this_node = this;
183 if (cgraph_fnver_htab == NULL)
184 return NULL;
186 ret = (struct cgraph_function_version_info *)
187 htab_find (cgraph_fnver_htab, &key);
189 return ret;
192 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
193 corresponding to cgraph_node NODE. */
194 struct cgraph_function_version_info *
195 cgraph_node::insert_new_function_version (void)
197 void **slot;
199 version_info_node = NULL;
200 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
201 version_info_node->this_node = this;
203 if (cgraph_fnver_htab == NULL)
204 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
205 cgraph_fnver_htab_eq, NULL);
207 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
208 gcc_assert (slot != NULL);
209 *slot = version_info_node;
210 return version_info_node;
213 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
214 DECL is a duplicate declaration. */
215 void
216 cgraph_node::delete_function_version (tree decl)
218 struct cgraph_node *decl_node = cgraph_node::get (decl);
219 struct cgraph_function_version_info *decl_v = NULL;
221 if (decl_node == NULL)
222 return;
224 decl_v = decl_node->function_version ();
226 if (decl_v == NULL)
227 return;
229 if (decl_v->prev != NULL)
230 decl_v->prev->next = decl_v->next;
232 if (decl_v->next != NULL)
233 decl_v->next->prev = decl_v->prev;
235 if (cgraph_fnver_htab != NULL)
236 htab_remove_elt (cgraph_fnver_htab, decl_v);
238 decl_node->remove ();
241 /* Record that DECL1 and DECL2 are semantically identical function
242 versions. */
243 void
244 cgraph_node::record_function_versions (tree decl1, tree decl2)
246 struct cgraph_node *decl1_node = cgraph_node::get_create (decl1);
247 struct cgraph_node *decl2_node = cgraph_node::get_create (decl2);
248 struct cgraph_function_version_info *decl1_v = NULL;
249 struct cgraph_function_version_info *decl2_v = NULL;
250 struct cgraph_function_version_info *before;
251 struct cgraph_function_version_info *after;
253 gcc_assert (decl1_node != NULL && decl2_node != NULL);
254 decl1_v = decl1_node->function_version ();
255 decl2_v = decl2_node->function_version ();
257 if (decl1_v != NULL && decl2_v != NULL)
258 return;
260 if (decl1_v == NULL)
261 decl1_v = decl1_node->insert_new_function_version ();
263 if (decl2_v == NULL)
264 decl2_v = decl2_node->insert_new_function_version ();
266 /* Chain decl2_v and decl1_v. All semantically identical versions
267 will be chained together. */
269 before = decl1_v;
270 after = decl2_v;
272 while (before->next != NULL)
273 before = before->next;
275 while (after->prev != NULL)
276 after= after->prev;
278 before->next = after;
279 after->prev = before;
282 /* Macros to access the next item in the list of free cgraph nodes and
283 edges. */
284 #define NEXT_FREE_NODE(NODE) dyn_cast<cgraph_node *> ((NODE)->next)
285 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
286 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
288 /* Register HOOK to be called with DATA on each removed edge. */
289 struct cgraph_edge_hook_list *
290 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
292 struct cgraph_edge_hook_list *entry;
293 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
295 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
296 entry->hook = hook;
297 entry->data = data;
298 entry->next = NULL;
299 while (*ptr)
300 ptr = &(*ptr)->next;
301 *ptr = entry;
302 return entry;
305 /* Remove ENTRY from the list of hooks called on removing edges. */
306 void
307 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
309 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
311 while (*ptr != entry)
312 ptr = &(*ptr)->next;
313 *ptr = entry->next;
314 free (entry);
317 /* Call all edge removal hooks. */
318 static void
319 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
321 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
322 while (entry)
324 entry->hook (e, entry->data);
325 entry = entry->next;
329 /* Register HOOK to be called with DATA on each removed node. */
330 struct cgraph_node_hook_list *
331 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
333 struct cgraph_node_hook_list *entry;
334 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
336 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
337 entry->hook = hook;
338 entry->data = data;
339 entry->next = NULL;
340 while (*ptr)
341 ptr = &(*ptr)->next;
342 *ptr = entry;
343 return entry;
346 /* Remove ENTRY from the list of hooks called on removing nodes. */
347 void
348 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
350 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
352 while (*ptr != entry)
353 ptr = &(*ptr)->next;
354 *ptr = entry->next;
355 free (entry);
358 /* Call all node removal hooks. */
359 static void
360 cgraph_call_node_removal_hooks (struct cgraph_node *node)
362 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
363 while (entry)
365 entry->hook (node, entry->data);
366 entry = entry->next;
370 /* Register HOOK to be called with DATA on each inserted node. */
371 struct cgraph_node_hook_list *
372 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
374 struct cgraph_node_hook_list *entry;
375 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
377 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
378 entry->hook = hook;
379 entry->data = data;
380 entry->next = NULL;
381 while (*ptr)
382 ptr = &(*ptr)->next;
383 *ptr = entry;
384 return entry;
387 /* Remove ENTRY from the list of hooks called on inserted nodes. */
388 void
389 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
391 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
393 while (*ptr != entry)
394 ptr = &(*ptr)->next;
395 *ptr = entry->next;
396 free (entry);
399 /* Call all node insertion hooks. */
400 void
401 cgraph_node::call_function_insertion_hooks (void)
403 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
404 while (entry)
406 entry->hook (this, entry->data);
407 entry = entry->next;
411 /* Register HOOK to be called with DATA on each duplicated edge. */
412 struct cgraph_2edge_hook_list *
413 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
415 struct cgraph_2edge_hook_list *entry;
416 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
418 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
419 entry->hook = hook;
420 entry->data = data;
421 entry->next = NULL;
422 while (*ptr)
423 ptr = &(*ptr)->next;
424 *ptr = entry;
425 return entry;
428 /* Remove ENTRY from the list of hooks called on duplicating edges. */
429 void
430 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
432 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
434 while (*ptr != entry)
435 ptr = &(*ptr)->next;
436 *ptr = entry->next;
437 free (entry);
440 /* Call all edge duplication hooks. */
441 void
442 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
443 struct cgraph_edge *cs2)
445 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
446 while (entry)
448 entry->hook (cs1, cs2, entry->data);
449 entry = entry->next;
453 /* Register HOOK to be called with DATA on each duplicated node. */
454 struct cgraph_2node_hook_list *
455 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
457 struct cgraph_2node_hook_list *entry;
458 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
460 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
461 entry->hook = hook;
462 entry->data = data;
463 entry->next = NULL;
464 while (*ptr)
465 ptr = &(*ptr)->next;
466 *ptr = entry;
467 return entry;
470 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
471 void
472 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
474 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
476 while (*ptr != entry)
477 ptr = &(*ptr)->next;
478 *ptr = entry->next;
479 free (entry);
482 /* Call all node duplication hooks. */
483 void
484 cgraph_node::call_duplication_hooks (struct cgraph_node *node2)
486 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
487 while (entry)
489 entry->hook (this, node2, entry->data);
490 entry = entry->next;
494 /* Allocate new callgraph node. */
496 static inline struct cgraph_node *
497 cgraph_allocate_node (void)
499 struct cgraph_node *node;
501 if (free_nodes)
503 node = free_nodes;
504 free_nodes = NEXT_FREE_NODE (node);
506 else
508 node = ggc_cleared_alloc<cgraph_node> ();
509 node->uid = cgraph_max_uid++;
512 return node;
515 /* Allocate new callgraph node and insert it into basic data structures. */
517 cgraph_node *
518 cgraph_node::create_empty (void)
520 struct cgraph_node *node = cgraph_allocate_node ();
522 node->type = SYMTAB_FUNCTION;
523 node->frequency = NODE_FREQUENCY_NORMAL;
524 node->count_materialization_scale = REG_BR_PROB_BASE;
525 cgraph_n_nodes++;
526 return node;
529 /* Return cgraph node assigned to DECL. Create new one when needed. */
531 cgraph_node *
532 cgraph_node::create (tree decl)
534 struct cgraph_node *node = cgraph_node::create_empty ();
535 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
537 node->decl = decl;
538 node->register_symbol ();
540 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
542 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
543 node->next_nested = node->origin->nested;
544 node->origin->nested = node;
546 return node;
549 /* Try to find a call graph node for declaration DECL and if it does not exist
550 or if it corresponds to an inline clone, create a new one. */
552 cgraph_node *
553 cgraph_node::get_create (tree decl)
555 struct cgraph_node *first_clone = cgraph_node::get (decl);
557 if (first_clone && !first_clone->global.inlined_to)
558 return first_clone;
560 struct cgraph_node *node = cgraph_node::create (decl);
561 if (first_clone)
563 first_clone->clone_of = node;
564 node->clones = first_clone;
565 symtab_prevail_in_asm_name_hash (node);
566 node->decl->decl_with_vis.symtab_node = node;
567 if (dump_file)
568 fprintf (dump_file, "Introduced new external node "
569 "(%s/%i) and turned into root of the clone tree.\n",
570 xstrdup (node->name ()), node->order);
572 else if (dump_file)
573 fprintf (dump_file, "Introduced new external node "
574 "(%s/%i).\n", xstrdup (node->name ()),
575 node->order);
576 return node;
579 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
580 the function body is associated with (not necessarily cgraph_node (DECL). */
582 cgraph_node *
583 cgraph_node::create_alias (tree alias, tree target)
585 cgraph_node *alias_node;
587 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
588 || TREE_CODE (target) == IDENTIFIER_NODE);
589 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
590 alias_node = cgraph_node::get_create (alias);
591 gcc_assert (!alias_node->definition);
592 alias_node->alias_target = target;
593 alias_node->definition = true;
594 alias_node->alias = true;
595 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
596 alias_node->weakref = true;
597 return alias_node;
600 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
601 and NULL otherwise.
602 Same body aliases are output whenever the body of DECL is output,
603 and cgraph_node::get (ALIAS) transparently returns
604 cgraph_node::get (DECL). */
606 struct cgraph_node *
607 cgraph_node::create_same_body_alias (tree alias, tree decl)
609 struct cgraph_node *n;
610 #ifndef ASM_OUTPUT_DEF
611 /* If aliases aren't supported by the assembler, fail. */
612 return NULL;
613 #endif
614 /* Langhooks can create same body aliases of symbols not defined.
615 Those are useless. Drop them on the floor. */
616 if (cgraph_global_info_ready)
617 return NULL;
619 n = cgraph_node::create_alias (alias, decl);
620 n->cpp_implicit_alias = true;
621 if (cpp_implicit_aliases_done)
622 n->resolve_alias (cgraph_node::get (decl));
623 return n;
626 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
627 aliases DECL with an adjustments made into the first parameter.
628 See comments in thunk_adjust for detail on the parameters. */
630 struct cgraph_node *
631 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
632 HOST_WIDE_INT fixed_offset,
633 HOST_WIDE_INT virtual_value,
634 tree virtual_offset,
635 tree real_alias)
637 struct cgraph_node *node;
639 node = cgraph_node::get (alias);
640 if (node)
641 node->reset ();
642 else
643 node = cgraph_node::create (alias);
644 gcc_checking_assert (!virtual_offset
645 || wi::eq_p (virtual_offset, virtual_value));
646 node->thunk.fixed_offset = fixed_offset;
647 node->thunk.this_adjusting = this_adjusting;
648 node->thunk.virtual_value = virtual_value;
649 node->thunk.virtual_offset_p = virtual_offset != NULL;
650 node->thunk.alias = real_alias;
651 node->thunk.thunk_p = true;
652 node->definition = true;
654 return node;
657 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
658 Return NULL if there's no such node. */
660 cgraph_node *
661 cgraph_node::get_for_asmname (tree asmname)
663 /* We do not want to look at inline clones. */
664 for (symtab_node *node = symtab_node_for_asm (asmname);
665 node;
666 node = node->next_sharing_asm_name)
668 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
669 if (cn && !cn->global.inlined_to)
670 return cn;
672 return NULL;
675 /* Returns a hash value for X (which really is a cgraph_edge). */
677 static hashval_t
678 edge_hash (const void *x)
680 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
683 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
685 static int
686 edge_eq (const void *x, const void *y)
688 return ((const struct cgraph_edge *) x)->call_stmt == y;
691 /* Add call graph edge E to call site hash of its caller. */
693 static inline void
694 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
696 void **slot;
697 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
698 e->call_stmt,
699 htab_hash_pointer (e->call_stmt),
700 INSERT);
701 *slot = e;
704 /* Add call graph edge E to call site hash of its caller. */
706 static inline void
707 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
709 void **slot;
710 /* There are two speculative edges for every statement (one direct,
711 one indirect); always hash the direct one. */
712 if (e->speculative && e->indirect_unknown_callee)
713 return;
714 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
715 e->call_stmt,
716 htab_hash_pointer (e->call_stmt),
717 INSERT);
718 if (*slot)
720 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
721 if (e->callee)
722 *slot = e;
723 return;
725 gcc_assert (!*slot || e->speculative);
726 *slot = e;
729 /* Return the callgraph edge representing the GIMPLE_CALL statement
730 CALL_STMT. */
732 cgraph_edge *
733 cgraph_node::get_edge (gimple call_stmt)
735 struct cgraph_edge *e, *e2;
736 int n = 0;
738 if (call_site_hash)
739 return (struct cgraph_edge *)
740 htab_find_with_hash (call_site_hash, call_stmt,
741 htab_hash_pointer (call_stmt));
743 /* This loop may turn out to be performance problem. In such case adding
744 hashtables into call nodes with very many edges is probably best
745 solution. It is not good idea to add pointer into CALL_EXPR itself
746 because we want to make possible having multiple cgraph nodes representing
747 different clones of the same body before the body is actually cloned. */
748 for (e = callees; e; e = e->next_callee)
750 if (e->call_stmt == call_stmt)
751 break;
752 n++;
755 if (!e)
756 for (e = indirect_calls; e; e = e->next_callee)
758 if (e->call_stmt == call_stmt)
759 break;
760 n++;
763 if (n > 100)
765 call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
766 for (e2 = callees; e2; e2 = e2->next_callee)
767 cgraph_add_edge_to_call_site_hash (e2);
768 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
769 cgraph_add_edge_to_call_site_hash (e2);
772 return e;
776 /* Change field call_stmt of edge E to NEW_STMT.
777 If UPDATE_SPECULATIVE and E is any component of speculative
778 edge, then update all components. */
780 void
781 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
782 bool update_speculative)
784 tree decl;
786 /* Speculative edges has three component, update all of them
787 when asked to. */
788 if (update_speculative && e->speculative)
790 struct cgraph_edge *direct, *indirect;
791 struct ipa_ref *ref;
793 cgraph_speculative_call_info (e, direct, indirect, ref);
794 cgraph_set_call_stmt (direct, new_stmt, false);
795 cgraph_set_call_stmt (indirect, new_stmt, false);
796 ref->stmt = new_stmt;
797 return;
800 /* Only direct speculative edges go to call_site_hash. */
801 if (e->caller->call_site_hash
802 && (!e->speculative || !e->indirect_unknown_callee))
804 htab_remove_elt_with_hash (e->caller->call_site_hash,
805 e->call_stmt,
806 htab_hash_pointer (e->call_stmt));
809 e->call_stmt = new_stmt;
810 if (e->indirect_unknown_callee
811 && (decl = gimple_call_fndecl (new_stmt)))
813 /* Constant propagation (and possibly also inlining?) can turn an
814 indirect call into a direct one. */
815 struct cgraph_node *new_callee = cgraph_node::get (decl);
817 gcc_checking_assert (new_callee);
818 e = cgraph_make_edge_direct (e, new_callee);
821 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
822 e->can_throw_external = stmt_can_throw_external (new_stmt);
823 pop_cfun ();
824 if (e->caller->call_site_hash)
825 cgraph_add_edge_to_call_site_hash (e);
828 /* Allocate a cgraph_edge structure and fill it with data according to the
829 parameters of which only CALLEE can be NULL (when creating an indirect call
830 edge). */
832 cgraph_edge *
833 cgraph_node::create_edge (cgraph_node *caller, cgraph_node *callee,
834 gimple call_stmt, gcov_type count, int freq,
835 bool indir_unknown_callee)
837 cgraph_edge *edge;
839 /* LTO does not actually have access to the call_stmt since these
840 have not been loaded yet. */
841 if (call_stmt)
843 /* This is a rather expensive check possibly triggering
844 construction of call stmt hashtable. */
845 #ifdef ENABLE_CHECKING
846 struct cgraph_edge *e;
847 gcc_checking_assert (
848 !(e = caller->get_edge (call_stmt)) || e->speculative);
849 #endif
851 gcc_assert (is_gimple_call (call_stmt));
854 if (free_edges)
856 edge = free_edges;
857 free_edges = NEXT_FREE_EDGE (edge);
859 else
861 edge = ggc_alloc<struct cgraph_edge> ();
862 edge->uid = cgraph_edge_max_uid++;
865 edge->aux = NULL;
866 edge->caller = caller;
867 edge->callee = callee;
868 edge->prev_caller = NULL;
869 edge->next_caller = NULL;
870 edge->prev_callee = NULL;
871 edge->next_callee = NULL;
872 edge->lto_stmt_uid = 0;
874 edge->count = count;
875 gcc_assert (count >= 0);
876 edge->frequency = freq;
877 gcc_assert (freq >= 0);
878 gcc_assert (freq <= CGRAPH_FREQ_MAX);
880 edge->call_stmt = call_stmt;
881 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
882 edge->can_throw_external
883 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
884 pop_cfun ();
885 if (call_stmt
886 && callee && callee->decl
887 && !gimple_check_call_matching_types (call_stmt, callee->decl,
888 false))
889 edge->call_stmt_cannot_inline_p = true;
890 else
891 edge->call_stmt_cannot_inline_p = false;
893 edge->indirect_info = NULL;
894 edge->indirect_inlining_edge = 0;
895 edge->speculative = false;
896 edge->indirect_unknown_callee = indir_unknown_callee;
897 if (call_stmt && caller->call_site_hash)
898 cgraph_add_edge_to_call_site_hash (edge);
900 return edge;
903 /* Create edge from a given function to CALLEE in the cgraph. */
905 struct cgraph_edge *
906 cgraph_node::create_edge (struct cgraph_node *callee,
907 gimple call_stmt, gcov_type count, int freq)
909 cgraph_edge *edge = cgraph_node::create_edge (this, callee, call_stmt,
910 count, freq, false);
912 initialize_inline_failed (edge);
914 edge->next_caller = callee->callers;
915 if (callee->callers)
916 callee->callers->prev_caller = edge;
917 edge->next_callee = callees;
918 if (callees)
919 callees->prev_callee = edge;
920 callees = edge;
921 callee->callers = edge;
923 return edge;
926 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
928 struct cgraph_indirect_call_info *
929 cgraph_allocate_init_indirect_info (void)
931 struct cgraph_indirect_call_info *ii;
933 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
934 ii->param_index = -1;
935 return ii;
938 /* Create an indirect edge with a yet-undetermined callee where the call
939 statement destination is a formal parameter of the caller with index
940 PARAM_INDEX. */
942 struct cgraph_edge *
943 cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
944 gcov_type count, int freq)
946 struct cgraph_edge *edge = cgraph_node::create_edge (this, NULL, call_stmt,
947 count, freq, true);
948 tree target;
950 initialize_inline_failed (edge);
952 edge->indirect_info = cgraph_allocate_init_indirect_info ();
953 edge->indirect_info->ecf_flags = ecf_flags;
955 /* Record polymorphic call info. */
956 if (call_stmt
957 && (target = gimple_call_fn (call_stmt))
958 && virtual_method_call_p (target))
960 tree otr_type;
961 HOST_WIDE_INT otr_token;
962 ipa_polymorphic_call_context context;
964 get_polymorphic_call_info (decl,
965 target,
966 &otr_type, &otr_token,
967 &context, call_stmt);
969 /* Only record types can have virtual calls. */
970 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
971 edge->indirect_info->polymorphic = true;
972 edge->indirect_info->param_index = -1;
973 edge->indirect_info->otr_token = otr_token;
974 edge->indirect_info->otr_type = otr_type;
975 edge->indirect_info->outer_type = context.outer_type;
976 edge->indirect_info->offset = context.offset;
977 edge->indirect_info->maybe_in_construction
978 = context.maybe_in_construction;
979 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
982 edge->next_callee = indirect_calls;
983 if (indirect_calls)
984 indirect_calls->prev_callee = edge;
985 indirect_calls = edge;
987 return edge;
990 /* Remove the edge E from the list of the callers of the callee. */
992 static inline void
993 cgraph_edge_remove_callee (struct cgraph_edge *e)
995 gcc_assert (!e->indirect_unknown_callee);
996 if (e->prev_caller)
997 e->prev_caller->next_caller = e->next_caller;
998 if (e->next_caller)
999 e->next_caller->prev_caller = e->prev_caller;
1000 if (!e->prev_caller)
1001 e->callee->callers = e->next_caller;
1004 /* Remove the edge E from the list of the callees of the caller. */
1006 static inline void
1007 cgraph_edge_remove_caller (struct cgraph_edge *e)
1009 if (e->prev_callee)
1010 e->prev_callee->next_callee = e->next_callee;
1011 if (e->next_callee)
1012 e->next_callee->prev_callee = e->prev_callee;
1013 if (!e->prev_callee)
1015 if (e->indirect_unknown_callee)
1016 e->caller->indirect_calls = e->next_callee;
1017 else
1018 e->caller->callees = e->next_callee;
1020 if (e->caller->call_site_hash)
1021 htab_remove_elt_with_hash (e->caller->call_site_hash,
1022 e->call_stmt,
1023 htab_hash_pointer (e->call_stmt));
1026 /* Put the edge onto the free list. */
1028 static void
1029 cgraph_free_edge (struct cgraph_edge *e)
1031 int uid = e->uid;
1033 if (e->indirect_info)
1034 ggc_free (e->indirect_info);
1036 /* Clear out the edge so we do not dangle pointers. */
1037 memset (e, 0, sizeof (*e));
1038 e->uid = uid;
1039 NEXT_FREE_EDGE (e) = free_edges;
1040 free_edges = e;
1043 /* Remove the edge E in the cgraph. */
1045 void
1046 cgraph_remove_edge (struct cgraph_edge *e)
1048 /* Call all edge removal hooks. */
1049 cgraph_call_edge_removal_hooks (e);
1051 if (!e->indirect_unknown_callee)
1052 /* Remove from callers list of the callee. */
1053 cgraph_edge_remove_callee (e);
1055 /* Remove from callees list of the callers. */
1056 cgraph_edge_remove_caller (e);
1058 /* Put the edge onto the free list. */
1059 cgraph_free_edge (e);
1062 /* Set callee of call graph edge E and add it to the corresponding set of
1063 callers. */
1065 static void
1066 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1068 e->prev_caller = NULL;
1069 if (n->callers)
1070 n->callers->prev_caller = e;
1071 e->next_caller = n->callers;
1072 n->callers = e;
1073 e->callee = n;
1076 /* Turn edge E into speculative call calling N2. Update
1077 the profile so the direct call is taken COUNT times
1078 with FREQUENCY.
1080 At clone materialization time, the indirect call E will
1081 be expanded as:
1083 if (call_dest == N2)
1084 n2 ();
1085 else
1086 call call_dest
1088 At this time the function just creates the direct call,
1089 the referencd representing the if conditional and attaches
1090 them all to the orginal indirect call statement.
1092 Return direct edge created. */
1094 struct cgraph_edge *
1095 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1096 struct cgraph_node *n2,
1097 gcov_type direct_count,
1098 int direct_frequency)
1100 struct cgraph_node *n = e->caller;
1101 struct ipa_ref *ref = NULL;
1102 struct cgraph_edge *e2;
1104 if (dump_file)
1106 fprintf (dump_file, "Indirect call -> speculative call"
1107 " %s/%i => %s/%i\n",
1108 xstrdup (n->name ()), n->order,
1109 xstrdup (n2->name ()), n2->order);
1111 e->speculative = true;
1112 e2 = n->create_edge (n2, e->call_stmt, direct_count, direct_frequency);
1113 initialize_inline_failed (e2);
1114 e2->speculative = true;
1115 if (TREE_NOTHROW (n2->decl))
1116 e2->can_throw_external = false;
1117 else
1118 e2->can_throw_external = e->can_throw_external;
1119 e2->lto_stmt_uid = e->lto_stmt_uid;
1120 e->count -= e2->count;
1121 e->frequency -= e2->frequency;
1122 cgraph_call_edge_duplication_hooks (e, e2);
1123 ref = n->add_reference (n2, IPA_REF_ADDR, e->call_stmt);
1124 ref->lto_stmt_uid = e->lto_stmt_uid;
1125 ref->speculative = e->speculative;
1126 n2->mark_address_taken ();
1127 return e2;
1130 /* Speculative call consist of three components:
1131 1) an indirect edge representing the original call
1132 2) an direct edge representing the new call
1133 3) ADDR_EXPR reference representing the speculative check.
1134 All three components are attached to single statement (the indirect
1135 call) and if one of them exists, all of them must exist.
1137 Given speculative call edge E, return all three components.
1140 void
1141 cgraph_speculative_call_info (struct cgraph_edge *e,
1142 struct cgraph_edge *&direct,
1143 struct cgraph_edge *&indirect,
1144 struct ipa_ref *&reference)
1146 struct ipa_ref *ref;
1147 int i;
1148 struct cgraph_edge *e2;
1150 if (!e->indirect_unknown_callee)
1151 for (e2 = e->caller->indirect_calls;
1152 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1153 e2 = e2->next_callee)
1155 else
1157 e2 = e;
1158 /* We can take advantage of the call stmt hash. */
1159 if (e2->call_stmt)
1161 e = e->caller->get_edge (e2->call_stmt);
1162 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1164 else
1165 for (e = e->caller->callees;
1166 e2->call_stmt != e->call_stmt
1167 || e2->lto_stmt_uid != e->lto_stmt_uid;
1168 e = e->next_callee)
1171 gcc_assert (e->speculative && e2->speculative);
1172 direct = e;
1173 indirect = e2;
1175 reference = NULL;
1176 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1177 if (ref->speculative
1178 && ((ref->stmt && ref->stmt == e->call_stmt)
1179 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1181 reference = ref;
1182 break;
1185 /* Speculative edge always consist of all three components - direct edge,
1186 indirect and reference. */
1188 gcc_assert (e && e2 && ref);
1191 /* Redirect callee of E to N. The function does not update underlying
1192 call expression. */
1194 void
1195 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1197 /* Remove from callers list of the current callee. */
1198 cgraph_edge_remove_callee (e);
1200 /* Insert to callers list of the new callee. */
1201 cgraph_set_edge_callee (e, n);
1204 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1205 Remove the speculative call sequence and return edge representing the call.
1206 It is up to caller to redirect the call as appropriate. */
1208 struct cgraph_edge *
1209 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1211 struct cgraph_edge *e2;
1212 struct ipa_ref *ref;
1214 gcc_assert (edge->speculative);
1215 cgraph_speculative_call_info (edge, e2, edge, ref);
1216 if (!callee_decl
1217 || !ref->referred->semantically_equivalent_p
1218 (symtab_node::get (callee_decl)))
1220 if (dump_file)
1222 if (callee_decl)
1224 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1225 "turned out to have contradicting known target ",
1226 xstrdup (edge->caller->name ()), edge->caller->order,
1227 xstrdup (e2->callee->name ()), e2->callee->order);
1228 print_generic_expr (dump_file, callee_decl, 0);
1229 fprintf (dump_file, "\n");
1231 else
1233 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1234 xstrdup (edge->caller->name ()), edge->caller->order,
1235 xstrdup (e2->callee->name ()), e2->callee->order);
1239 else
1241 struct cgraph_edge *tmp = edge;
1242 if (dump_file)
1243 fprintf (dump_file, "Speculative call turned into direct call.\n");
1244 edge = e2;
1245 e2 = tmp;
1246 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1247 in the functions inlined through it. */
1249 edge->count += e2->count;
1250 edge->frequency += e2->frequency;
1251 if (edge->frequency > CGRAPH_FREQ_MAX)
1252 edge->frequency = CGRAPH_FREQ_MAX;
1253 edge->speculative = false;
1254 e2->speculative = false;
1255 ref->remove_reference ();
1256 if (e2->indirect_unknown_callee || e2->inline_failed)
1257 cgraph_remove_edge (e2);
1258 else
1259 e2->callee->remove_symbol_and_inline_clones ();
1260 if (edge->caller->call_site_hash)
1261 cgraph_update_edge_in_call_site_hash (edge);
1262 return edge;
1265 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1266 CALLEE. DELTA is an integer constant that is to be added to the this
1267 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1269 struct cgraph_edge *
1270 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1272 gcc_assert (edge->indirect_unknown_callee);
1274 /* If we are redirecting speculative call, make it non-speculative. */
1275 if (edge->indirect_unknown_callee && edge->speculative)
1277 edge = cgraph_resolve_speculation (edge, callee->decl);
1279 /* On successful speculation just return the pre existing direct edge. */
1280 if (!edge->indirect_unknown_callee)
1281 return edge;
1284 edge->indirect_unknown_callee = 0;
1285 ggc_free (edge->indirect_info);
1286 edge->indirect_info = NULL;
1288 /* Get the edge out of the indirect edge list. */
1289 if (edge->prev_callee)
1290 edge->prev_callee->next_callee = edge->next_callee;
1291 if (edge->next_callee)
1292 edge->next_callee->prev_callee = edge->prev_callee;
1293 if (!edge->prev_callee)
1294 edge->caller->indirect_calls = edge->next_callee;
1296 /* Put it into the normal callee list */
1297 edge->prev_callee = NULL;
1298 edge->next_callee = edge->caller->callees;
1299 if (edge->caller->callees)
1300 edge->caller->callees->prev_callee = edge;
1301 edge->caller->callees = edge;
1303 /* Insert to callers list of the new callee. */
1304 cgraph_set_edge_callee (edge, callee);
1306 if (edge->call_stmt)
1307 edge->call_stmt_cannot_inline_p
1308 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1309 false);
1311 /* We need to re-determine the inlining status of the edge. */
1312 initialize_inline_failed (edge);
1313 return edge;
1316 /* If necessary, change the function declaration in the call statement
1317 associated with E so that it corresponds to the edge callee. */
1319 gimple
1320 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1322 tree decl = gimple_call_fndecl (e->call_stmt);
1323 tree lhs = gimple_call_lhs (e->call_stmt);
1324 gimple new_stmt;
1325 gimple_stmt_iterator gsi;
1326 #ifdef ENABLE_CHECKING
1327 struct cgraph_node *node;
1328 #endif
1330 if (e->speculative)
1332 struct cgraph_edge *e2;
1333 gimple new_stmt;
1334 struct ipa_ref *ref;
1336 cgraph_speculative_call_info (e, e, e2, ref);
1337 /* If there already is an direct call (i.e. as a result of inliner's
1338 substitution), forget about speculating. */
1339 if (decl)
1340 e = cgraph_resolve_speculation (e, decl);
1341 /* If types do not match, speculation was likely wrong.
1342 The direct edge was posisbly redirected to the clone with a different
1343 signature. We did not update the call statement yet, so compare it
1344 with the reference that still points to the proper type. */
1345 else if (!gimple_check_call_matching_types (e->call_stmt,
1346 ref->referred->decl,
1347 true))
1349 if (dump_file)
1350 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1351 "Type mismatch.\n",
1352 xstrdup (e->caller->name ()),
1353 e->caller->order,
1354 xstrdup (e->callee->name ()),
1355 e->callee->order);
1356 e = cgraph_resolve_speculation (e, NULL);
1357 /* We are producing the final function body and will throw away the
1358 callgraph edges really soon. Reset the counts/frequencies to
1359 keep verifier happy in the case of roundoff errors. */
1360 e->count = gimple_bb (e->call_stmt)->count;
1361 e->frequency = compute_call_stmt_bb_frequency
1362 (e->caller->decl, gimple_bb (e->call_stmt));
1364 /* Expand speculation into GIMPLE code. */
1365 else
1367 if (dump_file)
1368 fprintf (dump_file,
1369 "Expanding speculative call of %s/%i -> %s/%i count:"
1370 "%"PRId64"\n",
1371 xstrdup (e->caller->name ()),
1372 e->caller->order,
1373 xstrdup (e->callee->name ()),
1374 e->callee->order,
1375 (int64_t)e->count);
1376 gcc_assert (e2->speculative);
1377 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1378 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
1379 e->count || e2->count
1380 ? RDIV (e->count * REG_BR_PROB_BASE,
1381 e->count + e2->count)
1382 : e->frequency || e2->frequency
1383 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1384 e->frequency + e2->frequency)
1385 : REG_BR_PROB_BASE / 2,
1386 e->count, e->count + e2->count);
1387 e->speculative = false;
1388 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1389 false);
1390 e->frequency = compute_call_stmt_bb_frequency
1391 (e->caller->decl, gimple_bb (e->call_stmt));
1392 e2->frequency = compute_call_stmt_bb_frequency
1393 (e2->caller->decl, gimple_bb (e2->call_stmt));
1394 e2->speculative = false;
1395 ref->speculative = false;
1396 ref->stmt = NULL;
1397 /* Indirect edges are not both in the call site hash.
1398 get it updated. */
1399 if (e->caller->call_site_hash)
1400 cgraph_update_edge_in_call_site_hash (e2);
1401 pop_cfun ();
1402 /* Continue redirecting E to proper target. */
1406 if (e->indirect_unknown_callee
1407 || decl == e->callee->decl)
1408 return e->call_stmt;
1410 #ifdef ENABLE_CHECKING
1411 if (decl)
1413 node = cgraph_node::get (decl);
1414 gcc_assert (!node || !node->clone.combined_args_to_skip);
1416 #endif
1418 if (cgraph_dump_file)
1420 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1421 xstrdup (e->caller->name ()), e->caller->order,
1422 xstrdup (e->callee->name ()), e->callee->order);
1423 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1424 if (e->callee->clone.combined_args_to_skip)
1426 fprintf (cgraph_dump_file, " combined args to skip: ");
1427 dump_bitmap (cgraph_dump_file,
1428 e->callee->clone.combined_args_to_skip);
1432 if (e->callee->clone.combined_args_to_skip)
1434 int lp_nr;
1436 new_stmt
1437 = gimple_call_copy_skip_args (e->call_stmt,
1438 e->callee->clone.combined_args_to_skip);
1439 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1440 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1442 if (gimple_vdef (new_stmt)
1443 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1444 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1446 gsi = gsi_for_stmt (e->call_stmt);
1447 gsi_replace (&gsi, new_stmt, false);
1448 /* We need to defer cleaning EH info on the new statement to
1449 fixup-cfg. We may not have dominator information at this point
1450 and thus would end up with unreachable blocks and have no way
1451 to communicate that we need to run CFG cleanup then. */
1452 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1453 if (lp_nr != 0)
1455 remove_stmt_from_eh_lp (e->call_stmt);
1456 add_stmt_to_eh_lp (new_stmt, lp_nr);
1459 else
1461 new_stmt = e->call_stmt;
1462 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1463 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1466 /* If the call becomes noreturn, remove the lhs. */
1467 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1469 if (TREE_CODE (lhs) == SSA_NAME)
1471 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1472 TREE_TYPE (lhs), NULL);
1473 var = get_or_create_ssa_default_def
1474 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1475 gimple set_stmt = gimple_build_assign (lhs, var);
1476 gsi = gsi_for_stmt (new_stmt);
1477 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1478 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1480 gimple_call_set_lhs (new_stmt, NULL_TREE);
1481 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1484 /* If new callee has no static chain, remove it. */
1485 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1487 gimple_call_set_chain (new_stmt, NULL);
1488 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1491 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1493 if (cgraph_dump_file)
1495 fprintf (cgraph_dump_file, " updated to:");
1496 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1498 return new_stmt;
1501 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1502 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1503 of OLD_STMT if it was previously call statement.
1504 If NEW_STMT is NULL, the call has been dropped without any
1505 replacement. */
1507 static void
1508 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1509 gimple old_stmt, tree old_call,
1510 gimple new_stmt)
1512 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1513 ? gimple_call_fndecl (new_stmt) : 0;
1515 /* We are seeing indirect calls, then there is nothing to update. */
1516 if (!new_call && !old_call)
1517 return;
1518 /* See if we turned indirect call into direct call or folded call to one builtin
1519 into different builtin. */
1520 if (old_call != new_call)
1522 struct cgraph_edge *e = node->get_edge (old_stmt);
1523 struct cgraph_edge *ne = NULL;
1524 gcov_type count;
1525 int frequency;
1527 if (e)
1529 /* See if the edge is already there and has the correct callee. It
1530 might be so because of indirect inlining has already updated
1531 it. We also might've cloned and redirected the edge. */
1532 if (new_call && e->callee)
1534 struct cgraph_node *callee = e->callee;
1535 while (callee)
1537 if (callee->decl == new_call
1538 || callee->former_clone_of == new_call)
1540 cgraph_set_call_stmt (e, new_stmt);
1541 return;
1543 callee = callee->clone_of;
1547 /* Otherwise remove edge and create new one; we can't simply redirect
1548 since function has changed, so inline plan and other information
1549 attached to edge is invalid. */
1550 count = e->count;
1551 frequency = e->frequency;
1552 if (e->indirect_unknown_callee || e->inline_failed)
1553 cgraph_remove_edge (e);
1554 else
1555 e->callee->remove_symbol_and_inline_clones ();
1557 else if (new_call)
1559 /* We are seeing new direct call; compute profile info based on BB. */
1560 basic_block bb = gimple_bb (new_stmt);
1561 count = bb->count;
1562 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1563 bb);
1566 if (new_call)
1568 ne = node->create_edge (cgraph_node::get_create (new_call),
1569 new_stmt, count, frequency);
1570 gcc_assert (ne->inline_failed);
1573 /* We only updated the call stmt; update pointer in cgraph edge.. */
1574 else if (old_stmt != new_stmt)
1575 cgraph_set_call_stmt (node->get_edge (old_stmt), new_stmt);
1578 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1579 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1580 of OLD_STMT before it was updated (updating can happen inplace). */
1582 void
1583 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1585 struct cgraph_node *orig = cgraph_node::get (cfun->decl);
1586 struct cgraph_node *node;
1588 gcc_checking_assert (orig);
1589 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1590 if (orig->clones)
1591 for (node = orig->clones; node != orig;)
1593 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1594 if (node->clones)
1595 node = node->clones;
1596 else if (node->next_sibling_clone)
1597 node = node->next_sibling_clone;
1598 else
1600 while (node != orig && !node->next_sibling_clone)
1601 node = node->clone_of;
1602 if (node != orig)
1603 node = node->next_sibling_clone;
1609 /* Remove all callees from the node. */
1611 void
1612 cgraph_node::remove_callees (void)
1614 struct cgraph_edge *e, *f;
1616 /* It is sufficient to remove the edges from the lists of callers of
1617 the callees. The callee list of the node can be zapped with one
1618 assignment. */
1619 for (e = callees; e; e = f)
1621 f = e->next_callee;
1622 cgraph_call_edge_removal_hooks (e);
1623 if (!e->indirect_unknown_callee)
1624 cgraph_edge_remove_callee (e);
1625 cgraph_free_edge (e);
1627 for (e = indirect_calls; e; e = f)
1629 f = e->next_callee;
1630 cgraph_call_edge_removal_hooks (e);
1631 if (!e->indirect_unknown_callee)
1632 cgraph_edge_remove_callee (e);
1633 cgraph_free_edge (e);
1635 indirect_calls = NULL;
1636 callees = NULL;
1637 if (call_site_hash)
1639 htab_delete (call_site_hash);
1640 call_site_hash = NULL;
1644 /* Remove all callers from the node. */
1646 void
1647 cgraph_node::remove_callers (void)
1649 struct cgraph_edge *e, *f;
1651 /* It is sufficient to remove the edges from the lists of callees of
1652 the callers. The caller list of the node can be zapped with one
1653 assignment. */
1654 for (e = callers; e; e = f)
1656 f = e->next_caller;
1657 cgraph_call_edge_removal_hooks (e);
1658 cgraph_edge_remove_caller (e);
1659 cgraph_free_edge (e);
1661 callers = NULL;
1664 /* Helper function for cgraph_release_function_body and free_lang_data.
1665 It releases body from function DECL without having to inspect its
1666 possibly non-existent symtab node. */
1668 void
1669 release_function_body (tree decl)
1671 if (DECL_STRUCT_FUNCTION (decl))
1673 push_cfun (DECL_STRUCT_FUNCTION (decl));
1674 if (cfun->cfg
1675 && current_loops)
1677 cfun->curr_properties &= ~PROP_loops;
1678 loop_optimizer_finalize ();
1680 if (cfun->gimple_df)
1682 delete_tree_ssa ();
1683 delete_tree_cfg_annotations ();
1684 cfun->eh = NULL;
1686 if (cfun->cfg)
1688 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1689 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1690 clear_edges ();
1691 cfun->cfg = NULL;
1693 if (cfun->value_histograms)
1694 free_histograms ();
1695 pop_cfun ();
1696 gimple_set_body (decl, NULL);
1697 /* Struct function hangs a lot of data that would leak if we didn't
1698 removed all pointers to it. */
1699 ggc_free (DECL_STRUCT_FUNCTION (decl));
1700 DECL_STRUCT_FUNCTION (decl) = NULL;
1702 DECL_SAVED_TREE (decl) = NULL;
1705 /* Release memory used to represent body of function.
1706 Use this only for functions that are released before being translated to
1707 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1708 are free'd in final.c via free_after_compilation(). */
1710 void
1711 cgraph_node::release_body (void)
1713 ipa_transforms_to_apply.release ();
1714 if (!used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1716 DECL_RESULT (decl) = NULL;
1717 DECL_ARGUMENTS (decl) = NULL;
1719 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1720 of its associated function function declaration because it's
1721 needed to emit debug info later. */
1722 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1723 DECL_INITIAL (decl) = error_mark_node;
1724 release_function_body (decl);
1725 if (lto_file_data)
1726 lto_free_function_in_decl_state_for_node (this);
1729 /* Remove function from symbol table. */
1731 void
1732 cgraph_node::remove (void)
1734 struct cgraph_node *n;
1735 int uid = this->uid;
1737 cgraph_call_node_removal_hooks (this);
1738 remove_callers ();
1739 remove_callees ();
1740 ipa_transforms_to_apply.release ();
1742 /* Incremental inlining access removed nodes stored in the postorder list.
1744 force_output = false;
1745 forced_by_abi = false;
1746 for (n = nested; n; n = n->next_nested)
1747 n->origin = NULL;
1748 nested = NULL;
1749 if (origin)
1751 struct cgraph_node **node2 = &origin->nested;
1753 while (*node2 != this)
1754 node2 = &(*node2)->next_nested;
1755 *node2 = next_nested;
1757 unregister ();
1758 if (prev_sibling_clone)
1759 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1760 else if (clone_of)
1761 clone_of->clones = next_sibling_clone;
1762 if (next_sibling_clone)
1763 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1764 if (clones)
1766 struct cgraph_node *n, *next;
1768 if (clone_of)
1770 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1771 n->clone_of = clone_of;
1772 n->clone_of = clone_of;
1773 n->next_sibling_clone = clone_of->clones;
1774 if (clone_of->clones)
1775 clone_of->clones->prev_sibling_clone = n;
1776 clone_of->clones = clones;
1778 else
1780 /* We are removing node with clones. This makes clones inconsistent,
1781 but assume they will be removed subsequently and just keep clone
1782 tree intact. This can happen in unreachable function removal since
1783 we remove unreachable functions in random order, not by bottom-up
1784 walk of clone trees. */
1785 for (n = clones; n; n = next)
1787 next = n->next_sibling_clone;
1788 n->next_sibling_clone = NULL;
1789 n->prev_sibling_clone = NULL;
1790 n->clone_of = NULL;
1795 /* While all the clones are removed after being proceeded, the function
1796 itself is kept in the cgraph even after it is compiled. Check whether
1797 we are done with this body and reclaim it proactively if this is the case.
1799 if (cgraph_state != CGRAPH_LTO_STREAMING)
1801 n = cgraph_node::get (decl);
1802 if (!n
1803 || (!n->clones && !n->clone_of && !n->global.inlined_to
1804 && (cgraph_global_info_ready
1805 && (TREE_ASM_WRITTEN (n->decl)
1806 || DECL_EXTERNAL (n->decl)
1807 || !n->analyzed
1808 || (!flag_wpa && n->in_other_partition)))))
1809 release_body ();
1812 decl = NULL;
1813 if (call_site_hash)
1815 htab_delete (call_site_hash);
1816 call_site_hash = NULL;
1818 cgraph_n_nodes--;
1820 /* Clear out the node to NULL all pointers and add the node to the free
1821 list. */
1822 memset (this, 0, sizeof (*this));
1823 type = SYMTAB_FUNCTION;
1824 this->uid = uid;
1825 SET_NEXT_FREE_NODE (this, free_nodes);
1826 free_nodes = this;
1829 /* Likewise indicate that a node is having address taken. */
1831 void
1832 cgraph_node::mark_address_taken (void)
1834 /* Indirect inlining can figure out that all uses of the address are
1835 inlined. */
1836 if (global.inlined_to)
1838 gcc_assert (cfun->after_inlining);
1839 gcc_assert (callers->indirect_inlining_edge);
1840 return;
1842 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1843 IPA_REF_ADDR reference exists (and thus it should be set on node
1844 representing alias we take address of) and as a test whether address
1845 of the object was taken (and thus it should be set on node alias is
1846 referring to). We should remove the first use and the remove the
1847 following set. */
1848 address_taken = 1;
1849 cgraph_node *node = ultimate_alias_target ();
1850 node->address_taken = 1;
1853 /* Return local info for the compiled function. */
1855 struct cgraph_local_info *
1856 cgraph_local_info (tree decl)
1858 struct cgraph_node *node;
1860 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1861 node = cgraph_node::get (decl);
1862 if (!node)
1863 return NULL;
1864 return &node->local;
1867 /* Return local info for the compiled function. */
1869 struct cgraph_global_info *
1870 cgraph_global_info (tree decl)
1872 struct cgraph_node *node;
1874 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1875 node = cgraph_node::get (decl);
1876 if (!node)
1877 return NULL;
1878 return &node->global;
1881 /* Return local info for the compiled function. */
1883 struct cgraph_rtl_info *
1884 cgraph_rtl_info (tree decl)
1886 struct cgraph_node *node;
1888 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1889 node = cgraph_node::get (decl);
1890 if (!node
1891 || (decl != current_function_decl
1892 && !TREE_ASM_WRITTEN (node->decl)))
1893 return NULL;
1894 return &node->rtl;
1897 /* Return a string describing the failure REASON. */
1899 const char*
1900 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1902 #undef DEFCIFCODE
1903 #define DEFCIFCODE(code, type, string) string,
1905 static const char *cif_string_table[CIF_N_REASONS] = {
1906 #include "cif-code.def"
1909 /* Signedness of an enum type is implementation defined, so cast it
1910 to unsigned before testing. */
1911 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1912 return cif_string_table[reason];
1915 /* Return a type describing the failure REASON. */
1917 cgraph_inline_failed_type_t
1918 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1920 #undef DEFCIFCODE
1921 #define DEFCIFCODE(code, type, string) type,
1923 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1924 #include "cif-code.def"
1927 /* Signedness of an enum type is implementation defined, so cast it
1928 to unsigned before testing. */
1929 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1930 return cif_type_table[reason];
1933 /* Names used to print out the availability enum. */
1934 const char * const cgraph_availability_names[] =
1935 {"unset", "not_available", "overwritable", "available", "local"};
1938 /* Dump call graph node to file F. */
1940 void
1941 cgraph_node::dump (FILE *f)
1943 struct cgraph_edge *edge;
1944 int indirect_calls_count = 0;
1946 dump_base (f);
1948 if (global.inlined_to)
1949 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1950 xstrdup (name ()),
1951 order,
1952 xstrdup (global.inlined_to->name ()),
1953 global.inlined_to->order);
1954 if (clone_of)
1955 fprintf (f, " Clone of %s/%i\n",
1956 clone_of->asm_name (),
1957 clone_of->order);
1958 if (cgraph_function_flags_ready)
1959 fprintf (f, " Availability: %s\n",
1960 cgraph_availability_names [get_availability ()]);
1962 if (profile_id)
1963 fprintf (f, " Profile id: %i\n",
1964 profile_id);
1965 fprintf (f, " First run: %i\n", tp_first_run);
1966 fprintf (f, " Function flags:");
1967 if (count)
1968 fprintf (f, " executed %"PRId64"x",
1969 (int64_t)count);
1970 if (origin)
1971 fprintf (f, " nested in: %s", origin->asm_name ());
1972 if (gimple_has_body_p (decl))
1973 fprintf (f, " body");
1974 if (process)
1975 fprintf (f, " process");
1976 if (local.local)
1977 fprintf (f, " local");
1978 if (local.redefined_extern_inline)
1979 fprintf (f, " redefined_extern_inline");
1980 if (only_called_at_startup)
1981 fprintf (f, " only_called_at_startup");
1982 if (only_called_at_exit)
1983 fprintf (f, " only_called_at_exit");
1984 if (tm_clone)
1985 fprintf (f, " tm_clone");
1986 if (DECL_STATIC_CONSTRUCTOR (decl))
1987 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
1988 if (DECL_STATIC_DESTRUCTOR (decl))
1989 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
1991 fprintf (f, "\n");
1993 if (thunk.thunk_p)
1995 fprintf (f, " Thunk");
1996 if (thunk.alias)
1997 fprintf (f, " of %s (asm: %s)",
1998 lang_hooks.decl_printable_name (thunk.alias, 2),
1999 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2000 fprintf (f, " fixed offset %i virtual value %i has "
2001 "virtual offset %i)\n",
2002 (int)thunk.fixed_offset,
2003 (int)thunk.virtual_value,
2004 (int)thunk.virtual_offset_p);
2006 if (alias && thunk.alias
2007 && DECL_P (thunk.alias))
2009 fprintf (f, " Alias of %s",
2010 lang_hooks.decl_printable_name (thunk.alias, 2));
2011 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2012 fprintf (f, " (asm: %s)",
2013 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2014 fprintf (f, "\n");
2017 fprintf (f, " Called by: ");
2019 for (edge = callers; edge; edge = edge->next_caller)
2021 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2022 edge->caller->order);
2023 if (edge->count)
2024 fprintf (f, "(%"PRId64"x) ",
2025 (int64_t)edge->count);
2026 if (edge->frequency)
2027 fprintf (f, "(%.2f per call) ",
2028 edge->frequency / (double)CGRAPH_FREQ_BASE);
2029 if (edge->speculative)
2030 fprintf (f, "(speculative) ");
2031 if (!edge->inline_failed)
2032 fprintf (f, "(inlined) ");
2033 if (edge->indirect_inlining_edge)
2034 fprintf (f, "(indirect_inlining) ");
2035 if (edge->can_throw_external)
2036 fprintf (f, "(can throw external) ");
2039 fprintf (f, "\n Calls: ");
2040 for (edge = callees; edge; edge = edge->next_callee)
2042 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2043 edge->callee->order);
2044 if (edge->speculative)
2045 fprintf (f, "(speculative) ");
2046 if (!edge->inline_failed)
2047 fprintf (f, "(inlined) ");
2048 if (edge->indirect_inlining_edge)
2049 fprintf (f, "(indirect_inlining) ");
2050 if (edge->count)
2051 fprintf (f, "(%"PRId64"x) ",
2052 (int64_t)edge->count);
2053 if (edge->frequency)
2054 fprintf (f, "(%.2f per call) ",
2055 edge->frequency / (double)CGRAPH_FREQ_BASE);
2056 if (edge->can_throw_external)
2057 fprintf (f, "(can throw external) ");
2059 fprintf (f, "\n");
2061 for (edge = indirect_calls; edge; edge = edge->next_callee)
2062 indirect_calls_count++;
2063 if (indirect_calls_count)
2064 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2065 indirect_calls_count);
2068 /* Dump call graph node NODE to stderr. */
2070 DEBUG_FUNCTION void
2071 cgraph_node::debug (void)
2073 dump (stderr);
2076 /* Dump the callgraph to file F. */
2078 void
2079 cgraph_node::dump_cgraph (FILE *f)
2081 struct cgraph_node *node;
2083 fprintf (f, "callgraph:\n\n");
2084 FOR_EACH_FUNCTION (node)
2085 node->dump (f);
2088 /* Return true when the DECL can possibly be inlined. */
2090 bool
2091 cgraph_function_possibly_inlined_p (tree decl)
2093 if (!cgraph_global_info_ready)
2094 return !DECL_UNINLINABLE (decl);
2095 return DECL_POSSIBLY_INLINED (decl);
2098 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2099 void
2100 cgraph_node::unnest (void)
2102 struct cgraph_node **node2 = &origin->nested;
2103 gcc_assert (origin);
2105 while (*node2 != this)
2106 node2 = &(*node2)->next_nested;
2107 *node2 = next_nested;
2108 origin = NULL;
2111 /* Return function availability. See cgraph.h for description of individual
2112 return values. */
2113 enum availability
2114 cgraph_node::get_availability (void)
2116 enum availability avail;
2117 if (!analyzed)
2118 avail = AVAIL_NOT_AVAILABLE;
2119 else if (local.local)
2120 avail = AVAIL_LOCAL;
2121 else if (alias && weakref)
2122 ultimate_alias_target (&avail);
2123 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2124 avail = AVAIL_INTERPOSABLE;
2125 else if (!externally_visible)
2126 avail = AVAIL_AVAILABLE;
2127 /* Inline functions are safe to be analyzed even if their symbol can
2128 be overwritten at runtime. It is not meaningful to enforce any sane
2129 behaviour on replacing inline function by different body. */
2130 else if (DECL_DECLARED_INLINE_P (decl))
2131 avail = AVAIL_AVAILABLE;
2133 /* If the function can be overwritten, return OVERWRITABLE. Take
2134 care at least of two notable extensions - the COMDAT functions
2135 used to share template instantiations in C++ (this is symmetric
2136 to code cp_cannot_inline_tree_fn and probably shall be shared and
2137 the inlinability hooks completely eliminated).
2139 ??? Does the C++ one definition rule allow us to always return
2140 AVAIL_AVAILABLE here? That would be good reason to preserve this
2141 bit. */
2143 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2144 avail = AVAIL_INTERPOSABLE;
2145 else avail = AVAIL_AVAILABLE;
2147 return avail;
2150 /* Worker for cgraph_node_can_be_local_p. */
2151 static bool
2152 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node, void *)
2154 return !(!node->force_output
2155 && ((DECL_COMDAT (node->decl)
2156 && !node->forced_by_abi
2157 && !node->used_from_object_file_p ()
2158 && !node->same_comdat_group)
2159 || !node->externally_visible));
2162 /* Return true if cgraph_node can be made local for API change.
2163 Extern inline functions and C++ COMDAT functions can be made local
2164 at the expense of possible code size growth if function is used in multiple
2165 compilation units. */
2166 bool
2167 cgraph_node::can_be_local_p (void)
2169 return (!address_taken
2170 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2171 NULL, true));
2174 /* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
2175 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2176 skipped. */
2178 bool
2179 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2180 (cgraph_node *, void *),
2181 void *data,
2182 bool include_overwritable)
2184 struct cgraph_edge *e;
2185 struct ipa_ref *ref;
2187 if (callback (this, data))
2188 return true;
2189 for (e = callers; e; e = e->next_caller)
2190 if (e->caller->thunk.thunk_p
2191 && (include_overwritable
2192 || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2193 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2194 include_overwritable))
2195 return true;
2197 FOR_EACH_ALIAS (this, ref)
2199 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2200 if (include_overwritable
2201 || alias->get_availability () > AVAIL_INTERPOSABLE)
2202 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2203 include_overwritable))
2204 return true;
2206 return false;
2209 /* Call calback on function and aliases associated to the function.
2210 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2211 skipped. */
2213 bool
2214 cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2215 void *),
2216 void *data,
2217 bool include_overwritable)
2219 struct ipa_ref *ref;
2221 if (callback (this, data))
2222 return true;
2224 FOR_EACH_ALIAS (this, ref)
2226 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2227 if (include_overwritable
2228 || alias->get_availability () > AVAIL_INTERPOSABLE)
2229 if (alias->call_for_symbol_and_aliases (callback, data,
2230 include_overwritable))
2231 return true;
2233 return false;
2236 /* Worker to bring NODE local. */
2238 bool
2239 cgraph_node::make_local (struct cgraph_node *node, void *)
2241 gcc_checking_assert (node->can_be_local_p ());
2242 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2244 node->make_decl_local ();
2245 node->set_section (NULL);
2246 node->set_comdat_group (NULL);
2247 node->externally_visible = false;
2248 node->forced_by_abi = false;
2249 node->local.local = true;
2250 node->set_section (NULL);
2251 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2252 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2253 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2254 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2256 return false;
2259 /* Bring cgraph node local. */
2261 void
2262 cgraph_node::make_local (void)
2264 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2267 /* Worker to set nothrow flag. */
2269 static bool
2270 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2272 struct cgraph_edge *e;
2274 TREE_NOTHROW (node->decl) = data != NULL;
2276 if (data != NULL)
2277 for (e = node->callers; e; e = e->next_caller)
2278 e->can_throw_external = false;
2279 return false;
2282 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2283 if any to NOTHROW. */
2285 void
2286 cgraph_node::set_nothrow_flag (bool nothrow)
2288 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2289 (void *)(size_t)nothrow, false);
2292 /* Worker to set const flag. */
2294 static bool
2295 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2297 /* Static constructors and destructors without a side effect can be
2298 optimized out. */
2299 if (data && !((size_t)data & 2))
2301 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2302 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2303 if (DECL_STATIC_DESTRUCTOR (node->decl))
2304 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2306 TREE_READONLY (node->decl) = data != NULL;
2307 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2308 return false;
2311 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2312 if any to READONLY. */
2314 void
2315 cgraph_node::set_const_flag (bool readonly, bool looping)
2317 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2318 (void *)(size_t)(readonly + (int)looping * 2),
2319 false);
2322 /* Worker to set pure flag. */
2324 static bool
2325 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2327 /* Static constructors and destructors without a side effect can be
2328 optimized out. */
2329 if (data && !((size_t)data & 2))
2331 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2332 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2333 if (DECL_STATIC_DESTRUCTOR (node->decl))
2334 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2336 DECL_PURE_P (node->decl) = data != NULL;
2337 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2338 return false;
2341 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2342 if any to PURE. */
2344 void
2345 cgraph_node::set_pure_flag (bool pure, bool looping)
2347 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2348 (void *)(size_t)(pure + (int)looping * 2),
2349 false);
2352 /* Return true when cgraph_node can not return or throw and thus
2353 it is safe to ignore its side effects for IPA analysis. */
2355 bool
2356 cgraph_node::cannot_return_p (void)
2358 int flags = flags_from_decl_or_type (decl);
2359 if (!flag_exceptions)
2360 return (flags & ECF_NORETURN) != 0;
2361 else
2362 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2363 == (ECF_NORETURN | ECF_NOTHROW));
2366 /* Return true when call of E can not lead to return from caller
2367 and thus it is safe to ignore its side effects for IPA analysis
2368 when computing side effects of the caller.
2369 FIXME: We could actually mark all edges that have no reaching
2370 patch to the exit block or throw to get better results. */
2371 bool
2372 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2374 if (e->caller->cannot_return_p ())
2375 return true;
2376 if (e->indirect_unknown_callee)
2378 int flags = e->indirect_info->ecf_flags;
2379 if (!flag_exceptions)
2380 return (flags & ECF_NORETURN) != 0;
2381 else
2382 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2383 == (ECF_NORETURN | ECF_NOTHROW));
2385 else
2386 return e->callee->cannot_return_p ();
2389 /* Return true when function can be removed from callgraph
2390 if all direct calls are eliminated. */
2392 bool
2393 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2395 gcc_assert (!global.inlined_to);
2396 /* Extern inlines can always go, we will use the external definition. */
2397 if (DECL_EXTERNAL (decl))
2398 return true;
2399 /* When function is needed, we can not remove it. */
2400 if (force_output || used_from_other_partition)
2401 return false;
2402 if (DECL_STATIC_CONSTRUCTOR (decl)
2403 || DECL_STATIC_DESTRUCTOR (decl))
2404 return false;
2405 /* Only COMDAT functions can be removed if externally visible. */
2406 if (externally_visible
2407 && (!DECL_COMDAT (decl)
2408 || forced_by_abi
2409 || used_from_object_file_p ()))
2410 return false;
2411 return true;
2414 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2416 static bool
2417 nonremovable_p (struct cgraph_node *node, void *)
2419 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2422 /* Return true when function cgraph_node and its aliases can be removed from
2423 callgraph if all direct calls are eliminated. */
2425 bool
2426 cgraph_node::can_remove_if_no_direct_calls_p (void)
2428 /* Extern inlines can always go, we will use the external definition. */
2429 if (DECL_EXTERNAL (decl))
2430 return true;
2431 if (address_taken)
2432 return false;
2433 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2436 /* Return true when function cgraph_node can be expected to be removed
2437 from program when direct calls in this compilation unit are removed.
2439 As a special case COMDAT functions are
2440 cgraph_can_remove_if_no_direct_calls_p while the are not
2441 cgraph_only_called_directly_p (it is possible they are called from other
2442 unit)
2444 This function behaves as cgraph_only_called_directly_p because eliminating
2445 all uses of COMDAT function does not make it necessarily disappear from
2446 the program unless we are compiling whole program or we do LTO. In this
2447 case we know we win since dynamic linking will not really discard the
2448 linkonce section. */
2450 bool
2451 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2453 gcc_assert (!global.inlined_to);
2455 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2456 NULL, true))
2457 return false;
2458 if (!in_lto_p && !flag_whole_program)
2459 return only_called_directly_p ();
2460 else
2462 if (DECL_EXTERNAL (decl))
2463 return true;
2464 return can_remove_if_no_direct_calls_p ();
2469 /* Worker for cgraph_only_called_directly_p. */
2471 static bool
2472 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *)
2474 return !node->only_called_directly_or_aliased_p ();
2477 /* Return true when function cgraph_node and all its aliases are only called
2478 directly.
2479 i.e. it is not externally visible, address was not taken and
2480 it is not used in any other non-standard way. */
2482 bool
2483 cgraph_node::only_called_directly_p (void)
2485 gcc_assert (ultimate_alias_target () == this);
2486 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2487 NULL, true);
2491 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2493 static bool
2494 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2496 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2497 struct cgraph_edge *cs;
2498 enum availability avail;
2499 node->ultimate_alias_target (&avail);
2501 if (avail > AVAIL_INTERPOSABLE)
2502 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2503 if (!cs->indirect_inlining_edge)
2504 redirect_callers->safe_push (cs);
2505 return false;
2508 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2509 cgraph_node (i.e. are not overwritable). */
2511 vec<cgraph_edge *>
2512 cgraph_node::collect_callers (void)
2514 vec<cgraph_edge *> redirect_callers = vNULL;
2515 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2516 &redirect_callers, false);
2517 return redirect_callers;
2520 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2522 static bool
2523 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2525 bool skipped_thunk = false;
2526 node = node->ultimate_alias_target ();
2527 node2 = node2->ultimate_alias_target ();
2529 /* There are no virtual clones of thunks so check former_clone_of or if we
2530 might have skipped thunks because this adjustments are no longer
2531 necessary. */
2532 while (node->thunk.thunk_p)
2534 if (node2->former_clone_of == node->decl)
2535 return true;
2536 if (!node->thunk.this_adjusting)
2537 return false;
2538 node = node->callees->callee->ultimate_alias_target ();
2539 skipped_thunk = true;
2542 if (skipped_thunk)
2544 if (!node2->clone.args_to_skip
2545 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2546 return false;
2547 if (node2->former_clone_of == node->decl)
2548 return true;
2549 else if (!node2->clone_of)
2550 return false;
2553 while (node != node2 && node2)
2554 node2 = node2->clone_of;
2555 return node2 != NULL;
2558 /* Verify edge E count and frequency. */
2560 static bool
2561 verify_edge_count_and_frequency (struct cgraph_edge *e)
2563 bool error_found = false;
2564 if (e->count < 0)
2566 error ("caller edge count is negative");
2567 error_found = true;
2569 if (e->frequency < 0)
2571 error ("caller edge frequency is negative");
2572 error_found = true;
2574 if (e->frequency > CGRAPH_FREQ_MAX)
2576 error ("caller edge frequency is too large");
2577 error_found = true;
2579 if (gimple_has_body_p (e->caller->decl)
2580 && !e->caller->global.inlined_to
2581 && !e->speculative
2582 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2583 Remove this once edges are actually removed from the function at that time. */
2584 && (e->frequency
2585 || (inline_edge_summary_vec.exists ()
2586 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2587 || !inline_edge_summary (e)->predicate)))
2588 && (e->frequency
2589 != compute_call_stmt_bb_frequency (e->caller->decl,
2590 gimple_bb (e->call_stmt))))
2592 error ("caller edge frequency %i does not match BB frequency %i",
2593 e->frequency,
2594 compute_call_stmt_bb_frequency (e->caller->decl,
2595 gimple_bb (e->call_stmt)));
2596 error_found = true;
2598 return error_found;
2601 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2602 static void
2603 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2605 bool fndecl_was_null = false;
2606 /* debug_gimple_stmt needs correct cfun */
2607 if (cfun != this_cfun)
2608 set_cfun (this_cfun);
2609 /* ...and an actual current_function_decl */
2610 if (!current_function_decl)
2612 current_function_decl = this_cfun->decl;
2613 fndecl_was_null = true;
2615 debug_gimple_stmt (stmt);
2616 if (fndecl_was_null)
2617 current_function_decl = NULL;
2620 /* Verify that call graph edge E corresponds to DECL from the associated
2621 statement. Return true if the verification should fail. */
2623 static bool
2624 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2626 struct cgraph_node *node;
2628 if (!decl || e->callee->global.inlined_to)
2629 return false;
2630 if (cgraph_state == CGRAPH_LTO_STREAMING)
2631 return false;
2632 node = cgraph_node::get (decl);
2634 /* We do not know if a node from a different partition is an alias or what it
2635 aliases and therefore cannot do the former_clone_of check reliably. When
2636 body_removed is set, we have lost all information about what was alias or
2637 thunk of and also cannot proceed. */
2638 if (!node
2639 || node->body_removed
2640 || node->in_other_partition
2641 || e->callee->in_other_partition)
2642 return false;
2644 node = node->ultimate_alias_target ();
2646 /* Optimizers can redirect unreachable calls or calls triggering undefined
2647 behaviour to builtin_unreachable. */
2648 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2649 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2650 return false;
2652 if (e->callee->former_clone_of != node->decl
2653 && (node != e->callee->ultimate_alias_target ())
2654 && !clone_of_p (node, e->callee))
2655 return true;
2656 else
2657 return false;
2660 /* Verify cgraph nodes of given cgraph node. */
2661 DEBUG_FUNCTION void
2662 cgraph_node::verify_node (void)
2664 struct cgraph_edge *e;
2665 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2666 basic_block this_block;
2667 gimple_stmt_iterator gsi;
2668 bool error_found = false;
2670 if (seen_error ())
2671 return;
2673 timevar_push (TV_CGRAPH_VERIFY);
2674 error_found |= verify_base ();
2675 for (e = callees; e; e = e->next_callee)
2676 if (e->aux)
2678 error ("aux field set for edge %s->%s",
2679 identifier_to_locale (e->caller->name ()),
2680 identifier_to_locale (e->callee->name ()));
2681 error_found = true;
2683 if (count < 0)
2685 error ("execution count is negative");
2686 error_found = true;
2688 if (global.inlined_to && same_comdat_group)
2690 error ("inline clone in same comdat group list");
2691 error_found = true;
2693 if (!definition && !in_other_partition && local.local)
2695 error ("local symbols must be defined");
2696 error_found = true;
2698 if (global.inlined_to && externally_visible)
2700 error ("externally visible inline clone");
2701 error_found = true;
2703 if (global.inlined_to && address_taken)
2705 error ("inline clone with address taken");
2706 error_found = true;
2708 if (global.inlined_to && force_output)
2710 error ("inline clone is forced to output");
2711 error_found = true;
2713 for (e = indirect_calls; e; e = e->next_callee)
2715 if (e->aux)
2717 error ("aux field set for indirect edge from %s",
2718 identifier_to_locale (e->caller->name ()));
2719 error_found = true;
2721 if (!e->indirect_unknown_callee
2722 || !e->indirect_info)
2724 error ("An indirect edge from %s is not marked as indirect or has "
2725 "associated indirect_info, the corresponding statement is: ",
2726 identifier_to_locale (e->caller->name ()));
2727 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2728 error_found = true;
2731 bool check_comdat = comdat_local_p ();
2732 for (e = callers; e; e = e->next_caller)
2734 if (verify_edge_count_and_frequency (e))
2735 error_found = true;
2736 if (check_comdat
2737 && !in_same_comdat_group_p (e->caller))
2739 error ("comdat-local function called by %s outside its comdat",
2740 identifier_to_locale (e->caller->name ()));
2741 error_found = true;
2743 if (!e->inline_failed)
2745 if (global.inlined_to
2746 != (e->caller->global.inlined_to
2747 ? e->caller->global.inlined_to : e->caller))
2749 error ("inlined_to pointer is wrong");
2750 error_found = true;
2752 if (callers->next_caller)
2754 error ("multiple inline callers");
2755 error_found = true;
2758 else
2759 if (global.inlined_to)
2761 error ("inlined_to pointer set for noninline callers");
2762 error_found = true;
2765 for (e = indirect_calls; e; e = e->next_callee)
2766 if (verify_edge_count_and_frequency (e))
2767 error_found = true;
2768 if (!callers && global.inlined_to)
2770 error ("inlined_to pointer is set but no predecessors found");
2771 error_found = true;
2773 if (global.inlined_to == this)
2775 error ("inlined_to pointer refers to itself");
2776 error_found = true;
2779 if (clone_of)
2781 struct cgraph_node *n;
2782 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2783 if (n == this)
2784 break;
2785 if (!n)
2787 error ("cgraph_node has wrong clone_of");
2788 error_found = true;
2791 if (clones)
2793 struct cgraph_node *n;
2794 for (n = clones; n; n = n->next_sibling_clone)
2795 if (n->clone_of != this)
2796 break;
2797 if (n)
2799 error ("cgraph_node has wrong clone list");
2800 error_found = true;
2803 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2805 error ("cgraph_node is in clone list but it is not clone");
2806 error_found = true;
2808 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2810 error ("cgraph_node has wrong prev_clone pointer");
2811 error_found = true;
2813 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2815 error ("double linked list of clones corrupted");
2816 error_found = true;
2819 if (analyzed && alias)
2821 bool ref_found = false;
2822 int i;
2823 struct ipa_ref *ref = NULL;
2825 if (callees)
2827 error ("Alias has call edges");
2828 error_found = true;
2830 for (i = 0; iterate_reference (i, ref); i++)
2831 if (ref->use != IPA_REF_ALIAS)
2833 error ("Alias has non-alias reference");
2834 error_found = true;
2836 else if (ref_found)
2838 error ("Alias has more than one alias reference");
2839 error_found = true;
2841 else
2842 ref_found = true;
2843 if (!ref_found)
2845 error ("Analyzed alias has no reference");
2846 error_found = true;
2849 if (analyzed && thunk.thunk_p)
2851 if (!callees)
2853 error ("No edge out of thunk node");
2854 error_found = true;
2856 else if (callees->next_callee)
2858 error ("More than one edge out of thunk node");
2859 error_found = true;
2861 if (gimple_has_body_p (decl))
2863 error ("Thunk is not supposed to have body");
2864 error_found = true;
2867 else if (analyzed && gimple_has_body_p (decl)
2868 && !TREE_ASM_WRITTEN (decl)
2869 && (!DECL_EXTERNAL (decl) || global.inlined_to)
2870 && !flag_wpa)
2872 if (this_cfun->cfg)
2874 pointer_set_t *stmts = pointer_set_create ();
2875 int i;
2876 struct ipa_ref *ref = NULL;
2878 /* Reach the trees by walking over the CFG, and note the
2879 enclosing basic-blocks in the call edges. */
2880 FOR_EACH_BB_FN (this_block, this_cfun)
2882 for (gsi = gsi_start_phis (this_block);
2883 !gsi_end_p (gsi); gsi_next (&gsi))
2884 pointer_set_insert (stmts, gsi_stmt (gsi));
2885 for (gsi = gsi_start_bb (this_block);
2886 !gsi_end_p (gsi);
2887 gsi_next (&gsi))
2889 gimple stmt = gsi_stmt (gsi);
2890 pointer_set_insert (stmts, stmt);
2891 if (is_gimple_call (stmt))
2893 struct cgraph_edge *e = get_edge (stmt);
2894 tree decl = gimple_call_fndecl (stmt);
2895 if (e)
2897 if (e->aux)
2899 error ("shared call_stmt:");
2900 cgraph_debug_gimple_stmt (this_cfun, stmt);
2901 error_found = true;
2903 if (!e->indirect_unknown_callee)
2905 if (verify_edge_corresponds_to_fndecl (e, decl))
2907 error ("edge points to wrong declaration:");
2908 debug_tree (e->callee->decl);
2909 fprintf (stderr," Instead of:");
2910 debug_tree (decl);
2911 error_found = true;
2914 else if (decl)
2916 error ("an indirect edge with unknown callee "
2917 "corresponding to a call_stmt with "
2918 "a known declaration:");
2919 error_found = true;
2920 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2922 e->aux = (void *)1;
2924 else if (decl)
2926 error ("missing callgraph edge for call stmt:");
2927 cgraph_debug_gimple_stmt (this_cfun, stmt);
2928 error_found = true;
2933 for (i = 0; iterate_reference (i, ref); i++)
2934 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2936 error ("reference to dead statement");
2937 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2938 error_found = true;
2940 pointer_set_destroy (stmts);
2942 else
2943 /* No CFG available?! */
2944 gcc_unreachable ();
2946 for (e = callees; e; e = e->next_callee)
2948 if (!e->aux)
2950 error ("edge %s->%s has no corresponding call_stmt",
2951 identifier_to_locale (e->caller->name ()),
2952 identifier_to_locale (e->callee->name ()));
2953 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2954 error_found = true;
2956 e->aux = 0;
2958 for (e = indirect_calls; e; e = e->next_callee)
2960 if (!e->aux && !e->speculative)
2962 error ("an indirect edge from %s has no corresponding call_stmt",
2963 identifier_to_locale (e->caller->name ()));
2964 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2965 error_found = true;
2967 e->aux = 0;
2970 if (error_found)
2972 dump (stderr);
2973 internal_error ("verify_cgraph_node failed");
2975 timevar_pop (TV_CGRAPH_VERIFY);
2978 /* Verify whole cgraph structure. */
2979 DEBUG_FUNCTION void
2980 cgraph_node::verify_cgraph_nodes (void)
2982 struct cgraph_node *node;
2984 if (seen_error ())
2985 return;
2987 FOR_EACH_FUNCTION (node)
2988 node->verify ();
2991 /* Walk the alias chain to return the function cgraph_node is alias of.
2992 Walk through thunk, too.
2993 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2995 cgraph_node *
2996 cgraph_node::function_symbol (enum availability *availability)
2998 cgraph_node *node = NULL;
3002 node = ultimate_alias_target (availability);
3003 if (node->thunk.thunk_p)
3005 node = node->callees->callee;
3006 if (availability)
3008 enum availability a;
3009 a = node->get_availability ();
3010 if (a < *availability)
3011 *availability = a;
3013 node = node->ultimate_alias_target (availability);
3015 } while (node && node->thunk.thunk_p);
3016 return node;
3019 /* When doing LTO, read cgraph_node's body from disk if it is not already
3020 present. */
3022 bool
3023 cgraph_node::get_body (void)
3025 struct lto_file_decl_data *file_data;
3026 const char *data, *name;
3027 size_t len;
3028 tree decl = this->decl;
3030 if (DECL_RESULT (decl))
3031 return false;
3033 gcc_assert (in_lto_p);
3035 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3037 file_data = lto_file_data;
3038 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3040 /* We may have renamed the declaration, e.g., a static function. */
3041 name = lto_get_decl_name_mapping (file_data, name);
3043 data = lto_get_section_data (file_data, LTO_section_function_body,
3044 name, &len);
3045 if (!data)
3047 debug ();
3048 fatal_error ("%s: section %s is missing",
3049 file_data->file_name,
3050 name);
3053 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3055 lto_input_function_body (file_data, this, data);
3056 lto_stats.num_function_bodies++;
3057 lto_free_section_data (file_data, LTO_section_function_body, name,
3058 data, len);
3059 lto_free_function_in_decl_state_for_node (this);
3061 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3063 return true;
3066 /* Verify if the type of the argument matches that of the function
3067 declaration. If we cannot verify this or there is a mismatch,
3068 return false. */
3070 static bool
3071 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3073 tree parms, p;
3074 unsigned int i, nargs;
3076 /* Calls to internal functions always match their signature. */
3077 if (gimple_call_internal_p (stmt))
3078 return true;
3080 nargs = gimple_call_num_args (stmt);
3082 /* Get argument types for verification. */
3083 if (fndecl)
3084 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3085 else
3086 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3088 /* Verify if the type of the argument matches that of the function
3089 declaration. If we cannot verify this or there is a mismatch,
3090 return false. */
3091 if (fndecl && DECL_ARGUMENTS (fndecl))
3093 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3094 i < nargs;
3095 i++, p = DECL_CHAIN (p))
3097 tree arg;
3098 /* We cannot distinguish a varargs function from the case
3099 of excess parameters, still deferring the inlining decision
3100 to the callee is possible. */
3101 if (!p)
3102 break;
3103 arg = gimple_call_arg (stmt, i);
3104 if (p == error_mark_node
3105 || DECL_ARG_TYPE (p) == error_mark_node
3106 || arg == error_mark_node
3107 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3108 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3109 return false;
3111 if (args_count_match && p)
3112 return false;
3114 else if (parms)
3116 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3118 tree arg;
3119 /* If this is a varargs function defer inlining decision
3120 to callee. */
3121 if (!p)
3122 break;
3123 arg = gimple_call_arg (stmt, i);
3124 if (TREE_VALUE (p) == error_mark_node
3125 || arg == error_mark_node
3126 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3127 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3128 && !fold_convertible_p (TREE_VALUE (p), arg)))
3129 return false;
3132 else
3134 if (nargs != 0)
3135 return false;
3137 return true;
3140 /* Verify if the type of the argument and lhs of CALL_STMT matches
3141 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3142 true, the arg count needs to be the same.
3143 If we cannot verify this or there is a mismatch, return false. */
3145 bool
3146 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3147 bool args_count_match)
3149 tree lhs;
3151 if ((DECL_RESULT (callee)
3152 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3153 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3154 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3155 TREE_TYPE (lhs))
3156 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3157 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3158 return false;
3159 return true;
3162 #include "gt-cgraph.h"