Fix PR 93568 (thinko)
[official-gcc.git] / gcc / cgraph.c
blob294b2d392fdbec82363175f8bc4f7e8720a66f44
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"
67 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
68 #include "tree-pass.h"
70 /* Queue of cgraph nodes scheduled to be lowered. */
71 symtab_node *x_cgraph_nodes_queue;
72 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
74 /* Symbol table global context. */
75 symbol_table *symtab;
77 /* List of hooks triggered on cgraph_edge events. */
78 struct cgraph_edge_hook_list {
79 cgraph_edge_hook hook;
80 void *data;
81 struct cgraph_edge_hook_list *next;
84 /* List of hooks triggered on cgraph_node events. */
85 struct cgraph_node_hook_list {
86 cgraph_node_hook hook;
87 void *data;
88 struct cgraph_node_hook_list *next;
91 /* List of hooks triggered on events involving two cgraph_edges. */
92 struct cgraph_2edge_hook_list {
93 cgraph_2edge_hook hook;
94 void *data;
95 struct cgraph_2edge_hook_list *next;
98 /* List of hooks triggered on events involving two cgraph_nodes. */
99 struct cgraph_2node_hook_list {
100 cgraph_2node_hook hook;
101 void *data;
102 struct cgraph_2node_hook_list *next;
105 /* Hash descriptor for cgraph_function_version_info. */
107 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
109 static hashval_t hash (cgraph_function_version_info *);
110 static bool equal (cgraph_function_version_info *,
111 cgraph_function_version_info *);
114 /* Map a cgraph_node to cgraph_function_version_info using this htab.
115 The cgraph_function_version_info has a THIS_NODE field that is the
116 corresponding cgraph_node.. */
118 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
120 /* Hash function for cgraph_fnver_htab. */
121 hashval_t
122 function_version_hasher::hash (cgraph_function_version_info *ptr)
124 int uid = ptr->this_node->get_uid ();
125 return (hashval_t)(uid);
128 /* eq function for cgraph_fnver_htab. */
129 bool
130 function_version_hasher::equal (cgraph_function_version_info *n1,
131 cgraph_function_version_info *n2)
133 return n1->this_node->get_uid () == n2->this_node->get_uid ();
136 /* Mark as GC root all allocated nodes. */
137 static GTY(()) struct cgraph_function_version_info *
138 version_info_node = NULL;
140 /* Return true if NODE's address can be compared. */
142 bool
143 symtab_node::address_can_be_compared_p ()
145 /* Address of virtual tables and functions is never compared. */
146 if (DECL_VIRTUAL_P (decl))
147 return false;
148 /* Address of C++ cdtors is never compared. */
149 if (is_a <cgraph_node *> (this)
150 && (DECL_CXX_CONSTRUCTOR_P (decl)
151 || DECL_CXX_DESTRUCTOR_P (decl)))
152 return false;
153 /* Constant pool symbols addresses are never compared.
154 flag_merge_constants permits us to assume the same on readonly vars. */
155 if (is_a <varpool_node *> (this)
156 && (DECL_IN_CONSTANT_POOL (decl)
157 || (flag_merge_constants >= 2
158 && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
159 return false;
160 return true;
163 /* Get the cgraph_function_version_info node corresponding to node. */
164 cgraph_function_version_info *
165 cgraph_node::function_version (void)
167 cgraph_function_version_info key;
168 key.this_node = this;
170 if (cgraph_fnver_htab == NULL)
171 return NULL;
173 return cgraph_fnver_htab->find (&key);
176 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
177 corresponding to cgraph_node NODE. */
178 cgraph_function_version_info *
179 cgraph_node::insert_new_function_version (void)
181 version_info_node = NULL;
182 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
183 version_info_node->this_node = this;
185 if (cgraph_fnver_htab == NULL)
186 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
188 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
189 = version_info_node;
190 return version_info_node;
193 /* Remove the cgraph_function_version_info node given by DECL_V. */
194 static void
195 delete_function_version (cgraph_function_version_info *decl_v)
197 if (decl_v == NULL)
198 return;
200 if (version_info_node == decl_v)
201 version_info_node = NULL;
203 if (decl_v->prev != NULL)
204 decl_v->prev->next = decl_v->next;
206 if (decl_v->next != NULL)
207 decl_v->next->prev = decl_v->prev;
209 if (cgraph_fnver_htab != NULL)
210 cgraph_fnver_htab->remove_elt (decl_v);
213 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
214 DECL is a duplicate declaration. */
215 void
216 cgraph_node::delete_function_version_by_decl (tree decl)
218 cgraph_node *decl_node = cgraph_node::get (decl);
220 if (decl_node == NULL)
221 return;
223 delete_function_version (decl_node->function_version ());
225 decl_node->remove ();
228 /* Record that DECL1 and DECL2 are semantically identical function
229 versions. */
230 void
231 cgraph_node::record_function_versions (tree decl1, tree decl2)
233 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
234 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
235 cgraph_function_version_info *decl1_v = NULL;
236 cgraph_function_version_info *decl2_v = NULL;
237 cgraph_function_version_info *before;
238 cgraph_function_version_info *after;
240 gcc_assert (decl1_node != NULL && decl2_node != NULL);
241 decl1_v = decl1_node->function_version ();
242 decl2_v = decl2_node->function_version ();
244 if (decl1_v != NULL && decl2_v != NULL)
245 return;
247 if (decl1_v == NULL)
248 decl1_v = decl1_node->insert_new_function_version ();
250 if (decl2_v == NULL)
251 decl2_v = decl2_node->insert_new_function_version ();
253 /* Chain decl2_v and decl1_v. All semantically identical versions
254 will be chained together. */
256 before = decl1_v;
257 after = decl2_v;
259 while (before->next != NULL)
260 before = before->next;
262 while (after->prev != NULL)
263 after= after->prev;
265 before->next = after;
266 after->prev = before;
269 /* Initialize callgraph dump file. */
271 void
272 symbol_table::initialize (void)
274 if (!dump_file)
275 dump_file = dump_begin (TDI_cgraph, NULL);
277 if (!ipa_clones_dump_file)
278 ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
281 /* Allocate new callgraph node and insert it into basic data structures. */
283 cgraph_node *
284 symbol_table::create_empty (void)
286 cgraph_count++;
287 return new (ggc_alloc<cgraph_node> ()) cgraph_node (cgraph_max_uid++);
290 /* Register HOOK to be called with DATA on each removed edge. */
291 cgraph_edge_hook_list *
292 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
294 cgraph_edge_hook_list *entry;
295 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
297 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
298 entry->hook = hook;
299 entry->data = data;
300 entry->next = NULL;
301 while (*ptr)
302 ptr = &(*ptr)->next;
303 *ptr = entry;
304 return entry;
307 /* Remove ENTRY from the list of hooks called on removing edges. */
308 void
309 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
311 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
313 while (*ptr != entry)
314 ptr = &(*ptr)->next;
315 *ptr = entry->next;
316 free (entry);
319 /* Call all edge removal hooks. */
320 void
321 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
323 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
324 while (entry)
326 entry->hook (e, entry->data);
327 entry = entry->next;
331 /* Register HOOK to be called with DATA on each removed node. */
332 cgraph_node_hook_list *
333 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
335 cgraph_node_hook_list *entry;
336 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
338 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
339 entry->hook = hook;
340 entry->data = data;
341 entry->next = NULL;
342 while (*ptr)
343 ptr = &(*ptr)->next;
344 *ptr = entry;
345 return entry;
348 /* Remove ENTRY from the list of hooks called on removing nodes. */
349 void
350 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
352 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
354 while (*ptr != entry)
355 ptr = &(*ptr)->next;
356 *ptr = entry->next;
357 free (entry);
360 /* Call all node removal hooks. */
361 void
362 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
364 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
365 while (entry)
367 entry->hook (node, entry->data);
368 entry = entry->next;
372 /* Call all node removal hooks. */
373 void
374 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
376 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
377 while (entry)
379 entry->hook (node, entry->data);
380 entry = entry->next;
385 /* Register HOOK to be called with DATA on each inserted node. */
386 cgraph_node_hook_list *
387 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
389 cgraph_node_hook_list *entry;
390 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
392 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
393 entry->hook = hook;
394 entry->data = data;
395 entry->next = NULL;
396 while (*ptr)
397 ptr = &(*ptr)->next;
398 *ptr = entry;
399 return entry;
402 /* Remove ENTRY from the list of hooks called on inserted nodes. */
403 void
404 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
406 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
408 while (*ptr != entry)
409 ptr = &(*ptr)->next;
410 *ptr = entry->next;
411 free (entry);
414 /* Register HOOK to be called with DATA on each duplicated edge. */
415 cgraph_2edge_hook_list *
416 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
418 cgraph_2edge_hook_list *entry;
419 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
421 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
422 entry->hook = hook;
423 entry->data = data;
424 entry->next = NULL;
425 while (*ptr)
426 ptr = &(*ptr)->next;
427 *ptr = entry;
428 return entry;
431 /* Remove ENTRY from the list of hooks called on duplicating edges. */
432 void
433 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
435 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
437 while (*ptr != entry)
438 ptr = &(*ptr)->next;
439 *ptr = entry->next;
440 free (entry);
443 /* Call all edge duplication hooks. */
444 void
445 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
447 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
448 while (entry)
450 entry->hook (cs1, cs2, entry->data);
451 entry = entry->next;
455 /* Register HOOK to be called with DATA on each duplicated node. */
456 cgraph_2node_hook_list *
457 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
459 cgraph_2node_hook_list *entry;
460 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
462 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
463 entry->hook = hook;
464 entry->data = data;
465 entry->next = NULL;
466 while (*ptr)
467 ptr = &(*ptr)->next;
468 *ptr = entry;
469 return entry;
472 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
473 void
474 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
476 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
478 while (*ptr != entry)
479 ptr = &(*ptr)->next;
480 *ptr = entry->next;
481 free (entry);
484 /* Call all node duplication hooks. */
485 void
486 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
487 cgraph_node *node2)
489 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
490 while (entry)
492 entry->hook (node, node2, entry->data);
493 entry = entry->next;
497 /* Return cgraph node assigned to DECL. Create new one when needed. */
499 cgraph_node *
500 cgraph_node::create (tree decl)
502 cgraph_node *node = symtab->create_empty ();
503 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
505 node->decl = decl;
507 if ((flag_openacc || flag_openmp)
508 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
510 node->offloadable = 1;
511 if (ENABLE_OFFLOADING)
512 g->have_offload = true;
515 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
516 node->ifunc_resolver = true;
518 node->register_symbol ();
520 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
522 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
523 node->next_nested = node->origin->nested;
524 node->origin->nested = node;
526 return node;
529 /* Try to find a call graph node for declaration DECL and if it does not exist
530 or if it corresponds to an inline clone, create a new one. */
532 cgraph_node *
533 cgraph_node::get_create (tree decl)
535 cgraph_node *first_clone = cgraph_node::get (decl);
537 if (first_clone && !first_clone->inlined_to)
538 return first_clone;
540 cgraph_node *node = cgraph_node::create (decl);
541 if (first_clone)
543 first_clone->clone_of = node;
544 node->clones = first_clone;
545 node->order = first_clone->order;
546 symtab->symtab_prevail_in_asm_name_hash (node);
547 node->decl->decl_with_vis.symtab_node = node;
548 if (dump_file)
549 fprintf (dump_file, "Introduced new external node "
550 "(%s) and turned into root of the clone tree.\n",
551 node->dump_name ());
553 else if (dump_file)
554 fprintf (dump_file, "Introduced new external node "
555 "(%s).\n", node->dump_name ());
556 return node;
559 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
560 the function body is associated with (not necessarily cgraph_node (DECL). */
562 cgraph_node *
563 cgraph_node::create_alias (tree alias, tree target)
565 cgraph_node *alias_node;
567 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
568 || TREE_CODE (target) == IDENTIFIER_NODE);
569 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
570 alias_node = cgraph_node::get_create (alias);
571 gcc_assert (!alias_node->definition);
572 alias_node->alias_target = target;
573 alias_node->definition = true;
574 alias_node->alias = true;
575 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
576 alias_node->transparent_alias = alias_node->weakref = true;
577 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
578 alias_node->ifunc_resolver = true;
579 return alias_node;
582 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
583 and NULL otherwise.
584 Same body aliases are output whenever the body of DECL is output,
585 and cgraph_node::get (ALIAS) transparently returns
586 cgraph_node::get (DECL). */
588 cgraph_node *
589 cgraph_node::create_same_body_alias (tree alias, tree decl)
591 cgraph_node *n;
593 /* If aliases aren't supported by the assembler, fail. */
594 if (!TARGET_SUPPORTS_ALIASES)
595 return NULL;
597 /* Langhooks can create same body aliases of symbols not defined.
598 Those are useless. Drop them on the floor. */
599 if (symtab->global_info_ready)
600 return NULL;
602 n = cgraph_node::create_alias (alias, decl);
603 n->cpp_implicit_alias = true;
604 if (symtab->cpp_implicit_aliases_done)
605 n->resolve_alias (cgraph_node::get (decl));
606 return n;
609 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
610 aliases DECL with an adjustments made into the first parameter.
611 See comments in struct cgraph_thunk_info for detail on the parameters. */
613 cgraph_node *
614 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
615 HOST_WIDE_INT fixed_offset,
616 HOST_WIDE_INT virtual_value,
617 HOST_WIDE_INT indirect_offset,
618 tree virtual_offset,
619 tree real_alias)
621 cgraph_node *node;
623 node = cgraph_node::get (alias);
624 if (node)
625 node->reset ();
626 else
627 node = cgraph_node::create (alias);
629 /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE. */
630 gcc_checking_assert (virtual_offset
631 ? virtual_value == wi::to_wide (virtual_offset)
632 : virtual_value == 0);
634 node->thunk.fixed_offset = fixed_offset;
635 node->thunk.virtual_value = virtual_value;
636 node->thunk.indirect_offset = indirect_offset;
637 node->thunk.alias = real_alias;
638 node->thunk.this_adjusting = this_adjusting;
639 node->thunk.virtual_offset_p = virtual_offset != NULL;
640 node->thunk.thunk_p = true;
641 node->definition = true;
643 return node;
646 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
647 Return NULL if there's no such node. */
649 cgraph_node *
650 cgraph_node::get_for_asmname (tree asmname)
652 /* We do not want to look at inline clones. */
653 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
654 node;
655 node = node->next_sharing_asm_name)
657 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
658 if (cn && !cn->inlined_to)
659 return cn;
661 return NULL;
664 /* Returns a hash value for X (which really is a cgraph_edge). */
666 hashval_t
667 cgraph_edge_hasher::hash (cgraph_edge *e)
669 /* This is a really poor hash function, but it is what htab_hash_pointer
670 uses. */
671 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
674 /* Returns a hash value for X (which really is a cgraph_edge). */
676 hashval_t
677 cgraph_edge_hasher::hash (gimple *call_stmt)
679 /* This is a really poor hash function, but it is what htab_hash_pointer
680 uses. */
681 return (hashval_t) ((intptr_t)call_stmt >> 3);
684 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
686 inline bool
687 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
689 return x->call_stmt == y;
692 /* Add call graph edge E to call site hash of its caller. */
694 static inline void
695 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
697 gimple *call = e->call_stmt;
698 *e->caller->call_site_hash->find_slot_with_hash
699 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
702 /* Add call graph edge E to call site hash of its caller. */
704 static inline void
705 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
707 /* There are two speculative edges for every statement (one direct,
708 one indirect); always hash the direct one. */
709 if (e->speculative && e->indirect_unknown_callee)
710 return;
711 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
712 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
713 if (*slot)
715 gcc_assert (((cgraph_edge *)*slot)->speculative);
716 if (e->callee && (!e->prev_callee
717 || !e->prev_callee->speculative
718 || e->prev_callee->call_stmt != e->call_stmt))
719 *slot = e;
720 return;
722 gcc_assert (!*slot || e->speculative);
723 *slot = e;
726 /* Return the callgraph edge representing the GIMPLE_CALL statement
727 CALL_STMT. */
729 cgraph_edge *
730 cgraph_node::get_edge (gimple *call_stmt)
732 cgraph_edge *e, *e2;
733 int n = 0;
735 if (call_site_hash)
736 return call_site_hash->find_with_hash
737 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
739 /* This loop may turn out to be performance problem. In such case adding
740 hashtables into call nodes with very many edges is probably best
741 solution. It is not good idea to add pointer into CALL_EXPR itself
742 because we want to make possible having multiple cgraph nodes representing
743 different clones of the same body before the body is actually cloned. */
744 for (e = callees; e; e = e->next_callee)
746 if (e->call_stmt == call_stmt)
747 break;
748 n++;
751 if (!e)
752 for (e = indirect_calls; e; e = e->next_callee)
754 if (e->call_stmt == call_stmt)
755 break;
756 n++;
759 if (n > 100)
761 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
762 for (e2 = callees; e2; e2 = e2->next_callee)
763 cgraph_add_edge_to_call_site_hash (e2);
764 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
765 cgraph_add_edge_to_call_site_hash (e2);
768 return e;
772 /* Change field call_stmt of edge E to NEW_STMT. If UPDATE_SPECULATIVE and E
773 is any component of speculative edge, then update all components.
774 Speculations can be resolved in the process and EDGE can be removed and
775 deallocated. Return the edge that now represents the call. */
777 cgraph_edge *
778 cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
779 bool update_speculative)
781 tree decl;
783 /* Speculative edges has three component, update all of them
784 when asked to. */
785 if (update_speculative && e->speculative)
787 cgraph_edge *direct, *indirect, *next;
788 ipa_ref *ref;
789 bool e_indirect = e->indirect_unknown_callee;
790 int n = 0;
792 direct = e->first_speculative_call_target ();
793 indirect = e->speculative_call_indirect_edge ();
795 gcall *old_stmt = direct->call_stmt;
796 for (cgraph_edge *d = direct; d; d = next)
798 next = d->next_speculative_call_target ();
799 cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
800 gcc_assert (d2 == d);
801 n++;
803 gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
804 for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
805 if (ref->speculative && ref->stmt == old_stmt)
807 ref->stmt = new_stmt;
808 n--;
811 indirect = set_call_stmt (indirect, new_stmt, false);
812 return e_indirect ? indirect : direct;
815 /* Only direct speculative edges go to call_site_hash. */
816 if (e->caller->call_site_hash
817 && (!e->speculative || !e->indirect_unknown_callee)
818 /* It is possible that edge was previously speculative. In this case
819 we have different value in call stmt hash which needs preserving. */
820 && e->caller->get_edge (e->call_stmt) == e)
821 e->caller->call_site_hash->remove_elt_with_hash
822 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
824 e->call_stmt = new_stmt;
825 if (e->indirect_unknown_callee
826 && (decl = gimple_call_fndecl (new_stmt)))
828 /* Constant propagation (and possibly also inlining?) can turn an
829 indirect call into a direct one. */
830 cgraph_node *new_callee = cgraph_node::get (decl);
832 gcc_checking_assert (new_callee);
833 e = make_direct (e, new_callee);
836 function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
837 e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
838 /* Update call stite hash. For speculative calls we only record the first
839 direct edge. */
840 if (e->caller->call_site_hash
841 && (!e->speculative
842 || (e->callee
843 && (!e->prev_callee || !e->prev_callee->speculative
844 || e->prev_callee->call_stmt != e->call_stmt))
845 || (e->speculative && !e->callee)))
846 cgraph_add_edge_to_call_site_hash (e);
847 return e;
850 /* Allocate a cgraph_edge structure and fill it with data according to the
851 parameters of which only CALLEE can be NULL (when creating an indirect call
852 edge). CLONING_P should be set if properties that are copied from an
853 original edge should not be calculated. */
855 cgraph_edge *
856 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
857 gcall *call_stmt, profile_count count,
858 bool indir_unknown_callee, bool cloning_p)
860 cgraph_edge *edge;
862 /* LTO does not actually have access to the call_stmt since these
863 have not been loaded yet. */
864 if (call_stmt)
866 /* This is a rather expensive check possibly triggering
867 construction of call stmt hashtable. */
868 cgraph_edge *e;
869 gcc_checking_assert (!(e = caller->get_edge (call_stmt))
870 || e->speculative);
872 gcc_assert (is_gimple_call (call_stmt));
875 edge = ggc_alloc<cgraph_edge> ();
876 edge->m_summary_id = -1;
877 edges_count++;
879 gcc_assert (++edges_max_uid != 0);
880 edge->m_uid = edges_max_uid;
881 edge->aux = NULL;
882 edge->caller = caller;
883 edge->callee = callee;
884 edge->prev_caller = NULL;
885 edge->next_caller = NULL;
886 edge->prev_callee = NULL;
887 edge->next_callee = NULL;
888 edge->lto_stmt_uid = 0;
889 edge->speculative_id = 0;
891 edge->count = count;
892 edge->call_stmt = call_stmt;
893 edge->indirect_info = NULL;
894 edge->indirect_inlining_edge = 0;
895 edge->speculative = false;
896 edge->indirect_unknown_callee = indir_unknown_callee;
897 if (call_stmt && caller->call_site_hash)
898 cgraph_add_edge_to_call_site_hash (edge);
900 if (cloning_p)
901 return edge;
903 edge->can_throw_external
904 = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
905 call_stmt) : false;
906 edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
907 edge->call_stmt_cannot_inline_p = false;
909 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
910 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
911 edge->in_polymorphic_cdtor
912 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
913 caller->decl);
914 else
915 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
917 return edge;
920 /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
921 be set if properties that are copied from an original edge should not be
922 calculated. */
924 cgraph_edge *
925 cgraph_node::create_edge (cgraph_node *callee,
926 gcall *call_stmt, profile_count count, bool cloning_p)
928 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
929 false, cloning_p);
931 if (!cloning_p)
932 initialize_inline_failed (edge);
934 edge->next_caller = callee->callers;
935 if (callee->callers)
936 callee->callers->prev_caller = edge;
937 edge->next_callee = callees;
938 if (callees)
939 callees->prev_callee = edge;
940 callees = edge;
941 callee->callers = edge;
943 return edge;
946 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
948 cgraph_indirect_call_info *
949 cgraph_allocate_init_indirect_info (void)
951 cgraph_indirect_call_info *ii;
953 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
954 ii->param_index = -1;
955 return ii;
958 /* Create an indirect edge with a yet-undetermined callee where the call
959 statement destination is a formal parameter of the caller with index
960 PARAM_INDEX. CLONING_P should be set if properties that are copied from an
961 original edge should not be calculated and indirect_info structure should
962 not be calculated. */
964 cgraph_edge *
965 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
966 profile_count count,
967 bool cloning_p)
969 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
970 cloning_p);
971 tree target;
973 if (!cloning_p)
974 initialize_inline_failed (edge);
976 edge->indirect_info = cgraph_allocate_init_indirect_info ();
977 edge->indirect_info->ecf_flags = ecf_flags;
978 edge->indirect_info->vptr_changed = true;
980 /* Record polymorphic call info. */
981 if (!cloning_p
982 && call_stmt
983 && (target = gimple_call_fn (call_stmt))
984 && virtual_method_call_p (target))
986 ipa_polymorphic_call_context context (decl, target, call_stmt);
988 /* Only record types can have virtual calls. */
989 edge->indirect_info->polymorphic = true;
990 edge->indirect_info->param_index = -1;
991 edge->indirect_info->otr_token
992 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
993 edge->indirect_info->otr_type = obj_type_ref_class (target);
994 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
995 edge->indirect_info->context = context;
998 edge->next_callee = indirect_calls;
999 if (indirect_calls)
1000 indirect_calls->prev_callee = edge;
1001 indirect_calls = edge;
1003 return edge;
1006 /* Remove the edge from the list of the callees of the caller. */
1008 void
1009 cgraph_edge::remove_caller (void)
1011 if (prev_callee)
1012 prev_callee->next_callee = next_callee;
1013 if (next_callee)
1014 next_callee->prev_callee = prev_callee;
1015 if (!prev_callee)
1017 if (indirect_unknown_callee)
1018 caller->indirect_calls = next_callee;
1019 else
1020 caller->callees = next_callee;
1022 if (caller->call_site_hash
1023 && this == caller->get_edge (call_stmt))
1024 caller->call_site_hash->remove_elt_with_hash
1025 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1028 /* Put the edge onto the free list. */
1030 void
1031 symbol_table::free_edge (cgraph_edge *e)
1033 edges_count--;
1034 if (e->m_summary_id != -1)
1035 edge_released_summary_ids.safe_push (e->m_summary_id);
1037 if (e->indirect_info)
1038 ggc_free (e->indirect_info);
1039 ggc_free (e);
1042 /* Remove the edge in the cgraph. */
1044 void
1045 cgraph_edge::remove (cgraph_edge *edge)
1047 /* Call all edge removal hooks. */
1048 symtab->call_edge_removal_hooks (edge);
1050 if (!edge->indirect_unknown_callee)
1051 /* Remove from callers list of the callee. */
1052 edge->remove_callee ();
1054 /* Remove from callees list of the callers. */
1055 edge->remove_caller ();
1057 /* Put the edge onto the free list. */
1058 symtab->free_edge (edge);
1061 /* Turn edge into speculative call calling N2. Update
1062 the profile so the direct call is taken COUNT times
1063 with FREQUENCY.
1065 At clone materialization time, the indirect call E will
1066 be expanded as:
1068 if (call_dest == N2)
1069 n2 ();
1070 else
1071 call call_dest
1073 At this time the function just creates the direct call,
1074 the reference representing the if conditional and attaches
1075 them all to the original indirect call statement.
1077 speculative_id is used to link direct calls with their corresponding
1078 IPA_REF_ADDR references when representing speculative calls.
1080 Return direct edge created. */
1082 cgraph_edge *
1083 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1084 unsigned int speculative_id)
1086 cgraph_node *n = caller;
1087 ipa_ref *ref = NULL;
1088 cgraph_edge *e2;
1090 if (dump_file)
1091 fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1092 n->dump_name (), n2->dump_name ());
1093 speculative = true;
1094 e2 = n->create_edge (n2, call_stmt, direct_count);
1095 initialize_inline_failed (e2);
1096 e2->speculative = true;
1097 if (TREE_NOTHROW (n2->decl))
1098 e2->can_throw_external = false;
1099 else
1100 e2->can_throw_external = can_throw_external;
1101 e2->lto_stmt_uid = lto_stmt_uid;
1102 e2->speculative_id = speculative_id;
1103 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1104 indirect_info->num_speculative_call_targets++;
1105 count -= e2->count;
1106 symtab->call_edge_duplication_hooks (this, e2);
1107 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1108 ref->lto_stmt_uid = lto_stmt_uid;
1109 ref->speculative_id = speculative_id;
1110 ref->speculative = speculative;
1111 n2->mark_address_taken ();
1112 return e2;
1115 /* Speculative call consists of an indirect edge and one or more
1116 direct edge+ref pairs.
1118 Given an edge which is part of speculative call, return the first
1119 direct call edge in the speculative call sequence. */
1121 cgraph_edge *
1122 cgraph_edge::first_speculative_call_target ()
1124 cgraph_edge *e = this;
1126 gcc_checking_assert (e->speculative);
1127 if (e->callee)
1129 while (e->prev_callee && e->prev_callee->speculative
1130 && e->prev_callee->call_stmt == e->call_stmt
1131 && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1132 e = e->prev_callee;
1133 return e;
1135 /* Call stmt site hash always points to the first target of the
1136 speculative call sequence. */
1137 if (e->call_stmt)
1138 return e->caller->get_edge (e->call_stmt);
1139 for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1140 if (e2->speculative
1141 && e->call_stmt == e2->call_stmt
1142 && e->lto_stmt_uid == e2->lto_stmt_uid)
1143 return e2;
1146 /* We always maintain first direct edge in the call site hash, if one
1147 exists. E is going to be removed. See if it is first one and update
1148 hash accordingly. INDIRECT is the indirect edge of speculative call.
1149 We assume that INDIRECT->num_speculative_call_targets_p () is already
1150 updated for removal of E. */
1151 static void
1152 update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1153 cgraph_edge *indirect)
1155 if (e->caller->call_site_hash)
1157 if (e->caller->get_edge (e->call_stmt) != e)
1159 else if (!indirect->num_speculative_call_targets_p ())
1160 cgraph_update_edge_in_call_site_hash (indirect);
1161 else
1163 gcc_checking_assert (e->next_callee && e->next_callee->speculative
1164 && e->next_callee->call_stmt == e->call_stmt);
1165 cgraph_update_edge_in_call_site_hash (e->next_callee);
1170 /* Speculative call EDGE turned out to be direct call to CALLEE_DECL. Remove
1171 the speculative call sequence and return edge representing the call, the
1172 original EDGE can be removed and deallocated. Return the edge that now
1173 represents the call.
1175 For "speculative" indirect call that contains multiple "speculative"
1176 targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1177 decrease the count and only remove current direct edge.
1179 If no speculative direct call left to the speculative indirect call, remove
1180 the speculative of both the indirect call and corresponding direct edge.
1182 It is up to caller to iteratively resolve each "speculative" direct call and
1183 redirect the call as appropriate. */
1185 cgraph_edge *
1186 cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1188 cgraph_edge *e2;
1189 ipa_ref *ref;
1191 gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1192 if (!edge->callee)
1193 e2 = edge->first_speculative_call_target ();
1194 else
1195 e2 = edge;
1196 ref = e2->speculative_call_target_ref ();
1197 edge = edge->speculative_call_indirect_edge ();
1198 if (!callee_decl
1199 || !ref->referred->semantically_equivalent_p
1200 (symtab_node::get (callee_decl)))
1202 if (dump_file)
1204 if (callee_decl)
1206 fprintf (dump_file, "Speculative indirect call %s => %s has "
1207 "turned out to have contradicting known target ",
1208 edge->caller->dump_name (),
1209 e2->callee->dump_name ());
1210 print_generic_expr (dump_file, callee_decl);
1211 fprintf (dump_file, "\n");
1213 else
1215 fprintf (dump_file, "Removing speculative call %s => %s\n",
1216 edge->caller->dump_name (),
1217 e2->callee->dump_name ());
1221 else
1223 cgraph_edge *tmp = edge;
1224 if (dump_file)
1225 fprintf (dump_file, "Speculative call turned into direct call.\n");
1226 edge = e2;
1227 e2 = tmp;
1228 /* FIXME: If EDGE is inlined, we should scale up the frequencies
1229 and counts in the functions inlined through it. */
1231 edge->count += e2->count;
1232 if (edge->num_speculative_call_targets_p ())
1234 /* The indirect edge has multiple speculative targets, don't remove
1235 speculative until all related direct edges are resolved. */
1236 edge->indirect_info->num_speculative_call_targets--;
1237 if (!edge->indirect_info->num_speculative_call_targets)
1238 edge->speculative = false;
1240 else
1241 edge->speculative = false;
1242 e2->speculative = false;
1243 update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1244 ref->remove_reference ();
1245 if (e2->indirect_unknown_callee || e2->inline_failed)
1246 remove (e2);
1247 else
1248 e2->callee->remove_symbol_and_inline_clones ();
1249 return edge;
1252 /* Return edge corresponding to speculative call to a given target.
1253 NULL if speculative call does not have one. */
1255 cgraph_edge *
1256 cgraph_edge::speculative_call_for_target (cgraph_node *target)
1258 for (cgraph_edge *direct = first_speculative_call_target ();
1259 direct;
1260 direct = direct->next_speculative_call_target ())
1261 if (direct->speculative_call_target_ref ()
1262 ->referred->semantically_equivalent_p (target))
1263 return direct;
1264 return NULL;
1267 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1268 CALLEE. Speculations can be resolved in the process and EDGE can be removed
1269 and deallocated. Return the edge that now represents the call. */
1271 cgraph_edge *
1272 cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1274 gcc_assert (edge->indirect_unknown_callee);
1276 /* If we are redirecting speculative call, make it non-speculative. */
1277 if (edge->speculative)
1279 cgraph_edge *found = NULL;
1280 cgraph_edge *direct, *next;
1282 edge = edge->speculative_call_indirect_edge ();
1284 /* Look all speculative targets and remove all but one corresponding
1285 to callee (if it exists). */
1286 for (direct = edge->first_speculative_call_target ();
1287 direct;
1288 direct = next)
1290 next = direct->next_speculative_call_target ();
1292 /* Compare ref not direct->callee. Direct edge is possibly
1293 inlined or redirected. */
1294 if (!direct->speculative_call_target_ref ()
1295 ->referred->semantically_equivalent_p (callee))
1296 edge = direct->resolve_speculation (direct, NULL);
1297 else
1299 gcc_checking_assert (!found);
1300 found = direct;
1304 /* On successful speculation just remove the indirect edge and
1305 return the pre existing direct edge.
1306 It is important to not remove it and redirect because the direct
1307 edge may be inlined or redirected. */
1308 if (found)
1310 cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1311 gcc_checking_assert (!found->speculative && e2 == found);
1312 return found;
1314 gcc_checking_assert (!edge->speculative);
1317 edge->indirect_unknown_callee = 0;
1318 ggc_free (edge->indirect_info);
1319 edge->indirect_info = NULL;
1321 /* Get the edge out of the indirect edge list. */
1322 if (edge->prev_callee)
1323 edge->prev_callee->next_callee = edge->next_callee;
1324 if (edge->next_callee)
1325 edge->next_callee->prev_callee = edge->prev_callee;
1326 if (!edge->prev_callee)
1327 edge->caller->indirect_calls = edge->next_callee;
1329 /* Put it into the normal callee list */
1330 edge->prev_callee = NULL;
1331 edge->next_callee = edge->caller->callees;
1332 if (edge->caller->callees)
1333 edge->caller->callees->prev_callee = edge;
1334 edge->caller->callees = edge;
1336 /* Insert to callers list of the new callee. */
1337 edge->set_callee (callee);
1339 /* We need to re-determine the inlining status of the edge. */
1340 initialize_inline_failed (edge);
1341 return edge;
1344 /* If necessary, change the function declaration in the call statement
1345 associated with E so that it corresponds to the edge callee. Speculations
1346 can be resolved in the process and EDGE can be removed and deallocated.
1348 The edge could be one of speculative direct call generated from speculative
1349 indirect call. In this circumstance, decrease the speculative targets
1350 count (i.e. num_speculative_call_targets) and redirect call stmt to the
1351 corresponding i-th target. If no speculative direct call left to the
1352 speculative indirect call, remove "speculative" of the indirect call and
1353 also redirect stmt to it's final direct target.
1355 It is up to caller to iteratively transform each "speculative"
1356 direct call as appropriate. */
1358 gimple *
1359 cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e)
1361 tree decl = gimple_call_fndecl (e->call_stmt);
1362 gcall *new_stmt;
1363 gimple_stmt_iterator gsi;
1365 if (e->speculative)
1367 /* If there already is an direct call (i.e. as a result of inliner's
1368 substitution), forget about speculating. */
1369 if (decl)
1370 e = make_direct (e->speculative_call_indirect_edge (),
1371 cgraph_node::get (decl));
1372 else
1374 /* Be sure we redirect all speculative targets before poking
1375 abou tindirect edge. */
1376 gcc_checking_assert (e->callee);
1377 cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1378 gcall *new_stmt;
1379 ipa_ref *ref;
1381 /* Expand speculation into GIMPLE code. */
1382 if (dump_file)
1384 fprintf (dump_file,
1385 "Expanding speculative call of %s -> %s count: ",
1386 e->caller->dump_name (),
1387 e->callee->dump_name ());
1388 e->count.dump (dump_file);
1389 fprintf (dump_file, "\n");
1391 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1393 profile_count all = indirect->count;
1394 for (cgraph_edge *e2 = e->first_speculative_call_target ();
1396 e2 = e2->next_speculative_call_target ())
1397 all = all + e2->count;
1398 profile_probability prob = e->count.probability_in (all);
1399 if (!prob.initialized_p ())
1400 prob = profile_probability::even ();
1401 ref = e->speculative_call_target_ref ();
1402 new_stmt = gimple_ic (e->call_stmt,
1403 dyn_cast<cgraph_node *> (ref->referred),
1404 prob);
1405 e->speculative = false;
1406 if (indirect->num_speculative_call_targets_p ())
1408 /* The indirect edge has multiple speculative targets, don't
1409 remove speculative until all related direct edges are
1410 redirected. */
1411 indirect->indirect_info->num_speculative_call_targets--;
1412 if (!indirect->indirect_info->num_speculative_call_targets)
1413 indirect->speculative = false;
1415 else
1416 indirect->speculative = false;
1417 /* Indirect edges are not both in the call site hash.
1418 get it updated. */
1419 update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1420 cgraph_edge::set_call_stmt (e, new_stmt, false);
1421 e->count = gimple_bb (e->call_stmt)->count;
1423 /* Once we are done with expanding the sequence, update also indirect
1424 call probability. Until then the basic block accounts for the
1425 sum of indirect edge and all non-expanded speculations. */
1426 if (!indirect->speculative)
1427 indirect->count = gimple_bb (indirect->call_stmt)->count;
1428 ref->speculative = false;
1429 ref->stmt = NULL;
1430 pop_cfun ();
1431 /* Continue redirecting E to proper target. */
1436 if (e->indirect_unknown_callee
1437 || decl == e->callee->decl)
1438 return e->call_stmt;
1440 if (flag_checking && decl)
1442 cgraph_node *node = cgraph_node::get (decl);
1443 gcc_assert (!node || !node->clone.param_adjustments);
1446 if (symtab->dump_file)
1448 fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1449 e->caller->dump_name (), e->callee->dump_name ());
1450 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1451 if (e->callee->clone.param_adjustments)
1452 e->callee->clone.param_adjustments->dump (symtab->dump_file);
1453 unsigned performed_len
1454 = vec_safe_length (e->caller->clone.performed_splits);
1455 if (performed_len > 0)
1456 fprintf (symtab->dump_file, "Performed splits records:\n");
1457 for (unsigned i = 0; i < performed_len; i++)
1459 ipa_param_performed_split *sm
1460 = &(*e->caller->clone.performed_splits)[i];
1461 print_node_brief (symtab->dump_file, " dummy_decl: ", sm->dummy_decl,
1462 TDF_UID);
1463 fprintf (symtab->dump_file, ", unit_offset: %u\n", sm->unit_offset);
1467 if (ipa_param_adjustments *padjs = e->callee->clone.param_adjustments)
1469 /* We need to defer cleaning EH info on the new statement to
1470 fixup-cfg. We may not have dominator information at this point
1471 and thus would end up with unreachable blocks and have no way
1472 to communicate that we need to run CFG cleanup then. */
1473 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1474 if (lp_nr != 0)
1475 remove_stmt_from_eh_lp (e->call_stmt);
1477 tree old_fntype = gimple_call_fntype (e->call_stmt);
1478 new_stmt = padjs->modify_call (e->call_stmt,
1479 e->caller->clone.performed_splits,
1480 e->callee->decl, false);
1481 cgraph_node *origin = e->callee;
1482 while (origin->clone_of)
1483 origin = origin->clone_of;
1485 if ((origin->former_clone_of
1486 && old_fntype == TREE_TYPE (origin->former_clone_of))
1487 || old_fntype == TREE_TYPE (origin->decl))
1488 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1489 else
1491 tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1492 gimple_call_set_fntype (new_stmt, new_fntype);
1495 if (lp_nr != 0)
1496 add_stmt_to_eh_lp (new_stmt, lp_nr);
1498 else
1500 new_stmt = e->call_stmt;
1501 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1502 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1505 /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1506 adjust gimple_call_fntype too. */
1507 if (gimple_call_noreturn_p (new_stmt)
1508 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1509 && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1510 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1511 == void_type_node))
1512 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1514 /* If the call becomes noreturn, remove the LHS if possible. */
1515 tree lhs = gimple_call_lhs (new_stmt);
1516 if (lhs
1517 && gimple_call_noreturn_p (new_stmt)
1518 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1519 || should_remove_lhs_p (lhs)))
1521 if (TREE_CODE (lhs) == SSA_NAME)
1523 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1524 TREE_TYPE (lhs), NULL);
1525 var = get_or_create_ssa_default_def
1526 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1527 gimple *set_stmt = gimple_build_assign (lhs, var);
1528 gsi = gsi_for_stmt (new_stmt);
1529 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1530 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1532 gimple_call_set_lhs (new_stmt, NULL_TREE);
1533 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1536 /* If new callee has no static chain, remove it. */
1537 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1539 gimple_call_set_chain (new_stmt, NULL);
1540 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1543 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1544 new_stmt);
1546 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1548 if (symtab->dump_file)
1550 fprintf (symtab->dump_file, " updated to:");
1551 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1553 return new_stmt;
1556 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1557 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1558 of OLD_STMT if it was previously call statement.
1559 If NEW_STMT is NULL, the call has been dropped without any
1560 replacement. */
1562 static void
1563 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1564 gimple *old_stmt, tree old_call,
1565 gimple *new_stmt)
1567 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1568 ? gimple_call_fndecl (new_stmt) : 0;
1570 /* We are seeing indirect calls, then there is nothing to update. */
1571 if (!new_call && !old_call)
1572 return;
1573 /* See if we turned indirect call into direct call or folded call to one builtin
1574 into different builtin. */
1575 if (old_call != new_call)
1577 cgraph_edge *e = node->get_edge (old_stmt);
1578 cgraph_edge *ne = NULL;
1579 profile_count count;
1581 if (e)
1583 /* Keep calls marked as dead dead. */
1584 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1585 && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
1587 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1588 as_a <gcall *> (new_stmt));
1589 return;
1591 /* See if the edge is already there and has the correct callee. It
1592 might be so because of indirect inlining has already updated
1593 it. We also might've cloned and redirected the edge. */
1594 if (new_call && e->callee)
1596 cgraph_node *callee = e->callee;
1597 while (callee)
1599 if (callee->decl == new_call
1600 || callee->former_clone_of == new_call)
1602 cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
1603 return;
1605 callee = callee->clone_of;
1609 /* Otherwise remove edge and create new one; we can't simply redirect
1610 since function has changed, so inline plan and other information
1611 attached to edge is invalid. */
1612 count = e->count;
1613 if (e->indirect_unknown_callee || e->inline_failed)
1614 cgraph_edge::remove (e);
1615 else
1616 e->callee->remove_symbol_and_inline_clones ();
1618 else if (new_call)
1620 /* We are seeing new direct call; compute profile info based on BB. */
1621 basic_block bb = gimple_bb (new_stmt);
1622 count = bb->count;
1625 if (new_call)
1627 ne = node->create_edge (cgraph_node::get_create (new_call),
1628 as_a <gcall *> (new_stmt), count);
1629 gcc_assert (ne->inline_failed);
1632 /* We only updated the call stmt; update pointer in cgraph edge.. */
1633 else if (old_stmt != new_stmt)
1634 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1635 as_a <gcall *> (new_stmt));
1638 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1639 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1640 of OLD_STMT before it was updated (updating can happen inplace). */
1642 void
1643 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1644 gimple *new_stmt)
1646 cgraph_node *orig = cgraph_node::get (cfun->decl);
1647 cgraph_node *node;
1649 gcc_checking_assert (orig);
1650 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1651 if (orig->clones)
1652 for (node = orig->clones; node != orig;)
1654 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1655 if (node->clones)
1656 node = node->clones;
1657 else if (node->next_sibling_clone)
1658 node = node->next_sibling_clone;
1659 else
1661 while (node != orig && !node->next_sibling_clone)
1662 node = node->clone_of;
1663 if (node != orig)
1664 node = node->next_sibling_clone;
1670 /* Remove all callees from the node. */
1672 void
1673 cgraph_node::remove_callees (void)
1675 cgraph_edge *e, *f;
1677 /* It is sufficient to remove the edges from the lists of callers of
1678 the callees. The callee list of the node can be zapped with one
1679 assignment. */
1680 for (e = callees; e; e = f)
1682 f = e->next_callee;
1683 symtab->call_edge_removal_hooks (e);
1684 if (!e->indirect_unknown_callee)
1685 e->remove_callee ();
1686 symtab->free_edge (e);
1688 for (e = indirect_calls; e; e = f)
1690 f = e->next_callee;
1691 symtab->call_edge_removal_hooks (e);
1692 if (!e->indirect_unknown_callee)
1693 e->remove_callee ();
1694 symtab->free_edge (e);
1696 indirect_calls = NULL;
1697 callees = NULL;
1698 if (call_site_hash)
1700 call_site_hash->empty ();
1701 call_site_hash = NULL;
1705 /* Remove all callers from the node. */
1707 void
1708 cgraph_node::remove_callers (void)
1710 cgraph_edge *e, *f;
1712 /* It is sufficient to remove the edges from the lists of callees of
1713 the callers. The caller list of the node can be zapped with one
1714 assignment. */
1715 for (e = callers; e; e = f)
1717 f = e->next_caller;
1718 symtab->call_edge_removal_hooks (e);
1719 e->remove_caller ();
1720 symtab->free_edge (e);
1722 callers = NULL;
1725 /* Helper function for cgraph_release_function_body and free_lang_data.
1726 It releases body from function DECL without having to inspect its
1727 possibly non-existent symtab node. */
1729 void
1730 release_function_body (tree decl)
1732 function *fn = DECL_STRUCT_FUNCTION (decl);
1733 if (fn)
1735 if (fn->cfg
1736 && loops_for_fn (fn))
1738 fn->curr_properties &= ~PROP_loops;
1739 loop_optimizer_finalize (fn);
1741 if (fn->gimple_df)
1743 delete_tree_ssa (fn);
1744 fn->eh = NULL;
1746 if (fn->cfg)
1748 gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1749 gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1750 delete_tree_cfg_annotations (fn);
1751 clear_edges (fn);
1752 fn->cfg = NULL;
1754 if (fn->value_histograms)
1755 free_histograms (fn);
1756 gimple_set_body (decl, NULL);
1757 /* Struct function hangs a lot of data that would leak if we didn't
1758 removed all pointers to it. */
1759 ggc_free (fn);
1760 DECL_STRUCT_FUNCTION (decl) = NULL;
1762 DECL_SAVED_TREE (decl) = NULL;
1765 /* Release memory used to represent body of function.
1766 Use this only for functions that are released before being translated to
1767 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1768 are free'd in final.c via free_after_compilation().
1769 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1771 void
1772 cgraph_node::release_body (bool keep_arguments)
1774 ipa_transforms_to_apply.release ();
1775 if (!used_as_abstract_origin && symtab->state != PARSING)
1777 DECL_RESULT (decl) = NULL;
1779 if (!keep_arguments)
1780 DECL_ARGUMENTS (decl) = NULL;
1782 /* If the node is abstract and needed, then do not clear
1783 DECL_INITIAL of its associated function declaration because it's
1784 needed to emit debug info later. */
1785 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1786 DECL_INITIAL (decl) = error_mark_node;
1787 release_function_body (decl);
1788 if (lto_file_data)
1790 lto_free_function_in_decl_state_for_node (this);
1791 lto_file_data = NULL;
1795 /* Remove function from symbol table. */
1797 void
1798 cgraph_node::remove (void)
1800 if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1801 fprintf (symtab->ipa_clones_dump_file,
1802 "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1803 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1804 DECL_SOURCE_COLUMN (decl));
1806 symtab->call_cgraph_removal_hooks (this);
1807 remove_callers ();
1808 remove_callees ();
1809 ipa_transforms_to_apply.release ();
1810 delete_function_version (function_version ());
1812 /* Incremental inlining access removed nodes stored in the postorder list.
1814 force_output = false;
1815 forced_by_abi = false;
1816 cgraph_node *next;
1817 for (cgraph_node *n = nested; n; n = next)
1819 next = n->next_nested;
1820 n->origin = NULL;
1821 n->next_nested = NULL;
1823 nested = NULL;
1824 if (origin)
1826 cgraph_node **node2 = &origin->nested;
1828 while (*node2 != this)
1829 node2 = &(*node2)->next_nested;
1830 *node2 = next_nested;
1832 unregister ();
1833 if (prev_sibling_clone)
1834 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1835 else if (clone_of)
1836 clone_of->clones = next_sibling_clone;
1837 if (next_sibling_clone)
1838 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1839 if (clones)
1841 cgraph_node *n, *next;
1843 if (clone_of)
1845 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1846 n->clone_of = clone_of;
1847 n->clone_of = clone_of;
1848 n->next_sibling_clone = clone_of->clones;
1849 if (clone_of->clones)
1850 clone_of->clones->prev_sibling_clone = n;
1851 clone_of->clones = clones;
1853 else
1855 /* We are removing node with clones. This makes clones inconsistent,
1856 but assume they will be removed subsequently and just keep clone
1857 tree intact. This can happen in unreachable function removal since
1858 we remove unreachable functions in random order, not by bottom-up
1859 walk of clone trees. */
1860 for (n = clones; n; n = next)
1862 next = n->next_sibling_clone;
1863 n->next_sibling_clone = NULL;
1864 n->prev_sibling_clone = NULL;
1865 n->clone_of = NULL;
1870 /* While all the clones are removed after being proceeded, the function
1871 itself is kept in the cgraph even after it is compiled. Check whether
1872 we are done with this body and reclaim it proactively if this is the case.
1874 if (symtab->state != LTO_STREAMING)
1876 cgraph_node *n = cgraph_node::get (decl);
1877 if (!n
1878 || (!n->clones && !n->clone_of && !n->inlined_to
1879 && ((symtab->global_info_ready || in_lto_p)
1880 && (TREE_ASM_WRITTEN (n->decl)
1881 || DECL_EXTERNAL (n->decl)
1882 || !n->analyzed
1883 || (!flag_wpa && n->in_other_partition)))))
1884 release_body ();
1886 else
1888 lto_free_function_in_decl_state_for_node (this);
1889 lto_file_data = NULL;
1892 decl = NULL;
1893 if (call_site_hash)
1895 call_site_hash->empty ();
1896 call_site_hash = NULL;
1899 symtab->release_symbol (this);
1902 /* Likewise indicate that a node is having address taken. */
1904 void
1905 cgraph_node::mark_address_taken (void)
1907 /* Indirect inlining can figure out that all uses of the address are
1908 inlined. */
1909 if (inlined_to)
1911 gcc_assert (cfun->after_inlining);
1912 gcc_assert (callers->indirect_inlining_edge);
1913 return;
1915 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1916 IPA_REF_ADDR reference exists (and thus it should be set on node
1917 representing alias we take address of) and as a test whether address
1918 of the object was taken (and thus it should be set on node alias is
1919 referring to). We should remove the first use and the remove the
1920 following set. */
1921 address_taken = 1;
1922 cgraph_node *node = ultimate_alias_target ();
1923 node->address_taken = 1;
1926 /* Return local info node for the compiled function. */
1928 cgraph_node *
1929 cgraph_node::local_info_node (tree decl)
1931 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1932 cgraph_node *node = get (decl);
1933 if (!node)
1934 return NULL;
1935 return node->ultimate_alias_target ();
1938 /* Return RTL info for the compiled function. */
1940 cgraph_rtl_info *
1941 cgraph_node::rtl_info (const_tree decl)
1943 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1944 cgraph_node *node = get (decl);
1945 if (!node)
1946 return NULL;
1947 enum availability avail;
1948 node = node->ultimate_alias_target (&avail);
1949 if (decl != current_function_decl
1950 && (avail < AVAIL_AVAILABLE
1951 || (node->decl != current_function_decl
1952 && !TREE_ASM_WRITTEN (node->decl))))
1953 return NULL;
1954 /* Allocate if it doesn't exist. */
1955 if (node->rtl == NULL)
1957 node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
1958 SET_HARD_REG_SET (node->rtl->function_used_regs);
1960 return node->rtl;
1963 /* Return a string describing the failure REASON. */
1965 const char*
1966 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1968 #undef DEFCIFCODE
1969 #define DEFCIFCODE(code, type, string) string,
1971 static const char *cif_string_table[CIF_N_REASONS] = {
1972 #include "cif-code.def"
1975 /* Signedness of an enum type is implementation defined, so cast it
1976 to unsigned before testing. */
1977 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1978 return cif_string_table[reason];
1981 /* Return a type describing the failure REASON. */
1983 cgraph_inline_failed_type_t
1984 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1986 #undef DEFCIFCODE
1987 #define DEFCIFCODE(code, type, string) type,
1989 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1990 #include "cif-code.def"
1993 /* Signedness of an enum type is implementation defined, so cast it
1994 to unsigned before testing. */
1995 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1996 return cif_type_table[reason];
1999 /* Names used to print out the availability enum. */
2000 const char * const cgraph_availability_names[] =
2001 {"unset", "not_available", "overwritable", "available", "local"};
2003 /* Output flags of edge to a file F. */
2005 void
2006 cgraph_edge::dump_edge_flags (FILE *f)
2008 if (speculative)
2009 fprintf (f, "(speculative) ");
2010 if (!inline_failed)
2011 fprintf (f, "(inlined) ");
2012 if (call_stmt_cannot_inline_p)
2013 fprintf (f, "(call_stmt_cannot_inline_p) ");
2014 if (indirect_inlining_edge)
2015 fprintf (f, "(indirect_inlining) ");
2016 if (count.initialized_p ())
2018 fprintf (f, "(");
2019 count.dump (f);
2020 fprintf (f, ",");
2021 fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2023 if (can_throw_external)
2024 fprintf (f, "(can throw external) ");
2027 /* Dump call graph node to file F. */
2029 void
2030 cgraph_node::dump (FILE *f)
2032 cgraph_edge *edge;
2034 dump_base (f);
2036 if (inlined_to)
2037 fprintf (f, " Function %s is inline copy in %s\n",
2038 dump_name (),
2039 inlined_to->dump_name ());
2040 if (clone_of)
2041 fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2042 if (symtab->function_flags_ready)
2043 fprintf (f, " Availability: %s\n",
2044 cgraph_availability_names [get_availability ()]);
2046 if (profile_id)
2047 fprintf (f, " Profile id: %i\n",
2048 profile_id);
2049 if (unit_id)
2050 fprintf (f, " Unit id: %i\n",
2051 unit_id);
2052 cgraph_function_version_info *vi = function_version ();
2053 if (vi != NULL)
2055 fprintf (f, " Version info: ");
2056 if (vi->prev != NULL)
2058 fprintf (f, "prev: ");
2059 fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2061 if (vi->next != NULL)
2063 fprintf (f, "next: ");
2064 fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2066 if (vi->dispatcher_resolver != NULL_TREE)
2067 fprintf (f, "dispatcher: %s",
2068 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2070 fprintf (f, "\n");
2072 fprintf (f, " Function flags:");
2073 if (count.initialized_p ())
2075 fprintf (f, " count:");
2076 count.dump (f);
2078 if (tp_first_run > 0)
2079 fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2080 if (origin)
2081 fprintf (f, " nested in:%s", origin->dump_asm_name ());
2082 if (gimple_has_body_p (decl))
2083 fprintf (f, " body");
2084 if (process)
2085 fprintf (f, " process");
2086 if (local)
2087 fprintf (f, " local");
2088 if (redefined_extern_inline)
2089 fprintf (f, " redefined_extern_inline");
2090 if (only_called_at_startup)
2091 fprintf (f, " only_called_at_startup");
2092 if (only_called_at_exit)
2093 fprintf (f, " only_called_at_exit");
2094 if (tm_clone)
2095 fprintf (f, " tm_clone");
2096 if (calls_comdat_local)
2097 fprintf (f, " calls_comdat_local");
2098 if (icf_merged)
2099 fprintf (f, " icf_merged");
2100 if (merged_comdat)
2101 fprintf (f, " merged_comdat");
2102 if (merged_extern_inline)
2103 fprintf (f, " merged_extern_inline");
2104 if (split_part)
2105 fprintf (f, " split_part");
2106 if (indirect_call_target)
2107 fprintf (f, " indirect_call_target");
2108 if (nonfreeing_fn)
2109 fprintf (f, " nonfreeing_fn");
2110 if (DECL_STATIC_CONSTRUCTOR (decl))
2111 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2112 if (DECL_STATIC_DESTRUCTOR (decl))
2113 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2114 if (frequency == NODE_FREQUENCY_HOT)
2115 fprintf (f, " hot");
2116 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2117 fprintf (f, " unlikely_executed");
2118 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2119 fprintf (f, " executed_once");
2120 if (opt_for_fn (decl, optimize_size))
2121 fprintf (f, " optimize_size");
2122 if (parallelized_function)
2123 fprintf (f, " parallelized_function");
2124 if (DECL_IS_OPERATOR_NEW_P (decl))
2125 fprintf (f, " operator_new");
2126 if (DECL_IS_OPERATOR_DELETE_P (decl))
2127 fprintf (f, " operator_delete");
2130 fprintf (f, "\n");
2132 if (thunk.thunk_p)
2134 fprintf (f, " Thunk");
2135 if (thunk.alias)
2136 fprintf (f, " of %s (asm:%s)",
2137 lang_hooks.decl_printable_name (thunk.alias, 2),
2138 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2139 fprintf (f, " fixed offset %i virtual value %i indirect_offset %i "
2140 "has virtual offset %i\n",
2141 (int)thunk.fixed_offset,
2142 (int)thunk.virtual_value,
2143 (int)thunk.indirect_offset,
2144 (int)thunk.virtual_offset_p);
2146 else if (former_thunk_p ())
2147 fprintf (f, " Former thunk fixed offset %i virtual value %i "
2148 "indirect_offset %i has virtual offset %i\n",
2149 (int)thunk.fixed_offset,
2150 (int)thunk.virtual_value,
2151 (int)thunk.indirect_offset,
2152 (int)thunk.virtual_offset_p);
2153 if (alias && thunk.alias
2154 && DECL_P (thunk.alias))
2156 fprintf (f, " Alias of %s",
2157 lang_hooks.decl_printable_name (thunk.alias, 2));
2158 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2159 fprintf (f, " (asm:%s)",
2160 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2161 fprintf (f, "\n");
2164 fprintf (f, " Called by: ");
2166 profile_count sum = profile_count::zero ();
2167 for (edge = callers; edge; edge = edge->next_caller)
2169 fprintf (f, "%s ", edge->caller->dump_asm_name ());
2170 edge->dump_edge_flags (f);
2171 if (edge->count.initialized_p ())
2172 sum += edge->count.ipa ();
2175 fprintf (f, "\n Calls: ");
2176 for (edge = callees; edge; edge = edge->next_callee)
2178 fprintf (f, "%s ", edge->callee->dump_asm_name ());
2179 edge->dump_edge_flags (f);
2181 fprintf (f, "\n");
2183 if (count.ipa ().initialized_p ())
2185 bool ok = true;
2186 bool min = false;
2187 ipa_ref *ref;
2189 FOR_EACH_ALIAS (this, ref)
2190 if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2191 sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2193 if (inlined_to
2194 || (symtab->state < EXPANSION
2195 && ultimate_alias_target () == this && only_called_directly_p ()))
2196 ok = !count.ipa ().differs_from_p (sum);
2197 else if (count.ipa () > profile_count::from_gcov_type (100)
2198 && count.ipa () < sum.apply_scale (99, 100))
2199 ok = false, min = true;
2200 if (!ok)
2202 fprintf (f, " Invalid sum of caller counts ");
2203 sum.dump (f);
2204 if (min)
2205 fprintf (f, ", should be at most ");
2206 else
2207 fprintf (f, ", should be ");
2208 count.ipa ().dump (f);
2209 fprintf (f, "\n");
2213 for (edge = indirect_calls; edge; edge = edge->next_callee)
2215 if (edge->indirect_info->polymorphic)
2217 fprintf (f, " Polymorphic indirect call of type ");
2218 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2219 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2221 else
2222 fprintf (f, " Indirect call");
2223 edge->dump_edge_flags (f);
2224 if (edge->indirect_info->param_index != -1)
2226 fprintf (f, " of param:%i", edge->indirect_info->param_index);
2227 if (edge->indirect_info->agg_contents)
2228 fprintf (f, " loaded from %s %s at offset %i",
2229 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2230 edge->indirect_info->by_ref ? "passed by reference":"",
2231 (int)edge->indirect_info->offset);
2232 if (edge->indirect_info->vptr_changed)
2233 fprintf (f, " (vptr maybe changed)");
2235 fprintf (f, " Num speculative call targets: %i",
2236 edge->indirect_info->num_speculative_call_targets);
2237 fprintf (f, "\n");
2238 if (edge->indirect_info->polymorphic)
2239 edge->indirect_info->context.dump (f);
2243 /* Dump call graph node to file F in graphviz format. */
2245 void
2246 cgraph_node::dump_graphviz (FILE *f)
2248 cgraph_edge *edge;
2250 for (edge = callees; edge; edge = edge->next_callee)
2252 cgraph_node *callee = edge->callee;
2254 fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2259 /* Dump call graph node NODE to stderr. */
2261 DEBUG_FUNCTION void
2262 cgraph_node::debug (void)
2264 dump (stderr);
2267 /* Dump the callgraph to file F. */
2269 void
2270 cgraph_node::dump_cgraph (FILE *f)
2272 cgraph_node *node;
2274 fprintf (f, "callgraph:\n\n");
2275 FOR_EACH_FUNCTION (node)
2276 node->dump (f);
2279 /* Return true when the DECL can possibly be inlined. */
2281 bool
2282 cgraph_function_possibly_inlined_p (tree decl)
2284 if (!symtab->global_info_ready)
2285 return !DECL_UNINLINABLE (decl);
2286 return DECL_POSSIBLY_INLINED (decl);
2289 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2290 void
2291 cgraph_node::unnest (void)
2293 cgraph_node **node2 = &origin->nested;
2294 gcc_assert (origin);
2296 while (*node2 != this)
2297 node2 = &(*node2)->next_nested;
2298 *node2 = next_nested;
2299 origin = NULL;
2302 /* Return function availability. See cgraph.h for description of individual
2303 return values. */
2304 enum availability
2305 cgraph_node::get_availability (symtab_node *ref)
2307 if (ref)
2309 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2310 if (cref)
2311 ref = cref->inlined_to;
2313 enum availability avail;
2314 if (!analyzed)
2315 avail = AVAIL_NOT_AVAILABLE;
2316 else if (local)
2317 avail = AVAIL_LOCAL;
2318 else if (inlined_to)
2319 avail = AVAIL_AVAILABLE;
2320 else if (transparent_alias)
2321 ultimate_alias_target (&avail, ref);
2322 else if (ifunc_resolver
2323 || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2324 avail = AVAIL_INTERPOSABLE;
2325 else if (!externally_visible)
2326 avail = AVAIL_AVAILABLE;
2327 /* If this is a reference from symbol itself and there are no aliases, we
2328 may be sure that the symbol was not interposed by something else because
2329 the symbol itself would be unreachable otherwise.
2331 Also comdat groups are always resolved in groups. */
2332 else if ((this == ref && !has_aliases_p ())
2333 || (ref && get_comdat_group ()
2334 && get_comdat_group () == ref->get_comdat_group ()))
2335 avail = AVAIL_AVAILABLE;
2336 /* Inline functions are safe to be analyzed even if their symbol can
2337 be overwritten at runtime. It is not meaningful to enforce any sane
2338 behavior on replacing inline function by different body. */
2339 else if (DECL_DECLARED_INLINE_P (decl))
2340 avail = AVAIL_AVAILABLE;
2342 /* If the function can be overwritten, return OVERWRITABLE. Take
2343 care at least of two notable extensions - the COMDAT functions
2344 used to share template instantiations in C++ (this is symmetric
2345 to code cp_cannot_inline_tree_fn and probably shall be shared and
2346 the inlinability hooks completely eliminated). */
2348 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2349 avail = AVAIL_INTERPOSABLE;
2350 else avail = AVAIL_AVAILABLE;
2352 return avail;
2355 /* Worker for cgraph_node_can_be_local_p. */
2356 static bool
2357 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2359 return !(!node->force_output
2360 && !node->ifunc_resolver
2361 /* Limitation of gas requires us to output targets of symver aliases
2362 as global symbols. This is binutils PR 25295. */
2363 && !node->symver
2364 && ((DECL_COMDAT (node->decl)
2365 && !node->forced_by_abi
2366 && !node->used_from_object_file_p ()
2367 && !node->same_comdat_group)
2368 || !node->externally_visible));
2371 /* Return true if cgraph_node can be made local for API change.
2372 Extern inline functions and C++ COMDAT functions can be made local
2373 at the expense of possible code size growth if function is used in multiple
2374 compilation units. */
2375 bool
2376 cgraph_node::can_be_local_p (void)
2378 return (!address_taken
2379 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2380 NULL, true));
2383 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2384 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2385 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2386 skipped. */
2387 bool
2388 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2389 (cgraph_node *, void *),
2390 void *data,
2391 bool include_overwritable,
2392 bool exclude_virtual_thunks)
2394 cgraph_edge *e;
2395 ipa_ref *ref;
2396 enum availability avail = AVAIL_AVAILABLE;
2398 if (include_overwritable
2399 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2401 if (callback (this, data))
2402 return true;
2404 FOR_EACH_ALIAS (this, ref)
2406 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2407 if (include_overwritable
2408 || alias->get_availability () > AVAIL_INTERPOSABLE)
2409 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2410 include_overwritable,
2411 exclude_virtual_thunks))
2412 return true;
2414 if (avail <= AVAIL_INTERPOSABLE)
2415 return false;
2416 for (e = callers; e; e = e->next_caller)
2417 if (e->caller->thunk.thunk_p
2418 && (include_overwritable
2419 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2420 && !(exclude_virtual_thunks
2421 && e->caller->thunk.virtual_offset_p))
2422 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2423 include_overwritable,
2424 exclude_virtual_thunks))
2425 return true;
2427 return false;
2430 /* Worker to bring NODE local. */
2432 bool
2433 cgraph_node::make_local (cgraph_node *node, void *)
2435 gcc_checking_assert (node->can_be_local_p ());
2436 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2438 node->make_decl_local ();
2439 node->set_section (NULL);
2440 node->set_comdat_group (NULL);
2441 node->externally_visible = false;
2442 node->forced_by_abi = false;
2443 node->local = true;
2444 node->set_section (NULL);
2445 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2446 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2447 && !flag_incremental_link);
2448 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2449 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2451 return false;
2454 /* Bring cgraph node local. */
2456 void
2457 cgraph_node::make_local (void)
2459 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2462 /* Worker to set nothrow flag. */
2464 static void
2465 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2466 bool *changed)
2468 cgraph_edge *e;
2470 if (nothrow && !TREE_NOTHROW (node->decl))
2472 /* With non-call exceptions we can't say for sure if other function body
2473 was not possibly optimized to still throw. */
2474 if (!non_call || node->binds_to_current_def_p ())
2476 TREE_NOTHROW (node->decl) = true;
2477 *changed = true;
2478 for (e = node->callers; e; e = e->next_caller)
2479 e->can_throw_external = false;
2482 else if (!nothrow && TREE_NOTHROW (node->decl))
2484 TREE_NOTHROW (node->decl) = false;
2485 *changed = true;
2487 ipa_ref *ref;
2488 FOR_EACH_ALIAS (node, ref)
2490 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2491 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2492 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2494 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2495 if (e->caller->thunk.thunk_p
2496 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2497 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2500 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2501 if any to NOTHROW. */
2503 bool
2504 cgraph_node::set_nothrow_flag (bool nothrow)
2506 bool changed = false;
2507 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2509 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2510 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2511 else
2513 ipa_ref *ref;
2515 FOR_EACH_ALIAS (this, ref)
2517 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2518 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2519 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2522 return changed;
2525 /* Worker to set malloc flag. */
2526 static void
2527 set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2529 if (malloc_p && !DECL_IS_MALLOC (node->decl))
2531 DECL_IS_MALLOC (node->decl) = true;
2532 *changed = true;
2535 ipa_ref *ref;
2536 FOR_EACH_ALIAS (node, ref)
2538 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2539 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2540 set_malloc_flag_1 (alias, malloc_p, changed);
2543 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2544 if (e->caller->thunk.thunk_p
2545 && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2546 set_malloc_flag_1 (e->caller, malloc_p, changed);
2549 /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2551 bool
2552 cgraph_node::set_malloc_flag (bool malloc_p)
2554 bool changed = false;
2556 if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2557 set_malloc_flag_1 (this, malloc_p, &changed);
2558 else
2560 ipa_ref *ref;
2562 FOR_EACH_ALIAS (this, ref)
2564 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2565 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2566 set_malloc_flag_1 (alias, malloc_p, &changed);
2569 return changed;
2572 /* Worker to set_const_flag. */
2574 static void
2575 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2576 bool *changed)
2578 /* Static constructors and destructors without a side effect can be
2579 optimized out. */
2580 if (set_const && !looping)
2582 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2584 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2585 *changed = true;
2587 if (DECL_STATIC_DESTRUCTOR (node->decl))
2589 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2590 *changed = true;
2593 if (!set_const)
2595 if (TREE_READONLY (node->decl))
2597 TREE_READONLY (node->decl) = 0;
2598 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2599 *changed = true;
2602 else
2604 /* Consider function:
2606 bool a(int *p)
2608 return *p==*p;
2611 During early optimization we will turn this into:
2613 bool a(int *p)
2615 return true;
2618 Now if this function will be detected as CONST however when interposed
2619 it may end up being just pure. We always must assume the worst
2620 scenario here. */
2621 if (TREE_READONLY (node->decl))
2623 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2625 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2626 *changed = true;
2629 else if (node->binds_to_current_def_p ())
2631 TREE_READONLY (node->decl) = true;
2632 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2633 DECL_PURE_P (node->decl) = false;
2634 *changed = true;
2636 else
2638 if (dump_file && (dump_flags & TDF_DETAILS))
2639 fprintf (dump_file, "Dropping state to PURE because function does "
2640 "not bind to current def.\n");
2641 if (!DECL_PURE_P (node->decl))
2643 DECL_PURE_P (node->decl) = true;
2644 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2645 *changed = true;
2647 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2649 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2650 *changed = true;
2655 ipa_ref *ref;
2656 FOR_EACH_ALIAS (node, ref)
2658 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2659 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2660 set_const_flag_1 (alias, set_const, looping, changed);
2662 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2663 if (e->caller->thunk.thunk_p
2664 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2666 /* Virtual thunks access virtual offset in the vtable, so they can
2667 only be pure, never const. */
2668 if (set_const
2669 && (e->caller->thunk.virtual_offset_p
2670 || !node->binds_to_current_def_p (e->caller)))
2671 *changed |= e->caller->set_pure_flag (true, looping);
2672 else
2673 set_const_flag_1 (e->caller, set_const, looping, changed);
2677 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2678 If SET_CONST if false, clear the flag.
2680 When setting the flag be careful about possible interposition and
2681 do not set the flag for functions that can be interposed and set pure
2682 flag for functions that can bind to other definition.
2684 Return true if any change was done. */
2686 bool
2687 cgraph_node::set_const_flag (bool set_const, bool looping)
2689 bool changed = false;
2690 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2691 set_const_flag_1 (this, set_const, looping, &changed);
2692 else
2694 ipa_ref *ref;
2696 FOR_EACH_ALIAS (this, ref)
2698 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2699 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2700 set_const_flag_1 (alias, set_const, looping, &changed);
2703 return changed;
2706 /* Info used by set_pure_flag_1. */
2708 struct set_pure_flag_info
2710 bool pure;
2711 bool looping;
2712 bool changed;
2715 /* Worker to set_pure_flag. */
2717 static bool
2718 set_pure_flag_1 (cgraph_node *node, void *data)
2720 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2721 /* Static constructors and destructors without a side effect can be
2722 optimized out. */
2723 if (info->pure && !info->looping)
2725 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2727 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2728 info->changed = true;
2730 if (DECL_STATIC_DESTRUCTOR (node->decl))
2732 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2733 info->changed = true;
2736 if (info->pure)
2738 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2740 DECL_PURE_P (node->decl) = true;
2741 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2742 info->changed = true;
2744 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2745 && !info->looping)
2747 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2748 info->changed = true;
2751 else
2753 if (DECL_PURE_P (node->decl))
2755 DECL_PURE_P (node->decl) = false;
2756 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2757 info->changed = true;
2760 return false;
2763 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2764 if any to PURE.
2766 When setting the flag, be careful about possible interposition.
2767 Return true if any change was done. */
2769 bool
2770 cgraph_node::set_pure_flag (bool pure, bool looping)
2772 struct set_pure_flag_info info = {pure, looping, false};
2773 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2774 return info.changed;
2777 /* Return true when cgraph_node cannot return or throw and thus
2778 it is safe to ignore its side effects for IPA analysis. */
2780 bool
2781 cgraph_node::cannot_return_p (void)
2783 int flags = flags_from_decl_or_type (decl);
2784 if (!opt_for_fn (decl, flag_exceptions))
2785 return (flags & ECF_NORETURN) != 0;
2786 else
2787 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2788 == (ECF_NORETURN | ECF_NOTHROW));
2791 /* Return true when call of edge cannot lead to return from caller
2792 and thus it is safe to ignore its side effects for IPA analysis
2793 when computing side effects of the caller.
2794 FIXME: We could actually mark all edges that have no reaching
2795 patch to the exit block or throw to get better results. */
2796 bool
2797 cgraph_edge::cannot_lead_to_return_p (void)
2799 if (caller->cannot_return_p ())
2800 return true;
2801 if (indirect_unknown_callee)
2803 int flags = indirect_info->ecf_flags;
2804 if (!opt_for_fn (caller->decl, flag_exceptions))
2805 return (flags & ECF_NORETURN) != 0;
2806 else
2807 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2808 == (ECF_NORETURN | ECF_NOTHROW));
2810 else
2811 return callee->cannot_return_p ();
2814 /* Return true if the edge may be considered hot. */
2816 bool
2817 cgraph_edge::maybe_hot_p (void)
2819 if (!maybe_hot_count_p (NULL, count.ipa ()))
2820 return false;
2821 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2822 || (callee
2823 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2824 return false;
2825 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2826 && (callee
2827 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2828 return false;
2829 if (opt_for_fn (caller->decl, optimize_size))
2830 return false;
2831 if (caller->frequency == NODE_FREQUENCY_HOT)
2832 return true;
2833 if (!count.initialized_p ())
2834 return true;
2835 cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
2836 if (!where->count.initialized_p ())
2837 return false;
2838 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2840 if (count.apply_scale (2, 1) < where->count.apply_scale (3, 1))
2841 return false;
2843 else if (count.apply_scale (param_hot_bb_frequency_fraction , 1)
2844 < where->count)
2845 return false;
2846 return true;
2849 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2851 static bool
2852 nonremovable_p (cgraph_node *node, void *)
2854 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2857 /* Return true if whole comdat group can be removed if there are no direct
2858 calls to THIS. */
2860 bool
2861 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2863 struct ipa_ref *ref;
2865 /* For local symbols or non-comdat group it is the same as
2866 can_remove_if_no_direct_calls_p. */
2867 if (!externally_visible || !same_comdat_group)
2869 if (DECL_EXTERNAL (decl))
2870 return true;
2871 if (address_taken)
2872 return false;
2873 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2876 if (will_inline && address_taken)
2877 return false;
2879 /* Otherwise check if we can remove the symbol itself and then verify
2880 that only uses of the comdat groups are direct call to THIS
2881 or its aliases. */
2882 if (!can_remove_if_no_direct_calls_and_refs_p ())
2883 return false;
2885 /* Check that all refs come from within the comdat group. */
2886 for (int i = 0; iterate_referring (i, ref); i++)
2887 if (ref->referring->get_comdat_group () != get_comdat_group ())
2888 return false;
2890 struct cgraph_node *target = ultimate_alias_target ();
2891 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2892 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2894 if (!externally_visible)
2895 continue;
2896 if (!next->alias
2897 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2898 return false;
2900 /* If we see different symbol than THIS, be sure to check calls. */
2901 if (next->ultimate_alias_target () != target)
2902 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2903 if (e->caller->get_comdat_group () != get_comdat_group ()
2904 || will_inline)
2905 return false;
2907 /* If function is not being inlined, we care only about
2908 references outside of the comdat group. */
2909 if (!will_inline)
2910 for (int i = 0; next->iterate_referring (i, ref); i++)
2911 if (ref->referring->get_comdat_group () != get_comdat_group ())
2912 return false;
2914 return true;
2917 /* Return true when function cgraph_node can be expected to be removed
2918 from program when direct calls in this compilation unit are removed.
2920 As a special case COMDAT functions are
2921 cgraph_can_remove_if_no_direct_calls_p while the are not
2922 cgraph_only_called_directly_p (it is possible they are called from other
2923 unit)
2925 This function behaves as cgraph_only_called_directly_p because eliminating
2926 all uses of COMDAT function does not make it necessarily disappear from
2927 the program unless we are compiling whole program or we do LTO. In this
2928 case we know we win since dynamic linking will not really discard the
2929 linkonce section. */
2931 bool
2932 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2933 (bool will_inline)
2935 gcc_assert (!inlined_to);
2936 if (DECL_EXTERNAL (decl))
2937 return true;
2939 if (!in_lto_p && !flag_whole_program)
2941 /* If the symbol is in comdat group, we need to verify that whole comdat
2942 group becomes unreachable. Technically we could skip references from
2943 within the group, too. */
2944 if (!only_called_directly_p ())
2945 return false;
2946 if (same_comdat_group && externally_visible)
2948 struct cgraph_node *target = ultimate_alias_target ();
2950 if (will_inline && address_taken)
2951 return true;
2952 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2953 next != this;
2954 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2956 if (!externally_visible)
2957 continue;
2958 if (!next->alias
2959 && !next->only_called_directly_p ())
2960 return false;
2962 /* If we see different symbol than THIS,
2963 be sure to check calls. */
2964 if (next->ultimate_alias_target () != target)
2965 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2966 if (e->caller->get_comdat_group () != get_comdat_group ()
2967 || will_inline)
2968 return false;
2971 return true;
2973 else
2974 return can_remove_if_no_direct_calls_p (will_inline);
2978 /* Worker for cgraph_only_called_directly_p. */
2980 static bool
2981 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2983 return !node->only_called_directly_or_aliased_p ();
2986 /* Return true when function cgraph_node and all its aliases are only called
2987 directly.
2988 i.e. it is not externally visible, address was not taken and
2989 it is not used in any other non-standard way. */
2991 bool
2992 cgraph_node::only_called_directly_p (void)
2994 gcc_assert (ultimate_alias_target () == this);
2995 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2996 NULL, true);
3000 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3002 static bool
3003 collect_callers_of_node_1 (cgraph_node *node, void *data)
3005 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3006 cgraph_edge *cs;
3007 enum availability avail;
3008 node->ultimate_alias_target (&avail);
3010 if (avail > AVAIL_INTERPOSABLE)
3011 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3012 if (!cs->indirect_inlining_edge
3013 && !cs->caller->thunk.thunk_p)
3014 redirect_callers->safe_push (cs);
3015 return false;
3018 /* Collect all callers of cgraph_node and its aliases that are known to lead to
3019 cgraph_node (i.e. are not overwritable). */
3021 vec<cgraph_edge *>
3022 cgraph_node::collect_callers (void)
3024 vec<cgraph_edge *> redirect_callers = vNULL;
3025 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3026 &redirect_callers, false);
3027 return redirect_callers;
3031 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
3032 optimistically true if this cannot be determined. */
3034 static bool
3035 clone_of_p (cgraph_node *node, cgraph_node *node2)
3037 node = node->ultimate_alias_target ();
3038 node2 = node2->ultimate_alias_target ();
3040 if (node2->clone_of == node
3041 || node2->former_clone_of == node->decl)
3042 return true;
3044 if (!node->thunk.thunk_p && !node->former_thunk_p ())
3046 while (node2 && node->decl != node2->decl)
3047 node2 = node2->clone_of;
3048 return node2 != NULL;
3051 /* There are no virtual clones of thunks so check former_clone_of or if we
3052 might have skipped thunks because this adjustments are no longer
3053 necessary. */
3054 while (node->thunk.thunk_p || node->former_thunk_p ())
3056 if (!node->thunk.this_adjusting)
3057 return false;
3058 /* In case of instrumented expanded thunks, which can have multiple calls
3059 in them, we do not know how to continue and just have to be
3060 optimistic. */
3061 if (node->callees->next_callee)
3062 return true;
3063 node = node->callees->callee->ultimate_alias_target ();
3065 if (!node2->clone.param_adjustments
3066 || node2->clone.param_adjustments->first_param_intact_p ())
3067 return false;
3068 if (node2->former_clone_of == node->decl)
3069 return true;
3071 cgraph_node *n2 = node2;
3072 while (n2 && node->decl != n2->decl)
3073 n2 = n2->clone_of;
3074 if (n2)
3075 return true;
3078 return false;
3081 /* Verify edge count and frequency. */
3083 bool
3084 cgraph_edge::verify_count ()
3086 bool error_found = false;
3087 if (!count.verify ())
3089 error ("caller edge count invalid");
3090 error_found = true;
3092 return error_found;
3095 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3096 static void
3097 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3099 bool fndecl_was_null = false;
3100 /* debug_gimple_stmt needs correct cfun */
3101 if (cfun != this_cfun)
3102 set_cfun (this_cfun);
3103 /* ...and an actual current_function_decl */
3104 if (!current_function_decl)
3106 current_function_decl = this_cfun->decl;
3107 fndecl_was_null = true;
3109 debug_gimple_stmt (stmt);
3110 if (fndecl_was_null)
3111 current_function_decl = NULL;
3114 /* Verify that call graph edge corresponds to DECL from the associated
3115 statement. Return true if the verification should fail. */
3117 bool
3118 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3120 cgraph_node *node;
3122 if (!decl || callee->inlined_to)
3123 return false;
3124 if (symtab->state == LTO_STREAMING)
3125 return false;
3126 node = cgraph_node::get (decl);
3128 /* We do not know if a node from a different partition is an alias or what it
3129 aliases and therefore cannot do the former_clone_of check reliably. When
3130 body_removed is set, we have lost all information about what was alias or
3131 thunk of and also cannot proceed. */
3132 if (!node
3133 || node->body_removed
3134 || node->in_other_partition
3135 || callee->icf_merged
3136 || callee->in_other_partition)
3137 return false;
3139 node = node->ultimate_alias_target ();
3141 /* Optimizers can redirect unreachable calls or calls triggering undefined
3142 behavior to builtin_unreachable. */
3144 if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE))
3145 return false;
3147 if (callee->former_clone_of != node->decl
3148 && (node != callee->ultimate_alias_target ())
3149 && !clone_of_p (node, callee))
3150 return true;
3151 else
3152 return false;
3155 /* Disable warnings about missing quoting in GCC diagnostics for
3156 the verification errors. Their format strings don't follow GCC
3157 diagnostic conventions and the calls are ultimately followed by
3158 one to internal_error. */
3159 #if __GNUC__ >= 10
3160 # pragma GCC diagnostic push
3161 # pragma GCC diagnostic ignored "-Wformat-diag"
3162 #endif
3164 /* Verify consistency of speculative call in NODE corresponding to STMT
3165 and LTO_STMT_UID. If INDIRECT is set, assume that it is the indirect
3166 edge of call sequence. Return true if error is found.
3168 This function is called to every component of indirect call (direct edges,
3169 indirect edge and refs). To save duplicated work, do full testing only
3170 in that case. */
3171 static bool
3172 verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3173 unsigned int lto_stmt_uid,
3174 struct cgraph_edge *indirect)
3176 if (indirect == NULL)
3178 for (indirect = node->indirect_calls; indirect;
3179 indirect = indirect->next_callee)
3180 if (indirect->call_stmt == stmt
3181 && indirect->lto_stmt_uid == lto_stmt_uid)
3182 break;
3183 if (!indirect)
3185 error ("missing indirect call in speculative call sequence");
3186 return true;
3188 if (!indirect->speculative)
3190 error ("indirect call in speculative call sequence has no "
3191 "speculative flag");
3192 return true;
3194 return false;
3197 /* Maximal number of targets. We probably will never want to have more than
3198 this. */
3199 const unsigned int num = 256;
3200 cgraph_edge *direct_calls[num];
3201 ipa_ref *refs[num];
3203 for (unsigned int i = 0; i < num; i++)
3205 direct_calls[i] = NULL;
3206 refs[i] = NULL;
3209 cgraph_edge *first_call = NULL;
3210 cgraph_edge *prev_call = NULL;
3212 for (cgraph_edge *direct = node->callees; direct;
3213 direct = direct->next_callee)
3214 if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3216 if (!first_call)
3217 first_call = direct;
3218 if (prev_call && direct != prev_call->next_callee)
3220 error ("speculative edges are not adjacent");
3221 return true;
3223 prev_call = direct;
3224 if (!direct->speculative)
3226 error ("direct call to %s in speculative call sequence has no "
3227 "speculative flag", direct->callee->dump_name ());
3228 return true;
3230 if (direct->speculative_id >= num)
3232 error ("direct call to %s in speculative call sequence has "
3233 "speculative_uid %i out of range",
3234 direct->callee->dump_name (), direct->speculative_id);
3235 return true;
3237 if (direct_calls[direct->speculative_id])
3239 error ("duplicate direct call to %s in speculative call sequence "
3240 "with speculative_uid %i",
3241 direct->callee->dump_name (), direct->speculative_id);
3242 return true;
3244 direct_calls[direct->speculative_id] = direct;
3247 if (first_call->call_stmt
3248 && first_call != node->get_edge (first_call->call_stmt))
3250 error ("call stmt hash does not point to first direct edge of "
3251 "speculative call sequence ");
3252 return true;
3255 ipa_ref *ref;
3256 for (int i = 0; node->iterate_reference (i, ref); i++)
3257 if (ref->speculative
3258 && ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3260 if (ref->speculative_id >= num)
3262 error ("direct call to %s in speculative call sequence has "
3263 "speculative_uid %i out of range",
3264 ref->referred->dump_name (), ref->speculative_id);
3265 return true;
3267 if (refs[ref->speculative_id])
3269 error ("duplicate reference %s in speculative call sequence "
3270 "with speculative_uid %i",
3271 ref->referred->dump_name (), ref->speculative_id);
3272 return true;
3274 refs[ref->speculative_id] = ref;
3277 int num_targets = 0;
3278 for (unsigned int i = 0 ; i < num ; i++)
3280 if (refs[i] && !direct_calls[i])
3282 error ("missing direct call for speculation %i", i);
3283 return true;
3285 if (!refs[i] && direct_calls[i])
3287 error ("missing ref for speculation %i", i);
3288 return true;
3290 if (refs[i] != NULL)
3291 num_targets++;
3294 if (num_targets != indirect->num_speculative_call_targets_p ())
3296 error ("number of speculative targets %i mismatched with "
3297 "num_speculative_targets %i",
3298 num_targets,
3299 indirect->num_speculative_call_targets_p ());
3300 return true;
3302 return false;
3305 /* Verify cgraph nodes of given cgraph node. */
3306 DEBUG_FUNCTION void
3307 cgraph_node::verify_node (void)
3309 cgraph_edge *e;
3310 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3311 basic_block this_block;
3312 gimple_stmt_iterator gsi;
3313 bool error_found = false;
3314 int i;
3315 ipa_ref *ref = NULL;
3317 if (seen_error ())
3318 return;
3320 timevar_push (TV_CGRAPH_VERIFY);
3321 error_found |= verify_base ();
3322 for (e = callees; e; e = e->next_callee)
3323 if (e->aux)
3325 error ("aux field set for edge %s->%s",
3326 identifier_to_locale (e->caller->name ()),
3327 identifier_to_locale (e->callee->name ()));
3328 error_found = true;
3330 if (!count.verify ())
3332 error ("cgraph count invalid");
3333 error_found = true;
3335 if (inlined_to && same_comdat_group)
3337 error ("inline clone in same comdat group list");
3338 error_found = true;
3340 if (inlined_to && !count.compatible_p (inlined_to->count))
3342 error ("inline clone count is not compatible");
3343 count.debug ();
3344 inlined_to->count.debug ();
3345 error_found = true;
3347 if (tp_first_run < 0)
3349 error ("tp_first_run must be non-negative");
3350 error_found = true;
3352 if (!definition && !in_other_partition && local)
3354 error ("local symbols must be defined");
3355 error_found = true;
3357 if (inlined_to && externally_visible)
3359 error ("externally visible inline clone");
3360 error_found = true;
3362 if (inlined_to && address_taken)
3364 error ("inline clone with address taken");
3365 error_found = true;
3367 if (inlined_to && force_output)
3369 error ("inline clone is forced to output");
3370 error_found = true;
3372 if (calls_comdat_local && !same_comdat_group)
3374 error ("calls_comdat_local is set outside of a comdat group");
3375 error_found = true;
3377 for (e = indirect_calls; e; e = e->next_callee)
3379 if (e->aux)
3381 error ("aux field set for indirect edge from %s",
3382 identifier_to_locale (e->caller->name ()));
3383 error_found = true;
3385 if (!e->count.compatible_p (count))
3387 error ("edge count is not compatible with function count");
3388 e->count.debug ();
3389 count.debug ();
3390 error_found = true;
3392 if (!e->indirect_unknown_callee
3393 || !e->indirect_info)
3395 error ("An indirect edge from %s is not marked as indirect or has "
3396 "associated indirect_info, the corresponding statement is: ",
3397 identifier_to_locale (e->caller->name ()));
3398 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3399 error_found = true;
3401 if (e->call_stmt && e->lto_stmt_uid)
3403 error ("edge has both cal_stmt and lto_stmt_uid set");
3404 error_found = true;
3407 bool check_comdat = comdat_local_p ();
3408 for (e = callers; e; e = e->next_caller)
3410 if (e->verify_count ())
3411 error_found = true;
3412 if (check_comdat
3413 && !in_same_comdat_group_p (e->caller))
3415 error ("comdat-local function called by %s outside its comdat",
3416 identifier_to_locale (e->caller->name ()));
3417 error_found = true;
3419 if (!e->inline_failed)
3421 if (inlined_to
3422 != (e->caller->inlined_to
3423 ? e->caller->inlined_to : e->caller))
3425 error ("inlined_to pointer is wrong");
3426 error_found = true;
3428 if (callers->next_caller)
3430 error ("multiple inline callers");
3431 error_found = true;
3434 else
3435 if (inlined_to)
3437 error ("inlined_to pointer set for noninline callers");
3438 error_found = true;
3441 for (e = callees; e; e = e->next_callee)
3443 if (e->verify_count ())
3444 error_found = true;
3445 if (!e->count.compatible_p (count))
3447 error ("edge count is not compatible with function count");
3448 e->count.debug ();
3449 count.debug ();
3450 error_found = true;
3452 if (gimple_has_body_p (e->caller->decl)
3453 && !e->caller->inlined_to
3454 && !e->speculative
3455 /* Optimized out calls are redirected to __builtin_unreachable. */
3456 && (e->count.nonzero_p ()
3457 || ! e->callee->decl
3458 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
3459 && count
3460 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3461 && (!e->count.ipa_p ()
3462 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3464 error ("caller edge count does not match BB count");
3465 fprintf (stderr, "edge count: ");
3466 e->count.dump (stderr);
3467 fprintf (stderr, "\n bb count: ");
3468 gimple_bb (e->call_stmt)->count.dump (stderr);
3469 fprintf (stderr, "\n");
3470 error_found = true;
3472 if (e->call_stmt && e->lto_stmt_uid)
3474 error ("edge has both cal_stmt and lto_stmt_uid set");
3475 error_found = true;
3477 if (e->speculative
3478 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3479 NULL))
3480 error_found = true;
3482 for (e = indirect_calls; e; e = e->next_callee)
3484 if (e->verify_count ())
3485 error_found = true;
3486 if (gimple_has_body_p (e->caller->decl)
3487 && !e->caller->inlined_to
3488 && !e->speculative
3489 && e->count.ipa_p ()
3490 && count
3491 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3492 && (!e->count.ipa_p ()
3493 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3495 error ("indirect call count does not match BB count");
3496 fprintf (stderr, "edge count: ");
3497 e->count.dump (stderr);
3498 fprintf (stderr, "\n bb count: ");
3499 gimple_bb (e->call_stmt)->count.dump (stderr);
3500 fprintf (stderr, "\n");
3501 error_found = true;
3503 if (e->speculative
3504 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3506 error_found = true;
3508 for (i = 0; iterate_reference (i, ref); i++)
3510 if (ref->stmt && ref->lto_stmt_uid)
3512 error ("reference has both cal_stmt and lto_stmt_uid set");
3513 error_found = true;
3515 if (ref->speculative
3516 && verify_speculative_call (this, ref->stmt,
3517 ref->lto_stmt_uid, NULL))
3518 error_found = true;
3521 if (!callers && inlined_to)
3523 error ("inlined_to pointer is set but no predecessors found");
3524 error_found = true;
3526 if (inlined_to == this)
3528 error ("inlined_to pointer refers to itself");
3529 error_found = true;
3532 if (clone_of)
3534 cgraph_node *first_clone = clone_of->clones;
3535 if (first_clone != this)
3537 if (prev_sibling_clone->clone_of != clone_of)
3539 error ("cgraph_node has wrong clone_of");
3540 error_found = true;
3544 if (clones)
3546 cgraph_node *n;
3547 for (n = clones; n; n = n->next_sibling_clone)
3548 if (n->clone_of != this)
3549 break;
3550 if (n)
3552 error ("cgraph_node has wrong clone list");
3553 error_found = true;
3556 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3558 error ("cgraph_node is in clone list but it is not clone");
3559 error_found = true;
3561 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3563 error ("cgraph_node has wrong prev_clone pointer");
3564 error_found = true;
3566 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3568 error ("double linked list of clones corrupted");
3569 error_found = true;
3572 if (analyzed && alias)
3574 bool ref_found = false;
3575 int i;
3576 ipa_ref *ref = NULL;
3578 if (callees)
3580 error ("Alias has call edges");
3581 error_found = true;
3583 for (i = 0; iterate_reference (i, ref); i++)
3584 if (ref->use != IPA_REF_ALIAS)
3586 error ("Alias has non-alias reference");
3587 error_found = true;
3589 else if (ref_found)
3591 error ("Alias has more than one alias reference");
3592 error_found = true;
3594 else
3595 ref_found = true;
3596 if (!ref_found)
3598 error ("Analyzed alias has no reference");
3599 error_found = true;
3603 if (analyzed && thunk.thunk_p)
3605 if (!callees)
3607 error ("No edge out of thunk node");
3608 error_found = true;
3610 else if (callees->next_callee)
3612 error ("More than one edge out of thunk node");
3613 error_found = true;
3615 if (gimple_has_body_p (decl) && !inlined_to)
3617 error ("Thunk is not supposed to have body");
3618 error_found = true;
3621 else if (analyzed && gimple_has_body_p (decl)
3622 && !TREE_ASM_WRITTEN (decl)
3623 && (!DECL_EXTERNAL (decl) || inlined_to)
3624 && !flag_wpa)
3626 if (this_cfun->cfg)
3628 hash_set<gimple *> stmts;
3630 /* Reach the trees by walking over the CFG, and note the
3631 enclosing basic-blocks in the call edges. */
3632 FOR_EACH_BB_FN (this_block, this_cfun)
3634 for (gsi = gsi_start_phis (this_block);
3635 !gsi_end_p (gsi); gsi_next (&gsi))
3636 stmts.add (gsi_stmt (gsi));
3637 for (gsi = gsi_start_bb (this_block);
3638 !gsi_end_p (gsi);
3639 gsi_next (&gsi))
3641 gimple *stmt = gsi_stmt (gsi);
3642 stmts.add (stmt);
3643 if (is_gimple_call (stmt))
3645 cgraph_edge *e = get_edge (stmt);
3646 tree decl = gimple_call_fndecl (stmt);
3647 if (e)
3649 if (e->aux)
3651 error ("shared call_stmt:");
3652 cgraph_debug_gimple_stmt (this_cfun, stmt);
3653 error_found = true;
3655 if (!e->indirect_unknown_callee)
3657 if (e->verify_corresponds_to_fndecl (decl))
3659 error ("edge points to wrong declaration:");
3660 debug_tree (e->callee->decl);
3661 fprintf (stderr," Instead of:");
3662 debug_tree (decl);
3663 error_found = true;
3666 else if (decl)
3668 error ("an indirect edge with unknown callee "
3669 "corresponding to a call_stmt with "
3670 "a known declaration:");
3671 error_found = true;
3672 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3674 e->aux = (void *)1;
3676 else if (decl)
3678 error ("missing callgraph edge for call stmt:");
3679 cgraph_debug_gimple_stmt (this_cfun, stmt);
3680 error_found = true;
3685 for (i = 0; iterate_reference (i, ref); i++)
3686 if (ref->stmt && !stmts.contains (ref->stmt))
3688 error ("reference to dead statement");
3689 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3690 error_found = true;
3693 else
3694 /* No CFG available?! */
3695 gcc_unreachable ();
3697 for (e = callees; e; e = e->next_callee)
3699 if (!e->aux && !e->speculative)
3701 error ("edge %s->%s has no corresponding call_stmt",
3702 identifier_to_locale (e->caller->name ()),
3703 identifier_to_locale (e->callee->name ()));
3704 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3705 error_found = true;
3707 e->aux = 0;
3709 for (e = indirect_calls; e; e = e->next_callee)
3711 if (!e->aux && !e->speculative)
3713 error ("an indirect edge from %s has no corresponding call_stmt",
3714 identifier_to_locale (e->caller->name ()));
3715 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3716 error_found = true;
3718 e->aux = 0;
3722 if (nested != NULL)
3724 for (cgraph_node *n = nested; n != NULL; n = n->next_nested)
3726 if (n->origin == NULL)
3728 error ("missing origin for a node in a nested list");
3729 error_found = true;
3731 else if (n->origin != this)
3733 error ("origin points to a different parent");
3734 error_found = true;
3735 break;
3739 if (next_nested != NULL && origin == NULL)
3741 error ("missing origin for a node in a nested list");
3742 error_found = true;
3745 if (error_found)
3747 dump (stderr);
3748 internal_error ("verify_cgraph_node failed");
3750 timevar_pop (TV_CGRAPH_VERIFY);
3753 /* Verify whole cgraph structure. */
3754 DEBUG_FUNCTION void
3755 cgraph_node::verify_cgraph_nodes (void)
3757 cgraph_node *node;
3759 if (seen_error ())
3760 return;
3762 FOR_EACH_FUNCTION (node)
3763 node->verify ();
3766 #if __GNUC__ >= 10
3767 # pragma GCC diagnostic pop
3768 #endif
3770 /* Walk the alias chain to return the function cgraph_node is alias of.
3771 Walk through thunks, too.
3772 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3773 When REF is non-NULL, assume that reference happens in symbol REF
3774 when determining the availability. */
3776 cgraph_node *
3777 cgraph_node::function_symbol (enum availability *availability,
3778 struct symtab_node *ref)
3780 cgraph_node *node = ultimate_alias_target (availability, ref);
3782 while (node->thunk.thunk_p)
3784 ref = node;
3785 node = node->callees->callee;
3786 if (availability)
3788 enum availability a;
3789 a = node->get_availability (ref);
3790 if (a < *availability)
3791 *availability = a;
3793 node = node->ultimate_alias_target (availability, ref);
3795 return node;
3798 /* Walk the alias chain to return the function cgraph_node is alias of.
3799 Walk through non virtual thunks, too. Thus we return either a function
3800 or a virtual thunk node.
3801 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3802 When REF is non-NULL, assume that reference happens in symbol REF
3803 when determining the availability. */
3805 cgraph_node *
3806 cgraph_node::function_or_virtual_thunk_symbol
3807 (enum availability *availability,
3808 struct symtab_node *ref)
3810 cgraph_node *node = ultimate_alias_target (availability, ref);
3812 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3814 ref = node;
3815 node = node->callees->callee;
3816 if (availability)
3818 enum availability a;
3819 a = node->get_availability (ref);
3820 if (a < *availability)
3821 *availability = a;
3823 node = node->ultimate_alias_target (availability, ref);
3825 return node;
3828 /* When doing LTO, read cgraph_node's body from disk if it is not already
3829 present. */
3831 bool
3832 cgraph_node::get_untransformed_body (void)
3834 lto_file_decl_data *file_data;
3835 const char *data, *name;
3836 size_t len;
3837 tree decl = this->decl;
3839 /* Check if body is already there. Either we have gimple body or
3840 the function is thunk and in that case we set DECL_ARGUMENTS. */
3841 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3842 return false;
3844 gcc_assert (in_lto_p && !DECL_RESULT (decl));
3846 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3848 file_data = lto_file_data;
3849 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3851 /* We may have renamed the declaration, e.g., a static function. */
3852 name = lto_get_decl_name_mapping (file_data, name);
3853 struct lto_in_decl_state *decl_state
3854 = lto_get_function_in_decl_state (file_data, decl);
3856 cgraph_node *origin = this;
3857 while (origin->clone_of)
3858 origin = origin->clone_of;
3860 int stream_order = origin->order - file_data->order_base;
3861 data = lto_get_section_data (file_data, LTO_section_function_body,
3862 name, stream_order, &len,
3863 decl_state->compressed);
3864 if (!data)
3865 fatal_error (input_location, "%s: section %s.%d is missing",
3866 file_data->file_name, name, stream_order);
3868 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3870 if (!quiet_flag)
3871 fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
3872 lto_input_function_body (file_data, this, data);
3873 lto_stats.num_function_bodies++;
3874 lto_free_section_data (file_data, LTO_section_function_body, name,
3875 data, len, decl_state->compressed);
3876 lto_free_function_in_decl_state_for_node (this);
3877 /* Keep lto file data so ipa-inline-analysis knows about cross module
3878 inlining. */
3880 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3882 return true;
3885 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3886 if it is not already present. When some IPA transformations are scheduled,
3887 apply them. */
3889 bool
3890 cgraph_node::get_body (void)
3892 bool updated;
3894 updated = get_untransformed_body ();
3896 /* Getting transformed body makes no sense for inline clones;
3897 we should never use this on real clones because they are materialized
3898 early.
3899 TODO: Materializing clones here will likely lead to smaller LTRANS
3900 footprint. */
3901 gcc_assert (!inlined_to && !clone_of);
3902 if (ipa_transforms_to_apply.exists ())
3904 opt_pass *saved_current_pass = current_pass;
3905 FILE *saved_dump_file = dump_file;
3906 const char *saved_dump_file_name = dump_file_name;
3907 dump_flags_t saved_dump_flags = dump_flags;
3908 dump_file_name = NULL;
3909 set_dump_file (NULL);
3911 push_cfun (DECL_STRUCT_FUNCTION (decl));
3913 update_ssa (TODO_update_ssa_only_virtuals);
3914 execute_all_ipa_transforms (true);
3915 cgraph_edge::rebuild_edges ();
3916 free_dominance_info (CDI_DOMINATORS);
3917 free_dominance_info (CDI_POST_DOMINATORS);
3918 pop_cfun ();
3919 updated = true;
3921 current_pass = saved_current_pass;
3922 set_dump_file (saved_dump_file);
3923 dump_file_name = saved_dump_file_name;
3924 dump_flags = saved_dump_flags;
3926 return updated;
3929 /* Return the DECL_STRUCT_FUNCTION of the function. */
3931 struct function *
3932 cgraph_node::get_fun () const
3934 const cgraph_node *node = this;
3935 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3937 while (!fun && node->clone_of)
3939 node = node->clone_of;
3940 fun = DECL_STRUCT_FUNCTION (node->decl);
3943 return fun;
3946 /* Reset all state within cgraph.c so that we can rerun the compiler
3947 within the same process. For use by toplev::finalize. */
3949 void
3950 cgraph_c_finalize (void)
3952 symtab = NULL;
3954 x_cgraph_nodes_queue = NULL;
3956 cgraph_fnver_htab = NULL;
3957 version_info_node = NULL;
3960 /* A worker for call_for_symbol_and_aliases. */
3962 bool
3963 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
3964 void *),
3965 void *data,
3966 bool include_overwritable)
3968 ipa_ref *ref;
3969 FOR_EACH_ALIAS (this, ref)
3971 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3972 if (include_overwritable
3973 || alias->get_availability () > AVAIL_INTERPOSABLE)
3974 if (alias->call_for_symbol_and_aliases (callback, data,
3975 include_overwritable))
3976 return true;
3978 return false;
3981 /* Return true if NODE has thunk. */
3983 bool
3984 cgraph_node::has_thunk_p (cgraph_node *node, void *)
3986 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3987 if (e->caller->thunk.thunk_p)
3988 return true;
3989 return false;
3992 /* Expected frequency of executions within the function. */
3994 sreal
3995 cgraph_edge::sreal_frequency ()
3997 return count.to_sreal_scale (caller->inlined_to
3998 ? caller->inlined_to->count
3999 : caller->count);
4003 /* During LTO stream in this can be used to check whether call can possibly
4004 be internal to the current translation unit. */
4006 bool
4007 cgraph_edge::possibly_call_in_translation_unit_p (void)
4009 gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4011 /* While incremental linking we may end up getting function body later. */
4012 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4013 return true;
4015 /* We may be smarter here and avoid streaming in indirect calls we can't
4016 track, but that would require arranging streaming the indirect call
4017 summary first. */
4018 if (!callee)
4019 return true;
4021 /* If callee is local to the original translation unit, it will be
4022 defined. */
4023 if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4024 return true;
4026 /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4027 yet) and see if it is a definition. In fact we may also resolve aliases,
4028 but that is probably not too important. */
4029 symtab_node *node = callee;
4030 for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4031 node = node->previous_sharing_asm_name;
4032 if (node->previous_sharing_asm_name)
4033 node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4034 gcc_assert (TREE_PUBLIC (node->decl));
4035 return node->get_availability () >= AVAIL_INTERPOSABLE;
4038 /* Return num_speculative_targets of this edge. */
4041 cgraph_edge::num_speculative_call_targets_p (void)
4043 return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4046 /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4047 This needs to be a global so that it can be a GC root, and thus
4048 prevent the stashed copy from being garbage-collected if the GC runs
4049 during a symbol_table_test. */
4051 symbol_table *saved_symtab;
4053 #if CHECKING_P
4055 namespace selftest {
4057 /* class selftest::symbol_table_test. */
4059 /* Constructor. Store the old value of symtab, and create a new one. */
4061 symbol_table_test::symbol_table_test ()
4063 gcc_assert (saved_symtab == NULL);
4064 saved_symtab = symtab;
4065 symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4068 /* Destructor. Restore the old value of symtab. */
4070 symbol_table_test::~symbol_table_test ()
4072 gcc_assert (saved_symtab != NULL);
4073 symtab = saved_symtab;
4074 saved_symtab = NULL;
4077 /* Verify that symbol_table_test works. */
4079 static void
4080 test_symbol_table_test ()
4082 /* Simulate running two selftests involving symbol tables. */
4083 for (int i = 0; i < 2; i++)
4085 symbol_table_test stt;
4086 tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4087 get_identifier ("test_decl"),
4088 build_function_type_list (void_type_node,
4089 NULL_TREE));
4090 cgraph_node *node = cgraph_node::get_create (test_decl);
4091 gcc_assert (node);
4093 /* Verify that the node has order 0 on both iterations,
4094 and thus that nodes have predictable dump names in selftests. */
4095 ASSERT_EQ (node->order, 0);
4096 ASSERT_STREQ (node->dump_name (), "test_decl/0");
4100 /* Run all of the selftests within this file. */
4102 void
4103 cgraph_c_tests ()
4105 test_symbol_table_test ();
4108 } // namespace selftest
4110 #endif /* CHECKING_P */
4112 #include "gt-cgraph.h"