tree-optimization/114485 - neg induction with partial vectors
[official-gcc.git] / gcc / cgraph.cc
blob473d8410bc976b647914996a56534b2febe69eae
1 /* Callgraph handling code.
2 Copyright (C) 2003-2024 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 "sreal.h"
55 #include "ipa-cp.h"
56 #include "ipa-prop.h"
57 #include "ipa-fnsummary.h"
58 #include "cfgloop.h"
59 #include "gimple-pretty-print.h"
60 #include "tree-dfa.h"
61 #include "profile.h"
62 #include "context.h"
63 #include "gimplify.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "selftest.h"
67 #include "tree-into-ssa.h"
68 #include "ipa-inline.h"
69 #include "tree-nested.h"
70 #include "symtab-thunks.h"
71 #include "symtab-clones.h"
73 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
74 #include "tree-pass.h"
76 /* Queue of cgraph nodes scheduled to be lowered. */
77 symtab_node *x_cgraph_nodes_queue;
78 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
80 /* Symbol table global context. */
81 symbol_table *symtab;
83 /* List of hooks triggered on cgraph_edge events. */
84 struct cgraph_edge_hook_list {
85 cgraph_edge_hook hook;
86 void *data;
87 struct cgraph_edge_hook_list *next;
90 /* List of hooks triggered on cgraph_node events. */
91 struct cgraph_node_hook_list {
92 cgraph_node_hook hook;
93 void *data;
94 struct cgraph_node_hook_list *next;
97 /* List of hooks triggered on events involving two cgraph_edges. */
98 struct cgraph_2edge_hook_list {
99 cgraph_2edge_hook hook;
100 void *data;
101 struct cgraph_2edge_hook_list *next;
104 /* List of hooks triggered on events involving two cgraph_nodes. */
105 struct cgraph_2node_hook_list {
106 cgraph_2node_hook hook;
107 void *data;
108 struct cgraph_2node_hook_list *next;
111 /* Hash descriptor for cgraph_function_version_info. */
113 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
115 static hashval_t hash (cgraph_function_version_info *);
116 static bool equal (cgraph_function_version_info *,
117 cgraph_function_version_info *);
120 /* Map a cgraph_node to cgraph_function_version_info using this htab.
121 The cgraph_function_version_info has a THIS_NODE field that is the
122 corresponding cgraph_node.. */
124 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
126 /* Hash function for cgraph_fnver_htab. */
127 hashval_t
128 function_version_hasher::hash (cgraph_function_version_info *ptr)
130 int uid = ptr->this_node->get_uid ();
131 return (hashval_t)(uid);
134 /* eq function for cgraph_fnver_htab. */
135 bool
136 function_version_hasher::equal (cgraph_function_version_info *n1,
137 cgraph_function_version_info *n2)
139 return n1->this_node->get_uid () == n2->this_node->get_uid ();
142 /* Mark as GC root all allocated nodes. */
143 static GTY(()) struct cgraph_function_version_info *
144 version_info_node = NULL;
146 /* Return true if NODE's address can be compared. */
148 bool
149 symtab_node::address_can_be_compared_p ()
151 /* Address of virtual tables and functions is never compared. */
152 if (DECL_VIRTUAL_P (decl))
153 return false;
154 /* Address of C++ cdtors is never compared. */
155 if (is_a <cgraph_node *> (this)
156 && (DECL_CXX_CONSTRUCTOR_P (decl)
157 || DECL_CXX_DESTRUCTOR_P (decl)))
158 return false;
159 /* Constant pool symbols addresses are never compared.
160 flag_merge_constants permits us to assume the same on readonly vars. */
161 if (is_a <varpool_node *> (this)
162 && (DECL_IN_CONSTANT_POOL (decl)
163 || ((flag_merge_constants >= 2 || DECL_MERGEABLE (decl))
164 && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
165 return false;
166 return true;
169 /* Get the cgraph_function_version_info node corresponding to node. */
170 cgraph_function_version_info *
171 cgraph_node::function_version (void)
173 cgraph_function_version_info key;
174 key.this_node = this;
176 if (cgraph_fnver_htab == NULL)
177 return NULL;
179 return cgraph_fnver_htab->find (&key);
182 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
183 corresponding to cgraph_node NODE. */
184 cgraph_function_version_info *
185 cgraph_node::insert_new_function_version (void)
187 version_info_node = NULL;
188 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
189 version_info_node->this_node = this;
191 if (cgraph_fnver_htab == NULL)
192 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
194 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
195 = version_info_node;
196 return version_info_node;
199 /* Remove the cgraph_function_version_info node given by DECL_V. */
200 static void
201 delete_function_version (cgraph_function_version_info *decl_v)
203 if (decl_v == NULL)
204 return;
206 if (version_info_node == decl_v)
207 version_info_node = NULL;
209 if (decl_v->prev != NULL)
210 decl_v->prev->next = decl_v->next;
212 if (decl_v->next != NULL)
213 decl_v->next->prev = decl_v->prev;
215 if (cgraph_fnver_htab != NULL)
216 cgraph_fnver_htab->remove_elt (decl_v);
219 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
220 DECL is a duplicate declaration. */
221 void
222 cgraph_node::delete_function_version_by_decl (tree decl)
224 cgraph_node *decl_node = cgraph_node::get (decl);
226 if (decl_node == NULL)
227 return;
229 delete_function_version (decl_node->function_version ());
231 decl_node->remove ();
234 /* Record that DECL1 and DECL2 are semantically identical function
235 versions. */
236 void
237 cgraph_node::record_function_versions (tree decl1, tree decl2)
239 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
240 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
241 cgraph_function_version_info *decl1_v = NULL;
242 cgraph_function_version_info *decl2_v = NULL;
243 cgraph_function_version_info *before;
244 cgraph_function_version_info *after;
246 gcc_assert (decl1_node != NULL && decl2_node != NULL);
247 decl1_v = decl1_node->function_version ();
248 decl2_v = decl2_node->function_version ();
250 if (decl1_v != NULL && decl2_v != NULL)
251 return;
253 if (decl1_v == NULL)
254 decl1_v = decl1_node->insert_new_function_version ();
256 if (decl2_v == NULL)
257 decl2_v = decl2_node->insert_new_function_version ();
259 /* Chain decl2_v and decl1_v. All semantically identical versions
260 will be chained together. */
262 before = decl1_v;
263 after = decl2_v;
265 while (before->next != NULL)
266 before = before->next;
268 while (after->prev != NULL)
269 after= after->prev;
271 before->next = after;
272 after->prev = before;
275 /* Initialize callgraph dump file. */
277 void
278 symbol_table::initialize (void)
280 if (!dump_file)
281 dump_file = dump_begin (TDI_cgraph, NULL);
283 if (!ipa_clones_dump_file)
284 ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
287 /* Allocate new callgraph node and insert it into basic data structures. */
289 cgraph_node *
290 symbol_table::create_empty (void)
292 cgraph_count++;
293 return new (ggc_alloc<cgraph_node> ()) cgraph_node (cgraph_max_uid++);
296 /* Register HOOK to be called with DATA on each removed edge. */
297 cgraph_edge_hook_list *
298 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
300 cgraph_edge_hook_list *entry;
301 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
303 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
304 entry->hook = hook;
305 entry->data = data;
306 entry->next = NULL;
307 while (*ptr)
308 ptr = &(*ptr)->next;
309 *ptr = entry;
310 return entry;
313 /* Remove ENTRY from the list of hooks called on removing edges. */
314 void
315 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
317 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
319 while (*ptr != entry)
320 ptr = &(*ptr)->next;
321 *ptr = entry->next;
322 free (entry);
325 /* Call all edge removal hooks. */
326 void
327 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
329 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
330 while (entry)
332 entry->hook (e, entry->data);
333 entry = entry->next;
337 /* Register HOOK to be called with DATA on each removed node. */
338 cgraph_node_hook_list *
339 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
341 cgraph_node_hook_list *entry;
342 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
344 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
345 entry->hook = hook;
346 entry->data = data;
347 entry->next = NULL;
348 while (*ptr)
349 ptr = &(*ptr)->next;
350 *ptr = entry;
351 return entry;
354 /* Remove ENTRY from the list of hooks called on removing nodes. */
355 void
356 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
358 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
360 while (*ptr != entry)
361 ptr = &(*ptr)->next;
362 *ptr = entry->next;
363 free (entry);
366 /* Call all node removal hooks. */
367 void
368 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
370 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
371 while (entry)
373 entry->hook (node, entry->data);
374 entry = entry->next;
378 /* Call all node removal hooks. */
379 void
380 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
382 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
383 while (entry)
385 entry->hook (node, entry->data);
386 entry = entry->next;
391 /* Register HOOK to be called with DATA on each inserted node. */
392 cgraph_node_hook_list *
393 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
395 cgraph_node_hook_list *entry;
396 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
398 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
399 entry->hook = hook;
400 entry->data = data;
401 entry->next = NULL;
402 while (*ptr)
403 ptr = &(*ptr)->next;
404 *ptr = entry;
405 return entry;
408 /* Remove ENTRY from the list of hooks called on inserted nodes. */
409 void
410 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
412 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
414 while (*ptr != entry)
415 ptr = &(*ptr)->next;
416 *ptr = entry->next;
417 free (entry);
420 /* Register HOOK to be called with DATA on each duplicated edge. */
421 cgraph_2edge_hook_list *
422 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
424 cgraph_2edge_hook_list *entry;
425 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
427 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
428 entry->hook = hook;
429 entry->data = data;
430 entry->next = NULL;
431 while (*ptr)
432 ptr = &(*ptr)->next;
433 *ptr = entry;
434 return entry;
437 /* Remove ENTRY from the list of hooks called on duplicating edges. */
438 void
439 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
441 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
443 while (*ptr != entry)
444 ptr = &(*ptr)->next;
445 *ptr = entry->next;
446 free (entry);
449 /* Call all edge duplication hooks. */
450 void
451 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
453 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
454 while (entry)
456 entry->hook (cs1, cs2, entry->data);
457 entry = entry->next;
461 /* Register HOOK to be called with DATA on each duplicated node. */
462 cgraph_2node_hook_list *
463 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
465 cgraph_2node_hook_list *entry;
466 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
468 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
469 entry->hook = hook;
470 entry->data = data;
471 entry->next = NULL;
472 while (*ptr)
473 ptr = &(*ptr)->next;
474 *ptr = entry;
475 return entry;
478 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
479 void
480 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
482 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
484 while (*ptr != entry)
485 ptr = &(*ptr)->next;
486 *ptr = entry->next;
487 free (entry);
490 /* Call all node duplication hooks. */
491 void
492 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
493 cgraph_node *node2)
495 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
496 while (entry)
498 entry->hook (node, node2, entry->data);
499 entry = entry->next;
503 /* Return cgraph node assigned to DECL. Create new one when needed. */
505 cgraph_node *
506 cgraph_node::create (tree decl)
508 cgraph_node *node = symtab->create_empty ();
509 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
511 node->decl = decl;
512 node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
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 ();
526 maybe_record_nested_function (node);
528 return node;
531 /* Try to find a call graph node for declaration DECL and if it does not exist
532 or if it corresponds to an inline clone, create a new one. */
534 cgraph_node *
535 cgraph_node::get_create (tree decl)
537 cgraph_node *first_clone = cgraph_node::get (decl);
539 if (first_clone && !first_clone->inlined_to)
540 return first_clone;
542 cgraph_node *node = cgraph_node::create (decl);
543 if (first_clone)
545 first_clone->clone_of = node;
546 node->clones = first_clone;
547 node->order = first_clone->order;
548 symtab->symtab_prevail_in_asm_name_hash (node);
549 node->decl->decl_with_vis.symtab_node = node;
550 if (dump_file && symtab->state != PARSING)
551 fprintf (dump_file, "Introduced new external node "
552 "(%s) and turned into root of the clone tree.\n",
553 node->dump_name ());
555 else if (dump_file && symtab->state != PARSING)
556 fprintf (dump_file, "Introduced new external node "
557 "(%s).\n", node->dump_name ());
558 return node;
561 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
562 the function body is associated with
563 (not necessarily cgraph_node (DECL)). */
565 cgraph_node *
566 cgraph_node::create_alias (tree alias, tree target)
568 cgraph_node *alias_node;
570 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
571 || TREE_CODE (target) == IDENTIFIER_NODE);
572 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
573 alias_node = cgraph_node::get_create (alias);
574 gcc_assert (!alias_node->definition);
575 alias_node->alias_target = target;
576 alias_node->definition = true;
577 alias_node->alias = true;
578 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
579 alias_node->transparent_alias = alias_node->weakref = true;
580 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
581 alias_node->ifunc_resolver = true;
582 return alias_node;
585 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
586 and NULL otherwise.
587 Same body aliases are output whenever the body of DECL is output,
588 and cgraph_node::get (ALIAS) transparently returns
589 cgraph_node::get (DECL). */
591 cgraph_node *
592 cgraph_node::create_same_body_alias (tree alias, tree decl)
594 cgraph_node *n;
596 /* If aliases aren't supported by the assembler, fail. */
597 if (!TARGET_SUPPORTS_ALIASES)
598 return NULL;
600 /* Langhooks can create same body aliases of symbols not defined.
601 Those are useless. Drop them on the floor. */
602 if (symtab->global_info_ready)
603 return NULL;
605 n = cgraph_node::create_alias (alias, decl);
606 n->cpp_implicit_alias = true;
607 if (symtab->cpp_implicit_aliases_done)
608 n->resolve_alias (cgraph_node::get (decl));
609 return n;
612 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
613 aliases DECL with an adjustments made into the first parameter.
614 See comments in struct cgraph_thunk_info for detail on the parameters. */
616 cgraph_node *
617 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
618 HOST_WIDE_INT fixed_offset,
619 HOST_WIDE_INT virtual_value,
620 HOST_WIDE_INT indirect_offset,
621 tree virtual_offset,
622 tree real_alias)
624 cgraph_node *node;
626 node = cgraph_node::get (alias);
627 if (node)
628 node->reset ();
629 else
630 node = cgraph_node::create (alias);
632 /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE. */
633 gcc_checking_assert (virtual_offset
634 ? virtual_value == wi::to_wide (virtual_offset)
635 : virtual_value == 0);
637 node->thunk = true;
638 node->definition = true;
640 thunk_info *i;
641 thunk_info local_info;
642 if (symtab->state < CONSTRUCTION)
643 i = &local_info;
644 else
645 i = thunk_info::get_create (node);
646 i->fixed_offset = fixed_offset;
647 i->virtual_value = virtual_value;
648 i->indirect_offset = indirect_offset;
649 i->alias = real_alias;
650 i->this_adjusting = this_adjusting;
651 i->virtual_offset_p = virtual_offset != NULL;
652 if (symtab->state < CONSTRUCTION)
653 i->register_early (node);
655 return node;
658 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
659 Return NULL if there's no such node. */
661 cgraph_node *
662 cgraph_node::get_for_asmname (tree asmname)
664 /* We do not want to look at inline clones. */
665 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
666 node;
667 node = node->next_sharing_asm_name)
669 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
670 if (cn && !cn->inlined_to)
671 return cn;
673 return NULL;
676 /* Returns a hash value for X (which really is a cgraph_edge). */
678 hashval_t
679 cgraph_edge_hasher::hash (cgraph_edge *e)
681 /* This is a really poor hash function, but it is what htab_hash_pointer
682 uses. */
683 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
686 /* Returns a hash value for X (which really is a cgraph_edge). */
688 hashval_t
689 cgraph_edge_hasher::hash (gimple *call_stmt)
691 /* This is a really poor hash function, but it is what htab_hash_pointer
692 uses. */
693 return (hashval_t) ((intptr_t)call_stmt >> 3);
696 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
698 inline bool
699 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
701 return x->call_stmt == y;
704 /* Add call graph edge E to call site hash of its caller. */
706 static inline void
707 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
709 gimple *call = e->call_stmt;
710 *e->caller->call_site_hash->find_slot_with_hash
711 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
714 /* Add call graph edge E to call site hash of its caller. */
716 static inline void
717 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
719 /* There are two speculative edges for every statement (one direct,
720 one indirect); always hash the direct one. */
721 if (e->speculative && e->indirect_unknown_callee)
722 return;
723 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
724 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
725 if (*slot)
727 gcc_assert (((cgraph_edge *)*slot)->speculative);
728 if (e->callee && (!e->prev_callee
729 || !e->prev_callee->speculative
730 || e->prev_callee->call_stmt != e->call_stmt))
731 *slot = e;
732 return;
734 gcc_assert (!*slot || e->speculative);
735 *slot = e;
738 /* Return the callgraph edge representing the GIMPLE_CALL statement
739 CALL_STMT. */
741 cgraph_edge *
742 cgraph_node::get_edge (gimple *call_stmt)
744 cgraph_edge *e, *e2;
745 int n = 0;
747 if (call_site_hash)
748 return call_site_hash->find_with_hash
749 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
751 /* This loop may turn out to be performance problem. In such case adding
752 hashtables into call nodes with very many edges is probably best
753 solution. It is not good idea to add pointer into CALL_EXPR itself
754 because we want to make possible having multiple cgraph nodes representing
755 different clones of the same body before the body is actually cloned. */
756 for (e = callees; e; e = e->next_callee)
758 if (e->call_stmt == call_stmt)
759 break;
760 n++;
763 if (!e)
764 for (e = indirect_calls; e; e = e->next_callee)
766 if (e->call_stmt == call_stmt)
767 break;
768 n++;
771 if (n > 100)
773 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
774 for (e2 = callees; e2; e2 = e2->next_callee)
775 cgraph_add_edge_to_call_site_hash (e2);
776 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
777 cgraph_add_edge_to_call_site_hash (e2);
780 return e;
784 /* Change field call_stmt of edge E to NEW_STMT. If UPDATE_SPECULATIVE and E
785 is any component of speculative edge, then update all components.
786 Speculations can be resolved in the process and EDGE can be removed and
787 deallocated. Return the edge that now represents the call. */
789 cgraph_edge *
790 cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
791 bool update_speculative)
793 tree decl;
795 cgraph_node *new_direct_callee = NULL;
796 if ((e->indirect_unknown_callee || e->speculative)
797 && (decl = gimple_call_fndecl (new_stmt)))
799 /* Constant propagation and especially inlining can turn an indirect call
800 into a direct one. */
801 new_direct_callee = cgraph_node::get (decl);
802 gcc_checking_assert (new_direct_callee);
805 /* Speculative edges has three component, update all of them
806 when asked to. */
807 if (update_speculative && e->speculative
808 /* If we are about to resolve the speculation by calling make_direct
809 below, do not bother going over all the speculative edges now. */
810 && !new_direct_callee)
812 cgraph_edge *direct, *indirect, *next;
813 ipa_ref *ref;
814 bool e_indirect = e->indirect_unknown_callee;
815 int n = 0;
817 direct = e->first_speculative_call_target ();
818 indirect = e->speculative_call_indirect_edge ();
820 gcall *old_stmt = direct->call_stmt;
821 for (cgraph_edge *d = direct; d; d = next)
823 next = d->next_speculative_call_target ();
824 cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
825 gcc_assert (d2 == d);
826 n++;
828 gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
829 for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
830 if (ref->speculative && ref->stmt == old_stmt)
832 ref->stmt = new_stmt;
833 n--;
836 indirect = set_call_stmt (indirect, new_stmt, false);
837 return e_indirect ? indirect : direct;
840 if (new_direct_callee)
841 e = make_direct (e, new_direct_callee);
843 /* Only direct speculative edges go to call_site_hash. */
844 if (e->caller->call_site_hash
845 && (!e->speculative || !e->indirect_unknown_callee)
846 /* It is possible that edge was previously speculative. In this case
847 we have different value in call stmt hash which needs preserving. */
848 && e->caller->get_edge (e->call_stmt) == e)
849 e->caller->call_site_hash->remove_elt_with_hash
850 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
852 e->call_stmt = new_stmt;
854 function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
855 e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
856 /* Update call stite hash. For speculative calls we only record the first
857 direct edge. */
858 if (e->caller->call_site_hash
859 && (!e->speculative
860 || (e->callee
861 && (!e->prev_callee || !e->prev_callee->speculative
862 || e->prev_callee->call_stmt != e->call_stmt))
863 || (e->speculative && !e->callee)))
864 cgraph_add_edge_to_call_site_hash (e);
865 return e;
868 /* Allocate a cgraph_edge structure and fill it with data according to the
869 parameters of which only CALLEE can be NULL (when creating an indirect call
870 edge). CLONING_P should be set if properties that are copied from an
871 original edge should not be calculated. */
873 cgraph_edge *
874 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
875 gcall *call_stmt, profile_count count,
876 bool indir_unknown_callee, bool cloning_p)
878 cgraph_edge *edge;
880 /* LTO does not actually have access to the call_stmt since these
881 have not been loaded yet. */
882 if (call_stmt)
884 /* This is a rather expensive check possibly triggering
885 construction of call stmt hashtable. */
886 cgraph_edge *e;
887 gcc_checking_assert (!(e = caller->get_edge (call_stmt))
888 || e->speculative);
890 gcc_assert (is_gimple_call (call_stmt));
893 edge = ggc_alloc<cgraph_edge> ();
894 edge->m_summary_id = -1;
895 edges_count++;
897 gcc_assert (++edges_max_uid != 0);
898 edge->m_uid = edges_max_uid;
899 edge->aux = NULL;
900 edge->caller = caller;
901 edge->callee = callee;
902 edge->prev_caller = NULL;
903 edge->next_caller = NULL;
904 edge->prev_callee = NULL;
905 edge->next_callee = NULL;
906 edge->lto_stmt_uid = 0;
907 edge->speculative_id = 0;
909 edge->count = count;
910 edge->call_stmt = call_stmt;
911 edge->indirect_info = NULL;
912 edge->indirect_inlining_edge = 0;
913 edge->speculative = false;
914 edge->indirect_unknown_callee = indir_unknown_callee;
915 if (call_stmt && caller->call_site_hash)
916 cgraph_add_edge_to_call_site_hash (edge);
918 if (cloning_p)
919 return edge;
921 edge->can_throw_external
922 = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
923 call_stmt) : false;
924 edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
925 edge->call_stmt_cannot_inline_p = false;
927 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
928 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
929 edge->in_polymorphic_cdtor
930 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
931 caller->decl);
932 else
933 edge->in_polymorphic_cdtor = caller->thunk;
934 if (callee)
935 caller->calls_declare_variant_alt |= callee->declare_variant_alt;
937 if (callee && symtab->state != LTO_STREAMING
938 && edge->callee->comdat_local_p ())
939 edge->caller->calls_comdat_local = true;
941 return edge;
944 /* Create edge from a given function to CALLEE in the cgraph. CLONING_P should
945 be set if properties that are copied from an original edge should not be
946 calculated. */
948 cgraph_edge *
949 cgraph_node::create_edge (cgraph_node *callee,
950 gcall *call_stmt, profile_count count, bool cloning_p)
952 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
953 false, cloning_p);
955 if (!cloning_p)
956 initialize_inline_failed (edge);
958 edge->next_caller = callee->callers;
959 if (callee->callers)
960 callee->callers->prev_caller = edge;
961 edge->next_callee = callees;
962 if (callees)
963 callees->prev_callee = edge;
964 callees = edge;
965 callee->callers = edge;
967 return edge;
970 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
972 cgraph_indirect_call_info *
973 cgraph_allocate_init_indirect_info (void)
975 cgraph_indirect_call_info *ii;
977 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
978 ii->param_index = -1;
979 return ii;
982 /* Create an indirect edge with a yet-undetermined callee where the call
983 statement destination is a formal parameter of the caller with index
984 PARAM_INDEX. CLONING_P should be set if properties that are copied from an
985 original edge should not be calculated and indirect_info structure should
986 not be calculated. */
988 cgraph_edge *
989 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
990 profile_count count,
991 bool cloning_p)
993 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
994 cloning_p);
995 tree target;
997 if (!cloning_p)
998 initialize_inline_failed (edge);
1000 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1001 edge->indirect_info->ecf_flags = ecf_flags;
1002 edge->indirect_info->vptr_changed = true;
1004 /* Record polymorphic call info. */
1005 if (!cloning_p
1006 && call_stmt
1007 && (target = gimple_call_fn (call_stmt))
1008 && virtual_method_call_p (target))
1010 ipa_polymorphic_call_context context (decl, target, call_stmt);
1012 /* Only record types can have virtual calls. */
1013 edge->indirect_info->polymorphic = true;
1014 edge->indirect_info->param_index = -1;
1015 edge->indirect_info->otr_token
1016 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
1017 edge->indirect_info->otr_type = obj_type_ref_class (target);
1018 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
1019 edge->indirect_info->context = context;
1022 edge->next_callee = indirect_calls;
1023 if (indirect_calls)
1024 indirect_calls->prev_callee = edge;
1025 indirect_calls = edge;
1027 return edge;
1030 /* Remove the edge from the list of the callees of the caller. */
1032 void
1033 cgraph_edge::remove_caller (void)
1035 if (prev_callee)
1036 prev_callee->next_callee = next_callee;
1037 if (next_callee)
1038 next_callee->prev_callee = prev_callee;
1039 if (!prev_callee)
1041 if (indirect_unknown_callee)
1042 caller->indirect_calls = next_callee;
1043 else
1044 caller->callees = next_callee;
1046 if (caller->call_site_hash
1047 && this == caller->get_edge (call_stmt))
1048 caller->call_site_hash->remove_elt_with_hash
1049 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
1052 /* Put the edge onto the free list. */
1054 void
1055 symbol_table::free_edge (cgraph_edge *e)
1057 edges_count--;
1058 if (e->m_summary_id != -1)
1059 edge_released_summary_ids.safe_push (e->m_summary_id);
1061 if (e->indirect_info)
1062 ggc_free (e->indirect_info);
1063 ggc_free (e);
1066 /* Remove the edge in the cgraph. */
1068 void
1069 cgraph_edge::remove (cgraph_edge *edge)
1071 /* Call all edge removal hooks. */
1072 symtab->call_edge_removal_hooks (edge);
1074 if (!edge->indirect_unknown_callee)
1075 /* Remove from callers list of the callee. */
1076 edge->remove_callee ();
1078 /* Remove from callees list of the callers. */
1079 edge->remove_caller ();
1081 /* Put the edge onto the free list. */
1082 symtab->free_edge (edge);
1085 /* Turn edge into speculative call calling N2. Update
1086 the profile so the direct call is taken COUNT times
1087 with FREQUENCY.
1089 At clone materialization time, the indirect call E will
1090 be expanded as:
1092 if (call_dest == N2)
1093 n2 ();
1094 else
1095 call call_dest
1097 At this time the function just creates the direct call,
1098 the reference representing the if conditional and attaches
1099 them all to the original indirect call statement.
1101 speculative_id is used to link direct calls with their corresponding
1102 IPA_REF_ADDR references when representing speculative calls.
1104 Return direct edge created. */
1106 cgraph_edge *
1107 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1108 unsigned int speculative_id)
1110 cgraph_node *n = caller;
1111 ipa_ref *ref = NULL;
1112 cgraph_edge *e2;
1114 if (dump_file)
1115 fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1116 n->dump_name (), n2->dump_name ());
1117 speculative = true;
1118 e2 = n->create_edge (n2, call_stmt, direct_count);
1119 initialize_inline_failed (e2);
1120 e2->speculative = true;
1121 if (TREE_NOTHROW (n2->decl))
1122 e2->can_throw_external = false;
1123 else
1124 e2->can_throw_external = can_throw_external;
1125 e2->lto_stmt_uid = lto_stmt_uid;
1126 e2->speculative_id = speculative_id;
1127 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1128 indirect_info->num_speculative_call_targets++;
1129 count -= e2->count;
1130 symtab->call_edge_duplication_hooks (this, e2);
1131 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1132 ref->lto_stmt_uid = lto_stmt_uid;
1133 ref->speculative_id = speculative_id;
1134 ref->speculative = speculative;
1135 n2->mark_address_taken ();
1136 return e2;
1139 /* Speculative call consists of an indirect edge and one or more
1140 direct edge+ref pairs.
1142 Given an edge which is part of speculative call, return the first
1143 direct call edge in the speculative call sequence. */
1145 cgraph_edge *
1146 cgraph_edge::first_speculative_call_target ()
1148 cgraph_edge *e = this;
1150 gcc_checking_assert (e->speculative);
1151 if (e->callee)
1153 while (e->prev_callee && e->prev_callee->speculative
1154 && e->prev_callee->call_stmt == e->call_stmt
1155 && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1156 e = e->prev_callee;
1157 return e;
1159 /* Call stmt site hash always points to the first target of the
1160 speculative call sequence. */
1161 if (e->call_stmt)
1162 return e->caller->get_edge (e->call_stmt);
1163 for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1164 if (e2->speculative
1165 && e->call_stmt == e2->call_stmt
1166 && e->lto_stmt_uid == e2->lto_stmt_uid)
1167 return e2;
1170 /* We always maintain first direct edge in the call site hash, if one
1171 exists. E is going to be removed. See if it is first one and update
1172 hash accordingly. INDIRECT is the indirect edge of speculative call.
1173 We assume that INDIRECT->num_speculative_call_targets_p () is already
1174 updated for removal of E. */
1175 static void
1176 update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1177 cgraph_edge *indirect)
1179 if (e->caller->call_site_hash)
1181 if (e->caller->get_edge (e->call_stmt) != e)
1183 else if (!indirect->num_speculative_call_targets_p ())
1184 cgraph_update_edge_in_call_site_hash (indirect);
1185 else
1187 gcc_checking_assert (e->next_callee && e->next_callee->speculative
1188 && e->next_callee->call_stmt == e->call_stmt);
1189 cgraph_update_edge_in_call_site_hash (e->next_callee);
1194 /* Speculative call EDGE turned out to be direct call to CALLEE_DECL. Remove
1195 the speculative call sequence and return edge representing the call, the
1196 original EDGE can be removed and deallocated. Return the edge that now
1197 represents the call.
1199 For "speculative" indirect call that contains multiple "speculative"
1200 targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1201 decrease the count and only remove current direct edge.
1203 If no speculative direct call left to the speculative indirect call, remove
1204 the speculative of both the indirect call and corresponding direct edge.
1206 It is up to caller to iteratively resolve each "speculative" direct call and
1207 redirect the call as appropriate. */
1209 cgraph_edge *
1210 cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1212 cgraph_edge *e2;
1213 ipa_ref *ref;
1215 gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1216 if (!edge->callee)
1217 e2 = edge->first_speculative_call_target ();
1218 else
1219 e2 = edge;
1220 ref = e2->speculative_call_target_ref ();
1221 edge = edge->speculative_call_indirect_edge ();
1222 if (!callee_decl
1223 || !ref->referred->semantically_equivalent_p
1224 (symtab_node::get (callee_decl)))
1226 if (dump_file)
1228 if (callee_decl)
1230 fprintf (dump_file, "Speculative indirect call %s => %s has "
1231 "turned out to have contradicting known target ",
1232 edge->caller->dump_name (),
1233 e2->callee->dump_name ());
1234 print_generic_expr (dump_file, callee_decl);
1235 fprintf (dump_file, "\n");
1237 else
1239 fprintf (dump_file, "Removing speculative call %s => %s\n",
1240 edge->caller->dump_name (),
1241 e2->callee->dump_name ());
1245 else
1247 cgraph_edge *tmp = edge;
1248 if (dump_file)
1249 fprintf (dump_file, "Speculative call turned into direct call.\n");
1250 edge = e2;
1251 e2 = tmp;
1252 /* FIXME: If EDGE is inlined, we should scale up the frequencies
1253 and counts in the functions inlined through it. */
1255 edge->count += e2->count;
1256 if (edge->num_speculative_call_targets_p ())
1258 /* The indirect edge has multiple speculative targets, don't remove
1259 speculative until all related direct edges are resolved. */
1260 edge->indirect_info->num_speculative_call_targets--;
1261 if (!edge->indirect_info->num_speculative_call_targets)
1262 edge->speculative = false;
1264 else
1265 edge->speculative = false;
1266 e2->speculative = false;
1267 update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1268 ref->remove_reference ();
1269 if (e2->indirect_unknown_callee || e2->inline_failed)
1270 remove (e2);
1271 else
1272 e2->callee->remove_symbol_and_inline_clones ();
1273 return edge;
1276 /* Return edge corresponding to speculative call to a given target.
1277 NULL if speculative call does not have one. */
1279 cgraph_edge *
1280 cgraph_edge::speculative_call_for_target (cgraph_node *target)
1282 for (cgraph_edge *direct = first_speculative_call_target ();
1283 direct;
1284 direct = direct->next_speculative_call_target ())
1285 if (direct->speculative_call_target_ref ()
1286 ->referred->semantically_equivalent_p (target))
1287 return direct;
1288 return NULL;
1291 /* Make an indirect or speculative EDGE with an unknown callee an ordinary edge
1292 leading to CALLEE. Speculations can be resolved in the process and EDGE can
1293 be removed and deallocated. Return the edge that now represents the
1294 call. */
1296 cgraph_edge *
1297 cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1299 gcc_assert (edge->indirect_unknown_callee || edge->speculative);
1301 /* If we are redirecting speculative call, make it non-speculative. */
1302 if (edge->speculative)
1304 cgraph_edge *found = NULL;
1305 cgraph_edge *direct, *next;
1307 edge = edge->speculative_call_indirect_edge ();
1309 /* Look all speculative targets and remove all but one corresponding
1310 to callee (if it exists). */
1311 for (direct = edge->first_speculative_call_target ();
1312 direct;
1313 direct = next)
1315 next = direct->next_speculative_call_target ();
1317 /* Compare ref not direct->callee. Direct edge is possibly
1318 inlined or redirected. */
1319 if (!direct->speculative_call_target_ref ()
1320 ->referred->semantically_equivalent_p (callee))
1321 edge = direct->resolve_speculation (direct, NULL);
1322 else
1324 gcc_checking_assert (!found);
1325 found = direct;
1329 /* On successful speculation just remove the indirect edge and
1330 return the pre existing direct edge.
1331 It is important to not remove it and redirect because the direct
1332 edge may be inlined or redirected. */
1333 if (found)
1335 cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1336 gcc_checking_assert (!found->speculative && e2 == found);
1337 return found;
1339 gcc_checking_assert (!edge->speculative);
1342 edge->indirect_unknown_callee = 0;
1343 ggc_free (edge->indirect_info);
1344 edge->indirect_info = NULL;
1346 /* Get the edge out of the indirect edge list. */
1347 if (edge->prev_callee)
1348 edge->prev_callee->next_callee = edge->next_callee;
1349 if (edge->next_callee)
1350 edge->next_callee->prev_callee = edge->prev_callee;
1351 if (!edge->prev_callee)
1352 edge->caller->indirect_calls = edge->next_callee;
1354 /* Put it into the normal callee list */
1355 edge->prev_callee = NULL;
1356 edge->next_callee = edge->caller->callees;
1357 if (edge->caller->callees)
1358 edge->caller->callees->prev_callee = edge;
1359 edge->caller->callees = edge;
1361 /* Insert to callers list of the new callee. */
1362 edge->set_callee (callee);
1364 /* We need to re-determine the inlining status of the edge. */
1365 initialize_inline_failed (edge);
1366 return edge;
1369 /* Redirect callee of the edge to N. The function does not update underlying
1370 call expression. */
1372 void
1373 cgraph_edge::redirect_callee (cgraph_node *n)
1375 bool loc = callee->comdat_local_p ();
1376 /* Remove from callers list of the current callee. */
1377 remove_callee ();
1379 /* Insert to callers list of the new callee. */
1380 set_callee (n);
1382 if (!inline_failed)
1383 return;
1384 if (!loc && n->comdat_local_p ())
1386 cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1387 to->calls_comdat_local = true;
1389 else if (loc && !n->comdat_local_p ())
1391 cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1392 gcc_checking_assert (to->calls_comdat_local);
1393 to->calls_comdat_local = to->check_calls_comdat_local_p ();
1397 /* If necessary, change the function declaration in the call statement
1398 associated with E so that it corresponds to the edge callee. Speculations
1399 can be resolved in the process and EDGE can be removed and deallocated.
1401 The edge could be one of speculative direct call generated from speculative
1402 indirect call. In this circumstance, decrease the speculative targets
1403 count (i.e. num_speculative_call_targets) and redirect call stmt to the
1404 corresponding i-th target. If no speculative direct call left to the
1405 speculative indirect call, remove "speculative" of the indirect call and
1406 also redirect stmt to it's final direct target.
1408 When called from within tree-inline, KILLED_SSAs has to contain the pointer
1409 to killed_new_ssa_names within the copy_body_data structure and SSAs
1410 discovered to be useless (if LHS is removed) will be added to it, otherwise
1411 it needs to be NULL.
1413 It is up to caller to iteratively transform each "speculative"
1414 direct call as appropriate. */
1416 gimple *
1417 cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e,
1418 hash_set <tree> *killed_ssas)
1420 tree decl = gimple_call_fndecl (e->call_stmt);
1421 gcall *new_stmt;
1423 if (e->speculative)
1425 /* If there already is an direct call (i.e. as a result of inliner's
1426 substitution), forget about speculating. */
1427 if (decl)
1428 e = make_direct (e->speculative_call_indirect_edge (),
1429 cgraph_node::get (decl));
1430 else
1432 /* Be sure we redirect all speculative targets before poking
1433 about indirect edge. */
1434 gcc_checking_assert (e->callee);
1435 cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1436 gcall *new_stmt;
1437 ipa_ref *ref;
1439 /* Expand speculation into GIMPLE code. */
1440 if (dump_file)
1442 fprintf (dump_file,
1443 "Expanding speculative call of %s -> %s count: ",
1444 e->caller->dump_name (),
1445 e->callee->dump_name ());
1446 e->count.dump (dump_file);
1447 fprintf (dump_file, "\n");
1449 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1451 profile_count all = indirect->count;
1452 for (cgraph_edge *e2 = e->first_speculative_call_target ();
1454 e2 = e2->next_speculative_call_target ())
1455 all = all + e2->count;
1456 profile_probability prob = e->count.probability_in (all);
1457 if (!prob.initialized_p ())
1458 prob = profile_probability::even ();
1459 ref = e->speculative_call_target_ref ();
1460 new_stmt = gimple_ic (e->call_stmt,
1461 dyn_cast<cgraph_node *> (ref->referred),
1462 prob);
1463 e->speculative = false;
1464 if (indirect->num_speculative_call_targets_p ())
1466 /* The indirect edge has multiple speculative targets, don't
1467 remove speculative until all related direct edges are
1468 redirected. */
1469 indirect->indirect_info->num_speculative_call_targets--;
1470 if (!indirect->indirect_info->num_speculative_call_targets)
1471 indirect->speculative = false;
1473 else
1474 indirect->speculative = false;
1475 /* Indirect edges are not both in the call site hash.
1476 get it updated. */
1477 update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1478 cgraph_edge::set_call_stmt (e, new_stmt, false);
1479 e->count = gimple_bb (e->call_stmt)->count;
1481 /* Once we are done with expanding the sequence, update also indirect
1482 call probability. Until then the basic block accounts for the
1483 sum of indirect edge and all non-expanded speculations. */
1484 if (!indirect->speculative)
1485 indirect->count = gimple_bb (indirect->call_stmt)->count;
1486 ref->speculative = false;
1487 ref->stmt = NULL;
1488 pop_cfun ();
1489 /* Continue redirecting E to proper target. */
1494 if (e->indirect_unknown_callee
1495 || decl == e->callee->decl)
1496 return e->call_stmt;
1498 if (decl && ipa_saved_clone_sources)
1500 tree *p = ipa_saved_clone_sources->get (e->callee);
1501 if (p && decl == *p)
1503 gimple_call_set_fndecl (e->call_stmt, e->callee->decl);
1504 return e->call_stmt;
1507 if (flag_checking && decl)
1509 if (cgraph_node *node = cgraph_node::get (decl))
1511 clone_info *info = clone_info::get (node);
1512 gcc_assert (!info || !info->param_adjustments);
1516 clone_info *callee_info = clone_info::get (e->callee);
1517 if (symtab->dump_file)
1519 fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1520 e->caller->dump_name (), e->callee->dump_name ());
1521 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1522 if (callee_info && callee_info->param_adjustments)
1523 callee_info->param_adjustments->dump (symtab->dump_file);
1526 if (ipa_param_adjustments *padjs
1527 = callee_info ? callee_info->param_adjustments : NULL)
1529 /* We need to defer cleaning EH info on the new statement to
1530 fixup-cfg. We may not have dominator information at this point
1531 and thus would end up with unreachable blocks and have no way
1532 to communicate that we need to run CFG cleanup then. */
1533 int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1534 if (lp_nr != 0)
1535 remove_stmt_from_eh_lp (e->call_stmt);
1537 tree old_fntype = gimple_call_fntype (e->call_stmt);
1538 new_stmt = padjs->modify_call (e, false, killed_ssas);
1539 cgraph_node *origin = e->callee;
1540 while (origin->clone_of)
1541 origin = origin->clone_of;
1543 if ((origin->former_clone_of
1544 && old_fntype == TREE_TYPE (origin->former_clone_of))
1545 || old_fntype == TREE_TYPE (origin->decl))
1546 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1547 else
1549 tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1550 gimple_call_set_fntype (new_stmt, new_fntype);
1553 if (lp_nr != 0)
1554 add_stmt_to_eh_lp (new_stmt, lp_nr);
1556 else
1558 if (flag_checking
1559 && !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
1560 BUILT_IN_UNREACHABLE_TRAP))
1561 ipa_verify_edge_has_no_modifications (e);
1562 new_stmt = e->call_stmt;
1563 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1564 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1567 /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1568 adjust gimple_call_fntype too. */
1569 if (gimple_call_noreturn_p (new_stmt)
1570 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1571 && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1572 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1573 == void_type_node))
1574 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1576 /* If the call becomes noreturn, remove the LHS if possible. */
1577 tree lhs = gimple_call_lhs (new_stmt);
1578 if (lhs
1579 && gimple_call_noreturn_p (new_stmt)
1580 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1581 || should_remove_lhs_p (lhs)))
1583 gimple_call_set_lhs (new_stmt, NULL_TREE);
1584 /* We need to fix up the SSA name to avoid checking errors. */
1585 if (TREE_CODE (lhs) == SSA_NAME)
1587 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1588 TREE_TYPE (lhs), NULL);
1589 SET_SSA_NAME_VAR_OR_IDENTIFIER (lhs, var);
1590 SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
1591 set_ssa_default_def (DECL_STRUCT_FUNCTION (e->caller->decl),
1592 var, lhs);
1594 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1597 /* If new callee has no static chain, remove it. */
1598 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1600 gimple_call_set_chain (new_stmt, NULL);
1601 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1604 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1605 new_stmt);
1607 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1609 if (symtab->dump_file)
1611 fprintf (symtab->dump_file, " updated to:");
1612 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1614 return new_stmt;
1617 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1618 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1619 of OLD_STMT if it was previously call statement.
1620 If NEW_STMT is NULL, the call has been dropped without any
1621 replacement. */
1623 static void
1624 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1625 gimple *old_stmt, tree old_call,
1626 gimple *new_stmt)
1628 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1629 ? gimple_call_fndecl (new_stmt) : 0;
1631 /* We are seeing indirect calls, then there is nothing to update. */
1632 if (!new_call && !old_call)
1633 return;
1634 /* See if we turned indirect call into direct call or folded call to one builtin
1635 into different builtin. */
1636 if (old_call != new_call)
1638 cgraph_edge *e = node->get_edge (old_stmt);
1639 cgraph_edge *ne = NULL;
1640 profile_count count;
1642 if (e)
1644 /* Keep calls marked as dead dead. */
1645 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1646 && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
1647 BUILT_IN_UNREACHABLE_TRAP))
1649 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1650 as_a <gcall *> (new_stmt));
1651 return;
1653 /* See if the edge is already there and has the correct callee. It
1654 might be so because of indirect inlining has already updated
1655 it. We also might've cloned and redirected the edge. */
1656 if (new_call && e->callee)
1658 cgraph_node *callee = e->callee;
1659 while (callee)
1661 if (callee->decl == new_call
1662 || callee->former_clone_of == new_call)
1664 cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
1665 return;
1667 callee = callee->clone_of;
1671 /* Otherwise remove edge and create new one; we can't simply redirect
1672 since function has changed, so inline plan and other information
1673 attached to edge is invalid. */
1674 count = e->count;
1675 if (e->indirect_unknown_callee || e->inline_failed)
1676 cgraph_edge::remove (e);
1677 else
1678 e->callee->remove_symbol_and_inline_clones ();
1680 else if (new_call)
1682 /* We are seeing new direct call; compute profile info based on BB. */
1683 basic_block bb = gimple_bb (new_stmt);
1684 count = bb->count;
1687 if (new_call)
1689 ne = node->create_edge (cgraph_node::get_create (new_call),
1690 as_a <gcall *> (new_stmt), count);
1691 gcc_assert (ne->inline_failed);
1694 /* We only updated the call stmt; update pointer in cgraph edge.. */
1695 else if (old_stmt != new_stmt)
1696 cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1697 as_a <gcall *> (new_stmt));
1700 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1701 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1702 of OLD_STMT before it was updated (updating can happen inplace). */
1704 void
1705 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1706 gimple *new_stmt)
1708 cgraph_node *orig = cgraph_node::get (cfun->decl);
1709 cgraph_node *node;
1711 gcc_checking_assert (orig);
1712 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1713 if (orig->clones)
1714 for (node = orig->clones; node != orig;)
1716 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl,
1717 new_stmt);
1718 if (node->clones)
1719 node = node->clones;
1720 else if (node->next_sibling_clone)
1721 node = node->next_sibling_clone;
1722 else
1724 while (node != orig && !node->next_sibling_clone)
1725 node = node->clone_of;
1726 if (node != orig)
1727 node = node->next_sibling_clone;
1733 /* Remove all callees from the node. */
1735 void
1736 cgraph_node::remove_callees (void)
1738 cgraph_edge *e, *f;
1740 calls_comdat_local = false;
1742 /* It is sufficient to remove the edges from the lists of callers of
1743 the callees. The callee list of the node can be zapped with one
1744 assignment. */
1745 for (e = callees; e; e = f)
1747 f = e->next_callee;
1748 symtab->call_edge_removal_hooks (e);
1749 if (!e->indirect_unknown_callee)
1750 e->remove_callee ();
1751 symtab->free_edge (e);
1753 for (e = indirect_calls; e; e = f)
1755 f = e->next_callee;
1756 symtab->call_edge_removal_hooks (e);
1757 if (!e->indirect_unknown_callee)
1758 e->remove_callee ();
1759 symtab->free_edge (e);
1761 indirect_calls = NULL;
1762 callees = NULL;
1763 if (call_site_hash)
1765 call_site_hash->empty ();
1766 call_site_hash = NULL;
1770 /* Remove all callers from the node. */
1772 void
1773 cgraph_node::remove_callers (void)
1775 cgraph_edge *e, *f;
1777 /* It is sufficient to remove the edges from the lists of callees of
1778 the callers. The caller list of the node can be zapped with one
1779 assignment. */
1780 for (e = callers; e; e = f)
1782 f = e->next_caller;
1783 symtab->call_edge_removal_hooks (e);
1784 e->remove_caller ();
1785 symtab->free_edge (e);
1787 callers = NULL;
1790 /* Helper function for cgraph_release_function_body and free_lang_data.
1791 It releases body from function DECL without having to inspect its
1792 possibly non-existent symtab node. */
1794 void
1795 release_function_body (tree decl)
1797 function *fn = DECL_STRUCT_FUNCTION (decl);
1798 if (fn)
1800 if (fn->cfg
1801 && loops_for_fn (fn))
1803 fn->curr_properties &= ~PROP_loops;
1804 loop_optimizer_finalize (fn);
1806 if (fn->gimple_df)
1808 delete_tree_ssa (fn);
1809 fn->eh = NULL;
1811 if (fn->cfg)
1813 gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1814 gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1815 delete_tree_cfg_annotations (fn);
1816 free_cfg (fn);
1817 fn->cfg = NULL;
1819 if (fn->value_histograms)
1820 free_histograms (fn);
1821 gimple_set_body (decl, NULL);
1822 /* Struct function hangs a lot of data that would leak if we didn't
1823 removed all pointers to it. */
1824 ggc_free (fn);
1825 DECL_STRUCT_FUNCTION (decl) = NULL;
1827 DECL_SAVED_TREE (decl) = NULL;
1830 /* Release memory used to represent body of function.
1831 Use this only for functions that are released before being translated to
1832 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1833 are free'd in final.cc via free_after_compilation().
1834 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1836 void
1837 cgraph_node::release_body (bool keep_arguments)
1839 ipa_transforms_to_apply.release ();
1840 if (!used_as_abstract_origin && symtab->state != PARSING)
1842 DECL_RESULT (decl) = NULL;
1844 if (!keep_arguments)
1845 DECL_ARGUMENTS (decl) = NULL;
1847 /* If the node is abstract and needed, then do not clear
1848 DECL_INITIAL of its associated function declaration because it's
1849 needed to emit debug info later. */
1850 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1851 DECL_INITIAL (decl) = error_mark_node;
1852 release_function_body (decl);
1853 if (lto_file_data)
1855 lto_free_function_in_decl_state_for_node (this);
1856 lto_file_data = NULL;
1858 if (flag_checking && clones)
1860 /* It is invalid to release body before materializing clones except
1861 for thunks that don't really need a body. Verify also that we do
1862 not leak pointers to the call statements. */
1863 for (cgraph_node *node = clones; node;
1864 node = node->next_sibling_clone)
1865 gcc_assert (node->thunk && !node->callees->call_stmt);
1867 remove_callees ();
1868 remove_all_references ();
1871 /* Remove function from symbol table. */
1873 void
1874 cgraph_node::remove (void)
1876 bool clone_info_set = false;
1877 clone_info *info, saved_info;
1878 if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1879 fprintf (symtab->ipa_clones_dump_file,
1880 "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1881 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1882 DECL_SOURCE_COLUMN (decl));
1884 if ((info = clone_info::get (this)) != NULL)
1886 saved_info = *info;
1887 clone_info_set = true;
1889 symtab->call_cgraph_removal_hooks (this);
1890 remove_callers ();
1891 remove_callees ();
1892 ipa_transforms_to_apply.release ();
1893 delete_function_version (function_version ());
1895 /* Incremental inlining access removed nodes stored in the postorder list.
1897 force_output = false;
1898 forced_by_abi = false;
1900 unregister (clone_info_set ? &saved_info : NULL);
1901 if (prev_sibling_clone)
1902 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1903 else if (clone_of)
1905 clone_of->clones = next_sibling_clone;
1906 if (!clones)
1908 bool need_body = false;
1909 for (cgraph_node *n = clone_of; n; n = n->clone_of)
1910 if (n->analyzed || n->clones)
1912 need_body = true;
1913 break;
1915 if (!need_body)
1916 clone_of->release_body ();
1919 if (next_sibling_clone)
1920 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1921 if (clones)
1923 cgraph_node *n, *next;
1925 if (clone_of)
1927 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1928 n->clone_of = clone_of;
1929 n->clone_of = clone_of;
1930 n->next_sibling_clone = clone_of->clones;
1931 if (clone_of->clones)
1932 clone_of->clones->prev_sibling_clone = n;
1933 clone_of->clones = clones;
1935 else
1937 /* We are removing node with clones. This makes clones inconsistent,
1938 but assume they will be removed subsequently and just keep clone
1939 tree intact. This can happen in unreachable function removal since
1940 we remove unreachable functions in random order, not by bottom-up
1941 walk of clone trees. */
1942 for (n = clones; n; n = next)
1944 next = n->next_sibling_clone;
1945 n->next_sibling_clone = NULL;
1946 n->prev_sibling_clone = NULL;
1947 n->clone_of = NULL;
1952 /* While all the clones are removed after being proceeded, the function
1953 itself is kept in the cgraph even after it is compiled. Check whether
1954 we are done with this body and reclaim it proactively if this is the case.
1956 if (symtab->state != LTO_STREAMING)
1958 cgraph_node *n = cgraph_node::get (decl);
1959 if (!n
1960 || (!n->clones && !n->clone_of && !n->inlined_to
1961 && ((symtab->global_info_ready || in_lto_p)
1962 && (TREE_ASM_WRITTEN (n->decl)
1963 || DECL_EXTERNAL (n->decl)
1964 || !n->analyzed
1965 || (!flag_wpa && n->in_other_partition)))))
1966 release_body ();
1968 else
1970 lto_free_function_in_decl_state_for_node (this);
1971 lto_file_data = NULL;
1974 decl = NULL;
1975 if (call_site_hash)
1977 call_site_hash->empty ();
1978 call_site_hash = NULL;
1981 symtab->release_symbol (this);
1984 /* Likewise indicate that a node is having address taken. */
1986 void
1987 cgraph_node::mark_address_taken (void)
1989 /* Indirect inlining can figure out that all uses of the address are
1990 inlined. */
1991 if (inlined_to)
1993 gcc_assert (cfun->after_inlining);
1994 gcc_assert (callers->indirect_inlining_edge);
1995 return;
1997 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1998 IPA_REF_ADDR reference exists (and thus it should be set on node
1999 representing alias we take address of) and as a test whether address
2000 of the object was taken (and thus it should be set on node alias is
2001 referring to). We should remove the first use and the remove the
2002 following set. */
2003 address_taken = 1;
2004 cgraph_node *node = ultimate_alias_target ();
2005 node->address_taken = 1;
2008 /* Return local info node for the compiled function. */
2010 cgraph_node *
2011 cgraph_node::local_info_node (tree decl)
2013 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2014 cgraph_node *node = get (decl);
2015 if (!node)
2016 return NULL;
2017 return node->ultimate_alias_target ();
2020 /* Return RTL info for the compiled function. */
2022 cgraph_rtl_info *
2023 cgraph_node::rtl_info (const_tree decl)
2025 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2026 cgraph_node *node = get (decl);
2027 if (!node)
2028 return NULL;
2029 enum availability avail;
2030 node = node->ultimate_alias_target (&avail);
2031 if (decl != current_function_decl
2032 && (avail < AVAIL_AVAILABLE
2033 || (node->decl != current_function_decl
2034 && !TREE_ASM_WRITTEN (node->decl))))
2035 return NULL;
2036 /* Allocate if it doesn't exist. */
2037 if (node->rtl == NULL)
2039 node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
2040 SET_HARD_REG_SET (node->rtl->function_used_regs);
2042 return node->rtl;
2045 /* Return a string describing the failure REASON. */
2047 const char*
2048 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
2050 #undef DEFCIFCODE
2051 #define DEFCIFCODE(code, type, string) string,
2053 static const char *cif_string_table[CIF_N_REASONS] = {
2054 #include "cif-code.def"
2057 /* Signedness of an enum type is implementation defined, so cast it
2058 to unsigned before testing. */
2059 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2060 return cif_string_table[reason];
2063 /* Return a type describing the failure REASON. */
2065 cgraph_inline_failed_type_t
2066 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2068 #undef DEFCIFCODE
2069 #define DEFCIFCODE(code, type, string) type,
2071 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2072 #include "cif-code.def"
2075 /* Signedness of an enum type is implementation defined, so cast it
2076 to unsigned before testing. */
2077 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2078 return cif_type_table[reason];
2081 /* Names used to print out the availability enum. */
2082 const char * const cgraph_availability_names[] =
2083 {"unset", "not_available", "overwritable", "available", "local"};
2085 /* Output flags of edge to a file F. */
2087 void
2088 cgraph_edge::dump_edge_flags (FILE *f)
2090 if (speculative)
2091 fprintf (f, "(speculative) ");
2092 if (!inline_failed)
2093 fprintf (f, "(inlined) ");
2094 if (call_stmt_cannot_inline_p)
2095 fprintf (f, "(call_stmt_cannot_inline_p) ");
2096 if (indirect_inlining_edge)
2097 fprintf (f, "(indirect_inlining) ");
2098 if (count.initialized_p ())
2100 fprintf (f, "(");
2101 count.dump (f);
2102 fprintf (f, ",");
2103 fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2105 if (can_throw_external)
2106 fprintf (f, "(can throw external) ");
2109 /* Dump edge to stderr. */
2111 void
2112 cgraph_edge::debug (void)
2114 fprintf (stderr, "%s -> %s ", caller->dump_asm_name (),
2115 callee == NULL ? "(null)" : callee->dump_asm_name ());
2116 dump_edge_flags (stderr);
2117 fprintf (stderr, "\n\n");
2118 caller->debug ();
2119 if (callee != NULL)
2120 callee->debug ();
2123 /* Dump call graph node to file F. */
2125 void
2126 cgraph_node::dump (FILE *f)
2128 cgraph_edge *edge;
2130 dump_base (f);
2132 if (inlined_to)
2133 fprintf (f, " Function %s is inline copy in %s\n",
2134 dump_name (),
2135 inlined_to->dump_name ());
2136 if (clone_of)
2137 fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2138 if (symtab->function_flags_ready)
2139 fprintf (f, " Availability: %s\n",
2140 cgraph_availability_names [get_availability ()]);
2142 if (profile_id)
2143 fprintf (f, " Profile id: %i\n",
2144 profile_id);
2145 if (unit_id)
2146 fprintf (f, " Unit id: %i\n",
2147 unit_id);
2148 cgraph_function_version_info *vi = function_version ();
2149 if (vi != NULL)
2151 fprintf (f, " Version info: ");
2152 if (vi->prev != NULL)
2154 fprintf (f, "prev: ");
2155 fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2157 if (vi->next != NULL)
2159 fprintf (f, "next: ");
2160 fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2162 if (vi->dispatcher_resolver != NULL_TREE)
2163 fprintf (f, "dispatcher: %s",
2164 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2166 fprintf (f, "\n");
2168 fprintf (f, " Function flags:");
2169 if (count.initialized_p ())
2171 fprintf (f, " count:");
2172 count.dump (f);
2174 if (tp_first_run > 0)
2175 fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2176 if (cgraph_node *origin = nested_function_origin (this))
2177 fprintf (f, " nested in:%s", origin->dump_asm_name ());
2178 if (gimple_has_body_p (decl))
2179 fprintf (f, " body");
2180 if (process)
2181 fprintf (f, " process");
2182 if (local)
2183 fprintf (f, " local");
2184 if (redefined_extern_inline)
2185 fprintf (f, " redefined_extern_inline");
2186 if (only_called_at_startup)
2187 fprintf (f, " only_called_at_startup");
2188 if (only_called_at_exit)
2189 fprintf (f, " only_called_at_exit");
2190 if (tm_clone)
2191 fprintf (f, " tm_clone");
2192 if (calls_comdat_local)
2193 fprintf (f, " calls_comdat_local");
2194 if (icf_merged)
2195 fprintf (f, " icf_merged");
2196 if (merged_comdat)
2197 fprintf (f, " merged_comdat");
2198 if (merged_extern_inline)
2199 fprintf (f, " merged_extern_inline");
2200 if (split_part)
2201 fprintf (f, " split_part");
2202 if (indirect_call_target)
2203 fprintf (f, " indirect_call_target");
2204 if (nonfreeing_fn)
2205 fprintf (f, " nonfreeing_fn");
2206 if (DECL_STATIC_CONSTRUCTOR (decl))
2207 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2208 if (DECL_STATIC_DESTRUCTOR (decl))
2209 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2210 if (frequency == NODE_FREQUENCY_HOT)
2211 fprintf (f, " hot");
2212 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2213 fprintf (f, " unlikely_executed");
2214 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2215 fprintf (f, " executed_once");
2216 if (opt_for_fn (decl, optimize_size))
2217 fprintf (f, " optimize_size");
2218 if (parallelized_function)
2219 fprintf (f, " parallelized_function");
2220 if (DECL_IS_MALLOC (decl))
2221 fprintf (f, " decl_is_malloc");
2222 if (DECL_IS_OPERATOR_NEW_P (decl))
2223 fprintf (f, " %soperator_new",
2224 DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2225 if (DECL_IS_OPERATOR_DELETE_P (decl))
2226 fprintf (f, " %soperator_delete",
2227 DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2229 if (DECL_STATIC_CHAIN (decl))
2230 fprintf (f, " static_chain");
2232 fprintf (f, "\n");
2234 if (thunk)
2236 fprintf (f, " Thunk");
2237 thunk_info::get (this)->dump (f);
2239 else if (former_thunk_p ())
2241 fprintf (f, " Former thunk ");
2242 thunk_info::get (this)->dump (f);
2244 else gcc_checking_assert (!thunk_info::get (this));
2246 fprintf (f, " Called by: ");
2248 profile_count sum = profile_count::zero ();
2249 for (edge = callers; edge; edge = edge->next_caller)
2251 fprintf (f, "%s ", edge->caller->dump_asm_name ());
2252 edge->dump_edge_flags (f);
2253 if (edge->count.initialized_p ())
2254 sum += edge->count.ipa ();
2257 fprintf (f, "\n Calls: ");
2258 for (edge = callees; edge; edge = edge->next_callee)
2260 fprintf (f, "%s ", edge->callee->dump_asm_name ());
2261 edge->dump_edge_flags (f);
2263 fprintf (f, "\n");
2265 if (!body_removed && count.ipa ().initialized_p ())
2267 bool ok = true;
2268 bool min = false;
2269 ipa_ref *ref;
2271 FOR_EACH_ALIAS (this, ref)
2272 if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2273 sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2275 if (inlined_to
2276 || (symtab->state < EXPANSION
2277 && ultimate_alias_target () == this && only_called_directly_p ()))
2278 ok = !count.ipa ().differs_from_p (sum);
2279 else if (count.ipa () > profile_count::from_gcov_type (100)
2280 && count.ipa () < sum.apply_scale (99, 100))
2281 ok = false, min = true;
2282 if (!ok)
2284 fprintf (f, " Invalid sum of caller counts ");
2285 sum.dump (f);
2286 if (min)
2287 fprintf (f, ", should be at most ");
2288 else
2289 fprintf (f, ", should be ");
2290 count.ipa ().dump (f);
2291 fprintf (f, "\n");
2295 for (edge = indirect_calls; edge; edge = edge->next_callee)
2297 if (edge->indirect_info->polymorphic)
2299 fprintf (f, " Polymorphic indirect call of type ");
2300 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2301 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2303 else
2304 fprintf (f, " Indirect call");
2305 edge->dump_edge_flags (f);
2306 if (edge->indirect_info->param_index != -1)
2308 fprintf (f, "of param:%i ", edge->indirect_info->param_index);
2309 if (edge->indirect_info->agg_contents)
2310 fprintf (f, "loaded from %s %s at offset %i ",
2311 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2312 edge->indirect_info->by_ref ? "passed by reference":"",
2313 (int)edge->indirect_info->offset);
2314 if (edge->indirect_info->vptr_changed)
2315 fprintf (f, "(vptr maybe changed) ");
2317 fprintf (f, "num speculative call targets: %i\n",
2318 edge->indirect_info->num_speculative_call_targets);
2319 if (edge->indirect_info->polymorphic)
2320 edge->indirect_info->context.dump (f);
2324 /* Dump call graph node to file F in graphviz format. */
2326 void
2327 cgraph_node::dump_graphviz (FILE *f)
2329 cgraph_edge *edge;
2331 for (edge = callees; edge; edge = edge->next_callee)
2333 cgraph_node *callee = edge->callee;
2335 fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2340 /* Dump call graph node NODE to stderr. */
2342 DEBUG_FUNCTION void
2343 cgraph_node::debug (void)
2345 dump (stderr);
2348 /* Dump the callgraph to file F. */
2350 void
2351 cgraph_node::dump_cgraph (FILE *f)
2353 cgraph_node *node;
2355 fprintf (f, "callgraph:\n\n");
2356 FOR_EACH_FUNCTION (node)
2357 node->dump (f);
2360 /* Return true when the DECL can possibly be inlined. */
2362 bool
2363 cgraph_function_possibly_inlined_p (tree decl)
2365 if (!symtab->global_info_ready)
2366 return !DECL_UNINLINABLE (decl);
2367 return DECL_POSSIBLY_INLINED (decl);
2370 /* Return function availability. See cgraph.h for description of individual
2371 return values. */
2372 enum availability
2373 cgraph_node::get_availability (symtab_node *ref)
2375 if (ref)
2377 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2378 if (cref)
2379 ref = cref->inlined_to;
2381 enum availability avail;
2382 if (!analyzed && !in_other_partition)
2383 avail = AVAIL_NOT_AVAILABLE;
2384 else if (local)
2385 avail = AVAIL_LOCAL;
2386 else if (inlined_to)
2387 avail = AVAIL_AVAILABLE;
2388 else if (transparent_alias)
2389 ultimate_alias_target (&avail, ref);
2390 else if (ifunc_resolver
2391 || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2392 avail = AVAIL_INTERPOSABLE;
2393 else if (!externally_visible)
2394 avail = AVAIL_AVAILABLE;
2395 /* If this is a reference from symbol itself and there are no aliases, we
2396 may be sure that the symbol was not interposed by something else because
2397 the symbol itself would be unreachable otherwise.
2399 Also comdat groups are always resolved in groups. */
2400 else if ((this == ref && !has_aliases_p ())
2401 || (ref && get_comdat_group ()
2402 && get_comdat_group () == ref->get_comdat_group ()))
2403 avail = AVAIL_AVAILABLE;
2404 /* Inline functions are safe to be analyzed even if their symbol can
2405 be overwritten at runtime. It is not meaningful to enforce any sane
2406 behavior on replacing inline function by different body. */
2407 else if (DECL_DECLARED_INLINE_P (decl))
2408 avail = AVAIL_AVAILABLE;
2410 /* If the function can be overwritten, return OVERWRITABLE. Take
2411 care at least of two notable extensions - the COMDAT functions
2412 used to share template instantiations in C++ (this is symmetric
2413 to code cp_cannot_inline_tree_fn and probably shall be shared and
2414 the inlinability hooks completely eliminated). */
2416 else if (decl_replaceable_p (decl, semantic_interposition)
2417 && !DECL_EXTERNAL (decl))
2418 avail = AVAIL_INTERPOSABLE;
2419 else avail = AVAIL_AVAILABLE;
2421 return avail;
2424 /* Worker for cgraph_node_can_be_local_p. */
2425 static bool
2426 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2428 return !(!node->force_output
2429 && !node->ifunc_resolver
2430 /* Limitation of gas requires us to output targets of symver aliases
2431 as global symbols. This is binutils PR 25295. */
2432 && !node->symver
2433 && ((DECL_COMDAT (node->decl)
2434 && !node->forced_by_abi
2435 && !node->used_from_object_file_p ()
2436 && !node->same_comdat_group)
2437 || !node->externally_visible));
2440 /* Return true if cgraph_node can be made local for API change.
2441 Extern inline functions and C++ COMDAT functions can be made local
2442 at the expense of possible code size growth if function is used in multiple
2443 compilation units. */
2444 bool
2445 cgraph_node::can_be_local_p (void)
2447 return (!address_taken
2448 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2449 NULL, true));
2452 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2453 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2454 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2455 skipped. */
2456 bool
2457 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2458 (cgraph_node *, void *),
2459 void *data,
2460 bool include_overwritable,
2461 bool exclude_virtual_thunks)
2463 cgraph_edge *e;
2464 ipa_ref *ref;
2465 enum availability avail = AVAIL_AVAILABLE;
2467 if (include_overwritable
2468 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2470 if (callback (this, data))
2471 return true;
2473 FOR_EACH_ALIAS (this, ref)
2475 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2476 if (include_overwritable
2477 || alias->get_availability () > AVAIL_INTERPOSABLE)
2478 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2479 include_overwritable,
2480 exclude_virtual_thunks))
2481 return true;
2483 if (avail <= AVAIL_INTERPOSABLE)
2484 return false;
2485 for (e = callers; e; e = e->next_caller)
2486 if (e->caller->thunk
2487 && (include_overwritable
2488 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2489 && !(exclude_virtual_thunks
2490 && thunk_info::get (e->caller)->virtual_offset_p))
2491 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2492 include_overwritable,
2493 exclude_virtual_thunks))
2494 return true;
2496 return false;
2499 /* Worker to bring NODE local. */
2501 bool
2502 cgraph_node::make_local (cgraph_node *node, void *)
2504 gcc_checking_assert (node->can_be_local_p ());
2505 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2507 node->make_decl_local ();
2508 node->set_section (NULL);
2509 node->set_comdat_group (NULL);
2510 node->externally_visible = false;
2511 node->forced_by_abi = false;
2512 node->local = true;
2513 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2514 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2515 && !flag_incremental_link);
2516 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2517 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2519 return false;
2522 /* Bring cgraph node local. */
2524 void
2525 cgraph_node::make_local (void)
2527 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2530 /* Worker to set nothrow flag. */
2532 static void
2533 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2534 bool *changed)
2536 cgraph_edge *e;
2538 if (nothrow && !TREE_NOTHROW (node->decl))
2540 /* With non-call exceptions we can't say for sure if other function body
2541 was not possibly optimized to still throw. */
2542 if (!non_call || node->binds_to_current_def_p ())
2544 TREE_NOTHROW (node->decl) = true;
2545 *changed = true;
2546 for (e = node->callers; e; e = e->next_caller)
2547 e->can_throw_external = false;
2550 else if (!nothrow && TREE_NOTHROW (node->decl))
2552 TREE_NOTHROW (node->decl) = false;
2553 *changed = true;
2555 ipa_ref *ref;
2556 FOR_EACH_ALIAS (node, ref)
2558 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2559 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2560 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2562 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2563 if (e->caller->thunk
2564 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2565 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2568 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2569 if any to NOTHROW. */
2571 bool
2572 cgraph_node::set_nothrow_flag (bool nothrow)
2574 bool changed = false;
2575 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2577 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2578 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2579 else
2581 ipa_ref *ref;
2583 FOR_EACH_ALIAS (this, ref)
2585 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2586 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2587 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2590 return changed;
2593 /* Worker to set malloc flag. */
2594 static void
2595 set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2597 if (malloc_p && !DECL_IS_MALLOC (node->decl))
2599 DECL_IS_MALLOC (node->decl) = true;
2600 *changed = true;
2603 ipa_ref *ref;
2604 FOR_EACH_ALIAS (node, ref)
2606 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2607 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2608 set_malloc_flag_1 (alias, malloc_p, changed);
2611 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2612 if (e->caller->thunk
2613 && (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2614 set_malloc_flag_1 (e->caller, malloc_p, changed);
2617 /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any. */
2619 bool
2620 cgraph_node::set_malloc_flag (bool malloc_p)
2622 bool changed = false;
2624 if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2625 set_malloc_flag_1 (this, malloc_p, &changed);
2626 else
2628 ipa_ref *ref;
2630 FOR_EACH_ALIAS (this, ref)
2632 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2633 if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2634 set_malloc_flag_1 (alias, malloc_p, &changed);
2637 return changed;
2640 /* Worker to set malloc flag. */
2641 static void
2642 add_detected_attribute_1 (cgraph_node *node, const char *attr, bool *changed)
2644 if (!lookup_attribute (attr, DECL_ATTRIBUTES (node->decl)))
2646 DECL_ATTRIBUTES (node->decl) = tree_cons (get_identifier (attr),
2647 NULL_TREE, DECL_ATTRIBUTES (node->decl));
2648 *changed = true;
2651 ipa_ref *ref;
2652 FOR_EACH_ALIAS (node, ref)
2654 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2655 if (alias->get_availability () > AVAIL_INTERPOSABLE)
2656 add_detected_attribute_1 (alias, attr, changed);
2659 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2660 if (e->caller->thunk
2661 && (e->caller->get_availability () > AVAIL_INTERPOSABLE))
2662 add_detected_attribute_1 (e->caller, attr, changed);
2665 /* Add attribyte ATTR to function and its aliases. */
2667 bool
2668 cgraph_node::add_detected_attribute (const char *attr)
2670 bool changed = false;
2672 if (get_availability () > AVAIL_INTERPOSABLE)
2673 add_detected_attribute_1 (this, attr, &changed);
2674 else
2676 ipa_ref *ref;
2678 FOR_EACH_ALIAS (this, ref)
2680 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2681 if (alias->get_availability () > AVAIL_INTERPOSABLE)
2682 add_detected_attribute_1 (alias, attr, &changed);
2685 return changed;
2688 /* Worker to set noreturng flag. */
2689 static void
2690 set_noreturn_flag_1 (cgraph_node *node, bool noreturn_p, bool *changed)
2692 if (noreturn_p && !TREE_THIS_VOLATILE (node->decl))
2694 TREE_THIS_VOLATILE (node->decl) = true;
2695 *changed = true;
2698 ipa_ref *ref;
2699 FOR_EACH_ALIAS (node, ref)
2701 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2702 if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2703 set_noreturn_flag_1 (alias, noreturn_p, changed);
2706 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2707 if (e->caller->thunk
2708 && (!noreturn_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2709 set_noreturn_flag_1 (e->caller, noreturn_p, changed);
2712 /* Set TREE_THIS_VOLATILE on NODE's decl and on NODE's aliases if any. */
2714 bool
2715 cgraph_node::set_noreturn_flag (bool noreturn_p)
2717 bool changed = false;
2719 if (!noreturn_p || get_availability () > AVAIL_INTERPOSABLE)
2720 set_noreturn_flag_1 (this, noreturn_p, &changed);
2721 else
2723 ipa_ref *ref;
2725 FOR_EACH_ALIAS (this, ref)
2727 cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2728 if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2729 set_noreturn_flag_1 (alias, noreturn_p, &changed);
2732 return changed;
2735 /* Worker to set_const_flag. */
2737 static void
2738 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2739 bool *changed)
2741 /* Static constructors and destructors without a side effect can be
2742 optimized out. */
2743 if (set_const && !looping)
2745 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2747 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2748 *changed = true;
2750 if (DECL_STATIC_DESTRUCTOR (node->decl))
2752 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2753 *changed = true;
2756 if (!set_const)
2758 if (TREE_READONLY (node->decl))
2760 TREE_READONLY (node->decl) = 0;
2761 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2762 *changed = true;
2765 else
2767 /* Consider function:
2769 bool a(int *p)
2771 return *p==*p;
2774 During early optimization we will turn this into:
2776 bool a(int *p)
2778 return true;
2781 Now if this function will be detected as CONST however when interposed
2782 it may end up being just pure. We always must assume the worst
2783 scenario here. */
2784 if (TREE_READONLY (node->decl))
2786 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2788 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2789 *changed = true;
2792 else if (node->binds_to_current_def_p ())
2794 TREE_READONLY (node->decl) = true;
2795 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2796 DECL_PURE_P (node->decl) = false;
2797 *changed = true;
2799 else
2801 if (dump_file && (dump_flags & TDF_DETAILS))
2802 fprintf (dump_file, "Dropping state to PURE because function does "
2803 "not bind to current def.\n");
2804 if (!DECL_PURE_P (node->decl))
2806 DECL_PURE_P (node->decl) = true;
2807 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2808 *changed = true;
2810 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2812 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2813 *changed = true;
2818 ipa_ref *ref;
2819 FOR_EACH_ALIAS (node, ref)
2821 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2822 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2823 set_const_flag_1 (alias, set_const, looping, changed);
2825 for (struct cgraph_node *n = node->simd_clones; n != NULL;
2826 n = n->simdclone->next_clone)
2827 set_const_flag_1 (n, set_const, looping, changed);
2828 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2829 if (e->caller->thunk
2830 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2832 /* Virtual thunks access virtual offset in the vtable, so they can
2833 only be pure, never const. */
2834 if (set_const
2835 && (thunk_info::get (e->caller)->virtual_offset_p
2836 || !node->binds_to_current_def_p (e->caller)))
2837 *changed |= e->caller->set_pure_flag (true, looping);
2838 else
2839 set_const_flag_1 (e->caller, set_const, looping, changed);
2843 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2844 If SET_CONST if false, clear the flag.
2846 When setting the flag be careful about possible interposition and
2847 do not set the flag for functions that can be interposed and set pure
2848 flag for functions that can bind to other definition.
2850 Return true if any change was done. */
2852 bool
2853 cgraph_node::set_const_flag (bool set_const, bool looping)
2855 bool changed = false;
2856 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2857 set_const_flag_1 (this, set_const, looping, &changed);
2858 else
2860 ipa_ref *ref;
2862 FOR_EACH_ALIAS (this, ref)
2864 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2865 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2866 set_const_flag_1 (alias, set_const, looping, &changed);
2869 return changed;
2872 /* Info used by set_pure_flag_1. */
2874 struct set_pure_flag_info
2876 bool pure;
2877 bool looping;
2878 bool changed;
2881 /* Worker to set_pure_flag. */
2883 static bool
2884 set_pure_flag_1 (cgraph_node *node, void *data)
2886 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2887 /* Static constructors and destructors without a side effect can be
2888 optimized out. */
2889 if (info->pure && !info->looping)
2891 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2893 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2894 info->changed = true;
2896 if (DECL_STATIC_DESTRUCTOR (node->decl))
2898 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2899 info->changed = true;
2902 if (info->pure)
2904 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2906 DECL_PURE_P (node->decl) = true;
2907 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2908 info->changed = true;
2910 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2911 && !info->looping)
2913 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2914 info->changed = true;
2917 else
2919 if (DECL_PURE_P (node->decl))
2921 DECL_PURE_P (node->decl) = false;
2922 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2923 info->changed = true;
2926 return false;
2929 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2930 if any to PURE.
2932 When setting the flag, be careful about possible interposition.
2933 Return true if any change was done. */
2935 bool
2936 cgraph_node::set_pure_flag (bool pure, bool looping)
2938 struct set_pure_flag_info info = {pure, looping, false};
2939 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2940 for (struct cgraph_node *n = simd_clones; n != NULL;
2941 n = n->simdclone->next_clone)
2942 set_pure_flag_1 (n, &info);
2943 return info.changed;
2946 /* Return true when cgraph_node cannot return or throw and thus
2947 it is safe to ignore its side effects for IPA analysis. */
2949 bool
2950 cgraph_node::cannot_return_p (void)
2952 int flags = flags_from_decl_or_type (decl);
2953 if (!opt_for_fn (decl, flag_exceptions))
2954 return (flags & ECF_NORETURN) != 0;
2955 else
2956 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2957 == (ECF_NORETURN | ECF_NOTHROW));
2960 /* Return true when call of edge cannot lead to return from caller
2961 and thus it is safe to ignore its side effects for IPA analysis
2962 when computing side effects of the caller.
2963 FIXME: We could actually mark all edges that have no reaching
2964 patch to the exit block or throw to get better results. */
2965 bool
2966 cgraph_edge::cannot_lead_to_return_p (void)
2968 if (caller->cannot_return_p ())
2969 return true;
2970 if (indirect_unknown_callee)
2972 int flags = indirect_info->ecf_flags;
2973 if (!opt_for_fn (caller->decl, flag_exceptions))
2974 return (flags & ECF_NORETURN) != 0;
2975 else
2976 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2977 == (ECF_NORETURN | ECF_NOTHROW));
2979 else
2980 return callee->cannot_return_p ();
2983 /* Return true if the edge may be considered hot. */
2985 bool
2986 cgraph_edge::maybe_hot_p (void)
2988 if (!maybe_hot_count_p (NULL, count.ipa ()))
2989 return false;
2990 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2991 || (callee
2992 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2993 return false;
2994 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2995 && (callee
2996 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2997 return false;
2998 if (opt_for_fn (caller->decl, optimize_size))
2999 return false;
3000 if (caller->frequency == NODE_FREQUENCY_HOT)
3001 return true;
3002 if (!count.initialized_p ())
3003 return true;
3004 cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
3005 if (!where->count.initialized_p ())
3006 return false;
3007 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
3009 if (count * 2 < where->count * 3)
3010 return false;
3012 else if (count * param_hot_bb_frequency_fraction < where->count)
3013 return false;
3014 return true;
3017 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
3019 static bool
3020 nonremovable_p (cgraph_node *node, void *)
3022 return !node->can_remove_if_no_direct_calls_and_refs_p ();
3025 /* Return true if whole comdat group can be removed if there are no direct
3026 calls to THIS. */
3028 bool
3029 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
3031 struct ipa_ref *ref;
3033 /* For local symbols or non-comdat group it is the same as
3034 can_remove_if_no_direct_calls_p. */
3035 if (!externally_visible || !same_comdat_group)
3037 if (DECL_EXTERNAL (decl))
3038 return true;
3039 if (address_taken)
3040 return false;
3041 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
3044 if (will_inline && address_taken)
3045 return false;
3047 /* Otherwise check if we can remove the symbol itself and then verify
3048 that only uses of the comdat groups are direct call to THIS
3049 or its aliases. */
3050 if (!can_remove_if_no_direct_calls_and_refs_p ())
3051 return false;
3053 /* Check that all refs come from within the comdat group. */
3054 for (int i = 0; iterate_referring (i, ref); i++)
3055 if (ref->referring->get_comdat_group () != get_comdat_group ())
3056 return false;
3058 struct cgraph_node *target = ultimate_alias_target ();
3059 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3060 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3062 if (!externally_visible)
3063 continue;
3064 if (!next->alias
3065 && !next->can_remove_if_no_direct_calls_and_refs_p ())
3066 return false;
3068 /* If we see different symbol than THIS, be sure to check calls. */
3069 if (next->ultimate_alias_target () != target)
3070 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3071 if (e->caller->get_comdat_group () != get_comdat_group ()
3072 || will_inline)
3073 return false;
3075 /* If function is not being inlined, we care only about
3076 references outside of the comdat group. */
3077 if (!will_inline)
3078 for (int i = 0; next->iterate_referring (i, ref); i++)
3079 if (ref->referring->get_comdat_group () != get_comdat_group ())
3080 return false;
3082 return true;
3085 /* Return true when function cgraph_node can be expected to be removed
3086 from program when direct calls in this compilation unit are removed.
3088 As a special case COMDAT functions are
3089 cgraph_can_remove_if_no_direct_calls_p while the are not
3090 cgraph_only_called_directly_p (it is possible they are called from other
3091 unit)
3093 This function behaves as cgraph_only_called_directly_p because eliminating
3094 all uses of COMDAT function does not make it necessarily disappear from
3095 the program unless we are compiling whole program or we do LTO. In this
3096 case we know we win since dynamic linking will not really discard the
3097 linkonce section. */
3099 bool
3100 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
3101 (bool will_inline)
3103 gcc_assert (!inlined_to);
3104 if (DECL_EXTERNAL (decl))
3105 return true;
3107 if (!in_lto_p && !flag_whole_program)
3109 /* If the symbol is in comdat group, we need to verify that whole comdat
3110 group becomes unreachable. Technically we could skip references from
3111 within the group, too. */
3112 if (!only_called_directly_p ())
3113 return false;
3114 if (same_comdat_group && externally_visible)
3116 struct cgraph_node *target = ultimate_alias_target ();
3118 if (will_inline && address_taken)
3119 return true;
3120 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3121 next != this;
3122 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3124 if (!externally_visible)
3125 continue;
3126 if (!next->alias
3127 && !next->only_called_directly_p ())
3128 return false;
3130 /* If we see different symbol than THIS,
3131 be sure to check calls. */
3132 if (next->ultimate_alias_target () != target)
3133 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3134 if (e->caller->get_comdat_group () != get_comdat_group ()
3135 || will_inline)
3136 return false;
3139 return true;
3141 else
3142 return can_remove_if_no_direct_calls_p (will_inline);
3146 /* Worker for cgraph_only_called_directly_p. */
3148 static bool
3149 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
3151 return !node->only_called_directly_or_aliased_p ();
3154 /* Return true when function cgraph_node and all its aliases are only called
3155 directly.
3156 i.e. it is not externally visible, address was not taken and
3157 it is not used in any other non-standard way. */
3159 bool
3160 cgraph_node::only_called_directly_p (void)
3162 gcc_assert (ultimate_alias_target () == this);
3163 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
3164 NULL, true);
3168 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
3170 static bool
3171 collect_callers_of_node_1 (cgraph_node *node, void *data)
3173 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3174 cgraph_edge *cs;
3175 enum availability avail;
3176 node->ultimate_alias_target (&avail);
3178 if (avail > AVAIL_INTERPOSABLE)
3179 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3180 if (!cs->indirect_inlining_edge
3181 && !cs->caller->thunk)
3182 redirect_callers->safe_push (cs);
3183 return false;
3186 /* Collect all callers of cgraph_node and its aliases that are known to lead to
3187 cgraph_node (i.e. are not overwritable). */
3189 auto_vec<cgraph_edge *>
3190 cgraph_node::collect_callers (void)
3192 auto_vec<cgraph_edge *> redirect_callers;
3193 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3194 &redirect_callers, false);
3195 return redirect_callers;
3199 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. Return
3200 optimistically true if this cannot be determined. */
3202 static bool
3203 clone_of_p (cgraph_node *node, cgraph_node *node2)
3205 node = node->ultimate_alias_target ();
3206 node2 = node2->ultimate_alias_target ();
3208 if (node2->clone_of == node
3209 || node2->former_clone_of == node->decl)
3210 return true;
3212 if (!node->thunk && !node->former_thunk_p ())
3214 while (node2
3215 && node->decl != node2->decl
3216 && node->decl != node2->former_clone_of)
3217 node2 = node2->clone_of;
3218 return node2 != NULL;
3221 /* There are no virtual clones of thunks so check former_clone_of or if we
3222 might have skipped thunks because this adjustments are no longer
3223 necessary. */
3224 while (node->thunk || node->former_thunk_p ())
3226 if (!thunk_info::get (node)->this_adjusting)
3227 return false;
3228 /* In case of instrumented expanded thunks, which can have multiple calls
3229 in them, we do not know how to continue and just have to be
3230 optimistic. The same applies if all calls have already been inlined
3231 into the thunk. */
3232 if (!node->callees || node->callees->next_callee)
3233 return true;
3234 node = node->callees->callee->ultimate_alias_target ();
3236 clone_info *info = clone_info::get (node2);
3237 if (!info || !info->param_adjustments
3238 || info->param_adjustments->first_param_intact_p ())
3239 return false;
3240 if (node2->former_clone_of == node->decl
3241 || node2->former_clone_of == node->former_clone_of)
3242 return true;
3244 cgraph_node *n2 = node2;
3245 while (n2 && node->decl != n2->decl)
3246 n2 = n2->clone_of;
3247 if (n2)
3248 return true;
3251 return false;
3254 /* Verify edge count and frequency. */
3256 bool
3257 cgraph_edge::verify_count ()
3259 bool error_found = false;
3260 if (!count.verify ())
3262 error ("caller edge count invalid");
3263 error_found = true;
3265 return error_found;
3268 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3269 static void
3270 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3272 bool fndecl_was_null = false;
3273 /* debug_gimple_stmt needs correct cfun */
3274 if (cfun != this_cfun)
3275 set_cfun (this_cfun);
3276 /* ...and an actual current_function_decl */
3277 if (!current_function_decl)
3279 current_function_decl = this_cfun->decl;
3280 fndecl_was_null = true;
3282 debug_gimple_stmt (stmt);
3283 if (fndecl_was_null)
3284 current_function_decl = NULL;
3287 /* Verify that call graph edge corresponds to DECL from the associated
3288 statement. Return true if the verification should fail. */
3290 bool
3291 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3293 cgraph_node *node;
3295 if (!decl || callee->inlined_to)
3296 return false;
3297 if (symtab->state == LTO_STREAMING)
3298 return false;
3299 node = cgraph_node::get (decl);
3301 /* We do not know if a node from a different partition is an alias or what it
3302 aliases and therefore cannot do the former_clone_of check reliably. When
3303 body_removed is set, we have lost all information about what was alias or
3304 thunk of and also cannot proceed. */
3305 if (!node
3306 || node->body_removed
3307 || node->in_other_partition
3308 || callee->icf_merged
3309 || callee->in_other_partition)
3310 return false;
3312 node = node->ultimate_alias_target ();
3314 /* Optimizers can redirect unreachable calls or calls triggering undefined
3315 behavior to __builtin_unreachable or __builtin_unreachable trap. */
3317 if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE,
3318 BUILT_IN_UNREACHABLE_TRAP))
3319 return false;
3321 if (callee->former_clone_of != node->decl
3322 && (node != callee->ultimate_alias_target ())
3323 && !clone_of_p (node, callee))
3324 return true;
3325 else
3326 return false;
3329 /* Disable warnings about missing quoting in GCC diagnostics for
3330 the verification errors. Their format strings don't follow GCC
3331 diagnostic conventions and the calls are ultimately followed by
3332 one to internal_error. */
3333 #if __GNUC__ >= 10
3334 # pragma GCC diagnostic push
3335 # pragma GCC diagnostic ignored "-Wformat-diag"
3336 #endif
3338 /* Verify consistency of speculative call in NODE corresponding to STMT
3339 and LTO_STMT_UID. If INDIRECT is set, assume that it is the indirect
3340 edge of call sequence. Return true if error is found.
3342 This function is called to every component of indirect call (direct edges,
3343 indirect edge and refs). To save duplicated work, do full testing only
3344 in that case. */
3345 static bool
3346 verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3347 unsigned int lto_stmt_uid,
3348 struct cgraph_edge *indirect)
3350 if (indirect == NULL)
3352 for (indirect = node->indirect_calls; indirect;
3353 indirect = indirect->next_callee)
3354 if (indirect->call_stmt == stmt
3355 && indirect->lto_stmt_uid == lto_stmt_uid)
3356 break;
3357 if (!indirect)
3359 error ("missing indirect call in speculative call sequence");
3360 return true;
3362 if (!indirect->speculative)
3364 error ("indirect call in speculative call sequence has no "
3365 "speculative flag");
3366 return true;
3368 return false;
3371 /* Maximal number of targets. We probably will never want to have more than
3372 this. */
3373 const unsigned int num = 256;
3374 cgraph_edge *direct_calls[num];
3375 ipa_ref *refs[num];
3377 for (unsigned int i = 0; i < num; i++)
3379 direct_calls[i] = NULL;
3380 refs[i] = NULL;
3383 cgraph_edge *first_call = NULL;
3384 cgraph_edge *prev_call = NULL;
3386 for (cgraph_edge *direct = node->callees; direct;
3387 direct = direct->next_callee)
3388 if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3390 if (!first_call)
3391 first_call = direct;
3392 if (prev_call && direct != prev_call->next_callee)
3394 error ("speculative edges are not adjacent");
3395 return true;
3397 prev_call = direct;
3398 if (!direct->speculative)
3400 error ("direct call to %s in speculative call sequence has no "
3401 "speculative flag", direct->callee->dump_name ());
3402 return true;
3404 if (direct->speculative_id >= num)
3406 error ("direct call to %s in speculative call sequence has "
3407 "speculative_id %i out of range",
3408 direct->callee->dump_name (), direct->speculative_id);
3409 return true;
3411 if (direct_calls[direct->speculative_id])
3413 error ("duplicate direct call to %s in speculative call sequence "
3414 "with speculative_id %i",
3415 direct->callee->dump_name (), direct->speculative_id);
3416 return true;
3418 direct_calls[direct->speculative_id] = direct;
3421 if (first_call->call_stmt
3422 && first_call != node->get_edge (first_call->call_stmt))
3424 error ("call stmt hash does not point to first direct edge of "
3425 "speculative call sequence");
3426 return true;
3429 ipa_ref *ref;
3430 for (int i = 0; node->iterate_reference (i, ref); i++)
3431 if (ref->speculative
3432 && ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3434 if (ref->speculative_id >= num)
3436 error ("direct call to %s in speculative call sequence has "
3437 "speculative_id %i out of range",
3438 ref->referred->dump_name (), ref->speculative_id);
3439 return true;
3441 if (refs[ref->speculative_id])
3443 error ("duplicate reference %s in speculative call sequence "
3444 "with speculative_id %i",
3445 ref->referred->dump_name (), ref->speculative_id);
3446 return true;
3448 refs[ref->speculative_id] = ref;
3451 int num_targets = 0;
3452 for (unsigned int i = 0 ; i < num ; i++)
3454 if (refs[i] && !direct_calls[i])
3456 error ("missing direct call for speculation %i", i);
3457 return true;
3459 if (!refs[i] && direct_calls[i])
3461 error ("missing ref for speculation %i", i);
3462 return true;
3464 if (refs[i] != NULL)
3465 num_targets++;
3468 if (num_targets != indirect->num_speculative_call_targets_p ())
3470 error ("number of speculative targets %i mismatched with "
3471 "num_speculative_call_targets %i",
3472 num_targets,
3473 indirect->num_speculative_call_targets_p ());
3474 return true;
3476 return false;
3479 /* Verify cgraph nodes of given cgraph node. */
3480 DEBUG_FUNCTION void
3481 cgraph_node::verify_node (void)
3483 cgraph_edge *e;
3484 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3485 basic_block this_block;
3486 gimple_stmt_iterator gsi;
3487 bool error_found = false;
3488 int i;
3489 ipa_ref *ref = NULL;
3491 if (seen_error ())
3492 return;
3494 timevar_push (TV_CGRAPH_VERIFY);
3495 error_found |= verify_base ();
3496 for (e = callees; e; e = e->next_callee)
3497 if (e->aux)
3499 error ("aux field set for edge %s->%s",
3500 identifier_to_locale (e->caller->name ()),
3501 identifier_to_locale (e->callee->name ()));
3502 error_found = true;
3504 if (!count.verify ())
3506 error ("cgraph count invalid");
3507 error_found = true;
3509 if (inlined_to && same_comdat_group)
3511 error ("inline clone in same comdat group list");
3512 error_found = true;
3514 if (inlined_to && !count.compatible_p (inlined_to->count))
3516 error ("inline clone count is not compatible");
3517 count.debug ();
3518 inlined_to->count.debug ();
3519 error_found = true;
3521 if (tp_first_run < 0)
3523 error ("tp_first_run must be non-negative");
3524 error_found = true;
3526 if (!definition && !in_other_partition && local)
3528 error ("local symbols must be defined");
3529 error_found = true;
3531 if (inlined_to && externally_visible)
3533 error ("externally visible inline clone");
3534 error_found = true;
3536 if (inlined_to && address_taken)
3538 error ("inline clone with address taken");
3539 error_found = true;
3541 if (inlined_to && force_output)
3543 error ("inline clone is forced to output");
3544 error_found = true;
3546 if (symtab->state != LTO_STREAMING)
3548 if (calls_comdat_local && !same_comdat_group)
3550 error ("calls_comdat_local is set outside of a comdat group");
3551 error_found = true;
3553 if (!inlined_to && calls_comdat_local != check_calls_comdat_local_p ())
3555 error ("invalid calls_comdat_local flag");
3556 error_found = true;
3559 if (DECL_IS_MALLOC (decl)
3560 && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3562 error ("malloc attribute should be used for a function that "
3563 "returns a pointer");
3564 error_found = true;
3566 if (definition
3567 && externally_visible
3568 /* For aliases in lto1 free_lang_data doesn't guarantee preservation
3569 of opt_for_fn (decl, flag_semantic_interposition). See PR105399. */
3570 && (!alias || !in_lto_p)
3571 && semantic_interposition
3572 != opt_for_fn (decl, flag_semantic_interposition))
3574 error ("semantic interposition mismatch");
3575 error_found = true;
3577 for (e = indirect_calls; e; e = e->next_callee)
3579 if (e->aux)
3581 error ("aux field set for indirect edge from %s",
3582 identifier_to_locale (e->caller->name ()));
3583 error_found = true;
3585 if (!e->count.compatible_p (count))
3587 error ("edge count is not compatible with function count");
3588 e->count.debug ();
3589 count.debug ();
3590 error_found = true;
3592 if (!e->indirect_unknown_callee
3593 || !e->indirect_info)
3595 error ("An indirect edge from %s is not marked as indirect or has "
3596 "associated indirect_info, the corresponding statement is: ",
3597 identifier_to_locale (e->caller->name ()));
3598 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3599 error_found = true;
3601 if (e->call_stmt && e->lto_stmt_uid)
3603 error ("edge has both call_stmt and lto_stmt_uid set");
3604 error_found = true;
3607 bool check_comdat = comdat_local_p ();
3608 for (e = callers; e; e = e->next_caller)
3610 if (e->verify_count ())
3611 error_found = true;
3612 if (check_comdat
3613 && !in_same_comdat_group_p (e->caller))
3615 error ("comdat-local function called by %s outside its comdat",
3616 identifier_to_locale (e->caller->name ()));
3617 error_found = true;
3619 if (!e->inline_failed)
3621 if (inlined_to
3622 != (e->caller->inlined_to
3623 ? e->caller->inlined_to : e->caller))
3625 error ("inlined_to pointer is wrong");
3626 error_found = true;
3628 if (callers->next_caller)
3630 error ("multiple inline callers");
3631 error_found = true;
3634 else
3635 if (inlined_to)
3637 error ("inlined_to pointer set for noninline callers");
3638 error_found = true;
3641 for (e = callees; e; e = e->next_callee)
3643 if (e->verify_count ())
3644 error_found = true;
3645 if (!e->count.compatible_p (count))
3647 error ("edge count is not compatible with function count");
3648 e->count.debug ();
3649 count.debug ();
3650 error_found = true;
3652 if (gimple_has_body_p (e->caller->decl)
3653 && !e->caller->inlined_to
3654 && !e->speculative
3655 /* Optimized out calls are redirected to __builtin_unreachable. */
3656 && (e->count.nonzero_p ()
3657 || ! e->callee->decl
3658 || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE,
3659 BUILT_IN_UNREACHABLE_TRAP))
3660 && count
3661 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3662 && (!e->count.ipa_p ()
3663 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3665 error ("caller edge count does not match BB count");
3666 fprintf (stderr, "edge count: ");
3667 e->count.dump (stderr);
3668 fprintf (stderr, "\n bb count: ");
3669 gimple_bb (e->call_stmt)->count.dump (stderr);
3670 fprintf (stderr, "\n");
3671 error_found = true;
3673 if (e->call_stmt && e->lto_stmt_uid)
3675 error ("edge has both call_stmt and lto_stmt_uid set");
3676 error_found = true;
3678 if (e->speculative
3679 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3680 NULL))
3681 error_found = true;
3683 for (e = indirect_calls; e; e = e->next_callee)
3685 if (e->verify_count ())
3686 error_found = true;
3687 if (gimple_has_body_p (e->caller->decl)
3688 && !e->caller->inlined_to
3689 && !e->speculative
3690 && e->count.ipa_p ()
3691 && count
3692 == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3693 && (!e->count.ipa_p ()
3694 && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3696 error ("indirect call count does not match BB count");
3697 fprintf (stderr, "edge count: ");
3698 e->count.dump (stderr);
3699 fprintf (stderr, "\n bb count: ");
3700 gimple_bb (e->call_stmt)->count.dump (stderr);
3701 fprintf (stderr, "\n");
3702 error_found = true;
3704 if (e->speculative
3705 && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3707 error_found = true;
3709 for (i = 0; iterate_reference (i, ref); i++)
3711 if (ref->stmt && ref->lto_stmt_uid)
3713 error ("reference has both stmt and lto_stmt_uid set");
3714 error_found = true;
3716 if (ref->speculative
3717 && verify_speculative_call (this, ref->stmt,
3718 ref->lto_stmt_uid, NULL))
3719 error_found = true;
3722 if (!callers && inlined_to)
3724 error ("inlined_to pointer is set but no predecessors found");
3725 error_found = true;
3727 if (inlined_to == this)
3729 error ("inlined_to pointer refers to itself");
3730 error_found = true;
3733 if (clone_of)
3735 cgraph_node *first_clone = clone_of->clones;
3736 if (first_clone != this)
3738 if (prev_sibling_clone->clone_of != clone_of)
3740 error ("cgraph_node has wrong clone_of");
3741 error_found = true;
3745 if (clones)
3747 cgraph_node *n;
3748 for (n = clones; n; n = n->next_sibling_clone)
3749 if (n->clone_of != this)
3750 break;
3751 if (n)
3753 error ("cgraph_node has wrong clone list");
3754 error_found = true;
3757 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3759 error ("cgraph_node is in clone list but it is not clone");
3760 error_found = true;
3762 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3764 error ("cgraph_node has wrong prev_clone pointer");
3765 error_found = true;
3767 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3769 error ("double linked list of clones corrupted");
3770 error_found = true;
3773 if (analyzed && alias)
3775 bool ref_found = false;
3776 int i;
3777 ipa_ref *ref = NULL;
3779 if (callees)
3781 error ("Alias has call edges");
3782 error_found = true;
3784 for (i = 0; iterate_reference (i, ref); i++)
3785 if (ref->use != IPA_REF_ALIAS)
3787 error ("Alias has non-alias reference");
3788 error_found = true;
3790 else if (ref_found)
3792 error ("Alias has more than one alias reference");
3793 error_found = true;
3795 else
3796 ref_found = true;
3797 if (!ref_found)
3799 error ("Analyzed alias has no reference");
3800 error_found = true;
3804 if (analyzed && thunk)
3806 if (!callees)
3808 error ("No edge out of thunk node");
3809 error_found = true;
3811 else if (callees->next_callee)
3813 error ("More than one edge out of thunk node");
3814 error_found = true;
3816 if (gimple_has_body_p (decl) && !inlined_to)
3818 error ("Thunk is not supposed to have body");
3819 error_found = true;
3822 else if (analyzed && gimple_has_body_p (decl)
3823 && !TREE_ASM_WRITTEN (decl)
3824 && (!DECL_EXTERNAL (decl) || inlined_to)
3825 && !flag_wpa)
3827 if ((this_cfun->curr_properties & PROP_assumptions_done) != 0)
3829 else if (this_cfun->cfg)
3831 hash_set<gimple *> stmts;
3833 /* Reach the trees by walking over the CFG, and note the
3834 enclosing basic-blocks in the call edges. */
3835 FOR_EACH_BB_FN (this_block, this_cfun)
3837 for (gsi = gsi_start_phis (this_block);
3838 !gsi_end_p (gsi); gsi_next (&gsi))
3839 stmts.add (gsi_stmt (gsi));
3840 for (gsi = gsi_start_bb (this_block);
3841 !gsi_end_p (gsi);
3842 gsi_next (&gsi))
3844 gimple *stmt = gsi_stmt (gsi);
3845 stmts.add (stmt);
3846 if (is_gimple_call (stmt))
3848 cgraph_edge *e = get_edge (stmt);
3849 tree decl = gimple_call_fndecl (stmt);
3850 if (e)
3852 if (e->aux)
3854 error ("shared call_stmt:");
3855 cgraph_debug_gimple_stmt (this_cfun, stmt);
3856 error_found = true;
3858 if (!e->indirect_unknown_callee)
3860 if (e->verify_corresponds_to_fndecl (decl))
3862 error ("edge points to wrong declaration:");
3863 debug_tree (e->callee->decl);
3864 fprintf (stderr," Instead of:");
3865 debug_tree (decl);
3866 error_found = true;
3869 else if (decl)
3871 error ("an indirect edge with unknown callee "
3872 "corresponding to a call_stmt with "
3873 "a known declaration:");
3874 error_found = true;
3875 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3877 e->aux = (void *)1;
3879 else if (decl)
3881 error ("missing callgraph edge for call stmt:");
3882 cgraph_debug_gimple_stmt (this_cfun, stmt);
3883 error_found = true;
3888 for (i = 0; iterate_reference (i, ref); i++)
3889 if (ref->stmt && !stmts.contains (ref->stmt))
3891 error ("reference to dead statement");
3892 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3893 error_found = true;
3896 else
3897 /* No CFG available?! */
3898 gcc_unreachable ();
3900 for (e = callees; e; e = e->next_callee)
3902 if (!e->aux && !e->speculative)
3904 error ("edge %s->%s has no corresponding call_stmt",
3905 identifier_to_locale (e->caller->name ()),
3906 identifier_to_locale (e->callee->name ()));
3907 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3908 error_found = true;
3910 e->aux = 0;
3912 for (e = indirect_calls; e; e = e->next_callee)
3914 if (!e->aux && !e->speculative)
3916 error ("an indirect edge from %s has no corresponding call_stmt",
3917 identifier_to_locale (e->caller->name ()));
3918 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3919 error_found = true;
3921 e->aux = 0;
3925 if (nested_function_info *info = nested_function_info::get (this))
3927 if (info->nested != NULL)
3929 for (cgraph_node *n = info->nested; n != NULL;
3930 n = next_nested_function (n))
3932 nested_function_info *ninfo = nested_function_info::get (n);
3933 if (ninfo->origin == NULL)
3935 error ("missing origin for a node in a nested list");
3936 error_found = true;
3938 else if (ninfo->origin != this)
3940 error ("origin points to a different parent");
3941 error_found = true;
3942 break;
3946 if (info->next_nested != NULL && info->origin == NULL)
3948 error ("missing origin for a node in a nested list");
3949 error_found = true;
3953 if (error_found)
3955 dump (stderr);
3956 internal_error ("verify_cgraph_node failed");
3958 timevar_pop (TV_CGRAPH_VERIFY);
3961 /* Verify whole cgraph structure. */
3962 DEBUG_FUNCTION void
3963 cgraph_node::verify_cgraph_nodes (void)
3965 cgraph_node *node;
3967 if (seen_error ())
3968 return;
3970 FOR_EACH_FUNCTION (node)
3971 node->verify ();
3974 #if __GNUC__ >= 10
3975 # pragma GCC diagnostic pop
3976 #endif
3978 /* Walk the alias chain to return the function cgraph_node is alias of.
3979 Walk through thunks, too.
3980 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3981 When REF is non-NULL, assume that reference happens in symbol REF
3982 when determining the availability. */
3984 cgraph_node *
3985 cgraph_node::function_symbol (enum availability *availability,
3986 struct symtab_node *ref)
3988 cgraph_node *node = ultimate_alias_target (availability, ref);
3990 while (node->thunk)
3992 enum availability a;
3994 ref = node;
3995 node = node->callees->callee;
3996 node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3997 if (availability && a < *availability)
3998 *availability = a;
4000 return node;
4003 /* Walk the alias chain to return the function cgraph_node is alias of.
4004 Walk through non virtual thunks, too. Thus we return either a function
4005 or a virtual thunk node.
4006 When AVAILABILITY is non-NULL, get minimal availability in the chain.
4007 When REF is non-NULL, assume that reference happens in symbol REF
4008 when determining the availability. */
4010 cgraph_node *
4011 cgraph_node::function_or_virtual_thunk_symbol
4012 (enum availability *availability,
4013 struct symtab_node *ref)
4015 cgraph_node *node = ultimate_alias_target (availability, ref);
4017 while (node->thunk && !thunk_info::get (node)->virtual_offset_p)
4019 enum availability a;
4021 ref = node;
4022 node = node->callees->callee;
4023 node = node->ultimate_alias_target (availability ? &a : NULL, ref);
4024 if (availability && a < *availability)
4025 *availability = a;
4027 return node;
4030 /* When doing LTO, read cgraph_node's body from disk if it is not already
4031 present. Also perform any necessary clone materializations. */
4033 bool
4034 cgraph_node::get_untransformed_body ()
4036 lto_file_decl_data *file_data;
4037 const char *data, *name;
4038 size_t len;
4039 tree decl = this->decl;
4041 /* See if there is clone to be materialized.
4042 (inline clones does not need materialization, but we can be seeing
4043 an inline clone of real clone). */
4044 cgraph_node *p = this;
4045 for (cgraph_node *c = clone_of; c; c = c->clone_of)
4047 if (c->decl != decl)
4048 p->materialize_clone ();
4049 p = c;
4052 /* Check if body is already there. Either we have gimple body or
4053 the function is thunk and in that case we set DECL_ARGUMENTS. */
4054 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
4055 return false;
4057 gcc_assert (in_lto_p && !DECL_RESULT (decl));
4059 timevar_push (TV_IPA_LTO_GIMPLE_IN);
4061 file_data = lto_file_data;
4062 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4064 /* We may have renamed the declaration, e.g., a static function. */
4065 name = lto_get_decl_name_mapping (file_data, name);
4066 struct lto_in_decl_state *decl_state
4067 = lto_get_function_in_decl_state (file_data, decl);
4069 cgraph_node *origin = this;
4070 while (origin->clone_of)
4071 origin = origin->clone_of;
4073 int stream_order = origin->order - file_data->order_base;
4074 data = lto_get_section_data (file_data, LTO_section_function_body,
4075 name, stream_order, &len,
4076 decl_state->compressed);
4077 if (!data)
4078 fatal_error (input_location, "%s: section %s.%d is missing",
4079 file_data->file_name, name, stream_order);
4081 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
4083 if (!quiet_flag)
4084 fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4085 lto_input_function_body (file_data, this, data);
4086 lto_stats.num_function_bodies++;
4087 lto_free_section_data (file_data, LTO_section_function_body, name,
4088 data, len, decl_state->compressed);
4089 lto_free_function_in_decl_state_for_node (this);
4090 /* Keep lto file data so ipa-inline-analysis knows about cross module
4091 inlining. */
4093 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
4095 return true;
4098 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
4099 if it is not already present. When some IPA transformations are scheduled,
4100 apply them. */
4102 bool
4103 cgraph_node::get_body (void)
4105 bool updated;
4107 updated = get_untransformed_body ();
4109 /* Getting transformed body makes no sense for inline clones;
4110 we should never use this on real clones because they are materialized
4111 early.
4112 TODO: Materializing clones here will likely lead to smaller LTRANS
4113 footprint. */
4114 gcc_assert (!inlined_to && !clone_of);
4115 if (ipa_transforms_to_apply.exists ())
4117 opt_pass *saved_current_pass = current_pass;
4118 FILE *saved_dump_file = dump_file;
4119 const char *saved_dump_file_name = dump_file_name;
4120 dump_flags_t saved_dump_flags = dump_flags;
4121 dump_file_name = NULL;
4122 set_dump_file (NULL);
4124 push_cfun (DECL_STRUCT_FUNCTION (decl));
4126 update_ssa (TODO_update_ssa_only_virtuals);
4127 execute_all_ipa_transforms (true);
4128 cgraph_edge::rebuild_edges ();
4129 free_dominance_info (CDI_DOMINATORS);
4130 free_dominance_info (CDI_POST_DOMINATORS);
4131 pop_cfun ();
4132 updated = true;
4134 current_pass = saved_current_pass;
4135 set_dump_file (saved_dump_file);
4136 dump_file_name = saved_dump_file_name;
4137 dump_flags = saved_dump_flags;
4139 return updated;
4142 /* Return the DECL_STRUCT_FUNCTION of the function. */
4144 struct function *
4145 cgraph_node::get_fun () const
4147 const cgraph_node *node = this;
4148 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
4150 while (!fun && node->clone_of)
4152 node = node->clone_of;
4153 fun = DECL_STRUCT_FUNCTION (node->decl);
4156 return fun;
4159 /* Reset all state within cgraph.cc so that we can rerun the compiler
4160 within the same process. For use by toplev::finalize. */
4162 void
4163 cgraph_cc_finalize (void)
4165 nested_function_info::release ();
4166 thunk_info::release ();
4167 clone_info::release ();
4168 symtab = NULL;
4170 x_cgraph_nodes_queue = NULL;
4172 cgraph_fnver_htab = NULL;
4173 version_info_node = NULL;
4176 /* A worker for call_for_symbol_and_aliases. */
4178 bool
4179 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
4180 void *),
4181 void *data,
4182 bool include_overwritable)
4184 ipa_ref *ref;
4185 FOR_EACH_ALIAS (this, ref)
4187 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
4188 if (include_overwritable
4189 || alias->get_availability () > AVAIL_INTERPOSABLE)
4190 if (alias->call_for_symbol_and_aliases (callback, data,
4191 include_overwritable))
4192 return true;
4194 return false;
4197 /* Return true if NODE has thunk. */
4199 bool
4200 cgraph_node::has_thunk_p (cgraph_node *node, void *)
4202 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
4203 if (e->caller->thunk)
4204 return true;
4205 return false;
4208 /* Expected frequency of executions within the function. */
4210 sreal
4211 cgraph_edge::sreal_frequency ()
4213 return count.to_sreal_scale (caller->inlined_to
4214 ? caller->inlined_to->count
4215 : caller->count);
4219 /* During LTO stream in this can be used to check whether call can possibly
4220 be internal to the current translation unit. */
4222 bool
4223 cgraph_edge::possibly_call_in_translation_unit_p (void)
4225 gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4227 /* While incremental linking we may end up getting function body later. */
4228 if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4229 return true;
4231 /* We may be smarter here and avoid streaming in indirect calls we can't
4232 track, but that would require arranging streaming the indirect call
4233 summary first. */
4234 if (!callee)
4235 return true;
4237 /* If callee is local to the original translation unit, it will be
4238 defined. */
4239 if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4240 return true;
4242 /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4243 yet) and see if it is a definition. In fact we may also resolve aliases,
4244 but that is probably not too important. */
4245 symtab_node *node = callee;
4246 for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4247 node = node->previous_sharing_asm_name;
4248 if (node->previous_sharing_asm_name)
4249 node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4250 gcc_assert (TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl));
4251 return node->get_availability () >= AVAIL_INTERPOSABLE;
4254 /* Return num_speculative_targets of this edge. */
4257 cgraph_edge::num_speculative_call_targets_p (void)
4259 return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4262 /* Check if function calls comdat local. This is used to recompute
4263 calls_comdat_local flag after function transformations. */
4264 bool
4265 cgraph_node::check_calls_comdat_local_p ()
4267 for (cgraph_edge *e = callees; e; e = e->next_callee)
4268 if (e->inline_failed
4269 ? e->callee->comdat_local_p ()
4270 : e->callee->check_calls_comdat_local_p ())
4271 return true;
4272 return false;
4275 /* Return true if this node represents a former, i.e. an expanded, thunk. */
4277 bool
4278 cgraph_node::former_thunk_p (void)
4280 if (thunk)
4281 return false;
4282 thunk_info *i = thunk_info::get (this);
4283 if (!i)
4284 return false;
4285 gcc_checking_assert (i->fixed_offset || i->virtual_offset_p
4286 || i->indirect_offset);
4287 return true;
4290 /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4291 This needs to be a global so that it can be a GC root, and thus
4292 prevent the stashed copy from being garbage-collected if the GC runs
4293 during a symbol_table_test. */
4295 symbol_table *saved_symtab;
4297 #if CHECKING_P
4299 namespace selftest {
4301 /* class selftest::symbol_table_test. */
4303 /* Constructor. Store the old value of symtab, and create a new one. */
4305 symbol_table_test::symbol_table_test ()
4307 gcc_assert (saved_symtab == NULL);
4308 saved_symtab = symtab;
4309 symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4312 /* Destructor. Restore the old value of symtab. */
4314 symbol_table_test::~symbol_table_test ()
4316 gcc_assert (saved_symtab != NULL);
4317 symtab = saved_symtab;
4318 saved_symtab = NULL;
4321 /* Verify that symbol_table_test works. */
4323 static void
4324 test_symbol_table_test ()
4326 /* Simulate running two selftests involving symbol tables. */
4327 for (int i = 0; i < 2; i++)
4329 symbol_table_test stt;
4330 tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4331 get_identifier ("test_decl"),
4332 build_function_type_list (void_type_node,
4333 NULL_TREE));
4334 cgraph_node *node = cgraph_node::get_create (test_decl);
4335 gcc_assert (node);
4337 /* Verify that the node has order 0 on both iterations,
4338 and thus that nodes have predictable dump names in selftests. */
4339 ASSERT_EQ (node->order, 0);
4340 ASSERT_STREQ (node->dump_name (), "test_decl/0");
4344 /* Run all of the selftests within this file. */
4346 void
4347 cgraph_cc_tests ()
4349 test_symbol_table_test ();
4352 } // namespace selftest
4354 #endif /* CHECKING_P */
4356 #include "gt-cgraph.h"