Merge trunk version 204659 into gupc branch.
[official-gcc.git] / gcc / cgraph.c
blobf3666fab6dd4c160d17fd694ac18fb1b188ab089
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 "tree-inline.h"
32 #include "langhooks.h"
33 #include "hashtab.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "debug.h"
38 #include "target.h"
39 #include "basic-block.h"
40 #include "cgraph.h"
41 #include "intl.h"
42 #include "gimple.h"
43 #include "timevar.h"
44 #include "dumpfile.h"
45 #include "gimple-ssa.h"
46 #include "cgraph.h"
47 #include "tree-cfg.h"
48 #include "tree-ssa.h"
49 #include "value-prof.h"
50 #include "except.h"
51 #include "diagnostic-core.h"
52 #include "rtl.h"
53 #include "ipa-utils.h"
54 #include "lto-streamer.h"
55 #include "ipa-inline.h"
56 #include "cfgloop.h"
57 #include "gimple-pretty-print.h"
59 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
60 #include "tree-pass.h"
62 static void cgraph_node_remove_callers (struct cgraph_node *node);
63 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
64 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
66 /* Queue of cgraph nodes scheduled to be lowered. */
67 symtab_node *x_cgraph_nodes_queue;
68 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
70 /* Number of nodes in existence. */
71 int cgraph_n_nodes;
73 /* Maximal uid used in cgraph nodes. */
74 int cgraph_max_uid;
76 /* Maximal uid used in cgraph edges. */
77 int cgraph_edge_max_uid;
79 /* Set when whole unit has been analyzed so we can access global info. */
80 bool cgraph_global_info_ready = false;
82 /* What state callgraph is in right now. */
83 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
85 /* Set when the cgraph is fully build and the basic flags are computed. */
86 bool cgraph_function_flags_ready = false;
88 /* List of hooks triggered on cgraph_edge events. */
89 struct cgraph_edge_hook_list {
90 cgraph_edge_hook hook;
91 void *data;
92 struct cgraph_edge_hook_list *next;
95 /* List of hooks triggered on cgraph_node events. */
96 struct cgraph_node_hook_list {
97 cgraph_node_hook hook;
98 void *data;
99 struct cgraph_node_hook_list *next;
102 /* List of hooks triggered on events involving two cgraph_edges. */
103 struct cgraph_2edge_hook_list {
104 cgraph_2edge_hook hook;
105 void *data;
106 struct cgraph_2edge_hook_list *next;
109 /* List of hooks triggered on events involving two cgraph_nodes. */
110 struct cgraph_2node_hook_list {
111 cgraph_2node_hook hook;
112 void *data;
113 struct cgraph_2node_hook_list *next;
116 /* List of hooks triggered when an edge is removed. */
117 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
118 /* List of hooks triggered when a node is removed. */
119 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
120 /* List of hooks triggered when an edge is duplicated. */
121 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
122 /* List of hooks triggered when a node is duplicated. */
123 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
124 /* List of hooks triggered when an function is inserted. */
125 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
127 /* Head of a linked list of unused (freed) call graph nodes.
128 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
129 static GTY(()) struct cgraph_node *free_nodes;
130 /* Head of a linked list of unused (freed) call graph edges.
131 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
132 static GTY(()) struct cgraph_edge *free_edges;
134 /* Did procss_same_body_aliases run? */
135 bool cpp_implicit_aliases_done;
137 /* Map a cgraph_node to cgraph_function_version_info using this htab.
138 The cgraph_function_version_info has a THIS_NODE field that is the
139 corresponding cgraph_node.. */
141 static GTY((param_is (struct cgraph_function_version_info))) htab_t
142 cgraph_fnver_htab = NULL;
144 /* Hash function for cgraph_fnver_htab. */
145 static hashval_t
146 cgraph_fnver_htab_hash (const void *ptr)
148 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
149 return (hashval_t)(uid);
152 /* eq function for cgraph_fnver_htab. */
153 static int
154 cgraph_fnver_htab_eq (const void *p1, const void *p2)
156 const struct cgraph_function_version_info *n1
157 = (const struct cgraph_function_version_info *)p1;
158 const struct cgraph_function_version_info *n2
159 = (const struct cgraph_function_version_info *)p2;
161 return n1->this_node->uid == n2->this_node->uid;
164 /* Mark as GC root all allocated nodes. */
165 static GTY(()) struct cgraph_function_version_info *
166 version_info_node = NULL;
168 /* Get the cgraph_function_version_info node corresponding to node. */
169 struct cgraph_function_version_info *
170 get_cgraph_node_version (struct cgraph_node *node)
172 struct cgraph_function_version_info *ret;
173 struct cgraph_function_version_info key;
174 key.this_node = node;
176 if (cgraph_fnver_htab == NULL)
177 return NULL;
179 ret = (struct cgraph_function_version_info *)
180 htab_find (cgraph_fnver_htab, &key);
182 return ret;
185 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
186 corresponding to cgraph_node NODE. */
187 struct cgraph_function_version_info *
188 insert_new_cgraph_node_version (struct cgraph_node *node)
190 void **slot;
192 version_info_node = NULL;
193 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
194 version_info_node->this_node = node;
196 if (cgraph_fnver_htab == NULL)
197 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
198 cgraph_fnver_htab_eq, NULL);
200 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
201 gcc_assert (slot != NULL);
202 *slot = version_info_node;
203 return version_info_node;
206 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
207 DECL is a duplicate declaration. */
208 void
209 delete_function_version (tree decl)
211 struct cgraph_node *decl_node = cgraph_get_node (decl);
212 struct cgraph_function_version_info *decl_v = NULL;
214 if (decl_node == NULL)
215 return;
217 decl_v = get_cgraph_node_version (decl_node);
219 if (decl_v == NULL)
220 return;
222 if (decl_v->prev != NULL)
223 decl_v->prev->next = decl_v->next;
225 if (decl_v->next != NULL)
226 decl_v->next->prev = decl_v->prev;
228 if (cgraph_fnver_htab != NULL)
229 htab_remove_elt (cgraph_fnver_htab, decl_v);
231 cgraph_remove_node (decl_node);
234 /* Record that DECL1 and DECL2 are semantically identical function
235 versions. */
236 void
237 record_function_versions (tree decl1, tree decl2)
239 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
240 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
241 struct cgraph_function_version_info *decl1_v = NULL;
242 struct cgraph_function_version_info *decl2_v = NULL;
243 struct cgraph_function_version_info *before;
244 struct cgraph_function_version_info *after;
246 gcc_assert (decl1_node != NULL && decl2_node != NULL);
247 decl1_v = get_cgraph_node_version (decl1_node);
248 decl2_v = get_cgraph_node_version (decl2_node);
250 if (decl1_v != NULL && decl2_v != NULL)
251 return;
253 if (decl1_v == NULL)
254 decl1_v = insert_new_cgraph_node_version (decl1_node);
256 if (decl2_v == NULL)
257 decl2_v = insert_new_cgraph_node_version (decl2_node);
259 /* Chain decl2_v and decl1_v. All semantically identical versions
260 will be chained together. */
262 before = decl1_v;
263 after = decl2_v;
265 while (before->next != NULL)
266 before = before->next;
268 while (after->prev != NULL)
269 after= after->prev;
271 before->next = after;
272 after->prev = before;
275 /* Macros to access the next item in the list of free cgraph nodes and
276 edges. */
277 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->next)
278 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
279 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
281 /* Register HOOK to be called with DATA on each removed edge. */
282 struct cgraph_edge_hook_list *
283 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
285 struct cgraph_edge_hook_list *entry;
286 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
288 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
289 entry->hook = hook;
290 entry->data = data;
291 entry->next = NULL;
292 while (*ptr)
293 ptr = &(*ptr)->next;
294 *ptr = entry;
295 return entry;
298 /* Remove ENTRY from the list of hooks called on removing edges. */
299 void
300 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
302 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
304 while (*ptr != entry)
305 ptr = &(*ptr)->next;
306 *ptr = entry->next;
307 free (entry);
310 /* Call all edge removal hooks. */
311 static void
312 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
314 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
315 while (entry)
317 entry->hook (e, entry->data);
318 entry = entry->next;
322 /* Register HOOK to be called with DATA on each removed node. */
323 struct cgraph_node_hook_list *
324 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
326 struct cgraph_node_hook_list *entry;
327 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
329 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
330 entry->hook = hook;
331 entry->data = data;
332 entry->next = NULL;
333 while (*ptr)
334 ptr = &(*ptr)->next;
335 *ptr = entry;
336 return entry;
339 /* Remove ENTRY from the list of hooks called on removing nodes. */
340 void
341 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
343 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
345 while (*ptr != entry)
346 ptr = &(*ptr)->next;
347 *ptr = entry->next;
348 free (entry);
351 /* Call all node removal hooks. */
352 static void
353 cgraph_call_node_removal_hooks (struct cgraph_node *node)
355 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
356 while (entry)
358 entry->hook (node, entry->data);
359 entry = entry->next;
363 /* Register HOOK to be called with DATA on each inserted node. */
364 struct cgraph_node_hook_list *
365 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
367 struct cgraph_node_hook_list *entry;
368 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
370 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
371 entry->hook = hook;
372 entry->data = data;
373 entry->next = NULL;
374 while (*ptr)
375 ptr = &(*ptr)->next;
376 *ptr = entry;
377 return entry;
380 /* Remove ENTRY from the list of hooks called on inserted nodes. */
381 void
382 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
384 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
386 while (*ptr != entry)
387 ptr = &(*ptr)->next;
388 *ptr = entry->next;
389 free (entry);
392 /* Call all node insertion hooks. */
393 void
394 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
396 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
397 while (entry)
399 entry->hook (node, entry->data);
400 entry = entry->next;
404 /* Register HOOK to be called with DATA on each duplicated edge. */
405 struct cgraph_2edge_hook_list *
406 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
408 struct cgraph_2edge_hook_list *entry;
409 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
411 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
412 entry->hook = hook;
413 entry->data = data;
414 entry->next = NULL;
415 while (*ptr)
416 ptr = &(*ptr)->next;
417 *ptr = entry;
418 return entry;
421 /* Remove ENTRY from the list of hooks called on duplicating edges. */
422 void
423 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
425 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
427 while (*ptr != entry)
428 ptr = &(*ptr)->next;
429 *ptr = entry->next;
430 free (entry);
433 /* Call all edge duplication hooks. */
434 void
435 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
436 struct cgraph_edge *cs2)
438 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
439 while (entry)
441 entry->hook (cs1, cs2, entry->data);
442 entry = entry->next;
446 /* Register HOOK to be called with DATA on each duplicated node. */
447 struct cgraph_2node_hook_list *
448 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
450 struct cgraph_2node_hook_list *entry;
451 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
453 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
454 entry->hook = hook;
455 entry->data = data;
456 entry->next = NULL;
457 while (*ptr)
458 ptr = &(*ptr)->next;
459 *ptr = entry;
460 return entry;
463 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
464 void
465 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
467 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
469 while (*ptr != entry)
470 ptr = &(*ptr)->next;
471 *ptr = entry->next;
472 free (entry);
475 /* Call all node duplication hooks. */
476 void
477 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
478 struct cgraph_node *node2)
480 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
481 while (entry)
483 entry->hook (node1, node2, entry->data);
484 entry = entry->next;
488 /* Allocate new callgraph node. */
490 static inline struct cgraph_node *
491 cgraph_allocate_node (void)
493 struct cgraph_node *node;
495 if (free_nodes)
497 node = free_nodes;
498 free_nodes = NEXT_FREE_NODE (node);
500 else
502 node = ggc_alloc_cleared_cgraph_node ();
503 node->uid = cgraph_max_uid++;
506 return node;
509 /* Allocate new callgraph node and insert it into basic data structures. */
511 struct cgraph_node *
512 cgraph_create_empty_node (void)
514 struct cgraph_node *node = cgraph_allocate_node ();
516 node->type = SYMTAB_FUNCTION;
517 node->frequency = NODE_FREQUENCY_NORMAL;
518 node->count_materialization_scale = REG_BR_PROB_BASE;
519 cgraph_n_nodes++;
520 return node;
523 /* Return cgraph node assigned to DECL. Create new one when needed. */
525 struct cgraph_node *
526 cgraph_create_node (tree decl)
528 struct cgraph_node *node = cgraph_create_empty_node ();
529 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
531 node->decl = decl;
532 symtab_register_node (node);
534 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
536 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
537 node->next_nested = node->origin->nested;
538 node->origin->nested = node;
540 return node;
543 /* Try to find a call graph node for declaration DECL and if it does not exist,
544 create it. */
546 struct cgraph_node *
547 cgraph_get_create_node (tree decl)
549 struct cgraph_node *node;
551 node = cgraph_get_node (decl);
552 if (node)
553 return node;
555 return cgraph_create_node (decl);
558 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
559 the function body is associated with (not necessarily cgraph_node (DECL). */
561 struct cgraph_node *
562 cgraph_create_function_alias (tree alias, tree target)
564 struct cgraph_node *alias_node;
566 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
567 || TREE_CODE (target) == IDENTIFIER_NODE);
568 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
569 alias_node = cgraph_get_create_node (alias);
570 gcc_assert (!alias_node->definition);
571 alias_node->alias_target = target;
572 alias_node->definition = true;
573 alias_node->alias = true;
574 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
575 alias_node->weakref = true;
576 return alias_node;
579 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
580 and NULL otherwise.
581 Same body aliases are output whenever the body of DECL is output,
582 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
584 struct cgraph_node *
585 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
587 struct cgraph_node *n;
588 #ifndef ASM_OUTPUT_DEF
589 /* If aliases aren't supported by the assembler, fail. */
590 return NULL;
591 #endif
592 /* Langhooks can create same body aliases of symbols not defined.
593 Those are useless. Drop them on the floor. */
594 if (cgraph_global_info_ready)
595 return NULL;
597 n = cgraph_create_function_alias (alias, decl);
598 n->cpp_implicit_alias = true;
599 if (cpp_implicit_aliases_done)
600 symtab_resolve_alias (n,
601 cgraph_get_node (decl));
602 return n;
605 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
606 aliases DECL with an adjustments made into the first parameter.
607 See comments in thunk_adjust for detail on the parameters. */
609 struct cgraph_node *
610 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
611 tree alias, tree decl ATTRIBUTE_UNUSED,
612 bool this_adjusting,
613 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
614 tree virtual_offset,
615 tree real_alias)
617 struct cgraph_node *node;
619 node = cgraph_get_node (alias);
620 if (node)
622 gcc_assert (node->definition);
623 gcc_assert (!node->alias);
624 gcc_assert (!node->thunk.thunk_p);
625 cgraph_remove_node (node);
628 node = cgraph_create_node (alias);
629 gcc_checking_assert (!virtual_offset
630 || tree_to_double_int (virtual_offset) ==
631 double_int::from_shwi (virtual_value));
632 node->thunk.fixed_offset = fixed_offset;
633 node->thunk.this_adjusting = this_adjusting;
634 node->thunk.virtual_value = virtual_value;
635 node->thunk.virtual_offset_p = virtual_offset != NULL;
636 node->thunk.alias = real_alias;
637 node->thunk.thunk_p = true;
638 node->definition = true;
640 return node;
643 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
644 Return NULL if there's no such node. */
646 struct cgraph_node *
647 cgraph_node_for_asm (tree asmname)
649 /* We do not want to look at inline clones. */
650 for (symtab_node *node = symtab_node_for_asm (asmname);
651 node;
652 node = node->next_sharing_asm_name)
654 cgraph_node *cn = dyn_cast <cgraph_node> (node);
655 if (cn && !cn->global.inlined_to)
656 return cn;
658 return NULL;
661 /* Returns a hash value for X (which really is a cgraph_edge). */
663 static hashval_t
664 edge_hash (const void *x)
666 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
669 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
671 static int
672 edge_eq (const void *x, const void *y)
674 return ((const struct cgraph_edge *) x)->call_stmt == y;
677 /* Add call graph edge E to call site hash of its caller. */
679 static inline void
680 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
682 void **slot;
683 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
684 e->call_stmt,
685 htab_hash_pointer (e->call_stmt),
686 INSERT);
687 *slot = e;
690 /* Add call graph edge E to call site hash of its caller. */
692 static inline void
693 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
695 void **slot;
696 /* There are two speculative edges for every statement (one direct,
697 one indirect); always hash the direct one. */
698 if (e->speculative && e->indirect_unknown_callee)
699 return;
700 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
701 e->call_stmt,
702 htab_hash_pointer (e->call_stmt),
703 INSERT);
704 if (*slot)
706 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
707 if (e->callee)
708 *slot = e;
709 return;
711 gcc_assert (!*slot || e->speculative);
712 *slot = e;
715 /* Return the callgraph edge representing the GIMPLE_CALL statement
716 CALL_STMT. */
718 struct cgraph_edge *
719 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
721 struct cgraph_edge *e, *e2;
722 int n = 0;
724 if (node->call_site_hash)
725 return (struct cgraph_edge *)
726 htab_find_with_hash (node->call_site_hash, call_stmt,
727 htab_hash_pointer (call_stmt));
729 /* This loop may turn out to be performance problem. In such case adding
730 hashtables into call nodes with very many edges is probably best
731 solution. It is not good idea to add pointer into CALL_EXPR itself
732 because we want to make possible having multiple cgraph nodes representing
733 different clones of the same body before the body is actually cloned. */
734 for (e = node->callees; e; e = e->next_callee)
736 if (e->call_stmt == call_stmt)
737 break;
738 n++;
741 if (!e)
742 for (e = node->indirect_calls; e; e = e->next_callee)
744 if (e->call_stmt == call_stmt)
745 break;
746 n++;
749 if (n > 100)
751 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
752 for (e2 = node->callees; e2; e2 = e2->next_callee)
753 cgraph_add_edge_to_call_site_hash (e2);
754 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
755 cgraph_add_edge_to_call_site_hash (e2);
758 return e;
762 /* Change field call_stmt of edge E to NEW_STMT.
763 If UPDATE_SPECULATIVE and E is any component of speculative
764 edge, then update all components. */
766 void
767 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
768 bool update_speculative)
770 tree decl;
772 /* Speculative edges has three component, update all of them
773 when asked to. */
774 if (update_speculative && e->speculative)
776 struct cgraph_edge *direct, *indirect;
777 struct ipa_ref *ref;
779 cgraph_speculative_call_info (e, direct, indirect, ref);
780 cgraph_set_call_stmt (direct, new_stmt, false);
781 cgraph_set_call_stmt (indirect, new_stmt, false);
782 ref->stmt = new_stmt;
783 return;
786 /* Only direct speculative edges go to call_site_hash. */
787 if (e->caller->call_site_hash
788 && (!e->speculative || !e->indirect_unknown_callee))
790 htab_remove_elt_with_hash (e->caller->call_site_hash,
791 e->call_stmt,
792 htab_hash_pointer (e->call_stmt));
795 e->call_stmt = new_stmt;
796 if (e->indirect_unknown_callee
797 && (decl = gimple_call_fndecl (new_stmt)))
799 /* Constant propagation (and possibly also inlining?) can turn an
800 indirect call into a direct one. */
801 struct cgraph_node *new_callee = cgraph_get_node (decl);
803 gcc_checking_assert (new_callee);
804 e = cgraph_make_edge_direct (e, new_callee);
807 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
808 e->can_throw_external = stmt_can_throw_external (new_stmt);
809 pop_cfun ();
810 if (e->caller->call_site_hash)
811 cgraph_add_edge_to_call_site_hash (e);
814 /* Allocate a cgraph_edge structure and fill it with data according to the
815 parameters of which only CALLEE can be NULL (when creating an indirect call
816 edge). */
818 static struct cgraph_edge *
819 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
820 gimple call_stmt, gcov_type count, int freq,
821 bool indir_unknown_callee)
823 struct cgraph_edge *edge;
825 /* LTO does not actually have access to the call_stmt since these
826 have not been loaded yet. */
827 if (call_stmt)
829 /* This is a rather expensive check possibly triggering
830 construction of call stmt hashtable. */
831 #ifdef ENABLE_CHECKING
832 struct cgraph_edge *e;
833 gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
834 #endif
836 gcc_assert (is_gimple_call (call_stmt));
839 if (free_edges)
841 edge = free_edges;
842 free_edges = NEXT_FREE_EDGE (edge);
844 else
846 edge = ggc_alloc_cgraph_edge ();
847 edge->uid = cgraph_edge_max_uid++;
850 edge->aux = NULL;
851 edge->caller = caller;
852 edge->callee = callee;
853 edge->prev_caller = NULL;
854 edge->next_caller = NULL;
855 edge->prev_callee = NULL;
856 edge->next_callee = NULL;
857 edge->lto_stmt_uid = 0;
859 edge->count = count;
860 gcc_assert (count >= 0);
861 edge->frequency = freq;
862 gcc_assert (freq >= 0);
863 gcc_assert (freq <= CGRAPH_FREQ_MAX);
865 edge->call_stmt = call_stmt;
866 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
867 edge->can_throw_external
868 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
869 pop_cfun ();
870 if (call_stmt
871 && callee && callee->decl
872 && !gimple_check_call_matching_types (call_stmt, callee->decl,
873 false))
874 edge->call_stmt_cannot_inline_p = true;
875 else
876 edge->call_stmt_cannot_inline_p = false;
878 edge->indirect_info = NULL;
879 edge->indirect_inlining_edge = 0;
880 edge->speculative = false;
881 edge->indirect_unknown_callee = indir_unknown_callee;
882 if (call_stmt && caller->call_site_hash)
883 cgraph_add_edge_to_call_site_hash (edge);
885 return edge;
888 /* Create edge from CALLER to CALLEE in the cgraph. */
890 struct cgraph_edge *
891 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
892 gimple call_stmt, gcov_type count, int freq)
894 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
895 count, freq, false);
897 initialize_inline_failed (edge);
899 edge->next_caller = callee->callers;
900 if (callee->callers)
901 callee->callers->prev_caller = edge;
902 edge->next_callee = caller->callees;
903 if (caller->callees)
904 caller->callees->prev_callee = edge;
905 caller->callees = edge;
906 callee->callers = edge;
908 return edge;
911 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
913 struct cgraph_indirect_call_info *
914 cgraph_allocate_init_indirect_info (void)
916 struct cgraph_indirect_call_info *ii;
918 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
919 ii->param_index = -1;
920 return ii;
923 /* Create an indirect edge with a yet-undetermined callee where the call
924 statement destination is a formal parameter of the caller with index
925 PARAM_INDEX. */
927 struct cgraph_edge *
928 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
929 int ecf_flags,
930 gcov_type count, int freq)
932 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
933 count, freq, true);
934 tree target;
936 initialize_inline_failed (edge);
938 edge->indirect_info = cgraph_allocate_init_indirect_info ();
939 edge->indirect_info->ecf_flags = ecf_flags;
941 /* Record polymorphic call info. */
942 if (call_stmt
943 && (target = gimple_call_fn (call_stmt))
944 && virtual_method_call_p (target))
946 tree type = obj_type_ref_class (target);
949 /* Only record types can have virtual calls. */
950 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
951 edge->indirect_info->param_index = -1;
952 edge->indirect_info->otr_token
953 = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
954 edge->indirect_info->otr_type = type;
955 edge->indirect_info->polymorphic = 1;
958 edge->next_callee = caller->indirect_calls;
959 if (caller->indirect_calls)
960 caller->indirect_calls->prev_callee = edge;
961 caller->indirect_calls = edge;
963 return edge;
966 /* Remove the edge E from the list of the callers of the callee. */
968 static inline void
969 cgraph_edge_remove_callee (struct cgraph_edge *e)
971 gcc_assert (!e->indirect_unknown_callee);
972 if (e->prev_caller)
973 e->prev_caller->next_caller = e->next_caller;
974 if (e->next_caller)
975 e->next_caller->prev_caller = e->prev_caller;
976 if (!e->prev_caller)
977 e->callee->callers = e->next_caller;
980 /* Remove the edge E from the list of the callees of the caller. */
982 static inline void
983 cgraph_edge_remove_caller (struct cgraph_edge *e)
985 if (e->prev_callee)
986 e->prev_callee->next_callee = e->next_callee;
987 if (e->next_callee)
988 e->next_callee->prev_callee = e->prev_callee;
989 if (!e->prev_callee)
991 if (e->indirect_unknown_callee)
992 e->caller->indirect_calls = e->next_callee;
993 else
994 e->caller->callees = e->next_callee;
996 if (e->caller->call_site_hash)
997 htab_remove_elt_with_hash (e->caller->call_site_hash,
998 e->call_stmt,
999 htab_hash_pointer (e->call_stmt));
1002 /* Put the edge onto the free list. */
1004 static void
1005 cgraph_free_edge (struct cgraph_edge *e)
1007 int uid = e->uid;
1009 if (e->indirect_info)
1010 ggc_free (e->indirect_info);
1012 /* Clear out the edge so we do not dangle pointers. */
1013 memset (e, 0, sizeof (*e));
1014 e->uid = uid;
1015 NEXT_FREE_EDGE (e) = free_edges;
1016 free_edges = e;
1019 /* Remove the edge E in the cgraph. */
1021 void
1022 cgraph_remove_edge (struct cgraph_edge *e)
1024 /* Call all edge removal hooks. */
1025 cgraph_call_edge_removal_hooks (e);
1027 if (!e->indirect_unknown_callee)
1028 /* Remove from callers list of the callee. */
1029 cgraph_edge_remove_callee (e);
1031 /* Remove from callees list of the callers. */
1032 cgraph_edge_remove_caller (e);
1034 /* Put the edge onto the free list. */
1035 cgraph_free_edge (e);
1038 /* Set callee of call graph edge E and add it to the corresponding set of
1039 callers. */
1041 static void
1042 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1044 e->prev_caller = NULL;
1045 if (n->callers)
1046 n->callers->prev_caller = e;
1047 e->next_caller = n->callers;
1048 n->callers = e;
1049 e->callee = n;
1052 /* Turn edge E into speculative call calling N2. Update
1053 the profile so the direct call is taken COUNT times
1054 with FREQUENCY.
1056 At clone materialization time, the indirect call E will
1057 be expanded as:
1059 if (call_dest == N2)
1060 n2 ();
1061 else
1062 call call_dest
1064 At this time the function just creates the direct call,
1065 the referencd representing the if conditional and attaches
1066 them all to the orginal indirect call statement.
1068 Return direct edge created. */
1070 struct cgraph_edge *
1071 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1072 struct cgraph_node *n2,
1073 gcov_type direct_count,
1074 int direct_frequency)
1076 struct cgraph_node *n = e->caller;
1077 struct ipa_ref *ref;
1078 struct cgraph_edge *e2;
1080 if (dump_file)
1082 fprintf (dump_file, "Indirect call -> speculative call"
1083 " %s/%i => %s/%i\n",
1084 xstrdup (cgraph_node_name (n)), n->order,
1085 xstrdup (cgraph_node_name (n2)), n2->order);
1087 e->speculative = true;
1088 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1089 initialize_inline_failed (e2);
1090 e2->speculative = true;
1091 if (TREE_NOTHROW (n2->decl))
1092 e2->can_throw_external = false;
1093 else
1094 e2->can_throw_external = e->can_throw_external;
1095 e2->lto_stmt_uid = e->lto_stmt_uid;
1096 e->count -= e2->count;
1097 e->frequency -= e2->frequency;
1098 cgraph_call_edge_duplication_hooks (e, e2);
1099 ref = ipa_record_reference (n, n2,
1100 IPA_REF_ADDR, e->call_stmt);
1101 ref->lto_stmt_uid = e->lto_stmt_uid;
1102 ref->speculative = e->speculative;
1103 cgraph_mark_address_taken_node (n2);
1104 return e2;
1107 /* Speculative call consist of three components:
1108 1) an indirect edge representing the original call
1109 2) an direct edge representing the new call
1110 3) ADDR_EXPR reference representing the speculative check.
1111 All three components are attached to single statement (the indirect
1112 call) and if one of them exists, all of them must exist.
1114 Given speculative call edge E, return all three components.
1117 void
1118 cgraph_speculative_call_info (struct cgraph_edge *e,
1119 struct cgraph_edge *&direct,
1120 struct cgraph_edge *&indirect,
1121 struct ipa_ref *&reference)
1123 struct ipa_ref *ref;
1124 int i;
1125 struct cgraph_edge *e2;
1127 if (!e->indirect_unknown_callee)
1128 for (e2 = e->caller->indirect_calls;
1129 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1130 e2 = e2->next_callee)
1132 else
1134 e2 = e;
1135 /* We can take advantage of the call stmt hash. */
1136 if (e2->call_stmt)
1138 e = cgraph_edge (e->caller, e2->call_stmt);
1139 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1141 else
1142 for (e = e->caller->callees;
1143 e2->call_stmt != e->call_stmt
1144 || e2->lto_stmt_uid != e->lto_stmt_uid;
1145 e = e->next_callee)
1148 gcc_assert (e->speculative && e2->speculative);
1149 direct = e;
1150 indirect = e2;
1152 reference = NULL;
1153 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->ref_list,
1154 i, ref); i++)
1155 if (ref->speculative
1156 && ((ref->stmt && ref->stmt == e->call_stmt)
1157 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1159 reference = ref;
1160 break;
1163 /* Speculative edge always consist of all three components - direct edge,
1164 indirect and reference. */
1166 gcc_assert (e && e2 && ref);
1169 /* Redirect callee of E to N. The function does not update underlying
1170 call expression. */
1172 void
1173 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1175 /* Remove from callers list of the current callee. */
1176 cgraph_edge_remove_callee (e);
1178 /* Insert to callers list of the new callee. */
1179 cgraph_set_edge_callee (e, n);
1182 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1183 Remove the speculative call sequence and return edge representing the call.
1184 It is up to caller to redirect the call as appropriate. */
1186 struct cgraph_edge *
1187 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1189 struct cgraph_edge *e2;
1190 struct ipa_ref *ref;
1192 gcc_assert (edge->speculative);
1193 cgraph_speculative_call_info (edge, e2, edge, ref);
1194 if (!callee_decl
1195 || !symtab_semantically_equivalent_p (ref->referred,
1196 symtab_get_node (callee_decl)))
1198 if (dump_file)
1200 if (callee_decl)
1202 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1203 "turned out to have contradicting known target ",
1204 xstrdup (cgraph_node_name (edge->caller)), edge->caller->order,
1205 xstrdup (cgraph_node_name (e2->callee)), e2->callee->order);
1206 print_generic_expr (dump_file, callee_decl, 0);
1207 fprintf (dump_file, "\n");
1209 else
1211 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1212 xstrdup (cgraph_node_name (edge->caller)), edge->caller->order,
1213 xstrdup (cgraph_node_name (e2->callee)), e2->callee->order);
1217 else
1219 struct cgraph_edge *tmp = edge;
1220 if (dump_file)
1221 fprintf (dump_file, "Speculative call turned into direct call.\n");
1222 edge = e2;
1223 e2 = tmp;
1224 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1225 in the functions inlined through it. */
1227 edge->count += e2->count;
1228 edge->frequency += e2->frequency;
1229 if (edge->frequency > CGRAPH_FREQ_MAX)
1230 edge->frequency = CGRAPH_FREQ_MAX;
1231 edge->speculative = false;
1232 e2->speculative = false;
1233 ipa_remove_reference (ref);
1234 if (e2->indirect_unknown_callee || e2->inline_failed)
1235 cgraph_remove_edge (e2);
1236 else
1237 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1238 if (edge->caller->call_site_hash)
1239 cgraph_update_edge_in_call_site_hash (edge);
1240 return edge;
1243 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1244 CALLEE. DELTA is an integer constant that is to be added to the this
1245 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1247 struct cgraph_edge *
1248 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1250 gcc_assert (edge->indirect_unknown_callee);
1252 /* If we are redirecting speculative call, make it non-speculative. */
1253 if (edge->indirect_unknown_callee && edge->speculative)
1255 edge = cgraph_resolve_speculation (edge, callee->decl);
1257 /* On successful speculation just return the pre existing direct edge. */
1258 if (!edge->indirect_unknown_callee)
1259 return edge;
1262 edge->indirect_unknown_callee = 0;
1263 ggc_free (edge->indirect_info);
1264 edge->indirect_info = NULL;
1266 /* Get the edge out of the indirect edge list. */
1267 if (edge->prev_callee)
1268 edge->prev_callee->next_callee = edge->next_callee;
1269 if (edge->next_callee)
1270 edge->next_callee->prev_callee = edge->prev_callee;
1271 if (!edge->prev_callee)
1272 edge->caller->indirect_calls = edge->next_callee;
1274 /* Put it into the normal callee list */
1275 edge->prev_callee = NULL;
1276 edge->next_callee = edge->caller->callees;
1277 if (edge->caller->callees)
1278 edge->caller->callees->prev_callee = edge;
1279 edge->caller->callees = edge;
1281 /* Insert to callers list of the new callee. */
1282 cgraph_set_edge_callee (edge, callee);
1284 if (edge->call_stmt)
1285 edge->call_stmt_cannot_inline_p
1286 = !gimple_check_call_matching_types (edge->call_stmt, callee->decl,
1287 false);
1289 /* We need to re-determine the inlining status of the edge. */
1290 initialize_inline_failed (edge);
1291 return edge;
1294 /* If necessary, change the function declaration in the call statement
1295 associated with E so that it corresponds to the edge callee. */
1297 gimple
1298 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1300 tree decl = gimple_call_fndecl (e->call_stmt);
1301 gimple new_stmt;
1302 gimple_stmt_iterator gsi;
1303 #ifdef ENABLE_CHECKING
1304 struct cgraph_node *node;
1305 #endif
1307 if (e->speculative)
1309 struct cgraph_edge *e2;
1310 gimple new_stmt;
1311 struct ipa_ref *ref;
1313 cgraph_speculative_call_info (e, e, e2, ref);
1314 /* If there already is an direct call (i.e. as a result of inliner's
1315 substitution), forget about speculating. */
1316 if (decl)
1317 e = cgraph_resolve_speculation (e, decl);
1318 /* If types do not match, speculation was likely wrong.
1319 The direct edge was posisbly redirected to the clone with a different
1320 signature. We did not update the call statement yet, so compare it
1321 with the reference that still points to the proper type. */
1322 else if (!gimple_check_call_matching_types (e->call_stmt,
1323 ref->referred->decl,
1324 true))
1326 if (dump_file)
1327 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1328 "Type mismatch.\n",
1329 xstrdup (cgraph_node_name (e->caller)),
1330 e->caller->order,
1331 xstrdup (cgraph_node_name (e->callee)),
1332 e->callee->order);
1333 e = cgraph_resolve_speculation (e, NULL);
1334 /* We are producing the final function body and will throw away the
1335 callgraph edges really soon. Reset the counts/frequencies to
1336 keep verifier happy in the case of roundoff errors. */
1337 e->count = gimple_bb (e->call_stmt)->count;
1338 e->frequency = compute_call_stmt_bb_frequency
1339 (e->caller->decl, gimple_bb (e->call_stmt));
1341 /* Expand speculation into GIMPLE code. */
1342 else
1344 if (dump_file)
1345 fprintf (dump_file,
1346 "Expanding speculative call of %s/%i -> %s/%i count:"
1347 HOST_WIDEST_INT_PRINT_DEC"\n",
1348 xstrdup (cgraph_node_name (e->caller)),
1349 e->caller->order,
1350 xstrdup (cgraph_node_name (e->callee)),
1351 e->callee->order,
1352 (HOST_WIDEST_INT)e->count);
1353 gcc_assert (e2->speculative);
1354 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1355 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1356 e->count || e2->count
1357 ? RDIV (e->count * REG_BR_PROB_BASE,
1358 e->count + e2->count)
1359 : e->frequency || e2->frequency
1360 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1361 e->frequency + e2->frequency)
1362 : REG_BR_PROB_BASE / 2,
1363 e->count, e->count + e2->count);
1364 e->speculative = false;
1365 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1366 new_stmt, false);
1367 e->frequency = compute_call_stmt_bb_frequency
1368 (e->caller->decl, gimple_bb (e->call_stmt));
1369 e2->frequency = compute_call_stmt_bb_frequency
1370 (e2->caller->decl, gimple_bb (e2->call_stmt));
1371 e2->speculative = false;
1372 ref->speculative = false;
1373 ref->stmt = NULL;
1374 /* Indirect edges are not both in the call site hash.
1375 get it updated. */
1376 if (e->caller->call_site_hash)
1377 cgraph_update_edge_in_call_site_hash (e2);
1378 pop_cfun ();
1379 /* Continue redirecting E to proper target. */
1383 if (e->indirect_unknown_callee
1384 || decl == e->callee->decl)
1385 return e->call_stmt;
1387 #ifdef ENABLE_CHECKING
1388 if (decl)
1390 node = cgraph_get_node (decl);
1391 gcc_assert (!node || !node->clone.combined_args_to_skip);
1393 #endif
1395 if (cgraph_dump_file)
1397 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1398 xstrdup (cgraph_node_name (e->caller)), e->caller->order,
1399 xstrdup (cgraph_node_name (e->callee)), e->callee->order);
1400 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1401 if (e->callee->clone.combined_args_to_skip)
1403 fprintf (cgraph_dump_file, " combined args to skip: ");
1404 dump_bitmap (cgraph_dump_file,
1405 e->callee->clone.combined_args_to_skip);
1409 if (e->callee->clone.combined_args_to_skip)
1411 int lp_nr;
1413 new_stmt
1414 = gimple_call_copy_skip_args (e->call_stmt,
1415 e->callee->clone.combined_args_to_skip);
1416 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1417 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1419 if (gimple_vdef (new_stmt)
1420 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1421 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1423 gsi = gsi_for_stmt (e->call_stmt);
1424 gsi_replace (&gsi, new_stmt, false);
1425 /* We need to defer cleaning EH info on the new statement to
1426 fixup-cfg. We may not have dominator information at this point
1427 and thus would end up with unreachable blocks and have no way
1428 to communicate that we need to run CFG cleanup then. */
1429 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1430 if (lp_nr != 0)
1432 remove_stmt_from_eh_lp (e->call_stmt);
1433 add_stmt_to_eh_lp (new_stmt, lp_nr);
1436 else
1438 new_stmt = e->call_stmt;
1439 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1440 update_stmt (new_stmt);
1443 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1445 if (cgraph_dump_file)
1447 fprintf (cgraph_dump_file, " updated to:");
1448 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1450 return new_stmt;
1453 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1454 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1455 of OLD_STMT if it was previously call statement.
1456 If NEW_STMT is NULL, the call has been dropped without any
1457 replacement. */
1459 static void
1460 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1461 gimple old_stmt, tree old_call,
1462 gimple new_stmt)
1464 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1465 ? gimple_call_fndecl (new_stmt) : 0;
1467 /* We are seeing indirect calls, then there is nothing to update. */
1468 if (!new_call && !old_call)
1469 return;
1470 /* See if we turned indirect call into direct call or folded call to one builtin
1471 into different builtin. */
1472 if (old_call != new_call)
1474 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1475 struct cgraph_edge *ne = NULL;
1476 gcov_type count;
1477 int frequency;
1479 if (e)
1481 /* See if the edge is already there and has the correct callee. It
1482 might be so because of indirect inlining has already updated
1483 it. We also might've cloned and redirected the edge. */
1484 if (new_call && e->callee)
1486 struct cgraph_node *callee = e->callee;
1487 while (callee)
1489 if (callee->decl == new_call
1490 || callee->former_clone_of == new_call)
1491 return;
1492 callee = callee->clone_of;
1496 /* Otherwise remove edge and create new one; we can't simply redirect
1497 since function has changed, so inline plan and other information
1498 attached to edge is invalid. */
1499 count = e->count;
1500 frequency = e->frequency;
1501 cgraph_remove_edge (e);
1503 else if (new_call)
1505 /* We are seeing new direct call; compute profile info based on BB. */
1506 basic_block bb = gimple_bb (new_stmt);
1507 count = bb->count;
1508 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1509 bb);
1512 if (new_call)
1514 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1515 new_stmt, count, frequency);
1516 gcc_assert (ne->inline_failed);
1519 /* We only updated the call stmt; update pointer in cgraph edge.. */
1520 else if (old_stmt != new_stmt)
1521 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1524 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1525 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1526 of OLD_STMT before it was updated (updating can happen inplace). */
1528 void
1529 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1531 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1532 struct cgraph_node *node;
1534 gcc_checking_assert (orig);
1535 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1536 if (orig->clones)
1537 for (node = orig->clones; node != orig;)
1539 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1540 if (node->clones)
1541 node = node->clones;
1542 else if (node->next_sibling_clone)
1543 node = node->next_sibling_clone;
1544 else
1546 while (node != orig && !node->next_sibling_clone)
1547 node = node->clone_of;
1548 if (node != orig)
1549 node = node->next_sibling_clone;
1555 /* Remove all callees from the node. */
1557 void
1558 cgraph_node_remove_callees (struct cgraph_node *node)
1560 struct cgraph_edge *e, *f;
1562 /* It is sufficient to remove the edges from the lists of callers of
1563 the callees. The callee list of the node can be zapped with one
1564 assignment. */
1565 for (e = node->callees; e; e = f)
1567 f = e->next_callee;
1568 cgraph_call_edge_removal_hooks (e);
1569 if (!e->indirect_unknown_callee)
1570 cgraph_edge_remove_callee (e);
1571 cgraph_free_edge (e);
1573 for (e = node->indirect_calls; e; e = f)
1575 f = e->next_callee;
1576 cgraph_call_edge_removal_hooks (e);
1577 if (!e->indirect_unknown_callee)
1578 cgraph_edge_remove_callee (e);
1579 cgraph_free_edge (e);
1581 node->indirect_calls = NULL;
1582 node->callees = NULL;
1583 if (node->call_site_hash)
1585 htab_delete (node->call_site_hash);
1586 node->call_site_hash = NULL;
1590 /* Remove all callers from the node. */
1592 static void
1593 cgraph_node_remove_callers (struct cgraph_node *node)
1595 struct cgraph_edge *e, *f;
1597 /* It is sufficient to remove the edges from the lists of callees of
1598 the callers. The caller list of the node can be zapped with one
1599 assignment. */
1600 for (e = node->callers; e; e = f)
1602 f = e->next_caller;
1603 cgraph_call_edge_removal_hooks (e);
1604 cgraph_edge_remove_caller (e);
1605 cgraph_free_edge (e);
1607 node->callers = NULL;
1610 /* Helper function for cgraph_release_function_body and free_lang_data.
1611 It releases body from function DECL without having to inspect its
1612 possibly non-existent symtab node. */
1614 void
1615 release_function_body (tree decl)
1617 if (DECL_STRUCT_FUNCTION (decl))
1619 push_cfun (DECL_STRUCT_FUNCTION (decl));
1620 if (cfun->cfg
1621 && current_loops)
1623 cfun->curr_properties &= ~PROP_loops;
1624 loop_optimizer_finalize ();
1626 if (cfun->gimple_df)
1628 delete_tree_ssa ();
1629 delete_tree_cfg_annotations ();
1630 cfun->eh = NULL;
1632 if (cfun->cfg)
1634 gcc_assert (dom_computed[0] == DOM_NONE);
1635 gcc_assert (dom_computed[1] == DOM_NONE);
1636 clear_edges ();
1637 cfun->cfg = NULL;
1639 if (cfun->value_histograms)
1640 free_histograms ();
1641 pop_cfun ();
1642 gimple_set_body (decl, NULL);
1643 /* Struct function hangs a lot of data that would leak if we didn't
1644 removed all pointers to it. */
1645 ggc_free (DECL_STRUCT_FUNCTION (decl));
1646 DECL_STRUCT_FUNCTION (decl) = NULL;
1648 DECL_SAVED_TREE (decl) = NULL;
1651 /* Release memory used to represent body of function NODE.
1652 Use this only for functions that are released before being translated to
1653 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1654 are free'd in final.c via free_after_compilation(). */
1656 void
1657 cgraph_release_function_body (struct cgraph_node *node)
1659 node->ipa_transforms_to_apply.release ();
1660 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1662 DECL_RESULT (node->decl) = NULL;
1663 DECL_ARGUMENTS (node->decl) = NULL;
1665 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1666 of its associated function function declaration because it's
1667 needed to emit debug info later. */
1668 if (!node->used_as_abstract_origin && DECL_INITIAL (node->decl))
1669 DECL_INITIAL (node->decl) = error_mark_node;
1670 release_function_body (node->decl);
1671 if (node->lto_file_data)
1672 lto_free_function_in_decl_state_for_node (node);
1675 /* Remove the node from cgraph. */
1677 void
1678 cgraph_remove_node (struct cgraph_node *node)
1680 struct cgraph_node *n;
1681 int uid = node->uid;
1683 cgraph_call_node_removal_hooks (node);
1684 cgraph_node_remove_callers (node);
1685 cgraph_node_remove_callees (node);
1686 node->ipa_transforms_to_apply.release ();
1688 /* Incremental inlining access removed nodes stored in the postorder list.
1690 node->force_output = false;
1691 node->forced_by_abi = false;
1692 for (n = node->nested; n; n = n->next_nested)
1693 n->origin = NULL;
1694 node->nested = NULL;
1695 if (node->origin)
1697 struct cgraph_node **node2 = &node->origin->nested;
1699 while (*node2 != node)
1700 node2 = &(*node2)->next_nested;
1701 *node2 = node->next_nested;
1703 symtab_unregister_node (node);
1704 if (node->prev_sibling_clone)
1705 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1706 else if (node->clone_of)
1707 node->clone_of->clones = node->next_sibling_clone;
1708 if (node->next_sibling_clone)
1709 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1710 if (node->clones)
1712 struct cgraph_node *n, *next;
1714 if (node->clone_of)
1716 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1717 n->clone_of = node->clone_of;
1718 n->clone_of = node->clone_of;
1719 n->next_sibling_clone = node->clone_of->clones;
1720 if (node->clone_of->clones)
1721 node->clone_of->clones->prev_sibling_clone = n;
1722 node->clone_of->clones = node->clones;
1724 else
1726 /* We are removing node with clones. This makes clones inconsistent,
1727 but assume they will be removed subsequently and just keep clone
1728 tree intact. This can happen in unreachable function removal since
1729 we remove unreachable functions in random order, not by bottom-up
1730 walk of clone trees. */
1731 for (n = node->clones; n; n = next)
1733 next = n->next_sibling_clone;
1734 n->next_sibling_clone = NULL;
1735 n->prev_sibling_clone = NULL;
1736 n->clone_of = NULL;
1741 /* While all the clones are removed after being proceeded, the function
1742 itself is kept in the cgraph even after it is compiled. Check whether
1743 we are done with this body and reclaim it proactively if this is the case.
1745 if (cgraph_state != CGRAPH_LTO_STREAMING)
1747 n = cgraph_get_node (node->decl);
1748 if (!n
1749 || (!n->clones && !n->clone_of && !n->global.inlined_to
1750 && (cgraph_global_info_ready
1751 && (TREE_ASM_WRITTEN (n->decl)
1752 || DECL_EXTERNAL (n->decl)
1753 || !n->analyzed
1754 || (!flag_wpa && n->in_other_partition)))))
1755 cgraph_release_function_body (node);
1758 node->decl = NULL;
1759 if (node->call_site_hash)
1761 htab_delete (node->call_site_hash);
1762 node->call_site_hash = NULL;
1764 cgraph_n_nodes--;
1766 /* Clear out the node to NULL all pointers and add the node to the free
1767 list. */
1768 memset (node, 0, sizeof (*node));
1769 node->type = SYMTAB_FUNCTION;
1770 node->uid = uid;
1771 SET_NEXT_FREE_NODE (node, free_nodes);
1772 free_nodes = node;
1775 /* Likewise indicate that a node is having address taken. */
1777 void
1778 cgraph_mark_address_taken_node (struct cgraph_node *node)
1780 /* Indirect inlining can figure out that all uses of the address are
1781 inlined. */
1782 if (node->global.inlined_to)
1784 gcc_assert (cfun->after_inlining);
1785 gcc_assert (node->callers->indirect_inlining_edge);
1786 return;
1788 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1789 IPA_REF_ADDR reference exists (and thus it should be set on node
1790 representing alias we take address of) and as a test whether address
1791 of the object was taken (and thus it should be set on node alias is
1792 referring to). We should remove the first use and the remove the
1793 following set. */
1794 node->address_taken = 1;
1795 node = cgraph_function_or_thunk_node (node, NULL);
1796 node->address_taken = 1;
1799 /* Return local info for the compiled function. */
1801 struct cgraph_local_info *
1802 cgraph_local_info (tree decl)
1804 struct cgraph_node *node;
1806 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1807 node = cgraph_get_node (decl);
1808 if (!node)
1809 return NULL;
1810 return &node->local;
1813 /* Return local info for the compiled function. */
1815 struct cgraph_global_info *
1816 cgraph_global_info (tree decl)
1818 struct cgraph_node *node;
1820 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1821 node = cgraph_get_node (decl);
1822 if (!node)
1823 return NULL;
1824 return &node->global;
1827 /* Return local info for the compiled function. */
1829 struct cgraph_rtl_info *
1830 cgraph_rtl_info (tree decl)
1832 struct cgraph_node *node;
1834 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1835 node = cgraph_get_node (decl);
1836 if (!node
1837 || (decl != current_function_decl
1838 && !TREE_ASM_WRITTEN (node->decl)))
1839 return NULL;
1840 return &node->rtl;
1843 /* Return a string describing the failure REASON. */
1845 const char*
1846 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1848 #undef DEFCIFCODE
1849 #define DEFCIFCODE(code, string) string,
1851 static const char *cif_string_table[CIF_N_REASONS] = {
1852 #include "cif-code.def"
1855 /* Signedness of an enum type is implementation defined, so cast it
1856 to unsigned before testing. */
1857 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1858 return cif_string_table[reason];
1861 /* Names used to print out the availability enum. */
1862 const char * const cgraph_availability_names[] =
1863 {"unset", "not_available", "overwritable", "available", "local"};
1866 /* Dump call graph node NODE to file F. */
1868 void
1869 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1871 struct cgraph_edge *edge;
1872 int indirect_calls_count = 0;
1874 dump_symtab_base (f, node);
1876 if (node->global.inlined_to)
1877 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1878 xstrdup (cgraph_node_name (node)),
1879 node->order,
1880 xstrdup (cgraph_node_name (node->global.inlined_to)),
1881 node->global.inlined_to->order);
1882 if (node->clone_of)
1883 fprintf (f, " Clone of %s/%i\n",
1884 cgraph_node_asm_name (node->clone_of),
1885 node->clone_of->order);
1886 if (cgraph_function_flags_ready)
1887 fprintf (f, " Availability: %s\n",
1888 cgraph_availability_names [cgraph_function_body_availability (node)]);
1890 if (node->profile_id)
1891 fprintf (f, " Profile id: %i\n",
1892 node->profile_id);
1893 fprintf (f, " Function flags:");
1894 if (node->count)
1895 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1896 (HOST_WIDEST_INT)node->count);
1897 if (node->origin)
1898 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1899 if (gimple_has_body_p (node->decl))
1900 fprintf (f, " body");
1901 if (node->process)
1902 fprintf (f, " process");
1903 if (node->local.local)
1904 fprintf (f, " local");
1905 if (node->local.redefined_extern_inline)
1906 fprintf (f, " redefined_extern_inline");
1907 if (node->only_called_at_startup)
1908 fprintf (f, " only_called_at_startup");
1909 if (node->only_called_at_exit)
1910 fprintf (f, " only_called_at_exit");
1911 if (node->tm_clone)
1912 fprintf (f, " tm_clone");
1914 fprintf (f, "\n");
1916 if (node->thunk.thunk_p)
1918 fprintf (f, " Thunk");
1919 if (node->thunk.alias)
1920 fprintf (f, " of %s (asm: %s)",
1921 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1922 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1923 fprintf (f, " fixed offset %i virtual value %i has "
1924 "virtual offset %i)\n",
1925 (int)node->thunk.fixed_offset,
1926 (int)node->thunk.virtual_value,
1927 (int)node->thunk.virtual_offset_p);
1929 if (node->alias && node->thunk.alias
1930 && DECL_P (node->thunk.alias))
1932 fprintf (f, " Alias of %s",
1933 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1934 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1935 fprintf (f, " (asm: %s)",
1936 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1937 fprintf (f, "\n");
1940 fprintf (f, " Called by: ");
1942 for (edge = node->callers; edge; edge = edge->next_caller)
1944 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1945 edge->caller->order);
1946 if (edge->count)
1947 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1948 (HOST_WIDEST_INT)edge->count);
1949 if (edge->frequency)
1950 fprintf (f, "(%.2f per call) ",
1951 edge->frequency / (double)CGRAPH_FREQ_BASE);
1952 if (edge->speculative)
1953 fprintf (f, "(speculative) ");
1954 if (!edge->inline_failed)
1955 fprintf (f, "(inlined) ");
1956 if (edge->indirect_inlining_edge)
1957 fprintf (f, "(indirect_inlining) ");
1958 if (edge->can_throw_external)
1959 fprintf (f, "(can throw external) ");
1962 fprintf (f, "\n Calls: ");
1963 for (edge = node->callees; edge; edge = edge->next_callee)
1965 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1966 edge->callee->order);
1967 if (edge->speculative)
1968 fprintf (f, "(speculative) ");
1969 if (!edge->inline_failed)
1970 fprintf (f, "(inlined) ");
1971 if (edge->indirect_inlining_edge)
1972 fprintf (f, "(indirect_inlining) ");
1973 if (edge->count)
1974 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1975 (HOST_WIDEST_INT)edge->count);
1976 if (edge->frequency)
1977 fprintf (f, "(%.2f per call) ",
1978 edge->frequency / (double)CGRAPH_FREQ_BASE);
1979 if (edge->can_throw_external)
1980 fprintf (f, "(can throw external) ");
1982 fprintf (f, "\n");
1984 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1985 indirect_calls_count++;
1986 if (indirect_calls_count)
1987 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1988 indirect_calls_count);
1992 /* Dump call graph node NODE to stderr. */
1994 DEBUG_FUNCTION void
1995 debug_cgraph_node (struct cgraph_node *node)
1997 dump_cgraph_node (stderr, node);
2001 /* Dump the callgraph to file F. */
2003 void
2004 dump_cgraph (FILE *f)
2006 struct cgraph_node *node;
2008 fprintf (f, "callgraph:\n\n");
2009 FOR_EACH_FUNCTION (node)
2010 dump_cgraph_node (f, node);
2014 /* Dump the call graph to stderr. */
2016 DEBUG_FUNCTION void
2017 debug_cgraph (void)
2019 dump_cgraph (stderr);
2022 /* Return true when the DECL can possibly be inlined. */
2023 bool
2024 cgraph_function_possibly_inlined_p (tree decl)
2026 if (!cgraph_global_info_ready)
2027 return !DECL_UNINLINABLE (decl);
2028 return DECL_POSSIBLY_INLINED (decl);
2031 /* NODE is no longer nested function; update cgraph accordingly. */
2032 void
2033 cgraph_unnest_node (struct cgraph_node *node)
2035 struct cgraph_node **node2 = &node->origin->nested;
2036 gcc_assert (node->origin);
2038 while (*node2 != node)
2039 node2 = &(*node2)->next_nested;
2040 *node2 = node->next_nested;
2041 node->origin = NULL;
2044 /* Return function availability. See cgraph.h for description of individual
2045 return values. */
2046 enum availability
2047 cgraph_function_body_availability (struct cgraph_node *node)
2049 enum availability avail;
2050 if (!node->analyzed)
2051 avail = AVAIL_NOT_AVAILABLE;
2052 else if (node->local.local)
2053 avail = AVAIL_LOCAL;
2054 else if (node->alias && node->weakref)
2055 cgraph_function_or_thunk_node (node, &avail);
2056 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->decl)))
2057 avail = AVAIL_OVERWRITABLE;
2058 else if (!node->externally_visible)
2059 avail = AVAIL_AVAILABLE;
2060 /* Inline functions are safe to be analyzed even if their symbol can
2061 be overwritten at runtime. It is not meaningful to enforce any sane
2062 behaviour on replacing inline function by different body. */
2063 else if (DECL_DECLARED_INLINE_P (node->decl))
2064 avail = AVAIL_AVAILABLE;
2066 /* If the function can be overwritten, return OVERWRITABLE. Take
2067 care at least of two notable extensions - the COMDAT functions
2068 used to share template instantiations in C++ (this is symmetric
2069 to code cp_cannot_inline_tree_fn and probably shall be shared and
2070 the inlinability hooks completely eliminated).
2072 ??? Does the C++ one definition rule allow us to always return
2073 AVAIL_AVAILABLE here? That would be good reason to preserve this
2074 bit. */
2076 else if (decl_replaceable_p (node->decl)
2077 && !DECL_EXTERNAL (node->decl))
2078 avail = AVAIL_OVERWRITABLE;
2079 else avail = AVAIL_AVAILABLE;
2081 return avail;
2084 /* Worker for cgraph_node_can_be_local_p. */
2085 static bool
2086 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2087 void *data ATTRIBUTE_UNUSED)
2089 return !(!node->force_output
2090 && ((DECL_COMDAT (node->decl)
2091 && !node->forced_by_abi
2092 && !symtab_used_from_object_file_p (node)
2093 && !node->same_comdat_group)
2094 || !node->externally_visible));
2097 /* Return true if NODE can be made local for API change.
2098 Extern inline functions and C++ COMDAT functions can be made local
2099 at the expense of possible code size growth if function is used in multiple
2100 compilation units. */
2101 bool
2102 cgraph_node_can_be_local_p (struct cgraph_node *node)
2104 return (!node->address_taken
2105 && !cgraph_for_node_and_aliases (node,
2106 cgraph_node_cannot_be_local_p_1,
2107 NULL, true));
2110 /* Call calback on NODE, thunks and aliases associated to NODE.
2111 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2112 skipped. */
2114 bool
2115 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2116 bool (*callback) (struct cgraph_node *, void *),
2117 void *data,
2118 bool include_overwritable)
2120 struct cgraph_edge *e;
2121 int i;
2122 struct ipa_ref *ref;
2124 if (callback (node, data))
2125 return true;
2126 for (e = node->callers; e; e = e->next_caller)
2127 if (e->caller->thunk.thunk_p
2128 && (include_overwritable
2129 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2130 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2131 include_overwritable))
2132 return true;
2133 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2134 if (ref->use == IPA_REF_ALIAS)
2136 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2137 if (include_overwritable
2138 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2139 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2140 include_overwritable))
2141 return true;
2143 return false;
2146 /* Call calback on NODE and aliases associated to NODE.
2147 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2148 skipped. */
2150 bool
2151 cgraph_for_node_and_aliases (struct cgraph_node *node,
2152 bool (*callback) (struct cgraph_node *, void *),
2153 void *data,
2154 bool include_overwritable)
2156 int i;
2157 struct ipa_ref *ref;
2159 if (callback (node, data))
2160 return true;
2161 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
2162 if (ref->use == IPA_REF_ALIAS)
2164 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2165 if (include_overwritable
2166 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2167 if (cgraph_for_node_and_aliases (alias, callback, data,
2168 include_overwritable))
2169 return true;
2171 return false;
2174 /* Worker to bring NODE local. */
2176 static bool
2177 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2179 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2180 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2182 symtab_make_decl_local (node->decl);
2184 node->externally_visible = false;
2185 node->forced_by_abi = false;
2186 node->local.local = true;
2187 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2188 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2189 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2190 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2192 return false;
2195 /* Bring NODE local. */
2197 void
2198 cgraph_make_node_local (struct cgraph_node *node)
2200 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2201 NULL, true);
2204 /* Worker to set nothrow flag. */
2206 static bool
2207 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2209 struct cgraph_edge *e;
2211 TREE_NOTHROW (node->decl) = data != NULL;
2213 if (data != NULL)
2214 for (e = node->callers; e; e = e->next_caller)
2215 e->can_throw_external = false;
2216 return false;
2219 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2220 if any to NOTHROW. */
2222 void
2223 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2225 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2226 (void *)(size_t)nothrow, false);
2229 /* Worker to set const flag. */
2231 static bool
2232 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2234 /* Static constructors and destructors without a side effect can be
2235 optimized out. */
2236 if (data && !((size_t)data & 2))
2238 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2239 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2240 if (DECL_STATIC_DESTRUCTOR (node->decl))
2241 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2243 TREE_READONLY (node->decl) = data != NULL;
2244 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2245 return false;
2248 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2249 if any to READONLY. */
2251 void
2252 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2254 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2255 (void *)(size_t)(readonly + (int)looping * 2),
2256 false);
2259 /* Worker to set pure flag. */
2261 static bool
2262 cgraph_set_pure_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 DECL_PURE_P (node->decl) = data != NULL;
2274 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2275 return false;
2278 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2279 if any to PURE. */
2281 void
2282 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2284 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2285 (void *)(size_t)(pure + (int)looping * 2),
2286 false);
2289 /* Return true when NODE can not return or throw and thus
2290 it is safe to ignore its side effects for IPA analysis. */
2292 bool
2293 cgraph_node_cannot_return (struct cgraph_node *node)
2295 int flags = flags_from_decl_or_type (node->decl);
2296 if (!flag_exceptions)
2297 return (flags & ECF_NORETURN) != 0;
2298 else
2299 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2300 == (ECF_NORETURN | ECF_NOTHROW));
2303 /* Return true when call of E can not lead to return from caller
2304 and thus it is safe to ignore its side effects for IPA analysis
2305 when computing side effects of the caller.
2306 FIXME: We could actually mark all edges that have no reaching
2307 patch to EXIT_BLOCK_PTR or throw to get better results. */
2308 bool
2309 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2311 if (cgraph_node_cannot_return (e->caller))
2312 return true;
2313 if (e->indirect_unknown_callee)
2315 int flags = e->indirect_info->ecf_flags;
2316 if (!flag_exceptions)
2317 return (flags & ECF_NORETURN) != 0;
2318 else
2319 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2320 == (ECF_NORETURN | ECF_NOTHROW));
2322 else
2323 return cgraph_node_cannot_return (e->callee);
2326 /* Return true when function NODE can be removed from callgraph
2327 if all direct calls are eliminated. */
2329 bool
2330 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2332 gcc_assert (!node->global.inlined_to);
2333 /* Extern inlines can always go, we will use the external definition. */
2334 if (DECL_EXTERNAL (node->decl))
2335 return true;
2336 /* When function is needed, we can not remove it. */
2337 if (node->force_output || node->used_from_other_partition)
2338 return false;
2339 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2340 || DECL_STATIC_DESTRUCTOR (node->decl))
2341 return false;
2342 /* Only COMDAT functions can be removed if externally visible. */
2343 if (node->externally_visible
2344 && (!DECL_COMDAT (node->decl)
2345 || node->forced_by_abi
2346 || symtab_used_from_object_file_p (node)))
2347 return false;
2348 return true;
2351 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2353 static bool
2354 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2356 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2359 /* Return true when function NODE and its aliases can be removed from callgraph
2360 if all direct calls are eliminated. */
2362 bool
2363 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2365 /* Extern inlines can always go, we will use the external definition. */
2366 if (DECL_EXTERNAL (node->decl))
2367 return true;
2368 if (node->address_taken)
2369 return false;
2370 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2373 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2375 static bool
2376 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2378 return symtab_used_from_object_file_p (node);
2381 /* Return true when function NODE can be expected to be removed
2382 from program when direct calls in this compilation unit are removed.
2384 As a special case COMDAT functions are
2385 cgraph_can_remove_if_no_direct_calls_p while the are not
2386 cgraph_only_called_directly_p (it is possible they are called from other
2387 unit)
2389 This function behaves as cgraph_only_called_directly_p because eliminating
2390 all uses of COMDAT function does not make it necessarily disappear from
2391 the program unless we are compiling whole program or we do LTO. In this
2392 case we know we win since dynamic linking will not really discard the
2393 linkonce section. */
2395 bool
2396 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2398 gcc_assert (!node->global.inlined_to);
2399 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2400 return false;
2401 if (!in_lto_p && !flag_whole_program)
2402 return cgraph_only_called_directly_p (node);
2403 else
2405 if (DECL_EXTERNAL (node->decl))
2406 return true;
2407 return cgraph_can_remove_if_no_direct_calls_p (node);
2412 /* Worker for cgraph_only_called_directly_p. */
2414 static bool
2415 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2417 return !cgraph_only_called_directly_or_aliased_p (node);
2420 /* Return true when function NODE and all its aliases are only called
2421 directly.
2422 i.e. it is not externally visible, address was not taken and
2423 it is not used in any other non-standard way. */
2425 bool
2426 cgraph_only_called_directly_p (struct cgraph_node *node)
2428 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2429 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2430 NULL, true);
2434 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2436 static bool
2437 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2439 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2440 struct cgraph_edge *cs;
2441 enum availability avail;
2442 cgraph_function_or_thunk_node (node, &avail);
2444 if (avail > AVAIL_OVERWRITABLE)
2445 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2446 if (!cs->indirect_inlining_edge)
2447 redirect_callers->safe_push (cs);
2448 return false;
2451 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2452 (i.e. are not overwritable). */
2454 vec<cgraph_edge_p>
2455 collect_callers_of_node (struct cgraph_node *node)
2457 vec<cgraph_edge_p> redirect_callers = vNULL;
2458 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2459 &redirect_callers, false);
2460 return redirect_callers;
2463 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2464 static bool
2465 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2467 node = cgraph_function_or_thunk_node (node, NULL);
2468 node2 = cgraph_function_or_thunk_node (node2, NULL);
2469 while (node != node2 && node2)
2470 node2 = node2->clone_of;
2471 return node2 != NULL;
2474 /* Verify edge E count and frequency. */
2476 static bool
2477 verify_edge_count_and_frequency (struct cgraph_edge *e)
2479 bool error_found = false;
2480 if (e->count < 0)
2482 error ("caller edge count is negative");
2483 error_found = true;
2485 if (e->frequency < 0)
2487 error ("caller edge frequency is negative");
2488 error_found = true;
2490 if (e->frequency > CGRAPH_FREQ_MAX)
2492 error ("caller edge frequency is too large");
2493 error_found = true;
2495 if (gimple_has_body_p (e->caller->decl)
2496 && !e->caller->global.inlined_to
2497 && !e->speculative
2498 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2499 Remove this once edges are actually removed from the function at that time. */
2500 && (e->frequency
2501 || (inline_edge_summary_vec.exists ()
2502 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2503 || !inline_edge_summary (e)->predicate)))
2504 && (e->frequency
2505 != compute_call_stmt_bb_frequency (e->caller->decl,
2506 gimple_bb (e->call_stmt))))
2508 error ("caller edge frequency %i does not match BB frequency %i",
2509 e->frequency,
2510 compute_call_stmt_bb_frequency (e->caller->decl,
2511 gimple_bb (e->call_stmt)));
2512 error_found = true;
2514 return error_found;
2517 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2518 static void
2519 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2521 bool fndecl_was_null = false;
2522 /* debug_gimple_stmt needs correct cfun */
2523 if (cfun != this_cfun)
2524 set_cfun (this_cfun);
2525 /* ...and an actual current_function_decl */
2526 if (!current_function_decl)
2528 current_function_decl = this_cfun->decl;
2529 fndecl_was_null = true;
2531 debug_gimple_stmt (stmt);
2532 if (fndecl_was_null)
2533 current_function_decl = NULL;
2536 /* Verify that call graph edge E corresponds to DECL from the associated
2537 statement. Return true if the verification should fail. */
2539 static bool
2540 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2542 struct cgraph_node *node;
2544 if (!decl || e->callee->global.inlined_to)
2545 return false;
2546 if (cgraph_state == CGRAPH_LTO_STREAMING)
2547 return false;
2548 node = cgraph_get_node (decl);
2550 /* We do not know if a node from a different partition is an alias or what it
2551 aliases and therefore cannot do the former_clone_of check reliably. */
2552 if (!node || node->in_other_partition || e->callee->in_other_partition)
2553 return false;
2554 node = cgraph_function_or_thunk_node (node, NULL);
2556 if (e->callee->former_clone_of != node->decl
2557 /* IPA-CP sometimes redirect edge to clone and then back to the former
2558 function. This ping-pong has to go, eventually. */
2559 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2560 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2561 return true;
2562 else
2563 return false;
2566 /* Verify cgraph nodes of given cgraph node. */
2567 DEBUG_FUNCTION void
2568 verify_cgraph_node (struct cgraph_node *node)
2570 struct cgraph_edge *e;
2571 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
2572 basic_block this_block;
2573 gimple_stmt_iterator gsi;
2574 bool error_found = false;
2576 if (seen_error ())
2577 return;
2579 timevar_push (TV_CGRAPH_VERIFY);
2580 error_found |= verify_symtab_base (node);
2581 for (e = node->callees; e; e = e->next_callee)
2582 if (e->aux)
2584 error ("aux field set for edge %s->%s",
2585 identifier_to_locale (cgraph_node_name (e->caller)),
2586 identifier_to_locale (cgraph_node_name (e->callee)));
2587 error_found = true;
2589 if (node->count < 0)
2591 error ("execution count is negative");
2592 error_found = true;
2594 if (node->global.inlined_to && node->same_comdat_group)
2596 error ("inline clone in same comdat group list");
2597 error_found = true;
2599 if (!node->definition && !node->in_other_partition && node->local.local)
2601 error ("local symbols must be defined");
2602 error_found = true;
2604 if (node->global.inlined_to && node->externally_visible)
2606 error ("externally visible inline clone");
2607 error_found = true;
2609 if (node->global.inlined_to && node->address_taken)
2611 error ("inline clone with address taken");
2612 error_found = true;
2614 if (node->global.inlined_to && node->force_output)
2616 error ("inline clone is forced to output");
2617 error_found = true;
2619 for (e = node->indirect_calls; e; e = e->next_callee)
2621 if (e->aux)
2623 error ("aux field set for indirect edge from %s",
2624 identifier_to_locale (cgraph_node_name (e->caller)));
2625 error_found = true;
2627 if (!e->indirect_unknown_callee
2628 || !e->indirect_info)
2630 error ("An indirect edge from %s is not marked as indirect or has "
2631 "associated indirect_info, the corresponding statement is: ",
2632 identifier_to_locale (cgraph_node_name (e->caller)));
2633 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2634 error_found = true;
2637 for (e = node->callers; e; e = e->next_caller)
2639 if (verify_edge_count_and_frequency (e))
2640 error_found = true;
2641 if (!e->inline_failed)
2643 if (node->global.inlined_to
2644 != (e->caller->global.inlined_to
2645 ? e->caller->global.inlined_to : e->caller))
2647 error ("inlined_to pointer is wrong");
2648 error_found = true;
2650 if (node->callers->next_caller)
2652 error ("multiple inline callers");
2653 error_found = true;
2656 else
2657 if (node->global.inlined_to)
2659 error ("inlined_to pointer set for noninline callers");
2660 error_found = true;
2663 for (e = node->indirect_calls; e; e = e->next_callee)
2664 if (verify_edge_count_and_frequency (e))
2665 error_found = true;
2666 if (!node->callers && node->global.inlined_to)
2668 error ("inlined_to pointer is set but no predecessors found");
2669 error_found = true;
2671 if (node->global.inlined_to == node)
2673 error ("inlined_to pointer refers to itself");
2674 error_found = true;
2677 if (node->clone_of)
2679 struct cgraph_node *n;
2680 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2681 if (n == node)
2682 break;
2683 if (!n)
2685 error ("node has wrong clone_of");
2686 error_found = true;
2689 if (node->clones)
2691 struct cgraph_node *n;
2692 for (n = node->clones; n; n = n->next_sibling_clone)
2693 if (n->clone_of != node)
2694 break;
2695 if (n)
2697 error ("node has wrong clone list");
2698 error_found = true;
2701 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2703 error ("node is in clone list but it is not clone");
2704 error_found = true;
2706 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2708 error ("node has wrong prev_clone pointer");
2709 error_found = true;
2711 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2713 error ("double linked list of clones corrupted");
2714 error_found = true;
2717 if (node->analyzed && node->alias)
2719 bool ref_found = false;
2720 int i;
2721 struct ipa_ref *ref;
2723 if (node->callees)
2725 error ("Alias has call edges");
2726 error_found = true;
2728 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list,
2729 i, ref); i++)
2730 if (ref->use != IPA_REF_ALIAS)
2732 error ("Alias has non-alias reference");
2733 error_found = true;
2735 else if (ref_found)
2737 error ("Alias has more than one alias reference");
2738 error_found = true;
2740 else
2741 ref_found = true;
2742 if (!ref_found)
2744 error ("Analyzed alias has no reference");
2745 error_found = true;
2748 if (node->analyzed && node->thunk.thunk_p)
2750 if (!node->callees)
2752 error ("No edge out of thunk node");
2753 error_found = true;
2755 else if (node->callees->next_callee)
2757 error ("More than one edge out of thunk node");
2758 error_found = true;
2760 if (gimple_has_body_p (node->decl))
2762 error ("Thunk is not supposed to have body");
2763 error_found = true;
2766 else if (node->analyzed && gimple_has_body_p (node->decl)
2767 && !TREE_ASM_WRITTEN (node->decl)
2768 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
2769 && !flag_wpa)
2771 if (this_cfun->cfg)
2773 pointer_set_t *stmts = pointer_set_create ();
2774 int i;
2775 struct ipa_ref *ref;
2777 /* Reach the trees by walking over the CFG, and note the
2778 enclosing basic-blocks in the call edges. */
2779 FOR_EACH_BB_FN (this_block, this_cfun)
2781 for (gsi = gsi_start_phis (this_block);
2782 !gsi_end_p (gsi); gsi_next (&gsi))
2783 pointer_set_insert (stmts, gsi_stmt (gsi));
2784 for (gsi = gsi_start_bb (this_block);
2785 !gsi_end_p (gsi);
2786 gsi_next (&gsi))
2788 gimple stmt = gsi_stmt (gsi);
2789 pointer_set_insert (stmts, stmt);
2790 if (is_gimple_call (stmt))
2792 struct cgraph_edge *e = cgraph_edge (node, stmt);
2793 tree decl = gimple_call_fndecl (stmt);
2794 if (e)
2796 if (e->aux)
2798 error ("shared call_stmt:");
2799 cgraph_debug_gimple_stmt (this_cfun, stmt);
2800 error_found = true;
2802 if (!e->indirect_unknown_callee)
2804 if (verify_edge_corresponds_to_fndecl (e, decl))
2806 error ("edge points to wrong declaration:");
2807 debug_tree (e->callee->decl);
2808 fprintf (stderr," Instead of:");
2809 debug_tree (decl);
2810 error_found = true;
2813 else if (decl)
2815 error ("an indirect edge with unknown callee "
2816 "corresponding to a call_stmt with "
2817 "a known declaration:");
2818 error_found = true;
2819 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2821 e->aux = (void *)1;
2823 else if (decl)
2825 error ("missing callgraph edge for call stmt:");
2826 cgraph_debug_gimple_stmt (this_cfun, stmt);
2827 error_found = true;
2832 for (i = 0;
2833 ipa_ref_list_reference_iterate (&node->ref_list, i, ref);
2834 i++)
2835 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2837 error ("reference to dead statement");
2838 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2839 error_found = true;
2841 pointer_set_destroy (stmts);
2843 else
2844 /* No CFG available?! */
2845 gcc_unreachable ();
2847 for (e = node->callees; e; e = e->next_callee)
2849 if (!e->aux)
2851 error ("edge %s->%s has no corresponding call_stmt",
2852 identifier_to_locale (cgraph_node_name (e->caller)),
2853 identifier_to_locale (cgraph_node_name (e->callee)));
2854 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2855 error_found = true;
2857 e->aux = 0;
2859 for (e = node->indirect_calls; e; e = e->next_callee)
2861 if (!e->aux && !e->speculative)
2863 error ("an indirect edge from %s has no corresponding call_stmt",
2864 identifier_to_locale (cgraph_node_name (e->caller)));
2865 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2866 error_found = true;
2868 e->aux = 0;
2871 if (error_found)
2873 dump_cgraph_node (stderr, node);
2874 internal_error ("verify_cgraph_node failed");
2876 timevar_pop (TV_CGRAPH_VERIFY);
2879 /* Verify whole cgraph structure. */
2880 DEBUG_FUNCTION void
2881 verify_cgraph (void)
2883 struct cgraph_node *node;
2885 if (seen_error ())
2886 return;
2888 FOR_EACH_FUNCTION (node)
2889 verify_cgraph_node (node);
2892 /* Create external decl node for DECL.
2893 The difference i nbetween cgraph_get_create_node and
2894 cgraph_get_create_real_symbol_node is that cgraph_get_create_node
2895 may return inline clone, while cgraph_get_create_real_symbol_node
2896 will create a new node in this case.
2897 FIXME: This function should be removed once clones are put out of decl
2898 hash. */
2900 struct cgraph_node *
2901 cgraph_get_create_real_symbol_node (tree decl)
2903 struct cgraph_node *first_clone = cgraph_get_node (decl);
2904 struct cgraph_node *node;
2905 /* create symbol table node. even if inline clone exists, we can not take
2906 it as a target of non-inlined call. */
2907 node = cgraph_get_node (decl);
2908 if (node && !node->global.inlined_to)
2909 return node;
2911 node = cgraph_create_node (decl);
2913 /* ok, we previously inlined the function, then removed the offline copy and
2914 now we want it back for external call. this can happen when devirtualizing
2915 while inlining function called once that happens after extern inlined and
2916 virtuals are already removed. in this case introduce the external node
2917 and make it available for call. */
2918 if (first_clone)
2920 first_clone->clone_of = node;
2921 node->clones = first_clone;
2922 symtab_prevail_in_asm_name_hash (node);
2923 symtab_insert_node_to_hashtable (node);
2924 if (dump_file)
2925 fprintf (dump_file, "Introduced new external node "
2926 "(%s/%i) and turned into root of the clone tree.\n",
2927 xstrdup (cgraph_node_name (node)), node->order);
2929 else if (dump_file)
2930 fprintf (dump_file, "Introduced new external node "
2931 "(%s/%i).\n", xstrdup (cgraph_node_name (node)),
2932 node->order);
2933 return node;
2937 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2938 Walk through thunk, too.
2939 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2941 struct cgraph_node *
2942 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2946 node = cgraph_function_or_thunk_node (node, availability);
2947 if (node->thunk.thunk_p)
2949 node = node->callees->callee;
2950 if (availability)
2952 enum availability a;
2953 a = cgraph_function_body_availability (node);
2954 if (a < *availability)
2955 *availability = a;
2957 node = cgraph_function_or_thunk_node (node, availability);
2959 } while (node && node->thunk.thunk_p);
2960 return node;
2963 /* When doing LTO, read NODE's body from disk if it is not already present. */
2965 bool
2966 cgraph_get_body (struct cgraph_node *node)
2968 struct lto_file_decl_data *file_data;
2969 const char *data, *name;
2970 size_t len;
2971 tree decl = node->decl;
2973 if (DECL_RESULT (decl))
2974 return false;
2976 gcc_assert (in_lto_p);
2978 file_data = node->lto_file_data;
2979 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2981 /* We may have renamed the declaration, e.g., a static function. */
2982 name = lto_get_decl_name_mapping (file_data, name);
2984 data = lto_get_section_data (file_data, LTO_section_function_body,
2985 name, &len);
2986 if (!data)
2988 dump_cgraph_node (stderr, node);
2989 fatal_error ("%s: section %s is missing",
2990 file_data->file_name,
2991 name);
2994 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2996 lto_input_function_body (file_data, node, data);
2997 lto_stats.num_function_bodies++;
2998 lto_free_section_data (file_data, LTO_section_function_body, name,
2999 data, len);
3000 lto_free_function_in_decl_state_for_node (node);
3001 return true;
3004 /* Verify if the type of the argument matches that of the function
3005 declaration. If we cannot verify this or there is a mismatch,
3006 return false. */
3008 static bool
3009 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3011 tree parms, p;
3012 unsigned int i, nargs;
3014 /* Calls to internal functions always match their signature. */
3015 if (gimple_call_internal_p (stmt))
3016 return true;
3018 nargs = gimple_call_num_args (stmt);
3020 /* Get argument types for verification. */
3021 if (fndecl)
3022 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3023 else
3024 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3026 /* Verify if the type of the argument matches that of the function
3027 declaration. If we cannot verify this or there is a mismatch,
3028 return false. */
3029 if (fndecl && DECL_ARGUMENTS (fndecl))
3031 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3032 i < nargs;
3033 i++, p = DECL_CHAIN (p))
3035 tree arg;
3036 /* We cannot distinguish a varargs function from the case
3037 of excess parameters, still deferring the inlining decision
3038 to the callee is possible. */
3039 if (!p)
3040 break;
3041 arg = gimple_call_arg (stmt, i);
3042 if (p == error_mark_node
3043 || arg == error_mark_node
3044 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3045 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3046 return false;
3048 if (args_count_match && p)
3049 return false;
3051 else if (parms)
3053 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3055 tree arg;
3056 /* If this is a varargs function defer inlining decision
3057 to callee. */
3058 if (!p)
3059 break;
3060 arg = gimple_call_arg (stmt, i);
3061 if (TREE_VALUE (p) == error_mark_node
3062 || arg == error_mark_node
3063 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3064 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3065 && !fold_convertible_p (TREE_VALUE (p), arg)))
3066 return false;
3069 else
3071 if (nargs != 0)
3072 return false;
3074 return true;
3077 /* Verify if the type of the argument and lhs of CALL_STMT matches
3078 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3079 true, the arg count needs to be the same.
3080 If we cannot verify this or there is a mismatch, return false. */
3082 bool
3083 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3084 bool args_count_match)
3086 tree lhs;
3088 if ((DECL_RESULT (callee)
3089 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3090 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3091 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3092 TREE_TYPE (lhs))
3093 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3094 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3095 return false;
3096 return true;
3099 #include "gt-cgraph.h"