Simplify convert_modes, ignoring invalid old modes for CONST_INTs.
[official-gcc.git] / gcc / cgraph.c
blob8eedaa493aaeda45a98d42f1423e89c2e8248e53
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 "tree-ssa.h"
46 #include "value-prof.h"
47 #include "except.h"
48 #include "diagnostic-core.h"
49 #include "rtl.h"
50 #include "ipa-utils.h"
51 #include "lto-streamer.h"
52 #include "ipa-inline.h"
53 #include "cfgloop.h"
54 #include "gimple-pretty-print.h"
56 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
57 #include "tree-pass.h"
59 static void cgraph_node_remove_callers (struct cgraph_node *node);
60 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
61 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
63 /* Queue of cgraph nodes scheduled to be lowered. */
64 symtab_node x_cgraph_nodes_queue;
65 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
67 /* Number of nodes in existence. */
68 int cgraph_n_nodes;
70 /* Maximal uid used in cgraph nodes. */
71 int cgraph_max_uid;
73 /* Maximal uid used in cgraph edges. */
74 int cgraph_edge_max_uid;
76 /* Set when whole unit has been analyzed so we can access global info. */
77 bool cgraph_global_info_ready = false;
79 /* What state callgraph is in right now. */
80 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
82 /* Set when the cgraph is fully build and the basic flags are computed. */
83 bool cgraph_function_flags_ready = false;
85 /* List of hooks triggered on cgraph_edge events. */
86 struct cgraph_edge_hook_list {
87 cgraph_edge_hook hook;
88 void *data;
89 struct cgraph_edge_hook_list *next;
92 /* List of hooks triggered on cgraph_node events. */
93 struct cgraph_node_hook_list {
94 cgraph_node_hook hook;
95 void *data;
96 struct cgraph_node_hook_list *next;
99 /* List of hooks triggered on events involving two cgraph_edges. */
100 struct cgraph_2edge_hook_list {
101 cgraph_2edge_hook hook;
102 void *data;
103 struct cgraph_2edge_hook_list *next;
106 /* List of hooks triggered on events involving two cgraph_nodes. */
107 struct cgraph_2node_hook_list {
108 cgraph_2node_hook hook;
109 void *data;
110 struct cgraph_2node_hook_list *next;
113 /* List of hooks triggered when an edge is removed. */
114 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
115 /* List of hooks triggered when a node is removed. */
116 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
117 /* List of hooks triggered when an edge is duplicated. */
118 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
119 /* List of hooks triggered when a node is duplicated. */
120 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
121 /* List of hooks triggered when an function is inserted. */
122 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
124 /* Head of a linked list of unused (freed) call graph nodes.
125 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
126 static GTY(()) struct cgraph_node *free_nodes;
127 /* Head of a linked list of unused (freed) call graph edges.
128 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
129 static GTY(()) struct cgraph_edge *free_edges;
131 /* Did procss_same_body_aliases run? */
132 bool cpp_implicit_aliases_done;
134 /* Map a cgraph_node to cgraph_function_version_info using this htab.
135 The cgraph_function_version_info has a THIS_NODE field that is the
136 corresponding cgraph_node.. */
138 static htab_t GTY((param_is (struct cgraph_function_version_info *)))
139 cgraph_fnver_htab = NULL;
141 /* Hash function for cgraph_fnver_htab. */
142 static hashval_t
143 cgraph_fnver_htab_hash (const void *ptr)
145 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
146 return (hashval_t)(uid);
149 /* eq function for cgraph_fnver_htab. */
150 static int
151 cgraph_fnver_htab_eq (const void *p1, const void *p2)
153 const struct cgraph_function_version_info *n1
154 = (const struct cgraph_function_version_info *)p1;
155 const struct cgraph_function_version_info *n2
156 = (const struct cgraph_function_version_info *)p2;
158 return n1->this_node->uid == n2->this_node->uid;
161 /* Mark as GC root all allocated nodes. */
162 static GTY(()) struct cgraph_function_version_info *
163 version_info_node = NULL;
165 /* Get the cgraph_function_version_info node corresponding to node. */
166 struct cgraph_function_version_info *
167 get_cgraph_node_version (struct cgraph_node *node)
169 struct cgraph_function_version_info *ret;
170 struct cgraph_function_version_info key;
171 key.this_node = node;
173 if (cgraph_fnver_htab == NULL)
174 return NULL;
176 ret = (struct cgraph_function_version_info *)
177 htab_find (cgraph_fnver_htab, &key);
179 return ret;
182 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
183 corresponding to cgraph_node NODE. */
184 struct cgraph_function_version_info *
185 insert_new_cgraph_node_version (struct cgraph_node *node)
187 void **slot;
189 version_info_node = NULL;
190 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
191 version_info_node->this_node = node;
193 if (cgraph_fnver_htab == NULL)
194 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
195 cgraph_fnver_htab_eq, NULL);
197 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
198 gcc_assert (slot != NULL);
199 *slot = version_info_node;
200 return version_info_node;
203 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
204 DECL is a duplicate declaration. */
205 void
206 delete_function_version (tree decl)
208 struct cgraph_node *decl_node = cgraph_get_node (decl);
209 struct cgraph_function_version_info *decl_v = NULL;
211 if (decl_node == NULL)
212 return;
214 decl_v = get_cgraph_node_version (decl_node);
216 if (decl_v == NULL)
217 return;
219 if (decl_v->prev != NULL)
220 decl_v->prev->next = decl_v->next;
222 if (decl_v->next != NULL)
223 decl_v->next->prev = decl_v->prev;
225 if (cgraph_fnver_htab != NULL)
226 htab_remove_elt (cgraph_fnver_htab, decl_v);
228 cgraph_remove_node (decl_node);
231 /* Record that DECL1 and DECL2 are semantically identical function
232 versions. */
233 void
234 record_function_versions (tree decl1, tree decl2)
236 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
237 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
238 struct cgraph_function_version_info *decl1_v = NULL;
239 struct cgraph_function_version_info *decl2_v = NULL;
240 struct cgraph_function_version_info *before;
241 struct cgraph_function_version_info *after;
243 gcc_assert (decl1_node != NULL && decl2_node != NULL);
244 decl1_v = get_cgraph_node_version (decl1_node);
245 decl2_v = get_cgraph_node_version (decl2_node);
247 if (decl1_v != NULL && decl2_v != NULL)
248 return;
250 if (decl1_v == NULL)
251 decl1_v = insert_new_cgraph_node_version (decl1_node);
253 if (decl2_v == NULL)
254 decl2_v = insert_new_cgraph_node_version (decl2_node);
256 /* Chain decl2_v and decl1_v. All semantically identical versions
257 will be chained together. */
259 before = decl1_v;
260 after = decl2_v;
262 while (before->next != NULL)
263 before = before->next;
265 while (after->prev != NULL)
266 after= after->prev;
268 before->next = after;
269 after->prev = before;
272 /* Macros to access the next item in the list of free cgraph nodes and
273 edges. */
274 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
275 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
276 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
278 /* Register HOOK to be called with DATA on each removed edge. */
279 struct cgraph_edge_hook_list *
280 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
282 struct cgraph_edge_hook_list *entry;
283 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
285 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
286 entry->hook = hook;
287 entry->data = data;
288 entry->next = NULL;
289 while (*ptr)
290 ptr = &(*ptr)->next;
291 *ptr = entry;
292 return entry;
295 /* Remove ENTRY from the list of hooks called on removing edges. */
296 void
297 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
299 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
301 while (*ptr != entry)
302 ptr = &(*ptr)->next;
303 *ptr = entry->next;
304 free (entry);
307 /* Call all edge removal hooks. */
308 static void
309 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
311 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
312 while (entry)
314 entry->hook (e, entry->data);
315 entry = entry->next;
319 /* Register HOOK to be called with DATA on each removed node. */
320 struct cgraph_node_hook_list *
321 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
323 struct cgraph_node_hook_list *entry;
324 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
326 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
327 entry->hook = hook;
328 entry->data = data;
329 entry->next = NULL;
330 while (*ptr)
331 ptr = &(*ptr)->next;
332 *ptr = entry;
333 return entry;
336 /* Remove ENTRY from the list of hooks called on removing nodes. */
337 void
338 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
340 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
342 while (*ptr != entry)
343 ptr = &(*ptr)->next;
344 *ptr = entry->next;
345 free (entry);
348 /* Call all node removal hooks. */
349 static void
350 cgraph_call_node_removal_hooks (struct cgraph_node *node)
352 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
353 while (entry)
355 entry->hook (node, entry->data);
356 entry = entry->next;
360 /* Register HOOK to be called with DATA on each inserted node. */
361 struct cgraph_node_hook_list *
362 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
364 struct cgraph_node_hook_list *entry;
365 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
367 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
368 entry->hook = hook;
369 entry->data = data;
370 entry->next = NULL;
371 while (*ptr)
372 ptr = &(*ptr)->next;
373 *ptr = entry;
374 return entry;
377 /* Remove ENTRY from the list of hooks called on inserted nodes. */
378 void
379 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
381 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
383 while (*ptr != entry)
384 ptr = &(*ptr)->next;
385 *ptr = entry->next;
386 free (entry);
389 /* Call all node insertion hooks. */
390 void
391 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
393 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
394 while (entry)
396 entry->hook (node, entry->data);
397 entry = entry->next;
401 /* Register HOOK to be called with DATA on each duplicated edge. */
402 struct cgraph_2edge_hook_list *
403 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
405 struct cgraph_2edge_hook_list *entry;
406 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
408 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
409 entry->hook = hook;
410 entry->data = data;
411 entry->next = NULL;
412 while (*ptr)
413 ptr = &(*ptr)->next;
414 *ptr = entry;
415 return entry;
418 /* Remove ENTRY from the list of hooks called on duplicating edges. */
419 void
420 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
422 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
424 while (*ptr != entry)
425 ptr = &(*ptr)->next;
426 *ptr = entry->next;
427 free (entry);
430 /* Call all edge duplication hooks. */
431 void
432 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
433 struct cgraph_edge *cs2)
435 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
436 while (entry)
438 entry->hook (cs1, cs2, entry->data);
439 entry = entry->next;
443 /* Register HOOK to be called with DATA on each duplicated node. */
444 struct cgraph_2node_hook_list *
445 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
447 struct cgraph_2node_hook_list *entry;
448 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
450 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
451 entry->hook = hook;
452 entry->data = data;
453 entry->next = NULL;
454 while (*ptr)
455 ptr = &(*ptr)->next;
456 *ptr = entry;
457 return entry;
460 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
461 void
462 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
464 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
466 while (*ptr != entry)
467 ptr = &(*ptr)->next;
468 *ptr = entry->next;
469 free (entry);
472 /* Call all node duplication hooks. */
473 void
474 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
475 struct cgraph_node *node2)
477 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
478 while (entry)
480 entry->hook (node1, node2, entry->data);
481 entry = entry->next;
485 /* Allocate new callgraph node. */
487 static inline struct cgraph_node *
488 cgraph_allocate_node (void)
490 struct cgraph_node *node;
492 if (free_nodes)
494 node = free_nodes;
495 free_nodes = NEXT_FREE_NODE (node);
497 else
499 node = ggc_alloc_cleared_cgraph_node ();
500 node->uid = cgraph_max_uid++;
503 return node;
506 /* Allocate new callgraph node and insert it into basic data structures. */
508 struct cgraph_node *
509 cgraph_create_empty_node (void)
511 struct cgraph_node *node = cgraph_allocate_node ();
513 node->symbol.type = SYMTAB_FUNCTION;
514 node->frequency = NODE_FREQUENCY_NORMAL;
515 node->count_materialization_scale = REG_BR_PROB_BASE;
516 cgraph_n_nodes++;
517 return node;
520 /* Return cgraph node assigned to DECL. Create new one when needed. */
522 struct cgraph_node *
523 cgraph_create_node (tree decl)
525 struct cgraph_node *node = cgraph_create_empty_node ();
526 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
528 node->symbol.decl = decl;
529 symtab_register_node ((symtab_node) node);
531 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
533 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
534 node->next_nested = node->origin->nested;
535 node->origin->nested = node;
537 return node;
540 /* Try to find a call graph node for declaration DECL and if it does not exist,
541 create it. */
543 struct cgraph_node *
544 cgraph_get_create_node (tree decl)
546 struct cgraph_node *node;
548 node = cgraph_get_node (decl);
549 if (node)
550 return node;
552 return cgraph_create_node (decl);
555 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
556 the function body is associated with (not necessarily cgraph_node (DECL). */
558 struct cgraph_node *
559 cgraph_create_function_alias (tree alias, tree target)
561 struct cgraph_node *alias_node;
563 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
564 || TREE_CODE (target) == IDENTIFIER_NODE);
565 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
566 alias_node = cgraph_get_create_node (alias);
567 gcc_assert (!alias_node->symbol.definition);
568 alias_node->symbol.alias_target = target;
569 alias_node->symbol.definition = true;
570 alias_node->symbol.alias = true;
571 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
572 alias_node->symbol.weakref = true;
573 return alias_node;
576 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
577 and NULL otherwise.
578 Same body aliases are output whenever the body of DECL is output,
579 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
581 struct cgraph_node *
582 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
584 struct cgraph_node *n;
585 #ifndef ASM_OUTPUT_DEF
586 /* If aliases aren't supported by the assembler, fail. */
587 return NULL;
588 #endif
589 /* Langhooks can create same body aliases of symbols not defined.
590 Those are useless. Drop them on the floor. */
591 if (cgraph_global_info_ready)
592 return NULL;
594 n = cgraph_create_function_alias (alias, decl);
595 n->symbol.cpp_implicit_alias = true;
596 if (cpp_implicit_aliases_done)
597 symtab_resolve_alias ((symtab_node)n,
598 (symtab_node)cgraph_get_node (decl));
599 return n;
602 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
603 aliases DECL with an adjustments made into the first parameter.
604 See comments in thunk_adjust for detail on the parameters. */
606 struct cgraph_node *
607 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
608 tree alias, tree decl ATTRIBUTE_UNUSED,
609 bool this_adjusting,
610 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
611 tree virtual_offset,
612 tree real_alias)
614 struct cgraph_node *node;
616 node = cgraph_get_node (alias);
617 if (node)
619 gcc_assert (node->symbol.definition);
620 gcc_assert (!node->symbol.alias);
621 gcc_assert (!node->thunk.thunk_p);
622 cgraph_remove_node (node);
625 node = cgraph_create_node (alias);
626 gcc_checking_assert (!virtual_offset
627 || wi::eq_p (virtual_offset, virtual_value));
628 node->thunk.fixed_offset = fixed_offset;
629 node->thunk.this_adjusting = this_adjusting;
630 node->thunk.virtual_value = virtual_value;
631 node->thunk.virtual_offset_p = virtual_offset != NULL;
632 node->thunk.alias = real_alias;
633 node->thunk.thunk_p = true;
634 node->symbol.definition = true;
636 return node;
639 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
640 Return NULL if there's no such node. */
642 struct cgraph_node *
643 cgraph_node_for_asm (tree asmname)
645 /* We do not want to look at inline clones. */
646 for (symtab_node node = symtab_node_for_asm (asmname);
647 node;
648 node = node->symbol.next_sharing_asm_name)
650 cgraph_node *cn = dyn_cast <cgraph_node> (node);
651 if (cn && !cn->global.inlined_to)
652 return cn;
654 return NULL;
657 /* Returns a hash value for X (which really is a cgraph_edge). */
659 static hashval_t
660 edge_hash (const void *x)
662 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
665 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
667 static int
668 edge_eq (const void *x, const void *y)
670 return ((const struct cgraph_edge *) x)->call_stmt == y;
673 /* Add call graph edge E to call site hash of its caller. */
675 static inline void
676 cgraph_update_edge_in_call_site_hash (struct cgraph_edge *e)
678 void **slot;
679 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
680 e->call_stmt,
681 htab_hash_pointer (e->call_stmt),
682 INSERT);
683 *slot = e;
686 /* Add call graph edge E to call site hash of its caller. */
688 static inline void
689 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
691 void **slot;
692 /* There are two speculative edges for every statement (one direct,
693 one indirect); always hash the direct one. */
694 if (e->speculative && e->indirect_unknown_callee)
695 return;
696 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
697 e->call_stmt,
698 htab_hash_pointer (e->call_stmt),
699 INSERT);
700 if (*slot)
702 gcc_assert (((struct cgraph_edge *)*slot)->speculative);
703 if (e->callee)
704 *slot = e;
705 return;
707 gcc_assert (!*slot || e->speculative);
708 *slot = e;
711 /* Return the callgraph edge representing the GIMPLE_CALL statement
712 CALL_STMT. */
714 struct cgraph_edge *
715 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
717 struct cgraph_edge *e, *e2;
718 int n = 0;
720 if (node->call_site_hash)
721 return (struct cgraph_edge *)
722 htab_find_with_hash (node->call_site_hash, call_stmt,
723 htab_hash_pointer (call_stmt));
725 /* This loop may turn out to be performance problem. In such case adding
726 hashtables into call nodes with very many edges is probably best
727 solution. It is not good idea to add pointer into CALL_EXPR itself
728 because we want to make possible having multiple cgraph nodes representing
729 different clones of the same body before the body is actually cloned. */
730 for (e = node->callees; e; e = e->next_callee)
732 if (e->call_stmt == call_stmt)
733 break;
734 n++;
737 if (!e)
738 for (e = node->indirect_calls; e; e = e->next_callee)
740 if (e->call_stmt == call_stmt)
741 break;
742 n++;
745 if (n > 100)
747 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
748 for (e2 = node->callees; e2; e2 = e2->next_callee)
749 cgraph_add_edge_to_call_site_hash (e2);
750 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
751 cgraph_add_edge_to_call_site_hash (e2);
754 return e;
758 /* Change field call_stmt of edge E to NEW_STMT.
759 If UPDATE_SPECULATIVE and E is any component of speculative
760 edge, then update all components. */
762 void
763 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt,
764 bool update_speculative)
766 tree decl;
768 /* Speculative edges has three component, update all of them
769 when asked to. */
770 if (update_speculative && e->speculative)
772 struct cgraph_edge *direct, *indirect;
773 struct ipa_ref *ref;
775 cgraph_speculative_call_info (e, direct, indirect, ref);
776 cgraph_set_call_stmt (direct, new_stmt, false);
777 cgraph_set_call_stmt (indirect, new_stmt, false);
778 ref->stmt = new_stmt;
779 return;
782 /* Only direct speculative edges go to call_site_hash. */
783 if (e->caller->call_site_hash
784 && (!e->speculative || !e->indirect_unknown_callee))
786 htab_remove_elt_with_hash (e->caller->call_site_hash,
787 e->call_stmt,
788 htab_hash_pointer (e->call_stmt));
791 e->call_stmt = new_stmt;
792 if (e->indirect_unknown_callee
793 && (decl = gimple_call_fndecl (new_stmt)))
795 /* Constant propagation (and possibly also inlining?) can turn an
796 indirect call into a direct one. */
797 struct cgraph_node *new_callee = cgraph_get_node (decl);
799 gcc_checking_assert (new_callee);
800 e = cgraph_make_edge_direct (e, new_callee);
803 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
804 e->can_throw_external = stmt_can_throw_external (new_stmt);
805 pop_cfun ();
806 if (e->caller->call_site_hash)
807 cgraph_add_edge_to_call_site_hash (e);
810 /* Allocate a cgraph_edge structure and fill it with data according to the
811 parameters of which only CALLEE can be NULL (when creating an indirect call
812 edge). */
814 static struct cgraph_edge *
815 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
816 gimple call_stmt, gcov_type count, int freq)
818 struct cgraph_edge *edge;
820 /* LTO does not actually have access to the call_stmt since these
821 have not been loaded yet. */
822 if (call_stmt)
824 /* This is a rather expensive check possibly triggering
825 construction of call stmt hashtable. */
826 #ifdef ENABLE_CHECKING
827 struct cgraph_edge *e;
828 gcc_checking_assert (!(e=cgraph_edge (caller, call_stmt)) || e->speculative);
829 #endif
831 gcc_assert (is_gimple_call (call_stmt));
834 if (free_edges)
836 edge = free_edges;
837 free_edges = NEXT_FREE_EDGE (edge);
839 else
841 edge = ggc_alloc_cgraph_edge ();
842 edge->uid = cgraph_edge_max_uid++;
845 edge->aux = NULL;
846 edge->caller = caller;
847 edge->callee = callee;
848 edge->prev_caller = NULL;
849 edge->next_caller = NULL;
850 edge->prev_callee = NULL;
851 edge->next_callee = NULL;
852 edge->lto_stmt_uid = 0;
854 edge->count = count;
855 gcc_assert (count >= 0);
856 edge->frequency = freq;
857 gcc_assert (freq >= 0);
858 gcc_assert (freq <= CGRAPH_FREQ_MAX);
860 edge->call_stmt = call_stmt;
861 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
862 edge->can_throw_external
863 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
864 pop_cfun ();
865 if (call_stmt
866 && callee && callee->symbol.decl
867 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl,
868 false))
869 edge->call_stmt_cannot_inline_p = true;
870 else
871 edge->call_stmt_cannot_inline_p = false;
873 edge->indirect_info = NULL;
874 edge->indirect_inlining_edge = 0;
875 edge->speculative = false;
876 if (call_stmt && caller->call_site_hash)
877 cgraph_add_edge_to_call_site_hash (edge);
879 return edge;
882 /* Create edge from CALLER to CALLEE in the cgraph. */
884 struct cgraph_edge *
885 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
886 gimple call_stmt, gcov_type count, int freq)
888 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
889 count, freq);
891 edge->indirect_unknown_callee = 0;
892 initialize_inline_failed (edge);
894 edge->next_caller = callee->callers;
895 if (callee->callers)
896 callee->callers->prev_caller = edge;
897 edge->next_callee = caller->callees;
898 if (caller->callees)
899 caller->callees->prev_callee = edge;
900 caller->callees = edge;
901 callee->callers = edge;
903 return edge;
906 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
908 struct cgraph_indirect_call_info *
909 cgraph_allocate_init_indirect_info (void)
911 struct cgraph_indirect_call_info *ii;
913 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
914 ii->param_index = -1;
915 return ii;
918 /* Create an indirect edge with a yet-undetermined callee where the call
919 statement destination is a formal parameter of the caller with index
920 PARAM_INDEX. */
922 struct cgraph_edge *
923 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
924 int ecf_flags,
925 gcov_type count, int freq)
927 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
928 count, freq);
929 tree target;
931 edge->indirect_unknown_callee = 1;
932 initialize_inline_failed (edge);
934 edge->indirect_info = cgraph_allocate_init_indirect_info ();
935 edge->indirect_info->ecf_flags = ecf_flags;
937 /* Record polymorphic call info. */
938 if (call_stmt
939 && (target = gimple_call_fn (call_stmt))
940 && virtual_method_call_p (target))
942 tree type = obj_type_ref_class (target);
945 /* Only record types can have virtual calls. */
946 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
947 edge->indirect_info->param_index = -1;
948 edge->indirect_info->otr_token
949 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
950 edge->indirect_info->otr_type = type;
951 edge->indirect_info->polymorphic = 1;
954 edge->next_callee = caller->indirect_calls;
955 if (caller->indirect_calls)
956 caller->indirect_calls->prev_callee = edge;
957 caller->indirect_calls = edge;
959 return edge;
962 /* Remove the edge E from the list of the callers of the callee. */
964 static inline void
965 cgraph_edge_remove_callee (struct cgraph_edge *e)
967 gcc_assert (!e->indirect_unknown_callee);
968 if (e->prev_caller)
969 e->prev_caller->next_caller = e->next_caller;
970 if (e->next_caller)
971 e->next_caller->prev_caller = e->prev_caller;
972 if (!e->prev_caller)
973 e->callee->callers = e->next_caller;
976 /* Remove the edge E from the list of the callees of the caller. */
978 static inline void
979 cgraph_edge_remove_caller (struct cgraph_edge *e)
981 if (e->prev_callee)
982 e->prev_callee->next_callee = e->next_callee;
983 if (e->next_callee)
984 e->next_callee->prev_callee = e->prev_callee;
985 if (!e->prev_callee)
987 if (e->indirect_unknown_callee)
988 e->caller->indirect_calls = e->next_callee;
989 else
990 e->caller->callees = e->next_callee;
992 if (e->caller->call_site_hash)
993 htab_remove_elt_with_hash (e->caller->call_site_hash,
994 e->call_stmt,
995 htab_hash_pointer (e->call_stmt));
998 /* Put the edge onto the free list. */
1000 static void
1001 cgraph_free_edge (struct cgraph_edge *e)
1003 int uid = e->uid;
1005 if (e->indirect_info)
1006 ggc_free (e->indirect_info);
1008 /* Clear out the edge so we do not dangle pointers. */
1009 memset (e, 0, sizeof (*e));
1010 e->uid = uid;
1011 NEXT_FREE_EDGE (e) = free_edges;
1012 free_edges = e;
1015 /* Remove the edge E in the cgraph. */
1017 void
1018 cgraph_remove_edge (struct cgraph_edge *e)
1020 /* Call all edge removal hooks. */
1021 cgraph_call_edge_removal_hooks (e);
1023 if (!e->indirect_unknown_callee)
1024 /* Remove from callers list of the callee. */
1025 cgraph_edge_remove_callee (e);
1027 /* Remove from callees list of the callers. */
1028 cgraph_edge_remove_caller (e);
1030 /* Put the edge onto the free list. */
1031 cgraph_free_edge (e);
1034 /* Set callee of call graph edge E and add it to the corresponding set of
1035 callers. */
1037 static void
1038 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1040 e->prev_caller = NULL;
1041 if (n->callers)
1042 n->callers->prev_caller = e;
1043 e->next_caller = n->callers;
1044 n->callers = e;
1045 e->callee = n;
1048 /* Turn edge E into speculative call calling N2. Update
1049 the profile so the direct call is taken COUNT times
1050 with FREQUENCY.
1052 At clone materialization time, the indirect call E will
1053 be expanded as:
1055 if (call_dest == N2)
1056 n2 ();
1057 else
1058 call call_dest
1060 At this time the function just creates the direct call,
1061 the referencd representing the if conditional and attaches
1062 them all to the orginal indirect call statement.
1064 Return direct edge created. */
1066 struct cgraph_edge *
1067 cgraph_turn_edge_to_speculative (struct cgraph_edge *e,
1068 struct cgraph_node *n2,
1069 gcov_type direct_count,
1070 int direct_frequency)
1072 struct cgraph_node *n = e->caller;
1073 struct ipa_ref *ref;
1074 struct cgraph_edge *e2;
1076 if (dump_file)
1078 fprintf (dump_file, "Indirect call -> speculative call"
1079 " %s/%i => %s/%i\n",
1080 xstrdup (cgraph_node_name (n)), n->symbol.order,
1081 xstrdup (cgraph_node_name (n2)), n2->symbol.order);
1083 e->speculative = true;
1084 e2 = cgraph_create_edge (n, n2, e->call_stmt, direct_count, direct_frequency);
1085 initialize_inline_failed (e2);
1086 e2->speculative = true;
1087 if (TREE_NOTHROW (n2->symbol.decl))
1088 e2->can_throw_external = false;
1089 else
1090 e2->can_throw_external = e->can_throw_external;
1091 e2->lto_stmt_uid = e->lto_stmt_uid;
1092 e->count -= e2->count;
1093 e->frequency -= e2->frequency;
1094 cgraph_call_edge_duplication_hooks (e, e2);
1095 ref = ipa_record_reference ((symtab_node)n, (symtab_node)n2,
1096 IPA_REF_ADDR, e->call_stmt);
1097 ref->lto_stmt_uid = e->lto_stmt_uid;
1098 ref->speculative = e->speculative;
1099 cgraph_mark_address_taken_node (n2);
1100 return e2;
1103 /* Speculative call consist of three components:
1104 1) an indirect edge representing the original call
1105 2) an direct edge representing the new call
1106 3) ADDR_EXPR reference representing the speculative check.
1107 All three components are attached to single statement (the indirect
1108 call) and if one of them exists, all of them must exist.
1110 Given speculative call edge E, return all three components.
1113 void
1114 cgraph_speculative_call_info (struct cgraph_edge *e,
1115 struct cgraph_edge *&direct,
1116 struct cgraph_edge *&indirect,
1117 struct ipa_ref *&reference)
1119 struct ipa_ref *ref;
1120 int i;
1121 struct cgraph_edge *e2;
1123 if (!e->indirect_unknown_callee)
1124 for (e2 = e->caller->indirect_calls;
1125 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1126 e2 = e2->next_callee)
1128 else
1130 e2 = e;
1131 /* We can take advantage of the call stmt hash. */
1132 if (e2->call_stmt)
1134 e = cgraph_edge (e->caller, e2->call_stmt);
1135 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1137 else
1138 for (e = e->caller->callees;
1139 e2->call_stmt != e->call_stmt
1140 || e2->lto_stmt_uid != e->lto_stmt_uid;
1141 e = e->next_callee)
1144 gcc_assert (e->speculative && e2->speculative);
1145 direct = e;
1146 indirect = e2;
1148 reference = NULL;
1149 for (i = 0; ipa_ref_list_reference_iterate (&e->caller->symbol.ref_list,
1150 i, ref); i++)
1151 if (ref->speculative
1152 && ((ref->stmt && ref->stmt == e->call_stmt)
1153 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1155 reference = ref;
1156 break;
1159 /* Speculative edge always consist of all three components - direct edge,
1160 indirect and reference. */
1162 gcc_assert (e && e2 && ref);
1165 /* Redirect callee of E to N. The function does not update underlying
1166 call expression. */
1168 void
1169 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1171 /* Remove from callers list of the current callee. */
1172 cgraph_edge_remove_callee (e);
1174 /* Insert to callers list of the new callee. */
1175 cgraph_set_edge_callee (e, n);
1178 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1179 Remove the speculative call sequence and return edge representing the call.
1180 It is up to caller to redirect the call as appropriate. */
1182 struct cgraph_edge *
1183 cgraph_resolve_speculation (struct cgraph_edge *edge, tree callee_decl)
1185 struct cgraph_edge *e2;
1186 struct ipa_ref *ref;
1188 gcc_assert (edge->speculative);
1189 cgraph_speculative_call_info (edge, e2, edge, ref);
1190 if (!callee_decl
1191 || !symtab_semantically_equivalent_p ((symtab_node) ref->referred,
1192 symtab_get_node (callee_decl)))
1194 if (dump_file)
1196 if (callee_decl)
1198 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1199 "turned out to have contradicting known target ",
1200 xstrdup (cgraph_node_name (edge->caller)), edge->caller->symbol.order,
1201 xstrdup (cgraph_node_name (e2->callee)), e2->callee->symbol.order);
1202 print_generic_expr (dump_file, callee_decl, 0);
1203 fprintf (dump_file, "\n");
1205 else
1207 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1208 xstrdup (cgraph_node_name (edge->caller)), edge->caller->symbol.order,
1209 xstrdup (cgraph_node_name (e2->callee)), e2->callee->symbol.order);
1213 else
1215 struct cgraph_edge *tmp = edge;
1216 if (dump_file)
1217 fprintf (dump_file, "Speculative call turned into direct call.\n");
1218 edge = e2;
1219 e2 = tmp;
1220 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1221 in the functions inlined through it. */
1223 edge->count += e2->count;
1224 edge->frequency += e2->frequency;
1225 if (edge->frequency > CGRAPH_FREQ_MAX)
1226 edge->frequency = CGRAPH_FREQ_MAX;
1227 edge->speculative = false;
1228 e2->speculative = false;
1229 ipa_remove_reference (ref);
1230 if (e2->indirect_unknown_callee || e2->inline_failed)
1231 cgraph_remove_edge (e2);
1232 else
1233 cgraph_remove_node_and_inline_clones (e2->callee, NULL);
1234 if (edge->caller->call_site_hash)
1235 cgraph_update_edge_in_call_site_hash (edge);
1236 return edge;
1239 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1240 CALLEE. DELTA is an integer constant that is to be added to the this
1241 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1243 struct cgraph_edge *
1244 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1246 gcc_assert (edge->indirect_unknown_callee);
1248 /* If we are redirecting speculative call, make it non-speculative. */
1249 if (edge->indirect_unknown_callee && edge->speculative)
1251 edge = cgraph_resolve_speculation (edge, callee->symbol.decl);
1253 /* On successful speculation just return the pre existing direct edge. */
1254 if (!edge->indirect_unknown_callee)
1255 return edge;
1258 edge->indirect_unknown_callee = 0;
1259 ggc_free (edge->indirect_info);
1260 edge->indirect_info = NULL;
1262 /* Get the edge out of the indirect edge list. */
1263 if (edge->prev_callee)
1264 edge->prev_callee->next_callee = edge->next_callee;
1265 if (edge->next_callee)
1266 edge->next_callee->prev_callee = edge->prev_callee;
1267 if (!edge->prev_callee)
1268 edge->caller->indirect_calls = edge->next_callee;
1270 /* Put it into the normal callee list */
1271 edge->prev_callee = NULL;
1272 edge->next_callee = edge->caller->callees;
1273 if (edge->caller->callees)
1274 edge->caller->callees->prev_callee = edge;
1275 edge->caller->callees = edge;
1277 /* Insert to callers list of the new callee. */
1278 cgraph_set_edge_callee (edge, callee);
1280 if (edge->call_stmt)
1281 edge->call_stmt_cannot_inline_p
1282 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl,
1283 false);
1285 /* We need to re-determine the inlining status of the edge. */
1286 initialize_inline_failed (edge);
1287 return edge;
1290 /* If necessary, change the function declaration in the call statement
1291 associated with E so that it corresponds to the edge callee. */
1293 gimple
1294 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1296 tree decl = gimple_call_fndecl (e->call_stmt);
1297 gimple new_stmt;
1298 gimple_stmt_iterator gsi;
1299 #ifdef ENABLE_CHECKING
1300 struct cgraph_node *node;
1301 #endif
1303 if (e->speculative)
1305 struct cgraph_edge *e2;
1306 gimple new_stmt;
1307 struct ipa_ref *ref;
1309 cgraph_speculative_call_info (e, e, e2, ref);
1310 /* If there already is an direct call (i.e. as a result of inliner's
1311 substitution), forget about speculating. */
1312 if (decl)
1313 e = cgraph_resolve_speculation (e, decl);
1314 /* If types do not match, speculation was likely wrong.
1315 The direct edge was posisbly redirected to the clone with a different
1316 signature. We did not update the call statement yet, so compare it
1317 with the reference that still points to the proper type. */
1318 else if (!gimple_check_call_matching_types (e->call_stmt,
1319 ref->referred->symbol.decl,
1320 true))
1322 if (dump_file)
1323 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1324 "Type mismatch.\n",
1325 xstrdup (cgraph_node_name (e->caller)),
1326 e->caller->symbol.order,
1327 xstrdup (cgraph_node_name (e->callee)),
1328 e->callee->symbol.order);
1329 e = cgraph_resolve_speculation (e, NULL);
1330 /* We are producing the final function body and will throw away the
1331 callgraph edges really soon. Reset the counts/frequencies to
1332 keep verifier happy in the case of roundoff errors. */
1333 e->count = gimple_bb (e->call_stmt)->count;
1334 e->frequency = compute_call_stmt_bb_frequency
1335 (e->caller->symbol.decl, gimple_bb (e->call_stmt));
1337 /* Expand speculation into GIMPLE code. */
1338 else
1340 if (dump_file)
1341 fprintf (dump_file,
1342 "Expanding speculative call of %s/%i -> %s/%i count:"
1343 HOST_WIDEST_INT_PRINT_DEC"\n",
1344 xstrdup (cgraph_node_name (e->caller)),
1345 e->caller->symbol.order,
1346 xstrdup (cgraph_node_name (e->callee)),
1347 e->callee->symbol.order,
1348 (HOST_WIDEST_INT)e->count);
1349 gcc_assert (e2->speculative);
1350 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
1351 new_stmt = gimple_ic (e->call_stmt, cgraph (ref->referred),
1352 e->count || e2->count
1353 ? RDIV (e->count * REG_BR_PROB_BASE,
1354 e->count + e2->count)
1355 : e->frequency || e2->frequency
1356 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1357 e->frequency + e2->frequency)
1358 : REG_BR_PROB_BASE / 2,
1359 e->count, e->count + e2->count);
1360 e->speculative = false;
1361 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt,
1362 new_stmt, false);
1363 e->frequency = compute_call_stmt_bb_frequency
1364 (e->caller->symbol.decl, gimple_bb (e->call_stmt));
1365 e2->frequency = compute_call_stmt_bb_frequency
1366 (e2->caller->symbol.decl, gimple_bb (e2->call_stmt));
1367 e2->speculative = false;
1368 ref->speculative = false;
1369 ref->stmt = NULL;
1370 /* Indirect edges are not both in the call site hash.
1371 get it updated. */
1372 if (e->caller->call_site_hash)
1373 cgraph_update_edge_in_call_site_hash (e2);
1374 pop_cfun ();
1375 /* Continue redirecting E to proper target. */
1379 if (e->indirect_unknown_callee
1380 || decl == e->callee->symbol.decl)
1381 return e->call_stmt;
1383 #ifdef ENABLE_CHECKING
1384 if (decl)
1386 node = cgraph_get_node (decl);
1387 gcc_assert (!node || !node->clone.combined_args_to_skip);
1389 #endif
1391 if (cgraph_dump_file)
1393 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1394 xstrdup (cgraph_node_name (e->caller)), e->caller->symbol.order,
1395 xstrdup (cgraph_node_name (e->callee)), e->callee->symbol.order);
1396 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1397 if (e->callee->clone.combined_args_to_skip)
1399 fprintf (cgraph_dump_file, " combined args to skip: ");
1400 dump_bitmap (cgraph_dump_file,
1401 e->callee->clone.combined_args_to_skip);
1405 if (e->callee->clone.combined_args_to_skip)
1407 int lp_nr;
1409 new_stmt
1410 = gimple_call_copy_skip_args (e->call_stmt,
1411 e->callee->clone.combined_args_to_skip);
1412 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1413 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1415 if (gimple_vdef (new_stmt)
1416 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1417 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1419 gsi = gsi_for_stmt (e->call_stmt);
1420 gsi_replace (&gsi, new_stmt, false);
1421 /* We need to defer cleaning EH info on the new statement to
1422 fixup-cfg. We may not have dominator information at this point
1423 and thus would end up with unreachable blocks and have no way
1424 to communicate that we need to run CFG cleanup then. */
1425 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1426 if (lp_nr != 0)
1428 remove_stmt_from_eh_lp (e->call_stmt);
1429 add_stmt_to_eh_lp (new_stmt, lp_nr);
1432 else
1434 new_stmt = e->call_stmt;
1435 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1436 update_stmt (new_stmt);
1439 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt, false);
1441 if (cgraph_dump_file)
1443 fprintf (cgraph_dump_file, " updated to:");
1444 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1446 return new_stmt;
1449 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1450 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1451 of OLD_STMT if it was previously call statement.
1452 If NEW_STMT is NULL, the call has been dropped without any
1453 replacement. */
1455 static void
1456 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1457 gimple old_stmt, tree old_call,
1458 gimple new_stmt)
1460 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1461 ? gimple_call_fndecl (new_stmt) : 0;
1463 /* We are seeing indirect calls, then there is nothing to update. */
1464 if (!new_call && !old_call)
1465 return;
1466 /* See if we turned indirect call into direct call or folded call to one builtin
1467 into different builtin. */
1468 if (old_call != new_call)
1470 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1471 struct cgraph_edge *ne = NULL;
1472 gcov_type count;
1473 int frequency;
1475 if (e)
1477 /* See if the edge is already there and has the correct callee. It
1478 might be so because of indirect inlining has already updated
1479 it. We also might've cloned and redirected the edge. */
1480 if (new_call && e->callee)
1482 struct cgraph_node *callee = e->callee;
1483 while (callee)
1485 if (callee->symbol.decl == new_call
1486 || callee->former_clone_of == new_call)
1487 return;
1488 callee = callee->clone_of;
1492 /* Otherwise remove edge and create new one; we can't simply redirect
1493 since function has changed, so inline plan and other information
1494 attached to edge is invalid. */
1495 count = e->count;
1496 frequency = e->frequency;
1497 cgraph_remove_edge (e);
1499 else if (new_call)
1501 /* We are seeing new direct call; compute profile info based on BB. */
1502 basic_block bb = gimple_bb (new_stmt);
1503 count = bb->count;
1504 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1505 bb);
1508 if (new_call)
1510 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1511 new_stmt, count, frequency);
1512 gcc_assert (ne->inline_failed);
1515 /* We only updated the call stmt; update pointer in cgraph edge.. */
1516 else if (old_stmt != new_stmt)
1517 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1520 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1521 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1522 of OLD_STMT before it was updated (updating can happen inplace). */
1524 void
1525 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1527 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1528 struct cgraph_node *node;
1530 gcc_checking_assert (orig);
1531 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1532 if (orig->clones)
1533 for (node = orig->clones; node != orig;)
1535 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1536 if (node->clones)
1537 node = node->clones;
1538 else if (node->next_sibling_clone)
1539 node = node->next_sibling_clone;
1540 else
1542 while (node != orig && !node->next_sibling_clone)
1543 node = node->clone_of;
1544 if (node != orig)
1545 node = node->next_sibling_clone;
1551 /* Remove all callees from the node. */
1553 void
1554 cgraph_node_remove_callees (struct cgraph_node *node)
1556 struct cgraph_edge *e, *f;
1558 /* It is sufficient to remove the edges from the lists of callers of
1559 the callees. The callee list of the node can be zapped with one
1560 assignment. */
1561 for (e = node->callees; e; e = f)
1563 f = e->next_callee;
1564 cgraph_call_edge_removal_hooks (e);
1565 if (!e->indirect_unknown_callee)
1566 cgraph_edge_remove_callee (e);
1567 cgraph_free_edge (e);
1569 for (e = node->indirect_calls; e; e = f)
1571 f = e->next_callee;
1572 cgraph_call_edge_removal_hooks (e);
1573 if (!e->indirect_unknown_callee)
1574 cgraph_edge_remove_callee (e);
1575 cgraph_free_edge (e);
1577 node->indirect_calls = NULL;
1578 node->callees = NULL;
1579 if (node->call_site_hash)
1581 htab_delete (node->call_site_hash);
1582 node->call_site_hash = NULL;
1586 /* Remove all callers from the node. */
1588 static void
1589 cgraph_node_remove_callers (struct cgraph_node *node)
1591 struct cgraph_edge *e, *f;
1593 /* It is sufficient to remove the edges from the lists of callees of
1594 the callers. The caller list of the node can be zapped with one
1595 assignment. */
1596 for (e = node->callers; e; e = f)
1598 f = e->next_caller;
1599 cgraph_call_edge_removal_hooks (e);
1600 cgraph_edge_remove_caller (e);
1601 cgraph_free_edge (e);
1603 node->callers = NULL;
1606 /* Helper function for cgraph_release_function_body and free_lang_data.
1607 It releases body from function DECL without having to inspect its
1608 possibly non-existent symtab node. */
1610 void
1611 release_function_body (tree decl)
1613 if (DECL_STRUCT_FUNCTION (decl))
1615 push_cfun (DECL_STRUCT_FUNCTION (decl));
1616 if (cfun->cfg
1617 && current_loops)
1619 cfun->curr_properties &= ~PROP_loops;
1620 loop_optimizer_finalize ();
1622 if (cfun->gimple_df)
1624 delete_tree_ssa ();
1625 delete_tree_cfg_annotations ();
1626 cfun->eh = NULL;
1628 if (cfun->cfg)
1630 gcc_assert (dom_computed[0] == DOM_NONE);
1631 gcc_assert (dom_computed[1] == DOM_NONE);
1632 clear_edges ();
1633 cfun->cfg = NULL;
1635 if (cfun->value_histograms)
1636 free_histograms ();
1637 pop_cfun ();
1638 gimple_set_body (decl, NULL);
1639 /* Struct function hangs a lot of data that would leak if we didn't
1640 removed all pointers to it. */
1641 ggc_free (DECL_STRUCT_FUNCTION (decl));
1642 DECL_STRUCT_FUNCTION (decl) = NULL;
1644 DECL_SAVED_TREE (decl) = NULL;
1647 /* Release memory used to represent body of function NODE.
1648 Use this only for functions that are released before being translated to
1649 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1650 are free'd in final.c via free_after_compilation(). */
1652 void
1653 cgraph_release_function_body (struct cgraph_node *node)
1655 node->ipa_transforms_to_apply.release ();
1656 if (!node->used_as_abstract_origin && cgraph_state != CGRAPH_STATE_PARSING)
1658 DECL_RESULT (node->symbol.decl) = NULL;
1659 DECL_ARGUMENTS (node->symbol.decl) = NULL;
1661 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1662 of its associated function function declaration because it's
1663 needed to emit debug info later. */
1664 if (!node->used_as_abstract_origin && DECL_INITIAL (node->symbol.decl))
1665 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1666 release_function_body (node->symbol.decl);
1667 if (node->symbol.lto_file_data)
1668 lto_free_function_in_decl_state_for_node ((symtab_node) node);
1671 /* Remove the node from cgraph. */
1673 void
1674 cgraph_remove_node (struct cgraph_node *node)
1676 struct cgraph_node *n;
1677 int uid = node->uid;
1679 cgraph_call_node_removal_hooks (node);
1680 cgraph_node_remove_callers (node);
1681 cgraph_node_remove_callees (node);
1682 node->ipa_transforms_to_apply.release ();
1684 /* Incremental inlining access removed nodes stored in the postorder list.
1686 node->symbol.force_output = false;
1687 node->symbol.forced_by_abi = false;
1688 for (n = node->nested; n; n = n->next_nested)
1689 n->origin = NULL;
1690 node->nested = NULL;
1691 if (node->origin)
1693 struct cgraph_node **node2 = &node->origin->nested;
1695 while (*node2 != node)
1696 node2 = &(*node2)->next_nested;
1697 *node2 = node->next_nested;
1699 symtab_unregister_node ((symtab_node)node);
1700 if (node->prev_sibling_clone)
1701 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1702 else if (node->clone_of)
1703 node->clone_of->clones = node->next_sibling_clone;
1704 if (node->next_sibling_clone)
1705 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1706 if (node->clones)
1708 struct cgraph_node *n, *next;
1710 if (node->clone_of)
1712 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1713 n->clone_of = node->clone_of;
1714 n->clone_of = node->clone_of;
1715 n->next_sibling_clone = node->clone_of->clones;
1716 if (node->clone_of->clones)
1717 node->clone_of->clones->prev_sibling_clone = n;
1718 node->clone_of->clones = node->clones;
1720 else
1722 /* We are removing node with clones. This makes clones inconsistent,
1723 but assume they will be removed subsequently and just keep clone
1724 tree intact. This can happen in unreachable function removal since
1725 we remove unreachable functions in random order, not by bottom-up
1726 walk of clone trees. */
1727 for (n = node->clones; n; n = next)
1729 next = n->next_sibling_clone;
1730 n->next_sibling_clone = NULL;
1731 n->prev_sibling_clone = NULL;
1732 n->clone_of = NULL;
1737 /* While all the clones are removed after being proceeded, the function
1738 itself is kept in the cgraph even after it is compiled. Check whether
1739 we are done with this body and reclaim it proactively if this is the case.
1741 if (cgraph_state != CGRAPH_LTO_STREAMING)
1743 n = cgraph_get_node (node->symbol.decl);
1744 if (!n
1745 || (!n->clones && !n->clone_of && !n->global.inlined_to
1746 && (cgraph_global_info_ready
1747 && (TREE_ASM_WRITTEN (n->symbol.decl)
1748 || DECL_EXTERNAL (n->symbol.decl)
1749 || !n->symbol.analyzed
1750 || (!flag_wpa && n->symbol.in_other_partition)))))
1751 cgraph_release_function_body (node);
1754 node->symbol.decl = NULL;
1755 if (node->call_site_hash)
1757 htab_delete (node->call_site_hash);
1758 node->call_site_hash = NULL;
1760 cgraph_n_nodes--;
1762 /* Clear out the node to NULL all pointers and add the node to the free
1763 list. */
1764 memset (node, 0, sizeof (*node));
1765 node->symbol.type = SYMTAB_FUNCTION;
1766 node->uid = uid;
1767 SET_NEXT_FREE_NODE (node, free_nodes);
1768 free_nodes = node;
1771 /* Likewise indicate that a node is having address taken. */
1773 void
1774 cgraph_mark_address_taken_node (struct cgraph_node *node)
1776 /* Indirect inlining can figure out that all uses of the address are
1777 inlined. */
1778 if (node->global.inlined_to)
1780 gcc_assert (cfun->after_inlining);
1781 gcc_assert (node->callers->indirect_inlining_edge);
1782 return;
1784 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1785 IPA_REF_ADDR reference exists (and thus it should be set on node
1786 representing alias we take address of) and as a test whether address
1787 of the object was taken (and thus it should be set on node alias is
1788 referring to). We should remove the first use and the remove the
1789 following set. */
1790 node->symbol.address_taken = 1;
1791 node = cgraph_function_or_thunk_node (node, NULL);
1792 node->symbol.address_taken = 1;
1795 /* Return local info for the compiled function. */
1797 struct cgraph_local_info *
1798 cgraph_local_info (tree decl)
1800 struct cgraph_node *node;
1802 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1803 node = cgraph_get_node (decl);
1804 if (!node)
1805 return NULL;
1806 return &node->local;
1809 /* Return local info for the compiled function. */
1811 struct cgraph_global_info *
1812 cgraph_global_info (tree decl)
1814 struct cgraph_node *node;
1816 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1817 node = cgraph_get_node (decl);
1818 if (!node)
1819 return NULL;
1820 return &node->global;
1823 /* Return local info for the compiled function. */
1825 struct cgraph_rtl_info *
1826 cgraph_rtl_info (tree decl)
1828 struct cgraph_node *node;
1830 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1831 node = cgraph_get_node (decl);
1832 if (!node
1833 || (decl != current_function_decl
1834 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1835 return NULL;
1836 return &node->rtl;
1839 /* Return a string describing the failure REASON. */
1841 const char*
1842 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1844 #undef DEFCIFCODE
1845 #define DEFCIFCODE(code, string) string,
1847 static const char *cif_string_table[CIF_N_REASONS] = {
1848 #include "cif-code.def"
1851 /* Signedness of an enum type is implementation defined, so cast it
1852 to unsigned before testing. */
1853 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1854 return cif_string_table[reason];
1857 /* Names used to print out the availability enum. */
1858 const char * const cgraph_availability_names[] =
1859 {"unset", "not_available", "overwritable", "available", "local"};
1862 /* Dump call graph node NODE to file F. */
1864 void
1865 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1867 struct cgraph_edge *edge;
1868 int indirect_calls_count = 0;
1870 dump_symtab_base (f, (symtab_node) node);
1872 if (node->global.inlined_to)
1873 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1874 xstrdup (cgraph_node_name (node)),
1875 node->symbol.order,
1876 xstrdup (cgraph_node_name (node->global.inlined_to)),
1877 node->global.inlined_to->symbol.order);
1878 if (node->clone_of)
1879 fprintf (f, " Clone of %s/%i\n",
1880 cgraph_node_asm_name (node->clone_of),
1881 node->clone_of->symbol.order);
1882 if (cgraph_function_flags_ready)
1883 fprintf (f, " Availability: %s\n",
1884 cgraph_availability_names [cgraph_function_body_availability (node)]);
1886 if (node->profile_id)
1887 fprintf (f, " Profile id: %i\n",
1888 node->profile_id);
1889 fprintf (f, " Function flags:");
1890 if (node->count)
1891 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1892 (HOST_WIDEST_INT)node->count);
1893 if (node->origin)
1894 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1895 if (gimple_has_body_p (node->symbol.decl))
1896 fprintf (f, " body");
1897 if (node->process)
1898 fprintf (f, " process");
1899 if (node->local.local)
1900 fprintf (f, " local");
1901 if (node->local.redefined_extern_inline)
1902 fprintf (f, " redefined_extern_inline");
1903 if (node->only_called_at_startup)
1904 fprintf (f, " only_called_at_startup");
1905 if (node->only_called_at_exit)
1906 fprintf (f, " only_called_at_exit");
1907 if (node->tm_clone)
1908 fprintf (f, " tm_clone");
1910 fprintf (f, "\n");
1912 if (node->thunk.thunk_p)
1914 fprintf (f, " Thunk");
1915 if (node->thunk.alias)
1916 fprintf (f, " of %s (asm: %s)",
1917 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1918 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1919 fprintf (f, " fixed offset %i virtual value %i has "
1920 "virtual offset %i)\n",
1921 (int)node->thunk.fixed_offset,
1922 (int)node->thunk.virtual_value,
1923 (int)node->thunk.virtual_offset_p);
1925 if (node->symbol.alias && node->thunk.alias
1926 && DECL_P (node->thunk.alias))
1928 fprintf (f, " Alias of %s",
1929 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1930 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1931 fprintf (f, " (asm: %s)",
1932 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1933 fprintf (f, "\n");
1936 fprintf (f, " Called by: ");
1938 for (edge = node->callers; edge; edge = edge->next_caller)
1940 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1941 edge->caller->symbol.order);
1942 if (edge->count)
1943 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1944 (HOST_WIDEST_INT)edge->count);
1945 if (edge->frequency)
1946 fprintf (f, "(%.2f per call) ",
1947 edge->frequency / (double)CGRAPH_FREQ_BASE);
1948 if (edge->speculative)
1949 fprintf (f, "(speculative) ");
1950 if (!edge->inline_failed)
1951 fprintf (f, "(inlined) ");
1952 if (edge->indirect_inlining_edge)
1953 fprintf (f, "(indirect_inlining) ");
1954 if (edge->can_throw_external)
1955 fprintf (f, "(can throw external) ");
1958 fprintf (f, "\n Calls: ");
1959 for (edge = node->callees; edge; edge = edge->next_callee)
1961 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1962 edge->callee->symbol.order);
1963 if (edge->speculative)
1964 fprintf (f, "(speculative) ");
1965 if (!edge->inline_failed)
1966 fprintf (f, "(inlined) ");
1967 if (edge->indirect_inlining_edge)
1968 fprintf (f, "(indirect_inlining) ");
1969 if (edge->count)
1970 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1971 (HOST_WIDEST_INT)edge->count);
1972 if (edge->frequency)
1973 fprintf (f, "(%.2f per call) ",
1974 edge->frequency / (double)CGRAPH_FREQ_BASE);
1975 if (edge->can_throw_external)
1976 fprintf (f, "(can throw external) ");
1978 fprintf (f, "\n");
1980 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1981 indirect_calls_count++;
1982 if (indirect_calls_count)
1983 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1984 indirect_calls_count);
1988 /* Dump call graph node NODE to stderr. */
1990 DEBUG_FUNCTION void
1991 debug_cgraph_node (struct cgraph_node *node)
1993 dump_cgraph_node (stderr, node);
1997 /* Dump the callgraph to file F. */
1999 void
2000 dump_cgraph (FILE *f)
2002 struct cgraph_node *node;
2004 fprintf (f, "callgraph:\n\n");
2005 FOR_EACH_FUNCTION (node)
2006 dump_cgraph_node (f, node);
2010 /* Dump the call graph to stderr. */
2012 DEBUG_FUNCTION void
2013 debug_cgraph (void)
2015 dump_cgraph (stderr);
2018 /* Return true when the DECL can possibly be inlined. */
2019 bool
2020 cgraph_function_possibly_inlined_p (tree decl)
2022 if (!cgraph_global_info_ready)
2023 return !DECL_UNINLINABLE (decl);
2024 return DECL_POSSIBLY_INLINED (decl);
2027 /* NODE is no longer nested function; update cgraph accordingly. */
2028 void
2029 cgraph_unnest_node (struct cgraph_node *node)
2031 struct cgraph_node **node2 = &node->origin->nested;
2032 gcc_assert (node->origin);
2034 while (*node2 != node)
2035 node2 = &(*node2)->next_nested;
2036 *node2 = node->next_nested;
2037 node->origin = NULL;
2040 /* Return function availability. See cgraph.h for description of individual
2041 return values. */
2042 enum availability
2043 cgraph_function_body_availability (struct cgraph_node *node)
2045 enum availability avail;
2046 if (!node->symbol.analyzed)
2047 avail = AVAIL_NOT_AVAILABLE;
2048 else if (node->local.local)
2049 avail = AVAIL_LOCAL;
2050 else if (node->symbol.alias && node->symbol.weakref)
2051 cgraph_function_or_thunk_node (node, &avail);
2052 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (node->symbol.decl)))
2053 avail = AVAIL_OVERWRITABLE;
2054 else if (!node->symbol.externally_visible)
2055 avail = AVAIL_AVAILABLE;
2056 /* Inline functions are safe to be analyzed even if their symbol can
2057 be overwritten at runtime. It is not meaningful to enforce any sane
2058 behaviour on replacing inline function by different body. */
2059 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
2060 avail = AVAIL_AVAILABLE;
2062 /* If the function can be overwritten, return OVERWRITABLE. Take
2063 care at least of two notable extensions - the COMDAT functions
2064 used to share template instantiations in C++ (this is symmetric
2065 to code cp_cannot_inline_tree_fn and probably shall be shared and
2066 the inlinability hooks completely eliminated).
2068 ??? Does the C++ one definition rule allow us to always return
2069 AVAIL_AVAILABLE here? That would be good reason to preserve this
2070 bit. */
2072 else if (decl_replaceable_p (node->symbol.decl)
2073 && !DECL_EXTERNAL (node->symbol.decl))
2074 avail = AVAIL_OVERWRITABLE;
2075 else avail = AVAIL_AVAILABLE;
2077 return avail;
2080 /* Worker for cgraph_node_can_be_local_p. */
2081 static bool
2082 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2083 void *data ATTRIBUTE_UNUSED)
2085 return !(!node->symbol.force_output
2086 && ((DECL_COMDAT (node->symbol.decl)
2087 && !node->symbol.forced_by_abi
2088 && !symtab_used_from_object_file_p ((symtab_node) node)
2089 && !node->symbol.same_comdat_group)
2090 || !node->symbol.externally_visible));
2093 /* Return true if NODE can be made local for API change.
2094 Extern inline functions and C++ COMDAT functions can be made local
2095 at the expense of possible code size growth if function is used in multiple
2096 compilation units. */
2097 bool
2098 cgraph_node_can_be_local_p (struct cgraph_node *node)
2100 return (!node->symbol.address_taken
2101 && !cgraph_for_node_and_aliases (node,
2102 cgraph_node_cannot_be_local_p_1,
2103 NULL, true));
2106 /* Call calback on NODE, thunks and aliases associated to NODE.
2107 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2108 skipped. */
2110 bool
2111 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2112 bool (*callback) (struct cgraph_node *, void *),
2113 void *data,
2114 bool include_overwritable)
2116 struct cgraph_edge *e;
2117 int i;
2118 struct ipa_ref *ref;
2120 if (callback (node, data))
2121 return true;
2122 for (e = node->callers; e; e = e->next_caller)
2123 if (e->caller->thunk.thunk_p
2124 && (include_overwritable
2125 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2126 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
2127 include_overwritable))
2128 return true;
2129 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
2130 if (ref->use == IPA_REF_ALIAS)
2132 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2133 if (include_overwritable
2134 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2135 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
2136 include_overwritable))
2137 return true;
2139 return false;
2142 /* Call calback on NODE and aliases associated to NODE.
2143 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2144 skipped. */
2146 bool
2147 cgraph_for_node_and_aliases (struct cgraph_node *node,
2148 bool (*callback) (struct cgraph_node *, void *),
2149 void *data,
2150 bool include_overwritable)
2152 int i;
2153 struct ipa_ref *ref;
2155 if (callback (node, data))
2156 return true;
2157 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
2158 if (ref->use == IPA_REF_ALIAS)
2160 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2161 if (include_overwritable
2162 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
2163 if (cgraph_for_node_and_aliases (alias, callback, data,
2164 include_overwritable))
2165 return true;
2167 return false;
2170 /* Worker to bring NODE local. */
2172 static bool
2173 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2175 gcc_checking_assert (cgraph_node_can_be_local_p (node));
2176 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
2178 symtab_make_decl_local (node->symbol.decl);
2180 node->symbol.externally_visible = false;
2181 node->symbol.forced_by_abi = false;
2182 node->local.local = true;
2183 node->symbol.unique_name = (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
2184 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2185 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
2186 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2188 return false;
2191 /* Bring NODE local. */
2193 void
2194 cgraph_make_node_local (struct cgraph_node *node)
2196 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2197 NULL, true);
2200 /* Worker to set nothrow flag. */
2202 static bool
2203 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2205 struct cgraph_edge *e;
2207 TREE_NOTHROW (node->symbol.decl) = data != NULL;
2209 if (data != NULL)
2210 for (e = node->callers; e; e = e->next_caller)
2211 e->can_throw_external = false;
2212 return false;
2215 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2216 if any to NOTHROW. */
2218 void
2219 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2221 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2222 (void *)(size_t)nothrow, false);
2225 /* Worker to set const flag. */
2227 static bool
2228 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
2230 /* Static constructors and destructors without a side effect can be
2231 optimized out. */
2232 if (data && !((size_t)data & 2))
2234 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
2235 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
2236 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2237 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
2239 TREE_READONLY (node->symbol.decl) = data != NULL;
2240 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
2241 return false;
2244 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
2245 if any to READONLY. */
2247 void
2248 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2250 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2251 (void *)(size_t)(readonly + (int)looping * 2),
2252 false);
2255 /* Worker to set pure flag. */
2257 static bool
2258 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2260 /* Static constructors and destructors without a side effect can be
2261 optimized out. */
2262 if (data && !((size_t)data & 2))
2264 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
2265 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
2266 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2267 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
2269 DECL_PURE_P (node->symbol.decl) = data != NULL;
2270 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
2271 return false;
2274 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2275 if any to PURE. */
2277 void
2278 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2280 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2281 (void *)(size_t)(pure + (int)looping * 2),
2282 false);
2285 /* Return true when NODE can not return or throw and thus
2286 it is safe to ignore its side effects for IPA analysis. */
2288 bool
2289 cgraph_node_cannot_return (struct cgraph_node *node)
2291 int flags = flags_from_decl_or_type (node->symbol.decl);
2292 if (!flag_exceptions)
2293 return (flags & ECF_NORETURN) != 0;
2294 else
2295 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2296 == (ECF_NORETURN | ECF_NOTHROW));
2299 /* Return true when call of E can not lead to return from caller
2300 and thus it is safe to ignore its side effects for IPA analysis
2301 when computing side effects of the caller.
2302 FIXME: We could actually mark all edges that have no reaching
2303 patch to EXIT_BLOCK_PTR or throw to get better results. */
2304 bool
2305 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2307 if (cgraph_node_cannot_return (e->caller))
2308 return true;
2309 if (e->indirect_unknown_callee)
2311 int flags = e->indirect_info->ecf_flags;
2312 if (!flag_exceptions)
2313 return (flags & ECF_NORETURN) != 0;
2314 else
2315 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2316 == (ECF_NORETURN | ECF_NOTHROW));
2318 else
2319 return cgraph_node_cannot_return (e->callee);
2322 /* Return true when function NODE can be removed from callgraph
2323 if all direct calls are eliminated. */
2325 bool
2326 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2328 gcc_assert (!node->global.inlined_to);
2329 /* Extern inlines can always go, we will use the external definition. */
2330 if (DECL_EXTERNAL (node->symbol.decl))
2331 return true;
2332 /* When function is needed, we can not remove it. */
2333 if (node->symbol.force_output || node->symbol.used_from_other_partition)
2334 return false;
2335 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
2336 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2337 return false;
2338 /* Only COMDAT functions can be removed if externally visible. */
2339 if (node->symbol.externally_visible
2340 && (!DECL_COMDAT (node->symbol.decl)
2341 || node->symbol.forced_by_abi
2342 || symtab_used_from_object_file_p ((symtab_node) node)))
2343 return false;
2344 return true;
2347 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2349 static bool
2350 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2352 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2355 /* Return true when function NODE and its aliases can be removed from callgraph
2356 if all direct calls are eliminated. */
2358 bool
2359 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2361 /* Extern inlines can always go, we will use the external definition. */
2362 if (DECL_EXTERNAL (node->symbol.decl))
2363 return true;
2364 if (node->symbol.address_taken)
2365 return false;
2366 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2369 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2371 static bool
2372 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2374 return symtab_used_from_object_file_p ((symtab_node) node);
2377 /* Return true when function NODE can be expected to be removed
2378 from program when direct calls in this compilation unit are removed.
2380 As a special case COMDAT functions are
2381 cgraph_can_remove_if_no_direct_calls_p while the are not
2382 cgraph_only_called_directly_p (it is possible they are called from other
2383 unit)
2385 This function behaves as cgraph_only_called_directly_p because eliminating
2386 all uses of COMDAT function does not make it necessarily disappear from
2387 the program unless we are compiling whole program or we do LTO. In this
2388 case we know we win since dynamic linking will not really discard the
2389 linkonce section. */
2391 bool
2392 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2394 gcc_assert (!node->global.inlined_to);
2395 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2396 return false;
2397 if (!in_lto_p && !flag_whole_program)
2398 return cgraph_only_called_directly_p (node);
2399 else
2401 if (DECL_EXTERNAL (node->symbol.decl))
2402 return true;
2403 return cgraph_can_remove_if_no_direct_calls_p (node);
2408 /* Worker for cgraph_only_called_directly_p. */
2410 static bool
2411 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2413 return !cgraph_only_called_directly_or_aliased_p (node);
2416 /* Return true when function NODE and all its aliases are only called
2417 directly.
2418 i.e. it is not externally visible, address was not taken and
2419 it is not used in any other non-standard way. */
2421 bool
2422 cgraph_only_called_directly_p (struct cgraph_node *node)
2424 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2425 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2426 NULL, true);
2430 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2432 static bool
2433 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2435 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2436 struct cgraph_edge *cs;
2437 enum availability avail;
2438 cgraph_function_or_thunk_node (node, &avail);
2440 if (avail > AVAIL_OVERWRITABLE)
2441 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2442 if (!cs->indirect_inlining_edge)
2443 redirect_callers->safe_push (cs);
2444 return false;
2447 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2448 (i.e. are not overwritable). */
2450 vec<cgraph_edge_p>
2451 collect_callers_of_node (struct cgraph_node *node)
2453 vec<cgraph_edge_p> redirect_callers = vNULL;
2454 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2455 &redirect_callers, false);
2456 return redirect_callers;
2459 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2460 static bool
2461 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2463 node = cgraph_function_or_thunk_node (node, NULL);
2464 node2 = cgraph_function_or_thunk_node (node2, NULL);
2465 while (node != node2 && node2)
2466 node2 = node2->clone_of;
2467 return node2 != NULL;
2470 /* Verify edge E count and frequency. */
2472 static bool
2473 verify_edge_count_and_frequency (struct cgraph_edge *e)
2475 bool error_found = false;
2476 if (e->count < 0)
2478 error ("caller edge count is negative");
2479 error_found = true;
2481 if (e->frequency < 0)
2483 error ("caller edge frequency is negative");
2484 error_found = true;
2486 if (e->frequency > CGRAPH_FREQ_MAX)
2488 error ("caller edge frequency is too large");
2489 error_found = true;
2491 if (gimple_has_body_p (e->caller->symbol.decl)
2492 && !e->caller->global.inlined_to
2493 && !e->speculative
2494 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2495 Remove this once edges are actually removed from the function at that time. */
2496 && (e->frequency
2497 || (inline_edge_summary_vec.exists ()
2498 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2499 || !inline_edge_summary (e)->predicate)))
2500 && (e->frequency
2501 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2502 gimple_bb (e->call_stmt))))
2504 error ("caller edge frequency %i does not match BB frequency %i",
2505 e->frequency,
2506 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2507 gimple_bb (e->call_stmt)));
2508 error_found = true;
2510 return error_found;
2513 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2514 static void
2515 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2517 bool fndecl_was_null = false;
2518 /* debug_gimple_stmt needs correct cfun */
2519 if (cfun != this_cfun)
2520 set_cfun (this_cfun);
2521 /* ...and an actual current_function_decl */
2522 if (!current_function_decl)
2524 current_function_decl = this_cfun->decl;
2525 fndecl_was_null = true;
2527 debug_gimple_stmt (stmt);
2528 if (fndecl_was_null)
2529 current_function_decl = NULL;
2532 /* Verify that call graph edge E corresponds to DECL from the associated
2533 statement. Return true if the verification should fail. */
2535 static bool
2536 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2538 struct cgraph_node *node;
2540 if (!decl || e->callee->global.inlined_to)
2541 return false;
2542 if (cgraph_state == CGRAPH_LTO_STREAMING)
2543 return false;
2544 node = cgraph_get_node (decl);
2546 /* We do not know if a node from a different partition is an alias or what it
2547 aliases and therefore cannot do the former_clone_of check reliably. */
2548 if (!node || node->symbol.in_other_partition || e->callee->symbol.in_other_partition)
2549 return false;
2550 node = cgraph_function_or_thunk_node (node, NULL);
2552 if (e->callee->former_clone_of != node->symbol.decl
2553 /* IPA-CP sometimes redirect edge to clone and then back to the former
2554 function. This ping-pong has to go, eventually. */
2555 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2556 && !clone_of_p (cgraph_function_or_thunk_node (node, NULL), e->callee))
2557 return true;
2558 else
2559 return false;
2562 /* Verify cgraph nodes of given cgraph node. */
2563 DEBUG_FUNCTION void
2564 verify_cgraph_node (struct cgraph_node *node)
2566 struct cgraph_edge *e;
2567 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2568 basic_block this_block;
2569 gimple_stmt_iterator gsi;
2570 bool error_found = false;
2572 if (seen_error ())
2573 return;
2575 timevar_push (TV_CGRAPH_VERIFY);
2576 error_found |= verify_symtab_base ((symtab_node) node);
2577 for (e = node->callees; e; e = e->next_callee)
2578 if (e->aux)
2580 error ("aux field set for edge %s->%s",
2581 identifier_to_locale (cgraph_node_name (e->caller)),
2582 identifier_to_locale (cgraph_node_name (e->callee)));
2583 error_found = true;
2585 if (node->count < 0)
2587 error ("execution count is negative");
2588 error_found = true;
2590 if (node->global.inlined_to && node->symbol.same_comdat_group)
2592 error ("inline clone in same comdat group list");
2593 error_found = true;
2595 if (!node->symbol.definition && !node->symbol.in_other_partition && node->local.local)
2597 error ("local symbols must be defined");
2598 error_found = true;
2600 if (node->global.inlined_to && node->symbol.externally_visible)
2602 error ("externally visible inline clone");
2603 error_found = true;
2605 if (node->global.inlined_to && node->symbol.address_taken)
2607 error ("inline clone with address taken");
2608 error_found = true;
2610 if (node->global.inlined_to && node->symbol.force_output)
2612 error ("inline clone is forced to output");
2613 error_found = true;
2615 for (e = node->indirect_calls; e; e = e->next_callee)
2617 if (e->aux)
2619 error ("aux field set for indirect edge from %s",
2620 identifier_to_locale (cgraph_node_name (e->caller)));
2621 error_found = true;
2623 if (!e->indirect_unknown_callee
2624 || !e->indirect_info)
2626 error ("An indirect edge from %s is not marked as indirect or has "
2627 "associated indirect_info, the corresponding statement is: ",
2628 identifier_to_locale (cgraph_node_name (e->caller)));
2629 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2630 error_found = true;
2633 for (e = node->callers; e; e = e->next_caller)
2635 if (verify_edge_count_and_frequency (e))
2636 error_found = true;
2637 if (!e->inline_failed)
2639 if (node->global.inlined_to
2640 != (e->caller->global.inlined_to
2641 ? e->caller->global.inlined_to : e->caller))
2643 error ("inlined_to pointer is wrong");
2644 error_found = true;
2646 if (node->callers->next_caller)
2648 error ("multiple inline callers");
2649 error_found = true;
2652 else
2653 if (node->global.inlined_to)
2655 error ("inlined_to pointer set for noninline callers");
2656 error_found = true;
2659 for (e = node->indirect_calls; e; e = e->next_callee)
2660 if (verify_edge_count_and_frequency (e))
2661 error_found = true;
2662 if (!node->callers && node->global.inlined_to)
2664 error ("inlined_to pointer is set but no predecessors found");
2665 error_found = true;
2667 if (node->global.inlined_to == node)
2669 error ("inlined_to pointer refers to itself");
2670 error_found = true;
2673 if (node->clone_of)
2675 struct cgraph_node *n;
2676 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2677 if (n == node)
2678 break;
2679 if (!n)
2681 error ("node has wrong clone_of");
2682 error_found = true;
2685 if (node->clones)
2687 struct cgraph_node *n;
2688 for (n = node->clones; n; n = n->next_sibling_clone)
2689 if (n->clone_of != node)
2690 break;
2691 if (n)
2693 error ("node has wrong clone list");
2694 error_found = true;
2697 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2699 error ("node is in clone list but it is not clone");
2700 error_found = true;
2702 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2704 error ("node has wrong prev_clone pointer");
2705 error_found = true;
2707 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2709 error ("double linked list of clones corrupted");
2710 error_found = true;
2713 if (node->symbol.analyzed && node->symbol.alias)
2715 bool ref_found = false;
2716 int i;
2717 struct ipa_ref *ref;
2719 if (node->callees)
2721 error ("Alias has call edges");
2722 error_found = true;
2724 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2725 i, ref); i++)
2726 if (ref->use != IPA_REF_ALIAS)
2728 error ("Alias has non-alias reference");
2729 error_found = true;
2731 else if (ref_found)
2733 error ("Alias has more than one alias reference");
2734 error_found = true;
2736 else
2737 ref_found = true;
2738 if (!ref_found)
2740 error ("Analyzed alias has no reference");
2741 error_found = true;
2744 if (node->symbol.analyzed && node->thunk.thunk_p)
2746 if (!node->callees)
2748 error ("No edge out of thunk node");
2749 error_found = true;
2751 else if (node->callees->next_callee)
2753 error ("More than one edge out of thunk node");
2754 error_found = true;
2756 if (gimple_has_body_p (node->symbol.decl))
2758 error ("Thunk is not supposed to have body");
2759 error_found = true;
2762 else if (node->symbol.analyzed && gimple_has_body_p (node->symbol.decl)
2763 && !TREE_ASM_WRITTEN (node->symbol.decl)
2764 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2765 && !flag_wpa)
2767 if (this_cfun->cfg)
2769 pointer_set_t *stmts = pointer_set_create ();
2770 int i;
2771 struct ipa_ref *ref;
2773 /* Reach the trees by walking over the CFG, and note the
2774 enclosing basic-blocks in the call edges. */
2775 FOR_EACH_BB_FN (this_block, this_cfun)
2777 for (gsi = gsi_start_phis (this_block);
2778 !gsi_end_p (gsi); gsi_next (&gsi))
2779 pointer_set_insert (stmts, gsi_stmt (gsi));
2780 for (gsi = gsi_start_bb (this_block);
2781 !gsi_end_p (gsi);
2782 gsi_next (&gsi))
2784 gimple stmt = gsi_stmt (gsi);
2785 pointer_set_insert (stmts, stmt);
2786 if (is_gimple_call (stmt))
2788 struct cgraph_edge *e = cgraph_edge (node, stmt);
2789 tree decl = gimple_call_fndecl (stmt);
2790 if (e)
2792 if (e->aux)
2794 error ("shared call_stmt:");
2795 cgraph_debug_gimple_stmt (this_cfun, stmt);
2796 error_found = true;
2798 if (!e->indirect_unknown_callee)
2800 if (verify_edge_corresponds_to_fndecl (e, decl))
2802 error ("edge points to wrong declaration:");
2803 debug_tree (e->callee->symbol.decl);
2804 fprintf (stderr," Instead of:");
2805 debug_tree (decl);
2806 error_found = true;
2809 else if (decl)
2811 error ("an indirect edge with unknown callee "
2812 "corresponding to a call_stmt with "
2813 "a known declaration:");
2814 error_found = true;
2815 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2817 e->aux = (void *)1;
2819 else if (decl)
2821 error ("missing callgraph edge for call stmt:");
2822 cgraph_debug_gimple_stmt (this_cfun, stmt);
2823 error_found = true;
2828 for (i = 0;
2829 ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref);
2830 i++)
2831 if (ref->stmt && !pointer_set_contains (stmts, ref->stmt))
2833 error ("reference to dead statement");
2834 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2835 error_found = true;
2837 pointer_set_destroy (stmts);
2839 else
2840 /* No CFG available?! */
2841 gcc_unreachable ();
2843 for (e = node->callees; e; e = e->next_callee)
2845 if (!e->aux)
2847 error ("edge %s->%s has no corresponding call_stmt",
2848 identifier_to_locale (cgraph_node_name (e->caller)),
2849 identifier_to_locale (cgraph_node_name (e->callee)));
2850 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2851 error_found = true;
2853 e->aux = 0;
2855 for (e = node->indirect_calls; e; e = e->next_callee)
2857 if (!e->aux && !e->speculative)
2859 error ("an indirect edge from %s has no corresponding call_stmt",
2860 identifier_to_locale (cgraph_node_name (e->caller)));
2861 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2862 error_found = true;
2864 e->aux = 0;
2867 if (error_found)
2869 dump_cgraph_node (stderr, node);
2870 internal_error ("verify_cgraph_node failed");
2872 timevar_pop (TV_CGRAPH_VERIFY);
2875 /* Verify whole cgraph structure. */
2876 DEBUG_FUNCTION void
2877 verify_cgraph (void)
2879 struct cgraph_node *node;
2881 if (seen_error ())
2882 return;
2884 FOR_EACH_FUNCTION (node)
2885 verify_cgraph_node (node);
2888 /* Create external decl node for DECL.
2889 The difference i nbetween cgraph_get_create_node and
2890 cgraph_get_create_real_symbol_node is that cgraph_get_create_node
2891 may return inline clone, while cgraph_get_create_real_symbol_node
2892 will create a new node in this case.
2893 FIXME: This function should be removed once clones are put out of decl
2894 hash. */
2896 struct cgraph_node *
2897 cgraph_get_create_real_symbol_node (tree decl)
2899 struct cgraph_node *first_clone = cgraph_get_node (decl);
2900 struct cgraph_node *node;
2901 /* create symbol table node. even if inline clone exists, we can not take
2902 it as a target of non-inlined call. */
2903 node = cgraph_get_node (decl);
2904 if (node && !node->global.inlined_to)
2905 return node;
2907 node = cgraph_create_node (decl);
2909 /* ok, we previously inlined the function, then removed the offline copy and
2910 now we want it back for external call. this can happen when devirtualizing
2911 while inlining function called once that happens after extern inlined and
2912 virtuals are already removed. in this case introduce the external node
2913 and make it available for call. */
2914 if (first_clone)
2916 first_clone->clone_of = node;
2917 node->clones = first_clone;
2918 symtab_prevail_in_asm_name_hash ((symtab_node) node);
2919 symtab_insert_node_to_hashtable ((symtab_node) node);
2920 if (dump_file)
2921 fprintf (dump_file, "Introduced new external node "
2922 "(%s/%i) and turned into root of the clone tree.\n",
2923 xstrdup (cgraph_node_name (node)), node->symbol.order);
2925 else if (dump_file)
2926 fprintf (dump_file, "Introduced new external node "
2927 "(%s/%i).\n", xstrdup (cgraph_node_name (node)),
2928 node->symbol.order);
2929 return node;
2933 /* Given NODE, walk the alias chain to return the function NODE is alias of.
2934 Walk through thunk, too.
2935 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2937 struct cgraph_node *
2938 cgraph_function_node (struct cgraph_node *node, enum availability *availability)
2942 node = cgraph_function_or_thunk_node (node, availability);
2943 if (node->thunk.thunk_p)
2945 node = node->callees->callee;
2946 if (availability)
2948 enum availability a;
2949 a = cgraph_function_body_availability (node);
2950 if (a < *availability)
2951 *availability = a;
2953 node = cgraph_function_or_thunk_node (node, availability);
2955 } while (node && node->thunk.thunk_p);
2956 return node;
2959 /* When doing LTO, read NODE's body from disk if it is not already present. */
2961 bool
2962 cgraph_get_body (struct cgraph_node *node)
2964 struct lto_file_decl_data *file_data;
2965 const char *data, *name;
2966 size_t len;
2967 tree decl = node->symbol.decl;
2969 if (DECL_RESULT (decl))
2970 return false;
2972 gcc_assert (in_lto_p);
2974 file_data = node->symbol.lto_file_data;
2975 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2977 /* We may have renamed the declaration, e.g., a static function. */
2978 name = lto_get_decl_name_mapping (file_data, name);
2980 data = lto_get_section_data (file_data, LTO_section_function_body,
2981 name, &len);
2982 if (!data)
2984 dump_cgraph_node (stderr, node);
2985 fatal_error ("%s: section %s is missing",
2986 file_data->file_name,
2987 name);
2990 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2992 lto_input_function_body (file_data, node, data);
2993 lto_stats.num_function_bodies++;
2994 lto_free_section_data (file_data, LTO_section_function_body, name,
2995 data, len);
2996 lto_free_function_in_decl_state_for_node ((symtab_node) node);
2997 return true;
3000 /* Verify if the type of the argument matches that of the function
3001 declaration. If we cannot verify this or there is a mismatch,
3002 return false. */
3004 static bool
3005 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3007 tree parms, p;
3008 unsigned int i, nargs;
3010 /* Calls to internal functions always match their signature. */
3011 if (gimple_call_internal_p (stmt))
3012 return true;
3014 nargs = gimple_call_num_args (stmt);
3016 /* Get argument types for verification. */
3017 if (fndecl)
3018 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3019 else
3020 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3022 /* Verify if the type of the argument matches that of the function
3023 declaration. If we cannot verify this or there is a mismatch,
3024 return false. */
3025 if (fndecl && DECL_ARGUMENTS (fndecl))
3027 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3028 i < nargs;
3029 i++, p = DECL_CHAIN (p))
3031 tree arg;
3032 /* We cannot distinguish a varargs function from the case
3033 of excess parameters, still deferring the inlining decision
3034 to the callee is possible. */
3035 if (!p)
3036 break;
3037 arg = gimple_call_arg (stmt, i);
3038 if (p == error_mark_node
3039 || arg == error_mark_node
3040 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3041 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3042 return false;
3044 if (args_count_match && p)
3045 return false;
3047 else if (parms)
3049 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3051 tree arg;
3052 /* If this is a varargs function defer inlining decision
3053 to callee. */
3054 if (!p)
3055 break;
3056 arg = gimple_call_arg (stmt, i);
3057 if (TREE_VALUE (p) == error_mark_node
3058 || arg == error_mark_node
3059 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3060 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3061 && !fold_convertible_p (TREE_VALUE (p), arg)))
3062 return false;
3065 else
3067 if (nargs != 0)
3068 return false;
3070 return true;
3073 /* Verify if the type of the argument and lhs of CALL_STMT matches
3074 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3075 true, the arg count needs to be the same.
3076 If we cannot verify this or there is a mismatch, return false. */
3078 bool
3079 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3080 bool args_count_match)
3082 tree lhs;
3084 if ((DECL_RESULT (callee)
3085 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3086 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3087 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3088 TREE_TYPE (lhs))
3089 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3090 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3091 return false;
3092 return true;
3095 #include "gt-cgraph.h"