remove pointer-set.[ch]
[official-gcc.git] / gcc / cgraph.c
blob5a0b9033c25e69e4b5f702bc4b34190fc70d6d40
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 "hash-set.h"
38 #include "toplev.h"
39 #include "flags.h"
40 #include "debug.h"
41 #include "target.h"
42 #include "cgraph.h"
43 #include "intl.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "tree-eh.h"
47 #include "gimple-expr.h"
48 #include "gimple.h"
49 #include "gimple-iterator.h"
50 #include "timevar.h"
51 #include "dumpfile.h"
52 #include "gimple-ssa.h"
53 #include "cgraph.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa.h"
56 #include "value-prof.h"
57 #include "except.h"
58 #include "diagnostic-core.h"
59 #include "rtl.h"
60 #include "ipa-utils.h"
61 #include "lto-streamer.h"
62 #include "ipa-inline.h"
63 #include "cfgloop.h"
64 #include "gimple-pretty-print.h"
65 #include "expr.h"
66 #include "tree-dfa.h"
68 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
69 #include "tree-pass.h"
71 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
72 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
74 /* Queue of cgraph nodes scheduled to be lowered. */
75 symtab_node *x_cgraph_nodes_queue;
76 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
78 /* Number of nodes in existence. */
79 int cgraph_n_nodes;
81 /* Maximal uid used in cgraph nodes. */
82 int cgraph_max_uid;
84 /* Maximal uid used in cgraph edges. */
85 int cgraph_edge_max_uid;
87 /* Set when whole unit has been analyzed so we can access global info. */
88 bool cgraph_global_info_ready = false;
90 /* What state callgraph is in right now. */
91 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
93 /* Set when the cgraph is fully build and the basic flags are computed. */
94 bool cgraph_function_flags_ready = false;
96 /* List of hooks triggered on cgraph_edge events. */
97 struct cgraph_edge_hook_list {
98 cgraph_edge_hook hook;
99 void *data;
100 struct cgraph_edge_hook_list *next;
103 /* List of hooks triggered on cgraph_node events. */
104 struct cgraph_node_hook_list {
105 cgraph_node_hook hook;
106 void *data;
107 struct cgraph_node_hook_list *next;
110 /* List of hooks triggered on events involving two cgraph_edges. */
111 struct cgraph_2edge_hook_list {
112 cgraph_2edge_hook hook;
113 void *data;
114 struct cgraph_2edge_hook_list *next;
117 /* List of hooks triggered on events involving two cgraph_nodes. */
118 struct cgraph_2node_hook_list {
119 cgraph_2node_hook hook;
120 void *data;
121 struct cgraph_2node_hook_list *next;
124 /* List of hooks triggered when an edge is removed. */
125 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
126 /* List of hooks triggered when a node is removed. */
127 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
128 /* List of hooks triggered when an edge is duplicated. */
129 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
130 /* List of hooks triggered when a node is duplicated. */
131 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
132 /* List of hooks triggered when an function is inserted. */
133 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
135 /* Head of a linked list of unused (freed) call graph nodes.
136 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
137 static GTY(()) struct cgraph_node *free_nodes;
138 /* Head of a linked list of unused (freed) call graph edges.
139 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
140 static GTY(()) struct cgraph_edge *free_edges;
142 /* Did procss_same_body_aliases run? */
143 bool cpp_implicit_aliases_done;
145 /* Map a cgraph_node to cgraph_function_version_info using this htab.
146 The cgraph_function_version_info has a THIS_NODE field that is the
147 corresponding cgraph_node.. */
149 static GTY((param_is (struct cgraph_function_version_info))) htab_t
150 cgraph_fnver_htab = NULL;
152 /* Hash function for cgraph_fnver_htab. */
153 static hashval_t
154 cgraph_fnver_htab_hash (const void *ptr)
156 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
157 return (hashval_t)(uid);
160 /* eq function for cgraph_fnver_htab. */
161 static int
162 cgraph_fnver_htab_eq (const void *p1, const void *p2)
164 const struct cgraph_function_version_info *n1
165 = (const struct cgraph_function_version_info *)p1;
166 const struct cgraph_function_version_info *n2
167 = (const struct cgraph_function_version_info *)p2;
169 return n1->this_node->uid == n2->this_node->uid;
172 /* Mark as GC root all allocated nodes. */
173 static GTY(()) struct cgraph_function_version_info *
174 version_info_node = NULL;
176 /* Get the cgraph_function_version_info node corresponding to node. */
177 struct cgraph_function_version_info *
178 cgraph_node::function_version (void)
180 struct cgraph_function_version_info *ret;
181 struct cgraph_function_version_info key;
182 key.this_node = this;
184 if (cgraph_fnver_htab == NULL)
185 return NULL;
187 ret = (struct cgraph_function_version_info *)
188 htab_find (cgraph_fnver_htab, &key);
190 return ret;
193 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
194 corresponding to cgraph_node NODE. */
195 struct cgraph_function_version_info *
196 cgraph_node::insert_new_function_version (void)
198 void **slot;
200 version_info_node = NULL;
201 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
202 version_info_node->this_node = this;
204 if (cgraph_fnver_htab == NULL)
205 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
206 cgraph_fnver_htab_eq, NULL);
208 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
209 gcc_assert (slot != NULL);
210 *slot = version_info_node;
211 return version_info_node;
214 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
215 DECL is a duplicate declaration. */
216 void
217 cgraph_node::delete_function_version (tree decl)
219 struct cgraph_node *decl_node = cgraph_node::get (decl);
220 struct cgraph_function_version_info *decl_v = NULL;
222 if (decl_node == NULL)
223 return;
225 decl_v = decl_node->function_version ();
227 if (decl_v == NULL)
228 return;
230 if (decl_v->prev != NULL)
231 decl_v->prev->next = decl_v->next;
233 if (decl_v->next != NULL)
234 decl_v->next->prev = decl_v->prev;
236 if (cgraph_fnver_htab != NULL)
237 htab_remove_elt (cgraph_fnver_htab, decl_v);
239 decl_node->remove ();
242 /* Record that DECL1 and DECL2 are semantically identical function
243 versions. */
244 void
245 cgraph_node::record_function_versions (tree decl1, tree decl2)
247 struct cgraph_node *decl1_node = cgraph_node::get_create (decl1);
248 struct cgraph_node *decl2_node = cgraph_node::get_create (decl2);
249 struct cgraph_function_version_info *decl1_v = NULL;
250 struct cgraph_function_version_info *decl2_v = NULL;
251 struct cgraph_function_version_info *before;
252 struct cgraph_function_version_info *after;
254 gcc_assert (decl1_node != NULL && decl2_node != NULL);
255 decl1_v = decl1_node->function_version ();
256 decl2_v = decl2_node->function_version ();
258 if (decl1_v != NULL && decl2_v != NULL)
259 return;
261 if (decl1_v == NULL)
262 decl1_v = decl1_node->insert_new_function_version ();
264 if (decl2_v == NULL)
265 decl2_v = decl2_node->insert_new_function_version ();
267 /* Chain decl2_v and decl1_v. All semantically identical versions
268 will be chained together. */
270 before = decl1_v;
271 after = decl2_v;
273 while (before->next != NULL)
274 before = before->next;
276 while (after->prev != NULL)
277 after= after->prev;
279 before->next = after;
280 after->prev = before;
283 /* Macros to access the next item in the list of free cgraph nodes and
284 edges. */
285 #define NEXT_FREE_NODE(NODE) dyn_cast<cgraph_node *> ((NODE)->next)
286 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
287 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
289 /* Register HOOK to be called with DATA on each removed edge. */
290 struct cgraph_edge_hook_list *
291 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
293 struct cgraph_edge_hook_list *entry;
294 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
296 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
297 entry->hook = hook;
298 entry->data = data;
299 entry->next = NULL;
300 while (*ptr)
301 ptr = &(*ptr)->next;
302 *ptr = entry;
303 return entry;
306 /* Remove ENTRY from the list of hooks called on removing edges. */
307 void
308 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
310 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
312 while (*ptr != entry)
313 ptr = &(*ptr)->next;
314 *ptr = entry->next;
315 free (entry);
318 /* Call all edge removal hooks. */
319 static void
320 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
322 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
323 while (entry)
325 entry->hook (e, entry->data);
326 entry = entry->next;
330 /* Register HOOK to be called with DATA on each removed node. */
331 struct cgraph_node_hook_list *
332 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
334 struct cgraph_node_hook_list *entry;
335 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
337 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
338 entry->hook = hook;
339 entry->data = data;
340 entry->next = NULL;
341 while (*ptr)
342 ptr = &(*ptr)->next;
343 *ptr = entry;
344 return entry;
347 /* Remove ENTRY from the list of hooks called on removing nodes. */
348 void
349 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
351 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
353 while (*ptr != entry)
354 ptr = &(*ptr)->next;
355 *ptr = entry->next;
356 free (entry);
359 /* Call all node removal hooks. */
360 static void
361 cgraph_call_node_removal_hooks (struct cgraph_node *node)
363 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
364 while (entry)
366 entry->hook (node, entry->data);
367 entry = entry->next;
371 /* Register HOOK to be called with DATA on each inserted node. */
372 struct cgraph_node_hook_list *
373 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
375 struct cgraph_node_hook_list *entry;
376 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
378 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
379 entry->hook = hook;
380 entry->data = data;
381 entry->next = NULL;
382 while (*ptr)
383 ptr = &(*ptr)->next;
384 *ptr = entry;
385 return entry;
388 /* Remove ENTRY from the list of hooks called on inserted nodes. */
389 void
390 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
392 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
394 while (*ptr != entry)
395 ptr = &(*ptr)->next;
396 *ptr = entry->next;
397 free (entry);
400 /* Call all node insertion hooks. */
401 void
402 cgraph_node::call_function_insertion_hooks (void)
404 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
405 while (entry)
407 entry->hook (this, entry->data);
408 entry = entry->next;
412 /* Register HOOK to be called with DATA on each duplicated edge. */
413 struct cgraph_2edge_hook_list *
414 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
416 struct cgraph_2edge_hook_list *entry;
417 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
419 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
420 entry->hook = hook;
421 entry->data = data;
422 entry->next = NULL;
423 while (*ptr)
424 ptr = &(*ptr)->next;
425 *ptr = entry;
426 return entry;
429 /* Remove ENTRY from the list of hooks called on duplicating edges. */
430 void
431 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
433 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
435 while (*ptr != entry)
436 ptr = &(*ptr)->next;
437 *ptr = entry->next;
438 free (entry);
441 /* Call all edge duplication hooks. */
442 void
443 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
444 struct cgraph_edge *cs2)
446 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
447 while (entry)
449 entry->hook (cs1, cs2, entry->data);
450 entry = entry->next;
454 /* Register HOOK to be called with DATA on each duplicated node. */
455 struct cgraph_2node_hook_list *
456 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
458 struct cgraph_2node_hook_list *entry;
459 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
461 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
462 entry->hook = hook;
463 entry->data = data;
464 entry->next = NULL;
465 while (*ptr)
466 ptr = &(*ptr)->next;
467 *ptr = entry;
468 return entry;
471 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
472 void
473 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
475 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
477 while (*ptr != entry)
478 ptr = &(*ptr)->next;
479 *ptr = entry->next;
480 free (entry);
483 /* Call all node duplication hooks. */
484 void
485 cgraph_node::call_duplication_hooks (struct cgraph_node *node2)
487 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
488 while (entry)
490 entry->hook (this, node2, entry->data);
491 entry = entry->next;
495 /* Allocate new callgraph node. */
497 static inline struct cgraph_node *
498 cgraph_allocate_node (void)
500 struct cgraph_node *node;
502 if (free_nodes)
504 node = free_nodes;
505 free_nodes = NEXT_FREE_NODE (node);
507 else
509 node = ggc_cleared_alloc<cgraph_node> ();
510 node->uid = cgraph_max_uid++;
513 return node;
516 /* Allocate new callgraph node and insert it into basic data structures. */
518 cgraph_node *
519 cgraph_node::create_empty (void)
521 struct cgraph_node *node = cgraph_allocate_node ();
523 node->type = SYMTAB_FUNCTION;
524 node->frequency = NODE_FREQUENCY_NORMAL;
525 node->count_materialization_scale = REG_BR_PROB_BASE;
526 cgraph_n_nodes++;
527 return node;
530 /* Return cgraph node assigned to DECL. Create new one when needed. */
532 cgraph_node *
533 cgraph_node::create (tree decl)
535 struct cgraph_node *node = cgraph_node::create_empty ();
536 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
538 node->decl = decl;
539 node->register_symbol ();
541 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
543 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
544 node->next_nested = node->origin->nested;
545 node->origin->nested = node;
547 return node;
550 /* Try to find a call graph node for declaration DECL and if it does not exist
551 or if it corresponds to an inline clone, create a new one. */
553 cgraph_node *
554 cgraph_node::get_create (tree decl)
556 struct cgraph_node *first_clone = cgraph_node::get (decl);
558 if (first_clone && !first_clone->global.inlined_to)
559 return first_clone;
561 struct cgraph_node *node = cgraph_node::create (decl);
562 if (first_clone)
564 first_clone->clone_of = node;
565 node->clones = first_clone;
566 symtab_prevail_in_asm_name_hash (node);
567 node->decl->decl_with_vis.symtab_node = node;
568 if (dump_file)
569 fprintf (dump_file, "Introduced new external node "
570 "(%s/%i) and turned into root of the clone tree.\n",
571 xstrdup (node->name ()), node->order);
573 else if (dump_file)
574 fprintf (dump_file, "Introduced new external node "
575 "(%s/%i).\n", xstrdup (node->name ()),
576 node->order);
577 return node;
580 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
581 the function body is associated with (not necessarily cgraph_node (DECL). */
583 cgraph_node *
584 cgraph_node::create_alias (tree alias, tree target)
586 cgraph_node *alias_node;
588 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
589 || TREE_CODE (target) == IDENTIFIER_NODE);
590 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
591 alias_node = cgraph_node::get_create (alias);
592 gcc_assert (!alias_node->definition);
593 alias_node->alias_target = target;
594 alias_node->definition = true;
595 alias_node->alias = true;
596 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
597 alias_node->weakref = true;
598 return alias_node;
601 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
602 and NULL otherwise.
603 Same body aliases are output whenever the body of DECL is output,
604 and cgraph_node::get (ALIAS) transparently returns
605 cgraph_node::get (DECL). */
607 struct cgraph_node *
608 cgraph_node::create_same_body_alias (tree alias, tree decl)
610 struct cgraph_node *n;
611 #ifndef ASM_OUTPUT_DEF
612 /* If aliases aren't supported by the assembler, fail. */
613 return NULL;
614 #endif
615 /* Langhooks can create same body aliases of symbols not defined.
616 Those are useless. Drop them on the floor. */
617 if (cgraph_global_info_ready)
618 return NULL;
620 n = cgraph_node::create_alias (alias, decl);
621 n->cpp_implicit_alias = true;
622 if (cpp_implicit_aliases_done)
623 n->resolve_alias (cgraph_node::get (decl));
624 return n;
627 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
628 aliases DECL with an adjustments made into the first parameter.
629 See comments in thunk_adjust for detail on the parameters. */
631 struct cgraph_node *
632 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
633 HOST_WIDE_INT fixed_offset,
634 HOST_WIDE_INT virtual_value,
635 tree virtual_offset,
636 tree real_alias)
638 struct cgraph_node *node;
640 node = cgraph_node::get (alias);
641 if (node)
642 node->reset ();
643 else
644 node = cgraph_node::create (alias);
645 gcc_checking_assert (!virtual_offset
646 || wi::eq_p (virtual_offset, virtual_value));
647 node->thunk.fixed_offset = fixed_offset;
648 node->thunk.this_adjusting = this_adjusting;
649 node->thunk.virtual_value = virtual_value;
650 node->thunk.virtual_offset_p = virtual_offset != NULL;
651 node->thunk.alias = real_alias;
652 node->thunk.thunk_p = true;
653 node->definition = true;
655 return node;
658 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
659 Return NULL if there's no such node. */
661 cgraph_node *
662 cgraph_node::get_for_asmname (tree asmname)
664 /* We do not want to look at inline clones. */
665 for (symtab_node *node = symtab_node_for_asm (asmname);
666 node;
667 node = node->next_sharing_asm_name)
669 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
670 if (cn && !cn->global.inlined_to)
671 return cn;
673 return NULL;
676 /* Returns a hash value for X (which really is a cgraph_edge). */
678 static hashval_t
679 edge_hash (const void *x)
681 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
684 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
686 static int
687 edge_eq (const void *x, const void *y)
689 return ((const struct cgraph_edge *) x)->call_stmt == y;
692 /* Add call graph edge E to call site hash of its caller. */
694 static inline void
695 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
697 void **slot;
698 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
699 e->call_stmt,
700 htab_hash_pointer (e->call_stmt),
701 INSERT);
702 *slot = e;
705 /* Add call graph edge E to call site hash of its caller. */
707 static inline void
708 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
710 void **slot;
711 /* There are two speculative edges for every statement (one direct,
712 one indirect); always hash the direct one. */
713 if (e->speculative && e->indirect_unknown_callee)
714 return;
715 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
716 e->call_stmt,
717 htab_hash_pointer (e->call_stmt),
718 INSERT);
719 if (*slot)
721 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
722 if (e->callee)
723 *slot = e;
724 return;
726 gcc_assert (!*slot || e->speculative);
727 *slot = e;
730 /* Return the callgraph edge representing the GIMPLE_CALL statement
731 CALL_STMT. */
733 cgraph_edge *
734 cgraph_node::get_edge (gimple call_stmt)
736 struct cgraph_edge *e, *e2;
737 int n = 0;
739 if (call_site_hash)
740 return (struct cgraph_edge *)
741 htab_find_with_hash (call_site_hash, call_stmt,
742 htab_hash_pointer (call_stmt));
744 /* This loop may turn out to be performance problem. In such case adding
745 hashtables into call nodes with very many edges is probably best
746 solution. It is not good idea to add pointer into CALL_EXPR itself
747 because we want to make possible having multiple cgraph nodes representing
748 different clones of the same body before the body is actually cloned. */
749 for (e = callees; e; e = e->next_callee)
751 if (e->call_stmt == call_stmt)
752 break;
753 n++;
756 if (!e)
757 for (e = indirect_calls; e; e = e->next_callee)
759 if (e->call_stmt == call_stmt)
760 break;
761 n++;
764 if (n > 100)
766 call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
767 for (e2 = callees; e2; e2 = e2->next_callee)
768 cgraph_add_edge_to_call_site_hash (e2);
769 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
770 cgraph_add_edge_to_call_site_hash (e2);
773 return e;
777 /* Change field call_stmt of edge E to NEW_STMT.
778 If UPDATE_SPECULATIVE and E is any component of speculative
779 edge, then update all components. */
781 void
782 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
783 bool update_speculative)
785 tree decl;
787 /* Speculative edges has three component, update all of them
788 when asked to. */
789 if (update_speculative && e->speculative)
791 struct cgraph_edge *direct, *indirect;
792 struct ipa_ref *ref;
794 cgraph_speculative_call_info (e, direct, indirect, ref);
795 cgraph_set_call_stmt (direct, new_stmt, false);
796 cgraph_set_call_stmt (indirect, new_stmt, false);
797 ref->stmt = new_stmt;
798 return;
801 /* Only direct speculative edges go to call_site_hash. */
802 if (e->caller->call_site_hash
803 && (!e->speculative || !e->indirect_unknown_callee))
805 htab_remove_elt_with_hash (e->caller->call_site_hash,
806 e->call_stmt,
807 htab_hash_pointer (e->call_stmt));
810 e->call_stmt = new_stmt;
811 if (e->indirect_unknown_callee
812 && (decl = gimple_call_fndecl (new_stmt)))
814 /* Constant propagation (and possibly also inlining?) can turn an
815 indirect call into a direct one. */
816 struct cgraph_node *new_callee = cgraph_node::get (decl);
818 gcc_checking_assert (new_callee);
819 e = cgraph_make_edge_direct (e, new_callee);
822 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
823 e->can_throw_external = stmt_can_throw_external (new_stmt);
824 pop_cfun ();
825 if (e->caller->call_site_hash)
826 cgraph_add_edge_to_call_site_hash (e);
829 /* Allocate a cgraph_edge structure and fill it with data according to the
830 parameters of which only CALLEE can be NULL (when creating an indirect call
831 edge). */
833 cgraph_edge *
834 cgraph_node::create_edge (cgraph_node *caller, cgraph_node *callee,
835 gimple call_stmt, gcov_type count, int freq,
836 bool indir_unknown_callee)
838 cgraph_edge *edge;
840 /* LTO does not actually have access to the call_stmt since these
841 have not been loaded yet. */
842 if (call_stmt)
844 /* This is a rather expensive check possibly triggering
845 construction of call stmt hashtable. */
846 #ifdef ENABLE_CHECKING
847 struct cgraph_edge *e;
848 gcc_checking_assert (
849 !(e = caller->get_edge (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<struct 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 a given function to CALLEE in the cgraph. */
906 struct cgraph_edge *
907 cgraph_node::create_edge (struct cgraph_node *callee,
908 gimple call_stmt, gcov_type count, int freq)
910 cgraph_edge *edge = cgraph_node::create_edge (this, 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 = callees;
919 if (callees)
920 callees->prev_callee = edge;
921 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_cleared_alloc<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_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
945 gcov_type count, int freq)
947 struct cgraph_edge *edge = cgraph_node::create_edge (this, NULL, call_stmt,
948 count, freq, true);
949 tree target;
951 initialize_inline_failed (edge);
953 edge->indirect_info = cgraph_allocate_init_indirect_info ();
954 edge->indirect_info->ecf_flags = ecf_flags;
956 /* Record polymorphic call info. */
957 if (call_stmt
958 && (target = gimple_call_fn (call_stmt))
959 && virtual_method_call_p (target))
961 tree otr_type;
962 HOST_WIDE_INT otr_token;
963 ipa_polymorphic_call_context context;
965 get_polymorphic_call_info (decl,
966 target,
967 &otr_type, &otr_token,
968 &context, call_stmt);
970 /* Only record types can have virtual calls. */
971 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
972 edge->indirect_info->polymorphic = true;
973 edge->indirect_info->param_index = -1;
974 edge->indirect_info->otr_token = otr_token;
975 edge->indirect_info->otr_type = otr_type;
976 edge->indirect_info->outer_type = context.outer_type;
977 edge->indirect_info->speculative_outer_type
978 = context.speculative_outer_type;
979 edge->indirect_info->offset = context.offset;
980 edge->indirect_info->speculative_offset = context.speculative_offset;
981 edge->indirect_info->maybe_in_construction
982 = context.maybe_in_construction;
983 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
984 edge->indirect_info->speculative_maybe_derived_type
985 = context.speculative_maybe_derived_type;
988 edge->next_callee = indirect_calls;
989 if (indirect_calls)
990 indirect_calls->prev_callee = edge;
991 indirect_calls = edge;
993 return edge;
996 /* Remove the edge E from the list of the callers of the callee. */
998 static inline void
999 cgraph_edge_remove_callee (struct cgraph_edge *e)
1001 gcc_assert (!e->indirect_unknown_callee);
1002 if (e->prev_caller)
1003 e->prev_caller->next_caller = e->next_caller;
1004 if (e->next_caller)
1005 e->next_caller->prev_caller = e->prev_caller;
1006 if (!e->prev_caller)
1007 e->callee->callers = e->next_caller;
1010 /* Remove the edge E from the list of the callees of the caller. */
1012 static inline void
1013 cgraph_edge_remove_caller (struct cgraph_edge *e)
1015 if (e->prev_callee)
1016 e->prev_callee->next_callee = e->next_callee;
1017 if (e->next_callee)
1018 e->next_callee->prev_callee = e->prev_callee;
1019 if (!e->prev_callee)
1021 if (e->indirect_unknown_callee)
1022 e->caller->indirect_calls = e->next_callee;
1023 else
1024 e->caller->callees = e->next_callee;
1026 if (e->caller->call_site_hash)
1027 htab_remove_elt_with_hash (e->caller->call_site_hash,
1028 e->call_stmt,
1029 htab_hash_pointer (e->call_stmt));
1032 /* Put the edge onto the free list. */
1034 static void
1035 cgraph_free_edge (struct cgraph_edge *e)
1037 int uid = e->uid;
1039 if (e->indirect_info)
1040 ggc_free (e->indirect_info);
1042 /* Clear out the edge so we do not dangle pointers. */
1043 memset (e, 0, sizeof (*e));
1044 e->uid = uid;
1045 NEXT_FREE_EDGE (e) = free_edges;
1046 free_edges = e;
1049 /* Remove the edge E in the cgraph. */
1051 void
1052 cgraph_remove_edge (struct cgraph_edge *e)
1054 /* Call all edge removal hooks. */
1055 cgraph_call_edge_removal_hooks (e);
1057 if (!e->indirect_unknown_callee)
1058 /* Remove from callers list of the callee. */
1059 cgraph_edge_remove_callee (e);
1061 /* Remove from callees list of the callers. */
1062 cgraph_edge_remove_caller (e);
1064 /* Put the edge onto the free list. */
1065 cgraph_free_edge (e);
1068 /* Set callee of call graph edge E and add it to the corresponding set of
1069 callers. */
1071 static void
1072 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1074 e->prev_caller = NULL;
1075 if (n->callers)
1076 n->callers->prev_caller = e;
1077 e->next_caller = n->callers;
1078 n->callers = e;
1079 e->callee = n;
1082 /* Turn edge E into speculative call calling N2. Update
1083 the profile so the direct call is taken COUNT times
1084 with FREQUENCY.
1086 At clone materialization time, the indirect call E will
1087 be expanded as:
1089 if (call_dest == N2)
1090 n2 ();
1091 else
1092 call call_dest
1094 At this time the function just creates the direct call,
1095 the referencd representing the if conditional and attaches
1096 them all to the orginal indirect call statement.
1098 Return direct edge created. */
1100 struct cgraph_edge *
1101 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1102 struct cgraph_node *n2,
1103 gcov_type direct_count,
1104 int direct_frequency)
1106 struct cgraph_node *n = e->caller;
1107 struct ipa_ref *ref = NULL;
1108 struct cgraph_edge *e2;
1110 if (dump_file)
1112 fprintf (dump_file, "Indirect call -> speculative call"
1113 " %s/%i => %s/%i\n",
1114 xstrdup (n->name ()), n->order,
1115 xstrdup (n2->name ()), n2->order);
1117 e->speculative = true;
1118 e2 = n->create_edge (n2, e->call_stmt, direct_count, direct_frequency);
1119 initialize_inline_failed (e2);
1120 e2->speculative = true;
1121 if (TREE_NOTHROW (n2->decl))
1122 e2->can_throw_external = false;
1123 else
1124 e2->can_throw_external = e->can_throw_external;
1125 e2->lto_stmt_uid = e->lto_stmt_uid;
1126 e->count -= e2->count;
1127 e->frequency -= e2->frequency;
1128 cgraph_call_edge_duplication_hooks (e, e2);
1129 ref = n->add_reference (n2, IPA_REF_ADDR, e->call_stmt);
1130 ref->lto_stmt_uid = e->lto_stmt_uid;
1131 ref->speculative = e->speculative;
1132 n2->mark_address_taken ();
1133 return e2;
1136 /* Speculative call consist of three components:
1137 1) an indirect edge representing the original call
1138 2) an direct edge representing the new call
1139 3) ADDR_EXPR reference representing the speculative check.
1140 All three components are attached to single statement (the indirect
1141 call) and if one of them exists, all of them must exist.
1143 Given speculative call edge E, return all three components.
1146 void
1147 cgraph_speculative_call_info (struct cgraph_edge *e,
1148 struct cgraph_edge *&direct,
1149 struct cgraph_edge *&indirect,
1150 struct ipa_ref *&reference)
1152 struct ipa_ref *ref;
1153 int i;
1154 struct cgraph_edge *e2;
1156 if (!e->indirect_unknown_callee)
1157 for (e2 = e->caller->indirect_calls;
1158 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1159 e2 = e2->next_callee)
1161 else
1163 e2 = e;
1164 /* We can take advantage of the call stmt hash. */
1165 if (e2->call_stmt)
1167 e = e->caller->get_edge (e2->call_stmt);
1168 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1170 else
1171 for (e = e->caller->callees;
1172 e2->call_stmt != e->call_stmt
1173 || e2->lto_stmt_uid != e->lto_stmt_uid;
1174 e = e->next_callee)
1177 gcc_assert (e->speculative && e2->speculative);
1178 direct = e;
1179 indirect = e2;
1181 reference = NULL;
1182 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1183 if (ref->speculative
1184 && ((ref->stmt && ref->stmt == e->call_stmt)
1185 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1187 reference = ref;
1188 break;
1191 /* Speculative edge always consist of all three components - direct edge,
1192 indirect and reference. */
1194 gcc_assert (e && e2 && ref);
1197 /* Redirect callee of E to N. The function does not update underlying
1198 call expression. */
1200 void
1201 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1203 /* Remove from callers list of the current callee. */
1204 cgraph_edge_remove_callee (e);
1206 /* Insert to callers list of the new callee. */
1207 cgraph_set_edge_callee (e, n);
1210 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1211 Remove the speculative call sequence and return edge representing the call.
1212 It is up to caller to redirect the call as appropriate. */
1214 struct cgraph_edge *
1215 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1217 struct cgraph_edge *e2;
1218 struct ipa_ref *ref;
1220 gcc_assert (edge->speculative);
1221 cgraph_speculative_call_info (edge, e2, edge, ref);
1222 if (!callee_decl
1223 || !ref->referred->semantically_equivalent_p
1224 (symtab_node::get (callee_decl)))
1226 if (dump_file)
1228 if (callee_decl)
1230 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1231 "turned out to have contradicting known target ",
1232 xstrdup (edge->caller->name ()), edge->caller->order,
1233 xstrdup (e2->callee->name ()), e2->callee->order);
1234 print_generic_expr (dump_file, callee_decl, 0);
1235 fprintf (dump_file, "\n");
1237 else
1239 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1240 xstrdup (edge->caller->name ()), edge->caller->order,
1241 xstrdup (e2->callee->name ()), e2->callee->order);
1245 else
1247 struct cgraph_edge *tmp = edge;
1248 if (dump_file)
1249 fprintf (dump_file, "Speculative call turned into direct call.\n");
1250 edge = e2;
1251 e2 = tmp;
1252 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1253 in the functions inlined through it. */
1255 edge->count += e2->count;
1256 edge->frequency += e2->frequency;
1257 if (edge->frequency > CGRAPH_FREQ_MAX)
1258 edge->frequency = CGRAPH_FREQ_MAX;
1259 edge->speculative = false;
1260 e2->speculative = false;
1261 ref->remove_reference ();
1262 if (e2->indirect_unknown_callee || e2->inline_failed)
1263 cgraph_remove_edge (e2);
1264 else
1265 e2->callee->remove_symbol_and_inline_clones ();
1266 if (edge->caller->call_site_hash)
1267 cgraph_update_edge_in_call_site_hash (edge);
1268 return edge;
1271 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1272 CALLEE. DELTA is an integer constant that is to be added to the this
1273 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1275 struct cgraph_edge *
1276 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1278 gcc_assert (edge->indirect_unknown_callee);
1280 /* If we are redirecting speculative call, make it non-speculative. */
1281 if (edge->indirect_unknown_callee && edge->speculative)
1283 edge = cgraph_resolve_speculation (edge, callee->decl);
1285 /* On successful speculation just return the pre existing direct edge. */
1286 if (!edge->indirect_unknown_callee)
1287 return edge;
1290 edge->indirect_unknown_callee = 0;
1291 ggc_free (edge->indirect_info);
1292 edge->indirect_info = NULL;
1294 /* Get the edge out of the indirect edge list. */
1295 if (edge->prev_callee)
1296 edge->prev_callee->next_callee = edge->next_callee;
1297 if (edge->next_callee)
1298 edge->next_callee->prev_callee = edge->prev_callee;
1299 if (!edge->prev_callee)
1300 edge->caller->indirect_calls = edge->next_callee;
1302 /* Put it into the normal callee list */
1303 edge->prev_callee = NULL;
1304 edge->next_callee = edge->caller->callees;
1305 if (edge->caller->callees)
1306 edge->caller->callees->prev_callee = edge;
1307 edge->caller->callees = edge;
1309 /* Insert to callers list of the new callee. */
1310 cgraph_set_edge_callee (edge, callee);
1312 if (edge->call_stmt)
1313 edge->call_stmt_cannot_inline_p
1314 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1315 false);
1317 /* We need to re-determine the inlining status of the edge. */
1318 initialize_inline_failed (edge);
1319 return edge;
1322 /* If necessary, change the function declaration in the call statement
1323 associated with E so that it corresponds to the edge callee. */
1325 gimple
1326 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1328 tree decl = gimple_call_fndecl (e->call_stmt);
1329 tree lhs = gimple_call_lhs (e->call_stmt);
1330 gimple new_stmt;
1331 gimple_stmt_iterator gsi;
1332 #ifdef ENABLE_CHECKING
1333 struct cgraph_node *node;
1334 #endif
1336 if (e->speculative)
1338 struct cgraph_edge *e2;
1339 gimple new_stmt;
1340 struct ipa_ref *ref;
1342 cgraph_speculative_call_info (e, e, e2, ref);
1343 /* If there already is an direct call (i.e. as a result of inliner's
1344 substitution), forget about speculating. */
1345 if (decl)
1346 e = cgraph_resolve_speculation (e, decl);
1347 /* If types do not match, speculation was likely wrong.
1348 The direct edge was posisbly redirected to the clone with a different
1349 signature. We did not update the call statement yet, so compare it
1350 with the reference that still points to the proper type. */
1351 else if (!gimple_check_call_matching_types (e->call_stmt,
1352 ref->referred->decl,
1353 true))
1355 if (dump_file)
1356 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1357 "Type mismatch.\n",
1358 xstrdup (e->caller->name ()),
1359 e->caller->order,
1360 xstrdup (e->callee->name ()),
1361 e->callee->order);
1362 e = cgraph_resolve_speculation (e, NULL);
1363 /* We are producing the final function body and will throw away the
1364 callgraph edges really soon. Reset the counts/frequencies to
1365 keep verifier happy in the case of roundoff errors. */
1366 e->count = gimple_bb (e->call_stmt)->count;
1367 e->frequency = compute_call_stmt_bb_frequency
1368 (e->caller->decl, gimple_bb (e->call_stmt));
1370 /* Expand speculation into GIMPLE code. */
1371 else
1373 if (dump_file)
1374 fprintf (dump_file,
1375 "Expanding speculative call of %s/%i -> %s/%i count:"
1376 "%"PRId64"\n",
1377 xstrdup (e->caller->name ()),
1378 e->caller->order,
1379 xstrdup (e->callee->name ()),
1380 e->callee->order,
1381 (int64_t)e->count);
1382 gcc_assert (e2->speculative);
1383 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1384 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
1385 e->count || e2->count
1386 ? RDIV (e->count * REG_BR_PROB_BASE,
1387 e->count + e2->count)
1388 : e->frequency || e2->frequency
1389 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1390 e->frequency + e2->frequency)
1391 : REG_BR_PROB_BASE / 2,
1392 e->count, e->count + e2->count);
1393 e->speculative = false;
1394 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1395 false);
1396 e->frequency = compute_call_stmt_bb_frequency
1397 (e->caller->decl, gimple_bb (e->call_stmt));
1398 e2->frequency = compute_call_stmt_bb_frequency
1399 (e2->caller->decl, gimple_bb (e2->call_stmt));
1400 e2->speculative = false;
1401 ref->speculative = false;
1402 ref->stmt = NULL;
1403 /* Indirect edges are not both in the call site hash.
1404 get it updated. */
1405 if (e->caller->call_site_hash)
1406 cgraph_update_edge_in_call_site_hash (e2);
1407 pop_cfun ();
1408 /* Continue redirecting E to proper target. */
1412 if (e->indirect_unknown_callee
1413 || decl == e->callee->decl)
1414 return e->call_stmt;
1416 #ifdef ENABLE_CHECKING
1417 if (decl)
1419 node = cgraph_node::get (decl);
1420 gcc_assert (!node || !node->clone.combined_args_to_skip);
1422 #endif
1424 if (cgraph_dump_file)
1426 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1427 xstrdup (e->caller->name ()), e->caller->order,
1428 xstrdup (e->callee->name ()), e->callee->order);
1429 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1430 if (e->callee->clone.combined_args_to_skip)
1432 fprintf (cgraph_dump_file, " combined args to skip: ");
1433 dump_bitmap (cgraph_dump_file,
1434 e->callee->clone.combined_args_to_skip);
1438 if (e->callee->clone.combined_args_to_skip)
1440 int lp_nr;
1442 new_stmt
1443 = gimple_call_copy_skip_args (e->call_stmt,
1444 e->callee->clone.combined_args_to_skip);
1445 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1446 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1448 if (gimple_vdef (new_stmt)
1449 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1450 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1452 gsi = gsi_for_stmt (e->call_stmt);
1453 gsi_replace (&gsi, new_stmt, false);
1454 /* We need to defer cleaning EH info on the new statement to
1455 fixup-cfg. We may not have dominator information at this point
1456 and thus would end up with unreachable blocks and have no way
1457 to communicate that we need to run CFG cleanup then. */
1458 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1459 if (lp_nr != 0)
1461 remove_stmt_from_eh_lp (e->call_stmt);
1462 add_stmt_to_eh_lp (new_stmt, lp_nr);
1465 else
1467 new_stmt = e->call_stmt;
1468 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1469 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1472 /* If the call becomes noreturn, remove the lhs. */
1473 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1475 if (TREE_CODE (lhs) == SSA_NAME)
1477 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1478 TREE_TYPE (lhs), NULL);
1479 var = get_or_create_ssa_default_def
1480 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1481 gimple set_stmt = gimple_build_assign (lhs, var);
1482 gsi = gsi_for_stmt (new_stmt);
1483 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1484 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1486 gimple_call_set_lhs (new_stmt, NULL_TREE);
1487 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1490 /* If new callee has no static chain, remove it. */
1491 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1493 gimple_call_set_chain (new_stmt, NULL);
1494 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1497 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1499 if (cgraph_dump_file)
1501 fprintf (cgraph_dump_file, " updated to:");
1502 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1504 return new_stmt;
1507 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1508 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1509 of OLD_STMT if it was previously call statement.
1510 If NEW_STMT is NULL, the call has been dropped without any
1511 replacement. */
1513 static void
1514 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1515 gimple old_stmt, tree old_call,
1516 gimple new_stmt)
1518 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1519 ? gimple_call_fndecl (new_stmt) : 0;
1521 /* We are seeing indirect calls, then there is nothing to update. */
1522 if (!new_call && !old_call)
1523 return;
1524 /* See if we turned indirect call into direct call or folded call to one builtin
1525 into different builtin. */
1526 if (old_call != new_call)
1528 struct cgraph_edge *e = node->get_edge (old_stmt);
1529 struct cgraph_edge *ne = NULL;
1530 gcov_type count;
1531 int frequency;
1533 if (e)
1535 /* See if the edge is already there and has the correct callee. It
1536 might be so because of indirect inlining has already updated
1537 it. We also might've cloned and redirected the edge. */
1538 if (new_call && e->callee)
1540 struct cgraph_node *callee = e->callee;
1541 while (callee)
1543 if (callee->decl == new_call
1544 || callee->former_clone_of == new_call)
1546 cgraph_set_call_stmt (e, new_stmt);
1547 return;
1549 callee = callee->clone_of;
1553 /* Otherwise remove edge and create new one; we can't simply redirect
1554 since function has changed, so inline plan and other information
1555 attached to edge is invalid. */
1556 count = e->count;
1557 frequency = e->frequency;
1558 if (e->indirect_unknown_callee || e->inline_failed)
1559 cgraph_remove_edge (e);
1560 else
1561 e->callee->remove_symbol_and_inline_clones ();
1563 else if (new_call)
1565 /* We are seeing new direct call; compute profile info based on BB. */
1566 basic_block bb = gimple_bb (new_stmt);
1567 count = bb->count;
1568 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1569 bb);
1572 if (new_call)
1574 ne = node->create_edge (cgraph_node::get_create (new_call),
1575 new_stmt, count, frequency);
1576 gcc_assert (ne->inline_failed);
1579 /* We only updated the call stmt; update pointer in cgraph edge.. */
1580 else if (old_stmt != new_stmt)
1581 cgraph_set_call_stmt (node->get_edge (old_stmt), new_stmt);
1584 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1585 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1586 of OLD_STMT before it was updated (updating can happen inplace). */
1588 void
1589 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1591 struct cgraph_node *orig = cgraph_node::get (cfun->decl);
1592 struct cgraph_node *node;
1594 gcc_checking_assert (orig);
1595 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1596 if (orig->clones)
1597 for (node = orig->clones; node != orig;)
1599 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1600 if (node->clones)
1601 node = node->clones;
1602 else if (node->next_sibling_clone)
1603 node = node->next_sibling_clone;
1604 else
1606 while (node != orig && !node->next_sibling_clone)
1607 node = node->clone_of;
1608 if (node != orig)
1609 node = node->next_sibling_clone;
1615 /* Remove all callees from the node. */
1617 void
1618 cgraph_node::remove_callees (void)
1620 struct cgraph_edge *e, *f;
1622 /* It is sufficient to remove the edges from the lists of callers of
1623 the callees. The callee list of the node can be zapped with one
1624 assignment. */
1625 for (e = callees; e; e = f)
1627 f = e->next_callee;
1628 cgraph_call_edge_removal_hooks (e);
1629 if (!e->indirect_unknown_callee)
1630 cgraph_edge_remove_callee (e);
1631 cgraph_free_edge (e);
1633 for (e = indirect_calls; e; e = f)
1635 f = e->next_callee;
1636 cgraph_call_edge_removal_hooks (e);
1637 if (!e->indirect_unknown_callee)
1638 cgraph_edge_remove_callee (e);
1639 cgraph_free_edge (e);
1641 indirect_calls = NULL;
1642 callees = NULL;
1643 if (call_site_hash)
1645 htab_delete (call_site_hash);
1646 call_site_hash = NULL;
1650 /* Remove all callers from the node. */
1652 void
1653 cgraph_node::remove_callers (void)
1655 struct cgraph_edge *e, *f;
1657 /* It is sufficient to remove the edges from the lists of callees of
1658 the callers. The caller list of the node can be zapped with one
1659 assignment. */
1660 for (e = callers; e; e = f)
1662 f = e->next_caller;
1663 cgraph_call_edge_removal_hooks (e);
1664 cgraph_edge_remove_caller (e);
1665 cgraph_free_edge (e);
1667 callers = NULL;
1670 /* Helper function for cgraph_release_function_body and free_lang_data.
1671 It releases body from function DECL without having to inspect its
1672 possibly non-existent symtab node. */
1674 void
1675 release_function_body (tree decl)
1677 if (DECL_STRUCT_FUNCTION (decl))
1679 push_cfun (DECL_STRUCT_FUNCTION (decl));
1680 if (cfun->cfg
1681 && current_loops)
1683 cfun->curr_properties &= ~PROP_loops;
1684 loop_optimizer_finalize ();
1686 if (cfun->gimple_df)
1688 delete_tree_ssa ();
1689 delete_tree_cfg_annotations ();
1690 cfun->eh = NULL;
1692 if (cfun->cfg)
1694 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1695 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1696 clear_edges ();
1697 cfun->cfg = NULL;
1699 if (cfun->value_histograms)
1700 free_histograms ();
1701 pop_cfun ();
1702 gimple_set_body (decl, NULL);
1703 /* Struct function hangs a lot of data that would leak if we didn't
1704 removed all pointers to it. */
1705 ggc_free (DECL_STRUCT_FUNCTION (decl));
1706 DECL_STRUCT_FUNCTION (decl) = NULL;
1708 DECL_SAVED_TREE (decl) = NULL;
1711 /* Release memory used to represent body of function.
1712 Use this only for functions that are released before being translated to
1713 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1714 are free'd in final.c via free_after_compilation(). */
1716 void
1717 cgraph_node::release_body (void)
1719 ipa_transforms_to_apply.release ();
1720 if (!used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1722 DECL_RESULT (decl) = NULL;
1723 DECL_ARGUMENTS (decl) = NULL;
1725 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1726 of its associated function function declaration because it's
1727 needed to emit debug info later. */
1728 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1729 DECL_INITIAL (decl) = error_mark_node;
1730 release_function_body (decl);
1731 if (lto_file_data)
1732 lto_free_function_in_decl_state_for_node (this);
1735 /* Remove function from symbol table. */
1737 void
1738 cgraph_node::remove (void)
1740 struct cgraph_node *n;
1741 int uid = this->uid;
1743 cgraph_call_node_removal_hooks (this);
1744 remove_callers ();
1745 remove_callees ();
1746 ipa_transforms_to_apply.release ();
1748 /* Incremental inlining access removed nodes stored in the postorder list.
1750 force_output = false;
1751 forced_by_abi = false;
1752 for (n = nested; n; n = n->next_nested)
1753 n->origin = NULL;
1754 nested = NULL;
1755 if (origin)
1757 struct cgraph_node **node2 = &origin->nested;
1759 while (*node2 != this)
1760 node2 = &(*node2)->next_nested;
1761 *node2 = next_nested;
1763 unregister ();
1764 if (prev_sibling_clone)
1765 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1766 else if (clone_of)
1767 clone_of->clones = next_sibling_clone;
1768 if (next_sibling_clone)
1769 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1770 if (clones)
1772 struct cgraph_node *n, *next;
1774 if (clone_of)
1776 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1777 n->clone_of = clone_of;
1778 n->clone_of = clone_of;
1779 n->next_sibling_clone = clone_of->clones;
1780 if (clone_of->clones)
1781 clone_of->clones->prev_sibling_clone = n;
1782 clone_of->clones = clones;
1784 else
1786 /* We are removing node with clones. This makes clones inconsistent,
1787 but assume they will be removed subsequently and just keep clone
1788 tree intact. This can happen in unreachable function removal since
1789 we remove unreachable functions in random order, not by bottom-up
1790 walk of clone trees. */
1791 for (n = clones; n; n = next)
1793 next = n->next_sibling_clone;
1794 n->next_sibling_clone = NULL;
1795 n->prev_sibling_clone = NULL;
1796 n->clone_of = NULL;
1801 /* While all the clones are removed after being proceeded, the function
1802 itself is kept in the cgraph even after it is compiled. Check whether
1803 we are done with this body and reclaim it proactively if this is the case.
1805 if (cgraph_state != CGRAPH_LTO_STREAMING)
1807 n = cgraph_node::get (decl);
1808 if (!n
1809 || (!n->clones && !n->clone_of && !n->global.inlined_to
1810 && (cgraph_global_info_ready
1811 && (TREE_ASM_WRITTEN (n->decl)
1812 || DECL_EXTERNAL (n->decl)
1813 || !n->analyzed
1814 || (!flag_wpa && n->in_other_partition)))))
1815 release_body ();
1818 decl = NULL;
1819 if (call_site_hash)
1821 htab_delete (call_site_hash);
1822 call_site_hash = NULL;
1824 cgraph_n_nodes--;
1826 /* Clear out the node to NULL all pointers and add the node to the free
1827 list. */
1828 memset (this, 0, sizeof (*this));
1829 type = SYMTAB_FUNCTION;
1830 this->uid = uid;
1831 SET_NEXT_FREE_NODE (this, free_nodes);
1832 free_nodes = this;
1835 /* Likewise indicate that a node is having address taken. */
1837 void
1838 cgraph_node::mark_address_taken (void)
1840 /* Indirect inlining can figure out that all uses of the address are
1841 inlined. */
1842 if (global.inlined_to)
1844 gcc_assert (cfun->after_inlining);
1845 gcc_assert (callers->indirect_inlining_edge);
1846 return;
1848 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1849 IPA_REF_ADDR reference exists (and thus it should be set on node
1850 representing alias we take address of) and as a test whether address
1851 of the object was taken (and thus it should be set on node alias is
1852 referring to). We should remove the first use and the remove the
1853 following set. */
1854 address_taken = 1;
1855 cgraph_node *node = ultimate_alias_target ();
1856 node->address_taken = 1;
1859 /* Return local info for the compiled function. */
1861 struct cgraph_local_info *
1862 cgraph_local_info (tree decl)
1864 struct cgraph_node *node;
1866 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1867 node = cgraph_node::get (decl);
1868 if (!node)
1869 return NULL;
1870 return &node->local;
1873 /* Return local info for the compiled function. */
1875 struct cgraph_global_info *
1876 cgraph_global_info (tree decl)
1878 struct cgraph_node *node;
1880 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1881 node = cgraph_node::get (decl);
1882 if (!node)
1883 return NULL;
1884 return &node->global;
1887 /* Return local info for the compiled function. */
1889 struct cgraph_rtl_info *
1890 cgraph_rtl_info (tree decl)
1892 struct cgraph_node *node;
1894 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1895 node = cgraph_node::get (decl);
1896 if (!node
1897 || (decl != current_function_decl
1898 && !TREE_ASM_WRITTEN (node->decl)))
1899 return NULL;
1900 return &node->rtl;
1903 /* Return a string describing the failure REASON. */
1905 const char*
1906 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1908 #undef DEFCIFCODE
1909 #define DEFCIFCODE(code, type, string) string,
1911 static const char *cif_string_table[CIF_N_REASONS] = {
1912 #include "cif-code.def"
1915 /* Signedness of an enum type is implementation defined, so cast it
1916 to unsigned before testing. */
1917 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1918 return cif_string_table[reason];
1921 /* Return a type describing the failure REASON. */
1923 cgraph_inline_failed_type_t
1924 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1926 #undef DEFCIFCODE
1927 #define DEFCIFCODE(code, type, string) type,
1929 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1930 #include "cif-code.def"
1933 /* Signedness of an enum type is implementation defined, so cast it
1934 to unsigned before testing. */
1935 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1936 return cif_type_table[reason];
1939 /* Names used to print out the availability enum. */
1940 const char * const cgraph_availability_names[] =
1941 {"unset", "not_available", "overwritable", "available", "local"};
1944 /* Dump call graph node to file F. */
1946 void
1947 cgraph_node::dump (FILE *f)
1949 struct cgraph_edge *edge;
1950 int indirect_calls_count = 0;
1952 dump_base (f);
1954 if (global.inlined_to)
1955 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1956 xstrdup (name ()),
1957 order,
1958 xstrdup (global.inlined_to->name ()),
1959 global.inlined_to->order);
1960 if (clone_of)
1961 fprintf (f, " Clone of %s/%i\n",
1962 clone_of->asm_name (),
1963 clone_of->order);
1964 if (cgraph_function_flags_ready)
1965 fprintf (f, " Availability: %s\n",
1966 cgraph_availability_names [get_availability ()]);
1968 if (profile_id)
1969 fprintf (f, " Profile id: %i\n",
1970 profile_id);
1971 fprintf (f, " First run: %i\n", tp_first_run);
1972 fprintf (f, " Function flags:");
1973 if (count)
1974 fprintf (f, " executed %"PRId64"x",
1975 (int64_t)count);
1976 if (origin)
1977 fprintf (f, " nested in: %s", origin->asm_name ());
1978 if (gimple_has_body_p (decl))
1979 fprintf (f, " body");
1980 if (process)
1981 fprintf (f, " process");
1982 if (local.local)
1983 fprintf (f, " local");
1984 if (local.redefined_extern_inline)
1985 fprintf (f, " redefined_extern_inline");
1986 if (only_called_at_startup)
1987 fprintf (f, " only_called_at_startup");
1988 if (only_called_at_exit)
1989 fprintf (f, " only_called_at_exit");
1990 if (tm_clone)
1991 fprintf (f, " tm_clone");
1992 if (DECL_STATIC_CONSTRUCTOR (decl))
1993 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
1994 if (DECL_STATIC_DESTRUCTOR (decl))
1995 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
1997 fprintf (f, "\n");
1999 if (thunk.thunk_p)
2001 fprintf (f, " Thunk");
2002 if (thunk.alias)
2003 fprintf (f, " of %s (asm: %s)",
2004 lang_hooks.decl_printable_name (thunk.alias, 2),
2005 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2006 fprintf (f, " fixed offset %i virtual value %i has "
2007 "virtual offset %i)\n",
2008 (int)thunk.fixed_offset,
2009 (int)thunk.virtual_value,
2010 (int)thunk.virtual_offset_p);
2012 if (alias && thunk.alias
2013 && DECL_P (thunk.alias))
2015 fprintf (f, " Alias of %s",
2016 lang_hooks.decl_printable_name (thunk.alias, 2));
2017 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2018 fprintf (f, " (asm: %s)",
2019 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2020 fprintf (f, "\n");
2023 fprintf (f, " Called by: ");
2025 for (edge = callers; edge; edge = edge->next_caller)
2027 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2028 edge->caller->order);
2029 if (edge->count)
2030 fprintf (f, "(%"PRId64"x) ",
2031 (int64_t)edge->count);
2032 if (edge->frequency)
2033 fprintf (f, "(%.2f per call) ",
2034 edge->frequency / (double)CGRAPH_FREQ_BASE);
2035 if (edge->speculative)
2036 fprintf (f, "(speculative) ");
2037 if (!edge->inline_failed)
2038 fprintf (f, "(inlined) ");
2039 if (edge->indirect_inlining_edge)
2040 fprintf (f, "(indirect_inlining) ");
2041 if (edge->can_throw_external)
2042 fprintf (f, "(can throw external) ");
2045 fprintf (f, "\n Calls: ");
2046 for (edge = callees; edge; edge = edge->next_callee)
2048 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2049 edge->callee->order);
2050 if (edge->speculative)
2051 fprintf (f, "(speculative) ");
2052 if (!edge->inline_failed)
2053 fprintf (f, "(inlined) ");
2054 if (edge->indirect_inlining_edge)
2055 fprintf (f, "(indirect_inlining) ");
2056 if (edge->count)
2057 fprintf (f, "(%"PRId64"x) ",
2058 (int64_t)edge->count);
2059 if (edge->frequency)
2060 fprintf (f, "(%.2f per call) ",
2061 edge->frequency / (double)CGRAPH_FREQ_BASE);
2062 if (edge->can_throw_external)
2063 fprintf (f, "(can throw external) ");
2065 fprintf (f, "\n");
2067 for (edge = indirect_calls; edge; edge = edge->next_callee)
2068 indirect_calls_count++;
2069 if (indirect_calls_count)
2070 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2071 indirect_calls_count);
2074 /* Dump call graph node NODE to stderr. */
2076 DEBUG_FUNCTION void
2077 cgraph_node::debug (void)
2079 dump (stderr);
2082 /* Dump the callgraph to file F. */
2084 void
2085 cgraph_node::dump_cgraph (FILE *f)
2087 struct cgraph_node *node;
2089 fprintf (f, "callgraph:\n\n");
2090 FOR_EACH_FUNCTION (node)
2091 node->dump (f);
2094 /* Return true when the DECL can possibly be inlined. */
2096 bool
2097 cgraph_function_possibly_inlined_p (tree decl)
2099 if (!cgraph_global_info_ready)
2100 return !DECL_UNINLINABLE (decl);
2101 return DECL_POSSIBLY_INLINED (decl);
2104 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2105 void
2106 cgraph_node::unnest (void)
2108 struct cgraph_node **node2 = &origin->nested;
2109 gcc_assert (origin);
2111 while (*node2 != this)
2112 node2 = &(*node2)->next_nested;
2113 *node2 = next_nested;
2114 origin = NULL;
2117 /* Return function availability. See cgraph.h for description of individual
2118 return values. */
2119 enum availability
2120 cgraph_node::get_availability (void)
2122 enum availability avail;
2123 if (!analyzed)
2124 avail = AVAIL_NOT_AVAILABLE;
2125 else if (local.local)
2126 avail = AVAIL_LOCAL;
2127 else if (alias && weakref)
2128 ultimate_alias_target (&avail);
2129 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2130 avail = AVAIL_INTERPOSABLE;
2131 else if (!externally_visible)
2132 avail = AVAIL_AVAILABLE;
2133 /* Inline functions are safe to be analyzed even if their symbol can
2134 be overwritten at runtime. It is not meaningful to enforce any sane
2135 behaviour on replacing inline function by different body. */
2136 else if (DECL_DECLARED_INLINE_P (decl))
2137 avail = AVAIL_AVAILABLE;
2139 /* If the function can be overwritten, return OVERWRITABLE. Take
2140 care at least of two notable extensions - the COMDAT functions
2141 used to share template instantiations in C++ (this is symmetric
2142 to code cp_cannot_inline_tree_fn and probably shall be shared and
2143 the inlinability hooks completely eliminated).
2145 ??? Does the C++ one definition rule allow us to always return
2146 AVAIL_AVAILABLE here? That would be good reason to preserve this
2147 bit. */
2149 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2150 avail = AVAIL_INTERPOSABLE;
2151 else avail = AVAIL_AVAILABLE;
2153 return avail;
2156 /* Worker for cgraph_node_can_be_local_p. */
2157 static bool
2158 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node, void *)
2160 return !(!node->force_output
2161 && ((DECL_COMDAT (node->decl)
2162 && !node->forced_by_abi
2163 && !node->used_from_object_file_p ()
2164 && !node->same_comdat_group)
2165 || !node->externally_visible));
2168 /* Return true if cgraph_node can be made local for API change.
2169 Extern inline functions and C++ COMDAT functions can be made local
2170 at the expense of possible code size growth if function is used in multiple
2171 compilation units. */
2172 bool
2173 cgraph_node::can_be_local_p (void)
2175 return (!address_taken
2176 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2177 NULL, true));
2180 /* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
2181 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2182 skipped. */
2184 bool
2185 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2186 (cgraph_node *, void *),
2187 void *data,
2188 bool include_overwritable)
2190 struct cgraph_edge *e;
2191 struct ipa_ref *ref;
2193 if (callback (this, data))
2194 return true;
2195 for (e = callers; e; e = e->next_caller)
2196 if (e->caller->thunk.thunk_p
2197 && (include_overwritable
2198 || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2199 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2200 include_overwritable))
2201 return true;
2203 FOR_EACH_ALIAS (this, ref)
2205 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2206 if (include_overwritable
2207 || alias->get_availability () > AVAIL_INTERPOSABLE)
2208 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2209 include_overwritable))
2210 return true;
2212 return false;
2215 /* Call calback on function and aliases associated to the function.
2216 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2217 skipped. */
2219 bool
2220 cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2221 void *),
2222 void *data,
2223 bool include_overwritable)
2225 struct ipa_ref *ref;
2227 if (callback (this, data))
2228 return true;
2230 FOR_EACH_ALIAS (this, ref)
2232 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2233 if (include_overwritable
2234 || alias->get_availability () > AVAIL_INTERPOSABLE)
2235 if (alias->call_for_symbol_and_aliases (callback, data,
2236 include_overwritable))
2237 return true;
2239 return false;
2242 /* Worker to bring NODE local. */
2244 bool
2245 cgraph_node::make_local (struct cgraph_node *node, void *)
2247 gcc_checking_assert (node->can_be_local_p ());
2248 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2250 node->make_decl_local ();
2251 node->set_section (NULL);
2252 node->set_comdat_group (NULL);
2253 node->externally_visible = false;
2254 node->forced_by_abi = false;
2255 node->local.local = true;
2256 node->set_section (NULL);
2257 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2258 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2259 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2260 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2262 return false;
2265 /* Bring cgraph node local. */
2267 void
2268 cgraph_node::make_local (void)
2270 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2273 /* Worker to set nothrow flag. */
2275 static bool
2276 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2278 struct cgraph_edge *e;
2280 TREE_NOTHROW (node->decl) = data != NULL;
2282 if (data != NULL)
2283 for (e = node->callers; e; e = e->next_caller)
2284 e->can_throw_external = false;
2285 return false;
2288 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2289 if any to NOTHROW. */
2291 void
2292 cgraph_node::set_nothrow_flag (bool nothrow)
2294 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2295 (void *)(size_t)nothrow, false);
2298 /* Worker to set const flag. */
2300 static bool
2301 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2303 /* Static constructors and destructors without a side effect can be
2304 optimized out. */
2305 if (data && !((size_t)data & 2))
2307 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2308 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2309 if (DECL_STATIC_DESTRUCTOR (node->decl))
2310 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2312 TREE_READONLY (node->decl) = data != NULL;
2313 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2314 return false;
2317 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2318 if any to READONLY. */
2320 void
2321 cgraph_node::set_const_flag (bool readonly, bool looping)
2323 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2324 (void *)(size_t)(readonly + (int)looping * 2),
2325 false);
2328 /* Worker to set pure flag. */
2330 static bool
2331 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2333 /* Static constructors and destructors without a side effect can be
2334 optimized out. */
2335 if (data && !((size_t)data & 2))
2337 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2338 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2339 if (DECL_STATIC_DESTRUCTOR (node->decl))
2340 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2342 DECL_PURE_P (node->decl) = data != NULL;
2343 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2344 return false;
2347 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2348 if any to PURE. */
2350 void
2351 cgraph_node::set_pure_flag (bool pure, bool looping)
2353 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2354 (void *)(size_t)(pure + (int)looping * 2),
2355 false);
2358 /* Return true when cgraph_node can not return or throw and thus
2359 it is safe to ignore its side effects for IPA analysis. */
2361 bool
2362 cgraph_node::cannot_return_p (void)
2364 int flags = flags_from_decl_or_type (decl);
2365 if (!flag_exceptions)
2366 return (flags & ECF_NORETURN) != 0;
2367 else
2368 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2369 == (ECF_NORETURN | ECF_NOTHROW));
2372 /* Return true when call of E can not lead to return from caller
2373 and thus it is safe to ignore its side effects for IPA analysis
2374 when computing side effects of the caller.
2375 FIXME: We could actually mark all edges that have no reaching
2376 patch to the exit block or throw to get better results. */
2377 bool
2378 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2380 if (e->caller->cannot_return_p ())
2381 return true;
2382 if (e->indirect_unknown_callee)
2384 int flags = e->indirect_info->ecf_flags;
2385 if (!flag_exceptions)
2386 return (flags & ECF_NORETURN) != 0;
2387 else
2388 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2389 == (ECF_NORETURN | ECF_NOTHROW));
2391 else
2392 return e->callee->cannot_return_p ();
2395 /* Return true when function can be removed from callgraph
2396 if all direct calls are eliminated. */
2398 bool
2399 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2401 gcc_assert (!global.inlined_to);
2402 /* Extern inlines can always go, we will use the external definition. */
2403 if (DECL_EXTERNAL (decl))
2404 return true;
2405 /* When function is needed, we can not remove it. */
2406 if (force_output || used_from_other_partition)
2407 return false;
2408 if (DECL_STATIC_CONSTRUCTOR (decl)
2409 || DECL_STATIC_DESTRUCTOR (decl))
2410 return false;
2411 /* Only COMDAT functions can be removed if externally visible. */
2412 if (externally_visible
2413 && (!DECL_COMDAT (decl)
2414 || forced_by_abi
2415 || used_from_object_file_p ()))
2416 return false;
2417 return true;
2420 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2422 static bool
2423 nonremovable_p (struct cgraph_node *node, void *)
2425 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2428 /* Return true when function cgraph_node and its aliases can be removed from
2429 callgraph if all direct calls are eliminated. */
2431 bool
2432 cgraph_node::can_remove_if_no_direct_calls_p (void)
2434 /* Extern inlines can always go, we will use the external definition. */
2435 if (DECL_EXTERNAL (decl))
2436 return true;
2437 if (address_taken)
2438 return false;
2439 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2442 /* Return true when function cgraph_node can be expected to be removed
2443 from program when direct calls in this compilation unit are removed.
2445 As a special case COMDAT functions are
2446 cgraph_can_remove_if_no_direct_calls_p while the are not
2447 cgraph_only_called_directly_p (it is possible they are called from other
2448 unit)
2450 This function behaves as cgraph_only_called_directly_p because eliminating
2451 all uses of COMDAT function does not make it necessarily disappear from
2452 the program unless we are compiling whole program or we do LTO. In this
2453 case we know we win since dynamic linking will not really discard the
2454 linkonce section. */
2456 bool
2457 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2459 gcc_assert (!global.inlined_to);
2461 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2462 NULL, true))
2463 return false;
2464 if (!in_lto_p && !flag_whole_program)
2465 return only_called_directly_p ();
2466 else
2468 if (DECL_EXTERNAL (decl))
2469 return true;
2470 return can_remove_if_no_direct_calls_p ();
2475 /* Worker for cgraph_only_called_directly_p. */
2477 static bool
2478 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *)
2480 return !node->only_called_directly_or_aliased_p ();
2483 /* Return true when function cgraph_node and all its aliases are only called
2484 directly.
2485 i.e. it is not externally visible, address was not taken and
2486 it is not used in any other non-standard way. */
2488 bool
2489 cgraph_node::only_called_directly_p (void)
2491 gcc_assert (ultimate_alias_target () == this);
2492 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2493 NULL, true);
2497 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2499 static bool
2500 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2502 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2503 struct cgraph_edge *cs;
2504 enum availability avail;
2505 node->ultimate_alias_target (&avail);
2507 if (avail > AVAIL_INTERPOSABLE)
2508 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2509 if (!cs->indirect_inlining_edge)
2510 redirect_callers->safe_push (cs);
2511 return false;
2514 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2515 cgraph_node (i.e. are not overwritable). */
2517 vec<cgraph_edge *>
2518 cgraph_node::collect_callers (void)
2520 vec<cgraph_edge *> redirect_callers = vNULL;
2521 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2522 &redirect_callers, false);
2523 return redirect_callers;
2526 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2528 static bool
2529 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2531 bool skipped_thunk = false;
2532 node = node->ultimate_alias_target ();
2533 node2 = node2->ultimate_alias_target ();
2535 /* There are no virtual clones of thunks so check former_clone_of or if we
2536 might have skipped thunks because this adjustments are no longer
2537 necessary. */
2538 while (node->thunk.thunk_p)
2540 if (node2->former_clone_of == node->decl)
2541 return true;
2542 if (!node->thunk.this_adjusting)
2543 return false;
2544 node = node->callees->callee->ultimate_alias_target ();
2545 skipped_thunk = true;
2548 if (skipped_thunk)
2550 if (!node2->clone.args_to_skip
2551 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2552 return false;
2553 if (node2->former_clone_of == node->decl)
2554 return true;
2555 else if (!node2->clone_of)
2556 return false;
2559 while (node != node2 && node2)
2560 node2 = node2->clone_of;
2561 return node2 != NULL;
2564 /* Verify edge E count and frequency. */
2566 static bool
2567 verify_edge_count_and_frequency (struct cgraph_edge *e)
2569 bool error_found = false;
2570 if (e->count < 0)
2572 error ("caller edge count is negative");
2573 error_found = true;
2575 if (e->frequency < 0)
2577 error ("caller edge frequency is negative");
2578 error_found = true;
2580 if (e->frequency > CGRAPH_FREQ_MAX)
2582 error ("caller edge frequency is too large");
2583 error_found = true;
2585 if (gimple_has_body_p (e->caller->decl)
2586 && !e->caller->global.inlined_to
2587 && !e->speculative
2588 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2589 Remove this once edges are actually removed from the function at that time. */
2590 && (e->frequency
2591 || (inline_edge_summary_vec.exists ()
2592 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2593 || !inline_edge_summary (e)->predicate)))
2594 && (e->frequency
2595 != compute_call_stmt_bb_frequency (e->caller->decl,
2596 gimple_bb (e->call_stmt))))
2598 error ("caller edge frequency %i does not match BB frequency %i",
2599 e->frequency,
2600 compute_call_stmt_bb_frequency (e->caller->decl,
2601 gimple_bb (e->call_stmt)));
2602 error_found = true;
2604 return error_found;
2607 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2608 static void
2609 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2611 bool fndecl_was_null = false;
2612 /* debug_gimple_stmt needs correct cfun */
2613 if (cfun != this_cfun)
2614 set_cfun (this_cfun);
2615 /* ...and an actual current_function_decl */
2616 if (!current_function_decl)
2618 current_function_decl = this_cfun->decl;
2619 fndecl_was_null = true;
2621 debug_gimple_stmt (stmt);
2622 if (fndecl_was_null)
2623 current_function_decl = NULL;
2626 /* Verify that call graph edge E corresponds to DECL from the associated
2627 statement. Return true if the verification should fail. */
2629 static bool
2630 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2632 struct cgraph_node *node;
2634 if (!decl || e->callee->global.inlined_to)
2635 return false;
2636 if (cgraph_state == CGRAPH_LTO_STREAMING)
2637 return false;
2638 node = cgraph_node::get (decl);
2640 /* We do not know if a node from a different partition is an alias or what it
2641 aliases and therefore cannot do the former_clone_of check reliably. When
2642 body_removed is set, we have lost all information about what was alias or
2643 thunk of and also cannot proceed. */
2644 if (!node
2645 || node->body_removed
2646 || node->in_other_partition
2647 || e->callee->in_other_partition)
2648 return false;
2650 node = node->ultimate_alias_target ();
2652 /* Optimizers can redirect unreachable calls or calls triggering undefined
2653 behaviour to builtin_unreachable. */
2654 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2655 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2656 return false;
2658 if (e->callee->former_clone_of != node->decl
2659 && (node != e->callee->ultimate_alias_target ())
2660 && !clone_of_p (node, e->callee))
2661 return true;
2662 else
2663 return false;
2666 /* Verify cgraph nodes of given cgraph node. */
2667 DEBUG_FUNCTION void
2668 cgraph_node::verify_node (void)
2670 struct cgraph_edge *e;
2671 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2672 basic_block this_block;
2673 gimple_stmt_iterator gsi;
2674 bool error_found = false;
2676 if (seen_error ())
2677 return;
2679 timevar_push (TV_CGRAPH_VERIFY);
2680 error_found |= verify_base ();
2681 for (e = callees; e; e = e->next_callee)
2682 if (e->aux)
2684 error ("aux field set for edge %s->%s",
2685 identifier_to_locale (e->caller->name ()),
2686 identifier_to_locale (e->callee->name ()));
2687 error_found = true;
2689 if (count < 0)
2691 error ("execution count is negative");
2692 error_found = true;
2694 if (global.inlined_to && same_comdat_group)
2696 error ("inline clone in same comdat group list");
2697 error_found = true;
2699 if (!definition && !in_other_partition && local.local)
2701 error ("local symbols must be defined");
2702 error_found = true;
2704 if (global.inlined_to && externally_visible)
2706 error ("externally visible inline clone");
2707 error_found = true;
2709 if (global.inlined_to && address_taken)
2711 error ("inline clone with address taken");
2712 error_found = true;
2714 if (global.inlined_to && force_output)
2716 error ("inline clone is forced to output");
2717 error_found = true;
2719 for (e = indirect_calls; e; e = e->next_callee)
2721 if (e->aux)
2723 error ("aux field set for indirect edge from %s",
2724 identifier_to_locale (e->caller->name ()));
2725 error_found = true;
2727 if (!e->indirect_unknown_callee
2728 || !e->indirect_info)
2730 error ("An indirect edge from %s is not marked as indirect or has "
2731 "associated indirect_info, the corresponding statement is: ",
2732 identifier_to_locale (e->caller->name ()));
2733 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2734 error_found = true;
2737 bool check_comdat = comdat_local_p ();
2738 for (e = callers; e; e = e->next_caller)
2740 if (verify_edge_count_and_frequency (e))
2741 error_found = true;
2742 if (check_comdat
2743 && !in_same_comdat_group_p (e->caller))
2745 error ("comdat-local function called by %s outside its comdat",
2746 identifier_to_locale (e->caller->name ()));
2747 error_found = true;
2749 if (!e->inline_failed)
2751 if (global.inlined_to
2752 != (e->caller->global.inlined_to
2753 ? e->caller->global.inlined_to : e->caller))
2755 error ("inlined_to pointer is wrong");
2756 error_found = true;
2758 if (callers->next_caller)
2760 error ("multiple inline callers");
2761 error_found = true;
2764 else
2765 if (global.inlined_to)
2767 error ("inlined_to pointer set for noninline callers");
2768 error_found = true;
2771 for (e = indirect_calls; e; e = e->next_callee)
2772 if (verify_edge_count_and_frequency (e))
2773 error_found = true;
2774 if (!callers && global.inlined_to)
2776 error ("inlined_to pointer is set but no predecessors found");
2777 error_found = true;
2779 if (global.inlined_to == this)
2781 error ("inlined_to pointer refers to itself");
2782 error_found = true;
2785 if (clone_of)
2787 struct cgraph_node *n;
2788 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2789 if (n == this)
2790 break;
2791 if (!n)
2793 error ("cgraph_node has wrong clone_of");
2794 error_found = true;
2797 if (clones)
2799 struct cgraph_node *n;
2800 for (n = clones; n; n = n->next_sibling_clone)
2801 if (n->clone_of != this)
2802 break;
2803 if (n)
2805 error ("cgraph_node has wrong clone list");
2806 error_found = true;
2809 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2811 error ("cgraph_node is in clone list but it is not clone");
2812 error_found = true;
2814 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2816 error ("cgraph_node has wrong prev_clone pointer");
2817 error_found = true;
2819 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2821 error ("double linked list of clones corrupted");
2822 error_found = true;
2825 if (analyzed && alias)
2827 bool ref_found = false;
2828 int i;
2829 struct ipa_ref *ref = NULL;
2831 if (callees)
2833 error ("Alias has call edges");
2834 error_found = true;
2836 for (i = 0; iterate_reference (i, ref); i++)
2837 if (ref->use != IPA_REF_ALIAS)
2839 error ("Alias has non-alias reference");
2840 error_found = true;
2842 else if (ref_found)
2844 error ("Alias has more than one alias reference");
2845 error_found = true;
2847 else
2848 ref_found = true;
2849 if (!ref_found)
2851 error ("Analyzed alias has no reference");
2852 error_found = true;
2855 if (analyzed && thunk.thunk_p)
2857 if (!callees)
2859 error ("No edge out of thunk node");
2860 error_found = true;
2862 else if (callees->next_callee)
2864 error ("More than one edge out of thunk node");
2865 error_found = true;
2867 if (gimple_has_body_p (decl))
2869 error ("Thunk is not supposed to have body");
2870 error_found = true;
2873 else if (analyzed && gimple_has_body_p (decl)
2874 && !TREE_ASM_WRITTEN (decl)
2875 && (!DECL_EXTERNAL (decl) || global.inlined_to)
2876 && !flag_wpa)
2878 if (this_cfun->cfg)
2880 hash_set<gimple> stmts;
2881 int i;
2882 struct ipa_ref *ref = NULL;
2884 /* Reach the trees by walking over the CFG, and note the
2885 enclosing basic-blocks in the call edges. */
2886 FOR_EACH_BB_FN (this_block, this_cfun)
2888 for (gsi = gsi_start_phis (this_block);
2889 !gsi_end_p (gsi); gsi_next (&gsi))
2890 stmts.add (gsi_stmt (gsi));
2891 for (gsi = gsi_start_bb (this_block);
2892 !gsi_end_p (gsi);
2893 gsi_next (&gsi))
2895 gimple stmt = gsi_stmt (gsi);
2896 stmts.add (stmt);
2897 if (is_gimple_call (stmt))
2899 struct cgraph_edge *e = get_edge (stmt);
2900 tree decl = gimple_call_fndecl (stmt);
2901 if (e)
2903 if (e->aux)
2905 error ("shared call_stmt:");
2906 cgraph_debug_gimple_stmt (this_cfun, stmt);
2907 error_found = true;
2909 if (!e->indirect_unknown_callee)
2911 if (verify_edge_corresponds_to_fndecl (e, decl))
2913 error ("edge points to wrong declaration:");
2914 debug_tree (e->callee->decl);
2915 fprintf (stderr," Instead of:");
2916 debug_tree (decl);
2917 error_found = true;
2920 else if (decl)
2922 error ("an indirect edge with unknown callee "
2923 "corresponding to a call_stmt with "
2924 "a known declaration:");
2925 error_found = true;
2926 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2928 e->aux = (void *)1;
2930 else if (decl)
2932 error ("missing callgraph edge for call stmt:");
2933 cgraph_debug_gimple_stmt (this_cfun, stmt);
2934 error_found = true;
2939 for (i = 0; iterate_reference (i, ref); i++)
2940 if (ref->stmt && !stmts.contains (ref->stmt))
2942 error ("reference to dead statement");
2943 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2944 error_found = true;
2947 else
2948 /* No CFG available?! */
2949 gcc_unreachable ();
2951 for (e = callees; e; e = e->next_callee)
2953 if (!e->aux)
2955 error ("edge %s->%s has no corresponding call_stmt",
2956 identifier_to_locale (e->caller->name ()),
2957 identifier_to_locale (e->callee->name ()));
2958 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2959 error_found = true;
2961 e->aux = 0;
2963 for (e = indirect_calls; e; e = e->next_callee)
2965 if (!e->aux && !e->speculative)
2967 error ("an indirect edge from %s has no corresponding call_stmt",
2968 identifier_to_locale (e->caller->name ()));
2969 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2970 error_found = true;
2972 e->aux = 0;
2975 if (error_found)
2977 dump (stderr);
2978 internal_error ("verify_cgraph_node failed");
2980 timevar_pop (TV_CGRAPH_VERIFY);
2983 /* Verify whole cgraph structure. */
2984 DEBUG_FUNCTION void
2985 cgraph_node::verify_cgraph_nodes (void)
2987 struct cgraph_node *node;
2989 if (seen_error ())
2990 return;
2992 FOR_EACH_FUNCTION (node)
2993 node->verify ();
2996 /* Walk the alias chain to return the function cgraph_node is alias of.
2997 Walk through thunk, too.
2998 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3000 cgraph_node *
3001 cgraph_node::function_symbol (enum availability *availability)
3003 cgraph_node *node = NULL;
3007 node = ultimate_alias_target (availability);
3008 if (node->thunk.thunk_p)
3010 node = node->callees->callee;
3011 if (availability)
3013 enum availability a;
3014 a = node->get_availability ();
3015 if (a < *availability)
3016 *availability = a;
3018 node = node->ultimate_alias_target (availability);
3020 } while (node && node->thunk.thunk_p);
3021 return node;
3024 /* When doing LTO, read cgraph_node's body from disk if it is not already
3025 present. */
3027 bool
3028 cgraph_node::get_body (void)
3030 struct lto_file_decl_data *file_data;
3031 const char *data, *name;
3032 size_t len;
3033 tree decl = this->decl;
3035 if (DECL_RESULT (decl))
3036 return false;
3038 gcc_assert (in_lto_p);
3040 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3042 file_data = lto_file_data;
3043 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3045 /* We may have renamed the declaration, e.g., a static function. */
3046 name = lto_get_decl_name_mapping (file_data, name);
3048 data = lto_get_section_data (file_data, LTO_section_function_body,
3049 name, &len);
3050 if (!data)
3051 fatal_error ("%s: section %s is missing",
3052 file_data->file_name,
3053 name);
3055 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3057 lto_input_function_body (file_data, this, data);
3058 lto_stats.num_function_bodies++;
3059 lto_free_section_data (file_data, LTO_section_function_body, name,
3060 data, len);
3061 lto_free_function_in_decl_state_for_node (this);
3063 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3065 return true;
3068 /* Verify if the type of the argument matches that of the function
3069 declaration. If we cannot verify this or there is a mismatch,
3070 return false. */
3072 static bool
3073 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3075 tree parms, p;
3076 unsigned int i, nargs;
3078 /* Calls to internal functions always match their signature. */
3079 if (gimple_call_internal_p (stmt))
3080 return true;
3082 nargs = gimple_call_num_args (stmt);
3084 /* Get argument types for verification. */
3085 if (fndecl)
3086 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3087 else
3088 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3090 /* Verify if the type of the argument matches that of the function
3091 declaration. If we cannot verify this or there is a mismatch,
3092 return false. */
3093 if (fndecl && DECL_ARGUMENTS (fndecl))
3095 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3096 i < nargs;
3097 i++, p = DECL_CHAIN (p))
3099 tree arg;
3100 /* We cannot distinguish a varargs function from the case
3101 of excess parameters, still deferring the inlining decision
3102 to the callee is possible. */
3103 if (!p)
3104 break;
3105 arg = gimple_call_arg (stmt, i);
3106 if (p == error_mark_node
3107 || DECL_ARG_TYPE (p) == error_mark_node
3108 || arg == error_mark_node
3109 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3110 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3111 return false;
3113 if (args_count_match && p)
3114 return false;
3116 else if (parms)
3118 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3120 tree arg;
3121 /* If this is a varargs function defer inlining decision
3122 to callee. */
3123 if (!p)
3124 break;
3125 arg = gimple_call_arg (stmt, i);
3126 if (TREE_VALUE (p) == error_mark_node
3127 || arg == error_mark_node
3128 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3129 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3130 && !fold_convertible_p (TREE_VALUE (p), arg)))
3131 return false;
3134 else
3136 if (nargs != 0)
3137 return false;
3139 return true;
3142 /* Verify if the type of the argument and lhs of CALL_STMT matches
3143 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3144 true, the arg count needs to be the same.
3145 If we cannot verify this or there is a mismatch, return false. */
3147 bool
3148 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3149 bool args_count_match)
3151 tree lhs;
3153 if ((DECL_RESULT (callee)
3154 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3155 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3156 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3157 TREE_TYPE (lhs))
3158 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3159 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3160 return false;
3161 return true;
3164 #include "gt-cgraph.h"