* config/rs6000/rs6000.c (rs6000_option_override_internal): Do not
[official-gcc.git] / gcc / cgraph.c
blob6e7b7c4fa3d034dfb27828d0fd9e6ee65324921e
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
24 The call-graph is a data structure designed for intra-procedural optimization.
25 It represents a multi-graph where nodes are functions and edges are call sites. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "tree-inline.h"
33 #include "langhooks.h"
34 #include "hashtab.h"
35 #include "toplev.h"
36 #include "flags.h"
37 #include "ggc.h"
38 #include "debug.h"
39 #include "target.h"
40 #include "basic-block.h"
41 #include "cgraph.h"
42 #include "intl.h"
43 #include "gimple.h"
44 #include "timevar.h"
45 #include "dumpfile.h"
46 #include "tree-flow.h"
47 #include "value-prof.h"
48 #include "except.h"
49 #include "diagnostic-core.h"
50 #include "rtl.h"
51 #include "ipa-utils.h"
52 #include "lto-streamer.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 /* Macros to access the next item in the list of free cgraph nodes and
136 edges. */
137 #define NEXT_FREE_NODE(NODE) cgraph ((NODE)->symbol.next)
138 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->symbol.next = (symtab_node)NODE2
139 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
141 /* Register HOOK to be called with DATA on each removed edge. */
142 struct cgraph_edge_hook_list *
143 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
145 struct cgraph_edge_hook_list *entry;
146 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
148 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
149 entry->hook = hook;
150 entry->data = data;
151 entry->next = NULL;
152 while (*ptr)
153 ptr = &(*ptr)->next;
154 *ptr = entry;
155 return entry;
158 /* Remove ENTRY from the list of hooks called on removing edges. */
159 void
160 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
162 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
164 while (*ptr != entry)
165 ptr = &(*ptr)->next;
166 *ptr = entry->next;
167 free (entry);
170 /* Call all edge removal hooks. */
171 static void
172 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
174 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
175 while (entry)
177 entry->hook (e, entry->data);
178 entry = entry->next;
182 /* Register HOOK to be called with DATA on each removed node. */
183 struct cgraph_node_hook_list *
184 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
186 struct cgraph_node_hook_list *entry;
187 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
189 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
190 entry->hook = hook;
191 entry->data = data;
192 entry->next = NULL;
193 while (*ptr)
194 ptr = &(*ptr)->next;
195 *ptr = entry;
196 return entry;
199 /* Remove ENTRY from the list of hooks called on removing nodes. */
200 void
201 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
203 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
205 while (*ptr != entry)
206 ptr = &(*ptr)->next;
207 *ptr = entry->next;
208 free (entry);
211 /* Call all node removal hooks. */
212 static void
213 cgraph_call_node_removal_hooks (struct cgraph_node *node)
215 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
216 while (entry)
218 entry->hook (node, entry->data);
219 entry = entry->next;
223 /* Register HOOK to be called with DATA on each inserted node. */
224 struct cgraph_node_hook_list *
225 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
227 struct cgraph_node_hook_list *entry;
228 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
230 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
231 entry->hook = hook;
232 entry->data = data;
233 entry->next = NULL;
234 while (*ptr)
235 ptr = &(*ptr)->next;
236 *ptr = entry;
237 return entry;
240 /* Remove ENTRY from the list of hooks called on inserted nodes. */
241 void
242 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
244 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
246 while (*ptr != entry)
247 ptr = &(*ptr)->next;
248 *ptr = entry->next;
249 free (entry);
252 /* Call all node insertion hooks. */
253 void
254 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
256 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
257 while (entry)
259 entry->hook (node, entry->data);
260 entry = entry->next;
264 /* Register HOOK to be called with DATA on each duplicated edge. */
265 struct cgraph_2edge_hook_list *
266 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
268 struct cgraph_2edge_hook_list *entry;
269 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
271 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
272 entry->hook = hook;
273 entry->data = data;
274 entry->next = NULL;
275 while (*ptr)
276 ptr = &(*ptr)->next;
277 *ptr = entry;
278 return entry;
281 /* Remove ENTRY from the list of hooks called on duplicating edges. */
282 void
283 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
285 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
287 while (*ptr != entry)
288 ptr = &(*ptr)->next;
289 *ptr = entry->next;
290 free (entry);
293 /* Call all edge duplication hooks. */
294 void
295 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
296 struct cgraph_edge *cs2)
298 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
299 while (entry)
301 entry->hook (cs1, cs2, entry->data);
302 entry = entry->next;
306 /* Register HOOK to be called with DATA on each duplicated node. */
307 struct cgraph_2node_hook_list *
308 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
310 struct cgraph_2node_hook_list *entry;
311 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
313 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
314 entry->hook = hook;
315 entry->data = data;
316 entry->next = NULL;
317 while (*ptr)
318 ptr = &(*ptr)->next;
319 *ptr = entry;
320 return entry;
323 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
324 void
325 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
327 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
329 while (*ptr != entry)
330 ptr = &(*ptr)->next;
331 *ptr = entry->next;
332 free (entry);
335 /* Call all node duplication hooks. */
336 void
337 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
338 struct cgraph_node *node2)
340 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
341 while (entry)
343 entry->hook (node1, node2, entry->data);
344 entry = entry->next;
348 /* Allocate new callgraph node. */
350 static inline struct cgraph_node *
351 cgraph_allocate_node (void)
353 struct cgraph_node *node;
355 if (free_nodes)
357 node = free_nodes;
358 free_nodes = NEXT_FREE_NODE (node);
360 else
362 node = ggc_alloc_cleared_cgraph_node ();
363 node->uid = cgraph_max_uid++;
366 return node;
369 /* Allocate new callgraph node and insert it into basic data structures. */
371 struct cgraph_node *
372 cgraph_create_empty_node (void)
374 struct cgraph_node *node = cgraph_allocate_node ();
376 node->symbol.type = SYMTAB_FUNCTION;
377 node->frequency = NODE_FREQUENCY_NORMAL;
378 node->count_materialization_scale = REG_BR_PROB_BASE;
379 cgraph_n_nodes++;
380 return node;
383 /* Return cgraph node assigned to DECL. Create new one when needed. */
385 struct cgraph_node *
386 cgraph_create_node (tree decl)
388 struct cgraph_node *node = cgraph_create_empty_node ();
389 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
391 node->symbol.decl = decl;
392 symtab_register_node ((symtab_node) node);
394 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
396 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
397 node->next_nested = node->origin->nested;
398 node->origin->nested = node;
400 return node;
403 /* Try to find a call graph node for declaration DECL and if it does not exist,
404 create it. */
406 struct cgraph_node *
407 cgraph_get_create_node (tree decl)
409 struct cgraph_node *node;
411 node = cgraph_get_node (decl);
412 if (node)
413 return node;
415 return cgraph_create_node (decl);
418 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
419 the function body is associated with (not necessarily cgraph_node (DECL). */
421 struct cgraph_node *
422 cgraph_create_function_alias (tree alias, tree decl)
424 struct cgraph_node *alias_node;
426 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
427 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
428 alias_node = cgraph_get_create_node (alias);
429 gcc_assert (!alias_node->local.finalized);
430 alias_node->thunk.alias = decl;
431 alias_node->local.finalized = true;
432 alias_node->alias = 1;
433 return alias_node;
436 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
437 and NULL otherwise.
438 Same body aliases are output whenever the body of DECL is output,
439 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
441 struct cgraph_node *
442 cgraph_same_body_alias (struct cgraph_node *decl_node ATTRIBUTE_UNUSED, tree alias, tree decl)
444 struct cgraph_node *n;
445 #ifndef ASM_OUTPUT_DEF
446 /* If aliases aren't supported by the assembler, fail. */
447 return NULL;
448 #endif
449 /* Langhooks can create same body aliases of symbols not defined.
450 Those are useless. Drop them on the floor. */
451 if (cgraph_global_info_ready)
452 return NULL;
454 n = cgraph_create_function_alias (alias, decl);
455 n->same_body_alias = true;
456 if (same_body_aliases_done)
457 ipa_record_reference ((symtab_node)n, (symtab_node)cgraph_get_node (decl),
458 IPA_REF_ALIAS, NULL);
459 return n;
462 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
463 aliases DECL with an adjustments made into the first parameter.
464 See comments in thunk_adjust for detail on the parameters. */
466 struct cgraph_node *
467 cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
468 tree alias, tree decl ATTRIBUTE_UNUSED,
469 bool this_adjusting,
470 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
471 tree virtual_offset,
472 tree real_alias)
474 struct cgraph_node *node;
476 node = cgraph_get_node (alias);
477 if (node)
479 gcc_assert (node->local.finalized);
480 gcc_assert (!node->alias);
481 gcc_assert (!node->thunk.thunk_p);
482 cgraph_remove_node (node);
485 node = cgraph_create_node (alias);
486 gcc_checking_assert (!virtual_offset
487 || tree_to_double_int (virtual_offset) ==
488 double_int::from_shwi (virtual_value));
489 node->thunk.fixed_offset = fixed_offset;
490 node->thunk.this_adjusting = this_adjusting;
491 node->thunk.virtual_value = virtual_value;
492 node->thunk.virtual_offset_p = virtual_offset != NULL;
493 node->thunk.alias = real_alias;
494 node->thunk.thunk_p = true;
495 node->local.finalized = true;
497 return node;
500 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
501 Return NULL if there's no such node. */
503 struct cgraph_node *
504 cgraph_node_for_asm (tree asmname)
506 symtab_node node = symtab_node_for_asm (asmname);
508 /* We do not want to look at inline clones. */
509 for (node = symtab_node_for_asm (asmname); node; node = node->symbol.next_sharing_asm_name)
510 if (symtab_function_p (node) && !cgraph(node)->global.inlined_to)
511 return cgraph (node);
512 return NULL;
515 /* Returns a hash value for X (which really is a cgraph_edge). */
517 static hashval_t
518 edge_hash (const void *x)
520 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
523 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
525 static int
526 edge_eq (const void *x, const void *y)
528 return ((const struct cgraph_edge *) x)->call_stmt == y;
531 /* Add call graph edge E to call site hash of its caller. */
533 static inline void
534 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
536 void **slot;
537 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
538 e->call_stmt,
539 htab_hash_pointer (e->call_stmt),
540 INSERT);
541 gcc_assert (!*slot);
542 *slot = e;
545 /* Return the callgraph edge representing the GIMPLE_CALL statement
546 CALL_STMT. */
548 struct cgraph_edge *
549 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
551 struct cgraph_edge *e, *e2;
552 int n = 0;
554 if (node->call_site_hash)
555 return (struct cgraph_edge *)
556 htab_find_with_hash (node->call_site_hash, call_stmt,
557 htab_hash_pointer (call_stmt));
559 /* This loop may turn out to be performance problem. In such case adding
560 hashtables into call nodes with very many edges is probably best
561 solution. It is not good idea to add pointer into CALL_EXPR itself
562 because we want to make possible having multiple cgraph nodes representing
563 different clones of the same body before the body is actually cloned. */
564 for (e = node->callees; e; e = e->next_callee)
566 if (e->call_stmt == call_stmt)
567 break;
568 n++;
571 if (!e)
572 for (e = node->indirect_calls; e; e = e->next_callee)
574 if (e->call_stmt == call_stmt)
575 break;
576 n++;
579 if (n > 100)
581 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
582 for (e2 = node->callees; e2; e2 = e2->next_callee)
583 cgraph_add_edge_to_call_site_hash (e2);
584 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
585 cgraph_add_edge_to_call_site_hash (e2);
588 return e;
592 /* Change field call_stmt of edge E to NEW_STMT. */
594 void
595 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
597 tree decl;
599 if (e->caller->call_site_hash)
601 htab_remove_elt_with_hash (e->caller->call_site_hash,
602 e->call_stmt,
603 htab_hash_pointer (e->call_stmt));
606 e->call_stmt = new_stmt;
607 if (e->indirect_unknown_callee
608 && (decl = gimple_call_fndecl (new_stmt)))
610 /* Constant propagation (and possibly also inlining?) can turn an
611 indirect call into a direct one. */
612 struct cgraph_node *new_callee = cgraph_get_node (decl);
614 gcc_checking_assert (new_callee);
615 cgraph_make_edge_direct (e, new_callee);
618 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
619 e->can_throw_external = stmt_can_throw_external (new_stmt);
620 pop_cfun ();
621 if (e->caller->call_site_hash)
622 cgraph_add_edge_to_call_site_hash (e);
625 /* Allocate a cgraph_edge structure and fill it with data according to the
626 parameters of which only CALLEE can be NULL (when creating an indirect call
627 edge). */
629 static struct cgraph_edge *
630 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
631 gimple call_stmt, gcov_type count, int freq)
633 struct cgraph_edge *edge;
635 /* LTO does not actually have access to the call_stmt since these
636 have not been loaded yet. */
637 if (call_stmt)
639 /* This is a rather expensive check possibly triggering
640 construction of call stmt hashtable. */
641 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
643 gcc_assert (is_gimple_call (call_stmt));
646 if (free_edges)
648 edge = free_edges;
649 free_edges = NEXT_FREE_EDGE (edge);
651 else
653 edge = ggc_alloc_cgraph_edge ();
654 edge->uid = cgraph_edge_max_uid++;
657 edge->aux = NULL;
658 edge->caller = caller;
659 edge->callee = callee;
660 edge->prev_caller = NULL;
661 edge->next_caller = NULL;
662 edge->prev_callee = NULL;
663 edge->next_callee = NULL;
665 edge->count = count;
666 gcc_assert (count >= 0);
667 edge->frequency = freq;
668 gcc_assert (freq >= 0);
669 gcc_assert (freq <= CGRAPH_FREQ_MAX);
671 edge->call_stmt = call_stmt;
672 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
673 edge->can_throw_external
674 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
675 pop_cfun ();
676 if (call_stmt
677 && callee && callee->symbol.decl
678 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
679 edge->call_stmt_cannot_inline_p = true;
680 else
681 edge->call_stmt_cannot_inline_p = false;
682 if (call_stmt && caller->call_site_hash)
683 cgraph_add_edge_to_call_site_hash (edge);
685 edge->indirect_info = NULL;
686 edge->indirect_inlining_edge = 0;
688 return edge;
691 /* Create edge from CALLER to CALLEE in the cgraph. */
693 struct cgraph_edge *
694 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
695 gimple call_stmt, gcov_type count, int freq)
697 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
698 count, freq);
700 edge->indirect_unknown_callee = 0;
701 initialize_inline_failed (edge);
703 edge->next_caller = callee->callers;
704 if (callee->callers)
705 callee->callers->prev_caller = edge;
706 edge->next_callee = caller->callees;
707 if (caller->callees)
708 caller->callees->prev_callee = edge;
709 caller->callees = edge;
710 callee->callers = edge;
712 return edge;
715 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
717 struct cgraph_indirect_call_info *
718 cgraph_allocate_init_indirect_info (void)
720 struct cgraph_indirect_call_info *ii;
722 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
723 ii->param_index = -1;
724 return ii;
727 /* Create an indirect edge with a yet-undetermined callee where the call
728 statement destination is a formal parameter of the caller with index
729 PARAM_INDEX. */
731 struct cgraph_edge *
732 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
733 int ecf_flags,
734 gcov_type count, int freq)
736 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
737 count, freq);
739 edge->indirect_unknown_callee = 1;
740 initialize_inline_failed (edge);
742 edge->indirect_info = cgraph_allocate_init_indirect_info ();
743 edge->indirect_info->ecf_flags = ecf_flags;
745 edge->next_callee = caller->indirect_calls;
746 if (caller->indirect_calls)
747 caller->indirect_calls->prev_callee = edge;
748 caller->indirect_calls = edge;
750 return edge;
753 /* Remove the edge E from the list of the callers of the callee. */
755 static inline void
756 cgraph_edge_remove_callee (struct cgraph_edge *e)
758 gcc_assert (!e->indirect_unknown_callee);
759 if (e->prev_caller)
760 e->prev_caller->next_caller = e->next_caller;
761 if (e->next_caller)
762 e->next_caller->prev_caller = e->prev_caller;
763 if (!e->prev_caller)
764 e->callee->callers = e->next_caller;
767 /* Remove the edge E from the list of the callees of the caller. */
769 static inline void
770 cgraph_edge_remove_caller (struct cgraph_edge *e)
772 if (e->prev_callee)
773 e->prev_callee->next_callee = e->next_callee;
774 if (e->next_callee)
775 e->next_callee->prev_callee = e->prev_callee;
776 if (!e->prev_callee)
778 if (e->indirect_unknown_callee)
779 e->caller->indirect_calls = e->next_callee;
780 else
781 e->caller->callees = e->next_callee;
783 if (e->caller->call_site_hash)
784 htab_remove_elt_with_hash (e->caller->call_site_hash,
785 e->call_stmt,
786 htab_hash_pointer (e->call_stmt));
789 /* Put the edge onto the free list. */
791 static void
792 cgraph_free_edge (struct cgraph_edge *e)
794 int uid = e->uid;
796 /* Clear out the edge so we do not dangle pointers. */
797 memset (e, 0, sizeof (*e));
798 e->uid = uid;
799 NEXT_FREE_EDGE (e) = free_edges;
800 free_edges = e;
803 /* Remove the edge E in the cgraph. */
805 void
806 cgraph_remove_edge (struct cgraph_edge *e)
808 /* Call all edge removal hooks. */
809 cgraph_call_edge_removal_hooks (e);
811 if (!e->indirect_unknown_callee)
812 /* Remove from callers list of the callee. */
813 cgraph_edge_remove_callee (e);
815 /* Remove from callees list of the callers. */
816 cgraph_edge_remove_caller (e);
818 /* Put the edge onto the free list. */
819 cgraph_free_edge (e);
822 /* Set callee of call graph edge E and add it to the corresponding set of
823 callers. */
825 static void
826 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
828 e->prev_caller = NULL;
829 if (n->callers)
830 n->callers->prev_caller = e;
831 e->next_caller = n->callers;
832 n->callers = e;
833 e->callee = n;
836 /* Redirect callee of E to N. The function does not update underlying
837 call expression. */
839 void
840 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
842 /* Remove from callers list of the current callee. */
843 cgraph_edge_remove_callee (e);
845 /* Insert to callers list of the new callee. */
846 cgraph_set_edge_callee (e, n);
849 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
850 CALLEE. DELTA is an integer constant that is to be added to the this
851 pointer (first parameter) to compensate for skipping a thunk adjustment. */
853 void
854 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
856 edge->indirect_unknown_callee = 0;
858 /* Get the edge out of the indirect edge list. */
859 if (edge->prev_callee)
860 edge->prev_callee->next_callee = edge->next_callee;
861 if (edge->next_callee)
862 edge->next_callee->prev_callee = edge->prev_callee;
863 if (!edge->prev_callee)
864 edge->caller->indirect_calls = edge->next_callee;
866 /* Put it into the normal callee list */
867 edge->prev_callee = NULL;
868 edge->next_callee = edge->caller->callees;
869 if (edge->caller->callees)
870 edge->caller->callees->prev_callee = edge;
871 edge->caller->callees = edge;
873 /* Insert to callers list of the new callee. */
874 cgraph_set_edge_callee (edge, callee);
876 if (edge->call_stmt)
877 edge->call_stmt_cannot_inline_p
878 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
880 /* We need to re-determine the inlining status of the edge. */
881 initialize_inline_failed (edge);
884 /* If necessary, change the function declaration in the call statement
885 associated with E so that it corresponds to the edge callee. */
887 gimple
888 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
890 tree decl = gimple_call_fndecl (e->call_stmt);
891 gimple new_stmt;
892 gimple_stmt_iterator gsi;
893 #ifdef ENABLE_CHECKING
894 struct cgraph_node *node;
895 #endif
897 if (e->indirect_unknown_callee
898 || decl == e->callee->symbol.decl)
899 return e->call_stmt;
901 #ifdef ENABLE_CHECKING
902 if (decl)
904 node = cgraph_get_node (decl);
905 gcc_assert (!node || !node->clone.combined_args_to_skip);
907 #endif
909 if (cgraph_dump_file)
911 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
912 xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
913 xstrdup (cgraph_node_name (e->callee)), e->callee->uid);
914 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
915 if (e->callee->clone.combined_args_to_skip)
917 fprintf (cgraph_dump_file, " combined args to skip: ");
918 dump_bitmap (cgraph_dump_file,
919 e->callee->clone.combined_args_to_skip);
923 if (e->callee->clone.combined_args_to_skip)
925 int lp_nr;
927 new_stmt
928 = gimple_call_copy_skip_args (e->call_stmt,
929 e->callee->clone.combined_args_to_skip);
930 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
932 if (gimple_vdef (new_stmt)
933 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
934 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
936 gsi = gsi_for_stmt (e->call_stmt);
937 gsi_replace (&gsi, new_stmt, false);
938 /* We need to defer cleaning EH info on the new statement to
939 fixup-cfg. We may not have dominator information at this point
940 and thus would end up with unreachable blocks and have no way
941 to communicate that we need to run CFG cleanup then. */
942 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
943 if (lp_nr != 0)
945 remove_stmt_from_eh_lp (e->call_stmt);
946 add_stmt_to_eh_lp (new_stmt, lp_nr);
949 else
951 new_stmt = e->call_stmt;
952 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
953 update_stmt (new_stmt);
956 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
958 if (cgraph_dump_file)
960 fprintf (cgraph_dump_file, " updated to:");
961 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
963 return new_stmt;
966 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
967 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
968 of OLD_STMT if it was previously call statement.
969 If NEW_STMT is NULL, the call has been dropped without any
970 replacement. */
972 static void
973 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
974 gimple old_stmt, tree old_call,
975 gimple new_stmt)
977 tree new_call = (new_stmt && is_gimple_call (new_stmt))
978 ? gimple_call_fndecl (new_stmt) : 0;
980 /* We are seeing indirect calls, then there is nothing to update. */
981 if (!new_call && !old_call)
982 return;
983 /* See if we turned indirect call into direct call or folded call to one builtin
984 into different builtin. */
985 if (old_call != new_call)
987 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
988 struct cgraph_edge *ne = NULL;
989 gcov_type count;
990 int frequency;
992 if (e)
994 /* See if the edge is already there and has the correct callee. It
995 might be so because of indirect inlining has already updated
996 it. We also might've cloned and redirected the edge. */
997 if (new_call && e->callee)
999 struct cgraph_node *callee = e->callee;
1000 while (callee)
1002 if (callee->symbol.decl == new_call
1003 || callee->former_clone_of == new_call)
1004 return;
1005 callee = callee->clone_of;
1009 /* Otherwise remove edge and create new one; we can't simply redirect
1010 since function has changed, so inline plan and other information
1011 attached to edge is invalid. */
1012 count = e->count;
1013 frequency = e->frequency;
1014 cgraph_remove_edge (e);
1016 else if (new_call)
1018 /* We are seeing new direct call; compute profile info based on BB. */
1019 basic_block bb = gimple_bb (new_stmt);
1020 count = bb->count;
1021 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1022 bb);
1025 if (new_call)
1027 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1028 new_stmt, count, frequency);
1029 gcc_assert (ne->inline_failed);
1032 /* We only updated the call stmt; update pointer in cgraph edge.. */
1033 else if (old_stmt != new_stmt)
1034 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1037 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1038 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1039 of OLD_STMT before it was updated (updating can happen inplace). */
1041 void
1042 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1044 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1045 struct cgraph_node *node;
1047 gcc_checking_assert (orig);
1048 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1049 if (orig->clones)
1050 for (node = orig->clones; node != orig;)
1052 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1053 if (node->clones)
1054 node = node->clones;
1055 else if (node->next_sibling_clone)
1056 node = node->next_sibling_clone;
1057 else
1059 while (node != orig && !node->next_sibling_clone)
1060 node = node->clone_of;
1061 if (node != orig)
1062 node = node->next_sibling_clone;
1068 /* Remove all callees from the node. */
1070 void
1071 cgraph_node_remove_callees (struct cgraph_node *node)
1073 struct cgraph_edge *e, *f;
1075 /* It is sufficient to remove the edges from the lists of callers of
1076 the callees. The callee list of the node can be zapped with one
1077 assignment. */
1078 for (e = node->callees; e; e = f)
1080 f = e->next_callee;
1081 cgraph_call_edge_removal_hooks (e);
1082 if (!e->indirect_unknown_callee)
1083 cgraph_edge_remove_callee (e);
1084 cgraph_free_edge (e);
1086 for (e = node->indirect_calls; e; e = f)
1088 f = e->next_callee;
1089 cgraph_call_edge_removal_hooks (e);
1090 if (!e->indirect_unknown_callee)
1091 cgraph_edge_remove_callee (e);
1092 cgraph_free_edge (e);
1094 node->indirect_calls = NULL;
1095 node->callees = NULL;
1096 if (node->call_site_hash)
1098 htab_delete (node->call_site_hash);
1099 node->call_site_hash = NULL;
1103 /* Remove all callers from the node. */
1105 static void
1106 cgraph_node_remove_callers (struct cgraph_node *node)
1108 struct cgraph_edge *e, *f;
1110 /* It is sufficient to remove the edges from the lists of callees of
1111 the callers. The caller list of the node can be zapped with one
1112 assignment. */
1113 for (e = node->callers; e; e = f)
1115 f = e->next_caller;
1116 cgraph_call_edge_removal_hooks (e);
1117 cgraph_edge_remove_caller (e);
1118 cgraph_free_edge (e);
1120 node->callers = NULL;
1123 /* Release memory used to represent body of function NODE. */
1125 void
1126 cgraph_release_function_body (struct cgraph_node *node)
1128 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1130 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1131 if (cfun->cfg
1132 && current_loops)
1134 cfun->curr_properties &= ~PROP_loops;
1135 loop_optimizer_finalize ();
1137 if (cfun->gimple_df)
1139 delete_tree_ssa ();
1140 delete_tree_cfg_annotations ();
1141 cfun->eh = NULL;
1143 if (cfun->cfg)
1145 gcc_assert (dom_computed[0] == DOM_NONE);
1146 gcc_assert (dom_computed[1] == DOM_NONE);
1147 clear_edges ();
1149 if (cfun->value_histograms)
1150 free_histograms ();
1151 pop_cfun();
1152 gimple_set_body (node->symbol.decl, NULL);
1153 VEC_free (ipa_opt_pass, heap,
1154 node->ipa_transforms_to_apply);
1155 /* Struct function hangs a lot of data that would leak if we didn't
1156 removed all pointers to it. */
1157 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1158 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1160 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1161 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1162 of its associated function function declaration because it's
1163 needed to emit debug info later. */
1164 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1165 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1168 /* Remove the node from cgraph. */
1170 void
1171 cgraph_remove_node (struct cgraph_node *node)
1173 struct cgraph_node *n;
1174 int uid = node->uid;
1176 cgraph_call_node_removal_hooks (node);
1177 cgraph_node_remove_callers (node);
1178 cgraph_node_remove_callees (node);
1179 VEC_free (ipa_opt_pass, heap,
1180 node->ipa_transforms_to_apply);
1182 /* Incremental inlining access removed nodes stored in the postorder list.
1184 node->symbol.force_output = false;
1185 for (n = node->nested; n; n = n->next_nested)
1186 n->origin = NULL;
1187 node->nested = NULL;
1188 if (node->origin)
1190 struct cgraph_node **node2 = &node->origin->nested;
1192 while (*node2 != node)
1193 node2 = &(*node2)->next_nested;
1194 *node2 = node->next_nested;
1196 symtab_unregister_node ((symtab_node)node);
1197 if (node->prev_sibling_clone)
1198 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1199 else if (node->clone_of)
1200 node->clone_of->clones = node->next_sibling_clone;
1201 if (node->next_sibling_clone)
1202 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1203 if (node->clones)
1205 struct cgraph_node *n, *next;
1207 if (node->clone_of)
1209 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1210 n->clone_of = node->clone_of;
1211 n->clone_of = node->clone_of;
1212 n->next_sibling_clone = node->clone_of->clones;
1213 if (node->clone_of->clones)
1214 node->clone_of->clones->prev_sibling_clone = n;
1215 node->clone_of->clones = node->clones;
1217 else
1219 /* We are removing node with clones. This makes clones inconsistent,
1220 but assume they will be removed subsequently and just keep clone
1221 tree intact. This can happen in unreachable function removal since
1222 we remove unreachable functions in random order, not by bottom-up
1223 walk of clone trees. */
1224 for (n = node->clones; n; n = next)
1226 next = n->next_sibling_clone;
1227 n->next_sibling_clone = NULL;
1228 n->prev_sibling_clone = NULL;
1229 n->clone_of = NULL;
1234 /* While all the clones are removed after being proceeded, the function
1235 itself is kept in the cgraph even after it is compiled. Check whether
1236 we are done with this body and reclaim it proactively if this is the case.
1238 n = cgraph_get_node (node->symbol.decl);
1239 if (!n
1240 || (!n->clones && !n->clone_of && !n->global.inlined_to
1241 && (cgraph_global_info_ready
1242 && (TREE_ASM_WRITTEN (n->symbol.decl)
1243 || DECL_EXTERNAL (n->symbol.decl)
1244 || !n->analyzed
1245 || n->symbol.in_other_partition))))
1246 cgraph_release_function_body (node);
1248 node->symbol.decl = NULL;
1249 if (node->call_site_hash)
1251 htab_delete (node->call_site_hash);
1252 node->call_site_hash = NULL;
1254 cgraph_n_nodes--;
1256 /* Clear out the node to NULL all pointers and add the node to the free
1257 list. */
1258 memset (node, 0, sizeof(*node));
1259 node->symbol.type = SYMTAB_FUNCTION;
1260 node->uid = uid;
1261 SET_NEXT_FREE_NODE (node, free_nodes);
1262 free_nodes = node;
1265 /* Likewise indicate that a node is having address taken. */
1267 void
1268 cgraph_mark_address_taken_node (struct cgraph_node *node)
1270 gcc_assert (!node->global.inlined_to);
1271 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1272 IPA_REF_ADDR reference exists (and thus it should be set on node
1273 representing alias we take address of) and as a test whether address
1274 of the object was taken (and thus it should be set on node alias is
1275 referring to). We should remove the first use and the remove the
1276 following set. */
1277 node->symbol.address_taken = 1;
1278 node = cgraph_function_or_thunk_node (node, NULL);
1279 node->symbol.address_taken = 1;
1282 /* Return local info for the compiled function. */
1284 struct cgraph_local_info *
1285 cgraph_local_info (tree decl)
1287 struct cgraph_node *node;
1289 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1290 node = cgraph_get_node (decl);
1291 if (!node)
1292 return NULL;
1293 return &node->local;
1296 /* Return local info for the compiled function. */
1298 struct cgraph_global_info *
1299 cgraph_global_info (tree decl)
1301 struct cgraph_node *node;
1303 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1304 node = cgraph_get_node (decl);
1305 if (!node)
1306 return NULL;
1307 return &node->global;
1310 /* Return local info for the compiled function. */
1312 struct cgraph_rtl_info *
1313 cgraph_rtl_info (tree decl)
1315 struct cgraph_node *node;
1317 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1318 node = cgraph_get_node (decl);
1319 if (!node
1320 || (decl != current_function_decl
1321 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1322 return NULL;
1323 return &node->rtl;
1326 /* Return a string describing the failure REASON. */
1328 const char*
1329 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1331 #undef DEFCIFCODE
1332 #define DEFCIFCODE(code, string) string,
1334 static const char *cif_string_table[CIF_N_REASONS] = {
1335 #include "cif-code.def"
1338 /* Signedness of an enum type is implementation defined, so cast it
1339 to unsigned before testing. */
1340 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1341 return cif_string_table[reason];
1344 /* Names used to print out the availability enum. */
1345 const char * const cgraph_availability_names[] =
1346 {"unset", "not_available", "overwritable", "available", "local"};
1349 /* Dump call graph node NODE to file F. */
1351 void
1352 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1354 struct cgraph_edge *edge;
1355 int indirect_calls_count = 0;
1357 dump_symtab_base (f, (symtab_node) node);
1359 if (node->global.inlined_to)
1360 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1361 xstrdup (cgraph_node_name (node)),
1362 node->symbol.order,
1363 xstrdup (cgraph_node_name (node->global.inlined_to)),
1364 node->global.inlined_to->symbol.order);
1365 if (node->clone_of)
1366 fprintf (f, " Clone of %s/%i\n",
1367 cgraph_node_asm_name (node->clone_of),
1368 node->clone_of->symbol.order);
1369 if (cgraph_function_flags_ready)
1370 fprintf (f, " Availability: %s\n",
1371 cgraph_availability_names [cgraph_function_body_availability (node)]);
1373 fprintf (f, " Function flags:");
1374 if (node->analyzed)
1375 fprintf (f, " analyzed");
1376 if (node->count)
1377 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1378 (HOST_WIDEST_INT)node->count);
1379 if (node->origin)
1380 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1381 if (gimple_has_body_p (node->symbol.decl))
1382 fprintf (f, " body");
1383 if (node->process)
1384 fprintf (f, " process");
1385 if (node->local.local)
1386 fprintf (f, " local");
1387 if (node->local.finalized)
1388 fprintf (f, " finalized");
1389 if (node->local.redefined_extern_inline)
1390 fprintf (f, " redefined_extern_inline");
1391 if (node->only_called_at_startup)
1392 fprintf (f, " only_called_at_startup");
1393 if (node->only_called_at_exit)
1394 fprintf (f, " only_called_at_exit");
1395 else if (node->alias)
1396 fprintf (f, " alias");
1397 if (node->tm_clone)
1398 fprintf (f, " tm_clone");
1400 fprintf (f, "\n");
1402 if (node->thunk.thunk_p)
1404 fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1405 "virtual offset %i)\n",
1406 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1407 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1408 (int)node->thunk.fixed_offset,
1409 (int)node->thunk.virtual_value,
1410 (int)node->thunk.virtual_offset_p);
1412 if (node->alias && node->thunk.alias)
1414 fprintf (f, " Alias of %s",
1415 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1416 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1417 fprintf (f, " (asm: %s)",
1418 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1419 fprintf (f, "\n");
1422 fprintf (f, " Called by: ");
1424 for (edge = node->callers; edge; edge = edge->next_caller)
1426 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1427 edge->caller->symbol.order);
1428 if (edge->count)
1429 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1430 (HOST_WIDEST_INT)edge->count);
1431 if (edge->frequency)
1432 fprintf (f, "(%.2f per call) ",
1433 edge->frequency / (double)CGRAPH_FREQ_BASE);
1434 if (!edge->inline_failed)
1435 fprintf(f, "(inlined) ");
1436 if (edge->indirect_inlining_edge)
1437 fprintf(f, "(indirect_inlining) ");
1438 if (edge->can_throw_external)
1439 fprintf(f, "(can throw external) ");
1442 fprintf (f, "\n Calls: ");
1443 for (edge = node->callees; edge; edge = edge->next_callee)
1445 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1446 edge->callee->symbol.order);
1447 if (!edge->inline_failed)
1448 fprintf(f, "(inlined) ");
1449 if (edge->indirect_inlining_edge)
1450 fprintf(f, "(indirect_inlining) ");
1451 if (edge->count)
1452 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1453 (HOST_WIDEST_INT)edge->count);
1454 if (edge->frequency)
1455 fprintf (f, "(%.2f per call) ",
1456 edge->frequency / (double)CGRAPH_FREQ_BASE);
1457 if (edge->can_throw_external)
1458 fprintf(f, "(can throw external) ");
1460 fprintf (f, "\n");
1462 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1463 indirect_calls_count++;
1464 if (indirect_calls_count)
1465 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1466 indirect_calls_count);
1470 /* Dump call graph node NODE to stderr. */
1472 DEBUG_FUNCTION void
1473 debug_cgraph_node (struct cgraph_node *node)
1475 dump_cgraph_node (stderr, node);
1479 /* Dump the callgraph to file F. */
1481 void
1482 dump_cgraph (FILE *f)
1484 struct cgraph_node *node;
1486 fprintf (f, "callgraph:\n\n");
1487 FOR_EACH_FUNCTION (node)
1488 dump_cgraph_node (f, node);
1492 /* Dump the call graph to stderr. */
1494 DEBUG_FUNCTION void
1495 debug_cgraph (void)
1497 dump_cgraph (stderr);
1500 /* Return true when the DECL can possibly be inlined. */
1501 bool
1502 cgraph_function_possibly_inlined_p (tree decl)
1504 if (!cgraph_global_info_ready)
1505 return !DECL_UNINLINABLE (decl);
1506 return DECL_POSSIBLY_INLINED (decl);
1509 /* NODE is no longer nested function; update cgraph accordingly. */
1510 void
1511 cgraph_unnest_node (struct cgraph_node *node)
1513 struct cgraph_node **node2 = &node->origin->nested;
1514 gcc_assert (node->origin);
1516 while (*node2 != node)
1517 node2 = &(*node2)->next_nested;
1518 *node2 = node->next_nested;
1519 node->origin = NULL;
1522 /* Return function availability. See cgraph.h for description of individual
1523 return values. */
1524 enum availability
1525 cgraph_function_body_availability (struct cgraph_node *node)
1527 enum availability avail;
1528 gcc_assert (cgraph_function_flags_ready);
1529 if (!node->analyzed)
1530 avail = AVAIL_NOT_AVAILABLE;
1531 else if (node->local.local)
1532 avail = AVAIL_LOCAL;
1533 else if (!node->symbol.externally_visible)
1534 avail = AVAIL_AVAILABLE;
1535 /* Inline functions are safe to be analyzed even if their symbol can
1536 be overwritten at runtime. It is not meaningful to enforce any sane
1537 behaviour on replacing inline function by different body. */
1538 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1539 avail = AVAIL_AVAILABLE;
1541 /* If the function can be overwritten, return OVERWRITABLE. Take
1542 care at least of two notable extensions - the COMDAT functions
1543 used to share template instantiations in C++ (this is symmetric
1544 to code cp_cannot_inline_tree_fn and probably shall be shared and
1545 the inlinability hooks completely eliminated).
1547 ??? Does the C++ one definition rule allow us to always return
1548 AVAIL_AVAILABLE here? That would be good reason to preserve this
1549 bit. */
1551 else if (decl_replaceable_p (node->symbol.decl)
1552 && !DECL_EXTERNAL (node->symbol.decl))
1553 avail = AVAIL_OVERWRITABLE;
1554 else avail = AVAIL_AVAILABLE;
1556 return avail;
1559 /* Worker for cgraph_node_can_be_local_p. */
1560 static bool
1561 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1562 void *data ATTRIBUTE_UNUSED)
1564 return !(!node->symbol.force_output
1565 && ((DECL_COMDAT (node->symbol.decl)
1566 && !node->symbol.same_comdat_group)
1567 || !node->symbol.externally_visible));
1570 /* Return true if NODE can be made local for API change.
1571 Extern inline functions and C++ COMDAT functions can be made local
1572 at the expense of possible code size growth if function is used in multiple
1573 compilation units. */
1574 bool
1575 cgraph_node_can_be_local_p (struct cgraph_node *node)
1577 return (!node->symbol.address_taken
1578 && !cgraph_for_node_and_aliases (node,
1579 cgraph_node_cannot_be_local_p_1,
1580 NULL, true));
1583 /* Call calback on NODE, thunks and aliases associated to NODE.
1584 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1585 skipped. */
1587 bool
1588 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1589 bool (*callback) (struct cgraph_node *, void *),
1590 void *data,
1591 bool include_overwritable)
1593 struct cgraph_edge *e;
1594 int i;
1595 struct ipa_ref *ref;
1597 if (callback (node, data))
1598 return true;
1599 for (e = node->callers; e; e = e->next_caller)
1600 if (e->caller->thunk.thunk_p
1601 && (include_overwritable
1602 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1603 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1604 include_overwritable))
1605 return true;
1606 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1607 if (ref->use == IPA_REF_ALIAS)
1609 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1610 if (include_overwritable
1611 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1612 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1613 include_overwritable))
1614 return true;
1616 return false;
1619 /* Call calback on NODE and aliases associated to NODE.
1620 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1621 skipped. */
1623 bool
1624 cgraph_for_node_and_aliases (struct cgraph_node *node,
1625 bool (*callback) (struct cgraph_node *, void *),
1626 void *data,
1627 bool include_overwritable)
1629 int i;
1630 struct ipa_ref *ref;
1632 if (callback (node, data))
1633 return true;
1634 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1635 if (ref->use == IPA_REF_ALIAS)
1637 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1638 if (include_overwritable
1639 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1640 if (cgraph_for_node_and_aliases (alias, callback, data,
1641 include_overwritable))
1642 return true;
1644 return false;
1647 /* Worker to bring NODE local. */
1649 static bool
1650 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1652 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1653 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1655 symtab_make_decl_local (node->symbol.decl);
1657 node->symbol.externally_visible = false;
1658 node->local.local = true;
1659 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1660 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1662 return false;
1665 /* Bring NODE local. */
1667 void
1668 cgraph_make_node_local (struct cgraph_node *node)
1670 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1671 NULL, true);
1674 /* Worker to set nothrow flag. */
1676 static bool
1677 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1679 struct cgraph_edge *e;
1681 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1683 if (data != NULL)
1684 for (e = node->callers; e; e = e->next_caller)
1685 e->can_throw_external = false;
1686 return false;
1689 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1690 if any to NOTHROW. */
1692 void
1693 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1695 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1696 (void *)(size_t)nothrow, false);
1699 /* Worker to set const flag. */
1701 static bool
1702 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1704 /* Static constructors and destructors without a side effect can be
1705 optimized out. */
1706 if (data && !((size_t)data & 2))
1708 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1709 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1710 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1711 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1713 TREE_READONLY (node->symbol.decl) = data != NULL;
1714 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1715 return false;
1718 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1719 if any to READONLY. */
1721 void
1722 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1724 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1725 (void *)(size_t)(readonly + (int)looping * 2),
1726 false);
1729 /* Worker to set pure flag. */
1731 static bool
1732 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1734 /* Static constructors and destructors without a side effect can be
1735 optimized out. */
1736 if (data && !((size_t)data & 2))
1738 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1739 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1740 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1741 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1743 DECL_PURE_P (node->symbol.decl) = data != NULL;
1744 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1745 return false;
1748 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1749 if any to PURE. */
1751 void
1752 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1754 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1755 (void *)(size_t)(pure + (int)looping * 2),
1756 false);
1759 /* Data used by cgraph_propagate_frequency. */
1761 struct cgraph_propagate_frequency_data
1763 bool maybe_unlikely_executed;
1764 bool maybe_executed_once;
1765 bool only_called_at_startup;
1766 bool only_called_at_exit;
1769 /* Worker for cgraph_propagate_frequency_1. */
1771 static bool
1772 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1774 struct cgraph_propagate_frequency_data *d;
1775 struct cgraph_edge *edge;
1777 d = (struct cgraph_propagate_frequency_data *)data;
1778 for (edge = node->callers;
1779 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1780 || d->only_called_at_startup || d->only_called_at_exit);
1781 edge = edge->next_caller)
1783 if (edge->caller != node)
1785 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1786 /* It makes sense to put main() together with the static constructors.
1787 It will be executed for sure, but rest of functions called from
1788 main are definitely not at startup only. */
1789 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1790 d->only_called_at_startup = 0;
1791 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1793 if (!edge->frequency)
1794 continue;
1795 switch (edge->caller->frequency)
1797 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1798 break;
1799 case NODE_FREQUENCY_EXECUTED_ONCE:
1800 if (dump_file && (dump_flags & TDF_DETAILS))
1801 fprintf (dump_file, " Called by %s that is executed once\n",
1802 cgraph_node_name (edge->caller));
1803 d->maybe_unlikely_executed = false;
1804 if (inline_edge_summary (edge)->loop_depth)
1806 d->maybe_executed_once = false;
1807 if (dump_file && (dump_flags & TDF_DETAILS))
1808 fprintf (dump_file, " Called in loop\n");
1810 break;
1811 case NODE_FREQUENCY_HOT:
1812 case NODE_FREQUENCY_NORMAL:
1813 if (dump_file && (dump_flags & TDF_DETAILS))
1814 fprintf (dump_file, " Called by %s that is normal or hot\n",
1815 cgraph_node_name (edge->caller));
1816 d->maybe_unlikely_executed = false;
1817 d->maybe_executed_once = false;
1818 break;
1821 return edge != NULL;
1824 /* See if the frequency of NODE can be updated based on frequencies of its
1825 callers. */
1826 bool
1827 cgraph_propagate_frequency (struct cgraph_node *node)
1829 struct cgraph_propagate_frequency_data d = {true, true, true, true};
1830 bool changed = false;
1832 if (!node->local.local)
1833 return false;
1834 gcc_assert (node->analyzed);
1835 if (dump_file && (dump_flags & TDF_DETAILS))
1836 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
1838 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
1840 if ((d.only_called_at_startup && !d.only_called_at_exit)
1841 && !node->only_called_at_startup)
1843 node->only_called_at_startup = true;
1844 if (dump_file)
1845 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
1846 cgraph_node_name (node));
1847 changed = true;
1849 if ((d.only_called_at_exit && !d.only_called_at_startup)
1850 && !node->only_called_at_exit)
1852 node->only_called_at_exit = true;
1853 if (dump_file)
1854 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
1855 cgraph_node_name (node));
1856 changed = true;
1858 /* These come either from profile or user hints; never update them. */
1859 if (node->frequency == NODE_FREQUENCY_HOT
1860 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1861 return changed;
1862 if (d.maybe_unlikely_executed)
1864 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
1865 if (dump_file)
1866 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
1867 cgraph_node_name (node));
1868 changed = true;
1870 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
1872 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1873 if (dump_file)
1874 fprintf (dump_file, "Node %s promoted to executed once.\n",
1875 cgraph_node_name (node));
1876 changed = true;
1878 return changed;
1881 /* Return true when NODE can not return or throw and thus
1882 it is safe to ignore its side effects for IPA analysis. */
1884 bool
1885 cgraph_node_cannot_return (struct cgraph_node *node)
1887 int flags = flags_from_decl_or_type (node->symbol.decl);
1888 if (!flag_exceptions)
1889 return (flags & ECF_NORETURN) != 0;
1890 else
1891 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1892 == (ECF_NORETURN | ECF_NOTHROW));
1895 /* Return true when call of E can not lead to return from caller
1896 and thus it is safe to ignore its side effects for IPA analysis
1897 when computing side effects of the caller.
1898 FIXME: We could actually mark all edges that have no reaching
1899 patch to EXIT_BLOCK_PTR or throw to get better results. */
1900 bool
1901 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
1903 if (cgraph_node_cannot_return (e->caller))
1904 return true;
1905 if (e->indirect_unknown_callee)
1907 int flags = e->indirect_info->ecf_flags;
1908 if (!flag_exceptions)
1909 return (flags & ECF_NORETURN) != 0;
1910 else
1911 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1912 == (ECF_NORETURN | ECF_NOTHROW));
1914 else
1915 return cgraph_node_cannot_return (e->callee);
1918 /* Return true when function NODE can be removed from callgraph
1919 if all direct calls are eliminated. */
1921 bool
1922 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
1924 gcc_assert (!node->global.inlined_to);
1925 /* Extern inlines can always go, we will use the external definition. */
1926 if (DECL_EXTERNAL (node->symbol.decl))
1927 return true;
1928 /* When function is needed, we can not remove it. */
1929 if (node->symbol.force_output || node->symbol.used_from_other_partition)
1930 return false;
1931 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1932 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1933 return false;
1934 /* Only COMDAT functions can be removed if externally visible. */
1935 if (node->symbol.externally_visible
1936 && (!DECL_COMDAT (node->symbol.decl)
1937 || symtab_used_from_object_file_p ((symtab_node) node)))
1938 return false;
1939 return true;
1942 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1944 static bool
1945 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1947 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
1950 /* Return true when function NODE and its aliases can be removed from callgraph
1951 if all direct calls are eliminated. */
1953 bool
1954 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
1956 /* Extern inlines can always go, we will use the external definition. */
1957 if (DECL_EXTERNAL (node->symbol.decl))
1958 return true;
1959 if (node->symbol.address_taken)
1960 return false;
1961 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
1964 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1966 static bool
1967 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1969 return symtab_used_from_object_file_p ((symtab_node) node);
1972 /* Return true when function NODE can be expected to be removed
1973 from program when direct calls in this compilation unit are removed.
1975 As a special case COMDAT functions are
1976 cgraph_can_remove_if_no_direct_calls_p while the are not
1977 cgraph_only_called_directly_p (it is possible they are called from other
1978 unit)
1980 This function behaves as cgraph_only_called_directly_p because eliminating
1981 all uses of COMDAT function does not make it necessarily disappear from
1982 the program unless we are compiling whole program or we do LTO. In this
1983 case we know we win since dynamic linking will not really discard the
1984 linkonce section. */
1986 bool
1987 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
1989 gcc_assert (!node->global.inlined_to);
1990 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
1991 return false;
1992 if (!in_lto_p && !flag_whole_program)
1993 return cgraph_only_called_directly_p (node);
1994 else
1996 if (DECL_EXTERNAL (node->symbol.decl))
1997 return true;
1998 return cgraph_can_remove_if_no_direct_calls_p (node);
2003 /* Worker for cgraph_only_called_directly_p. */
2005 static bool
2006 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2008 return !cgraph_only_called_directly_or_aliased_p (node);
2011 /* Return true when function NODE and all its aliases are only called
2012 directly.
2013 i.e. it is not externally visible, address was not taken and
2014 it is not used in any other non-standard way. */
2016 bool
2017 cgraph_only_called_directly_p (struct cgraph_node *node)
2019 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2020 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2021 NULL, true);
2025 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2027 static bool
2028 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2030 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
2031 struct cgraph_edge *cs;
2032 enum availability avail;
2033 cgraph_function_or_thunk_node (node, &avail);
2035 if (avail > AVAIL_OVERWRITABLE)
2036 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2037 if (!cs->indirect_inlining_edge)
2038 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
2039 return false;
2042 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2043 (i.e. are not overwritable). */
2045 VEC (cgraph_edge_p, heap) *
2046 collect_callers_of_node (struct cgraph_node *node)
2048 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
2049 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2050 &redirect_callers, false);
2051 return redirect_callers;
2054 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2055 static bool
2056 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2058 node = cgraph_function_or_thunk_node (node, NULL);
2059 node2 = cgraph_function_or_thunk_node (node2, NULL);
2060 while (node != node2 && node2)
2061 node2 = node2->clone_of;
2062 return node2 != NULL;
2065 /* Verify edge E count and frequency. */
2067 static bool
2068 verify_edge_count_and_frequency (struct cgraph_edge *e)
2070 bool error_found = false;
2071 if (e->count < 0)
2073 error ("caller edge count is negative");
2074 error_found = true;
2076 if (e->frequency < 0)
2078 error ("caller edge frequency is negative");
2079 error_found = true;
2081 if (e->frequency > CGRAPH_FREQ_MAX)
2083 error ("caller edge frequency is too large");
2084 error_found = true;
2086 if (gimple_has_body_p (e->caller->symbol.decl)
2087 && !e->caller->global.inlined_to
2088 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2089 Remove this once edges are actually removed from the function at that time. */
2090 && (e->frequency
2091 || (inline_edge_summary_vec
2092 && ((VEC_length(inline_edge_summary_t, inline_edge_summary_vec)
2093 <= (unsigned) e->uid)
2094 || !inline_edge_summary (e)->predicate)))
2095 && (e->frequency
2096 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2097 gimple_bb (e->call_stmt))))
2099 error ("caller edge frequency %i does not match BB frequency %i",
2100 e->frequency,
2101 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2102 gimple_bb (e->call_stmt)));
2103 error_found = true;
2105 return error_found;
2108 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2109 static void
2110 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2112 /* debug_gimple_stmt needs correct cfun */
2113 if (cfun != this_cfun)
2114 set_cfun (this_cfun);
2115 debug_gimple_stmt (stmt);
2118 /* Verify that call graph edge E corresponds to DECL from the associated
2119 statement. Return true if the verification should fail. */
2121 static bool
2122 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2124 struct cgraph_node *node;
2126 if (!decl || e->callee->global.inlined_to)
2127 return false;
2128 node = cgraph_get_node (decl);
2130 /* We do not know if a node from a different partition is an alias or what it
2131 aliases and therefore cannot do the former_clone_of check reliably. */
2132 if (!node || node->symbol.in_other_partition)
2133 return false;
2134 node = cgraph_function_or_thunk_node (node, NULL);
2136 if ((e->callee->former_clone_of != node->symbol.decl
2137 && (!node->same_body_alias
2138 || e->callee->former_clone_of != node->thunk.alias))
2139 /* IPA-CP sometimes redirect edge to clone and then back to the former
2140 function. This ping-pong has to go, eventually. */
2141 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2142 && !clone_of_p (node, e->callee)
2143 /* If decl is a same body alias of some other decl, allow e->callee to be
2144 a clone of a clone of that other decl too. */
2145 && (!node->same_body_alias
2146 || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee)))
2147 return true;
2148 else
2149 return false;
2152 /* Verify cgraph nodes of given cgraph node. */
2153 DEBUG_FUNCTION void
2154 verify_cgraph_node (struct cgraph_node *node)
2156 struct cgraph_edge *e;
2157 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2158 basic_block this_block;
2159 gimple_stmt_iterator gsi;
2160 bool error_found = false;
2162 if (seen_error ())
2163 return;
2165 timevar_push (TV_CGRAPH_VERIFY);
2166 error_found |= verify_symtab_base ((symtab_node) node);
2167 for (e = node->callees; e; e = e->next_callee)
2168 if (e->aux)
2170 error ("aux field set for edge %s->%s",
2171 identifier_to_locale (cgraph_node_name (e->caller)),
2172 identifier_to_locale (cgraph_node_name (e->callee)));
2173 error_found = true;
2175 if (node->count < 0)
2177 error ("execution count is negative");
2178 error_found = true;
2180 if (node->global.inlined_to && node->symbol.same_comdat_group)
2182 error ("inline clone in same comdat group list");
2183 error_found = true;
2185 if (node->global.inlined_to && node->symbol.externally_visible)
2187 error ("externally visible inline clone");
2188 error_found = true;
2190 if (node->global.inlined_to && node->symbol.address_taken)
2192 error ("inline clone with address taken");
2193 error_found = true;
2195 if (node->global.inlined_to && node->symbol.force_output)
2197 error ("inline clone is forced to output");
2198 error_found = true;
2200 for (e = node->indirect_calls; e; e = e->next_callee)
2202 if (e->aux)
2204 error ("aux field set for indirect edge from %s",
2205 identifier_to_locale (cgraph_node_name (e->caller)));
2206 error_found = true;
2208 if (!e->indirect_unknown_callee
2209 || !e->indirect_info)
2211 error ("An indirect edge from %s is not marked as indirect or has "
2212 "associated indirect_info, the corresponding statement is: ",
2213 identifier_to_locale (cgraph_node_name (e->caller)));
2214 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2215 error_found = true;
2218 for (e = node->callers; e; e = e->next_caller)
2220 if (verify_edge_count_and_frequency (e))
2221 error_found = true;
2222 if (!e->inline_failed)
2224 if (node->global.inlined_to
2225 != (e->caller->global.inlined_to
2226 ? e->caller->global.inlined_to : e->caller))
2228 error ("inlined_to pointer is wrong");
2229 error_found = true;
2231 if (node->callers->next_caller)
2233 error ("multiple inline callers");
2234 error_found = true;
2237 else
2238 if (node->global.inlined_to)
2240 error ("inlined_to pointer set for noninline callers");
2241 error_found = true;
2244 for (e = node->indirect_calls; e; e = e->next_callee)
2245 if (verify_edge_count_and_frequency (e))
2246 error_found = true;
2247 if (!node->callers && node->global.inlined_to)
2249 error ("inlined_to pointer is set but no predecessors found");
2250 error_found = true;
2252 if (node->global.inlined_to == node)
2254 error ("inlined_to pointer refers to itself");
2255 error_found = true;
2258 if (node->clone_of)
2260 struct cgraph_node *n;
2261 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2262 if (n == node)
2263 break;
2264 if (!n)
2266 error ("node has wrong clone_of");
2267 error_found = true;
2270 if (node->clones)
2272 struct cgraph_node *n;
2273 for (n = node->clones; n; n = n->next_sibling_clone)
2274 if (n->clone_of != node)
2275 break;
2276 if (n)
2278 error ("node has wrong clone list");
2279 error_found = true;
2282 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2284 error ("node is in clone list but it is not clone");
2285 error_found = true;
2287 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2289 error ("node has wrong prev_clone pointer");
2290 error_found = true;
2292 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2294 error ("double linked list of clones corrupted");
2295 error_found = true;
2298 if (node->analyzed && node->alias)
2300 bool ref_found = false;
2301 int i;
2302 struct ipa_ref *ref;
2304 if (node->callees)
2306 error ("Alias has call edges");
2307 error_found = true;
2309 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2310 i, ref); i++)
2311 if (ref->use != IPA_REF_ALIAS)
2313 error ("Alias has non-alias reference");
2314 error_found = true;
2316 else if (ref_found)
2318 error ("Alias has more than one alias reference");
2319 error_found = true;
2321 else
2322 ref_found = true;
2323 if (!ref_found)
2325 error ("Analyzed alias has no reference");
2326 error_found = true;
2329 if (node->analyzed && node->thunk.thunk_p)
2331 if (!node->callees)
2333 error ("No edge out of thunk node");
2334 error_found = true;
2336 else if (node->callees->next_callee)
2338 error ("More than one edge out of thunk node");
2339 error_found = true;
2341 if (gimple_has_body_p (node->symbol.decl))
2343 error ("Thunk is not supposed to have body");
2344 error_found = true;
2347 else if (node->analyzed && gimple_has_body_p (node->symbol.decl)
2348 && !TREE_ASM_WRITTEN (node->symbol.decl)
2349 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2350 && !flag_wpa)
2352 if (this_cfun->cfg)
2354 /* The nodes we're interested in are never shared, so walk
2355 the tree ignoring duplicates. */
2356 struct pointer_set_t *visited_nodes = pointer_set_create ();
2357 /* Reach the trees by walking over the CFG, and note the
2358 enclosing basic-blocks in the call edges. */
2359 FOR_EACH_BB_FN (this_block, this_cfun)
2360 for (gsi = gsi_start_bb (this_block);
2361 !gsi_end_p (gsi);
2362 gsi_next (&gsi))
2364 gimple stmt = gsi_stmt (gsi);
2365 if (is_gimple_call (stmt))
2367 struct cgraph_edge *e = cgraph_edge (node, stmt);
2368 tree decl = gimple_call_fndecl (stmt);
2369 if (e)
2371 if (e->aux)
2373 error ("shared call_stmt:");
2374 cgraph_debug_gimple_stmt (this_cfun, stmt);
2375 error_found = true;
2377 if (!e->indirect_unknown_callee)
2379 if (verify_edge_corresponds_to_fndecl (e, decl))
2381 error ("edge points to wrong declaration:");
2382 debug_tree (e->callee->symbol.decl);
2383 fprintf (stderr," Instead of:");
2384 debug_tree (decl);
2385 error_found = true;
2388 else if (decl)
2390 error ("an indirect edge with unknown callee "
2391 "corresponding to a call_stmt with "
2392 "a known declaration:");
2393 error_found = true;
2394 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2396 e->aux = (void *)1;
2398 else if (decl)
2400 error ("missing callgraph edge for call stmt:");
2401 cgraph_debug_gimple_stmt (this_cfun, stmt);
2402 error_found = true;
2406 pointer_set_destroy (visited_nodes);
2408 else
2409 /* No CFG available?! */
2410 gcc_unreachable ();
2412 for (e = node->callees; e; e = e->next_callee)
2414 if (!e->aux)
2416 error ("edge %s->%s has no corresponding call_stmt",
2417 identifier_to_locale (cgraph_node_name (e->caller)),
2418 identifier_to_locale (cgraph_node_name (e->callee)));
2419 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2420 error_found = true;
2422 e->aux = 0;
2424 for (e = node->indirect_calls; e; e = e->next_callee)
2426 if (!e->aux)
2428 error ("an indirect edge from %s has no corresponding call_stmt",
2429 identifier_to_locale (cgraph_node_name (e->caller)));
2430 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2431 error_found = true;
2433 e->aux = 0;
2436 if (error_found)
2438 dump_cgraph_node (stderr, node);
2439 internal_error ("verify_cgraph_node failed");
2441 timevar_pop (TV_CGRAPH_VERIFY);
2444 /* Verify whole cgraph structure. */
2445 DEBUG_FUNCTION void
2446 verify_cgraph (void)
2448 struct cgraph_node *node;
2450 if (seen_error ())
2451 return;
2453 FOR_EACH_FUNCTION (node)
2454 verify_cgraph_node (node);
2456 #include "gt-cgraph.h"