[RS6000] Power10 ICE running gcc.target/powerpc/ppc-ne0-1.c
[official-gcc.git] / gcc / cgraph.c
blob9f317733c91343ff9f5329a10b396d6c4eb46e6b
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"
68 #include "symtab-thunks.h"
70 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
71 #include "tree-pass.h"
73 /* Queue of cgraph nodes scheduled to be lowered. */
74 symtab_node *x_cgraph_nodes_queue;
75 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
77 /* Symbol table global context. */
78 symbol_table *symtab;
80 /* List of hooks triggered on cgraph_edge events. */
81 struct cgraph_edge_hook_list {
82 cgraph_edge_hook hook;
83 void *data;
84 struct cgraph_edge_hook_list *next;
87 /* List of hooks triggered on cgraph_node events. */
88 struct cgraph_node_hook_list {
89 cgraph_node_hook hook;
90 void *data;
91 struct cgraph_node_hook_list *next;
94 /* List of hooks triggered on events involving two cgraph_edges. */
95 struct cgraph_2edge_hook_list {
96 cgraph_2edge_hook hook;
97 void *data;
98 struct cgraph_2edge_hook_list *next;
101 /* List of hooks triggered on events involving two cgraph_nodes. */
102 struct cgraph_2node_hook_list {
103 cgraph_2node_hook hook;
104 void *data;
105 struct cgraph_2node_hook_list *next;
108 /* Hash descriptor for cgraph_function_version_info. */
110 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
112 static hashval_t hash (cgraph_function_version_info *);
113 static bool equal (cgraph_function_version_info *,
114 cgraph_function_version_info *);
117 /* Map a cgraph_node to cgraph_function_version_info using this htab.
118 The cgraph_function_version_info has a THIS_NODE field that is the
119 corresponding cgraph_node.. */
121 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
123 /* Hash function for cgraph_fnver_htab. */
124 hashval_t
125 function_version_hasher::hash (cgraph_function_version_info *ptr)
127 int uid = ptr->this_node->get_uid ();
128 return (hashval_t)(uid);
131 /* eq function for cgraph_fnver_htab. */
132 bool
133 function_version_hasher::equal (cgraph_function_version_info *n1,
134 cgraph_function_version_info *n2)
136 return n1->this_node->get_uid () == n2->this_node->get_uid ();
139 /* Mark as GC root all allocated nodes. */
140 static GTY(()) struct cgraph_function_version_info *
141 version_info_node = NULL;
143 /* Return true if NODE's address can be compared. */
145 bool
146 symtab_node::address_can_be_compared_p ()
148 /* Address of virtual tables and functions is never compared. */
149 if (DECL_VIRTUAL_P (decl))
150 return false;
151 /* Address of C++ cdtors is never compared. */
152 if (is_a <cgraph_node *> (this)
153 && (DECL_CXX_CONSTRUCTOR_P (decl)
154 || DECL_CXX_DESTRUCTOR_P (decl)))
155 return false;
156 /* Constant pool symbols addresses are never compared.
157 flag_merge_constants permits us to assume the same on readonly vars. */
158 if (is_a <varpool_node *> (this)
159 && (DECL_IN_CONSTANT_POOL (decl)
160 || (flag_merge_constants >= 2
161 && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
162 return false;
163 return true;
166 /* Get the cgraph_function_version_info node corresponding to node. */
167 cgraph_function_version_info *
168 cgraph_node::function_version (void)
170 cgraph_function_version_info key;
171 key.this_node = this;
173 if (cgraph_fnver_htab == NULL)
174 return NULL;
176 return cgraph_fnver_htab->find (&key);
179 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
180 corresponding to cgraph_node NODE. */
181 cgraph_function_version_info *
182 cgraph_node::insert_new_function_version (void)
184 version_info_node = NULL;
185 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
186 version_info_node->this_node = this;
188 if (cgraph_fnver_htab == NULL)
189 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
191 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
192 = version_info_node;
193 return version_info_node;
196 /* Remove the cgraph_function_version_info node given by DECL_V. */
197 static void
198 delete_function_version (cgraph_function_version_info *decl_v)
200 if (decl_v == NULL)
201 return;
203 if (version_info_node == decl_v)
204 version_info_node = NULL;
206 if (decl_v->prev != NULL)
207 decl_v->prev->next = decl_v->next;
209 if (decl_v->next != NULL)
210 decl_v->next->prev = decl_v->prev;
212 if (cgraph_fnver_htab != NULL)
213 cgraph_fnver_htab->remove_elt (decl_v);
216 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
217 DECL is a duplicate declaration. */
218 void
219 cgraph_node::delete_function_version_by_decl (tree decl)
221 cgraph_node *decl_node = cgraph_node::get (decl);
223 if (decl_node == NULL)
224 return;
226 delete_function_version (decl_node->function_version ());
228 decl_node->remove ();
231 /* Record that DECL1 and DECL2 are semantically identical function
232 versions. */
233 void
234 cgraph_node::record_function_versions (tree decl1, tree decl2)
236 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
237 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
238 cgraph_function_version_info *decl1_v = NULL;
239 cgraph_function_version_info *decl2_v = NULL;
240 cgraph_function_version_info *before;
241 cgraph_function_version_info *after;
243 gcc_assert (decl1_node != NULL && decl2_node != NULL);
244 decl1_v = decl1_node->function_version ();
245 decl2_v = decl2_node->function_version ();
247 if (decl1_v != NULL && decl2_v != NULL)
248 return;
250 if (decl1_v == NULL)
251 decl1_v = decl1_node->insert_new_function_version ();
253 if (decl2_v == NULL)
254 decl2_v = decl2_node->insert_new_function_version ();
256 /* Chain decl2_v and decl1_v. All semantically identical versions
257 will be chained together. */
259 before = decl1_v;
260 after = decl2_v;
262 while (before->next != NULL)
263 before = before->next;
265 while (after->prev != NULL)
266 after= after->prev;
268 before->next = after;
269 after->prev = before;
272 /* Initialize callgraph dump file. */
274 void
275 symbol_table::initialize (void)
277 if (!dump_file)
278 dump_file = dump_begin (TDI_cgraph, NULL);
280 if (!ipa_clones_dump_file)
281 ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
284 /* Allocate new callgraph node and insert it into basic data structures. */
286 cgraph_node *
287 symbol_table::create_empty (void)
289 cgraph_count++;
290 return new (ggc_alloc<cgraph_node> ()) cgraph_node (cgraph_max_uid++);
293 /* Register HOOK to be called with DATA on each removed edge. */
294 cgraph_edge_hook_list *
295 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
297 cgraph_edge_hook_list *entry;
298 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
300 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
301 entry->hook = hook;
302 entry->data = data;
303 entry->next = NULL;
304 while (*ptr)
305 ptr = &(*ptr)->next;
306 *ptr = entry;
307 return entry;
310 /* Remove ENTRY from the list of hooks called on removing edges. */
311 void
312 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
314 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
316 while (*ptr != entry)
317 ptr = &(*ptr)->next;
318 *ptr = entry->next;
319 free (entry);
322 /* Call all edge removal hooks. */
323 void
324 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
326 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
327 while (entry)
329 entry->hook (e, entry->data);
330 entry = entry->next;
334 /* Register HOOK to be called with DATA on each removed node. */
335 cgraph_node_hook_list *
336 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
338 cgraph_node_hook_list *entry;
339 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
341 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
342 entry->hook = hook;
343 entry->data = data;
344 entry->next = NULL;
345 while (*ptr)
346 ptr = &(*ptr)->next;
347 *ptr = entry;
348 return entry;
351 /* Remove ENTRY from the list of hooks called on removing nodes. */
352 void
353 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
355 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
357 while (*ptr != entry)
358 ptr = &(*ptr)->next;
359 *ptr = entry->next;
360 free (entry);
363 /* Call all node removal hooks. */
364 void
365 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
367 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
368 while (entry)
370 entry->hook (node, entry->data);
371 entry = entry->next;
375 /* Call all node removal hooks. */
376 void
377 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
379 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
380 while (entry)
382 entry->hook (node, entry->data);
383 entry = entry->next;
388 /* Register HOOK to be called with DATA on each inserted node. */
389 cgraph_node_hook_list *
390 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
392 cgraph_node_hook_list *entry;
393 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
395 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
396 entry->hook = hook;
397 entry->data = data;
398 entry->next = NULL;
399 while (*ptr)
400 ptr = &(*ptr)->next;
401 *ptr = entry;
402 return entry;
405 /* Remove ENTRY from the list of hooks called on inserted nodes. */
406 void
407 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
409 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
411 while (*ptr != entry)
412 ptr = &(*ptr)->next;
413 *ptr = entry->next;
414 free (entry);
417 /* Register HOOK to be called with DATA on each duplicated edge. */
418 cgraph_2edge_hook_list *
419 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
421 cgraph_2edge_hook_list *entry;
422 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
424 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
425 entry->hook = hook;
426 entry->data = data;
427 entry->next = NULL;
428 while (*ptr)
429 ptr = &(*ptr)->next;
430 *ptr = entry;
431 return entry;
434 /* Remove ENTRY from the list of hooks called on duplicating edges. */
435 void
436 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
438 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
440 while (*ptr != entry)
441 ptr = &(*ptr)->next;
442 *ptr = entry->next;
443 free (entry);
446 /* Call all edge duplication hooks. */
447 void
448 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
450 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
451 while (entry)
453 entry->hook (cs1, cs2, entry->data);
454 entry = entry->next;
458 /* Register HOOK to be called with DATA on each duplicated node. */
459 cgraph_2node_hook_list *
460 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
462 cgraph_2node_hook_list *entry;
463 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
465 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
466 entry->hook = hook;
467 entry->data = data;
468 entry->next = NULL;
469 while (*ptr)
470 ptr = &(*ptr)->next;
471 *ptr = entry;
472 return entry;
475 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
476 void
477 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
479 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
481 while (*ptr != entry)
482 ptr = &(*ptr)->next;
483 *ptr = entry->next;
484 free (entry);
487 /* Call all node duplication hooks. */
488 void
489 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
490 cgraph_node *node2)
492 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
493 while (entry)
495 entry->hook (node, node2, entry->data);
496 entry = entry->next;
500 /* Return cgraph node assigned to DECL. Create new one when needed. */
502 cgraph_node *
503 cgraph_node::create (tree decl)
505 cgraph_node *node = symtab->create_empty ();
506 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
508 node->decl = decl;
510 if ((flag_openacc || flag_openmp)
511 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
513 node->offloadable = 1;
514 if (ENABLE_OFFLOADING)
515 g->have_offload = true;
518 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
519 node->ifunc_resolver = true;
521 node->register_symbol ();
522 maybe_record_nested_function (node);
524 return node;
527 /* Try to find a call graph node for declaration DECL and if it does not exist
528 or if it corresponds to an inline clone, create a new one. */
530 cgraph_node *
531 cgraph_node::get_create (tree decl)
533 cgraph_node *first_clone = cgraph_node::get (decl);
535 if (first_clone && !first_clone->inlined_to)
536 return first_clone;
538 cgraph_node *node = cgraph_node::create (decl);
539 if (first_clone)
541 first_clone->clone_of = node;
542 node->clones = first_clone;
543 node->order = first_clone->order;
544 symtab->symtab_prevail_in_asm_name_hash (node);
545 node->decl->decl_with_vis.symtab_node = node;
546 if (dump_file)
547 fprintf (dump_file, "Introduced new external node "
548 "(%s) and turned into root of the clone tree.\n",
549 node->dump_name ());
551 else if (dump_file)
552 fprintf (dump_file, "Introduced new external node "
553 "(%s).\n", node->dump_name ());
554 return node;
557 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
558 the function body is associated with
559 (not necessarily cgraph_node (DECL)). */
561 cgraph_node *
562 cgraph_node::create_alias (tree alias, tree target)
564 cgraph_node *alias_node;
566 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
567 || TREE_CODE (target) == IDENTIFIER_NODE);
568 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
569 alias_node = cgraph_node::get_create (alias);
570 gcc_assert (!alias_node->definition);
571 alias_node->alias_target = target;
572 alias_node->definition = true;
573 alias_node->alias = true;
574 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
575 alias_node->transparent_alias = alias_node->weakref = true;
576 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
577 alias_node->ifunc_resolver = true;
578 return alias_node;
581 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
582 and NULL otherwise.
583 Same body aliases are output whenever the body of DECL is output,
584 and cgraph_node::get (ALIAS) transparently returns
585 cgraph_node::get (DECL). */
587 cgraph_node *
588 cgraph_node::create_same_body_alias (tree alias, tree decl)
590 cgraph_node *n;
592 /* If aliases aren't supported by the assembler, fail. */
593 if (!TARGET_SUPPORTS_ALIASES)
594 return NULL;
596 /* Langhooks can create same body aliases of symbols not defined.
597 Those are useless. Drop them on the floor. */
598 if (symtab->global_info_ready)
599 return NULL;
601 n = cgraph_node::create_alias (alias, decl);
602 n->cpp_implicit_alias = true;
603 if (symtab->cpp_implicit_aliases_done)
604 n->resolve_alias (cgraph_node::get (decl));
605 return n;
608 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
609 aliases DECL with an adjustments made into the first parameter.
610 See comments in struct cgraph_thunk_info for detail on the parameters. */
612 cgraph_node *
613 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
614 HOST_WIDE_INT fixed_offset,
615 HOST_WIDE_INT virtual_value,
616 HOST_WIDE_INT indirect_offset,
617 tree virtual_offset,
618 tree real_alias)
620 cgraph_node *node;
622 node = cgraph_node::get (alias);
623 if (node)
624 node->reset ();
625 else
626 node = cgraph_node::create (alias);
628 /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE. */
629 gcc_checking_assert (virtual_offset
630 ? virtual_value == wi::to_wide (virtual_offset)
631 : virtual_value == 0);
633 node->thunk = true;
634 node->definition = true;
636 thunk_info *i = thunk_info::get_create (node);
637 i->fixed_offset = fixed_offset;
638 i->virtual_value = virtual_value;
639 i->indirect_offset = indirect_offset;
640 i->alias = real_alias;
641 i->this_adjusting = this_adjusting;
642 i->virtual_offset_p = virtual_offset != NULL;
644 return node;
647 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
648 Return NULL if there's no such node. */
650 cgraph_node *
651 cgraph_node::get_for_asmname (tree asmname)
653 /* We do not want to look at inline clones. */
654 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
655 node;
656 node = node->next_sharing_asm_name)
658 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
659 if (cn && !cn->inlined_to)
660 return cn;
662 return NULL;
665 /* Returns a hash value for X (which really is a cgraph_edge). */
667 hashval_t
668 cgraph_edge_hasher::hash (cgraph_edge *e)
670 /* This is a really poor hash function, but it is what htab_hash_pointer
671 uses. */
672 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
675 /* Returns a hash value for X (which really is a cgraph_edge). */
677 hashval_t
678 cgraph_edge_hasher::hash (gimple *call_stmt)
680 /* This is a really poor hash function, but it is what htab_hash_pointer
681 uses. */
682 return (hashval_t) ((intptr_t)call_stmt >> 3);
685 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
687 inline bool
688 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
690 return x->call_stmt == y;
693 /* Add call graph edge E to call site hash of its caller. */
695 static inline void
696 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
698 gimple *call = e->call_stmt;
699 *e->caller->call_site_hash->find_slot_with_hash
700 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
703 /* Add call graph edge E to call site hash of its caller. */
705 static inline void
706 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
708 /* There are two speculative edges for every statement (one direct,
709 one indirect); always hash the direct one. */
710 if (e->speculative && e->indirect_unknown_callee)
711 return;
712 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
713 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
714 if (*slot)
716 gcc_assert (((cgraph_edge *)*slot)->speculative);
717 if (e->callee && (!e->prev_callee
718 || !e->prev_callee->speculative
719 || e->prev_callee->call_stmt != e->call_stmt))
720 *slot = e;
721 return;
723 gcc_assert (!*slot || e->speculative);
724 *slot = e;
727 /* Return the callgraph edge representing the GIMPLE_CALL statement
728 CALL_STMT. */
730 cgraph_edge *
731 cgraph_node::get_edge (gimple *call_stmt)
733 cgraph_edge *e, *e2;
734 int n = 0;
736 if (call_site_hash)
737 return call_site_hash->find_with_hash
738 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
740 /* This loop may turn out to be performance problem. In such case adding
741 hashtables into call nodes with very many edges is probably best
742 solution. It is not good idea to add pointer into CALL_EXPR itself
743 because we want to make possible having multiple cgraph nodes representing
744 different clones of the same body before the body is actually cloned. */
745 for (e = callees; e; e = e->next_callee)
747 if (e->call_stmt == call_stmt)
748 break;
749 n++;
752 if (!e)
753 for (e = indirect_calls; e; e = e->next_callee)
755 if (e->call_stmt == call_stmt)
756 break;
757 n++;
760 if (n > 100)
762 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
763 for (e2 = callees; e2; e2 = e2->next_callee)
764 cgraph_add_edge_to_call_site_hash (e2);
765 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
766 cgraph_add_edge_to_call_site_hash (e2);
769 return e;
773 /* Change field call_stmt of edge E to NEW_STMT. If UPDATE_SPECULATIVE and E
774 is any component of speculative edge, then update all components.
775 Speculations can be resolved in the process and EDGE can be removed and
776 deallocated. Return the edge that now represents the call. */
778 cgraph_edge *
779 cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
780 bool update_speculative)
782 tree decl;
784 /* Speculative edges has three component, update all of them
785 when asked to. */
786 if (update_speculative && e->speculative)
788 cgraph_edge *direct, *indirect, *next;
789 ipa_ref *ref;
790 bool e_indirect = e->indirect_unknown_callee;
791 int n = 0;
793 direct = e->first_speculative_call_target ();
794 indirect = e->speculative_call_indirect_edge ();
796 gcall *old_stmt = direct->call_stmt;
797 for (cgraph_edge *d = direct; d; d = next)
799 next = d->next_speculative_call_target ();
800 cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
801 gcc_assert (d2 == d);
802 n++;
804 gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
805 for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
806 if (ref->speculative && ref->stmt == old_stmt)
808 ref->stmt = new_stmt;
809 n--;
812 indirect = set_call_stmt (indirect, new_stmt, false);
813 return e_indirect ? indirect : direct;
816 /* Only direct speculative edges go to call_site_hash. */
817 if (e->caller->call_site_hash
818 && (!e->speculative || !e->indirect_unknown_callee)
819 /* It is possible that edge was previously speculative. In this case
820 we have different value in call stmt hash which needs preserving. */
821 && e->caller->get_edge (e->call_stmt) == e)
822 e->caller->call_site_hash->remove_elt_with_hash
823 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
825 e->call_stmt = new_stmt;
826 if (e->indirect_unknown_callee
827 && (decl = gimple_call_fndecl (new_stmt)))
829 /* Constant propagation (and possibly also inlining?) can turn an
830 indirect call into a direct one. */
831 cgraph_node *new_callee = cgraph_node::get (decl);
833 gcc_checking_assert (new_callee);
834 e = make_direct (e, new_callee);
837 function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
838 e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
839 /* Update call stite hash. For speculative calls we only record the first
840 direct edge. */
841 if (e->caller->call_site_hash
842 && (!e->speculative
843 || (e->callee
844 && (!e->prev_callee || !e->prev_callee->speculative
845 || e->prev_callee->call_stmt != e->call_stmt))
846 || (e->speculative && !e->callee)))
847 cgraph_add_edge_to_call_site_hash (e);
848 return e;
851 /* Allocate a cgraph_edge structure and fill it with data according to the
852 parameters of which only CALLEE can be NULL (when creating an indirect call
853 edge). CLONING_P should be set if properties that are copied from an
854 original edge should not be calculated. */
856 cgraph_edge *
857 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
858 gcall *call_stmt, profile_count count,
859 bool indir_unknown_callee, bool cloning_p)
861 cgraph_edge *edge;
863 /* LTO does not actually have access to the call_stmt since these
864 have not been loaded yet. */
865 if (call_stmt)
867 /* This is a rather expensive check possibly triggering
868 construction of call stmt hashtable. */
869 cgraph_edge *e;
870 gcc_checking_assert (!(e = caller->get_edge (call_stmt))
871 || e->speculative);
873 gcc_assert (is_gimple_call (call_stmt));
876 edge = ggc_alloc<cgraph_edge> ();
877 edge->m_summary_id = -1;
878 edges_count++;
880 gcc_assert (++edges_max_uid != 0);
881 edge->m_uid = edges_max_uid;
882 edge->aux = NULL;
883 edge->caller = caller;
884 edge->callee = callee;
885 edge->prev_caller = NULL;
886 edge->next_caller = NULL;
887 edge->prev_callee = NULL;
888 edge->next_callee = NULL;
889 edge->lto_stmt_uid = 0;
890 edge->speculative_id = 0;
892 edge->count = count;
893 edge->call_stmt = call_stmt;
894 edge->indirect_info = NULL;
895 edge->indirect_inlining_edge = 0;
896 edge->speculative = false;
897 edge->indirect_unknown_callee = indir_unknown_callee;
898 if (call_stmt && caller->call_site_hash)
899 cgraph_add_edge_to_call_site_hash (edge);
901 if (cloning_p)
902 return edge;
904 edge->can_throw_external
905 = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
906 call_stmt) : false;
907 edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
908 edge->call_stmt_cannot_inline_p = false;
910 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
911 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
912 edge->in_polymorphic_cdtor
913 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
914 caller->decl);
915 else
916 edge->in_polymorphic_cdtor = caller->thunk;
917 if (callee)
918 caller->calls_declare_variant_alt |= callee->declare_variant_alt;
920 if (callee && symtab->state != LTO_STREAMING
921 && edge->callee->comdat_local_p ())
922 edge->caller->calls_comdat_local = true;
924 return edge;
927 /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
928 be set if properties that are copied from an original edge should not be
929 calculated. */
931 cgraph_edge *
932 cgraph_node::create_edge (cgraph_node *callee,
933 gcall *call_stmt, profile_count count, bool cloning_p)
935 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
936 false, cloning_p);
938 if (!cloning_p)
939 initialize_inline_failed (edge);
941 edge->next_caller = callee->callers;
942 if (callee->callers)
943 callee->callers->prev_caller = edge;
944 edge->next_callee = callees;
945 if (callees)
946 callees->prev_callee = edge;
947 callees = edge;
948 callee->callers = edge;
950 return edge;
953 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
955 cgraph_indirect_call_info *
956 cgraph_allocate_init_indirect_info (void)
958 cgraph_indirect_call_info *ii;
960 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
961 ii->param_index = -1;
962 return ii;
965 /* Create an indirect edge with a yet-undetermined callee where the call
966 statement destination is a formal parameter of the caller with index
967 PARAM_INDEX. CLONING_P should be set if properties that are copied from an
968 original edge should not be calculated and indirect_info structure should
969 not be calculated. */
971 cgraph_edge *
972 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
973 profile_count count,
974 bool cloning_p)
976 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
977 cloning_p);
978 tree target;
980 if (!cloning_p)
981 initialize_inline_failed (edge);
983 edge->indirect_info = cgraph_allocate_init_indirect_info ();
984 edge->indirect_info->ecf_flags = ecf_flags;
985 edge->indirect_info->vptr_changed = true;
987 /* Record polymorphic call info. */
988 if (!cloning_p
989 && call_stmt
990 && (target = gimple_call_fn (call_stmt))
991 && virtual_method_call_p (target))
993 ipa_polymorphic_call_context context (decl, target, call_stmt);
995 /* Only record types can have virtual calls. */
996 edge->indirect_info->polymorphic = true;
997 edge->indirect_info->param_index = -1;
998 edge->indirect_info->otr_token
999 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
1000 edge->indirect_info->otr_type = obj_type_ref_class (target);
1001 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
1002 edge->indirect_info->context = context;
1005 edge->next_callee = indirect_calls;
1006 if (indirect_calls)
1007 indirect_calls->prev_callee = edge;
1008 indirect_calls = edge;
1010 return edge;
1013 /* Remove the edge from the list of the callees of the caller. */
1015 void
1016 cgraph_edge::remove_caller (void)
1018 if (prev_callee)
1019 prev_callee->next_callee = next_callee;
1020 if (next_callee)
1021 next_callee->prev_callee = prev_callee;
1022 if (!prev_callee)
1024 if (indirect_unknown_callee)
1025 caller->indirect_calls = next_callee;
1026 else
1027 caller->callees = next_callee;
1029 if (caller->call_site_hash
1030 && this == caller->get_edge (call_stmt))
1031 caller->call_site_hash->remove_elt_with_hash
1032 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1035 /* Put the edge onto the free list. */
1037 void
1038 symbol_table::free_edge (cgraph_edge *e)
1040 edges_count--;
1041 if (e->m_summary_id != -1)
1042 edge_released_summary_ids.safe_push (e->m_summary_id);
1044 if (e->indirect_info)
1045 ggc_free (e->indirect_info);
1046 ggc_free (e);
1049 /* Remove the edge in the cgraph. */
1051 void
1052 cgraph_edge::remove (cgraph_edge *edge)
1054 /* Call all edge removal hooks. */
1055 symtab->call_edge_removal_hooks (edge);
1057 if (!edge->indirect_unknown_callee)
1058 /* Remove from callers list of the callee. */
1059 edge->remove_callee ();
1061 /* Remove from callees list of the callers. */
1062 edge->remove_caller ();
1064 /* Put the edge onto the free list. */
1065 symtab->free_edge (edge);
1068 /* Turn edge into speculative call calling N2. Update
1069 the profile so the direct call is taken COUNT times
1070 with FREQUENCY.
1072 At clone materialization time, the indirect call E will
1073 be expanded as:
1075 if (call_dest == N2)
1076 n2 ();
1077 else
1078 call call_dest
1080 At this time the function just creates the direct call,
1081 the reference representing the if conditional and attaches
1082 them all to the original indirect call statement.
1084 speculative_id is used to link direct calls with their corresponding
1085 IPA_REF_ADDR references when representing speculative calls.
1087 Return direct edge created. */
1089 cgraph_edge *
1090 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1091 unsigned int speculative_id)
1093 cgraph_node *n = caller;
1094 ipa_ref *ref = NULL;
1095 cgraph_edge *e2;
1097 if (dump_file)
1098 fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1099 n->dump_name (), n2->dump_name ());
1100 speculative = true;
1101 e2 = n->create_edge (n2, call_stmt, direct_count);
1102 initialize_inline_failed (e2);
1103 e2->speculative = true;
1104 if (TREE_NOTHROW (n2->decl))
1105 e2->can_throw_external = false;
1106 else
1107 e2->can_throw_external = can_throw_external;
1108 e2->lto_stmt_uid = lto_stmt_uid;
1109 e2->speculative_id = speculative_id;
1110 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1111 indirect_info->num_speculative_call_targets++;
1112 count -= e2->count;
1113 symtab->call_edge_duplication_hooks (this, e2);
1114 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1115 ref->lto_stmt_uid = lto_stmt_uid;
1116 ref->speculative_id = speculative_id;
1117 ref->speculative = speculative;
1118 n2->mark_address_taken ();
1119 return e2;
1122 /* Speculative call consists of an indirect edge and one or more
1123 direct edge+ref pairs.
1125 Given an edge which is part of speculative call, return the first
1126 direct call edge in the speculative call sequence. */
1128 cgraph_edge *
1129 cgraph_edge::first_speculative_call_target ()
1131 cgraph_edge *e = this;
1133 gcc_checking_assert (e->speculative);
1134 if (e->callee)
1136 while (e->prev_callee && e->prev_callee->speculative
1137 && e->prev_callee->call_stmt == e->call_stmt
1138 && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1139 e = e->prev_callee;
1140 return e;
1142 /* Call stmt site hash always points to the first target of the
1143 speculative call sequence. */
1144 if (e->call_stmt)
1145 return e->caller->get_edge (e->call_stmt);
1146 for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1147 if (e2->speculative
1148 && e->call_stmt == e2->call_stmt
1149 && e->lto_stmt_uid == e2->lto_stmt_uid)
1150 return e2;
1153 /* We always maintain first direct edge in the call site hash, if one
1154 exists. E is going to be removed. See if it is first one and update
1155 hash accordingly. INDIRECT is the indirect edge of speculative call.
1156 We assume that INDIRECT->num_speculative_call_targets_p () is already
1157 updated for removal of E. */
1158 static void
1159 update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1160 cgraph_edge *indirect)
1162 if (e->caller->call_site_hash)
1164 if (e->caller->get_edge (e->call_stmt) != e)
1166 else if (!indirect->num_speculative_call_targets_p ())
1167 cgraph_update_edge_in_call_site_hash (indirect);
1168 else
1170 gcc_checking_assert (e->next_callee && e->next_callee->speculative
1171 && e->next_callee->call_stmt == e->call_stmt);
1172 cgraph_update_edge_in_call_site_hash (e->next_callee);
1177 /* Speculative call EDGE turned out to be direct call to CALLEE_DECL. Remove
1178 the speculative call sequence and return edge representing the call, the
1179 original EDGE can be removed and deallocated. Return the edge that now
1180 represents the call.
1182 For "speculative" indirect call that contains multiple "speculative"
1183 targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1184 decrease the count and only remove current direct edge.
1186 If no speculative direct call left to the speculative indirect call, remove
1187 the speculative of both the indirect call and corresponding direct edge.
1189 It is up to caller to iteratively resolve each "speculative" direct call and
1190 redirect the call as appropriate. */
1192 cgraph_edge *
1193 cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1195 cgraph_edge *e2;
1196 ipa_ref *ref;
1198 gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1199 if (!edge->callee)
1200 e2 = edge->first_speculative_call_target ();
1201 else
1202 e2 = edge;
1203 ref = e2->speculative_call_target_ref ();
1204 edge = edge->speculative_call_indirect_edge ();
1205 if (!callee_decl
1206 || !ref->referred->semantically_equivalent_p
1207 (symtab_node::get (callee_decl)))
1209 if (dump_file)
1211 if (callee_decl)
1213 fprintf (dump_file, "Speculative indirect call %s => %s has "
1214 "turned out to have contradicting known target ",
1215 edge->caller->dump_name (),
1216 e2->callee->dump_name ());
1217 print_generic_expr (dump_file, callee_decl);
1218 fprintf (dump_file, "\n");
1220 else
1222 fprintf (dump_file, "Removing speculative call %s => %s\n",
1223 edge->caller->dump_name (),
1224 e2->callee->dump_name ());
1228 else
1230 cgraph_edge *tmp = edge;
1231 if (dump_file)
1232 fprintf (dump_file, "Speculative call turned into direct call.\n");
1233 edge = e2;
1234 e2 = tmp;
1235 /* FIXME: If EDGE is inlined, we should scale up the frequencies
1236 and counts in the functions inlined through it. */
1238 edge->count += e2->count;
1239 if (edge->num_speculative_call_targets_p ())
1241 /* The indirect edge has multiple speculative targets, don't remove
1242 speculative until all related direct edges are resolved. */
1243 edge->indirect_info->num_speculative_call_targets--;
1244 if (!edge->indirect_info->num_speculative_call_targets)
1245 edge->speculative = false;
1247 else
1248 edge->speculative = false;
1249 e2->speculative = false;
1250 update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1251 ref->remove_reference ();
1252 if (e2->indirect_unknown_callee || e2->inline_failed)
1253 remove (e2);
1254 else
1255 e2->callee->remove_symbol_and_inline_clones ();
1256 return edge;
1259 /* Return edge corresponding to speculative call to a given target.
1260 NULL if speculative call does not have one. */
1262 cgraph_edge *
1263 cgraph_edge::speculative_call_for_target (cgraph_node *target)
1265 for (cgraph_edge *direct = first_speculative_call_target ();
1266 direct;
1267 direct = direct->next_speculative_call_target ())
1268 if (direct->speculative_call_target_ref ()
1269 ->referred->semantically_equivalent_p (target))
1270 return direct;
1271 return NULL;
1274 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1275 CALLEE. Speculations can be resolved in the process and EDGE can be removed
1276 and deallocated. Return the edge that now represents the call. */
1278 cgraph_edge *
1279 cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1281 gcc_assert (edge->indirect_unknown_callee);
1283 /* If we are redirecting speculative call, make it non-speculative. */
1284 if (edge->speculative)
1286 cgraph_edge *found = NULL;
1287 cgraph_edge *direct, *next;
1289 edge = edge->speculative_call_indirect_edge ();
1291 /* Look all speculative targets and remove all but one corresponding
1292 to callee (if it exists). */
1293 for (direct = edge->first_speculative_call_target ();
1294 direct;
1295 direct = next)
1297 next = direct->next_speculative_call_target ();
1299 /* Compare ref not direct->callee. Direct edge is possibly
1300 inlined or redirected. */
1301 if (!direct->speculative_call_target_ref ()
1302 ->referred->semantically_equivalent_p (callee))
1303 edge = direct->resolve_speculation (direct, NULL);
1304 else
1306 gcc_checking_assert (!found);
1307 found = direct;
1311 /* On successful speculation just remove the indirect edge and
1312 return the pre existing direct edge.
1313 It is important to not remove it and redirect because the direct
1314 edge may be inlined or redirected. */
1315 if (found)
1317 cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1318 gcc_checking_assert (!found->speculative && e2 == found);
1319 return found;
1321 gcc_checking_assert (!edge->speculative);
1324 edge->indirect_unknown_callee = 0;
1325 ggc_free (edge->indirect_info);
1326 edge->indirect_info = NULL;
1328 /* Get the edge out of the indirect edge list. */
1329 if (edge->prev_callee)
1330 edge->prev_callee->next_callee = edge->next_callee;
1331 if (edge->next_callee)
1332 edge->next_callee->prev_callee = edge->prev_callee;
1333 if (!edge->prev_callee)
1334 edge->caller->indirect_calls = edge->next_callee;
1336 /* Put it into the normal callee list */
1337 edge->prev_callee = NULL;
1338 edge->next_callee = edge->caller->callees;
1339 if (edge->caller->callees)
1340 edge->caller->callees->prev_callee = edge;
1341 edge->caller->callees = edge;
1343 /* Insert to callers list of the new callee. */
1344 edge->set_callee (callee);
1346 /* We need to re-determine the inlining status of the edge. */
1347 initialize_inline_failed (edge);
1348 return edge;
1351 /* Redirect callee of the edge to N. The function does not update underlying
1352 call expression. */
1354 void
1355 cgraph_edge::redirect_callee (cgraph_node *n)
1357 bool loc = callee->comdat_local_p ();
1358 /* Remove from callers list of the current callee. */
1359 remove_callee ();
1361 /* Insert to callers list of the new callee. */
1362 set_callee (n);
1364 if (!inline_failed)
1365 return;
1366 if (!loc && n->comdat_local_p ())
1368 cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1369 to->calls_comdat_local = true;
1371 else if (loc && !n->comdat_local_p ())
1373 cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1374 gcc_checking_assert (to->calls_comdat_local);
1375 to->calls_comdat_local = to->check_calls_comdat_local_p ();
1379 /* If necessary, change the function declaration in the call statement
1380 associated with E so that it corresponds to the edge callee. Speculations
1381 can be resolved in the process and EDGE can be removed and deallocated.
1383 The edge could be one of speculative direct call generated from speculative
1384 indirect call. In this circumstance, decrease the speculative targets
1385 count (i.e. num_speculative_call_targets) and redirect call stmt to the
1386 corresponding i-th target. If no speculative direct call left to the
1387 speculative indirect call, remove "speculative" of the indirect call and
1388 also redirect stmt to it's final direct target.
1390 It is up to caller to iteratively transform each "speculative"
1391 direct call as appropriate. */
1393 gimple *
1394 cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e)
1396 tree decl = gimple_call_fndecl (e->call_stmt);
1397 gcall *new_stmt;
1398 gimple_stmt_iterator gsi;
1400 if (e->speculative)
1402 /* If there already is an direct call (i.e. as a result of inliner's
1403 substitution), forget about speculating. */
1404 if (decl)
1405 e = make_direct (e->speculative_call_indirect_edge (),
1406 cgraph_node::get (decl));
1407 else
1409 /* Be sure we redirect all speculative targets before poking
1410 abou tindirect edge. */
1411 gcc_checking_assert (e->callee);
1412 cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1413 gcall *new_stmt;
1414 ipa_ref *ref;
1416 /* Expand speculation into GIMPLE code. */
1417 if (dump_file)
1419 fprintf (dump_file,
1420 "Expanding speculative call of %s -> %s count: ",
1421 e->caller->dump_name (),
1422 e->callee->dump_name ());
1423 e->count.dump (dump_file);
1424 fprintf (dump_file, "\n");
1426 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1428 profile_count all = indirect->count;
1429 for (cgraph_edge *e2 = e->first_speculative_call_target ();
1431 e2 = e2->next_speculative_call_target ())
1432 all = all + e2->count;
1433 profile_probability prob = e->count.probability_in (all);
1434 if (!prob.initialized_p ())
1435 prob = profile_probability::even ();
1436 ref = e->speculative_call_target_ref ();
1437 new_stmt = gimple_ic (e->call_stmt,
1438 dyn_cast<cgraph_node *> (ref->referred),
1439 prob);
1440 e->speculative = false;
1441 if (indirect->num_speculative_call_targets_p ())
1443 /* The indirect edge has multiple speculative targets, don't
1444 remove speculative until all related direct edges are
1445 redirected. */
1446 indirect->indirect_info->num_speculative_call_targets--;
1447 if (!indirect->indirect_info->num_speculative_call_targets)
1448 indirect->speculative = false;
1450 else
1451 indirect->speculative = false;
1452 /* Indirect edges are not both in the call site hash.
1453 get it updated. */
1454 update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1455 cgraph_edge::set_call_stmt (e, new_stmt, false);
1456 e->count = gimple_bb (e->call_stmt)->count;
1458 /* Once we are done with expanding the sequence, update also indirect
1459 call probability. Until then the basic block accounts for the
1460 sum of indirect edge and all non-expanded speculations. */
1461 if (!indirect->speculative)
1462 indirect->count = gimple_bb (indirect->call_stmt)->count;
1463 ref->speculative = false;
1464 ref->stmt = NULL;
1465 pop_cfun ();
1466 /* Continue redirecting E to proper target. */
1471 if (e->indirect_unknown_callee
1472 || decl == e->callee->decl)
1473 return e->call_stmt;
1475 if (decl && ipa_saved_clone_sources)
1477 tree *p = ipa_saved_clone_sources->get (e->callee);
1478 if (p && decl == *p)
1480 gimple_call_set_fndecl (e->call_stmt, e->callee->decl);
1481 return e->call_stmt;
1485 if (flag_checking && decl)
1487 cgraph_node *node = cgraph_node::get (decl);
1488 gcc_assert (!node || !node->clone.param_adjustments);
1491 if (symtab->dump_file)
1493 fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1494 e->caller->dump_name (), e->callee->dump_name ());
1495 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1496 if (e->callee->clone.param_adjustments)
1497 e->callee->clone.param_adjustments->dump (symtab->dump_file);
1498 unsigned performed_len
1499 = vec_safe_length (e->caller->clone.performed_splits);
1500 if (performed_len > 0)
1501 fprintf (symtab->dump_file, "Performed splits records:\n");
1502 for (unsigned i = 0; i < performed_len; i++)
1504 ipa_param_performed_split *sm
1505 = &(*e->caller->clone.performed_splits)[i];
1506 print_node_brief (symtab->dump_file, " dummy_decl: ", sm->dummy_decl,
1507 TDF_UID);
1508 fprintf (symtab->dump_file, ", unit_offset: %u\n", sm->unit_offset);
1512 if (ipa_param_adjustments *padjs = e->callee->clone.param_adjustments)
1514 /* We need to defer cleaning EH info on the new statement to
1515 fixup-cfg. We may not have dominator information at this point
1516 and thus would end up with unreachable blocks and have no way
1517 to communicate that we need to run CFG cleanup then. */
1518 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1519 if (lp_nr != 0)
1520 remove_stmt_from_eh_lp (e->call_stmt);
1522 tree old_fntype = gimple_call_fntype (e->call_stmt);
1523 new_stmt = padjs->modify_call (e->call_stmt,
1524 e->caller->clone.performed_splits,
1525 e->callee->decl, false);
1526 cgraph_node *origin = e->callee;
1527 while (origin->clone_of)
1528 origin = origin->clone_of;
1530 if ((origin->former_clone_of
1531 && old_fntype == TREE_TYPE (origin->former_clone_of))
1532 || old_fntype == TREE_TYPE (origin->decl))
1533 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1534 else
1536 tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1537 gimple_call_set_fntype (new_stmt, new_fntype);
1540 if (lp_nr != 0)
1541 add_stmt_to_eh_lp (new_stmt, lp_nr);
1543 else
1545 new_stmt = e->call_stmt;
1546 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1547 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1550 /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1551 adjust gimple_call_fntype too. */
1552 if (gimple_call_noreturn_p (new_stmt)
1553 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1554 && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1555 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1556 == void_type_node))
1557 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1559 /* If the call becomes noreturn, remove the LHS if possible. */
1560 tree lhs = gimple_call_lhs (new_stmt);
1561 if (lhs
1562 && gimple_call_noreturn_p (new_stmt)
1563 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1564 || should_remove_lhs_p (lhs)))
1566 if (TREE_CODE (lhs) == SSA_NAME)
1568 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1569 TREE_TYPE (lhs), NULL);
1570 var = get_or_create_ssa_default_def
1571 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1572 gimple *set_stmt = gimple_build_assign (lhs, var);
1573 gsi = gsi_for_stmt (new_stmt);
1574 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1575 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1577 gimple_call_set_lhs (new_stmt, NULL_TREE);
1578 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1581 /* If new callee has no static chain, remove it. */
1582 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1584 gimple_call_set_chain (new_stmt, NULL);
1585 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1588 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1589 new_stmt);
1591 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1593 if (symtab->dump_file)
1595 fprintf (symtab->dump_file, " updated to:");
1596 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1598 return new_stmt;
1601 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1602 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1603 of OLD_STMT if it was previously call statement.
1604 If NEW_STMT is NULL, the call has been dropped without any
1605 replacement. */
1607 static void
1608 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1609 gimple *old_stmt, tree old_call,
1610 gimple *new_stmt)
1612 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1613 ? gimple_call_fndecl (new_stmt) : 0;
1615 /* We are seeing indirect calls, then there is nothing to update. */
1616 if (!new_call && !old_call)
1617 return;
1618 /* See if we turned indirect call into direct call or folded call to one builtin
1619 into different builtin. */
1620 if (old_call != new_call)
1622 cgraph_edge *e = node->get_edge (old_stmt);
1623 cgraph_edge *ne = NULL;
1624 profile_count count;
1626 if (e)
1628 /* Keep calls marked as dead dead. */
1629 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1630 && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
1632 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1633 as_a <gcall *> (new_stmt));
1634 return;
1636 /* See if the edge is already there and has the correct callee. It
1637 might be so because of indirect inlining has already updated
1638 it. We also might've cloned and redirected the edge. */
1639 if (new_call && e->callee)
1641 cgraph_node *callee = e->callee;
1642 while (callee)
1644 if (callee->decl == new_call
1645 || callee->former_clone_of == new_call)
1647 cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
1648 return;
1650 callee = callee->clone_of;
1654 /* Otherwise remove edge and create new one; we can't simply redirect
1655 since function has changed, so inline plan and other information
1656 attached to edge is invalid. */
1657 count = e->count;
1658 if (e->indirect_unknown_callee || e->inline_failed)
1659 cgraph_edge::remove (e);
1660 else
1661 e->callee->remove_symbol_and_inline_clones ();
1663 else if (new_call)
1665 /* We are seeing new direct call; compute profile info based on BB. */
1666 basic_block bb = gimple_bb (new_stmt);
1667 count = bb->count;
1670 if (new_call)
1672 ne = node->create_edge (cgraph_node::get_create (new_call),
1673 as_a <gcall *> (new_stmt), count);
1674 gcc_assert (ne->inline_failed);
1677 /* We only updated the call stmt; update pointer in cgraph edge.. */
1678 else if (old_stmt != new_stmt)
1679 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1680 as_a <gcall *> (new_stmt));
1683 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1684 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1685 of OLD_STMT before it was updated (updating can happen inplace). */
1687 void
1688 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1689 gimple *new_stmt)
1691 cgraph_node *orig = cgraph_node::get (cfun->decl);
1692 cgraph_node *node;
1694 gcc_checking_assert (orig);
1695 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1696 if (orig->clones)
1697 for (node = orig->clones; node != orig;)
1699 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1700 if (node->clones)
1701 node = node->clones;
1702 else if (node->next_sibling_clone)
1703 node = node->next_sibling_clone;
1704 else
1706 while (node != orig && !node->next_sibling_clone)
1707 node = node->clone_of;
1708 if (node != orig)
1709 node = node->next_sibling_clone;
1715 /* Remove all callees from the node. */
1717 void
1718 cgraph_node::remove_callees (void)
1720 cgraph_edge *e, *f;
1722 calls_comdat_local = false;
1724 /* It is sufficient to remove the edges from the lists of callers of
1725 the callees. The callee list of the node can be zapped with one
1726 assignment. */
1727 for (e = callees; e; e = f)
1729 f = e->next_callee;
1730 symtab->call_edge_removal_hooks (e);
1731 if (!e->indirect_unknown_callee)
1732 e->remove_callee ();
1733 symtab->free_edge (e);
1735 for (e = indirect_calls; e; e = f)
1737 f = e->next_callee;
1738 symtab->call_edge_removal_hooks (e);
1739 if (!e->indirect_unknown_callee)
1740 e->remove_callee ();
1741 symtab->free_edge (e);
1743 indirect_calls = NULL;
1744 callees = NULL;
1745 if (call_site_hash)
1747 call_site_hash->empty ();
1748 call_site_hash = NULL;
1752 /* Remove all callers from the node. */
1754 void
1755 cgraph_node::remove_callers (void)
1757 cgraph_edge *e, *f;
1759 /* It is sufficient to remove the edges from the lists of callees of
1760 the callers. The caller list of the node can be zapped with one
1761 assignment. */
1762 for (e = callers; e; e = f)
1764 f = e->next_caller;
1765 symtab->call_edge_removal_hooks (e);
1766 e->remove_caller ();
1767 symtab->free_edge (e);
1769 callers = NULL;
1772 /* Helper function for cgraph_release_function_body and free_lang_data.
1773 It releases body from function DECL without having to inspect its
1774 possibly non-existent symtab node. */
1776 void
1777 release_function_body (tree decl)
1779 function *fn = DECL_STRUCT_FUNCTION (decl);
1780 if (fn)
1782 if (fn->cfg
1783 && loops_for_fn (fn))
1785 fn->curr_properties &= ~PROP_loops;
1786 loop_optimizer_finalize (fn);
1788 if (fn->gimple_df)
1790 delete_tree_ssa (fn);
1791 fn->eh = NULL;
1793 if (fn->cfg)
1795 gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1796 gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1797 delete_tree_cfg_annotations (fn);
1798 clear_edges (fn);
1799 fn->cfg = NULL;
1801 if (fn->value_histograms)
1802 free_histograms (fn);
1803 gimple_set_body (decl, NULL);
1804 /* Struct function hangs a lot of data that would leak if we didn't
1805 removed all pointers to it. */
1806 ggc_free (fn);
1807 DECL_STRUCT_FUNCTION (decl) = NULL;
1809 DECL_SAVED_TREE (decl) = NULL;
1812 /* Release memory used to represent body of function.
1813 Use this only for functions that are released before being translated to
1814 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1815 are free'd in final.c via free_after_compilation().
1816 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1818 void
1819 cgraph_node::release_body (bool keep_arguments)
1821 ipa_transforms_to_apply.release ();
1822 if (!used_as_abstract_origin && symtab->state != PARSING)
1824 DECL_RESULT (decl) = NULL;
1826 if (!keep_arguments)
1827 DECL_ARGUMENTS (decl) = NULL;
1829 /* If the node is abstract and needed, then do not clear
1830 DECL_INITIAL of its associated function declaration because it's
1831 needed to emit debug info later. */
1832 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1833 DECL_INITIAL (decl) = error_mark_node;
1834 release_function_body (decl);
1835 if (lto_file_data)
1837 lto_free_function_in_decl_state_for_node (this);
1838 lto_file_data = NULL;
1842 /* Remove function from symbol table. */
1844 void
1845 cgraph_node::remove (void)
1847 if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1848 fprintf (symtab->ipa_clones_dump_file,
1849 "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1850 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1851 DECL_SOURCE_COLUMN (decl));
1853 symtab->call_cgraph_removal_hooks (this);
1854 remove_callers ();
1855 remove_callees ();
1856 ipa_transforms_to_apply.release ();
1857 delete_function_version (function_version ());
1859 /* Incremental inlining access removed nodes stored in the postorder list.
1861 force_output = false;
1862 forced_by_abi = false;
1864 unregister ();
1865 if (prev_sibling_clone)
1866 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1867 else if (clone_of)
1868 clone_of->clones = next_sibling_clone;
1869 if (next_sibling_clone)
1870 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1871 if (clones)
1873 cgraph_node *n, *next;
1875 if (clone_of)
1877 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1878 n->clone_of = clone_of;
1879 n->clone_of = clone_of;
1880 n->next_sibling_clone = clone_of->clones;
1881 if (clone_of->clones)
1882 clone_of->clones->prev_sibling_clone = n;
1883 clone_of->clones = clones;
1885 else
1887 /* We are removing node with clones. This makes clones inconsistent,
1888 but assume they will be removed subsequently and just keep clone
1889 tree intact. This can happen in unreachable function removal since
1890 we remove unreachable functions in random order, not by bottom-up
1891 walk of clone trees. */
1892 for (n = clones; n; n = next)
1894 next = n->next_sibling_clone;
1895 n->next_sibling_clone = NULL;
1896 n->prev_sibling_clone = NULL;
1897 n->clone_of = NULL;
1902 /* While all the clones are removed after being proceeded, the function
1903 itself is kept in the cgraph even after it is compiled. Check whether
1904 we are done with this body and reclaim it proactively if this is the case.
1906 if (symtab->state != LTO_STREAMING)
1908 cgraph_node *n = cgraph_node::get (decl);
1909 if (!n
1910 || (!n->clones && !n->clone_of && !n->inlined_to
1911 && ((symtab->global_info_ready || in_lto_p)
1912 && (TREE_ASM_WRITTEN (n->decl)
1913 || DECL_EXTERNAL (n->decl)
1914 || !n->analyzed
1915 || (!flag_wpa && n->in_other_partition)))))
1916 release_body ();
1918 else
1920 lto_free_function_in_decl_state_for_node (this);
1921 lto_file_data = NULL;
1924 decl = NULL;
1925 if (call_site_hash)
1927 call_site_hash->empty ();
1928 call_site_hash = NULL;
1931 symtab->release_symbol (this);
1934 /* Likewise indicate that a node is having address taken. */
1936 void
1937 cgraph_node::mark_address_taken (void)
1939 /* Indirect inlining can figure out that all uses of the address are
1940 inlined. */
1941 if (inlined_to)
1943 gcc_assert (cfun->after_inlining);
1944 gcc_assert (callers->indirect_inlining_edge);
1945 return;
1947 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1948 IPA_REF_ADDR reference exists (and thus it should be set on node
1949 representing alias we take address of) and as a test whether address
1950 of the object was taken (and thus it should be set on node alias is
1951 referring to). We should remove the first use and the remove the
1952 following set. */
1953 address_taken = 1;
1954 cgraph_node *node = ultimate_alias_target ();
1955 node->address_taken = 1;
1958 /* Return local info node for the compiled function. */
1960 cgraph_node *
1961 cgraph_node::local_info_node (tree decl)
1963 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1964 cgraph_node *node = get (decl);
1965 if (!node)
1966 return NULL;
1967 return node->ultimate_alias_target ();
1970 /* Return RTL info for the compiled function. */
1972 cgraph_rtl_info *
1973 cgraph_node::rtl_info (const_tree decl)
1975 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1976 cgraph_node *node = get (decl);
1977 if (!node)
1978 return NULL;
1979 enum availability avail;
1980 node = node->ultimate_alias_target (&avail);
1981 if (decl != current_function_decl
1982 && (avail < AVAIL_AVAILABLE
1983 || (node->decl != current_function_decl
1984 && !TREE_ASM_WRITTEN (node->decl))))
1985 return NULL;
1986 /* Allocate if it doesn't exist. */
1987 if (node->rtl == NULL)
1989 node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
1990 SET_HARD_REG_SET (node->rtl->function_used_regs);
1992 return node->rtl;
1995 /* Return a string describing the failure REASON. */
1997 const char*
1998 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
2000 #undef DEFCIFCODE
2001 #define DEFCIFCODE(code, type, string) string,
2003 static const char *cif_string_table[CIF_N_REASONS] = {
2004 #include "cif-code.def"
2007 /* Signedness of an enum type is implementation defined, so cast it
2008 to unsigned before testing. */
2009 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2010 return cif_string_table[reason];
2013 /* Return a type describing the failure REASON. */
2015 cgraph_inline_failed_type_t
2016 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2018 #undef DEFCIFCODE
2019 #define DEFCIFCODE(code, type, string) type,
2021 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2022 #include "cif-code.def"
2025 /* Signedness of an enum type is implementation defined, so cast it
2026 to unsigned before testing. */
2027 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2028 return cif_type_table[reason];
2031 /* Names used to print out the availability enum. */
2032 const char * const cgraph_availability_names[] =
2033 {"unset", "not_available", "overwritable", "available", "local"};
2035 /* Output flags of edge to a file F. */
2037 void
2038 cgraph_edge::dump_edge_flags (FILE *f)
2040 if (speculative)
2041 fprintf (f, "(speculative) ");
2042 if (!inline_failed)
2043 fprintf (f, "(inlined) ");
2044 if (call_stmt_cannot_inline_p)
2045 fprintf (f, "(call_stmt_cannot_inline_p) ");
2046 if (indirect_inlining_edge)
2047 fprintf (f, "(indirect_inlining) ");
2048 if (count.initialized_p ())
2050 fprintf (f, "(");
2051 count.dump (f);
2052 fprintf (f, ",");
2053 fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2055 if (can_throw_external)
2056 fprintf (f, "(can throw external) ");
2059 /* Dump edge to stderr. */
2061 void
2062 cgraph_edge::debug (void)
2064 fprintf (stderr, "%s -> %s ", caller->dump_asm_name (),
2065 callee == NULL ? "(null)" : callee->dump_asm_name ());
2066 dump_edge_flags (stderr);
2067 fprintf (stderr, "\n\n");
2068 caller->debug ();
2069 if (callee != NULL)
2070 callee->debug ();
2073 /* Dump call graph node to file F. */
2075 void
2076 cgraph_node::dump (FILE *f)
2078 cgraph_edge *edge;
2080 dump_base (f);
2082 if (inlined_to)
2083 fprintf (f, " Function %s is inline copy in %s\n",
2084 dump_name (),
2085 inlined_to->dump_name ());
2086 if (clone_of)
2087 fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2088 if (symtab->function_flags_ready)
2089 fprintf (f, " Availability: %s\n",
2090 cgraph_availability_names [get_availability ()]);
2092 if (profile_id)
2093 fprintf (f, " Profile id: %i\n",
2094 profile_id);
2095 if (unit_id)
2096 fprintf (f, " Unit id: %i\n",
2097 unit_id);
2098 cgraph_function_version_info *vi = function_version ();
2099 if (vi != NULL)
2101 fprintf (f, " Version info: ");
2102 if (vi->prev != NULL)
2104 fprintf (f, "prev: ");
2105 fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2107 if (vi->next != NULL)
2109 fprintf (f, "next: ");
2110 fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2112 if (vi->dispatcher_resolver != NULL_TREE)
2113 fprintf (f, "dispatcher: %s",
2114 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2116 fprintf (f, "\n");
2118 fprintf (f, " Function flags:");
2119 if (count.initialized_p ())
2121 fprintf (f, " count:");
2122 count.dump (f);
2124 if (tp_first_run > 0)
2125 fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2126 if (cgraph_node *origin = nested_function_origin (this))
2127 fprintf (f, " nested in:%s", origin->dump_asm_name ());
2128 if (gimple_has_body_p (decl))
2129 fprintf (f, " body");
2130 if (process)
2131 fprintf (f, " process");
2132 if (local)
2133 fprintf (f, " local");
2134 if (redefined_extern_inline)
2135 fprintf (f, " redefined_extern_inline");
2136 if (only_called_at_startup)
2137 fprintf (f, " only_called_at_startup");
2138 if (only_called_at_exit)
2139 fprintf (f, " only_called_at_exit");
2140 if (tm_clone)
2141 fprintf (f, " tm_clone");
2142 if (calls_comdat_local)
2143 fprintf (f, " calls_comdat_local");
2144 if (icf_merged)
2145 fprintf (f, " icf_merged");
2146 if (merged_comdat)
2147 fprintf (f, " merged_comdat");
2148 if (merged_extern_inline)
2149 fprintf (f, " merged_extern_inline");
2150 if (split_part)
2151 fprintf (f, " split_part");
2152 if (indirect_call_target)
2153 fprintf (f, " indirect_call_target");
2154 if (nonfreeing_fn)
2155 fprintf (f, " nonfreeing_fn");
2156 if (DECL_STATIC_CONSTRUCTOR (decl))
2157 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2158 if (DECL_STATIC_DESTRUCTOR (decl))
2159 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2160 if (frequency == NODE_FREQUENCY_HOT)
2161 fprintf (f, " hot");
2162 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2163 fprintf (f, " unlikely_executed");
2164 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2165 fprintf (f, " executed_once");
2166 if (opt_for_fn (decl, optimize_size))
2167 fprintf (f, " optimize_size");
2168 if (parallelized_function)
2169 fprintf (f, " parallelized_function");
2170 if (DECL_IS_OPERATOR_NEW_P (decl))
2171 fprintf (f, " %soperator_new",
2172 DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2173 if (DECL_IS_OPERATOR_DELETE_P (decl))
2174 fprintf (f, " %soperator_delete",
2175 DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2177 fprintf (f, "\n");
2179 if (thunk)
2181 fprintf (f, " Thunk");
2182 thunk_info::get (this)->dump (f);
2184 else if (former_thunk_p ())
2186 fprintf (f, " Former thunk ");
2187 thunk_info::get (this)->dump (f);
2189 else gcc_checking_assert (!thunk_info::get (this));
2191 fprintf (f, " Called by: ");
2193 profile_count sum = profile_count::zero ();
2194 for (edge = callers; edge; edge = edge->next_caller)
2196 fprintf (f, "%s ", edge->caller->dump_asm_name ());
2197 edge->dump_edge_flags (f);
2198 if (edge->count.initialized_p ())
2199 sum += edge->count.ipa ();
2202 fprintf (f, "\n Calls: ");
2203 for (edge = callees; edge; edge = edge->next_callee)
2205 fprintf (f, "%s ", edge->callee->dump_asm_name ());
2206 edge->dump_edge_flags (f);
2208 fprintf (f, "\n");
2210 if (count.ipa ().initialized_p ())
2212 bool ok = true;
2213 bool min = false;
2214 ipa_ref *ref;
2216 FOR_EACH_ALIAS (this, ref)
2217 if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2218 sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2220 if (inlined_to
2221 || (symtab->state < EXPANSION
2222 && ultimate_alias_target () == this && only_called_directly_p ()))
2223 ok = !count.ipa ().differs_from_p (sum);
2224 else if (count.ipa () > profile_count::from_gcov_type (100)
2225 && count.ipa () < sum.apply_scale (99, 100))
2226 ok = false, min = true;
2227 if (!ok)
2229 fprintf (f, " Invalid sum of caller counts ");
2230 sum.dump (f);
2231 if (min)
2232 fprintf (f, ", should be at most ");
2233 else
2234 fprintf (f, ", should be ");
2235 count.ipa ().dump (f);
2236 fprintf (f, "\n");
2240 for (edge = indirect_calls; edge; edge = edge->next_callee)
2242 if (edge->indirect_info->polymorphic)
2244 fprintf (f, " Polymorphic indirect call of type ");
2245 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2246 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2248 else
2249 fprintf (f, " Indirect call");
2250 edge->dump_edge_flags (f);
2251 if (edge->indirect_info->param_index != -1)
2253 fprintf (f, "of param:%i ", edge->indirect_info->param_index);
2254 if (edge->indirect_info->agg_contents)
2255 fprintf (f, "loaded from %s %s at offset %i ",
2256 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2257 edge->indirect_info->by_ref ? "passed by reference":"",
2258 (int)edge->indirect_info->offset);
2259 if (edge->indirect_info->vptr_changed)
2260 fprintf (f, "(vptr maybe changed) ");
2262 fprintf (f, "num speculative call targets: %i\n",
2263 edge->indirect_info->num_speculative_call_targets);
2264 if (edge->indirect_info->polymorphic)
2265 edge->indirect_info->context.dump (f);
2269 /* Dump call graph node to file F in graphviz format. */
2271 void
2272 cgraph_node::dump_graphviz (FILE *f)
2274 cgraph_edge *edge;
2276 for (edge = callees; edge; edge = edge->next_callee)
2278 cgraph_node *callee = edge->callee;
2280 fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2285 /* Dump call graph node NODE to stderr. */
2287 DEBUG_FUNCTION void
2288 cgraph_node::debug (void)
2290 dump (stderr);
2293 /* Dump the callgraph to file F. */
2295 void
2296 cgraph_node::dump_cgraph (FILE *f)
2298 cgraph_node *node;
2300 fprintf (f, "callgraph:\n\n");
2301 FOR_EACH_FUNCTION (node)
2302 node->dump (f);
2305 /* Return true when the DECL can possibly be inlined. */
2307 bool
2308 cgraph_function_possibly_inlined_p (tree decl)
2310 if (!symtab->global_info_ready)
2311 return !DECL_UNINLINABLE (decl);
2312 return DECL_POSSIBLY_INLINED (decl);
2315 /* Return function availability. See cgraph.h for description of individual
2316 return values. */
2317 enum availability
2318 cgraph_node::get_availability (symtab_node *ref)
2320 if (ref)
2322 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2323 if (cref)
2324 ref = cref->inlined_to;
2326 enum availability avail;
2327 if (!analyzed && !in_other_partition)
2328 avail = AVAIL_NOT_AVAILABLE;
2329 else if (local)
2330 avail = AVAIL_LOCAL;
2331 else if (inlined_to)
2332 avail = AVAIL_AVAILABLE;
2333 else if (transparent_alias)
2334 ultimate_alias_target (&avail, ref);
2335 else if (ifunc_resolver
2336 || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2337 avail = AVAIL_INTERPOSABLE;
2338 else if (!externally_visible)
2339 avail = AVAIL_AVAILABLE;
2340 /* If this is a reference from symbol itself and there are no aliases, we
2341 may be sure that the symbol was not interposed by something else because
2342 the symbol itself would be unreachable otherwise.
2344 Also comdat groups are always resolved in groups. */
2345 else if ((this == ref && !has_aliases_p ())
2346 || (ref && get_comdat_group ()
2347 && get_comdat_group () == ref->get_comdat_group ()))
2348 avail = AVAIL_AVAILABLE;
2349 /* Inline functions are safe to be analyzed even if their symbol can
2350 be overwritten at runtime. It is not meaningful to enforce any sane
2351 behavior on replacing inline function by different body. */
2352 else if (DECL_DECLARED_INLINE_P (decl))
2353 avail = AVAIL_AVAILABLE;
2355 /* If the function can be overwritten, return OVERWRITABLE. Take
2356 care at least of two notable extensions - the COMDAT functions
2357 used to share template instantiations in C++ (this is symmetric
2358 to code cp_cannot_inline_tree_fn and probably shall be shared and
2359 the inlinability hooks completely eliminated). */
2361 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2362 avail = AVAIL_INTERPOSABLE;
2363 else avail = AVAIL_AVAILABLE;
2365 return avail;
2368 /* Worker for cgraph_node_can_be_local_p. */
2369 static bool
2370 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2372 return !(!node->force_output
2373 && !node->ifunc_resolver
2374 /* Limitation of gas requires us to output targets of symver aliases
2375 as global symbols. This is binutils PR 25295. */
2376 && !node->symver
2377 && ((DECL_COMDAT (node->decl)
2378 && !node->forced_by_abi
2379 && !node->used_from_object_file_p ()
2380 && !node->same_comdat_group)
2381 || !node->externally_visible));
2384 /* Return true if cgraph_node can be made local for API change.
2385 Extern inline functions and C++ COMDAT functions can be made local
2386 at the expense of possible code size growth if function is used in multiple
2387 compilation units. */
2388 bool
2389 cgraph_node::can_be_local_p (void)
2391 return (!address_taken
2392 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2393 NULL, true));
2396 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2397 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2398 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2399 skipped. */
2400 bool
2401 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2402 (cgraph_node *, void *),
2403 void *data,
2404 bool include_overwritable,
2405 bool exclude_virtual_thunks)
2407 cgraph_edge *e;
2408 ipa_ref *ref;
2409 enum availability avail = AVAIL_AVAILABLE;
2411 if (include_overwritable
2412 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2414 if (callback (this, data))
2415 return true;
2417 FOR_EACH_ALIAS (this, ref)
2419 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2420 if (include_overwritable
2421 || alias->get_availability () > AVAIL_INTERPOSABLE)
2422 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2423 include_overwritable,
2424 exclude_virtual_thunks))
2425 return true;
2427 if (avail <= AVAIL_INTERPOSABLE)
2428 return false;
2429 for (e = callers; e; e = e->next_caller)
2430 if (e->caller->thunk
2431 && (include_overwritable
2432 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2433 && !(exclude_virtual_thunks
2434 && thunk_info::get (e->caller)->virtual_offset_p))
2435 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2436 include_overwritable,
2437 exclude_virtual_thunks))
2438 return true;
2440 return false;
2443 /* Worker to bring NODE local. */
2445 bool
2446 cgraph_node::make_local (cgraph_node *node, void *)
2448 gcc_checking_assert (node->can_be_local_p ());
2449 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2451 node->make_decl_local ();
2452 node->set_section (NULL);
2453 node->set_comdat_group (NULL);
2454 node->externally_visible = false;
2455 node->forced_by_abi = false;
2456 node->local = true;
2457 node->set_section (NULL);
2458 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2459 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2460 && !flag_incremental_link);
2461 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2462 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2464 return false;
2467 /* Bring cgraph node local. */
2469 void
2470 cgraph_node::make_local (void)
2472 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2475 /* Worker to set nothrow flag. */
2477 static void
2478 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2479 bool *changed)
2481 cgraph_edge *e;
2483 if (nothrow && !TREE_NOTHROW (node->decl))
2485 /* With non-call exceptions we can't say for sure if other function body
2486 was not possibly optimized to still throw. */
2487 if (!non_call || node->binds_to_current_def_p ())
2489 TREE_NOTHROW (node->decl) = true;
2490 *changed = true;
2491 for (e = node->callers; e; e = e->next_caller)
2492 e->can_throw_external = false;
2495 else if (!nothrow && TREE_NOTHROW (node->decl))
2497 TREE_NOTHROW (node->decl) = false;
2498 *changed = true;
2500 ipa_ref *ref;
2501 FOR_EACH_ALIAS (node, ref)
2503 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2504 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2505 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2507 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2508 if (e->caller->thunk
2509 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2510 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2513 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2514 if any to NOTHROW. */
2516 bool
2517 cgraph_node::set_nothrow_flag (bool nothrow)
2519 bool changed = false;
2520 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2522 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2523 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2524 else
2526 ipa_ref *ref;
2528 FOR_EACH_ALIAS (this, ref)
2530 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2531 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2532 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2535 return changed;
2538 /* Worker to set malloc flag. */
2539 static void
2540 set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2542 if (malloc_p && !DECL_IS_MALLOC (node->decl))
2544 DECL_IS_MALLOC (node->decl) = true;
2545 *changed = true;
2548 ipa_ref *ref;
2549 FOR_EACH_ALIAS (node, ref)
2551 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2552 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2553 set_malloc_flag_1 (alias, malloc_p, changed);
2556 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2557 if (e->caller->thunk
2558 && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2559 set_malloc_flag_1 (e->caller, malloc_p, changed);
2562 /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2564 bool
2565 cgraph_node::set_malloc_flag (bool malloc_p)
2567 bool changed = false;
2569 if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2570 set_malloc_flag_1 (this, malloc_p, &changed);
2571 else
2573 ipa_ref *ref;
2575 FOR_EACH_ALIAS (this, ref)
2577 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2578 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2579 set_malloc_flag_1 (alias, malloc_p, &changed);
2582 return changed;
2585 /* Worker to set_const_flag. */
2587 static void
2588 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2589 bool *changed)
2591 /* Static constructors and destructors without a side effect can be
2592 optimized out. */
2593 if (set_const && !looping)
2595 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2597 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2598 *changed = true;
2600 if (DECL_STATIC_DESTRUCTOR (node->decl))
2602 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2603 *changed = true;
2606 if (!set_const)
2608 if (TREE_READONLY (node->decl))
2610 TREE_READONLY (node->decl) = 0;
2611 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2612 *changed = true;
2615 else
2617 /* Consider function:
2619 bool a(int *p)
2621 return *p==*p;
2624 During early optimization we will turn this into:
2626 bool a(int *p)
2628 return true;
2631 Now if this function will be detected as CONST however when interposed
2632 it may end up being just pure. We always must assume the worst
2633 scenario here. */
2634 if (TREE_READONLY (node->decl))
2636 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2638 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2639 *changed = true;
2642 else if (node->binds_to_current_def_p ())
2644 TREE_READONLY (node->decl) = true;
2645 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2646 DECL_PURE_P (node->decl) = false;
2647 *changed = true;
2649 else
2651 if (dump_file && (dump_flags & TDF_DETAILS))
2652 fprintf (dump_file, "Dropping state to PURE because function does "
2653 "not bind to current def.\n");
2654 if (!DECL_PURE_P (node->decl))
2656 DECL_PURE_P (node->decl) = true;
2657 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2658 *changed = true;
2660 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2662 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2663 *changed = true;
2668 ipa_ref *ref;
2669 FOR_EACH_ALIAS (node, ref)
2671 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2672 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2673 set_const_flag_1 (alias, set_const, looping, changed);
2675 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2676 if (e->caller->thunk
2677 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2679 /* Virtual thunks access virtual offset in the vtable, so they can
2680 only be pure, never const. */
2681 if (set_const
2682 && (thunk_info::get (e->caller)->virtual_offset_p
2683 || !node->binds_to_current_def_p (e->caller)))
2684 *changed |= e->caller->set_pure_flag (true, looping);
2685 else
2686 set_const_flag_1 (e->caller, set_const, looping, changed);
2690 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2691 If SET_CONST if false, clear the flag.
2693 When setting the flag be careful about possible interposition and
2694 do not set the flag for functions that can be interposed and set pure
2695 flag for functions that can bind to other definition.
2697 Return true if any change was done. */
2699 bool
2700 cgraph_node::set_const_flag (bool set_const, bool looping)
2702 bool changed = false;
2703 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2704 set_const_flag_1 (this, set_const, looping, &changed);
2705 else
2707 ipa_ref *ref;
2709 FOR_EACH_ALIAS (this, ref)
2711 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2712 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2713 set_const_flag_1 (alias, set_const, looping, &changed);
2716 return changed;
2719 /* Info used by set_pure_flag_1. */
2721 struct set_pure_flag_info
2723 bool pure;
2724 bool looping;
2725 bool changed;
2728 /* Worker to set_pure_flag. */
2730 static bool
2731 set_pure_flag_1 (cgraph_node *node, void *data)
2733 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2734 /* Static constructors and destructors without a side effect can be
2735 optimized out. */
2736 if (info->pure && !info->looping)
2738 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2740 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2741 info->changed = true;
2743 if (DECL_STATIC_DESTRUCTOR (node->decl))
2745 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2746 info->changed = true;
2749 if (info->pure)
2751 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2753 DECL_PURE_P (node->decl) = true;
2754 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2755 info->changed = true;
2757 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2758 && !info->looping)
2760 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2761 info->changed = true;
2764 else
2766 if (DECL_PURE_P (node->decl))
2768 DECL_PURE_P (node->decl) = false;
2769 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2770 info->changed = true;
2773 return false;
2776 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2777 if any to PURE.
2779 When setting the flag, be careful about possible interposition.
2780 Return true if any change was done. */
2782 bool
2783 cgraph_node::set_pure_flag (bool pure, bool looping)
2785 struct set_pure_flag_info info = {pure, looping, false};
2786 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2787 return info.changed;
2790 /* Return true when cgraph_node cannot return or throw and thus
2791 it is safe to ignore its side effects for IPA analysis. */
2793 bool
2794 cgraph_node::cannot_return_p (void)
2796 int flags = flags_from_decl_or_type (decl);
2797 if (!opt_for_fn (decl, flag_exceptions))
2798 return (flags & ECF_NORETURN) != 0;
2799 else
2800 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2801 == (ECF_NORETURN | ECF_NOTHROW));
2804 /* Return true when call of edge cannot lead to return from caller
2805 and thus it is safe to ignore its side effects for IPA analysis
2806 when computing side effects of the caller.
2807 FIXME: We could actually mark all edges that have no reaching
2808 patch to the exit block or throw to get better results. */
2809 bool
2810 cgraph_edge::cannot_lead_to_return_p (void)
2812 if (caller->cannot_return_p ())
2813 return true;
2814 if (indirect_unknown_callee)
2816 int flags = indirect_info->ecf_flags;
2817 if (!opt_for_fn (caller->decl, flag_exceptions))
2818 return (flags & ECF_NORETURN) != 0;
2819 else
2820 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2821 == (ECF_NORETURN | ECF_NOTHROW));
2823 else
2824 return callee->cannot_return_p ();
2827 /* Return true if the edge may be considered hot. */
2829 bool
2830 cgraph_edge::maybe_hot_p (void)
2832 if (!maybe_hot_count_p (NULL, count.ipa ()))
2833 return false;
2834 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2835 || (callee
2836 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2837 return false;
2838 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2839 && (callee
2840 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2841 return false;
2842 if (opt_for_fn (caller->decl, optimize_size))
2843 return false;
2844 if (caller->frequency == NODE_FREQUENCY_HOT)
2845 return true;
2846 if (!count.initialized_p ())
2847 return true;
2848 cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
2849 if (!where->count.initialized_p ())
2850 return false;
2851 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2853 if (count.apply_scale (2, 1) < where->count.apply_scale (3, 1))
2854 return false;
2856 else if (count.apply_scale (param_hot_bb_frequency_fraction , 1)
2857 < where->count)
2858 return false;
2859 return true;
2862 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2864 static bool
2865 nonremovable_p (cgraph_node *node, void *)
2867 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2870 /* Return true if whole comdat group can be removed if there are no direct
2871 calls to THIS. */
2873 bool
2874 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2876 struct ipa_ref *ref;
2878 /* For local symbols or non-comdat group it is the same as
2879 can_remove_if_no_direct_calls_p. */
2880 if (!externally_visible || !same_comdat_group)
2882 if (DECL_EXTERNAL (decl))
2883 return true;
2884 if (address_taken)
2885 return false;
2886 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2889 if (will_inline && address_taken)
2890 return false;
2892 /* Otherwise check if we can remove the symbol itself and then verify
2893 that only uses of the comdat groups are direct call to THIS
2894 or its aliases. */
2895 if (!can_remove_if_no_direct_calls_and_refs_p ())
2896 return false;
2898 /* Check that all refs come from within the comdat group. */
2899 for (int i = 0; iterate_referring (i, ref); i++)
2900 if (ref->referring->get_comdat_group () != get_comdat_group ())
2901 return false;
2903 struct cgraph_node *target = ultimate_alias_target ();
2904 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2905 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2907 if (!externally_visible)
2908 continue;
2909 if (!next->alias
2910 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2911 return false;
2913 /* If we see different symbol than THIS, be sure to check calls. */
2914 if (next->ultimate_alias_target () != target)
2915 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2916 if (e->caller->get_comdat_group () != get_comdat_group ()
2917 || will_inline)
2918 return false;
2920 /* If function is not being inlined, we care only about
2921 references outside of the comdat group. */
2922 if (!will_inline)
2923 for (int i = 0; next->iterate_referring (i, ref); i++)
2924 if (ref->referring->get_comdat_group () != get_comdat_group ())
2925 return false;
2927 return true;
2930 /* Return true when function cgraph_node can be expected to be removed
2931 from program when direct calls in this compilation unit are removed.
2933 As a special case COMDAT functions are
2934 cgraph_can_remove_if_no_direct_calls_p while the are not
2935 cgraph_only_called_directly_p (it is possible they are called from other
2936 unit)
2938 This function behaves as cgraph_only_called_directly_p because eliminating
2939 all uses of COMDAT function does not make it necessarily disappear from
2940 the program unless we are compiling whole program or we do LTO. In this
2941 case we know we win since dynamic linking will not really discard the
2942 linkonce section. */
2944 bool
2945 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2946 (bool will_inline)
2948 gcc_assert (!inlined_to);
2949 if (DECL_EXTERNAL (decl))
2950 return true;
2952 if (!in_lto_p && !flag_whole_program)
2954 /* If the symbol is in comdat group, we need to verify that whole comdat
2955 group becomes unreachable. Technically we could skip references from
2956 within the group, too. */
2957 if (!only_called_directly_p ())
2958 return false;
2959 if (same_comdat_group && externally_visible)
2961 struct cgraph_node *target = ultimate_alias_target ();
2963 if (will_inline && address_taken)
2964 return true;
2965 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2966 next != this;
2967 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2969 if (!externally_visible)
2970 continue;
2971 if (!next->alias
2972 && !next->only_called_directly_p ())
2973 return false;
2975 /* If we see different symbol than THIS,
2976 be sure to check calls. */
2977 if (next->ultimate_alias_target () != target)
2978 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2979 if (e->caller->get_comdat_group () != get_comdat_group ()
2980 || will_inline)
2981 return false;
2984 return true;
2986 else
2987 return can_remove_if_no_direct_calls_p (will_inline);
2991 /* Worker for cgraph_only_called_directly_p. */
2993 static bool
2994 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2996 return !node->only_called_directly_or_aliased_p ();
2999 /* Return true when function cgraph_node and all its aliases are only called
3000 directly.
3001 i.e. it is not externally visible, address was not taken and
3002 it is not used in any other non-standard way. */
3004 bool
3005 cgraph_node::only_called_directly_p (void)
3007 gcc_assert (ultimate_alias_target () == this);
3008 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
3009 NULL, true);
3013 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3015 static bool
3016 collect_callers_of_node_1 (cgraph_node *node, void *data)
3018 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3019 cgraph_edge *cs;
3020 enum availability avail;
3021 node->ultimate_alias_target (&avail);
3023 if (avail > AVAIL_INTERPOSABLE)
3024 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3025 if (!cs->indirect_inlining_edge
3026 && !cs->caller->thunk)
3027 redirect_callers->safe_push (cs);
3028 return false;
3031 /* Collect all callers of cgraph_node and its aliases that are known to lead to
3032 cgraph_node (i.e. are not overwritable). */
3034 vec<cgraph_edge *>
3035 cgraph_node::collect_callers (void)
3037 vec<cgraph_edge *> redirect_callers = vNULL;
3038 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3039 &redirect_callers, false);
3040 return redirect_callers;
3044 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
3045 optimistically true if this cannot be determined. */
3047 static bool
3048 clone_of_p (cgraph_node *node, cgraph_node *node2)
3050 node = node->ultimate_alias_target ();
3051 node2 = node2->ultimate_alias_target ();
3053 if (node2->clone_of == node
3054 || node2->former_clone_of == node->decl)
3055 return true;
3057 if (!node->thunk && !node->former_thunk_p ())
3059 while (node2 && node->decl != node2->decl)
3060 node2 = node2->clone_of;
3061 return node2 != NULL;
3064 /* There are no virtual clones of thunks so check former_clone_of or if we
3065 might have skipped thunks because this adjustments are no longer
3066 necessary. */
3067 while (node->thunk || node->former_thunk_p ())
3069 if (!thunk_info::get (node)->this_adjusting)
3070 return false;
3071 /* In case of instrumented expanded thunks, which can have multiple calls
3072 in them, we do not know how to continue and just have to be
3073 optimistic. The same applies if all calls have already been inlined
3074 into the thunk. */
3075 if (!node->callees || node->callees->next_callee)
3076 return true;
3077 node = node->callees->callee->ultimate_alias_target ();
3079 if (!node2->clone.param_adjustments
3080 || node2->clone.param_adjustments->first_param_intact_p ())
3081 return false;
3082 if (node2->former_clone_of == node->decl
3083 || node2->former_clone_of == node->former_clone_of)
3084 return true;
3086 cgraph_node *n2 = node2;
3087 while (n2 && node->decl != n2->decl)
3088 n2 = n2->clone_of;
3089 if (n2)
3090 return true;
3093 return false;
3096 /* Verify edge count and frequency. */
3098 bool
3099 cgraph_edge::verify_count ()
3101 bool error_found = false;
3102 if (!count.verify ())
3104 error ("caller edge count invalid");
3105 error_found = true;
3107 return error_found;
3110 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3111 static void
3112 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3114 bool fndecl_was_null = false;
3115 /* debug_gimple_stmt needs correct cfun */
3116 if (cfun != this_cfun)
3117 set_cfun (this_cfun);
3118 /* ...and an actual current_function_decl */
3119 if (!current_function_decl)
3121 current_function_decl = this_cfun->decl;
3122 fndecl_was_null = true;
3124 debug_gimple_stmt (stmt);
3125 if (fndecl_was_null)
3126 current_function_decl = NULL;
3129 /* Verify that call graph edge corresponds to DECL from the associated
3130 statement. Return true if the verification should fail. */
3132 bool
3133 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3135 cgraph_node *node;
3137 if (!decl || callee->inlined_to)
3138 return false;
3139 if (symtab->state == LTO_STREAMING)
3140 return false;
3141 node = cgraph_node::get (decl);
3143 /* We do not know if a node from a different partition is an alias or what it
3144 aliases and therefore cannot do the former_clone_of check reliably. When
3145 body_removed is set, we have lost all information about what was alias or
3146 thunk of and also cannot proceed. */
3147 if (!node
3148 || node->body_removed
3149 || node->in_other_partition
3150 || callee->icf_merged
3151 || callee->in_other_partition)
3152 return false;
3154 node = node->ultimate_alias_target ();
3156 /* Optimizers can redirect unreachable calls or calls triggering undefined
3157 behavior to builtin_unreachable. */
3159 if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE))
3160 return false;
3162 if (callee->former_clone_of != node->decl
3163 && (node != callee->ultimate_alias_target ())
3164 && !clone_of_p (node, callee))
3165 return true;
3166 else
3167 return false;
3170 /* Disable warnings about missing quoting in GCC diagnostics for
3171 the verification errors. Their format strings don't follow GCC
3172 diagnostic conventions and the calls are ultimately followed by
3173 one to internal_error. */
3174 #if __GNUC__ >= 10
3175 # pragma GCC diagnostic push
3176 # pragma GCC diagnostic ignored "-Wformat-diag"
3177 #endif
3179 /* Verify consistency of speculative call in NODE corresponding to STMT
3180 and LTO_STMT_UID. If INDIRECT is set, assume that it is the indirect
3181 edge of call sequence. Return true if error is found.
3183 This function is called to every component of indirect call (direct edges,
3184 indirect edge and refs). To save duplicated work, do full testing only
3185 in that case. */
3186 static bool
3187 verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3188 unsigned int lto_stmt_uid,
3189 struct cgraph_edge *indirect)
3191 if (indirect == NULL)
3193 for (indirect = node->indirect_calls; indirect;
3194 indirect = indirect->next_callee)
3195 if (indirect->call_stmt == stmt
3196 && indirect->lto_stmt_uid == lto_stmt_uid)
3197 break;
3198 if (!indirect)
3200 error ("missing indirect call in speculative call sequence");
3201 return true;
3203 if (!indirect->speculative)
3205 error ("indirect call in speculative call sequence has no "
3206 "speculative flag");
3207 return true;
3209 return false;
3212 /* Maximal number of targets. We probably will never want to have more than
3213 this. */
3214 const unsigned int num = 256;
3215 cgraph_edge *direct_calls[num];
3216 ipa_ref *refs[num];
3218 for (unsigned int i = 0; i < num; i++)
3220 direct_calls[i] = NULL;
3221 refs[i] = NULL;
3224 cgraph_edge *first_call = NULL;
3225 cgraph_edge *prev_call = NULL;
3227 for (cgraph_edge *direct = node->callees; direct;
3228 direct = direct->next_callee)
3229 if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3231 if (!first_call)
3232 first_call = direct;
3233 if (prev_call && direct != prev_call->next_callee)
3235 error ("speculative edges are not adjacent");
3236 return true;
3238 prev_call = direct;
3239 if (!direct->speculative)
3241 error ("direct call to %s in speculative call sequence has no "
3242 "speculative flag", direct->callee->dump_name ());
3243 return true;
3245 if (direct->speculative_id >= num)
3247 error ("direct call to %s in speculative call sequence has "
3248 "speculative_id %i out of range",
3249 direct->callee->dump_name (), direct->speculative_id);
3250 return true;
3252 if (direct_calls[direct->speculative_id])
3254 error ("duplicate direct call to %s in speculative call sequence "
3255 "with speculative_id %i",
3256 direct->callee->dump_name (), direct->speculative_id);
3257 return true;
3259 direct_calls[direct->speculative_id] = direct;
3262 if (first_call->call_stmt
3263 && first_call != node->get_edge (first_call->call_stmt))
3265 error ("call stmt hash does not point to first direct edge of "
3266 "speculative call sequence");
3267 return true;
3270 ipa_ref *ref;
3271 for (int i = 0; node->iterate_reference (i, ref); i++)
3272 if (ref->speculative
3273 && ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3275 if (ref->speculative_id >= num)
3277 error ("direct call to %s in speculative call sequence has "
3278 "speculative_id %i out of range",
3279 ref->referred->dump_name (), ref->speculative_id);
3280 return true;
3282 if (refs[ref->speculative_id])
3284 error ("duplicate reference %s in speculative call sequence "
3285 "with speculative_id %i",
3286 ref->referred->dump_name (), ref->speculative_id);
3287 return true;
3289 refs[ref->speculative_id] = ref;
3292 int num_targets = 0;
3293 for (unsigned int i = 0 ; i < num ; i++)
3295 if (refs[i] && !direct_calls[i])
3297 error ("missing direct call for speculation %i", i);
3298 return true;
3300 if (!refs[i] && direct_calls[i])
3302 error ("missing ref for speculation %i", i);
3303 return true;
3305 if (refs[i] != NULL)
3306 num_targets++;
3309 if (num_targets != indirect->num_speculative_call_targets_p ())
3311 error ("number of speculative targets %i mismatched with "
3312 "num_speculative_call_targets %i",
3313 num_targets,
3314 indirect->num_speculative_call_targets_p ());
3315 return true;
3317 return false;
3320 /* Verify cgraph nodes of given cgraph node. */
3321 DEBUG_FUNCTION void
3322 cgraph_node::verify_node (void)
3324 cgraph_edge *e;
3325 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3326 basic_block this_block;
3327 gimple_stmt_iterator gsi;
3328 bool error_found = false;
3329 int i;
3330 ipa_ref *ref = NULL;
3332 if (seen_error ())
3333 return;
3335 timevar_push (TV_CGRAPH_VERIFY);
3336 error_found |= verify_base ();
3337 for (e = callees; e; e = e->next_callee)
3338 if (e->aux)
3340 error ("aux field set for edge %s->%s",
3341 identifier_to_locale (e->caller->name ()),
3342 identifier_to_locale (e->callee->name ()));
3343 error_found = true;
3345 if (!count.verify ())
3347 error ("cgraph count invalid");
3348 error_found = true;
3350 if (inlined_to && same_comdat_group)
3352 error ("inline clone in same comdat group list");
3353 error_found = true;
3355 if (inlined_to && !count.compatible_p (inlined_to->count))
3357 error ("inline clone count is not compatible");
3358 count.debug ();
3359 inlined_to->count.debug ();
3360 error_found = true;
3362 if (tp_first_run < 0)
3364 error ("tp_first_run must be non-negative");
3365 error_found = true;
3367 if (!definition && !in_other_partition && local)
3369 error ("local symbols must be defined");
3370 error_found = true;
3372 if (inlined_to && externally_visible)
3374 error ("externally visible inline clone");
3375 error_found = true;
3377 if (inlined_to && address_taken)
3379 error ("inline clone with address taken");
3380 error_found = true;
3382 if (inlined_to && force_output)
3384 error ("inline clone is forced to output");
3385 error_found = true;
3387 if (symtab->state != LTO_STREAMING)
3389 if (calls_comdat_local && !same_comdat_group)
3391 error ("calls_comdat_local is set outside of a comdat group");
3392 error_found = true;
3394 if (!inlined_to && calls_comdat_local != check_calls_comdat_local_p ())
3396 error ("invalid calls_comdat_local flag");
3397 error_found = true;
3400 if (DECL_IS_MALLOC (decl)
3401 && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3403 error ("malloc attribute should be used for a function that "
3404 "returns a pointer");
3405 error_found = true;
3407 for (e = indirect_calls; e; e = e->next_callee)
3409 if (e->aux)
3411 error ("aux field set for indirect edge from %s",
3412 identifier_to_locale (e->caller->name ()));
3413 error_found = true;
3415 if (!e->count.compatible_p (count))
3417 error ("edge count is not compatible with function count");
3418 e->count.debug ();
3419 count.debug ();
3420 error_found = true;
3422 if (!e->indirect_unknown_callee
3423 || !e->indirect_info)
3425 error ("An indirect edge from %s is not marked as indirect or has "
3426 "associated indirect_info, the corresponding statement is: ",
3427 identifier_to_locale (e->caller->name ()));
3428 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3429 error_found = true;
3431 if (e->call_stmt && e->lto_stmt_uid)
3433 error ("edge has both call_stmt and lto_stmt_uid set");
3434 error_found = true;
3437 bool check_comdat = comdat_local_p ();
3438 for (e = callers; e; e = e->next_caller)
3440 if (e->verify_count ())
3441 error_found = true;
3442 if (check_comdat
3443 && !in_same_comdat_group_p (e->caller))
3445 error ("comdat-local function called by %s outside its comdat",
3446 identifier_to_locale (e->caller->name ()));
3447 error_found = true;
3449 if (!e->inline_failed)
3451 if (inlined_to
3452 != (e->caller->inlined_to
3453 ? e->caller->inlined_to : e->caller))
3455 error ("inlined_to pointer is wrong");
3456 error_found = true;
3458 if (callers->next_caller)
3460 error ("multiple inline callers");
3461 error_found = true;
3464 else
3465 if (inlined_to)
3467 error ("inlined_to pointer set for noninline callers");
3468 error_found = true;
3471 for (e = callees; e; e = e->next_callee)
3473 if (e->verify_count ())
3474 error_found = true;
3475 if (!e->count.compatible_p (count))
3477 error ("edge count is not compatible with function count");
3478 e->count.debug ();
3479 count.debug ();
3480 error_found = true;
3482 if (gimple_has_body_p (e->caller->decl)
3483 && !e->caller->inlined_to
3484 && !e->speculative
3485 /* Optimized out calls are redirected to __builtin_unreachable. */
3486 && (e->count.nonzero_p ()
3487 || ! e->callee->decl
3488 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
3489 && count
3490 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3491 && (!e->count.ipa_p ()
3492 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3494 error ("caller edge count does not match BB count");
3495 fprintf (stderr, "edge count: ");
3496 e->count.dump (stderr);
3497 fprintf (stderr, "\n bb count: ");
3498 gimple_bb (e->call_stmt)->count.dump (stderr);
3499 fprintf (stderr, "\n");
3500 error_found = true;
3502 if (e->call_stmt && e->lto_stmt_uid)
3504 error ("edge has both call_stmt and lto_stmt_uid set");
3505 error_found = true;
3507 if (e->speculative
3508 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3509 NULL))
3510 error_found = true;
3512 for (e = indirect_calls; e; e = e->next_callee)
3514 if (e->verify_count ())
3515 error_found = true;
3516 if (gimple_has_body_p (e->caller->decl)
3517 && !e->caller->inlined_to
3518 && !e->speculative
3519 && e->count.ipa_p ()
3520 && count
3521 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3522 && (!e->count.ipa_p ()
3523 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3525 error ("indirect call count does not match BB count");
3526 fprintf (stderr, "edge count: ");
3527 e->count.dump (stderr);
3528 fprintf (stderr, "\n bb count: ");
3529 gimple_bb (e->call_stmt)->count.dump (stderr);
3530 fprintf (stderr, "\n");
3531 error_found = true;
3533 if (e->speculative
3534 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3536 error_found = true;
3538 for (i = 0; iterate_reference (i, ref); i++)
3540 if (ref->stmt && ref->lto_stmt_uid)
3542 error ("reference has both stmt and lto_stmt_uid set");
3543 error_found = true;
3545 if (ref->speculative
3546 && verify_speculative_call (this, ref->stmt,
3547 ref->lto_stmt_uid, NULL))
3548 error_found = true;
3551 if (!callers && inlined_to)
3553 error ("inlined_to pointer is set but no predecessors found");
3554 error_found = true;
3556 if (inlined_to == this)
3558 error ("inlined_to pointer refers to itself");
3559 error_found = true;
3562 if (clone_of)
3564 cgraph_node *first_clone = clone_of->clones;
3565 if (first_clone != this)
3567 if (prev_sibling_clone->clone_of != clone_of)
3569 error ("cgraph_node has wrong clone_of");
3570 error_found = true;
3574 if (clones)
3576 cgraph_node *n;
3577 for (n = clones; n; n = n->next_sibling_clone)
3578 if (n->clone_of != this)
3579 break;
3580 if (n)
3582 error ("cgraph_node has wrong clone list");
3583 error_found = true;
3586 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3588 error ("cgraph_node is in clone list but it is not clone");
3589 error_found = true;
3591 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3593 error ("cgraph_node has wrong prev_clone pointer");
3594 error_found = true;
3596 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3598 error ("double linked list of clones corrupted");
3599 error_found = true;
3602 if (analyzed && alias)
3604 bool ref_found = false;
3605 int i;
3606 ipa_ref *ref = NULL;
3608 if (callees)
3610 error ("Alias has call edges");
3611 error_found = true;
3613 for (i = 0; iterate_reference (i, ref); i++)
3614 if (ref->use != IPA_REF_ALIAS)
3616 error ("Alias has non-alias reference");
3617 error_found = true;
3619 else if (ref_found)
3621 error ("Alias has more than one alias reference");
3622 error_found = true;
3624 else
3625 ref_found = true;
3626 if (!ref_found)
3628 error ("Analyzed alias has no reference");
3629 error_found = true;
3633 if (analyzed && thunk)
3635 if (!callees)
3637 error ("No edge out of thunk node");
3638 error_found = true;
3640 else if (callees->next_callee)
3642 error ("More than one edge out of thunk node");
3643 error_found = true;
3645 if (gimple_has_body_p (decl) && !inlined_to)
3647 error ("Thunk is not supposed to have body");
3648 error_found = true;
3651 else if (analyzed && gimple_has_body_p (decl)
3652 && !TREE_ASM_WRITTEN (decl)
3653 && (!DECL_EXTERNAL (decl) || inlined_to)
3654 && !flag_wpa)
3656 if (this_cfun->cfg)
3658 hash_set<gimple *> stmts;
3660 /* Reach the trees by walking over the CFG, and note the
3661 enclosing basic-blocks in the call edges. */
3662 FOR_EACH_BB_FN (this_block, this_cfun)
3664 for (gsi = gsi_start_phis (this_block);
3665 !gsi_end_p (gsi); gsi_next (&gsi))
3666 stmts.add (gsi_stmt (gsi));
3667 for (gsi = gsi_start_bb (this_block);
3668 !gsi_end_p (gsi);
3669 gsi_next (&gsi))
3671 gimple *stmt = gsi_stmt (gsi);
3672 stmts.add (stmt);
3673 if (is_gimple_call (stmt))
3675 cgraph_edge *e = get_edge (stmt);
3676 tree decl = gimple_call_fndecl (stmt);
3677 if (e)
3679 if (e->aux)
3681 error ("shared call_stmt:");
3682 cgraph_debug_gimple_stmt (this_cfun, stmt);
3683 error_found = true;
3685 if (!e->indirect_unknown_callee)
3687 if (e->verify_corresponds_to_fndecl (decl))
3689 error ("edge points to wrong declaration:");
3690 debug_tree (e->callee->decl);
3691 fprintf (stderr," Instead of:");
3692 debug_tree (decl);
3693 error_found = true;
3696 else if (decl)
3698 error ("an indirect edge with unknown callee "
3699 "corresponding to a call_stmt with "
3700 "a known declaration:");
3701 error_found = true;
3702 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3704 e->aux = (void *)1;
3706 else if (decl)
3708 error ("missing callgraph edge for call stmt:");
3709 cgraph_debug_gimple_stmt (this_cfun, stmt);
3710 error_found = true;
3715 for (i = 0; iterate_reference (i, ref); i++)
3716 if (ref->stmt && !stmts.contains (ref->stmt))
3718 error ("reference to dead statement");
3719 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3720 error_found = true;
3723 else
3724 /* No CFG available?! */
3725 gcc_unreachable ();
3727 for (e = callees; e; e = e->next_callee)
3729 if (!e->aux && !e->speculative)
3731 error ("edge %s->%s has no corresponding call_stmt",
3732 identifier_to_locale (e->caller->name ()),
3733 identifier_to_locale (e->callee->name ()));
3734 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3735 error_found = true;
3737 e->aux = 0;
3739 for (e = indirect_calls; e; e = e->next_callee)
3741 if (!e->aux && !e->speculative)
3743 error ("an indirect edge from %s has no corresponding call_stmt",
3744 identifier_to_locale (e->caller->name ()));
3745 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3746 error_found = true;
3748 e->aux = 0;
3752 if (nested_function_info *info = nested_function_info::get (this))
3754 if (info->nested != NULL)
3756 for (cgraph_node *n = info->nested; n != NULL;
3757 n = next_nested_function (n))
3759 nested_function_info *ninfo = nested_function_info::get (n);
3760 if (ninfo->origin == NULL)
3762 error ("missing origin for a node in a nested list");
3763 error_found = true;
3765 else if (ninfo->origin != this)
3767 error ("origin points to a different parent");
3768 error_found = true;
3769 break;
3773 if (info->next_nested != NULL && info->origin == NULL)
3775 error ("missing origin for a node in a nested list");
3776 error_found = true;
3780 if (error_found)
3782 dump (stderr);
3783 internal_error ("verify_cgraph_node failed");
3785 timevar_pop (TV_CGRAPH_VERIFY);
3788 /* Verify whole cgraph structure. */
3789 DEBUG_FUNCTION void
3790 cgraph_node::verify_cgraph_nodes (void)
3792 cgraph_node *node;
3794 if (seen_error ())
3795 return;
3797 FOR_EACH_FUNCTION (node)
3798 node->verify ();
3801 #if __GNUC__ >= 10
3802 # pragma GCC diagnostic pop
3803 #endif
3805 /* Walk the alias chain to return the function cgraph_node is alias of.
3806 Walk through thunks, too.
3807 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3808 When REF is non-NULL, assume that reference happens in symbol REF
3809 when determining the availability. */
3811 cgraph_node *
3812 cgraph_node::function_symbol (enum availability *availability,
3813 struct symtab_node *ref)
3815 cgraph_node *node = ultimate_alias_target (availability, ref);
3817 while (node->thunk)
3819 enum availability a;
3821 ref = node;
3822 node = node->callees->callee;
3823 node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3824 if (availability && a < *availability)
3825 *availability = a;
3827 return node;
3830 /* Walk the alias chain to return the function cgraph_node is alias of.
3831 Walk through non virtual thunks, too. Thus we return either a function
3832 or a virtual thunk node.
3833 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3834 When REF is non-NULL, assume that reference happens in symbol REF
3835 when determining the availability. */
3837 cgraph_node *
3838 cgraph_node::function_or_virtual_thunk_symbol
3839 (enum availability *availability,
3840 struct symtab_node *ref)
3842 cgraph_node *node = ultimate_alias_target (availability, ref);
3844 while (node->thunk && !thunk_info::get (node)->virtual_offset_p)
3846 enum availability a;
3848 ref = node;
3849 node = node->callees->callee;
3850 node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3851 if (availability && a < *availability)
3852 *availability = a;
3854 return node;
3857 /* When doing LTO, read cgraph_node's body from disk if it is not already
3858 present. Also perform any necessary clone materializations. */
3860 bool
3861 cgraph_node::get_untransformed_body ()
3863 lto_file_decl_data *file_data;
3864 const char *data, *name;
3865 size_t len;
3866 tree decl = this->decl;
3868 /* See if there is clone to be materialized.
3869 (inline clones does not need materialization, but we can be seeing
3870 an inline clone of real clone). */
3871 cgraph_node *p = this;
3872 for (cgraph_node *c = clone_of; c; c = c->clone_of)
3874 if (c->decl != decl)
3875 p->materialize_clone ();
3876 p = c;
3879 /* Check if body is already there. Either we have gimple body or
3880 the function is thunk and in that case we set DECL_ARGUMENTS. */
3881 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3882 return false;
3884 gcc_assert (in_lto_p && !DECL_RESULT (decl));
3886 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3888 file_data = lto_file_data;
3889 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3891 /* We may have renamed the declaration, e.g., a static function. */
3892 name = lto_get_decl_name_mapping (file_data, name);
3893 struct lto_in_decl_state *decl_state
3894 = lto_get_function_in_decl_state (file_data, decl);
3896 cgraph_node *origin = this;
3897 while (origin->clone_of)
3898 origin = origin->clone_of;
3900 int stream_order = origin->order - file_data->order_base;
3901 data = lto_get_section_data (file_data, LTO_section_function_body,
3902 name, stream_order, &len,
3903 decl_state->compressed);
3904 if (!data)
3905 fatal_error (input_location, "%s: section %s.%d is missing",
3906 file_data->file_name, name, stream_order);
3908 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3910 if (!quiet_flag)
3911 fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
3912 lto_input_function_body (file_data, this, data);
3913 lto_stats.num_function_bodies++;
3914 lto_free_section_data (file_data, LTO_section_function_body, name,
3915 data, len, decl_state->compressed);
3916 lto_free_function_in_decl_state_for_node (this);
3917 /* Keep lto file data so ipa-inline-analysis knows about cross module
3918 inlining. */
3920 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3922 return true;
3925 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3926 if it is not already present. When some IPA transformations are scheduled,
3927 apply them. */
3929 bool
3930 cgraph_node::get_body (void)
3932 bool updated;
3934 updated = get_untransformed_body ();
3936 /* Getting transformed body makes no sense for inline clones;
3937 we should never use this on real clones because they are materialized
3938 early.
3939 TODO: Materializing clones here will likely lead to smaller LTRANS
3940 footprint. */
3941 gcc_assert (!inlined_to && !clone_of);
3942 if (ipa_transforms_to_apply.exists ())
3944 opt_pass *saved_current_pass = current_pass;
3945 FILE *saved_dump_file = dump_file;
3946 const char *saved_dump_file_name = dump_file_name;
3947 dump_flags_t saved_dump_flags = dump_flags;
3948 dump_file_name = NULL;
3949 set_dump_file (NULL);
3951 push_cfun (DECL_STRUCT_FUNCTION (decl));
3953 update_ssa (TODO_update_ssa_only_virtuals);
3954 execute_all_ipa_transforms (true);
3955 cgraph_edge::rebuild_edges ();
3956 free_dominance_info (CDI_DOMINATORS);
3957 free_dominance_info (CDI_POST_DOMINATORS);
3958 pop_cfun ();
3959 updated = true;
3961 current_pass = saved_current_pass;
3962 set_dump_file (saved_dump_file);
3963 dump_file_name = saved_dump_file_name;
3964 dump_flags = saved_dump_flags;
3966 return updated;
3969 /* Return the DECL_STRUCT_FUNCTION of the function. */
3971 struct function *
3972 cgraph_node::get_fun () const
3974 const cgraph_node *node = this;
3975 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3977 while (!fun && node->clone_of)
3979 node = node->clone_of;
3980 fun = DECL_STRUCT_FUNCTION (node->decl);
3983 return fun;
3986 /* Reset all state within cgraph.c so that we can rerun the compiler
3987 within the same process. For use by toplev::finalize. */
3989 void
3990 cgraph_c_finalize (void)
3992 nested_function_info::release ();
3993 thunk_info::release ();
3994 symtab = NULL;
3996 x_cgraph_nodes_queue = NULL;
3998 cgraph_fnver_htab = NULL;
3999 version_info_node = NULL;
4002 /* A worker for call_for_symbol_and_aliases. */
4004 bool
4005 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
4006 void *),
4007 void *data,
4008 bool include_overwritable)
4010 ipa_ref *ref;
4011 FOR_EACH_ALIAS (this, ref)
4013 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
4014 if (include_overwritable
4015 || alias->get_availability () > AVAIL_INTERPOSABLE)
4016 if (alias->call_for_symbol_and_aliases (callback, data,
4017 include_overwritable))
4018 return true;
4020 return false;
4023 /* Return true if NODE has thunk. */
4025 bool
4026 cgraph_node::has_thunk_p (cgraph_node *node, void *)
4028 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
4029 if (e->caller->thunk)
4030 return true;
4031 return false;
4034 /* Expected frequency of executions within the function. */
4036 sreal
4037 cgraph_edge::sreal_frequency ()
4039 return count.to_sreal_scale (caller->inlined_to
4040 ? caller->inlined_to->count
4041 : caller->count);
4045 /* During LTO stream in this can be used to check whether call can possibly
4046 be internal to the current translation unit. */
4048 bool
4049 cgraph_edge::possibly_call_in_translation_unit_p (void)
4051 gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4053 /* While incremental linking we may end up getting function body later. */
4054 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4055 return true;
4057 /* We may be smarter here and avoid streaming in indirect calls we can't
4058 track, but that would require arranging streaming the indirect call
4059 summary first. */
4060 if (!callee)
4061 return true;
4063 /* If callee is local to the original translation unit, it will be
4064 defined. */
4065 if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4066 return true;
4068 /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4069 yet) and see if it is a definition. In fact we may also resolve aliases,
4070 but that is probably not too important. */
4071 symtab_node *node = callee;
4072 for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4073 node = node->previous_sharing_asm_name;
4074 if (node->previous_sharing_asm_name)
4075 node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4076 gcc_assert (TREE_PUBLIC (node->decl));
4077 return node->get_availability () >= AVAIL_INTERPOSABLE;
4080 /* Return num_speculative_targets of this edge. */
4083 cgraph_edge::num_speculative_call_targets_p (void)
4085 return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4088 /* Check if function calls comdat local. This is used to recompute
4089 calls_comdat_local flag after function transformations. */
4090 bool
4091 cgraph_node::check_calls_comdat_local_p ()
4093 for (cgraph_edge *e = callees; e; e = e->next_callee)
4094 if (e->inline_failed
4095 ? e->callee->comdat_local_p ()
4096 : e->callee->check_calls_comdat_local_p ())
4097 return true;
4098 return false;
4101 /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4102 This needs to be a global so that it can be a GC root, and thus
4103 prevent the stashed copy from being garbage-collected if the GC runs
4104 during a symbol_table_test. */
4106 symbol_table *saved_symtab;
4108 #if CHECKING_P
4110 namespace selftest {
4112 /* class selftest::symbol_table_test. */
4114 /* Constructor. Store the old value of symtab, and create a new one. */
4116 symbol_table_test::symbol_table_test ()
4118 gcc_assert (saved_symtab == NULL);
4119 saved_symtab = symtab;
4120 symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4123 /* Destructor. Restore the old value of symtab. */
4125 symbol_table_test::~symbol_table_test ()
4127 gcc_assert (saved_symtab != NULL);
4128 symtab = saved_symtab;
4129 saved_symtab = NULL;
4132 /* Verify that symbol_table_test works. */
4134 static void
4135 test_symbol_table_test ()
4137 /* Simulate running two selftests involving symbol tables. */
4138 for (int i = 0; i < 2; i++)
4140 symbol_table_test stt;
4141 tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4142 get_identifier ("test_decl"),
4143 build_function_type_list (void_type_node,
4144 NULL_TREE));
4145 cgraph_node *node = cgraph_node::get_create (test_decl);
4146 gcc_assert (node);
4148 /* Verify that the node has order 0 on both iterations,
4149 and thus that nodes have predictable dump names in selftests. */
4150 ASSERT_EQ (node->order, 0);
4151 ASSERT_STREQ (node->dump_name (), "test_decl/0");
4155 /* Run all of the selftests within this file. */
4157 void
4158 cgraph_c_tests ()
4160 test_symbol_table_test ();
4163 } // namespace selftest
4165 /* Return true if this node represents a former, i.e. an expanded, thunk. */
4167 bool
4168 cgraph_node::former_thunk_p (void)
4170 if (thunk)
4171 return false;
4172 thunk_info *i = thunk_info::get (this);
4173 if (!i)
4174 return false;
4175 gcc_checking_assert (i->fixed_offset || i->virtual_offset_p
4176 || i->indirect_offset);
4177 return true;
4180 #endif /* CHECKING_P */
4182 #include "gt-cgraph.h"