Daily bump.
[official-gcc.git] / gcc / cgraph.c
blobd7c9ba617955122de611c43a8dbadc0280be580a
1 /* Callgraph handling code.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for inter-procedural
24 optimization. It represents a multi-graph where nodes are functions
25 (symbols within symbol table) and edges are call sites. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "backend.h"
31 #include "target.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "gimple.h"
35 #include "predict.h"
36 #include "alloc-pool.h"
37 #include "gimple-ssa.h"
38 #include "cgraph.h"
39 #include "lto-streamer.h"
40 #include "fold-const.h"
41 #include "varasm.h"
42 #include "calls.h"
43 #include "print-tree.h"
44 #include "langhooks.h"
45 #include "intl.h"
46 #include "tree-eh.h"
47 #include "gimple-iterator.h"
48 #include "tree-cfg.h"
49 #include "tree-ssa.h"
50 #include "value-prof.h"
51 #include "ipa-utils.h"
52 #include "symbol-summary.h"
53 #include "tree-vrp.h"
54 #include "ipa-prop.h"
55 #include "ipa-fnsummary.h"
56 #include "cfgloop.h"
57 #include "gimple-pretty-print.h"
58 #include "tree-dfa.h"
59 #include "profile.h"
60 #include "params.h"
61 #include "tree-chkp.h"
62 #include "context.h"
63 #include "gimplify.h"
65 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
66 #include "tree-pass.h"
68 /* Queue of cgraph nodes scheduled to be lowered. */
69 symtab_node *x_cgraph_nodes_queue;
70 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
72 /* Symbol table global context. */
73 symbol_table *symtab;
75 /* List of hooks triggered on cgraph_edge events. */
76 struct cgraph_edge_hook_list {
77 cgraph_edge_hook hook;
78 void *data;
79 struct cgraph_edge_hook_list *next;
82 /* List of hooks triggered on cgraph_node events. */
83 struct cgraph_node_hook_list {
84 cgraph_node_hook hook;
85 void *data;
86 struct cgraph_node_hook_list *next;
89 /* List of hooks triggered on events involving two cgraph_edges. */
90 struct cgraph_2edge_hook_list {
91 cgraph_2edge_hook hook;
92 void *data;
93 struct cgraph_2edge_hook_list *next;
96 /* List of hooks triggered on events involving two cgraph_nodes. */
97 struct cgraph_2node_hook_list {
98 cgraph_2node_hook hook;
99 void *data;
100 struct cgraph_2node_hook_list *next;
103 /* Hash descriptor for cgraph_function_version_info. */
105 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
107 static hashval_t hash (cgraph_function_version_info *);
108 static bool equal (cgraph_function_version_info *,
109 cgraph_function_version_info *);
112 /* Map a cgraph_node to cgraph_function_version_info using this htab.
113 The cgraph_function_version_info has a THIS_NODE field that is the
114 corresponding cgraph_node.. */
116 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
118 /* Hash function for cgraph_fnver_htab. */
119 hashval_t
120 function_version_hasher::hash (cgraph_function_version_info *ptr)
122 int uid = ptr->this_node->uid;
123 return (hashval_t)(uid);
126 /* eq function for cgraph_fnver_htab. */
127 bool
128 function_version_hasher::equal (cgraph_function_version_info *n1,
129 cgraph_function_version_info *n2)
131 return n1->this_node->uid == n2->this_node->uid;
134 /* Mark as GC root all allocated nodes. */
135 static GTY(()) struct cgraph_function_version_info *
136 version_info_node = NULL;
138 /* Return true if NODE's address can be compared. */
140 bool
141 symtab_node::address_can_be_compared_p ()
143 /* Address of virtual tables and functions is never compared. */
144 if (DECL_VIRTUAL_P (decl))
145 return false;
146 /* Address of C++ cdtors is never compared. */
147 if (is_a <cgraph_node *> (this)
148 && (DECL_CXX_CONSTRUCTOR_P (decl)
149 || DECL_CXX_DESTRUCTOR_P (decl)))
150 return false;
151 /* Constant pool symbols addresses are never compared.
152 flag_merge_constants permits us to assume the same on readonly vars. */
153 if (is_a <varpool_node *> (this)
154 && (DECL_IN_CONSTANT_POOL (decl)
155 || (flag_merge_constants >= 2
156 && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
157 return false;
158 return true;
161 /* Get the cgraph_function_version_info node corresponding to node. */
162 cgraph_function_version_info *
163 cgraph_node::function_version (void)
165 cgraph_function_version_info key;
166 key.this_node = this;
168 if (cgraph_fnver_htab == NULL)
169 return NULL;
171 return cgraph_fnver_htab->find (&key);
174 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
175 corresponding to cgraph_node NODE. */
176 cgraph_function_version_info *
177 cgraph_node::insert_new_function_version (void)
179 version_info_node = NULL;
180 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
181 version_info_node->this_node = this;
183 if (cgraph_fnver_htab == NULL)
184 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
186 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
187 = version_info_node;
188 return version_info_node;
191 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
192 DECL is a duplicate declaration. */
193 void
194 cgraph_node::delete_function_version (tree decl)
196 cgraph_node *decl_node = cgraph_node::get (decl);
197 cgraph_function_version_info *decl_v = NULL;
199 if (decl_node == NULL)
200 return;
202 decl_v = decl_node->function_version ();
204 if (decl_v == NULL)
205 return;
207 if (decl_v->prev != NULL)
208 decl_v->prev->next = decl_v->next;
210 if (decl_v->next != NULL)
211 decl_v->next->prev = decl_v->prev;
213 if (cgraph_fnver_htab != NULL)
214 cgraph_fnver_htab->remove_elt (decl_v);
216 decl_node->remove ();
219 /* Record that DECL1 and DECL2 are semantically identical function
220 versions. */
221 void
222 cgraph_node::record_function_versions (tree decl1, tree decl2)
224 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
225 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
226 cgraph_function_version_info *decl1_v = NULL;
227 cgraph_function_version_info *decl2_v = NULL;
228 cgraph_function_version_info *before;
229 cgraph_function_version_info *after;
231 gcc_assert (decl1_node != NULL && decl2_node != NULL);
232 decl1_v = decl1_node->function_version ();
233 decl2_v = decl2_node->function_version ();
235 if (decl1_v != NULL && decl2_v != NULL)
236 return;
238 if (decl1_v == NULL)
239 decl1_v = decl1_node->insert_new_function_version ();
241 if (decl2_v == NULL)
242 decl2_v = decl2_node->insert_new_function_version ();
244 /* Chain decl2_v and decl1_v. All semantically identical versions
245 will be chained together. */
247 before = decl1_v;
248 after = decl2_v;
250 while (before->next != NULL)
251 before = before->next;
253 while (after->prev != NULL)
254 after= after->prev;
256 before->next = after;
257 after->prev = before;
260 /* Initialize callgraph dump file. */
262 void
263 symbol_table::initialize (void)
265 if (!dump_file)
266 dump_file = dump_begin (TDI_cgraph, NULL);
268 if (!ipa_clones_dump_file)
269 ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
272 /* Allocate new callgraph node and insert it into basic data structures. */
274 cgraph_node *
275 symbol_table::create_empty (void)
277 cgraph_node *node = allocate_cgraph_symbol ();
279 node->type = SYMTAB_FUNCTION;
280 node->frequency = NODE_FREQUENCY_NORMAL;
281 node->count_materialization_scale = REG_BR_PROB_BASE;
282 cgraph_count++;
284 return node;
287 /* Register HOOK to be called with DATA on each removed edge. */
288 cgraph_edge_hook_list *
289 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
291 cgraph_edge_hook_list *entry;
292 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
294 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
295 entry->hook = hook;
296 entry->data = data;
297 entry->next = NULL;
298 while (*ptr)
299 ptr = &(*ptr)->next;
300 *ptr = entry;
301 return entry;
304 /* Remove ENTRY from the list of hooks called on removing edges. */
305 void
306 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
308 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
310 while (*ptr != entry)
311 ptr = &(*ptr)->next;
312 *ptr = entry->next;
313 free (entry);
316 /* Call all edge removal hooks. */
317 void
318 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
320 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
321 while (entry)
323 entry->hook (e, entry->data);
324 entry = entry->next;
328 /* Register HOOK to be called with DATA on each removed node. */
329 cgraph_node_hook_list *
330 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
332 cgraph_node_hook_list *entry;
333 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
335 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
336 entry->hook = hook;
337 entry->data = data;
338 entry->next = NULL;
339 while (*ptr)
340 ptr = &(*ptr)->next;
341 *ptr = entry;
342 return entry;
345 /* Remove ENTRY from the list of hooks called on removing nodes. */
346 void
347 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
349 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
351 while (*ptr != entry)
352 ptr = &(*ptr)->next;
353 *ptr = entry->next;
354 free (entry);
357 /* Call all node removal hooks. */
358 void
359 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
361 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
362 while (entry)
364 entry->hook (node, entry->data);
365 entry = entry->next;
369 /* Call all node removal hooks. */
370 void
371 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
373 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
374 while (entry)
376 entry->hook (node, entry->data);
377 entry = entry->next;
382 /* Register HOOK to be called with DATA on each inserted node. */
383 cgraph_node_hook_list *
384 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
386 cgraph_node_hook_list *entry;
387 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
389 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
390 entry->hook = hook;
391 entry->data = data;
392 entry->next = NULL;
393 while (*ptr)
394 ptr = &(*ptr)->next;
395 *ptr = entry;
396 return entry;
399 /* Remove ENTRY from the list of hooks called on inserted nodes. */
400 void
401 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
403 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
405 while (*ptr != entry)
406 ptr = &(*ptr)->next;
407 *ptr = entry->next;
408 free (entry);
411 /* Register HOOK to be called with DATA on each duplicated edge. */
412 cgraph_2edge_hook_list *
413 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
415 cgraph_2edge_hook_list *entry;
416 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
418 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
419 entry->hook = hook;
420 entry->data = data;
421 entry->next = NULL;
422 while (*ptr)
423 ptr = &(*ptr)->next;
424 *ptr = entry;
425 return entry;
428 /* Remove ENTRY from the list of hooks called on duplicating edges. */
429 void
430 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
432 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
434 while (*ptr != entry)
435 ptr = &(*ptr)->next;
436 *ptr = entry->next;
437 free (entry);
440 /* Call all edge duplication hooks. */
441 void
442 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
444 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
445 while (entry)
447 entry->hook (cs1, cs2, entry->data);
448 entry = entry->next;
452 /* Register HOOK to be called with DATA on each duplicated node. */
453 cgraph_2node_hook_list *
454 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
456 cgraph_2node_hook_list *entry;
457 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
459 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
460 entry->hook = hook;
461 entry->data = data;
462 entry->next = NULL;
463 while (*ptr)
464 ptr = &(*ptr)->next;
465 *ptr = entry;
466 return entry;
469 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
470 void
471 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
473 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
475 while (*ptr != entry)
476 ptr = &(*ptr)->next;
477 *ptr = entry->next;
478 free (entry);
481 /* Call all node duplication hooks. */
482 void
483 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
484 cgraph_node *node2)
486 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
487 while (entry)
489 entry->hook (node, node2, entry->data);
490 entry = entry->next;
494 /* Return cgraph node assigned to DECL. Create new one when needed. */
496 cgraph_node *
497 cgraph_node::create (tree decl)
499 cgraph_node *node = symtab->create_empty ();
500 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
502 node->decl = decl;
504 node->count = profile_count::uninitialized ();
506 if ((flag_openacc || flag_openmp)
507 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
509 node->offloadable = 1;
510 if (ENABLE_OFFLOADING)
511 g->have_offload = true;
514 node->register_symbol ();
516 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
518 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
519 node->next_nested = node->origin->nested;
520 node->origin->nested = node;
522 return node;
525 /* Try to find a call graph node for declaration DECL and if it does not exist
526 or if it corresponds to an inline clone, create a new one. */
528 cgraph_node *
529 cgraph_node::get_create (tree decl)
531 cgraph_node *first_clone = cgraph_node::get (decl);
533 if (first_clone && !first_clone->global.inlined_to)
534 return first_clone;
536 cgraph_node *node = cgraph_node::create (decl);
537 if (first_clone)
539 first_clone->clone_of = node;
540 node->clones = first_clone;
541 symtab->symtab_prevail_in_asm_name_hash (node);
542 node->decl->decl_with_vis.symtab_node = node;
543 if (dump_file)
544 fprintf (dump_file, "Introduced new external node "
545 "(%s) and turned into root of the clone tree.\n",
546 node->dump_name ());
548 else if (dump_file)
549 fprintf (dump_file, "Introduced new external node "
550 "(%s).\n", node->dump_name ());
551 return node;
554 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
555 the function body is associated with (not necessarily cgraph_node (DECL). */
557 cgraph_node *
558 cgraph_node::create_alias (tree alias, tree target)
560 cgraph_node *alias_node;
562 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
563 || TREE_CODE (target) == IDENTIFIER_NODE);
564 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
565 alias_node = cgraph_node::get_create (alias);
566 gcc_assert (!alias_node->definition);
567 alias_node->alias_target = target;
568 alias_node->definition = true;
569 alias_node->alias = true;
570 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
571 alias_node->transparent_alias = alias_node->weakref = true;
572 return alias_node;
575 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
576 and NULL otherwise.
577 Same body aliases are output whenever the body of DECL is output,
578 and cgraph_node::get (ALIAS) transparently returns
579 cgraph_node::get (DECL). */
581 cgraph_node *
582 cgraph_node::create_same_body_alias (tree alias, tree decl)
584 cgraph_node *n;
585 #ifndef ASM_OUTPUT_DEF
586 /* If aliases aren't supported by the assembler, fail. */
587 return NULL;
588 #endif
589 /* Langhooks can create same body aliases of symbols not defined.
590 Those are useless. Drop them on the floor. */
591 if (symtab->global_info_ready)
592 return NULL;
594 n = cgraph_node::create_alias (alias, decl);
595 n->cpp_implicit_alias = true;
596 if (symtab->cpp_implicit_aliases_done)
597 n->resolve_alias (cgraph_node::get (decl));
598 return n;
601 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
602 aliases DECL with an adjustments made into the first parameter.
603 See comments in thunk_adjust for detail on the parameters. */
605 cgraph_node *
606 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
607 HOST_WIDE_INT fixed_offset,
608 HOST_WIDE_INT virtual_value,
609 tree virtual_offset,
610 tree real_alias)
612 cgraph_node *node;
614 node = cgraph_node::get (alias);
615 if (node)
616 node->reset ();
617 else
618 node = cgraph_node::create (alias);
619 gcc_checking_assert (!virtual_offset
620 || wi::eq_p (virtual_offset, virtual_value));
621 node->thunk.fixed_offset = fixed_offset;
622 node->thunk.this_adjusting = this_adjusting;
623 node->thunk.virtual_value = virtual_value;
624 node->thunk.virtual_offset_p = virtual_offset != NULL;
625 node->thunk.alias = real_alias;
626 node->thunk.thunk_p = true;
627 node->definition = true;
629 return node;
632 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
633 Return NULL if there's no such node. */
635 cgraph_node *
636 cgraph_node::get_for_asmname (tree asmname)
638 /* We do not want to look at inline clones. */
639 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
640 node;
641 node = node->next_sharing_asm_name)
643 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
644 if (cn && !cn->global.inlined_to)
645 return cn;
647 return NULL;
650 /* Returns a hash value for X (which really is a cgraph_edge). */
652 hashval_t
653 cgraph_edge_hasher::hash (cgraph_edge *e)
655 /* This is a really poor hash function, but it is what htab_hash_pointer
656 uses. */
657 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
660 /* Returns a hash value for X (which really is a cgraph_edge). */
662 hashval_t
663 cgraph_edge_hasher::hash (gimple *call_stmt)
665 /* This is a really poor hash function, but it is what htab_hash_pointer
666 uses. */
667 return (hashval_t) ((intptr_t)call_stmt >> 3);
670 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y. */
672 inline bool
673 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
675 return x->call_stmt == y;
678 /* Add call graph edge E to call site hash of its caller. */
680 static inline void
681 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
683 gimple *call = e->call_stmt;
684 *e->caller->call_site_hash->find_slot_with_hash
685 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
688 /* Add call graph edge E to call site hash of its caller. */
690 static inline void
691 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
693 /* There are two speculative edges for every statement (one direct,
694 one indirect); always hash the direct one. */
695 if (e->speculative && e->indirect_unknown_callee)
696 return;
697 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
698 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
699 if (*slot)
701 gcc_assert (((cgraph_edge *)*slot)->speculative);
702 if (e->callee)
703 *slot = e;
704 return;
706 gcc_assert (!*slot || e->speculative);
707 *slot = e;
710 /* Return the callgraph edge representing the GIMPLE_CALL statement
711 CALL_STMT. */
713 cgraph_edge *
714 cgraph_node::get_edge (gimple *call_stmt)
716 cgraph_edge *e, *e2;
717 int n = 0;
719 if (call_site_hash)
720 return call_site_hash->find_with_hash
721 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
723 /* This loop may turn out to be performance problem. In such case adding
724 hashtables into call nodes with very many edges is probably best
725 solution. It is not good idea to add pointer into CALL_EXPR itself
726 because we want to make possible having multiple cgraph nodes representing
727 different clones of the same body before the body is actually cloned. */
728 for (e = callees; e; e = e->next_callee)
730 if (e->call_stmt == call_stmt)
731 break;
732 n++;
735 if (!e)
736 for (e = indirect_calls; e; e = e->next_callee)
738 if (e->call_stmt == call_stmt)
739 break;
740 n++;
743 if (n > 100)
745 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
746 for (e2 = callees; e2; e2 = e2->next_callee)
747 cgraph_add_edge_to_call_site_hash (e2);
748 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
749 cgraph_add_edge_to_call_site_hash (e2);
752 return e;
756 /* Change field call_stmt of edge to NEW_STMT.
757 If UPDATE_SPECULATIVE and E is any component of speculative
758 edge, then update all components. */
760 void
761 cgraph_edge::set_call_stmt (gcall *new_stmt, bool update_speculative)
763 tree decl;
765 /* Speculative edges has three component, update all of them
766 when asked to. */
767 if (update_speculative && speculative)
769 cgraph_edge *direct, *indirect;
770 ipa_ref *ref;
772 speculative_call_info (direct, indirect, ref);
773 direct->set_call_stmt (new_stmt, false);
774 indirect->set_call_stmt (new_stmt, false);
775 ref->stmt = new_stmt;
776 return;
779 /* Only direct speculative edges go to call_site_hash. */
780 if (caller->call_site_hash
781 && (!speculative || !indirect_unknown_callee))
783 caller->call_site_hash->remove_elt_with_hash
784 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
787 cgraph_edge *e = this;
789 call_stmt = new_stmt;
790 if (indirect_unknown_callee
791 && (decl = gimple_call_fndecl (new_stmt)))
793 /* Constant propagation (and possibly also inlining?) can turn an
794 indirect call into a direct one. */
795 cgraph_node *new_callee = cgraph_node::get (decl);
797 gcc_checking_assert (new_callee);
798 e = make_direct (new_callee);
801 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
802 e->can_throw_external = stmt_can_throw_external (new_stmt);
803 pop_cfun ();
804 if (e->caller->call_site_hash)
805 cgraph_add_edge_to_call_site_hash (e);
808 /* Allocate a cgraph_edge structure and fill it with data according to the
809 parameters of which only CALLEE can be NULL (when creating an indirect call
810 edge). */
812 cgraph_edge *
813 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
814 gcall *call_stmt, profile_count count, int freq,
815 bool indir_unknown_callee)
817 cgraph_edge *edge;
819 /* LTO does not actually have access to the call_stmt since these
820 have not been loaded yet. */
821 if (call_stmt)
823 /* This is a rather expensive check possibly triggering
824 construction of call stmt hashtable. */
825 cgraph_edge *e;
826 gcc_checking_assert (!(e = caller->get_edge (call_stmt))
827 || e->speculative);
829 gcc_assert (is_gimple_call (call_stmt));
832 if (free_edges)
834 edge = free_edges;
835 free_edges = NEXT_FREE_EDGE (edge);
837 else
839 edge = ggc_alloc<cgraph_edge> ();
840 edge->uid = edges_max_uid++;
843 edges_count++;
845 edge->aux = NULL;
846 edge->caller = caller;
847 edge->callee = callee;
848 edge->prev_caller = NULL;
849 edge->next_caller = NULL;
850 edge->prev_callee = NULL;
851 edge->next_callee = NULL;
852 edge->lto_stmt_uid = 0;
854 edge->count = count;
855 edge->frequency = freq;
856 gcc_checking_assert (freq >= 0);
857 gcc_checking_assert (freq <= CGRAPH_FREQ_MAX);
859 edge->call_stmt = call_stmt;
860 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
861 edge->can_throw_external
862 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
863 pop_cfun ();
864 if (call_stmt
865 && callee && callee->decl
866 && !gimple_check_call_matching_types (call_stmt, callee->decl,
867 false))
869 edge->inline_failed = CIF_MISMATCHED_ARGUMENTS;
870 edge->call_stmt_cannot_inline_p = true;
872 else
874 edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
875 edge->call_stmt_cannot_inline_p = false;
878 edge->indirect_info = NULL;
879 edge->indirect_inlining_edge = 0;
880 edge->speculative = false;
881 edge->indirect_unknown_callee = indir_unknown_callee;
882 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
883 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
884 edge->in_polymorphic_cdtor
885 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
886 caller->decl);
887 else
888 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
889 if (call_stmt && caller->call_site_hash)
890 cgraph_add_edge_to_call_site_hash (edge);
892 return edge;
895 /* Create edge from a given function to CALLEE in the cgraph. */
897 cgraph_edge *
898 cgraph_node::create_edge (cgraph_node *callee,
899 gcall *call_stmt, profile_count count, int freq)
901 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
902 freq, false);
904 initialize_inline_failed (edge);
906 edge->next_caller = callee->callers;
907 if (callee->callers)
908 callee->callers->prev_caller = edge;
909 edge->next_callee = callees;
910 if (callees)
911 callees->prev_callee = edge;
912 callees = edge;
913 callee->callers = edge;
915 return edge;
918 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
920 cgraph_indirect_call_info *
921 cgraph_allocate_init_indirect_info (void)
923 cgraph_indirect_call_info *ii;
925 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
926 ii->param_index = -1;
927 return ii;
930 /* Create an indirect edge with a yet-undetermined callee where the call
931 statement destination is a formal parameter of the caller with index
932 PARAM_INDEX. */
934 cgraph_edge *
935 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
936 profile_count count, int freq,
937 bool compute_indirect_info)
939 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt,
940 count, freq, true);
941 tree target;
943 initialize_inline_failed (edge);
945 edge->indirect_info = cgraph_allocate_init_indirect_info ();
946 edge->indirect_info->ecf_flags = ecf_flags;
947 edge->indirect_info->vptr_changed = true;
949 /* Record polymorphic call info. */
950 if (compute_indirect_info
951 && call_stmt
952 && (target = gimple_call_fn (call_stmt))
953 && virtual_method_call_p (target))
955 ipa_polymorphic_call_context context (decl, target, call_stmt);
957 /* Only record types can have virtual calls. */
958 edge->indirect_info->polymorphic = true;
959 edge->indirect_info->param_index = -1;
960 edge->indirect_info->otr_token
961 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
962 edge->indirect_info->otr_type = obj_type_ref_class (target);
963 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
964 edge->indirect_info->context = context;
967 edge->next_callee = indirect_calls;
968 if (indirect_calls)
969 indirect_calls->prev_callee = edge;
970 indirect_calls = edge;
972 return edge;
975 /* Remove the edge from the list of the callees of the caller. */
977 void
978 cgraph_edge::remove_caller (void)
980 if (prev_callee)
981 prev_callee->next_callee = next_callee;
982 if (next_callee)
983 next_callee->prev_callee = prev_callee;
984 if (!prev_callee)
986 if (indirect_unknown_callee)
987 caller->indirect_calls = next_callee;
988 else
989 caller->callees = next_callee;
991 if (caller->call_site_hash)
992 caller->call_site_hash->remove_elt_with_hash
993 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
996 /* Put the edge onto the free list. */
998 void
999 symbol_table::free_edge (cgraph_edge *e)
1001 int uid = e->uid;
1003 if (e->indirect_info)
1004 ggc_free (e->indirect_info);
1006 /* Clear out the edge so we do not dangle pointers. */
1007 memset (e, 0, sizeof (*e));
1008 e->uid = uid;
1009 NEXT_FREE_EDGE (e) = free_edges;
1010 free_edges = e;
1011 edges_count--;
1014 /* Remove the edge in the cgraph. */
1016 void
1017 cgraph_edge::remove (void)
1019 /* Call all edge removal hooks. */
1020 symtab->call_edge_removal_hooks (this);
1022 if (!indirect_unknown_callee)
1023 /* Remove from callers list of the callee. */
1024 remove_callee ();
1026 /* Remove from callees list of the callers. */
1027 remove_caller ();
1029 /* Put the edge onto the free list. */
1030 symtab->free_edge (this);
1033 /* Turn edge into speculative call calling N2. Update
1034 the profile so the direct call is taken COUNT times
1035 with FREQUENCY.
1037 At clone materialization time, the indirect call E will
1038 be expanded as:
1040 if (call_dest == N2)
1041 n2 ();
1042 else
1043 call call_dest
1045 At this time the function just creates the direct call,
1046 the referencd representing the if conditional and attaches
1047 them all to the orginal indirect call statement.
1049 Return direct edge created. */
1051 cgraph_edge *
1052 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1053 int direct_frequency)
1055 cgraph_node *n = caller;
1056 ipa_ref *ref = NULL;
1057 cgraph_edge *e2;
1059 if (dump_file)
1060 fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1061 n->dump_name (), n2->dump_name ());
1062 speculative = true;
1063 e2 = n->create_edge (n2, call_stmt, direct_count, direct_frequency);
1064 initialize_inline_failed (e2);
1065 e2->speculative = true;
1066 if (TREE_NOTHROW (n2->decl))
1067 e2->can_throw_external = false;
1068 else
1069 e2->can_throw_external = can_throw_external;
1070 e2->lto_stmt_uid = lto_stmt_uid;
1071 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1072 count -= e2->count;
1073 frequency -= e2->frequency;
1074 symtab->call_edge_duplication_hooks (this, e2);
1075 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1076 ref->lto_stmt_uid = lto_stmt_uid;
1077 ref->speculative = speculative;
1078 n2->mark_address_taken ();
1079 return e2;
1082 /* Speculative call consist of three components:
1083 1) an indirect edge representing the original call
1084 2) an direct edge representing the new call
1085 3) ADDR_EXPR reference representing the speculative check.
1086 All three components are attached to single statement (the indirect
1087 call) and if one of them exists, all of them must exist.
1089 Given speculative call edge, return all three components.
1092 void
1093 cgraph_edge::speculative_call_info (cgraph_edge *&direct,
1094 cgraph_edge *&indirect,
1095 ipa_ref *&reference)
1097 ipa_ref *ref;
1098 int i;
1099 cgraph_edge *e2;
1100 cgraph_edge *e = this;
1102 if (!e->indirect_unknown_callee)
1103 for (e2 = e->caller->indirect_calls;
1104 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1105 e2 = e2->next_callee)
1107 else
1109 e2 = e;
1110 /* We can take advantage of the call stmt hash. */
1111 if (e2->call_stmt)
1113 e = e->caller->get_edge (e2->call_stmt);
1114 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1116 else
1117 for (e = e->caller->callees;
1118 e2->call_stmt != e->call_stmt
1119 || e2->lto_stmt_uid != e->lto_stmt_uid;
1120 e = e->next_callee)
1123 gcc_assert (e->speculative && e2->speculative);
1124 direct = e;
1125 indirect = e2;
1127 reference = NULL;
1128 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1129 if (ref->speculative
1130 && ((ref->stmt && ref->stmt == e->call_stmt)
1131 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1133 reference = ref;
1134 break;
1137 /* Speculative edge always consist of all three components - direct edge,
1138 indirect and reference. */
1140 gcc_assert (e && e2 && ref);
1143 /* Speculative call edge turned out to be direct call to CALLE_DECL.
1144 Remove the speculative call sequence and return edge representing the call.
1145 It is up to caller to redirect the call as appropriate. */
1147 cgraph_edge *
1148 cgraph_edge::resolve_speculation (tree callee_decl)
1150 cgraph_edge *edge = this;
1151 cgraph_edge *e2;
1152 ipa_ref *ref;
1154 gcc_assert (edge->speculative);
1155 edge->speculative_call_info (e2, edge, ref);
1156 if (!callee_decl
1157 || !ref->referred->semantically_equivalent_p
1158 (symtab_node::get (callee_decl)))
1160 if (dump_file)
1162 if (callee_decl)
1164 fprintf (dump_file, "Speculative indirect call %s => %s has "
1165 "turned out to have contradicting known target ",
1166 edge->caller->dump_name (),
1167 e2->callee->dump_name ());
1168 print_generic_expr (dump_file, callee_decl);
1169 fprintf (dump_file, "\n");
1171 else
1173 fprintf (dump_file, "Removing speculative call %s => %s\n",
1174 edge->caller->dump_name (),
1175 e2->callee->dump_name ());
1179 else
1181 cgraph_edge *tmp = edge;
1182 if (dump_file)
1183 fprintf (dump_file, "Speculative call turned into direct call.\n");
1184 edge = e2;
1185 e2 = tmp;
1186 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1187 in the functions inlined through it. */
1189 edge->count += e2->count;
1190 edge->frequency += e2->frequency;
1191 if (edge->frequency > CGRAPH_FREQ_MAX)
1192 edge->frequency = CGRAPH_FREQ_MAX;
1193 edge->speculative = false;
1194 e2->speculative = false;
1195 ref->remove_reference ();
1196 if (e2->indirect_unknown_callee || e2->inline_failed)
1197 e2->remove ();
1198 else
1199 e2->callee->remove_symbol_and_inline_clones ();
1200 if (edge->caller->call_site_hash)
1201 cgraph_update_edge_in_call_site_hash (edge);
1202 return edge;
1205 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1206 CALLEE. DELTA is an integer constant that is to be added to the this
1207 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1209 cgraph_edge *
1210 cgraph_edge::make_direct (cgraph_node *callee)
1212 cgraph_edge *edge = this;
1213 gcc_assert (indirect_unknown_callee);
1215 /* If we are redirecting speculative call, make it non-speculative. */
1216 if (indirect_unknown_callee && speculative)
1218 edge = edge->resolve_speculation (callee->decl);
1220 /* On successful speculation just return the pre existing direct edge. */
1221 if (!indirect_unknown_callee)
1222 return edge;
1225 indirect_unknown_callee = 0;
1226 ggc_free (indirect_info);
1227 indirect_info = NULL;
1229 /* Get the edge out of the indirect edge list. */
1230 if (prev_callee)
1231 prev_callee->next_callee = next_callee;
1232 if (next_callee)
1233 next_callee->prev_callee = prev_callee;
1234 if (!prev_callee)
1235 caller->indirect_calls = next_callee;
1237 /* Put it into the normal callee list */
1238 prev_callee = NULL;
1239 next_callee = caller->callees;
1240 if (caller->callees)
1241 caller->callees->prev_callee = edge;
1242 caller->callees = edge;
1244 /* Insert to callers list of the new callee. */
1245 edge->set_callee (callee);
1247 if (call_stmt
1248 && !gimple_check_call_matching_types (call_stmt, callee->decl, false))
1250 call_stmt_cannot_inline_p = true;
1251 inline_failed = CIF_MISMATCHED_ARGUMENTS;
1254 /* We need to re-determine the inlining status of the edge. */
1255 initialize_inline_failed (edge);
1256 return edge;
1259 /* If necessary, change the function declaration in the call statement
1260 associated with E so that it corresponds to the edge callee. */
1262 gimple *
1263 cgraph_edge::redirect_call_stmt_to_callee (void)
1265 cgraph_edge *e = this;
1267 tree decl = gimple_call_fndecl (e->call_stmt);
1268 gcall *new_stmt;
1269 gimple_stmt_iterator gsi;
1270 bool skip_bounds = false;
1272 if (e->speculative)
1274 cgraph_edge *e2;
1275 gcall *new_stmt;
1276 ipa_ref *ref;
1278 e->speculative_call_info (e, e2, ref);
1279 /* If there already is an direct call (i.e. as a result of inliner's
1280 substitution), forget about speculating. */
1281 if (decl)
1282 e = e->resolve_speculation (decl);
1283 /* If types do not match, speculation was likely wrong.
1284 The direct edge was possibly redirected to the clone with a different
1285 signature. We did not update the call statement yet, so compare it
1286 with the reference that still points to the proper type. */
1287 else if (!gimple_check_call_matching_types (e->call_stmt,
1288 ref->referred->decl,
1289 true))
1291 if (dump_file)
1292 fprintf (dump_file, "Not expanding speculative call of %s -> %s\n"
1293 "Type mismatch.\n",
1294 e->caller->dump_name (),
1295 e->callee->dump_name ());
1296 e = e->resolve_speculation ();
1297 /* We are producing the final function body and will throw away the
1298 callgraph edges really soon. Reset the counts/frequencies to
1299 keep verifier happy in the case of roundoff errors. */
1300 e->count = gimple_bb (e->call_stmt)->count;
1301 e->frequency = compute_call_stmt_bb_frequency
1302 (e->caller->decl, gimple_bb (e->call_stmt));
1304 /* Expand speculation into GIMPLE code. */
1305 else
1307 if (dump_file)
1309 fprintf (dump_file,
1310 "Expanding speculative call of %s -> %s count: ",
1311 e->caller->dump_name (),
1312 e->callee->dump_name ());
1313 e->count.dump (dump_file);
1314 fprintf (dump_file, "\n");
1316 gcc_assert (e2->speculative);
1317 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1319 profile_probability prob = e->count.probability_in (e->count
1320 + e2->count);
1321 if (prob.initialized_p ())
1323 else if (e->frequency || e2->frequency)
1324 prob = profile_probability::probability_in_gcov_type
1325 (e->frequency, e->frequency + e2->frequency).guessed ();
1326 else
1327 prob = profile_probability::even ();
1328 new_stmt = gimple_ic (e->call_stmt,
1329 dyn_cast<cgraph_node *> (ref->referred),
1330 prob, e->count, e->count + e2->count);
1331 e->speculative = false;
1332 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1333 false);
1335 /* Fix edges for BUILT_IN_CHKP_BNDRET calls attached to the
1336 processed call stmt. */
1337 if (gimple_call_with_bounds_p (new_stmt)
1338 && gimple_call_lhs (new_stmt)
1339 && chkp_retbnd_call_by_val (gimple_call_lhs (e2->call_stmt)))
1341 tree dresult = gimple_call_lhs (new_stmt);
1342 tree iresult = gimple_call_lhs (e2->call_stmt);
1343 gcall *dbndret = chkp_retbnd_call_by_val (dresult);
1344 gcall *ibndret = chkp_retbnd_call_by_val (iresult);
1345 struct cgraph_edge *iedge
1346 = e2->caller->cgraph_node::get_edge (ibndret);
1347 struct cgraph_edge *dedge;
1349 if (dbndret)
1351 dedge = iedge->caller->create_edge (iedge->callee,
1352 dbndret, e->count,
1353 e->frequency);
1354 dedge->frequency = compute_call_stmt_bb_frequency
1355 (dedge->caller->decl, gimple_bb (dedge->call_stmt));
1357 iedge->frequency = compute_call_stmt_bb_frequency
1358 (iedge->caller->decl, gimple_bb (iedge->call_stmt));
1361 e->frequency = compute_call_stmt_bb_frequency
1362 (e->caller->decl, gimple_bb (e->call_stmt));
1363 e2->frequency = compute_call_stmt_bb_frequency
1364 (e2->caller->decl, gimple_bb (e2->call_stmt));
1365 e2->speculative = false;
1366 ref->speculative = false;
1367 ref->stmt = NULL;
1368 /* Indirect edges are not both in the call site hash.
1369 get it updated. */
1370 if (e->caller->call_site_hash)
1371 cgraph_update_edge_in_call_site_hash (e2);
1372 pop_cfun ();
1373 /* Continue redirecting E to proper target. */
1377 /* We might propagate instrumented function pointer into
1378 not instrumented function and vice versa. In such a
1379 case we need to either fix function declaration or
1380 remove bounds from call statement. */
1381 if (flag_check_pointer_bounds && e->callee)
1382 skip_bounds = chkp_redirect_edge (e);
1384 if (e->indirect_unknown_callee
1385 || (decl == e->callee->decl
1386 && !skip_bounds))
1387 return e->call_stmt;
1389 if (flag_checking && decl)
1391 cgraph_node *node = cgraph_node::get (decl);
1392 gcc_assert (!node || !node->clone.combined_args_to_skip);
1395 if (symtab->dump_file)
1397 fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1398 e->caller->dump_name (), e->callee->dump_name ());
1399 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1400 if (e->callee->clone.combined_args_to_skip)
1402 fprintf (symtab->dump_file, " combined args to skip: ");
1403 dump_bitmap (symtab->dump_file,
1404 e->callee->clone.combined_args_to_skip);
1408 if (e->callee->clone.combined_args_to_skip
1409 || skip_bounds)
1411 int lp_nr;
1413 new_stmt = e->call_stmt;
1414 if (e->callee->clone.combined_args_to_skip)
1415 new_stmt
1416 = gimple_call_copy_skip_args (new_stmt,
1417 e->callee->clone.combined_args_to_skip);
1418 if (skip_bounds)
1419 new_stmt = chkp_copy_call_skip_bounds (new_stmt);
1421 tree old_fntype = gimple_call_fntype (e->call_stmt);
1422 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1423 cgraph_node *origin = e->callee;
1424 while (origin->clone_of)
1425 origin = origin->clone_of;
1427 if ((origin->former_clone_of
1428 && old_fntype == TREE_TYPE (origin->former_clone_of))
1429 || old_fntype == TREE_TYPE (origin->decl))
1430 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1431 else
1433 bitmap skip = e->callee->clone.combined_args_to_skip;
1434 tree t = cgraph_build_function_type_skip_args (old_fntype, skip,
1435 false);
1436 gimple_call_set_fntype (new_stmt, t);
1439 if (gimple_vdef (new_stmt)
1440 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1441 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1443 gsi = gsi_for_stmt (e->call_stmt);
1445 /* For optimized away parameters, add on the caller side
1446 before the call
1447 DEBUG D#X => parm_Y(D)
1448 stmts and associate D#X with parm in decl_debug_args_lookup
1449 vector to say for debug info that if parameter parm had been passed,
1450 it would have value parm_Y(D). */
1451 if (e->callee->clone.combined_args_to_skip && MAY_HAVE_DEBUG_STMTS)
1453 vec<tree, va_gc> **debug_args
1454 = decl_debug_args_lookup (e->callee->decl);
1455 tree old_decl = gimple_call_fndecl (e->call_stmt);
1456 if (debug_args && old_decl)
1458 tree parm;
1459 unsigned i = 0, num;
1460 unsigned len = vec_safe_length (*debug_args);
1461 unsigned nargs = gimple_call_num_args (e->call_stmt);
1462 for (parm = DECL_ARGUMENTS (old_decl), num = 0;
1463 parm && num < nargs;
1464 parm = DECL_CHAIN (parm), num++)
1465 if (bitmap_bit_p (e->callee->clone.combined_args_to_skip, num)
1466 && is_gimple_reg (parm))
1468 unsigned last = i;
1470 while (i < len && (**debug_args)[i] != DECL_ORIGIN (parm))
1471 i += 2;
1472 if (i >= len)
1474 i = 0;
1475 while (i < last
1476 && (**debug_args)[i] != DECL_ORIGIN (parm))
1477 i += 2;
1478 if (i >= last)
1479 continue;
1481 tree ddecl = (**debug_args)[i + 1];
1482 tree arg = gimple_call_arg (e->call_stmt, num);
1483 if (!useless_type_conversion_p (TREE_TYPE (ddecl),
1484 TREE_TYPE (arg)))
1486 tree rhs1;
1487 if (!fold_convertible_p (TREE_TYPE (ddecl), arg))
1488 continue;
1489 if (TREE_CODE (arg) == SSA_NAME
1490 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg))
1491 && (rhs1
1492 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (arg)))
1493 && useless_type_conversion_p (TREE_TYPE (ddecl),
1494 TREE_TYPE (rhs1)))
1495 arg = rhs1;
1496 else
1497 arg = fold_convert (TREE_TYPE (ddecl), arg);
1500 gimple *def_temp
1501 = gimple_build_debug_bind (ddecl, unshare_expr (arg),
1502 e->call_stmt);
1503 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
1508 gsi_replace (&gsi, new_stmt, false);
1509 /* We need to defer cleaning EH info on the new statement to
1510 fixup-cfg. We may not have dominator information at this point
1511 and thus would end up with unreachable blocks and have no way
1512 to communicate that we need to run CFG cleanup then. */
1513 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1514 if (lp_nr != 0)
1516 remove_stmt_from_eh_lp (e->call_stmt);
1517 add_stmt_to_eh_lp (new_stmt, lp_nr);
1520 else
1522 new_stmt = e->call_stmt;
1523 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1524 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1527 /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1528 adjust gimple_call_fntype too. */
1529 if (gimple_call_noreturn_p (new_stmt)
1530 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1531 && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1532 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1533 == void_type_node))
1534 gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1536 /* If the call becomes noreturn, remove the LHS if possible. */
1537 tree lhs = gimple_call_lhs (new_stmt);
1538 if (lhs
1539 && gimple_call_noreturn_p (new_stmt)
1540 && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1541 || should_remove_lhs_p (lhs)))
1543 if (TREE_CODE (lhs) == SSA_NAME)
1545 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1546 TREE_TYPE (lhs), NULL);
1547 var = get_or_create_ssa_default_def
1548 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1549 gimple *set_stmt = gimple_build_assign (lhs, var);
1550 gsi = gsi_for_stmt (new_stmt);
1551 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1552 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1554 gimple_call_set_lhs (new_stmt, NULL_TREE);
1555 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1558 /* If new callee has no static chain, remove it. */
1559 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1561 gimple_call_set_chain (new_stmt, NULL);
1562 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1565 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1566 new_stmt);
1568 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1570 if (symtab->dump_file)
1572 fprintf (symtab->dump_file, " updated to:");
1573 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1575 return new_stmt;
1578 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1579 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1580 of OLD_STMT if it was previously call statement.
1581 If NEW_STMT is NULL, the call has been dropped without any
1582 replacement. */
1584 static void
1585 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1586 gimple *old_stmt, tree old_call,
1587 gimple *new_stmt)
1589 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1590 ? gimple_call_fndecl (new_stmt) : 0;
1592 /* We are seeing indirect calls, then there is nothing to update. */
1593 if (!new_call && !old_call)
1594 return;
1595 /* See if we turned indirect call into direct call or folded call to one builtin
1596 into different builtin. */
1597 if (old_call != new_call)
1599 cgraph_edge *e = node->get_edge (old_stmt);
1600 cgraph_edge *ne = NULL;
1601 profile_count count;
1602 int frequency;
1604 if (e)
1606 /* Keep calls marked as dead dead. */
1607 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1608 && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
1609 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
1611 node->get_edge (old_stmt)->set_call_stmt
1612 (as_a <gcall *> (new_stmt));
1613 return;
1615 /* See if the edge is already there and has the correct callee. It
1616 might be so because of indirect inlining has already updated
1617 it. We also might've cloned and redirected the edge. */
1618 if (new_call && e->callee)
1620 cgraph_node *callee = e->callee;
1621 while (callee)
1623 if (callee->decl == new_call
1624 || callee->former_clone_of == new_call)
1626 e->set_call_stmt (as_a <gcall *> (new_stmt));
1627 return;
1629 callee = callee->clone_of;
1633 /* Otherwise remove edge and create new one; we can't simply redirect
1634 since function has changed, so inline plan and other information
1635 attached to edge is invalid. */
1636 count = e->count;
1637 frequency = e->frequency;
1638 if (e->indirect_unknown_callee || e->inline_failed)
1639 e->remove ();
1640 else
1641 e->callee->remove_symbol_and_inline_clones ();
1643 else if (new_call)
1645 /* We are seeing new direct call; compute profile info based on BB. */
1646 basic_block bb = gimple_bb (new_stmt);
1647 count = bb->count;
1648 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1649 bb);
1652 if (new_call)
1654 ne = node->create_edge (cgraph_node::get_create (new_call),
1655 as_a <gcall *> (new_stmt), count,
1656 frequency);
1657 gcc_assert (ne->inline_failed);
1660 /* We only updated the call stmt; update pointer in cgraph edge.. */
1661 else if (old_stmt != new_stmt)
1662 node->get_edge (old_stmt)->set_call_stmt (as_a <gcall *> (new_stmt));
1665 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1666 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1667 of OLD_STMT before it was updated (updating can happen inplace). */
1669 void
1670 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1671 gimple *new_stmt)
1673 cgraph_node *orig = cgraph_node::get (cfun->decl);
1674 cgraph_node *node;
1676 gcc_checking_assert (orig);
1677 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1678 if (orig->clones)
1679 for (node = orig->clones; node != orig;)
1681 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1682 if (node->clones)
1683 node = node->clones;
1684 else if (node->next_sibling_clone)
1685 node = node->next_sibling_clone;
1686 else
1688 while (node != orig && !node->next_sibling_clone)
1689 node = node->clone_of;
1690 if (node != orig)
1691 node = node->next_sibling_clone;
1697 /* Remove all callees from the node. */
1699 void
1700 cgraph_node::remove_callees (void)
1702 cgraph_edge *e, *f;
1704 /* It is sufficient to remove the edges from the lists of callers of
1705 the callees. The callee list of the node can be zapped with one
1706 assignment. */
1707 for (e = callees; e; e = f)
1709 f = e->next_callee;
1710 symtab->call_edge_removal_hooks (e);
1711 if (!e->indirect_unknown_callee)
1712 e->remove_callee ();
1713 symtab->free_edge (e);
1715 for (e = indirect_calls; e; e = f)
1717 f = e->next_callee;
1718 symtab->call_edge_removal_hooks (e);
1719 if (!e->indirect_unknown_callee)
1720 e->remove_callee ();
1721 symtab->free_edge (e);
1723 indirect_calls = NULL;
1724 callees = NULL;
1725 if (call_site_hash)
1727 call_site_hash->empty ();
1728 call_site_hash = NULL;
1732 /* Remove all callers from the node. */
1734 void
1735 cgraph_node::remove_callers (void)
1737 cgraph_edge *e, *f;
1739 /* It is sufficient to remove the edges from the lists of callees of
1740 the callers. The caller list of the node can be zapped with one
1741 assignment. */
1742 for (e = callers; e; e = f)
1744 f = e->next_caller;
1745 symtab->call_edge_removal_hooks (e);
1746 e->remove_caller ();
1747 symtab->free_edge (e);
1749 callers = NULL;
1752 /* Helper function for cgraph_release_function_body and free_lang_data.
1753 It releases body from function DECL without having to inspect its
1754 possibly non-existent symtab node. */
1756 void
1757 release_function_body (tree decl)
1759 function *fn = DECL_STRUCT_FUNCTION (decl);
1760 if (fn)
1762 if (fn->cfg
1763 && loops_for_fn (fn))
1765 fn->curr_properties &= ~PROP_loops;
1766 loop_optimizer_finalize (fn);
1768 if (fn->gimple_df)
1770 delete_tree_ssa (fn);
1771 fn->eh = NULL;
1773 if (fn->cfg)
1775 gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1776 gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1777 delete_tree_cfg_annotations (fn);
1778 clear_edges (fn);
1779 fn->cfg = NULL;
1781 if (fn->value_histograms)
1782 free_histograms (fn);
1783 gimple_set_body (decl, NULL);
1784 /* Struct function hangs a lot of data that would leak if we didn't
1785 removed all pointers to it. */
1786 ggc_free (fn);
1787 DECL_STRUCT_FUNCTION (decl) = NULL;
1789 DECL_SAVED_TREE (decl) = NULL;
1792 /* Release memory used to represent body of function.
1793 Use this only for functions that are released before being translated to
1794 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1795 are free'd in final.c via free_after_compilation().
1796 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1798 void
1799 cgraph_node::release_body (bool keep_arguments)
1801 ipa_transforms_to_apply.release ();
1802 if (!used_as_abstract_origin && symtab->state != PARSING)
1804 DECL_RESULT (decl) = NULL;
1806 if (!keep_arguments)
1807 DECL_ARGUMENTS (decl) = NULL;
1809 /* If the node is abstract and needed, then do not clear
1810 DECL_INITIAL of its associated function declaration because it's
1811 needed to emit debug info later. */
1812 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1813 DECL_INITIAL (decl) = error_mark_node;
1814 release_function_body (decl);
1815 if (lto_file_data)
1817 lto_free_function_in_decl_state_for_node (this);
1818 lto_file_data = NULL;
1822 /* Remove function from symbol table. */
1824 void
1825 cgraph_node::remove (void)
1827 cgraph_node *n;
1828 int uid = this->uid;
1830 if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1831 fprintf (symtab->ipa_clones_dump_file,
1832 "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1833 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1834 DECL_SOURCE_COLUMN (decl));
1836 symtab->call_cgraph_removal_hooks (this);
1837 remove_callers ();
1838 remove_callees ();
1839 ipa_transforms_to_apply.release ();
1841 /* Incremental inlining access removed nodes stored in the postorder list.
1843 force_output = false;
1844 forced_by_abi = false;
1845 for (n = nested; n; n = n->next_nested)
1846 n->origin = NULL;
1847 nested = NULL;
1848 if (origin)
1850 cgraph_node **node2 = &origin->nested;
1852 while (*node2 != this)
1853 node2 = &(*node2)->next_nested;
1854 *node2 = next_nested;
1856 unregister ();
1857 if (prev_sibling_clone)
1858 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1859 else if (clone_of)
1860 clone_of->clones = next_sibling_clone;
1861 if (next_sibling_clone)
1862 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1863 if (clones)
1865 cgraph_node *n, *next;
1867 if (clone_of)
1869 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1870 n->clone_of = clone_of;
1871 n->clone_of = clone_of;
1872 n->next_sibling_clone = clone_of->clones;
1873 if (clone_of->clones)
1874 clone_of->clones->prev_sibling_clone = n;
1875 clone_of->clones = clones;
1877 else
1879 /* We are removing node with clones. This makes clones inconsistent,
1880 but assume they will be removed subsequently and just keep clone
1881 tree intact. This can happen in unreachable function removal since
1882 we remove unreachable functions in random order, not by bottom-up
1883 walk of clone trees. */
1884 for (n = clones; n; n = next)
1886 next = n->next_sibling_clone;
1887 n->next_sibling_clone = NULL;
1888 n->prev_sibling_clone = NULL;
1889 n->clone_of = NULL;
1894 /* While all the clones are removed after being proceeded, the function
1895 itself is kept in the cgraph even after it is compiled. Check whether
1896 we are done with this body and reclaim it proactively if this is the case.
1898 if (symtab->state != LTO_STREAMING)
1900 n = cgraph_node::get (decl);
1901 if (!n
1902 || (!n->clones && !n->clone_of && !n->global.inlined_to
1903 && ((symtab->global_info_ready || in_lto_p)
1904 && (TREE_ASM_WRITTEN (n->decl)
1905 || DECL_EXTERNAL (n->decl)
1906 || !n->analyzed
1907 || (!flag_wpa && n->in_other_partition)))))
1908 release_body ();
1910 else
1912 lto_free_function_in_decl_state_for_node (this);
1913 lto_file_data = NULL;
1916 decl = NULL;
1917 if (call_site_hash)
1919 call_site_hash->empty ();
1920 call_site_hash = NULL;
1923 if (instrumented_version)
1925 instrumented_version->instrumented_version = NULL;
1926 instrumented_version = NULL;
1929 symtab->release_symbol (this, uid);
1932 /* Likewise indicate that a node is having address taken. */
1934 void
1935 cgraph_node::mark_address_taken (void)
1937 /* Indirect inlining can figure out that all uses of the address are
1938 inlined. */
1939 if (global.inlined_to)
1941 gcc_assert (cfun->after_inlining);
1942 gcc_assert (callers->indirect_inlining_edge);
1943 return;
1945 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1946 IPA_REF_ADDR reference exists (and thus it should be set on node
1947 representing alias we take address of) and as a test whether address
1948 of the object was taken (and thus it should be set on node alias is
1949 referring to). We should remove the first use and the remove the
1950 following set. */
1951 address_taken = 1;
1952 cgraph_node *node = ultimate_alias_target ();
1953 node->address_taken = 1;
1956 /* Return local info for the compiled function. */
1958 cgraph_local_info *
1959 cgraph_node::local_info (tree decl)
1961 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1962 cgraph_node *node = get (decl);
1963 if (!node)
1964 return NULL;
1965 return &node->ultimate_alias_target ()->local;
1968 /* Return local info for the compiled function. */
1970 cgraph_rtl_info *
1971 cgraph_node::rtl_info (tree decl)
1973 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1974 cgraph_node *node = get (decl);
1975 if (!node)
1976 return NULL;
1977 enum availability avail;
1978 node = node->ultimate_alias_target (&avail);
1979 if (decl != current_function_decl
1980 && (avail < AVAIL_AVAILABLE
1981 || (node->decl != current_function_decl
1982 && !TREE_ASM_WRITTEN (node->decl))))
1983 return NULL;
1984 /* Allocate if it doesn't exist. */
1985 if (node->rtl == NULL)
1986 node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
1987 return node->rtl;
1990 /* Return a string describing the failure REASON. */
1992 const char*
1993 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1995 #undef DEFCIFCODE
1996 #define DEFCIFCODE(code, type, string) string,
1998 static const char *cif_string_table[CIF_N_REASONS] = {
1999 #include "cif-code.def"
2002 /* Signedness of an enum type is implementation defined, so cast it
2003 to unsigned before testing. */
2004 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2005 return cif_string_table[reason];
2008 /* Return a type describing the failure REASON. */
2010 cgraph_inline_failed_type_t
2011 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2013 #undef DEFCIFCODE
2014 #define DEFCIFCODE(code, type, string) type,
2016 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2017 #include "cif-code.def"
2020 /* Signedness of an enum type is implementation defined, so cast it
2021 to unsigned before testing. */
2022 gcc_assert ((unsigned) reason < CIF_N_REASONS);
2023 return cif_type_table[reason];
2026 /* Names used to print out the availability enum. */
2027 const char * const cgraph_availability_names[] =
2028 {"unset", "not_available", "overwritable", "available", "local"};
2030 /* Output flags of edge to a file F. */
2032 void
2033 cgraph_edge::dump_edge_flags (FILE *f)
2035 if (speculative)
2036 fprintf (f, "(speculative) ");
2037 if (!inline_failed)
2038 fprintf (f, "(inlined) ");
2039 if (call_stmt_cannot_inline_p)
2040 fprintf (f, "(call_stmt_cannot_inline_p) ");
2041 if (indirect_inlining_edge)
2042 fprintf (f, "(indirect_inlining) ");
2043 if (count.initialized_p ())
2045 fprintf (f, "(");
2046 count.dump (f);
2047 fprintf (f, ")");
2049 if (frequency)
2050 fprintf (f, "(%.2f per call) ", frequency / (double)CGRAPH_FREQ_BASE);
2051 if (can_throw_external)
2052 fprintf (f, "(can throw external) ");
2055 /* Dump call graph node to file F. */
2057 void
2058 cgraph_node::dump (FILE *f)
2060 cgraph_edge *edge;
2062 dump_base (f);
2064 if (global.inlined_to)
2065 fprintf (f, " Function %s is inline copy in %s\n",
2066 dump_name (),
2067 global.inlined_to->dump_name ());
2068 if (clone_of)
2069 fprintf (f, " Clone of %s\n", clone_of->dump_asm_name ());
2070 if (symtab->function_flags_ready)
2071 fprintf (f, " Availability: %s\n",
2072 cgraph_availability_names [get_availability ()]);
2074 if (profile_id)
2075 fprintf (f, " Profile id: %i\n",
2076 profile_id);
2077 fprintf (f, " First run: %i\n", tp_first_run);
2078 cgraph_function_version_info *vi = function_version ();
2079 if (vi != NULL)
2081 fprintf (f, " Version info: ");
2082 if (vi->prev != NULL)
2084 fprintf (f, "prev: ");
2085 fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2087 if (vi->next != NULL)
2089 fprintf (f, "next: ");
2090 fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2092 if (vi->dispatcher_resolver != NULL_TREE)
2093 fprintf (f, "dispatcher: %s",
2094 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2096 fprintf (f, "\n");
2098 fprintf (f, " Function flags:");
2099 if (count.initialized_p ())
2101 fprintf (f, " count: ");
2102 count.dump (f);
2104 if (origin)
2105 fprintf (f, " nested in: %s", origin->asm_name ());
2106 if (gimple_has_body_p (decl))
2107 fprintf (f, " body");
2108 if (process)
2109 fprintf (f, " process");
2110 if (local.local)
2111 fprintf (f, " local");
2112 if (local.redefined_extern_inline)
2113 fprintf (f, " redefined_extern_inline");
2114 if (only_called_at_startup)
2115 fprintf (f, " only_called_at_startup");
2116 if (only_called_at_exit)
2117 fprintf (f, " only_called_at_exit");
2118 if (tm_clone)
2119 fprintf (f, " tm_clone");
2120 if (calls_comdat_local)
2121 fprintf (f, " calls_comdat_local");
2122 if (icf_merged)
2123 fprintf (f, " icf_merged");
2124 if (merged_comdat)
2125 fprintf (f, " merged_comdat");
2126 if (split_part)
2127 fprintf (f, " split_part");
2128 if (indirect_call_target)
2129 fprintf (f, " indirect_call_target");
2130 if (nonfreeing_fn)
2131 fprintf (f, " nonfreeing_fn");
2132 if (DECL_STATIC_CONSTRUCTOR (decl))
2133 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2134 if (DECL_STATIC_DESTRUCTOR (decl))
2135 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2136 if (frequency == NODE_FREQUENCY_HOT)
2137 fprintf (f, " hot");
2138 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2139 fprintf (f, " unlikely_executed");
2140 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2141 fprintf (f, " executed_once");
2142 if (only_called_at_startup)
2143 fprintf (f, " only_called_at_startup");
2144 if (only_called_at_exit)
2145 fprintf (f, " only_called_at_exit");
2146 if (opt_for_fn (decl, optimize_size))
2147 fprintf (f, " optimize_size");
2148 if (parallelized_function)
2149 fprintf (f, " parallelized_function");
2151 fprintf (f, "\n");
2153 if (thunk.thunk_p)
2155 fprintf (f, " Thunk");
2156 if (thunk.alias)
2157 fprintf (f, " of %s (asm: %s)",
2158 lang_hooks.decl_printable_name (thunk.alias, 2),
2159 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2160 fprintf (f, " fixed offset %i virtual value %i has "
2161 "virtual offset %i)\n",
2162 (int)thunk.fixed_offset,
2163 (int)thunk.virtual_value,
2164 (int)thunk.virtual_offset_p);
2166 if (alias && thunk.alias
2167 && DECL_P (thunk.alias))
2169 fprintf (f, " Alias of %s",
2170 lang_hooks.decl_printable_name (thunk.alias, 2));
2171 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2172 fprintf (f, " (asm: %s)",
2173 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2174 fprintf (f, "\n");
2177 fprintf (f, " Called by: ");
2179 profile_count sum = profile_count::zero ();
2180 for (edge = callers; edge; edge = edge->next_caller)
2182 fprintf (f, "%s ", edge->caller->dump_name ());
2183 edge->dump_edge_flags (f);
2184 if (edge->count.initialized_p ())
2185 sum += edge->count;
2188 fprintf (f, "\n Calls: ");
2189 for (edge = callees; edge; edge = edge->next_callee)
2191 fprintf (f, "%s ", edge->callee->dump_name ());
2192 edge->dump_edge_flags (f);
2194 fprintf (f, "\n");
2196 if (count.initialized_p ())
2198 bool ok = true;
2199 bool min = false;
2200 ipa_ref *ref;
2202 FOR_EACH_ALIAS (this, ref)
2203 if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2204 sum += dyn_cast <cgraph_node *> (ref->referring)->count;
2206 if (global.inlined_to
2207 || (symtab->state < EXPANSION
2208 && ultimate_alias_target () == this && only_called_directly_p ()))
2209 ok = !count.differs_from_p (sum);
2210 else if (count > profile_count::from_gcov_type (100)
2211 && count < sum.apply_scale (99, 100))
2212 ok = false, min = true;
2213 if (!ok)
2215 fprintf (f, " Invalid sum of caller counts ");
2216 sum.dump (f);
2217 if (min)
2218 fprintf (f, ", should be at most ");
2219 else
2220 fprintf (f, ", should be ");
2221 count.dump (f);
2222 fprintf (f, "\n");
2226 for (edge = indirect_calls; edge; edge = edge->next_callee)
2228 if (edge->indirect_info->polymorphic)
2230 fprintf (f, " Polymorphic indirect call of type ");
2231 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2232 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2234 else
2235 fprintf (f, " Indirect call");
2236 edge->dump_edge_flags (f);
2237 if (edge->indirect_info->param_index != -1)
2239 fprintf (f, " of param:%i", edge->indirect_info->param_index);
2240 if (edge->indirect_info->agg_contents)
2241 fprintf (f, " loaded from %s %s at offset %i",
2242 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2243 edge->indirect_info->by_ref ? "passed by reference":"",
2244 (int)edge->indirect_info->offset);
2245 if (edge->indirect_info->vptr_changed)
2246 fprintf (f, " (vptr maybe changed)");
2248 fprintf (f, "\n");
2249 if (edge->indirect_info->polymorphic)
2250 edge->indirect_info->context.dump (f);
2253 if (instrumentation_clone)
2254 fprintf (f, " Is instrumented version.\n");
2255 else if (instrumented_version)
2256 fprintf (f, " Has instrumented version.\n");
2259 /* Dump call graph node NODE to stderr. */
2261 DEBUG_FUNCTION void
2262 cgraph_node::debug (void)
2264 dump (stderr);
2267 /* Dump the callgraph to file F. */
2269 void
2270 cgraph_node::dump_cgraph (FILE *f)
2272 cgraph_node *node;
2274 fprintf (f, "callgraph:\n\n");
2275 FOR_EACH_FUNCTION (node)
2276 node->dump (f);
2279 /* Return true when the DECL can possibly be inlined. */
2281 bool
2282 cgraph_function_possibly_inlined_p (tree decl)
2284 if (!symtab->global_info_ready)
2285 return !DECL_UNINLINABLE (decl);
2286 return DECL_POSSIBLY_INLINED (decl);
2289 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2290 void
2291 cgraph_node::unnest (void)
2293 cgraph_node **node2 = &origin->nested;
2294 gcc_assert (origin);
2296 while (*node2 != this)
2297 node2 = &(*node2)->next_nested;
2298 *node2 = next_nested;
2299 origin = NULL;
2302 /* Return function availability. See cgraph.h for description of individual
2303 return values. */
2304 enum availability
2305 cgraph_node::get_availability (symtab_node *ref)
2307 if (ref)
2309 cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2310 if (cref)
2311 ref = cref->global.inlined_to;
2313 enum availability avail;
2314 if (!analyzed)
2315 avail = AVAIL_NOT_AVAILABLE;
2316 else if (local.local)
2317 avail = AVAIL_LOCAL;
2318 else if (global.inlined_to)
2319 avail = AVAIL_AVAILABLE;
2320 else if (transparent_alias)
2321 ultimate_alias_target (&avail, ref);
2322 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl))
2323 || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2324 avail = AVAIL_INTERPOSABLE;
2325 else if (!externally_visible)
2326 avail = AVAIL_AVAILABLE;
2327 /* If this is a reference from symbol itself and there are no aliases, we
2328 may be sure that the symbol was not interposed by something else because
2329 the symbol itself would be unreachable otherwise.
2331 Also comdat groups are always resolved in groups. */
2332 else if ((this == ref && !has_aliases_p ())
2333 || (ref && get_comdat_group ()
2334 && get_comdat_group () == ref->get_comdat_group ()))
2335 avail = AVAIL_AVAILABLE;
2336 /* Inline functions are safe to be analyzed even if their symbol can
2337 be overwritten at runtime. It is not meaningful to enforce any sane
2338 behavior on replacing inline function by different body. */
2339 else if (DECL_DECLARED_INLINE_P (decl))
2340 avail = AVAIL_AVAILABLE;
2342 /* If the function can be overwritten, return OVERWRITABLE. Take
2343 care at least of two notable extensions - the COMDAT functions
2344 used to share template instantiations in C++ (this is symmetric
2345 to code cp_cannot_inline_tree_fn and probably shall be shared and
2346 the inlinability hooks completely eliminated). */
2348 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2349 avail = AVAIL_INTERPOSABLE;
2350 else avail = AVAIL_AVAILABLE;
2352 return avail;
2355 /* Worker for cgraph_node_can_be_local_p. */
2356 static bool
2357 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2359 return !(!node->force_output
2360 && ((DECL_COMDAT (node->decl)
2361 && !node->forced_by_abi
2362 && !node->used_from_object_file_p ()
2363 && !node->same_comdat_group)
2364 || !node->externally_visible));
2367 /* Return true if cgraph_node can be made local for API change.
2368 Extern inline functions and C++ COMDAT functions can be made local
2369 at the expense of possible code size growth if function is used in multiple
2370 compilation units. */
2371 bool
2372 cgraph_node::can_be_local_p (void)
2374 return (!address_taken
2375 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2376 NULL, true));
2379 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2380 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2381 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2382 skipped. */
2383 bool
2384 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2385 (cgraph_node *, void *),
2386 void *data,
2387 bool include_overwritable,
2388 bool exclude_virtual_thunks)
2390 cgraph_edge *e;
2391 ipa_ref *ref;
2392 enum availability avail = AVAIL_AVAILABLE;
2394 if (include_overwritable
2395 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2397 if (callback (this, data))
2398 return true;
2400 FOR_EACH_ALIAS (this, ref)
2402 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2403 if (include_overwritable
2404 || alias->get_availability () > AVAIL_INTERPOSABLE)
2405 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2406 include_overwritable,
2407 exclude_virtual_thunks))
2408 return true;
2410 if (avail <= AVAIL_INTERPOSABLE)
2411 return false;
2412 for (e = callers; e; e = e->next_caller)
2413 if (e->caller->thunk.thunk_p
2414 && (include_overwritable
2415 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2416 && !(exclude_virtual_thunks
2417 && e->caller->thunk.virtual_offset_p))
2418 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2419 include_overwritable,
2420 exclude_virtual_thunks))
2421 return true;
2423 return false;
2426 /* Worker to bring NODE local. */
2428 bool
2429 cgraph_node::make_local (cgraph_node *node, void *)
2431 gcc_checking_assert (node->can_be_local_p ());
2432 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2434 node->make_decl_local ();
2435 node->set_section (NULL);
2436 node->set_comdat_group (NULL);
2437 node->externally_visible = false;
2438 node->forced_by_abi = false;
2439 node->local.local = true;
2440 node->set_section (NULL);
2441 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2442 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2443 && !flag_incremental_link);
2444 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2445 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2447 return false;
2450 /* Bring cgraph node local. */
2452 void
2453 cgraph_node::make_local (void)
2455 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2458 /* Worker to set nothrow flag. */
2460 static void
2461 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2462 bool *changed)
2464 cgraph_edge *e;
2466 if (nothrow && !TREE_NOTHROW (node->decl))
2468 /* With non-call exceptions we can't say for sure if other function body
2469 was not possibly optimized to stil throw. */
2470 if (!non_call || node->binds_to_current_def_p ())
2472 TREE_NOTHROW (node->decl) = true;
2473 *changed = true;
2474 for (e = node->callers; e; e = e->next_caller)
2475 e->can_throw_external = false;
2478 else if (!nothrow && TREE_NOTHROW (node->decl))
2480 TREE_NOTHROW (node->decl) = false;
2481 *changed = true;
2483 ipa_ref *ref;
2484 FOR_EACH_ALIAS (node, ref)
2486 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2487 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2488 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2490 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2491 if (e->caller->thunk.thunk_p
2492 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2493 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2496 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2497 if any to NOTHROW. */
2499 bool
2500 cgraph_node::set_nothrow_flag (bool nothrow)
2502 bool changed = false;
2503 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2505 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2506 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2507 else
2509 ipa_ref *ref;
2511 FOR_EACH_ALIAS (this, ref)
2513 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2514 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2515 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2518 return changed;
2521 /* Worker to set_const_flag. */
2523 static void
2524 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2525 bool *changed)
2527 /* Static constructors and destructors without a side effect can be
2528 optimized out. */
2529 if (set_const && !looping)
2531 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2533 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2534 *changed = true;
2536 if (DECL_STATIC_DESTRUCTOR (node->decl))
2538 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2539 *changed = true;
2542 if (!set_const)
2544 if (TREE_READONLY (node->decl))
2546 TREE_READONLY (node->decl) = 0;
2547 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2548 *changed = true;
2551 else
2553 /* Consider function:
2555 bool a(int *p)
2557 return *p==*p;
2560 During early optimization we will turn this into:
2562 bool a(int *p)
2564 return true;
2567 Now if this function will be detected as CONST however when interposed
2568 it may end up being just pure. We always must assume the worst
2569 scenario here. */
2570 if (TREE_READONLY (node->decl))
2572 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2574 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2575 *changed = true;
2578 else if (node->binds_to_current_def_p ())
2580 TREE_READONLY (node->decl) = true;
2581 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2582 DECL_PURE_P (node->decl) = false;
2583 *changed = true;
2585 else
2587 if (dump_file && (dump_flags & TDF_DETAILS))
2588 fprintf (dump_file, "Dropping state to PURE because function does "
2589 "not bind to current def.\n");
2590 if (!DECL_PURE_P (node->decl))
2592 DECL_PURE_P (node->decl) = true;
2593 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2594 *changed = true;
2596 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2598 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2599 *changed = true;
2604 ipa_ref *ref;
2605 FOR_EACH_ALIAS (node, ref)
2607 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2608 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2609 set_const_flag_1 (alias, set_const, looping, changed);
2611 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2612 if (e->caller->thunk.thunk_p
2613 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2615 /* Virtual thunks access virtual offset in the vtable, so they can
2616 only be pure, never const. */
2617 if (set_const
2618 && (e->caller->thunk.virtual_offset_p
2619 || !node->binds_to_current_def_p (e->caller)))
2620 *changed |= e->caller->set_pure_flag (true, looping);
2621 else
2622 set_const_flag_1 (e->caller, set_const, looping, changed);
2626 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2627 If SET_CONST if false, clear the flag.
2629 When setting the flag be careful about possible interposition and
2630 do not set the flag for functions that can be interposet and set pure
2631 flag for functions that can bind to other definition.
2633 Return true if any change was done. */
2635 bool
2636 cgraph_node::set_const_flag (bool set_const, bool looping)
2638 bool changed = false;
2639 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2640 set_const_flag_1 (this, set_const, looping, &changed);
2641 else
2643 ipa_ref *ref;
2645 FOR_EACH_ALIAS (this, ref)
2647 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2648 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2649 set_const_flag_1 (alias, set_const, looping, &changed);
2652 return changed;
2655 /* Info used by set_pure_flag_1. */
2657 struct set_pure_flag_info
2659 bool pure;
2660 bool looping;
2661 bool changed;
2664 /* Worker to set_pure_flag. */
2666 static bool
2667 set_pure_flag_1 (cgraph_node *node, void *data)
2669 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2670 /* Static constructors and destructors without a side effect can be
2671 optimized out. */
2672 if (info->pure && !info->looping)
2674 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2676 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2677 info->changed = true;
2679 if (DECL_STATIC_DESTRUCTOR (node->decl))
2681 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2682 info->changed = true;
2685 if (info->pure)
2687 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2689 DECL_PURE_P (node->decl) = true;
2690 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2691 info->changed = true;
2693 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2694 && !info->looping)
2696 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2697 info->changed = true;
2700 else
2702 if (DECL_PURE_P (node->decl))
2704 DECL_PURE_P (node->decl) = false;
2705 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2706 info->changed = true;
2709 return false;
2712 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2713 if any to PURE.
2715 When setting the flag, be careful about possible interposition.
2716 Return true if any change was done. */
2718 bool
2719 cgraph_node::set_pure_flag (bool pure, bool looping)
2721 struct set_pure_flag_info info = {pure, looping, false};
2722 if (!pure)
2723 looping = false;
2724 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2725 return info.changed;
2728 /* Return true when cgraph_node can not return or throw and thus
2729 it is safe to ignore its side effects for IPA analysis. */
2731 bool
2732 cgraph_node::cannot_return_p (void)
2734 int flags = flags_from_decl_or_type (decl);
2735 if (!opt_for_fn (decl, flag_exceptions))
2736 return (flags & ECF_NORETURN) != 0;
2737 else
2738 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2739 == (ECF_NORETURN | ECF_NOTHROW));
2742 /* Return true when call of edge can not lead to return from caller
2743 and thus it is safe to ignore its side effects for IPA analysis
2744 when computing side effects of the caller.
2745 FIXME: We could actually mark all edges that have no reaching
2746 patch to the exit block or throw to get better results. */
2747 bool
2748 cgraph_edge::cannot_lead_to_return_p (void)
2750 if (caller->cannot_return_p ())
2751 return true;
2752 if (indirect_unknown_callee)
2754 int flags = indirect_info->ecf_flags;
2755 if (!opt_for_fn (caller->decl, flag_exceptions))
2756 return (flags & ECF_NORETURN) != 0;
2757 else
2758 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2759 == (ECF_NORETURN | ECF_NOTHROW));
2761 else
2762 return callee->cannot_return_p ();
2765 /* Return true if the call can be hot. */
2767 bool
2768 cgraph_edge::maybe_hot_p (void)
2770 if (!maybe_hot_count_p (NULL, count))
2771 return false;
2772 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2773 || (callee
2774 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2775 return false;
2776 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2777 && (callee
2778 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2779 return false;
2780 if (opt_for_fn (caller->decl, optimize_size))
2781 return false;
2782 if (caller->frequency == NODE_FREQUENCY_HOT)
2783 return true;
2784 /* If profile is now known yet, be conservative.
2785 FIXME: this predicate is used by early inliner and can do better there. */
2786 if (symtab->state < IPA_SSA)
2787 return true;
2788 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE
2789 && frequency < CGRAPH_FREQ_BASE * 3 / 2)
2790 return false;
2791 if (opt_for_fn (caller->decl, flag_guess_branch_prob))
2793 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0
2794 || frequency <= (CGRAPH_FREQ_BASE
2795 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
2796 return false;
2798 return true;
2801 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2803 static bool
2804 nonremovable_p (cgraph_node *node, void *)
2806 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2809 /* Return true if whole comdat group can be removed if there are no direct
2810 calls to THIS. */
2812 bool
2813 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2815 struct ipa_ref *ref;
2817 /* For local symbols or non-comdat group it is the same as
2818 can_remove_if_no_direct_calls_p. */
2819 if (!externally_visible || !same_comdat_group)
2821 if (DECL_EXTERNAL (decl))
2822 return true;
2823 if (address_taken)
2824 return false;
2825 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2828 if (will_inline && address_taken)
2829 return false;
2831 /* Otheriwse check if we can remove the symbol itself and then verify
2832 that only uses of the comdat groups are direct call to THIS
2833 or its aliases. */
2834 if (!can_remove_if_no_direct_calls_and_refs_p ())
2835 return false;
2837 /* Check that all refs come from within the comdat group. */
2838 for (int i = 0; iterate_referring (i, ref); i++)
2839 if (ref->referring->get_comdat_group () != get_comdat_group ())
2840 return false;
2842 struct cgraph_node *target = ultimate_alias_target ();
2843 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2844 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2846 if (!externally_visible)
2847 continue;
2848 if (!next->alias
2849 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2850 return false;
2852 /* If we see different symbol than THIS, be sure to check calls. */
2853 if (next->ultimate_alias_target () != target)
2854 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2855 if (e->caller->get_comdat_group () != get_comdat_group ()
2856 || will_inline)
2857 return false;
2859 /* If function is not being inlined, we care only about
2860 references outside of the comdat group. */
2861 if (!will_inline)
2862 for (int i = 0; next->iterate_referring (i, ref); i++)
2863 if (ref->referring->get_comdat_group () != get_comdat_group ())
2864 return false;
2866 return true;
2869 /* Return true when function cgraph_node can be expected to be removed
2870 from program when direct calls in this compilation unit are removed.
2872 As a special case COMDAT functions are
2873 cgraph_can_remove_if_no_direct_calls_p while the are not
2874 cgraph_only_called_directly_p (it is possible they are called from other
2875 unit)
2877 This function behaves as cgraph_only_called_directly_p because eliminating
2878 all uses of COMDAT function does not make it necessarily disappear from
2879 the program unless we are compiling whole program or we do LTO. In this
2880 case we know we win since dynamic linking will not really discard the
2881 linkonce section. */
2883 bool
2884 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2885 (bool will_inline)
2887 gcc_assert (!global.inlined_to);
2888 if (DECL_EXTERNAL (decl))
2889 return true;
2891 if (!in_lto_p && !flag_whole_program)
2893 /* If the symbol is in comdat group, we need to verify that whole comdat
2894 group becomes unreachable. Technically we could skip references from
2895 within the group, too. */
2896 if (!only_called_directly_p ())
2897 return false;
2898 if (same_comdat_group && externally_visible)
2900 struct cgraph_node *target = ultimate_alias_target ();
2902 if (will_inline && address_taken)
2903 return true;
2904 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2905 next != this;
2906 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2908 if (!externally_visible)
2909 continue;
2910 if (!next->alias
2911 && !next->only_called_directly_p ())
2912 return false;
2914 /* If we see different symbol than THIS,
2915 be sure to check calls. */
2916 if (next->ultimate_alias_target () != target)
2917 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2918 if (e->caller->get_comdat_group () != get_comdat_group ()
2919 || will_inline)
2920 return false;
2923 return true;
2925 else
2926 return can_remove_if_no_direct_calls_p (will_inline);
2930 /* Worker for cgraph_only_called_directly_p. */
2932 static bool
2933 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2935 return !node->only_called_directly_or_aliased_p ();
2938 /* Return true when function cgraph_node and all its aliases are only called
2939 directly.
2940 i.e. it is not externally visible, address was not taken and
2941 it is not used in any other non-standard way. */
2943 bool
2944 cgraph_node::only_called_directly_p (void)
2946 gcc_assert (ultimate_alias_target () == this);
2947 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2948 NULL, true);
2952 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2954 static bool
2955 collect_callers_of_node_1 (cgraph_node *node, void *data)
2957 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2958 cgraph_edge *cs;
2959 enum availability avail;
2960 node->ultimate_alias_target (&avail);
2962 if (avail > AVAIL_INTERPOSABLE)
2963 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2964 if (!cs->indirect_inlining_edge
2965 && !cs->caller->thunk.thunk_p)
2966 redirect_callers->safe_push (cs);
2967 return false;
2970 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2971 cgraph_node (i.e. are not overwritable). */
2973 vec<cgraph_edge *>
2974 cgraph_node::collect_callers (void)
2976 vec<cgraph_edge *> redirect_callers = vNULL;
2977 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2978 &redirect_callers, false);
2979 return redirect_callers;
2982 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2984 static bool
2985 clone_of_p (cgraph_node *node, cgraph_node *node2)
2987 bool skipped_thunk = false;
2988 node = node->ultimate_alias_target ();
2989 node2 = node2->ultimate_alias_target ();
2991 /* There are no virtual clones of thunks so check former_clone_of or if we
2992 might have skipped thunks because this adjustments are no longer
2993 necessary. */
2994 while (node->thunk.thunk_p)
2996 if (node2->former_clone_of == node->decl)
2997 return true;
2998 if (!node->thunk.this_adjusting)
2999 return false;
3000 node = node->callees->callee->ultimate_alias_target ();
3001 skipped_thunk = true;
3004 if (skipped_thunk)
3006 if (!node2->clone.args_to_skip
3007 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
3008 return false;
3009 if (node2->former_clone_of == node->decl)
3010 return true;
3011 else if (!node2->clone_of)
3012 return false;
3015 while (node != node2 && node2)
3016 node2 = node2->clone_of;
3017 return node2 != NULL;
3020 /* Verify edge count and frequency. */
3022 bool
3023 cgraph_edge::verify_count_and_frequency ()
3025 bool error_found = false;
3026 if (count < 0)
3028 error ("caller edge count is negative");
3029 error_found = true;
3031 if (frequency < 0)
3033 error ("caller edge frequency is negative");
3034 error_found = true;
3036 if (frequency > CGRAPH_FREQ_MAX)
3038 error ("caller edge frequency is too large");
3039 error_found = true;
3041 return error_found;
3044 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3045 static void
3046 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3048 bool fndecl_was_null = false;
3049 /* debug_gimple_stmt needs correct cfun */
3050 if (cfun != this_cfun)
3051 set_cfun (this_cfun);
3052 /* ...and an actual current_function_decl */
3053 if (!current_function_decl)
3055 current_function_decl = this_cfun->decl;
3056 fndecl_was_null = true;
3058 debug_gimple_stmt (stmt);
3059 if (fndecl_was_null)
3060 current_function_decl = NULL;
3063 /* Verify that call graph edge corresponds to DECL from the associated
3064 statement. Return true if the verification should fail. */
3066 bool
3067 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3069 cgraph_node *node;
3071 if (!decl || callee->global.inlined_to)
3072 return false;
3073 if (symtab->state == LTO_STREAMING)
3074 return false;
3075 node = cgraph_node::get (decl);
3077 /* We do not know if a node from a different partition is an alias or what it
3078 aliases and therefore cannot do the former_clone_of check reliably. When
3079 body_removed is set, we have lost all information about what was alias or
3080 thunk of and also cannot proceed. */
3081 if (!node
3082 || node->body_removed
3083 || node->in_other_partition
3084 || callee->icf_merged
3085 || callee->in_other_partition)
3086 return false;
3088 node = node->ultimate_alias_target ();
3090 /* Optimizers can redirect unreachable calls or calls triggering undefined
3091 behavior to builtin_unreachable. */
3092 if (DECL_BUILT_IN_CLASS (callee->decl) == BUILT_IN_NORMAL
3093 && DECL_FUNCTION_CODE (callee->decl) == BUILT_IN_UNREACHABLE)
3094 return false;
3096 if (callee->former_clone_of != node->decl
3097 && (node != callee->ultimate_alias_target ())
3098 && !clone_of_p (node, callee))
3099 return true;
3100 else
3101 return false;
3104 /* Verify cgraph nodes of given cgraph node. */
3105 DEBUG_FUNCTION void
3106 cgraph_node::verify_node (void)
3108 cgraph_edge *e;
3109 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3110 basic_block this_block;
3111 gimple_stmt_iterator gsi;
3112 bool error_found = false;
3114 if (seen_error ())
3115 return;
3117 timevar_push (TV_CGRAPH_VERIFY);
3118 error_found |= verify_base ();
3119 for (e = callees; e; e = e->next_callee)
3120 if (e->aux)
3122 error ("aux field set for edge %s->%s",
3123 identifier_to_locale (e->caller->name ()),
3124 identifier_to_locale (e->callee->name ()));
3125 error_found = true;
3127 if (count < 0)
3129 error ("execution count is negative");
3130 error_found = true;
3132 if (global.inlined_to && same_comdat_group)
3134 error ("inline clone in same comdat group list");
3135 error_found = true;
3137 if (!definition && !in_other_partition && local.local)
3139 error ("local symbols must be defined");
3140 error_found = true;
3142 if (global.inlined_to && externally_visible)
3144 error ("externally visible inline clone");
3145 error_found = true;
3147 if (global.inlined_to && address_taken)
3149 error ("inline clone with address taken");
3150 error_found = true;
3152 if (global.inlined_to && force_output)
3154 error ("inline clone is forced to output");
3155 error_found = true;
3157 for (e = indirect_calls; e; e = e->next_callee)
3159 if (e->aux)
3161 error ("aux field set for indirect edge from %s",
3162 identifier_to_locale (e->caller->name ()));
3163 error_found = true;
3165 if (!e->indirect_unknown_callee
3166 || !e->indirect_info)
3168 error ("An indirect edge from %s is not marked as indirect or has "
3169 "associated indirect_info, the corresponding statement is: ",
3170 identifier_to_locale (e->caller->name ()));
3171 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3172 error_found = true;
3175 bool check_comdat = comdat_local_p ();
3176 for (e = callers; e; e = e->next_caller)
3178 if (e->verify_count_and_frequency ())
3179 error_found = true;
3180 if (check_comdat
3181 && !in_same_comdat_group_p (e->caller))
3183 error ("comdat-local function called by %s outside its comdat",
3184 identifier_to_locale (e->caller->name ()));
3185 error_found = true;
3187 if (!e->inline_failed)
3189 if (global.inlined_to
3190 != (e->caller->global.inlined_to
3191 ? e->caller->global.inlined_to : e->caller))
3193 error ("inlined_to pointer is wrong");
3194 error_found = true;
3196 if (callers->next_caller)
3198 error ("multiple inline callers");
3199 error_found = true;
3202 else
3203 if (global.inlined_to)
3205 error ("inlined_to pointer set for noninline callers");
3206 error_found = true;
3209 for (e = callees; e; e = e->next_callee)
3211 if (e->verify_count_and_frequency ())
3212 error_found = true;
3213 if (gimple_has_body_p (e->caller->decl)
3214 && !e->caller->global.inlined_to
3215 && !e->speculative
3216 /* Optimized out calls are redirected to __builtin_unreachable. */
3217 && (e->frequency
3218 || ! e->callee->decl
3219 || DECL_BUILT_IN_CLASS (e->callee->decl) != BUILT_IN_NORMAL
3220 || DECL_FUNCTION_CODE (e->callee->decl) != BUILT_IN_UNREACHABLE)
3221 && (e->frequency
3222 != compute_call_stmt_bb_frequency (e->caller->decl,
3223 gimple_bb (e->call_stmt))))
3225 error ("caller edge frequency %i does not match BB frequency %i",
3226 e->frequency,
3227 compute_call_stmt_bb_frequency (e->caller->decl,
3228 gimple_bb (e->call_stmt)));
3229 error_found = true;
3232 for (e = indirect_calls; e; e = e->next_callee)
3234 if (e->verify_count_and_frequency ())
3235 error_found = true;
3236 if (gimple_has_body_p (e->caller->decl)
3237 && !e->caller->global.inlined_to
3238 && !e->speculative
3239 && (e->frequency
3240 != compute_call_stmt_bb_frequency (e->caller->decl,
3241 gimple_bb (e->call_stmt))))
3243 error ("indirect call frequency %i does not match BB frequency %i",
3244 e->frequency,
3245 compute_call_stmt_bb_frequency (e->caller->decl,
3246 gimple_bb (e->call_stmt)));
3247 error_found = true;
3250 if (!callers && global.inlined_to)
3252 error ("inlined_to pointer is set but no predecessors found");
3253 error_found = true;
3255 if (global.inlined_to == this)
3257 error ("inlined_to pointer refers to itself");
3258 error_found = true;
3261 if (clone_of)
3263 cgraph_node *n;
3264 for (n = clone_of->clones; n; n = n->next_sibling_clone)
3265 if (n == this)
3266 break;
3267 if (!n)
3269 error ("cgraph_node has wrong clone_of");
3270 error_found = true;
3273 if (clones)
3275 cgraph_node *n;
3276 for (n = clones; n; n = n->next_sibling_clone)
3277 if (n->clone_of != this)
3278 break;
3279 if (n)
3281 error ("cgraph_node has wrong clone list");
3282 error_found = true;
3285 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3287 error ("cgraph_node is in clone list but it is not clone");
3288 error_found = true;
3290 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3292 error ("cgraph_node has wrong prev_clone pointer");
3293 error_found = true;
3295 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3297 error ("double linked list of clones corrupted");
3298 error_found = true;
3301 if (analyzed && alias)
3303 bool ref_found = false;
3304 int i;
3305 ipa_ref *ref = NULL;
3307 if (callees)
3309 error ("Alias has call edges");
3310 error_found = true;
3312 for (i = 0; iterate_reference (i, ref); i++)
3313 if (ref->use == IPA_REF_CHKP)
3315 else if (ref->use != IPA_REF_ALIAS)
3317 error ("Alias has non-alias reference");
3318 error_found = true;
3320 else if (ref_found)
3322 error ("Alias has more than one alias reference");
3323 error_found = true;
3325 else
3326 ref_found = true;
3327 if (!ref_found)
3329 error ("Analyzed alias has no reference");
3330 error_found = true;
3334 /* Check instrumented version reference. */
3335 if (instrumented_version
3336 && instrumented_version->instrumented_version != this)
3338 error ("Instrumentation clone does not reference original node");
3339 error_found = true;
3342 /* Cannot have orig_decl for not instrumented nodes. */
3343 if (!instrumentation_clone && orig_decl)
3345 error ("Not instrumented node has non-NULL original declaration");
3346 error_found = true;
3349 /* If original not instrumented node still exists then we may check
3350 original declaration is set properly. */
3351 if (instrumented_version
3352 && orig_decl
3353 && orig_decl != instrumented_version->decl)
3355 error ("Instrumented node has wrong original declaration");
3356 error_found = true;
3359 /* Check all nodes have chkp reference to their instrumented versions. */
3360 if (analyzed
3361 && instrumented_version
3362 && !instrumentation_clone)
3364 bool ref_found = false;
3365 int i;
3366 struct ipa_ref *ref;
3368 for (i = 0; iterate_reference (i, ref); i++)
3369 if (ref->use == IPA_REF_CHKP)
3371 if (ref_found)
3373 error ("Node has more than one chkp reference");
3374 error_found = true;
3376 if (ref->referred != instrumented_version)
3378 error ("Wrong node is referenced with chkp reference");
3379 error_found = true;
3381 ref_found = true;
3384 if (!ref_found)
3386 error ("Analyzed node has no reference to instrumented version");
3387 error_found = true;
3391 if (instrumentation_clone
3392 && DECL_BUILT_IN_CLASS (decl) == NOT_BUILT_IN)
3394 tree name = DECL_ASSEMBLER_NAME (decl);
3395 tree orig_name = DECL_ASSEMBLER_NAME (orig_decl);
3397 if (!IDENTIFIER_TRANSPARENT_ALIAS (name)
3398 || TREE_CHAIN (name) != orig_name)
3400 error ("Alias chain for instrumented node is broken");
3401 error_found = true;
3405 if (analyzed && thunk.thunk_p)
3407 if (!callees)
3409 error ("No edge out of thunk node");
3410 error_found = true;
3412 else if (callees->next_callee)
3414 error ("More than one edge out of thunk node");
3415 error_found = true;
3417 if (gimple_has_body_p (decl) && !global.inlined_to)
3419 error ("Thunk is not supposed to have body");
3420 error_found = true;
3422 if (thunk.add_pointer_bounds_args
3423 && !instrumented_version->semantically_equivalent_p (callees->callee))
3425 error ("Instrumentation thunk has wrong edge callee");
3426 error_found = true;
3429 else if (analyzed && gimple_has_body_p (decl)
3430 && !TREE_ASM_WRITTEN (decl)
3431 && (!DECL_EXTERNAL (decl) || global.inlined_to)
3432 && !flag_wpa)
3434 if (this_cfun->cfg)
3436 hash_set<gimple *> stmts;
3437 int i;
3438 ipa_ref *ref = NULL;
3440 /* Reach the trees by walking over the CFG, and note the
3441 enclosing basic-blocks in the call edges. */
3442 FOR_EACH_BB_FN (this_block, this_cfun)
3444 for (gsi = gsi_start_phis (this_block);
3445 !gsi_end_p (gsi); gsi_next (&gsi))
3446 stmts.add (gsi_stmt (gsi));
3447 for (gsi = gsi_start_bb (this_block);
3448 !gsi_end_p (gsi);
3449 gsi_next (&gsi))
3451 gimple *stmt = gsi_stmt (gsi);
3452 stmts.add (stmt);
3453 if (is_gimple_call (stmt))
3455 cgraph_edge *e = get_edge (stmt);
3456 tree decl = gimple_call_fndecl (stmt);
3457 if (e)
3459 if (e->aux)
3461 error ("shared call_stmt:");
3462 cgraph_debug_gimple_stmt (this_cfun, stmt);
3463 error_found = true;
3465 if (!e->indirect_unknown_callee)
3467 if (e->verify_corresponds_to_fndecl (decl))
3469 error ("edge points to wrong declaration:");
3470 debug_tree (e->callee->decl);
3471 fprintf (stderr," Instead of:");
3472 debug_tree (decl);
3473 error_found = true;
3476 else if (decl)
3478 error ("an indirect edge with unknown callee "
3479 "corresponding to a call_stmt with "
3480 "a known declaration:");
3481 error_found = true;
3482 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3484 e->aux = (void *)1;
3486 else if (decl)
3488 error ("missing callgraph edge for call stmt:");
3489 cgraph_debug_gimple_stmt (this_cfun, stmt);
3490 error_found = true;
3495 for (i = 0; iterate_reference (i, ref); i++)
3496 if (ref->stmt && !stmts.contains (ref->stmt))
3498 error ("reference to dead statement");
3499 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3500 error_found = true;
3503 else
3504 /* No CFG available?! */
3505 gcc_unreachable ();
3507 for (e = callees; e; e = e->next_callee)
3509 if (!e->aux)
3511 error ("edge %s->%s has no corresponding call_stmt",
3512 identifier_to_locale (e->caller->name ()),
3513 identifier_to_locale (e->callee->name ()));
3514 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3515 error_found = true;
3517 e->aux = 0;
3519 for (e = indirect_calls; e; e = e->next_callee)
3521 if (!e->aux && !e->speculative)
3523 error ("an indirect edge from %s has no corresponding call_stmt",
3524 identifier_to_locale (e->caller->name ()));
3525 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3526 error_found = true;
3528 e->aux = 0;
3531 if (error_found)
3533 dump (stderr);
3534 internal_error ("verify_cgraph_node failed");
3536 timevar_pop (TV_CGRAPH_VERIFY);
3539 /* Verify whole cgraph structure. */
3540 DEBUG_FUNCTION void
3541 cgraph_node::verify_cgraph_nodes (void)
3543 cgraph_node *node;
3545 if (seen_error ())
3546 return;
3548 FOR_EACH_FUNCTION (node)
3549 node->verify ();
3552 /* Walk the alias chain to return the function cgraph_node is alias of.
3553 Walk through thunks, too.
3554 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3555 When REF is non-NULL, assume that reference happens in symbol REF
3556 when determining the availability. */
3558 cgraph_node *
3559 cgraph_node::function_symbol (enum availability *availability,
3560 struct symtab_node *ref)
3562 cgraph_node *node = ultimate_alias_target (availability, ref);
3564 while (node->thunk.thunk_p)
3566 ref = node;
3567 node = node->callees->callee;
3568 if (availability)
3570 enum availability a;
3571 a = node->get_availability (ref);
3572 if (a < *availability)
3573 *availability = a;
3575 node = node->ultimate_alias_target (availability, ref);
3577 return node;
3580 /* Walk the alias chain to return the function cgraph_node is alias of.
3581 Walk through non virtual thunks, too. Thus we return either a function
3582 or a virtual thunk node.
3583 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3584 When REF is non-NULL, assume that reference happens in symbol REF
3585 when determining the availability. */
3587 cgraph_node *
3588 cgraph_node::function_or_virtual_thunk_symbol
3589 (enum availability *availability,
3590 struct symtab_node *ref)
3592 cgraph_node *node = ultimate_alias_target (availability, ref);
3594 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3596 ref = node;
3597 node = node->callees->callee;
3598 if (availability)
3600 enum availability a;
3601 a = node->get_availability (ref);
3602 if (a < *availability)
3603 *availability = a;
3605 node = node->ultimate_alias_target (availability, ref);
3607 return node;
3610 /* When doing LTO, read cgraph_node's body from disk if it is not already
3611 present. */
3613 bool
3614 cgraph_node::get_untransformed_body (void)
3616 lto_file_decl_data *file_data;
3617 const char *data, *name;
3618 size_t len;
3619 tree decl = this->decl;
3621 /* Check if body is already there. Either we have gimple body or
3622 the function is thunk and in that case we set DECL_ARGUMENTS. */
3623 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3624 return false;
3626 gcc_assert (in_lto_p && !DECL_RESULT (decl));
3628 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3630 file_data = lto_file_data;
3631 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3633 /* We may have renamed the declaration, e.g., a static function. */
3634 name = lto_get_decl_name_mapping (file_data, name);
3635 struct lto_in_decl_state *decl_state
3636 = lto_get_function_in_decl_state (file_data, decl);
3638 data = lto_get_section_data (file_data, LTO_section_function_body,
3639 name, &len, decl_state->compressed);
3640 if (!data)
3641 fatal_error (input_location, "%s: section %s is missing",
3642 file_data->file_name,
3643 name);
3645 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3647 lto_input_function_body (file_data, this, data);
3648 lto_stats.num_function_bodies++;
3649 lto_free_section_data (file_data, LTO_section_function_body, name,
3650 data, len, decl_state->compressed);
3651 lto_free_function_in_decl_state_for_node (this);
3652 /* Keep lto file data so ipa-inline-analysis knows about cross module
3653 inlining. */
3655 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3657 return true;
3660 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3661 if it is not already present. When some IPA transformations are scheduled,
3662 apply them. */
3664 bool
3665 cgraph_node::get_body (void)
3667 bool updated;
3669 updated = get_untransformed_body ();
3671 /* Getting transformed body makes no sense for inline clones;
3672 we should never use this on real clones because they are materialized
3673 early.
3674 TODO: Materializing clones here will likely lead to smaller LTRANS
3675 footprint. */
3676 gcc_assert (!global.inlined_to && !clone_of);
3677 if (ipa_transforms_to_apply.exists ())
3679 opt_pass *saved_current_pass = current_pass;
3680 FILE *saved_dump_file = dump_file;
3681 const char *saved_dump_file_name = dump_file_name;
3682 dump_flags_t saved_dump_flags = dump_flags;
3683 dump_file_name = NULL;
3684 dump_file = NULL;
3686 push_cfun (DECL_STRUCT_FUNCTION (decl));
3687 execute_all_ipa_transforms ();
3688 cgraph_edge::rebuild_edges ();
3689 free_dominance_info (CDI_DOMINATORS);
3690 free_dominance_info (CDI_POST_DOMINATORS);
3691 pop_cfun ();
3692 updated = true;
3694 current_pass = saved_current_pass;
3695 dump_file = saved_dump_file;
3696 dump_file_name = saved_dump_file_name;
3697 dump_flags = saved_dump_flags;
3699 return updated;
3702 /* Return the DECL_STRUCT_FUNCTION of the function. */
3704 struct function *
3705 cgraph_node::get_fun (void)
3707 cgraph_node *node = this;
3708 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3710 while (!fun && node->clone_of)
3712 node = node->clone_of;
3713 fun = DECL_STRUCT_FUNCTION (node->decl);
3716 return fun;
3719 /* Verify if the type of the argument matches that of the function
3720 declaration. If we cannot verify this or there is a mismatch,
3721 return false. */
3723 static bool
3724 gimple_check_call_args (gimple *stmt, tree fndecl, bool args_count_match)
3726 tree parms, p;
3727 unsigned int i, nargs;
3729 /* Calls to internal functions always match their signature. */
3730 if (gimple_call_internal_p (stmt))
3731 return true;
3733 nargs = gimple_call_num_args (stmt);
3735 /* Get argument types for verification. */
3736 if (fndecl)
3737 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3738 else
3739 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3741 /* Verify if the type of the argument matches that of the function
3742 declaration. If we cannot verify this or there is a mismatch,
3743 return false. */
3744 if (fndecl && DECL_ARGUMENTS (fndecl))
3746 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3747 i < nargs;
3748 i++, p = DECL_CHAIN (p))
3750 tree arg;
3751 /* We cannot distinguish a varargs function from the case
3752 of excess parameters, still deferring the inlining decision
3753 to the callee is possible. */
3754 if (!p)
3755 break;
3756 arg = gimple_call_arg (stmt, i);
3757 if (p == error_mark_node
3758 || DECL_ARG_TYPE (p) == error_mark_node
3759 || arg == error_mark_node
3760 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3761 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3762 return false;
3764 if (args_count_match && p)
3765 return false;
3767 else if (parms)
3769 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3771 tree arg;
3772 /* If this is a varargs function defer inlining decision
3773 to callee. */
3774 if (!p)
3775 break;
3776 arg = gimple_call_arg (stmt, i);
3777 if (TREE_VALUE (p) == error_mark_node
3778 || arg == error_mark_node
3779 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3780 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3781 && !fold_convertible_p (TREE_VALUE (p), arg)))
3782 return false;
3785 else
3787 if (nargs != 0)
3788 return false;
3790 return true;
3793 /* Verify if the type of the argument and lhs of CALL_STMT matches
3794 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3795 true, the arg count needs to be the same.
3796 If we cannot verify this or there is a mismatch, return false. */
3798 bool
3799 gimple_check_call_matching_types (gimple *call_stmt, tree callee,
3800 bool args_count_match)
3802 tree lhs;
3804 if ((DECL_RESULT (callee)
3805 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3806 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3807 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3808 TREE_TYPE (lhs))
3809 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3810 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3811 return false;
3812 return true;
3815 /* Reset all state within cgraph.c so that we can rerun the compiler
3816 within the same process. For use by toplev::finalize. */
3818 void
3819 cgraph_c_finalize (void)
3821 symtab = NULL;
3823 x_cgraph_nodes_queue = NULL;
3825 cgraph_fnver_htab = NULL;
3826 version_info_node = NULL;
3829 /* A wroker for call_for_symbol_and_aliases. */
3831 bool
3832 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
3833 void *),
3834 void *data,
3835 bool include_overwritable)
3837 ipa_ref *ref;
3838 FOR_EACH_ALIAS (this, ref)
3840 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3841 if (include_overwritable
3842 || alias->get_availability () > AVAIL_INTERPOSABLE)
3843 if (alias->call_for_symbol_and_aliases (callback, data,
3844 include_overwritable))
3845 return true;
3847 return false;
3850 /* Return true if NODE has thunk. */
3852 bool
3853 cgraph_node::has_thunk_p (cgraph_node *node, void *)
3855 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3856 if (e->caller->thunk.thunk_p)
3857 return true;
3858 return false;
3861 #include "gt-cgraph.h"