Daily bump.
[official-gcc.git] / gcc / cgraph.c
blob6711aeb828e70f0331c1071503ed232fcec0a855
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));
1318 new_stmt = gimple_ic (e->call_stmt,
1319 dyn_cast<cgraph_node *> (ref->referred),
1320 /* FIXME: cleanup. */
1321 profile_probability::from_reg_br_prob_base (
1322 e->count > profile_count::zero ()
1323 || e2->count > profile_count::zero ()
1324 ? e->count.probability_in
1325 (e->count + e2->count).to_reg_br_prob_base ()
1326 : e->frequency || e2->frequency
1327 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1328 e->frequency + e2->frequency)
1329 : REG_BR_PROB_BASE / 2),
1330 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 avail = AVAIL_INTERPOSABLE;
2324 else if (!externally_visible)
2325 avail = AVAIL_AVAILABLE;
2326 /* If this is a reference from symbol itself and there are no aliases, we
2327 may be sure that the symbol was not interposed by something else because
2328 the symbol itself would be unreachable otherwise.
2330 Also comdat groups are always resolved in groups. */
2331 else if ((this == ref && !has_aliases_p ())
2332 || (ref && get_comdat_group ()
2333 && get_comdat_group () == ref->get_comdat_group ()))
2334 avail = AVAIL_AVAILABLE;
2335 /* Inline functions are safe to be analyzed even if their symbol can
2336 be overwritten at runtime. It is not meaningful to enforce any sane
2337 behavior on replacing inline function by different body. */
2338 else if (DECL_DECLARED_INLINE_P (decl))
2339 avail = AVAIL_AVAILABLE;
2341 /* If the function can be overwritten, return OVERWRITABLE. Take
2342 care at least of two notable extensions - the COMDAT functions
2343 used to share template instantiations in C++ (this is symmetric
2344 to code cp_cannot_inline_tree_fn and probably shall be shared and
2345 the inlinability hooks completely eliminated). */
2347 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2348 avail = AVAIL_INTERPOSABLE;
2349 else avail = AVAIL_AVAILABLE;
2351 return avail;
2354 /* Worker for cgraph_node_can_be_local_p. */
2355 static bool
2356 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2358 return !(!node->force_output
2359 && ((DECL_COMDAT (node->decl)
2360 && !node->forced_by_abi
2361 && !node->used_from_object_file_p ()
2362 && !node->same_comdat_group)
2363 || !node->externally_visible));
2366 /* Return true if cgraph_node can be made local for API change.
2367 Extern inline functions and C++ COMDAT functions can be made local
2368 at the expense of possible code size growth if function is used in multiple
2369 compilation units. */
2370 bool
2371 cgraph_node::can_be_local_p (void)
2373 return (!address_taken
2374 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2375 NULL, true));
2378 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2379 When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2380 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2381 skipped. */
2382 bool
2383 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2384 (cgraph_node *, void *),
2385 void *data,
2386 bool include_overwritable,
2387 bool exclude_virtual_thunks)
2389 cgraph_edge *e;
2390 ipa_ref *ref;
2391 enum availability avail = AVAIL_AVAILABLE;
2393 if (include_overwritable
2394 || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2396 if (callback (this, data))
2397 return true;
2399 FOR_EACH_ALIAS (this, ref)
2401 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2402 if (include_overwritable
2403 || alias->get_availability () > AVAIL_INTERPOSABLE)
2404 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2405 include_overwritable,
2406 exclude_virtual_thunks))
2407 return true;
2409 if (avail <= AVAIL_INTERPOSABLE)
2410 return false;
2411 for (e = callers; e; e = e->next_caller)
2412 if (e->caller->thunk.thunk_p
2413 && (include_overwritable
2414 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2415 && !(exclude_virtual_thunks
2416 && e->caller->thunk.virtual_offset_p))
2417 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2418 include_overwritable,
2419 exclude_virtual_thunks))
2420 return true;
2422 return false;
2425 /* Worker to bring NODE local. */
2427 bool
2428 cgraph_node::make_local (cgraph_node *node, void *)
2430 gcc_checking_assert (node->can_be_local_p ());
2431 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2433 node->make_decl_local ();
2434 node->set_section (NULL);
2435 node->set_comdat_group (NULL);
2436 node->externally_visible = false;
2437 node->forced_by_abi = false;
2438 node->local.local = true;
2439 node->set_section (NULL);
2440 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2441 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2442 && !flag_incremental_link);
2443 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2444 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2446 return false;
2449 /* Bring cgraph node local. */
2451 void
2452 cgraph_node::make_local (void)
2454 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2457 /* Worker to set nothrow flag. */
2459 static void
2460 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2461 bool *changed)
2463 cgraph_edge *e;
2465 if (nothrow && !TREE_NOTHROW (node->decl))
2467 /* With non-call exceptions we can't say for sure if other function body
2468 was not possibly optimized to stil throw. */
2469 if (!non_call || node->binds_to_current_def_p ())
2471 TREE_NOTHROW (node->decl) = true;
2472 *changed = true;
2473 for (e = node->callers; e; e = e->next_caller)
2474 e->can_throw_external = false;
2477 else if (!nothrow && TREE_NOTHROW (node->decl))
2479 TREE_NOTHROW (node->decl) = false;
2480 *changed = true;
2482 ipa_ref *ref;
2483 FOR_EACH_ALIAS (node, ref)
2485 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2486 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2487 set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2489 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2490 if (e->caller->thunk.thunk_p
2491 && (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2492 set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2495 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2496 if any to NOTHROW. */
2498 bool
2499 cgraph_node::set_nothrow_flag (bool nothrow)
2501 bool changed = false;
2502 bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2504 if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2505 set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2506 else
2508 ipa_ref *ref;
2510 FOR_EACH_ALIAS (this, ref)
2512 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2513 if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2514 set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2517 return changed;
2520 /* Worker to set_const_flag. */
2522 static void
2523 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2524 bool *changed)
2526 /* Static constructors and destructors without a side effect can be
2527 optimized out. */
2528 if (set_const && !looping)
2530 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2532 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2533 *changed = true;
2535 if (DECL_STATIC_DESTRUCTOR (node->decl))
2537 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2538 *changed = true;
2541 if (!set_const)
2543 if (TREE_READONLY (node->decl))
2545 TREE_READONLY (node->decl) = 0;
2546 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2547 *changed = true;
2550 else
2552 /* Consider function:
2554 bool a(int *p)
2556 return *p==*p;
2559 During early optimization we will turn this into:
2561 bool a(int *p)
2563 return true;
2566 Now if this function will be detected as CONST however when interposed
2567 it may end up being just pure. We always must assume the worst
2568 scenario here. */
2569 if (TREE_READONLY (node->decl))
2571 if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2573 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2574 *changed = true;
2577 else if (node->binds_to_current_def_p ())
2579 TREE_READONLY (node->decl) = true;
2580 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2581 DECL_PURE_P (node->decl) = false;
2582 *changed = true;
2584 else
2586 if (dump_file && (dump_flags & TDF_DETAILS))
2587 fprintf (dump_file, "Dropping state to PURE because function does "
2588 "not bind to current def.\n");
2589 if (!DECL_PURE_P (node->decl))
2591 DECL_PURE_P (node->decl) = true;
2592 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2593 *changed = true;
2595 else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2597 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2598 *changed = true;
2603 ipa_ref *ref;
2604 FOR_EACH_ALIAS (node, ref)
2606 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2607 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2608 set_const_flag_1 (alias, set_const, looping, changed);
2610 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2611 if (e->caller->thunk.thunk_p
2612 && (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2614 /* Virtual thunks access virtual offset in the vtable, so they can
2615 only be pure, never const. */
2616 if (set_const
2617 && (e->caller->thunk.virtual_offset_p
2618 || !node->binds_to_current_def_p (e->caller)))
2619 *changed |= e->caller->set_pure_flag (true, looping);
2620 else
2621 set_const_flag_1 (e->caller, set_const, looping, changed);
2625 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2626 If SET_CONST if false, clear the flag.
2628 When setting the flag be careful about possible interposition and
2629 do not set the flag for functions that can be interposet and set pure
2630 flag for functions that can bind to other definition.
2632 Return true if any change was done. */
2634 bool
2635 cgraph_node::set_const_flag (bool set_const, bool looping)
2637 bool changed = false;
2638 if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2639 set_const_flag_1 (this, set_const, looping, &changed);
2640 else
2642 ipa_ref *ref;
2644 FOR_EACH_ALIAS (this, ref)
2646 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2647 if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2648 set_const_flag_1 (alias, set_const, looping, &changed);
2651 return changed;
2654 /* Info used by set_pure_flag_1. */
2656 struct set_pure_flag_info
2658 bool pure;
2659 bool looping;
2660 bool changed;
2663 /* Worker to set_pure_flag. */
2665 static bool
2666 set_pure_flag_1 (cgraph_node *node, void *data)
2668 struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2669 /* Static constructors and destructors without a side effect can be
2670 optimized out. */
2671 if (info->pure && !info->looping)
2673 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2675 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2676 info->changed = true;
2678 if (DECL_STATIC_DESTRUCTOR (node->decl))
2680 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2681 info->changed = true;
2684 if (info->pure)
2686 if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2688 DECL_PURE_P (node->decl) = true;
2689 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2690 info->changed = true;
2692 else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2693 && !info->looping)
2695 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2696 info->changed = true;
2699 else
2701 if (DECL_PURE_P (node->decl))
2703 DECL_PURE_P (node->decl) = false;
2704 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2705 info->changed = true;
2708 return false;
2711 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2712 if any to PURE.
2714 When setting the flag, be careful about possible interposition.
2715 Return true if any change was done. */
2717 bool
2718 cgraph_node::set_pure_flag (bool pure, bool looping)
2720 struct set_pure_flag_info info = {pure, looping, false};
2721 if (!pure)
2722 looping = false;
2723 call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2724 return info.changed;
2727 /* Return true when cgraph_node can not return or throw and thus
2728 it is safe to ignore its side effects for IPA analysis. */
2730 bool
2731 cgraph_node::cannot_return_p (void)
2733 int flags = flags_from_decl_or_type (decl);
2734 if (!opt_for_fn (decl, flag_exceptions))
2735 return (flags & ECF_NORETURN) != 0;
2736 else
2737 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2738 == (ECF_NORETURN | ECF_NOTHROW));
2741 /* Return true when call of edge can not lead to return from caller
2742 and thus it is safe to ignore its side effects for IPA analysis
2743 when computing side effects of the caller.
2744 FIXME: We could actually mark all edges that have no reaching
2745 patch to the exit block or throw to get better results. */
2746 bool
2747 cgraph_edge::cannot_lead_to_return_p (void)
2749 if (caller->cannot_return_p ())
2750 return true;
2751 if (indirect_unknown_callee)
2753 int flags = indirect_info->ecf_flags;
2754 if (!opt_for_fn (caller->decl, flag_exceptions))
2755 return (flags & ECF_NORETURN) != 0;
2756 else
2757 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2758 == (ECF_NORETURN | ECF_NOTHROW));
2760 else
2761 return callee->cannot_return_p ();
2764 /* Return true if the call can be hot. */
2766 bool
2767 cgraph_edge::maybe_hot_p (void)
2769 if (!maybe_hot_count_p (NULL, count))
2770 return false;
2771 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2772 || (callee
2773 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2774 return false;
2775 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2776 && (callee
2777 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2778 return false;
2779 if (opt_for_fn (caller->decl, optimize_size))
2780 return false;
2781 if (caller->frequency == NODE_FREQUENCY_HOT)
2782 return true;
2783 /* If profile is now known yet, be conservative.
2784 FIXME: this predicate is used by early inliner and can do better there. */
2785 if (symtab->state < IPA_SSA)
2786 return true;
2787 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE
2788 && frequency < CGRAPH_FREQ_BASE * 3 / 2)
2789 return false;
2790 if (opt_for_fn (caller->decl, flag_guess_branch_prob))
2792 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0
2793 || frequency <= (CGRAPH_FREQ_BASE
2794 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
2795 return false;
2797 return true;
2800 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2802 static bool
2803 nonremovable_p (cgraph_node *node, void *)
2805 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2808 /* Return true if whole comdat group can be removed if there are no direct
2809 calls to THIS. */
2811 bool
2812 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2814 struct ipa_ref *ref;
2816 /* For local symbols or non-comdat group it is the same as
2817 can_remove_if_no_direct_calls_p. */
2818 if (!externally_visible || !same_comdat_group)
2820 if (DECL_EXTERNAL (decl))
2821 return true;
2822 if (address_taken)
2823 return false;
2824 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2827 if (will_inline && address_taken)
2828 return false;
2830 /* Otheriwse check if we can remove the symbol itself and then verify
2831 that only uses of the comdat groups are direct call to THIS
2832 or its aliases. */
2833 if (!can_remove_if_no_direct_calls_and_refs_p ())
2834 return false;
2836 /* Check that all refs come from within the comdat group. */
2837 for (int i = 0; iterate_referring (i, ref); i++)
2838 if (ref->referring->get_comdat_group () != get_comdat_group ())
2839 return false;
2841 struct cgraph_node *target = ultimate_alias_target ();
2842 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2843 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2845 if (!externally_visible)
2846 continue;
2847 if (!next->alias
2848 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2849 return false;
2851 /* If we see different symbol than THIS, be sure to check calls. */
2852 if (next->ultimate_alias_target () != target)
2853 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2854 if (e->caller->get_comdat_group () != get_comdat_group ()
2855 || will_inline)
2856 return false;
2858 /* If function is not being inlined, we care only about
2859 references outside of the comdat group. */
2860 if (!will_inline)
2861 for (int i = 0; next->iterate_referring (i, ref); i++)
2862 if (ref->referring->get_comdat_group () != get_comdat_group ())
2863 return false;
2865 return true;
2868 /* Return true when function cgraph_node can be expected to be removed
2869 from program when direct calls in this compilation unit are removed.
2871 As a special case COMDAT functions are
2872 cgraph_can_remove_if_no_direct_calls_p while the are not
2873 cgraph_only_called_directly_p (it is possible they are called from other
2874 unit)
2876 This function behaves as cgraph_only_called_directly_p because eliminating
2877 all uses of COMDAT function does not make it necessarily disappear from
2878 the program unless we are compiling whole program or we do LTO. In this
2879 case we know we win since dynamic linking will not really discard the
2880 linkonce section. */
2882 bool
2883 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2884 (bool will_inline)
2886 gcc_assert (!global.inlined_to);
2887 if (DECL_EXTERNAL (decl))
2888 return true;
2890 if (!in_lto_p && !flag_whole_program)
2892 /* If the symbol is in comdat group, we need to verify that whole comdat
2893 group becomes unreachable. Technically we could skip references from
2894 within the group, too. */
2895 if (!only_called_directly_p ())
2896 return false;
2897 if (same_comdat_group && externally_visible)
2899 struct cgraph_node *target = ultimate_alias_target ();
2901 if (will_inline && address_taken)
2902 return true;
2903 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2904 next != this;
2905 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2907 if (!externally_visible)
2908 continue;
2909 if (!next->alias
2910 && !next->only_called_directly_p ())
2911 return false;
2913 /* If we see different symbol than THIS,
2914 be sure to check calls. */
2915 if (next->ultimate_alias_target () != target)
2916 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2917 if (e->caller->get_comdat_group () != get_comdat_group ()
2918 || will_inline)
2919 return false;
2922 return true;
2924 else
2925 return can_remove_if_no_direct_calls_p (will_inline);
2929 /* Worker for cgraph_only_called_directly_p. */
2931 static bool
2932 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2934 return !node->only_called_directly_or_aliased_p ();
2937 /* Return true when function cgraph_node and all its aliases are only called
2938 directly.
2939 i.e. it is not externally visible, address was not taken and
2940 it is not used in any other non-standard way. */
2942 bool
2943 cgraph_node::only_called_directly_p (void)
2945 gcc_assert (ultimate_alias_target () == this);
2946 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2947 NULL, true);
2951 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2953 static bool
2954 collect_callers_of_node_1 (cgraph_node *node, void *data)
2956 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2957 cgraph_edge *cs;
2958 enum availability avail;
2959 node->ultimate_alias_target (&avail);
2961 if (avail > AVAIL_INTERPOSABLE)
2962 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2963 if (!cs->indirect_inlining_edge
2964 && !cs->caller->thunk.thunk_p)
2965 redirect_callers->safe_push (cs);
2966 return false;
2969 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2970 cgraph_node (i.e. are not overwritable). */
2972 vec<cgraph_edge *>
2973 cgraph_node::collect_callers (void)
2975 vec<cgraph_edge *> redirect_callers = vNULL;
2976 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2977 &redirect_callers, false);
2978 return redirect_callers;
2981 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2983 static bool
2984 clone_of_p (cgraph_node *node, cgraph_node *node2)
2986 bool skipped_thunk = false;
2987 node = node->ultimate_alias_target ();
2988 node2 = node2->ultimate_alias_target ();
2990 /* There are no virtual clones of thunks so check former_clone_of or if we
2991 might have skipped thunks because this adjustments are no longer
2992 necessary. */
2993 while (node->thunk.thunk_p)
2995 if (node2->former_clone_of == node->decl)
2996 return true;
2997 if (!node->thunk.this_adjusting)
2998 return false;
2999 node = node->callees->callee->ultimate_alias_target ();
3000 skipped_thunk = true;
3003 if (skipped_thunk)
3005 if (!node2->clone.args_to_skip
3006 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
3007 return false;
3008 if (node2->former_clone_of == node->decl)
3009 return true;
3010 else if (!node2->clone_of)
3011 return false;
3014 while (node != node2 && node2)
3015 node2 = node2->clone_of;
3016 return node2 != NULL;
3019 /* Verify edge count and frequency. */
3021 bool
3022 cgraph_edge::verify_count_and_frequency ()
3024 bool error_found = false;
3025 if (count < 0)
3027 error ("caller edge count is negative");
3028 error_found = true;
3030 if (frequency < 0)
3032 error ("caller edge frequency is negative");
3033 error_found = true;
3035 if (frequency > CGRAPH_FREQ_MAX)
3037 error ("caller edge frequency is too large");
3038 error_found = true;
3040 return error_found;
3043 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
3044 static void
3045 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3047 bool fndecl_was_null = false;
3048 /* debug_gimple_stmt needs correct cfun */
3049 if (cfun != this_cfun)
3050 set_cfun (this_cfun);
3051 /* ...and an actual current_function_decl */
3052 if (!current_function_decl)
3054 current_function_decl = this_cfun->decl;
3055 fndecl_was_null = true;
3057 debug_gimple_stmt (stmt);
3058 if (fndecl_was_null)
3059 current_function_decl = NULL;
3062 /* Verify that call graph edge corresponds to DECL from the associated
3063 statement. Return true if the verification should fail. */
3065 bool
3066 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3068 cgraph_node *node;
3070 if (!decl || callee->global.inlined_to)
3071 return false;
3072 if (symtab->state == LTO_STREAMING)
3073 return false;
3074 node = cgraph_node::get (decl);
3076 /* We do not know if a node from a different partition is an alias or what it
3077 aliases and therefore cannot do the former_clone_of check reliably. When
3078 body_removed is set, we have lost all information about what was alias or
3079 thunk of and also cannot proceed. */
3080 if (!node
3081 || node->body_removed
3082 || node->in_other_partition
3083 || callee->icf_merged
3084 || callee->in_other_partition)
3085 return false;
3087 node = node->ultimate_alias_target ();
3089 /* Optimizers can redirect unreachable calls or calls triggering undefined
3090 behavior to builtin_unreachable. */
3091 if (DECL_BUILT_IN_CLASS (callee->decl) == BUILT_IN_NORMAL
3092 && DECL_FUNCTION_CODE (callee->decl) == BUILT_IN_UNREACHABLE)
3093 return false;
3095 if (callee->former_clone_of != node->decl
3096 && (node != callee->ultimate_alias_target ())
3097 && !clone_of_p (node, callee))
3098 return true;
3099 else
3100 return false;
3103 /* Verify cgraph nodes of given cgraph node. */
3104 DEBUG_FUNCTION void
3105 cgraph_node::verify_node (void)
3107 cgraph_edge *e;
3108 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3109 basic_block this_block;
3110 gimple_stmt_iterator gsi;
3111 bool error_found = false;
3113 if (seen_error ())
3114 return;
3116 timevar_push (TV_CGRAPH_VERIFY);
3117 error_found |= verify_base ();
3118 for (e = callees; e; e = e->next_callee)
3119 if (e->aux)
3121 error ("aux field set for edge %s->%s",
3122 identifier_to_locale (e->caller->name ()),
3123 identifier_to_locale (e->callee->name ()));
3124 error_found = true;
3126 if (count < 0)
3128 error ("execution count is negative");
3129 error_found = true;
3131 if (global.inlined_to && same_comdat_group)
3133 error ("inline clone in same comdat group list");
3134 error_found = true;
3136 if (!definition && !in_other_partition && local.local)
3138 error ("local symbols must be defined");
3139 error_found = true;
3141 if (global.inlined_to && externally_visible)
3143 error ("externally visible inline clone");
3144 error_found = true;
3146 if (global.inlined_to && address_taken)
3148 error ("inline clone with address taken");
3149 error_found = true;
3151 if (global.inlined_to && force_output)
3153 error ("inline clone is forced to output");
3154 error_found = true;
3156 for (e = indirect_calls; e; e = e->next_callee)
3158 if (e->aux)
3160 error ("aux field set for indirect edge from %s",
3161 identifier_to_locale (e->caller->name ()));
3162 error_found = true;
3164 if (!e->indirect_unknown_callee
3165 || !e->indirect_info)
3167 error ("An indirect edge from %s is not marked as indirect or has "
3168 "associated indirect_info, the corresponding statement is: ",
3169 identifier_to_locale (e->caller->name ()));
3170 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3171 error_found = true;
3174 bool check_comdat = comdat_local_p ();
3175 for (e = callers; e; e = e->next_caller)
3177 if (e->verify_count_and_frequency ())
3178 error_found = true;
3179 if (check_comdat
3180 && !in_same_comdat_group_p (e->caller))
3182 error ("comdat-local function called by %s outside its comdat",
3183 identifier_to_locale (e->caller->name ()));
3184 error_found = true;
3186 if (!e->inline_failed)
3188 if (global.inlined_to
3189 != (e->caller->global.inlined_to
3190 ? e->caller->global.inlined_to : e->caller))
3192 error ("inlined_to pointer is wrong");
3193 error_found = true;
3195 if (callers->next_caller)
3197 error ("multiple inline callers");
3198 error_found = true;
3201 else
3202 if (global.inlined_to)
3204 error ("inlined_to pointer set for noninline callers");
3205 error_found = true;
3208 for (e = callees; e; e = e->next_callee)
3210 if (e->verify_count_and_frequency ())
3211 error_found = true;
3212 if (gimple_has_body_p (e->caller->decl)
3213 && !e->caller->global.inlined_to
3214 && !e->speculative
3215 /* Optimized out calls are redirected to __builtin_unreachable. */
3216 && (e->frequency
3217 || ! e->callee->decl
3218 || DECL_BUILT_IN_CLASS (e->callee->decl) != BUILT_IN_NORMAL
3219 || DECL_FUNCTION_CODE (e->callee->decl) != BUILT_IN_UNREACHABLE)
3220 && (e->frequency
3221 != compute_call_stmt_bb_frequency (e->caller->decl,
3222 gimple_bb (e->call_stmt))))
3224 error ("caller edge frequency %i does not match BB frequency %i",
3225 e->frequency,
3226 compute_call_stmt_bb_frequency (e->caller->decl,
3227 gimple_bb (e->call_stmt)));
3228 error_found = true;
3231 for (e = indirect_calls; e; e = e->next_callee)
3233 if (e->verify_count_and_frequency ())
3234 error_found = true;
3235 if (gimple_has_body_p (e->caller->decl)
3236 && !e->caller->global.inlined_to
3237 && !e->speculative
3238 && (e->frequency
3239 != compute_call_stmt_bb_frequency (e->caller->decl,
3240 gimple_bb (e->call_stmt))))
3242 error ("indirect call frequency %i does not match BB frequency %i",
3243 e->frequency,
3244 compute_call_stmt_bb_frequency (e->caller->decl,
3245 gimple_bb (e->call_stmt)));
3246 error_found = true;
3249 if (!callers && global.inlined_to)
3251 error ("inlined_to pointer is set but no predecessors found");
3252 error_found = true;
3254 if (global.inlined_to == this)
3256 error ("inlined_to pointer refers to itself");
3257 error_found = true;
3260 if (clone_of)
3262 cgraph_node *n;
3263 for (n = clone_of->clones; n; n = n->next_sibling_clone)
3264 if (n == this)
3265 break;
3266 if (!n)
3268 error ("cgraph_node has wrong clone_of");
3269 error_found = true;
3272 if (clones)
3274 cgraph_node *n;
3275 for (n = clones; n; n = n->next_sibling_clone)
3276 if (n->clone_of != this)
3277 break;
3278 if (n)
3280 error ("cgraph_node has wrong clone list");
3281 error_found = true;
3284 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3286 error ("cgraph_node is in clone list but it is not clone");
3287 error_found = true;
3289 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3291 error ("cgraph_node has wrong prev_clone pointer");
3292 error_found = true;
3294 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3296 error ("double linked list of clones corrupted");
3297 error_found = true;
3300 if (analyzed && alias)
3302 bool ref_found = false;
3303 int i;
3304 ipa_ref *ref = NULL;
3306 if (callees)
3308 error ("Alias has call edges");
3309 error_found = true;
3311 for (i = 0; iterate_reference (i, ref); i++)
3312 if (ref->use == IPA_REF_CHKP)
3314 else if (ref->use != IPA_REF_ALIAS)
3316 error ("Alias has non-alias reference");
3317 error_found = true;
3319 else if (ref_found)
3321 error ("Alias has more than one alias reference");
3322 error_found = true;
3324 else
3325 ref_found = true;
3326 if (!ref_found)
3328 error ("Analyzed alias has no reference");
3329 error_found = true;
3333 /* Check instrumented version reference. */
3334 if (instrumented_version
3335 && instrumented_version->instrumented_version != this)
3337 error ("Instrumentation clone does not reference original node");
3338 error_found = true;
3341 /* Cannot have orig_decl for not instrumented nodes. */
3342 if (!instrumentation_clone && orig_decl)
3344 error ("Not instrumented node has non-NULL original declaration");
3345 error_found = true;
3348 /* If original not instrumented node still exists then we may check
3349 original declaration is set properly. */
3350 if (instrumented_version
3351 && orig_decl
3352 && orig_decl != instrumented_version->decl)
3354 error ("Instrumented node has wrong original declaration");
3355 error_found = true;
3358 /* Check all nodes have chkp reference to their instrumented versions. */
3359 if (analyzed
3360 && instrumented_version
3361 && !instrumentation_clone)
3363 bool ref_found = false;
3364 int i;
3365 struct ipa_ref *ref;
3367 for (i = 0; iterate_reference (i, ref); i++)
3368 if (ref->use == IPA_REF_CHKP)
3370 if (ref_found)
3372 error ("Node has more than one chkp reference");
3373 error_found = true;
3375 if (ref->referred != instrumented_version)
3377 error ("Wrong node is referenced with chkp reference");
3378 error_found = true;
3380 ref_found = true;
3383 if (!ref_found)
3385 error ("Analyzed node has no reference to instrumented version");
3386 error_found = true;
3390 if (instrumentation_clone
3391 && DECL_BUILT_IN_CLASS (decl) == NOT_BUILT_IN)
3393 tree name = DECL_ASSEMBLER_NAME (decl);
3394 tree orig_name = DECL_ASSEMBLER_NAME (orig_decl);
3396 if (!IDENTIFIER_TRANSPARENT_ALIAS (name)
3397 || TREE_CHAIN (name) != orig_name)
3399 error ("Alias chain for instrumented node is broken");
3400 error_found = true;
3404 if (analyzed && thunk.thunk_p)
3406 if (!callees)
3408 error ("No edge out of thunk node");
3409 error_found = true;
3411 else if (callees->next_callee)
3413 error ("More than one edge out of thunk node");
3414 error_found = true;
3416 if (gimple_has_body_p (decl) && !global.inlined_to)
3418 error ("Thunk is not supposed to have body");
3419 error_found = true;
3421 if (thunk.add_pointer_bounds_args
3422 && !instrumented_version->semantically_equivalent_p (callees->callee))
3424 error ("Instrumentation thunk has wrong edge callee");
3425 error_found = true;
3428 else if (analyzed && gimple_has_body_p (decl)
3429 && !TREE_ASM_WRITTEN (decl)
3430 && (!DECL_EXTERNAL (decl) || global.inlined_to)
3431 && !flag_wpa)
3433 if (this_cfun->cfg)
3435 hash_set<gimple *> stmts;
3436 int i;
3437 ipa_ref *ref = NULL;
3439 /* Reach the trees by walking over the CFG, and note the
3440 enclosing basic-blocks in the call edges. */
3441 FOR_EACH_BB_FN (this_block, this_cfun)
3443 for (gsi = gsi_start_phis (this_block);
3444 !gsi_end_p (gsi); gsi_next (&gsi))
3445 stmts.add (gsi_stmt (gsi));
3446 for (gsi = gsi_start_bb (this_block);
3447 !gsi_end_p (gsi);
3448 gsi_next (&gsi))
3450 gimple *stmt = gsi_stmt (gsi);
3451 stmts.add (stmt);
3452 if (is_gimple_call (stmt))
3454 cgraph_edge *e = get_edge (stmt);
3455 tree decl = gimple_call_fndecl (stmt);
3456 if (e)
3458 if (e->aux)
3460 error ("shared call_stmt:");
3461 cgraph_debug_gimple_stmt (this_cfun, stmt);
3462 error_found = true;
3464 if (!e->indirect_unknown_callee)
3466 if (e->verify_corresponds_to_fndecl (decl))
3468 error ("edge points to wrong declaration:");
3469 debug_tree (e->callee->decl);
3470 fprintf (stderr," Instead of:");
3471 debug_tree (decl);
3472 error_found = true;
3475 else if (decl)
3477 error ("an indirect edge with unknown callee "
3478 "corresponding to a call_stmt with "
3479 "a known declaration:");
3480 error_found = true;
3481 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3483 e->aux = (void *)1;
3485 else if (decl)
3487 error ("missing callgraph edge for call stmt:");
3488 cgraph_debug_gimple_stmt (this_cfun, stmt);
3489 error_found = true;
3494 for (i = 0; iterate_reference (i, ref); i++)
3495 if (ref->stmt && !stmts.contains (ref->stmt))
3497 error ("reference to dead statement");
3498 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3499 error_found = true;
3502 else
3503 /* No CFG available?! */
3504 gcc_unreachable ();
3506 for (e = callees; e; e = e->next_callee)
3508 if (!e->aux)
3510 error ("edge %s->%s has no corresponding call_stmt",
3511 identifier_to_locale (e->caller->name ()),
3512 identifier_to_locale (e->callee->name ()));
3513 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3514 error_found = true;
3516 e->aux = 0;
3518 for (e = indirect_calls; e; e = e->next_callee)
3520 if (!e->aux && !e->speculative)
3522 error ("an indirect edge from %s has no corresponding call_stmt",
3523 identifier_to_locale (e->caller->name ()));
3524 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3525 error_found = true;
3527 e->aux = 0;
3530 if (error_found)
3532 dump (stderr);
3533 internal_error ("verify_cgraph_node failed");
3535 timevar_pop (TV_CGRAPH_VERIFY);
3538 /* Verify whole cgraph structure. */
3539 DEBUG_FUNCTION void
3540 cgraph_node::verify_cgraph_nodes (void)
3542 cgraph_node *node;
3544 if (seen_error ())
3545 return;
3547 FOR_EACH_FUNCTION (node)
3548 node->verify ();
3551 /* Walk the alias chain to return the function cgraph_node is alias of.
3552 Walk through thunks, too.
3553 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3554 When REF is non-NULL, assume that reference happens in symbol REF
3555 when determining the availability. */
3557 cgraph_node *
3558 cgraph_node::function_symbol (enum availability *availability,
3559 struct symtab_node *ref)
3561 cgraph_node *node = ultimate_alias_target (availability, ref);
3563 while (node->thunk.thunk_p)
3565 ref = node;
3566 node = node->callees->callee;
3567 if (availability)
3569 enum availability a;
3570 a = node->get_availability (ref);
3571 if (a < *availability)
3572 *availability = a;
3574 node = node->ultimate_alias_target (availability, ref);
3576 return node;
3579 /* Walk the alias chain to return the function cgraph_node is alias of.
3580 Walk through non virtual thunks, too. Thus we return either a function
3581 or a virtual thunk node.
3582 When AVAILABILITY is non-NULL, get minimal availability in the chain.
3583 When REF is non-NULL, assume that reference happens in symbol REF
3584 when determining the availability. */
3586 cgraph_node *
3587 cgraph_node::function_or_virtual_thunk_symbol
3588 (enum availability *availability,
3589 struct symtab_node *ref)
3591 cgraph_node *node = ultimate_alias_target (availability, ref);
3593 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3595 ref = node;
3596 node = node->callees->callee;
3597 if (availability)
3599 enum availability a;
3600 a = node->get_availability (ref);
3601 if (a < *availability)
3602 *availability = a;
3604 node = node->ultimate_alias_target (availability, ref);
3606 return node;
3609 /* When doing LTO, read cgraph_node's body from disk if it is not already
3610 present. */
3612 bool
3613 cgraph_node::get_untransformed_body (void)
3615 lto_file_decl_data *file_data;
3616 const char *data, *name;
3617 size_t len;
3618 tree decl = this->decl;
3620 /* Check if body is already there. Either we have gimple body or
3621 the function is thunk and in that case we set DECL_ARGUMENTS. */
3622 if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3623 return false;
3625 gcc_assert (in_lto_p && !DECL_RESULT (decl));
3627 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3629 file_data = lto_file_data;
3630 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3632 /* We may have renamed the declaration, e.g., a static function. */
3633 name = lto_get_decl_name_mapping (file_data, name);
3634 struct lto_in_decl_state *decl_state
3635 = lto_get_function_in_decl_state (file_data, decl);
3637 data = lto_get_section_data (file_data, LTO_section_function_body,
3638 name, &len, decl_state->compressed);
3639 if (!data)
3640 fatal_error (input_location, "%s: section %s is missing",
3641 file_data->file_name,
3642 name);
3644 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3646 lto_input_function_body (file_data, this, data);
3647 lto_stats.num_function_bodies++;
3648 lto_free_section_data (file_data, LTO_section_function_body, name,
3649 data, len, decl_state->compressed);
3650 lto_free_function_in_decl_state_for_node (this);
3651 /* Keep lto file data so ipa-inline-analysis knows about cross module
3652 inlining. */
3654 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3656 return true;
3659 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3660 if it is not already present. When some IPA transformations are scheduled,
3661 apply them. */
3663 bool
3664 cgraph_node::get_body (void)
3666 bool updated;
3668 updated = get_untransformed_body ();
3670 /* Getting transformed body makes no sense for inline clones;
3671 we should never use this on real clones because they are materialized
3672 early.
3673 TODO: Materializing clones here will likely lead to smaller LTRANS
3674 footprint. */
3675 gcc_assert (!global.inlined_to && !clone_of);
3676 if (ipa_transforms_to_apply.exists ())
3678 opt_pass *saved_current_pass = current_pass;
3679 FILE *saved_dump_file = dump_file;
3680 const char *saved_dump_file_name = dump_file_name;
3681 dump_flags_t saved_dump_flags = dump_flags;
3682 dump_file_name = NULL;
3683 dump_file = NULL;
3685 push_cfun (DECL_STRUCT_FUNCTION (decl));
3686 execute_all_ipa_transforms ();
3687 cgraph_edge::rebuild_edges ();
3688 free_dominance_info (CDI_DOMINATORS);
3689 free_dominance_info (CDI_POST_DOMINATORS);
3690 pop_cfun ();
3691 updated = true;
3693 current_pass = saved_current_pass;
3694 dump_file = saved_dump_file;
3695 dump_file_name = saved_dump_file_name;
3696 dump_flags = saved_dump_flags;
3698 return updated;
3701 /* Return the DECL_STRUCT_FUNCTION of the function. */
3703 struct function *
3704 cgraph_node::get_fun (void)
3706 cgraph_node *node = this;
3707 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3709 while (!fun && node->clone_of)
3711 node = node->clone_of;
3712 fun = DECL_STRUCT_FUNCTION (node->decl);
3715 return fun;
3718 /* Verify if the type of the argument matches that of the function
3719 declaration. If we cannot verify this or there is a mismatch,
3720 return false. */
3722 static bool
3723 gimple_check_call_args (gimple *stmt, tree fndecl, bool args_count_match)
3725 tree parms, p;
3726 unsigned int i, nargs;
3728 /* Calls to internal functions always match their signature. */
3729 if (gimple_call_internal_p (stmt))
3730 return true;
3732 nargs = gimple_call_num_args (stmt);
3734 /* Get argument types for verification. */
3735 if (fndecl)
3736 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3737 else
3738 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3740 /* Verify if the type of the argument matches that of the function
3741 declaration. If we cannot verify this or there is a mismatch,
3742 return false. */
3743 if (fndecl && DECL_ARGUMENTS (fndecl))
3745 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3746 i < nargs;
3747 i++, p = DECL_CHAIN (p))
3749 tree arg;
3750 /* We cannot distinguish a varargs function from the case
3751 of excess parameters, still deferring the inlining decision
3752 to the callee is possible. */
3753 if (!p)
3754 break;
3755 arg = gimple_call_arg (stmt, i);
3756 if (p == error_mark_node
3757 || DECL_ARG_TYPE (p) == error_mark_node
3758 || arg == error_mark_node
3759 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3760 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3761 return false;
3763 if (args_count_match && p)
3764 return false;
3766 else if (parms)
3768 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3770 tree arg;
3771 /* If this is a varargs function defer inlining decision
3772 to callee. */
3773 if (!p)
3774 break;
3775 arg = gimple_call_arg (stmt, i);
3776 if (TREE_VALUE (p) == error_mark_node
3777 || arg == error_mark_node
3778 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3779 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3780 && !fold_convertible_p (TREE_VALUE (p), arg)))
3781 return false;
3784 else
3786 if (nargs != 0)
3787 return false;
3789 return true;
3792 /* Verify if the type of the argument and lhs of CALL_STMT matches
3793 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3794 true, the arg count needs to be the same.
3795 If we cannot verify this or there is a mismatch, return false. */
3797 bool
3798 gimple_check_call_matching_types (gimple *call_stmt, tree callee,
3799 bool args_count_match)
3801 tree lhs;
3803 if ((DECL_RESULT (callee)
3804 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3805 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3806 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3807 TREE_TYPE (lhs))
3808 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3809 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3810 return false;
3811 return true;
3814 /* Reset all state within cgraph.c so that we can rerun the compiler
3815 within the same process. For use by toplev::finalize. */
3817 void
3818 cgraph_c_finalize (void)
3820 symtab = NULL;
3822 x_cgraph_nodes_queue = NULL;
3824 cgraph_fnver_htab = NULL;
3825 version_info_node = NULL;
3828 /* A wroker for call_for_symbol_and_aliases. */
3830 bool
3831 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
3832 void *),
3833 void *data,
3834 bool include_overwritable)
3836 ipa_ref *ref;
3837 FOR_EACH_ALIAS (this, ref)
3839 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3840 if (include_overwritable
3841 || alias->get_availability () > AVAIL_INTERPOSABLE)
3842 if (alias->call_for_symbol_and_aliases (callback, data,
3843 include_overwritable))
3844 return true;
3846 return false;
3849 /* Return true if NODE has thunk. */
3851 bool
3852 cgraph_node::has_thunk_p (cgraph_node *node, void *)
3854 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3855 if (e->caller->thunk.thunk_p)
3856 return true;
3857 return false;
3860 #include "gt-cgraph.h"