svn merge -r 218997:219183 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / cgraph.c
blobde672bdeeff803bf90b066ed950f9dd960c09d69
1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "varasm.h"
32 #include "calls.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "toplev.h"
39 #include "flags.h"
40 #include "debug.h"
41 #include "target.h"
42 #include "predict.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "basic-block.h"
46 #include "hash-map.h"
47 #include "is-a.h"
48 #include "plugin-api.h"
49 #include "vec.h"
50 #include "machmode.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "intl.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "tree-eh.h"
60 #include "gimple-expr.h"
61 #include "gimple.h"
62 #include "gimple-iterator.h"
63 #include "timevar.h"
64 #include "dumpfile.h"
65 #include "gimple-ssa.h"
66 #include "tree-cfg.h"
67 #include "tree-ssa.h"
68 #include "value-prof.h"
69 #include "except.h"
70 #include "diagnostic-core.h"
71 #include "rtl.h"
72 #include "ipa-utils.h"
73 #include "lto-streamer.h"
74 #include "alloc-pool.h"
75 #include "symbol-summary.h"
76 #include "ipa-prop.h"
77 #include "ipa-inline.h"
78 #include "cfgloop.h"
79 #include "gimple-pretty-print.h"
80 #include "expr.h"
81 #include "tree-dfa.h"
82 #include "profile.h"
83 #include "params.h"
84 #include "tree-chkp.h"
85 #include "context.h"
87 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
88 #include "tree-pass.h"
90 /* Queue of cgraph nodes scheduled to be lowered. */
91 symtab_node *x_cgraph_nodes_queue;
92 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
94 /* Symbol table global context. */
95 symbol_table *symtab;
97 /* List of hooks triggered on cgraph_edge events. */
98 struct cgraph_edge_hook_list {
99 cgraph_edge_hook hook;
100 void *data;
101 struct cgraph_edge_hook_list *next;
104 /* List of hooks triggered on cgraph_node events. */
105 struct cgraph_node_hook_list {
106 cgraph_node_hook hook;
107 void *data;
108 struct cgraph_node_hook_list *next;
111 /* List of hooks triggered on events involving two cgraph_edges. */
112 struct cgraph_2edge_hook_list {
113 cgraph_2edge_hook hook;
114 void *data;
115 struct cgraph_2edge_hook_list *next;
118 /* List of hooks triggered on events involving two cgraph_nodes. */
119 struct cgraph_2node_hook_list {
120 cgraph_2node_hook hook;
121 void *data;
122 struct cgraph_2node_hook_list *next;
125 /* Hash descriptor for cgraph_function_version_info. */
127 struct function_version_hasher : ggc_hasher<cgraph_function_version_info *>
129 static hashval_t hash (cgraph_function_version_info *);
130 static bool equal (cgraph_function_version_info *,
131 cgraph_function_version_info *);
134 /* Map a cgraph_node to cgraph_function_version_info using this htab.
135 The cgraph_function_version_info has a THIS_NODE field that is the
136 corresponding cgraph_node.. */
138 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
140 /* Hash function for cgraph_fnver_htab. */
141 hashval_t
142 function_version_hasher::hash (cgraph_function_version_info *ptr)
144 int uid = ptr->this_node->uid;
145 return (hashval_t)(uid);
148 /* eq function for cgraph_fnver_htab. */
149 bool
150 function_version_hasher::equal (cgraph_function_version_info *n1,
151 cgraph_function_version_info *n2)
153 return n1->this_node->uid == n2->this_node->uid;
156 /* Mark as GC root all allocated nodes. */
157 static GTY(()) struct cgraph_function_version_info *
158 version_info_node = NULL;
160 /* Get the cgraph_function_version_info node corresponding to node. */
161 cgraph_function_version_info *
162 cgraph_node::function_version (void)
164 cgraph_function_version_info key;
165 key.this_node = this;
167 if (cgraph_fnver_htab == NULL)
168 return NULL;
170 return cgraph_fnver_htab->find (&key);
173 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
174 corresponding to cgraph_node NODE. */
175 cgraph_function_version_info *
176 cgraph_node::insert_new_function_version (void)
178 version_info_node = NULL;
179 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
180 version_info_node->this_node = this;
182 if (cgraph_fnver_htab == NULL)
183 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
185 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
186 = version_info_node;
187 return version_info_node;
190 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
191 DECL is a duplicate declaration. */
192 void
193 cgraph_node::delete_function_version (tree decl)
195 cgraph_node *decl_node = cgraph_node::get (decl);
196 cgraph_function_version_info *decl_v = NULL;
198 if (decl_node == NULL)
199 return;
201 decl_v = decl_node->function_version ();
203 if (decl_v == NULL)
204 return;
206 if (decl_v->prev != NULL)
207 decl_v->prev->next = decl_v->next;
209 if (decl_v->next != NULL)
210 decl_v->next->prev = decl_v->prev;
212 if (cgraph_fnver_htab != NULL)
213 cgraph_fnver_htab->remove_elt (decl_v);
215 decl_node->remove ();
218 /* Record that DECL1 and DECL2 are semantically identical function
219 versions. */
220 void
221 cgraph_node::record_function_versions (tree decl1, tree decl2)
223 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
224 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
225 cgraph_function_version_info *decl1_v = NULL;
226 cgraph_function_version_info *decl2_v = NULL;
227 cgraph_function_version_info *before;
228 cgraph_function_version_info *after;
230 gcc_assert (decl1_node != NULL && decl2_node != NULL);
231 decl1_v = decl1_node->function_version ();
232 decl2_v = decl2_node->function_version ();
234 if (decl1_v != NULL && decl2_v != NULL)
235 return;
237 if (decl1_v == NULL)
238 decl1_v = decl1_node->insert_new_function_version ();
240 if (decl2_v == NULL)
241 decl2_v = decl2_node->insert_new_function_version ();
243 /* Chain decl2_v and decl1_v. All semantically identical versions
244 will be chained together. */
246 before = decl1_v;
247 after = decl2_v;
249 while (before->next != NULL)
250 before = before->next;
252 while (after->prev != NULL)
253 after= after->prev;
255 before->next = after;
256 after->prev = before;
259 /* Initialize callgraph dump file. */
261 void
262 symbol_table::initialize (void)
264 if (!dump_file)
265 dump_file = dump_begin (TDI_cgraph, NULL);
268 /* Allocate new callgraph node and insert it into basic data structures. */
270 cgraph_node *
271 symbol_table::create_empty (void)
273 cgraph_node *node = allocate_cgraph_symbol ();
275 node->type = SYMTAB_FUNCTION;
276 node->frequency = NODE_FREQUENCY_NORMAL;
277 node->count_materialization_scale = REG_BR_PROB_BASE;
278 cgraph_count++;
280 return node;
283 /* Register HOOK to be called with DATA on each removed edge. */
284 cgraph_edge_hook_list *
285 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
287 cgraph_edge_hook_list *entry;
288 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
290 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
291 entry->hook = hook;
292 entry->data = data;
293 entry->next = NULL;
294 while (*ptr)
295 ptr = &(*ptr)->next;
296 *ptr = entry;
297 return entry;
300 /* Remove ENTRY from the list of hooks called on removing edges. */
301 void
302 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
304 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
306 while (*ptr != entry)
307 ptr = &(*ptr)->next;
308 *ptr = entry->next;
309 free (entry);
312 /* Call all edge removal hooks. */
313 void
314 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
316 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
317 while (entry)
319 entry->hook (e, entry->data);
320 entry = entry->next;
324 /* Register HOOK to be called with DATA on each removed node. */
325 cgraph_node_hook_list *
326 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
328 cgraph_node_hook_list *entry;
329 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
331 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
332 entry->hook = hook;
333 entry->data = data;
334 entry->next = NULL;
335 while (*ptr)
336 ptr = &(*ptr)->next;
337 *ptr = entry;
338 return entry;
341 /* Remove ENTRY from the list of hooks called on removing nodes. */
342 void
343 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
345 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
347 while (*ptr != entry)
348 ptr = &(*ptr)->next;
349 *ptr = entry->next;
350 free (entry);
353 /* Call all node removal hooks. */
354 void
355 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
357 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
358 while (entry)
360 entry->hook (node, entry->data);
361 entry = entry->next;
365 /* Call all node removal hooks. */
366 void
367 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
369 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
370 while (entry)
372 entry->hook (node, entry->data);
373 entry = entry->next;
378 /* Register HOOK to be called with DATA on each inserted node. */
379 cgraph_node_hook_list *
380 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
382 cgraph_node_hook_list *entry;
383 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
385 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
386 entry->hook = hook;
387 entry->data = data;
388 entry->next = NULL;
389 while (*ptr)
390 ptr = &(*ptr)->next;
391 *ptr = entry;
392 return entry;
395 /* Remove ENTRY from the list of hooks called on inserted nodes. */
396 void
397 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
399 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
401 while (*ptr != entry)
402 ptr = &(*ptr)->next;
403 *ptr = entry->next;
404 free (entry);
407 /* Register HOOK to be called with DATA on each duplicated edge. */
408 cgraph_2edge_hook_list *
409 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
411 cgraph_2edge_hook_list *entry;
412 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
414 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
415 entry->hook = hook;
416 entry->data = data;
417 entry->next = NULL;
418 while (*ptr)
419 ptr = &(*ptr)->next;
420 *ptr = entry;
421 return entry;
424 /* Remove ENTRY from the list of hooks called on duplicating edges. */
425 void
426 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
428 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
430 while (*ptr != entry)
431 ptr = &(*ptr)->next;
432 *ptr = entry->next;
433 free (entry);
436 /* Call all edge duplication hooks. */
437 void
438 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
440 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
441 while (entry)
443 entry->hook (cs1, cs2, entry->data);
444 entry = entry->next;
448 /* Register HOOK to be called with DATA on each duplicated node. */
449 cgraph_2node_hook_list *
450 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
452 cgraph_2node_hook_list *entry;
453 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
455 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
456 entry->hook = hook;
457 entry->data = data;
458 entry->next = NULL;
459 while (*ptr)
460 ptr = &(*ptr)->next;
461 *ptr = entry;
462 return entry;
465 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
466 void
467 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
469 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
471 while (*ptr != entry)
472 ptr = &(*ptr)->next;
473 *ptr = entry->next;
474 free (entry);
477 /* Call all node duplication hooks. */
478 void
479 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
480 cgraph_node *node2)
482 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
483 while (entry)
485 entry->hook (node, node2, entry->data);
486 entry = entry->next;
490 /* Return cgraph node assigned to DECL. Create new one when needed. */
492 cgraph_node *
493 cgraph_node::create (tree decl)
495 cgraph_node *node = symtab->create_empty ();
496 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
498 node->decl = decl;
500 if ((flag_openacc || flag_openmp)
501 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
503 node->offloadable = 1;
504 #ifdef ENABLE_OFFLOADING
505 g->have_offload = true;
506 #endif
509 node->register_symbol ();
511 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
513 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
514 node->next_nested = node->origin->nested;
515 node->origin->nested = node;
517 return node;
520 /* Try to find a call graph node for declaration DECL and if it does not exist
521 or if it corresponds to an inline clone, create a new one. */
523 cgraph_node *
524 cgraph_node::get_create (tree decl)
526 cgraph_node *first_clone = cgraph_node::get (decl);
528 if (first_clone && !first_clone->global.inlined_to)
529 return first_clone;
531 cgraph_node *node = cgraph_node::create (decl);
532 if (first_clone)
534 first_clone->clone_of = node;
535 node->clones = first_clone;
536 symtab->symtab_prevail_in_asm_name_hash (node);
537 node->decl->decl_with_vis.symtab_node = node;
538 if (dump_file)
539 fprintf (dump_file, "Introduced new external node "
540 "(%s/%i) and turned into root of the clone tree.\n",
541 xstrdup_for_dump (node->name ()), node->order);
543 else if (dump_file)
544 fprintf (dump_file, "Introduced new external node "
545 "(%s/%i).\n", xstrdup_for_dump (node->name ()),
546 node->order);
547 return node;
550 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
551 the function body is associated with (not necessarily cgraph_node (DECL). */
553 cgraph_node *
554 cgraph_node::create_alias (tree alias, tree target)
556 cgraph_node *alias_node;
558 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
559 || TREE_CODE (target) == IDENTIFIER_NODE);
560 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
561 alias_node = cgraph_node::get_create (alias);
562 gcc_assert (!alias_node->definition);
563 alias_node->alias_target = target;
564 alias_node->definition = true;
565 alias_node->alias = true;
566 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
567 alias_node->weakref = true;
568 return alias_node;
571 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
572 and NULL otherwise.
573 Same body aliases are output whenever the body of DECL is output,
574 and cgraph_node::get (ALIAS) transparently returns
575 cgraph_node::get (DECL). */
577 cgraph_node *
578 cgraph_node::create_same_body_alias (tree alias, tree decl)
580 cgraph_node *n;
581 #ifndef ASM_OUTPUT_DEF
582 /* If aliases aren't supported by the assembler, fail. */
583 return NULL;
584 #endif
585 /* Langhooks can create same body aliases of symbols not defined.
586 Those are useless. Drop them on the floor. */
587 if (symtab->global_info_ready)
588 return NULL;
590 n = cgraph_node::create_alias (alias, decl);
591 n->cpp_implicit_alias = true;
592 if (symtab->cpp_implicit_aliases_done)
593 n->resolve_alias (cgraph_node::get (decl));
594 return n;
597 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
598 aliases DECL with an adjustments made into the first parameter.
599 See comments in thunk_adjust for detail on the parameters. */
601 cgraph_node *
602 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
603 HOST_WIDE_INT fixed_offset,
604 HOST_WIDE_INT virtual_value,
605 tree virtual_offset,
606 tree real_alias)
608 cgraph_node *node;
610 node = cgraph_node::get (alias);
611 if (node)
612 node->reset ();
613 else
614 node = cgraph_node::create (alias);
615 gcc_checking_assert (!virtual_offset
616 || wi::eq_p (virtual_offset, virtual_value));
617 node->thunk.fixed_offset = fixed_offset;
618 node->thunk.this_adjusting = this_adjusting;
619 node->thunk.virtual_value = virtual_value;
620 node->thunk.virtual_offset_p = virtual_offset != NULL;
621 node->thunk.alias = real_alias;
622 node->thunk.thunk_p = true;
623 node->definition = true;
625 return node;
628 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
629 Return NULL if there's no such node. */
631 cgraph_node *
632 cgraph_node::get_for_asmname (tree asmname)
634 /* We do not want to look at inline clones. */
635 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
636 node;
637 node = node->next_sharing_asm_name)
639 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
640 if (cn && !cn->global.inlined_to)
641 return cn;
643 return NULL;
646 /* Returns a hash value for X (which really is a cgraph_edge). */
648 hashval_t
649 cgraph_edge_hasher::hash (cgraph_edge *e)
651 return htab_hash_pointer (e->call_stmt);
654 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
656 inline bool
657 cgraph_edge_hasher::equal (cgraph_edge *x, gimple y)
659 return x->call_stmt == y;
662 /* Add call graph edge E to call site hash of its caller. */
664 static inline void
665 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
667 gimple call = e->call_stmt;
668 *e->caller->call_site_hash->find_slot_with_hash (call,
669 htab_hash_pointer (call),
670 INSERT) = e;
673 /* Add call graph edge E to call site hash of its caller. */
675 static inline void
676 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
678 /* There are two speculative edges for every statement (one direct,
679 one indirect); always hash the direct one. */
680 if (e->speculative && e->indirect_unknown_callee)
681 return;
682 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
683 (e->call_stmt,
684 htab_hash_pointer (e->call_stmt), INSERT);
685 if (*slot)
687 gcc_assert (((cgraph_edge *)*slot)->speculative);
688 if (e->callee)
689 *slot = e;
690 return;
692 gcc_assert (!*slot || e->speculative);
693 *slot = e;
696 /* Return the callgraph edge representing the GIMPLE_CALL statement
697 CALL_STMT. */
699 cgraph_edge *
700 cgraph_node::get_edge (gimple call_stmt)
702 cgraph_edge *e, *e2;
703 int n = 0;
705 if (call_site_hash)
706 return call_site_hash->find_with_hash (call_stmt,
707 htab_hash_pointer (call_stmt));
709 /* This loop may turn out to be performance problem. In such case adding
710 hashtables into call nodes with very many edges is probably best
711 solution. It is not good idea to add pointer into CALL_EXPR itself
712 because we want to make possible having multiple cgraph nodes representing
713 different clones of the same body before the body is actually cloned. */
714 for (e = callees; e; e = e->next_callee)
716 if (e->call_stmt == call_stmt)
717 break;
718 n++;
721 if (!e)
722 for (e = indirect_calls; e; e = e->next_callee)
724 if (e->call_stmt == call_stmt)
725 break;
726 n++;
729 if (n > 100)
731 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
732 for (e2 = callees; e2; e2 = e2->next_callee)
733 cgraph_add_edge_to_call_site_hash (e2);
734 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
735 cgraph_add_edge_to_call_site_hash (e2);
738 return e;
742 /* Change field call_stmt of edge to NEW_STMT.
743 If UPDATE_SPECULATIVE and E is any component of speculative
744 edge, then update all components. */
746 void
747 cgraph_edge::set_call_stmt (gcall *new_stmt, bool update_speculative)
749 tree decl;
751 /* Speculative edges has three component, update all of them
752 when asked to. */
753 if (update_speculative && speculative)
755 cgraph_edge *direct, *indirect;
756 ipa_ref *ref;
758 speculative_call_info (direct, indirect, ref);
759 direct->set_call_stmt (new_stmt, false);
760 indirect->set_call_stmt (new_stmt, false);
761 ref->stmt = new_stmt;
762 return;
765 /* Only direct speculative edges go to call_site_hash. */
766 if (caller->call_site_hash
767 && (!speculative || !indirect_unknown_callee))
769 caller->call_site_hash->remove_elt_with_hash
770 (call_stmt, htab_hash_pointer (call_stmt));
773 cgraph_edge *e = this;
775 call_stmt = new_stmt;
776 if (indirect_unknown_callee
777 && (decl = gimple_call_fndecl (new_stmt)))
779 /* Constant propagation (and possibly also inlining?) can turn an
780 indirect call into a direct one. */
781 cgraph_node *new_callee = cgraph_node::get (decl);
783 gcc_checking_assert (new_callee);
784 e = make_direct (new_callee);
787 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
788 e->can_throw_external = stmt_can_throw_external (new_stmt);
789 pop_cfun ();
790 if (e->caller->call_site_hash)
791 cgraph_add_edge_to_call_site_hash (e);
794 /* Allocate a cgraph_edge structure and fill it with data according to the
795 parameters of which only CALLEE can be NULL (when creating an indirect call
796 edge). */
798 cgraph_edge *
799 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
800 gcall *call_stmt, gcov_type count, int freq,
801 bool indir_unknown_callee)
803 cgraph_edge *edge;
805 /* LTO does not actually have access to the call_stmt since these
806 have not been loaded yet. */
807 if (call_stmt)
809 /* This is a rather expensive check possibly triggering
810 construction of call stmt hashtable. */
811 #ifdef ENABLE_CHECKING
812 cgraph_edge *e;
813 gcc_checking_assert (
814 !(e = caller->get_edge (call_stmt)) || e->speculative);
815 #endif
817 gcc_assert (is_gimple_call (call_stmt));
820 if (free_edges)
822 edge = free_edges;
823 free_edges = NEXT_FREE_EDGE (edge);
825 else
827 edge = ggc_alloc<cgraph_edge> ();
828 edge->uid = edges_max_uid++;
831 edges_count++;
833 edge->aux = NULL;
834 edge->caller = caller;
835 edge->callee = callee;
836 edge->prev_caller = NULL;
837 edge->next_caller = NULL;
838 edge->prev_callee = NULL;
839 edge->next_callee = NULL;
840 edge->lto_stmt_uid = 0;
842 edge->count = count;
843 gcc_assert (count >= 0);
844 edge->frequency = freq;
845 gcc_assert (freq >= 0);
846 gcc_assert (freq <= CGRAPH_FREQ_MAX);
848 edge->call_stmt = call_stmt;
849 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
850 edge->can_throw_external
851 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
852 pop_cfun ();
853 if (call_stmt
854 && callee && callee->decl
855 && !gimple_check_call_matching_types (call_stmt, callee->decl,
856 false))
857 edge->call_stmt_cannot_inline_p = true;
858 else
859 edge->call_stmt_cannot_inline_p = false;
861 edge->indirect_info = NULL;
862 edge->indirect_inlining_edge = 0;
863 edge->speculative = false;
864 edge->indirect_unknown_callee = indir_unknown_callee;
865 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
866 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
867 edge->in_polymorphic_cdtor
868 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
869 caller->decl);
870 else
871 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
872 if (call_stmt && caller->call_site_hash)
873 cgraph_add_edge_to_call_site_hash (edge);
875 return edge;
878 /* Create edge from a given function to CALLEE in the cgraph. */
880 cgraph_edge *
881 cgraph_node::create_edge (cgraph_node *callee,
882 gcall *call_stmt, gcov_type count, int freq)
884 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
885 freq, false);
887 initialize_inline_failed (edge);
889 edge->next_caller = callee->callers;
890 if (callee->callers)
891 callee->callers->prev_caller = edge;
892 edge->next_callee = callees;
893 if (callees)
894 callees->prev_callee = edge;
895 callees = edge;
896 callee->callers = edge;
898 return edge;
901 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
903 cgraph_indirect_call_info *
904 cgraph_allocate_init_indirect_info (void)
906 cgraph_indirect_call_info *ii;
908 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
909 ii->param_index = -1;
910 return ii;
913 /* Create an indirect edge with a yet-undetermined callee where the call
914 statement destination is a formal parameter of the caller with index
915 PARAM_INDEX. */
917 cgraph_edge *
918 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
919 gcov_type count, int freq,
920 bool compute_indirect_info)
922 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt,
923 count, freq, true);
924 tree target;
926 initialize_inline_failed (edge);
928 edge->indirect_info = cgraph_allocate_init_indirect_info ();
929 edge->indirect_info->ecf_flags = ecf_flags;
930 edge->indirect_info->vptr_changed = true;
932 /* Record polymorphic call info. */
933 if (compute_indirect_info
934 && call_stmt
935 && (target = gimple_call_fn (call_stmt))
936 && virtual_method_call_p (target))
938 ipa_polymorphic_call_context context (decl, target, call_stmt);
940 /* Only record types can have virtual calls. */
941 edge->indirect_info->polymorphic = true;
942 edge->indirect_info->param_index = -1;
943 edge->indirect_info->otr_token
944 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
945 edge->indirect_info->otr_type = obj_type_ref_class (target);
946 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
947 edge->indirect_info->context = context;
950 edge->next_callee = indirect_calls;
951 if (indirect_calls)
952 indirect_calls->prev_callee = edge;
953 indirect_calls = edge;
955 return edge;
958 /* Remove the edge from the list of the callers of the callee. */
960 void
961 cgraph_edge::remove_callee (void)
963 gcc_assert (!indirect_unknown_callee);
964 if (prev_caller)
965 prev_caller->next_caller = next_caller;
966 if (next_caller)
967 next_caller->prev_caller = prev_caller;
968 if (!prev_caller)
969 callee->callers = next_caller;
972 /* Remove the edge from the list of the callees of the caller. */
974 void
975 cgraph_edge::remove_caller (void)
977 if (prev_callee)
978 prev_callee->next_callee = next_callee;
979 if (next_callee)
980 next_callee->prev_callee = prev_callee;
981 if (!prev_callee)
983 if (indirect_unknown_callee)
984 caller->indirect_calls = next_callee;
985 else
986 caller->callees = next_callee;
988 if (caller->call_site_hash)
989 caller->call_site_hash->remove_elt_with_hash (call_stmt,
990 htab_hash_pointer (call_stmt));
993 /* Put the edge onto the free list. */
995 void
996 symbol_table::free_edge (cgraph_edge *e)
998 int uid = e->uid;
1000 if (e->indirect_info)
1001 ggc_free (e->indirect_info);
1003 /* Clear out the edge so we do not dangle pointers. */
1004 memset (e, 0, sizeof (*e));
1005 e->uid = uid;
1006 NEXT_FREE_EDGE (e) = free_edges;
1007 free_edges = e;
1008 edges_count--;
1011 /* Remove the edge in the cgraph. */
1013 void
1014 cgraph_edge::remove (void)
1016 /* Call all edge removal hooks. */
1017 symtab->call_edge_removal_hooks (this);
1019 if (!indirect_unknown_callee)
1020 /* Remove from callers list of the callee. */
1021 remove_callee ();
1023 /* Remove from callees list of the callers. */
1024 remove_caller ();
1026 /* Put the edge onto the free list. */
1027 symtab->free_edge (this);
1030 /* Set callee of call graph edge E and add it to the corresponding set of
1031 callers. */
1033 static void
1034 cgraph_set_edge_callee (cgraph_edge *e, cgraph_node *n)
1036 e->prev_caller = NULL;
1037 if (n->callers)
1038 n->callers->prev_caller = e;
1039 e->next_caller = n->callers;
1040 n->callers = e;
1041 e->callee = n;
1044 /* Turn edge into speculative call calling N2. Update
1045 the profile so the direct call is taken COUNT times
1046 with FREQUENCY.
1048 At clone materialization time, the indirect call E will
1049 be expanded as:
1051 if (call_dest == N2)
1052 n2 ();
1053 else
1054 call call_dest
1056 At this time the function just creates the direct call,
1057 the referencd representing the if conditional and attaches
1058 them all to the orginal indirect call statement.
1060 Return direct edge created. */
1062 cgraph_edge *
1063 cgraph_edge::make_speculative (cgraph_node *n2, gcov_type direct_count,
1064 int direct_frequency)
1066 cgraph_node *n = caller;
1067 ipa_ref *ref = NULL;
1068 cgraph_edge *e2;
1070 if (dump_file)
1072 fprintf (dump_file, "Indirect call -> speculative call"
1073 " %s/%i => %s/%i\n",
1074 xstrdup_for_dump (n->name ()), n->order,
1075 xstrdup_for_dump (n2->name ()), n2->order);
1077 speculative = true;
1078 e2 = n->create_edge (n2, call_stmt, direct_count, direct_frequency);
1079 initialize_inline_failed (e2);
1080 e2->speculative = true;
1081 if (TREE_NOTHROW (n2->decl))
1082 e2->can_throw_external = false;
1083 else
1084 e2->can_throw_external = can_throw_external;
1085 e2->lto_stmt_uid = lto_stmt_uid;
1086 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1087 count -= e2->count;
1088 frequency -= e2->frequency;
1089 symtab->call_edge_duplication_hooks (this, e2);
1090 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1091 ref->lto_stmt_uid = lto_stmt_uid;
1092 ref->speculative = speculative;
1093 n2->mark_address_taken ();
1094 return e2;
1097 /* Speculative call consist of three components:
1098 1) an indirect edge representing the original call
1099 2) an direct edge representing the new call
1100 3) ADDR_EXPR reference representing the speculative check.
1101 All three components are attached to single statement (the indirect
1102 call) and if one of them exists, all of them must exist.
1104 Given speculative call edge, return all three components.
1107 void
1108 cgraph_edge::speculative_call_info (cgraph_edge *&direct,
1109 cgraph_edge *&indirect,
1110 ipa_ref *&reference)
1112 ipa_ref *ref;
1113 int i;
1114 cgraph_edge *e2;
1115 cgraph_edge *e = this;
1117 if (!e->indirect_unknown_callee)
1118 for (e2 = e->caller->indirect_calls;
1119 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1120 e2 = e2->next_callee)
1122 else
1124 e2 = e;
1125 /* We can take advantage of the call stmt hash. */
1126 if (e2->call_stmt)
1128 e = e->caller->get_edge (e2->call_stmt);
1129 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1131 else
1132 for (e = e->caller->callees;
1133 e2->call_stmt != e->call_stmt
1134 || e2->lto_stmt_uid != e->lto_stmt_uid;
1135 e = e->next_callee)
1138 gcc_assert (e->speculative && e2->speculative);
1139 direct = e;
1140 indirect = e2;
1142 reference = NULL;
1143 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1144 if (ref->speculative
1145 && ((ref->stmt && ref->stmt == e->call_stmt)
1146 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1148 reference = ref;
1149 break;
1152 /* Speculative edge always consist of all three components - direct edge,
1153 indirect and reference. */
1155 gcc_assert (e && e2 && ref);
1158 /* Redirect callee of the edge to N. The function does not update underlying
1159 call expression. */
1161 void
1162 cgraph_edge::redirect_callee (cgraph_node *n)
1164 /* Remove from callers list of the current callee. */
1165 remove_callee ();
1167 /* Insert to callers list of the new callee. */
1168 cgraph_set_edge_callee (this, n);
1171 /* Speculative call edge turned out to be direct call to CALLE_DECL.
1172 Remove the speculative call sequence and return edge representing the call.
1173 It is up to caller to redirect the call as appropriate. */
1175 cgraph_edge *
1176 cgraph_edge::resolve_speculation (tree callee_decl)
1178 cgraph_edge *edge = this;
1179 cgraph_edge *e2;
1180 ipa_ref *ref;
1182 gcc_assert (edge->speculative);
1183 edge->speculative_call_info (e2, edge, ref);
1184 if (!callee_decl
1185 || !ref->referred->semantically_equivalent_p
1186 (symtab_node::get (callee_decl)))
1188 if (dump_file)
1190 if (callee_decl)
1192 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1193 "turned out to have contradicting known target ",
1194 xstrdup_for_dump (edge->caller->name ()),
1195 edge->caller->order,
1196 xstrdup_for_dump (e2->callee->name ()),
1197 e2->callee->order);
1198 print_generic_expr (dump_file, callee_decl, 0);
1199 fprintf (dump_file, "\n");
1201 else
1203 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1204 xstrdup_for_dump (edge->caller->name ()),
1205 edge->caller->order,
1206 xstrdup_for_dump (e2->callee->name ()),
1207 e2->callee->order);
1211 else
1213 cgraph_edge *tmp = edge;
1214 if (dump_file)
1215 fprintf (dump_file, "Speculative call turned into direct call.\n");
1216 edge = e2;
1217 e2 = tmp;
1218 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1219 in the functions inlined through it. */
1221 edge->count += e2->count;
1222 edge->frequency += e2->frequency;
1223 if (edge->frequency > CGRAPH_FREQ_MAX)
1224 edge->frequency = CGRAPH_FREQ_MAX;
1225 edge->speculative = false;
1226 e2->speculative = false;
1227 ref->remove_reference ();
1228 if (e2->indirect_unknown_callee || e2->inline_failed)
1229 e2->remove ();
1230 else
1231 e2->callee->remove_symbol_and_inline_clones ();
1232 if (edge->caller->call_site_hash)
1233 cgraph_update_edge_in_call_site_hash (edge);
1234 return edge;
1237 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1238 CALLEE. DELTA is an integer constant that is to be added to the this
1239 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1241 cgraph_edge *
1242 cgraph_edge::make_direct (cgraph_node *callee)
1244 cgraph_edge *edge = this;
1245 gcc_assert (indirect_unknown_callee);
1247 /* If we are redirecting speculative call, make it non-speculative. */
1248 if (indirect_unknown_callee && speculative)
1250 edge = edge->resolve_speculation (callee->decl);
1252 /* On successful speculation just return the pre existing direct edge. */
1253 if (!indirect_unknown_callee)
1254 return edge;
1257 indirect_unknown_callee = 0;
1258 ggc_free (indirect_info);
1259 indirect_info = NULL;
1261 /* Get the edge out of the indirect edge list. */
1262 if (prev_callee)
1263 prev_callee->next_callee = next_callee;
1264 if (next_callee)
1265 next_callee->prev_callee = prev_callee;
1266 if (!prev_callee)
1267 caller->indirect_calls = next_callee;
1269 /* Put it into the normal callee list */
1270 prev_callee = NULL;
1271 next_callee = caller->callees;
1272 if (caller->callees)
1273 caller->callees->prev_callee = edge;
1274 caller->callees = edge;
1276 /* Insert to callers list of the new callee. */
1277 cgraph_set_edge_callee (edge, callee);
1279 if (call_stmt)
1280 call_stmt_cannot_inline_p
1281 = !gimple_check_call_matching_types (call_stmt, callee->decl,
1282 false);
1284 /* We need to re-determine the inlining status of the edge. */
1285 initialize_inline_failed (edge);
1286 return edge;
1289 /* If necessary, change the function declaration in the call statement
1290 associated with E so that it corresponds to the edge callee. */
1292 gimple
1293 cgraph_edge::redirect_call_stmt_to_callee (void)
1295 cgraph_edge *e = this;
1297 tree decl = gimple_call_fndecl (e->call_stmt);
1298 tree lhs = gimple_call_lhs (e->call_stmt);
1299 gcall *new_stmt;
1300 gimple_stmt_iterator gsi;
1301 #ifdef ENABLE_CHECKING
1302 cgraph_node *node;
1303 #endif
1305 if (e->speculative)
1307 cgraph_edge *e2;
1308 gcall *new_stmt;
1309 ipa_ref *ref;
1311 e->speculative_call_info (e, e2, ref);
1312 /* If there already is an direct call (i.e. as a result of inliner's
1313 substitution), forget about speculating. */
1314 if (decl)
1315 e = e->resolve_speculation (decl);
1316 /* If types do not match, speculation was likely wrong.
1317 The direct edge was posisbly redirected to the clone with a different
1318 signature. We did not update the call statement yet, so compare it
1319 with the reference that still points to the proper type. */
1320 else if (!gimple_check_call_matching_types (e->call_stmt,
1321 ref->referred->decl,
1322 true))
1324 if (dump_file)
1325 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1326 "Type mismatch.\n",
1327 xstrdup_for_dump (e->caller->name ()),
1328 e->caller->order,
1329 xstrdup_for_dump (e->callee->name ()),
1330 e->callee->order);
1331 e = e->resolve_speculation ();
1332 /* We are producing the final function body and will throw away the
1333 callgraph edges really soon. Reset the counts/frequencies to
1334 keep verifier happy in the case of roundoff errors. */
1335 e->count = gimple_bb (e->call_stmt)->count;
1336 e->frequency = compute_call_stmt_bb_frequency
1337 (e->caller->decl, gimple_bb (e->call_stmt));
1339 /* Expand speculation into GIMPLE code. */
1340 else
1342 if (dump_file)
1343 fprintf (dump_file,
1344 "Expanding speculative call of %s/%i -> %s/%i count:"
1345 "%"PRId64"\n",
1346 xstrdup_for_dump (e->caller->name ()),
1347 e->caller->order,
1348 xstrdup_for_dump (e->callee->name ()),
1349 e->callee->order,
1350 (int64_t)e->count);
1351 gcc_assert (e2->speculative);
1352 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1353 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
1354 e->count || e2->count
1355 ? RDIV (e->count * REG_BR_PROB_BASE,
1356 e->count + e2->count)
1357 : e->frequency || e2->frequency
1358 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1359 e->frequency + e2->frequency)
1360 : REG_BR_PROB_BASE / 2,
1361 e->count, e->count + e2->count);
1362 e->speculative = false;
1363 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1364 false);
1366 /* Fix edges for BUILT_IN_CHKP_BNDRET calls attached to the
1367 processed call stmt. */
1368 if (gimple_call_with_bounds_p (new_stmt)
1369 && gimple_call_lhs (new_stmt)
1370 && chkp_retbnd_call_by_val (gimple_call_lhs (e2->call_stmt)))
1372 tree dresult = gimple_call_lhs (new_stmt);
1373 tree iresult = gimple_call_lhs (e2->call_stmt);
1374 gcall *dbndret = chkp_retbnd_call_by_val (dresult);
1375 gcall *ibndret = chkp_retbnd_call_by_val (iresult);
1376 struct cgraph_edge *iedge
1377 = e2->caller->cgraph_node::get_edge (ibndret);
1378 struct cgraph_edge *dedge;
1380 if (dbndret)
1382 dedge = iedge->caller->create_edge (iedge->callee,
1383 dbndret, e->count,
1384 e->frequency);
1385 dedge->frequency = compute_call_stmt_bb_frequency
1386 (dedge->caller->decl, gimple_bb (dedge->call_stmt));
1388 iedge->frequency = compute_call_stmt_bb_frequency
1389 (iedge->caller->decl, gimple_bb (iedge->call_stmt));
1392 e->frequency = compute_call_stmt_bb_frequency
1393 (e->caller->decl, gimple_bb (e->call_stmt));
1394 e2->frequency = compute_call_stmt_bb_frequency
1395 (e2->caller->decl, gimple_bb (e2->call_stmt));
1396 e2->speculative = false;
1397 ref->speculative = false;
1398 ref->stmt = NULL;
1399 /* Indirect edges are not both in the call site hash.
1400 get it updated. */
1401 if (e->caller->call_site_hash)
1402 cgraph_update_edge_in_call_site_hash (e2);
1403 pop_cfun ();
1404 /* Continue redirecting E to proper target. */
1408 if (e->indirect_unknown_callee
1409 || decl == e->callee->decl)
1410 return e->call_stmt;
1412 #ifdef ENABLE_CHECKING
1413 if (decl)
1415 node = cgraph_node::get (decl);
1416 gcc_assert (!node || !node->clone.combined_args_to_skip);
1418 #endif
1420 if (symtab->dump_file)
1422 fprintf (symtab->dump_file, "updating call of %s/%i -> %s/%i: ",
1423 xstrdup_for_dump (e->caller->name ()), e->caller->order,
1424 xstrdup_for_dump (e->callee->name ()), e->callee->order);
1425 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1426 if (e->callee->clone.combined_args_to_skip)
1428 fprintf (symtab->dump_file, " combined args to skip: ");
1429 dump_bitmap (symtab->dump_file,
1430 e->callee->clone.combined_args_to_skip);
1434 if (e->callee->clone.combined_args_to_skip)
1436 int lp_nr;
1438 new_stmt
1439 = gimple_call_copy_skip_args (e->call_stmt,
1440 e->callee->clone.combined_args_to_skip);
1441 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1442 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1444 if (gimple_vdef (new_stmt)
1445 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1446 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1448 gsi = gsi_for_stmt (e->call_stmt);
1449 gsi_replace (&gsi, new_stmt, false);
1450 /* We need to defer cleaning EH info on the new statement to
1451 fixup-cfg. We may not have dominator information at this point
1452 and thus would end up with unreachable blocks and have no way
1453 to communicate that we need to run CFG cleanup then. */
1454 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1455 if (lp_nr != 0)
1457 remove_stmt_from_eh_lp (e->call_stmt);
1458 add_stmt_to_eh_lp (new_stmt, lp_nr);
1461 else
1463 new_stmt = e->call_stmt;
1464 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1465 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1468 /* If the call becomes noreturn, remove the lhs. */
1469 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1471 if (TREE_CODE (lhs) == SSA_NAME)
1473 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1474 TREE_TYPE (lhs), NULL);
1475 var = get_or_create_ssa_default_def
1476 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1477 gimple set_stmt = gimple_build_assign (lhs, var);
1478 gsi = gsi_for_stmt (new_stmt);
1479 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1480 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1482 gimple_call_set_lhs (new_stmt, NULL_TREE);
1483 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1486 /* If new callee has no static chain, remove it. */
1487 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1489 gimple_call_set_chain (new_stmt, NULL);
1490 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1493 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1495 if (symtab->dump_file)
1497 fprintf (symtab->dump_file, " updated to:");
1498 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1500 return new_stmt;
1503 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1504 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1505 of OLD_STMT if it was previously call statement.
1506 If NEW_STMT is NULL, the call has been dropped without any
1507 replacement. */
1509 static void
1510 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1511 gimple old_stmt, tree old_call,
1512 gimple new_stmt)
1514 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1515 ? gimple_call_fndecl (new_stmt) : 0;
1517 /* We are seeing indirect calls, then there is nothing to update. */
1518 if (!new_call && !old_call)
1519 return;
1520 /* See if we turned indirect call into direct call or folded call to one builtin
1521 into different builtin. */
1522 if (old_call != new_call)
1524 cgraph_edge *e = node->get_edge (old_stmt);
1525 cgraph_edge *ne = NULL;
1526 gcov_type count;
1527 int frequency;
1529 if (e)
1531 /* See if the edge is already there and has the correct callee. It
1532 might be so because of indirect inlining has already updated
1533 it. We also might've cloned and redirected the edge. */
1534 if (new_call && e->callee)
1536 cgraph_node *callee = e->callee;
1537 while (callee)
1539 if (callee->decl == new_call
1540 || callee->former_clone_of == new_call)
1542 e->set_call_stmt (as_a <gcall *> (new_stmt));
1543 return;
1545 callee = callee->clone_of;
1549 /* Otherwise remove edge and create new one; we can't simply redirect
1550 since function has changed, so inline plan and other information
1551 attached to edge is invalid. */
1552 count = e->count;
1553 frequency = e->frequency;
1554 if (e->indirect_unknown_callee || e->inline_failed)
1555 e->remove ();
1556 else
1557 e->callee->remove_symbol_and_inline_clones ();
1559 else if (new_call)
1561 /* We are seeing new direct call; compute profile info based on BB. */
1562 basic_block bb = gimple_bb (new_stmt);
1563 count = bb->count;
1564 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1565 bb);
1568 if (new_call)
1570 ne = node->create_edge (cgraph_node::get_create (new_call),
1571 as_a <gcall *> (new_stmt), count,
1572 frequency);
1573 gcc_assert (ne->inline_failed);
1576 /* We only updated the call stmt; update pointer in cgraph edge.. */
1577 else if (old_stmt != new_stmt)
1578 node->get_edge (old_stmt)->set_call_stmt (as_a <gcall *> (new_stmt));
1581 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1582 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1583 of OLD_STMT before it was updated (updating can happen inplace). */
1585 void
1586 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1588 cgraph_node *orig = cgraph_node::get (cfun->decl);
1589 cgraph_node *node;
1591 gcc_checking_assert (orig);
1592 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1593 if (orig->clones)
1594 for (node = orig->clones; node != orig;)
1596 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1597 if (node->clones)
1598 node = node->clones;
1599 else if (node->next_sibling_clone)
1600 node = node->next_sibling_clone;
1601 else
1603 while (node != orig && !node->next_sibling_clone)
1604 node = node->clone_of;
1605 if (node != orig)
1606 node = node->next_sibling_clone;
1612 /* Remove all callees from the node. */
1614 void
1615 cgraph_node::remove_callees (void)
1617 cgraph_edge *e, *f;
1619 /* It is sufficient to remove the edges from the lists of callers of
1620 the callees. The callee list of the node can be zapped with one
1621 assignment. */
1622 for (e = callees; e; e = f)
1624 f = e->next_callee;
1625 symtab->call_edge_removal_hooks (e);
1626 if (!e->indirect_unknown_callee)
1627 e->remove_callee ();
1628 symtab->free_edge (e);
1630 for (e = indirect_calls; e; e = f)
1632 f = e->next_callee;
1633 symtab->call_edge_removal_hooks (e);
1634 if (!e->indirect_unknown_callee)
1635 e->remove_callee ();
1636 symtab->free_edge (e);
1638 indirect_calls = NULL;
1639 callees = NULL;
1640 if (call_site_hash)
1642 call_site_hash->empty ();
1643 call_site_hash = NULL;
1647 /* Remove all callers from the node. */
1649 void
1650 cgraph_node::remove_callers (void)
1652 cgraph_edge *e, *f;
1654 /* It is sufficient to remove the edges from the lists of callees of
1655 the callers. The caller list of the node can be zapped with one
1656 assignment. */
1657 for (e = callers; e; e = f)
1659 f = e->next_caller;
1660 symtab->call_edge_removal_hooks (e);
1661 e->remove_caller ();
1662 symtab->free_edge (e);
1664 callers = NULL;
1667 /* Helper function for cgraph_release_function_body and free_lang_data.
1668 It releases body from function DECL without having to inspect its
1669 possibly non-existent symtab node. */
1671 void
1672 release_function_body (tree decl)
1674 if (DECL_STRUCT_FUNCTION (decl))
1676 if (DECL_STRUCT_FUNCTION (decl)->cfg
1677 || DECL_STRUCT_FUNCTION (decl)->gimple_df)
1679 push_cfun (DECL_STRUCT_FUNCTION (decl));
1680 if (cfun->cfg
1681 && current_loops)
1683 cfun->curr_properties &= ~PROP_loops;
1684 loop_optimizer_finalize ();
1686 if (cfun->gimple_df)
1688 delete_tree_ssa ();
1689 delete_tree_cfg_annotations ();
1690 cfun->eh = NULL;
1692 if (cfun->cfg)
1694 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1695 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1696 clear_edges ();
1697 cfun->cfg = NULL;
1699 if (cfun->value_histograms)
1700 free_histograms ();
1701 pop_cfun ();
1703 gimple_set_body (decl, NULL);
1704 /* Struct function hangs a lot of data that would leak if we didn't
1705 removed all pointers to it. */
1706 ggc_free (DECL_STRUCT_FUNCTION (decl));
1707 DECL_STRUCT_FUNCTION (decl) = NULL;
1709 DECL_SAVED_TREE (decl) = NULL;
1712 /* Release memory used to represent body of function.
1713 Use this only for functions that are released before being translated to
1714 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1715 are free'd in final.c via free_after_compilation().
1716 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1718 void
1719 cgraph_node::release_body (bool keep_arguments)
1721 ipa_transforms_to_apply.release ();
1722 if (!used_as_abstract_origin && symtab->state != PARSING)
1724 DECL_RESULT (decl) = NULL;
1726 if (!keep_arguments)
1727 DECL_ARGUMENTS (decl) = NULL;
1729 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1730 of its associated function function declaration because it's
1731 needed to emit debug info later. */
1732 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1733 DECL_INITIAL (decl) = error_mark_node;
1734 release_function_body (decl);
1735 if (lto_file_data)
1736 lto_free_function_in_decl_state_for_node (this);
1739 /* Remove function from symbol table. */
1741 void
1742 cgraph_node::remove (void)
1744 cgraph_node *n;
1745 int uid = this->uid;
1747 symtab->call_cgraph_removal_hooks (this);
1748 remove_callers ();
1749 remove_callees ();
1750 ipa_transforms_to_apply.release ();
1752 /* Incremental inlining access removed nodes stored in the postorder list.
1754 force_output = false;
1755 forced_by_abi = false;
1756 for (n = nested; n; n = n->next_nested)
1757 n->origin = NULL;
1758 nested = NULL;
1759 if (origin)
1761 cgraph_node **node2 = &origin->nested;
1763 while (*node2 != this)
1764 node2 = &(*node2)->next_nested;
1765 *node2 = next_nested;
1767 unregister ();
1768 if (prev_sibling_clone)
1769 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1770 else if (clone_of)
1771 clone_of->clones = next_sibling_clone;
1772 if (next_sibling_clone)
1773 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1774 if (clones)
1776 cgraph_node *n, *next;
1778 if (clone_of)
1780 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1781 n->clone_of = clone_of;
1782 n->clone_of = clone_of;
1783 n->next_sibling_clone = clone_of->clones;
1784 if (clone_of->clones)
1785 clone_of->clones->prev_sibling_clone = n;
1786 clone_of->clones = clones;
1788 else
1790 /* We are removing node with clones. This makes clones inconsistent,
1791 but assume they will be removed subsequently and just keep clone
1792 tree intact. This can happen in unreachable function removal since
1793 we remove unreachable functions in random order, not by bottom-up
1794 walk of clone trees. */
1795 for (n = clones; n; n = next)
1797 next = n->next_sibling_clone;
1798 n->next_sibling_clone = NULL;
1799 n->prev_sibling_clone = NULL;
1800 n->clone_of = NULL;
1805 /* While all the clones are removed after being proceeded, the function
1806 itself is kept in the cgraph even after it is compiled. Check whether
1807 we are done with this body and reclaim it proactively if this is the case.
1809 if (symtab->state != LTO_STREAMING)
1811 n = cgraph_node::get (decl);
1812 if (!n
1813 || (!n->clones && !n->clone_of && !n->global.inlined_to
1814 && (symtab->global_info_ready
1815 && (TREE_ASM_WRITTEN (n->decl)
1816 || DECL_EXTERNAL (n->decl)
1817 || !n->analyzed
1818 || (!flag_wpa && n->in_other_partition)))))
1819 release_body ();
1822 decl = NULL;
1823 if (call_site_hash)
1825 call_site_hash->empty ();
1826 call_site_hash = NULL;
1829 if (instrumented_version)
1831 instrumented_version->instrumented_version = NULL;
1832 instrumented_version = NULL;
1835 symtab->release_symbol (this, uid);
1838 /* Likewise indicate that a node is having address taken. */
1840 void
1841 cgraph_node::mark_address_taken (void)
1843 /* Indirect inlining can figure out that all uses of the address are
1844 inlined. */
1845 if (global.inlined_to)
1847 gcc_assert (cfun->after_inlining);
1848 gcc_assert (callers->indirect_inlining_edge);
1849 return;
1851 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1852 IPA_REF_ADDR reference exists (and thus it should be set on node
1853 representing alias we take address of) and as a test whether address
1854 of the object was taken (and thus it should be set on node alias is
1855 referring to). We should remove the first use and the remove the
1856 following set. */
1857 address_taken = 1;
1858 cgraph_node *node = ultimate_alias_target ();
1859 node->address_taken = 1;
1862 /* Return local info for the compiled function. */
1864 cgraph_local_info *
1865 cgraph_node::local_info (tree decl)
1867 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1868 cgraph_node *node = get (decl);
1869 if (!node)
1870 return NULL;
1871 return &node->local;
1874 /* Return global info for the compiled function. */
1876 cgraph_global_info *
1877 cgraph_node::global_info (tree decl)
1879 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1880 && symtab->global_info_ready);
1881 cgraph_node *node = get (decl);
1882 if (!node)
1883 return NULL;
1884 return &node->global;
1887 /* Return local info for the compiled function. */
1889 cgraph_rtl_info *
1890 cgraph_node::rtl_info (tree decl)
1892 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1893 cgraph_node *node = get (decl);
1894 if (!node
1895 || (decl != current_function_decl
1896 && !TREE_ASM_WRITTEN (node->decl)))
1897 return NULL;
1898 return &node->rtl;
1901 /* Return a string describing the failure REASON. */
1903 const char*
1904 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1906 #undef DEFCIFCODE
1907 #define DEFCIFCODE(code, type, string) string,
1909 static const char *cif_string_table[CIF_N_REASONS] = {
1910 #include "cif-code.def"
1913 /* Signedness of an enum type is implementation defined, so cast it
1914 to unsigned before testing. */
1915 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1916 return cif_string_table[reason];
1919 /* Return a type describing the failure REASON. */
1921 cgraph_inline_failed_type_t
1922 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1924 #undef DEFCIFCODE
1925 #define DEFCIFCODE(code, type, string) type,
1927 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1928 #include "cif-code.def"
1931 /* Signedness of an enum type is implementation defined, so cast it
1932 to unsigned before testing. */
1933 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1934 return cif_type_table[reason];
1937 /* Names used to print out the availability enum. */
1938 const char * const cgraph_availability_names[] =
1939 {"unset", "not_available", "overwritable", "available", "local"};
1941 /* Output flags of edge E. */
1943 static void
1944 dump_edge_flags (FILE *f, struct cgraph_edge *edge)
1946 if (edge->speculative)
1947 fprintf (f, "(speculative) ");
1948 if (!edge->inline_failed)
1949 fprintf (f, "(inlined) ");
1950 if (edge->indirect_inlining_edge)
1951 fprintf (f, "(indirect_inlining) ");
1952 if (edge->count)
1953 fprintf (f, "(%"PRId64"x) ",
1954 (int64_t)edge->count);
1955 if (edge->frequency)
1956 fprintf (f, "(%.2f per call) ",
1957 edge->frequency / (double)CGRAPH_FREQ_BASE);
1958 if (edge->can_throw_external)
1959 fprintf (f, "(can throw external) ");
1962 /* Dump call graph node to file F. */
1964 void
1965 cgraph_node::dump (FILE *f)
1967 cgraph_edge *edge;
1969 dump_base (f);
1971 if (global.inlined_to)
1972 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1973 xstrdup_for_dump (name ()),
1974 order,
1975 xstrdup_for_dump (global.inlined_to->name ()),
1976 global.inlined_to->order);
1977 if (clone_of)
1978 fprintf (f, " Clone of %s/%i\n",
1979 clone_of->asm_name (),
1980 clone_of->order);
1981 if (symtab->function_flags_ready)
1982 fprintf (f, " Availability: %s\n",
1983 cgraph_availability_names [get_availability ()]);
1985 if (profile_id)
1986 fprintf (f, " Profile id: %i\n",
1987 profile_id);
1988 fprintf (f, " First run: %i\n", tp_first_run);
1989 fprintf (f, " Function flags:");
1990 if (count)
1991 fprintf (f, " executed %"PRId64"x",
1992 (int64_t)count);
1993 if (origin)
1994 fprintf (f, " nested in: %s", origin->asm_name ());
1995 if (gimple_has_body_p (decl))
1996 fprintf (f, " body");
1997 if (process)
1998 fprintf (f, " process");
1999 if (local.local)
2000 fprintf (f, " local");
2001 if (local.redefined_extern_inline)
2002 fprintf (f, " redefined_extern_inline");
2003 if (only_called_at_startup)
2004 fprintf (f, " only_called_at_startup");
2005 if (only_called_at_exit)
2006 fprintf (f, " only_called_at_exit");
2007 if (tm_clone)
2008 fprintf (f, " tm_clone");
2009 if (icf_merged)
2010 fprintf (f, " icf_merged");
2011 if (nonfreeing_fn)
2012 fprintf (f, " nonfreeing_fn");
2013 if (DECL_STATIC_CONSTRUCTOR (decl))
2014 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2015 if (DECL_STATIC_DESTRUCTOR (decl))
2016 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2018 fprintf (f, "\n");
2020 if (thunk.thunk_p)
2022 fprintf (f, " Thunk");
2023 if (thunk.alias)
2024 fprintf (f, " of %s (asm: %s)",
2025 lang_hooks.decl_printable_name (thunk.alias, 2),
2026 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2027 fprintf (f, " fixed offset %i virtual value %i has "
2028 "virtual offset %i)\n",
2029 (int)thunk.fixed_offset,
2030 (int)thunk.virtual_value,
2031 (int)thunk.virtual_offset_p);
2033 if (alias && thunk.alias
2034 && DECL_P (thunk.alias))
2036 fprintf (f, " Alias of %s",
2037 lang_hooks.decl_printable_name (thunk.alias, 2));
2038 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2039 fprintf (f, " (asm: %s)",
2040 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2041 fprintf (f, "\n");
2044 fprintf (f, " Called by: ");
2046 for (edge = callers; edge; edge = edge->next_caller)
2048 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2049 edge->caller->order);
2050 dump_edge_flags (f, edge);
2053 fprintf (f, "\n Calls: ");
2054 for (edge = callees; edge; edge = edge->next_callee)
2056 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2057 edge->callee->order);
2058 dump_edge_flags (f, edge);
2060 fprintf (f, "\n");
2062 for (edge = indirect_calls; edge; edge = edge->next_callee)
2064 if (edge->indirect_info->polymorphic)
2066 fprintf (f, " Polymorphic indirect call of type ");
2067 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2068 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2070 else
2071 fprintf (f, " Indirect call");
2072 dump_edge_flags (f, edge);
2073 if (edge->indirect_info->param_index != -1)
2075 fprintf (f, " of param:%i", edge->indirect_info->param_index);
2076 if (edge->indirect_info->agg_contents)
2077 fprintf (f, " loaded from %s %s at offset %i",
2078 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2079 edge->indirect_info->by_ref ? "passed by reference":"",
2080 (int)edge->indirect_info->offset);
2081 if (edge->indirect_info->vptr_changed)
2082 fprintf (f, " (vptr maybe changed)");
2084 fprintf (f, "\n");
2085 if (edge->indirect_info->polymorphic)
2086 edge->indirect_info->context.dump (f);
2089 if (instrumentation_clone)
2090 fprintf (f, " Is instrumented version.\n");
2091 else if (instrumented_version)
2092 fprintf (f, " Has instrumented version.\n");
2095 /* Dump call graph node NODE to stderr. */
2097 DEBUG_FUNCTION void
2098 cgraph_node::debug (void)
2100 dump (stderr);
2103 /* Dump the callgraph to file F. */
2105 void
2106 cgraph_node::dump_cgraph (FILE *f)
2108 cgraph_node *node;
2110 fprintf (f, "callgraph:\n\n");
2111 FOR_EACH_FUNCTION (node)
2112 node->dump (f);
2115 /* Return true when the DECL can possibly be inlined. */
2117 bool
2118 cgraph_function_possibly_inlined_p (tree decl)
2120 if (!symtab->global_info_ready)
2121 return !DECL_UNINLINABLE (decl);
2122 return DECL_POSSIBLY_INLINED (decl);
2125 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2126 void
2127 cgraph_node::unnest (void)
2129 cgraph_node **node2 = &origin->nested;
2130 gcc_assert (origin);
2132 while (*node2 != this)
2133 node2 = &(*node2)->next_nested;
2134 *node2 = next_nested;
2135 origin = NULL;
2138 /* Return function availability. See cgraph.h for description of individual
2139 return values. */
2140 enum availability
2141 cgraph_node::get_availability (void)
2143 enum availability avail;
2144 if (!analyzed)
2145 avail = AVAIL_NOT_AVAILABLE;
2146 else if (local.local)
2147 avail = AVAIL_LOCAL;
2148 else if (alias && weakref)
2149 ultimate_alias_target (&avail);
2150 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2151 avail = AVAIL_INTERPOSABLE;
2152 else if (!externally_visible)
2153 avail = AVAIL_AVAILABLE;
2154 /* Inline functions are safe to be analyzed even if their symbol can
2155 be overwritten at runtime. It is not meaningful to enforce any sane
2156 behaviour on replacing inline function by different body. */
2157 else if (DECL_DECLARED_INLINE_P (decl))
2158 avail = AVAIL_AVAILABLE;
2160 /* If the function can be overwritten, return OVERWRITABLE. Take
2161 care at least of two notable extensions - the COMDAT functions
2162 used to share template instantiations in C++ (this is symmetric
2163 to code cp_cannot_inline_tree_fn and probably shall be shared and
2164 the inlinability hooks completely eliminated).
2166 ??? Does the C++ one definition rule allow us to always return
2167 AVAIL_AVAILABLE here? That would be good reason to preserve this
2168 bit. */
2170 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2171 avail = AVAIL_INTERPOSABLE;
2172 else avail = AVAIL_AVAILABLE;
2174 return avail;
2177 /* Worker for cgraph_node_can_be_local_p. */
2178 static bool
2179 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2181 return !(!node->force_output
2182 && ((DECL_COMDAT (node->decl)
2183 && !node->forced_by_abi
2184 && !node->used_from_object_file_p ()
2185 && !node->same_comdat_group)
2186 || !node->externally_visible));
2189 /* Return true if cgraph_node can be made local for API change.
2190 Extern inline functions and C++ COMDAT functions can be made local
2191 at the expense of possible code size growth if function is used in multiple
2192 compilation units. */
2193 bool
2194 cgraph_node::can_be_local_p (void)
2196 return (!address_taken
2197 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2198 NULL, true));
2201 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2202 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2203 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2204 skipped. */
2205 bool
2206 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2207 (cgraph_node *, void *),
2208 void *data,
2209 bool include_overwritable,
2210 bool exclude_virtual_thunks)
2212 cgraph_edge *e;
2213 ipa_ref *ref;
2215 if (callback (this, data))
2216 return true;
2217 for (e = callers; e; e = e->next_caller)
2218 if (e->caller->thunk.thunk_p
2219 && (include_overwritable
2220 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2221 && !(exclude_virtual_thunks
2222 && e->caller->thunk.virtual_offset_p))
2223 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2224 include_overwritable,
2225 exclude_virtual_thunks))
2226 return true;
2228 FOR_EACH_ALIAS (this, ref)
2230 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2231 if (include_overwritable
2232 || alias->get_availability () > AVAIL_INTERPOSABLE)
2233 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2234 include_overwritable,
2235 exclude_virtual_thunks))
2236 return true;
2238 return false;
2241 /* Call callback on function and aliases associated to the function.
2242 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2243 skipped. */
2245 bool
2246 cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2247 void *),
2248 void *data,
2249 bool include_overwritable)
2251 ipa_ref *ref;
2253 if (callback (this, data))
2254 return true;
2256 FOR_EACH_ALIAS (this, ref)
2258 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2259 if (include_overwritable
2260 || alias->get_availability () > AVAIL_INTERPOSABLE)
2261 if (alias->call_for_symbol_and_aliases (callback, data,
2262 include_overwritable))
2263 return true;
2265 return false;
2268 /* Worker to bring NODE local. */
2270 bool
2271 cgraph_node::make_local (cgraph_node *node, void *)
2273 gcc_checking_assert (node->can_be_local_p ());
2274 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2276 node->make_decl_local ();
2277 node->set_section (NULL);
2278 node->set_comdat_group (NULL);
2279 node->externally_visible = false;
2280 node->forced_by_abi = false;
2281 node->local.local = true;
2282 node->set_section (NULL);
2283 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2284 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2285 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2286 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2288 return false;
2291 /* Bring cgraph node local. */
2293 void
2294 cgraph_node::make_local (void)
2296 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2299 /* Worker to set nothrow flag. */
2301 static bool
2302 cgraph_set_nothrow_flag_1 (cgraph_node *node, void *data)
2304 cgraph_edge *e;
2306 TREE_NOTHROW (node->decl) = data != NULL;
2308 if (data != NULL)
2309 for (e = node->callers; e; e = e->next_caller)
2310 e->can_throw_external = false;
2311 return false;
2314 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2315 if any to NOTHROW. */
2317 void
2318 cgraph_node::set_nothrow_flag (bool nothrow)
2320 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2321 (void *)(size_t)nothrow, false);
2324 /* Worker to set const flag. */
2326 static bool
2327 cgraph_set_const_flag_1 (cgraph_node *node, void *data)
2329 /* Static constructors and destructors without a side effect can be
2330 optimized out. */
2331 if (data && !((size_t)data & 2))
2333 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2334 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2335 if (DECL_STATIC_DESTRUCTOR (node->decl))
2336 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2338 TREE_READONLY (node->decl) = data != NULL;
2339 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2340 return false;
2343 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2344 if any to READONLY. */
2346 void
2347 cgraph_node::set_const_flag (bool readonly, bool looping)
2349 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2350 (void *)(size_t)(readonly + (int)looping * 2),
2351 false, true);
2354 /* Worker to set pure flag. */
2356 static bool
2357 cgraph_set_pure_flag_1 (cgraph_node *node, void *data)
2359 /* Static constructors and destructors without a side effect can be
2360 optimized out. */
2361 if (data && !((size_t)data & 2))
2363 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2364 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2365 if (DECL_STATIC_DESTRUCTOR (node->decl))
2366 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2368 DECL_PURE_P (node->decl) = data != NULL;
2369 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2370 return false;
2373 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2374 if any to PURE. */
2376 void
2377 cgraph_node::set_pure_flag (bool pure, bool looping)
2379 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2380 (void *)(size_t)(pure + (int)looping * 2),
2381 false, true);
2384 /* Return true when cgraph_node can not return or throw and thus
2385 it is safe to ignore its side effects for IPA analysis. */
2387 bool
2388 cgraph_node::cannot_return_p (void)
2390 int flags = flags_from_decl_or_type (decl);
2391 if (!opt_for_fn (decl, flag_exceptions))
2392 return (flags & ECF_NORETURN) != 0;
2393 else
2394 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2395 == (ECF_NORETURN | ECF_NOTHROW));
2398 /* Return true when call of edge can not lead to return from caller
2399 and thus it is safe to ignore its side effects for IPA analysis
2400 when computing side effects of the caller.
2401 FIXME: We could actually mark all edges that have no reaching
2402 patch to the exit block or throw to get better results. */
2403 bool
2404 cgraph_edge::cannot_lead_to_return_p (void)
2406 if (caller->cannot_return_p ())
2407 return true;
2408 if (indirect_unknown_callee)
2410 int flags = indirect_info->ecf_flags;
2411 if (!opt_for_fn (caller->decl, flag_exceptions))
2412 return (flags & ECF_NORETURN) != 0;
2413 else
2414 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2415 == (ECF_NORETURN | ECF_NOTHROW));
2417 else
2418 return callee->cannot_return_p ();
2421 /* Return true if the call can be hot. */
2423 bool
2424 cgraph_edge::maybe_hot_p (void)
2426 /* TODO: Export profile_status from cfun->cfg to cgraph_node. */
2427 if (profile_info
2428 && opt_for_fn (caller->decl, flag_branch_probabilities)
2429 && !maybe_hot_count_p (NULL, count))
2430 return false;
2431 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2432 || (callee
2433 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2434 return false;
2435 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2436 && (callee
2437 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2438 return false;
2439 if (opt_for_fn (caller->decl, optimize_size))
2440 return false;
2441 if (caller->frequency == NODE_FREQUENCY_HOT)
2442 return true;
2443 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE
2444 && frequency < CGRAPH_FREQ_BASE * 3 / 2)
2445 return false;
2446 if (opt_for_fn (caller->decl, flag_guess_branch_prob))
2448 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0
2449 || frequency <= (CGRAPH_FREQ_BASE
2450 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
2451 return false;
2453 return true;
2456 /* Return true when function can be removed from callgraph
2457 if all direct calls are eliminated. */
2459 bool
2460 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2462 gcc_assert (!global.inlined_to);
2463 /* Instrumentation clones should not be removed before
2464 instrumentation happens. New callers may appear after
2465 instrumentation. */
2466 if (instrumentation_clone
2467 && !chkp_function_instrumented_p (decl))
2468 return false;
2469 /* Extern inlines can always go, we will use the external definition. */
2470 if (DECL_EXTERNAL (decl))
2471 return true;
2472 /* When function is needed, we can not remove it. */
2473 if (force_output || used_from_other_partition)
2474 return false;
2475 if (DECL_STATIC_CONSTRUCTOR (decl)
2476 || DECL_STATIC_DESTRUCTOR (decl))
2477 return false;
2478 /* Only COMDAT functions can be removed if externally visible. */
2479 if (externally_visible
2480 && (!DECL_COMDAT (decl)
2481 || forced_by_abi
2482 || used_from_object_file_p ()))
2483 return false;
2484 return true;
2487 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2489 static bool
2490 nonremovable_p (cgraph_node *node, void *)
2492 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2495 /* Return true when function cgraph_node and its aliases can be removed from
2496 callgraph if all direct calls are eliminated. */
2498 bool
2499 cgraph_node::can_remove_if_no_direct_calls_p (void)
2501 /* Extern inlines can always go, we will use the external definition. */
2502 if (DECL_EXTERNAL (decl))
2503 return true;
2504 if (address_taken)
2505 return false;
2506 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2509 /* Return true when function cgraph_node can be expected to be removed
2510 from program when direct calls in this compilation unit are removed.
2512 As a special case COMDAT functions are
2513 cgraph_can_remove_if_no_direct_calls_p while the are not
2514 cgraph_only_called_directly_p (it is possible they are called from other
2515 unit)
2517 This function behaves as cgraph_only_called_directly_p because eliminating
2518 all uses of COMDAT function does not make it necessarily disappear from
2519 the program unless we are compiling whole program or we do LTO. In this
2520 case we know we win since dynamic linking will not really discard the
2521 linkonce section. */
2523 bool
2524 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2526 gcc_assert (!global.inlined_to);
2528 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2529 NULL, true))
2530 return false;
2531 if (!in_lto_p && !flag_whole_program)
2532 return only_called_directly_p ();
2533 else
2535 if (DECL_EXTERNAL (decl))
2536 return true;
2537 return can_remove_if_no_direct_calls_p ();
2542 /* Worker for cgraph_only_called_directly_p. */
2544 static bool
2545 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2547 return !node->only_called_directly_or_aliased_p ();
2550 /* Return true when function cgraph_node and all its aliases are only called
2551 directly.
2552 i.e. it is not externally visible, address was not taken and
2553 it is not used in any other non-standard way. */
2555 bool
2556 cgraph_node::only_called_directly_p (void)
2558 gcc_assert (ultimate_alias_target () == this);
2559 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2560 NULL, true);
2564 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2566 static bool
2567 collect_callers_of_node_1 (cgraph_node *node, void *data)
2569 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2570 cgraph_edge *cs;
2571 enum availability avail;
2572 node->ultimate_alias_target (&avail);
2574 if (avail > AVAIL_INTERPOSABLE)
2575 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2576 if (!cs->indirect_inlining_edge)
2577 redirect_callers->safe_push (cs);
2578 return false;
2581 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2582 cgraph_node (i.e. are not overwritable). */
2584 vec<cgraph_edge *>
2585 cgraph_node::collect_callers (void)
2587 vec<cgraph_edge *> redirect_callers = vNULL;
2588 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2589 &redirect_callers, false);
2590 return redirect_callers;
2593 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2595 static bool
2596 clone_of_p (cgraph_node *node, cgraph_node *node2)
2598 bool skipped_thunk = false;
2599 node = node->ultimate_alias_target ();
2600 node2 = node2->ultimate_alias_target ();
2602 /* There are no virtual clones of thunks so check former_clone_of or if we
2603 might have skipped thunks because this adjustments are no longer
2604 necessary. */
2605 while (node->thunk.thunk_p)
2607 if (node2->former_clone_of == node->decl)
2608 return true;
2609 if (!node->thunk.this_adjusting)
2610 return false;
2611 node = node->callees->callee->ultimate_alias_target ();
2612 skipped_thunk = true;
2615 if (skipped_thunk)
2617 if (!node2->clone.args_to_skip
2618 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2619 return false;
2620 if (node2->former_clone_of == node->decl)
2621 return true;
2622 else if (!node2->clone_of)
2623 return false;
2626 while (node != node2 && node2)
2627 node2 = node2->clone_of;
2628 return node2 != NULL;
2631 /* Verify edge E count and frequency. */
2633 static bool
2634 verify_edge_count_and_frequency (cgraph_edge *e)
2636 bool error_found = false;
2637 if (e->count < 0)
2639 error ("caller edge count is negative");
2640 error_found = true;
2642 if (e->frequency < 0)
2644 error ("caller edge frequency is negative");
2645 error_found = true;
2647 if (e->frequency > CGRAPH_FREQ_MAX)
2649 error ("caller edge frequency is too large");
2650 error_found = true;
2652 if (gimple_has_body_p (e->caller->decl)
2653 && !e->caller->global.inlined_to
2654 && !e->speculative
2655 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2656 Remove this once edges are actually removed from the function at that time. */
2657 && (e->frequency
2658 || (inline_edge_summary_vec.exists ()
2659 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2660 || !inline_edge_summary (e)->predicate)))
2661 && (e->frequency
2662 != compute_call_stmt_bb_frequency (e->caller->decl,
2663 gimple_bb (e->call_stmt))))
2665 error ("caller edge frequency %i does not match BB frequency %i",
2666 e->frequency,
2667 compute_call_stmt_bb_frequency (e->caller->decl,
2668 gimple_bb (e->call_stmt)));
2669 error_found = true;
2671 return error_found;
2674 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2675 static void
2676 cgraph_debug_gimple_stmt (function *this_cfun, gimple stmt)
2678 bool fndecl_was_null = false;
2679 /* debug_gimple_stmt needs correct cfun */
2680 if (cfun != this_cfun)
2681 set_cfun (this_cfun);
2682 /* ...and an actual current_function_decl */
2683 if (!current_function_decl)
2685 current_function_decl = this_cfun->decl;
2686 fndecl_was_null = true;
2688 debug_gimple_stmt (stmt);
2689 if (fndecl_was_null)
2690 current_function_decl = NULL;
2693 /* Verify that call graph edge E corresponds to DECL from the associated
2694 statement. Return true if the verification should fail. */
2696 static bool
2697 verify_edge_corresponds_to_fndecl (cgraph_edge *e, tree decl)
2699 cgraph_node *node;
2701 if (!decl || e->callee->global.inlined_to)
2702 return false;
2703 if (symtab->state == LTO_STREAMING)
2704 return false;
2705 node = cgraph_node::get (decl);
2707 /* We do not know if a node from a different partition is an alias or what it
2708 aliases and therefore cannot do the former_clone_of check reliably. When
2709 body_removed is set, we have lost all information about what was alias or
2710 thunk of and also cannot proceed. */
2711 if (!node
2712 || node->body_removed
2713 || node->in_other_partition
2714 || node->icf_merged
2715 || e->callee->in_other_partition)
2716 return false;
2718 node = node->ultimate_alias_target ();
2720 /* Optimizers can redirect unreachable calls or calls triggering undefined
2721 behaviour to builtin_unreachable. */
2722 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2723 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2724 return false;
2726 if (e->callee->former_clone_of != node->decl
2727 && (node != e->callee->ultimate_alias_target ())
2728 && !clone_of_p (node, e->callee))
2729 return true;
2730 else
2731 return false;
2734 /* Verify cgraph nodes of given cgraph node. */
2735 DEBUG_FUNCTION void
2736 cgraph_node::verify_node (void)
2738 cgraph_edge *e;
2739 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2740 basic_block this_block;
2741 gimple_stmt_iterator gsi;
2742 bool error_found = false;
2744 if (seen_error ())
2745 return;
2747 timevar_push (TV_CGRAPH_VERIFY);
2748 error_found |= verify_base ();
2749 for (e = callees; e; e = e->next_callee)
2750 if (e->aux)
2752 error ("aux field set for edge %s->%s",
2753 identifier_to_locale (e->caller->name ()),
2754 identifier_to_locale (e->callee->name ()));
2755 error_found = true;
2757 if (count < 0)
2759 error ("execution count is negative");
2760 error_found = true;
2762 if (global.inlined_to && same_comdat_group)
2764 error ("inline clone in same comdat group list");
2765 error_found = true;
2767 if (!definition && !in_other_partition && local.local)
2769 error ("local symbols must be defined");
2770 error_found = true;
2772 if (global.inlined_to && externally_visible)
2774 error ("externally visible inline clone");
2775 error_found = true;
2777 if (global.inlined_to && address_taken)
2779 error ("inline clone with address taken");
2780 error_found = true;
2782 if (global.inlined_to && force_output)
2784 error ("inline clone is forced to output");
2785 error_found = true;
2787 for (e = indirect_calls; e; e = e->next_callee)
2789 if (e->aux)
2791 error ("aux field set for indirect edge from %s",
2792 identifier_to_locale (e->caller->name ()));
2793 error_found = true;
2795 if (!e->indirect_unknown_callee
2796 || !e->indirect_info)
2798 error ("An indirect edge from %s is not marked as indirect or has "
2799 "associated indirect_info, the corresponding statement is: ",
2800 identifier_to_locale (e->caller->name ()));
2801 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2802 error_found = true;
2805 bool check_comdat = comdat_local_p ();
2806 for (e = callers; e; e = e->next_caller)
2808 if (verify_edge_count_and_frequency (e))
2809 error_found = true;
2810 if (check_comdat
2811 && !in_same_comdat_group_p (e->caller))
2813 error ("comdat-local function called by %s outside its comdat",
2814 identifier_to_locale (e->caller->name ()));
2815 error_found = true;
2817 if (!e->inline_failed)
2819 if (global.inlined_to
2820 != (e->caller->global.inlined_to
2821 ? e->caller->global.inlined_to : e->caller))
2823 error ("inlined_to pointer is wrong");
2824 error_found = true;
2826 if (callers->next_caller)
2828 error ("multiple inline callers");
2829 error_found = true;
2832 else
2833 if (global.inlined_to)
2835 error ("inlined_to pointer set for noninline callers");
2836 error_found = true;
2839 for (e = indirect_calls; e; e = e->next_callee)
2840 if (verify_edge_count_and_frequency (e))
2841 error_found = true;
2842 if (!callers && global.inlined_to)
2844 error ("inlined_to pointer is set but no predecessors found");
2845 error_found = true;
2847 if (global.inlined_to == this)
2849 error ("inlined_to pointer refers to itself");
2850 error_found = true;
2853 if (clone_of)
2855 cgraph_node *n;
2856 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2857 if (n == this)
2858 break;
2859 if (!n)
2861 error ("cgraph_node has wrong clone_of");
2862 error_found = true;
2865 if (clones)
2867 cgraph_node *n;
2868 for (n = clones; n; n = n->next_sibling_clone)
2869 if (n->clone_of != this)
2870 break;
2871 if (n)
2873 error ("cgraph_node has wrong clone list");
2874 error_found = true;
2877 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2879 error ("cgraph_node is in clone list but it is not clone");
2880 error_found = true;
2882 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2884 error ("cgraph_node has wrong prev_clone pointer");
2885 error_found = true;
2887 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2889 error ("double linked list of clones corrupted");
2890 error_found = true;
2893 if (analyzed && alias)
2895 bool ref_found = false;
2896 int i;
2897 ipa_ref *ref = NULL;
2899 if (callees)
2901 error ("Alias has call edges");
2902 error_found = true;
2904 for (i = 0; iterate_reference (i, ref); i++)
2905 if (ref->use == IPA_REF_CHKP)
2907 else if (ref->use != IPA_REF_ALIAS)
2909 error ("Alias has non-alias reference");
2910 error_found = true;
2912 else if (ref_found)
2914 error ("Alias has more than one alias reference");
2915 error_found = true;
2917 else
2918 ref_found = true;
2919 if (!ref_found)
2921 error ("Analyzed alias has no reference");
2922 error_found = true;
2926 /* Check instrumented version reference. */
2927 if (instrumented_version
2928 && instrumented_version->instrumented_version != this)
2930 error ("Instrumentation clone does not reference original node");
2931 error_found = true;
2934 /* Cannot have orig_decl for not instrumented nodes. */
2935 if (!instrumentation_clone && orig_decl)
2937 error ("Not instrumented node has non-NULL original declaration");
2938 error_found = true;
2941 /* If original not instrumented node still exists then we may check
2942 original declaration is set properly. */
2943 if (instrumented_version
2944 && orig_decl
2945 && orig_decl != instrumented_version->decl)
2947 error ("Instrumented node has wrong original declaration");
2948 error_found = true;
2951 /* Check all nodes have chkp reference to their instrumented versions. */
2952 if (analyzed
2953 && instrumented_version
2954 && !instrumentation_clone)
2956 bool ref_found = false;
2957 int i;
2958 struct ipa_ref *ref;
2960 for (i = 0; iterate_reference (i, ref); i++)
2961 if (ref->use == IPA_REF_CHKP)
2963 if (ref_found)
2965 error ("Node has more than one chkp reference");
2966 error_found = true;
2968 if (ref->referred != instrumented_version)
2970 error ("Wrong node is referenced with chkp reference");
2971 error_found = true;
2973 ref_found = true;
2976 if (!ref_found)
2978 error ("Analyzed node has no reference to instrumented version");
2979 error_found = true;
2983 if (analyzed && thunk.thunk_p)
2985 if (!callees)
2987 error ("No edge out of thunk node");
2988 error_found = true;
2990 else if (callees->next_callee)
2992 error ("More than one edge out of thunk node");
2993 error_found = true;
2995 if (gimple_has_body_p (decl))
2997 error ("Thunk is not supposed to have body");
2998 error_found = true;
3000 if (thunk.add_pointer_bounds_args
3001 && !instrumented_version->semantically_equivalent_p (callees->callee))
3003 error ("Instrumentation thunk has wrong edge callee");
3004 error_found = true;
3007 else if (analyzed && gimple_has_body_p (decl)
3008 && !TREE_ASM_WRITTEN (decl)
3009 && (!DECL_EXTERNAL (decl) || global.inlined_to)
3010 && !flag_wpa)
3012 if (this_cfun->cfg)
3014 hash_set<gimple> stmts;
3015 int i;
3016 ipa_ref *ref = NULL;
3018 /* Reach the trees by walking over the CFG, and note the
3019 enclosing basic-blocks in the call edges. */
3020 FOR_EACH_BB_FN (this_block, this_cfun)
3022 for (gsi = gsi_start_phis (this_block);
3023 !gsi_end_p (gsi); gsi_next (&gsi))
3024 stmts.add (gsi_stmt (gsi));
3025 for (gsi = gsi_start_bb (this_block);
3026 !gsi_end_p (gsi);
3027 gsi_next (&gsi))
3029 gimple stmt = gsi_stmt (gsi);
3030 stmts.add (stmt);
3031 if (is_gimple_call (stmt))
3033 cgraph_edge *e = get_edge (stmt);
3034 tree decl = gimple_call_fndecl (stmt);
3035 if (e)
3037 if (e->aux)
3039 error ("shared call_stmt:");
3040 cgraph_debug_gimple_stmt (this_cfun, stmt);
3041 error_found = true;
3043 if (!e->indirect_unknown_callee)
3045 if (verify_edge_corresponds_to_fndecl (e, decl))
3047 error ("edge points to wrong declaration:");
3048 debug_tree (e->callee->decl);
3049 fprintf (stderr," Instead of:");
3050 debug_tree (decl);
3051 error_found = true;
3054 else if (decl)
3056 error ("an indirect edge with unknown callee "
3057 "corresponding to a call_stmt with "
3058 "a known declaration:");
3059 error_found = true;
3060 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3062 e->aux = (void *)1;
3064 else if (decl)
3066 error ("missing callgraph edge for call stmt:");
3067 cgraph_debug_gimple_stmt (this_cfun, stmt);
3068 error_found = true;
3073 for (i = 0; iterate_reference (i, ref); i++)
3074 if (ref->stmt && !stmts.contains (ref->stmt))
3076 error ("reference to dead statement");
3077 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3078 error_found = true;
3081 else
3082 /* No CFG available?! */
3083 gcc_unreachable ();
3085 for (e = callees; e; e = e->next_callee)
3087 if (!e->aux)
3089 error ("edge %s->%s has no corresponding call_stmt",
3090 identifier_to_locale (e->caller->name ()),
3091 identifier_to_locale (e->callee->name ()));
3092 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3093 error_found = true;
3095 e->aux = 0;
3097 for (e = indirect_calls; e; e = e->next_callee)
3099 if (!e->aux && !e->speculative)
3101 error ("an indirect edge from %s has no corresponding call_stmt",
3102 identifier_to_locale (e->caller->name ()));
3103 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3104 error_found = true;
3106 e->aux = 0;
3109 if (error_found)
3111 dump (stderr);
3112 internal_error ("verify_cgraph_node failed");
3114 timevar_pop (TV_CGRAPH_VERIFY);
3117 /* Verify whole cgraph structure. */
3118 DEBUG_FUNCTION void
3119 cgraph_node::verify_cgraph_nodes (void)
3121 cgraph_node *node;
3123 if (seen_error ())
3124 return;
3126 FOR_EACH_FUNCTION (node)
3127 node->verify ();
3130 /* Walk the alias chain to return the function cgraph_node is alias of.
3131 Walk through thunks, too.
3132 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3134 cgraph_node *
3135 cgraph_node::function_symbol (enum availability *availability)
3137 cgraph_node *node = ultimate_alias_target (availability);
3139 while (node->thunk.thunk_p)
3141 node = node->callees->callee;
3142 if (availability)
3144 enum availability a;
3145 a = node->get_availability ();
3146 if (a < *availability)
3147 *availability = a;
3149 node = node->ultimate_alias_target (availability);
3151 return node;
3154 /* Walk the alias chain to return the function cgraph_node is alias of.
3155 Walk through non virtual thunks, too. Thus we return either a function
3156 or a virtual thunk node.
3157 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3159 cgraph_node *
3160 cgraph_node::function_or_virtual_thunk_symbol
3161 (enum availability *availability)
3163 cgraph_node *node = ultimate_alias_target (availability);
3165 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3167 node = node->callees->callee;
3168 if (availability)
3170 enum availability a;
3171 a = node->get_availability ();
3172 if (a < *availability)
3173 *availability = a;
3175 node = node->ultimate_alias_target (availability);
3177 return node;
3180 /* When doing LTO, read cgraph_node's body from disk if it is not already
3181 present. */
3183 bool
3184 cgraph_node::get_untransformed_body (void)
3186 lto_file_decl_data *file_data;
3187 const char *data, *name;
3188 size_t len;
3189 tree decl = this->decl;
3191 if (DECL_RESULT (decl))
3192 return false;
3194 gcc_assert (in_lto_p);
3196 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3198 file_data = lto_file_data;
3199 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3201 /* We may have renamed the declaration, e.g., a static function. */
3202 name = lto_get_decl_name_mapping (file_data, name);
3204 data = lto_get_section_data (file_data, LTO_section_function_body,
3205 name, &len);
3206 if (!data)
3207 fatal_error ("%s: section %s is missing",
3208 file_data->file_name,
3209 name);
3211 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3213 lto_input_function_body (file_data, this, data);
3214 lto_stats.num_function_bodies++;
3215 lto_free_section_data (file_data, LTO_section_function_body, name,
3216 data, len);
3217 lto_free_function_in_decl_state_for_node (this);
3219 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3221 return true;
3224 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3225 if it is not already present. When some IPA transformations are scheduled,
3226 apply them. */
3228 bool
3229 cgraph_node::get_body (void)
3231 bool updated;
3233 updated = get_untransformed_body ();
3235 /* Getting transformed body makes no sense for inline clones;
3236 we should never use this on real clones becuase they are materialized
3237 early.
3238 TODO: Materializing clones here will likely lead to smaller LTRANS
3239 footprint. */
3240 gcc_assert (!global.inlined_to && !clone_of);
3241 if (ipa_transforms_to_apply.exists ())
3243 opt_pass *saved_current_pass = current_pass;
3244 FILE *saved_dump_file = dump_file;
3245 int saved_dump_flags = dump_flags;
3247 push_cfun (DECL_STRUCT_FUNCTION (decl));
3248 execute_all_ipa_transforms ();
3249 cgraph_edge::rebuild_edges ();
3250 free_dominance_info (CDI_DOMINATORS);
3251 free_dominance_info (CDI_POST_DOMINATORS);
3252 pop_cfun ();
3253 updated = true;
3255 current_pass = saved_current_pass;
3256 dump_file = saved_dump_file;
3257 dump_flags = saved_dump_flags;
3259 return updated;
3262 /* Return the DECL_STRUCT_FUNCTION of the function. */
3264 struct function *
3265 cgraph_node::get_fun (void)
3267 cgraph_node *node = this;
3268 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3270 while (!fun && node->clone_of)
3272 node = node->clone_of;
3273 fun = DECL_STRUCT_FUNCTION (node->decl);
3276 return fun;
3279 /* Verify if the type of the argument matches that of the function
3280 declaration. If we cannot verify this or there is a mismatch,
3281 return false. */
3283 static bool
3284 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3286 tree parms, p;
3287 unsigned int i, nargs;
3289 /* Calls to internal functions always match their signature. */
3290 if (gimple_call_internal_p (stmt))
3291 return true;
3293 nargs = gimple_call_num_args (stmt);
3295 /* Get argument types for verification. */
3296 if (fndecl)
3297 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3298 else
3299 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3301 /* Verify if the type of the argument matches that of the function
3302 declaration. If we cannot verify this or there is a mismatch,
3303 return false. */
3304 if (fndecl && DECL_ARGUMENTS (fndecl))
3306 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3307 i < nargs;
3308 i++, p = DECL_CHAIN (p))
3310 tree arg;
3311 /* We cannot distinguish a varargs function from the case
3312 of excess parameters, still deferring the inlining decision
3313 to the callee is possible. */
3314 if (!p)
3315 break;
3316 arg = gimple_call_arg (stmt, i);
3317 if (p == error_mark_node
3318 || DECL_ARG_TYPE (p) == error_mark_node
3319 || arg == error_mark_node
3320 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3321 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3322 return false;
3324 if (args_count_match && p)
3325 return false;
3327 else if (parms)
3329 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3331 tree arg;
3332 /* If this is a varargs function defer inlining decision
3333 to callee. */
3334 if (!p)
3335 break;
3336 arg = gimple_call_arg (stmt, i);
3337 if (TREE_VALUE (p) == error_mark_node
3338 || arg == error_mark_node
3339 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3340 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3341 && !fold_convertible_p (TREE_VALUE (p), arg)))
3342 return false;
3345 else
3347 if (nargs != 0)
3348 return false;
3350 return true;
3353 /* Verify if the type of the argument and lhs of CALL_STMT matches
3354 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3355 true, the arg count needs to be the same.
3356 If we cannot verify this or there is a mismatch, return false. */
3358 bool
3359 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3360 bool args_count_match)
3362 tree lhs;
3364 if ((DECL_RESULT (callee)
3365 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3366 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3367 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3368 TREE_TYPE (lhs))
3369 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3370 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3371 return false;
3372 return true;
3375 /* Reset all state within cgraph.c so that we can rerun the compiler
3376 within the same process. For use by toplev::finalize. */
3378 void
3379 cgraph_c_finalize (void)
3381 symtab = NULL;
3383 x_cgraph_nodes_queue = NULL;
3385 cgraph_fnver_htab = NULL;
3386 version_info_node = NULL;
3389 #include "gt-cgraph.h"