Merge from trunk:
[official-gcc.git] / main / gcc / cgraph.c
blob2e6cb47e298e3fc794a664342e40ea376f3cdd86
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 "l-ipo.h"
63 #include "ipa-inline.h"
64 #include "cfgloop.h"
65 #include "gimple-pretty-print.h"
66 #include "expr.h"
67 #include "tree-dfa.h"
69 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
70 #include "tree-pass.h"
72 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
73 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
75 /* Queue of cgraph nodes scheduled to be lowered. */
76 symtab_node *x_cgraph_nodes_queue;
77 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
79 /* Number of nodes in existence. */
80 int cgraph_n_nodes;
82 /* Maximal uid used in cgraph nodes. */
83 int cgraph_max_uid;
85 /* Maximal uid used in cgraph edges. */
86 int cgraph_edge_max_uid;
88 /* Set when whole unit has been analyzed so we can access global info. */
89 bool cgraph_global_info_ready = false;
91 /* What state callgraph is in right now. */
92 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
94 /* Set when the cgraph is fully build and the basic flags are computed. */
95 bool cgraph_function_flags_ready = false;
97 /* List of hooks triggered on cgraph_edge events. */
98 struct cgraph_edge_hook_list {
99 cgraph_edge_hook hook;
100 void *data;
101 struct cgraph_edge_hook_list *next;
104 /* List of hooks triggered on cgraph_node events. */
105 struct cgraph_node_hook_list {
106 cgraph_node_hook hook;
107 void *data;
108 struct cgraph_node_hook_list *next;
111 /* List of hooks triggered on events involving two cgraph_edges. */
112 struct cgraph_2edge_hook_list {
113 cgraph_2edge_hook hook;
114 void *data;
115 struct cgraph_2edge_hook_list *next;
118 /* List of hooks triggered on events involving two cgraph_nodes. */
119 struct cgraph_2node_hook_list {
120 cgraph_2node_hook hook;
121 void *data;
122 struct cgraph_2node_hook_list *next;
125 /* List of hooks triggered when an edge is removed. */
126 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
127 /* List of hooks triggered when a node is removed. */
128 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
129 /* List of hooks triggered when an edge is duplicated. */
130 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
131 /* List of hooks triggered when a node is duplicated. */
132 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
133 /* List of hooks triggered when an function is inserted. */
134 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
136 /* Head of a linked list of unused (freed) call graph nodes.
137 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
138 static GTY(()) struct cgraph_node *free_nodes;
139 /* Head of a linked list of unused (freed) call graph edges.
140 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
141 static GTY(()) struct cgraph_edge *free_edges;
143 /* Did procss_same_body_aliases run? */
144 bool cpp_implicit_aliases_done;
146 /* Map a cgraph_node to cgraph_function_version_info using this htab.
147 The cgraph_function_version_info has a THIS_NODE field that is the
148 corresponding cgraph_node.. */
150 static GTY((param_is (struct cgraph_function_version_info))) htab_t
151 cgraph_fnver_htab = NULL;
153 /* Hash function for cgraph_fnver_htab. */
154 static hashval_t
155 cgraph_fnver_htab_hash (const void *ptr)
157 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
158 return (hashval_t)(uid);
161 /* eq function for cgraph_fnver_htab. */
162 static int
163 cgraph_fnver_htab_eq (const void *p1, const void *p2)
165 const struct cgraph_function_version_info *n1
166 = (const struct cgraph_function_version_info *)p1;
167 const struct cgraph_function_version_info *n2
168 = (const struct cgraph_function_version_info *)p2;
170 return n1->this_node->uid == n2->this_node->uid;
173 /* Mark as GC root all allocated nodes. */
174 static GTY(()) struct cgraph_function_version_info *
175 version_info_node = NULL;
177 /* Get the cgraph_function_version_info node corresponding to node. */
178 struct cgraph_function_version_info *
179 cgraph_node::function_version (void)
181 struct cgraph_function_version_info *ret;
182 struct cgraph_function_version_info key;
183 key.this_node = this;
185 if (cgraph_fnver_htab == NULL)
186 return NULL;
188 ret = (struct cgraph_function_version_info *)
189 htab_find (cgraph_fnver_htab, &key);
191 return ret;
194 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
195 corresponding to cgraph_node NODE. */
196 struct cgraph_function_version_info *
197 cgraph_node::insert_new_function_version (void)
199 void **slot;
201 version_info_node = NULL;
202 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
203 version_info_node->this_node = this;
205 if (cgraph_fnver_htab == NULL)
206 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
207 cgraph_fnver_htab_eq, NULL);
209 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
210 gcc_assert (slot != NULL);
211 *slot = version_info_node;
212 return version_info_node;
215 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
216 DECL is a duplicate declaration. */
217 void
218 cgraph_node::delete_function_version (tree decl)
220 struct cgraph_node *decl_node = cgraph_node::get (decl);
221 struct cgraph_function_version_info *decl_v = NULL;
223 if (decl_node == NULL)
224 return;
226 decl_v = decl_node->function_version ();
228 if (decl_v == NULL)
229 return;
231 if (decl_v->prev != NULL)
232 decl_v->prev->next = decl_v->next;
234 if (decl_v->next != NULL)
235 decl_v->next->prev = decl_v->prev;
237 if (cgraph_fnver_htab != NULL)
238 htab_remove_elt (cgraph_fnver_htab, decl_v);
240 decl_node->remove ();
243 /* Record that DECL1 and DECL2 are semantically identical function
244 versions. */
245 void
246 cgraph_node::record_function_versions (tree decl1, tree decl2)
248 struct cgraph_node *decl1_node = cgraph_node::get_create (decl1);
249 struct cgraph_node *decl2_node = cgraph_node::get_create (decl2);
250 struct cgraph_function_version_info *decl1_v = NULL;
251 struct cgraph_function_version_info *decl2_v = NULL;
252 struct cgraph_function_version_info *before;
253 struct cgraph_function_version_info *after;
255 gcc_assert (decl1_node != NULL && decl2_node != NULL);
256 decl1_v = decl1_node->function_version ();
257 decl2_v = decl2_node->function_version ();
259 if (decl1_v != NULL && decl2_v != NULL)
260 return;
262 if (decl1_v == NULL)
263 decl1_v = decl1_node->insert_new_function_version ();
265 if (decl2_v == NULL)
266 decl2_v = decl2_node->insert_new_function_version ();
268 /* Chain decl2_v and decl1_v. All semantically identical versions
269 will be chained together. */
271 before = decl1_v;
272 after = decl2_v;
274 while (before->next != NULL)
275 before = before->next;
277 while (after->prev != NULL)
278 after= after->prev;
280 before->next = after;
281 after->prev = before;
284 /* Macros to access the next item in the list of free cgraph nodes and
285 edges. */
286 #define NEXT_FREE_NODE(NODE) dyn_cast<cgraph_node *> ((NODE)->next)
287 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
288 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
290 /* Register HOOK to be called with DATA on each removed edge. */
291 struct cgraph_edge_hook_list *
292 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
294 struct cgraph_edge_hook_list *entry;
295 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
297 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
298 entry->hook = hook;
299 entry->data = data;
300 entry->next = NULL;
301 while (*ptr)
302 ptr = &(*ptr)->next;
303 *ptr = entry;
304 return entry;
307 /* Remove ENTRY from the list of hooks called on removing edges. */
308 void
309 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
311 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
313 while (*ptr != entry)
314 ptr = &(*ptr)->next;
315 *ptr = entry->next;
316 free (entry);
319 /* Call all edge removal hooks. */
320 static void
321 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
323 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
324 while (entry)
326 entry->hook (e, entry->data);
327 entry = entry->next;
331 /* Register HOOK to be called with DATA on each removed node. */
332 struct cgraph_node_hook_list *
333 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
335 struct cgraph_node_hook_list *entry;
336 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
338 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
339 entry->hook = hook;
340 entry->data = data;
341 entry->next = NULL;
342 while (*ptr)
343 ptr = &(*ptr)->next;
344 *ptr = entry;
345 return entry;
348 /* Remove ENTRY from the list of hooks called on removing nodes. */
349 void
350 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
352 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
354 while (*ptr != entry)
355 ptr = &(*ptr)->next;
356 *ptr = entry->next;
357 free (entry);
360 /* Call all node removal hooks. */
361 static void
362 cgraph_call_node_removal_hooks (struct cgraph_node *node)
364 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
365 while (entry)
367 entry->hook (node, entry->data);
368 entry = entry->next;
372 /* Register HOOK to be called with DATA on each inserted node. */
373 struct cgraph_node_hook_list *
374 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
376 struct cgraph_node_hook_list *entry;
377 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
379 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
380 entry->hook = hook;
381 entry->data = data;
382 entry->next = NULL;
383 while (*ptr)
384 ptr = &(*ptr)->next;
385 *ptr = entry;
386 return entry;
389 /* Remove ENTRY from the list of hooks called on inserted nodes. */
390 void
391 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
393 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
395 while (*ptr != entry)
396 ptr = &(*ptr)->next;
397 *ptr = entry->next;
398 free (entry);
401 /* Call all node insertion hooks. */
402 void
403 cgraph_node::call_function_insertion_hooks (void)
405 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
406 while (entry)
408 entry->hook (this, entry->data);
409 entry = entry->next;
413 /* Register HOOK to be called with DATA on each duplicated edge. */
414 struct cgraph_2edge_hook_list *
415 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
417 struct cgraph_2edge_hook_list *entry;
418 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
420 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
421 entry->hook = hook;
422 entry->data = data;
423 entry->next = NULL;
424 while (*ptr)
425 ptr = &(*ptr)->next;
426 *ptr = entry;
427 return entry;
430 /* Remove ENTRY from the list of hooks called on duplicating edges. */
431 void
432 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
434 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
436 while (*ptr != entry)
437 ptr = &(*ptr)->next;
438 *ptr = entry->next;
439 free (entry);
442 /* Call all edge duplication hooks. */
443 void
444 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
445 struct cgraph_edge *cs2)
447 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
448 while (entry)
450 entry->hook (cs1, cs2, entry->data);
451 entry = entry->next;
455 /* Register HOOK to be called with DATA on each duplicated node. */
456 struct cgraph_2node_hook_list *
457 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
459 struct cgraph_2node_hook_list *entry;
460 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
462 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
463 entry->hook = hook;
464 entry->data = data;
465 entry->next = NULL;
466 while (*ptr)
467 ptr = &(*ptr)->next;
468 *ptr = entry;
469 return entry;
472 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
473 void
474 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
476 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
478 while (*ptr != entry)
479 ptr = &(*ptr)->next;
480 *ptr = entry->next;
481 free (entry);
484 /* Call all node duplication hooks. */
485 void
486 cgraph_node::call_duplication_hooks (struct cgraph_node *node2)
488 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
489 while (entry)
491 entry->hook (this, node2, entry->data);
492 entry = entry->next;
496 /* Allocate new callgraph node. */
498 static inline struct cgraph_node *
499 cgraph_allocate_node (void)
501 struct cgraph_node *node;
503 if (free_nodes)
505 node = free_nodes;
506 free_nodes = NEXT_FREE_NODE (node);
508 else
510 node = ggc_cleared_alloc<cgraph_node> ();
511 node->uid = cgraph_max_uid++;
514 return node;
517 /* Allocate new callgraph node and insert it into basic data structures. */
519 cgraph_node *
520 cgraph_node::create_empty (void)
522 struct cgraph_node *node = cgraph_allocate_node ();
524 node->type = SYMTAB_FUNCTION;
525 node->frequency = NODE_FREQUENCY_NORMAL;
526 node->count_materialization_scale = REG_BR_PROB_BASE;
527 cgraph_n_nodes++;
528 return node;
531 /* Return cgraph node assigned to DECL. Create new one when needed. */
533 cgraph_node *
534 cgraph_node::create (tree decl)
536 struct cgraph_node *node = cgraph_node::create_empty ();
537 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
539 node->decl = decl;
540 node->register_symbol ();
542 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
544 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
545 node->next_nested = node->origin->nested;
546 node->origin->nested = node;
548 return node;
551 /* Try to find a call graph node for declaration DECL and if it does not exist
552 or if it corresponds to an inline clone, create a new one. */
554 cgraph_node *
555 cgraph_node::get_create (tree decl)
557 struct cgraph_node *first_clone = cgraph_node::get (decl);
559 if (first_clone && !first_clone->global.inlined_to)
560 return first_clone;
562 struct cgraph_node *node = cgraph_node::create (decl);
563 if (first_clone)
565 first_clone->clone_of = node;
566 node->clones = first_clone;
567 symtab_prevail_in_asm_name_hash (node);
568 node->decl->decl_with_vis.symtab_node = node;
569 if (dump_file)
570 fprintf (dump_file, "Introduced new external node "
571 "(%s/%i) and turned into root of the clone tree.\n",
572 xstrdup (node->name ()), node->order);
574 else if (dump_file)
575 fprintf (dump_file, "Introduced new external node "
576 "(%s/%i).\n", xstrdup (node->name ()),
577 node->order);
578 return node;
581 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
582 the function body is associated with (not necessarily cgraph_node (DECL). */
584 cgraph_node *
585 cgraph_node::create_alias (tree alias, tree target)
587 cgraph_node *alias_node;
589 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
590 || TREE_CODE (target) == IDENTIFIER_NODE);
591 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
592 alias_node = cgraph_node::get_create (alias);
593 gcc_assert (!alias_node->definition);
594 alias_node->alias_target = target;
595 alias_node->definition = true;
596 alias_node->alias = true;
597 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
598 alias_node->weakref = true;
599 return alias_node;
602 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
603 and NULL otherwise.
604 Same body aliases are output whenever the body of DECL is output,
605 and cgraph_node::get (ALIAS) transparently returns
606 cgraph_node::get (DECL). */
608 struct cgraph_node *
609 cgraph_node::create_same_body_alias (tree alias, tree decl)
611 struct cgraph_node *n;
612 #ifndef ASM_OUTPUT_DEF
613 /* If aliases aren't supported by the assembler, fail. */
614 return NULL;
615 #endif
616 /* Langhooks can create same body aliases of symbols not defined.
617 Those are useless. Drop them on the floor. */
618 if (cgraph_global_info_ready)
619 return NULL;
621 n = cgraph_node::create_alias (alias, decl);
622 n->cpp_implicit_alias = true;
623 if (cpp_implicit_aliases_done)
624 n->resolve_alias (cgraph_node::get (decl));
625 return n;
628 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
629 aliases DECL with an adjustments made into the first parameter.
630 See comments in thunk_adjust for detail on the parameters. */
632 struct cgraph_node *
633 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
634 HOST_WIDE_INT fixed_offset,
635 HOST_WIDE_INT virtual_value,
636 tree virtual_offset,
637 tree real_alias)
639 struct cgraph_node *node;
641 node = cgraph_node::get (alias);
642 if (node)
643 node->reset ();
644 else
645 node = cgraph_node::create (alias);
646 gcc_checking_assert (!virtual_offset
647 || wi::eq_p (virtual_offset, virtual_value));
648 node->thunk.fixed_offset = fixed_offset;
649 node->thunk.this_adjusting = this_adjusting;
650 node->thunk.virtual_value = virtual_value;
651 node->thunk.virtual_offset_p = virtual_offset != NULL;
652 node->thunk.alias = real_alias;
653 node->thunk.thunk_p = true;
654 node->definition = true;
656 return node;
659 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
660 Return NULL if there's no such node. */
662 cgraph_node *
663 cgraph_node::get_for_asmname (tree asmname)
665 /* We do not want to look at inline clones. */
666 for (symtab_node *node = symtab_node_for_asm (asmname);
667 node;
668 node = node->next_sharing_asm_name)
670 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
671 if (cn && !cn->global.inlined_to)
672 return cn;
674 return NULL;
677 /* Returns a hash value for X (which really is a cgraph_edge). */
679 static hashval_t
680 edge_hash (const void *x)
682 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
685 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
687 static int
688 edge_eq (const void *x, const void *y)
690 return ((const struct cgraph_edge *) x)->call_stmt == y;
693 /* Add call graph edge E to call site hash of its caller. */
695 static inline void
696 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
698 void **slot;
699 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
700 e->call_stmt,
701 htab_hash_pointer (e->call_stmt),
702 INSERT);
703 *slot = e;
706 /* Add call graph edge E to call site hash of its caller. */
708 static inline void
709 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
711 void **slot;
712 /* There are two speculative edges for every statement (one direct,
713 one indirect); always hash the direct one. */
714 if (e->speculative && e->indirect_unknown_callee)
715 return;
716 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
717 e->call_stmt,
718 htab_hash_pointer (e->call_stmt),
719 INSERT);
720 if (*slot)
722 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
723 if (e->callee)
724 *slot = e;
725 return;
727 gcc_assert (!*slot || e->speculative);
728 *slot = e;
731 /* Return the callgraph edge representing the GIMPLE_CALL statement
732 CALL_STMT. */
734 cgraph_edge *
735 cgraph_node::get_edge (gimple call_stmt)
737 struct cgraph_edge *e, *e2;
738 int n = 0;
740 if (call_site_hash)
741 return (struct cgraph_edge *)
742 htab_find_with_hash (call_site_hash, call_stmt,
743 htab_hash_pointer (call_stmt));
745 /* This loop may turn out to be performance problem. In such case adding
746 hashtables into call nodes with very many edges is probably best
747 solution. It is not good idea to add pointer into CALL_EXPR itself
748 because we want to make possible having multiple cgraph nodes representing
749 different clones of the same body before the body is actually cloned. */
750 for (e = callees; e; e = e->next_callee)
752 if (e->call_stmt == call_stmt)
753 break;
754 n++;
757 if (!e)
758 for (e = indirect_calls; e; e = e->next_callee)
760 if (e->call_stmt == call_stmt)
761 break;
762 n++;
765 if (n > 100)
767 call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
768 for (e2 = callees; e2; e2 = e2->next_callee)
769 /* Skip fake edges. */
770 if (e2->call_stmt)
771 cgraph_add_edge_to_call_site_hash (e2);
772 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
773 cgraph_add_edge_to_call_site_hash (e2);
776 return e;
780 /* Change field call_stmt of edge E to NEW_STMT.
781 If UPDATE_SPECULATIVE and E is any component of speculative
782 edge, then update all components. */
784 void
785 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
786 bool update_speculative)
788 tree decl;
790 /* Speculative edges has three component, update all of them
791 when asked to. */
792 if (update_speculative && e->speculative)
794 struct cgraph_edge *direct, *indirect;
795 struct ipa_ref *ref;
797 cgraph_speculative_call_info (e, direct, indirect, ref);
798 cgraph_set_call_stmt (direct, new_stmt, false);
799 cgraph_set_call_stmt (indirect, new_stmt, false);
800 ref->stmt = new_stmt;
801 return;
804 /* Only direct speculative edges go to call_site_hash. */
805 if (e->caller->call_site_hash
806 && (!e->speculative || !e->indirect_unknown_callee))
808 htab_remove_elt_with_hash (e->caller->call_site_hash,
809 e->call_stmt,
810 htab_hash_pointer (e->call_stmt));
813 e->call_stmt = new_stmt;
814 if (e->indirect_unknown_callee
815 && (decl = gimple_call_fndecl (new_stmt)))
817 /* Constant propagation (and possibly also inlining?) can turn an
818 indirect call into a direct one. */
819 struct cgraph_node *new_callee = cgraph_node::get (decl);
820 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
821 new_callee = cgraph_lipo_get_resolved_node (decl);
823 gcc_checking_assert (new_callee);
824 e = cgraph_make_edge_direct (e, new_callee);
827 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
828 e->can_throw_external = stmt_can_throw_external (new_stmt);
829 pop_cfun ();
830 if (e->caller->call_site_hash)
831 cgraph_add_edge_to_call_site_hash (e);
834 /* Allocate a cgraph_edge structure and fill it with data according to the
835 parameters of which only CALLEE can be NULL (when creating an indirect call
836 edge). */
838 cgraph_edge *
839 cgraph_node::create_edge (cgraph_node *caller, cgraph_node *callee,
840 gimple call_stmt, gcov_type count, int freq,
841 bool indir_unknown_callee)
843 cgraph_edge *edge;
845 /* LTO does not actually have access to the call_stmt since these
846 have not been loaded yet. */
847 if (call_stmt)
849 /* This is a rather expensive check possibly triggering
850 construction of call stmt hashtable. */
851 #ifdef ENABLE_CHECKING
852 struct cgraph_edge *e;
853 gcc_checking_assert (
854 !(e = caller->get_edge (call_stmt)) || e->speculative);
855 #endif
857 gcc_assert (is_gimple_call (call_stmt));
860 if (free_edges)
862 edge = free_edges;
863 free_edges = NEXT_FREE_EDGE (edge);
865 else
867 edge = ggc_alloc<struct cgraph_edge> ();
868 edge->uid = cgraph_edge_max_uid++;
871 edge->aux = NULL;
872 edge->caller = caller;
873 edge->callee = callee;
874 edge->prev_caller = NULL;
875 edge->next_caller = NULL;
876 edge->prev_callee = NULL;
877 edge->next_callee = NULL;
878 edge->lto_stmt_uid = 0;
880 edge->count = count;
881 gcc_assert (count >= 0);
882 edge->frequency = freq;
883 gcc_assert (freq >= 0);
884 gcc_assert (freq <= CGRAPH_FREQ_MAX);
886 edge->call_stmt = call_stmt;
887 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
888 edge->can_throw_external
889 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
890 pop_cfun ();
891 if (call_stmt
892 && callee && callee->decl
893 && !gimple_check_call_matching_types (call_stmt, callee->decl,
894 false))
895 edge->call_stmt_cannot_inline_p = true;
896 else
897 edge->call_stmt_cannot_inline_p = false;
899 edge->indirect_info = NULL;
900 edge->indirect_inlining_edge = 0;
901 edge->speculative = false;
902 edge->indirect_unknown_callee = indir_unknown_callee;
903 if (call_stmt && caller->call_site_hash)
904 cgraph_add_edge_to_call_site_hash (edge);
906 return edge;
909 /* Create edge from a given function to CALLEE in the cgraph. */
911 struct cgraph_edge *
912 cgraph_node::create_edge (struct cgraph_node *callee,
913 gimple call_stmt, gcov_type count, int freq)
915 cgraph_edge *edge = cgraph_node::create_edge (this, callee, call_stmt,
916 count, freq, false);
918 initialize_inline_failed (edge);
920 edge->next_caller = callee->callers;
921 if (callee->callers)
922 callee->callers->prev_caller = edge;
923 edge->next_callee = callees;
924 if (callees)
925 callees->prev_callee = edge;
926 callees = edge;
927 callee->callers = edge;
929 return edge;
932 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
934 struct cgraph_indirect_call_info *
935 cgraph_allocate_init_indirect_info (void)
937 struct cgraph_indirect_call_info *ii;
939 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
940 ii->param_index = -1;
941 return ii;
944 /* Create an indirect edge with a yet-undetermined callee where the call
945 statement destination is a formal parameter of the caller with index
946 PARAM_INDEX. */
948 struct cgraph_edge *
949 cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
950 gcov_type count, int freq)
952 struct cgraph_edge *edge = cgraph_node::create_edge (this, NULL, call_stmt,
953 count, freq, true);
954 tree target;
956 initialize_inline_failed (edge);
958 edge->indirect_info = cgraph_allocate_init_indirect_info ();
959 edge->indirect_info->ecf_flags = ecf_flags;
961 /* Record polymorphic call info. */
962 if (call_stmt
963 && (target = gimple_call_fn (call_stmt))
964 && virtual_method_call_p (target))
966 tree otr_type;
967 HOST_WIDE_INT otr_token;
968 ipa_polymorphic_call_context context;
970 get_polymorphic_call_info (decl,
971 target,
972 &otr_type, &otr_token,
973 &context, call_stmt);
975 /* Only record types can have virtual calls. */
976 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
977 edge->indirect_info->polymorphic = true;
978 edge->indirect_info->param_index = -1;
979 edge->indirect_info->otr_token = otr_token;
980 edge->indirect_info->otr_type = otr_type;
981 edge->indirect_info->outer_type = context.outer_type;
982 edge->indirect_info->speculative_outer_type
983 = context.speculative_outer_type;
984 edge->indirect_info->offset = context.offset;
985 edge->indirect_info->speculative_offset = context.speculative_offset;
986 edge->indirect_info->maybe_in_construction
987 = context.maybe_in_construction;
988 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
989 edge->indirect_info->speculative_maybe_derived_type
990 = context.speculative_maybe_derived_type;
993 edge->next_callee = indirect_calls;
994 if (indirect_calls)
995 indirect_calls->prev_callee = edge;
996 indirect_calls = edge;
998 return edge;
1001 /* Remove the edge E from the list of the callers of the callee. */
1003 static inline void
1004 cgraph_edge_remove_callee (struct cgraph_edge *e)
1006 gcc_assert (!e->indirect_unknown_callee);
1007 if (e->prev_caller)
1008 e->prev_caller->next_caller = e->next_caller;
1009 if (e->next_caller)
1010 e->next_caller->prev_caller = e->prev_caller;
1011 if (!e->prev_caller)
1012 e->callee->callers = e->next_caller;
1015 /* Remove the edge E from the list of the callees of the caller. */
1017 static inline void
1018 cgraph_edge_remove_caller (struct cgraph_edge *e)
1020 if (e->prev_callee)
1021 e->prev_callee->next_callee = e->next_callee;
1022 if (e->next_callee)
1023 e->next_callee->prev_callee = e->prev_callee;
1024 if (!e->prev_callee)
1026 if (e->indirect_unknown_callee)
1027 e->caller->indirect_calls = e->next_callee;
1028 else
1029 e->caller->callees = e->next_callee;
1031 if (e->caller->call_site_hash && e->call_stmt)
1032 htab_remove_elt_with_hash (e->caller->call_site_hash,
1033 e->call_stmt,
1034 htab_hash_pointer (e->call_stmt));
1037 /* Put the edge onto the free list. */
1039 static void
1040 cgraph_free_edge (struct cgraph_edge *e)
1042 int uid = e->uid;
1044 if (e->indirect_info)
1045 ggc_free (e->indirect_info);
1047 /* Clear out the edge so we do not dangle pointers. */
1048 memset (e, 0, sizeof (*e));
1049 e->uid = uid;
1050 NEXT_FREE_EDGE (e) = free_edges;
1051 free_edges = e;
1054 /* Remove the edge E in the cgraph. */
1056 void
1057 cgraph_remove_edge (struct cgraph_edge *e)
1059 /* Call all edge removal hooks. */
1060 cgraph_call_edge_removal_hooks (e);
1062 if (!e->indirect_unknown_callee)
1063 /* Remove from callers list of the callee. */
1064 cgraph_edge_remove_callee (e);
1066 /* Remove from callees list of the callers. */
1067 cgraph_edge_remove_caller (e);
1069 /* Put the edge onto the free list. */
1070 cgraph_free_edge (e);
1073 /* Remove fake cgraph edges for indirect calls. NODE is the callee
1074 of the edges. */
1076 void
1077 cgraph_remove_fake_indirect_call_in_edges (struct cgraph_node *node)
1079 struct cgraph_edge *f, *e;
1081 if (!L_IPO_COMP_MODE)
1082 return;
1084 for (e = node->callers; e; e = f)
1086 f = e->next_caller;
1087 if (!e->call_stmt)
1088 cgraph_remove_edge (e);
1093 /* Set callee of call graph edge E and add it to the corresponding set of
1094 callers. */
1096 static void
1097 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1099 e->prev_caller = NULL;
1100 if (n->callers)
1101 n->callers->prev_caller = e;
1102 e->next_caller = n->callers;
1103 n->callers = e;
1104 e->callee = n;
1107 /* Turn edge E into speculative call calling N2. Update
1108 the profile so the direct call is taken COUNT times
1109 with FREQUENCY.
1111 At clone materialization time, the indirect call E will
1112 be expanded as:
1114 if (call_dest == N2)
1115 n2 ();
1116 else
1117 call call_dest
1119 At this time the function just creates the direct call,
1120 the referencd representing the if conditional and attaches
1121 them all to the orginal indirect call statement.
1123 Return direct edge created. */
1125 struct cgraph_edge *
1126 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1127 struct cgraph_node *n2,
1128 gcov_type direct_count,
1129 int direct_frequency)
1131 struct cgraph_node *n = e->caller;
1132 struct ipa_ref *ref = NULL;
1133 struct cgraph_edge *e2;
1135 if (dump_file)
1137 fprintf (dump_file, "Indirect call -> speculative call"
1138 " %s/%i => %s/%i\n",
1139 xstrdup (n->name ()), n->order,
1140 xstrdup (n2->name ()), n2->order);
1142 e->speculative = true;
1143 e2 = n->create_edge (n2, e->call_stmt, direct_count, direct_frequency);
1144 initialize_inline_failed (e2);
1145 e2->speculative = true;
1146 if (TREE_NOTHROW (n2->decl))
1147 e2->can_throw_external = false;
1148 else
1149 e2->can_throw_external = e->can_throw_external;
1150 e2->lto_stmt_uid = e->lto_stmt_uid;
1151 e->count -= e2->count;
1152 e->frequency -= e2->frequency;
1153 cgraph_call_edge_duplication_hooks (e, e2);
1154 ref = n->add_reference (n2, IPA_REF_ADDR, e->call_stmt);
1155 ref->lto_stmt_uid = e->lto_stmt_uid;
1156 ref->speculative = e->speculative;
1157 n2->mark_address_taken ();
1158 return e2;
1161 /* Speculative call consist of three components:
1162 1) an indirect edge representing the original call
1163 2) an direct edge representing the new call
1164 3) ADDR_EXPR reference representing the speculative check.
1165 All three components are attached to single statement (the indirect
1166 call) and if one of them exists, all of them must exist.
1168 Given speculative call edge E, return all three components.
1171 void
1172 cgraph_speculative_call_info (struct cgraph_edge *e,
1173 struct cgraph_edge *&direct,
1174 struct cgraph_edge *&indirect,
1175 struct ipa_ref *&reference)
1177 struct ipa_ref *ref;
1178 int i;
1179 struct cgraph_edge *e2;
1181 if (!e->indirect_unknown_callee)
1182 for (e2 = e->caller->indirect_calls;
1183 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1184 e2 = e2->next_callee)
1186 else
1188 e2 = e;
1189 /* We can take advantage of the call stmt hash. */
1190 if (e2->call_stmt)
1192 e = e->caller->get_edge (e2->call_stmt);
1193 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1195 else
1196 for (e = e->caller->callees;
1197 e2->call_stmt != e->call_stmt
1198 || e2->lto_stmt_uid != e->lto_stmt_uid;
1199 e = e->next_callee)
1202 gcc_assert (e->speculative && e2->speculative);
1203 direct = e;
1204 indirect = e2;
1206 reference = NULL;
1207 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1208 if (ref->speculative
1209 && ((ref->stmt && ref->stmt == e->call_stmt)
1210 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1212 reference = ref;
1213 break;
1216 /* Speculative edge always consist of all three components - direct edge,
1217 indirect and reference. */
1219 gcc_assert (e && e2 && ref);
1222 /* Redirect callee of E to N. The function does not update underlying
1223 call expression. */
1225 void
1226 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1228 /* Remove from callers list of the current callee. */
1229 cgraph_edge_remove_callee (e);
1231 /* Insert to callers list of the new callee. */
1232 cgraph_set_edge_callee (e, n);
1235 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1236 Remove the speculative call sequence and return edge representing the call.
1237 It is up to caller to redirect the call as appropriate. */
1239 struct cgraph_edge *
1240 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1242 struct cgraph_edge *e2;
1243 struct ipa_ref *ref;
1245 gcc_assert (edge->speculative);
1246 cgraph_speculative_call_info (edge, e2, edge, ref);
1247 if (!callee_decl
1248 || !ref->referred->semantically_equivalent_p
1249 (symtab_node::get (callee_decl)))
1251 if (dump_file)
1253 if (callee_decl)
1255 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1256 "turned out to have contradicting known target ",
1257 xstrdup (edge->caller->name ()), edge->caller->order,
1258 xstrdup (e2->callee->name ()), e2->callee->order);
1259 print_generic_expr (dump_file, callee_decl, 0);
1260 fprintf (dump_file, "\n");
1262 else
1264 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1265 xstrdup (edge->caller->name ()), edge->caller->order,
1266 xstrdup (e2->callee->name ()), e2->callee->order);
1270 else
1272 struct cgraph_edge *tmp = edge;
1273 if (dump_file)
1274 fprintf (dump_file, "Speculative call turned into direct call.\n");
1275 edge = e2;
1276 e2 = tmp;
1277 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1278 in the functions inlined through it. */
1280 edge->count += e2->count;
1281 edge->frequency += e2->frequency;
1282 if (edge->frequency > CGRAPH_FREQ_MAX)
1283 edge->frequency = CGRAPH_FREQ_MAX;
1284 edge->speculative = false;
1285 e2->speculative = false;
1286 ref->remove_reference ();
1287 if (e2->indirect_unknown_callee || e2->inline_failed)
1288 cgraph_remove_edge (e2);
1289 else
1290 e2->callee->remove_symbol_and_inline_clones ();
1291 if (edge->caller->call_site_hash)
1292 cgraph_update_edge_in_call_site_hash (edge);
1293 return edge;
1296 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1297 CALLEE. DELTA is an integer constant that is to be added to the this
1298 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1300 struct cgraph_edge *
1301 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1303 gcc_assert (edge->indirect_unknown_callee);
1305 /* If we are redirecting speculative call, make it non-speculative. */
1306 if (edge->indirect_unknown_callee && edge->speculative)
1308 edge = cgraph_resolve_speculation (edge, callee->decl);
1310 /* On successful speculation just return the pre existing direct edge. */
1311 if (!edge->indirect_unknown_callee)
1312 return edge;
1315 edge->indirect_unknown_callee = 0;
1316 ggc_free (edge->indirect_info);
1317 edge->indirect_info = NULL;
1319 /* Get the edge out of the indirect edge list. */
1320 if (edge->prev_callee)
1321 edge->prev_callee->next_callee = edge->next_callee;
1322 if (edge->next_callee)
1323 edge->next_callee->prev_callee = edge->prev_callee;
1324 if (!edge->prev_callee)
1325 edge->caller->indirect_calls = edge->next_callee;
1327 /* Put it into the normal callee list */
1328 edge->prev_callee = NULL;
1329 edge->next_callee = edge->caller->callees;
1330 if (edge->caller->callees)
1331 edge->caller->callees->prev_callee = edge;
1332 edge->caller->callees = edge;
1334 /* Insert to callers list of the new callee. */
1335 cgraph_set_edge_callee (edge, callee);
1337 if (edge->call_stmt)
1338 edge->call_stmt_cannot_inline_p
1339 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1340 false);
1342 /* We need to re-determine the inlining status of the edge. */
1343 initialize_inline_failed (edge);
1344 return edge;
1347 /* If necessary, change the function declaration in the call statement
1348 associated with E so that it corresponds to the edge callee. */
1350 gimple
1351 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1353 tree decl = gimple_call_fndecl (e->call_stmt);
1354 tree lhs = gimple_call_lhs (e->call_stmt);
1355 gimple new_stmt;
1356 gimple_stmt_iterator gsi;
1357 #ifdef ENABLE_CHECKING
1358 struct cgraph_node *node;
1359 #endif
1361 if (e->speculative)
1363 struct cgraph_edge *e2;
1364 gimple new_stmt;
1365 struct ipa_ref *ref;
1367 cgraph_speculative_call_info (e, e, e2, ref);
1368 /* If there already is an direct call (i.e. as a result of inliner's
1369 substitution), forget about speculating. */
1370 if (decl)
1371 e = cgraph_resolve_speculation (e, decl);
1372 /* If types do not match, speculation was likely wrong.
1373 The direct edge was posisbly redirected to the clone with a different
1374 signature. We did not update the call statement yet, so compare it
1375 with the reference that still points to the proper type. */
1376 else if (!gimple_check_call_matching_types (e->call_stmt,
1377 ref->referred->decl,
1378 true))
1380 if (dump_file)
1381 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1382 "Type mismatch.\n",
1383 xstrdup (e->caller->name ()),
1384 e->caller->order,
1385 xstrdup (e->callee->name ()),
1386 e->callee->order);
1387 e = cgraph_resolve_speculation (e, NULL);
1388 /* We are producing the final function body and will throw away the
1389 callgraph edges really soon. Reset the counts/frequencies to
1390 keep verifier happy in the case of roundoff errors. */
1391 e->count = gimple_bb (e->call_stmt)->count;
1392 e->frequency = compute_call_stmt_bb_frequency
1393 (e->caller->decl, gimple_bb (e->call_stmt));
1395 /* Expand speculation into GIMPLE code. */
1396 else
1398 if (dump_file)
1399 fprintf (dump_file,
1400 "Expanding speculative call of %s/%i -> %s/%i count:"
1401 "%"PRId64"\n",
1402 xstrdup (e->caller->name ()),
1403 e->caller->order,
1404 xstrdup (e->callee->name ()),
1405 e->callee->order,
1406 (int64_t)e->count);
1407 gcc_assert (e2->speculative);
1408 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1409 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
1410 e->count || e2->count
1411 ? RDIV (e->count * REG_BR_PROB_BASE,
1412 e->count + e2->count)
1413 : e->frequency || e2->frequency
1414 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1415 e->frequency + e2->frequency)
1416 : REG_BR_PROB_BASE / 2,
1417 e->count, e->count + e2->count);
1418 e->speculative = false;
1419 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1420 false);
1421 e->frequency = compute_call_stmt_bb_frequency
1422 (e->caller->decl, gimple_bb (e->call_stmt));
1423 e2->frequency = compute_call_stmt_bb_frequency
1424 (e2->caller->decl, gimple_bb (e2->call_stmt));
1425 e2->speculative = false;
1426 ref->speculative = false;
1427 ref->stmt = NULL;
1428 /* Indirect edges are not both in the call site hash.
1429 get it updated. */
1430 if (e->caller->call_site_hash)
1431 cgraph_update_edge_in_call_site_hash (e2);
1432 pop_cfun ();
1433 /* Continue redirecting E to proper target. */
1437 if (e->indirect_unknown_callee
1438 || decl == e->callee->decl)
1439 return e->call_stmt;
1441 #ifdef ENABLE_CHECKING
1442 if (decl)
1444 node = cgraph_node::get (decl);
1445 gcc_assert (!node || !node->clone.combined_args_to_skip);
1447 #endif
1449 if (cgraph_dump_file)
1451 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1452 xstrdup (e->caller->name ()), e->caller->order,
1453 xstrdup (e->callee->name ()), e->callee->order);
1454 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1455 if (e->callee->clone.combined_args_to_skip)
1457 fprintf (cgraph_dump_file, " combined args to skip: ");
1458 dump_bitmap (cgraph_dump_file,
1459 e->callee->clone.combined_args_to_skip);
1463 if (e->callee->clone.combined_args_to_skip)
1465 int lp_nr;
1467 new_stmt
1468 = gimple_call_copy_skip_args (e->call_stmt,
1469 e->callee->clone.combined_args_to_skip);
1470 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1471 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1473 if (gimple_vdef (new_stmt)
1474 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1475 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1477 gsi = gsi_for_stmt (e->call_stmt);
1478 gsi_replace (&gsi, new_stmt, false);
1479 /* We need to defer cleaning EH info on the new statement to
1480 fixup-cfg. We may not have dominator information at this point
1481 and thus would end up with unreachable blocks and have no way
1482 to communicate that we need to run CFG cleanup then. */
1483 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1484 if (lp_nr != 0)
1486 remove_stmt_from_eh_lp (e->call_stmt);
1487 add_stmt_to_eh_lp (new_stmt, lp_nr);
1490 else
1492 new_stmt = e->call_stmt;
1493 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1494 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1495 if (L_IPO_COMP_MODE)
1497 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1498 if (lp_nr != 0 && !stmt_could_throw_p (e->call_stmt))
1499 remove_stmt_from_eh_lp (e->call_stmt);
1503 /* If the call becomes noreturn, remove the lhs. */
1504 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1506 if (TREE_CODE (lhs) == SSA_NAME)
1508 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1509 TREE_TYPE (lhs), NULL);
1510 var = get_or_create_ssa_default_def
1511 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1512 gimple set_stmt = gimple_build_assign (lhs, var);
1513 gsi = gsi_for_stmt (new_stmt);
1514 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1515 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1517 gimple_call_set_lhs (new_stmt, NULL_TREE);
1518 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1521 /* If new callee has no static chain, remove it. */
1522 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1524 gimple_call_set_chain (new_stmt, NULL);
1525 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1528 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1530 if (cgraph_dump_file)
1532 fprintf (cgraph_dump_file, " updated to:");
1533 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1535 return new_stmt;
1538 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1539 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1540 of OLD_STMT if it was previously call statement.
1541 If NEW_STMT is NULL, the call has been dropped without any
1542 replacement. */
1544 static void
1545 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1546 gimple old_stmt, tree old_call,
1547 gimple new_stmt)
1549 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1550 ? gimple_call_fndecl (new_stmt) : 0;
1552 /* We are seeing indirect calls, then there is nothing to update. */
1553 if (!new_call && !old_call)
1554 return;
1555 /* See if we turned indirect call into direct call or folded call to one builtin
1556 into different builtin. */
1557 if (old_call != new_call)
1559 struct cgraph_edge *e = node->get_edge (old_stmt);
1560 struct cgraph_edge *ne = NULL;
1561 gcov_type count;
1562 int frequency;
1564 if (e)
1566 /* See if the edge is already there and has the correct callee. It
1567 might be so because of indirect inlining has already updated
1568 it. We also might've cloned and redirected the edge. */
1569 if (new_call && e->callee)
1571 struct cgraph_node *callee = e->callee;
1572 while (callee)
1574 if (callee->decl == new_call
1575 || callee->former_clone_of == new_call)
1577 cgraph_set_call_stmt (e, new_stmt);
1578 return;
1580 callee = callee->clone_of;
1584 /* Otherwise remove edge and create new one; we can't simply redirect
1585 since function has changed, so inline plan and other information
1586 attached to edge is invalid. */
1587 count = e->count;
1588 frequency = e->frequency;
1589 if (e->indirect_unknown_callee || e->inline_failed)
1590 cgraph_remove_edge (e);
1591 else
1592 e->callee->remove_symbol_and_inline_clones ();
1594 else if (new_call)
1596 /* We are seeing new direct call; compute profile info based on BB. */
1597 basic_block bb = gimple_bb (new_stmt);
1598 count = bb->count;
1599 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1600 bb);
1603 if (new_call)
1605 ne = node->create_edge (cgraph_node::get_create (new_call),
1606 new_stmt, count, frequency);
1607 gcc_assert (ne->inline_failed);
1610 /* We only updated the call stmt; update pointer in cgraph edge.. */
1611 else if (old_stmt != new_stmt)
1612 cgraph_set_call_stmt (node->get_edge (old_stmt), new_stmt);
1615 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1616 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1617 of OLD_STMT before it was updated (updating can happen inplace). */
1619 void
1620 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1622 struct cgraph_node *orig = cgraph_node::get (cfun->decl);
1623 struct cgraph_node *node;
1625 gcc_checking_assert (orig);
1626 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1627 if (orig->clones)
1628 for (node = orig->clones; node != orig;)
1630 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1631 if (node->clones)
1632 node = node->clones;
1633 else if (node->next_sibling_clone)
1634 node = node->next_sibling_clone;
1635 else
1637 while (node != orig && !node->next_sibling_clone)
1638 node = node->clone_of;
1639 if (node != orig)
1640 node = node->next_sibling_clone;
1646 /* Remove all callees from the node. */
1648 void
1649 cgraph_node::remove_callees (void)
1651 struct cgraph_edge *e, *f;
1653 /* It is sufficient to remove the edges from the lists of callers of
1654 the callees. The callee list of the node can be zapped with one
1655 assignment. */
1656 for (e = callees; e; e = f)
1658 f = e->next_callee;
1659 cgraph_call_edge_removal_hooks (e);
1660 if (!e->indirect_unknown_callee)
1661 cgraph_edge_remove_callee (e);
1662 cgraph_free_edge (e);
1664 for (e = indirect_calls; e; e = f)
1666 f = e->next_callee;
1667 cgraph_call_edge_removal_hooks (e);
1668 if (!e->indirect_unknown_callee)
1669 cgraph_edge_remove_callee (e);
1670 cgraph_free_edge (e);
1672 indirect_calls = NULL;
1673 callees = NULL;
1674 if (call_site_hash)
1676 htab_delete (call_site_hash);
1677 call_site_hash = NULL;
1681 /* Remove all callers from the node. */
1683 void
1684 cgraph_node::remove_callers (void)
1686 struct cgraph_edge *e, *f;
1688 /* It is sufficient to remove the edges from the lists of callees of
1689 the callers. The caller list of the node can be zapped with one
1690 assignment. */
1691 for (e = callers; e; e = f)
1693 f = e->next_caller;
1694 cgraph_call_edge_removal_hooks (e);
1695 cgraph_edge_remove_caller (e);
1696 cgraph_free_edge (e);
1698 callers = NULL;
1701 /* Helper function for cgraph_release_function_body and free_lang_data.
1702 It releases body from function DECL without having to inspect its
1703 possibly non-existent symtab node. */
1705 void
1706 release_function_body (tree decl)
1708 if (cgraph_node::get (decl)
1709 && cgraph_is_aux_decl_external (cgraph_node::get (decl)))
1710 DECL_EXTERNAL (decl) = 1;
1712 if (DECL_STRUCT_FUNCTION (decl))
1714 push_cfun (DECL_STRUCT_FUNCTION (decl));
1715 if (cfun->cfg
1716 && current_loops)
1718 cfun->curr_properties &= ~PROP_loops;
1719 loop_optimizer_finalize ();
1721 if (cfun->gimple_df)
1723 delete_tree_ssa ();
1724 delete_tree_cfg_annotations ();
1725 cfun->eh = NULL;
1727 if (cfun->cfg)
1729 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1730 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1731 clear_edges ();
1732 cfun->cfg = NULL;
1734 if (cfun->value_histograms)
1735 free_histograms ();
1736 pop_cfun ();
1737 gimple_set_body (decl, NULL);
1738 /* Struct function hangs a lot of data that would leak if we didn't
1739 removed all pointers to it. */
1740 ggc_free (DECL_STRUCT_FUNCTION (decl));
1741 DECL_STRUCT_FUNCTION (decl) = NULL;
1743 DECL_SAVED_TREE (decl) = NULL;
1746 /* Release memory used to represent body of function.
1747 Use this only for functions that are released before being translated to
1748 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1749 are free'd in final.c via free_after_compilation(). */
1751 void
1752 cgraph_node::release_body (void)
1754 ipa_transforms_to_apply.release ();
1755 if (!used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1757 DECL_RESULT (decl) = NULL;
1758 DECL_ARGUMENTS (decl) = NULL;
1760 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1761 of its associated function function declaration because it's
1762 needed to emit debug info later. */
1763 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1764 DECL_INITIAL (decl) = error_mark_node;
1765 release_function_body (decl);
1766 if (lto_file_data)
1767 lto_free_function_in_decl_state_for_node (this);
1770 /* Remove function from symbol table. */
1772 void
1773 cgraph_node::remove (void)
1775 struct cgraph_node *n;
1776 int uid = this->uid;
1778 cgraph_call_node_removal_hooks (this);
1779 remove_callers ();
1780 remove_callees ();
1781 ipa_transforms_to_apply.release ();
1783 /* Incremental inlining access removed nodes stored in the postorder list.
1785 force_output = false;
1786 forced_by_abi = false;
1787 for (n = nested; n; n = n->next_nested)
1788 n->origin = NULL;
1789 nested = NULL;
1790 if (origin)
1792 struct cgraph_node **node2 = &origin->nested;
1794 while (*node2 != this)
1795 node2 = &(*node2)->next_nested;
1796 *node2 = next_nested;
1798 unregister ();
1799 if (prev_sibling_clone)
1800 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1801 else if (clone_of)
1802 clone_of->clones = next_sibling_clone;
1803 if (next_sibling_clone)
1804 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1805 if (clones)
1807 struct cgraph_node *n, *next;
1809 if (clone_of)
1811 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1812 n->clone_of = clone_of;
1813 n->clone_of = clone_of;
1814 n->next_sibling_clone = clone_of->clones;
1815 if (clone_of->clones)
1816 clone_of->clones->prev_sibling_clone = n;
1817 clone_of->clones = clones;
1819 else
1821 /* We are removing node with clones. This makes clones inconsistent,
1822 but assume they will be removed subsequently and just keep clone
1823 tree intact. This can happen in unreachable function removal since
1824 we remove unreachable functions in random order, not by bottom-up
1825 walk of clone trees. */
1826 for (n = clones; n; n = next)
1828 next = n->next_sibling_clone;
1829 n->next_sibling_clone = NULL;
1830 n->prev_sibling_clone = NULL;
1831 n->clone_of = NULL;
1836 /* While all the clones are removed after being proceeded, the function
1837 itself is kept in the cgraph even after it is compiled. Check whether
1838 we are done with this body and reclaim it proactively if this is the case.
1840 if (cgraph_state != CGRAPH_LTO_STREAMING)
1842 n = cgraph_node::get (decl);
1843 if (!n
1844 || (!n->clones && !n->clone_of && !n->global.inlined_to
1845 && (cgraph_global_info_ready
1846 && (TREE_ASM_WRITTEN (n->decl)
1847 || DECL_EXTERNAL (n->decl)
1848 || !n->analyzed
1849 || (!flag_wpa && n->in_other_partition)))))
1850 release_body ();
1853 cgraph_remove_link_node (this);
1854 decl = NULL;
1855 if (call_site_hash)
1857 htab_delete (call_site_hash);
1858 call_site_hash = NULL;
1860 cgraph_n_nodes--;
1862 /* Clear out the node to NULL all pointers and add the node to the free
1863 list. */
1864 memset (this, 0, sizeof (*this));
1865 type = SYMTAB_FUNCTION;
1866 this->uid = uid;
1867 SET_NEXT_FREE_NODE (this, free_nodes);
1868 free_nodes = this;
1872 /* Likewise indicate that a node is having address taken. */
1874 void
1875 cgraph_node::mark_address_taken (void)
1877 /* Indirect inlining can figure out that all uses of the address are
1878 inlined. */
1879 if (global.inlined_to)
1881 gcc_assert (cfun->after_inlining);
1882 gcc_assert (callers->indirect_inlining_edge);
1883 return;
1885 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1886 IPA_REF_ADDR reference exists (and thus it should be set on node
1887 representing alias we take address of) and as a test whether address
1888 of the object was taken (and thus it should be set on node alias is
1889 referring to). We should remove the first use and the remove the
1890 following set. */
1891 address_taken = 1;
1892 cgraph_node *node = ultimate_alias_target ();
1893 node->address_taken = 1;
1896 /* Return local info for the compiled function. */
1898 struct cgraph_local_info *
1899 cgraph_local_info (tree decl)
1901 struct cgraph_node *node;
1903 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1904 node = cgraph_node::get (decl);
1905 if (!node)
1906 return NULL;
1907 return &node->local;
1910 /* Return local info for the compiled function. */
1912 struct cgraph_global_info *
1913 cgraph_global_info (tree decl)
1915 struct cgraph_node *node;
1917 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1918 node = cgraph_node::get (decl);
1919 if (!node)
1920 return NULL;
1921 return &node->global;
1924 /* Return local info for the compiled function. */
1926 struct cgraph_rtl_info *
1927 cgraph_rtl_info (tree decl)
1929 struct cgraph_node *node;
1931 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1932 node = cgraph_node::get (decl);
1933 if (!node
1934 || (decl != current_function_decl
1935 && !TREE_ASM_WRITTEN (node->decl)))
1936 return NULL;
1937 return &node->rtl;
1940 /* Return a string describing the failure REASON. */
1942 const char*
1943 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1945 #undef DEFCIFCODE
1946 #define DEFCIFCODE(code, type, string) string,
1948 static const char *cif_string_table[CIF_N_REASONS] = {
1949 #include "cif-code.def"
1952 /* Signedness of an enum type is implementation defined, so cast it
1953 to unsigned before testing. */
1954 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1955 return cif_string_table[reason];
1958 /* Return a type describing the failure REASON. */
1960 cgraph_inline_failed_type_t
1961 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1963 #undef DEFCIFCODE
1964 #define DEFCIFCODE(code, type, string) type,
1966 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1967 #include "cif-code.def"
1970 /* Signedness of an enum type is implementation defined, so cast it
1971 to unsigned before testing. */
1972 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1973 return cif_type_table[reason];
1976 /* Names used to print out the availability enum. */
1977 const char * const cgraph_availability_names[] =
1978 {"unset", "not_available", "overwritable", "available", "local"};
1981 /* Dump call graph node to file F. */
1983 void
1984 cgraph_node::dump (FILE *f)
1986 struct cgraph_edge *edge;
1987 int indirect_calls_count = 0;
1989 dump_base (f);
1991 if (global.inlined_to)
1992 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1993 xstrdup (name ()),
1994 order,
1995 xstrdup (global.inlined_to->name ()),
1996 global.inlined_to->order);
1997 if (clone_of)
1998 fprintf (f, " Clone of %s/%i\n",
1999 clone_of->asm_name (),
2000 clone_of->order);
2001 if (cgraph_function_flags_ready)
2002 fprintf (f, " Availability: %s\n",
2003 cgraph_availability_names [get_availability ()]);
2005 if (profile_id)
2006 fprintf (f, " Profile id: %i\n",
2007 profile_id);
2008 fprintf (f, " First run: %i\n", tp_first_run);
2009 fprintf (f, " Function flags:");
2010 if (count)
2011 fprintf (f, " executed %"PRId64"x",
2012 (int64_t)count);
2013 if (max_bb_count)
2014 fprintf (f, " hottest bb executed %"PRId64"x",
2015 (int64_t)max_bb_count);
2016 if (origin)
2017 fprintf (f, " nested in: %s", origin->asm_name ());
2018 if (gimple_has_body_p (decl))
2019 fprintf (f, " body");
2020 if (process)
2021 fprintf (f, " process");
2022 if (local.local)
2023 fprintf (f, " local");
2024 if (local.redefined_extern_inline)
2025 fprintf (f, " redefined_extern_inline");
2026 if (only_called_at_startup)
2027 fprintf (f, " only_called_at_startup");
2028 if (only_called_at_exit)
2029 fprintf (f, " only_called_at_exit");
2030 if (tm_clone)
2031 fprintf (f, " tm_clone");
2032 if (DECL_STATIC_CONSTRUCTOR (decl))
2033 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2034 if (DECL_STATIC_DESTRUCTOR (decl))
2035 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2037 fprintf (f, "\n");
2039 if (thunk.thunk_p)
2041 fprintf (f, " Thunk");
2042 if (thunk.alias)
2043 fprintf (f, " of %s (asm: %s)",
2044 lang_hooks.decl_printable_name (thunk.alias, 2),
2045 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2046 fprintf (f, " fixed offset %i virtual value %i has "
2047 "virtual offset %i)\n",
2048 (int)thunk.fixed_offset,
2049 (int)thunk.virtual_value,
2050 (int)thunk.virtual_offset_p);
2052 if (alias && thunk.alias
2053 && DECL_P (thunk.alias))
2055 fprintf (f, " Alias of %s",
2056 lang_hooks.decl_printable_name (thunk.alias, 2));
2057 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2058 fprintf (f, " (asm: %s)",
2059 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2060 fprintf (f, "\n");
2063 fprintf (f, " Called by: ");
2065 for (edge = callers; edge; edge = edge->next_caller)
2067 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2068 edge->caller->order);
2069 if (edge->count)
2070 fprintf (f, "(%"PRId64"x) ",
2071 (int64_t)edge->count);
2072 if (edge->frequency)
2073 fprintf (f, "(%.2f per call) ",
2074 edge->frequency / (double)CGRAPH_FREQ_BASE);
2075 if (edge->speculative)
2076 fprintf (f, "(speculative) ");
2077 if (!edge->inline_failed)
2078 fprintf (f, "(inlined) ");
2079 if (edge->indirect_inlining_edge)
2080 fprintf (f, "(indirect_inlining) ");
2081 if (edge->can_throw_external)
2082 fprintf (f, "(can throw external) ");
2085 fprintf (f, "\n Calls: ");
2086 for (edge = callees; edge; edge = edge->next_callee)
2088 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2089 edge->callee->order);
2090 if (edge->speculative)
2091 fprintf (f, "(speculative) ");
2092 if (!edge->inline_failed)
2093 fprintf (f, "(inlined) ");
2094 if (edge->indirect_inlining_edge)
2095 fprintf (f, "(indirect_inlining) ");
2096 if (edge->count)
2097 fprintf (f, "(%"PRId64"x) ",
2098 (int64_t)edge->count);
2099 if (edge->frequency)
2100 fprintf (f, "(%.2f per call) ",
2101 edge->frequency / (double)CGRAPH_FREQ_BASE);
2102 if (edge->can_throw_external)
2103 fprintf (f, "(can throw external) ");
2105 fprintf (f, "\n");
2107 for (edge = indirect_calls; edge; edge = edge->next_callee)
2108 indirect_calls_count++;
2109 if (indirect_calls_count)
2110 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2111 indirect_calls_count);
2114 /* Dump call graph node NODE to stderr. */
2116 DEBUG_FUNCTION void
2117 cgraph_node::debug (void)
2119 dump (stderr);
2122 /* Dump the callgraph to file F. */
2124 void
2125 cgraph_node::dump_cgraph (FILE *f)
2127 struct cgraph_node *node;
2129 fprintf (f, "callgraph:\n\n");
2130 FOR_EACH_FUNCTION (node)
2131 node->dump (f);
2134 /* Return true when the DECL can possibly be inlined. */
2136 bool
2137 cgraph_function_possibly_inlined_p (tree decl)
2139 if (!cgraph_global_info_ready)
2140 return !DECL_UNINLINABLE (decl);
2141 return DECL_POSSIBLY_INLINED (decl);
2144 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2145 void
2146 cgraph_node::unnest (void)
2148 struct cgraph_node **node2 = &origin->nested;
2149 gcc_assert (origin);
2151 while (*node2 != this)
2152 node2 = &(*node2)->next_nested;
2153 *node2 = next_nested;
2154 origin = NULL;
2157 /* Return function availability. See cgraph.h for description of individual
2158 return values. */
2159 enum availability
2160 cgraph_node::get_availability (void)
2162 enum availability avail;
2163 if (!analyzed)
2164 avail = AVAIL_NOT_AVAILABLE;
2165 else if (local.local)
2166 avail = AVAIL_LOCAL;
2167 else if (alias && weakref)
2168 ultimate_alias_target (&avail);
2169 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2170 avail = AVAIL_INTERPOSABLE;
2171 else if (!externally_visible)
2172 avail = AVAIL_AVAILABLE;
2173 /* Inline functions are safe to be analyzed even if their symbol can
2174 be overwritten at runtime. It is not meaningful to enforce any sane
2175 behaviour on replacing inline function by different body. */
2176 else if (DECL_DECLARED_INLINE_P (decl))
2177 avail = AVAIL_AVAILABLE;
2179 /* If the function can be overwritten, return OVERWRITABLE. Take
2180 care at least of two notable extensions - the COMDAT functions
2181 used to share template instantiations in C++ (this is symmetric
2182 to code cp_cannot_inline_tree_fn and probably shall be shared and
2183 the inlinability hooks completely eliminated).
2185 ??? Does the C++ one definition rule allow us to always return
2186 AVAIL_AVAILABLE here? That would be good reason to preserve this
2187 bit. */
2189 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2190 avail = AVAIL_INTERPOSABLE;
2191 else avail = AVAIL_AVAILABLE;
2193 return avail;
2196 /* Worker for cgraph_node_can_be_local_p. */
2197 static bool
2198 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node, void *)
2200 return !(!node->force_output
2201 && ((DECL_COMDAT (node->decl)
2202 && !node->forced_by_abi
2203 && !node->used_from_object_file_p ()
2204 && !node->same_comdat_group)
2205 || !node->externally_visible));
2208 /* Return true if cgraph_node can be made local for API change.
2209 Extern inline functions and C++ COMDAT functions can be made local
2210 at the expense of possible code size growth if function is used in multiple
2211 compilation units. */
2212 bool
2213 cgraph_node::can_be_local_p (void)
2215 return (!address_taken
2216 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2217 NULL, true));
2220 /* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
2221 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2222 skipped. */
2224 bool
2225 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2226 (cgraph_node *, void *),
2227 void *data,
2228 bool include_overwritable)
2230 struct cgraph_edge *e;
2231 struct ipa_ref *ref;
2233 if (callback (this, data))
2234 return true;
2235 for (e = callers; e; e = e->next_caller)
2236 if (e->caller->thunk.thunk_p
2237 && (include_overwritable
2238 || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2239 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2240 include_overwritable))
2241 return true;
2243 FOR_EACH_ALIAS (this, ref)
2245 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2246 if (include_overwritable
2247 || alias->get_availability () > AVAIL_INTERPOSABLE)
2248 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2249 include_overwritable))
2250 return true;
2252 return false;
2255 /* Call calback on function and aliases associated to the function.
2256 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2257 skipped. */
2259 bool
2260 cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2261 void *),
2262 void *data,
2263 bool include_overwritable)
2265 struct ipa_ref *ref;
2267 if (callback (this, data))
2268 return true;
2270 FOR_EACH_ALIAS (this, ref)
2272 struct cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2273 if (include_overwritable
2274 || alias->get_availability () > AVAIL_INTERPOSABLE)
2275 if (alias->call_for_symbol_and_aliases (callback, data,
2276 include_overwritable))
2277 return true;
2279 return false;
2282 /* Worker to bring NODE local. */
2284 bool
2285 cgraph_node::make_local (struct cgraph_node *node, void *)
2287 gcc_checking_assert (node->can_be_local_p ());
2288 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2290 node->make_decl_local ();
2291 node->set_section (NULL);
2292 node->set_comdat_group (NULL);
2293 node->externally_visible = false;
2294 node->forced_by_abi = false;
2295 node->local.local = true;
2296 node->set_section (NULL);
2297 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2298 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2299 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2300 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2302 return false;
2305 /* Bring cgraph node local. */
2307 void
2308 cgraph_node::make_local (void)
2310 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2313 /* Worker to set nothrow flag. */
2315 static bool
2316 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2318 struct cgraph_edge *e;
2320 TREE_NOTHROW (node->decl) = data != NULL;
2322 if (data != NULL)
2323 for (e = node->callers; e; e = e->next_caller)
2324 e->can_throw_external = false;
2325 return false;
2328 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2329 if any to NOTHROW. */
2331 void
2332 cgraph_node::set_nothrow_flag (bool nothrow)
2334 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2335 (void *)(size_t)nothrow, false);
2338 /* Worker to set const flag. */
2340 static bool
2341 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2343 /* Static constructors and destructors without a side effect can be
2344 optimized out. */
2345 if (data && !((size_t)data & 2))
2347 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2348 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2349 if (DECL_STATIC_DESTRUCTOR (node->decl))
2350 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2352 TREE_READONLY (node->decl) = data != NULL;
2353 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2354 return false;
2357 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2358 if any to READONLY. */
2360 void
2361 cgraph_node::set_const_flag (bool readonly, bool looping)
2363 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2364 (void *)(size_t)(readonly + (int)looping * 2),
2365 false);
2368 /* Worker to set pure flag. */
2370 static bool
2371 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2373 /* Static constructors and destructors without a side effect can be
2374 optimized out. */
2375 if (data && !((size_t)data & 2))
2377 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2378 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2379 if (DECL_STATIC_DESTRUCTOR (node->decl))
2380 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2382 DECL_PURE_P (node->decl) = data != NULL;
2383 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2384 return false;
2387 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2388 if any to PURE. */
2390 void
2391 cgraph_node::set_pure_flag (bool pure, bool looping)
2393 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2394 (void *)(size_t)(pure + (int)looping * 2),
2395 false);
2398 /* Return true when cgraph_node can not return or throw and thus
2399 it is safe to ignore its side effects for IPA analysis. */
2401 bool
2402 cgraph_node::cannot_return_p (void)
2404 int flags = flags_from_decl_or_type (decl);
2405 if (!flag_exceptions)
2406 return (flags & ECF_NORETURN) != 0;
2407 else
2408 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2409 == (ECF_NORETURN | ECF_NOTHROW));
2412 /* Return true when call of E can not lead to return from caller
2413 and thus it is safe to ignore its side effects for IPA analysis
2414 when computing side effects of the caller.
2415 FIXME: We could actually mark all edges that have no reaching
2416 patch to the exit block or throw to get better results. */
2417 bool
2418 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2420 if (e->caller->cannot_return_p ())
2421 return true;
2422 if (e->indirect_unknown_callee)
2424 int flags = e->indirect_info->ecf_flags;
2425 if (!flag_exceptions)
2426 return (flags & ECF_NORETURN) != 0;
2427 else
2428 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2429 == (ECF_NORETURN | ECF_NOTHROW));
2431 else
2432 return e->callee->cannot_return_p ();
2435 /* Return true when function can be removed from callgraph
2436 if all direct calls are eliminated. */
2438 bool
2439 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2441 gcc_assert (!global.inlined_to);
2442 /* Extern inlines can always go, we will use the external definition. */
2443 if (DECL_EXTERNAL (decl))
2444 return true;
2445 /* When function is needed, we can not remove it. */
2446 if (force_output || used_from_other_partition)
2447 return false;
2448 if (DECL_STATIC_CONSTRUCTOR (decl)
2449 || DECL_STATIC_DESTRUCTOR (decl))
2450 return false;
2451 /* Only COMDAT functions can be removed if externally visible. */
2452 if (externally_visible
2453 && (!DECL_COMDAT (decl)
2454 || forced_by_abi
2455 || used_from_object_file_p ()))
2456 return false;
2457 return true;
2460 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2462 static bool
2463 nonremovable_p (struct cgraph_node *node, void *)
2465 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2468 /* Return true when function cgraph_node and its aliases can be removed from
2469 callgraph if all direct calls are eliminated. */
2471 bool
2472 cgraph_node::can_remove_if_no_direct_calls_p (void)
2474 /* Extern inlines can always go, we will use the external definition. */
2475 if (DECL_EXTERNAL (decl))
2476 return true;
2477 if (address_taken)
2478 return false;
2479 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2482 /* Return true when function cgraph_node can be expected to be removed
2483 from program when direct calls in this compilation unit are removed.
2485 As a special case COMDAT functions are
2486 cgraph_can_remove_if_no_direct_calls_p while the are not
2487 cgraph_only_called_directly_p (it is possible they are called from other
2488 unit)
2490 This function behaves as cgraph_only_called_directly_p because eliminating
2491 all uses of COMDAT function does not make it necessarily disappear from
2492 the program unless we are compiling whole program or we do LTO. In this
2493 case we know we win since dynamic linking will not really discard the
2494 linkonce section. */
2496 bool
2497 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2499 gcc_assert (!global.inlined_to);
2501 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2502 NULL, true))
2503 return false;
2504 if (!in_lto_p && !flag_whole_program)
2505 return only_called_directly_p ();
2506 else
2508 if (DECL_EXTERNAL (decl))
2509 return true;
2510 return can_remove_if_no_direct_calls_p ();
2515 /* Worker for cgraph_only_called_directly_p. */
2517 static bool
2518 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *)
2520 return !node->only_called_directly_or_aliased_p ();
2523 /* Return true when function cgraph_node and all its aliases are only called
2524 directly.
2525 i.e. it is not externally visible, address was not taken and
2526 it is not used in any other non-standard way. */
2528 bool
2529 cgraph_node::only_called_directly_p (void)
2531 gcc_assert (ultimate_alias_target () == this);
2532 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2533 NULL, true);
2537 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2539 static bool
2540 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2542 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2543 struct cgraph_edge *cs;
2544 enum availability avail;
2545 node->ultimate_alias_target (&avail);
2547 if (avail > AVAIL_INTERPOSABLE)
2548 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2549 if (!cs->indirect_inlining_edge)
2550 redirect_callers->safe_push (cs);
2551 return false;
2554 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2555 cgraph_node (i.e. are not overwritable). */
2557 vec<cgraph_edge *>
2558 cgraph_node::collect_callers (void)
2560 vec<cgraph_edge *> redirect_callers = vNULL;
2561 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2562 &redirect_callers, false);
2563 return redirect_callers;
2566 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2568 static bool
2569 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2571 bool skipped_thunk = false;
2572 node = node->ultimate_alias_target ();
2573 node2 = node2->ultimate_alias_target ();
2575 /* There are no virtual clones of thunks so check former_clone_of or if we
2576 might have skipped thunks because this adjustments are no longer
2577 necessary. */
2578 while (node->thunk.thunk_p)
2580 if (node2->former_clone_of == node->decl)
2581 return true;
2582 if (!node->thunk.this_adjusting)
2583 return false;
2584 node = node->callees->callee->ultimate_alias_target ();
2585 skipped_thunk = true;
2588 if (skipped_thunk)
2590 if (!node2->clone.args_to_skip
2591 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2592 return false;
2593 if (node2->former_clone_of == node->decl)
2594 return true;
2595 else if (!node2->clone_of)
2596 return false;
2599 while (node != node2 && node2)
2600 node2 = node2->clone_of;
2601 return node2 != NULL;
2604 /* Verify edge E count and frequency. */
2606 static bool
2607 verify_edge_count_and_frequency (struct cgraph_edge *e)
2609 bool error_found = false;
2610 if (e->count < 0)
2612 error ("caller edge count is negative");
2613 error_found = true;
2615 if (e->frequency < 0)
2617 error ("caller edge frequency is negative");
2618 error_found = true;
2620 if (e->frequency > CGRAPH_FREQ_MAX)
2622 error ("caller edge frequency is too large");
2623 error_found = true;
2625 if (gimple_has_body_p (e->caller->decl)
2626 && e->call_stmt
2627 && !e->caller->global.inlined_to
2628 && !e->speculative
2629 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2630 Remove this once edges are actually removed from the function at that time. */
2631 && (e->frequency
2632 || (inline_edge_summary_vec.exists ()
2633 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2634 || !inline_edge_summary (e)->predicate)))
2635 && (e->frequency
2636 != compute_call_stmt_bb_frequency (e->caller->decl,
2637 gimple_bb (e->call_stmt))))
2639 error ("caller edge frequency %i does not match BB frequency %i",
2640 e->frequency,
2641 compute_call_stmt_bb_frequency (e->caller->decl,
2642 gimple_bb (e->call_stmt)));
2643 error_found = true;
2645 return error_found;
2648 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2649 static void
2650 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2652 bool fndecl_was_null = false;
2653 /* debug_gimple_stmt needs correct cfun */
2654 if (cfun != this_cfun)
2655 set_cfun (this_cfun);
2656 /* ...and an actual current_function_decl */
2657 if (!current_function_decl)
2659 current_function_decl = this_cfun->decl;
2660 fndecl_was_null = true;
2662 debug_gimple_stmt (stmt);
2663 if (fndecl_was_null)
2664 current_function_decl = NULL;
2667 /* Verify that call graph edge E corresponds to DECL from the associated
2668 statement. Return true if the verification should fail. */
2670 static bool
2671 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2673 struct cgraph_node *node;
2675 if (!decl || e->callee->global.inlined_to)
2676 return false;
2677 if (cgraph_state == CGRAPH_LTO_STREAMING)
2678 return false;
2679 node = cgraph_node::get (decl);
2681 /* We do not know if a node from a different partition is an alias or what it
2682 aliases and therefore cannot do the former_clone_of check reliably. When
2683 body_removed is set, we have lost all information about what was alias or
2684 thunk of and also cannot proceed. */
2685 if (!node
2686 || node->body_removed
2687 || node->in_other_partition
2688 || e->callee->in_other_partition)
2689 return false;
2691 node = node->ultimate_alias_target ();
2693 /* Optimizers can redirect unreachable calls or calls triggering undefined
2694 behaviour to builtin_unreachable. */
2695 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2696 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2697 return false;
2699 if (e->callee->former_clone_of != node->decl
2700 && (node != e->callee->ultimate_alias_target ())
2701 && !clone_of_p (node, e->callee))
2702 return true;
2703 else
2704 return false;
2707 /* Verify cgraph nodes of given cgraph node. */
2708 DEBUG_FUNCTION void
2709 cgraph_node::verify_node (void)
2711 struct cgraph_edge *e;
2712 struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2713 basic_block this_block;
2714 gimple_stmt_iterator gsi;
2715 bool error_found = false;
2717 if (seen_error ())
2718 return;
2720 timevar_push (TV_CGRAPH_VERIFY);
2721 error_found |= verify_base ();
2722 for (e = callees; e; e = e->next_callee)
2723 if (e->aux)
2725 error ("aux field set for edge %s->%s",
2726 identifier_to_locale (e->caller->name ()),
2727 identifier_to_locale (e->callee->name ()));
2728 error_found = true;
2730 if (count < 0)
2732 error ("execution count is negative");
2733 error_found = true;
2735 if (global.inlined_to && same_comdat_group)
2737 error ("inline clone in same comdat group list");
2738 error_found = true;
2740 if (!definition && !in_other_partition && local.local)
2742 error ("local symbols must be defined");
2743 error_found = true;
2745 if (global.inlined_to && externally_visible)
2747 error ("externally visible inline clone");
2748 error_found = true;
2750 if (global.inlined_to && address_taken)
2752 error ("inline clone with address taken");
2753 error_found = true;
2755 if (global.inlined_to && force_output)
2757 error ("inline clone is forced to output");
2758 error_found = true;
2760 for (e = indirect_calls; e; e = e->next_callee)
2762 if (e->aux)
2764 error ("aux field set for indirect edge from %s",
2765 identifier_to_locale (e->caller->name ()));
2766 error_found = true;
2768 if (!e->indirect_unknown_callee
2769 || !e->indirect_info)
2771 error ("An indirect edge from %s is not marked as indirect or has "
2772 "associated indirect_info, the corresponding statement is: ",
2773 identifier_to_locale (e->caller->name ()));
2774 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2775 error_found = true;
2778 bool check_comdat = comdat_local_p ();
2779 for (e = callers; e; e = e->next_caller)
2781 if (verify_edge_count_and_frequency (e))
2782 error_found = true;
2783 if (check_comdat
2784 && !in_same_comdat_group_p (e->caller))
2786 error ("comdat-local function called by %s outside its comdat",
2787 identifier_to_locale (e->caller->name ()));
2788 error_found = true;
2790 if (!e->inline_failed)
2792 if (global.inlined_to
2793 != (e->caller->global.inlined_to
2794 ? e->caller->global.inlined_to : e->caller))
2796 error ("inlined_to pointer is wrong");
2797 error_found = true;
2799 if (callers->next_caller)
2801 error ("multiple inline callers");
2802 error_found = true;
2805 else
2806 if (global.inlined_to)
2808 error ("inlined_to pointer set for noninline callers");
2809 error_found = true;
2812 for (e = indirect_calls; e; e = e->next_callee)
2813 if (verify_edge_count_and_frequency (e))
2814 error_found = true;
2815 if (!callers && global.inlined_to)
2817 error ("inlined_to pointer is set but no predecessors found");
2818 error_found = true;
2820 if (global.inlined_to == this)
2822 error ("inlined_to pointer refers to itself");
2823 error_found = true;
2826 if (clone_of)
2828 struct cgraph_node *n;
2829 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2830 if (n == this)
2831 break;
2832 if (!n)
2834 error ("cgraph_node has wrong clone_of");
2835 error_found = true;
2838 if (clones)
2840 struct cgraph_node *n;
2841 for (n = clones; n; n = n->next_sibling_clone)
2842 if (n->clone_of != this)
2843 break;
2844 if (n)
2846 error ("cgraph_node has wrong clone list");
2847 error_found = true;
2850 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2852 error ("cgraph_node is in clone list but it is not clone");
2853 error_found = true;
2855 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2857 error ("cgraph_node has wrong prev_clone pointer");
2858 error_found = true;
2860 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2862 error ("double linked list of clones corrupted");
2863 error_found = true;
2866 if (analyzed && alias)
2868 bool ref_found = false;
2869 int i;
2870 struct ipa_ref *ref = NULL;
2872 if (callees)
2874 error ("Alias has call edges");
2875 error_found = true;
2877 for (i = 0; iterate_reference (i, ref); i++)
2878 if (ref->use != IPA_REF_ALIAS)
2880 error ("Alias has non-alias reference");
2881 error_found = true;
2883 else if (ref_found
2884 /* in LIPO mode, the alias can refer to the real target also */
2885 && !L_IPO_COMP_MODE)
2887 error ("Alias has more than one alias reference");
2888 error_found = true;
2890 else
2891 ref_found = true;
2892 if (!ref_found)
2894 error ("Analyzed alias has no reference");
2895 error_found = true;
2898 if (analyzed && thunk.thunk_p)
2900 if (!callees)
2902 error ("No edge out of thunk node");
2903 error_found = true;
2905 else if (callees->next_callee)
2907 error ("More than one edge out of thunk node");
2908 error_found = true;
2910 if (gimple_has_body_p (decl))
2912 error ("Thunk is not supposed to have body");
2913 error_found = true;
2916 else if (analyzed && gimple_has_body_p (decl)
2917 && !TREE_ASM_WRITTEN (decl)
2918 && (!DECL_EXTERNAL (decl) || global.inlined_to)
2919 && !flag_wpa)
2921 if (this_cfun->cfg)
2923 hash_set<gimple> stmts;
2924 int i;
2925 struct ipa_ref *ref = NULL;
2927 /* Reach the trees by walking over the CFG, and note the
2928 enclosing basic-blocks in the call edges. */
2929 FOR_EACH_BB_FN (this_block, this_cfun)
2931 for (gsi = gsi_start_phis (this_block);
2932 !gsi_end_p (gsi); gsi_next (&gsi))
2933 stmts.add (gsi_stmt (gsi));
2934 for (gsi = gsi_start_bb (this_block);
2935 !gsi_end_p (gsi);
2936 gsi_next (&gsi))
2938 gimple stmt = gsi_stmt (gsi);
2939 stmts.add (stmt);
2940 if (is_gimple_call (stmt))
2942 struct cgraph_edge *e = get_edge (stmt);
2943 tree decl = gimple_call_fndecl (stmt);
2944 if (e)
2946 if (e->aux)
2948 error ("shared call_stmt:");
2949 cgraph_debug_gimple_stmt (this_cfun, stmt);
2950 error_found = true;
2952 if (!e->indirect_unknown_callee)
2954 if (verify_edge_corresponds_to_fndecl (e, decl))
2956 error ("edge points to wrong declaration:");
2957 debug_tree (e->callee->decl);
2958 fprintf (stderr," Instead of:");
2959 debug_tree (decl);
2960 error_found = true;
2963 else if (decl)
2965 error ("an indirect edge with unknown callee "
2966 "corresponding to a call_stmt with "
2967 "a known declaration:");
2968 error_found = true;
2969 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2971 e->aux = (void *)1;
2973 else if (decl)
2975 error ("missing callgraph edge for call stmt:");
2976 cgraph_debug_gimple_stmt (this_cfun, stmt);
2977 error_found = true;
2982 for (i = 0; iterate_reference (i, ref); i++)
2983 if (ref->stmt && !stmts.contains (ref->stmt))
2985 error ("reference to dead statement");
2986 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2987 error_found = true;
2990 else
2991 /* No CFG available?! */
2992 gcc_unreachable ();
2994 for (e = callees; e; e = e->next_callee)
2996 if (!e->aux && e->call_stmt)
2998 error ("edge %s->%s has no corresponding call_stmt",
2999 identifier_to_locale (e->caller->name ()),
3000 identifier_to_locale (e->callee->name ()));
3001 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3002 error_found = true;
3004 e->aux = 0;
3006 for (e = indirect_calls; e; e = e->next_callee)
3008 if (!e->aux && !e->speculative)
3010 error ("an indirect edge from %s has no corresponding call_stmt",
3011 identifier_to_locale (e->caller->name ()));
3012 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3013 error_found = true;
3015 e->aux = 0;
3018 if (error_found)
3020 dump (stderr);
3021 internal_error ("verify_cgraph_node failed");
3023 timevar_pop (TV_CGRAPH_VERIFY);
3026 /* Verify whole cgraph structure. */
3027 DEBUG_FUNCTION void
3028 cgraph_node::verify_cgraph_nodes (void)
3030 struct cgraph_node *node;
3032 if (seen_error ())
3033 return;
3035 FOR_EACH_FUNCTION (node)
3036 node->verify ();
3039 /* Walk the alias chain to return the function cgraph_node is alias of.
3040 Walk through thunk, too.
3041 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3043 cgraph_node *
3044 cgraph_node::function_symbol (enum availability *availability)
3046 cgraph_node *node = NULL;
3050 node = ultimate_alias_target (availability);
3051 if (node->thunk.thunk_p)
3053 node = node->callees->callee;
3054 if (availability)
3056 enum availability a;
3057 a = node->get_availability ();
3058 if (a < *availability)
3059 *availability = a;
3061 node = node->ultimate_alias_target (availability);
3063 } while (node && node->thunk.thunk_p);
3064 return node;
3067 /* When doing LTO, read cgraph_node's body from disk if it is not already
3068 present. */
3070 bool
3071 cgraph_node::get_body (void)
3073 struct lto_file_decl_data *file_data;
3074 const char *data, *name;
3075 size_t len;
3076 tree decl = this->decl;
3078 if (DECL_RESULT (decl))
3079 return false;
3081 gcc_assert (in_lto_p);
3083 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3085 file_data = lto_file_data;
3086 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3088 /* We may have renamed the declaration, e.g., a static function. */
3089 name = lto_get_decl_name_mapping (file_data, name);
3091 data = lto_get_section_data (file_data, LTO_section_function_body,
3092 name, &len);
3093 if (!data)
3094 fatal_error ("%s: section %s is missing",
3095 file_data->file_name,
3096 name);
3098 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3100 lto_input_function_body (file_data, this, data);
3101 lto_stats.num_function_bodies++;
3102 lto_free_section_data (file_data, LTO_section_function_body, name,
3103 data, len);
3104 lto_free_function_in_decl_state_for_node (this);
3106 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3108 return true;
3111 /* Verify if the type of the argument matches that of the function
3112 declaration. If we cannot verify this or there is a mismatch,
3113 return false. */
3115 static bool
3116 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3118 tree parms, p;
3119 unsigned int i, nargs;
3121 /* Calls to internal functions always match their signature. */
3122 if (gimple_call_internal_p (stmt))
3123 return true;
3125 nargs = gimple_call_num_args (stmt);
3127 /* Get argument types for verification. */
3128 if (fndecl)
3129 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3130 else
3131 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3133 /* Verify if the type of the argument matches that of the function
3134 declaration. If we cannot verify this or there is a mismatch,
3135 return false. */
3136 if (fndecl && DECL_ARGUMENTS (fndecl))
3138 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3139 i < nargs;
3140 i++, p = DECL_CHAIN (p))
3142 tree arg;
3143 /* We cannot distinguish a varargs function from the case
3144 of excess parameters, still deferring the inlining decision
3145 to the callee is possible. */
3146 if (!p)
3147 break;
3148 arg = gimple_call_arg (stmt, i);
3149 if (p == error_mark_node
3150 || DECL_ARG_TYPE (p) == error_mark_node
3151 || arg == error_mark_node
3152 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3153 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3154 return false;
3156 if (args_count_match && p)
3157 return false;
3159 else if (parms)
3161 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3163 tree arg;
3164 /* If this is a varargs function defer inlining decision
3165 to callee. */
3166 if (!p)
3167 break;
3168 arg = gimple_call_arg (stmt, i);
3169 if (TREE_VALUE (p) == error_mark_node
3170 || arg == error_mark_node
3171 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3172 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3173 && !fold_convertible_p (TREE_VALUE (p), arg)))
3174 return false;
3177 else
3179 if (nargs != 0)
3180 return false;
3182 return true;
3185 /* Verify if the type of the argument and lhs of CALL_STMT matches
3186 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3187 true, the arg count needs to be the same.
3188 If we cannot verify this or there is a mismatch, return false. */
3190 bool
3191 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3192 bool args_count_match)
3194 tree lhs;
3196 if ((DECL_RESULT (callee)
3197 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3198 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3199 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3200 TREE_TYPE (lhs))
3201 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3202 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3203 return false;
3204 return true;
3207 #include "gt-cgraph.h"