2014-08-15 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cgraph.c
blobcb49cdc9ef77ed5f77530f2d01e233259d4e730e
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,
946 bool compute_indirect_info)
948 struct cgraph_edge *edge = cgraph_node::create_edge (this, NULL, call_stmt,
949 count, freq, true);
950 tree target;
952 initialize_inline_failed (edge);
954 edge->indirect_info = cgraph_allocate_init_indirect_info ();
955 edge->indirect_info->ecf_flags = ecf_flags;
957 /* Record polymorphic call info. */
958 if (compute_indirect_info
959 && call_stmt
960 && (target = gimple_call_fn (call_stmt))
961 && virtual_method_call_p (target))
963 tree otr_type;
964 HOST_WIDE_INT otr_token;
965 ipa_polymorphic_call_context context;
967 get_polymorphic_call_info (decl,
968 target,
969 &otr_type, &otr_token,
970 &context, call_stmt);
972 /* Only record types can have virtual calls. */
973 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
974 edge->indirect_info->polymorphic = true;
975 edge->indirect_info->param_index = -1;
976 edge->indirect_info->otr_token = otr_token;
977 edge->indirect_info->otr_type = otr_type;
978 edge->indirect_info->outer_type = context.outer_type;
979 edge->indirect_info->speculative_outer_type
980 = context.speculative_outer_type;
981 edge->indirect_info->offset = context.offset;
982 edge->indirect_info->speculative_offset = context.speculative_offset;
983 edge->indirect_info->maybe_in_construction
984 = context.maybe_in_construction;
985 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
986 edge->indirect_info->speculative_maybe_derived_type
987 = context.speculative_maybe_derived_type;
990 edge->next_callee = indirect_calls;
991 if (indirect_calls)
992 indirect_calls->prev_callee = edge;
993 indirect_calls = edge;
995 return edge;
998 /* Remove the edge E from the list of the callers of the callee. */
1000 static inline void
1001 cgraph_edge_remove_callee (struct cgraph_edge *e)
1003 gcc_assert (!e->indirect_unknown_callee);
1004 if (e->prev_caller)
1005 e->prev_caller->next_caller = e->next_caller;
1006 if (e->next_caller)
1007 e->next_caller->prev_caller = e->prev_caller;
1008 if (!e->prev_caller)
1009 e->callee->callers = e->next_caller;
1012 /* Remove the edge E from the list of the callees of the caller. */
1014 static inline void
1015 cgraph_edge_remove_caller (struct cgraph_edge *e)
1017 if (e->prev_callee)
1018 e->prev_callee->next_callee = e->next_callee;
1019 if (e->next_callee)
1020 e->next_callee->prev_callee = e->prev_callee;
1021 if (!e->prev_callee)
1023 if (e->indirect_unknown_callee)
1024 e->caller->indirect_calls = e->next_callee;
1025 else
1026 e->caller->callees = e->next_callee;
1028 if (e->caller->call_site_hash)
1029 htab_remove_elt_with_hash (e->caller->call_site_hash,
1030 e->call_stmt,
1031 htab_hash_pointer (e->call_stmt));
1034 /* Put the edge onto the free list. */
1036 static void
1037 cgraph_free_edge (struct cgraph_edge *e)
1039 int uid = e->uid;
1041 if (e->indirect_info)
1042 ggc_free (e->indirect_info);
1044 /* Clear out the edge so we do not dangle pointers. */
1045 memset (e, 0, sizeof (*e));
1046 e->uid = uid;
1047 NEXT_FREE_EDGE (e) = free_edges;
1048 free_edges = e;
1051 /* Remove the edge E in the cgraph. */
1053 void
1054 cgraph_remove_edge (struct cgraph_edge *e)
1056 /* Call all edge removal hooks. */
1057 cgraph_call_edge_removal_hooks (e);
1059 if (!e->indirect_unknown_callee)
1060 /* Remove from callers list of the callee. */
1061 cgraph_edge_remove_callee (e);
1063 /* Remove from callees list of the callers. */
1064 cgraph_edge_remove_caller (e);
1066 /* Put the edge onto the free list. */
1067 cgraph_free_edge (e);
1070 /* Set callee of call graph edge E and add it to the corresponding set of
1071 callers. */
1073 static void
1074 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1076 e->prev_caller = NULL;
1077 if (n->callers)
1078 n->callers->prev_caller = e;
1079 e->next_caller = n->callers;
1080 n->callers = e;
1081 e->callee = n;
1084 /* Turn edge E into speculative call calling N2. Update
1085 the profile so the direct call is taken COUNT times
1086 with FREQUENCY.
1088 At clone materialization time, the indirect call E will
1089 be expanded as:
1091 if (call_dest == N2)
1092 n2 ();
1093 else
1094 call call_dest
1096 At this time the function just creates the direct call,
1097 the referencd representing the if conditional and attaches
1098 them all to the orginal indirect call statement.
1100 Return direct edge created. */
1102 struct cgraph_edge *
1103 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1104 struct cgraph_node *n2,
1105 gcov_type direct_count,
1106 int direct_frequency)
1108 struct cgraph_node *n = e->caller;
1109 struct ipa_ref *ref = NULL;
1110 struct cgraph_edge *e2;
1112 if (dump_file)
1114 fprintf (dump_file, "Indirect call -> speculative call"
1115 " %s/%i => %s/%i\n",
1116 xstrdup (n->name ()), n->order,
1117 xstrdup (n2->name ()), n2->order);
1119 e->speculative = true;
1120 e2 = n->create_edge (n2, e->call_stmt, direct_count, direct_frequency);
1121 initialize_inline_failed (e2);
1122 e2->speculative = true;
1123 if (TREE_NOTHROW (n2->decl))
1124 e2->can_throw_external = false;
1125 else
1126 e2->can_throw_external = e->can_throw_external;
1127 e2->lto_stmt_uid = e->lto_stmt_uid;
1128 e->count -= e2->count;
1129 e->frequency -= e2->frequency;
1130 cgraph_call_edge_duplication_hooks (e, e2);
1131 ref = n->add_reference (n2, IPA_REF_ADDR, e->call_stmt);
1132 ref->lto_stmt_uid = e->lto_stmt_uid;
1133 ref->speculative = e->speculative;
1134 n2->mark_address_taken ();
1135 return e2;
1138 /* Speculative call consist of three components:
1139 1) an indirect edge representing the original call
1140 2) an direct edge representing the new call
1141 3) ADDR_EXPR reference representing the speculative check.
1142 All three components are attached to single statement (the indirect
1143 call) and if one of them exists, all of them must exist.
1145 Given speculative call edge E, return all three components.
1148 void
1149 cgraph_speculative_call_info (struct cgraph_edge *e,
1150 struct cgraph_edge *&direct,
1151 struct cgraph_edge *&indirect,
1152 struct ipa_ref *&reference)
1154 struct ipa_ref *ref;
1155 int i;
1156 struct cgraph_edge *e2;
1158 if (!e->indirect_unknown_callee)
1159 for (e2 = e->caller->indirect_calls;
1160 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1161 e2 = e2->next_callee)
1163 else
1165 e2 = e;
1166 /* We can take advantage of the call stmt hash. */
1167 if (e2->call_stmt)
1169 e = e->caller->get_edge (e2->call_stmt);
1170 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1172 else
1173 for (e = e->caller->callees;
1174 e2->call_stmt != e->call_stmt
1175 || e2->lto_stmt_uid != e->lto_stmt_uid;
1176 e = e->next_callee)
1179 gcc_assert (e->speculative && e2->speculative);
1180 direct = e;
1181 indirect = e2;
1183 reference = NULL;
1184 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1185 if (ref->speculative
1186 && ((ref->stmt && ref->stmt == e->call_stmt)
1187 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1189 reference = ref;
1190 break;
1193 /* Speculative edge always consist of all three components - direct edge,
1194 indirect and reference. */
1196 gcc_assert (e && e2 && ref);
1199 /* Redirect callee of E to N. The function does not update underlying
1200 call expression. */
1202 void
1203 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1205 /* Remove from callers list of the current callee. */
1206 cgraph_edge_remove_callee (e);
1208 /* Insert to callers list of the new callee. */
1209 cgraph_set_edge_callee (e, n);
1212 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1213 Remove the speculative call sequence and return edge representing the call.
1214 It is up to caller to redirect the call as appropriate. */
1216 struct cgraph_edge *
1217 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1219 struct cgraph_edge *e2;
1220 struct ipa_ref *ref;
1222 gcc_assert (edge->speculative);
1223 cgraph_speculative_call_info (edge, e2, edge, ref);
1224 if (!callee_decl
1225 || !ref->referred->semantically_equivalent_p
1226 (symtab_node::get (callee_decl)))
1228 if (dump_file)
1230 if (callee_decl)
1232 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1233 "turned out to have contradicting known target ",
1234 xstrdup (edge->caller->name ()), edge->caller->order,
1235 xstrdup (e2->callee->name ()), e2->callee->order);
1236 print_generic_expr (dump_file, callee_decl, 0);
1237 fprintf (dump_file, "\n");
1239 else
1241 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1242 xstrdup (edge->caller->name ()), edge->caller->order,
1243 xstrdup (e2->callee->name ()), e2->callee->order);
1247 else
1249 struct cgraph_edge *tmp = edge;
1250 if (dump_file)
1251 fprintf (dump_file, "Speculative call turned into direct call.\n");
1252 edge = e2;
1253 e2 = tmp;
1254 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1255 in the functions inlined through it. */
1257 edge->count += e2->count;
1258 edge->frequency += e2->frequency;
1259 if (edge->frequency > CGRAPH_FREQ_MAX)
1260 edge->frequency = CGRAPH_FREQ_MAX;
1261 edge->speculative = false;
1262 e2->speculative = false;
1263 ref->remove_reference ();
1264 if (e2->indirect_unknown_callee || e2->inline_failed)
1265 cgraph_remove_edge (e2);
1266 else
1267 e2->callee->remove_symbol_and_inline_clones ();
1268 if (edge->caller->call_site_hash)
1269 cgraph_update_edge_in_call_site_hash (edge);
1270 return edge;
1273 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1274 CALLEE. DELTA is an integer constant that is to be added to the this
1275 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1277 struct cgraph_edge *
1278 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1280 gcc_assert (edge->indirect_unknown_callee);
1282 /* If we are redirecting speculative call, make it non-speculative. */
1283 if (edge->indirect_unknown_callee && edge->speculative)
1285 edge = cgraph_resolve_speculation (edge, callee->decl);
1287 /* On successful speculation just return the pre existing direct edge. */
1288 if (!edge->indirect_unknown_callee)
1289 return edge;
1292 edge->indirect_unknown_callee = 0;
1293 ggc_free (edge->indirect_info);
1294 edge->indirect_info = NULL;
1296 /* Get the edge out of the indirect edge list. */
1297 if (edge->prev_callee)
1298 edge->prev_callee->next_callee = edge->next_callee;
1299 if (edge->next_callee)
1300 edge->next_callee->prev_callee = edge->prev_callee;
1301 if (!edge->prev_callee)
1302 edge->caller->indirect_calls = edge->next_callee;
1304 /* Put it into the normal callee list */
1305 edge->prev_callee = NULL;
1306 edge->next_callee = edge->caller->callees;
1307 if (edge->caller->callees)
1308 edge->caller->callees->prev_callee = edge;
1309 edge->caller->callees = edge;
1311 /* Insert to callers list of the new callee. */
1312 cgraph_set_edge_callee (edge, callee);
1314 if (edge->call_stmt)
1315 edge->call_stmt_cannot_inline_p
1316 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1317 false);
1319 /* We need to re-determine the inlining status of the edge. */
1320 initialize_inline_failed (edge);
1321 return edge;
1324 /* If necessary, change the function declaration in the call statement
1325 associated with E so that it corresponds to the edge callee. */
1327 gimple
1328 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1330 tree decl = gimple_call_fndecl (e->call_stmt);
1331 tree lhs = gimple_call_lhs (e->call_stmt);
1332 gimple new_stmt;
1333 gimple_stmt_iterator gsi;
1334 #ifdef ENABLE_CHECKING
1335 struct cgraph_node *node;
1336 #endif
1338 if (e->speculative)
1340 struct cgraph_edge *e2;
1341 gimple new_stmt;
1342 struct ipa_ref *ref;
1344 cgraph_speculative_call_info (e, e, e2, ref);
1345 /* If there already is an direct call (i.e. as a result of inliner's
1346 substitution), forget about speculating. */
1347 if (decl)
1348 e = cgraph_resolve_speculation (e, decl);
1349 /* If types do not match, speculation was likely wrong.
1350 The direct edge was posisbly redirected to the clone with a different
1351 signature. We did not update the call statement yet, so compare it
1352 with the reference that still points to the proper type. */
1353 else if (!gimple_check_call_matching_types (e->call_stmt,
1354 ref->referred->decl,
1355 true))
1357 if (dump_file)
1358 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1359 "Type mismatch.\n",
1360 xstrdup (e->caller->name ()),
1361 e->caller->order,
1362 xstrdup (e->callee->name ()),
1363 e->callee->order);
1364 e = cgraph_resolve_speculation (e, NULL);
1365 /* We are producing the final function body and will throw away the
1366 callgraph edges really soon. Reset the counts/frequencies to
1367 keep verifier happy in the case of roundoff errors. */
1368 e->count = gimple_bb (e->call_stmt)->count;
1369 e->frequency = compute_call_stmt_bb_frequency
1370 (e->caller->decl, gimple_bb (e->call_stmt));
1372 /* Expand speculation into GIMPLE code. */
1373 else
1375 if (dump_file)
1376 fprintf (dump_file,
1377 "Expanding speculative call of %s/%i -> %s/%i count:"
1378 "%"PRId64"\n",
1379 xstrdup (e->caller->name ()),
1380 e->caller->order,
1381 xstrdup (e->callee->name ()),
1382 e->callee->order,
1383 (int64_t)e->count);
1384 gcc_assert (e2->speculative);
1385 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1386 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
1387 e->count || e2->count
1388 ? RDIV (e->count * REG_BR_PROB_BASE,
1389 e->count + e2->count)
1390 : e->frequency || e2->frequency
1391 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1392 e->frequency + e2->frequency)
1393 : REG_BR_PROB_BASE / 2,
1394 e->count, e->count + e2->count);
1395 e->speculative = false;
1396 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1397 false);
1398 e->frequency = compute_call_stmt_bb_frequency
1399 (e->caller->decl, gimple_bb (e->call_stmt));
1400 e2->frequency = compute_call_stmt_bb_frequency
1401 (e2->caller->decl, gimple_bb (e2->call_stmt));
1402 e2->speculative = false;
1403 ref->speculative = false;
1404 ref->stmt = NULL;
1405 /* Indirect edges are not both in the call site hash.
1406 get it updated. */
1407 if (e->caller->call_site_hash)
1408 cgraph_update_edge_in_call_site_hash (e2);
1409 pop_cfun ();
1410 /* Continue redirecting E to proper target. */
1414 if (e->indirect_unknown_callee
1415 || decl == e->callee->decl)
1416 return e->call_stmt;
1418 #ifdef ENABLE_CHECKING
1419 if (decl)
1421 node = cgraph_node::get (decl);
1422 gcc_assert (!node || !node->clone.combined_args_to_skip);
1424 #endif
1426 if (cgraph_dump_file)
1428 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1429 xstrdup (e->caller->name ()), e->caller->order,
1430 xstrdup (e->callee->name ()), e->callee->order);
1431 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1432 if (e->callee->clone.combined_args_to_skip)
1434 fprintf (cgraph_dump_file, " combined args to skip: ");
1435 dump_bitmap (cgraph_dump_file,
1436 e->callee->clone.combined_args_to_skip);
1440 if (e->callee->clone.combined_args_to_skip)
1442 int lp_nr;
1444 new_stmt
1445 = gimple_call_copy_skip_args (e->call_stmt,
1446 e->callee->clone.combined_args_to_skip);
1447 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1448 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1450 if (gimple_vdef (new_stmt)
1451 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1452 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1454 gsi = gsi_for_stmt (e->call_stmt);
1455 gsi_replace (&gsi, new_stmt, false);
1456 /* We need to defer cleaning EH info on the new statement to
1457 fixup-cfg. We may not have dominator information at this point
1458 and thus would end up with unreachable blocks and have no way
1459 to communicate that we need to run CFG cleanup then. */
1460 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1461 if (lp_nr != 0)
1463 remove_stmt_from_eh_lp (e->call_stmt);
1464 add_stmt_to_eh_lp (new_stmt, lp_nr);
1467 else
1469 new_stmt = e->call_stmt;
1470 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1471 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1474 /* If the call becomes noreturn, remove the lhs. */
1475 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1477 if (TREE_CODE (lhs) == SSA_NAME)
1479 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1480 TREE_TYPE (lhs), NULL);
1481 var = get_or_create_ssa_default_def
1482 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1483 gimple set_stmt = gimple_build_assign (lhs, var);
1484 gsi = gsi_for_stmt (new_stmt);
1485 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1486 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1488 gimple_call_set_lhs (new_stmt, NULL_TREE);
1489 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1492 /* If new callee has no static chain, remove it. */
1493 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1495 gimple_call_set_chain (new_stmt, NULL);
1496 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1499 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1501 if (cgraph_dump_file)
1503 fprintf (cgraph_dump_file, " updated to:");
1504 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1506 return new_stmt;
1509 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1510 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1511 of OLD_STMT if it was previously call statement.
1512 If NEW_STMT is NULL, the call has been dropped without any
1513 replacement. */
1515 static void
1516 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1517 gimple old_stmt, tree old_call,
1518 gimple new_stmt)
1520 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1521 ? gimple_call_fndecl (new_stmt) : 0;
1523 /* We are seeing indirect calls, then there is nothing to update. */
1524 if (!new_call && !old_call)
1525 return;
1526 /* See if we turned indirect call into direct call or folded call to one builtin
1527 into different builtin. */
1528 if (old_call != new_call)
1530 struct cgraph_edge *e = node->get_edge (old_stmt);
1531 struct cgraph_edge *ne = NULL;
1532 gcov_type count;
1533 int frequency;
1535 if (e)
1537 /* See if the edge is already there and has the correct callee. It
1538 might be so because of indirect inlining has already updated
1539 it. We also might've cloned and redirected the edge. */
1540 if (new_call && e->callee)
1542 struct cgraph_node *callee = e->callee;
1543 while (callee)
1545 if (callee->decl == new_call
1546 || callee->former_clone_of == new_call)
1548 cgraph_set_call_stmt (e, new_stmt);
1549 return;
1551 callee = callee->clone_of;
1555 /* Otherwise remove edge and create new one; we can't simply redirect
1556 since function has changed, so inline plan and other information
1557 attached to edge is invalid. */
1558 count = e->count;
1559 frequency = e->frequency;
1560 if (e->indirect_unknown_callee || e->inline_failed)
1561 cgraph_remove_edge (e);
1562 else
1563 e->callee->remove_symbol_and_inline_clones ();
1565 else if (new_call)
1567 /* We are seeing new direct call; compute profile info based on BB. */
1568 basic_block bb = gimple_bb (new_stmt);
1569 count = bb->count;
1570 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1571 bb);
1574 if (new_call)
1576 ne = node->create_edge (cgraph_node::get_create (new_call),
1577 new_stmt, count, frequency);
1578 gcc_assert (ne->inline_failed);
1581 /* We only updated the call stmt; update pointer in cgraph edge.. */
1582 else if (old_stmt != new_stmt)
1583 cgraph_set_call_stmt (node->get_edge (old_stmt), new_stmt);
1586 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1587 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1588 of OLD_STMT before it was updated (updating can happen inplace). */
1590 void
1591 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1593 struct cgraph_node *orig = cgraph_node::get (cfun->decl);
1594 struct cgraph_node *node;
1596 gcc_checking_assert (orig);
1597 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1598 if (orig->clones)
1599 for (node = orig->clones; node != orig;)
1601 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1602 if (node->clones)
1603 node = node->clones;
1604 else if (node->next_sibling_clone)
1605 node = node->next_sibling_clone;
1606 else
1608 while (node != orig && !node->next_sibling_clone)
1609 node = node->clone_of;
1610 if (node != orig)
1611 node = node->next_sibling_clone;
1617 /* Remove all callees from the node. */
1619 void
1620 cgraph_node::remove_callees (void)
1622 struct cgraph_edge *e, *f;
1624 /* It is sufficient to remove the edges from the lists of callers of
1625 the callees. The callee list of the node can be zapped with one
1626 assignment. */
1627 for (e = callees; 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 for (e = indirect_calls; e; e = f)
1637 f = e->next_callee;
1638 cgraph_call_edge_removal_hooks (e);
1639 if (!e->indirect_unknown_callee)
1640 cgraph_edge_remove_callee (e);
1641 cgraph_free_edge (e);
1643 indirect_calls = NULL;
1644 callees = NULL;
1645 if (call_site_hash)
1647 htab_delete (call_site_hash);
1648 call_site_hash = NULL;
1652 /* Remove all callers from the node. */
1654 void
1655 cgraph_node::remove_callers (void)
1657 struct cgraph_edge *e, *f;
1659 /* It is sufficient to remove the edges from the lists of callees of
1660 the callers. The caller list of the node can be zapped with one
1661 assignment. */
1662 for (e = callers; e; e = f)
1664 f = e->next_caller;
1665 cgraph_call_edge_removal_hooks (e);
1666 cgraph_edge_remove_caller (e);
1667 cgraph_free_edge (e);
1669 callers = NULL;
1672 /* Helper function for cgraph_release_function_body and free_lang_data.
1673 It releases body from function DECL without having to inspect its
1674 possibly non-existent symtab node. */
1676 void
1677 release_function_body (tree decl)
1679 if (DECL_STRUCT_FUNCTION (decl))
1681 push_cfun (DECL_STRUCT_FUNCTION (decl));
1682 if (cfun->cfg
1683 && current_loops)
1685 cfun->curr_properties &= ~PROP_loops;
1686 loop_optimizer_finalize ();
1688 if (cfun->gimple_df)
1690 delete_tree_ssa ();
1691 delete_tree_cfg_annotations ();
1692 cfun->eh = NULL;
1694 if (cfun->cfg)
1696 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1697 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1698 clear_edges ();
1699 cfun->cfg = NULL;
1701 if (cfun->value_histograms)
1702 free_histograms ();
1703 pop_cfun ();
1704 gimple_set_body (decl, NULL);
1705 /* Struct function hangs a lot of data that would leak if we didn't
1706 removed all pointers to it. */
1707 ggc_free (DECL_STRUCT_FUNCTION (decl));
1708 DECL_STRUCT_FUNCTION (decl) = NULL;
1710 DECL_SAVED_TREE (decl) = NULL;
1713 /* Release memory used to represent body of function.
1714 Use this only for functions that are released before being translated to
1715 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1716 are free'd in final.c via free_after_compilation(). */
1718 void
1719 cgraph_node::release_body (void)
1721 ipa_transforms_to_apply.release ();
1722 if (!used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1724 DECL_RESULT (decl) = NULL;
1725 DECL_ARGUMENTS (decl) = NULL;
1727 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1728 of its associated function function declaration because it's
1729 needed to emit debug info later. */
1730 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1731 DECL_INITIAL (decl) = error_mark_node;
1732 release_function_body (decl);
1733 if (lto_file_data)
1734 lto_free_function_in_decl_state_for_node (this);
1737 /* Remove function from symbol table. */
1739 void
1740 cgraph_node::remove (void)
1742 struct cgraph_node *n;
1743 int uid = this->uid;
1745 cgraph_call_node_removal_hooks (this);
1746 remove_callers ();
1747 remove_callees ();
1748 ipa_transforms_to_apply.release ();
1750 /* Incremental inlining access removed nodes stored in the postorder list.
1752 force_output = false;
1753 forced_by_abi = false;
1754 for (n = nested; n; n = n->next_nested)
1755 n->origin = NULL;
1756 nested = NULL;
1757 if (origin)
1759 struct cgraph_node **node2 = &origin->nested;
1761 while (*node2 != this)
1762 node2 = &(*node2)->next_nested;
1763 *node2 = next_nested;
1765 unregister ();
1766 if (prev_sibling_clone)
1767 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1768 else if (clone_of)
1769 clone_of->clones = next_sibling_clone;
1770 if (next_sibling_clone)
1771 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1772 if (clones)
1774 struct cgraph_node *n, *next;
1776 if (clone_of)
1778 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1779 n->clone_of = clone_of;
1780 n->clone_of = clone_of;
1781 n->next_sibling_clone = clone_of->clones;
1782 if (clone_of->clones)
1783 clone_of->clones->prev_sibling_clone = n;
1784 clone_of->clones = clones;
1786 else
1788 /* We are removing node with clones. This makes clones inconsistent,
1789 but assume they will be removed subsequently and just keep clone
1790 tree intact. This can happen in unreachable function removal since
1791 we remove unreachable functions in random order, not by bottom-up
1792 walk of clone trees. */
1793 for (n = clones; n; n = next)
1795 next = n->next_sibling_clone;
1796 n->next_sibling_clone = NULL;
1797 n->prev_sibling_clone = NULL;
1798 n->clone_of = NULL;
1803 /* While all the clones are removed after being proceeded, the function
1804 itself is kept in the cgraph even after it is compiled. Check whether
1805 we are done with this body and reclaim it proactively if this is the case.
1807 if (cgraph_state != CGRAPH_LTO_STREAMING)
1809 n = cgraph_node::get (decl);
1810 if (!n
1811 || (!n->clones && !n->clone_of && !n->global.inlined_to
1812 && (cgraph_global_info_ready
1813 && (TREE_ASM_WRITTEN (n->decl)
1814 || DECL_EXTERNAL (n->decl)
1815 || !n->analyzed
1816 || (!flag_wpa && n->in_other_partition)))))
1817 release_body ();
1820 decl = NULL;
1821 if (call_site_hash)
1823 htab_delete (call_site_hash);
1824 call_site_hash = NULL;
1826 cgraph_n_nodes--;
1828 /* Clear out the node to NULL all pointers and add the node to the free
1829 list. */
1830 memset (this, 0, sizeof (*this));
1831 type = SYMTAB_FUNCTION;
1832 this->uid = uid;
1833 SET_NEXT_FREE_NODE (this, free_nodes);
1834 free_nodes = this;
1837 /* Likewise indicate that a node is having address taken. */
1839 void
1840 cgraph_node::mark_address_taken (void)
1842 /* Indirect inlining can figure out that all uses of the address are
1843 inlined. */
1844 if (global.inlined_to)
1846 gcc_assert (cfun->after_inlining);
1847 gcc_assert (callers->indirect_inlining_edge);
1848 return;
1850 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1851 IPA_REF_ADDR reference exists (and thus it should be set on node
1852 representing alias we take address of) and as a test whether address
1853 of the object was taken (and thus it should be set on node alias is
1854 referring to). We should remove the first use and the remove the
1855 following set. */
1856 address_taken = 1;
1857 cgraph_node *node = ultimate_alias_target ();
1858 node->address_taken = 1;
1861 /* Return local info for the compiled function. */
1863 struct cgraph_local_info *
1864 cgraph_local_info (tree decl)
1866 struct cgraph_node *node;
1868 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1869 node = cgraph_node::get (decl);
1870 if (!node)
1871 return NULL;
1872 return &node->local;
1875 /* Return local info for the compiled function. */
1877 struct cgraph_global_info *
1878 cgraph_global_info (tree decl)
1880 struct cgraph_node *node;
1882 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1883 node = cgraph_node::get (decl);
1884 if (!node)
1885 return NULL;
1886 return &node->global;
1889 /* Return local info for the compiled function. */
1891 struct cgraph_rtl_info *
1892 cgraph_rtl_info (tree decl)
1894 struct cgraph_node *node;
1896 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1897 node = cgraph_node::get (decl);
1898 if (!node
1899 || (decl != current_function_decl
1900 && !TREE_ASM_WRITTEN (node->decl)))
1901 return NULL;
1902 return &node->rtl;
1905 /* Return a string describing the failure REASON. */
1907 const char*
1908 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1910 #undef DEFCIFCODE
1911 #define DEFCIFCODE(code, type, string) string,
1913 static const char *cif_string_table[CIF_N_REASONS] = {
1914 #include "cif-code.def"
1917 /* Signedness of an enum type is implementation defined, so cast it
1918 to unsigned before testing. */
1919 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1920 return cif_string_table[reason];
1923 /* Return a type describing the failure REASON. */
1925 cgraph_inline_failed_type_t
1926 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1928 #undef DEFCIFCODE
1929 #define DEFCIFCODE(code, type, string) type,
1931 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1932 #include "cif-code.def"
1935 /* Signedness of an enum type is implementation defined, so cast it
1936 to unsigned before testing. */
1937 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1938 return cif_type_table[reason];
1941 /* Names used to print out the availability enum. */
1942 const char * const cgraph_availability_names[] =
1943 {"unset", "not_available", "overwritable", "available", "local"};
1946 /* Dump call graph node to file F. */
1948 void
1949 cgraph_node::dump (FILE *f)
1951 struct cgraph_edge *edge;
1952 int indirect_calls_count = 0;
1954 dump_base (f);
1956 if (global.inlined_to)
1957 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1958 xstrdup (name ()),
1959 order,
1960 xstrdup (global.inlined_to->name ()),
1961 global.inlined_to->order);
1962 if (clone_of)
1963 fprintf (f, " Clone of %s/%i\n",
1964 clone_of->asm_name (),
1965 clone_of->order);
1966 if (cgraph_function_flags_ready)
1967 fprintf (f, " Availability: %s\n",
1968 cgraph_availability_names [get_availability ()]);
1970 if (profile_id)
1971 fprintf (f, " Profile id: %i\n",
1972 profile_id);
1973 fprintf (f, " First run: %i\n", tp_first_run);
1974 fprintf (f, " Function flags:");
1975 if (count)
1976 fprintf (f, " executed %"PRId64"x",
1977 (int64_t)count);
1978 if (origin)
1979 fprintf (f, " nested in: %s", origin->asm_name ());
1980 if (gimple_has_body_p (decl))
1981 fprintf (f, " body");
1982 if (process)
1983 fprintf (f, " process");
1984 if (local.local)
1985 fprintf (f, " local");
1986 if (local.redefined_extern_inline)
1987 fprintf (f, " redefined_extern_inline");
1988 if (only_called_at_startup)
1989 fprintf (f, " only_called_at_startup");
1990 if (only_called_at_exit)
1991 fprintf (f, " only_called_at_exit");
1992 if (tm_clone)
1993 fprintf (f, " tm_clone");
1994 if (DECL_STATIC_CONSTRUCTOR (decl))
1995 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
1996 if (DECL_STATIC_DESTRUCTOR (decl))
1997 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
1999 fprintf (f, "\n");
2001 if (thunk.thunk_p)
2003 fprintf (f, " Thunk");
2004 if (thunk.alias)
2005 fprintf (f, " of %s (asm: %s)",
2006 lang_hooks.decl_printable_name (thunk.alias, 2),
2007 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2008 fprintf (f, " fixed offset %i virtual value %i has "
2009 "virtual offset %i)\n",
2010 (int)thunk.fixed_offset,
2011 (int)thunk.virtual_value,
2012 (int)thunk.virtual_offset_p);
2014 if (alias && thunk.alias
2015 && DECL_P (thunk.alias))
2017 fprintf (f, " Alias of %s",
2018 lang_hooks.decl_printable_name (thunk.alias, 2));
2019 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2020 fprintf (f, " (asm: %s)",
2021 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2022 fprintf (f, "\n");
2025 fprintf (f, " Called by: ");
2027 for (edge = callers; edge; edge = edge->next_caller)
2029 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2030 edge->caller->order);
2031 if (edge->count)
2032 fprintf (f, "(%"PRId64"x) ",
2033 (int64_t)edge->count);
2034 if (edge->frequency)
2035 fprintf (f, "(%.2f per call) ",
2036 edge->frequency / (double)CGRAPH_FREQ_BASE);
2037 if (edge->speculative)
2038 fprintf (f, "(speculative) ");
2039 if (!edge->inline_failed)
2040 fprintf (f, "(inlined) ");
2041 if (edge->indirect_inlining_edge)
2042 fprintf (f, "(indirect_inlining) ");
2043 if (edge->can_throw_external)
2044 fprintf (f, "(can throw external) ");
2047 fprintf (f, "\n Calls: ");
2048 for (edge = callees; edge; edge = edge->next_callee)
2050 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2051 edge->callee->order);
2052 if (edge->speculative)
2053 fprintf (f, "(speculative) ");
2054 if (!edge->inline_failed)
2055 fprintf (f, "(inlined) ");
2056 if (edge->indirect_inlining_edge)
2057 fprintf (f, "(indirect_inlining) ");
2058 if (edge->count)
2059 fprintf (f, "(%"PRId64"x) ",
2060 (int64_t)edge->count);
2061 if (edge->frequency)
2062 fprintf (f, "(%.2f per call) ",
2063 edge->frequency / (double)CGRAPH_FREQ_BASE);
2064 if (edge->can_throw_external)
2065 fprintf (f, "(can throw external) ");
2067 fprintf (f, "\n");
2069 for (edge = indirect_calls; edge; edge = edge->next_callee)
2070 indirect_calls_count++;
2071 if (indirect_calls_count)
2072 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2073 indirect_calls_count);
2076 /* Dump call graph node NODE to stderr. */
2078 DEBUG_FUNCTION void
2079 cgraph_node::debug (void)
2081 dump (stderr);
2084 /* Dump the callgraph to file F. */
2086 void
2087 cgraph_node::dump_cgraph (FILE *f)
2089 struct cgraph_node *node;
2091 fprintf (f, "callgraph:\n\n");
2092 FOR_EACH_FUNCTION (node)
2093 node->dump (f);
2096 /* Return true when the DECL can possibly be inlined. */
2098 bool
2099 cgraph_function_possibly_inlined_p (tree decl)
2101 if (!cgraph_global_info_ready)
2102 return !DECL_UNINLINABLE (decl);
2103 return DECL_POSSIBLY_INLINED (decl);
2106 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2107 void
2108 cgraph_node::unnest (void)
2110 struct cgraph_node **node2 = &origin->nested;
2111 gcc_assert (origin);
2113 while (*node2 != this)
2114 node2 = &(*node2)->next_nested;
2115 *node2 = next_nested;
2116 origin = NULL;
2119 /* Return function availability. See cgraph.h for description of individual
2120 return values. */
2121 enum availability
2122 cgraph_node::get_availability (void)
2124 enum availability avail;
2125 if (!analyzed)
2126 avail = AVAIL_NOT_AVAILABLE;
2127 else if (local.local)
2128 avail = AVAIL_LOCAL;
2129 else if (alias && weakref)
2130 ultimate_alias_target (&avail);
2131 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2132 avail = AVAIL_INTERPOSABLE;
2133 else if (!externally_visible)
2134 avail = AVAIL_AVAILABLE;
2135 /* Inline functions are safe to be analyzed even if their symbol can
2136 be overwritten at runtime. It is not meaningful to enforce any sane
2137 behaviour on replacing inline function by different body. */
2138 else if (DECL_DECLARED_INLINE_P (decl))
2139 avail = AVAIL_AVAILABLE;
2141 /* If the function can be overwritten, return OVERWRITABLE. Take
2142 care at least of two notable extensions - the COMDAT functions
2143 used to share template instantiations in C++ (this is symmetric
2144 to code cp_cannot_inline_tree_fn and probably shall be shared and
2145 the inlinability hooks completely eliminated).
2147 ??? Does the C++ one definition rule allow us to always return
2148 AVAIL_AVAILABLE here? That would be good reason to preserve this
2149 bit. */
2151 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2152 avail = AVAIL_INTERPOSABLE;
2153 else avail = AVAIL_AVAILABLE;
2155 return avail;
2158 /* Worker for cgraph_node_can_be_local_p. */
2159 static bool
2160 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node, void *)
2162 return !(!node->force_output
2163 && ((DECL_COMDAT (node->decl)
2164 && !node->forced_by_abi
2165 && !node->used_from_object_file_p ()
2166 && !node->same_comdat_group)
2167 || !node->externally_visible));
2170 /* Return true if cgraph_node can be made local for API change.
2171 Extern inline functions and C++ COMDAT functions can be made local
2172 at the expense of possible code size growth if function is used in multiple
2173 compilation units. */
2174 bool
2175 cgraph_node::can_be_local_p (void)
2177 return (!address_taken
2178 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2179 NULL, true));
2182 /* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
2183 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2184 skipped. */
2186 bool
2187 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2188 (cgraph_node *, void *),
2189 void *data,
2190 bool include_overwritable)
2192 struct cgraph_edge *e;
2193 struct ipa_ref *ref;
2195 if (callback (this, data))
2196 return true;
2197 for (e = callers; e; e = e->next_caller)
2198 if (e->caller->thunk.thunk_p
2199 && (include_overwritable
2200 || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2201 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2202 include_overwritable))
2203 return true;
2205 FOR_EACH_ALIAS (this, ref)
2207 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2208 if (include_overwritable
2209 || alias->get_availability () > AVAIL_INTERPOSABLE)
2210 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2211 include_overwritable))
2212 return true;
2214 return false;
2217 /* Call calback on function and aliases associated to the function.
2218 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2219 skipped. */
2221 bool
2222 cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2223 void *),
2224 void *data,
2225 bool include_overwritable)
2227 struct ipa_ref *ref;
2229 if (callback (this, data))
2230 return true;
2232 FOR_EACH_ALIAS (this, ref)
2234 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2235 if (include_overwritable
2236 || alias->get_availability () > AVAIL_INTERPOSABLE)
2237 if (alias->call_for_symbol_and_aliases (callback, data,
2238 include_overwritable))
2239 return true;
2241 return false;
2244 /* Worker to bring NODE local. */
2246 bool
2247 cgraph_node::make_local (struct cgraph_node *node, void *)
2249 gcc_checking_assert (node->can_be_local_p ());
2250 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2252 node->make_decl_local ();
2253 node->set_section (NULL);
2254 node->set_comdat_group (NULL);
2255 node->externally_visible = false;
2256 node->forced_by_abi = false;
2257 node->local.local = true;
2258 node->set_section (NULL);
2259 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2260 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2261 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2262 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2264 return false;
2267 /* Bring cgraph node local. */
2269 void
2270 cgraph_node::make_local (void)
2272 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2275 /* Worker to set nothrow flag. */
2277 static bool
2278 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2280 struct cgraph_edge *e;
2282 TREE_NOTHROW (node->decl) = data != NULL;
2284 if (data != NULL)
2285 for (e = node->callers; e; e = e->next_caller)
2286 e->can_throw_external = false;
2287 return false;
2290 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2291 if any to NOTHROW. */
2293 void
2294 cgraph_node::set_nothrow_flag (bool nothrow)
2296 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2297 (void *)(size_t)nothrow, false);
2300 /* Worker to set const flag. */
2302 static bool
2303 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2305 /* Static constructors and destructors without a side effect can be
2306 optimized out. */
2307 if (data && !((size_t)data & 2))
2309 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2310 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2311 if (DECL_STATIC_DESTRUCTOR (node->decl))
2312 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2314 TREE_READONLY (node->decl) = data != NULL;
2315 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2316 return false;
2319 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2320 if any to READONLY. */
2322 void
2323 cgraph_node::set_const_flag (bool readonly, bool looping)
2325 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2326 (void *)(size_t)(readonly + (int)looping * 2),
2327 false);
2330 /* Worker to set pure flag. */
2332 static bool
2333 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2335 /* Static constructors and destructors without a side effect can be
2336 optimized out. */
2337 if (data && !((size_t)data & 2))
2339 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2340 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2341 if (DECL_STATIC_DESTRUCTOR (node->decl))
2342 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2344 DECL_PURE_P (node->decl) = data != NULL;
2345 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2346 return false;
2349 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2350 if any to PURE. */
2352 void
2353 cgraph_node::set_pure_flag (bool pure, bool looping)
2355 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2356 (void *)(size_t)(pure + (int)looping * 2),
2357 false);
2360 /* Return true when cgraph_node can not return or throw and thus
2361 it is safe to ignore its side effects for IPA analysis. */
2363 bool
2364 cgraph_node::cannot_return_p (void)
2366 int flags = flags_from_decl_or_type (decl);
2367 if (!flag_exceptions)
2368 return (flags & ECF_NORETURN) != 0;
2369 else
2370 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2371 == (ECF_NORETURN | ECF_NOTHROW));
2374 /* Return true when call of E can not lead to return from caller
2375 and thus it is safe to ignore its side effects for IPA analysis
2376 when computing side effects of the caller.
2377 FIXME: We could actually mark all edges that have no reaching
2378 patch to the exit block or throw to get better results. */
2379 bool
2380 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2382 if (e->caller->cannot_return_p ())
2383 return true;
2384 if (e->indirect_unknown_callee)
2386 int flags = e->indirect_info->ecf_flags;
2387 if (!flag_exceptions)
2388 return (flags & ECF_NORETURN) != 0;
2389 else
2390 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2391 == (ECF_NORETURN | ECF_NOTHROW));
2393 else
2394 return e->callee->cannot_return_p ();
2397 /* Return true when function can be removed from callgraph
2398 if all direct calls are eliminated. */
2400 bool
2401 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2403 gcc_assert (!global.inlined_to);
2404 /* Extern inlines can always go, we will use the external definition. */
2405 if (DECL_EXTERNAL (decl))
2406 return true;
2407 /* When function is needed, we can not remove it. */
2408 if (force_output || used_from_other_partition)
2409 return false;
2410 if (DECL_STATIC_CONSTRUCTOR (decl)
2411 || DECL_STATIC_DESTRUCTOR (decl))
2412 return false;
2413 /* Only COMDAT functions can be removed if externally visible. */
2414 if (externally_visible
2415 && (!DECL_COMDAT (decl)
2416 || forced_by_abi
2417 || used_from_object_file_p ()))
2418 return false;
2419 return true;
2422 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2424 static bool
2425 nonremovable_p (struct cgraph_node *node, void *)
2427 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2430 /* Return true when function cgraph_node and its aliases can be removed from
2431 callgraph if all direct calls are eliminated. */
2433 bool
2434 cgraph_node::can_remove_if_no_direct_calls_p (void)
2436 /* Extern inlines can always go, we will use the external definition. */
2437 if (DECL_EXTERNAL (decl))
2438 return true;
2439 if (address_taken)
2440 return false;
2441 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2444 /* Return true when function cgraph_node can be expected to be removed
2445 from program when direct calls in this compilation unit are removed.
2447 As a special case COMDAT functions are
2448 cgraph_can_remove_if_no_direct_calls_p while the are not
2449 cgraph_only_called_directly_p (it is possible they are called from other
2450 unit)
2452 This function behaves as cgraph_only_called_directly_p because eliminating
2453 all uses of COMDAT function does not make it necessarily disappear from
2454 the program unless we are compiling whole program or we do LTO. In this
2455 case we know we win since dynamic linking will not really discard the
2456 linkonce section. */
2458 bool
2459 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2461 gcc_assert (!global.inlined_to);
2463 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2464 NULL, true))
2465 return false;
2466 if (!in_lto_p && !flag_whole_program)
2467 return only_called_directly_p ();
2468 else
2470 if (DECL_EXTERNAL (decl))
2471 return true;
2472 return can_remove_if_no_direct_calls_p ();
2477 /* Worker for cgraph_only_called_directly_p. */
2479 static bool
2480 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *)
2482 return !node->only_called_directly_or_aliased_p ();
2485 /* Return true when function cgraph_node and all its aliases are only called
2486 directly.
2487 i.e. it is not externally visible, address was not taken and
2488 it is not used in any other non-standard way. */
2490 bool
2491 cgraph_node::only_called_directly_p (void)
2493 gcc_assert (ultimate_alias_target () == this);
2494 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2495 NULL, true);
2499 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2501 static bool
2502 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2504 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2505 struct cgraph_edge *cs;
2506 enum availability avail;
2507 node->ultimate_alias_target (&avail);
2509 if (avail > AVAIL_INTERPOSABLE)
2510 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2511 if (!cs->indirect_inlining_edge)
2512 redirect_callers->safe_push (cs);
2513 return false;
2516 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2517 cgraph_node (i.e. are not overwritable). */
2519 vec<cgraph_edge *>
2520 cgraph_node::collect_callers (void)
2522 vec<cgraph_edge *> redirect_callers = vNULL;
2523 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2524 &redirect_callers, false);
2525 return redirect_callers;
2528 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2530 static bool
2531 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2533 bool skipped_thunk = false;
2534 node = node->ultimate_alias_target ();
2535 node2 = node2->ultimate_alias_target ();
2537 /* There are no virtual clones of thunks so check former_clone_of or if we
2538 might have skipped thunks because this adjustments are no longer
2539 necessary. */
2540 while (node->thunk.thunk_p)
2542 if (node2->former_clone_of == node->decl)
2543 return true;
2544 if (!node->thunk.this_adjusting)
2545 return false;
2546 node = node->callees->callee->ultimate_alias_target ();
2547 skipped_thunk = true;
2550 if (skipped_thunk)
2552 if (!node2->clone.args_to_skip
2553 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2554 return false;
2555 if (node2->former_clone_of == node->decl)
2556 return true;
2557 else if (!node2->clone_of)
2558 return false;
2561 while (node != node2 && node2)
2562 node2 = node2->clone_of;
2563 return node2 != NULL;
2566 /* Verify edge E count and frequency. */
2568 static bool
2569 verify_edge_count_and_frequency (struct cgraph_edge *e)
2571 bool error_found = false;
2572 if (e->count < 0)
2574 error ("caller edge count is negative");
2575 error_found = true;
2577 if (e->frequency < 0)
2579 error ("caller edge frequency is negative");
2580 error_found = true;
2582 if (e->frequency > CGRAPH_FREQ_MAX)
2584 error ("caller edge frequency is too large");
2585 error_found = true;
2587 if (gimple_has_body_p (e->caller->decl)
2588 && !e->caller->global.inlined_to
2589 && !e->speculative
2590 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2591 Remove this once edges are actually removed from the function at that time. */
2592 && (e->frequency
2593 || (inline_edge_summary_vec.exists ()
2594 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2595 || !inline_edge_summary (e)->predicate)))
2596 && (e->frequency
2597 != compute_call_stmt_bb_frequency (e->caller->decl,
2598 gimple_bb (e->call_stmt))))
2600 error ("caller edge frequency %i does not match BB frequency %i",
2601 e->frequency,
2602 compute_call_stmt_bb_frequency (e->caller->decl,
2603 gimple_bb (e->call_stmt)));
2604 error_found = true;
2606 return error_found;
2609 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2610 static void
2611 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2613 bool fndecl_was_null = false;
2614 /* debug_gimple_stmt needs correct cfun */
2615 if (cfun != this_cfun)
2616 set_cfun (this_cfun);
2617 /* ...and an actual current_function_decl */
2618 if (!current_function_decl)
2620 current_function_decl = this_cfun->decl;
2621 fndecl_was_null = true;
2623 debug_gimple_stmt (stmt);
2624 if (fndecl_was_null)
2625 current_function_decl = NULL;
2628 /* Verify that call graph edge E corresponds to DECL from the associated
2629 statement. Return true if the verification should fail. */
2631 static bool
2632 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2634 struct cgraph_node *node;
2636 if (!decl || e->callee->global.inlined_to)
2637 return false;
2638 if (cgraph_state == CGRAPH_LTO_STREAMING)
2639 return false;
2640 node = cgraph_node::get (decl);
2642 /* We do not know if a node from a different partition is an alias or what it
2643 aliases and therefore cannot do the former_clone_of check reliably. When
2644 body_removed is set, we have lost all information about what was alias or
2645 thunk of and also cannot proceed. */
2646 if (!node
2647 || node->body_removed
2648 || node->in_other_partition
2649 || e->callee->in_other_partition)
2650 return false;
2652 node = node->ultimate_alias_target ();
2654 /* Optimizers can redirect unreachable calls or calls triggering undefined
2655 behaviour to builtin_unreachable. */
2656 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2657 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2658 return false;
2660 if (e->callee->former_clone_of != node->decl
2661 && (node != e->callee->ultimate_alias_target ())
2662 && !clone_of_p (node, e->callee))
2663 return true;
2664 else
2665 return false;
2668 /* Verify cgraph nodes of given cgraph node. */
2669 DEBUG_FUNCTION void
2670 cgraph_node::verify_node (void)
2672 struct cgraph_edge *e;
2673 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2674 basic_block this_block;
2675 gimple_stmt_iterator gsi;
2676 bool error_found = false;
2678 if (seen_error ())
2679 return;
2681 timevar_push (TV_CGRAPH_VERIFY);
2682 error_found |= verify_base ();
2683 for (e = callees; e; e = e->next_callee)
2684 if (e->aux)
2686 error ("aux field set for edge %s->%s",
2687 identifier_to_locale (e->caller->name ()),
2688 identifier_to_locale (e->callee->name ()));
2689 error_found = true;
2691 if (count < 0)
2693 error ("execution count is negative");
2694 error_found = true;
2696 if (global.inlined_to && same_comdat_group)
2698 error ("inline clone in same comdat group list");
2699 error_found = true;
2701 if (!definition && !in_other_partition && local.local)
2703 error ("local symbols must be defined");
2704 error_found = true;
2706 if (global.inlined_to && externally_visible)
2708 error ("externally visible inline clone");
2709 error_found = true;
2711 if (global.inlined_to && address_taken)
2713 error ("inline clone with address taken");
2714 error_found = true;
2716 if (global.inlined_to && force_output)
2718 error ("inline clone is forced to output");
2719 error_found = true;
2721 for (e = indirect_calls; e; e = e->next_callee)
2723 if (e->aux)
2725 error ("aux field set for indirect edge from %s",
2726 identifier_to_locale (e->caller->name ()));
2727 error_found = true;
2729 if (!e->indirect_unknown_callee
2730 || !e->indirect_info)
2732 error ("An indirect edge from %s is not marked as indirect or has "
2733 "associated indirect_info, the corresponding statement is: ",
2734 identifier_to_locale (e->caller->name ()));
2735 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2736 error_found = true;
2739 bool check_comdat = comdat_local_p ();
2740 for (e = callers; e; e = e->next_caller)
2742 if (verify_edge_count_and_frequency (e))
2743 error_found = true;
2744 if (check_comdat
2745 && !in_same_comdat_group_p (e->caller))
2747 error ("comdat-local function called by %s outside its comdat",
2748 identifier_to_locale (e->caller->name ()));
2749 error_found = true;
2751 if (!e->inline_failed)
2753 if (global.inlined_to
2754 != (e->caller->global.inlined_to
2755 ? e->caller->global.inlined_to : e->caller))
2757 error ("inlined_to pointer is wrong");
2758 error_found = true;
2760 if (callers->next_caller)
2762 error ("multiple inline callers");
2763 error_found = true;
2766 else
2767 if (global.inlined_to)
2769 error ("inlined_to pointer set for noninline callers");
2770 error_found = true;
2773 for (e = indirect_calls; e; e = e->next_callee)
2774 if (verify_edge_count_and_frequency (e))
2775 error_found = true;
2776 if (!callers && global.inlined_to)
2778 error ("inlined_to pointer is set but no predecessors found");
2779 error_found = true;
2781 if (global.inlined_to == this)
2783 error ("inlined_to pointer refers to itself");
2784 error_found = true;
2787 if (clone_of)
2789 struct cgraph_node *n;
2790 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2791 if (n == this)
2792 break;
2793 if (!n)
2795 error ("cgraph_node has wrong clone_of");
2796 error_found = true;
2799 if (clones)
2801 struct cgraph_node *n;
2802 for (n = clones; n; n = n->next_sibling_clone)
2803 if (n->clone_of != this)
2804 break;
2805 if (n)
2807 error ("cgraph_node has wrong clone list");
2808 error_found = true;
2811 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2813 error ("cgraph_node is in clone list but it is not clone");
2814 error_found = true;
2816 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2818 error ("cgraph_node has wrong prev_clone pointer");
2819 error_found = true;
2821 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2823 error ("double linked list of clones corrupted");
2824 error_found = true;
2827 if (analyzed && alias)
2829 bool ref_found = false;
2830 int i;
2831 struct ipa_ref *ref = NULL;
2833 if (callees)
2835 error ("Alias has call edges");
2836 error_found = true;
2838 for (i = 0; iterate_reference (i, ref); i++)
2839 if (ref->use != IPA_REF_ALIAS)
2841 error ("Alias has non-alias reference");
2842 error_found = true;
2844 else if (ref_found)
2846 error ("Alias has more than one alias reference");
2847 error_found = true;
2849 else
2850 ref_found = true;
2851 if (!ref_found)
2853 error ("Analyzed alias has no reference");
2854 error_found = true;
2857 if (analyzed && thunk.thunk_p)
2859 if (!callees)
2861 error ("No edge out of thunk node");
2862 error_found = true;
2864 else if (callees->next_callee)
2866 error ("More than one edge out of thunk node");
2867 error_found = true;
2869 if (gimple_has_body_p (decl))
2871 error ("Thunk is not supposed to have body");
2872 error_found = true;
2875 else if (analyzed && gimple_has_body_p (decl)
2876 && !TREE_ASM_WRITTEN (decl)
2877 && (!DECL_EXTERNAL (decl) || global.inlined_to)
2878 && !flag_wpa)
2880 if (this_cfun->cfg)
2882 hash_set<gimple> stmts;
2883 int i;
2884 struct ipa_ref *ref = NULL;
2886 /* Reach the trees by walking over the CFG, and note the
2887 enclosing basic-blocks in the call edges. */
2888 FOR_EACH_BB_FN (this_block, this_cfun)
2890 for (gsi = gsi_start_phis (this_block);
2891 !gsi_end_p (gsi); gsi_next (&gsi))
2892 stmts.add (gsi_stmt (gsi));
2893 for (gsi = gsi_start_bb (this_block);
2894 !gsi_end_p (gsi);
2895 gsi_next (&gsi))
2897 gimple stmt = gsi_stmt (gsi);
2898 stmts.add (stmt);
2899 if (is_gimple_call (stmt))
2901 struct cgraph_edge *e = get_edge (stmt);
2902 tree decl = gimple_call_fndecl (stmt);
2903 if (e)
2905 if (e->aux)
2907 error ("shared call_stmt:");
2908 cgraph_debug_gimple_stmt (this_cfun, stmt);
2909 error_found = true;
2911 if (!e->indirect_unknown_callee)
2913 if (verify_edge_corresponds_to_fndecl (e, decl))
2915 error ("edge points to wrong declaration:");
2916 debug_tree (e->callee->decl);
2917 fprintf (stderr," Instead of:");
2918 debug_tree (decl);
2919 error_found = true;
2922 else if (decl)
2924 error ("an indirect edge with unknown callee "
2925 "corresponding to a call_stmt with "
2926 "a known declaration:");
2927 error_found = true;
2928 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2930 e->aux = (void *)1;
2932 else if (decl)
2934 error ("missing callgraph edge for call stmt:");
2935 cgraph_debug_gimple_stmt (this_cfun, stmt);
2936 error_found = true;
2941 for (i = 0; iterate_reference (i, ref); i++)
2942 if (ref->stmt && !stmts.contains (ref->stmt))
2944 error ("reference to dead statement");
2945 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2946 error_found = true;
2949 else
2950 /* No CFG available?! */
2951 gcc_unreachable ();
2953 for (e = callees; e; e = e->next_callee)
2955 if (!e->aux)
2957 error ("edge %s->%s has no corresponding call_stmt",
2958 identifier_to_locale (e->caller->name ()),
2959 identifier_to_locale (e->callee->name ()));
2960 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2961 error_found = true;
2963 e->aux = 0;
2965 for (e = indirect_calls; e; e = e->next_callee)
2967 if (!e->aux && !e->speculative)
2969 error ("an indirect edge from %s has no corresponding call_stmt",
2970 identifier_to_locale (e->caller->name ()));
2971 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2972 error_found = true;
2974 e->aux = 0;
2977 if (error_found)
2979 dump (stderr);
2980 internal_error ("verify_cgraph_node failed");
2982 timevar_pop (TV_CGRAPH_VERIFY);
2985 /* Verify whole cgraph structure. */
2986 DEBUG_FUNCTION void
2987 cgraph_node::verify_cgraph_nodes (void)
2989 struct cgraph_node *node;
2991 if (seen_error ())
2992 return;
2994 FOR_EACH_FUNCTION (node)
2995 node->verify ();
2998 /* Walk the alias chain to return the function cgraph_node is alias of.
2999 Walk through thunk, too.
3000 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3002 cgraph_node *
3003 cgraph_node::function_symbol (enum availability *availability)
3005 cgraph_node *node = this;
3009 node = node->ultimate_alias_target (availability);
3010 if (node->thunk.thunk_p)
3012 node = node->callees->callee;
3013 if (availability)
3015 enum availability a;
3016 a = node->get_availability ();
3017 if (a < *availability)
3018 *availability = a;
3020 node = node->ultimate_alias_target (availability);
3022 } while (node && node->thunk.thunk_p);
3023 return node;
3026 /* When doing LTO, read cgraph_node's body from disk if it is not already
3027 present. */
3029 bool
3030 cgraph_node::get_body (void)
3032 struct lto_file_decl_data *file_data;
3033 const char *data, *name;
3034 size_t len;
3035 tree decl = this->decl;
3037 if (DECL_RESULT (decl))
3038 return false;
3040 gcc_assert (in_lto_p);
3042 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3044 file_data = lto_file_data;
3045 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3047 /* We may have renamed the declaration, e.g., a static function. */
3048 name = lto_get_decl_name_mapping (file_data, name);
3050 data = lto_get_section_data (file_data, LTO_section_function_body,
3051 name, &len);
3052 if (!data)
3053 fatal_error ("%s: section %s is missing",
3054 file_data->file_name,
3055 name);
3057 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3059 lto_input_function_body (file_data, this, data);
3060 lto_stats.num_function_bodies++;
3061 lto_free_section_data (file_data, LTO_section_function_body, name,
3062 data, len);
3063 lto_free_function_in_decl_state_for_node (this);
3065 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3067 return true;
3070 /* Verify if the type of the argument matches that of the function
3071 declaration. If we cannot verify this or there is a mismatch,
3072 return false. */
3074 static bool
3075 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3077 tree parms, p;
3078 unsigned int i, nargs;
3080 /* Calls to internal functions always match their signature. */
3081 if (gimple_call_internal_p (stmt))
3082 return true;
3084 nargs = gimple_call_num_args (stmt);
3086 /* Get argument types for verification. */
3087 if (fndecl)
3088 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3089 else
3090 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3092 /* Verify if the type of the argument matches that of the function
3093 declaration. If we cannot verify this or there is a mismatch,
3094 return false. */
3095 if (fndecl && DECL_ARGUMENTS (fndecl))
3097 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3098 i < nargs;
3099 i++, p = DECL_CHAIN (p))
3101 tree arg;
3102 /* We cannot distinguish a varargs function from the case
3103 of excess parameters, still deferring the inlining decision
3104 to the callee is possible. */
3105 if (!p)
3106 break;
3107 arg = gimple_call_arg (stmt, i);
3108 if (p == error_mark_node
3109 || DECL_ARG_TYPE (p) == error_mark_node
3110 || arg == error_mark_node
3111 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3112 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3113 return false;
3115 if (args_count_match && p)
3116 return false;
3118 else if (parms)
3120 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3122 tree arg;
3123 /* If this is a varargs function defer inlining decision
3124 to callee. */
3125 if (!p)
3126 break;
3127 arg = gimple_call_arg (stmt, i);
3128 if (TREE_VALUE (p) == error_mark_node
3129 || arg == error_mark_node
3130 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3131 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3132 && !fold_convertible_p (TREE_VALUE (p), arg)))
3133 return false;
3136 else
3138 if (nargs != 0)
3139 return false;
3141 return true;
3144 /* Verify if the type of the argument and lhs of CALL_STMT matches
3145 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3146 true, the arg count needs to be the same.
3147 If we cannot verify this or there is a mismatch, return false. */
3149 bool
3150 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3151 bool args_count_match)
3153 tree lhs;
3155 if ((DECL_RESULT (callee)
3156 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3157 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3158 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3159 TREE_TYPE (lhs))
3160 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3161 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3162 return false;
3163 return true;
3166 #include "gt-cgraph.h"