2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / cgraph.c
blob009a165cdfae57308cbc30f5c42b19363dfca9de
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "varasm.h"
32 #include "calls.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "hashtab.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "ggc.h"
40 #include "debug.h"
41 #include "target.h"
42 #include "basic-block.h"
43 #include "cgraph.h"
44 #include "intl.h"
45 #include "gimple.h"
46 #include "gimple-iterator.h"
47 #include "timevar.h"
48 #include "dumpfile.h"
49 #include "gimple-ssa.h"
50 #include "cgraph.h"
51 #include "tree-cfg.h"
52 #include "tree-ssa.h"
53 #include "value-prof.h"
54 #include "except.h"
55 #include "diagnostic-core.h"
56 #include "rtl.h"
57 #include "ipa-utils.h"
58 #include "lto-streamer.h"
59 #include "ipa-inline.h"
60 #include "cfgloop.h"
61 #include "gimple-pretty-print.h"
63 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
64 #include "tree-pass.h"
66 static void cgraph_node_remove_callers (struct cgraph_node *node);
67 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
68 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
70 /* Queue of cgraph nodes scheduled to be lowered. */
71 symtab_node *x_cgraph_nodes_queue;
72 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
74 /* Number of nodes in existence. */
75 int cgraph_n_nodes;
77 /* Maximal uid used in cgraph nodes. */
78 int cgraph_max_uid;
80 /* Maximal uid used in cgraph edges. */
81 int cgraph_edge_max_uid;
83 /* Set when whole unit has been analyzed so we can access global info. */
84 bool cgraph_global_info_ready = false;
86 /* What state callgraph is in right now. */
87 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
89 /* Set when the cgraph is fully build and the basic flags are computed. */
90 bool cgraph_function_flags_ready = false;
92 /* List of hooks triggered on cgraph_edge events. */
93 struct cgraph_edge_hook_list {
94 cgraph_edge_hook hook;
95 void *data;
96 struct cgraph_edge_hook_list *next;
99 /* List of hooks triggered on cgraph_node events. */
100 struct cgraph_node_hook_list {
101 cgraph_node_hook hook;
102 void *data;
103 struct cgraph_node_hook_list *next;
106 /* List of hooks triggered on events involving two cgraph_edges. */
107 struct cgraph_2edge_hook_list {
108 cgraph_2edge_hook hook;
109 void *data;
110 struct cgraph_2edge_hook_list *next;
113 /* List of hooks triggered on events involving two cgraph_nodes. */
114 struct cgraph_2node_hook_list {
115 cgraph_2node_hook hook;
116 void *data;
117 struct cgraph_2node_hook_list *next;
120 /* List of hooks triggered when an edge is removed. */
121 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
122 /* List of hooks triggered when a node is removed. */
123 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
124 /* List of hooks triggered when an edge is duplicated. */
125 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
126 /* List of hooks triggered when a node is duplicated. */
127 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
128 /* List of hooks triggered when an function is inserted. */
129 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
131 /* Head of a linked list of unused (freed) call graph nodes.
132 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
133 static GTY(()) struct cgraph_node *free_nodes;
134 /* Head of a linked list of unused (freed) call graph edges.
135 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
136 static GTY(()) struct cgraph_edge *free_edges;
138 /* Did procss_same_body_aliases run? */
139 bool cpp_implicit_aliases_done;
141 /* Map a cgraph_node to cgraph_function_version_info using this htab.
142 The cgraph_function_version_info has a THIS_NODE field that is the
143 corresponding cgraph_node.. */
145 static GTY((param_is (struct cgraph_function_version_info))) htab_t
146 cgraph_fnver_htab = NULL;
148 /* Hash function for cgraph_fnver_htab. */
149 static hashval_t
150 cgraph_fnver_htab_hash (const void *ptr)
152 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
153 return (hashval_t)(uid);
156 /* eq function for cgraph_fnver_htab. */
157 static int
158 cgraph_fnver_htab_eq (const void *p1, const void *p2)
160 const struct cgraph_function_version_info *n1
161 = (const struct cgraph_function_version_info *)p1;
162 const struct cgraph_function_version_info *n2
163 = (const struct cgraph_function_version_info *)p2;
165 return n1->this_node->uid == n2->this_node->uid;
168 /* Mark as GC root all allocated nodes. */
169 static GTY(()) struct cgraph_function_version_info *
170 version_info_node = NULL;
172 /* Get the cgraph_function_version_info node corresponding to node. */
173 struct cgraph_function_version_info *
174 get_cgraph_node_version (struct cgraph_node *node)
176 struct cgraph_function_version_info *ret;
177 struct cgraph_function_version_info key;
178 key.this_node = node;
180 if (cgraph_fnver_htab == NULL)
181 return NULL;
183 ret = (struct cgraph_function_version_info *)
184 htab_find (cgraph_fnver_htab, &key);
186 return ret;
189 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
190 corresponding to cgraph_node NODE. */
191 struct cgraph_function_version_info *
192 insert_new_cgraph_node_version (struct cgraph_node *node)
194 void **slot;
196 version_info_node = NULL;
197 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
198 version_info_node->this_node = node;
200 if (cgraph_fnver_htab == NULL)
201 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
202 cgraph_fnver_htab_eq, NULL);
204 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
205 gcc_assert (slot != NULL);
206 *slot = version_info_node;
207 return version_info_node;
210 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
211 DECL is a duplicate declaration. */
212 void
213 delete_function_version (tree decl)
215 struct cgraph_node *decl_node = cgraph_get_node (decl);
216 struct cgraph_function_version_info *decl_v = NULL;
218 if (decl_node == NULL)
219 return;
221 decl_v = get_cgraph_node_version (decl_node);
223 if (decl_v == NULL)
224 return;
226 if (decl_v->prev != NULL)
227 decl_v->prev->next = decl_v->next;
229 if (decl_v->next != NULL)
230 decl_v->next->prev = decl_v->prev;
232 if (cgraph_fnver_htab != NULL)
233 htab_remove_elt (cgraph_fnver_htab, decl_v);
235 cgraph_remove_node (decl_node);
238 /* Record that DECL1 and DECL2 are semantically identical function
239 versions. */
240 void
241 record_function_versions (tree decl1, tree decl2)
243 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
244 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
245 struct cgraph_function_version_info *decl1_v = NULL;
246 struct cgraph_function_version_info *decl2_v = NULL;
247 struct cgraph_function_version_info *before;
248 struct cgraph_function_version_info *after;
250 gcc_assert (decl1_node != NULL && decl2_node != NULL);
251 decl1_v = get_cgraph_node_version (decl1_node);
252 decl2_v = get_cgraph_node_version (decl2_node);
254 if (decl1_v != NULL && decl2_v != NULL)
255 return;
257 if (decl1_v == NULL)
258 decl1_v = insert_new_cgraph_node_version (decl1_node);
260 if (decl2_v == NULL)
261 decl2_v = insert_new_cgraph_node_version (decl2_node);
263 /* Chain decl2_v and decl1_v. All semantically identical versions
264 will be chained together. */
266 before = decl1_v;
267 after = decl2_v;
269 while (before->next != NULL)
270 before = before->next;
272 while (after->prev != NULL)
273 after= after->prev;
275 before->next = after;
276 after->prev = before;
279 /* Macros to access the next item in the list of free cgraph nodes and
280 edges. */
281 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->next)
282 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
283 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
285 /* Register HOOK to be called with DATA on each removed edge. */
286 struct cgraph_edge_hook_list *
287 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
289 struct cgraph_edge_hook_list *entry;
290 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
292 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
293 entry->hook = hook;
294 entry->data = data;
295 entry->next = NULL;
296 while (*ptr)
297 ptr = &(*ptr)->next;
298 *ptr = entry;
299 return entry;
302 /* Remove ENTRY from the list of hooks called on removing edges. */
303 void
304 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
306 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
308 while (*ptr != entry)
309 ptr = &(*ptr)->next;
310 *ptr = entry->next;
311 free (entry);
314 /* Call all edge removal hooks. */
315 static void
316 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
318 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
319 while (entry)
321 entry->hook (e, entry->data);
322 entry = entry->next;
326 /* Register HOOK to be called with DATA on each removed node. */
327 struct cgraph_node_hook_list *
328 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
330 struct cgraph_node_hook_list *entry;
331 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
333 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
334 entry->hook = hook;
335 entry->data = data;
336 entry->next = NULL;
337 while (*ptr)
338 ptr = &(*ptr)->next;
339 *ptr = entry;
340 return entry;
343 /* Remove ENTRY from the list of hooks called on removing nodes. */
344 void
345 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
347 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
349 while (*ptr != entry)
350 ptr = &(*ptr)->next;
351 *ptr = entry->next;
352 free (entry);
355 /* Call all node removal hooks. */
356 static void
357 cgraph_call_node_removal_hooks (struct cgraph_node *node)
359 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
360 while (entry)
362 entry->hook (node, entry->data);
363 entry = entry->next;
367 /* Register HOOK to be called with DATA on each inserted node. */
368 struct cgraph_node_hook_list *
369 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
371 struct cgraph_node_hook_list *entry;
372 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
374 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
375 entry->hook = hook;
376 entry->data = data;
377 entry->next = NULL;
378 while (*ptr)
379 ptr = &(*ptr)->next;
380 *ptr = entry;
381 return entry;
384 /* Remove ENTRY from the list of hooks called on inserted nodes. */
385 void
386 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
388 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
390 while (*ptr != entry)
391 ptr = &(*ptr)->next;
392 *ptr = entry->next;
393 free (entry);
396 /* Call all node insertion hooks. */
397 void
398 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
400 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
401 while (entry)
403 entry->hook (node, entry->data);
404 entry = entry->next;
408 /* Register HOOK to be called with DATA on each duplicated edge. */
409 struct cgraph_2edge_hook_list *
410 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
412 struct cgraph_2edge_hook_list *entry;
413 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
415 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
416 entry->hook = hook;
417 entry->data = data;
418 entry->next = NULL;
419 while (*ptr)
420 ptr = &(*ptr)->next;
421 *ptr = entry;
422 return entry;
425 /* Remove ENTRY from the list of hooks called on duplicating edges. */
426 void
427 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
429 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
431 while (*ptr != entry)
432 ptr = &(*ptr)->next;
433 *ptr = entry->next;
434 free (entry);
437 /* Call all edge duplication hooks. */
438 void
439 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
440 struct cgraph_edge *cs2)
442 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
443 while (entry)
445 entry->hook (cs1, cs2, entry->data);
446 entry = entry->next;
450 /* Register HOOK to be called with DATA on each duplicated node. */
451 struct cgraph_2node_hook_list *
452 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
454 struct cgraph_2node_hook_list *entry;
455 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
457 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
458 entry->hook = hook;
459 entry->data = data;
460 entry->next = NULL;
461 while (*ptr)
462 ptr = &(*ptr)->next;
463 *ptr = entry;
464 return entry;
467 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
468 void
469 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
471 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
473 while (*ptr != entry)
474 ptr = &(*ptr)->next;
475 *ptr = entry->next;
476 free (entry);
479 /* Call all node duplication hooks. */
480 void
481 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
482 struct cgraph_node *node2)
484 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
485 while (entry)
487 entry->hook (node1, node2, entry->data);
488 entry = entry->next;
492 /* Allocate new callgraph node. */
494 static inline struct cgraph_node *
495 cgraph_allocate_node (void)
497 struct cgraph_node *node;
499 if (free_nodes)
501 node = free_nodes;
502 free_nodes = NEXT_FREE_NODE (node);
504 else
506 node = ggc_alloc_cleared_cgraph_node ();
507 node->uid = cgraph_max_uid++;
510 return node;
513 /* Allocate new callgraph node and insert it into basic data structures. */
515 struct cgraph_node *
516 cgraph_create_empty_node (void)
518 struct cgraph_node *node = cgraph_allocate_node ();
520 node->type = SYMTAB_FUNCTION;
521 node->frequency = NODE_FREQUENCY_NORMAL;
522 node->count_materialization_scale = REG_BR_PROB_BASE;
523 cgraph_n_nodes++;
524 return node;
527 /* Return cgraph node assigned to DECL. Create new one when needed. */
529 struct cgraph_node *
530 cgraph_create_node (tree decl)
532 struct cgraph_node *node = cgraph_create_empty_node ();
533 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
535 node->decl = decl;
536 symtab_register_node (node);
538 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
540 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
541 node->next_nested = node->origin->nested;
542 node->origin->nested = node;
544 return node;
547 /* Try to find a call graph node for declaration DECL and if it does not exist
548 or if it corresponds to an inline clone, create a new one. */
550 struct cgraph_node *
551 cgraph_get_create_node (tree decl)
553 struct cgraph_node *first_clone = cgraph_get_node (decl);
555 if (first_clone && !first_clone->global.inlined_to)
556 return first_clone;
558 struct cgraph_node *node = cgraph_create_node (decl);
559 if (first_clone)
561 first_clone->clone_of = node;
562 node->clones = first_clone;
563 symtab_prevail_in_asm_name_hash (node);
564 symtab_insert_node_to_hashtable (node);
565 if (dump_file)
566 fprintf (dump_file, "Introduced new external node "
567 "(%s/%i) and turned into root of the clone tree.\n",
568 xstrdup (node->name ()), node->order);
570 else if (dump_file)
571 fprintf (dump_file, "Introduced new external node "
572 "(%s/%i).\n", xstrdup (node->name ()),
573 node->order);
574 return node;
577 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
578 the function body is associated with (not necessarily cgraph_node (DECL). */
580 struct cgraph_node *
581 cgraph_create_function_alias (tree alias, tree target)
583 struct cgraph_node *alias_node;
585 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
586 || TREE_CODE (target) == IDENTIFIER_NODE);
587 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
588 alias_node = cgraph_get_create_node (alias);
589 gcc_assert (!alias_node->definition);
590 alias_node->alias_target = target;
591 alias_node->definition = true;
592 alias_node->alias = true;
593 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
594 alias_node->weakref = true;
595 return alias_node;
598 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
599 and NULL otherwise.
600 Same body aliases are output whenever the body of DECL is output,
601 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
603 struct cgraph_node *
604 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
606 struct cgraph_node *n;
607 #ifndef ASM_OUTPUT_DEF
608 /* If aliases aren't supported by the assembler, fail. */
609 return NULL;
610 #endif
611 /* Langhooks can create same body aliases of symbols not defined.
612 Those are useless. Drop them on the floor. */
613 if (cgraph_global_info_ready)
614 return NULL;
616 n = cgraph_create_function_alias (alias, decl);
617 n->cpp_implicit_alias = true;
618 if (cpp_implicit_aliases_done)
619 symtab_resolve_alias (n,
620 cgraph_get_node (decl));
621 return n;
624 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
625 aliases DECL with an adjustments made into the first parameter.
626 See comments in thunk_adjust for detail on the parameters. */
628 struct cgraph_node *
629 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
630 tree alias, tree decl ATTRIBUTE_UNUSED,
631 bool this_adjusting,
632 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
633 tree virtual_offset,
634 tree real_alias)
636 struct cgraph_node *node;
638 node = cgraph_get_node (alias);
639 if (node)
641 gcc_assert (node->definition);
642 gcc_assert (!node->alias);
643 gcc_assert (!node->thunk.thunk_p);
644 cgraph_remove_node (node);
647 node = cgraph_create_node (alias);
648 gcc_checking_assert (!virtual_offset
649 || tree_to_double_int (virtual_offset) ==
650 double_int::from_shwi (virtual_value));
651 node->thunk.fixed_offset = fixed_offset;
652 node->thunk.this_adjusting = this_adjusting;
653 node->thunk.virtual_value = virtual_value;
654 node->thunk.virtual_offset_p = virtual_offset != NULL;
655 node->thunk.alias = real_alias;
656 node->thunk.thunk_p = true;
657 node->definition = true;
659 return node;
662 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
663 Return NULL if there's no such node. */
665 struct cgraph_node *
666 cgraph_node_for_asm (tree asmname)
668 /* We do not want to look at inline clones. */
669 for (symtab_node *node = symtab_node_for_asm (asmname);
670 node;
671 node = node->next_sharing_asm_name)
673 cgraph_node *cn = dyn_cast <cgraph_node> (node);
674 if (cn && !cn->global.inlined_to)
675 return cn;
677 return NULL;
680 /* Returns a hash value for X (which really is a cgraph_edge). */
682 static hashval_t
683 edge_hash (const void *x)
685 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
688 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
690 static int
691 edge_eq (const void *x, const void *y)
693 return ((const struct cgraph_edge *) x)->call_stmt == y;
696 /* Add call graph edge E to call site hash of its caller. */
698 static inline void
699 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
701 void **slot;
702 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
703 e->call_stmt,
704 htab_hash_pointer (e->call_stmt),
705 INSERT);
706 *slot = e;
709 /* Add call graph edge E to call site hash of its caller. */
711 static inline void
712 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
714 void **slot;
715 /* There are two speculative edges for every statement (one direct,
716 one indirect); always hash the direct one. */
717 if (e->speculative && e->indirect_unknown_callee)
718 return;
719 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
720 e->call_stmt,
721 htab_hash_pointer (e->call_stmt),
722 INSERT);
723 if (*slot)
725 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
726 if (e->callee)
727 *slot = e;
728 return;
730 gcc_assert (!*slot || e->speculative);
731 *slot = e;
734 /* Return the callgraph edge representing the GIMPLE_CALL statement
735 CALL_STMT. */
737 struct cgraph_edge *
738 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
740 struct cgraph_edge *e, *e2;
741 int n = 0;
743 if (node->call_site_hash)
744 return (struct cgraph_edge *)
745 htab_find_with_hash (node->call_site_hash, call_stmt,
746 htab_hash_pointer (call_stmt));
748 /* This loop may turn out to be performance problem. In such case adding
749 hashtables into call nodes with very many edges is probably best
750 solution. It is not good idea to add pointer into CALL_EXPR itself
751 because we want to make possible having multiple cgraph nodes representing
752 different clones of the same body before the body is actually cloned. */
753 for (e = node->callees; e; e = e->next_callee)
755 if (e->call_stmt == call_stmt)
756 break;
757 n++;
760 if (!e)
761 for (e = node->indirect_calls; e; e = e->next_callee)
763 if (e->call_stmt == call_stmt)
764 break;
765 n++;
768 if (n > 100)
770 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
771 for (e2 = node->callees; e2; e2 = e2->next_callee)
772 cgraph_add_edge_to_call_site_hash (e2);
773 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
774 cgraph_add_edge_to_call_site_hash (e2);
777 return e;
781 /* Change field call_stmt of edge E to NEW_STMT.
782 If UPDATE_SPECULATIVE and E is any component of speculative
783 edge, then update all components. */
785 void
786 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
787 bool update_speculative)
789 tree decl;
791 /* Speculative edges has three component, update all of them
792 when asked to. */
793 if (update_speculative && e->speculative)
795 struct cgraph_edge *direct, *indirect;
796 struct ipa_ref *ref;
798 cgraph_speculative_call_info (e, direct, indirect, ref);
799 cgraph_set_call_stmt (direct, new_stmt, false);
800 cgraph_set_call_stmt (indirect, new_stmt, false);
801 ref->stmt = new_stmt;
802 return;
805 /* Only direct speculative edges go to call_site_hash. */
806 if (e->caller->call_site_hash
807 && (!e->speculative || !e->indirect_unknown_callee))
809 htab_remove_elt_with_hash (e->caller->call_site_hash,
810 e->call_stmt,
811 htab_hash_pointer (e->call_stmt));
814 e->call_stmt = new_stmt;
815 if (e->indirect_unknown_callee
816 && (decl = gimple_call_fndecl (new_stmt)))
818 /* Constant propagation (and possibly also inlining?) can turn an
819 indirect call into a direct one. */
820 struct cgraph_node *new_callee = cgraph_get_node (decl);
822 gcc_checking_assert (new_callee);
823 e = cgraph_make_edge_direct (e, new_callee);
826 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
827 e->can_throw_external = stmt_can_throw_external (new_stmt);
828 pop_cfun ();
829 if (e->caller->call_site_hash)
830 cgraph_add_edge_to_call_site_hash (e);
833 /* Allocate a cgraph_edge structure and fill it with data according to the
834 parameters of which only CALLEE can be NULL (when creating an indirect call
835 edge). */
837 static struct cgraph_edge *
838 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
839 gimple call_stmt, gcov_type count, int freq,
840 bool indir_unknown_callee)
842 struct cgraph_edge *edge;
844 /* LTO does not actually have access to the call_stmt since these
845 have not been loaded yet. */
846 if (call_stmt)
848 /* This is a rather expensive check possibly triggering
849 construction of call stmt hashtable. */
850 #ifdef ENABLE_CHECKING
851 struct cgraph_edge *e;
852 gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
853 #endif
855 gcc_assert (is_gimple_call (call_stmt));
858 if (free_edges)
860 edge = free_edges;
861 free_edges = NEXT_FREE_EDGE (edge);
863 else
865 edge = ggc_alloc_cgraph_edge ();
866 edge->uid = cgraph_edge_max_uid++;
869 edge->aux = NULL;
870 edge->caller = caller;
871 edge->callee = callee;
872 edge->prev_caller = NULL;
873 edge->next_caller = NULL;
874 edge->prev_callee = NULL;
875 edge->next_callee = NULL;
876 edge->lto_stmt_uid = 0;
878 edge->count = count;
879 gcc_assert (count >= 0);
880 edge->frequency = freq;
881 gcc_assert (freq >= 0);
882 gcc_assert (freq <= CGRAPH_FREQ_MAX);
884 edge->call_stmt = call_stmt;
885 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
886 edge->can_throw_external
887 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
888 pop_cfun ();
889 if (call_stmt
890 && callee && callee->decl
891 && !gimple_check_call_matching_types (call_stmt, callee->decl,
892 false))
893 edge->call_stmt_cannot_inline_p = true;
894 else
895 edge->call_stmt_cannot_inline_p = false;
897 edge->indirect_info = NULL;
898 edge->indirect_inlining_edge = 0;
899 edge->speculative = false;
900 edge->indirect_unknown_callee = indir_unknown_callee;
901 if (call_stmt && caller->call_site_hash)
902 cgraph_add_edge_to_call_site_hash (edge);
904 return edge;
907 /* Create edge from CALLER to CALLEE in the cgraph. */
909 struct cgraph_edge *
910 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
911 gimple call_stmt, gcov_type count, int freq)
913 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
914 count, freq, false);
916 initialize_inline_failed (edge);
918 edge->next_caller = callee->callers;
919 if (callee->callers)
920 callee->callers->prev_caller = edge;
921 edge->next_callee = caller->callees;
922 if (caller->callees)
923 caller->callees->prev_callee = edge;
924 caller->callees = edge;
925 callee->callers = edge;
927 return edge;
930 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
932 struct cgraph_indirect_call_info *
933 cgraph_allocate_init_indirect_info (void)
935 struct cgraph_indirect_call_info *ii;
937 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
938 ii->param_index = -1;
939 return ii;
942 /* Create an indirect edge with a yet-undetermined callee where the call
943 statement destination is a formal parameter of the caller with index
944 PARAM_INDEX. */
946 struct cgraph_edge *
947 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
948 int ecf_flags,
949 gcov_type count, int freq)
951 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
952 count, freq, true);
953 tree target;
955 initialize_inline_failed (edge);
957 edge->indirect_info = cgraph_allocate_init_indirect_info ();
958 edge->indirect_info->ecf_flags = ecf_flags;
960 /* Record polymorphic call info. */
961 if (call_stmt
962 && (target = gimple_call_fn (call_stmt))
963 && virtual_method_call_p (target))
965 tree otr_type;
966 HOST_WIDE_INT otr_token;
967 ipa_polymorphic_call_context context;
969 get_polymorphic_call_info (caller->decl,
970 target,
971 &otr_type, &otr_token,
972 &context);
974 /* Only record types can have virtual calls. */
975 gcc_assert (TREE_CODE (otr_type) == RECORD_TYPE);
976 edge->indirect_info->polymorphic = true;
977 edge->indirect_info->param_index = -1;
978 edge->indirect_info->otr_token = otr_token;
979 edge->indirect_info->otr_type = otr_type;
980 edge->indirect_info->outer_type = context.outer_type;
981 edge->indirect_info->offset = context.offset;
982 edge->indirect_info->maybe_in_construction
983 = context.maybe_in_construction;
984 edge->indirect_info->maybe_derived_type = context.maybe_derived_type;
987 edge->next_callee = caller->indirect_calls;
988 if (caller->indirect_calls)
989 caller->indirect_calls->prev_callee = edge;
990 caller->indirect_calls = edge;
992 return edge;
995 /* Remove the edge E from the list of the callers of the callee. */
997 static inline void
998 cgraph_edge_remove_callee (struct cgraph_edge *e)
1000 gcc_assert (!e->indirect_unknown_callee);
1001 if (e->prev_caller)
1002 e->prev_caller->next_caller = e->next_caller;
1003 if (e->next_caller)
1004 e->next_caller->prev_caller = e->prev_caller;
1005 if (!e->prev_caller)
1006 e->callee->callers = e->next_caller;
1009 /* Remove the edge E from the list of the callees of the caller. */
1011 static inline void
1012 cgraph_edge_remove_caller (struct cgraph_edge *e)
1014 if (e->prev_callee)
1015 e->prev_callee->next_callee = e->next_callee;
1016 if (e->next_callee)
1017 e->next_callee->prev_callee = e->prev_callee;
1018 if (!e->prev_callee)
1020 if (e->indirect_unknown_callee)
1021 e->caller->indirect_calls = e->next_callee;
1022 else
1023 e->caller->callees = e->next_callee;
1025 if (e->caller->call_site_hash)
1026 htab_remove_elt_with_hash (e->caller->call_site_hash,
1027 e->call_stmt,
1028 htab_hash_pointer (e->call_stmt));
1031 /* Put the edge onto the free list. */
1033 static void
1034 cgraph_free_edge (struct cgraph_edge *e)
1036 int uid = e->uid;
1038 if (e->indirect_info)
1039 ggc_free (e->indirect_info);
1041 /* Clear out the edge so we do not dangle pointers. */
1042 memset (e, 0, sizeof (*e));
1043 e->uid = uid;
1044 NEXT_FREE_EDGE (e) = free_edges;
1045 free_edges = e;
1048 /* Remove the edge E in the cgraph. */
1050 void
1051 cgraph_remove_edge (struct cgraph_edge *e)
1053 /* Call all edge removal hooks. */
1054 cgraph_call_edge_removal_hooks (e);
1056 if (!e->indirect_unknown_callee)
1057 /* Remove from callers list of the callee. */
1058 cgraph_edge_remove_callee (e);
1060 /* Remove from callees list of the callers. */
1061 cgraph_edge_remove_caller (e);
1063 /* Put the edge onto the free list. */
1064 cgraph_free_edge (e);
1067 /* Set callee of call graph edge E and add it to the corresponding set of
1068 callers. */
1070 static void
1071 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1073 e->prev_caller = NULL;
1074 if (n->callers)
1075 n->callers->prev_caller = e;
1076 e->next_caller = n->callers;
1077 n->callers = e;
1078 e->callee = n;
1081 /* Turn edge E into speculative call calling N2. Update
1082 the profile so the direct call is taken COUNT times
1083 with FREQUENCY.
1085 At clone materialization time, the indirect call E will
1086 be expanded as:
1088 if (call_dest == N2)
1089 n2 ();
1090 else
1091 call call_dest
1093 At this time the function just creates the direct call,
1094 the referencd representing the if conditional and attaches
1095 them all to the orginal indirect call statement.
1097 Return direct edge created. */
1099 struct cgraph_edge *
1100 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1101 struct cgraph_node *n2,
1102 gcov_type direct_count,
1103 int direct_frequency)
1105 struct cgraph_node *n = e->caller;
1106 struct ipa_ref *ref;
1107 struct cgraph_edge *e2;
1109 if (dump_file)
1111 fprintf (dump_file, "Indirect call -> speculative call"
1112 " %s/%i => %s/%i\n",
1113 xstrdup (n->name ()), n->order,
1114 xstrdup (n2->name ()), n2->order);
1116 e->speculative = true;
1117 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1118 initialize_inline_failed (e2);
1119 e2->speculative = true;
1120 if (TREE_NOTHROW (n2->decl))
1121 e2->can_throw_external = false;
1122 else
1123 e2->can_throw_external = e->can_throw_external;
1124 e2->lto_stmt_uid = e->lto_stmt_uid;
1125 e->count -= e2->count;
1126 e->frequency -= e2->frequency;
1127 cgraph_call_edge_duplication_hooks (e, e2);
1128 ref = ipa_record_reference (n, n2,
1129 IPA_REF_ADDR, e->call_stmt);
1130 ref->lto_stmt_uid = e->lto_stmt_uid;
1131 ref->speculative = e->speculative;
1132 cgraph_mark_address_taken_node (n2);
1133 return e2;
1136 /* Speculative call consist of three components:
1137 1) an indirect edge representing the original call
1138 2) an direct edge representing the new call
1139 3) ADDR_EXPR reference representing the speculative check.
1140 All three components are attached to single statement (the indirect
1141 call) and if one of them exists, all of them must exist.
1143 Given speculative call edge E, return all three components.
1146 void
1147 cgraph_speculative_call_info (struct cgraph_edge *e,
1148 struct cgraph_edge *&direct,
1149 struct cgraph_edge *&indirect,
1150 struct ipa_ref *&reference)
1152 struct ipa_ref *ref;
1153 int i;
1154 struct cgraph_edge *e2;
1156 if (!e->indirect_unknown_callee)
1157 for (e2 = e->caller->indirect_calls;
1158 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1159 e2 = e2->next_callee)
1161 else
1163 e2 = e;
1164 /* We can take advantage of the call stmt hash. */
1165 if (e2->call_stmt)
1167 e = cgraph_edge (e->caller, e2->call_stmt);
1168 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1170 else
1171 for (e = e->caller->callees;
1172 e2->call_stmt != e->call_stmt
1173 || e2->lto_stmt_uid != e->lto_stmt_uid;
1174 e = e->next_callee)
1177 gcc_assert (e->speculative && e2->speculative);
1178 direct = e;
1179 indirect = e2;
1181 reference = NULL;
1182 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->ref_list,
1183 i, ref); i++)
1184 if (ref->speculative
1185 && ((ref->stmt && ref->stmt == e->call_stmt)
1186 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1188 reference = ref;
1189 break;
1192 /* Speculative edge always consist of all three components - direct edge,
1193 indirect and reference. */
1195 gcc_assert (e && e2 && ref);
1198 /* Redirect callee of E to N. The function does not update underlying
1199 call expression. */
1201 void
1202 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1204 /* Remove from callers list of the current callee. */
1205 cgraph_edge_remove_callee (e);
1207 /* Insert to callers list of the new callee. */
1208 cgraph_set_edge_callee (e, n);
1211 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1212 Remove the speculative call sequence and return edge representing the call.
1213 It is up to caller to redirect the call as appropriate. */
1215 struct cgraph_edge *
1216 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1218 struct cgraph_edge *e2;
1219 struct ipa_ref *ref;
1221 gcc_assert (edge->speculative);
1222 cgraph_speculative_call_info (edge, e2, edge, ref);
1223 if (!callee_decl
1224 || !symtab_semantically_equivalent_p (ref->referred,
1225 symtab_get_node (callee_decl)))
1227 if (dump_file)
1229 if (callee_decl)
1231 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1232 "turned out to have contradicting known target ",
1233 xstrdup (edge->caller->name ()), edge->caller->order,
1234 xstrdup (e2->callee->name ()), e2->callee->order);
1235 print_generic_expr (dump_file, callee_decl, 0);
1236 fprintf (dump_file, "\n");
1238 else
1240 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1241 xstrdup (edge->caller->name ()), edge->caller->order,
1242 xstrdup (e2->callee->name ()), e2->callee->order);
1246 else
1248 struct cgraph_edge *tmp = edge;
1249 if (dump_file)
1250 fprintf (dump_file, "Speculative call turned into direct call.\n");
1251 edge = e2;
1252 e2 = tmp;
1253 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1254 in the functions inlined through it. */
1256 edge->count += e2->count;
1257 edge->frequency += e2->frequency;
1258 if (edge->frequency > CGRAPH_FREQ_MAX)
1259 edge->frequency = CGRAPH_FREQ_MAX;
1260 edge->speculative = false;
1261 e2->speculative = false;
1262 ipa_remove_reference (ref);
1263 if (e2->indirect_unknown_callee || e2->inline_failed)
1264 cgraph_remove_edge (e2);
1265 else
1266 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1267 if (edge->caller->call_site_hash)
1268 cgraph_update_edge_in_call_site_hash (edge);
1269 return edge;
1272 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1273 CALLEE. DELTA is an integer constant that is to be added to the this
1274 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1276 struct cgraph_edge *
1277 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1279 gcc_assert (edge->indirect_unknown_callee);
1281 /* If we are redirecting speculative call, make it non-speculative. */
1282 if (edge->indirect_unknown_callee && edge->speculative)
1284 edge = cgraph_resolve_speculation (edge, callee->decl);
1286 /* On successful speculation just return the pre existing direct edge. */
1287 if (!edge->indirect_unknown_callee)
1288 return edge;
1291 edge->indirect_unknown_callee = 0;
1292 ggc_free (edge->indirect_info);
1293 edge->indirect_info = NULL;
1295 /* Get the edge out of the indirect edge list. */
1296 if (edge->prev_callee)
1297 edge->prev_callee->next_callee = edge->next_callee;
1298 if (edge->next_callee)
1299 edge->next_callee->prev_callee = edge->prev_callee;
1300 if (!edge->prev_callee)
1301 edge->caller->indirect_calls = edge->next_callee;
1303 /* Put it into the normal callee list */
1304 edge->prev_callee = NULL;
1305 edge->next_callee = edge->caller->callees;
1306 if (edge->caller->callees)
1307 edge->caller->callees->prev_callee = edge;
1308 edge->caller->callees = edge;
1310 /* Insert to callers list of the new callee. */
1311 cgraph_set_edge_callee (edge, callee);
1313 if (edge->call_stmt)
1314 edge->call_stmt_cannot_inline_p
1315 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1316 false);
1318 /* We need to re-determine the inlining status of the edge. */
1319 initialize_inline_failed (edge);
1320 return edge;
1323 /* If necessary, change the function declaration in the call statement
1324 associated with E so that it corresponds to the edge callee. */
1326 gimple
1327 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1329 tree decl = gimple_call_fndecl (e->call_stmt);
1330 gimple new_stmt;
1331 gimple_stmt_iterator gsi;
1332 #ifdef ENABLE_CHECKING
1333 struct cgraph_node *node;
1334 #endif
1336 if (e->speculative)
1338 struct cgraph_edge *e2;
1339 gimple new_stmt;
1340 struct ipa_ref *ref;
1342 cgraph_speculative_call_info (e, e, e2, ref);
1343 /* If there already is an direct call (i.e. as a result of inliner's
1344 substitution), forget about speculating. */
1345 if (decl)
1346 e = cgraph_resolve_speculation (e, decl);
1347 /* If types do not match, speculation was likely wrong.
1348 The direct edge was posisbly redirected to the clone with a different
1349 signature. We did not update the call statement yet, so compare it
1350 with the reference that still points to the proper type. */
1351 else if (!gimple_check_call_matching_types (e->call_stmt,
1352 ref->referred->decl,
1353 true))
1355 if (dump_file)
1356 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1357 "Type mismatch.\n",
1358 xstrdup (e->caller->name ()),
1359 e->caller->order,
1360 xstrdup (e->callee->name ()),
1361 e->callee->order);
1362 e = cgraph_resolve_speculation (e, NULL);
1363 /* We are producing the final function body and will throw away the
1364 callgraph edges really soon. Reset the counts/frequencies to
1365 keep verifier happy in the case of roundoff errors. */
1366 e->count = gimple_bb (e->call_stmt)->count;
1367 e->frequency = compute_call_stmt_bb_frequency
1368 (e->caller->decl, gimple_bb (e->call_stmt));
1370 /* Expand speculation into GIMPLE code. */
1371 else
1373 if (dump_file)
1374 fprintf (dump_file,
1375 "Expanding speculative call of %s/%i -> %s/%i count:"
1376 HOST_WIDEST_INT_PRINT_DEC"\n",
1377 xstrdup (e->caller->name ()),
1378 e->caller->order,
1379 xstrdup (e->callee->name ()),
1380 e->callee->order,
1381 (HOST_WIDEST_INT)e->count);
1382 gcc_assert (e2->speculative);
1383 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1384 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1385 e->count || e2->count
1386 ? RDIV (e->count * REG_BR_PROB_BASE,
1387 e->count + e2->count)
1388 : e->frequency || e2->frequency
1389 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1390 e->frequency + e2->frequency)
1391 : REG_BR_PROB_BASE / 2,
1392 e->count, e->count + e2->count);
1393 e->speculative = false;
1394 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1395 new_stmt, false);
1396 e->frequency = compute_call_stmt_bb_frequency
1397 (e->caller->decl, gimple_bb (e->call_stmt));
1398 e2->frequency = compute_call_stmt_bb_frequency
1399 (e2->caller->decl, gimple_bb (e2->call_stmt));
1400 e2->speculative = false;
1401 ref->speculative = false;
1402 ref->stmt = NULL;
1403 /* Indirect edges are not both in the call site hash.
1404 get it updated. */
1405 if (e->caller->call_site_hash)
1406 cgraph_update_edge_in_call_site_hash (e2);
1407 pop_cfun ();
1408 /* Continue redirecting E to proper target. */
1412 if (e->indirect_unknown_callee
1413 || decl == e->callee->decl)
1414 return e->call_stmt;
1416 #ifdef ENABLE_CHECKING
1417 if (decl)
1419 node = cgraph_get_node (decl);
1420 gcc_assert (!node || !node->clone.combined_args_to_skip);
1422 #endif
1424 if (cgraph_dump_file)
1426 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1427 xstrdup (e->caller->name ()), e->caller->order,
1428 xstrdup (e->callee->name ()), e->callee->order);
1429 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1430 if (e->callee->clone.combined_args_to_skip)
1432 fprintf (cgraph_dump_file, " combined args to skip: ");
1433 dump_bitmap (cgraph_dump_file,
1434 e->callee->clone.combined_args_to_skip);
1438 if (e->callee->clone.combined_args_to_skip)
1440 int lp_nr;
1442 new_stmt
1443 = gimple_call_copy_skip_args (e->call_stmt,
1444 e->callee->clone.combined_args_to_skip);
1445 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1446 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1448 if (gimple_vdef (new_stmt)
1449 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1450 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1452 gsi = gsi_for_stmt (e->call_stmt);
1453 gsi_replace (&gsi, new_stmt, false);
1454 /* We need to defer cleaning EH info on the new statement to
1455 fixup-cfg. We may not have dominator information at this point
1456 and thus would end up with unreachable blocks and have no way
1457 to communicate that we need to run CFG cleanup then. */
1458 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1459 if (lp_nr != 0)
1461 remove_stmt_from_eh_lp (e->call_stmt);
1462 add_stmt_to_eh_lp (new_stmt, lp_nr);
1465 else
1467 new_stmt = e->call_stmt;
1468 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1469 update_stmt (new_stmt);
1472 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1474 if (cgraph_dump_file)
1476 fprintf (cgraph_dump_file, " updated to:");
1477 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1479 return new_stmt;
1482 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1483 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1484 of OLD_STMT if it was previously call statement.
1485 If NEW_STMT is NULL, the call has been dropped without any
1486 replacement. */
1488 static void
1489 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1490 gimple old_stmt, tree old_call,
1491 gimple new_stmt)
1493 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1494 ? gimple_call_fndecl (new_stmt) : 0;
1496 /* We are seeing indirect calls, then there is nothing to update. */
1497 if (!new_call && !old_call)
1498 return;
1499 /* See if we turned indirect call into direct call or folded call to one builtin
1500 into different builtin. */
1501 if (old_call != new_call)
1503 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1504 struct cgraph_edge *ne = NULL;
1505 gcov_type count;
1506 int frequency;
1508 if (e)
1510 /* See if the edge is already there and has the correct callee. It
1511 might be so because of indirect inlining has already updated
1512 it. We also might've cloned and redirected the edge. */
1513 if (new_call && e->callee)
1515 struct cgraph_node *callee = e->callee;
1516 while (callee)
1518 if (callee->decl == new_call
1519 || callee->former_clone_of == new_call)
1520 return;
1521 callee = callee->clone_of;
1525 /* Otherwise remove edge and create new one; we can't simply redirect
1526 since function has changed, so inline plan and other information
1527 attached to edge is invalid. */
1528 count = e->count;
1529 frequency = e->frequency;
1530 cgraph_remove_edge (e);
1532 else if (new_call)
1534 /* We are seeing new direct call; compute profile info based on BB. */
1535 basic_block bb = gimple_bb (new_stmt);
1536 count = bb->count;
1537 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1538 bb);
1541 if (new_call)
1543 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1544 new_stmt, count, frequency);
1545 gcc_assert (ne->inline_failed);
1548 /* We only updated the call stmt; update pointer in cgraph edge.. */
1549 else if (old_stmt != new_stmt)
1550 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1553 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1554 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1555 of OLD_STMT before it was updated (updating can happen inplace). */
1557 void
1558 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1560 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1561 struct cgraph_node *node;
1563 gcc_checking_assert (orig);
1564 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1565 if (orig->clones)
1566 for (node = orig->clones; node != orig;)
1568 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1569 if (node->clones)
1570 node = node->clones;
1571 else if (node->next_sibling_clone)
1572 node = node->next_sibling_clone;
1573 else
1575 while (node != orig && !node->next_sibling_clone)
1576 node = node->clone_of;
1577 if (node != orig)
1578 node = node->next_sibling_clone;
1584 /* Remove all callees from the node. */
1586 void
1587 cgraph_node_remove_callees (struct cgraph_node *node)
1589 struct cgraph_edge *e, *f;
1591 /* It is sufficient to remove the edges from the lists of callers of
1592 the callees. The callee list of the node can be zapped with one
1593 assignment. */
1594 for (e = node->callees; e; e = f)
1596 f = e->next_callee;
1597 cgraph_call_edge_removal_hooks (e);
1598 if (!e->indirect_unknown_callee)
1599 cgraph_edge_remove_callee (e);
1600 cgraph_free_edge (e);
1602 for (e = node->indirect_calls; e; e = f)
1604 f = e->next_callee;
1605 cgraph_call_edge_removal_hooks (e);
1606 if (!e->indirect_unknown_callee)
1607 cgraph_edge_remove_callee (e);
1608 cgraph_free_edge (e);
1610 node->indirect_calls = NULL;
1611 node->callees = NULL;
1612 if (node->call_site_hash)
1614 htab_delete (node->call_site_hash);
1615 node->call_site_hash = NULL;
1619 /* Remove all callers from the node. */
1621 static void
1622 cgraph_node_remove_callers (struct cgraph_node *node)
1624 struct cgraph_edge *e, *f;
1626 /* It is sufficient to remove the edges from the lists of callees of
1627 the callers. The caller list of the node can be zapped with one
1628 assignment. */
1629 for (e = node->callers; e; e = f)
1631 f = e->next_caller;
1632 cgraph_call_edge_removal_hooks (e);
1633 cgraph_edge_remove_caller (e);
1634 cgraph_free_edge (e);
1636 node->callers = NULL;
1639 /* Helper function for cgraph_release_function_body and free_lang_data.
1640 It releases body from function DECL without having to inspect its
1641 possibly non-existent symtab node. */
1643 void
1644 release_function_body (tree decl)
1646 if (DECL_STRUCT_FUNCTION (decl))
1648 push_cfun (DECL_STRUCT_FUNCTION (decl));
1649 if (cfun->cfg
1650 && current_loops)
1652 cfun->curr_properties &= ~PROP_loops;
1653 loop_optimizer_finalize ();
1655 if (cfun->gimple_df)
1657 delete_tree_ssa ();
1658 delete_tree_cfg_annotations ();
1659 cfun->eh = NULL;
1661 if (cfun->cfg)
1663 gcc_assert (dom_computed[0] == DOM_NONE);
1664 gcc_assert (dom_computed[1] == DOM_NONE);
1665 clear_edges ();
1666 cfun->cfg = NULL;
1668 if (cfun->value_histograms)
1669 free_histograms ();
1670 pop_cfun ();
1671 gimple_set_body (decl, NULL);
1672 /* Struct function hangs a lot of data that would leak if we didn't
1673 removed all pointers to it. */
1674 ggc_free (DECL_STRUCT_FUNCTION (decl));
1675 DECL_STRUCT_FUNCTION (decl) = NULL;
1677 DECL_SAVED_TREE (decl) = NULL;
1680 /* Release memory used to represent body of function NODE.
1681 Use this only for functions that are released before being translated to
1682 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1683 are free'd in final.c via free_after_compilation(). */
1685 void
1686 cgraph_release_function_body (struct cgraph_node *node)
1688 node->ipa_transforms_to_apply.release ();
1689 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1691 DECL_RESULT (node->decl) = NULL;
1692 DECL_ARGUMENTS (node->decl) = NULL;
1694 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1695 of its associated function function declaration because it's
1696 needed to emit debug info later. */
1697 if (!node->used_as_abstract_origin && DECL_INITIAL (node->decl))
1698 DECL_INITIAL (node->decl) = error_mark_node;
1699 release_function_body (node->decl);
1700 if (node->lto_file_data)
1701 lto_free_function_in_decl_state_for_node (node);
1704 /* Remove the node from cgraph. */
1706 void
1707 cgraph_remove_node (struct cgraph_node *node)
1709 struct cgraph_node *n;
1710 int uid = node->uid;
1712 cgraph_call_node_removal_hooks (node);
1713 cgraph_node_remove_callers (node);
1714 cgraph_node_remove_callees (node);
1715 node->ipa_transforms_to_apply.release ();
1717 /* Incremental inlining access removed nodes stored in the postorder list.
1719 node->force_output = false;
1720 node->forced_by_abi = false;
1721 for (n = node->nested; n; n = n->next_nested)
1722 n->origin = NULL;
1723 node->nested = NULL;
1724 if (node->origin)
1726 struct cgraph_node **node2 = &node->origin->nested;
1728 while (*node2 != node)
1729 node2 = &(*node2)->next_nested;
1730 *node2 = node->next_nested;
1732 symtab_unregister_node (node);
1733 if (node->prev_sibling_clone)
1734 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1735 else if (node->clone_of)
1736 node->clone_of->clones = node->next_sibling_clone;
1737 if (node->next_sibling_clone)
1738 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1739 if (node->clones)
1741 struct cgraph_node *n, *next;
1743 if (node->clone_of)
1745 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1746 n->clone_of = node->clone_of;
1747 n->clone_of = node->clone_of;
1748 n->next_sibling_clone = node->clone_of->clones;
1749 if (node->clone_of->clones)
1750 node->clone_of->clones->prev_sibling_clone = n;
1751 node->clone_of->clones = node->clones;
1753 else
1755 /* We are removing node with clones. This makes clones inconsistent,
1756 but assume they will be removed subsequently and just keep clone
1757 tree intact. This can happen in unreachable function removal since
1758 we remove unreachable functions in random order, not by bottom-up
1759 walk of clone trees. */
1760 for (n = node->clones; n; n = next)
1762 next = n->next_sibling_clone;
1763 n->next_sibling_clone = NULL;
1764 n->prev_sibling_clone = NULL;
1765 n->clone_of = NULL;
1770 /* While all the clones are removed after being proceeded, the function
1771 itself is kept in the cgraph even after it is compiled. Check whether
1772 we are done with this body and reclaim it proactively if this is the case.
1774 if (cgraph_state != CGRAPH_LTO_STREAMING)
1776 n = cgraph_get_node (node->decl);
1777 if (!n
1778 || (!n->clones && !n->clone_of && !n->global.inlined_to
1779 && (cgraph_global_info_ready
1780 && (TREE_ASM_WRITTEN (n->decl)
1781 || DECL_EXTERNAL (n->decl)
1782 || !n->analyzed
1783 || (!flag_wpa && n->in_other_partition)))))
1784 cgraph_release_function_body (node);
1787 node->decl = NULL;
1788 if (node->call_site_hash)
1790 htab_delete (node->call_site_hash);
1791 node->call_site_hash = NULL;
1793 cgraph_n_nodes--;
1795 /* Clear out the node to NULL all pointers and add the node to the free
1796 list. */
1797 memset (node, 0, sizeof (*node));
1798 node->type = SYMTAB_FUNCTION;
1799 node->uid = uid;
1800 SET_NEXT_FREE_NODE (node, free_nodes);
1801 free_nodes = node;
1804 /* Likewise indicate that a node is having address taken. */
1806 void
1807 cgraph_mark_address_taken_node (struct cgraph_node *node)
1809 /* Indirect inlining can figure out that all uses of the address are
1810 inlined. */
1811 if (node->global.inlined_to)
1813 gcc_assert (cfun->after_inlining);
1814 gcc_assert (node->callers->indirect_inlining_edge);
1815 return;
1817 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1818 IPA_REF_ADDR reference exists (and thus it should be set on node
1819 representing alias we take address of) and as a test whether address
1820 of the object was taken (and thus it should be set on node alias is
1821 referring to). We should remove the first use and the remove the
1822 following set. */
1823 node->address_taken = 1;
1824 node = cgraph_function_or_thunk_node (node, NULL);
1825 node->address_taken = 1;
1828 /* Return local info for the compiled function. */
1830 struct cgraph_local_info *
1831 cgraph_local_info (tree decl)
1833 struct cgraph_node *node;
1835 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1836 node = cgraph_get_node (decl);
1837 if (!node)
1838 return NULL;
1839 return &node->local;
1842 /* Return local info for the compiled function. */
1844 struct cgraph_global_info *
1845 cgraph_global_info (tree decl)
1847 struct cgraph_node *node;
1849 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1850 node = cgraph_get_node (decl);
1851 if (!node)
1852 return NULL;
1853 return &node->global;
1856 /* Return local info for the compiled function. */
1858 struct cgraph_rtl_info *
1859 cgraph_rtl_info (tree decl)
1861 struct cgraph_node *node;
1863 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1864 node = cgraph_get_node (decl);
1865 if (!node
1866 || (decl != current_function_decl
1867 && !TREE_ASM_WRITTEN (node->decl)))
1868 return NULL;
1869 return &node->rtl;
1872 /* Return a string describing the failure REASON. */
1874 const char*
1875 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1877 #undef DEFCIFCODE
1878 #define DEFCIFCODE(code, string) string,
1880 static const char *cif_string_table[CIF_N_REASONS] = {
1881 #include "cif-code.def"
1884 /* Signedness of an enum type is implementation defined, so cast it
1885 to unsigned before testing. */
1886 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1887 return cif_string_table[reason];
1890 /* Names used to print out the availability enum. */
1891 const char * const cgraph_availability_names[] =
1892 {"unset", "not_available", "overwritable", "available", "local"};
1895 /* Dump call graph node NODE to file F. */
1897 void
1898 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1900 struct cgraph_edge *edge;
1901 int indirect_calls_count = 0;
1903 dump_symtab_base (f, node);
1905 if (node->global.inlined_to)
1906 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1907 xstrdup (node->name ()),
1908 node->order,
1909 xstrdup (node->global.inlined_to->name ()),
1910 node->global.inlined_to->order);
1911 if (node->clone_of)
1912 fprintf (f, " Clone of %s/%i\n",
1913 node->clone_of->asm_name (),
1914 node->clone_of->order);
1915 if (cgraph_function_flags_ready)
1916 fprintf (f, " Availability: %s\n",
1917 cgraph_availability_names [cgraph_function_body_availability (node)]);
1919 if (node->profile_id)
1920 fprintf (f, " Profile id: %i\n",
1921 node->profile_id);
1922 fprintf (f, " First run: %i\n", node->tp_first_run);
1923 fprintf (f, " Function flags:");
1924 if (node->count)
1925 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1926 (HOST_WIDEST_INT)node->count);
1927 if (node->origin)
1928 fprintf (f, " nested in: %s", node->origin->asm_name ());
1929 if (gimple_has_body_p (node->decl))
1930 fprintf (f, " body");
1931 if (node->process)
1932 fprintf (f, " process");
1933 if (node->local.local)
1934 fprintf (f, " local");
1935 if (node->local.redefined_extern_inline)
1936 fprintf (f, " redefined_extern_inline");
1937 if (node->only_called_at_startup)
1938 fprintf (f, " only_called_at_startup");
1939 if (node->only_called_at_exit)
1940 fprintf (f, " only_called_at_exit");
1941 if (node->tm_clone)
1942 fprintf (f, " tm_clone");
1944 fprintf (f, "\n");
1946 if (node->thunk.thunk_p)
1948 fprintf (f, " Thunk");
1949 if (node->thunk.alias)
1950 fprintf (f, " of %s (asm: %s)",
1951 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1952 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1953 fprintf (f, " fixed offset %i virtual value %i has "
1954 "virtual offset %i)\n",
1955 (int)node->thunk.fixed_offset,
1956 (int)node->thunk.virtual_value,
1957 (int)node->thunk.virtual_offset_p);
1959 if (node->alias && node->thunk.alias
1960 && DECL_P (node->thunk.alias))
1962 fprintf (f, " Alias of %s",
1963 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1964 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1965 fprintf (f, " (asm: %s)",
1966 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1967 fprintf (f, "\n");
1970 fprintf (f, " Called by: ");
1972 for (edge = node->callers; edge; edge = edge->next_caller)
1974 fprintf (f, "%s/%i ", edge->caller->asm_name (),
1975 edge->caller->order);
1976 if (edge->count)
1977 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1978 (HOST_WIDEST_INT)edge->count);
1979 if (edge->frequency)
1980 fprintf (f, "(%.2f per call) ",
1981 edge->frequency / (double)CGRAPH_FREQ_BASE);
1982 if (edge->speculative)
1983 fprintf (f, "(speculative) ");
1984 if (!edge->inline_failed)
1985 fprintf (f, "(inlined) ");
1986 if (edge->indirect_inlining_edge)
1987 fprintf (f, "(indirect_inlining) ");
1988 if (edge->can_throw_external)
1989 fprintf (f, "(can throw external) ");
1992 fprintf (f, "\n Calls: ");
1993 for (edge = node->callees; edge; edge = edge->next_callee)
1995 fprintf (f, "%s/%i ", edge->callee->asm_name (),
1996 edge->callee->order);
1997 if (edge->speculative)
1998 fprintf (f, "(speculative) ");
1999 if (!edge->inline_failed)
2000 fprintf (f, "(inlined) ");
2001 if (edge->indirect_inlining_edge)
2002 fprintf (f, "(indirect_inlining) ");
2003 if (edge->count)
2004 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
2005 (HOST_WIDEST_INT)edge->count);
2006 if (edge->frequency)
2007 fprintf (f, "(%.2f per call) ",
2008 edge->frequency / (double)CGRAPH_FREQ_BASE);
2009 if (edge->can_throw_external)
2010 fprintf (f, "(can throw external) ");
2012 fprintf (f, "\n");
2014 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2015 indirect_calls_count++;
2016 if (indirect_calls_count)
2017 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
2018 indirect_calls_count);
2022 /* Dump call graph node NODE to stderr. */
2024 DEBUG_FUNCTION void
2025 debug_cgraph_node (struct cgraph_node *node)
2027 dump_cgraph_node (stderr, node);
2031 /* Dump the callgraph to file F. */
2033 void
2034 dump_cgraph (FILE *f)
2036 struct cgraph_node *node;
2038 fprintf (f, "callgraph:\n\n");
2039 FOR_EACH_FUNCTION (node)
2040 dump_cgraph_node (f, node);
2044 /* Dump the call graph to stderr. */
2046 DEBUG_FUNCTION void
2047 debug_cgraph (void)
2049 dump_cgraph (stderr);
2052 /* Return true when the DECL can possibly be inlined. */
2053 bool
2054 cgraph_function_possibly_inlined_p (tree decl)
2056 if (!cgraph_global_info_ready)
2057 return !DECL_UNINLINABLE (decl);
2058 return DECL_POSSIBLY_INLINED (decl);
2061 /* NODE is no longer nested function; update cgraph accordingly. */
2062 void
2063 cgraph_unnest_node (struct cgraph_node *node)
2065 struct cgraph_node **node2 = &node->origin->nested;
2066 gcc_assert (node->origin);
2068 while (*node2 != node)
2069 node2 = &(*node2)->next_nested;
2070 *node2 = node->next_nested;
2071 node->origin = NULL;
2074 /* Return function availability. See cgraph.h for description of individual
2075 return values. */
2076 enum availability
2077 cgraph_function_body_availability (struct cgraph_node *node)
2079 enum availability avail;
2080 if (!node->analyzed)
2081 avail = AVAIL_NOT_AVAILABLE;
2082 else if (node->local.local)
2083 avail = AVAIL_LOCAL;
2084 else if (node->alias && node->weakref)
2085 cgraph_function_or_thunk_node (node, &avail);
2086 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->decl)))
2087 avail = AVAIL_OVERWRITABLE;
2088 else if (!node->externally_visible)
2089 avail = AVAIL_AVAILABLE;
2090 /* Inline functions are safe to be analyzed even if their symbol can
2091 be overwritten at runtime. It is not meaningful to enforce any sane
2092 behaviour on replacing inline function by different body. */
2093 else if (DECL_DECLARED_INLINE_P (node->decl))
2094 avail = AVAIL_AVAILABLE;
2096 /* If the function can be overwritten, return OVERWRITABLE. Take
2097 care at least of two notable extensions - the COMDAT functions
2098 used to share template instantiations in C++ (this is symmetric
2099 to code cp_cannot_inline_tree_fn and probably shall be shared and
2100 the inlinability hooks completely eliminated).
2102 ??? Does the C++ one definition rule allow us to always return
2103 AVAIL_AVAILABLE here? That would be good reason to preserve this
2104 bit. */
2106 else if (decl_replaceable_p (node->decl)
2107 && !DECL_EXTERNAL (node->decl))
2108 avail = AVAIL_OVERWRITABLE;
2109 else avail = AVAIL_AVAILABLE;
2111 return avail;
2114 /* Worker for cgraph_node_can_be_local_p. */
2115 static bool
2116 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2117 void *data ATTRIBUTE_UNUSED)
2119 return !(!node->force_output
2120 && ((DECL_COMDAT (node->decl)
2121 && !node->forced_by_abi
2122 && !symtab_used_from_object_file_p (node)
2123 && !node->same_comdat_group)
2124 || !node->externally_visible));
2127 /* Return true if NODE can be made local for API change.
2128 Extern inline functions and C++ COMDAT functions can be made local
2129 at the expense of possible code size growth if function is used in multiple
2130 compilation units. */
2131 bool
2132 cgraph_node_can_be_local_p (struct cgraph_node *node)
2134 return (!node->address_taken
2135 && !cgraph_for_node_and_aliases (node,
2136 cgraph_node_cannot_be_local_p_1,
2137 NULL, true));
2140 /* Call calback on NODE, thunks and aliases associated to NODE.
2141 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2142 skipped. */
2144 bool
2145 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2146 bool (*callback) (struct cgraph_node *, void *),
2147 void *data,
2148 bool include_overwritable)
2150 struct cgraph_edge *e;
2151 int i;
2152 struct ipa_ref *ref;
2154 if (callback (node, data))
2155 return true;
2156 for (e = node->callers; e; e = e->next_caller)
2157 if (e->caller->thunk.thunk_p
2158 && (include_overwritable
2159 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2160 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2161 include_overwritable))
2162 return true;
2163 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2164 if (ref->use == IPA_REF_ALIAS)
2166 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2167 if (include_overwritable
2168 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2169 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2170 include_overwritable))
2171 return true;
2173 return false;
2176 /* Call calback on NODE and aliases associated to NODE.
2177 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2178 skipped. */
2180 bool
2181 cgraph_for_node_and_aliases (struct cgraph_node *node,
2182 bool (*callback) (struct cgraph_node *, void *),
2183 void *data,
2184 bool include_overwritable)
2186 int i;
2187 struct ipa_ref *ref;
2189 if (callback (node, data))
2190 return true;
2191 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2192 if (ref->use == IPA_REF_ALIAS)
2194 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2195 if (include_overwritable
2196 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2197 if (cgraph_for_node_and_aliases (alias, callback, data,
2198 include_overwritable))
2199 return true;
2201 return false;
2204 /* Worker to bring NODE local. */
2206 static bool
2207 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2209 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2210 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2212 symtab_make_decl_local (node->decl);
2214 node->externally_visible = false;
2215 node->forced_by_abi = false;
2216 node->local.local = true;
2217 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2218 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2219 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2220 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2222 return false;
2225 /* Bring NODE local. */
2227 void
2228 cgraph_make_node_local (struct cgraph_node *node)
2230 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2231 NULL, true);
2234 /* Worker to set nothrow flag. */
2236 static bool
2237 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2239 struct cgraph_edge *e;
2241 TREE_NOTHROW (node->decl) = data != NULL;
2243 if (data != NULL)
2244 for (e = node->callers; e; e = e->next_caller)
2245 e->can_throw_external = false;
2246 return false;
2249 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2250 if any to NOTHROW. */
2252 void
2253 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2255 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2256 (void *)(size_t)nothrow, false);
2259 /* Worker to set const flag. */
2261 static bool
2262 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2264 /* Static constructors and destructors without a side effect can be
2265 optimized out. */
2266 if (data && !((size_t)data & 2))
2268 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2269 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2270 if (DECL_STATIC_DESTRUCTOR (node->decl))
2271 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2273 TREE_READONLY (node->decl) = data != NULL;
2274 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2275 return false;
2278 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2279 if any to READONLY. */
2281 void
2282 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2284 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2285 (void *)(size_t)(readonly + (int)looping * 2),
2286 false);
2289 /* Worker to set pure flag. */
2291 static bool
2292 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2294 /* Static constructors and destructors without a side effect can be
2295 optimized out. */
2296 if (data && !((size_t)data & 2))
2298 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2299 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2300 if (DECL_STATIC_DESTRUCTOR (node->decl))
2301 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2303 DECL_PURE_P (node->decl) = data != NULL;
2304 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2305 return false;
2308 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2309 if any to PURE. */
2311 void
2312 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2314 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2315 (void *)(size_t)(pure + (int)looping * 2),
2316 false);
2319 /* Return true when NODE can not return or throw and thus
2320 it is safe to ignore its side effects for IPA analysis. */
2322 bool
2323 cgraph_node_cannot_return (struct cgraph_node *node)
2325 int flags = flags_from_decl_or_type (node->decl);
2326 if (!flag_exceptions)
2327 return (flags & ECF_NORETURN) != 0;
2328 else
2329 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2330 == (ECF_NORETURN | ECF_NOTHROW));
2333 /* Return true when call of E can not lead to return from caller
2334 and thus it is safe to ignore its side effects for IPA analysis
2335 when computing side effects of the caller.
2336 FIXME: We could actually mark all edges that have no reaching
2337 patch to the exit block or throw to get better results. */
2338 bool
2339 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2341 if (cgraph_node_cannot_return (e->caller))
2342 return true;
2343 if (e->indirect_unknown_callee)
2345 int flags = e->indirect_info->ecf_flags;
2346 if (!flag_exceptions)
2347 return (flags & ECF_NORETURN) != 0;
2348 else
2349 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2350 == (ECF_NORETURN | ECF_NOTHROW));
2352 else
2353 return cgraph_node_cannot_return (e->callee);
2356 /* Return true when function NODE can be removed from callgraph
2357 if all direct calls are eliminated. */
2359 bool
2360 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2362 gcc_assert (!node->global.inlined_to);
2363 /* Extern inlines can always go, we will use the external definition. */
2364 if (DECL_EXTERNAL (node->decl))
2365 return true;
2366 /* When function is needed, we can not remove it. */
2367 if (node->force_output || node->used_from_other_partition)
2368 return false;
2369 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2370 || DECL_STATIC_DESTRUCTOR (node->decl))
2371 return false;
2372 /* Only COMDAT functions can be removed if externally visible. */
2373 if (node->externally_visible
2374 && (!DECL_COMDAT (node->decl)
2375 || node->forced_by_abi
2376 || symtab_used_from_object_file_p (node)))
2377 return false;
2378 return true;
2381 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2383 static bool
2384 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2386 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2389 /* Return true when function NODE and its aliases can be removed from callgraph
2390 if all direct calls are eliminated. */
2392 bool
2393 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2395 /* Extern inlines can always go, we will use the external definition. */
2396 if (DECL_EXTERNAL (node->decl))
2397 return true;
2398 if (node->address_taken)
2399 return false;
2400 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2403 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2405 static bool
2406 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2408 return symtab_used_from_object_file_p (node);
2411 /* Return true when function NODE can be expected to be removed
2412 from program when direct calls in this compilation unit are removed.
2414 As a special case COMDAT functions are
2415 cgraph_can_remove_if_no_direct_calls_p while the are not
2416 cgraph_only_called_directly_p (it is possible they are called from other
2417 unit)
2419 This function behaves as cgraph_only_called_directly_p because eliminating
2420 all uses of COMDAT function does not make it necessarily disappear from
2421 the program unless we are compiling whole program or we do LTO. In this
2422 case we know we win since dynamic linking will not really discard the
2423 linkonce section. */
2425 bool
2426 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2428 gcc_assert (!node->global.inlined_to);
2429 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2430 return false;
2431 if (!in_lto_p && !flag_whole_program)
2432 return cgraph_only_called_directly_p (node);
2433 else
2435 if (DECL_EXTERNAL (node->decl))
2436 return true;
2437 return cgraph_can_remove_if_no_direct_calls_p (node);
2442 /* Worker for cgraph_only_called_directly_p. */
2444 static bool
2445 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2447 return !cgraph_only_called_directly_or_aliased_p (node);
2450 /* Return true when function NODE and all its aliases are only called
2451 directly.
2452 i.e. it is not externally visible, address was not taken and
2453 it is not used in any other non-standard way. */
2455 bool
2456 cgraph_only_called_directly_p (struct cgraph_node *node)
2458 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2459 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2460 NULL, true);
2464 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2466 static bool
2467 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2469 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2470 struct cgraph_edge *cs;
2471 enum availability avail;
2472 cgraph_function_or_thunk_node (node, &avail);
2474 if (avail > AVAIL_OVERWRITABLE)
2475 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2476 if (!cs->indirect_inlining_edge)
2477 redirect_callers->safe_push (cs);
2478 return false;
2481 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2482 (i.e. are not overwritable). */
2484 vec<cgraph_edge_p>
2485 collect_callers_of_node (struct cgraph_node *node)
2487 vec<cgraph_edge_p> redirect_callers = vNULL;
2488 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2489 &redirect_callers, false);
2490 return redirect_callers;
2493 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2494 static bool
2495 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2497 node = cgraph_function_or_thunk_node (node, NULL);
2498 node2 = cgraph_function_or_thunk_node (node2, NULL);
2499 while (node != node2 && node2)
2500 node2 = node2->clone_of;
2501 return node2 != NULL;
2504 /* Verify edge E count and frequency. */
2506 static bool
2507 verify_edge_count_and_frequency (struct cgraph_edge *e)
2509 bool error_found = false;
2510 if (e->count < 0)
2512 error ("caller edge count is negative");
2513 error_found = true;
2515 if (e->frequency < 0)
2517 error ("caller edge frequency is negative");
2518 error_found = true;
2520 if (e->frequency > CGRAPH_FREQ_MAX)
2522 error ("caller edge frequency is too large");
2523 error_found = true;
2525 if (gimple_has_body_p (e->caller->decl)
2526 && !e->caller->global.inlined_to
2527 && !e->speculative
2528 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2529 Remove this once edges are actually removed from the function at that time. */
2530 && (e->frequency
2531 || (inline_edge_summary_vec.exists ()
2532 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2533 || !inline_edge_summary (e)->predicate)))
2534 && (e->frequency
2535 != compute_call_stmt_bb_frequency (e->caller->decl,
2536 gimple_bb (e->call_stmt))))
2538 error ("caller edge frequency %i does not match BB frequency %i",
2539 e->frequency,
2540 compute_call_stmt_bb_frequency (e->caller->decl,
2541 gimple_bb (e->call_stmt)));
2542 error_found = true;
2544 return error_found;
2547 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2548 static void
2549 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2551 bool fndecl_was_null = false;
2552 /* debug_gimple_stmt needs correct cfun */
2553 if (cfun != this_cfun)
2554 set_cfun (this_cfun);
2555 /* ...and an actual current_function_decl */
2556 if (!current_function_decl)
2558 current_function_decl = this_cfun->decl;
2559 fndecl_was_null = true;
2561 debug_gimple_stmt (stmt);
2562 if (fndecl_was_null)
2563 current_function_decl = NULL;
2566 /* Verify that call graph edge E corresponds to DECL from the associated
2567 statement. Return true if the verification should fail. */
2569 static bool
2570 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2572 struct cgraph_node *node;
2574 if (!decl || e->callee->global.inlined_to)
2575 return false;
2576 if (cgraph_state == CGRAPH_LTO_STREAMING)
2577 return false;
2578 node = cgraph_get_node (decl);
2580 /* We do not know if a node from a different partition is an alias or what it
2581 aliases and therefore cannot do the former_clone_of check reliably. */
2582 if (!node || node->in_other_partition || e->callee->in_other_partition)
2583 return false;
2584 node = cgraph_function_or_thunk_node (node, NULL);
2586 if (e->callee->former_clone_of != node->decl
2587 /* IPA-CP sometimes redirect edge to clone and then back to the former
2588 function. This ping-pong has to go, eventually. */
2589 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2590 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2591 return true;
2592 else
2593 return false;
2596 /* Verify cgraph nodes of given cgraph node. */
2597 DEBUG_FUNCTION void
2598 verify_cgraph_node (struct cgraph_node *node)
2600 struct cgraph_edge *e;
2601 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
2602 basic_block this_block;
2603 gimple_stmt_iterator gsi;
2604 bool error_found = false;
2606 if (seen_error ())
2607 return;
2609 timevar_push (TV_CGRAPH_VERIFY);
2610 error_found |= verify_symtab_base (node);
2611 for (e = node->callees; e; e = e->next_callee)
2612 if (e->aux)
2614 error ("aux field set for edge %s->%s",
2615 identifier_to_locale (e->caller->name ()),
2616 identifier_to_locale (e->callee->name ()));
2617 error_found = true;
2619 if (node->count < 0)
2621 error ("execution count is negative");
2622 error_found = true;
2624 if (node->global.inlined_to && node->same_comdat_group)
2626 error ("inline clone in same comdat group list");
2627 error_found = true;
2629 if (!node->definition && !node->in_other_partition && node->local.local)
2631 error ("local symbols must be defined");
2632 error_found = true;
2634 if (node->global.inlined_to && node->externally_visible)
2636 error ("externally visible inline clone");
2637 error_found = true;
2639 if (node->global.inlined_to && node->address_taken)
2641 error ("inline clone with address taken");
2642 error_found = true;
2644 if (node->global.inlined_to && node->force_output)
2646 error ("inline clone is forced to output");
2647 error_found = true;
2649 for (e = node->indirect_calls; e; e = e->next_callee)
2651 if (e->aux)
2653 error ("aux field set for indirect edge from %s",
2654 identifier_to_locale (e->caller->name ()));
2655 error_found = true;
2657 if (!e->indirect_unknown_callee
2658 || !e->indirect_info)
2660 error ("An indirect edge from %s is not marked as indirect or has "
2661 "associated indirect_info, the corresponding statement is: ",
2662 identifier_to_locale (e->caller->name ()));
2663 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2664 error_found = true;
2667 for (e = node->callers; e; e = e->next_caller)
2669 if (verify_edge_count_and_frequency (e))
2670 error_found = true;
2671 if (!e->inline_failed)
2673 if (node->global.inlined_to
2674 != (e->caller->global.inlined_to
2675 ? e->caller->global.inlined_to : e->caller))
2677 error ("inlined_to pointer is wrong");
2678 error_found = true;
2680 if (node->callers->next_caller)
2682 error ("multiple inline callers");
2683 error_found = true;
2686 else
2687 if (node->global.inlined_to)
2689 error ("inlined_to pointer set for noninline callers");
2690 error_found = true;
2693 for (e = node->indirect_calls; e; e = e->next_callee)
2694 if (verify_edge_count_and_frequency (e))
2695 error_found = true;
2696 if (!node->callers && node->global.inlined_to)
2698 error ("inlined_to pointer is set but no predecessors found");
2699 error_found = true;
2701 if (node->global.inlined_to == node)
2703 error ("inlined_to pointer refers to itself");
2704 error_found = true;
2707 if (node->clone_of)
2709 struct cgraph_node *n;
2710 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2711 if (n == node)
2712 break;
2713 if (!n)
2715 error ("node has wrong clone_of");
2716 error_found = true;
2719 if (node->clones)
2721 struct cgraph_node *n;
2722 for (n = node->clones; n; n = n->next_sibling_clone)
2723 if (n->clone_of != node)
2724 break;
2725 if (n)
2727 error ("node has wrong clone list");
2728 error_found = true;
2731 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2733 error ("node is in clone list but it is not clone");
2734 error_found = true;
2736 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2738 error ("node has wrong prev_clone pointer");
2739 error_found = true;
2741 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2743 error ("double linked list of clones corrupted");
2744 error_found = true;
2747 if (node->analyzed && node->alias)
2749 bool ref_found = false;
2750 int i;
2751 struct ipa_ref *ref;
2753 if (node->callees)
2755 error ("Alias has call edges");
2756 error_found = true;
2758 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
2759 i, ref); i++)
2760 if (ref->use != IPA_REF_ALIAS)
2762 error ("Alias has non-alias reference");
2763 error_found = true;
2765 else if (ref_found)
2767 error ("Alias has more than one alias reference");
2768 error_found = true;
2770 else
2771 ref_found = true;
2772 if (!ref_found)
2774 error ("Analyzed alias has no reference");
2775 error_found = true;
2778 if (node->analyzed && node->thunk.thunk_p)
2780 if (!node->callees)
2782 error ("No edge out of thunk node");
2783 error_found = true;
2785 else if (node->callees->next_callee)
2787 error ("More than one edge out of thunk node");
2788 error_found = true;
2790 if (gimple_has_body_p (node->decl))
2792 error ("Thunk is not supposed to have body");
2793 error_found = true;
2796 else if (node->analyzed && gimple_has_body_p (node->decl)
2797 && !TREE_ASM_WRITTEN (node->decl)
2798 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
2799 && !flag_wpa)
2801 if (this_cfun->cfg)
2803 pointer_set_t *stmts = pointer_set_create ();
2804 int i;
2805 struct ipa_ref *ref;
2807 /* Reach the trees by walking over the CFG, and note the
2808 enclosing basic-blocks in the call edges. */
2809 FOR_EACH_BB_FN (this_block, this_cfun)
2811 for (gsi = gsi_start_phis (this_block);
2812 !gsi_end_p (gsi); gsi_next (&gsi))
2813 pointer_set_insert (stmts, gsi_stmt (gsi));
2814 for (gsi = gsi_start_bb (this_block);
2815 !gsi_end_p (gsi);
2816 gsi_next (&gsi))
2818 gimple stmt = gsi_stmt (gsi);
2819 pointer_set_insert (stmts, stmt);
2820 if (is_gimple_call (stmt))
2822 struct cgraph_edge *e = cgraph_edge (node, stmt);
2823 tree decl = gimple_call_fndecl (stmt);
2824 if (e)
2826 if (e->aux)
2828 error ("shared call_stmt:");
2829 cgraph_debug_gimple_stmt (this_cfun, stmt);
2830 error_found = true;
2832 if (!e->indirect_unknown_callee)
2834 if (verify_edge_corresponds_to_fndecl (e, decl))
2836 error ("edge points to wrong declaration:");
2837 debug_tree (e->callee->decl);
2838 fprintf (stderr," Instead of:");
2839 debug_tree (decl);
2840 error_found = true;
2843 else if (decl)
2845 error ("an indirect edge with unknown callee "
2846 "corresponding to a call_stmt with "
2847 "a known declaration:");
2848 error_found = true;
2849 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2851 e->aux = (void *)1;
2853 else if (decl)
2855 error ("missing callgraph edge for call stmt:");
2856 cgraph_debug_gimple_stmt (this_cfun, stmt);
2857 error_found = true;
2862 for (i = 0;
2863 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
2864 i++)
2865 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2867 error ("reference to dead statement");
2868 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2869 error_found = true;
2871 pointer_set_destroy (stmts);
2873 else
2874 /* No CFG available?! */
2875 gcc_unreachable ();
2877 for (e = node->callees; e; e = e->next_callee)
2879 if (!e->aux)
2881 error ("edge %s->%s has no corresponding call_stmt",
2882 identifier_to_locale (e->caller->name ()),
2883 identifier_to_locale (e->callee->name ()));
2884 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2885 error_found = true;
2887 e->aux = 0;
2889 for (e = node->indirect_calls; e; e = e->next_callee)
2891 if (!e->aux && !e->speculative)
2893 error ("an indirect edge from %s has no corresponding call_stmt",
2894 identifier_to_locale (e->caller->name ()));
2895 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2896 error_found = true;
2898 e->aux = 0;
2901 if (error_found)
2903 dump_cgraph_node (stderr, node);
2904 internal_error ("verify_cgraph_node failed");
2906 timevar_pop (TV_CGRAPH_VERIFY);
2909 /* Verify whole cgraph structure. */
2910 DEBUG_FUNCTION void
2911 verify_cgraph (void)
2913 struct cgraph_node *node;
2915 if (seen_error ())
2916 return;
2918 FOR_EACH_FUNCTION (node)
2919 verify_cgraph_node (node);
2922 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2923 Walk through thunk, too.
2924 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2926 struct cgraph_node *
2927 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2931 node = cgraph_function_or_thunk_node (node, availability);
2932 if (node->thunk.thunk_p)
2934 node = node->callees->callee;
2935 if (availability)
2937 enum availability a;
2938 a = cgraph_function_body_availability (node);
2939 if (a < *availability)
2940 *availability = a;
2942 node = cgraph_function_or_thunk_node (node, availability);
2944 } while (node && node->thunk.thunk_p);
2945 return node;
2948 /* When doing LTO, read NODE's body from disk if it is not already present. */
2950 bool
2951 cgraph_get_body (struct cgraph_node *node)
2953 struct lto_file_decl_data *file_data;
2954 const char *data, *name;
2955 size_t len;
2956 tree decl = node->decl;
2958 if (DECL_RESULT (decl))
2959 return false;
2961 gcc_assert (in_lto_p);
2963 file_data = node->lto_file_data;
2964 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2966 /* We may have renamed the declaration, e.g., a static function. */
2967 name = lto_get_decl_name_mapping (file_data, name);
2969 data = lto_get_section_data (file_data, LTO_section_function_body,
2970 name, &len);
2971 if (!data)
2973 dump_cgraph_node (stderr, node);
2974 fatal_error ("%s: section %s is missing",
2975 file_data->file_name,
2976 name);
2979 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2981 lto_input_function_body (file_data, node, data);
2982 lto_stats.num_function_bodies++;
2983 lto_free_section_data (file_data, LTO_section_function_body, name,
2984 data, len);
2985 lto_free_function_in_decl_state_for_node (node);
2986 return true;
2989 /* Verify if the type of the argument matches that of the function
2990 declaration. If we cannot verify this or there is a mismatch,
2991 return false. */
2993 static bool
2994 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
2996 tree parms, p;
2997 unsigned int i, nargs;
2999 /* Calls to internal functions always match their signature. */
3000 if (gimple_call_internal_p (stmt))
3001 return true;
3003 nargs = gimple_call_num_args (stmt);
3005 /* Get argument types for verification. */
3006 if (fndecl)
3007 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3008 else
3009 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3011 /* Verify if the type of the argument matches that of the function
3012 declaration. If we cannot verify this or there is a mismatch,
3013 return false. */
3014 if (fndecl && DECL_ARGUMENTS (fndecl))
3016 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3017 i < nargs;
3018 i++, p = DECL_CHAIN (p))
3020 tree arg;
3021 /* We cannot distinguish a varargs function from the case
3022 of excess parameters, still deferring the inlining decision
3023 to the callee is possible. */
3024 if (!p)
3025 break;
3026 arg = gimple_call_arg (stmt, i);
3027 if (p == error_mark_node
3028 || arg == error_mark_node
3029 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3030 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3031 return false;
3033 if (args_count_match && p)
3034 return false;
3036 else if (parms)
3038 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3040 tree arg;
3041 /* If this is a varargs function defer inlining decision
3042 to callee. */
3043 if (!p)
3044 break;
3045 arg = gimple_call_arg (stmt, i);
3046 if (TREE_VALUE (p) == error_mark_node
3047 || arg == error_mark_node
3048 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3049 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3050 && !fold_convertible_p (TREE_VALUE (p), arg)))
3051 return false;
3054 else
3056 if (nargs != 0)
3057 return false;
3059 return true;
3062 /* Verify if the type of the argument and lhs of CALL_STMT matches
3063 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3064 true, the arg count needs to be the same.
3065 If we cannot verify this or there is a mismatch, return false. */
3067 bool
3068 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3069 bool args_count_match)
3071 tree lhs;
3073 if ((DECL_RESULT (callee)
3074 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3075 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3076 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3077 TREE_TYPE (lhs))
3078 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3079 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3080 return false;
3081 return true;
3084 #include "gt-cgraph.h"