Merged revisions 195901,195904,196012 via svnmerge from
[official-gcc.git] / main / gcc / cgraph.c
blobbb2d08e5781fac3f9e33b555c9f711f3deaeb3ec
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-flow.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 "l-ipo.h"
53 #include "ipa-inline.h"
54 #include "cfgloop.h"
55 #include "gimple-pretty-print.h"
57 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
58 #include "tree-pass.h"
60 static void cgraph_node_remove_callers (struct cgraph_node *node);
61 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
62 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
64 /* Queue of cgraph nodes scheduled to be lowered. */
65 symtab_node x_cgraph_nodes_queue;
66 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
68 /* Number of nodes in existence. */
69 int cgraph_n_nodes;
71 /* Maximal uid used in cgraph nodes. */
72 int cgraph_max_uid;
74 /* Maximal uid used in cgraph edges. */
75 int cgraph_edge_max_uid;
77 /* Set when whole unit has been analyzed so we can access global info. */
78 bool cgraph_global_info_ready = false;
80 /* What state callgraph is in right now. */
81 enum cgraph_state cgraph_state = CGRAPH_STATE_PARSING;
83 /* Set when the cgraph is fully build and the basic flags are computed. */
84 bool cgraph_function_flags_ready = false;
86 /* List of hooks triggered on cgraph_edge events. */
87 struct cgraph_edge_hook_list {
88 cgraph_edge_hook hook;
89 void *data;
90 struct cgraph_edge_hook_list *next;
93 /* List of hooks triggered on cgraph_node events. */
94 struct cgraph_node_hook_list {
95 cgraph_node_hook hook;
96 void *data;
97 struct cgraph_node_hook_list *next;
100 /* List of hooks triggered on events involving two cgraph_edges. */
101 struct cgraph_2edge_hook_list {
102 cgraph_2edge_hook hook;
103 void *data;
104 struct cgraph_2edge_hook_list *next;
107 /* List of hooks triggered on events involving two cgraph_nodes. */
108 struct cgraph_2node_hook_list {
109 cgraph_2node_hook hook;
110 void *data;
111 struct cgraph_2node_hook_list *next;
114 /* List of hooks triggered when an edge is removed. */
115 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
116 /* List of hooks triggered when a node is removed. */
117 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
118 /* List of hooks triggered when an edge is duplicated. */
119 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
120 /* List of hooks triggered when a node is duplicated. */
121 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
122 /* List of hooks triggered when an function is inserted. */
123 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
125 /* Head of a linked list of unused (freed) call graph nodes.
126 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
127 static GTY(()) struct cgraph_node *free_nodes;
128 /* Head of a linked list of unused (freed) call graph edges.
129 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
130 static GTY(()) struct cgraph_edge *free_edges;
132 /* Did procss_same_body_aliases run? */
133 bool same_body_aliases_done;
135 /* Map a cgraph_node to cgraph_function_version_info using this htab.
136 The cgraph_function_version_info has a THIS_NODE field that is the
137 corresponding cgraph_node.. */
139 static htab_t GTY((param_is (struct cgraph_function_version_info *)))
140 cgraph_fnver_htab = NULL;
142 /* Hash function for cgraph_fnver_htab. */
143 static hashval_t
144 cgraph_fnver_htab_hash (const void *ptr)
146 int uid = ((const struct cgraph_function_version_info *)ptr)->this_node->uid;
147 return (hashval_t)(uid);
150 /* eq function for cgraph_fnver_htab. */
151 static int
152 cgraph_fnver_htab_eq (const void *p1, const void *p2)
154 const struct cgraph_function_version_info *n1
155 = (const struct cgraph_function_version_info *)p1;
156 const struct cgraph_function_version_info *n2
157 = (const struct cgraph_function_version_info *)p2;
159 return n1->this_node->uid == n2->this_node->uid;
162 /* Mark as GC root all allocated nodes. */
163 static GTY(()) struct cgraph_function_version_info *
164 version_info_node = NULL;
166 /* Get the cgraph_function_version_info node corresponding to node. */
167 struct cgraph_function_version_info *
168 get_cgraph_node_version (struct cgraph_node *node)
170 struct cgraph_function_version_info *ret;
171 struct cgraph_function_version_info key;
172 key.this_node = node;
174 if (cgraph_fnver_htab == NULL)
175 return NULL;
177 ret = (struct cgraph_function_version_info *)
178 htab_find (cgraph_fnver_htab, &key);
180 return ret;
183 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
184 corresponding to cgraph_node NODE. */
185 struct cgraph_function_version_info *
186 insert_new_cgraph_node_version (struct cgraph_node *node)
188 void **slot;
190 version_info_node = NULL;
191 version_info_node = ggc_alloc_cleared_cgraph_function_version_info ();
192 version_info_node->this_node = node;
194 if (cgraph_fnver_htab == NULL)
195 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
196 cgraph_fnver_htab_eq, NULL);
198 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
199 gcc_assert (slot != NULL);
200 *slot = version_info_node;
201 return version_info_node;
204 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
205 DECL is a duplicate declaration. */
206 void
207 delete_function_version (tree decl)
209 struct cgraph_node *decl_node = cgraph_get_node (decl);
210 struct cgraph_function_version_info *decl_v = NULL;
212 if (decl_node == NULL)
213 return;
215 decl_v = get_cgraph_node_version (decl_node);
217 if (decl_v == NULL)
218 return;
220 if (decl_v->prev != NULL)
221 decl_v->prev->next = decl_v->next;
223 if (decl_v->next != NULL)
224 decl_v->next->prev = decl_v->prev;
226 if (cgraph_fnver_htab != NULL)
227 htab_remove_elt (cgraph_fnver_htab, decl_v);
229 cgraph_remove_node (decl_node);
232 /* Record that DECL1 and DECL2 are semantically identical function
233 versions. */
234 void
235 record_function_versions (tree decl1, tree decl2)
237 struct cgraph_node *decl1_node = cgraph_get_create_node (decl1);
238 struct cgraph_node *decl2_node = cgraph_get_create_node (decl2);
239 struct cgraph_function_version_info *decl1_v = NULL;
240 struct cgraph_function_version_info *decl2_v = NULL;
241 struct cgraph_function_version_info *before;
242 struct cgraph_function_version_info *after;
244 gcc_assert (decl1_node != NULL && decl2_node != NULL);
245 decl1_v = get_cgraph_node_version (decl1_node);
246 decl2_v = get_cgraph_node_version (decl2_node);
248 if (decl1_v != NULL && decl2_v != NULL)
249 return;
251 if (decl1_v == NULL)
252 decl1_v = insert_new_cgraph_node_version (decl1_node);
254 if (decl2_v == NULL)
255 decl2_v = insert_new_cgraph_node_version (decl2_node);
257 /* Chain decl2_v and decl1_v. All semantically identical versions
258 will be chained together. */
260 before = decl1_v;
261 after = decl2_v;
263 while (before->next != NULL)
264 before = before->next;
266 while (after->prev != NULL)
267 after= after->prev;
269 before->next = after;
270 after->prev = before;
273 /* Macros to access the next item in the list of free cgraph nodes and
274 edges. */
275 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
276 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
277 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
279 /* Register HOOK to be called with DATA on each removed edge. */
280 struct cgraph_edge_hook_list *
281 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
283 struct cgraph_edge_hook_list *entry;
284 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
286 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
287 entry->hook = hook;
288 entry->data = data;
289 entry->next = NULL;
290 while (*ptr)
291 ptr = &(*ptr)->next;
292 *ptr = entry;
293 return entry;
296 /* Remove ENTRY from the list of hooks called on removing edges. */
297 void
298 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
300 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
302 while (*ptr != entry)
303 ptr = &(*ptr)->next;
304 *ptr = entry->next;
305 free (entry);
308 /* Call all edge removal hooks. */
309 static void
310 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
312 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
313 while (entry)
315 entry->hook (e, entry->data);
316 entry = entry->next;
320 /* Register HOOK to be called with DATA on each removed node. */
321 struct cgraph_node_hook_list *
322 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
324 struct cgraph_node_hook_list *entry;
325 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
327 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
328 entry->hook = hook;
329 entry->data = data;
330 entry->next = NULL;
331 while (*ptr)
332 ptr = &(*ptr)->next;
333 *ptr = entry;
334 return entry;
337 /* Remove ENTRY from the list of hooks called on removing nodes. */
338 void
339 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
341 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
343 while (*ptr != entry)
344 ptr = &(*ptr)->next;
345 *ptr = entry->next;
346 free (entry);
349 /* Call all node removal hooks. */
350 static void
351 cgraph_call_node_removal_hooks (struct cgraph_node *node)
353 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
354 while (entry)
356 entry->hook (node, entry->data);
357 entry = entry->next;
361 /* Register HOOK to be called with DATA on each inserted node. */
362 struct cgraph_node_hook_list *
363 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
365 struct cgraph_node_hook_list *entry;
366 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
368 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
369 entry->hook = hook;
370 entry->data = data;
371 entry->next = NULL;
372 while (*ptr)
373 ptr = &(*ptr)->next;
374 *ptr = entry;
375 return entry;
378 /* Remove ENTRY from the list of hooks called on inserted nodes. */
379 void
380 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
382 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
384 while (*ptr != entry)
385 ptr = &(*ptr)->next;
386 *ptr = entry->next;
387 free (entry);
390 /* Call all node insertion hooks. */
391 void
392 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
394 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
395 while (entry)
397 entry->hook (node, entry->data);
398 entry = entry->next;
402 /* Register HOOK to be called with DATA on each duplicated edge. */
403 struct cgraph_2edge_hook_list *
404 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
406 struct cgraph_2edge_hook_list *entry;
407 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
409 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
410 entry->hook = hook;
411 entry->data = data;
412 entry->next = NULL;
413 while (*ptr)
414 ptr = &(*ptr)->next;
415 *ptr = entry;
416 return entry;
419 /* Remove ENTRY from the list of hooks called on duplicating edges. */
420 void
421 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
423 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
425 while (*ptr != entry)
426 ptr = &(*ptr)->next;
427 *ptr = entry->next;
428 free (entry);
431 /* Call all edge duplication hooks. */
432 void
433 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
434 struct cgraph_edge *cs2)
436 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
437 while (entry)
439 entry->hook (cs1, cs2, entry->data);
440 entry = entry->next;
444 /* Register HOOK to be called with DATA on each duplicated node. */
445 struct cgraph_2node_hook_list *
446 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
448 struct cgraph_2node_hook_list *entry;
449 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
451 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
452 entry->hook = hook;
453 entry->data = data;
454 entry->next = NULL;
455 while (*ptr)
456 ptr = &(*ptr)->next;
457 *ptr = entry;
458 return entry;
461 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
462 void
463 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
465 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
467 while (*ptr != entry)
468 ptr = &(*ptr)->next;
469 *ptr = entry->next;
470 free (entry);
473 /* Call all node duplication hooks. */
474 void
475 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
476 struct cgraph_node *node2)
478 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
479 while (entry)
481 entry->hook (node1, node2, entry->data);
482 entry = entry->next;
486 /* Allocate new callgraph node. */
488 static inline struct cgraph_node *
489 cgraph_allocate_node (void)
491 struct cgraph_node *node;
493 if (free_nodes)
495 node = free_nodes;
496 free_nodes = NEXT_FREE_NODE (node);
498 else
500 node = ggc_alloc_cleared_cgraph_node ();
501 node->uid = cgraph_max_uid++;
504 return node;
507 /* Allocate new callgraph node and insert it into basic data structures. */
509 struct cgraph_node *
510 cgraph_create_empty_node (void)
512 struct cgraph_node *node = cgraph_allocate_node ();
514 node->symbol.type = SYMTAB_FUNCTION;
515 node->frequency = NODE_FREQUENCY_NORMAL;
516 node->count_materialization_scale = REG_BR_PROB_BASE;
517 cgraph_n_nodes++;
518 return node;
521 /* Return cgraph node assigned to DECL. Create new one when needed. */
523 struct cgraph_node *
524 cgraph_create_node (tree decl)
526 struct cgraph_node *node = cgraph_create_empty_node ();
527 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
529 node->symbol.decl = decl;
530 symtab_register_node ((symtab_node) node);
532 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
534 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
535 node->next_nested = node->origin->nested;
536 node->origin->nested = node;
538 return node;
541 /* Try to find a call graph node for declaration DECL and if it does not exist,
542 create it. */
544 struct cgraph_node *
545 cgraph_get_create_node (tree decl)
547 struct cgraph_node *node;
549 node = cgraph_get_node (decl);
550 if (node)
551 return node;
553 return cgraph_create_node (decl);
556 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
557 the function body is associated with (not necessarily cgraph_node (DECL). */
559 struct cgraph_node *
560 cgraph_create_function_alias (tree alias, tree decl)
562 struct cgraph_node *alias_node;
564 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
565 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
566 alias_node = cgraph_get_create_node (alias);
567 gcc_assert (!alias_node->local.finalized);
568 alias_node->thunk.alias = decl;
569 alias_node->local.finalized = true;
570 alias_node->alias = 1;
571 return alias_node;
574 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
575 and NULL otherwise.
576 Same body aliases are output whenever the body of DECL is output,
577 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
579 struct cgraph_node *
580 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
582 struct cgraph_node *n;
583 #ifndef ASM_OUTPUT_DEF
584 /* If aliases aren't supported by the assembler, fail. */
585 return NULL;
586 #endif
587 /* Langhooks can create same body aliases of symbols not defined.
588 Those are useless. Drop them on the floor. */
589 if (cgraph_global_info_ready)
590 return NULL;
592 n = cgraph_create_function_alias (alias, decl);
593 n->same_body_alias = true;
594 if (same_body_aliases_done)
595 ipa_record_reference ((symtab_node)n, (symtab_node)cgraph_get_node (decl),
596 IPA_REF_ALIAS, NULL);
597 return n;
600 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
601 aliases DECL with an adjustments made into the first parameter.
602 See comments in thunk_adjust for detail on the parameters. */
604 struct cgraph_node *
605 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
606 tree alias, tree decl ATTRIBUTE_UNUSED,
607 bool this_adjusting,
608 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
609 tree virtual_offset,
610 tree real_alias)
612 struct cgraph_node *node;
614 node = cgraph_get_node (alias);
615 if (node)
617 gcc_assert (node->local.finalized);
618 gcc_assert (!node->alias);
619 gcc_assert (!node->thunk.thunk_p);
620 cgraph_remove_node (node);
623 node = cgraph_create_node (alias);
624 gcc_checking_assert (!virtual_offset
625 || tree_to_double_int (virtual_offset) ==
626 double_int::from_shwi (virtual_value));
627 node->thunk.fixed_offset = fixed_offset;
628 node->thunk.this_adjusting = this_adjusting;
629 node->thunk.virtual_value = virtual_value;
630 node->thunk.virtual_offset_p = virtual_offset != NULL;
631 node->thunk.alias = real_alias;
632 node->thunk.thunk_p = true;
633 node->local.finalized = true;
635 return node;
638 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
639 Return NULL if there's no such node. */
641 struct cgraph_node *
642 cgraph_node_for_asm (tree asmname)
644 /* We do not want to look at inline clones. */
645 for (symtab_node node = symtab_node_for_asm (asmname);
646 node;
647 node = node->symbol.next_sharing_asm_name)
649 cgraph_node *cn = dyn_cast <cgraph_node> (node);
650 if (cn && !cn->global.inlined_to)
651 return cn;
653 return NULL;
656 /* Returns a hash value for X (which really is a cgraph_edge). */
658 static hashval_t
659 edge_hash (const void *x)
661 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
664 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
666 static int
667 edge_eq (const void *x, const void *y)
669 return ((const struct cgraph_edge *) x)->call_stmt == y;
672 /* Add call graph edge E to call site hash of its caller. */
674 static inline void
675 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
677 void **slot;
678 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
679 e->call_stmt,
680 htab_hash_pointer (e->call_stmt),
681 INSERT);
682 gcc_assert (!*slot);
683 *slot = e;
686 /* Return the callgraph edge representing the GIMPLE_CALL statement
687 CALL_STMT. */
689 struct cgraph_edge *
690 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
692 struct cgraph_edge *e, *e2;
693 int n = 0;
695 if (node->call_site_hash)
696 return (struct cgraph_edge *)
697 htab_find_with_hash (node->call_site_hash, call_stmt,
698 htab_hash_pointer (call_stmt));
700 /* This loop may turn out to be performance problem. In such case adding
701 hashtables into call nodes with very many edges is probably best
702 solution. It is not good idea to add pointer into CALL_EXPR itself
703 because we want to make possible having multiple cgraph nodes representing
704 different clones of the same body before the body is actually cloned. */
705 for (e = node->callees; e; e = e->next_callee)
707 if (e->call_stmt == call_stmt)
708 break;
709 n++;
712 if (!e)
713 for (e = node->indirect_calls; e; e = e->next_callee)
715 if (e->call_stmt == call_stmt)
716 break;
717 n++;
720 if (n > 100)
722 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
723 for (e2 = node->callees; e2; e2 = e2->next_callee)
724 /* Skip fake edges. */
725 if (e2->call_stmt)
726 cgraph_add_edge_to_call_site_hash (e2);
727 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
728 cgraph_add_edge_to_call_site_hash (e2);
731 return e;
735 /* Change field call_stmt of edge E to NEW_STMT. */
737 void
738 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
740 tree decl;
742 if (e->caller->call_site_hash)
744 htab_remove_elt_with_hash (e->caller->call_site_hash,
745 e->call_stmt,
746 htab_hash_pointer (e->call_stmt));
749 e->call_stmt = new_stmt;
750 if (e->indirect_unknown_callee
751 && (decl = gimple_call_fndecl (new_stmt)))
753 /* Constant propagation (and possibly also inlining?) can turn an
754 indirect call into a direct one. */
755 struct cgraph_node *new_callee = cgraph_get_node (decl);
756 if (L_IPO_COMP_MODE && cgraph_pre_profiling_inlining_done)
757 new_callee = cgraph_lipo_get_resolved_node (decl);
759 gcc_checking_assert (new_callee);
760 cgraph_make_edge_direct (e, new_callee);
763 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
764 e->can_throw_external = stmt_can_throw_external (new_stmt);
765 pop_cfun ();
766 if (e->caller->call_site_hash)
767 cgraph_add_edge_to_call_site_hash (e);
770 /* Allocate a cgraph_edge structure and fill it with data according to the
771 parameters of which only CALLEE can be NULL (when creating an indirect call
772 edge). */
774 static struct cgraph_edge *
775 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
776 gimple call_stmt, gcov_type count, int freq)
778 struct cgraph_edge *edge;
780 /* LTO does not actually have access to the call_stmt since these
781 have not been loaded yet. */
782 if (call_stmt)
784 /* This is a rather expensive check possibly triggering
785 construction of call stmt hashtable. */
786 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
788 gcc_assert (is_gimple_call (call_stmt));
791 if (free_edges)
793 edge = free_edges;
794 free_edges = NEXT_FREE_EDGE (edge);
796 else
798 edge = ggc_alloc_cgraph_edge ();
799 edge->uid = cgraph_edge_max_uid++;
802 edge->aux = NULL;
803 edge->caller = caller;
804 edge->callee = callee;
805 edge->prev_caller = NULL;
806 edge->next_caller = NULL;
807 edge->prev_callee = NULL;
808 edge->next_callee = NULL;
810 edge->count = count;
811 gcc_assert (count >= 0);
812 edge->frequency = freq;
813 gcc_assert (freq >= 0);
814 gcc_assert (freq <= CGRAPH_FREQ_MAX);
816 edge->call_stmt = call_stmt;
817 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
818 edge->can_throw_external
819 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
820 pop_cfun ();
821 if (call_stmt
822 && callee && callee->symbol.decl
823 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
824 edge->call_stmt_cannot_inline_p = true;
825 else
826 edge->call_stmt_cannot_inline_p = false;
827 if (call_stmt && caller->call_site_hash)
828 cgraph_add_edge_to_call_site_hash (edge);
830 edge->indirect_info = NULL;
831 edge->indirect_inlining_edge = 0;
833 return edge;
836 /* Create edge from CALLER to CALLEE in the cgraph. */
838 struct cgraph_edge *
839 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
840 gimple call_stmt, gcov_type count, int freq)
842 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
843 count, freq);
845 edge->indirect_unknown_callee = 0;
846 initialize_inline_failed (edge);
848 edge->next_caller = callee->callers;
849 if (callee->callers)
850 callee->callers->prev_caller = edge;
851 edge->next_callee = caller->callees;
852 if (caller->callees)
853 caller->callees->prev_callee = edge;
854 caller->callees = edge;
855 callee->callers = edge;
857 return edge;
860 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
862 struct cgraph_indirect_call_info *
863 cgraph_allocate_init_indirect_info (void)
865 struct cgraph_indirect_call_info *ii;
867 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
868 ii->param_index = -1;
869 return ii;
872 /* Create an indirect edge with a yet-undetermined callee where the call
873 statement destination is a formal parameter of the caller with index
874 PARAM_INDEX. */
876 struct cgraph_edge *
877 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
878 int ecf_flags,
879 gcov_type count, int freq)
881 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
882 count, freq);
884 edge->indirect_unknown_callee = 1;
885 initialize_inline_failed (edge);
887 edge->indirect_info = cgraph_allocate_init_indirect_info ();
888 edge->indirect_info->ecf_flags = ecf_flags;
890 edge->next_callee = caller->indirect_calls;
891 if (caller->indirect_calls)
892 caller->indirect_calls->prev_callee = edge;
893 caller->indirect_calls = edge;
895 return edge;
898 /* Remove the edge E from the list of the callers of the callee. */
900 static inline void
901 cgraph_edge_remove_callee (struct cgraph_edge *e)
903 gcc_assert (!e->indirect_unknown_callee);
904 if (e->prev_caller)
905 e->prev_caller->next_caller = e->next_caller;
906 if (e->next_caller)
907 e->next_caller->prev_caller = e->prev_caller;
908 if (!e->prev_caller)
909 e->callee->callers = e->next_caller;
912 /* Remove the edge E from the list of the callees of the caller. */
914 static inline void
915 cgraph_edge_remove_caller (struct cgraph_edge *e)
917 if (e->prev_callee)
918 e->prev_callee->next_callee = e->next_callee;
919 if (e->next_callee)
920 e->next_callee->prev_callee = e->prev_callee;
921 if (!e->prev_callee)
923 if (e->indirect_unknown_callee)
924 e->caller->indirect_calls = e->next_callee;
925 else
926 e->caller->callees = e->next_callee;
928 if (e->caller->call_site_hash && e->call_stmt)
929 htab_remove_elt_with_hash (e->caller->call_site_hash,
930 e->call_stmt,
931 htab_hash_pointer (e->call_stmt));
934 /* Put the edge onto the free list. */
936 static void
937 cgraph_free_edge (struct cgraph_edge *e)
939 int uid = e->uid;
941 /* Clear out the edge so we do not dangle pointers. */
942 memset (e, 0, sizeof (*e));
943 e->uid = uid;
944 NEXT_FREE_EDGE (e) = free_edges;
945 free_edges = e;
948 /* Remove the edge E in the cgraph. */
950 void
951 cgraph_remove_edge (struct cgraph_edge *e)
953 /* Call all edge removal hooks. */
954 cgraph_call_edge_removal_hooks (e);
956 if (!e->indirect_unknown_callee)
957 /* Remove from callers list of the callee. */
958 cgraph_edge_remove_callee (e);
960 /* Remove from callees list of the callers. */
961 cgraph_edge_remove_caller (e);
963 /* Put the edge onto the free list. */
964 cgraph_free_edge (e);
967 /* Remove fake cgraph edges for indirect calls. NODE is the callee
968 of the edges. */
970 void
971 cgraph_remove_fake_indirect_call_in_edges (struct cgraph_node *node)
973 struct cgraph_edge *f, *e;
975 if (!L_IPO_COMP_MODE)
976 return;
978 for (e = node->callers; e; e = f)
980 f = e->next_caller;
981 if (!e->call_stmt)
982 cgraph_remove_edge (e);
987 /* Set callee of call graph edge E and add it to the corresponding set of
988 callers. */
990 static void
991 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
993 e->prev_caller = NULL;
994 if (n->callers)
995 n->callers->prev_caller = e;
996 e->next_caller = n->callers;
997 n->callers = e;
998 e->callee = n;
1001 /* Redirect callee of E to N. The function does not update underlying
1002 call expression. */
1004 void
1005 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1007 /* Remove from callers list of the current callee. */
1008 cgraph_edge_remove_callee (e);
1010 /* Insert to callers list of the new callee. */
1011 cgraph_set_edge_callee (e, n);
1014 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1015 CALLEE. DELTA is an integer constant that is to be added to the this
1016 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1018 void
1019 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
1021 edge->indirect_unknown_callee = 0;
1023 /* Get the edge out of the indirect edge list. */
1024 if (edge->prev_callee)
1025 edge->prev_callee->next_callee = edge->next_callee;
1026 if (edge->next_callee)
1027 edge->next_callee->prev_callee = edge->prev_callee;
1028 if (!edge->prev_callee)
1029 edge->caller->indirect_calls = edge->next_callee;
1031 /* Put it into the normal callee list */
1032 edge->prev_callee = NULL;
1033 edge->next_callee = edge->caller->callees;
1034 if (edge->caller->callees)
1035 edge->caller->callees->prev_callee = edge;
1036 edge->caller->callees = edge;
1038 /* Insert to callers list of the new callee. */
1039 cgraph_set_edge_callee (edge, callee);
1041 if (edge->call_stmt)
1042 edge->call_stmt_cannot_inline_p
1043 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
1045 /* We need to re-determine the inlining status of the edge. */
1046 initialize_inline_failed (edge);
1049 /* If necessary, change the function declaration in the call statement
1050 associated with E so that it corresponds to the edge callee. */
1052 gimple
1053 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
1055 tree decl = gimple_call_fndecl (e->call_stmt);
1056 gimple new_stmt;
1057 gimple_stmt_iterator gsi;
1058 #ifdef ENABLE_CHECKING
1059 struct cgraph_node *node;
1060 #endif
1062 if (e->indirect_unknown_callee
1063 || decl == e->callee->symbol.decl)
1064 return e->call_stmt;
1066 #ifdef ENABLE_CHECKING
1067 if (decl)
1069 node = cgraph_get_node (decl);
1070 gcc_assert (!node || !node->clone.combined_args_to_skip);
1072 #endif
1074 if (cgraph_dump_file)
1076 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
1077 xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
1078 xstrdup (cgraph_node_name (e->callee)), e->callee->uid);
1079 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1080 if (e->callee->clone.combined_args_to_skip)
1082 fprintf (cgraph_dump_file, " combined args to skip: ");
1083 dump_bitmap (cgraph_dump_file,
1084 e->callee->clone.combined_args_to_skip);
1088 if (e->callee->clone.combined_args_to_skip)
1090 int lp_nr;
1092 new_stmt
1093 = gimple_call_copy_skip_args (e->call_stmt,
1094 e->callee->clone.combined_args_to_skip);
1095 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1097 if (gimple_vdef (new_stmt)
1098 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1099 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1101 gsi = gsi_for_stmt (e->call_stmt);
1102 gsi_replace (&gsi, new_stmt, false);
1103 /* We need to defer cleaning EH info on the new statement to
1104 fixup-cfg. We may not have dominator information at this point
1105 and thus would end up with unreachable blocks and have no way
1106 to communicate that we need to run CFG cleanup then. */
1107 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1108 if (lp_nr != 0)
1110 remove_stmt_from_eh_lp (e->call_stmt);
1111 add_stmt_to_eh_lp (new_stmt, lp_nr);
1114 else
1116 new_stmt = e->call_stmt;
1117 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
1118 update_stmt (new_stmt);
1119 if (L_IPO_COMP_MODE)
1121 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1122 if (lp_nr != 0 && !stmt_could_throw_p (e->call_stmt))
1123 remove_stmt_from_eh_lp (e->call_stmt);
1127 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
1129 if (cgraph_dump_file)
1131 fprintf (cgraph_dump_file, " updated to:");
1132 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1134 return new_stmt;
1137 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1138 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1139 of OLD_STMT if it was previously call statement.
1140 If NEW_STMT is NULL, the call has been dropped without any
1141 replacement. */
1143 static void
1144 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1145 gimple old_stmt, tree old_call,
1146 gimple new_stmt)
1148 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1149 ? gimple_call_fndecl (new_stmt) : 0;
1151 /* We are seeing indirect calls, then there is nothing to update. */
1152 if (!new_call && !old_call)
1153 return;
1154 /* See if we turned indirect call into direct call or folded call to one builtin
1155 into different builtin. */
1156 if (old_call != new_call)
1158 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1159 struct cgraph_edge *ne = NULL;
1160 gcov_type count;
1161 int frequency;
1163 if (e)
1165 /* See if the edge is already there and has the correct callee. It
1166 might be so because of indirect inlining has already updated
1167 it. We also might've cloned and redirected the edge. */
1168 if (new_call && e->callee)
1170 struct cgraph_node *callee = e->callee;
1171 while (callee)
1173 if (callee->symbol.decl == new_call
1174 || callee->former_clone_of == new_call)
1175 return;
1176 callee = callee->clone_of;
1180 /* Otherwise remove edge and create new one; we can't simply redirect
1181 since function has changed, so inline plan and other information
1182 attached to edge is invalid. */
1183 count = e->count;
1184 frequency = e->frequency;
1185 cgraph_remove_edge (e);
1187 else if (new_call)
1189 /* We are seeing new direct call; compute profile info based on BB. */
1190 basic_block bb = gimple_bb (new_stmt);
1191 count = bb->count;
1192 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1193 bb);
1196 if (new_call)
1198 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1199 new_stmt, count, frequency);
1200 gcc_assert (ne->inline_failed);
1203 /* We only updated the call stmt; update pointer in cgraph edge.. */
1204 else if (old_stmt != new_stmt)
1205 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1208 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1209 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1210 of OLD_STMT before it was updated (updating can happen inplace). */
1212 void
1213 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1215 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1216 struct cgraph_node *node;
1218 gcc_checking_assert (orig);
1219 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1220 if (orig->clones)
1221 for (node = orig->clones; node != orig;)
1223 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1224 if (node->clones)
1225 node = node->clones;
1226 else if (node->next_sibling_clone)
1227 node = node->next_sibling_clone;
1228 else
1230 while (node != orig && !node->next_sibling_clone)
1231 node = node->clone_of;
1232 if (node != orig)
1233 node = node->next_sibling_clone;
1239 /* Remove all callees from the node. */
1241 void
1242 cgraph_node_remove_callees (struct cgraph_node *node)
1244 struct cgraph_edge *e, *f;
1246 /* It is sufficient to remove the edges from the lists of callers of
1247 the callees. The callee list of the node can be zapped with one
1248 assignment. */
1249 for (e = node->callees; e; e = f)
1251 f = e->next_callee;
1252 cgraph_call_edge_removal_hooks (e);
1253 if (!e->indirect_unknown_callee)
1254 cgraph_edge_remove_callee (e);
1255 cgraph_free_edge (e);
1257 for (e = node->indirect_calls; e; e = f)
1259 f = e->next_callee;
1260 cgraph_call_edge_removal_hooks (e);
1261 if (!e->indirect_unknown_callee)
1262 cgraph_edge_remove_callee (e);
1263 cgraph_free_edge (e);
1265 node->indirect_calls = NULL;
1266 node->callees = NULL;
1267 if (node->call_site_hash)
1269 htab_delete (node->call_site_hash);
1270 node->call_site_hash = NULL;
1274 /* Remove all callers from the node. */
1276 static void
1277 cgraph_node_remove_callers (struct cgraph_node *node)
1279 struct cgraph_edge *e, *f;
1281 /* It is sufficient to remove the edges from the lists of callees of
1282 the callers. The caller list of the node can be zapped with one
1283 assignment. */
1284 for (e = node->callers; e; e = f)
1286 f = e->next_caller;
1287 cgraph_call_edge_removal_hooks (e);
1288 cgraph_edge_remove_caller (e);
1289 cgraph_free_edge (e);
1291 node->callers = NULL;
1294 /* Release memory used to represent body of function NODE. */
1296 void
1297 cgraph_release_function_body (struct cgraph_node *node)
1299 if (cgraph_is_aux_decl_external (node))
1300 DECL_EXTERNAL (node->symbol.decl) = 1;
1301 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1303 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1304 if (cfun->cfg
1305 && current_loops)
1307 cfun->curr_properties &= ~PROP_loops;
1308 loop_optimizer_finalize ();
1310 if (cfun->gimple_df)
1312 delete_tree_ssa ();
1313 delete_tree_cfg_annotations ();
1314 cfun->eh = NULL;
1316 if (cfun->cfg)
1318 gcc_assert (dom_computed[0] == DOM_NONE);
1319 gcc_assert (dom_computed[1] == DOM_NONE);
1320 clear_edges ();
1322 if (cfun->value_histograms)
1323 free_histograms ();
1324 pop_cfun();
1325 gimple_set_body (node->symbol.decl, NULL);
1326 node->ipa_transforms_to_apply.release ();
1327 /* Struct function hangs a lot of data that would leak if we didn't
1328 removed all pointers to it. */
1329 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1330 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1332 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1333 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1334 of its associated function function declaration because it's
1335 needed to emit debug info later. */
1336 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1337 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1340 /* Remove the node from cgraph. */
1342 void
1343 cgraph_remove_node (struct cgraph_node *node)
1345 struct cgraph_node *n;
1346 int uid = node->uid;
1348 cgraph_call_node_removal_hooks (node);
1349 cgraph_node_remove_callers (node);
1350 cgraph_node_remove_callees (node);
1351 node->ipa_transforms_to_apply.release ();
1353 /* Incremental inlining access removed nodes stored in the postorder list.
1355 node->symbol.force_output = false;
1356 for (n = node->nested; n; n = n->next_nested)
1357 n->origin = NULL;
1358 node->nested = NULL;
1359 if (node->origin)
1361 struct cgraph_node **node2 = &node->origin->nested;
1363 while (*node2 != node)
1364 node2 = &(*node2)->next_nested;
1365 *node2 = node->next_nested;
1367 symtab_unregister_node ((symtab_node)node);
1368 if (node->prev_sibling_clone)
1369 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1370 else if (node->clone_of)
1371 node->clone_of->clones = node->next_sibling_clone;
1372 if (node->next_sibling_clone)
1373 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1374 if (node->clones)
1376 struct cgraph_node *n, *next;
1378 if (node->clone_of)
1380 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1381 n->clone_of = node->clone_of;
1382 n->clone_of = node->clone_of;
1383 n->next_sibling_clone = node->clone_of->clones;
1384 if (node->clone_of->clones)
1385 node->clone_of->clones->prev_sibling_clone = n;
1386 node->clone_of->clones = node->clones;
1388 else
1390 /* We are removing node with clones. This makes clones inconsistent,
1391 but assume they will be removed subsequently and just keep clone
1392 tree intact. This can happen in unreachable function removal since
1393 we remove unreachable functions in random order, not by bottom-up
1394 walk of clone trees. */
1395 for (n = node->clones; n; n = next)
1397 next = n->next_sibling_clone;
1398 n->next_sibling_clone = NULL;
1399 n->prev_sibling_clone = NULL;
1400 n->clone_of = NULL;
1405 /* While all the clones are removed after being proceeded, the function
1406 itself is kept in the cgraph even after it is compiled. Check whether
1407 we are done with this body and reclaim it proactively if this is the case.
1409 bool kill_body = false;
1410 n = cgraph_get_node (node->symbol.decl);
1411 if (!n
1412 || (!n->clones && !n->clone_of && !n->global.inlined_to
1413 && (cgraph_global_info_ready
1414 && (TREE_ASM_WRITTEN (n->symbol.decl)
1415 || DECL_EXTERNAL (n->symbol.decl)
1416 || !n->analyzed
1417 || n->symbol.in_other_partition))))
1418 kill_body = true;
1420 if (kill_body)
1421 cgraph_release_function_body (node);
1423 cgraph_remove_link_node (node);
1425 if (node->call_site_hash)
1427 htab_delete (node->call_site_hash);
1428 node->call_site_hash = NULL;
1430 cgraph_n_nodes--;
1432 /* Clear out the node to NULL all pointers and add the node to the free
1433 list. */
1434 memset (node, 0, sizeof(*node));
1435 node->symbol.type = SYMTAB_FUNCTION;
1436 node->uid = uid;
1437 SET_NEXT_FREE_NODE (node, free_nodes);
1438 free_nodes = node;
1442 /* Likewise indicate that a node is having address taken. */
1444 void
1445 cgraph_mark_address_taken_node (struct cgraph_node *node)
1447 gcc_assert (!node->global.inlined_to);
1448 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1449 IPA_REF_ADDR reference exists (and thus it should be set on node
1450 representing alias we take address of) and as a test whether address
1451 of the object was taken (and thus it should be set on node alias is
1452 referring to). We should remove the first use and the remove the
1453 following set. */
1454 node->symbol.address_taken = 1;
1455 node = cgraph_function_or_thunk_node (node, NULL);
1456 node->symbol.address_taken = 1;
1459 /* Return local info for the compiled function. */
1461 struct cgraph_local_info *
1462 cgraph_local_info (tree decl)
1464 struct cgraph_node *node;
1466 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1467 node = cgraph_get_node (decl);
1468 if (!node)
1469 return NULL;
1470 return &node->local;
1473 /* Return local info for the compiled function. */
1475 struct cgraph_global_info *
1476 cgraph_global_info (tree decl)
1478 struct cgraph_node *node;
1480 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1481 node = cgraph_get_node (decl);
1482 if (!node)
1483 return NULL;
1484 return &node->global;
1487 /* Return local info for the compiled function. */
1489 struct cgraph_rtl_info *
1490 cgraph_rtl_info (tree decl)
1492 struct cgraph_node *node;
1494 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1495 node = cgraph_get_node (decl);
1496 if (!node
1497 || (decl != current_function_decl
1498 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1499 return NULL;
1500 return &node->rtl;
1503 /* Return a string describing the failure REASON. */
1505 const char*
1506 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1508 #undef DEFCIFCODE
1509 #define DEFCIFCODE(code, string) string,
1511 static const char *cif_string_table[CIF_N_REASONS] = {
1512 #include "cif-code.def"
1515 /* Signedness of an enum type is implementation defined, so cast it
1516 to unsigned before testing. */
1517 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1518 return cif_string_table[reason];
1521 /* Names used to print out the availability enum. */
1522 const char * const cgraph_availability_names[] =
1523 {"unset", "not_available", "overwritable", "available", "local"};
1526 /* Dump call graph node NODE to file F. */
1528 void
1529 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1531 struct cgraph_edge *edge;
1532 int indirect_calls_count = 0;
1534 dump_symtab_base (f, (symtab_node) node);
1536 if (node->global.inlined_to)
1537 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1538 xstrdup (cgraph_node_name (node)),
1539 node->symbol.order,
1540 xstrdup (cgraph_node_name (node->global.inlined_to)),
1541 node->global.inlined_to->symbol.order);
1542 if (node->clone_of)
1543 fprintf (f, " Clone of %s/%i\n",
1544 cgraph_node_asm_name (node->clone_of),
1545 node->clone_of->symbol.order);
1546 if (cgraph_function_flags_ready)
1547 fprintf (f, " Availability: %s\n",
1548 cgraph_availability_names [cgraph_function_body_availability (node)]);
1550 fprintf (f, " Function flags:");
1551 if (node->analyzed)
1552 fprintf (f, " analyzed");
1553 if (node->count)
1554 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1555 (HOST_WIDEST_INT)node->count);
1556 if (node->origin)
1557 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1558 if (gimple_has_body_p (node->symbol.decl))
1559 fprintf (f, " body");
1560 if (node->process)
1561 fprintf (f, " process");
1562 if (node->local.local)
1563 fprintf (f, " local");
1564 if (node->local.finalized)
1565 fprintf (f, " finalized");
1566 if (node->local.redefined_extern_inline)
1567 fprintf (f, " redefined_extern_inline");
1568 if (node->only_called_at_startup)
1569 fprintf (f, " only_called_at_startup");
1570 if (node->only_called_at_exit)
1571 fprintf (f, " only_called_at_exit");
1572 else if (node->alias)
1573 fprintf (f, " alias");
1574 if (node->tm_clone)
1575 fprintf (f, " tm_clone");
1577 fprintf (f, "\n");
1579 if (node->thunk.thunk_p)
1581 fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1582 "virtual offset %i)\n",
1583 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1584 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1585 (int)node->thunk.fixed_offset,
1586 (int)node->thunk.virtual_value,
1587 (int)node->thunk.virtual_offset_p);
1589 if (node->alias && node->thunk.alias)
1591 fprintf (f, " Alias of %s",
1592 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1593 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1594 fprintf (f, " (asm: %s)",
1595 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1596 fprintf (f, "\n");
1599 fprintf (f, " Called by: ");
1601 for (edge = node->callers; edge; edge = edge->next_caller)
1603 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1604 edge->caller->symbol.order);
1605 if (edge->count)
1606 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1607 (HOST_WIDEST_INT)edge->count);
1608 if (edge->frequency)
1609 fprintf (f, "(%.2f per call) ",
1610 edge->frequency / (double)CGRAPH_FREQ_BASE);
1611 if (!edge->inline_failed)
1612 fprintf(f, "(inlined) ");
1613 if (edge->indirect_inlining_edge)
1614 fprintf(f, "(indirect_inlining) ");
1615 if (edge->can_throw_external)
1616 fprintf(f, "(can throw external) ");
1619 fprintf (f, "\n Calls: ");
1620 for (edge = node->callees; edge; edge = edge->next_callee)
1622 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1623 edge->callee->symbol.order);
1624 if (!edge->inline_failed)
1625 fprintf(f, "(inlined) ");
1626 if (edge->indirect_inlining_edge)
1627 fprintf(f, "(indirect_inlining) ");
1628 if (edge->count)
1629 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1630 (HOST_WIDEST_INT)edge->count);
1631 if (edge->frequency)
1632 fprintf (f, "(%.2f per call) ",
1633 edge->frequency / (double)CGRAPH_FREQ_BASE);
1634 if (edge->can_throw_external)
1635 fprintf(f, "(can throw external) ");
1637 fprintf (f, "\n");
1639 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1640 indirect_calls_count++;
1641 if (indirect_calls_count)
1642 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1643 indirect_calls_count);
1647 /* Dump call graph node NODE to stderr. */
1649 DEBUG_FUNCTION void
1650 debug_cgraph_node (struct cgraph_node *node)
1652 dump_cgraph_node (stderr, node);
1656 /* Dump the callgraph to file F. */
1658 void
1659 dump_cgraph (FILE *f)
1661 struct cgraph_node *node;
1663 fprintf (f, "callgraph:\n\n");
1664 FOR_EACH_FUNCTION (node)
1665 dump_cgraph_node (f, node);
1669 /* Dump the call graph to stderr. */
1671 DEBUG_FUNCTION void
1672 debug_cgraph (void)
1674 dump_cgraph (stderr);
1677 /* Return true when the DECL can possibly be inlined. */
1678 bool
1679 cgraph_function_possibly_inlined_p (tree decl)
1681 if (!cgraph_global_info_ready)
1682 return !DECL_UNINLINABLE (decl);
1683 return DECL_POSSIBLY_INLINED (decl);
1686 /* NODE is no longer nested function; update cgraph accordingly. */
1687 void
1688 cgraph_unnest_node (struct cgraph_node *node)
1690 struct cgraph_node **node2 = &node->origin->nested;
1691 gcc_assert (node->origin);
1693 while (*node2 != node)
1694 node2 = &(*node2)->next_nested;
1695 *node2 = node->next_nested;
1696 node->origin = NULL;
1699 /* Return function availability. See cgraph.h for description of individual
1700 return values. */
1701 enum availability
1702 cgraph_function_body_availability (struct cgraph_node *node)
1704 enum availability avail;
1705 gcc_assert (cgraph_function_flags_ready);
1706 if (!node->analyzed)
1707 avail = AVAIL_NOT_AVAILABLE;
1708 else if (node->local.local)
1709 avail = AVAIL_LOCAL;
1710 else if (!node->symbol.externally_visible)
1711 avail = AVAIL_AVAILABLE;
1712 /* Inline functions are safe to be analyzed even if their symbol can
1713 be overwritten at runtime. It is not meaningful to enforce any sane
1714 behaviour on replacing inline function by different body. */
1715 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1716 avail = AVAIL_AVAILABLE;
1718 /* If the function can be overwritten, return OVERWRITABLE. Take
1719 care at least of two notable extensions - the COMDAT functions
1720 used to share template instantiations in C++ (this is symmetric
1721 to code cp_cannot_inline_tree_fn and probably shall be shared and
1722 the inlinability hooks completely eliminated).
1724 ??? Does the C++ one definition rule allow us to always return
1725 AVAIL_AVAILABLE here? That would be good reason to preserve this
1726 bit. */
1728 else if (decl_replaceable_p (node->symbol.decl)
1729 && !DECL_EXTERNAL (node->symbol.decl))
1730 avail = AVAIL_OVERWRITABLE;
1731 else avail = AVAIL_AVAILABLE;
1733 return avail;
1736 /* Worker for cgraph_node_can_be_local_p. */
1737 static bool
1738 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1739 void *data ATTRIBUTE_UNUSED)
1741 return !(!node->symbol.force_output
1742 && ((DECL_COMDAT (node->symbol.decl)
1743 && !node->symbol.same_comdat_group)
1744 || !node->symbol.externally_visible));
1747 /* Return true if NODE can be made local for API change.
1748 Extern inline functions and C++ COMDAT functions can be made local
1749 at the expense of possible code size growth if function is used in multiple
1750 compilation units. */
1751 bool
1752 cgraph_node_can_be_local_p (struct cgraph_node *node)
1754 return (!node->symbol.address_taken
1755 && !cgraph_for_node_and_aliases (node,
1756 cgraph_node_cannot_be_local_p_1,
1757 NULL, true));
1760 /* Call calback on NODE, thunks and aliases associated to NODE.
1761 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1762 skipped. */
1764 bool
1765 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1766 bool (*callback) (struct cgraph_node *, void *),
1767 void *data,
1768 bool include_overwritable)
1770 struct cgraph_edge *e;
1771 int i;
1772 struct ipa_ref *ref;
1774 if (callback (node, data))
1775 return true;
1776 for (e = node->callers; e; e = e->next_caller)
1777 if (e->caller->thunk.thunk_p
1778 && (include_overwritable
1779 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1780 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1781 include_overwritable))
1782 return true;
1783 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1784 if (ref->use == IPA_REF_ALIAS)
1786 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1787 if (include_overwritable
1788 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1789 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1790 include_overwritable))
1791 return true;
1793 return false;
1796 /* Call calback on NODE and aliases associated to NODE.
1797 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1798 skipped. */
1800 bool
1801 cgraph_for_node_and_aliases (struct cgraph_node *node,
1802 bool (*callback) (struct cgraph_node *, void *),
1803 void *data,
1804 bool include_overwritable)
1806 int i;
1807 struct ipa_ref *ref;
1809 if (callback (node, data))
1810 return true;
1811 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1812 if (ref->use == IPA_REF_ALIAS)
1814 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1815 if (include_overwritable
1816 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1817 if (cgraph_for_node_and_aliases (alias, callback, data,
1818 include_overwritable))
1819 return true;
1821 return false;
1824 /* Worker to bring NODE local. */
1826 static bool
1827 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1829 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1830 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1832 symtab_make_decl_local (node->symbol.decl);
1834 node->symbol.externally_visible = false;
1835 node->local.local = true;
1836 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1837 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1839 return false;
1842 /* Bring NODE local. */
1844 void
1845 cgraph_make_node_local (struct cgraph_node *node)
1847 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1848 NULL, true);
1851 /* Worker to set nothrow flag. */
1853 static bool
1854 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1856 struct cgraph_edge *e;
1858 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1860 if (data != NULL)
1861 for (e = node->callers; e; e = e->next_caller)
1862 e->can_throw_external = false;
1863 return false;
1866 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1867 if any to NOTHROW. */
1869 void
1870 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1872 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1873 (void *)(size_t)nothrow, false);
1876 /* Worker to set const flag. */
1878 static bool
1879 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1881 /* Static constructors and destructors without a side effect can be
1882 optimized out. */
1883 if (data && !((size_t)data & 2))
1885 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1886 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1887 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1888 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1890 TREE_READONLY (node->symbol.decl) = data != NULL;
1891 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1892 return false;
1895 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1896 if any to READONLY. */
1898 void
1899 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1901 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1902 (void *)(size_t)(readonly + (int)looping * 2),
1903 false);
1906 /* Worker to set pure flag. */
1908 static bool
1909 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1911 /* Static constructors and destructors without a side effect can be
1912 optimized out. */
1913 if (data && !((size_t)data & 2))
1915 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1916 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1917 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1918 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1920 DECL_PURE_P (node->symbol.decl) = data != NULL;
1921 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1922 return false;
1925 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1926 if any to PURE. */
1928 void
1929 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1931 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1932 (void *)(size_t)(pure + (int)looping * 2),
1933 false);
1936 /* Data used by cgraph_propagate_frequency. */
1938 struct cgraph_propagate_frequency_data
1940 bool maybe_unlikely_executed;
1941 bool maybe_executed_once;
1942 bool only_called_at_startup;
1943 bool only_called_at_exit;
1946 /* Worker for cgraph_propagate_frequency_1. */
1948 static bool
1949 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1951 struct cgraph_propagate_frequency_data *d;
1952 struct cgraph_edge *edge;
1954 d = (struct cgraph_propagate_frequency_data *)data;
1955 for (edge = node->callers;
1956 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1957 || d->only_called_at_startup || d->only_called_at_exit);
1958 edge = edge->next_caller)
1960 if (edge->caller != node)
1962 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1963 /* It makes sense to put main() together with the static constructors.
1964 It will be executed for sure, but rest of functions called from
1965 main are definitely not at startup only. */
1966 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1967 d->only_called_at_startup = 0;
1968 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1970 if (!edge->frequency)
1971 continue;
1972 switch (edge->caller->frequency)
1974 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1975 break;
1976 case NODE_FREQUENCY_EXECUTED_ONCE:
1977 if (dump_file && (dump_flags & TDF_DETAILS))
1978 fprintf (dump_file, " Called by %s that is executed once\n",
1979 cgraph_node_name (edge->caller));
1980 d->maybe_unlikely_executed = false;
1981 if (inline_edge_summary (edge)->loop_depth)
1983 d->maybe_executed_once = false;
1984 if (dump_file && (dump_flags & TDF_DETAILS))
1985 fprintf (dump_file, " Called in loop\n");
1987 break;
1988 case NODE_FREQUENCY_HOT:
1989 case NODE_FREQUENCY_NORMAL:
1990 if (dump_file && (dump_flags & TDF_DETAILS))
1991 fprintf (dump_file, " Called by %s that is normal or hot\n",
1992 cgraph_node_name (edge->caller));
1993 d->maybe_unlikely_executed = false;
1994 d->maybe_executed_once = false;
1995 break;
1998 return edge != NULL;
2001 /* See if the frequency of NODE can be updated based on frequencies of its
2002 callers. */
2003 bool
2004 cgraph_propagate_frequency (struct cgraph_node *node)
2006 struct cgraph_propagate_frequency_data d = {true, true, true, true};
2007 bool changed = false;
2009 if (!node->local.local)
2010 return false;
2011 gcc_assert (node->analyzed);
2012 if (dump_file && (dump_flags & TDF_DETAILS))
2013 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2015 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2017 if ((d.only_called_at_startup && !d.only_called_at_exit)
2018 && !node->only_called_at_startup)
2020 node->only_called_at_startup = true;
2021 if (dump_file)
2022 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2023 cgraph_node_name (node));
2024 changed = true;
2026 if ((d.only_called_at_exit && !d.only_called_at_startup)
2027 && !node->only_called_at_exit)
2029 node->only_called_at_exit = true;
2030 if (dump_file)
2031 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2032 cgraph_node_name (node));
2033 changed = true;
2035 /* These come either from profile or user hints; never update them. */
2036 if (node->frequency == NODE_FREQUENCY_HOT
2037 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2038 return changed;
2039 if (d.maybe_unlikely_executed)
2041 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2042 if (dump_file)
2043 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2044 cgraph_node_name (node));
2045 changed = true;
2047 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2049 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2050 if (dump_file)
2051 fprintf (dump_file, "Node %s promoted to executed once.\n",
2052 cgraph_node_name (node));
2053 changed = true;
2055 return changed;
2058 /* Return true when NODE can not return or throw and thus
2059 it is safe to ignore its side effects for IPA analysis. */
2061 bool
2062 cgraph_node_cannot_return (struct cgraph_node *node)
2064 int flags = flags_from_decl_or_type (node->symbol.decl);
2065 if (!flag_exceptions)
2066 return (flags & ECF_NORETURN) != 0;
2067 else
2068 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2069 == (ECF_NORETURN | ECF_NOTHROW));
2072 /* Return true when call of E can not lead to return from caller
2073 and thus it is safe to ignore its side effects for IPA analysis
2074 when computing side effects of the caller.
2075 FIXME: We could actually mark all edges that have no reaching
2076 patch to EXIT_BLOCK_PTR or throw to get better results. */
2077 bool
2078 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2080 if (cgraph_node_cannot_return (e->caller))
2081 return true;
2082 if (e->indirect_unknown_callee)
2084 int flags = e->indirect_info->ecf_flags;
2085 if (!flag_exceptions)
2086 return (flags & ECF_NORETURN) != 0;
2087 else
2088 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2089 == (ECF_NORETURN | ECF_NOTHROW));
2091 else
2092 return cgraph_node_cannot_return (e->callee);
2095 /* Return true when function NODE can be removed from callgraph
2096 if all direct calls are eliminated. */
2098 bool
2099 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2101 gcc_assert (!node->global.inlined_to);
2102 /* Extern inlines can always go, we will use the external definition. */
2103 if (DECL_EXTERNAL (node->symbol.decl))
2104 return true;
2105 /* When function is needed, we can not remove it. */
2106 if (node->symbol.force_output || node->symbol.used_from_other_partition)
2107 return false;
2108 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
2109 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
2110 return false;
2111 /* Only COMDAT functions can be removed if externally visible. */
2112 if (node->symbol.externally_visible
2113 && (!DECL_COMDAT (node->symbol.decl)
2114 || symtab_used_from_object_file_p ((symtab_node) node)))
2115 return false;
2116 return true;
2119 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2121 static bool
2122 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2124 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
2127 /* Return true when function NODE and its aliases can be removed from callgraph
2128 if all direct calls are eliminated. */
2130 bool
2131 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
2133 /* Extern inlines can always go, we will use the external definition. */
2134 if (DECL_EXTERNAL (node->symbol.decl))
2135 return true;
2136 if (node->symbol.address_taken)
2137 return false;
2138 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
2141 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2143 static bool
2144 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2146 return symtab_used_from_object_file_p ((symtab_node) node);
2149 /* Return true when function NODE can be expected to be removed
2150 from program when direct calls in this compilation unit are removed.
2152 As a special case COMDAT functions are
2153 cgraph_can_remove_if_no_direct_calls_p while the are not
2154 cgraph_only_called_directly_p (it is possible they are called from other
2155 unit)
2157 This function behaves as cgraph_only_called_directly_p because eliminating
2158 all uses of COMDAT function does not make it necessarily disappear from
2159 the program unless we are compiling whole program or we do LTO. In this
2160 case we know we win since dynamic linking will not really discard the
2161 linkonce section. */
2163 bool
2164 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2166 gcc_assert (!node->global.inlined_to);
2167 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
2168 return false;
2169 if (!in_lto_p && !flag_whole_program)
2170 return cgraph_only_called_directly_p (node);
2171 else
2173 if (DECL_EXTERNAL (node->symbol.decl))
2174 return true;
2175 return cgraph_can_remove_if_no_direct_calls_p (node);
2180 /* Worker for cgraph_only_called_directly_p. */
2182 static bool
2183 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2185 return !cgraph_only_called_directly_or_aliased_p (node);
2188 /* Return true when function NODE and all its aliases are only called
2189 directly.
2190 i.e. it is not externally visible, address was not taken and
2191 it is not used in any other non-standard way. */
2193 bool
2194 cgraph_only_called_directly_p (struct cgraph_node *node)
2196 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2197 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2198 NULL, true);
2202 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2204 static bool
2205 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2207 vec<cgraph_edge_p> *redirect_callers = (vec<cgraph_edge_p> *)data;
2208 struct cgraph_edge *cs;
2209 enum availability avail;
2210 cgraph_function_or_thunk_node (node, &avail);
2212 if (avail > AVAIL_OVERWRITABLE)
2213 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2214 if (!cs->indirect_inlining_edge)
2215 redirect_callers->safe_push (cs);
2216 return false;
2219 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2220 (i.e. are not overwritable). */
2222 vec<cgraph_edge_p>
2223 collect_callers_of_node (struct cgraph_node *node)
2225 vec<cgraph_edge_p> redirect_callers = vNULL;
2226 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2227 &redirect_callers, false);
2228 return redirect_callers;
2231 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2232 static bool
2233 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2235 node = cgraph_function_or_thunk_node (node, NULL);
2236 node2 = cgraph_function_or_thunk_node (node2, NULL);
2237 while (node != node2 && node2)
2238 node2 = node2->clone_of;
2239 return node2 != NULL;
2242 /* Verify edge E count and frequency. */
2244 static bool
2245 verify_edge_count_and_frequency (struct cgraph_edge *e)
2247 bool error_found = false;
2248 if (e->count < 0)
2250 error ("caller edge count is negative");
2251 error_found = true;
2253 if (e->frequency < 0)
2255 error ("caller edge frequency is negative");
2256 error_found = true;
2258 if (e->frequency > CGRAPH_FREQ_MAX)
2260 error ("caller edge frequency is too large");
2261 error_found = true;
2263 if (gimple_has_body_p (e->caller->symbol.decl)
2264 && e->call_stmt
2265 && !e->caller->global.inlined_to
2266 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2267 Remove this once edges are actually removed from the function at that time. */
2268 && (e->frequency
2269 || (inline_edge_summary_vec.exists ()
2270 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2271 || !inline_edge_summary (e)->predicate)))
2272 && (e->frequency
2273 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2274 gimple_bb (e->call_stmt))))
2276 error ("caller edge frequency %i does not match BB frequency %i",
2277 e->frequency,
2278 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2279 gimple_bb (e->call_stmt)));
2280 error_found = true;
2282 return error_found;
2285 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2286 static void
2287 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2289 bool fndecl_was_null = false;
2290 /* debug_gimple_stmt needs correct cfun */
2291 if (cfun != this_cfun)
2292 set_cfun (this_cfun);
2293 /* ...and an actual current_function_decl */
2294 if (!current_function_decl)
2296 current_function_decl = this_cfun->decl;
2297 fndecl_was_null = true;
2299 debug_gimple_stmt (stmt);
2300 if (fndecl_was_null)
2301 current_function_decl = NULL;
2304 /* Verify that call graph edge E corresponds to DECL from the associated
2305 statement. Return true if the verification should fail. */
2307 static bool
2308 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2310 struct cgraph_node *node;
2312 if (!decl || e->callee->global.inlined_to)
2313 return false;
2314 node = cgraph_get_node (decl);
2316 /* We do not know if a node from a different partition is an alias or what it
2317 aliases and therefore cannot do the former_clone_of check reliably. */
2318 if (!node || node->symbol.in_other_partition)
2319 return false;
2320 node = cgraph_function_or_thunk_node (node, NULL);
2322 if ((e->callee->former_clone_of != node->symbol.decl
2323 && (!node->same_body_alias
2324 || e->callee->former_clone_of != node->thunk.alias))
2325 /* IPA-CP sometimes redirect edge to clone and then back to the former
2326 function. This ping-pong has to go, eventually. */
2327 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2328 && !clone_of_p (node, e->callee)
2329 /* If decl is a same body alias of some other decl, allow e->callee to be
2330 a clone of a clone of that other decl too. */
2331 && (!node->same_body_alias
2332 || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee)))
2333 return true;
2334 else
2335 return false;
2338 /* Verify cgraph nodes of given cgraph node. */
2339 DEBUG_FUNCTION void
2340 verify_cgraph_node (struct cgraph_node *node)
2342 struct cgraph_edge *e;
2343 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2344 basic_block this_block;
2345 gimple_stmt_iterator gsi;
2346 bool error_found = false;
2348 if (seen_error ())
2349 return;
2351 timevar_push (TV_CGRAPH_VERIFY);
2352 error_found |= verify_symtab_base ((symtab_node) node);
2353 for (e = node->callees; e; e = e->next_callee)
2354 if (e->aux)
2356 error ("aux field set for edge %s->%s",
2357 identifier_to_locale (cgraph_node_name (e->caller)),
2358 identifier_to_locale (cgraph_node_name (e->callee)));
2359 error_found = true;
2361 if (node->count < 0)
2363 error ("execution count is negative");
2364 error_found = true;
2366 if (node->global.inlined_to && node->symbol.same_comdat_group)
2368 error ("inline clone in same comdat group list");
2369 error_found = true;
2371 if (node->global.inlined_to && node->symbol.externally_visible)
2373 error ("externally visible inline clone");
2374 error_found = true;
2376 if (node->global.inlined_to && node->symbol.address_taken)
2378 error ("inline clone with address taken");
2379 error_found = true;
2381 if (node->global.inlined_to && node->symbol.force_output)
2383 error ("inline clone is forced to output");
2384 error_found = true;
2386 for (e = node->indirect_calls; e; e = e->next_callee)
2388 if (e->aux)
2390 error ("aux field set for indirect edge from %s",
2391 identifier_to_locale (cgraph_node_name (e->caller)));
2392 error_found = true;
2394 if (!e->indirect_unknown_callee
2395 || !e->indirect_info)
2397 error ("An indirect edge from %s is not marked as indirect or has "
2398 "associated indirect_info, the corresponding statement is: ",
2399 identifier_to_locale (cgraph_node_name (e->caller)));
2400 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2401 error_found = true;
2404 for (e = node->callers; e; e = e->next_caller)
2406 if (verify_edge_count_and_frequency (e))
2407 error_found = true;
2408 if (!e->inline_failed)
2410 if (node->global.inlined_to
2411 != (e->caller->global.inlined_to
2412 ? e->caller->global.inlined_to : e->caller))
2414 error ("inlined_to pointer is wrong");
2415 error_found = true;
2417 if (node->callers->next_caller)
2419 error ("multiple inline callers");
2420 error_found = true;
2423 else
2424 if (node->global.inlined_to)
2426 error ("inlined_to pointer set for noninline callers");
2427 error_found = true;
2430 for (e = node->indirect_calls; e; e = e->next_callee)
2431 if (verify_edge_count_and_frequency (e))
2432 error_found = true;
2433 if (!node->callers && node->global.inlined_to)
2435 error ("inlined_to pointer is set but no predecessors found");
2436 error_found = true;
2438 if (node->global.inlined_to == node)
2440 error ("inlined_to pointer refers to itself");
2441 error_found = true;
2444 if (node->clone_of)
2446 struct cgraph_node *n;
2447 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2448 if (n == node)
2449 break;
2450 if (!n)
2452 error ("node has wrong clone_of");
2453 error_found = true;
2456 if (node->clones)
2458 struct cgraph_node *n;
2459 for (n = node->clones; n; n = n->next_sibling_clone)
2460 if (n->clone_of != node)
2461 break;
2462 if (n)
2464 error ("node has wrong clone list");
2465 error_found = true;
2468 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2470 error ("node is in clone list but it is not clone");
2471 error_found = true;
2473 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2475 error ("node has wrong prev_clone pointer");
2476 error_found = true;
2478 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2480 error ("double linked list of clones corrupted");
2481 error_found = true;
2484 if (node->analyzed && node->alias)
2486 bool ref_found = false;
2487 int i;
2488 struct ipa_ref *ref;
2490 if (node->callees)
2492 error ("Alias has call edges");
2493 error_found = true;
2495 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2496 i, ref); i++)
2497 if (ref->use != IPA_REF_ALIAS)
2499 error ("Alias has non-alias reference");
2500 error_found = true;
2502 else if (ref_found
2503 /* in LIPO mode, the alias can refer to the real target also */
2504 && !L_IPO_COMP_MODE)
2506 error ("Alias has more than one alias reference");
2507 error_found = true;
2509 else
2510 ref_found = true;
2511 if (!ref_found)
2513 error ("Analyzed alias has no reference");
2514 error_found = true;
2517 if (node->analyzed && node->thunk.thunk_p)
2519 if (!node->callees)
2521 error ("No edge out of thunk node");
2522 error_found = true;
2524 else if (node->callees->next_callee)
2526 error ("More than one edge out of thunk node");
2527 error_found = true;
2529 if (gimple_has_body_p (node->symbol.decl))
2531 error ("Thunk is not supposed to have body");
2532 error_found = true;
2535 else if (node->analyzed && gimple_has_body_p (node->symbol.decl)
2536 && !TREE_ASM_WRITTEN (node->symbol.decl)
2537 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2538 && !flag_wpa)
2540 if (this_cfun->cfg)
2542 /* Reach the trees by walking over the CFG, and note the
2543 enclosing basic-blocks in the call edges. */
2544 FOR_EACH_BB_FN (this_block, this_cfun)
2545 for (gsi = gsi_start_bb (this_block);
2546 !gsi_end_p (gsi);
2547 gsi_next (&gsi))
2549 gimple stmt = gsi_stmt (gsi);
2550 if (is_gimple_call (stmt))
2552 struct cgraph_edge *e = cgraph_edge (node, stmt);
2553 tree decl = gimple_call_fndecl (stmt);
2554 if (e)
2556 if (e->aux)
2558 error ("shared call_stmt:");
2559 cgraph_debug_gimple_stmt (this_cfun, stmt);
2560 error_found = true;
2562 if (!e->indirect_unknown_callee)
2564 if (verify_edge_corresponds_to_fndecl (e, decl))
2566 error ("edge points to wrong declaration:");
2567 debug_tree (e->callee->symbol.decl);
2568 fprintf (stderr," Instead of:");
2569 debug_tree (decl);
2570 error_found = true;
2573 else if (decl)
2575 error ("an indirect edge with unknown callee "
2576 "corresponding to a call_stmt with "
2577 "a known declaration:");
2578 error_found = true;
2579 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2581 e->aux = (void *)1;
2583 else if (decl)
2585 error ("missing callgraph edge for call stmt:");
2586 cgraph_debug_gimple_stmt (this_cfun, stmt);
2587 error_found = true;
2592 else
2593 /* No CFG available?! */
2594 gcc_unreachable ();
2596 for (e = node->callees; e; e = e->next_callee)
2598 if (!e->aux && e->call_stmt)
2600 error ("edge %s->%s has no corresponding call_stmt",
2601 identifier_to_locale (cgraph_node_name (e->caller)),
2602 identifier_to_locale (cgraph_node_name (e->callee)));
2603 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2604 error_found = true;
2606 e->aux = 0;
2608 for (e = node->indirect_calls; e; e = e->next_callee)
2610 if (!e->aux)
2612 error ("an indirect edge from %s has no corresponding call_stmt",
2613 identifier_to_locale (cgraph_node_name (e->caller)));
2614 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2615 error_found = true;
2617 e->aux = 0;
2620 if (error_found)
2622 dump_cgraph_node (stderr, node);
2623 internal_error ("verify_cgraph_node failed");
2625 timevar_pop (TV_CGRAPH_VERIFY);
2628 /* Verify whole cgraph structure. */
2629 DEBUG_FUNCTION void
2630 verify_cgraph (void)
2632 struct cgraph_node *node;
2634 if (seen_error ())
2635 return;
2637 FOR_EACH_FUNCTION (node)
2638 verify_cgraph_node (node);
2640 #include "gt-cgraph.h"