testsuite: Correct vec-rlmi-rlnm.c testsuite expected result
[official-gcc.git] / gcc / cgraph.c
blob067984d773cc34393ad6624d3ef2a65217d090bc
1 /* Callgraph handling code.
2 Copyright (C) 2003-2020 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 inter-procedural
24 optimization. It represents a multi-graph where nodes are functions
25 (symbols within symbol table) and edges are call sites. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "backend.h"
31 #include "target.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "gimple.h"
35 #include "predict.h"
36 #include "alloc-pool.h"
37 #include "gimple-ssa.h"
38 #include "cgraph.h"
39 #include "lto-streamer.h"
40 #include "fold-const.h"
41 #include "varasm.h"
42 #include "calls.h"
43 #include "print-tree.h"
44 #include "langhooks.h"
45 #include "intl.h"
46 #include "tree-eh.h"
47 #include "gimple-iterator.h"
48 #include "tree-cfg.h"
49 #include "tree-ssa.h"
50 #include "value-prof.h"
51 #include "ipa-utils.h"
52 #include "symbol-summary.h"
53 #include "tree-vrp.h"
54 #include "ipa-prop.h"
55 #include "ipa-fnsummary.h"
56 #include "cfgloop.h"
57 #include "gimple-pretty-print.h"
58 #include "tree-dfa.h"
59 #include "profile.h"
60 #include "context.h"
61 #include "gimplify.h"
62 #include "stringpool.h"
63 #include "attribs.h"
64 #include "selftest.h"
65 #include "tree-into-ssa.h"
66 #include "ipa-inline.h"
67 #include "tree-nested.h"
69 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
70 #include "tree-pass.h"
72 /* Queue of cgraph nodes scheduled to be lowered. */
73 symtab_node *x_cgraph_nodes_queue;
74 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
76 /* Symbol table global context. */
77 symbol_table *symtab;
79 /* List of hooks triggered on cgraph_edge events. */
80 struct cgraph_edge_hook_list {
81 cgraph_edge_hook hook;
82 void *data;
83 struct cgraph_edge_hook_list *next;
86 /* List of hooks triggered on cgraph_node events. */
87 struct cgraph_node_hook_list {
88 cgraph_node_hook hook;
89 void *data;
90 struct cgraph_node_hook_list *next;
93 /* List of hooks triggered on events involving two cgraph_edges. */
94 struct cgraph_2edge_hook_list {
95 cgraph_2edge_hook hook;
96 void *data;
97 struct cgraph_2edge_hook_list *next;
100 /* List of hooks triggered on events involving two cgraph_nodes. */
101 struct cgraph_2node_hook_list {
102 cgraph_2node_hook hook;
103 void *data;
104 struct cgraph_2node_hook_list *next;
107 /* Hash descriptor for cgraph_function_version_info. */
109 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
111 static hashval_t hash (cgraph_function_version_info *);
112 static bool equal (cgraph_function_version_info *,
113 cgraph_function_version_info *);
116 /* Map a cgraph_node to cgraph_function_version_info using this htab.
117 The cgraph_function_version_info has a THIS_NODE field that is the
118 corresponding cgraph_node.. */
120 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
122 /* Hash function for cgraph_fnver_htab. */
123 hashval_t
124 function_version_hasher::hash (cgraph_function_version_info *ptr)
126 int uid = ptr->this_node->get_uid ();
127 return (hashval_t)(uid);
130 /* eq function for cgraph_fnver_htab. */
131 bool
132 function_version_hasher::equal (cgraph_function_version_info *n1,
133 cgraph_function_version_info *n2)
135 return n1->this_node->get_uid () == n2->this_node->get_uid ();
138 /* Mark as GC root all allocated nodes. */
139 static GTY(()) struct cgraph_function_version_info *
140 version_info_node = NULL;
142 /* Return true if NODE's address can be compared. */
144 bool
145 symtab_node::address_can_be_compared_p ()
147 /* Address of virtual tables and functions is never compared. */
148 if (DECL_VIRTUAL_P (decl))
149 return false;
150 /* Address of C++ cdtors is never compared. */
151 if (is_a <cgraph_node *> (this)
152 && (DECL_CXX_CONSTRUCTOR_P (decl)
153 || DECL_CXX_DESTRUCTOR_P (decl)))
154 return false;
155 /* Constant pool symbols addresses are never compared.
156 flag_merge_constants permits us to assume the same on readonly vars. */
157 if (is_a <varpool_node *> (this)
158 && (DECL_IN_CONSTANT_POOL (decl)
159 || (flag_merge_constants >= 2
160 && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
161 return false;
162 return true;
165 /* Get the cgraph_function_version_info node corresponding to node. */
166 cgraph_function_version_info *
167 cgraph_node::function_version (void)
169 cgraph_function_version_info key;
170 key.this_node = this;
172 if (cgraph_fnver_htab == NULL)
173 return NULL;
175 return cgraph_fnver_htab->find (&key);
178 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
179 corresponding to cgraph_node NODE. */
180 cgraph_function_version_info *
181 cgraph_node::insert_new_function_version (void)
183 version_info_node = NULL;
184 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
185 version_info_node->this_node = this;
187 if (cgraph_fnver_htab == NULL)
188 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
190 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
191 = version_info_node;
192 return version_info_node;
195 /* Remove the cgraph_function_version_info node given by DECL_V. */
196 static void
197 delete_function_version (cgraph_function_version_info *decl_v)
199 if (decl_v == NULL)
200 return;
202 if (version_info_node == decl_v)
203 version_info_node = NULL;
205 if (decl_v->prev != NULL)
206 decl_v->prev->next = decl_v->next;
208 if (decl_v->next != NULL)
209 decl_v->next->prev = decl_v->prev;
211 if (cgraph_fnver_htab != NULL)
212 cgraph_fnver_htab->remove_elt (decl_v);
215 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
216 DECL is a duplicate declaration. */
217 void
218 cgraph_node::delete_function_version_by_decl (tree decl)
220 cgraph_node *decl_node = cgraph_node::get (decl);
222 if (decl_node == NULL)
223 return;
225 delete_function_version (decl_node->function_version ());
227 decl_node->remove ();
230 /* Record that DECL1 and DECL2 are semantically identical function
231 versions. */
232 void
233 cgraph_node::record_function_versions (tree decl1, tree decl2)
235 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
236 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
237 cgraph_function_version_info *decl1_v = NULL;
238 cgraph_function_version_info *decl2_v = NULL;
239 cgraph_function_version_info *before;
240 cgraph_function_version_info *after;
242 gcc_assert (decl1_node != NULL && decl2_node != NULL);
243 decl1_v = decl1_node->function_version ();
244 decl2_v = decl2_node->function_version ();
246 if (decl1_v != NULL && decl2_v != NULL)
247 return;
249 if (decl1_v == NULL)
250 decl1_v = decl1_node->insert_new_function_version ();
252 if (decl2_v == NULL)
253 decl2_v = decl2_node->insert_new_function_version ();
255 /* Chain decl2_v and decl1_v. All semantically identical versions
256 will be chained together. */
258 before = decl1_v;
259 after = decl2_v;
261 while (before->next != NULL)
262 before = before->next;
264 while (after->prev != NULL)
265 after= after->prev;
267 before->next = after;
268 after->prev = before;
271 /* Initialize callgraph dump file. */
273 void
274 symbol_table::initialize (void)
276 if (!dump_file)
277 dump_file = dump_begin (TDI_cgraph, NULL);
279 if (!ipa_clones_dump_file)
280 ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
283 /* Allocate new callgraph node and insert it into basic data structures. */
285 cgraph_node *
286 symbol_table::create_empty (void)
288 cgraph_count++;
289 return new (ggc_alloc<cgraph_node> ()) cgraph_node (cgraph_max_uid++);
292 /* Register HOOK to be called with DATA on each removed edge. */
293 cgraph_edge_hook_list *
294 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
296 cgraph_edge_hook_list *entry;
297 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
299 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
300 entry->hook = hook;
301 entry->data = data;
302 entry->next = NULL;
303 while (*ptr)
304 ptr = &(*ptr)->next;
305 *ptr = entry;
306 return entry;
309 /* Remove ENTRY from the list of hooks called on removing edges. */
310 void
311 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
313 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
315 while (*ptr != entry)
316 ptr = &(*ptr)->next;
317 *ptr = entry->next;
318 free (entry);
321 /* Call all edge removal hooks. */
322 void
323 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
325 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
326 while (entry)
328 entry->hook (e, entry->data);
329 entry = entry->next;
333 /* Register HOOK to be called with DATA on each removed node. */
334 cgraph_node_hook_list *
335 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
337 cgraph_node_hook_list *entry;
338 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
340 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
341 entry->hook = hook;
342 entry->data = data;
343 entry->next = NULL;
344 while (*ptr)
345 ptr = &(*ptr)->next;
346 *ptr = entry;
347 return entry;
350 /* Remove ENTRY from the list of hooks called on removing nodes. */
351 void
352 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
354 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
356 while (*ptr != entry)
357 ptr = &(*ptr)->next;
358 *ptr = entry->next;
359 free (entry);
362 /* Call all node removal hooks. */
363 void
364 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
366 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
367 while (entry)
369 entry->hook (node, entry->data);
370 entry = entry->next;
374 /* Call all node removal hooks. */
375 void
376 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
378 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
379 while (entry)
381 entry->hook (node, entry->data);
382 entry = entry->next;
387 /* Register HOOK to be called with DATA on each inserted node. */
388 cgraph_node_hook_list *
389 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
391 cgraph_node_hook_list *entry;
392 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
394 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
395 entry->hook = hook;
396 entry->data = data;
397 entry->next = NULL;
398 while (*ptr)
399 ptr = &(*ptr)->next;
400 *ptr = entry;
401 return entry;
404 /* Remove ENTRY from the list of hooks called on inserted nodes. */
405 void
406 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
408 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
410 while (*ptr != entry)
411 ptr = &(*ptr)->next;
412 *ptr = entry->next;
413 free (entry);
416 /* Register HOOK to be called with DATA on each duplicated edge. */
417 cgraph_2edge_hook_list *
418 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
420 cgraph_2edge_hook_list *entry;
421 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
423 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
424 entry->hook = hook;
425 entry->data = data;
426 entry->next = NULL;
427 while (*ptr)
428 ptr = &(*ptr)->next;
429 *ptr = entry;
430 return entry;
433 /* Remove ENTRY from the list of hooks called on duplicating edges. */
434 void
435 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
437 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
439 while (*ptr != entry)
440 ptr = &(*ptr)->next;
441 *ptr = entry->next;
442 free (entry);
445 /* Call all edge duplication hooks. */
446 void
447 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
449 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
450 while (entry)
452 entry->hook (cs1, cs2, entry->data);
453 entry = entry->next;
457 /* Register HOOK to be called with DATA on each duplicated node. */
458 cgraph_2node_hook_list *
459 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
461 cgraph_2node_hook_list *entry;
462 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
464 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
465 entry->hook = hook;
466 entry->data = data;
467 entry->next = NULL;
468 while (*ptr)
469 ptr = &(*ptr)->next;
470 *ptr = entry;
471 return entry;
474 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
475 void
476 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
478 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
480 while (*ptr != entry)
481 ptr = &(*ptr)->next;
482 *ptr = entry->next;
483 free (entry);
486 /* Call all node duplication hooks. */
487 void
488 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
489 cgraph_node *node2)
491 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
492 while (entry)
494 entry->hook (node, node2, entry->data);
495 entry = entry->next;
499 /* Return cgraph node assigned to DECL. Create new one when needed. */
501 cgraph_node *
502 cgraph_node::create (tree decl)
504 cgraph_node *node = symtab->create_empty ();
505 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
507 node->decl = decl;
509 if ((flag_openacc || flag_openmp)
510 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
512 node->offloadable = 1;
513 if (ENABLE_OFFLOADING)
514 g->have_offload = true;
517 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
518 node->ifunc_resolver = true;
520 node->register_symbol ();
521 maybe_record_nested_function (node);
523 return node;
526 /* Try to find a call graph node for declaration DECL and if it does not exist
527 or if it corresponds to an inline clone, create a new one. */
529 cgraph_node *
530 cgraph_node::get_create (tree decl)
532 cgraph_node *first_clone = cgraph_node::get (decl);
534 if (first_clone && !first_clone->inlined_to)
535 return first_clone;
537 cgraph_node *node = cgraph_node::create (decl);
538 if (first_clone)
540 first_clone->clone_of = node;
541 node->clones = first_clone;
542 node->order = first_clone->order;
543 symtab->symtab_prevail_in_asm_name_hash (node);
544 node->decl->decl_with_vis.symtab_node = node;
545 if (dump_file)
546 fprintf (dump_file, "Introduced new external node "
547 "(%s) and turned into root of the clone tree.\n",
548 node->dump_name ());
550 else if (dump_file)
551 fprintf (dump_file, "Introduced new external node "
552 "(%s).\n", node->dump_name ());
553 return node;
556 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
557 the function body is associated with
558 (not necessarily cgraph_node (DECL)). */
560 cgraph_node *
561 cgraph_node::create_alias (tree alias, tree target)
563 cgraph_node *alias_node;
565 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
566 || TREE_CODE (target) == IDENTIFIER_NODE);
567 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
568 alias_node = cgraph_node::get_create (alias);
569 gcc_assert (!alias_node->definition);
570 alias_node->alias_target = target;
571 alias_node->definition = true;
572 alias_node->alias = true;
573 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
574 alias_node->transparent_alias = alias_node->weakref = true;
575 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
576 alias_node->ifunc_resolver = true;
577 return alias_node;
580 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
581 and NULL otherwise.
582 Same body aliases are output whenever the body of DECL is output,
583 and cgraph_node::get (ALIAS) transparently returns
584 cgraph_node::get (DECL). */
586 cgraph_node *
587 cgraph_node::create_same_body_alias (tree alias, tree decl)
589 cgraph_node *n;
591 /* If aliases aren't supported by the assembler, fail. */
592 if (!TARGET_SUPPORTS_ALIASES)
593 return NULL;
595 /* Langhooks can create same body aliases of symbols not defined.
596 Those are useless. Drop them on the floor. */
597 if (symtab->global_info_ready)
598 return NULL;
600 n = cgraph_node::create_alias (alias, decl);
601 n->cpp_implicit_alias = true;
602 if (symtab->cpp_implicit_aliases_done)
603 n->resolve_alias (cgraph_node::get (decl));
604 return n;
607 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
608 aliases DECL with an adjustments made into the first parameter.
609 See comments in struct cgraph_thunk_info for detail on the parameters. */
611 cgraph_node *
612 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
613 HOST_WIDE_INT fixed_offset,
614 HOST_WIDE_INT virtual_value,
615 HOST_WIDE_INT indirect_offset,
616 tree virtual_offset,
617 tree real_alias)
619 cgraph_node *node;
621 node = cgraph_node::get (alias);
622 if (node)
623 node->reset ();
624 else
625 node = cgraph_node::create (alias);
627 /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE. */
628 gcc_checking_assert (virtual_offset
629 ? virtual_value == wi::to_wide (virtual_offset)
630 : virtual_value == 0);
632 node->thunk.fixed_offset = fixed_offset;
633 node->thunk.virtual_value = virtual_value;
634 node->thunk.indirect_offset = indirect_offset;
635 node->thunk.alias = real_alias;
636 node->thunk.this_adjusting = this_adjusting;
637 node->thunk.virtual_offset_p = virtual_offset != NULL;
638 node->thunk.thunk_p = true;
639 node->definition = true;
641 return node;
644 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
645 Return NULL if there's no such node. */
647 cgraph_node *
648 cgraph_node::get_for_asmname (tree asmname)
650 /* We do not want to look at inline clones. */
651 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
652 node;
653 node = node->next_sharing_asm_name)
655 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
656 if (cn && !cn->inlined_to)
657 return cn;
659 return NULL;
662 /* Returns a hash value for X (which really is a cgraph_edge). */
664 hashval_t
665 cgraph_edge_hasher::hash (cgraph_edge *e)
667 /* This is a really poor hash function, but it is what htab_hash_pointer
668 uses. */
669 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
672 /* Returns a hash value for X (which really is a cgraph_edge). */
674 hashval_t
675 cgraph_edge_hasher::hash (gimple *call_stmt)
677 /* This is a really poor hash function, but it is what htab_hash_pointer
678 uses. */
679 return (hashval_t) ((intptr_t)call_stmt >> 3);
682 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
684 inline bool
685 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
687 return x->call_stmt == y;
690 /* Add call graph edge E to call site hash of its caller. */
692 static inline void
693 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
695 gimple *call = e->call_stmt;
696 *e->caller->call_site_hash->find_slot_with_hash
697 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
700 /* Add call graph edge E to call site hash of its caller. */
702 static inline void
703 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
705 /* There are two speculative edges for every statement (one direct,
706 one indirect); always hash the direct one. */
707 if (e->speculative && e->indirect_unknown_callee)
708 return;
709 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
710 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
711 if (*slot)
713 gcc_assert (((cgraph_edge *)*slot)->speculative);
714 if (e->callee && (!e->prev_callee
715 || !e->prev_callee->speculative
716 || e->prev_callee->call_stmt != e->call_stmt))
717 *slot = e;
718 return;
720 gcc_assert (!*slot || e->speculative);
721 *slot = e;
724 /* Return the callgraph edge representing the GIMPLE_CALL statement
725 CALL_STMT. */
727 cgraph_edge *
728 cgraph_node::get_edge (gimple *call_stmt)
730 cgraph_edge *e, *e2;
731 int n = 0;
733 if (call_site_hash)
734 return call_site_hash->find_with_hash
735 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
737 /* This loop may turn out to be performance problem. In such case adding
738 hashtables into call nodes with very many edges is probably best
739 solution. It is not good idea to add pointer into CALL_EXPR itself
740 because we want to make possible having multiple cgraph nodes representing
741 different clones of the same body before the body is actually cloned. */
742 for (e = callees; e; e = e->next_callee)
744 if (e->call_stmt == call_stmt)
745 break;
746 n++;
749 if (!e)
750 for (e = indirect_calls; e; e = e->next_callee)
752 if (e->call_stmt == call_stmt)
753 break;
754 n++;
757 if (n > 100)
759 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
760 for (e2 = callees; e2; e2 = e2->next_callee)
761 cgraph_add_edge_to_call_site_hash (e2);
762 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
763 cgraph_add_edge_to_call_site_hash (e2);
766 return e;
770 /* Change field call_stmt of edge E to NEW_STMT. If UPDATE_SPECULATIVE and E
771 is any component of speculative edge, then update all components.
772 Speculations can be resolved in the process and EDGE can be removed and
773 deallocated. Return the edge that now represents the call. */
775 cgraph_edge *
776 cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
777 bool update_speculative)
779 tree decl;
781 /* Speculative edges has three component, update all of them
782 when asked to. */
783 if (update_speculative && e->speculative)
785 cgraph_edge *direct, *indirect, *next;
786 ipa_ref *ref;
787 bool e_indirect = e->indirect_unknown_callee;
788 int n = 0;
790 direct = e->first_speculative_call_target ();
791 indirect = e->speculative_call_indirect_edge ();
793 gcall *old_stmt = direct->call_stmt;
794 for (cgraph_edge *d = direct; d; d = next)
796 next = d->next_speculative_call_target ();
797 cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
798 gcc_assert (d2 == d);
799 n++;
801 gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
802 for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
803 if (ref->speculative && ref->stmt == old_stmt)
805 ref->stmt = new_stmt;
806 n--;
809 indirect = set_call_stmt (indirect, new_stmt, false);
810 return e_indirect ? indirect : direct;
813 /* Only direct speculative edges go to call_site_hash. */
814 if (e->caller->call_site_hash
815 && (!e->speculative || !e->indirect_unknown_callee)
816 /* It is possible that edge was previously speculative. In this case
817 we have different value in call stmt hash which needs preserving. */
818 && e->caller->get_edge (e->call_stmt) == e)
819 e->caller->call_site_hash->remove_elt_with_hash
820 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
822 e->call_stmt = new_stmt;
823 if (e->indirect_unknown_callee
824 && (decl = gimple_call_fndecl (new_stmt)))
826 /* Constant propagation (and possibly also inlining?) can turn an
827 indirect call into a direct one. */
828 cgraph_node *new_callee = cgraph_node::get (decl);
830 gcc_checking_assert (new_callee);
831 e = make_direct (e, new_callee);
834 function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
835 e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
836 /* Update call stite hash. For speculative calls we only record the first
837 direct edge. */
838 if (e->caller->call_site_hash
839 && (!e->speculative
840 || (e->callee
841 && (!e->prev_callee || !e->prev_callee->speculative
842 || e->prev_callee->call_stmt != e->call_stmt))
843 || (e->speculative && !e->callee)))
844 cgraph_add_edge_to_call_site_hash (e);
845 return e;
848 /* Allocate a cgraph_edge structure and fill it with data according to the
849 parameters of which only CALLEE can be NULL (when creating an indirect call
850 edge). CLONING_P should be set if properties that are copied from an
851 original edge should not be calculated. */
853 cgraph_edge *
854 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
855 gcall *call_stmt, profile_count count,
856 bool indir_unknown_callee, bool cloning_p)
858 cgraph_edge *edge;
860 /* LTO does not actually have access to the call_stmt since these
861 have not been loaded yet. */
862 if (call_stmt)
864 /* This is a rather expensive check possibly triggering
865 construction of call stmt hashtable. */
866 cgraph_edge *e;
867 gcc_checking_assert (!(e = caller->get_edge (call_stmt))
868 || e->speculative);
870 gcc_assert (is_gimple_call (call_stmt));
873 edge = ggc_alloc<cgraph_edge> ();
874 edge->m_summary_id = -1;
875 edges_count++;
877 gcc_assert (++edges_max_uid != 0);
878 edge->m_uid = edges_max_uid;
879 edge->aux = NULL;
880 edge->caller = caller;
881 edge->callee = callee;
882 edge->prev_caller = NULL;
883 edge->next_caller = NULL;
884 edge->prev_callee = NULL;
885 edge->next_callee = NULL;
886 edge->lto_stmt_uid = 0;
887 edge->speculative_id = 0;
889 edge->count = count;
890 edge->call_stmt = call_stmt;
891 edge->indirect_info = NULL;
892 edge->indirect_inlining_edge = 0;
893 edge->speculative = false;
894 edge->indirect_unknown_callee = indir_unknown_callee;
895 if (call_stmt && caller->call_site_hash)
896 cgraph_add_edge_to_call_site_hash (edge);
898 if (cloning_p)
899 return edge;
901 edge->can_throw_external
902 = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
903 call_stmt) : false;
904 edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
905 edge->call_stmt_cannot_inline_p = false;
907 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
908 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
909 edge->in_polymorphic_cdtor
910 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
911 caller->decl);
912 else
913 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
914 if (callee)
915 caller->calls_declare_variant_alt |= callee->declare_variant_alt;
917 if (callee && symtab->state != LTO_STREAMING
918 && edge->callee->comdat_local_p ())
919 edge->caller->calls_comdat_local = true;
921 return edge;
924 /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
925 be set if properties that are copied from an original edge should not be
926 calculated. */
928 cgraph_edge *
929 cgraph_node::create_edge (cgraph_node *callee,
930 gcall *call_stmt, profile_count count, bool cloning_p)
932 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
933 false, cloning_p);
935 if (!cloning_p)
936 initialize_inline_failed (edge);
938 edge->next_caller = callee->callers;
939 if (callee->callers)
940 callee->callers->prev_caller = edge;
941 edge->next_callee = callees;
942 if (callees)
943 callees->prev_callee = edge;
944 callees = edge;
945 callee->callers = edge;
947 return edge;
950 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
952 cgraph_indirect_call_info *
953 cgraph_allocate_init_indirect_info (void)
955 cgraph_indirect_call_info *ii;
957 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
958 ii->param_index = -1;
959 return ii;
962 /* Create an indirect edge with a yet-undetermined callee where the call
963 statement destination is a formal parameter of the caller with index
964 PARAM_INDEX. CLONING_P should be set if properties that are copied from an
965 original edge should not be calculated and indirect_info structure should
966 not be calculated. */
968 cgraph_edge *
969 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
970 profile_count count,
971 bool cloning_p)
973 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
974 cloning_p);
975 tree target;
977 if (!cloning_p)
978 initialize_inline_failed (edge);
980 edge->indirect_info = cgraph_allocate_init_indirect_info ();
981 edge->indirect_info->ecf_flags = ecf_flags;
982 edge->indirect_info->vptr_changed = true;
984 /* Record polymorphic call info. */
985 if (!cloning_p
986 && call_stmt
987 && (target = gimple_call_fn (call_stmt))
988 && virtual_method_call_p (target))
990 ipa_polymorphic_call_context context (decl, target, call_stmt);
992 /* Only record types can have virtual calls. */
993 edge->indirect_info->polymorphic = true;
994 edge->indirect_info->param_index = -1;
995 edge->indirect_info->otr_token
996 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
997 edge->indirect_info->otr_type = obj_type_ref_class (target);
998 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
999 edge->indirect_info->context = context;
1002 edge->next_callee = indirect_calls;
1003 if (indirect_calls)
1004 indirect_calls->prev_callee = edge;
1005 indirect_calls = edge;
1007 return edge;
1010 /* Remove the edge from the list of the callees of the caller. */
1012 void
1013 cgraph_edge::remove_caller (void)
1015 if (prev_callee)
1016 prev_callee->next_callee = next_callee;
1017 if (next_callee)
1018 next_callee->prev_callee = prev_callee;
1019 if (!prev_callee)
1021 if (indirect_unknown_callee)
1022 caller->indirect_calls = next_callee;
1023 else
1024 caller->callees = next_callee;
1026 if (caller->call_site_hash
1027 && this == caller->get_edge (call_stmt))
1028 caller->call_site_hash->remove_elt_with_hash
1029 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1032 /* Put the edge onto the free list. */
1034 void
1035 symbol_table::free_edge (cgraph_edge *e)
1037 edges_count--;
1038 if (e->m_summary_id != -1)
1039 edge_released_summary_ids.safe_push (e->m_summary_id);
1041 if (e->indirect_info)
1042 ggc_free (e->indirect_info);
1043 ggc_free (e);
1046 /* Remove the edge in the cgraph. */
1048 void
1049 cgraph_edge::remove (cgraph_edge *edge)
1051 /* Call all edge removal hooks. */
1052 symtab->call_edge_removal_hooks (edge);
1054 if (!edge->indirect_unknown_callee)
1055 /* Remove from callers list of the callee. */
1056 edge->remove_callee ();
1058 /* Remove from callees list of the callers. */
1059 edge->remove_caller ();
1061 /* Put the edge onto the free list. */
1062 symtab->free_edge (edge);
1065 /* Turn edge into speculative call calling N2. Update
1066 the profile so the direct call is taken COUNT times
1067 with FREQUENCY.
1069 At clone materialization time, the indirect call E will
1070 be expanded as:
1072 if (call_dest == N2)
1073 n2 ();
1074 else
1075 call call_dest
1077 At this time the function just creates the direct call,
1078 the reference representing the if conditional and attaches
1079 them all to the original indirect call statement.
1081 speculative_id is used to link direct calls with their corresponding
1082 IPA_REF_ADDR references when representing speculative calls.
1084 Return direct edge created. */
1086 cgraph_edge *
1087 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1088 unsigned int speculative_id)
1090 cgraph_node *n = caller;
1091 ipa_ref *ref = NULL;
1092 cgraph_edge *e2;
1094 if (dump_file)
1095 fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1096 n->dump_name (), n2->dump_name ());
1097 speculative = true;
1098 e2 = n->create_edge (n2, call_stmt, direct_count);
1099 initialize_inline_failed (e2);
1100 e2->speculative = true;
1101 if (TREE_NOTHROW (n2->decl))
1102 e2->can_throw_external = false;
1103 else
1104 e2->can_throw_external = can_throw_external;
1105 e2->lto_stmt_uid = lto_stmt_uid;
1106 e2->speculative_id = speculative_id;
1107 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1108 indirect_info->num_speculative_call_targets++;
1109 count -= e2->count;
1110 symtab->call_edge_duplication_hooks (this, e2);
1111 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1112 ref->lto_stmt_uid = lto_stmt_uid;
1113 ref->speculative_id = speculative_id;
1114 ref->speculative = speculative;
1115 n2->mark_address_taken ();
1116 return e2;
1119 /* Speculative call consists of an indirect edge and one or more
1120 direct edge+ref pairs.
1122 Given an edge which is part of speculative call, return the first
1123 direct call edge in the speculative call sequence. */
1125 cgraph_edge *
1126 cgraph_edge::first_speculative_call_target ()
1128 cgraph_edge *e = this;
1130 gcc_checking_assert (e->speculative);
1131 if (e->callee)
1133 while (e->prev_callee && e->prev_callee->speculative
1134 && e->prev_callee->call_stmt == e->call_stmt
1135 && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1136 e = e->prev_callee;
1137 return e;
1139 /* Call stmt site hash always points to the first target of the
1140 speculative call sequence. */
1141 if (e->call_stmt)
1142 return e->caller->get_edge (e->call_stmt);
1143 for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1144 if (e2->speculative
1145 && e->call_stmt == e2->call_stmt
1146 && e->lto_stmt_uid == e2->lto_stmt_uid)
1147 return e2;
1150 /* We always maintain first direct edge in the call site hash, if one
1151 exists. E is going to be removed. See if it is first one and update
1152 hash accordingly. INDIRECT is the indirect edge of speculative call.
1153 We assume that INDIRECT->num_speculative_call_targets_p () is already
1154 updated for removal of E. */
1155 static void
1156 update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1157 cgraph_edge *indirect)
1159 if (e->caller->call_site_hash)
1161 if (e->caller->get_edge (e->call_stmt) != e)
1163 else if (!indirect->num_speculative_call_targets_p ())
1164 cgraph_update_edge_in_call_site_hash (indirect);
1165 else
1167 gcc_checking_assert (e->next_callee && e->next_callee->speculative
1168 && e->next_callee->call_stmt == e->call_stmt);
1169 cgraph_update_edge_in_call_site_hash (e->next_callee);
1174 /* Speculative call EDGE turned out to be direct call to CALLEE_DECL. Remove
1175 the speculative call sequence and return edge representing the call, the
1176 original EDGE can be removed and deallocated. Return the edge that now
1177 represents the call.
1179 For "speculative" indirect call that contains multiple "speculative"
1180 targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1181 decrease the count and only remove current direct edge.
1183 If no speculative direct call left to the speculative indirect call, remove
1184 the speculative of both the indirect call and corresponding direct edge.
1186 It is up to caller to iteratively resolve each "speculative" direct call and
1187 redirect the call as appropriate. */
1189 cgraph_edge *
1190 cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1192 cgraph_edge *e2;
1193 ipa_ref *ref;
1195 gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1196 if (!edge->callee)
1197 e2 = edge->first_speculative_call_target ();
1198 else
1199 e2 = edge;
1200 ref = e2->speculative_call_target_ref ();
1201 edge = edge->speculative_call_indirect_edge ();
1202 if (!callee_decl
1203 || !ref->referred->semantically_equivalent_p
1204 (symtab_node::get (callee_decl)))
1206 if (dump_file)
1208 if (callee_decl)
1210 fprintf (dump_file, "Speculative indirect call %s => %s has "
1211 "turned out to have contradicting known target ",
1212 edge->caller->dump_name (),
1213 e2->callee->dump_name ());
1214 print_generic_expr (dump_file, callee_decl);
1215 fprintf (dump_file, "\n");
1217 else
1219 fprintf (dump_file, "Removing speculative call %s => %s\n",
1220 edge->caller->dump_name (),
1221 e2->callee->dump_name ());
1225 else
1227 cgraph_edge *tmp = edge;
1228 if (dump_file)
1229 fprintf (dump_file, "Speculative call turned into direct call.\n");
1230 edge = e2;
1231 e2 = tmp;
1232 /* FIXME: If EDGE is inlined, we should scale up the frequencies
1233 and counts in the functions inlined through it. */
1235 edge->count += e2->count;
1236 if (edge->num_speculative_call_targets_p ())
1238 /* The indirect edge has multiple speculative targets, don't remove
1239 speculative until all related direct edges are resolved. */
1240 edge->indirect_info->num_speculative_call_targets--;
1241 if (!edge->indirect_info->num_speculative_call_targets)
1242 edge->speculative = false;
1244 else
1245 edge->speculative = false;
1246 e2->speculative = false;
1247 update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1248 ref->remove_reference ();
1249 if (e2->indirect_unknown_callee || e2->inline_failed)
1250 remove (e2);
1251 else
1252 e2->callee->remove_symbol_and_inline_clones ();
1253 return edge;
1256 /* Return edge corresponding to speculative call to a given target.
1257 NULL if speculative call does not have one. */
1259 cgraph_edge *
1260 cgraph_edge::speculative_call_for_target (cgraph_node *target)
1262 for (cgraph_edge *direct = first_speculative_call_target ();
1263 direct;
1264 direct = direct->next_speculative_call_target ())
1265 if (direct->speculative_call_target_ref ()
1266 ->referred->semantically_equivalent_p (target))
1267 return direct;
1268 return NULL;
1271 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1272 CALLEE. Speculations can be resolved in the process and EDGE can be removed
1273 and deallocated. Return the edge that now represents the call. */
1275 cgraph_edge *
1276 cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1278 gcc_assert (edge->indirect_unknown_callee);
1280 /* If we are redirecting speculative call, make it non-speculative. */
1281 if (edge->speculative)
1283 cgraph_edge *found = NULL;
1284 cgraph_edge *direct, *next;
1286 edge = edge->speculative_call_indirect_edge ();
1288 /* Look all speculative targets and remove all but one corresponding
1289 to callee (if it exists). */
1290 for (direct = edge->first_speculative_call_target ();
1291 direct;
1292 direct = next)
1294 next = direct->next_speculative_call_target ();
1296 /* Compare ref not direct->callee. Direct edge is possibly
1297 inlined or redirected. */
1298 if (!direct->speculative_call_target_ref ()
1299 ->referred->semantically_equivalent_p (callee))
1300 edge = direct->resolve_speculation (direct, NULL);
1301 else
1303 gcc_checking_assert (!found);
1304 found = direct;
1308 /* On successful speculation just remove the indirect edge and
1309 return the pre existing direct edge.
1310 It is important to not remove it and redirect because the direct
1311 edge may be inlined or redirected. */
1312 if (found)
1314 cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1315 gcc_checking_assert (!found->speculative && e2 == found);
1316 return found;
1318 gcc_checking_assert (!edge->speculative);
1321 edge->indirect_unknown_callee = 0;
1322 ggc_free (edge->indirect_info);
1323 edge->indirect_info = NULL;
1325 /* Get the edge out of the indirect edge list. */
1326 if (edge->prev_callee)
1327 edge->prev_callee->next_callee = edge->next_callee;
1328 if (edge->next_callee)
1329 edge->next_callee->prev_callee = edge->prev_callee;
1330 if (!edge->prev_callee)
1331 edge->caller->indirect_calls = edge->next_callee;
1333 /* Put it into the normal callee list */
1334 edge->prev_callee = NULL;
1335 edge->next_callee = edge->caller->callees;
1336 if (edge->caller->callees)
1337 edge->caller->callees->prev_callee = edge;
1338 edge->caller->callees = edge;
1340 /* Insert to callers list of the new callee. */
1341 edge->set_callee (callee);
1343 /* We need to re-determine the inlining status of the edge. */
1344 initialize_inline_failed (edge);
1345 return edge;
1348 /* Redirect callee of the edge to N. The function does not update underlying
1349 call expression. */
1351 void
1352 cgraph_edge::redirect_callee (cgraph_node *n)
1354 bool loc = callee->comdat_local_p ();
1355 /* Remove from callers list of the current callee. */
1356 remove_callee ();
1358 /* Insert to callers list of the new callee. */
1359 set_callee (n);
1361 if (!inline_failed)
1362 return;
1363 if (!loc && n->comdat_local_p ())
1365 cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1366 to->calls_comdat_local = true;
1368 else if (loc && !n->comdat_local_p ())
1370 cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1371 gcc_checking_assert (to->calls_comdat_local);
1372 to->calls_comdat_local = to->check_calls_comdat_local_p ();
1376 /* If necessary, change the function declaration in the call statement
1377 associated with E so that it corresponds to the edge callee. Speculations
1378 can be resolved in the process and EDGE can be removed and deallocated.
1380 The edge could be one of speculative direct call generated from speculative
1381 indirect call. In this circumstance, decrease the speculative targets
1382 count (i.e. num_speculative_call_targets) and redirect call stmt to the
1383 corresponding i-th target. If no speculative direct call left to the
1384 speculative indirect call, remove "speculative" of the indirect call and
1385 also redirect stmt to it's final direct target.
1387 It is up to caller to iteratively transform each "speculative"
1388 direct call as appropriate. */
1390 gimple *
1391 cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e)
1393 tree decl = gimple_call_fndecl (e->call_stmt);
1394 gcall *new_stmt;
1395 gimple_stmt_iterator gsi;
1397 if (e->speculative)
1399 /* If there already is an direct call (i.e. as a result of inliner's
1400 substitution), forget about speculating. */
1401 if (decl)
1402 e = make_direct (e->speculative_call_indirect_edge (),
1403 cgraph_node::get (decl));
1404 else
1406 /* Be sure we redirect all speculative targets before poking
1407 abou tindirect edge. */
1408 gcc_checking_assert (e->callee);
1409 cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1410 gcall *new_stmt;
1411 ipa_ref *ref;
1413 /* Expand speculation into GIMPLE code. */
1414 if (dump_file)
1416 fprintf (dump_file,
1417 "Expanding speculative call of %s -> %s count: ",
1418 e->caller->dump_name (),
1419 e->callee->dump_name ());
1420 e->count.dump (dump_file);
1421 fprintf (dump_file, "\n");
1423 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1425 profile_count all = indirect->count;
1426 for (cgraph_edge *e2 = e->first_speculative_call_target ();
1428 e2 = e2->next_speculative_call_target ())
1429 all = all + e2->count;
1430 profile_probability prob = e->count.probability_in (all);
1431 if (!prob.initialized_p ())
1432 prob = profile_probability::even ();
1433 ref = e->speculative_call_target_ref ();
1434 new_stmt = gimple_ic (e->call_stmt,
1435 dyn_cast<cgraph_node *> (ref->referred),
1436 prob);
1437 e->speculative = false;
1438 if (indirect->num_speculative_call_targets_p ())
1440 /* The indirect edge has multiple speculative targets, don't
1441 remove speculative until all related direct edges are
1442 redirected. */
1443 indirect->indirect_info->num_speculative_call_targets--;
1444 if (!indirect->indirect_info->num_speculative_call_targets)
1445 indirect->speculative = false;
1447 else
1448 indirect->speculative = false;
1449 /* Indirect edges are not both in the call site hash.
1450 get it updated. */
1451 update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1452 cgraph_edge::set_call_stmt (e, new_stmt, false);
1453 e->count = gimple_bb (e->call_stmt)->count;
1455 /* Once we are done with expanding the sequence, update also indirect
1456 call probability. Until then the basic block accounts for the
1457 sum of indirect edge and all non-expanded speculations. */
1458 if (!indirect->speculative)
1459 indirect->count = gimple_bb (indirect->call_stmt)->count;
1460 ref->speculative = false;
1461 ref->stmt = NULL;
1462 pop_cfun ();
1463 /* Continue redirecting E to proper target. */
1468 if (e->indirect_unknown_callee
1469 || decl == e->callee->decl)
1470 return e->call_stmt;
1472 if (decl && ipa_saved_clone_sources)
1474 tree *p = ipa_saved_clone_sources->get (e->callee);
1475 if (p && decl == *p)
1477 gimple_call_set_fndecl (e->call_stmt, e->callee->decl);
1478 return e->call_stmt;
1482 if (flag_checking && decl)
1484 cgraph_node *node = cgraph_node::get (decl);
1485 gcc_assert (!node || !node->clone.param_adjustments);
1488 if (symtab->dump_file)
1490 fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1491 e->caller->dump_name (), e->callee->dump_name ());
1492 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1493 if (e->callee->clone.param_adjustments)
1494 e->callee->clone.param_adjustments->dump (symtab->dump_file);
1495 unsigned performed_len
1496 = vec_safe_length (e->caller->clone.performed_splits);
1497 if (performed_len > 0)
1498 fprintf (symtab->dump_file, "Performed splits records:\n");
1499 for (unsigned i = 0; i < performed_len; i++)
1501 ipa_param_performed_split *sm
1502 = &(*e->caller->clone.performed_splits)[i];
1503 print_node_brief (symtab->dump_file, " dummy_decl: ", sm->dummy_decl,
1504 TDF_UID);
1505 fprintf (symtab->dump_file, ", unit_offset: %u\n", sm->unit_offset);
1509 if (ipa_param_adjustments *padjs = e->callee->clone.param_adjustments)
1511 /* We need to defer cleaning EH info on the new statement to
1512 fixup-cfg. We may not have dominator information at this point
1513 and thus would end up with unreachable blocks and have no way
1514 to communicate that we need to run CFG cleanup then. */
1515 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1516 if (lp_nr != 0)
1517 remove_stmt_from_eh_lp (e->call_stmt);
1519 tree old_fntype = gimple_call_fntype (e->call_stmt);
1520 new_stmt = padjs->modify_call (e->call_stmt,
1521 e->caller->clone.performed_splits,
1522 e->callee->decl, false);
1523 cgraph_node *origin = e->callee;
1524 while (origin->clone_of)
1525 origin = origin->clone_of;
1527 if ((origin->former_clone_of
1528 && old_fntype == TREE_TYPE (origin->former_clone_of))
1529 || old_fntype == TREE_TYPE (origin->decl))
1530 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1531 else
1533 tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1534 gimple_call_set_fntype (new_stmt, new_fntype);
1537 if (lp_nr != 0)
1538 add_stmt_to_eh_lp (new_stmt, lp_nr);
1540 else
1542 new_stmt = e->call_stmt;
1543 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1544 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1547 /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1548 adjust gimple_call_fntype too. */
1549 if (gimple_call_noreturn_p (new_stmt)
1550 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1551 && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1552 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1553 == void_type_node))
1554 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1556 /* If the call becomes noreturn, remove the LHS if possible. */
1557 tree lhs = gimple_call_lhs (new_stmt);
1558 if (lhs
1559 && gimple_call_noreturn_p (new_stmt)
1560 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1561 || should_remove_lhs_p (lhs)))
1563 if (TREE_CODE (lhs) == SSA_NAME)
1565 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1566 TREE_TYPE (lhs), NULL);
1567 var = get_or_create_ssa_default_def
1568 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1569 gimple *set_stmt = gimple_build_assign (lhs, var);
1570 gsi = gsi_for_stmt (new_stmt);
1571 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1572 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1574 gimple_call_set_lhs (new_stmt, NULL_TREE);
1575 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1578 /* If new callee has no static chain, remove it. */
1579 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1581 gimple_call_set_chain (new_stmt, NULL);
1582 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1585 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1586 new_stmt);
1588 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1590 if (symtab->dump_file)
1592 fprintf (symtab->dump_file, " updated to:");
1593 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1595 return new_stmt;
1598 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1599 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1600 of OLD_STMT if it was previously call statement.
1601 If NEW_STMT is NULL, the call has been dropped without any
1602 replacement. */
1604 static void
1605 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1606 gimple *old_stmt, tree old_call,
1607 gimple *new_stmt)
1609 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1610 ? gimple_call_fndecl (new_stmt) : 0;
1612 /* We are seeing indirect calls, then there is nothing to update. */
1613 if (!new_call && !old_call)
1614 return;
1615 /* See if we turned indirect call into direct call or folded call to one builtin
1616 into different builtin. */
1617 if (old_call != new_call)
1619 cgraph_edge *e = node->get_edge (old_stmt);
1620 cgraph_edge *ne = NULL;
1621 profile_count count;
1623 if (e)
1625 /* Keep calls marked as dead dead. */
1626 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1627 && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
1629 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1630 as_a <gcall *> (new_stmt));
1631 return;
1633 /* See if the edge is already there and has the correct callee. It
1634 might be so because of indirect inlining has already updated
1635 it. We also might've cloned and redirected the edge. */
1636 if (new_call && e->callee)
1638 cgraph_node *callee = e->callee;
1639 while (callee)
1641 if (callee->decl == new_call
1642 || callee->former_clone_of == new_call)
1644 cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
1645 return;
1647 callee = callee->clone_of;
1651 /* Otherwise remove edge and create new one; we can't simply redirect
1652 since function has changed, so inline plan and other information
1653 attached to edge is invalid. */
1654 count = e->count;
1655 if (e->indirect_unknown_callee || e->inline_failed)
1656 cgraph_edge::remove (e);
1657 else
1658 e->callee->remove_symbol_and_inline_clones ();
1660 else if (new_call)
1662 /* We are seeing new direct call; compute profile info based on BB. */
1663 basic_block bb = gimple_bb (new_stmt);
1664 count = bb->count;
1667 if (new_call)
1669 ne = node->create_edge (cgraph_node::get_create (new_call),
1670 as_a <gcall *> (new_stmt), count);
1671 gcc_assert (ne->inline_failed);
1674 /* We only updated the call stmt; update pointer in cgraph edge.. */
1675 else if (old_stmt != new_stmt)
1676 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1677 as_a <gcall *> (new_stmt));
1680 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1681 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1682 of OLD_STMT before it was updated (updating can happen inplace). */
1684 void
1685 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1686 gimple *new_stmt)
1688 cgraph_node *orig = cgraph_node::get (cfun->decl);
1689 cgraph_node *node;
1691 gcc_checking_assert (orig);
1692 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1693 if (orig->clones)
1694 for (node = orig->clones; node != orig;)
1696 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1697 if (node->clones)
1698 node = node->clones;
1699 else if (node->next_sibling_clone)
1700 node = node->next_sibling_clone;
1701 else
1703 while (node != orig && !node->next_sibling_clone)
1704 node = node->clone_of;
1705 if (node != orig)
1706 node = node->next_sibling_clone;
1712 /* Remove all callees from the node. */
1714 void
1715 cgraph_node::remove_callees (void)
1717 cgraph_edge *e, *f;
1719 calls_comdat_local = false;
1721 /* It is sufficient to remove the edges from the lists of callers of
1722 the callees. The callee list of the node can be zapped with one
1723 assignment. */
1724 for (e = callees; e; e = f)
1726 f = e->next_callee;
1727 symtab->call_edge_removal_hooks (e);
1728 if (!e->indirect_unknown_callee)
1729 e->remove_callee ();
1730 symtab->free_edge (e);
1732 for (e = indirect_calls; e; e = f)
1734 f = e->next_callee;
1735 symtab->call_edge_removal_hooks (e);
1736 if (!e->indirect_unknown_callee)
1737 e->remove_callee ();
1738 symtab->free_edge (e);
1740 indirect_calls = NULL;
1741 callees = NULL;
1742 if (call_site_hash)
1744 call_site_hash->empty ();
1745 call_site_hash = NULL;
1749 /* Remove all callers from the node. */
1751 void
1752 cgraph_node::remove_callers (void)
1754 cgraph_edge *e, *f;
1756 /* It is sufficient to remove the edges from the lists of callees of
1757 the callers. The caller list of the node can be zapped with one
1758 assignment. */
1759 for (e = callers; e; e = f)
1761 f = e->next_caller;
1762 symtab->call_edge_removal_hooks (e);
1763 e->remove_caller ();
1764 symtab->free_edge (e);
1766 callers = NULL;
1769 /* Helper function for cgraph_release_function_body and free_lang_data.
1770 It releases body from function DECL without having to inspect its
1771 possibly non-existent symtab node. */
1773 void
1774 release_function_body (tree decl)
1776 function *fn = DECL_STRUCT_FUNCTION (decl);
1777 if (fn)
1779 if (fn->cfg
1780 && loops_for_fn (fn))
1782 fn->curr_properties &= ~PROP_loops;
1783 loop_optimizer_finalize (fn);
1785 if (fn->gimple_df)
1787 delete_tree_ssa (fn);
1788 fn->eh = NULL;
1790 if (fn->cfg)
1792 gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1793 gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1794 delete_tree_cfg_annotations (fn);
1795 clear_edges (fn);
1796 fn->cfg = NULL;
1798 if (fn->value_histograms)
1799 free_histograms (fn);
1800 gimple_set_body (decl, NULL);
1801 /* Struct function hangs a lot of data that would leak if we didn't
1802 removed all pointers to it. */
1803 ggc_free (fn);
1804 DECL_STRUCT_FUNCTION (decl) = NULL;
1806 DECL_SAVED_TREE (decl) = NULL;
1809 /* Release memory used to represent body of function.
1810 Use this only for functions that are released before being translated to
1811 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1812 are free'd in final.c via free_after_compilation().
1813 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1815 void
1816 cgraph_node::release_body (bool keep_arguments)
1818 ipa_transforms_to_apply.release ();
1819 if (!used_as_abstract_origin && symtab->state != PARSING)
1821 DECL_RESULT (decl) = NULL;
1823 if (!keep_arguments)
1824 DECL_ARGUMENTS (decl) = NULL;
1826 /* If the node is abstract and needed, then do not clear
1827 DECL_INITIAL of its associated function declaration because it's
1828 needed to emit debug info later. */
1829 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1830 DECL_INITIAL (decl) = error_mark_node;
1831 release_function_body (decl);
1832 if (lto_file_data)
1834 lto_free_function_in_decl_state_for_node (this);
1835 lto_file_data = NULL;
1839 /* Remove function from symbol table. */
1841 void
1842 cgraph_node::remove (void)
1844 if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1845 fprintf (symtab->ipa_clones_dump_file,
1846 "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1847 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1848 DECL_SOURCE_COLUMN (decl));
1850 symtab->call_cgraph_removal_hooks (this);
1851 remove_callers ();
1852 remove_callees ();
1853 ipa_transforms_to_apply.release ();
1854 delete_function_version (function_version ());
1856 /* Incremental inlining access removed nodes stored in the postorder list.
1858 force_output = false;
1859 forced_by_abi = false;
1861 unregister ();
1862 if (prev_sibling_clone)
1863 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1864 else if (clone_of)
1865 clone_of->clones = next_sibling_clone;
1866 if (next_sibling_clone)
1867 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1868 if (clones)
1870 cgraph_node *n, *next;
1872 if (clone_of)
1874 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1875 n->clone_of = clone_of;
1876 n->clone_of = clone_of;
1877 n->next_sibling_clone = clone_of->clones;
1878 if (clone_of->clones)
1879 clone_of->clones->prev_sibling_clone = n;
1880 clone_of->clones = clones;
1882 else
1884 /* We are removing node with clones. This makes clones inconsistent,
1885 but assume they will be removed subsequently and just keep clone
1886 tree intact. This can happen in unreachable function removal since
1887 we remove unreachable functions in random order, not by bottom-up
1888 walk of clone trees. */
1889 for (n = clones; n; n = next)
1891 next = n->next_sibling_clone;
1892 n->next_sibling_clone = NULL;
1893 n->prev_sibling_clone = NULL;
1894 n->clone_of = NULL;
1899 /* While all the clones are removed after being proceeded, the function
1900 itself is kept in the cgraph even after it is compiled. Check whether
1901 we are done with this body and reclaim it proactively if this is the case.
1903 if (symtab->state != LTO_STREAMING)
1905 cgraph_node *n = cgraph_node::get (decl);
1906 if (!n
1907 || (!n->clones && !n->clone_of && !n->inlined_to
1908 && ((symtab->global_info_ready || in_lto_p)
1909 && (TREE_ASM_WRITTEN (n->decl)
1910 || DECL_EXTERNAL (n->decl)
1911 || !n->analyzed
1912 || (!flag_wpa && n->in_other_partition)))))
1913 release_body ();
1915 else
1917 lto_free_function_in_decl_state_for_node (this);
1918 lto_file_data = NULL;
1921 decl = NULL;
1922 if (call_site_hash)
1924 call_site_hash->empty ();
1925 call_site_hash = NULL;
1928 symtab->release_symbol (this);
1931 /* Likewise indicate that a node is having address taken. */
1933 void
1934 cgraph_node::mark_address_taken (void)
1936 /* Indirect inlining can figure out that all uses of the address are
1937 inlined. */
1938 if (inlined_to)
1940 gcc_assert (cfun->after_inlining);
1941 gcc_assert (callers->indirect_inlining_edge);
1942 return;
1944 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1945 IPA_REF_ADDR reference exists (and thus it should be set on node
1946 representing alias we take address of) and as a test whether address
1947 of the object was taken (and thus it should be set on node alias is
1948 referring to). We should remove the first use and the remove the
1949 following set. */
1950 address_taken = 1;
1951 cgraph_node *node = ultimate_alias_target ();
1952 node->address_taken = 1;
1955 /* Return local info node for the compiled function. */
1957 cgraph_node *
1958 cgraph_node::local_info_node (tree decl)
1960 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1961 cgraph_node *node = get (decl);
1962 if (!node)
1963 return NULL;
1964 return node->ultimate_alias_target ();
1967 /* Return RTL info for the compiled function. */
1969 cgraph_rtl_info *
1970 cgraph_node::rtl_info (const_tree decl)
1972 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1973 cgraph_node *node = get (decl);
1974 if (!node)
1975 return NULL;
1976 enum availability avail;
1977 node = node->ultimate_alias_target (&avail);
1978 if (decl != current_function_decl
1979 && (avail < AVAIL_AVAILABLE
1980 || (node->decl != current_function_decl
1981 && !TREE_ASM_WRITTEN (node->decl))))
1982 return NULL;
1983 /* Allocate if it doesn't exist. */
1984 if (node->rtl == NULL)
1986 node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
1987 SET_HARD_REG_SET (node->rtl->function_used_regs);
1989 return node->rtl;
1992 /* Return a string describing the failure REASON. */
1994 const char*
1995 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1997 #undef DEFCIFCODE
1998 #define DEFCIFCODE(code, type, string) string,
2000 static const char *cif_string_table[CIF_N_REASONS] = {
2001 #include "cif-code.def"
2004 /* Signedness of an enum type is implementation defined, so cast it
2005 to unsigned before testing. */
2006 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2007 return cif_string_table[reason];
2010 /* Return a type describing the failure REASON. */
2012 cgraph_inline_failed_type_t
2013 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2015 #undef DEFCIFCODE
2016 #define DEFCIFCODE(code, type, string) type,
2018 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2019 #include "cif-code.def"
2022 /* Signedness of an enum type is implementation defined, so cast it
2023 to unsigned before testing. */
2024 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2025 return cif_type_table[reason];
2028 /* Names used to print out the availability enum. */
2029 const char * const cgraph_availability_names[] =
2030 {"unset", "not_available", "overwritable", "available", "local"};
2032 /* Output flags of edge to a file F. */
2034 void
2035 cgraph_edge::dump_edge_flags (FILE *f)
2037 if (speculative)
2038 fprintf (f, "(speculative) ");
2039 if (!inline_failed)
2040 fprintf (f, "(inlined) ");
2041 if (call_stmt_cannot_inline_p)
2042 fprintf (f, "(call_stmt_cannot_inline_p) ");
2043 if (indirect_inlining_edge)
2044 fprintf (f, "(indirect_inlining) ");
2045 if (count.initialized_p ())
2047 fprintf (f, "(");
2048 count.dump (f);
2049 fprintf (f, ",");
2050 fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2052 if (can_throw_external)
2053 fprintf (f, "(can throw external) ");
2056 /* Dump edge to stderr. */
2058 void
2059 cgraph_edge::debug (void)
2061 fprintf (stderr, "%s -> %s ", caller->dump_asm_name (),
2062 callee == NULL ? "(null)" : callee->dump_asm_name ());
2063 dump_edge_flags (stderr);
2064 fprintf (stderr, "\n\n");
2065 caller->debug ();
2066 if (callee != NULL)
2067 callee->debug ();
2070 /* Dump call graph node to file F. */
2072 void
2073 cgraph_node::dump (FILE *f)
2075 cgraph_edge *edge;
2077 dump_base (f);
2079 if (inlined_to)
2080 fprintf (f, " Function %s is inline copy in %s\n",
2081 dump_name (),
2082 inlined_to->dump_name ());
2083 if (clone_of)
2084 fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2085 if (symtab->function_flags_ready)
2086 fprintf (f, " Availability: %s\n",
2087 cgraph_availability_names [get_availability ()]);
2089 if (profile_id)
2090 fprintf (f, " Profile id: %i\n",
2091 profile_id);
2092 if (unit_id)
2093 fprintf (f, " Unit id: %i\n",
2094 unit_id);
2095 cgraph_function_version_info *vi = function_version ();
2096 if (vi != NULL)
2098 fprintf (f, " Version info: ");
2099 if (vi->prev != NULL)
2101 fprintf (f, "prev: ");
2102 fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2104 if (vi->next != NULL)
2106 fprintf (f, "next: ");
2107 fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2109 if (vi->dispatcher_resolver != NULL_TREE)
2110 fprintf (f, "dispatcher: %s",
2111 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2113 fprintf (f, "\n");
2115 fprintf (f, " Function flags:");
2116 if (count.initialized_p ())
2118 fprintf (f, " count:");
2119 count.dump (f);
2121 if (tp_first_run > 0)
2122 fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2123 if (cgraph_node *origin = nested_function_origin (this))
2124 fprintf (f, " nested in:%s", origin->dump_asm_name ());
2125 if (gimple_has_body_p (decl))
2126 fprintf (f, " body");
2127 if (process)
2128 fprintf (f, " process");
2129 if (local)
2130 fprintf (f, " local");
2131 if (redefined_extern_inline)
2132 fprintf (f, " redefined_extern_inline");
2133 if (only_called_at_startup)
2134 fprintf (f, " only_called_at_startup");
2135 if (only_called_at_exit)
2136 fprintf (f, " only_called_at_exit");
2137 if (tm_clone)
2138 fprintf (f, " tm_clone");
2139 if (calls_comdat_local)
2140 fprintf (f, " calls_comdat_local");
2141 if (icf_merged)
2142 fprintf (f, " icf_merged");
2143 if (merged_comdat)
2144 fprintf (f, " merged_comdat");
2145 if (merged_extern_inline)
2146 fprintf (f, " merged_extern_inline");
2147 if (split_part)
2148 fprintf (f, " split_part");
2149 if (indirect_call_target)
2150 fprintf (f, " indirect_call_target");
2151 if (nonfreeing_fn)
2152 fprintf (f, " nonfreeing_fn");
2153 if (DECL_STATIC_CONSTRUCTOR (decl))
2154 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2155 if (DECL_STATIC_DESTRUCTOR (decl))
2156 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2157 if (frequency == NODE_FREQUENCY_HOT)
2158 fprintf (f, " hot");
2159 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2160 fprintf (f, " unlikely_executed");
2161 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2162 fprintf (f, " executed_once");
2163 if (opt_for_fn (decl, optimize_size))
2164 fprintf (f, " optimize_size");
2165 if (parallelized_function)
2166 fprintf (f, " parallelized_function");
2167 if (DECL_IS_OPERATOR_NEW_P (decl))
2168 fprintf (f, " %soperator_new",
2169 DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2170 if (DECL_IS_OPERATOR_DELETE_P (decl))
2171 fprintf (f, " %soperator_delete",
2172 DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2174 fprintf (f, "\n");
2176 if (thunk.thunk_p)
2178 fprintf (f, " Thunk");
2179 if (thunk.alias)
2180 fprintf (f, " of %s (asm:%s)",
2181 lang_hooks.decl_printable_name (thunk.alias, 2),
2182 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2183 fprintf (f, " fixed offset %i virtual value %i indirect_offset %i "
2184 "has virtual offset %i\n",
2185 (int)thunk.fixed_offset,
2186 (int)thunk.virtual_value,
2187 (int)thunk.indirect_offset,
2188 (int)thunk.virtual_offset_p);
2190 else if (former_thunk_p ())
2191 fprintf (f, " Former thunk fixed offset %i virtual value %i "
2192 "indirect_offset %i has virtual offset %i\n",
2193 (int)thunk.fixed_offset,
2194 (int)thunk.virtual_value,
2195 (int)thunk.indirect_offset,
2196 (int)thunk.virtual_offset_p);
2197 if (alias && thunk.alias
2198 && DECL_P (thunk.alias))
2200 fprintf (f, " Alias of %s",
2201 lang_hooks.decl_printable_name (thunk.alias, 2));
2202 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2203 fprintf (f, " (asm:%s)",
2204 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2205 fprintf (f, "\n");
2208 fprintf (f, " Called by: ");
2210 profile_count sum = profile_count::zero ();
2211 for (edge = callers; edge; edge = edge->next_caller)
2213 fprintf (f, "%s ", edge->caller->dump_asm_name ());
2214 edge->dump_edge_flags (f);
2215 if (edge->count.initialized_p ())
2216 sum += edge->count.ipa ();
2219 fprintf (f, "\n Calls: ");
2220 for (edge = callees; edge; edge = edge->next_callee)
2222 fprintf (f, "%s ", edge->callee->dump_asm_name ());
2223 edge->dump_edge_flags (f);
2225 fprintf (f, "\n");
2227 if (count.ipa ().initialized_p ())
2229 bool ok = true;
2230 bool min = false;
2231 ipa_ref *ref;
2233 FOR_EACH_ALIAS (this, ref)
2234 if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2235 sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2237 if (inlined_to
2238 || (symtab->state < EXPANSION
2239 && ultimate_alias_target () == this && only_called_directly_p ()))
2240 ok = !count.ipa ().differs_from_p (sum);
2241 else if (count.ipa () > profile_count::from_gcov_type (100)
2242 && count.ipa () < sum.apply_scale (99, 100))
2243 ok = false, min = true;
2244 if (!ok)
2246 fprintf (f, " Invalid sum of caller counts ");
2247 sum.dump (f);
2248 if (min)
2249 fprintf (f, ", should be at most ");
2250 else
2251 fprintf (f, ", should be ");
2252 count.ipa ().dump (f);
2253 fprintf (f, "\n");
2257 for (edge = indirect_calls; edge; edge = edge->next_callee)
2259 if (edge->indirect_info->polymorphic)
2261 fprintf (f, " Polymorphic indirect call of type ");
2262 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2263 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2265 else
2266 fprintf (f, " Indirect call");
2267 edge->dump_edge_flags (f);
2268 if (edge->indirect_info->param_index != -1)
2270 fprintf (f, "of param:%i ", edge->indirect_info->param_index);
2271 if (edge->indirect_info->agg_contents)
2272 fprintf (f, "loaded from %s %s at offset %i ",
2273 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2274 edge->indirect_info->by_ref ? "passed by reference":"",
2275 (int)edge->indirect_info->offset);
2276 if (edge->indirect_info->vptr_changed)
2277 fprintf (f, "(vptr maybe changed) ");
2279 fprintf (f, "num speculative call targets: %i\n",
2280 edge->indirect_info->num_speculative_call_targets);
2281 if (edge->indirect_info->polymorphic)
2282 edge->indirect_info->context.dump (f);
2286 /* Dump call graph node to file F in graphviz format. */
2288 void
2289 cgraph_node::dump_graphviz (FILE *f)
2291 cgraph_edge *edge;
2293 for (edge = callees; edge; edge = edge->next_callee)
2295 cgraph_node *callee = edge->callee;
2297 fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2302 /* Dump call graph node NODE to stderr. */
2304 DEBUG_FUNCTION void
2305 cgraph_node::debug (void)
2307 dump (stderr);
2310 /* Dump the callgraph to file F. */
2312 void
2313 cgraph_node::dump_cgraph (FILE *f)
2315 cgraph_node *node;
2317 fprintf (f, "callgraph:\n\n");
2318 FOR_EACH_FUNCTION (node)
2319 node->dump (f);
2322 /* Return true when the DECL can possibly be inlined. */
2324 bool
2325 cgraph_function_possibly_inlined_p (tree decl)
2327 if (!symtab->global_info_ready)
2328 return !DECL_UNINLINABLE (decl);
2329 return DECL_POSSIBLY_INLINED (decl);
2332 /* Return function availability. See cgraph.h for description of individual
2333 return values. */
2334 enum availability
2335 cgraph_node::get_availability (symtab_node *ref)
2337 if (ref)
2339 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2340 if (cref)
2341 ref = cref->inlined_to;
2343 enum availability avail;
2344 if (!analyzed && !in_other_partition)
2345 avail = AVAIL_NOT_AVAILABLE;
2346 else if (local)
2347 avail = AVAIL_LOCAL;
2348 else if (inlined_to)
2349 avail = AVAIL_AVAILABLE;
2350 else if (transparent_alias)
2351 ultimate_alias_target (&avail, ref);
2352 else if (ifunc_resolver
2353 || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2354 avail = AVAIL_INTERPOSABLE;
2355 else if (!externally_visible)
2356 avail = AVAIL_AVAILABLE;
2357 /* If this is a reference from symbol itself and there are no aliases, we
2358 may be sure that the symbol was not interposed by something else because
2359 the symbol itself would be unreachable otherwise.
2361 Also comdat groups are always resolved in groups. */
2362 else if ((this == ref && !has_aliases_p ())
2363 || (ref && get_comdat_group ()
2364 && get_comdat_group () == ref->get_comdat_group ()))
2365 avail = AVAIL_AVAILABLE;
2366 /* Inline functions are safe to be analyzed even if their symbol can
2367 be overwritten at runtime. It is not meaningful to enforce any sane
2368 behavior on replacing inline function by different body. */
2369 else if (DECL_DECLARED_INLINE_P (decl))
2370 avail = AVAIL_AVAILABLE;
2372 /* If the function can be overwritten, return OVERWRITABLE. Take
2373 care at least of two notable extensions - the COMDAT functions
2374 used to share template instantiations in C++ (this is symmetric
2375 to code cp_cannot_inline_tree_fn and probably shall be shared and
2376 the inlinability hooks completely eliminated). */
2378 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2379 avail = AVAIL_INTERPOSABLE;
2380 else avail = AVAIL_AVAILABLE;
2382 return avail;
2385 /* Worker for cgraph_node_can_be_local_p. */
2386 static bool
2387 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2389 return !(!node->force_output
2390 && !node->ifunc_resolver
2391 /* Limitation of gas requires us to output targets of symver aliases
2392 as global symbols. This is binutils PR 25295. */
2393 && !node->symver
2394 && ((DECL_COMDAT (node->decl)
2395 && !node->forced_by_abi
2396 && !node->used_from_object_file_p ()
2397 && !node->same_comdat_group)
2398 || !node->externally_visible));
2401 /* Return true if cgraph_node can be made local for API change.
2402 Extern inline functions and C++ COMDAT functions can be made local
2403 at the expense of possible code size growth if function is used in multiple
2404 compilation units. */
2405 bool
2406 cgraph_node::can_be_local_p (void)
2408 return (!address_taken
2409 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2410 NULL, true));
2413 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2414 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2415 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2416 skipped. */
2417 bool
2418 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2419 (cgraph_node *, void *),
2420 void *data,
2421 bool include_overwritable,
2422 bool exclude_virtual_thunks)
2424 cgraph_edge *e;
2425 ipa_ref *ref;
2426 enum availability avail = AVAIL_AVAILABLE;
2428 if (include_overwritable
2429 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2431 if (callback (this, data))
2432 return true;
2434 FOR_EACH_ALIAS (this, ref)
2436 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2437 if (include_overwritable
2438 || alias->get_availability () > AVAIL_INTERPOSABLE)
2439 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2440 include_overwritable,
2441 exclude_virtual_thunks))
2442 return true;
2444 if (avail <= AVAIL_INTERPOSABLE)
2445 return false;
2446 for (e = callers; e; e = e->next_caller)
2447 if (e->caller->thunk.thunk_p
2448 && (include_overwritable
2449 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2450 && !(exclude_virtual_thunks
2451 && e->caller->thunk.virtual_offset_p))
2452 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2453 include_overwritable,
2454 exclude_virtual_thunks))
2455 return true;
2457 return false;
2460 /* Worker to bring NODE local. */
2462 bool
2463 cgraph_node::make_local (cgraph_node *node, void *)
2465 gcc_checking_assert (node->can_be_local_p ());
2466 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2468 node->make_decl_local ();
2469 node->set_section (NULL);
2470 node->set_comdat_group (NULL);
2471 node->externally_visible = false;
2472 node->forced_by_abi = false;
2473 node->local = true;
2474 node->set_section (NULL);
2475 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2476 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2477 && !flag_incremental_link);
2478 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2479 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2481 return false;
2484 /* Bring cgraph node local. */
2486 void
2487 cgraph_node::make_local (void)
2489 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2492 /* Worker to set nothrow flag. */
2494 static void
2495 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2496 bool *changed)
2498 cgraph_edge *e;
2500 if (nothrow && !TREE_NOTHROW (node->decl))
2502 /* With non-call exceptions we can't say for sure if other function body
2503 was not possibly optimized to still throw. */
2504 if (!non_call || node->binds_to_current_def_p ())
2506 TREE_NOTHROW (node->decl) = true;
2507 *changed = true;
2508 for (e = node->callers; e; e = e->next_caller)
2509 e->can_throw_external = false;
2512 else if (!nothrow && TREE_NOTHROW (node->decl))
2514 TREE_NOTHROW (node->decl) = false;
2515 *changed = true;
2517 ipa_ref *ref;
2518 FOR_EACH_ALIAS (node, ref)
2520 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2521 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2522 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2524 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2525 if (e->caller->thunk.thunk_p
2526 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2527 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2530 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2531 if any to NOTHROW. */
2533 bool
2534 cgraph_node::set_nothrow_flag (bool nothrow)
2536 bool changed = false;
2537 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2539 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2540 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2541 else
2543 ipa_ref *ref;
2545 FOR_EACH_ALIAS (this, ref)
2547 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2548 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2549 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2552 return changed;
2555 /* Worker to set malloc flag. */
2556 static void
2557 set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2559 if (malloc_p && !DECL_IS_MALLOC (node->decl))
2561 DECL_IS_MALLOC (node->decl) = true;
2562 *changed = true;
2565 ipa_ref *ref;
2566 FOR_EACH_ALIAS (node, ref)
2568 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2569 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2570 set_malloc_flag_1 (alias, malloc_p, changed);
2573 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2574 if (e->caller->thunk.thunk_p
2575 && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2576 set_malloc_flag_1 (e->caller, malloc_p, changed);
2579 /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2581 bool
2582 cgraph_node::set_malloc_flag (bool malloc_p)
2584 bool changed = false;
2586 if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2587 set_malloc_flag_1 (this, malloc_p, &changed);
2588 else
2590 ipa_ref *ref;
2592 FOR_EACH_ALIAS (this, ref)
2594 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2595 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2596 set_malloc_flag_1 (alias, malloc_p, &changed);
2599 return changed;
2602 /* Worker to set_const_flag. */
2604 static void
2605 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2606 bool *changed)
2608 /* Static constructors and destructors without a side effect can be
2609 optimized out. */
2610 if (set_const && !looping)
2612 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2614 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2615 *changed = true;
2617 if (DECL_STATIC_DESTRUCTOR (node->decl))
2619 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2620 *changed = true;
2623 if (!set_const)
2625 if (TREE_READONLY (node->decl))
2627 TREE_READONLY (node->decl) = 0;
2628 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2629 *changed = true;
2632 else
2634 /* Consider function:
2636 bool a(int *p)
2638 return *p==*p;
2641 During early optimization we will turn this into:
2643 bool a(int *p)
2645 return true;
2648 Now if this function will be detected as CONST however when interposed
2649 it may end up being just pure. We always must assume the worst
2650 scenario here. */
2651 if (TREE_READONLY (node->decl))
2653 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2655 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2656 *changed = true;
2659 else if (node->binds_to_current_def_p ())
2661 TREE_READONLY (node->decl) = true;
2662 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2663 DECL_PURE_P (node->decl) = false;
2664 *changed = true;
2666 else
2668 if (dump_file && (dump_flags & TDF_DETAILS))
2669 fprintf (dump_file, "Dropping state to PURE because function does "
2670 "not bind to current def.\n");
2671 if (!DECL_PURE_P (node->decl))
2673 DECL_PURE_P (node->decl) = true;
2674 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2675 *changed = true;
2677 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2679 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2680 *changed = true;
2685 ipa_ref *ref;
2686 FOR_EACH_ALIAS (node, ref)
2688 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2689 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2690 set_const_flag_1 (alias, set_const, looping, changed);
2692 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2693 if (e->caller->thunk.thunk_p
2694 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2696 /* Virtual thunks access virtual offset in the vtable, so they can
2697 only be pure, never const. */
2698 if (set_const
2699 && (e->caller->thunk.virtual_offset_p
2700 || !node->binds_to_current_def_p (e->caller)))
2701 *changed |= e->caller->set_pure_flag (true, looping);
2702 else
2703 set_const_flag_1 (e->caller, set_const, looping, changed);
2707 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2708 If SET_CONST if false, clear the flag.
2710 When setting the flag be careful about possible interposition and
2711 do not set the flag for functions that can be interposed and set pure
2712 flag for functions that can bind to other definition.
2714 Return true if any change was done. */
2716 bool
2717 cgraph_node::set_const_flag (bool set_const, bool looping)
2719 bool changed = false;
2720 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2721 set_const_flag_1 (this, set_const, looping, &changed);
2722 else
2724 ipa_ref *ref;
2726 FOR_EACH_ALIAS (this, ref)
2728 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2729 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2730 set_const_flag_1 (alias, set_const, looping, &changed);
2733 return changed;
2736 /* Info used by set_pure_flag_1. */
2738 struct set_pure_flag_info
2740 bool pure;
2741 bool looping;
2742 bool changed;
2745 /* Worker to set_pure_flag. */
2747 static bool
2748 set_pure_flag_1 (cgraph_node *node, void *data)
2750 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2751 /* Static constructors and destructors without a side effect can be
2752 optimized out. */
2753 if (info->pure && !info->looping)
2755 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2757 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2758 info->changed = true;
2760 if (DECL_STATIC_DESTRUCTOR (node->decl))
2762 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2763 info->changed = true;
2766 if (info->pure)
2768 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2770 DECL_PURE_P (node->decl) = true;
2771 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2772 info->changed = true;
2774 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2775 && !info->looping)
2777 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2778 info->changed = true;
2781 else
2783 if (DECL_PURE_P (node->decl))
2785 DECL_PURE_P (node->decl) = false;
2786 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2787 info->changed = true;
2790 return false;
2793 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2794 if any to PURE.
2796 When setting the flag, be careful about possible interposition.
2797 Return true if any change was done. */
2799 bool
2800 cgraph_node::set_pure_flag (bool pure, bool looping)
2802 struct set_pure_flag_info info = {pure, looping, false};
2803 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2804 return info.changed;
2807 /* Return true when cgraph_node cannot return or throw and thus
2808 it is safe to ignore its side effects for IPA analysis. */
2810 bool
2811 cgraph_node::cannot_return_p (void)
2813 int flags = flags_from_decl_or_type (decl);
2814 if (!opt_for_fn (decl, flag_exceptions))
2815 return (flags & ECF_NORETURN) != 0;
2816 else
2817 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2818 == (ECF_NORETURN | ECF_NOTHROW));
2821 /* Return true when call of edge cannot lead to return from caller
2822 and thus it is safe to ignore its side effects for IPA analysis
2823 when computing side effects of the caller.
2824 FIXME: We could actually mark all edges that have no reaching
2825 patch to the exit block or throw to get better results. */
2826 bool
2827 cgraph_edge::cannot_lead_to_return_p (void)
2829 if (caller->cannot_return_p ())
2830 return true;
2831 if (indirect_unknown_callee)
2833 int flags = indirect_info->ecf_flags;
2834 if (!opt_for_fn (caller->decl, flag_exceptions))
2835 return (flags & ECF_NORETURN) != 0;
2836 else
2837 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2838 == (ECF_NORETURN | ECF_NOTHROW));
2840 else
2841 return callee->cannot_return_p ();
2844 /* Return true if the edge may be considered hot. */
2846 bool
2847 cgraph_edge::maybe_hot_p (void)
2849 if (!maybe_hot_count_p (NULL, count.ipa ()))
2850 return false;
2851 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2852 || (callee
2853 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2854 return false;
2855 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2856 && (callee
2857 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2858 return false;
2859 if (opt_for_fn (caller->decl, optimize_size))
2860 return false;
2861 if (caller->frequency == NODE_FREQUENCY_HOT)
2862 return true;
2863 if (!count.initialized_p ())
2864 return true;
2865 cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
2866 if (!where->count.initialized_p ())
2867 return false;
2868 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2870 if (count.apply_scale (2, 1) < where->count.apply_scale (3, 1))
2871 return false;
2873 else if (count.apply_scale (param_hot_bb_frequency_fraction , 1)
2874 < where->count)
2875 return false;
2876 return true;
2879 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2881 static bool
2882 nonremovable_p (cgraph_node *node, void *)
2884 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2887 /* Return true if whole comdat group can be removed if there are no direct
2888 calls to THIS. */
2890 bool
2891 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2893 struct ipa_ref *ref;
2895 /* For local symbols or non-comdat group it is the same as
2896 can_remove_if_no_direct_calls_p. */
2897 if (!externally_visible || !same_comdat_group)
2899 if (DECL_EXTERNAL (decl))
2900 return true;
2901 if (address_taken)
2902 return false;
2903 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2906 if (will_inline && address_taken)
2907 return false;
2909 /* Otherwise check if we can remove the symbol itself and then verify
2910 that only uses of the comdat groups are direct call to THIS
2911 or its aliases. */
2912 if (!can_remove_if_no_direct_calls_and_refs_p ())
2913 return false;
2915 /* Check that all refs come from within the comdat group. */
2916 for (int i = 0; iterate_referring (i, ref); i++)
2917 if (ref->referring->get_comdat_group () != get_comdat_group ())
2918 return false;
2920 struct cgraph_node *target = ultimate_alias_target ();
2921 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2922 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2924 if (!externally_visible)
2925 continue;
2926 if (!next->alias
2927 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2928 return false;
2930 /* If we see different symbol than THIS, be sure to check calls. */
2931 if (next->ultimate_alias_target () != target)
2932 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2933 if (e->caller->get_comdat_group () != get_comdat_group ()
2934 || will_inline)
2935 return false;
2937 /* If function is not being inlined, we care only about
2938 references outside of the comdat group. */
2939 if (!will_inline)
2940 for (int i = 0; next->iterate_referring (i, ref); i++)
2941 if (ref->referring->get_comdat_group () != get_comdat_group ())
2942 return false;
2944 return true;
2947 /* Return true when function cgraph_node can be expected to be removed
2948 from program when direct calls in this compilation unit are removed.
2950 As a special case COMDAT functions are
2951 cgraph_can_remove_if_no_direct_calls_p while the are not
2952 cgraph_only_called_directly_p (it is possible they are called from other
2953 unit)
2955 This function behaves as cgraph_only_called_directly_p because eliminating
2956 all uses of COMDAT function does not make it necessarily disappear from
2957 the program unless we are compiling whole program or we do LTO. In this
2958 case we know we win since dynamic linking will not really discard the
2959 linkonce section. */
2961 bool
2962 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2963 (bool will_inline)
2965 gcc_assert (!inlined_to);
2966 if (DECL_EXTERNAL (decl))
2967 return true;
2969 if (!in_lto_p && !flag_whole_program)
2971 /* If the symbol is in comdat group, we need to verify that whole comdat
2972 group becomes unreachable. Technically we could skip references from
2973 within the group, too. */
2974 if (!only_called_directly_p ())
2975 return false;
2976 if (same_comdat_group && externally_visible)
2978 struct cgraph_node *target = ultimate_alias_target ();
2980 if (will_inline && address_taken)
2981 return true;
2982 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2983 next != this;
2984 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2986 if (!externally_visible)
2987 continue;
2988 if (!next->alias
2989 && !next->only_called_directly_p ())
2990 return false;
2992 /* If we see different symbol than THIS,
2993 be sure to check calls. */
2994 if (next->ultimate_alias_target () != target)
2995 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2996 if (e->caller->get_comdat_group () != get_comdat_group ()
2997 || will_inline)
2998 return false;
3001 return true;
3003 else
3004 return can_remove_if_no_direct_calls_p (will_inline);
3008 /* Worker for cgraph_only_called_directly_p. */
3010 static bool
3011 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
3013 return !node->only_called_directly_or_aliased_p ();
3016 /* Return true when function cgraph_node and all its aliases are only called
3017 directly.
3018 i.e. it is not externally visible, address was not taken and
3019 it is not used in any other non-standard way. */
3021 bool
3022 cgraph_node::only_called_directly_p (void)
3024 gcc_assert (ultimate_alias_target () == this);
3025 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
3026 NULL, true);
3030 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3032 static bool
3033 collect_callers_of_node_1 (cgraph_node *node, void *data)
3035 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3036 cgraph_edge *cs;
3037 enum availability avail;
3038 node->ultimate_alias_target (&avail);
3040 if (avail > AVAIL_INTERPOSABLE)
3041 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3042 if (!cs->indirect_inlining_edge
3043 && !cs->caller->thunk.thunk_p)
3044 redirect_callers->safe_push (cs);
3045 return false;
3048 /* Collect all callers of cgraph_node and its aliases that are known to lead to
3049 cgraph_node (i.e. are not overwritable). */
3051 vec<cgraph_edge *>
3052 cgraph_node::collect_callers (void)
3054 vec<cgraph_edge *> redirect_callers = vNULL;
3055 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3056 &redirect_callers, false);
3057 return redirect_callers;
3061 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
3062 optimistically true if this cannot be determined. */
3064 static bool
3065 clone_of_p (cgraph_node *node, cgraph_node *node2)
3067 node = node->ultimate_alias_target ();
3068 node2 = node2->ultimate_alias_target ();
3070 if (node2->clone_of == node
3071 || node2->former_clone_of == node->decl)
3072 return true;
3074 if (!node->thunk.thunk_p && !node->former_thunk_p ())
3076 while (node2 && node->decl != node2->decl)
3077 node2 = node2->clone_of;
3078 return node2 != NULL;
3081 /* There are no virtual clones of thunks so check former_clone_of or if we
3082 might have skipped thunks because this adjustments are no longer
3083 necessary. */
3084 while (node->thunk.thunk_p || node->former_thunk_p ())
3086 if (!node->thunk.this_adjusting)
3087 return false;
3088 /* In case of instrumented expanded thunks, which can have multiple calls
3089 in them, we do not know how to continue and just have to be
3090 optimistic. The same applies if all calls have already been inlined
3091 into the thunk. */
3092 if (!node->callees || node->callees->next_callee)
3093 return true;
3094 node = node->callees->callee->ultimate_alias_target ();
3096 if (!node2->clone.param_adjustments
3097 || node2->clone.param_adjustments->first_param_intact_p ())
3098 return false;
3099 if (node2->former_clone_of == node->decl
3100 || node2->former_clone_of == node->former_clone_of)
3101 return true;
3103 cgraph_node *n2 = node2;
3104 while (n2 && node->decl != n2->decl)
3105 n2 = n2->clone_of;
3106 if (n2)
3107 return true;
3110 return false;
3113 /* Verify edge count and frequency. */
3115 bool
3116 cgraph_edge::verify_count ()
3118 bool error_found = false;
3119 if (!count.verify ())
3121 error ("caller edge count invalid");
3122 error_found = true;
3124 return error_found;
3127 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3128 static void
3129 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3131 bool fndecl_was_null = false;
3132 /* debug_gimple_stmt needs correct cfun */
3133 if (cfun != this_cfun)
3134 set_cfun (this_cfun);
3135 /* ...and an actual current_function_decl */
3136 if (!current_function_decl)
3138 current_function_decl = this_cfun->decl;
3139 fndecl_was_null = true;
3141 debug_gimple_stmt (stmt);
3142 if (fndecl_was_null)
3143 current_function_decl = NULL;
3146 /* Verify that call graph edge corresponds to DECL from the associated
3147 statement. Return true if the verification should fail. */
3149 bool
3150 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3152 cgraph_node *node;
3154 if (!decl || callee->inlined_to)
3155 return false;
3156 if (symtab->state == LTO_STREAMING)
3157 return false;
3158 node = cgraph_node::get (decl);
3160 /* We do not know if a node from a different partition is an alias or what it
3161 aliases and therefore cannot do the former_clone_of check reliably. When
3162 body_removed is set, we have lost all information about what was alias or
3163 thunk of and also cannot proceed. */
3164 if (!node
3165 || node->body_removed
3166 || node->in_other_partition
3167 || callee->icf_merged
3168 || callee->in_other_partition)
3169 return false;
3171 node = node->ultimate_alias_target ();
3173 /* Optimizers can redirect unreachable calls or calls triggering undefined
3174 behavior to builtin_unreachable. */
3176 if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE))
3177 return false;
3179 if (callee->former_clone_of != node->decl
3180 && (node != callee->ultimate_alias_target ())
3181 && !clone_of_p (node, callee))
3182 return true;
3183 else
3184 return false;
3187 /* Disable warnings about missing quoting in GCC diagnostics for
3188 the verification errors. Their format strings don't follow GCC
3189 diagnostic conventions and the calls are ultimately followed by
3190 one to internal_error. */
3191 #if __GNUC__ >= 10
3192 # pragma GCC diagnostic push
3193 # pragma GCC diagnostic ignored "-Wformat-diag"
3194 #endif
3196 /* Verify consistency of speculative call in NODE corresponding to STMT
3197 and LTO_STMT_UID. If INDIRECT is set, assume that it is the indirect
3198 edge of call sequence. Return true if error is found.
3200 This function is called to every component of indirect call (direct edges,
3201 indirect edge and refs). To save duplicated work, do full testing only
3202 in that case. */
3203 static bool
3204 verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3205 unsigned int lto_stmt_uid,
3206 struct cgraph_edge *indirect)
3208 if (indirect == NULL)
3210 for (indirect = node->indirect_calls; indirect;
3211 indirect = indirect->next_callee)
3212 if (indirect->call_stmt == stmt
3213 && indirect->lto_stmt_uid == lto_stmt_uid)
3214 break;
3215 if (!indirect)
3217 error ("missing indirect call in speculative call sequence");
3218 return true;
3220 if (!indirect->speculative)
3222 error ("indirect call in speculative call sequence has no "
3223 "speculative flag");
3224 return true;
3226 return false;
3229 /* Maximal number of targets. We probably will never want to have more than
3230 this. */
3231 const unsigned int num = 256;
3232 cgraph_edge *direct_calls[num];
3233 ipa_ref *refs[num];
3235 for (unsigned int i = 0; i < num; i++)
3237 direct_calls[i] = NULL;
3238 refs[i] = NULL;
3241 cgraph_edge *first_call = NULL;
3242 cgraph_edge *prev_call = NULL;
3244 for (cgraph_edge *direct = node->callees; direct;
3245 direct = direct->next_callee)
3246 if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3248 if (!first_call)
3249 first_call = direct;
3250 if (prev_call && direct != prev_call->next_callee)
3252 error ("speculative edges are not adjacent");
3253 return true;
3255 prev_call = direct;
3256 if (!direct->speculative)
3258 error ("direct call to %s in speculative call sequence has no "
3259 "speculative flag", direct->callee->dump_name ());
3260 return true;
3262 if (direct->speculative_id >= num)
3264 error ("direct call to %s in speculative call sequence has "
3265 "speculative_id %i out of range",
3266 direct->callee->dump_name (), direct->speculative_id);
3267 return true;
3269 if (direct_calls[direct->speculative_id])
3271 error ("duplicate direct call to %s in speculative call sequence "
3272 "with speculative_id %i",
3273 direct->callee->dump_name (), direct->speculative_id);
3274 return true;
3276 direct_calls[direct->speculative_id] = direct;
3279 if (first_call->call_stmt
3280 && first_call != node->get_edge (first_call->call_stmt))
3282 error ("call stmt hash does not point to first direct edge of "
3283 "speculative call sequence");
3284 return true;
3287 ipa_ref *ref;
3288 for (int i = 0; node->iterate_reference (i, ref); i++)
3289 if (ref->speculative
3290 && ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3292 if (ref->speculative_id >= num)
3294 error ("direct call to %s in speculative call sequence has "
3295 "speculative_id %i out of range",
3296 ref->referred->dump_name (), ref->speculative_id);
3297 return true;
3299 if (refs[ref->speculative_id])
3301 error ("duplicate reference %s in speculative call sequence "
3302 "with speculative_id %i",
3303 ref->referred->dump_name (), ref->speculative_id);
3304 return true;
3306 refs[ref->speculative_id] = ref;
3309 int num_targets = 0;
3310 for (unsigned int i = 0 ; i < num ; i++)
3312 if (refs[i] && !direct_calls[i])
3314 error ("missing direct call for speculation %i", i);
3315 return true;
3317 if (!refs[i] && direct_calls[i])
3319 error ("missing ref for speculation %i", i);
3320 return true;
3322 if (refs[i] != NULL)
3323 num_targets++;
3326 if (num_targets != indirect->num_speculative_call_targets_p ())
3328 error ("number of speculative targets %i mismatched with "
3329 "num_speculative_call_targets %i",
3330 num_targets,
3331 indirect->num_speculative_call_targets_p ());
3332 return true;
3334 return false;
3337 /* Verify cgraph nodes of given cgraph node. */
3338 DEBUG_FUNCTION void
3339 cgraph_node::verify_node (void)
3341 cgraph_edge *e;
3342 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3343 basic_block this_block;
3344 gimple_stmt_iterator gsi;
3345 bool error_found = false;
3346 int i;
3347 ipa_ref *ref = NULL;
3349 if (seen_error ())
3350 return;
3352 timevar_push (TV_CGRAPH_VERIFY);
3353 error_found |= verify_base ();
3354 for (e = callees; e; e = e->next_callee)
3355 if (e->aux)
3357 error ("aux field set for edge %s->%s",
3358 identifier_to_locale (e->caller->name ()),
3359 identifier_to_locale (e->callee->name ()));
3360 error_found = true;
3362 if (!count.verify ())
3364 error ("cgraph count invalid");
3365 error_found = true;
3367 if (inlined_to && same_comdat_group)
3369 error ("inline clone in same comdat group list");
3370 error_found = true;
3372 if (inlined_to && !count.compatible_p (inlined_to->count))
3374 error ("inline clone count is not compatible");
3375 count.debug ();
3376 inlined_to->count.debug ();
3377 error_found = true;
3379 if (tp_first_run < 0)
3381 error ("tp_first_run must be non-negative");
3382 error_found = true;
3384 if (!definition && !in_other_partition && local)
3386 error ("local symbols must be defined");
3387 error_found = true;
3389 if (inlined_to && externally_visible)
3391 error ("externally visible inline clone");
3392 error_found = true;
3394 if (inlined_to && address_taken)
3396 error ("inline clone with address taken");
3397 error_found = true;
3399 if (inlined_to && force_output)
3401 error ("inline clone is forced to output");
3402 error_found = true;
3404 if (symtab->state != LTO_STREAMING)
3406 if (calls_comdat_local && !same_comdat_group)
3408 error ("calls_comdat_local is set outside of a comdat group");
3409 error_found = true;
3411 if (!inlined_to && calls_comdat_local != check_calls_comdat_local_p ())
3413 error ("invalid calls_comdat_local flag");
3414 error_found = true;
3417 if (DECL_IS_MALLOC (decl)
3418 && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3420 error ("malloc attribute should be used for a function that "
3421 "returns a pointer");
3422 error_found = true;
3424 for (e = indirect_calls; e; e = e->next_callee)
3426 if (e->aux)
3428 error ("aux field set for indirect edge from %s",
3429 identifier_to_locale (e->caller->name ()));
3430 error_found = true;
3432 if (!e->count.compatible_p (count))
3434 error ("edge count is not compatible with function count");
3435 e->count.debug ();
3436 count.debug ();
3437 error_found = true;
3439 if (!e->indirect_unknown_callee
3440 || !e->indirect_info)
3442 error ("An indirect edge from %s is not marked as indirect or has "
3443 "associated indirect_info, the corresponding statement is: ",
3444 identifier_to_locale (e->caller->name ()));
3445 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3446 error_found = true;
3448 if (e->call_stmt && e->lto_stmt_uid)
3450 error ("edge has both call_stmt and lto_stmt_uid set");
3451 error_found = true;
3454 bool check_comdat = comdat_local_p ();
3455 for (e = callers; e; e = e->next_caller)
3457 if (e->verify_count ())
3458 error_found = true;
3459 if (check_comdat
3460 && !in_same_comdat_group_p (e->caller))
3462 error ("comdat-local function called by %s outside its comdat",
3463 identifier_to_locale (e->caller->name ()));
3464 error_found = true;
3466 if (!e->inline_failed)
3468 if (inlined_to
3469 != (e->caller->inlined_to
3470 ? e->caller->inlined_to : e->caller))
3472 error ("inlined_to pointer is wrong");
3473 error_found = true;
3475 if (callers->next_caller)
3477 error ("multiple inline callers");
3478 error_found = true;
3481 else
3482 if (inlined_to)
3484 error ("inlined_to pointer set for noninline callers");
3485 error_found = true;
3488 for (e = callees; e; e = e->next_callee)
3490 if (e->verify_count ())
3491 error_found = true;
3492 if (!e->count.compatible_p (count))
3494 error ("edge count is not compatible with function count");
3495 e->count.debug ();
3496 count.debug ();
3497 error_found = true;
3499 if (gimple_has_body_p (e->caller->decl)
3500 && !e->caller->inlined_to
3501 && !e->speculative
3502 /* Optimized out calls are redirected to __builtin_unreachable. */
3503 && (e->count.nonzero_p ()
3504 || ! e->callee->decl
3505 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
3506 && count
3507 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3508 && (!e->count.ipa_p ()
3509 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3511 error ("caller edge count does not match BB count");
3512 fprintf (stderr, "edge count: ");
3513 e->count.dump (stderr);
3514 fprintf (stderr, "\n bb count: ");
3515 gimple_bb (e->call_stmt)->count.dump (stderr);
3516 fprintf (stderr, "\n");
3517 error_found = true;
3519 if (e->call_stmt && e->lto_stmt_uid)
3521 error ("edge has both call_stmt and lto_stmt_uid set");
3522 error_found = true;
3524 if (e->speculative
3525 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3526 NULL))
3527 error_found = true;
3529 for (e = indirect_calls; e; e = e->next_callee)
3531 if (e->verify_count ())
3532 error_found = true;
3533 if (gimple_has_body_p (e->caller->decl)
3534 && !e->caller->inlined_to
3535 && !e->speculative
3536 && e->count.ipa_p ()
3537 && count
3538 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3539 && (!e->count.ipa_p ()
3540 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3542 error ("indirect call count does not match BB count");
3543 fprintf (stderr, "edge count: ");
3544 e->count.dump (stderr);
3545 fprintf (stderr, "\n bb count: ");
3546 gimple_bb (e->call_stmt)->count.dump (stderr);
3547 fprintf (stderr, "\n");
3548 error_found = true;
3550 if (e->speculative
3551 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3553 error_found = true;
3555 for (i = 0; iterate_reference (i, ref); i++)
3557 if (ref->stmt && ref->lto_stmt_uid)
3559 error ("reference has both stmt and lto_stmt_uid set");
3560 error_found = true;
3562 if (ref->speculative
3563 && verify_speculative_call (this, ref->stmt,
3564 ref->lto_stmt_uid, NULL))
3565 error_found = true;
3568 if (!callers && inlined_to)
3570 error ("inlined_to pointer is set but no predecessors found");
3571 error_found = true;
3573 if (inlined_to == this)
3575 error ("inlined_to pointer refers to itself");
3576 error_found = true;
3579 if (clone_of)
3581 cgraph_node *first_clone = clone_of->clones;
3582 if (first_clone != this)
3584 if (prev_sibling_clone->clone_of != clone_of)
3586 error ("cgraph_node has wrong clone_of");
3587 error_found = true;
3591 if (clones)
3593 cgraph_node *n;
3594 for (n = clones; n; n = n->next_sibling_clone)
3595 if (n->clone_of != this)
3596 break;
3597 if (n)
3599 error ("cgraph_node has wrong clone list");
3600 error_found = true;
3603 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3605 error ("cgraph_node is in clone list but it is not clone");
3606 error_found = true;
3608 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3610 error ("cgraph_node has wrong prev_clone pointer");
3611 error_found = true;
3613 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3615 error ("double linked list of clones corrupted");
3616 error_found = true;
3619 if (analyzed && alias)
3621 bool ref_found = false;
3622 int i;
3623 ipa_ref *ref = NULL;
3625 if (callees)
3627 error ("Alias has call edges");
3628 error_found = true;
3630 for (i = 0; iterate_reference (i, ref); i++)
3631 if (ref->use != IPA_REF_ALIAS)
3633 error ("Alias has non-alias reference");
3634 error_found = true;
3636 else if (ref_found)
3638 error ("Alias has more than one alias reference");
3639 error_found = true;
3641 else
3642 ref_found = true;
3643 if (!ref_found)
3645 error ("Analyzed alias has no reference");
3646 error_found = true;
3650 if (analyzed && thunk.thunk_p)
3652 if (!callees)
3654 error ("No edge out of thunk node");
3655 error_found = true;
3657 else if (callees->next_callee)
3659 error ("More than one edge out of thunk node");
3660 error_found = true;
3662 if (gimple_has_body_p (decl) && !inlined_to)
3664 error ("Thunk is not supposed to have body");
3665 error_found = true;
3668 else if (analyzed && gimple_has_body_p (decl)
3669 && !TREE_ASM_WRITTEN (decl)
3670 && (!DECL_EXTERNAL (decl) || inlined_to)
3671 && !flag_wpa)
3673 if (this_cfun->cfg)
3675 hash_set<gimple *> stmts;
3677 /* Reach the trees by walking over the CFG, and note the
3678 enclosing basic-blocks in the call edges. */
3679 FOR_EACH_BB_FN (this_block, this_cfun)
3681 for (gsi = gsi_start_phis (this_block);
3682 !gsi_end_p (gsi); gsi_next (&gsi))
3683 stmts.add (gsi_stmt (gsi));
3684 for (gsi = gsi_start_bb (this_block);
3685 !gsi_end_p (gsi);
3686 gsi_next (&gsi))
3688 gimple *stmt = gsi_stmt (gsi);
3689 stmts.add (stmt);
3690 if (is_gimple_call (stmt))
3692 cgraph_edge *e = get_edge (stmt);
3693 tree decl = gimple_call_fndecl (stmt);
3694 if (e)
3696 if (e->aux)
3698 error ("shared call_stmt:");
3699 cgraph_debug_gimple_stmt (this_cfun, stmt);
3700 error_found = true;
3702 if (!e->indirect_unknown_callee)
3704 if (e->verify_corresponds_to_fndecl (decl))
3706 error ("edge points to wrong declaration:");
3707 debug_tree (e->callee->decl);
3708 fprintf (stderr," Instead of:");
3709 debug_tree (decl);
3710 error_found = true;
3713 else if (decl)
3715 error ("an indirect edge with unknown callee "
3716 "corresponding to a call_stmt with "
3717 "a known declaration:");
3718 error_found = true;
3719 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3721 e->aux = (void *)1;
3723 else if (decl)
3725 error ("missing callgraph edge for call stmt:");
3726 cgraph_debug_gimple_stmt (this_cfun, stmt);
3727 error_found = true;
3732 for (i = 0; iterate_reference (i, ref); i++)
3733 if (ref->stmt && !stmts.contains (ref->stmt))
3735 error ("reference to dead statement");
3736 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3737 error_found = true;
3740 else
3741 /* No CFG available?! */
3742 gcc_unreachable ();
3744 for (e = callees; e; e = e->next_callee)
3746 if (!e->aux && !e->speculative)
3748 error ("edge %s->%s has no corresponding call_stmt",
3749 identifier_to_locale (e->caller->name ()),
3750 identifier_to_locale (e->callee->name ()));
3751 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3752 error_found = true;
3754 e->aux = 0;
3756 for (e = indirect_calls; e; e = e->next_callee)
3758 if (!e->aux && !e->speculative)
3760 error ("an indirect edge from %s has no corresponding call_stmt",
3761 identifier_to_locale (e->caller->name ()));
3762 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3763 error_found = true;
3765 e->aux = 0;
3769 if (nested_function_info *info = nested_function_info::get (this))
3771 if (info->nested != NULL)
3773 for (cgraph_node *n = info->nested; n != NULL;
3774 n = next_nested_function (n))
3776 nested_function_info *ninfo = nested_function_info::get (n);
3777 if (ninfo->origin == NULL)
3779 error ("missing origin for a node in a nested list");
3780 error_found = true;
3782 else if (ninfo->origin != this)
3784 error ("origin points to a different parent");
3785 error_found = true;
3786 break;
3790 if (info->next_nested != NULL && info->origin == NULL)
3792 error ("missing origin for a node in a nested list");
3793 error_found = true;
3797 if (error_found)
3799 dump (stderr);
3800 internal_error ("verify_cgraph_node failed");
3802 timevar_pop (TV_CGRAPH_VERIFY);
3805 /* Verify whole cgraph structure. */
3806 DEBUG_FUNCTION void
3807 cgraph_node::verify_cgraph_nodes (void)
3809 cgraph_node *node;
3811 if (seen_error ())
3812 return;
3814 FOR_EACH_FUNCTION (node)
3815 node->verify ();
3818 #if __GNUC__ >= 10
3819 # pragma GCC diagnostic pop
3820 #endif
3822 /* Walk the alias chain to return the function cgraph_node is alias of.
3823 Walk through thunks, too.
3824 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3825 When REF is non-NULL, assume that reference happens in symbol REF
3826 when determining the availability. */
3828 cgraph_node *
3829 cgraph_node::function_symbol (enum availability *availability,
3830 struct symtab_node *ref)
3832 cgraph_node *node = ultimate_alias_target (availability, ref);
3834 while (node->thunk.thunk_p)
3836 enum availability a;
3838 ref = node;
3839 node = node->callees->callee;
3840 node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3841 if (availability && a < *availability)
3842 *availability = a;
3844 return node;
3847 /* Walk the alias chain to return the function cgraph_node is alias of.
3848 Walk through non virtual thunks, too. Thus we return either a function
3849 or a virtual thunk node.
3850 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3851 When REF is non-NULL, assume that reference happens in symbol REF
3852 when determining the availability. */
3854 cgraph_node *
3855 cgraph_node::function_or_virtual_thunk_symbol
3856 (enum availability *availability,
3857 struct symtab_node *ref)
3859 cgraph_node *node = ultimate_alias_target (availability, ref);
3861 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3863 enum availability a;
3865 ref = node;
3866 node = node->callees->callee;
3867 node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3868 if (availability && a < *availability)
3869 *availability = a;
3871 return node;
3874 /* When doing LTO, read cgraph_node's body from disk if it is not already
3875 present. Also perform any necessary clone materializations. */
3877 bool
3878 cgraph_node::get_untransformed_body ()
3880 lto_file_decl_data *file_data;
3881 const char *data, *name;
3882 size_t len;
3883 tree decl = this->decl;
3885 /* See if there is clone to be materialized.
3886 (inline clones does not need materialization, but we can be seeing
3887 an inline clone of real clone). */
3888 cgraph_node *p = this;
3889 for (cgraph_node *c = clone_of; c; c = c->clone_of)
3891 if (c->decl != decl)
3892 p->materialize_clone ();
3893 p = c;
3896 /* Check if body is already there. Either we have gimple body or
3897 the function is thunk and in that case we set DECL_ARGUMENTS. */
3898 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3899 return false;
3901 gcc_assert (in_lto_p && !DECL_RESULT (decl));
3903 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3905 file_data = lto_file_data;
3906 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3908 /* We may have renamed the declaration, e.g., a static function. */
3909 name = lto_get_decl_name_mapping (file_data, name);
3910 struct lto_in_decl_state *decl_state
3911 = lto_get_function_in_decl_state (file_data, decl);
3913 cgraph_node *origin = this;
3914 while (origin->clone_of)
3915 origin = origin->clone_of;
3917 int stream_order = origin->order - file_data->order_base;
3918 data = lto_get_section_data (file_data, LTO_section_function_body,
3919 name, stream_order, &len,
3920 decl_state->compressed);
3921 if (!data)
3922 fatal_error (input_location, "%s: section %s.%d is missing",
3923 file_data->file_name, name, stream_order);
3925 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3927 if (!quiet_flag)
3928 fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
3929 lto_input_function_body (file_data, this, data);
3930 lto_stats.num_function_bodies++;
3931 lto_free_section_data (file_data, LTO_section_function_body, name,
3932 data, len, decl_state->compressed);
3933 lto_free_function_in_decl_state_for_node (this);
3934 /* Keep lto file data so ipa-inline-analysis knows about cross module
3935 inlining. */
3937 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3939 return true;
3942 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3943 if it is not already present. When some IPA transformations are scheduled,
3944 apply them. */
3946 bool
3947 cgraph_node::get_body (void)
3949 bool updated;
3951 updated = get_untransformed_body ();
3953 /* Getting transformed body makes no sense for inline clones;
3954 we should never use this on real clones because they are materialized
3955 early.
3956 TODO: Materializing clones here will likely lead to smaller LTRANS
3957 footprint. */
3958 gcc_assert (!inlined_to && !clone_of);
3959 if (ipa_transforms_to_apply.exists ())
3961 opt_pass *saved_current_pass = current_pass;
3962 FILE *saved_dump_file = dump_file;
3963 const char *saved_dump_file_name = dump_file_name;
3964 dump_flags_t saved_dump_flags = dump_flags;
3965 dump_file_name = NULL;
3966 set_dump_file (NULL);
3968 push_cfun (DECL_STRUCT_FUNCTION (decl));
3970 update_ssa (TODO_update_ssa_only_virtuals);
3971 execute_all_ipa_transforms (true);
3972 cgraph_edge::rebuild_edges ();
3973 free_dominance_info (CDI_DOMINATORS);
3974 free_dominance_info (CDI_POST_DOMINATORS);
3975 pop_cfun ();
3976 updated = true;
3978 current_pass = saved_current_pass;
3979 set_dump_file (saved_dump_file);
3980 dump_file_name = saved_dump_file_name;
3981 dump_flags = saved_dump_flags;
3983 return updated;
3986 /* Return the DECL_STRUCT_FUNCTION of the function. */
3988 struct function *
3989 cgraph_node::get_fun () const
3991 const cgraph_node *node = this;
3992 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3994 while (!fun && node->clone_of)
3996 node = node->clone_of;
3997 fun = DECL_STRUCT_FUNCTION (node->decl);
4000 return fun;
4003 /* Reset all state within cgraph.c so that we can rerun the compiler
4004 within the same process. For use by toplev::finalize. */
4006 void
4007 cgraph_c_finalize (void)
4009 nested_function_info::release ();
4010 symtab = NULL;
4012 x_cgraph_nodes_queue = NULL;
4014 cgraph_fnver_htab = NULL;
4015 version_info_node = NULL;
4018 /* A worker for call_for_symbol_and_aliases. */
4020 bool
4021 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
4022 void *),
4023 void *data,
4024 bool include_overwritable)
4026 ipa_ref *ref;
4027 FOR_EACH_ALIAS (this, ref)
4029 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
4030 if (include_overwritable
4031 || alias->get_availability () > AVAIL_INTERPOSABLE)
4032 if (alias->call_for_symbol_and_aliases (callback, data,
4033 include_overwritable))
4034 return true;
4036 return false;
4039 /* Return true if NODE has thunk. */
4041 bool
4042 cgraph_node::has_thunk_p (cgraph_node *node, void *)
4044 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
4045 if (e->caller->thunk.thunk_p)
4046 return true;
4047 return false;
4050 /* Expected frequency of executions within the function. */
4052 sreal
4053 cgraph_edge::sreal_frequency ()
4055 return count.to_sreal_scale (caller->inlined_to
4056 ? caller->inlined_to->count
4057 : caller->count);
4061 /* During LTO stream in this can be used to check whether call can possibly
4062 be internal to the current translation unit. */
4064 bool
4065 cgraph_edge::possibly_call_in_translation_unit_p (void)
4067 gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4069 /* While incremental linking we may end up getting function body later. */
4070 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4071 return true;
4073 /* We may be smarter here and avoid streaming in indirect calls we can't
4074 track, but that would require arranging streaming the indirect call
4075 summary first. */
4076 if (!callee)
4077 return true;
4079 /* If callee is local to the original translation unit, it will be
4080 defined. */
4081 if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4082 return true;
4084 /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4085 yet) and see if it is a definition. In fact we may also resolve aliases,
4086 but that is probably not too important. */
4087 symtab_node *node = callee;
4088 for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4089 node = node->previous_sharing_asm_name;
4090 if (node->previous_sharing_asm_name)
4091 node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4092 gcc_assert (TREE_PUBLIC (node->decl));
4093 return node->get_availability () >= AVAIL_INTERPOSABLE;
4096 /* Return num_speculative_targets of this edge. */
4099 cgraph_edge::num_speculative_call_targets_p (void)
4101 return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4104 /* Check if function calls comdat local. This is used to recompute
4105 calls_comdat_local flag after function transformations. */
4106 bool
4107 cgraph_node::check_calls_comdat_local_p ()
4109 for (cgraph_edge *e = callees; e; e = e->next_callee)
4110 if (e->inline_failed
4111 ? e->callee->comdat_local_p ()
4112 : e->callee->check_calls_comdat_local_p ())
4113 return true;
4114 return false;
4117 /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4118 This needs to be a global so that it can be a GC root, and thus
4119 prevent the stashed copy from being garbage-collected if the GC runs
4120 during a symbol_table_test. */
4122 symbol_table *saved_symtab;
4124 #if CHECKING_P
4126 namespace selftest {
4128 /* class selftest::symbol_table_test. */
4130 /* Constructor. Store the old value of symtab, and create a new one. */
4132 symbol_table_test::symbol_table_test ()
4134 gcc_assert (saved_symtab == NULL);
4135 saved_symtab = symtab;
4136 symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4139 /* Destructor. Restore the old value of symtab. */
4141 symbol_table_test::~symbol_table_test ()
4143 gcc_assert (saved_symtab != NULL);
4144 symtab = saved_symtab;
4145 saved_symtab = NULL;
4148 /* Verify that symbol_table_test works. */
4150 static void
4151 test_symbol_table_test ()
4153 /* Simulate running two selftests involving symbol tables. */
4154 for (int i = 0; i < 2; i++)
4156 symbol_table_test stt;
4157 tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4158 get_identifier ("test_decl"),
4159 build_function_type_list (void_type_node,
4160 NULL_TREE));
4161 cgraph_node *node = cgraph_node::get_create (test_decl);
4162 gcc_assert (node);
4164 /* Verify that the node has order 0 on both iterations,
4165 and thus that nodes have predictable dump names in selftests. */
4166 ASSERT_EQ (node->order, 0);
4167 ASSERT_STREQ (node->dump_name (), "test_decl/0");
4171 /* Run all of the selftests within this file. */
4173 void
4174 cgraph_c_tests ()
4176 test_symbol_table_test ();
4179 } // namespace selftest
4181 #endif /* CHECKING_P */
4183 #include "gt-cgraph.h"