typeck.c (cp_truthvalue_conversion): Add tsubst_flags_t parameter and use it in calls...
[official-gcc.git] / gcc / cgraph.c
blob1f7a5c58d98713f22e7704231dfdf6bf2634e539
1 /* Callgraph handling code.
2 Copyright (C) 2003-2019 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"
66 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
67 #include "tree-pass.h"
69 /* Queue of cgraph nodes scheduled to be lowered. */
70 symtab_node *x_cgraph_nodes_queue;
71 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
73 /* Symbol table global context. */
74 symbol_table *symtab;
76 /* List of hooks triggered on cgraph_edge events. */
77 struct cgraph_edge_hook_list {
78 cgraph_edge_hook hook;
79 void *data;
80 struct cgraph_edge_hook_list *next;
83 /* List of hooks triggered on cgraph_node events. */
84 struct cgraph_node_hook_list {
85 cgraph_node_hook hook;
86 void *data;
87 struct cgraph_node_hook_list *next;
90 /* List of hooks triggered on events involving two cgraph_edges. */
91 struct cgraph_2edge_hook_list {
92 cgraph_2edge_hook hook;
93 void *data;
94 struct cgraph_2edge_hook_list *next;
97 /* List of hooks triggered on events involving two cgraph_nodes. */
98 struct cgraph_2node_hook_list {
99 cgraph_2node_hook hook;
100 void *data;
101 struct cgraph_2node_hook_list *next;
104 /* Hash descriptor for cgraph_function_version_info. */
106 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
108 static hashval_t hash (cgraph_function_version_info *);
109 static bool equal (cgraph_function_version_info *,
110 cgraph_function_version_info *);
113 /* Map a cgraph_node to cgraph_function_version_info using this htab.
114 The cgraph_function_version_info has a THIS_NODE field that is the
115 corresponding cgraph_node.. */
117 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
119 /* Hash function for cgraph_fnver_htab. */
120 hashval_t
121 function_version_hasher::hash (cgraph_function_version_info *ptr)
123 int uid = ptr->this_node->get_uid ();
124 return (hashval_t)(uid);
127 /* eq function for cgraph_fnver_htab. */
128 bool
129 function_version_hasher::equal (cgraph_function_version_info *n1,
130 cgraph_function_version_info *n2)
132 return n1->this_node->get_uid () == n2->this_node->get_uid ();
135 /* Mark as GC root all allocated nodes. */
136 static GTY(()) struct cgraph_function_version_info *
137 version_info_node = NULL;
139 /* Return true if NODE's address can be compared. */
141 bool
142 symtab_node::address_can_be_compared_p ()
144 /* Address of virtual tables and functions is never compared. */
145 if (DECL_VIRTUAL_P (decl))
146 return false;
147 /* Address of C++ cdtors is never compared. */
148 if (is_a <cgraph_node *> (this)
149 && (DECL_CXX_CONSTRUCTOR_P (decl)
150 || DECL_CXX_DESTRUCTOR_P (decl)))
151 return false;
152 /* Constant pool symbols addresses are never compared.
153 flag_merge_constants permits us to assume the same on readonly vars. */
154 if (is_a <varpool_node *> (this)
155 && (DECL_IN_CONSTANT_POOL (decl)
156 || (flag_merge_constants >= 2
157 && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
158 return false;
159 return true;
162 /* Get the cgraph_function_version_info node corresponding to node. */
163 cgraph_function_version_info *
164 cgraph_node::function_version (void)
166 cgraph_function_version_info key;
167 key.this_node = this;
169 if (cgraph_fnver_htab == NULL)
170 return NULL;
172 return cgraph_fnver_htab->find (&key);
175 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
176 corresponding to cgraph_node NODE. */
177 cgraph_function_version_info *
178 cgraph_node::insert_new_function_version (void)
180 version_info_node = NULL;
181 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
182 version_info_node->this_node = this;
184 if (cgraph_fnver_htab == NULL)
185 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
187 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
188 = version_info_node;
189 return version_info_node;
192 /* Remove the cgraph_function_version_info node given by DECL_V. */
193 static void
194 delete_function_version (cgraph_function_version_info *decl_v)
196 if (decl_v == NULL)
197 return;
199 if (version_info_node == decl_v)
200 version_info_node = NULL;
202 if (decl_v->prev != NULL)
203 decl_v->prev->next = decl_v->next;
205 if (decl_v->next != NULL)
206 decl_v->next->prev = decl_v->prev;
208 if (cgraph_fnver_htab != NULL)
209 cgraph_fnver_htab->remove_elt (decl_v);
212 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
213 DECL is a duplicate declaration. */
214 void
215 cgraph_node::delete_function_version_by_decl (tree decl)
217 cgraph_node *decl_node = cgraph_node::get (decl);
219 if (decl_node == NULL)
220 return;
222 delete_function_version (decl_node->function_version ());
224 decl_node->remove ();
227 /* Record that DECL1 and DECL2 are semantically identical function
228 versions. */
229 void
230 cgraph_node::record_function_versions (tree decl1, tree decl2)
232 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
233 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
234 cgraph_function_version_info *decl1_v = NULL;
235 cgraph_function_version_info *decl2_v = NULL;
236 cgraph_function_version_info *before;
237 cgraph_function_version_info *after;
239 gcc_assert (decl1_node != NULL && decl2_node != NULL);
240 decl1_v = decl1_node->function_version ();
241 decl2_v = decl2_node->function_version ();
243 if (decl1_v != NULL && decl2_v != NULL)
244 return;
246 if (decl1_v == NULL)
247 decl1_v = decl1_node->insert_new_function_version ();
249 if (decl2_v == NULL)
250 decl2_v = decl2_node->insert_new_function_version ();
252 /* Chain decl2_v and decl1_v. All semantically identical versions
253 will be chained together. */
255 before = decl1_v;
256 after = decl2_v;
258 while (before->next != NULL)
259 before = before->next;
261 while (after->prev != NULL)
262 after= after->prev;
264 before->next = after;
265 after->prev = before;
268 /* Initialize callgraph dump file. */
270 void
271 symbol_table::initialize (void)
273 if (!dump_file)
274 dump_file = dump_begin (TDI_cgraph, NULL);
276 if (!ipa_clones_dump_file)
277 ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
280 /* Allocate new callgraph node and insert it into basic data structures. */
282 cgraph_node *
283 symbol_table::create_empty (void)
285 cgraph_node *node = allocate_cgraph_symbol ();
287 node->type = SYMTAB_FUNCTION;
288 node->frequency = NODE_FREQUENCY_NORMAL;
289 node->count_materialization_scale = REG_BR_PROB_BASE;
290 cgraph_count++;
292 return node;
295 /* Register HOOK to be called with DATA on each removed edge. */
296 cgraph_edge_hook_list *
297 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
299 cgraph_edge_hook_list *entry;
300 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
302 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
303 entry->hook = hook;
304 entry->data = data;
305 entry->next = NULL;
306 while (*ptr)
307 ptr = &(*ptr)->next;
308 *ptr = entry;
309 return entry;
312 /* Remove ENTRY from the list of hooks called on removing edges. */
313 void
314 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
316 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
318 while (*ptr != entry)
319 ptr = &(*ptr)->next;
320 *ptr = entry->next;
321 free (entry);
324 /* Call all edge removal hooks. */
325 void
326 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
328 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
329 while (entry)
331 entry->hook (e, entry->data);
332 entry = entry->next;
336 /* Register HOOK to be called with DATA on each removed node. */
337 cgraph_node_hook_list *
338 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
340 cgraph_node_hook_list *entry;
341 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
343 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
344 entry->hook = hook;
345 entry->data = data;
346 entry->next = NULL;
347 while (*ptr)
348 ptr = &(*ptr)->next;
349 *ptr = entry;
350 return entry;
353 /* Remove ENTRY from the list of hooks called on removing nodes. */
354 void
355 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
357 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
359 while (*ptr != entry)
360 ptr = &(*ptr)->next;
361 *ptr = entry->next;
362 free (entry);
365 /* Call all node removal hooks. */
366 void
367 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
369 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
370 while (entry)
372 entry->hook (node, entry->data);
373 entry = entry->next;
377 /* Call all node removal hooks. */
378 void
379 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
381 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
382 while (entry)
384 entry->hook (node, entry->data);
385 entry = entry->next;
390 /* Register HOOK to be called with DATA on each inserted node. */
391 cgraph_node_hook_list *
392 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
394 cgraph_node_hook_list *entry;
395 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
397 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
398 entry->hook = hook;
399 entry->data = data;
400 entry->next = NULL;
401 while (*ptr)
402 ptr = &(*ptr)->next;
403 *ptr = entry;
404 return entry;
407 /* Remove ENTRY from the list of hooks called on inserted nodes. */
408 void
409 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
411 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
413 while (*ptr != entry)
414 ptr = &(*ptr)->next;
415 *ptr = entry->next;
416 free (entry);
419 /* Register HOOK to be called with DATA on each duplicated edge. */
420 cgraph_2edge_hook_list *
421 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
423 cgraph_2edge_hook_list *entry;
424 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
426 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
427 entry->hook = hook;
428 entry->data = data;
429 entry->next = NULL;
430 while (*ptr)
431 ptr = &(*ptr)->next;
432 *ptr = entry;
433 return entry;
436 /* Remove ENTRY from the list of hooks called on duplicating edges. */
437 void
438 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
440 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
442 while (*ptr != entry)
443 ptr = &(*ptr)->next;
444 *ptr = entry->next;
445 free (entry);
448 /* Call all edge duplication hooks. */
449 void
450 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
452 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
453 while (entry)
455 entry->hook (cs1, cs2, entry->data);
456 entry = entry->next;
460 /* Register HOOK to be called with DATA on each duplicated node. */
461 cgraph_2node_hook_list *
462 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
464 cgraph_2node_hook_list *entry;
465 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
467 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
468 entry->hook = hook;
469 entry->data = data;
470 entry->next = NULL;
471 while (*ptr)
472 ptr = &(*ptr)->next;
473 *ptr = entry;
474 return entry;
477 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
478 void
479 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
481 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
483 while (*ptr != entry)
484 ptr = &(*ptr)->next;
485 *ptr = entry->next;
486 free (entry);
489 /* Call all node duplication hooks. */
490 void
491 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
492 cgraph_node *node2)
494 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
495 while (entry)
497 entry->hook (node, node2, entry->data);
498 entry = entry->next;
502 /* Return cgraph node assigned to DECL. Create new one when needed. */
504 cgraph_node *
505 cgraph_node::create (tree decl)
507 cgraph_node *node = symtab->create_empty ();
508 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
510 node->decl = decl;
512 node->count = profile_count::uninitialized ();
514 if ((flag_openacc || flag_openmp)
515 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
517 node->offloadable = 1;
518 if (ENABLE_OFFLOADING)
519 g->have_offload = true;
522 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
523 node->ifunc_resolver = true;
525 node->register_symbol ();
527 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
529 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
530 node->next_nested = node->origin->nested;
531 node->origin->nested = node;
533 return node;
536 /* Try to find a call graph node for declaration DECL and if it does not exist
537 or if it corresponds to an inline clone, create a new one. */
539 cgraph_node *
540 cgraph_node::get_create (tree decl)
542 cgraph_node *first_clone = cgraph_node::get (decl);
544 if (first_clone && !first_clone->inlined_to)
545 return first_clone;
547 cgraph_node *node = cgraph_node::create (decl);
548 if (first_clone)
550 first_clone->clone_of = node;
551 node->clones = first_clone;
552 node->order = first_clone->order;
553 symtab->symtab_prevail_in_asm_name_hash (node);
554 node->decl->decl_with_vis.symtab_node = node;
555 if (dump_file)
556 fprintf (dump_file, "Introduced new external node "
557 "(%s) and turned into root of the clone tree.\n",
558 node->dump_name ());
560 else if (dump_file)
561 fprintf (dump_file, "Introduced new external node "
562 "(%s).\n", node->dump_name ());
563 return node;
566 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
567 the function body is associated with (not necessarily cgraph_node (DECL). */
569 cgraph_node *
570 cgraph_node::create_alias (tree alias, tree target)
572 cgraph_node *alias_node;
574 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
575 || TREE_CODE (target) == IDENTIFIER_NODE);
576 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
577 alias_node = cgraph_node::get_create (alias);
578 gcc_assert (!alias_node->definition);
579 alias_node->alias_target = target;
580 alias_node->definition = true;
581 alias_node->alias = true;
582 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
583 alias_node->transparent_alias = alias_node->weakref = true;
584 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
585 alias_node->ifunc_resolver = true;
586 return alias_node;
589 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
590 and NULL otherwise.
591 Same body aliases are output whenever the body of DECL is output,
592 and cgraph_node::get (ALIAS) transparently returns
593 cgraph_node::get (DECL). */
595 cgraph_node *
596 cgraph_node::create_same_body_alias (tree alias, tree decl)
598 cgraph_node *n;
600 /* If aliases aren't supported by the assembler, fail. */
601 if (!TARGET_SUPPORTS_ALIASES)
602 return NULL;
604 /* Langhooks can create same body aliases of symbols not defined.
605 Those are useless. Drop them on the floor. */
606 if (symtab->global_info_ready)
607 return NULL;
609 n = cgraph_node::create_alias (alias, decl);
610 n->cpp_implicit_alias = true;
611 if (symtab->cpp_implicit_aliases_done)
612 n->resolve_alias (cgraph_node::get (decl));
613 return n;
616 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
617 aliases DECL with an adjustments made into the first parameter.
618 See comments in struct cgraph_thunk_info for detail on the parameters. */
620 cgraph_node *
621 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
622 HOST_WIDE_INT fixed_offset,
623 HOST_WIDE_INT virtual_value,
624 HOST_WIDE_INT indirect_offset,
625 tree virtual_offset,
626 tree real_alias)
628 cgraph_node *node;
630 node = cgraph_node::get (alias);
631 if (node)
632 node->reset ();
633 else
634 node = cgraph_node::create (alias);
636 /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE. */
637 gcc_checking_assert (virtual_offset
638 ? virtual_value == wi::to_wide (virtual_offset)
639 : virtual_value == 0);
641 node->thunk.fixed_offset = fixed_offset;
642 node->thunk.virtual_value = virtual_value;
643 node->thunk.indirect_offset = indirect_offset;
644 node->thunk.alias = real_alias;
645 node->thunk.this_adjusting = this_adjusting;
646 node->thunk.virtual_offset_p = virtual_offset != NULL;
647 node->thunk.thunk_p = true;
648 node->definition = true;
650 return node;
653 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
654 Return NULL if there's no such node. */
656 cgraph_node *
657 cgraph_node::get_for_asmname (tree asmname)
659 /* We do not want to look at inline clones. */
660 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
661 node;
662 node = node->next_sharing_asm_name)
664 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
665 if (cn && !cn->inlined_to)
666 return cn;
668 return NULL;
671 /* Returns a hash value for X (which really is a cgraph_edge). */
673 hashval_t
674 cgraph_edge_hasher::hash (cgraph_edge *e)
676 /* This is a really poor hash function, but it is what htab_hash_pointer
677 uses. */
678 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
681 /* Returns a hash value for X (which really is a cgraph_edge). */
683 hashval_t
684 cgraph_edge_hasher::hash (gimple *call_stmt)
686 /* This is a really poor hash function, but it is what htab_hash_pointer
687 uses. */
688 return (hashval_t) ((intptr_t)call_stmt >> 3);
691 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
693 inline bool
694 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
696 return x->call_stmt == y;
699 /* Add call graph edge E to call site hash of its caller. */
701 static inline void
702 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
704 gimple *call = e->call_stmt;
705 *e->caller->call_site_hash->find_slot_with_hash
706 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
709 /* Add call graph edge E to call site hash of its caller. */
711 static inline void
712 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
714 /* There are two speculative edges for every statement (one direct,
715 one indirect); always hash the direct one. */
716 if (e->speculative && e->indirect_unknown_callee)
717 return;
718 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
719 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
720 if (*slot)
722 gcc_assert (((cgraph_edge *)*slot)->speculative);
723 if (e->callee)
724 *slot = e;
725 return;
727 gcc_assert (!*slot || e->speculative);
728 *slot = e;
731 /* Return the callgraph edge representing the GIMPLE_CALL statement
732 CALL_STMT. */
734 cgraph_edge *
735 cgraph_node::get_edge (gimple *call_stmt)
737 cgraph_edge *e, *e2;
738 int n = 0;
740 if (call_site_hash)
741 return call_site_hash->find_with_hash
742 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
744 /* This loop may turn out to be performance problem. In such case adding
745 hashtables into call nodes with very many edges is probably best
746 solution. It is not good idea to add pointer into CALL_EXPR itself
747 because we want to make possible having multiple cgraph nodes representing
748 different clones of the same body before the body is actually cloned. */
749 for (e = callees; e; e = e->next_callee)
751 if (e->call_stmt == call_stmt)
752 break;
753 n++;
756 if (!e)
757 for (e = indirect_calls; e; e = e->next_callee)
759 if (e->call_stmt == call_stmt)
760 break;
761 n++;
764 if (n > 100)
766 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
767 for (e2 = callees; e2; e2 = e2->next_callee)
768 cgraph_add_edge_to_call_site_hash (e2);
769 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
770 cgraph_add_edge_to_call_site_hash (e2);
773 return e;
777 /* Change field call_stmt of edge to NEW_STMT.
778 If UPDATE_SPECULATIVE and E is any component of speculative
779 edge, then update all components. */
781 void
782 cgraph_edge::set_call_stmt (gcall *new_stmt, bool update_speculative)
784 tree decl;
786 /* Speculative edges has three component, update all of them
787 when asked to. */
788 if (update_speculative && speculative)
790 cgraph_edge *direct, *indirect;
791 ipa_ref *ref;
793 speculative_call_info (direct, indirect, ref);
794 direct->set_call_stmt (new_stmt, false);
795 indirect->set_call_stmt (new_stmt, false);
796 ref->stmt = new_stmt;
797 return;
800 /* Only direct speculative edges go to call_site_hash. */
801 if (caller->call_site_hash
802 && (!speculative || !indirect_unknown_callee))
804 caller->call_site_hash->remove_elt_with_hash
805 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
808 cgraph_edge *e = this;
810 call_stmt = new_stmt;
811 if (indirect_unknown_callee
812 && (decl = gimple_call_fndecl (new_stmt)))
814 /* Constant propagation (and possibly also inlining?) can turn an
815 indirect call into a direct one. */
816 cgraph_node *new_callee = cgraph_node::get (decl);
818 gcc_checking_assert (new_callee);
819 e = make_direct (new_callee);
822 function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
823 e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
824 if (e->caller->call_site_hash)
825 cgraph_add_edge_to_call_site_hash (e);
828 /* Allocate a cgraph_edge structure and fill it with data according to the
829 parameters of which only CALLEE can be NULL (when creating an indirect call
830 edge). CLONING_P should be set if properties that are copied from an
831 original edge should not be calculated. */
833 cgraph_edge *
834 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
835 gcall *call_stmt, profile_count count,
836 bool indir_unknown_callee, bool cloning_p)
838 cgraph_edge *edge;
840 /* LTO does not actually have access to the call_stmt since these
841 have not been loaded yet. */
842 if (call_stmt)
844 /* This is a rather expensive check possibly triggering
845 construction of call stmt hashtable. */
846 cgraph_edge *e;
847 gcc_checking_assert (!(e = caller->get_edge (call_stmt))
848 || e->speculative);
850 gcc_assert (is_gimple_call (call_stmt));
853 edge = ggc_alloc<cgraph_edge> ();
854 edge->m_summary_id = -1;
855 edges_count++;
857 gcc_assert (++edges_max_uid != 0);
858 edge->m_uid = edges_max_uid;
859 edge->aux = NULL;
860 edge->caller = caller;
861 edge->callee = callee;
862 edge->prev_caller = NULL;
863 edge->next_caller = NULL;
864 edge->prev_callee = NULL;
865 edge->next_callee = NULL;
866 edge->lto_stmt_uid = 0;
868 edge->count = count;
869 edge->call_stmt = call_stmt;
870 edge->indirect_info = NULL;
871 edge->indirect_inlining_edge = 0;
872 edge->speculative = false;
873 edge->indirect_unknown_callee = indir_unknown_callee;
874 if (call_stmt && caller->call_site_hash)
875 cgraph_add_edge_to_call_site_hash (edge);
877 if (cloning_p)
878 return edge;
880 edge->can_throw_external
881 = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
882 call_stmt) : false;
883 edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
884 edge->call_stmt_cannot_inline_p = false;
886 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
887 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
888 edge->in_polymorphic_cdtor
889 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
890 caller->decl);
891 else
892 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
894 return edge;
897 /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
898 be set if properties that are copied from an original edge should not be
899 calculated. */
901 cgraph_edge *
902 cgraph_node::create_edge (cgraph_node *callee,
903 gcall *call_stmt, profile_count count, bool cloning_p)
905 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
906 false, cloning_p);
908 if (!cloning_p)
909 initialize_inline_failed (edge);
911 edge->next_caller = callee->callers;
912 if (callee->callers)
913 callee->callers->prev_caller = edge;
914 edge->next_callee = callees;
915 if (callees)
916 callees->prev_callee = edge;
917 callees = edge;
918 callee->callers = edge;
920 return edge;
923 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
925 cgraph_indirect_call_info *
926 cgraph_allocate_init_indirect_info (void)
928 cgraph_indirect_call_info *ii;
930 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
931 ii->param_index = -1;
932 return ii;
935 /* Create an indirect edge with a yet-undetermined callee where the call
936 statement destination is a formal parameter of the caller with index
937 PARAM_INDEX. CLONING_P should be set if properties that are copied from an
938 original edge should not be calculated and indirect_info structure should
939 not be calculated. */
941 cgraph_edge *
942 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
943 profile_count count,
944 bool cloning_p)
946 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
947 cloning_p);
948 tree target;
950 if (!cloning_p)
951 initialize_inline_failed (edge);
953 edge->indirect_info = cgraph_allocate_init_indirect_info ();
954 edge->indirect_info->ecf_flags = ecf_flags;
955 edge->indirect_info->vptr_changed = true;
957 /* Record polymorphic call info. */
958 if (!cloning_p
959 && call_stmt
960 && (target = gimple_call_fn (call_stmt))
961 && virtual_method_call_p (target))
963 ipa_polymorphic_call_context context (decl, target, call_stmt);
965 /* Only record types can have virtual calls. */
966 edge->indirect_info->polymorphic = true;
967 edge->indirect_info->param_index = -1;
968 edge->indirect_info->otr_token
969 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
970 edge->indirect_info->otr_type = obj_type_ref_class (target);
971 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
972 edge->indirect_info->context = context;
975 edge->next_callee = indirect_calls;
976 if (indirect_calls)
977 indirect_calls->prev_callee = edge;
978 indirect_calls = edge;
980 return edge;
983 /* Remove the edge from the list of the callees of the caller. */
985 void
986 cgraph_edge::remove_caller (void)
988 if (prev_callee)
989 prev_callee->next_callee = next_callee;
990 if (next_callee)
991 next_callee->prev_callee = prev_callee;
992 if (!prev_callee)
994 if (indirect_unknown_callee)
995 caller->indirect_calls = next_callee;
996 else
997 caller->callees = next_callee;
999 if (caller->call_site_hash)
1000 caller->call_site_hash->remove_elt_with_hash
1001 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1004 /* Put the edge onto the free list. */
1006 void
1007 symbol_table::free_edge (cgraph_edge *e)
1009 edges_count--;
1010 if (e->m_summary_id != -1)
1011 edge_released_summary_ids.safe_push (e->m_summary_id);
1013 if (e->indirect_info)
1014 ggc_free (e->indirect_info);
1015 ggc_free (e);
1018 /* Remove the edge in the cgraph. */
1020 void
1021 cgraph_edge::remove (void)
1023 /* Call all edge removal hooks. */
1024 symtab->call_edge_removal_hooks (this);
1026 if (!indirect_unknown_callee)
1027 /* Remove from callers list of the callee. */
1028 remove_callee ();
1030 /* Remove from callees list of the callers. */
1031 remove_caller ();
1033 /* Put the edge onto the free list. */
1034 symtab->free_edge (this);
1037 /* Turn edge into speculative call calling N2. Update
1038 the profile so the direct call is taken COUNT times
1039 with FREQUENCY.
1041 At clone materialization time, the indirect call E will
1042 be expanded as:
1044 if (call_dest == N2)
1045 n2 ();
1046 else
1047 call call_dest
1049 At this time the function just creates the direct call,
1050 the reference representing the if conditional and attaches
1051 them all to the original indirect call statement.
1053 Return direct edge created. */
1055 cgraph_edge *
1056 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count)
1058 cgraph_node *n = caller;
1059 ipa_ref *ref = NULL;
1060 cgraph_edge *e2;
1062 if (dump_file)
1063 fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1064 n->dump_name (), n2->dump_name ());
1065 speculative = true;
1066 e2 = n->create_edge (n2, call_stmt, direct_count);
1067 initialize_inline_failed (e2);
1068 e2->speculative = true;
1069 if (TREE_NOTHROW (n2->decl))
1070 e2->can_throw_external = false;
1071 else
1072 e2->can_throw_external = can_throw_external;
1073 e2->lto_stmt_uid = lto_stmt_uid;
1074 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1075 count -= e2->count;
1076 symtab->call_edge_duplication_hooks (this, e2);
1077 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1078 ref->lto_stmt_uid = lto_stmt_uid;
1079 ref->speculative = speculative;
1080 n2->mark_address_taken ();
1081 return e2;
1084 /* Speculative call consist of three components:
1085 1) an indirect edge representing the original call
1086 2) an direct edge representing the new call
1087 3) ADDR_EXPR reference representing the speculative check.
1088 All three components are attached to single statement (the indirect
1089 call) and if one of them exists, all of them must exist.
1091 Given speculative call edge, return all three components.
1094 void
1095 cgraph_edge::speculative_call_info (cgraph_edge *&direct,
1096 cgraph_edge *&indirect,
1097 ipa_ref *&reference)
1099 ipa_ref *ref;
1100 int i;
1101 cgraph_edge *e2;
1102 cgraph_edge *e = this;
1104 if (!e->indirect_unknown_callee)
1105 for (e2 = e->caller->indirect_calls;
1106 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1107 e2 = e2->next_callee)
1109 else
1111 e2 = e;
1112 /* We can take advantage of the call stmt hash. */
1113 if (e2->call_stmt)
1115 e = e->caller->get_edge (e2->call_stmt);
1116 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1118 else
1119 for (e = e->caller->callees;
1120 e2->call_stmt != e->call_stmt
1121 || e2->lto_stmt_uid != e->lto_stmt_uid;
1122 e = e->next_callee)
1125 gcc_assert (e->speculative && e2->speculative);
1126 direct = e;
1127 indirect = e2;
1129 reference = NULL;
1130 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1131 if (ref->speculative
1132 && ((ref->stmt && ref->stmt == e->call_stmt)
1133 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1135 reference = ref;
1136 break;
1139 /* Speculative edge always consist of all three components - direct edge,
1140 indirect and reference. */
1142 gcc_assert (e && e2 && ref);
1145 /* Speculative call edge turned out to be direct call to CALLEE_DECL.
1146 Remove the speculative call sequence and return edge representing the call.
1147 It is up to caller to redirect the call as appropriate. */
1149 cgraph_edge *
1150 cgraph_edge::resolve_speculation (tree callee_decl)
1152 cgraph_edge *edge = this;
1153 cgraph_edge *e2;
1154 ipa_ref *ref;
1156 gcc_assert (edge->speculative);
1157 edge->speculative_call_info (e2, edge, ref);
1158 if (!callee_decl
1159 || !ref->referred->semantically_equivalent_p
1160 (symtab_node::get (callee_decl)))
1162 if (dump_file)
1164 if (callee_decl)
1166 fprintf (dump_file, "Speculative indirect call %s => %s has "
1167 "turned out to have contradicting known target ",
1168 edge->caller->dump_name (),
1169 e2->callee->dump_name ());
1170 print_generic_expr (dump_file, callee_decl);
1171 fprintf (dump_file, "\n");
1173 else
1175 fprintf (dump_file, "Removing speculative call %s => %s\n",
1176 edge->caller->dump_name (),
1177 e2->callee->dump_name ());
1181 else
1183 cgraph_edge *tmp = edge;
1184 if (dump_file)
1185 fprintf (dump_file, "Speculative call turned into direct call.\n");
1186 edge = e2;
1187 e2 = tmp;
1188 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1189 in the functions inlined through it. */
1191 edge->count += e2->count;
1192 edge->speculative = false;
1193 e2->speculative = false;
1194 ref->remove_reference ();
1195 if (e2->indirect_unknown_callee || e2->inline_failed)
1196 e2->remove ();
1197 else
1198 e2->callee->remove_symbol_and_inline_clones ();
1199 if (edge->caller->call_site_hash)
1200 cgraph_update_edge_in_call_site_hash (edge);
1201 return edge;
1204 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1205 CALLEE. DELTA is an integer constant that is to be added to the this
1206 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1208 cgraph_edge *
1209 cgraph_edge::make_direct (cgraph_node *callee)
1211 cgraph_edge *edge = this;
1212 gcc_assert (indirect_unknown_callee);
1214 /* If we are redirecting speculative call, make it non-speculative. */
1215 if (indirect_unknown_callee && speculative)
1217 edge = edge->resolve_speculation (callee->decl);
1219 /* On successful speculation just return the pre existing direct edge. */
1220 if (!edge->indirect_unknown_callee)
1221 return edge;
1224 indirect_unknown_callee = 0;
1225 ggc_free (indirect_info);
1226 indirect_info = NULL;
1228 /* Get the edge out of the indirect edge list. */
1229 if (prev_callee)
1230 prev_callee->next_callee = next_callee;
1231 if (next_callee)
1232 next_callee->prev_callee = prev_callee;
1233 if (!prev_callee)
1234 caller->indirect_calls = next_callee;
1236 /* Put it into the normal callee list */
1237 prev_callee = NULL;
1238 next_callee = caller->callees;
1239 if (caller->callees)
1240 caller->callees->prev_callee = edge;
1241 caller->callees = edge;
1243 /* Insert to callers list of the new callee. */
1244 edge->set_callee (callee);
1246 /* We need to re-determine the inlining status of the edge. */
1247 initialize_inline_failed (edge);
1248 return edge;
1251 /* If necessary, change the function declaration in the call statement
1252 associated with E so that it corresponds to the edge callee. */
1254 gimple *
1255 cgraph_edge::redirect_call_stmt_to_callee (void)
1257 cgraph_edge *e = this;
1259 tree decl = gimple_call_fndecl (e->call_stmt);
1260 gcall *new_stmt;
1261 gimple_stmt_iterator gsi;
1263 if (e->speculative)
1265 cgraph_edge *e2;
1266 gcall *new_stmt;
1267 ipa_ref *ref;
1269 e->speculative_call_info (e, e2, ref);
1270 /* If there already is an direct call (i.e. as a result of inliner's
1271 substitution), forget about speculating. */
1272 if (decl)
1273 e = e->resolve_speculation (decl);
1274 else
1276 /* Expand speculation into GIMPLE code. */
1277 if (dump_file)
1279 fprintf (dump_file,
1280 "Expanding speculative call of %s -> %s count: ",
1281 e->caller->dump_name (),
1282 e->callee->dump_name ());
1283 e->count.dump (dump_file);
1284 fprintf (dump_file, "\n");
1286 gcc_assert (e2->speculative);
1287 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1289 profile_probability prob = e->count.probability_in (e->count
1290 + e2->count);
1291 if (!prob.initialized_p ())
1292 prob = profile_probability::even ();
1293 new_stmt = gimple_ic (e->call_stmt,
1294 dyn_cast<cgraph_node *> (ref->referred),
1295 prob);
1296 e->speculative = false;
1297 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1298 false);
1299 e->count = gimple_bb (e->call_stmt)->count;
1300 e2->speculative = false;
1301 e2->count = gimple_bb (e2->call_stmt)->count;
1302 ref->speculative = false;
1303 ref->stmt = NULL;
1304 /* Indirect edges are not both in the call site hash.
1305 get it updated. */
1306 if (e->caller->call_site_hash)
1307 cgraph_update_edge_in_call_site_hash (e2);
1308 pop_cfun ();
1309 /* Continue redirecting E to proper target. */
1314 if (e->indirect_unknown_callee
1315 || decl == e->callee->decl)
1316 return e->call_stmt;
1318 if (flag_checking && decl)
1320 cgraph_node *node = cgraph_node::get (decl);
1321 gcc_assert (!node || !node->clone.param_adjustments);
1324 if (symtab->dump_file)
1326 fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1327 e->caller->dump_name (), e->callee->dump_name ());
1328 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1329 if (e->callee->clone.param_adjustments)
1330 e->callee->clone.param_adjustments->dump (symtab->dump_file);
1331 unsigned performed_len
1332 = vec_safe_length (e->caller->clone.performed_splits);
1333 if (performed_len > 0)
1334 fprintf (symtab->dump_file, "Performed splits records:\n");
1335 for (unsigned i = 0; i < performed_len; i++)
1337 ipa_param_performed_split *sm
1338 = &(*e->caller->clone.performed_splits)[i];
1339 print_node_brief (symtab->dump_file, " dummy_decl: ", sm->dummy_decl,
1340 TDF_UID);
1341 fprintf (symtab->dump_file, ", unit_offset: %u\n", sm->unit_offset);
1345 if (ipa_param_adjustments *padjs = e->callee->clone.param_adjustments)
1347 /* We need to defer cleaning EH info on the new statement to
1348 fixup-cfg. We may not have dominator information at this point
1349 and thus would end up with unreachable blocks and have no way
1350 to communicate that we need to run CFG cleanup then. */
1351 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1352 if (lp_nr != 0)
1353 remove_stmt_from_eh_lp (e->call_stmt);
1355 tree old_fntype = gimple_call_fntype (e->call_stmt);
1356 new_stmt = padjs->modify_call (e->call_stmt,
1357 e->caller->clone.performed_splits,
1358 e->callee->decl, false);
1359 cgraph_node *origin = e->callee;
1360 while (origin->clone_of)
1361 origin = origin->clone_of;
1363 if ((origin->former_clone_of
1364 && old_fntype == TREE_TYPE (origin->former_clone_of))
1365 || old_fntype == TREE_TYPE (origin->decl))
1366 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1367 else
1369 tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1370 gimple_call_set_fntype (new_stmt, new_fntype);
1373 if (lp_nr != 0)
1374 add_stmt_to_eh_lp (new_stmt, lp_nr);
1376 else
1378 new_stmt = e->call_stmt;
1379 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1380 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1383 /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1384 adjust gimple_call_fntype too. */
1385 if (gimple_call_noreturn_p (new_stmt)
1386 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1387 && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1388 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1389 == void_type_node))
1390 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1392 /* If the call becomes noreturn, remove the LHS if possible. */
1393 tree lhs = gimple_call_lhs (new_stmt);
1394 if (lhs
1395 && gimple_call_noreturn_p (new_stmt)
1396 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1397 || should_remove_lhs_p (lhs)))
1399 if (TREE_CODE (lhs) == SSA_NAME)
1401 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1402 TREE_TYPE (lhs), NULL);
1403 var = get_or_create_ssa_default_def
1404 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1405 gimple *set_stmt = gimple_build_assign (lhs, var);
1406 gsi = gsi_for_stmt (new_stmt);
1407 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1408 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1410 gimple_call_set_lhs (new_stmt, NULL_TREE);
1411 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1414 /* If new callee has no static chain, remove it. */
1415 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1417 gimple_call_set_chain (new_stmt, NULL);
1418 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1421 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1422 new_stmt);
1424 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1426 if (symtab->dump_file)
1428 fprintf (symtab->dump_file, " updated to:");
1429 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1431 return new_stmt;
1434 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1435 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1436 of OLD_STMT if it was previously call statement.
1437 If NEW_STMT is NULL, the call has been dropped without any
1438 replacement. */
1440 static void
1441 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1442 gimple *old_stmt, tree old_call,
1443 gimple *new_stmt)
1445 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1446 ? gimple_call_fndecl (new_stmt) : 0;
1448 /* We are seeing indirect calls, then there is nothing to update. */
1449 if (!new_call && !old_call)
1450 return;
1451 /* See if we turned indirect call into direct call or folded call to one builtin
1452 into different builtin. */
1453 if (old_call != new_call)
1455 cgraph_edge *e = node->get_edge (old_stmt);
1456 cgraph_edge *ne = NULL;
1457 profile_count count;
1459 if (e)
1461 /* Keep calls marked as dead dead. */
1462 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1463 && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
1465 node->get_edge (old_stmt)->set_call_stmt
1466 (as_a <gcall *> (new_stmt));
1467 return;
1469 /* See if the edge is already there and has the correct callee. It
1470 might be so because of indirect inlining has already updated
1471 it. We also might've cloned and redirected the edge. */
1472 if (new_call && e->callee)
1474 cgraph_node *callee = e->callee;
1475 while (callee)
1477 if (callee->decl == new_call
1478 || callee->former_clone_of == new_call)
1480 e->set_call_stmt (as_a <gcall *> (new_stmt));
1481 return;
1483 callee = callee->clone_of;
1487 /* Otherwise remove edge and create new one; we can't simply redirect
1488 since function has changed, so inline plan and other information
1489 attached to edge is invalid. */
1490 count = e->count;
1491 if (e->indirect_unknown_callee || e->inline_failed)
1492 e->remove ();
1493 else
1494 e->callee->remove_symbol_and_inline_clones ();
1496 else if (new_call)
1498 /* We are seeing new direct call; compute profile info based on BB. */
1499 basic_block bb = gimple_bb (new_stmt);
1500 count = bb->count;
1503 if (new_call)
1505 ne = node->create_edge (cgraph_node::get_create (new_call),
1506 as_a <gcall *> (new_stmt), count);
1507 gcc_assert (ne->inline_failed);
1510 /* We only updated the call stmt; update pointer in cgraph edge.. */
1511 else if (old_stmt != new_stmt)
1512 node->get_edge (old_stmt)->set_call_stmt (as_a <gcall *> (new_stmt));
1515 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1516 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1517 of OLD_STMT before it was updated (updating can happen inplace). */
1519 void
1520 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1521 gimple *new_stmt)
1523 cgraph_node *orig = cgraph_node::get (cfun->decl);
1524 cgraph_node *node;
1526 gcc_checking_assert (orig);
1527 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1528 if (orig->clones)
1529 for (node = orig->clones; node != orig;)
1531 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1532 if (node->clones)
1533 node = node->clones;
1534 else if (node->next_sibling_clone)
1535 node = node->next_sibling_clone;
1536 else
1538 while (node != orig && !node->next_sibling_clone)
1539 node = node->clone_of;
1540 if (node != orig)
1541 node = node->next_sibling_clone;
1547 /* Remove all callees from the node. */
1549 void
1550 cgraph_node::remove_callees (void)
1552 cgraph_edge *e, *f;
1554 /* It is sufficient to remove the edges from the lists of callers of
1555 the callees. The callee list of the node can be zapped with one
1556 assignment. */
1557 for (e = callees; e; e = f)
1559 f = e->next_callee;
1560 symtab->call_edge_removal_hooks (e);
1561 if (!e->indirect_unknown_callee)
1562 e->remove_callee ();
1563 symtab->free_edge (e);
1565 for (e = indirect_calls; e; e = f)
1567 f = e->next_callee;
1568 symtab->call_edge_removal_hooks (e);
1569 if (!e->indirect_unknown_callee)
1570 e->remove_callee ();
1571 symtab->free_edge (e);
1573 indirect_calls = NULL;
1574 callees = NULL;
1575 if (call_site_hash)
1577 call_site_hash->empty ();
1578 call_site_hash = NULL;
1582 /* Remove all callers from the node. */
1584 void
1585 cgraph_node::remove_callers (void)
1587 cgraph_edge *e, *f;
1589 /* It is sufficient to remove the edges from the lists of callees of
1590 the callers. The caller list of the node can be zapped with one
1591 assignment. */
1592 for (e = callers; e; e = f)
1594 f = e->next_caller;
1595 symtab->call_edge_removal_hooks (e);
1596 e->remove_caller ();
1597 symtab->free_edge (e);
1599 callers = NULL;
1602 /* Helper function for cgraph_release_function_body and free_lang_data.
1603 It releases body from function DECL without having to inspect its
1604 possibly non-existent symtab node. */
1606 void
1607 release_function_body (tree decl)
1609 function *fn = DECL_STRUCT_FUNCTION (decl);
1610 if (fn)
1612 if (fn->cfg
1613 && loops_for_fn (fn))
1615 fn->curr_properties &= ~PROP_loops;
1616 loop_optimizer_finalize (fn);
1618 if (fn->gimple_df)
1620 delete_tree_ssa (fn);
1621 fn->eh = NULL;
1623 if (fn->cfg)
1625 gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1626 gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1627 delete_tree_cfg_annotations (fn);
1628 clear_edges (fn);
1629 fn->cfg = NULL;
1631 if (fn->value_histograms)
1632 free_histograms (fn);
1633 gimple_set_body (decl, NULL);
1634 /* Struct function hangs a lot of data that would leak if we didn't
1635 removed all pointers to it. */
1636 ggc_free (fn);
1637 DECL_STRUCT_FUNCTION (decl) = NULL;
1639 DECL_SAVED_TREE (decl) = NULL;
1642 /* Release memory used to represent body of function.
1643 Use this only for functions that are released before being translated to
1644 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1645 are free'd in final.c via free_after_compilation().
1646 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1648 void
1649 cgraph_node::release_body (bool keep_arguments)
1651 ipa_transforms_to_apply.release ();
1652 if (!used_as_abstract_origin && symtab->state != PARSING)
1654 DECL_RESULT (decl) = NULL;
1656 if (!keep_arguments)
1657 DECL_ARGUMENTS (decl) = NULL;
1659 /* If the node is abstract and needed, then do not clear
1660 DECL_INITIAL of its associated function declaration because it's
1661 needed to emit debug info later. */
1662 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1663 DECL_INITIAL (decl) = error_mark_node;
1664 release_function_body (decl);
1665 if (lto_file_data)
1667 lto_free_function_in_decl_state_for_node (this);
1668 lto_file_data = NULL;
1672 /* Remove function from symbol table. */
1674 void
1675 cgraph_node::remove (void)
1677 if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1678 fprintf (symtab->ipa_clones_dump_file,
1679 "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1680 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1681 DECL_SOURCE_COLUMN (decl));
1683 symtab->call_cgraph_removal_hooks (this);
1684 remove_callers ();
1685 remove_callees ();
1686 ipa_transforms_to_apply.release ();
1687 delete_function_version (function_version ());
1689 /* Incremental inlining access removed nodes stored in the postorder list.
1691 force_output = false;
1692 forced_by_abi = false;
1693 cgraph_node *next;
1694 for (cgraph_node *n = nested; n; n = next)
1696 next = n->next_nested;
1697 n->origin = NULL;
1698 n->next_nested = NULL;
1700 nested = NULL;
1701 if (origin)
1703 cgraph_node **node2 = &origin->nested;
1705 while (*node2 != this)
1706 node2 = &(*node2)->next_nested;
1707 *node2 = next_nested;
1709 unregister ();
1710 if (prev_sibling_clone)
1711 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1712 else if (clone_of)
1713 clone_of->clones = next_sibling_clone;
1714 if (next_sibling_clone)
1715 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1716 if (clones)
1718 cgraph_node *n, *next;
1720 if (clone_of)
1722 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1723 n->clone_of = clone_of;
1724 n->clone_of = clone_of;
1725 n->next_sibling_clone = clone_of->clones;
1726 if (clone_of->clones)
1727 clone_of->clones->prev_sibling_clone = n;
1728 clone_of->clones = clones;
1730 else
1732 /* We are removing node with clones. This makes clones inconsistent,
1733 but assume they will be removed subsequently and just keep clone
1734 tree intact. This can happen in unreachable function removal since
1735 we remove unreachable functions in random order, not by bottom-up
1736 walk of clone trees. */
1737 for (n = clones; n; n = next)
1739 next = n->next_sibling_clone;
1740 n->next_sibling_clone = NULL;
1741 n->prev_sibling_clone = NULL;
1742 n->clone_of = NULL;
1747 /* While all the clones are removed after being proceeded, the function
1748 itself is kept in the cgraph even after it is compiled. Check whether
1749 we are done with this body and reclaim it proactively if this is the case.
1751 if (symtab->state != LTO_STREAMING)
1753 cgraph_node *n = cgraph_node::get (decl);
1754 if (!n
1755 || (!n->clones && !n->clone_of && !n->inlined_to
1756 && ((symtab->global_info_ready || in_lto_p)
1757 && (TREE_ASM_WRITTEN (n->decl)
1758 || DECL_EXTERNAL (n->decl)
1759 || !n->analyzed
1760 || (!flag_wpa && n->in_other_partition)))))
1761 release_body ();
1763 else
1765 lto_free_function_in_decl_state_for_node (this);
1766 lto_file_data = NULL;
1769 decl = NULL;
1770 if (call_site_hash)
1772 call_site_hash->empty ();
1773 call_site_hash = NULL;
1776 symtab->release_symbol (this);
1779 /* Likewise indicate that a node is having address taken. */
1781 void
1782 cgraph_node::mark_address_taken (void)
1784 /* Indirect inlining can figure out that all uses of the address are
1785 inlined. */
1786 if (inlined_to)
1788 gcc_assert (cfun->after_inlining);
1789 gcc_assert (callers->indirect_inlining_edge);
1790 return;
1792 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1793 IPA_REF_ADDR reference exists (and thus it should be set on node
1794 representing alias we take address of) and as a test whether address
1795 of the object was taken (and thus it should be set on node alias is
1796 referring to). We should remove the first use and the remove the
1797 following set. */
1798 address_taken = 1;
1799 cgraph_node *node = ultimate_alias_target ();
1800 node->address_taken = 1;
1803 /* Return local info node for the compiled function. */
1805 cgraph_node *
1806 cgraph_node::local_info_node (tree decl)
1808 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1809 cgraph_node *node = get (decl);
1810 if (!node)
1811 return NULL;
1812 return node->ultimate_alias_target ();
1815 /* Return RTL info for the compiled function. */
1817 cgraph_rtl_info *
1818 cgraph_node::rtl_info (const_tree decl)
1820 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1821 cgraph_node *node = get (decl);
1822 if (!node)
1823 return NULL;
1824 enum availability avail;
1825 node = node->ultimate_alias_target (&avail);
1826 if (decl != current_function_decl
1827 && (avail < AVAIL_AVAILABLE
1828 || (node->decl != current_function_decl
1829 && !TREE_ASM_WRITTEN (node->decl))))
1830 return NULL;
1831 /* Allocate if it doesn't exist. */
1832 if (node->rtl == NULL)
1834 node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
1835 SET_HARD_REG_SET (node->rtl->function_used_regs);
1837 return node->rtl;
1840 /* Return a string describing the failure REASON. */
1842 const char*
1843 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1845 #undef DEFCIFCODE
1846 #define DEFCIFCODE(code, type, string) string,
1848 static const char *cif_string_table[CIF_N_REASONS] = {
1849 #include "cif-code.def"
1852 /* Signedness of an enum type is implementation defined, so cast it
1853 to unsigned before testing. */
1854 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1855 return cif_string_table[reason];
1858 /* Return a type describing the failure REASON. */
1860 cgraph_inline_failed_type_t
1861 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1863 #undef DEFCIFCODE
1864 #define DEFCIFCODE(code, type, string) type,
1866 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1867 #include "cif-code.def"
1870 /* Signedness of an enum type is implementation defined, so cast it
1871 to unsigned before testing. */
1872 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1873 return cif_type_table[reason];
1876 /* Names used to print out the availability enum. */
1877 const char * const cgraph_availability_names[] =
1878 {"unset", "not_available", "overwritable", "available", "local"};
1880 /* Output flags of edge to a file F. */
1882 void
1883 cgraph_edge::dump_edge_flags (FILE *f)
1885 if (speculative)
1886 fprintf (f, "(speculative) ");
1887 if (!inline_failed)
1888 fprintf (f, "(inlined) ");
1889 if (call_stmt_cannot_inline_p)
1890 fprintf (f, "(call_stmt_cannot_inline_p) ");
1891 if (indirect_inlining_edge)
1892 fprintf (f, "(indirect_inlining) ");
1893 if (count.initialized_p ())
1895 fprintf (f, "(");
1896 count.dump (f);
1897 fprintf (f, ",");
1898 fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
1900 if (can_throw_external)
1901 fprintf (f, "(can throw external) ");
1904 /* Dump call graph node to file F. */
1906 void
1907 cgraph_node::dump (FILE *f)
1909 cgraph_edge *edge;
1911 dump_base (f);
1913 if (inlined_to)
1914 fprintf (f, " Function %s is inline copy in %s\n",
1915 dump_name (),
1916 inlined_to->dump_name ());
1917 if (clone_of)
1918 fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
1919 if (symtab->function_flags_ready)
1920 fprintf (f, " Availability: %s\n",
1921 cgraph_availability_names [get_availability ()]);
1923 if (profile_id)
1924 fprintf (f, " Profile id: %i\n",
1925 profile_id);
1926 cgraph_function_version_info *vi = function_version ();
1927 if (vi != NULL)
1929 fprintf (f, " Version info: ");
1930 if (vi->prev != NULL)
1932 fprintf (f, "prev: ");
1933 fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
1935 if (vi->next != NULL)
1937 fprintf (f, "next: ");
1938 fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
1940 if (vi->dispatcher_resolver != NULL_TREE)
1941 fprintf (f, "dispatcher: %s",
1942 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
1944 fprintf (f, "\n");
1946 fprintf (f, " Function flags:");
1947 if (count.initialized_p ())
1949 fprintf (f, " count:");
1950 count.dump (f);
1952 if (tp_first_run > 0)
1953 fprintf (f, " first_run:%i", tp_first_run);
1954 if (origin)
1955 fprintf (f, " nested in:%s", origin->asm_name ());
1956 if (gimple_has_body_p (decl))
1957 fprintf (f, " body");
1958 if (process)
1959 fprintf (f, " process");
1960 if (local)
1961 fprintf (f, " local");
1962 if (redefined_extern_inline)
1963 fprintf (f, " redefined_extern_inline");
1964 if (only_called_at_startup)
1965 fprintf (f, " only_called_at_startup");
1966 if (only_called_at_exit)
1967 fprintf (f, " only_called_at_exit");
1968 if (tm_clone)
1969 fprintf (f, " tm_clone");
1970 if (calls_comdat_local)
1971 fprintf (f, " calls_comdat_local");
1972 if (icf_merged)
1973 fprintf (f, " icf_merged");
1974 if (merged_comdat)
1975 fprintf (f, " merged_comdat");
1976 if (split_part)
1977 fprintf (f, " split_part");
1978 if (indirect_call_target)
1979 fprintf (f, " indirect_call_target");
1980 if (nonfreeing_fn)
1981 fprintf (f, " nonfreeing_fn");
1982 if (DECL_STATIC_CONSTRUCTOR (decl))
1983 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
1984 if (DECL_STATIC_DESTRUCTOR (decl))
1985 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
1986 if (frequency == NODE_FREQUENCY_HOT)
1987 fprintf (f, " hot");
1988 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
1989 fprintf (f, " unlikely_executed");
1990 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
1991 fprintf (f, " executed_once");
1992 if (opt_for_fn (decl, optimize_size))
1993 fprintf (f, " optimize_size");
1994 if (parallelized_function)
1995 fprintf (f, " parallelized_function");
1996 if (DECL_IS_OPERATOR_NEW_P (decl))
1997 fprintf (f, " operator_new");
1998 if (DECL_IS_OPERATOR_DELETE_P (decl))
1999 fprintf (f, " operator_delete");
2002 fprintf (f, "\n");
2004 if (thunk.thunk_p)
2006 fprintf (f, " Thunk");
2007 if (thunk.alias)
2008 fprintf (f, " of %s (asm:%s)",
2009 lang_hooks.decl_printable_name (thunk.alias, 2),
2010 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2011 fprintf (f, " fixed offset %i virtual value %i indirect_offset %i "
2012 "has virtual offset %i\n",
2013 (int)thunk.fixed_offset,
2014 (int)thunk.virtual_value,
2015 (int)thunk.indirect_offset,
2016 (int)thunk.virtual_offset_p);
2018 else if (former_thunk_p ())
2019 fprintf (f, " Former thunk fixed offset %i virtual value %i "
2020 "indirect_offset %i has virtual offset %i\n",
2021 (int)thunk.fixed_offset,
2022 (int)thunk.virtual_value,
2023 (int)thunk.indirect_offset,
2024 (int)thunk.virtual_offset_p);
2025 if (alias && thunk.alias
2026 && DECL_P (thunk.alias))
2028 fprintf (f, " Alias of %s",
2029 lang_hooks.decl_printable_name (thunk.alias, 2));
2030 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2031 fprintf (f, " (asm:%s)",
2032 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2033 fprintf (f, "\n");
2036 fprintf (f, " Called by: ");
2038 profile_count sum = profile_count::zero ();
2039 for (edge = callers; edge; edge = edge->next_caller)
2041 fprintf (f, "%s ", edge->caller->dump_name ());
2042 edge->dump_edge_flags (f);
2043 if (edge->count.initialized_p ())
2044 sum += edge->count.ipa ();
2047 fprintf (f, "\n Calls: ");
2048 for (edge = callees; edge; edge = edge->next_callee)
2050 fprintf (f, "%s ", edge->callee->dump_name ());
2051 edge->dump_edge_flags (f);
2053 fprintf (f, "\n");
2055 if (count.ipa ().initialized_p ())
2057 bool ok = true;
2058 bool min = false;
2059 ipa_ref *ref;
2061 FOR_EACH_ALIAS (this, ref)
2062 if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2063 sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2065 if (inlined_to
2066 || (symtab->state < EXPANSION
2067 && ultimate_alias_target () == this && only_called_directly_p ()))
2068 ok = !count.ipa ().differs_from_p (sum);
2069 else if (count.ipa () > profile_count::from_gcov_type (100)
2070 && count.ipa () < sum.apply_scale (99, 100))
2071 ok = false, min = true;
2072 if (!ok)
2074 fprintf (f, " Invalid sum of caller counts ");
2075 sum.dump (f);
2076 if (min)
2077 fprintf (f, ", should be at most ");
2078 else
2079 fprintf (f, ", should be ");
2080 count.ipa ().dump (f);
2081 fprintf (f, "\n");
2085 for (edge = indirect_calls; edge; edge = edge->next_callee)
2087 if (edge->indirect_info->polymorphic)
2089 fprintf (f, " Polymorphic indirect call of type ");
2090 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2091 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2093 else
2094 fprintf (f, " Indirect call");
2095 edge->dump_edge_flags (f);
2096 if (edge->indirect_info->param_index != -1)
2098 fprintf (f, " of param:%i", edge->indirect_info->param_index);
2099 if (edge->indirect_info->agg_contents)
2100 fprintf (f, " loaded from %s %s at offset %i",
2101 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2102 edge->indirect_info->by_ref ? "passed by reference":"",
2103 (int)edge->indirect_info->offset);
2104 if (edge->indirect_info->vptr_changed)
2105 fprintf (f, " (vptr maybe changed)");
2107 fprintf (f, "\n");
2108 if (edge->indirect_info->polymorphic)
2109 edge->indirect_info->context.dump (f);
2113 /* Dump call graph node to file F in graphviz format. */
2115 void
2116 cgraph_node::dump_graphviz (FILE *f)
2118 cgraph_edge *edge;
2120 for (edge = callees; edge; edge = edge->next_callee)
2122 cgraph_node *callee = edge->callee;
2124 fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2129 /* Dump call graph node NODE to stderr. */
2131 DEBUG_FUNCTION void
2132 cgraph_node::debug (void)
2134 dump (stderr);
2137 /* Dump the callgraph to file F. */
2139 void
2140 cgraph_node::dump_cgraph (FILE *f)
2142 cgraph_node *node;
2144 fprintf (f, "callgraph:\n\n");
2145 FOR_EACH_FUNCTION (node)
2146 node->dump (f);
2149 /* Return true when the DECL can possibly be inlined. */
2151 bool
2152 cgraph_function_possibly_inlined_p (tree decl)
2154 if (!symtab->global_info_ready)
2155 return !DECL_UNINLINABLE (decl);
2156 return DECL_POSSIBLY_INLINED (decl);
2159 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2160 void
2161 cgraph_node::unnest (void)
2163 cgraph_node **node2 = &origin->nested;
2164 gcc_assert (origin);
2166 while (*node2 != this)
2167 node2 = &(*node2)->next_nested;
2168 *node2 = next_nested;
2169 origin = NULL;
2172 /* Return function availability. See cgraph.h for description of individual
2173 return values. */
2174 enum availability
2175 cgraph_node::get_availability (symtab_node *ref)
2177 if (ref)
2179 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2180 if (cref)
2181 ref = cref->inlined_to;
2183 enum availability avail;
2184 if (!analyzed)
2185 avail = AVAIL_NOT_AVAILABLE;
2186 else if (local)
2187 avail = AVAIL_LOCAL;
2188 else if (inlined_to)
2189 avail = AVAIL_AVAILABLE;
2190 else if (transparent_alias)
2191 ultimate_alias_target (&avail, ref);
2192 else if (ifunc_resolver
2193 || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2194 avail = AVAIL_INTERPOSABLE;
2195 else if (!externally_visible)
2196 avail = AVAIL_AVAILABLE;
2197 /* If this is a reference from symbol itself and there are no aliases, we
2198 may be sure that the symbol was not interposed by something else because
2199 the symbol itself would be unreachable otherwise.
2201 Also comdat groups are always resolved in groups. */
2202 else if ((this == ref && !has_aliases_p ())
2203 || (ref && get_comdat_group ()
2204 && get_comdat_group () == ref->get_comdat_group ()))
2205 avail = AVAIL_AVAILABLE;
2206 /* Inline functions are safe to be analyzed even if their symbol can
2207 be overwritten at runtime. It is not meaningful to enforce any sane
2208 behavior on replacing inline function by different body. */
2209 else if (DECL_DECLARED_INLINE_P (decl))
2210 avail = AVAIL_AVAILABLE;
2212 /* If the function can be overwritten, return OVERWRITABLE. Take
2213 care at least of two notable extensions - the COMDAT functions
2214 used to share template instantiations in C++ (this is symmetric
2215 to code cp_cannot_inline_tree_fn and probably shall be shared and
2216 the inlinability hooks completely eliminated). */
2218 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2219 avail = AVAIL_INTERPOSABLE;
2220 else avail = AVAIL_AVAILABLE;
2222 return avail;
2225 /* Worker for cgraph_node_can_be_local_p. */
2226 static bool
2227 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2229 return !(!node->force_output
2230 && ((DECL_COMDAT (node->decl)
2231 && !node->forced_by_abi
2232 && !node->used_from_object_file_p ()
2233 && !node->same_comdat_group)
2234 || !node->externally_visible));
2237 /* Return true if cgraph_node can be made local for API change.
2238 Extern inline functions and C++ COMDAT functions can be made local
2239 at the expense of possible code size growth if function is used in multiple
2240 compilation units. */
2241 bool
2242 cgraph_node::can_be_local_p (void)
2244 return (!address_taken
2245 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2246 NULL, true));
2249 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2250 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2251 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2252 skipped. */
2253 bool
2254 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2255 (cgraph_node *, void *),
2256 void *data,
2257 bool include_overwritable,
2258 bool exclude_virtual_thunks)
2260 cgraph_edge *e;
2261 ipa_ref *ref;
2262 enum availability avail = AVAIL_AVAILABLE;
2264 if (include_overwritable
2265 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2267 if (callback (this, data))
2268 return true;
2270 FOR_EACH_ALIAS (this, ref)
2272 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2273 if (include_overwritable
2274 || alias->get_availability () > AVAIL_INTERPOSABLE)
2275 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2276 include_overwritable,
2277 exclude_virtual_thunks))
2278 return true;
2280 if (avail <= AVAIL_INTERPOSABLE)
2281 return false;
2282 for (e = callers; e; e = e->next_caller)
2283 if (e->caller->thunk.thunk_p
2284 && (include_overwritable
2285 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2286 && !(exclude_virtual_thunks
2287 && e->caller->thunk.virtual_offset_p))
2288 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2289 include_overwritable,
2290 exclude_virtual_thunks))
2291 return true;
2293 return false;
2296 /* Worker to bring NODE local. */
2298 bool
2299 cgraph_node::make_local (cgraph_node *node, void *)
2301 gcc_checking_assert (node->can_be_local_p ());
2302 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2304 node->make_decl_local ();
2305 node->set_section (NULL);
2306 node->set_comdat_group (NULL);
2307 node->externally_visible = false;
2308 node->forced_by_abi = false;
2309 node->local = true;
2310 node->set_section (NULL);
2311 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2312 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2313 && !flag_incremental_link);
2314 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2315 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2317 return false;
2320 /* Bring cgraph node local. */
2322 void
2323 cgraph_node::make_local (void)
2325 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2328 /* Worker to set nothrow flag. */
2330 static void
2331 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2332 bool *changed)
2334 cgraph_edge *e;
2336 if (nothrow && !TREE_NOTHROW (node->decl))
2338 /* With non-call exceptions we can't say for sure if other function body
2339 was not possibly optimized to still throw. */
2340 if (!non_call || node->binds_to_current_def_p ())
2342 TREE_NOTHROW (node->decl) = true;
2343 *changed = true;
2344 for (e = node->callers; e; e = e->next_caller)
2345 e->can_throw_external = false;
2348 else if (!nothrow && TREE_NOTHROW (node->decl))
2350 TREE_NOTHROW (node->decl) = false;
2351 *changed = true;
2353 ipa_ref *ref;
2354 FOR_EACH_ALIAS (node, ref)
2356 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2357 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2358 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2360 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2361 if (e->caller->thunk.thunk_p
2362 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2363 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2366 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2367 if any to NOTHROW. */
2369 bool
2370 cgraph_node::set_nothrow_flag (bool nothrow)
2372 bool changed = false;
2373 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2375 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2376 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2377 else
2379 ipa_ref *ref;
2381 FOR_EACH_ALIAS (this, ref)
2383 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2384 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2385 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2388 return changed;
2391 /* Worker to set malloc flag. */
2392 static void
2393 set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2395 if (malloc_p && !DECL_IS_MALLOC (node->decl))
2397 DECL_IS_MALLOC (node->decl) = true;
2398 *changed = true;
2401 ipa_ref *ref;
2402 FOR_EACH_ALIAS (node, ref)
2404 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2405 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2406 set_malloc_flag_1 (alias, malloc_p, changed);
2409 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2410 if (e->caller->thunk.thunk_p
2411 && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2412 set_malloc_flag_1 (e->caller, malloc_p, changed);
2415 /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2417 bool
2418 cgraph_node::set_malloc_flag (bool malloc_p)
2420 bool changed = false;
2422 if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2423 set_malloc_flag_1 (this, malloc_p, &changed);
2424 else
2426 ipa_ref *ref;
2428 FOR_EACH_ALIAS (this, ref)
2430 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2431 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2432 set_malloc_flag_1 (alias, malloc_p, &changed);
2435 return changed;
2438 /* Worker to set_const_flag. */
2440 static void
2441 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2442 bool *changed)
2444 /* Static constructors and destructors without a side effect can be
2445 optimized out. */
2446 if (set_const && !looping)
2448 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2450 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2451 *changed = true;
2453 if (DECL_STATIC_DESTRUCTOR (node->decl))
2455 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2456 *changed = true;
2459 if (!set_const)
2461 if (TREE_READONLY (node->decl))
2463 TREE_READONLY (node->decl) = 0;
2464 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2465 *changed = true;
2468 else
2470 /* Consider function:
2472 bool a(int *p)
2474 return *p==*p;
2477 During early optimization we will turn this into:
2479 bool a(int *p)
2481 return true;
2484 Now if this function will be detected as CONST however when interposed
2485 it may end up being just pure. We always must assume the worst
2486 scenario here. */
2487 if (TREE_READONLY (node->decl))
2489 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2491 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2492 *changed = true;
2495 else if (node->binds_to_current_def_p ())
2497 TREE_READONLY (node->decl) = true;
2498 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2499 DECL_PURE_P (node->decl) = false;
2500 *changed = true;
2502 else
2504 if (dump_file && (dump_flags & TDF_DETAILS))
2505 fprintf (dump_file, "Dropping state to PURE because function does "
2506 "not bind to current def.\n");
2507 if (!DECL_PURE_P (node->decl))
2509 DECL_PURE_P (node->decl) = true;
2510 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2511 *changed = true;
2513 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2515 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2516 *changed = true;
2521 ipa_ref *ref;
2522 FOR_EACH_ALIAS (node, ref)
2524 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2525 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2526 set_const_flag_1 (alias, set_const, looping, changed);
2528 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2529 if (e->caller->thunk.thunk_p
2530 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2532 /* Virtual thunks access virtual offset in the vtable, so they can
2533 only be pure, never const. */
2534 if (set_const
2535 && (e->caller->thunk.virtual_offset_p
2536 || !node->binds_to_current_def_p (e->caller)))
2537 *changed |= e->caller->set_pure_flag (true, looping);
2538 else
2539 set_const_flag_1 (e->caller, set_const, looping, changed);
2543 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2544 If SET_CONST if false, clear the flag.
2546 When setting the flag be careful about possible interposition and
2547 do not set the flag for functions that can be interposed and set pure
2548 flag for functions that can bind to other definition.
2550 Return true if any change was done. */
2552 bool
2553 cgraph_node::set_const_flag (bool set_const, bool looping)
2555 bool changed = false;
2556 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2557 set_const_flag_1 (this, set_const, looping, &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 (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2566 set_const_flag_1 (alias, set_const, looping, &changed);
2569 return changed;
2572 /* Info used by set_pure_flag_1. */
2574 struct set_pure_flag_info
2576 bool pure;
2577 bool looping;
2578 bool changed;
2581 /* Worker to set_pure_flag. */
2583 static bool
2584 set_pure_flag_1 (cgraph_node *node, void *data)
2586 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2587 /* Static constructors and destructors without a side effect can be
2588 optimized out. */
2589 if (info->pure && !info->looping)
2591 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2593 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2594 info->changed = true;
2596 if (DECL_STATIC_DESTRUCTOR (node->decl))
2598 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2599 info->changed = true;
2602 if (info->pure)
2604 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2606 DECL_PURE_P (node->decl) = true;
2607 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2608 info->changed = true;
2610 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2611 && !info->looping)
2613 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2614 info->changed = true;
2617 else
2619 if (DECL_PURE_P (node->decl))
2621 DECL_PURE_P (node->decl) = false;
2622 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2623 info->changed = true;
2626 return false;
2629 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2630 if any to PURE.
2632 When setting the flag, be careful about possible interposition.
2633 Return true if any change was done. */
2635 bool
2636 cgraph_node::set_pure_flag (bool pure, bool looping)
2638 struct set_pure_flag_info info = {pure, looping, false};
2639 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2640 return info.changed;
2643 /* Return true when cgraph_node cannot return or throw and thus
2644 it is safe to ignore its side effects for IPA analysis. */
2646 bool
2647 cgraph_node::cannot_return_p (void)
2649 int flags = flags_from_decl_or_type (decl);
2650 if (!opt_for_fn (decl, flag_exceptions))
2651 return (flags & ECF_NORETURN) != 0;
2652 else
2653 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2654 == (ECF_NORETURN | ECF_NOTHROW));
2657 /* Return true when call of edge cannot lead to return from caller
2658 and thus it is safe to ignore its side effects for IPA analysis
2659 when computing side effects of the caller.
2660 FIXME: We could actually mark all edges that have no reaching
2661 patch to the exit block or throw to get better results. */
2662 bool
2663 cgraph_edge::cannot_lead_to_return_p (void)
2665 if (caller->cannot_return_p ())
2666 return true;
2667 if (indirect_unknown_callee)
2669 int flags = indirect_info->ecf_flags;
2670 if (!opt_for_fn (caller->decl, flag_exceptions))
2671 return (flags & ECF_NORETURN) != 0;
2672 else
2673 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2674 == (ECF_NORETURN | ECF_NOTHROW));
2676 else
2677 return callee->cannot_return_p ();
2680 /* Return true if the edge may be considered hot. */
2682 bool
2683 cgraph_edge::maybe_hot_p (void)
2685 if (!maybe_hot_count_p (NULL, count.ipa ()))
2686 return false;
2687 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2688 || (callee
2689 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2690 return false;
2691 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2692 && (callee
2693 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2694 return false;
2695 if (opt_for_fn (caller->decl, optimize_size))
2696 return false;
2697 if (caller->frequency == NODE_FREQUENCY_HOT)
2698 return true;
2699 if (!count.initialized_p ())
2700 return true;
2701 cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
2702 if (!where->count.initialized_p ())
2703 return false;
2704 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2706 if (count.apply_scale (2, 1) < where->count.apply_scale (3, 1))
2707 return false;
2709 else if (count.apply_scale (param_hot_bb_frequency_fraction , 1)
2710 < where->count)
2711 return false;
2712 return true;
2715 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2717 static bool
2718 nonremovable_p (cgraph_node *node, void *)
2720 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2723 /* Return true if whole comdat group can be removed if there are no direct
2724 calls to THIS. */
2726 bool
2727 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2729 struct ipa_ref *ref;
2731 /* For local symbols or non-comdat group it is the same as
2732 can_remove_if_no_direct_calls_p. */
2733 if (!externally_visible || !same_comdat_group)
2735 if (DECL_EXTERNAL (decl))
2736 return true;
2737 if (address_taken)
2738 return false;
2739 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2742 if (will_inline && address_taken)
2743 return false;
2745 /* Otherwise check if we can remove the symbol itself and then verify
2746 that only uses of the comdat groups are direct call to THIS
2747 or its aliases. */
2748 if (!can_remove_if_no_direct_calls_and_refs_p ())
2749 return false;
2751 /* Check that all refs come from within the comdat group. */
2752 for (int i = 0; iterate_referring (i, ref); i++)
2753 if (ref->referring->get_comdat_group () != get_comdat_group ())
2754 return false;
2756 struct cgraph_node *target = ultimate_alias_target ();
2757 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2758 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2760 if (!externally_visible)
2761 continue;
2762 if (!next->alias
2763 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2764 return false;
2766 /* If we see different symbol than THIS, be sure to check calls. */
2767 if (next->ultimate_alias_target () != target)
2768 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2769 if (e->caller->get_comdat_group () != get_comdat_group ()
2770 || will_inline)
2771 return false;
2773 /* If function is not being inlined, we care only about
2774 references outside of the comdat group. */
2775 if (!will_inline)
2776 for (int i = 0; next->iterate_referring (i, ref); i++)
2777 if (ref->referring->get_comdat_group () != get_comdat_group ())
2778 return false;
2780 return true;
2783 /* Return true when function cgraph_node can be expected to be removed
2784 from program when direct calls in this compilation unit are removed.
2786 As a special case COMDAT functions are
2787 cgraph_can_remove_if_no_direct_calls_p while the are not
2788 cgraph_only_called_directly_p (it is possible they are called from other
2789 unit)
2791 This function behaves as cgraph_only_called_directly_p because eliminating
2792 all uses of COMDAT function does not make it necessarily disappear from
2793 the program unless we are compiling whole program or we do LTO. In this
2794 case we know we win since dynamic linking will not really discard the
2795 linkonce section. */
2797 bool
2798 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2799 (bool will_inline)
2801 gcc_assert (!inlined_to);
2802 if (DECL_EXTERNAL (decl))
2803 return true;
2805 if (!in_lto_p && !flag_whole_program)
2807 /* If the symbol is in comdat group, we need to verify that whole comdat
2808 group becomes unreachable. Technically we could skip references from
2809 within the group, too. */
2810 if (!only_called_directly_p ())
2811 return false;
2812 if (same_comdat_group && externally_visible)
2814 struct cgraph_node *target = ultimate_alias_target ();
2816 if (will_inline && address_taken)
2817 return true;
2818 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2819 next != this;
2820 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2822 if (!externally_visible)
2823 continue;
2824 if (!next->alias
2825 && !next->only_called_directly_p ())
2826 return false;
2828 /* If we see different symbol than THIS,
2829 be sure to check calls. */
2830 if (next->ultimate_alias_target () != target)
2831 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2832 if (e->caller->get_comdat_group () != get_comdat_group ()
2833 || will_inline)
2834 return false;
2837 return true;
2839 else
2840 return can_remove_if_no_direct_calls_p (will_inline);
2844 /* Worker for cgraph_only_called_directly_p. */
2846 static bool
2847 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2849 return !node->only_called_directly_or_aliased_p ();
2852 /* Return true when function cgraph_node and all its aliases are only called
2853 directly.
2854 i.e. it is not externally visible, address was not taken and
2855 it is not used in any other non-standard way. */
2857 bool
2858 cgraph_node::only_called_directly_p (void)
2860 gcc_assert (ultimate_alias_target () == this);
2861 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2862 NULL, true);
2866 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2868 static bool
2869 collect_callers_of_node_1 (cgraph_node *node, void *data)
2871 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2872 cgraph_edge *cs;
2873 enum availability avail;
2874 node->ultimate_alias_target (&avail);
2876 if (avail > AVAIL_INTERPOSABLE)
2877 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2878 if (!cs->indirect_inlining_edge
2879 && !cs->caller->thunk.thunk_p)
2880 redirect_callers->safe_push (cs);
2881 return false;
2884 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2885 cgraph_node (i.e. are not overwritable). */
2887 vec<cgraph_edge *>
2888 cgraph_node::collect_callers (void)
2890 vec<cgraph_edge *> redirect_callers = vNULL;
2891 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2892 &redirect_callers, false);
2893 return redirect_callers;
2897 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
2898 optimistically true if this cannot be determined. */
2900 static bool
2901 clone_of_p (cgraph_node *node, cgraph_node *node2)
2903 node = node->ultimate_alias_target ();
2904 node2 = node2->ultimate_alias_target ();
2906 if (node2->clone_of == node
2907 || node2->former_clone_of == node->decl)
2908 return true;
2910 if (!node->thunk.thunk_p && !node->former_thunk_p ())
2912 while (node2 && node->decl != node2->decl)
2913 node2 = node2->clone_of;
2914 return node2 != NULL;
2917 /* There are no virtual clones of thunks so check former_clone_of or if we
2918 might have skipped thunks because this adjustments are no longer
2919 necessary. */
2920 while (node->thunk.thunk_p || node->former_thunk_p ())
2922 if (!node->thunk.this_adjusting)
2923 return false;
2924 /* In case of instrumented expanded thunks, which can have multiple calls
2925 in them, we do not know how to continue and just have to be
2926 optimistic. */
2927 if (node->callees->next_callee)
2928 return true;
2929 node = node->callees->callee->ultimate_alias_target ();
2931 if (!node2->clone.param_adjustments
2932 || node2->clone.param_adjustments->first_param_intact_p ())
2933 return false;
2934 if (node2->former_clone_of == node->decl)
2935 return true;
2937 cgraph_node *n2 = node2;
2938 while (n2 && node->decl != n2->decl)
2939 n2 = n2->clone_of;
2940 if (n2)
2941 return true;
2944 return false;
2947 /* Verify edge count and frequency. */
2949 bool
2950 cgraph_edge::verify_count ()
2952 bool error_found = false;
2953 if (!count.verify ())
2955 error ("caller edge count invalid");
2956 error_found = true;
2958 return error_found;
2961 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2962 static void
2963 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
2965 bool fndecl_was_null = false;
2966 /* debug_gimple_stmt needs correct cfun */
2967 if (cfun != this_cfun)
2968 set_cfun (this_cfun);
2969 /* ...and an actual current_function_decl */
2970 if (!current_function_decl)
2972 current_function_decl = this_cfun->decl;
2973 fndecl_was_null = true;
2975 debug_gimple_stmt (stmt);
2976 if (fndecl_was_null)
2977 current_function_decl = NULL;
2980 /* Verify that call graph edge corresponds to DECL from the associated
2981 statement. Return true if the verification should fail. */
2983 bool
2984 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
2986 cgraph_node *node;
2988 if (!decl || callee->inlined_to)
2989 return false;
2990 if (symtab->state == LTO_STREAMING)
2991 return false;
2992 node = cgraph_node::get (decl);
2994 /* We do not know if a node from a different partition is an alias or what it
2995 aliases and therefore cannot do the former_clone_of check reliably. When
2996 body_removed is set, we have lost all information about what was alias or
2997 thunk of and also cannot proceed. */
2998 if (!node
2999 || node->body_removed
3000 || node->in_other_partition
3001 || callee->icf_merged
3002 || callee->in_other_partition)
3003 return false;
3005 node = node->ultimate_alias_target ();
3007 /* Optimizers can redirect unreachable calls or calls triggering undefined
3008 behavior to builtin_unreachable. */
3010 if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE))
3011 return false;
3013 if (callee->former_clone_of != node->decl
3014 && (node != callee->ultimate_alias_target ())
3015 && !clone_of_p (node, callee))
3016 return true;
3017 else
3018 return false;
3021 /* Disable warnings about missing quoting in GCC diagnostics for
3022 the verification errors. Their format strings don't follow GCC
3023 diagnostic conventions and the calls are ultimately followed by
3024 one to internal_error. */
3025 #if __GNUC__ >= 10
3026 # pragma GCC diagnostic push
3027 # pragma GCC diagnostic ignored "-Wformat-diag"
3028 #endif
3030 /* Verify cgraph nodes of given cgraph node. */
3031 DEBUG_FUNCTION void
3032 cgraph_node::verify_node (void)
3034 cgraph_edge *e;
3035 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3036 basic_block this_block;
3037 gimple_stmt_iterator gsi;
3038 bool error_found = false;
3040 if (seen_error ())
3041 return;
3043 timevar_push (TV_CGRAPH_VERIFY);
3044 error_found |= verify_base ();
3045 for (e = callees; e; e = e->next_callee)
3046 if (e->aux)
3048 error ("aux field set for edge %s->%s",
3049 identifier_to_locale (e->caller->name ()),
3050 identifier_to_locale (e->callee->name ()));
3051 error_found = true;
3053 if (!count.verify ())
3055 error ("cgraph count invalid");
3056 error_found = true;
3058 if (inlined_to && same_comdat_group)
3060 error ("inline clone in same comdat group list");
3061 error_found = true;
3063 if (!definition && !in_other_partition && local)
3065 error ("local symbols must be defined");
3066 error_found = true;
3068 if (inlined_to && externally_visible)
3070 error ("externally visible inline clone");
3071 error_found = true;
3073 if (inlined_to && address_taken)
3075 error ("inline clone with address taken");
3076 error_found = true;
3078 if (inlined_to && force_output)
3080 error ("inline clone is forced to output");
3081 error_found = true;
3083 for (e = indirect_calls; e; e = e->next_callee)
3085 if (e->aux)
3087 error ("aux field set for indirect edge from %s",
3088 identifier_to_locale (e->caller->name ()));
3089 error_found = true;
3091 if (!e->indirect_unknown_callee
3092 || !e->indirect_info)
3094 error ("An indirect edge from %s is not marked as indirect or has "
3095 "associated indirect_info, the corresponding statement is: ",
3096 identifier_to_locale (e->caller->name ()));
3097 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3098 error_found = true;
3101 bool check_comdat = comdat_local_p ();
3102 for (e = callers; e; e = e->next_caller)
3104 if (e->verify_count ())
3105 error_found = true;
3106 if (check_comdat
3107 && !in_same_comdat_group_p (e->caller))
3109 error ("comdat-local function called by %s outside its comdat",
3110 identifier_to_locale (e->caller->name ()));
3111 error_found = true;
3113 if (!e->inline_failed)
3115 if (inlined_to
3116 != (e->caller->inlined_to
3117 ? e->caller->inlined_to : e->caller))
3119 error ("inlined_to pointer is wrong");
3120 error_found = true;
3122 if (callers->next_caller)
3124 error ("multiple inline callers");
3125 error_found = true;
3128 else
3129 if (inlined_to)
3131 error ("inlined_to pointer set for noninline callers");
3132 error_found = true;
3135 for (e = callees; e; e = e->next_callee)
3137 if (e->verify_count ())
3138 error_found = true;
3139 if (gimple_has_body_p (e->caller->decl)
3140 && !e->caller->inlined_to
3141 && !e->speculative
3142 /* Optimized out calls are redirected to __builtin_unreachable. */
3143 && (e->count.nonzero_p ()
3144 || ! e->callee->decl
3145 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
3146 && count
3147 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3148 && (!e->count.ipa_p ()
3149 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3151 error ("caller edge count does not match BB count");
3152 fprintf (stderr, "edge count: ");
3153 e->count.dump (stderr);
3154 fprintf (stderr, "\n bb count: ");
3155 gimple_bb (e->call_stmt)->count.dump (stderr);
3156 fprintf (stderr, "\n");
3157 error_found = true;
3160 for (e = indirect_calls; e; e = e->next_callee)
3162 if (e->verify_count ())
3163 error_found = true;
3164 if (gimple_has_body_p (e->caller->decl)
3165 && !e->caller->inlined_to
3166 && !e->speculative
3167 && e->count.ipa_p ()
3168 && count
3169 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3170 && (!e->count.ipa_p ()
3171 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3173 error ("indirect call count does not match BB count");
3174 fprintf (stderr, "edge count: ");
3175 e->count.dump (stderr);
3176 fprintf (stderr, "\n bb count: ");
3177 gimple_bb (e->call_stmt)->count.dump (stderr);
3178 fprintf (stderr, "\n");
3179 error_found = true;
3182 if (!callers && inlined_to)
3184 error ("inlined_to pointer is set but no predecessors found");
3185 error_found = true;
3187 if (inlined_to == this)
3189 error ("inlined_to pointer refers to itself");
3190 error_found = true;
3193 if (clone_of)
3195 cgraph_node *first_clone = clone_of->clones;
3196 if (first_clone != this)
3198 if (prev_sibling_clone->clone_of != clone_of)
3200 error ("cgraph_node has wrong clone_of");
3201 error_found = true;
3205 if (clones)
3207 cgraph_node *n;
3208 for (n = clones; n; n = n->next_sibling_clone)
3209 if (n->clone_of != this)
3210 break;
3211 if (n)
3213 error ("cgraph_node has wrong clone list");
3214 error_found = true;
3217 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3219 error ("cgraph_node is in clone list but it is not clone");
3220 error_found = true;
3222 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3224 error ("cgraph_node has wrong prev_clone pointer");
3225 error_found = true;
3227 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3229 error ("double linked list of clones corrupted");
3230 error_found = true;
3233 if (analyzed && alias)
3235 bool ref_found = false;
3236 int i;
3237 ipa_ref *ref = NULL;
3239 if (callees)
3241 error ("Alias has call edges");
3242 error_found = true;
3244 for (i = 0; iterate_reference (i, ref); i++)
3245 if (ref->use != IPA_REF_ALIAS)
3247 error ("Alias has non-alias reference");
3248 error_found = true;
3250 else if (ref_found)
3252 error ("Alias has more than one alias reference");
3253 error_found = true;
3255 else
3256 ref_found = true;
3257 if (!ref_found)
3259 error ("Analyzed alias has no reference");
3260 error_found = true;
3264 if (analyzed && thunk.thunk_p)
3266 if (!callees)
3268 error ("No edge out of thunk node");
3269 error_found = true;
3271 else if (callees->next_callee)
3273 error ("More than one edge out of thunk node");
3274 error_found = true;
3276 if (gimple_has_body_p (decl) && !inlined_to)
3278 error ("Thunk is not supposed to have body");
3279 error_found = true;
3282 else if (analyzed && gimple_has_body_p (decl)
3283 && !TREE_ASM_WRITTEN (decl)
3284 && (!DECL_EXTERNAL (decl) || inlined_to)
3285 && !flag_wpa)
3287 if (this_cfun->cfg)
3289 hash_set<gimple *> stmts;
3290 int i;
3291 ipa_ref *ref = NULL;
3293 /* Reach the trees by walking over the CFG, and note the
3294 enclosing basic-blocks in the call edges. */
3295 FOR_EACH_BB_FN (this_block, this_cfun)
3297 for (gsi = gsi_start_phis (this_block);
3298 !gsi_end_p (gsi); gsi_next (&gsi))
3299 stmts.add (gsi_stmt (gsi));
3300 for (gsi = gsi_start_bb (this_block);
3301 !gsi_end_p (gsi);
3302 gsi_next (&gsi))
3304 gimple *stmt = gsi_stmt (gsi);
3305 stmts.add (stmt);
3306 if (is_gimple_call (stmt))
3308 cgraph_edge *e = get_edge (stmt);
3309 tree decl = gimple_call_fndecl (stmt);
3310 if (e)
3312 if (e->aux)
3314 error ("shared call_stmt:");
3315 cgraph_debug_gimple_stmt (this_cfun, stmt);
3316 error_found = true;
3318 if (!e->indirect_unknown_callee)
3320 if (e->verify_corresponds_to_fndecl (decl))
3322 error ("edge points to wrong declaration:");
3323 debug_tree (e->callee->decl);
3324 fprintf (stderr," Instead of:");
3325 debug_tree (decl);
3326 error_found = true;
3329 else if (decl)
3331 error ("an indirect edge with unknown callee "
3332 "corresponding to a call_stmt with "
3333 "a known declaration:");
3334 error_found = true;
3335 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3337 e->aux = (void *)1;
3339 else if (decl)
3341 error ("missing callgraph edge for call stmt:");
3342 cgraph_debug_gimple_stmt (this_cfun, stmt);
3343 error_found = true;
3348 for (i = 0; iterate_reference (i, ref); i++)
3349 if (ref->stmt && !stmts.contains (ref->stmt))
3351 error ("reference to dead statement");
3352 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3353 error_found = true;
3356 else
3357 /* No CFG available?! */
3358 gcc_unreachable ();
3360 for (e = callees; e; e = e->next_callee)
3362 if (!e->aux)
3364 error ("edge %s->%s has no corresponding call_stmt",
3365 identifier_to_locale (e->caller->name ()),
3366 identifier_to_locale (e->callee->name ()));
3367 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3368 error_found = true;
3370 e->aux = 0;
3372 for (e = indirect_calls; e; e = e->next_callee)
3374 if (!e->aux && !e->speculative)
3376 error ("an indirect edge from %s has no corresponding call_stmt",
3377 identifier_to_locale (e->caller->name ()));
3378 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3379 error_found = true;
3381 e->aux = 0;
3385 if (nested != NULL)
3387 for (cgraph_node *n = nested; n != NULL; n = n->next_nested)
3389 if (n->origin == NULL)
3391 error ("missing origin for a node in a nested list");
3392 error_found = true;
3394 else if (n->origin != this)
3396 error ("origin points to a different parent");
3397 error_found = true;
3398 break;
3402 if (next_nested != NULL && origin == NULL)
3404 error ("missing origin for a node in a nested list");
3405 error_found = true;
3408 if (error_found)
3410 dump (stderr);
3411 internal_error ("verify_cgraph_node failed");
3413 timevar_pop (TV_CGRAPH_VERIFY);
3416 /* Verify whole cgraph structure. */
3417 DEBUG_FUNCTION void
3418 cgraph_node::verify_cgraph_nodes (void)
3420 cgraph_node *node;
3422 if (seen_error ())
3423 return;
3425 FOR_EACH_FUNCTION (node)
3426 node->verify ();
3429 #if __GNUC__ >= 10
3430 # pragma GCC diagnostic pop
3431 #endif
3433 /* Walk the alias chain to return the function cgraph_node is alias of.
3434 Walk through thunks, too.
3435 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3436 When REF is non-NULL, assume that reference happens in symbol REF
3437 when determining the availability. */
3439 cgraph_node *
3440 cgraph_node::function_symbol (enum availability *availability,
3441 struct symtab_node *ref)
3443 cgraph_node *node = ultimate_alias_target (availability, ref);
3445 while (node->thunk.thunk_p)
3447 ref = node;
3448 node = node->callees->callee;
3449 if (availability)
3451 enum availability a;
3452 a = node->get_availability (ref);
3453 if (a < *availability)
3454 *availability = a;
3456 node = node->ultimate_alias_target (availability, ref);
3458 return node;
3461 /* Walk the alias chain to return the function cgraph_node is alias of.
3462 Walk through non virtual thunks, too. Thus we return either a function
3463 or a virtual thunk node.
3464 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3465 When REF is non-NULL, assume that reference happens in symbol REF
3466 when determining the availability. */
3468 cgraph_node *
3469 cgraph_node::function_or_virtual_thunk_symbol
3470 (enum availability *availability,
3471 struct symtab_node *ref)
3473 cgraph_node *node = ultimate_alias_target (availability, ref);
3475 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3477 ref = node;
3478 node = node->callees->callee;
3479 if (availability)
3481 enum availability a;
3482 a = node->get_availability (ref);
3483 if (a < *availability)
3484 *availability = a;
3486 node = node->ultimate_alias_target (availability, ref);
3488 return node;
3491 /* When doing LTO, read cgraph_node's body from disk if it is not already
3492 present. */
3494 bool
3495 cgraph_node::get_untransformed_body (void)
3497 lto_file_decl_data *file_data;
3498 const char *data, *name;
3499 size_t len;
3500 tree decl = this->decl;
3502 /* Check if body is already there. Either we have gimple body or
3503 the function is thunk and in that case we set DECL_ARGUMENTS. */
3504 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3505 return false;
3507 gcc_assert (in_lto_p && !DECL_RESULT (decl));
3509 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3511 file_data = lto_file_data;
3512 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3514 /* We may have renamed the declaration, e.g., a static function. */
3515 name = lto_get_decl_name_mapping (file_data, name);
3516 struct lto_in_decl_state *decl_state
3517 = lto_get_function_in_decl_state (file_data, decl);
3519 cgraph_node *origin = this;
3520 while (origin->clone_of)
3521 origin = origin->clone_of;
3523 int stream_order = origin->order - file_data->order_base;
3524 data = lto_get_section_data (file_data, LTO_section_function_body,
3525 name, stream_order, &len,
3526 decl_state->compressed);
3527 if (!data)
3528 fatal_error (input_location, "%s: section %s.%d is missing",
3529 file_data->file_name, name, stream_order);
3531 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3533 if (!quiet_flag)
3534 fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
3535 lto_input_function_body (file_data, this, data);
3536 lto_stats.num_function_bodies++;
3537 lto_free_section_data (file_data, LTO_section_function_body, name,
3538 data, len, decl_state->compressed);
3539 lto_free_function_in_decl_state_for_node (this);
3540 /* Keep lto file data so ipa-inline-analysis knows about cross module
3541 inlining. */
3543 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3545 return true;
3548 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3549 if it is not already present. When some IPA transformations are scheduled,
3550 apply them. */
3552 bool
3553 cgraph_node::get_body (void)
3555 bool updated;
3557 updated = get_untransformed_body ();
3559 /* Getting transformed body makes no sense for inline clones;
3560 we should never use this on real clones because they are materialized
3561 early.
3562 TODO: Materializing clones here will likely lead to smaller LTRANS
3563 footprint. */
3564 gcc_assert (!inlined_to && !clone_of);
3565 if (ipa_transforms_to_apply.exists ())
3567 opt_pass *saved_current_pass = current_pass;
3568 FILE *saved_dump_file = dump_file;
3569 const char *saved_dump_file_name = dump_file_name;
3570 dump_flags_t saved_dump_flags = dump_flags;
3571 dump_file_name = NULL;
3572 set_dump_file (NULL);
3574 push_cfun (DECL_STRUCT_FUNCTION (decl));
3575 execute_all_ipa_transforms (true);
3576 cgraph_edge::rebuild_edges ();
3577 free_dominance_info (CDI_DOMINATORS);
3578 free_dominance_info (CDI_POST_DOMINATORS);
3579 pop_cfun ();
3580 updated = true;
3582 current_pass = saved_current_pass;
3583 set_dump_file (saved_dump_file);
3584 dump_file_name = saved_dump_file_name;
3585 dump_flags = saved_dump_flags;
3587 return updated;
3590 /* Return the DECL_STRUCT_FUNCTION of the function. */
3592 struct function *
3593 cgraph_node::get_fun () const
3595 const cgraph_node *node = this;
3596 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3598 while (!fun && node->clone_of)
3600 node = node->clone_of;
3601 fun = DECL_STRUCT_FUNCTION (node->decl);
3604 return fun;
3607 /* Reset all state within cgraph.c so that we can rerun the compiler
3608 within the same process. For use by toplev::finalize. */
3610 void
3611 cgraph_c_finalize (void)
3613 symtab = NULL;
3615 x_cgraph_nodes_queue = NULL;
3617 cgraph_fnver_htab = NULL;
3618 version_info_node = NULL;
3621 /* A worker for call_for_symbol_and_aliases. */
3623 bool
3624 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
3625 void *),
3626 void *data,
3627 bool include_overwritable)
3629 ipa_ref *ref;
3630 FOR_EACH_ALIAS (this, ref)
3632 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3633 if (include_overwritable
3634 || alias->get_availability () > AVAIL_INTERPOSABLE)
3635 if (alias->call_for_symbol_and_aliases (callback, data,
3636 include_overwritable))
3637 return true;
3639 return false;
3642 /* Return true if NODE has thunk. */
3644 bool
3645 cgraph_node::has_thunk_p (cgraph_node *node, void *)
3647 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3648 if (e->caller->thunk.thunk_p)
3649 return true;
3650 return false;
3653 /* Expected frequency of executions within the function. */
3655 sreal
3656 cgraph_edge::sreal_frequency ()
3658 return count.to_sreal_scale (caller->inlined_to
3659 ? caller->inlined_to->count
3660 : caller->count);
3664 /* During LTO stream in this can be used to check whether call can possibly
3665 be internal to the current translation unit. */
3667 bool
3668 cgraph_edge::possibly_call_in_translation_unit_p (void)
3670 gcc_checking_assert (in_lto_p && caller->prevailing_p ());
3672 /* While incremental linking we may end up getting function body later. */
3673 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
3674 return true;
3676 /* We may be smarter here and avoid streaming in indirect calls we can't
3677 track, but that would require arranging streaming the indirect call
3678 summary first. */
3679 if (!callee)
3680 return true;
3682 /* If callee is local to the original translation unit, it will be
3683 defined. */
3684 if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
3685 return true;
3687 /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
3688 yet) and see if it is a definition. In fact we may also resolve aliases,
3689 but that is probably not too important. */
3690 symtab_node *node = callee;
3691 for (int n = 10; node->previous_sharing_asm_name && n ; n--)
3692 node = node->previous_sharing_asm_name;
3693 if (node->previous_sharing_asm_name)
3694 node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
3695 gcc_assert (TREE_PUBLIC (node->decl));
3696 return node->get_availability () >= AVAIL_INTERPOSABLE;
3699 /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
3700 This needs to be a global so that it can be a GC root, and thus
3701 prevent the stashed copy from being garbage-collected if the GC runs
3702 during a symbol_table_test. */
3704 symbol_table *saved_symtab;
3706 #if CHECKING_P
3708 namespace selftest {
3710 /* class selftest::symbol_table_test. */
3712 /* Constructor. Store the old value of symtab, and create a new one. */
3714 symbol_table_test::symbol_table_test ()
3716 gcc_assert (saved_symtab == NULL);
3717 saved_symtab = symtab;
3718 symtab = new (ggc_alloc <symbol_table> ()) symbol_table ();
3721 /* Destructor. Restore the old value of symtab. */
3723 symbol_table_test::~symbol_table_test ()
3725 gcc_assert (saved_symtab != NULL);
3726 symtab = saved_symtab;
3727 saved_symtab = NULL;
3730 /* Verify that symbol_table_test works. */
3732 static void
3733 test_symbol_table_test ()
3735 /* Simulate running two selftests involving symbol tables. */
3736 for (int i = 0; i < 2; i++)
3738 symbol_table_test stt;
3739 tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
3740 get_identifier ("test_decl"),
3741 build_function_type_list (void_type_node,
3742 NULL_TREE));
3743 cgraph_node *node = cgraph_node::get_create (test_decl);
3744 gcc_assert (node);
3746 /* Verify that the node has order 0 on both iterations,
3747 and thus that nodes have predictable dump names in selftests. */
3748 ASSERT_EQ (node->order, 0);
3749 ASSERT_STREQ (node->dump_name (), "test_decl/0");
3753 /* Run all of the selftests within this file. */
3755 void
3756 cgraph_c_tests ()
3758 test_symbol_table_test ();
3761 } // namespace selftest
3763 #endif /* CHECKING_P */
3765 #include "gt-cgraph.h"