* configure: Regenerated.
[official-gcc.git] / gcc / cgraph.c
blob3d4703b4b636b1d7b2abc9a86aeb9ed16c528726
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 tree old_decl = current_function_decl;
1131 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1132 if (cfun->cfg
1133 && current_loops)
1135 cfun->curr_properties &= ~PROP_loops;
1136 loop_optimizer_finalize ();
1138 if (cfun->gimple_df)
1140 current_function_decl = node->symbol.decl;
1141 delete_tree_ssa ();
1142 delete_tree_cfg_annotations ();
1143 cfun->eh = NULL;
1144 current_function_decl = old_decl;
1146 if (cfun->cfg)
1148 gcc_assert (dom_computed[0] == DOM_NONE);
1149 gcc_assert (dom_computed[1] == DOM_NONE);
1150 clear_edges ();
1152 if (cfun->value_histograms)
1153 free_histograms ();
1154 pop_cfun();
1155 gimple_set_body (node->symbol.decl, NULL);
1156 VEC_free (ipa_opt_pass, heap,
1157 node->ipa_transforms_to_apply);
1158 /* Struct function hangs a lot of data that would leak if we didn't
1159 removed all pointers to it. */
1160 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1161 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1163 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1164 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1165 of its associated function function declaration because it's
1166 needed to emit debug info later. */
1167 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1168 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1171 /* Remove the node from cgraph. */
1173 void
1174 cgraph_remove_node (struct cgraph_node *node)
1176 struct cgraph_node *n;
1177 int uid = node->uid;
1179 cgraph_call_node_removal_hooks (node);
1180 cgraph_node_remove_callers (node);
1181 cgraph_node_remove_callees (node);
1182 VEC_free (ipa_opt_pass, heap,
1183 node->ipa_transforms_to_apply);
1185 /* Incremental inlining access removed nodes stored in the postorder list.
1187 node->symbol.force_output = false;
1188 for (n = node->nested; n; n = n->next_nested)
1189 n->origin = NULL;
1190 node->nested = NULL;
1191 if (node->origin)
1193 struct cgraph_node **node2 = &node->origin->nested;
1195 while (*node2 != node)
1196 node2 = &(*node2)->next_nested;
1197 *node2 = node->next_nested;
1199 symtab_unregister_node ((symtab_node)node);
1200 if (node->prev_sibling_clone)
1201 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1202 else if (node->clone_of)
1203 node->clone_of->clones = node->next_sibling_clone;
1204 if (node->next_sibling_clone)
1205 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1206 if (node->clones)
1208 struct cgraph_node *n, *next;
1210 if (node->clone_of)
1212 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1213 n->clone_of = node->clone_of;
1214 n->clone_of = node->clone_of;
1215 n->next_sibling_clone = node->clone_of->clones;
1216 if (node->clone_of->clones)
1217 node->clone_of->clones->prev_sibling_clone = n;
1218 node->clone_of->clones = node->clones;
1220 else
1222 /* We are removing node with clones. This makes clones inconsistent,
1223 but assume they will be removed subsequently and just keep clone
1224 tree intact. This can happen in unreachable function removal since
1225 we remove unreachable functions in random order, not by bottom-up
1226 walk of clone trees. */
1227 for (n = node->clones; n; n = next)
1229 next = n->next_sibling_clone;
1230 n->next_sibling_clone = NULL;
1231 n->prev_sibling_clone = NULL;
1232 n->clone_of = NULL;
1237 /* While all the clones are removed after being proceeded, the function
1238 itself is kept in the cgraph even after it is compiled. Check whether
1239 we are done with this body and reclaim it proactively if this is the case.
1241 n = cgraph_get_node (node->symbol.decl);
1242 if (!n
1243 || (!n->clones && !n->clone_of && !n->global.inlined_to
1244 && (cgraph_global_info_ready
1245 && (TREE_ASM_WRITTEN (n->symbol.decl)
1246 || DECL_EXTERNAL (n->symbol.decl)
1247 || !n->analyzed
1248 || n->symbol.in_other_partition))))
1249 cgraph_release_function_body (node);
1251 node->symbol.decl = NULL;
1252 if (node->call_site_hash)
1254 htab_delete (node->call_site_hash);
1255 node->call_site_hash = NULL;
1257 cgraph_n_nodes--;
1259 /* Clear out the node to NULL all pointers and add the node to the free
1260 list. */
1261 memset (node, 0, sizeof(*node));
1262 node->symbol.type = SYMTAB_FUNCTION;
1263 node->uid = uid;
1264 SET_NEXT_FREE_NODE (node, free_nodes);
1265 free_nodes = node;
1268 /* Likewise indicate that a node is having address taken. */
1270 void
1271 cgraph_mark_address_taken_node (struct cgraph_node *node)
1273 gcc_assert (!node->global.inlined_to);
1274 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1275 IPA_REF_ADDR reference exists (and thus it should be set on node
1276 representing alias we take address of) and as a test whether address
1277 of the object was taken (and thus it should be set on node alias is
1278 referring to). We should remove the first use and the remove the
1279 following set. */
1280 node->symbol.address_taken = 1;
1281 node = cgraph_function_or_thunk_node (node, NULL);
1282 node->symbol.address_taken = 1;
1285 /* Return local info for the compiled function. */
1287 struct cgraph_local_info *
1288 cgraph_local_info (tree decl)
1290 struct cgraph_node *node;
1292 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1293 node = cgraph_get_node (decl);
1294 if (!node)
1295 return NULL;
1296 return &node->local;
1299 /* Return local info for the compiled function. */
1301 struct cgraph_global_info *
1302 cgraph_global_info (tree decl)
1304 struct cgraph_node *node;
1306 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1307 node = cgraph_get_node (decl);
1308 if (!node)
1309 return NULL;
1310 return &node->global;
1313 /* Return local info for the compiled function. */
1315 struct cgraph_rtl_info *
1316 cgraph_rtl_info (tree decl)
1318 struct cgraph_node *node;
1320 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1321 node = cgraph_get_node (decl);
1322 if (!node
1323 || (decl != current_function_decl
1324 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1325 return NULL;
1326 return &node->rtl;
1329 /* Return a string describing the failure REASON. */
1331 const char*
1332 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1334 #undef DEFCIFCODE
1335 #define DEFCIFCODE(code, string) string,
1337 static const char *cif_string_table[CIF_N_REASONS] = {
1338 #include "cif-code.def"
1341 /* Signedness of an enum type is implementation defined, so cast it
1342 to unsigned before testing. */
1343 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1344 return cif_string_table[reason];
1347 /* Names used to print out the availability enum. */
1348 const char * const cgraph_availability_names[] =
1349 {"unset", "not_available", "overwritable", "available", "local"};
1352 /* Dump call graph node NODE to file F. */
1354 void
1355 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1357 struct cgraph_edge *edge;
1358 int indirect_calls_count = 0;
1360 dump_symtab_base (f, (symtab_node) node);
1362 if (node->global.inlined_to)
1363 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1364 xstrdup (cgraph_node_name (node)),
1365 node->symbol.order,
1366 xstrdup (cgraph_node_name (node->global.inlined_to)),
1367 node->global.inlined_to->symbol.order);
1368 if (node->clone_of)
1369 fprintf (f, " Clone of %s/%i\n",
1370 cgraph_node_asm_name (node->clone_of),
1371 node->clone_of->symbol.order);
1372 if (cgraph_function_flags_ready)
1373 fprintf (f, " Availability: %s\n",
1374 cgraph_availability_names [cgraph_function_body_availability (node)]);
1376 fprintf (f, " Function flags:");
1377 if (node->analyzed)
1378 fprintf (f, " analyzed");
1379 if (node->count)
1380 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1381 (HOST_WIDEST_INT)node->count);
1382 if (node->origin)
1383 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1384 if (gimple_has_body_p (node->symbol.decl))
1385 fprintf (f, " body");
1386 if (node->process)
1387 fprintf (f, " process");
1388 if (node->local.local)
1389 fprintf (f, " local");
1390 if (node->local.finalized)
1391 fprintf (f, " finalized");
1392 if (node->local.redefined_extern_inline)
1393 fprintf (f, " redefined_extern_inline");
1394 if (node->only_called_at_startup)
1395 fprintf (f, " only_called_at_startup");
1396 if (node->only_called_at_exit)
1397 fprintf (f, " only_called_at_exit");
1398 else if (node->alias)
1399 fprintf (f, " alias");
1400 if (node->tm_clone)
1401 fprintf (f, " tm_clone");
1403 fprintf (f, "\n");
1405 if (node->thunk.thunk_p)
1407 fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1408 "virtual offset %i)\n",
1409 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1410 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1411 (int)node->thunk.fixed_offset,
1412 (int)node->thunk.virtual_value,
1413 (int)node->thunk.virtual_offset_p);
1415 if (node->alias && node->thunk.alias)
1417 fprintf (f, " Alias of %s",
1418 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1419 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1420 fprintf (f, " (asm: %s)",
1421 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1422 fprintf (f, "\n");
1425 fprintf (f, " Called by: ");
1427 for (edge = node->callers; edge; edge = edge->next_caller)
1429 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1430 edge->caller->symbol.order);
1431 if (edge->count)
1432 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1433 (HOST_WIDEST_INT)edge->count);
1434 if (edge->frequency)
1435 fprintf (f, "(%.2f per call) ",
1436 edge->frequency / (double)CGRAPH_FREQ_BASE);
1437 if (!edge->inline_failed)
1438 fprintf(f, "(inlined) ");
1439 if (edge->indirect_inlining_edge)
1440 fprintf(f, "(indirect_inlining) ");
1441 if (edge->can_throw_external)
1442 fprintf(f, "(can throw external) ");
1445 fprintf (f, "\n Calls: ");
1446 for (edge = node->callees; edge; edge = edge->next_callee)
1448 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1449 edge->callee->symbol.order);
1450 if (!edge->inline_failed)
1451 fprintf(f, "(inlined) ");
1452 if (edge->indirect_inlining_edge)
1453 fprintf(f, "(indirect_inlining) ");
1454 if (edge->count)
1455 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1456 (HOST_WIDEST_INT)edge->count);
1457 if (edge->frequency)
1458 fprintf (f, "(%.2f per call) ",
1459 edge->frequency / (double)CGRAPH_FREQ_BASE);
1460 if (edge->can_throw_external)
1461 fprintf(f, "(can throw external) ");
1463 fprintf (f, "\n");
1465 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1466 indirect_calls_count++;
1467 if (indirect_calls_count)
1468 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1469 indirect_calls_count);
1473 /* Dump call graph node NODE to stderr. */
1475 DEBUG_FUNCTION void
1476 debug_cgraph_node (struct cgraph_node *node)
1478 dump_cgraph_node (stderr, node);
1482 /* Dump the callgraph to file F. */
1484 void
1485 dump_cgraph (FILE *f)
1487 struct cgraph_node *node;
1489 fprintf (f, "callgraph:\n\n");
1490 FOR_EACH_FUNCTION (node)
1491 dump_cgraph_node (f, node);
1495 /* Dump the call graph to stderr. */
1497 DEBUG_FUNCTION void
1498 debug_cgraph (void)
1500 dump_cgraph (stderr);
1503 /* Return true when the DECL can possibly be inlined. */
1504 bool
1505 cgraph_function_possibly_inlined_p (tree decl)
1507 if (!cgraph_global_info_ready)
1508 return !DECL_UNINLINABLE (decl);
1509 return DECL_POSSIBLY_INLINED (decl);
1512 /* NODE is no longer nested function; update cgraph accordingly. */
1513 void
1514 cgraph_unnest_node (struct cgraph_node *node)
1516 struct cgraph_node **node2 = &node->origin->nested;
1517 gcc_assert (node->origin);
1519 while (*node2 != node)
1520 node2 = &(*node2)->next_nested;
1521 *node2 = node->next_nested;
1522 node->origin = NULL;
1525 /* Return function availability. See cgraph.h for description of individual
1526 return values. */
1527 enum availability
1528 cgraph_function_body_availability (struct cgraph_node *node)
1530 enum availability avail;
1531 gcc_assert (cgraph_function_flags_ready);
1532 if (!node->analyzed)
1533 avail = AVAIL_NOT_AVAILABLE;
1534 else if (node->local.local)
1535 avail = AVAIL_LOCAL;
1536 else if (!node->symbol.externally_visible)
1537 avail = AVAIL_AVAILABLE;
1538 /* Inline functions are safe to be analyzed even if their symbol can
1539 be overwritten at runtime. It is not meaningful to enforce any sane
1540 behaviour on replacing inline function by different body. */
1541 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1542 avail = AVAIL_AVAILABLE;
1544 /* If the function can be overwritten, return OVERWRITABLE. Take
1545 care at least of two notable extensions - the COMDAT functions
1546 used to share template instantiations in C++ (this is symmetric
1547 to code cp_cannot_inline_tree_fn and probably shall be shared and
1548 the inlinability hooks completely eliminated).
1550 ??? Does the C++ one definition rule allow us to always return
1551 AVAIL_AVAILABLE here? That would be good reason to preserve this
1552 bit. */
1554 else if (decl_replaceable_p (node->symbol.decl)
1555 && !DECL_EXTERNAL (node->symbol.decl))
1556 avail = AVAIL_OVERWRITABLE;
1557 else avail = AVAIL_AVAILABLE;
1559 return avail;
1562 /* Worker for cgraph_node_can_be_local_p. */
1563 static bool
1564 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1565 void *data ATTRIBUTE_UNUSED)
1567 return !(!node->symbol.force_output
1568 && ((DECL_COMDAT (node->symbol.decl)
1569 && !node->symbol.same_comdat_group)
1570 || !node->symbol.externally_visible));
1573 /* Return true if NODE can be made local for API change.
1574 Extern inline functions and C++ COMDAT functions can be made local
1575 at the expense of possible code size growth if function is used in multiple
1576 compilation units. */
1577 bool
1578 cgraph_node_can_be_local_p (struct cgraph_node *node)
1580 return (!node->symbol.address_taken
1581 && !cgraph_for_node_and_aliases (node,
1582 cgraph_node_cannot_be_local_p_1,
1583 NULL, true));
1586 /* Call calback on NODE, thunks and aliases associated to NODE.
1587 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1588 skipped. */
1590 bool
1591 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1592 bool (*callback) (struct cgraph_node *, void *),
1593 void *data,
1594 bool include_overwritable)
1596 struct cgraph_edge *e;
1597 int i;
1598 struct ipa_ref *ref;
1600 if (callback (node, data))
1601 return true;
1602 for (e = node->callers; e; e = e->next_caller)
1603 if (e->caller->thunk.thunk_p
1604 && (include_overwritable
1605 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1606 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1607 include_overwritable))
1608 return true;
1609 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1610 if (ref->use == IPA_REF_ALIAS)
1612 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1613 if (include_overwritable
1614 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1615 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1616 include_overwritable))
1617 return true;
1619 return false;
1622 /* Call calback on NODE and aliases associated to NODE.
1623 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1624 skipped. */
1626 bool
1627 cgraph_for_node_and_aliases (struct cgraph_node *node,
1628 bool (*callback) (struct cgraph_node *, void *),
1629 void *data,
1630 bool include_overwritable)
1632 int i;
1633 struct ipa_ref *ref;
1635 if (callback (node, data))
1636 return true;
1637 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1638 if (ref->use == IPA_REF_ALIAS)
1640 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1641 if (include_overwritable
1642 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1643 if (cgraph_for_node_and_aliases (alias, callback, data,
1644 include_overwritable))
1645 return true;
1647 return false;
1650 /* Worker to bring NODE local. */
1652 static bool
1653 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1655 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1656 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1658 symtab_make_decl_local (node->symbol.decl);
1660 node->symbol.externally_visible = false;
1661 node->local.local = true;
1662 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1663 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1665 return false;
1668 /* Bring NODE local. */
1670 void
1671 cgraph_make_node_local (struct cgraph_node *node)
1673 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1674 NULL, true);
1677 /* Worker to set nothrow flag. */
1679 static bool
1680 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1682 struct cgraph_edge *e;
1684 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1686 if (data != NULL)
1687 for (e = node->callers; e; e = e->next_caller)
1688 e->can_throw_external = false;
1689 return false;
1692 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1693 if any to NOTHROW. */
1695 void
1696 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1698 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1699 (void *)(size_t)nothrow, false);
1702 /* Worker to set const flag. */
1704 static bool
1705 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1707 /* Static constructors and destructors without a side effect can be
1708 optimized out. */
1709 if (data && !((size_t)data & 2))
1711 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1712 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1713 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1714 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1716 TREE_READONLY (node->symbol.decl) = data != NULL;
1717 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1718 return false;
1721 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1722 if any to READONLY. */
1724 void
1725 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1727 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1728 (void *)(size_t)(readonly + (int)looping * 2),
1729 false);
1732 /* Worker to set pure flag. */
1734 static bool
1735 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1737 /* Static pureructors and destructors without a side effect can be
1738 optimized out. */
1739 if (data && !((size_t)data & 2))
1741 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1742 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1743 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1744 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1746 DECL_PURE_P (node->symbol.decl) = data != NULL;
1747 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1748 return false;
1751 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1752 if any to PURE. */
1754 void
1755 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1757 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1758 (void *)(size_t)(pure + (int)looping * 2),
1759 false);
1762 /* Data used by cgraph_propagate_frequency. */
1764 struct cgraph_propagate_frequency_data
1766 bool maybe_unlikely_executed;
1767 bool maybe_executed_once;
1768 bool only_called_at_startup;
1769 bool only_called_at_exit;
1772 /* Worker for cgraph_propagate_frequency_1. */
1774 static bool
1775 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1777 struct cgraph_propagate_frequency_data *d;
1778 struct cgraph_edge *edge;
1780 d = (struct cgraph_propagate_frequency_data *)data;
1781 for (edge = node->callers;
1782 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1783 || d->only_called_at_startup || d->only_called_at_exit);
1784 edge = edge->next_caller)
1786 if (edge->caller != node)
1788 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1789 /* It makes sense to put main() together with the static constructors.
1790 It will be executed for sure, but rest of functions called from
1791 main are definitely not at startup only. */
1792 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1793 d->only_called_at_startup = 0;
1794 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1796 if (!edge->frequency)
1797 continue;
1798 switch (edge->caller->frequency)
1800 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1801 break;
1802 case NODE_FREQUENCY_EXECUTED_ONCE:
1803 if (dump_file && (dump_flags & TDF_DETAILS))
1804 fprintf (dump_file, " Called by %s that is executed once\n",
1805 cgraph_node_name (edge->caller));
1806 d->maybe_unlikely_executed = false;
1807 if (inline_edge_summary (edge)->loop_depth)
1809 d->maybe_executed_once = false;
1810 if (dump_file && (dump_flags & TDF_DETAILS))
1811 fprintf (dump_file, " Called in loop\n");
1813 break;
1814 case NODE_FREQUENCY_HOT:
1815 case NODE_FREQUENCY_NORMAL:
1816 if (dump_file && (dump_flags & TDF_DETAILS))
1817 fprintf (dump_file, " Called by %s that is normal or hot\n",
1818 cgraph_node_name (edge->caller));
1819 d->maybe_unlikely_executed = false;
1820 d->maybe_executed_once = false;
1821 break;
1824 return edge != NULL;
1827 /* See if the frequency of NODE can be updated based on frequencies of its
1828 callers. */
1829 bool
1830 cgraph_propagate_frequency (struct cgraph_node *node)
1832 struct cgraph_propagate_frequency_data d = {true, true, true, true};
1833 bool changed = false;
1835 if (!node->local.local)
1836 return false;
1837 gcc_assert (node->analyzed);
1838 if (dump_file && (dump_flags & TDF_DETAILS))
1839 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
1841 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
1843 if ((d.only_called_at_startup && !d.only_called_at_exit)
1844 && !node->only_called_at_startup)
1846 node->only_called_at_startup = true;
1847 if (dump_file)
1848 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
1849 cgraph_node_name (node));
1850 changed = true;
1852 if ((d.only_called_at_exit && !d.only_called_at_startup)
1853 && !node->only_called_at_exit)
1855 node->only_called_at_exit = true;
1856 if (dump_file)
1857 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
1858 cgraph_node_name (node));
1859 changed = true;
1861 /* These come either from profile or user hints; never update them. */
1862 if (node->frequency == NODE_FREQUENCY_HOT
1863 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1864 return changed;
1865 if (d.maybe_unlikely_executed)
1867 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
1868 if (dump_file)
1869 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
1870 cgraph_node_name (node));
1871 changed = true;
1873 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
1875 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1876 if (dump_file)
1877 fprintf (dump_file, "Node %s promoted to executed once.\n",
1878 cgraph_node_name (node));
1879 changed = true;
1881 return changed;
1884 /* Return true when NODE can not return or throw and thus
1885 it is safe to ignore its side effects for IPA analysis. */
1887 bool
1888 cgraph_node_cannot_return (struct cgraph_node *node)
1890 int flags = flags_from_decl_or_type (node->symbol.decl);
1891 if (!flag_exceptions)
1892 return (flags & ECF_NORETURN) != 0;
1893 else
1894 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1895 == (ECF_NORETURN | ECF_NOTHROW));
1898 /* Return true when call of E can not lead to return from caller
1899 and thus it is safe to ignore its side effects for IPA analysis
1900 when computing side effects of the caller.
1901 FIXME: We could actually mark all edges that have no reaching
1902 patch to EXIT_BLOCK_PTR or throw to get better results. */
1903 bool
1904 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
1906 if (cgraph_node_cannot_return (e->caller))
1907 return true;
1908 if (e->indirect_unknown_callee)
1910 int flags = e->indirect_info->ecf_flags;
1911 if (!flag_exceptions)
1912 return (flags & ECF_NORETURN) != 0;
1913 else
1914 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1915 == (ECF_NORETURN | ECF_NOTHROW));
1917 else
1918 return cgraph_node_cannot_return (e->callee);
1921 /* Return true when function NODE can be removed from callgraph
1922 if all direct calls are eliminated. */
1924 bool
1925 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
1927 gcc_assert (!node->global.inlined_to);
1928 /* Extern inlines can always go, we will use the external definition. */
1929 if (DECL_EXTERNAL (node->symbol.decl))
1930 return true;
1931 /* When function is needed, we can not remove it. */
1932 if (node->symbol.force_output || node->symbol.used_from_other_partition)
1933 return false;
1934 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1935 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1936 return false;
1937 /* Only COMDAT functions can be removed if externally visible. */
1938 if (node->symbol.externally_visible
1939 && (!DECL_COMDAT (node->symbol.decl)
1940 || symtab_used_from_object_file_p ((symtab_node) node)))
1941 return false;
1942 return true;
1945 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1947 static bool
1948 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1950 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
1953 /* Return true when function NODE and its aliases can be removed from callgraph
1954 if all direct calls are eliminated. */
1956 bool
1957 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
1959 /* Extern inlines can always go, we will use the external definition. */
1960 if (DECL_EXTERNAL (node->symbol.decl))
1961 return true;
1962 if (node->symbol.address_taken)
1963 return false;
1964 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
1967 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1969 static bool
1970 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1972 return symtab_used_from_object_file_p ((symtab_node) node);
1975 /* Return true when function NODE can be expected to be removed
1976 from program when direct calls in this compilation unit are removed.
1978 As a special case COMDAT functions are
1979 cgraph_can_remove_if_no_direct_calls_p while the are not
1980 cgraph_only_called_directly_p (it is possible they are called from other
1981 unit)
1983 This function behaves as cgraph_only_called_directly_p because eliminating
1984 all uses of COMDAT function does not make it necessarily disappear from
1985 the program unless we are compiling whole program or we do LTO. In this
1986 case we know we win since dynamic linking will not really discard the
1987 linkonce section. */
1989 bool
1990 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
1992 gcc_assert (!node->global.inlined_to);
1993 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
1994 return false;
1995 if (!in_lto_p && !flag_whole_program)
1996 return cgraph_only_called_directly_p (node);
1997 else
1999 if (DECL_EXTERNAL (node->symbol.decl))
2000 return true;
2001 return cgraph_can_remove_if_no_direct_calls_p (node);
2006 /* Worker for cgraph_only_called_directly_p. */
2008 static bool
2009 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2011 return !cgraph_only_called_directly_or_aliased_p (node);
2014 /* Return true when function NODE and all its aliases are only called
2015 directly.
2016 i.e. it is not externally visible, address was not taken and
2017 it is not used in any other non-standard way. */
2019 bool
2020 cgraph_only_called_directly_p (struct cgraph_node *node)
2022 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2023 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2024 NULL, true);
2028 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2030 static bool
2031 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2033 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
2034 struct cgraph_edge *cs;
2035 enum availability avail;
2036 cgraph_function_or_thunk_node (node, &avail);
2038 if (avail > AVAIL_OVERWRITABLE)
2039 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2040 if (!cs->indirect_inlining_edge)
2041 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
2042 return false;
2045 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2046 (i.e. are not overwritable). */
2048 VEC (cgraph_edge_p, heap) *
2049 collect_callers_of_node (struct cgraph_node *node)
2051 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
2052 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2053 &redirect_callers, false);
2054 return redirect_callers;
2057 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2058 static bool
2059 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2061 node = cgraph_function_or_thunk_node (node, NULL);
2062 node2 = cgraph_function_or_thunk_node (node2, NULL);
2063 while (node != node2 && node2)
2064 node2 = node2->clone_of;
2065 return node2 != NULL;
2068 /* Verify edge E count and frequency. */
2070 static bool
2071 verify_edge_count_and_frequency (struct cgraph_edge *e)
2073 bool error_found = false;
2074 if (e->count < 0)
2076 error ("caller edge count is negative");
2077 error_found = true;
2079 if (e->frequency < 0)
2081 error ("caller edge frequency is negative");
2082 error_found = true;
2084 if (e->frequency > CGRAPH_FREQ_MAX)
2086 error ("caller edge frequency is too large");
2087 error_found = true;
2089 if (gimple_has_body_p (e->caller->symbol.decl)
2090 && !e->caller->global.inlined_to
2091 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2092 Remove this once edges are actually removed from the function at that time. */
2093 && (e->frequency
2094 || (inline_edge_summary_vec
2095 && ((VEC_length(inline_edge_summary_t, inline_edge_summary_vec)
2096 <= (unsigned) e->uid)
2097 || !inline_edge_summary (e)->predicate)))
2098 && (e->frequency
2099 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2100 gimple_bb (e->call_stmt))))
2102 error ("caller edge frequency %i does not match BB frequency %i",
2103 e->frequency,
2104 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2105 gimple_bb (e->call_stmt)));
2106 error_found = true;
2108 return error_found;
2111 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2112 static void
2113 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2115 /* debug_gimple_stmt needs correct cfun */
2116 if (cfun != this_cfun)
2117 set_cfun (this_cfun);
2118 debug_gimple_stmt (stmt);
2121 /* Verify that call graph edge E corresponds to DECL from the associated
2122 statement. Return true if the verification should fail. */
2124 static bool
2125 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2127 struct cgraph_node *node;
2129 if (!decl || e->callee->global.inlined_to)
2130 return false;
2131 node = cgraph_get_node (decl);
2133 /* We do not know if a node from a different partition is an alias or what it
2134 aliases and therefore cannot do the former_clone_of check reliably. */
2135 if (!node || node->symbol.in_other_partition)
2136 return false;
2137 node = cgraph_function_or_thunk_node (node, NULL);
2139 if ((e->callee->former_clone_of != node->symbol.decl
2140 && (!node->same_body_alias
2141 || e->callee->former_clone_of != node->thunk.alias))
2142 /* IPA-CP sometimes redirect edge to clone and then back to the former
2143 function. This ping-pong has to go, eventually. */
2144 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2145 && !clone_of_p (node, e->callee)
2146 /* If decl is a same body alias of some other decl, allow e->callee to be
2147 a clone of a clone of that other decl too. */
2148 && (!node->same_body_alias
2149 || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee)))
2150 return true;
2151 else
2152 return false;
2155 /* Verify cgraph nodes of given cgraph node. */
2156 DEBUG_FUNCTION void
2157 verify_cgraph_node (struct cgraph_node *node)
2159 struct cgraph_edge *e;
2160 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2161 basic_block this_block;
2162 gimple_stmt_iterator gsi;
2163 bool error_found = false;
2165 if (seen_error ())
2166 return;
2168 timevar_push (TV_CGRAPH_VERIFY);
2169 error_found |= verify_symtab_base ((symtab_node) node);
2170 for (e = node->callees; e; e = e->next_callee)
2171 if (e->aux)
2173 error ("aux field set for edge %s->%s",
2174 identifier_to_locale (cgraph_node_name (e->caller)),
2175 identifier_to_locale (cgraph_node_name (e->callee)));
2176 error_found = true;
2178 if (node->count < 0)
2180 error ("execution count is negative");
2181 error_found = true;
2183 if (node->global.inlined_to && node->symbol.same_comdat_group)
2185 error ("inline clone in same comdat group list");
2186 error_found = true;
2188 if (node->global.inlined_to && node->symbol.externally_visible)
2190 error ("externally visible inline clone");
2191 error_found = true;
2193 if (node->global.inlined_to && node->symbol.address_taken)
2195 error ("inline clone with address taken");
2196 error_found = true;
2198 if (node->global.inlined_to && node->symbol.force_output)
2200 error ("inline clone is forced to output");
2201 error_found = true;
2203 for (e = node->indirect_calls; e; e = e->next_callee)
2205 if (e->aux)
2207 error ("aux field set for indirect edge from %s",
2208 identifier_to_locale (cgraph_node_name (e->caller)));
2209 error_found = true;
2211 if (!e->indirect_unknown_callee
2212 || !e->indirect_info)
2214 error ("An indirect edge from %s is not marked as indirect or has "
2215 "associated indirect_info, the corresponding statement is: ",
2216 identifier_to_locale (cgraph_node_name (e->caller)));
2217 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2218 error_found = true;
2221 for (e = node->callers; e; e = e->next_caller)
2223 if (verify_edge_count_and_frequency (e))
2224 error_found = true;
2225 if (!e->inline_failed)
2227 if (node->global.inlined_to
2228 != (e->caller->global.inlined_to
2229 ? e->caller->global.inlined_to : e->caller))
2231 error ("inlined_to pointer is wrong");
2232 error_found = true;
2234 if (node->callers->next_caller)
2236 error ("multiple inline callers");
2237 error_found = true;
2240 else
2241 if (node->global.inlined_to)
2243 error ("inlined_to pointer set for noninline callers");
2244 error_found = true;
2247 for (e = node->indirect_calls; e; e = e->next_callee)
2248 if (verify_edge_count_and_frequency (e))
2249 error_found = true;
2250 if (!node->callers && node->global.inlined_to)
2252 error ("inlined_to pointer is set but no predecessors found");
2253 error_found = true;
2255 if (node->global.inlined_to == node)
2257 error ("inlined_to pointer refers to itself");
2258 error_found = true;
2261 if (node->clone_of)
2263 struct cgraph_node *n;
2264 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2265 if (n == node)
2266 break;
2267 if (!n)
2269 error ("node has wrong clone_of");
2270 error_found = true;
2273 if (node->clones)
2275 struct cgraph_node *n;
2276 for (n = node->clones; n; n = n->next_sibling_clone)
2277 if (n->clone_of != node)
2278 break;
2279 if (n)
2281 error ("node has wrong clone list");
2282 error_found = true;
2285 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2287 error ("node is in clone list but it is not clone");
2288 error_found = true;
2290 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2292 error ("node has wrong prev_clone pointer");
2293 error_found = true;
2295 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2297 error ("double linked list of clones corrupted");
2298 error_found = true;
2301 if (node->analyzed && node->alias)
2303 bool ref_found = false;
2304 int i;
2305 struct ipa_ref *ref;
2307 if (node->callees)
2309 error ("Alias has call edges");
2310 error_found = true;
2312 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2313 i, ref); i++)
2314 if (ref->use != IPA_REF_ALIAS)
2316 error ("Alias has non-alias reference");
2317 error_found = true;
2319 else if (ref_found)
2321 error ("Alias has more than one alias reference");
2322 error_found = true;
2324 else
2325 ref_found = true;
2326 if (!ref_found)
2328 error ("Analyzed alias has no reference");
2329 error_found = true;
2332 if (node->analyzed && node->thunk.thunk_p)
2334 if (!node->callees)
2336 error ("No edge out of thunk node");
2337 error_found = true;
2339 else if (node->callees->next_callee)
2341 error ("More than one edge out of thunk node");
2342 error_found = true;
2344 if (gimple_has_body_p (node->symbol.decl))
2346 error ("Thunk is not supposed to have body");
2347 error_found = true;
2350 else if (node->analyzed && gimple_has_body_p (node->symbol.decl)
2351 && !TREE_ASM_WRITTEN (node->symbol.decl)
2352 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2353 && !flag_wpa)
2355 if (this_cfun->cfg)
2357 /* The nodes we're interested in are never shared, so walk
2358 the tree ignoring duplicates. */
2359 struct pointer_set_t *visited_nodes = pointer_set_create ();
2360 /* Reach the trees by walking over the CFG, and note the
2361 enclosing basic-blocks in the call edges. */
2362 FOR_EACH_BB_FN (this_block, this_cfun)
2363 for (gsi = gsi_start_bb (this_block);
2364 !gsi_end_p (gsi);
2365 gsi_next (&gsi))
2367 gimple stmt = gsi_stmt (gsi);
2368 if (is_gimple_call (stmt))
2370 struct cgraph_edge *e = cgraph_edge (node, stmt);
2371 tree decl = gimple_call_fndecl (stmt);
2372 if (e)
2374 if (e->aux)
2376 error ("shared call_stmt:");
2377 cgraph_debug_gimple_stmt (this_cfun, stmt);
2378 error_found = true;
2380 if (!e->indirect_unknown_callee)
2382 if (verify_edge_corresponds_to_fndecl (e, decl))
2384 error ("edge points to wrong declaration:");
2385 debug_tree (e->callee->symbol.decl);
2386 fprintf (stderr," Instead of:");
2387 debug_tree (decl);
2388 error_found = true;
2391 else if (decl)
2393 error ("an indirect edge with unknown callee "
2394 "corresponding to a call_stmt with "
2395 "a known declaration:");
2396 error_found = true;
2397 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2399 e->aux = (void *)1;
2401 else if (decl)
2403 error ("missing callgraph edge for call stmt:");
2404 cgraph_debug_gimple_stmt (this_cfun, stmt);
2405 error_found = true;
2409 pointer_set_destroy (visited_nodes);
2411 else
2412 /* No CFG available?! */
2413 gcc_unreachable ();
2415 for (e = node->callees; e; e = e->next_callee)
2417 if (!e->aux)
2419 error ("edge %s->%s has no corresponding call_stmt",
2420 identifier_to_locale (cgraph_node_name (e->caller)),
2421 identifier_to_locale (cgraph_node_name (e->callee)));
2422 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2423 error_found = true;
2425 e->aux = 0;
2427 for (e = node->indirect_calls; e; e = e->next_callee)
2429 if (!e->aux)
2431 error ("an indirect edge from %s has no corresponding call_stmt",
2432 identifier_to_locale (cgraph_node_name (e->caller)));
2433 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2434 error_found = true;
2436 e->aux = 0;
2439 if (error_found)
2441 dump_cgraph_node (stderr, node);
2442 internal_error ("verify_cgraph_node failed");
2444 timevar_pop (TV_CGRAPH_VERIFY);
2447 /* Verify whole cgraph structure. */
2448 DEBUG_FUNCTION void
2449 verify_cgraph (void)
2451 struct cgraph_node *node;
2453 if (seen_error ())
2454 return;
2456 FOR_EACH_FUNCTION (node)
2457 verify_cgraph_node (node);
2459 #include "gt-cgraph.h"