* g++.dg/cpp0x/fntmpdefarg3.C: New.
[official-gcc.git] / gcc / cgraph.c
blobc09d319e06f1c104f12991453c71c2e9db720b43
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 || double_int_equal_p
488 (tree_to_double_int (virtual_offset),
489 shwi_to_double_int (virtual_value)));
490 node->thunk.fixed_offset = fixed_offset;
491 node->thunk.this_adjusting = this_adjusting;
492 node->thunk.virtual_value = virtual_value;
493 node->thunk.virtual_offset_p = virtual_offset != NULL;
494 node->thunk.alias = real_alias;
495 node->thunk.thunk_p = true;
496 node->local.finalized = true;
498 return node;
501 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
502 Return NULL if there's no such node. */
504 struct cgraph_node *
505 cgraph_node_for_asm (tree asmname)
507 symtab_node node = symtab_node_for_asm (asmname);
509 /* We do not want to look at inline clones. */
510 for (node = symtab_node_for_asm (asmname); node; node = node->symbol.next_sharing_asm_name)
511 if (symtab_function_p (node) && !cgraph(node)->global.inlined_to)
512 return cgraph (node);
513 return NULL;
516 /* Returns a hash value for X (which really is a cgraph_edge). */
518 static hashval_t
519 edge_hash (const void *x)
521 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
524 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
526 static int
527 edge_eq (const void *x, const void *y)
529 return ((const struct cgraph_edge *) x)->call_stmt == y;
532 /* Add call graph edge E to call site hash of its caller. */
534 static inline void
535 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
537 void **slot;
538 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
539 e->call_stmt,
540 htab_hash_pointer (e->call_stmt),
541 INSERT);
542 gcc_assert (!*slot);
543 *slot = e;
546 /* Return the callgraph edge representing the GIMPLE_CALL statement
547 CALL_STMT. */
549 struct cgraph_edge *
550 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
552 struct cgraph_edge *e, *e2;
553 int n = 0;
555 if (node->call_site_hash)
556 return (struct cgraph_edge *)
557 htab_find_with_hash (node->call_site_hash, call_stmt,
558 htab_hash_pointer (call_stmt));
560 /* This loop may turn out to be performance problem. In such case adding
561 hashtables into call nodes with very many edges is probably best
562 solution. It is not good idea to add pointer into CALL_EXPR itself
563 because we want to make possible having multiple cgraph nodes representing
564 different clones of the same body before the body is actually cloned. */
565 for (e = node->callees; e; e = e->next_callee)
567 if (e->call_stmt == call_stmt)
568 break;
569 n++;
572 if (!e)
573 for (e = node->indirect_calls; e; e = e->next_callee)
575 if (e->call_stmt == call_stmt)
576 break;
577 n++;
580 if (n > 100)
582 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
583 for (e2 = node->callees; e2; e2 = e2->next_callee)
584 cgraph_add_edge_to_call_site_hash (e2);
585 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
586 cgraph_add_edge_to_call_site_hash (e2);
589 return e;
593 /* Change field call_stmt of edge E to NEW_STMT. */
595 void
596 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
598 tree decl;
600 if (e->caller->call_site_hash)
602 htab_remove_elt_with_hash (e->caller->call_site_hash,
603 e->call_stmt,
604 htab_hash_pointer (e->call_stmt));
607 e->call_stmt = new_stmt;
608 if (e->indirect_unknown_callee
609 && (decl = gimple_call_fndecl (new_stmt)))
611 /* Constant propagation (and possibly also inlining?) can turn an
612 indirect call into a direct one. */
613 struct cgraph_node *new_callee = cgraph_get_node (decl);
615 gcc_checking_assert (new_callee);
616 cgraph_make_edge_direct (e, new_callee);
619 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
620 e->can_throw_external = stmt_can_throw_external (new_stmt);
621 pop_cfun ();
622 if (e->caller->call_site_hash)
623 cgraph_add_edge_to_call_site_hash (e);
626 /* Allocate a cgraph_edge structure and fill it with data according to the
627 parameters of which only CALLEE can be NULL (when creating an indirect call
628 edge). */
630 static struct cgraph_edge *
631 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
632 gimple call_stmt, gcov_type count, int freq)
634 struct cgraph_edge *edge;
636 /* LTO does not actually have access to the call_stmt since these
637 have not been loaded yet. */
638 if (call_stmt)
640 /* This is a rather expensive check possibly triggering
641 construction of call stmt hashtable. */
642 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
644 gcc_assert (is_gimple_call (call_stmt));
647 if (free_edges)
649 edge = free_edges;
650 free_edges = NEXT_FREE_EDGE (edge);
652 else
654 edge = ggc_alloc_cgraph_edge ();
655 edge->uid = cgraph_edge_max_uid++;
658 edge->aux = NULL;
659 edge->caller = caller;
660 edge->callee = callee;
661 edge->prev_caller = NULL;
662 edge->next_caller = NULL;
663 edge->prev_callee = NULL;
664 edge->next_callee = NULL;
666 edge->count = count;
667 gcc_assert (count >= 0);
668 edge->frequency = freq;
669 gcc_assert (freq >= 0);
670 gcc_assert (freq <= CGRAPH_FREQ_MAX);
672 edge->call_stmt = call_stmt;
673 push_cfun (DECL_STRUCT_FUNCTION (caller->symbol.decl));
674 edge->can_throw_external
675 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
676 pop_cfun ();
677 if (call_stmt
678 && callee && callee->symbol.decl
679 && !gimple_check_call_matching_types (call_stmt, callee->symbol.decl))
680 edge->call_stmt_cannot_inline_p = true;
681 else
682 edge->call_stmt_cannot_inline_p = false;
683 if (call_stmt && caller->call_site_hash)
684 cgraph_add_edge_to_call_site_hash (edge);
686 edge->indirect_info = NULL;
687 edge->indirect_inlining_edge = 0;
689 return edge;
692 /* Create edge from CALLER to CALLEE in the cgraph. */
694 struct cgraph_edge *
695 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
696 gimple call_stmt, gcov_type count, int freq)
698 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
699 count, freq);
701 edge->indirect_unknown_callee = 0;
702 initialize_inline_failed (edge);
704 edge->next_caller = callee->callers;
705 if (callee->callers)
706 callee->callers->prev_caller = edge;
707 edge->next_callee = caller->callees;
708 if (caller->callees)
709 caller->callees->prev_callee = edge;
710 caller->callees = edge;
711 callee->callers = edge;
713 return edge;
716 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
718 struct cgraph_indirect_call_info *
719 cgraph_allocate_init_indirect_info (void)
721 struct cgraph_indirect_call_info *ii;
723 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
724 ii->param_index = -1;
725 return ii;
728 /* Create an indirect edge with a yet-undetermined callee where the call
729 statement destination is a formal parameter of the caller with index
730 PARAM_INDEX. */
732 struct cgraph_edge *
733 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
734 int ecf_flags,
735 gcov_type count, int freq)
737 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
738 count, freq);
740 edge->indirect_unknown_callee = 1;
741 initialize_inline_failed (edge);
743 edge->indirect_info = cgraph_allocate_init_indirect_info ();
744 edge->indirect_info->ecf_flags = ecf_flags;
746 edge->next_callee = caller->indirect_calls;
747 if (caller->indirect_calls)
748 caller->indirect_calls->prev_callee = edge;
749 caller->indirect_calls = edge;
751 return edge;
754 /* Remove the edge E from the list of the callers of the callee. */
756 static inline void
757 cgraph_edge_remove_callee (struct cgraph_edge *e)
759 gcc_assert (!e->indirect_unknown_callee);
760 if (e->prev_caller)
761 e->prev_caller->next_caller = e->next_caller;
762 if (e->next_caller)
763 e->next_caller->prev_caller = e->prev_caller;
764 if (!e->prev_caller)
765 e->callee->callers = e->next_caller;
768 /* Remove the edge E from the list of the callees of the caller. */
770 static inline void
771 cgraph_edge_remove_caller (struct cgraph_edge *e)
773 if (e->prev_callee)
774 e->prev_callee->next_callee = e->next_callee;
775 if (e->next_callee)
776 e->next_callee->prev_callee = e->prev_callee;
777 if (!e->prev_callee)
779 if (e->indirect_unknown_callee)
780 e->caller->indirect_calls = e->next_callee;
781 else
782 e->caller->callees = e->next_callee;
784 if (e->caller->call_site_hash)
785 htab_remove_elt_with_hash (e->caller->call_site_hash,
786 e->call_stmt,
787 htab_hash_pointer (e->call_stmt));
790 /* Put the edge onto the free list. */
792 static void
793 cgraph_free_edge (struct cgraph_edge *e)
795 int uid = e->uid;
797 /* Clear out the edge so we do not dangle pointers. */
798 memset (e, 0, sizeof (*e));
799 e->uid = uid;
800 NEXT_FREE_EDGE (e) = free_edges;
801 free_edges = e;
804 /* Remove the edge E in the cgraph. */
806 void
807 cgraph_remove_edge (struct cgraph_edge *e)
809 /* Call all edge removal hooks. */
810 cgraph_call_edge_removal_hooks (e);
812 if (!e->indirect_unknown_callee)
813 /* Remove from callers list of the callee. */
814 cgraph_edge_remove_callee (e);
816 /* Remove from callees list of the callers. */
817 cgraph_edge_remove_caller (e);
819 /* Put the edge onto the free list. */
820 cgraph_free_edge (e);
823 /* Set callee of call graph edge E and add it to the corresponding set of
824 callers. */
826 static void
827 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
829 e->prev_caller = NULL;
830 if (n->callers)
831 n->callers->prev_caller = e;
832 e->next_caller = n->callers;
833 n->callers = e;
834 e->callee = n;
837 /* Redirect callee of E to N. The function does not update underlying
838 call expression. */
840 void
841 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
843 /* Remove from callers list of the current callee. */
844 cgraph_edge_remove_callee (e);
846 /* Insert to callers list of the new callee. */
847 cgraph_set_edge_callee (e, n);
850 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
851 CALLEE. DELTA is an integer constant that is to be added to the this
852 pointer (first parameter) to compensate for skipping a thunk adjustment. */
854 void
855 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee)
857 edge->indirect_unknown_callee = 0;
859 /* Get the edge out of the indirect edge list. */
860 if (edge->prev_callee)
861 edge->prev_callee->next_callee = edge->next_callee;
862 if (edge->next_callee)
863 edge->next_callee->prev_callee = edge->prev_callee;
864 if (!edge->prev_callee)
865 edge->caller->indirect_calls = edge->next_callee;
867 /* Put it into the normal callee list */
868 edge->prev_callee = NULL;
869 edge->next_callee = edge->caller->callees;
870 if (edge->caller->callees)
871 edge->caller->callees->prev_callee = edge;
872 edge->caller->callees = edge;
874 /* Insert to callers list of the new callee. */
875 cgraph_set_edge_callee (edge, callee);
877 if (edge->call_stmt)
878 edge->call_stmt_cannot_inline_p
879 = !gimple_check_call_matching_types (edge->call_stmt, callee->symbol.decl);
881 /* We need to re-determine the inlining status of the edge. */
882 initialize_inline_failed (edge);
885 /* If necessary, change the function declaration in the call statement
886 associated with E so that it corresponds to the edge callee. */
888 gimple
889 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
891 tree decl = gimple_call_fndecl (e->call_stmt);
892 gimple new_stmt;
893 gimple_stmt_iterator gsi;
894 #ifdef ENABLE_CHECKING
895 struct cgraph_node *node;
896 #endif
898 if (e->indirect_unknown_callee
899 || decl == e->callee->symbol.decl)
900 return e->call_stmt;
902 #ifdef ENABLE_CHECKING
903 if (decl)
905 node = cgraph_get_node (decl);
906 gcc_assert (!node || !node->clone.combined_args_to_skip);
908 #endif
910 if (cgraph_dump_file)
912 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
913 xstrdup (cgraph_node_name (e->caller)), e->caller->uid,
914 xstrdup (cgraph_node_name (e->callee)), e->callee->uid);
915 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
916 if (e->callee->clone.combined_args_to_skip)
918 fprintf (cgraph_dump_file, " combined args to skip: ");
919 dump_bitmap (cgraph_dump_file,
920 e->callee->clone.combined_args_to_skip);
924 if (e->callee->clone.combined_args_to_skip)
926 int lp_nr;
928 new_stmt
929 = gimple_call_copy_skip_args (e->call_stmt,
930 e->callee->clone.combined_args_to_skip);
931 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
933 if (gimple_vdef (new_stmt)
934 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
935 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
937 gsi = gsi_for_stmt (e->call_stmt);
938 gsi_replace (&gsi, new_stmt, false);
939 /* We need to defer cleaning EH info on the new statement to
940 fixup-cfg. We may not have dominator information at this point
941 and thus would end up with unreachable blocks and have no way
942 to communicate that we need to run CFG cleanup then. */
943 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
944 if (lp_nr != 0)
946 remove_stmt_from_eh_lp (e->call_stmt);
947 add_stmt_to_eh_lp (new_stmt, lp_nr);
950 else
952 new_stmt = e->call_stmt;
953 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
954 update_stmt (new_stmt);
957 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
959 if (cgraph_dump_file)
961 fprintf (cgraph_dump_file, " updated to:");
962 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
964 return new_stmt;
967 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
968 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
969 of OLD_STMT if it was previously call statement.
970 If NEW_STMT is NULL, the call has been dropped without any
971 replacement. */
973 static void
974 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
975 gimple old_stmt, tree old_call,
976 gimple new_stmt)
978 tree new_call = (new_stmt && is_gimple_call (new_stmt))
979 ? gimple_call_fndecl (new_stmt) : 0;
981 /* We are seeing indirect calls, then there is nothing to update. */
982 if (!new_call && !old_call)
983 return;
984 /* See if we turned indirect call into direct call or folded call to one builtin
985 into different builtin. */
986 if (old_call != new_call)
988 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
989 struct cgraph_edge *ne = NULL;
990 gcov_type count;
991 int frequency;
993 if (e)
995 /* See if the edge is already there and has the correct callee. It
996 might be so because of indirect inlining has already updated
997 it. We also might've cloned and redirected the edge. */
998 if (new_call && e->callee)
1000 struct cgraph_node *callee = e->callee;
1001 while (callee)
1003 if (callee->symbol.decl == new_call
1004 || callee->former_clone_of == new_call)
1005 return;
1006 callee = callee->clone_of;
1010 /* Otherwise remove edge and create new one; we can't simply redirect
1011 since function has changed, so inline plan and other information
1012 attached to edge is invalid. */
1013 count = e->count;
1014 frequency = e->frequency;
1015 cgraph_remove_edge (e);
1017 else if (new_call)
1019 /* We are seeing new direct call; compute profile info based on BB. */
1020 basic_block bb = gimple_bb (new_stmt);
1021 count = bb->count;
1022 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1023 bb);
1026 if (new_call)
1028 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1029 new_stmt, count, frequency);
1030 gcc_assert (ne->inline_failed);
1033 /* We only updated the call stmt; update pointer in cgraph edge.. */
1034 else if (old_stmt != new_stmt)
1035 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1038 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1039 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1040 of OLD_STMT before it was updated (updating can happen inplace). */
1042 void
1043 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1045 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1046 struct cgraph_node *node;
1048 gcc_checking_assert (orig);
1049 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1050 if (orig->clones)
1051 for (node = orig->clones; node != orig;)
1053 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1054 if (node->clones)
1055 node = node->clones;
1056 else if (node->next_sibling_clone)
1057 node = node->next_sibling_clone;
1058 else
1060 while (node != orig && !node->next_sibling_clone)
1061 node = node->clone_of;
1062 if (node != orig)
1063 node = node->next_sibling_clone;
1069 /* Remove all callees from the node. */
1071 void
1072 cgraph_node_remove_callees (struct cgraph_node *node)
1074 struct cgraph_edge *e, *f;
1076 /* It is sufficient to remove the edges from the lists of callers of
1077 the callees. The callee list of the node can be zapped with one
1078 assignment. */
1079 for (e = node->callees; e; e = f)
1081 f = e->next_callee;
1082 cgraph_call_edge_removal_hooks (e);
1083 if (!e->indirect_unknown_callee)
1084 cgraph_edge_remove_callee (e);
1085 cgraph_free_edge (e);
1087 for (e = node->indirect_calls; e; e = f)
1089 f = e->next_callee;
1090 cgraph_call_edge_removal_hooks (e);
1091 if (!e->indirect_unknown_callee)
1092 cgraph_edge_remove_callee (e);
1093 cgraph_free_edge (e);
1095 node->indirect_calls = NULL;
1096 node->callees = NULL;
1097 if (node->call_site_hash)
1099 htab_delete (node->call_site_hash);
1100 node->call_site_hash = NULL;
1104 /* Remove all callers from the node. */
1106 static void
1107 cgraph_node_remove_callers (struct cgraph_node *node)
1109 struct cgraph_edge *e, *f;
1111 /* It is sufficient to remove the edges from the lists of callees of
1112 the callers. The caller list of the node can be zapped with one
1113 assignment. */
1114 for (e = node->callers; e; e = f)
1116 f = e->next_caller;
1117 cgraph_call_edge_removal_hooks (e);
1118 cgraph_edge_remove_caller (e);
1119 cgraph_free_edge (e);
1121 node->callers = NULL;
1124 /* Release memory used to represent body of function NODE. */
1126 void
1127 cgraph_release_function_body (struct cgraph_node *node)
1129 if (DECL_STRUCT_FUNCTION (node->symbol.decl))
1131 tree old_decl = current_function_decl;
1132 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1133 if (cfun->cfg
1134 && current_loops)
1136 cfun->curr_properties &= ~PROP_loops;
1137 loop_optimizer_finalize ();
1139 if (cfun->gimple_df)
1141 current_function_decl = node->symbol.decl;
1142 delete_tree_ssa ();
1143 delete_tree_cfg_annotations ();
1144 cfun->eh = NULL;
1145 current_function_decl = old_decl;
1147 if (cfun->cfg)
1149 gcc_assert (dom_computed[0] == DOM_NONE);
1150 gcc_assert (dom_computed[1] == DOM_NONE);
1151 clear_edges ();
1153 if (cfun->value_histograms)
1154 free_histograms ();
1155 pop_cfun();
1156 gimple_set_body (node->symbol.decl, NULL);
1157 VEC_free (ipa_opt_pass, heap,
1158 node->ipa_transforms_to_apply);
1159 /* Struct function hangs a lot of data that would leak if we didn't
1160 removed all pointers to it. */
1161 ggc_free (DECL_STRUCT_FUNCTION (node->symbol.decl));
1162 DECL_STRUCT_FUNCTION (node->symbol.decl) = NULL;
1164 DECL_SAVED_TREE (node->symbol.decl) = NULL;
1165 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1166 of its associated function function declaration because it's
1167 needed to emit debug info later. */
1168 if (!node->abstract_and_needed && DECL_INITIAL (node->symbol.decl))
1169 DECL_INITIAL (node->symbol.decl) = error_mark_node;
1172 /* Remove the node from cgraph. */
1174 void
1175 cgraph_remove_node (struct cgraph_node *node)
1177 struct cgraph_node *n;
1178 int uid = node->uid;
1180 cgraph_call_node_removal_hooks (node);
1181 cgraph_node_remove_callers (node);
1182 cgraph_node_remove_callees (node);
1183 VEC_free (ipa_opt_pass, heap,
1184 node->ipa_transforms_to_apply);
1186 /* Incremental inlining access removed nodes stored in the postorder list.
1188 node->symbol.force_output = false;
1189 for (n = node->nested; n; n = n->next_nested)
1190 n->origin = NULL;
1191 node->nested = NULL;
1192 if (node->origin)
1194 struct cgraph_node **node2 = &node->origin->nested;
1196 while (*node2 != node)
1197 node2 = &(*node2)->next_nested;
1198 *node2 = node->next_nested;
1200 symtab_unregister_node ((symtab_node)node);
1201 if (node->prev_sibling_clone)
1202 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1203 else if (node->clone_of)
1204 node->clone_of->clones = node->next_sibling_clone;
1205 if (node->next_sibling_clone)
1206 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1207 if (node->clones)
1209 struct cgraph_node *n, *next;
1211 if (node->clone_of)
1213 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1214 n->clone_of = node->clone_of;
1215 n->clone_of = node->clone_of;
1216 n->next_sibling_clone = node->clone_of->clones;
1217 if (node->clone_of->clones)
1218 node->clone_of->clones->prev_sibling_clone = n;
1219 node->clone_of->clones = node->clones;
1221 else
1223 /* We are removing node with clones. This makes clones inconsistent,
1224 but assume they will be removed subsequently and just keep clone
1225 tree intact. This can happen in unreachable function removal since
1226 we remove unreachable functions in random order, not by bottom-up
1227 walk of clone trees. */
1228 for (n = node->clones; n; n = next)
1230 next = n->next_sibling_clone;
1231 n->next_sibling_clone = NULL;
1232 n->prev_sibling_clone = NULL;
1233 n->clone_of = NULL;
1238 /* While all the clones are removed after being proceeded, the function
1239 itself is kept in the cgraph even after it is compiled. Check whether
1240 we are done with this body and reclaim it proactively if this is the case.
1242 n = cgraph_get_node (node->symbol.decl);
1243 if (!n
1244 || (!n->clones && !n->clone_of && !n->global.inlined_to
1245 && (cgraph_global_info_ready
1246 && (TREE_ASM_WRITTEN (n->symbol.decl)
1247 || DECL_EXTERNAL (n->symbol.decl)
1248 || !n->analyzed
1249 || n->symbol.in_other_partition))))
1250 cgraph_release_function_body (node);
1252 node->symbol.decl = NULL;
1253 if (node->call_site_hash)
1255 htab_delete (node->call_site_hash);
1256 node->call_site_hash = NULL;
1258 cgraph_n_nodes--;
1260 /* Clear out the node to NULL all pointers and add the node to the free
1261 list. */
1262 memset (node, 0, sizeof(*node));
1263 node->symbol.type = SYMTAB_FUNCTION;
1264 node->uid = uid;
1265 SET_NEXT_FREE_NODE (node, free_nodes);
1266 free_nodes = node;
1269 /* Likewise indicate that a node is having address taken. */
1271 void
1272 cgraph_mark_address_taken_node (struct cgraph_node *node)
1274 gcc_assert (!node->global.inlined_to);
1275 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1276 IPA_REF_ADDR reference exists (and thus it should be set on node
1277 representing alias we take address of) and as a test whether address
1278 of the object was taken (and thus it should be set on node alias is
1279 referring to). We should remove the first use and the remove the
1280 following set. */
1281 node->symbol.address_taken = 1;
1282 node = cgraph_function_or_thunk_node (node, NULL);
1283 node->symbol.address_taken = 1;
1286 /* Return local info for the compiled function. */
1288 struct cgraph_local_info *
1289 cgraph_local_info (tree decl)
1291 struct cgraph_node *node;
1293 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1294 node = cgraph_get_node (decl);
1295 if (!node)
1296 return NULL;
1297 return &node->local;
1300 /* Return local info for the compiled function. */
1302 struct cgraph_global_info *
1303 cgraph_global_info (tree decl)
1305 struct cgraph_node *node;
1307 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1308 node = cgraph_get_node (decl);
1309 if (!node)
1310 return NULL;
1311 return &node->global;
1314 /* Return local info for the compiled function. */
1316 struct cgraph_rtl_info *
1317 cgraph_rtl_info (tree decl)
1319 struct cgraph_node *node;
1321 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1322 node = cgraph_get_node (decl);
1323 if (!node
1324 || (decl != current_function_decl
1325 && !TREE_ASM_WRITTEN (node->symbol.decl)))
1326 return NULL;
1327 return &node->rtl;
1330 /* Return a string describing the failure REASON. */
1332 const char*
1333 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1335 #undef DEFCIFCODE
1336 #define DEFCIFCODE(code, string) string,
1338 static const char *cif_string_table[CIF_N_REASONS] = {
1339 #include "cif-code.def"
1342 /* Signedness of an enum type is implementation defined, so cast it
1343 to unsigned before testing. */
1344 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1345 return cif_string_table[reason];
1348 /* Names used to print out the availability enum. */
1349 const char * const cgraph_availability_names[] =
1350 {"unset", "not_available", "overwritable", "available", "local"};
1353 /* Dump call graph node NODE to file F. */
1355 void
1356 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1358 struct cgraph_edge *edge;
1359 int indirect_calls_count = 0;
1361 dump_symtab_base (f, (symtab_node) node);
1363 if (node->global.inlined_to)
1364 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1365 xstrdup (cgraph_node_name (node)),
1366 node->symbol.order,
1367 xstrdup (cgraph_node_name (node->global.inlined_to)),
1368 node->global.inlined_to->symbol.order);
1369 if (node->clone_of)
1370 fprintf (f, " Clone of %s/%i\n",
1371 cgraph_node_asm_name (node->clone_of),
1372 node->clone_of->symbol.order);
1373 if (cgraph_function_flags_ready)
1374 fprintf (f, " Availability: %s\n",
1375 cgraph_availability_names [cgraph_function_body_availability (node)]);
1377 fprintf (f, " Function flags:");
1378 if (node->analyzed)
1379 fprintf (f, " analyzed");
1380 if (node->count)
1381 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1382 (HOST_WIDEST_INT)node->count);
1383 if (node->origin)
1384 fprintf (f, " nested in: %s", cgraph_node_asm_name (node->origin));
1385 if (gimple_has_body_p (node->symbol.decl))
1386 fprintf (f, " body");
1387 if (node->process)
1388 fprintf (f, " process");
1389 if (node->local.local)
1390 fprintf (f, " local");
1391 if (node->local.finalized)
1392 fprintf (f, " finalized");
1393 if (node->local.redefined_extern_inline)
1394 fprintf (f, " redefined_extern_inline");
1395 if (node->only_called_at_startup)
1396 fprintf (f, " only_called_at_startup");
1397 if (node->only_called_at_exit)
1398 fprintf (f, " only_called_at_exit");
1399 else if (node->alias)
1400 fprintf (f, " alias");
1401 if (node->tm_clone)
1402 fprintf (f, " tm_clone");
1404 fprintf (f, "\n");
1406 if (node->thunk.thunk_p)
1408 fprintf (f, " Thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1409 "virtual offset %i)\n",
1410 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1411 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1412 (int)node->thunk.fixed_offset,
1413 (int)node->thunk.virtual_value,
1414 (int)node->thunk.virtual_offset_p);
1416 if (node->alias && node->thunk.alias)
1418 fprintf (f, " Alias of %s",
1419 lang_hooks.decl_printable_name (node->thunk.alias, 2));
1420 if (DECL_ASSEMBLER_NAME_SET_P (node->thunk.alias))
1421 fprintf (f, " (asm: %s)",
1422 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)));
1423 fprintf (f, "\n");
1426 fprintf (f, " Called by: ");
1428 for (edge = node->callers; edge; edge = edge->next_caller)
1430 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->caller),
1431 edge->caller->symbol.order);
1432 if (edge->count)
1433 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1434 (HOST_WIDEST_INT)edge->count);
1435 if (edge->frequency)
1436 fprintf (f, "(%.2f per call) ",
1437 edge->frequency / (double)CGRAPH_FREQ_BASE);
1438 if (!edge->inline_failed)
1439 fprintf(f, "(inlined) ");
1440 if (edge->indirect_inlining_edge)
1441 fprintf(f, "(indirect_inlining) ");
1442 if (edge->can_throw_external)
1443 fprintf(f, "(can throw external) ");
1446 fprintf (f, "\n Calls: ");
1447 for (edge = node->callees; edge; edge = edge->next_callee)
1449 fprintf (f, "%s/%i ", cgraph_node_asm_name (edge->callee),
1450 edge->callee->symbol.order);
1451 if (!edge->inline_failed)
1452 fprintf(f, "(inlined) ");
1453 if (edge->indirect_inlining_edge)
1454 fprintf(f, "(indirect_inlining) ");
1455 if (edge->count)
1456 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1457 (HOST_WIDEST_INT)edge->count);
1458 if (edge->frequency)
1459 fprintf (f, "(%.2f per call) ",
1460 edge->frequency / (double)CGRAPH_FREQ_BASE);
1461 if (edge->can_throw_external)
1462 fprintf(f, "(can throw external) ");
1464 fprintf (f, "\n");
1466 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1467 indirect_calls_count++;
1468 if (indirect_calls_count)
1469 fprintf (f, " Has %i outgoing edges for indirect calls.\n",
1470 indirect_calls_count);
1474 /* Dump call graph node NODE to stderr. */
1476 DEBUG_FUNCTION void
1477 debug_cgraph_node (struct cgraph_node *node)
1479 dump_cgraph_node (stderr, node);
1483 /* Dump the callgraph to file F. */
1485 void
1486 dump_cgraph (FILE *f)
1488 struct cgraph_node *node;
1490 fprintf (f, "callgraph:\n\n");
1491 FOR_EACH_FUNCTION (node)
1492 dump_cgraph_node (f, node);
1496 /* Dump the call graph to stderr. */
1498 DEBUG_FUNCTION void
1499 debug_cgraph (void)
1501 dump_cgraph (stderr);
1504 /* Return true when the DECL can possibly be inlined. */
1505 bool
1506 cgraph_function_possibly_inlined_p (tree decl)
1508 if (!cgraph_global_info_ready)
1509 return !DECL_UNINLINABLE (decl);
1510 return DECL_POSSIBLY_INLINED (decl);
1513 /* NODE is no longer nested function; update cgraph accordingly. */
1514 void
1515 cgraph_unnest_node (struct cgraph_node *node)
1517 struct cgraph_node **node2 = &node->origin->nested;
1518 gcc_assert (node->origin);
1520 while (*node2 != node)
1521 node2 = &(*node2)->next_nested;
1522 *node2 = node->next_nested;
1523 node->origin = NULL;
1526 /* Return function availability. See cgraph.h for description of individual
1527 return values. */
1528 enum availability
1529 cgraph_function_body_availability (struct cgraph_node *node)
1531 enum availability avail;
1532 gcc_assert (cgraph_function_flags_ready);
1533 if (!node->analyzed)
1534 avail = AVAIL_NOT_AVAILABLE;
1535 else if (node->local.local)
1536 avail = AVAIL_LOCAL;
1537 else if (!node->symbol.externally_visible)
1538 avail = AVAIL_AVAILABLE;
1539 /* Inline functions are safe to be analyzed even if their symbol can
1540 be overwritten at runtime. It is not meaningful to enforce any sane
1541 behaviour on replacing inline function by different body. */
1542 else if (DECL_DECLARED_INLINE_P (node->symbol.decl))
1543 avail = AVAIL_AVAILABLE;
1545 /* If the function can be overwritten, return OVERWRITABLE. Take
1546 care at least of two notable extensions - the COMDAT functions
1547 used to share template instantiations in C++ (this is symmetric
1548 to code cp_cannot_inline_tree_fn and probably shall be shared and
1549 the inlinability hooks completely eliminated).
1551 ??? Does the C++ one definition rule allow us to always return
1552 AVAIL_AVAILABLE here? That would be good reason to preserve this
1553 bit. */
1555 else if (decl_replaceable_p (node->symbol.decl)
1556 && !DECL_EXTERNAL (node->symbol.decl))
1557 avail = AVAIL_OVERWRITABLE;
1558 else avail = AVAIL_AVAILABLE;
1560 return avail;
1563 /* Worker for cgraph_node_can_be_local_p. */
1564 static bool
1565 cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
1566 void *data ATTRIBUTE_UNUSED)
1568 return !(!node->symbol.force_output
1569 && ((DECL_COMDAT (node->symbol.decl)
1570 && !node->symbol.same_comdat_group)
1571 || !node->symbol.externally_visible));
1574 /* Return true if NODE can be made local for API change.
1575 Extern inline functions and C++ COMDAT functions can be made local
1576 at the expense of possible code size growth if function is used in multiple
1577 compilation units. */
1578 bool
1579 cgraph_node_can_be_local_p (struct cgraph_node *node)
1581 return (!node->symbol.address_taken
1582 && !cgraph_for_node_and_aliases (node,
1583 cgraph_node_cannot_be_local_p_1,
1584 NULL, true));
1587 /* Call calback on NODE, thunks and aliases associated to NODE.
1588 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1589 skipped. */
1591 bool
1592 cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
1593 bool (*callback) (struct cgraph_node *, void *),
1594 void *data,
1595 bool include_overwritable)
1597 struct cgraph_edge *e;
1598 int i;
1599 struct ipa_ref *ref;
1601 if (callback (node, data))
1602 return true;
1603 for (e = node->callers; e; e = e->next_caller)
1604 if (e->caller->thunk.thunk_p
1605 && (include_overwritable
1606 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
1607 if (cgraph_for_node_thunks_and_aliases (e->caller, callback, data,
1608 include_overwritable))
1609 return true;
1610 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1611 if (ref->use == IPA_REF_ALIAS)
1613 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1614 if (include_overwritable
1615 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1616 if (cgraph_for_node_thunks_and_aliases (alias, callback, data,
1617 include_overwritable))
1618 return true;
1620 return false;
1623 /* Call calback on NODE and aliases associated to NODE.
1624 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1625 skipped. */
1627 bool
1628 cgraph_for_node_and_aliases (struct cgraph_node *node,
1629 bool (*callback) (struct cgraph_node *, void *),
1630 void *data,
1631 bool include_overwritable)
1633 int i;
1634 struct ipa_ref *ref;
1636 if (callback (node, data))
1637 return true;
1638 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
1639 if (ref->use == IPA_REF_ALIAS)
1641 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1642 if (include_overwritable
1643 || cgraph_function_body_availability (alias) > AVAIL_OVERWRITABLE)
1644 if (cgraph_for_node_and_aliases (alias, callback, data,
1645 include_overwritable))
1646 return true;
1648 return false;
1651 /* Worker to bring NODE local. */
1653 static bool
1654 cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1656 gcc_checking_assert (cgraph_node_can_be_local_p (node));
1657 if (DECL_COMDAT (node->symbol.decl) || DECL_EXTERNAL (node->symbol.decl))
1659 symtab_make_decl_local (node->symbol.decl);
1661 node->symbol.externally_visible = false;
1662 node->local.local = true;
1663 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1664 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
1666 return false;
1669 /* Bring NODE local. */
1671 void
1672 cgraph_make_node_local (struct cgraph_node *node)
1674 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
1675 NULL, true);
1678 /* Worker to set nothrow flag. */
1680 static bool
1681 cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
1683 struct cgraph_edge *e;
1685 TREE_NOTHROW (node->symbol.decl) = data != NULL;
1687 if (data != NULL)
1688 for (e = node->callers; e; e = e->next_caller)
1689 e->can_throw_external = false;
1690 return false;
1693 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
1694 if any to NOTHROW. */
1696 void
1697 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
1699 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
1700 (void *)(size_t)nothrow, false);
1703 /* Worker to set const flag. */
1705 static bool
1706 cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
1708 /* Static constructors and destructors without a side effect can be
1709 optimized out. */
1710 if (data && !((size_t)data & 2))
1712 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1713 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1714 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1715 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1717 TREE_READONLY (node->symbol.decl) = data != NULL;
1718 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1719 return false;
1722 /* Set TREE_READONLY on NODE's decl and on aliases of NODE
1723 if any to READONLY. */
1725 void
1726 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
1728 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
1729 (void *)(size_t)(readonly + (int)looping * 2),
1730 false);
1733 /* Worker to set pure flag. */
1735 static bool
1736 cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
1738 /* Static pureructors and destructors without a side effect can be
1739 optimized out. */
1740 if (data && !((size_t)data & 2))
1742 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1743 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
1744 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1745 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
1747 DECL_PURE_P (node->symbol.decl) = data != NULL;
1748 DECL_LOOPING_CONST_OR_PURE_P (node->symbol.decl) = ((size_t)data & 2) != 0;
1749 return false;
1752 /* Set DECL_PURE_P on NODE's decl and on aliases of NODE
1753 if any to PURE. */
1755 void
1756 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
1758 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
1759 (void *)(size_t)(pure + (int)looping * 2),
1760 false);
1763 /* Data used by cgraph_propagate_frequency. */
1765 struct cgraph_propagate_frequency_data
1767 bool maybe_unlikely_executed;
1768 bool maybe_executed_once;
1769 bool only_called_at_startup;
1770 bool only_called_at_exit;
1773 /* Worker for cgraph_propagate_frequency_1. */
1775 static bool
1776 cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
1778 struct cgraph_propagate_frequency_data *d;
1779 struct cgraph_edge *edge;
1781 d = (struct cgraph_propagate_frequency_data *)data;
1782 for (edge = node->callers;
1783 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
1784 || d->only_called_at_startup || d->only_called_at_exit);
1785 edge = edge->next_caller)
1787 if (edge->caller != node)
1789 d->only_called_at_startup &= edge->caller->only_called_at_startup;
1790 /* It makes sense to put main() together with the static constructors.
1791 It will be executed for sure, but rest of functions called from
1792 main are definitely not at startup only. */
1793 if (MAIN_NAME_P (DECL_NAME (edge->caller->symbol.decl)))
1794 d->only_called_at_startup = 0;
1795 d->only_called_at_exit &= edge->caller->only_called_at_exit;
1797 if (!edge->frequency)
1798 continue;
1799 switch (edge->caller->frequency)
1801 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
1802 break;
1803 case NODE_FREQUENCY_EXECUTED_ONCE:
1804 if (dump_file && (dump_flags & TDF_DETAILS))
1805 fprintf (dump_file, " Called by %s that is executed once\n",
1806 cgraph_node_name (edge->caller));
1807 d->maybe_unlikely_executed = false;
1808 if (inline_edge_summary (edge)->loop_depth)
1810 d->maybe_executed_once = false;
1811 if (dump_file && (dump_flags & TDF_DETAILS))
1812 fprintf (dump_file, " Called in loop\n");
1814 break;
1815 case NODE_FREQUENCY_HOT:
1816 case NODE_FREQUENCY_NORMAL:
1817 if (dump_file && (dump_flags & TDF_DETAILS))
1818 fprintf (dump_file, " Called by %s that is normal or hot\n",
1819 cgraph_node_name (edge->caller));
1820 d->maybe_unlikely_executed = false;
1821 d->maybe_executed_once = false;
1822 break;
1825 return edge != NULL;
1828 /* See if the frequency of NODE can be updated based on frequencies of its
1829 callers. */
1830 bool
1831 cgraph_propagate_frequency (struct cgraph_node *node)
1833 struct cgraph_propagate_frequency_data d = {true, true, true, true};
1834 bool changed = false;
1836 if (!node->local.local)
1837 return false;
1838 gcc_assert (node->analyzed);
1839 if (dump_file && (dump_flags & TDF_DETAILS))
1840 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
1842 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
1844 if ((d.only_called_at_startup && !d.only_called_at_exit)
1845 && !node->only_called_at_startup)
1847 node->only_called_at_startup = true;
1848 if (dump_file)
1849 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
1850 cgraph_node_name (node));
1851 changed = true;
1853 if ((d.only_called_at_exit && !d.only_called_at_startup)
1854 && !node->only_called_at_exit)
1856 node->only_called_at_exit = true;
1857 if (dump_file)
1858 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
1859 cgraph_node_name (node));
1860 changed = true;
1862 /* These come either from profile or user hints; never update them. */
1863 if (node->frequency == NODE_FREQUENCY_HOT
1864 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1865 return changed;
1866 if (d.maybe_unlikely_executed)
1868 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
1869 if (dump_file)
1870 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
1871 cgraph_node_name (node));
1872 changed = true;
1874 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
1876 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
1877 if (dump_file)
1878 fprintf (dump_file, "Node %s promoted to executed once.\n",
1879 cgraph_node_name (node));
1880 changed = true;
1882 return changed;
1885 /* Return true when NODE can not return or throw and thus
1886 it is safe to ignore its side effects for IPA analysis. */
1888 bool
1889 cgraph_node_cannot_return (struct cgraph_node *node)
1891 int flags = flags_from_decl_or_type (node->symbol.decl);
1892 if (!flag_exceptions)
1893 return (flags & ECF_NORETURN) != 0;
1894 else
1895 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1896 == (ECF_NORETURN | ECF_NOTHROW));
1899 /* Return true when call of E can not lead to return from caller
1900 and thus it is safe to ignore its side effects for IPA analysis
1901 when computing side effects of the caller.
1902 FIXME: We could actually mark all edges that have no reaching
1903 patch to EXIT_BLOCK_PTR or throw to get better results. */
1904 bool
1905 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
1907 if (cgraph_node_cannot_return (e->caller))
1908 return true;
1909 if (e->indirect_unknown_callee)
1911 int flags = e->indirect_info->ecf_flags;
1912 if (!flag_exceptions)
1913 return (flags & ECF_NORETURN) != 0;
1914 else
1915 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
1916 == (ECF_NORETURN | ECF_NOTHROW));
1918 else
1919 return cgraph_node_cannot_return (e->callee);
1922 /* Return true when function NODE can be removed from callgraph
1923 if all direct calls are eliminated. */
1925 bool
1926 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
1928 gcc_assert (!node->global.inlined_to);
1929 /* Extern inlines can always go, we will use the external definition. */
1930 if (DECL_EXTERNAL (node->symbol.decl))
1931 return true;
1932 /* When function is needed, we can not remove it. */
1933 if (node->symbol.force_output || node->symbol.used_from_other_partition)
1934 return false;
1935 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1936 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1937 return false;
1938 /* Only COMDAT functions can be removed if externally visible. */
1939 if (node->symbol.externally_visible
1940 && (!DECL_COMDAT (node->symbol.decl)
1941 || symtab_used_from_object_file_p ((symtab_node) node)))
1942 return false;
1943 return true;
1946 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1948 static bool
1949 nonremovable_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1951 return !cgraph_can_remove_if_no_direct_calls_and_refs_p (node);
1954 /* Return true when function NODE and its aliases can be removed from callgraph
1955 if all direct calls are eliminated. */
1957 bool
1958 cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node)
1960 /* Extern inlines can always go, we will use the external definition. */
1961 if (DECL_EXTERNAL (node->symbol.decl))
1962 return true;
1963 if (node->symbol.address_taken)
1964 return false;
1965 return !cgraph_for_node_and_aliases (node, nonremovable_p, NULL, true);
1968 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
1970 static bool
1971 used_from_object_file_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1973 return symtab_used_from_object_file_p ((symtab_node) node);
1976 /* Return true when function NODE can be expected to be removed
1977 from program when direct calls in this compilation unit are removed.
1979 As a special case COMDAT functions are
1980 cgraph_can_remove_if_no_direct_calls_p while the are not
1981 cgraph_only_called_directly_p (it is possible they are called from other
1982 unit)
1984 This function behaves as cgraph_only_called_directly_p because eliminating
1985 all uses of COMDAT function does not make it necessarily disappear from
1986 the program unless we are compiling whole program or we do LTO. In this
1987 case we know we win since dynamic linking will not really discard the
1988 linkonce section. */
1990 bool
1991 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
1993 gcc_assert (!node->global.inlined_to);
1994 if (cgraph_for_node_and_aliases (node, used_from_object_file_p, NULL, true))
1995 return false;
1996 if (!in_lto_p && !flag_whole_program)
1997 return cgraph_only_called_directly_p (node);
1998 else
2000 if (DECL_EXTERNAL (node->symbol.decl))
2001 return true;
2002 return cgraph_can_remove_if_no_direct_calls_p (node);
2007 /* Worker for cgraph_only_called_directly_p. */
2009 static bool
2010 cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2012 return !cgraph_only_called_directly_or_aliased_p (node);
2015 /* Return true when function NODE and all its aliases are only called
2016 directly.
2017 i.e. it is not externally visible, address was not taken and
2018 it is not used in any other non-standard way. */
2020 bool
2021 cgraph_only_called_directly_p (struct cgraph_node *node)
2023 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
2024 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
2025 NULL, true);
2029 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2031 static bool
2032 collect_callers_of_node_1 (struct cgraph_node *node, void *data)
2034 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
2035 struct cgraph_edge *cs;
2036 enum availability avail;
2037 cgraph_function_or_thunk_node (node, &avail);
2039 if (avail > AVAIL_OVERWRITABLE)
2040 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2041 if (!cs->indirect_inlining_edge)
2042 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
2043 return false;
2046 /* Collect all callers of NODE and its aliases that are known to lead to NODE
2047 (i.e. are not overwritable). */
2049 VEC (cgraph_edge_p, heap) *
2050 collect_callers_of_node (struct cgraph_node *node)
2052 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
2053 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
2054 &redirect_callers, false);
2055 return redirect_callers;
2058 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
2059 static bool
2060 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
2062 node = cgraph_function_or_thunk_node (node, NULL);
2063 node2 = cgraph_function_or_thunk_node (node2, NULL);
2064 while (node != node2 && node2)
2065 node2 = node2->clone_of;
2066 return node2 != NULL;
2069 /* Verify edge E count and frequency. */
2071 static bool
2072 verify_edge_count_and_frequency (struct cgraph_edge *e)
2074 bool error_found = false;
2075 if (e->count < 0)
2077 error ("caller edge count is negative");
2078 error_found = true;
2080 if (e->frequency < 0)
2082 error ("caller edge frequency is negative");
2083 error_found = true;
2085 if (e->frequency > CGRAPH_FREQ_MAX)
2087 error ("caller edge frequency is too large");
2088 error_found = true;
2090 if (gimple_has_body_p (e->caller->symbol.decl)
2091 && !e->caller->global.inlined_to
2092 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2093 Remove this once edges are actually removed from the function at that time. */
2094 && (e->frequency
2095 || (inline_edge_summary_vec
2096 && ((VEC_length(inline_edge_summary_t, inline_edge_summary_vec)
2097 <= (unsigned) e->uid)
2098 || !inline_edge_summary (e)->predicate)))
2099 && (e->frequency
2100 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2101 gimple_bb (e->call_stmt))))
2103 error ("caller edge frequency %i does not match BB frequency %i",
2104 e->frequency,
2105 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
2106 gimple_bb (e->call_stmt)));
2107 error_found = true;
2109 return error_found;
2112 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2113 static void
2114 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
2116 /* debug_gimple_stmt needs correct cfun */
2117 if (cfun != this_cfun)
2118 set_cfun (this_cfun);
2119 debug_gimple_stmt (stmt);
2122 /* Verify that call graph edge E corresponds to DECL from the associated
2123 statement. Return true if the verification should fail. */
2125 static bool
2126 verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
2128 struct cgraph_node *node;
2130 if (!decl || e->callee->global.inlined_to)
2131 return false;
2132 node = cgraph_get_node (decl);
2134 /* We do not know if a node from a different partition is an alias or what it
2135 aliases and therefore cannot do the former_clone_of check reliably. */
2136 if (!node || node->symbol.in_other_partition)
2137 return false;
2138 node = cgraph_function_or_thunk_node (node, NULL);
2140 if ((e->callee->former_clone_of != node->symbol.decl
2141 && (!node->same_body_alias
2142 || e->callee->former_clone_of != node->thunk.alias))
2143 /* IPA-CP sometimes redirect edge to clone and then back to the former
2144 function. This ping-pong has to go, eventually. */
2145 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
2146 && !clone_of_p (node, e->callee)
2147 /* If decl is a same body alias of some other decl, allow e->callee to be
2148 a clone of a clone of that other decl too. */
2149 && (!node->same_body_alias
2150 || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee)))
2151 return true;
2152 else
2153 return false;
2156 /* Verify cgraph nodes of given cgraph node. */
2157 DEBUG_FUNCTION void
2158 verify_cgraph_node (struct cgraph_node *node)
2160 struct cgraph_edge *e;
2161 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
2162 basic_block this_block;
2163 gimple_stmt_iterator gsi;
2164 bool error_found = false;
2166 if (seen_error ())
2167 return;
2169 timevar_push (TV_CGRAPH_VERIFY);
2170 error_found |= verify_symtab_base ((symtab_node) node);
2171 for (e = node->callees; e; e = e->next_callee)
2172 if (e->aux)
2174 error ("aux field set for edge %s->%s",
2175 identifier_to_locale (cgraph_node_name (e->caller)),
2176 identifier_to_locale (cgraph_node_name (e->callee)));
2177 error_found = true;
2179 if (node->count < 0)
2181 error ("execution count is negative");
2182 error_found = true;
2184 if (node->global.inlined_to && node->symbol.same_comdat_group)
2186 error ("inline clone in same comdat group list");
2187 error_found = true;
2189 if (node->global.inlined_to && node->symbol.externally_visible)
2191 error ("externally visible inline clone");
2192 error_found = true;
2194 if (node->global.inlined_to && node->symbol.address_taken)
2196 error ("inline clone with address taken");
2197 error_found = true;
2199 if (node->global.inlined_to && node->symbol.force_output)
2201 error ("inline clone is forced to output");
2202 error_found = true;
2204 for (e = node->indirect_calls; e; e = e->next_callee)
2206 if (e->aux)
2208 error ("aux field set for indirect edge from %s",
2209 identifier_to_locale (cgraph_node_name (e->caller)));
2210 error_found = true;
2212 if (!e->indirect_unknown_callee
2213 || !e->indirect_info)
2215 error ("An indirect edge from %s is not marked as indirect or has "
2216 "associated indirect_info, the corresponding statement is: ",
2217 identifier_to_locale (cgraph_node_name (e->caller)));
2218 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2219 error_found = true;
2222 for (e = node->callers; e; e = e->next_caller)
2224 if (verify_edge_count_and_frequency (e))
2225 error_found = true;
2226 if (!e->inline_failed)
2228 if (node->global.inlined_to
2229 != (e->caller->global.inlined_to
2230 ? e->caller->global.inlined_to : e->caller))
2232 error ("inlined_to pointer is wrong");
2233 error_found = true;
2235 if (node->callers->next_caller)
2237 error ("multiple inline callers");
2238 error_found = true;
2241 else
2242 if (node->global.inlined_to)
2244 error ("inlined_to pointer set for noninline callers");
2245 error_found = true;
2248 for (e = node->indirect_calls; e; e = e->next_callee)
2249 if (verify_edge_count_and_frequency (e))
2250 error_found = true;
2251 if (!node->callers && node->global.inlined_to)
2253 error ("inlined_to pointer is set but no predecessors found");
2254 error_found = true;
2256 if (node->global.inlined_to == node)
2258 error ("inlined_to pointer refers to itself");
2259 error_found = true;
2262 if (node->clone_of)
2264 struct cgraph_node *n;
2265 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
2266 if (n == node)
2267 break;
2268 if (!n)
2270 error ("node has wrong clone_of");
2271 error_found = true;
2274 if (node->clones)
2276 struct cgraph_node *n;
2277 for (n = node->clones; n; n = n->next_sibling_clone)
2278 if (n->clone_of != node)
2279 break;
2280 if (n)
2282 error ("node has wrong clone list");
2283 error_found = true;
2286 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
2288 error ("node is in clone list but it is not clone");
2289 error_found = true;
2291 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
2293 error ("node has wrong prev_clone pointer");
2294 error_found = true;
2296 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
2298 error ("double linked list of clones corrupted");
2299 error_found = true;
2302 if (node->analyzed && node->alias)
2304 bool ref_found = false;
2305 int i;
2306 struct ipa_ref *ref;
2308 if (node->callees)
2310 error ("Alias has call edges");
2311 error_found = true;
2313 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
2314 i, ref); i++)
2315 if (ref->use != IPA_REF_ALIAS)
2317 error ("Alias has non-alias reference");
2318 error_found = true;
2320 else if (ref_found)
2322 error ("Alias has more than one alias reference");
2323 error_found = true;
2325 else
2326 ref_found = true;
2327 if (!ref_found)
2329 error ("Analyzed alias has no reference");
2330 error_found = true;
2333 if (node->analyzed && node->thunk.thunk_p)
2335 if (!node->callees)
2337 error ("No edge out of thunk node");
2338 error_found = true;
2340 else if (node->callees->next_callee)
2342 error ("More than one edge out of thunk node");
2343 error_found = true;
2345 if (gimple_has_body_p (node->symbol.decl))
2347 error ("Thunk is not supposed to have body");
2348 error_found = true;
2351 else if (node->analyzed && gimple_has_body_p (node->symbol.decl)
2352 && !TREE_ASM_WRITTEN (node->symbol.decl)
2353 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
2354 && !flag_wpa)
2356 if (this_cfun->cfg)
2358 /* The nodes we're interested in are never shared, so walk
2359 the tree ignoring duplicates. */
2360 struct pointer_set_t *visited_nodes = pointer_set_create ();
2361 /* Reach the trees by walking over the CFG, and note the
2362 enclosing basic-blocks in the call edges. */
2363 FOR_EACH_BB_FN (this_block, this_cfun)
2364 for (gsi = gsi_start_bb (this_block);
2365 !gsi_end_p (gsi);
2366 gsi_next (&gsi))
2368 gimple stmt = gsi_stmt (gsi);
2369 if (is_gimple_call (stmt))
2371 struct cgraph_edge *e = cgraph_edge (node, stmt);
2372 tree decl = gimple_call_fndecl (stmt);
2373 if (e)
2375 if (e->aux)
2377 error ("shared call_stmt:");
2378 cgraph_debug_gimple_stmt (this_cfun, stmt);
2379 error_found = true;
2381 if (!e->indirect_unknown_callee)
2383 if (verify_edge_corresponds_to_fndecl (e, decl))
2385 error ("edge points to wrong declaration:");
2386 debug_tree (e->callee->symbol.decl);
2387 fprintf (stderr," Instead of:");
2388 debug_tree (decl);
2389 error_found = true;
2392 else if (decl)
2394 error ("an indirect edge with unknown callee "
2395 "corresponding to a call_stmt with "
2396 "a known declaration:");
2397 error_found = true;
2398 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2400 e->aux = (void *)1;
2402 else if (decl)
2404 error ("missing callgraph edge for call stmt:");
2405 cgraph_debug_gimple_stmt (this_cfun, stmt);
2406 error_found = true;
2410 pointer_set_destroy (visited_nodes);
2412 else
2413 /* No CFG available?! */
2414 gcc_unreachable ();
2416 for (e = node->callees; e; e = e->next_callee)
2418 if (!e->aux)
2420 error ("edge %s->%s has no corresponding call_stmt",
2421 identifier_to_locale (cgraph_node_name (e->caller)),
2422 identifier_to_locale (cgraph_node_name (e->callee)));
2423 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2424 error_found = true;
2426 e->aux = 0;
2428 for (e = node->indirect_calls; e; e = e->next_callee)
2430 if (!e->aux)
2432 error ("an indirect edge from %s has no corresponding call_stmt",
2433 identifier_to_locale (cgraph_node_name (e->caller)));
2434 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2435 error_found = true;
2437 e->aux = 0;
2440 if (error_found)
2442 dump_cgraph_node (stderr, node);
2443 internal_error ("verify_cgraph_node failed");
2445 timevar_pop (TV_CGRAPH_VERIFY);
2448 /* Verify whole cgraph structure. */
2449 DEBUG_FUNCTION void
2450 verify_cgraph (void)
2452 struct cgraph_node *node;
2454 if (seen_error ())
2455 return;
2457 FOR_EACH_FUNCTION (node)
2458 verify_cgraph_node (node);
2460 #include "gt-cgraph.h"