* tree.h (TYPE_OVERFLOW_SANITIZED): Define.
[official-gcc.git] / gcc / cgraph.c
blob7216b89718436157bfab5339129192328daabbcf
1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 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 intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "varasm.h"
32 #include "calls.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "toplev.h"
39 #include "flags.h"
40 #include "debug.h"
41 #include "target.h"
42 #include "predict.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "basic-block.h"
46 #include "hash-map.h"
47 #include "is-a.h"
48 #include "plugin-api.h"
49 #include "vec.h"
50 #include "machmode.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "intl.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "tree-eh.h"
60 #include "gimple-expr.h"
61 #include "gimple.h"
62 #include "gimple-iterator.h"
63 #include "timevar.h"
64 #include "dumpfile.h"
65 #include "gimple-ssa.h"
66 #include "tree-cfg.h"
67 #include "tree-ssa.h"
68 #include "value-prof.h"
69 #include "except.h"
70 #include "diagnostic-core.h"
71 #include "rtl.h"
72 #include "ipa-utils.h"
73 #include "lto-streamer.h"
74 #include "alloc-pool.h"
75 #include "ipa-prop.h"
76 #include "ipa-inline.h"
77 #include "cfgloop.h"
78 #include "gimple-pretty-print.h"
79 #include "expr.h"
80 #include "tree-dfa.h"
81 #include "profile.h"
82 #include "params.h"
83 #include "tree-chkp.h"
85 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
86 #include "tree-pass.h"
88 /* Queue of cgraph nodes scheduled to be lowered. */
89 symtab_node *x_cgraph_nodes_queue;
90 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
92 /* Symbol table global context. */
93 symbol_table *symtab;
95 /* List of hooks triggered on cgraph_edge events. */
96 struct cgraph_edge_hook_list {
97 cgraph_edge_hook hook;
98 void *data;
99 struct cgraph_edge_hook_list *next;
102 /* List of hooks triggered on cgraph_node events. */
103 struct cgraph_node_hook_list {
104 cgraph_node_hook hook;
105 void *data;
106 struct cgraph_node_hook_list *next;
109 /* List of hooks triggered on events involving two cgraph_edges. */
110 struct cgraph_2edge_hook_list {
111 cgraph_2edge_hook hook;
112 void *data;
113 struct cgraph_2edge_hook_list *next;
116 /* List of hooks triggered on events involving two cgraph_nodes. */
117 struct cgraph_2node_hook_list {
118 cgraph_2node_hook hook;
119 void *data;
120 struct cgraph_2node_hook_list *next;
123 /* Hash descriptor for cgraph_function_version_info. */
125 struct function_version_hasher : ggc_hasher<cgraph_function_version_info *>
127 static hashval_t hash (cgraph_function_version_info *);
128 static bool equal (cgraph_function_version_info *,
129 cgraph_function_version_info *);
132 /* Map a cgraph_node to cgraph_function_version_info using this htab.
133 The cgraph_function_version_info has a THIS_NODE field that is the
134 corresponding cgraph_node.. */
136 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
138 /* Hash function for cgraph_fnver_htab. */
139 hashval_t
140 function_version_hasher::hash (cgraph_function_version_info *ptr)
142 int uid = ptr->this_node->uid;
143 return (hashval_t)(uid);
146 /* eq function for cgraph_fnver_htab. */
147 bool
148 function_version_hasher::equal (cgraph_function_version_info *n1,
149 cgraph_function_version_info *n2)
151 return n1->this_node->uid == n2->this_node->uid;
154 /* Mark as GC root all allocated nodes. */
155 static GTY(()) struct cgraph_function_version_info *
156 version_info_node = NULL;
158 /* Get the cgraph_function_version_info node corresponding to node. */
159 cgraph_function_version_info *
160 cgraph_node::function_version (void)
162 cgraph_function_version_info key;
163 key.this_node = this;
165 if (cgraph_fnver_htab == NULL)
166 return NULL;
168 return cgraph_fnver_htab->find (&key);
171 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
172 corresponding to cgraph_node NODE. */
173 cgraph_function_version_info *
174 cgraph_node::insert_new_function_version (void)
176 version_info_node = NULL;
177 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
178 version_info_node->this_node = this;
180 if (cgraph_fnver_htab == NULL)
181 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
183 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
184 = version_info_node;
185 return version_info_node;
188 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
189 DECL is a duplicate declaration. */
190 void
191 cgraph_node::delete_function_version (tree decl)
193 cgraph_node *decl_node = cgraph_node::get (decl);
194 cgraph_function_version_info *decl_v = NULL;
196 if (decl_node == NULL)
197 return;
199 decl_v = decl_node->function_version ();
201 if (decl_v == NULL)
202 return;
204 if (decl_v->prev != NULL)
205 decl_v->prev->next = decl_v->next;
207 if (decl_v->next != NULL)
208 decl_v->next->prev = decl_v->prev;
210 if (cgraph_fnver_htab != NULL)
211 cgraph_fnver_htab->remove_elt (decl_v);
213 decl_node->remove ();
216 /* Record that DECL1 and DECL2 are semantically identical function
217 versions. */
218 void
219 cgraph_node::record_function_versions (tree decl1, tree decl2)
221 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
222 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
223 cgraph_function_version_info *decl1_v = NULL;
224 cgraph_function_version_info *decl2_v = NULL;
225 cgraph_function_version_info *before;
226 cgraph_function_version_info *after;
228 gcc_assert (decl1_node != NULL && decl2_node != NULL);
229 decl1_v = decl1_node->function_version ();
230 decl2_v = decl2_node->function_version ();
232 if (decl1_v != NULL && decl2_v != NULL)
233 return;
235 if (decl1_v == NULL)
236 decl1_v = decl1_node->insert_new_function_version ();
238 if (decl2_v == NULL)
239 decl2_v = decl2_node->insert_new_function_version ();
241 /* Chain decl2_v and decl1_v. All semantically identical versions
242 will be chained together. */
244 before = decl1_v;
245 after = decl2_v;
247 while (before->next != NULL)
248 before = before->next;
250 while (after->prev != NULL)
251 after= after->prev;
253 before->next = after;
254 after->prev = before;
257 /* Initialize callgraph dump file. */
259 void
260 symbol_table::initialize (void)
262 if (!dump_file)
263 dump_file = dump_begin (TDI_cgraph, NULL);
266 /* Allocate new callgraph node and insert it into basic data structures. */
268 cgraph_node *
269 symbol_table::create_empty (void)
271 cgraph_node *node = allocate_cgraph_symbol ();
273 node->type = SYMTAB_FUNCTION;
274 node->frequency = NODE_FREQUENCY_NORMAL;
275 node->count_materialization_scale = REG_BR_PROB_BASE;
276 cgraph_count++;
278 return node;
281 /* Register HOOK to be called with DATA on each removed edge. */
282 cgraph_edge_hook_list *
283 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
285 cgraph_edge_hook_list *entry;
286 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
288 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
289 entry->hook = hook;
290 entry->data = data;
291 entry->next = NULL;
292 while (*ptr)
293 ptr = &(*ptr)->next;
294 *ptr = entry;
295 return entry;
298 /* Remove ENTRY from the list of hooks called on removing edges. */
299 void
300 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
302 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
304 while (*ptr != entry)
305 ptr = &(*ptr)->next;
306 *ptr = entry->next;
307 free (entry);
310 /* Call all edge removal hooks. */
311 void
312 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
314 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
315 while (entry)
317 entry->hook (e, entry->data);
318 entry = entry->next;
322 /* Register HOOK to be called with DATA on each removed node. */
323 cgraph_node_hook_list *
324 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
326 cgraph_node_hook_list *entry;
327 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
329 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
330 entry->hook = hook;
331 entry->data = data;
332 entry->next = NULL;
333 while (*ptr)
334 ptr = &(*ptr)->next;
335 *ptr = entry;
336 return entry;
339 /* Remove ENTRY from the list of hooks called on removing nodes. */
340 void
341 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
343 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
345 while (*ptr != entry)
346 ptr = &(*ptr)->next;
347 *ptr = entry->next;
348 free (entry);
351 /* Call all node removal hooks. */
352 void
353 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
355 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
356 while (entry)
358 entry->hook (node, entry->data);
359 entry = entry->next;
363 /* Call all node removal hooks. */
364 void
365 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
367 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
368 while (entry)
370 entry->hook (node, entry->data);
371 entry = entry->next;
376 /* Register HOOK to be called with DATA on each inserted node. */
377 cgraph_node_hook_list *
378 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
380 cgraph_node_hook_list *entry;
381 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
383 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
384 entry->hook = hook;
385 entry->data = data;
386 entry->next = NULL;
387 while (*ptr)
388 ptr = &(*ptr)->next;
389 *ptr = entry;
390 return entry;
393 /* Remove ENTRY from the list of hooks called on inserted nodes. */
394 void
395 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
397 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
399 while (*ptr != entry)
400 ptr = &(*ptr)->next;
401 *ptr = entry->next;
402 free (entry);
405 /* Register HOOK to be called with DATA on each duplicated edge. */
406 cgraph_2edge_hook_list *
407 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
409 cgraph_2edge_hook_list *entry;
410 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
412 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
413 entry->hook = hook;
414 entry->data = data;
415 entry->next = NULL;
416 while (*ptr)
417 ptr = &(*ptr)->next;
418 *ptr = entry;
419 return entry;
422 /* Remove ENTRY from the list of hooks called on duplicating edges. */
423 void
424 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
426 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
428 while (*ptr != entry)
429 ptr = &(*ptr)->next;
430 *ptr = entry->next;
431 free (entry);
434 /* Call all edge duplication hooks. */
435 void
436 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
438 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
439 while (entry)
441 entry->hook (cs1, cs2, entry->data);
442 entry = entry->next;
446 /* Register HOOK to be called with DATA on each duplicated node. */
447 cgraph_2node_hook_list *
448 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
450 cgraph_2node_hook_list *entry;
451 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
453 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
454 entry->hook = hook;
455 entry->data = data;
456 entry->next = NULL;
457 while (*ptr)
458 ptr = &(*ptr)->next;
459 *ptr = entry;
460 return entry;
463 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
464 void
465 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
467 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
469 while (*ptr != entry)
470 ptr = &(*ptr)->next;
471 *ptr = entry->next;
472 free (entry);
475 /* Call all node duplication hooks. */
476 void
477 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
478 cgraph_node *node2)
480 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
481 while (entry)
483 entry->hook (node, node2, entry->data);
484 entry = entry->next;
488 /* Return cgraph node assigned to DECL. Create new one when needed. */
490 cgraph_node *
491 cgraph_node::create (tree decl)
493 cgraph_node *node = symtab->create_empty ();
494 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
496 node->decl = decl;
497 node->register_symbol ();
499 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
501 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
502 node->next_nested = node->origin->nested;
503 node->origin->nested = node;
505 return node;
508 /* Try to find a call graph node for declaration DECL and if it does not exist
509 or if it corresponds to an inline clone, create a new one. */
511 cgraph_node *
512 cgraph_node::get_create (tree decl)
514 cgraph_node *first_clone = cgraph_node::get (decl);
516 if (first_clone && !first_clone->global.inlined_to)
517 return first_clone;
519 cgraph_node *node = cgraph_node::create (decl);
520 if (first_clone)
522 first_clone->clone_of = node;
523 node->clones = first_clone;
524 symtab->symtab_prevail_in_asm_name_hash (node);
525 node->decl->decl_with_vis.symtab_node = node;
526 if (dump_file)
527 fprintf (dump_file, "Introduced new external node "
528 "(%s/%i) and turned into root of the clone tree.\n",
529 xstrdup (node->name ()), node->order);
531 else if (dump_file)
532 fprintf (dump_file, "Introduced new external node "
533 "(%s/%i).\n", xstrdup (node->name ()),
534 node->order);
535 return node;
538 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
539 the function body is associated with (not necessarily cgraph_node (DECL). */
541 cgraph_node *
542 cgraph_node::create_alias (tree alias, tree target)
544 cgraph_node *alias_node;
546 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
547 || TREE_CODE (target) == IDENTIFIER_NODE);
548 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
549 alias_node = cgraph_node::get_create (alias);
550 gcc_assert (!alias_node->definition);
551 alias_node->alias_target = target;
552 alias_node->definition = true;
553 alias_node->alias = true;
554 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
555 alias_node->weakref = true;
556 return alias_node;
559 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
560 and NULL otherwise.
561 Same body aliases are output whenever the body of DECL is output,
562 and cgraph_node::get (ALIAS) transparently returns
563 cgraph_node::get (DECL). */
565 cgraph_node *
566 cgraph_node::create_same_body_alias (tree alias, tree decl)
568 cgraph_node *n;
569 #ifndef ASM_OUTPUT_DEF
570 /* If aliases aren't supported by the assembler, fail. */
571 return NULL;
572 #endif
573 /* Langhooks can create same body aliases of symbols not defined.
574 Those are useless. Drop them on the floor. */
575 if (symtab->global_info_ready)
576 return NULL;
578 n = cgraph_node::create_alias (alias, decl);
579 n->cpp_implicit_alias = true;
580 if (symtab->cpp_implicit_aliases_done)
581 n->resolve_alias (cgraph_node::get (decl));
582 return n;
585 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
586 aliases DECL with an adjustments made into the first parameter.
587 See comments in thunk_adjust for detail on the parameters. */
589 cgraph_node *
590 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
591 HOST_WIDE_INT fixed_offset,
592 HOST_WIDE_INT virtual_value,
593 tree virtual_offset,
594 tree real_alias)
596 cgraph_node *node;
598 node = cgraph_node::get (alias);
599 if (node)
600 node->reset ();
601 else
602 node = cgraph_node::create (alias);
603 gcc_checking_assert (!virtual_offset
604 || wi::eq_p (virtual_offset, virtual_value));
605 node->thunk.fixed_offset = fixed_offset;
606 node->thunk.this_adjusting = this_adjusting;
607 node->thunk.virtual_value = virtual_value;
608 node->thunk.virtual_offset_p = virtual_offset != NULL;
609 node->thunk.alias = real_alias;
610 node->thunk.thunk_p = true;
611 node->definition = true;
613 return node;
616 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
617 Return NULL if there's no such node. */
619 cgraph_node *
620 cgraph_node::get_for_asmname (tree asmname)
622 /* We do not want to look at inline clones. */
623 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
624 node;
625 node = node->next_sharing_asm_name)
627 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
628 if (cn && !cn->global.inlined_to)
629 return cn;
631 return NULL;
634 /* Returns a hash value for X (which really is a cgraph_edge). */
636 hashval_t
637 cgraph_edge_hasher::hash (cgraph_edge *e)
639 return htab_hash_pointer (e->call_stmt);
642 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
644 inline bool
645 cgraph_edge_hasher::equal (cgraph_edge *x, gimple y)
647 return x->call_stmt == y;
650 /* Add call graph edge E to call site hash of its caller. */
652 static inline void
653 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
655 gimple call = e->call_stmt;
656 *e->caller->call_site_hash->find_slot_with_hash (call,
657 htab_hash_pointer (call),
658 INSERT) = e;
661 /* Add call graph edge E to call site hash of its caller. */
663 static inline void
664 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
666 /* There are two speculative edges for every statement (one direct,
667 one indirect); always hash the direct one. */
668 if (e->speculative && e->indirect_unknown_callee)
669 return;
670 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
671 (e->call_stmt,
672 htab_hash_pointer (e->call_stmt), INSERT);
673 if (*slot)
675 gcc_assert (((cgraph_edge *)*slot)->speculative);
676 if (e->callee)
677 *slot = e;
678 return;
680 gcc_assert (!*slot || e->speculative);
681 *slot = e;
684 /* Return the callgraph edge representing the GIMPLE_CALL statement
685 CALL_STMT. */
687 cgraph_edge *
688 cgraph_node::get_edge (gimple call_stmt)
690 cgraph_edge *e, *e2;
691 int n = 0;
693 if (call_site_hash)
694 return call_site_hash->find_with_hash (call_stmt,
695 htab_hash_pointer (call_stmt));
697 /* This loop may turn out to be performance problem. In such case adding
698 hashtables into call nodes with very many edges is probably best
699 solution. It is not good idea to add pointer into CALL_EXPR itself
700 because we want to make possible having multiple cgraph nodes representing
701 different clones of the same body before the body is actually cloned. */
702 for (e = callees; e; e = e->next_callee)
704 if (e->call_stmt == call_stmt)
705 break;
706 n++;
709 if (!e)
710 for (e = indirect_calls; e; e = e->next_callee)
712 if (e->call_stmt == call_stmt)
713 break;
714 n++;
717 if (n > 100)
719 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
720 for (e2 = callees; e2; e2 = e2->next_callee)
721 cgraph_add_edge_to_call_site_hash (e2);
722 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
723 cgraph_add_edge_to_call_site_hash (e2);
726 return e;
730 /* Change field call_stmt of edge to NEW_STMT.
731 If UPDATE_SPECULATIVE and E is any component of speculative
732 edge, then update all components. */
734 void
735 cgraph_edge::set_call_stmt (gimple new_stmt, bool update_speculative)
737 tree decl;
739 /* Speculative edges has three component, update all of them
740 when asked to. */
741 if (update_speculative && speculative)
743 cgraph_edge *direct, *indirect;
744 ipa_ref *ref;
746 speculative_call_info (direct, indirect, ref);
747 direct->set_call_stmt (new_stmt, false);
748 indirect->set_call_stmt (new_stmt, false);
749 ref->stmt = new_stmt;
750 return;
753 /* Only direct speculative edges go to call_site_hash. */
754 if (caller->call_site_hash
755 && (!speculative || !indirect_unknown_callee))
757 caller->call_site_hash->remove_elt_with_hash
758 (call_stmt, htab_hash_pointer (call_stmt));
761 cgraph_edge *e = this;
763 call_stmt = new_stmt;
764 if (indirect_unknown_callee
765 && (decl = gimple_call_fndecl (new_stmt)))
767 /* Constant propagation (and possibly also inlining?) can turn an
768 indirect call into a direct one. */
769 cgraph_node *new_callee = cgraph_node::get (decl);
771 gcc_checking_assert (new_callee);
772 e = make_direct (new_callee);
775 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
776 e->can_throw_external = stmt_can_throw_external (new_stmt);
777 pop_cfun ();
778 if (e->caller->call_site_hash)
779 cgraph_add_edge_to_call_site_hash (e);
782 /* Allocate a cgraph_edge structure and fill it with data according to the
783 parameters of which only CALLEE can be NULL (when creating an indirect call
784 edge). */
786 cgraph_edge *
787 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
788 gimple call_stmt, gcov_type count, int freq,
789 bool indir_unknown_callee)
791 cgraph_edge *edge;
793 /* LTO does not actually have access to the call_stmt since these
794 have not been loaded yet. */
795 if (call_stmt)
797 /* This is a rather expensive check possibly triggering
798 construction of call stmt hashtable. */
799 #ifdef ENABLE_CHECKING
800 cgraph_edge *e;
801 gcc_checking_assert (
802 !(e = caller->get_edge (call_stmt)) || e->speculative);
803 #endif
805 gcc_assert (is_gimple_call (call_stmt));
808 if (free_edges)
810 edge = free_edges;
811 free_edges = NEXT_FREE_EDGE (edge);
813 else
815 edge = ggc_alloc<cgraph_edge> ();
816 edge->uid = edges_max_uid++;
819 edges_count++;
821 edge->aux = NULL;
822 edge->caller = caller;
823 edge->callee = callee;
824 edge->prev_caller = NULL;
825 edge->next_caller = NULL;
826 edge->prev_callee = NULL;
827 edge->next_callee = NULL;
828 edge->lto_stmt_uid = 0;
830 edge->count = count;
831 gcc_assert (count >= 0);
832 edge->frequency = freq;
833 gcc_assert (freq >= 0);
834 gcc_assert (freq <= CGRAPH_FREQ_MAX);
836 edge->call_stmt = call_stmt;
837 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
838 edge->can_throw_external
839 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
840 pop_cfun ();
841 if (call_stmt
842 && callee && callee->decl
843 && !gimple_check_call_matching_types (call_stmt, callee->decl,
844 false))
845 edge->call_stmt_cannot_inline_p = true;
846 else
847 edge->call_stmt_cannot_inline_p = false;
849 edge->indirect_info = NULL;
850 edge->indirect_inlining_edge = 0;
851 edge->speculative = false;
852 edge->indirect_unknown_callee = indir_unknown_callee;
853 if (flag_devirtualize && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
854 edge->in_polymorphic_cdtor
855 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
856 caller->decl);
857 else
858 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
859 if (call_stmt && caller->call_site_hash)
860 cgraph_add_edge_to_call_site_hash (edge);
862 return edge;
865 /* Create edge from a given function to CALLEE in the cgraph. */
867 cgraph_edge *
868 cgraph_node::create_edge (cgraph_node *callee,
869 gimple call_stmt, gcov_type count, int freq)
871 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
872 freq, false);
874 initialize_inline_failed (edge);
876 edge->next_caller = callee->callers;
877 if (callee->callers)
878 callee->callers->prev_caller = edge;
879 edge->next_callee = callees;
880 if (callees)
881 callees->prev_callee = edge;
882 callees = edge;
883 callee->callers = edge;
885 return edge;
888 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
890 cgraph_indirect_call_info *
891 cgraph_allocate_init_indirect_info (void)
893 cgraph_indirect_call_info *ii;
895 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
896 ii->param_index = -1;
897 return ii;
900 /* Create an indirect edge with a yet-undetermined callee where the call
901 statement destination is a formal parameter of the caller with index
902 PARAM_INDEX. */
904 cgraph_edge *
905 cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
906 gcov_type count, int freq,
907 bool compute_indirect_info)
909 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt,
910 count, freq, true);
911 tree target;
913 initialize_inline_failed (edge);
915 edge->indirect_info = cgraph_allocate_init_indirect_info ();
916 edge->indirect_info->ecf_flags = ecf_flags;
917 edge->indirect_info->vptr_changed = true;
919 /* Record polymorphic call info. */
920 if (compute_indirect_info
921 && call_stmt
922 && (target = gimple_call_fn (call_stmt))
923 && virtual_method_call_p (target))
925 ipa_polymorphic_call_context context (decl, target, call_stmt);
927 /* Only record types can have virtual calls. */
928 edge->indirect_info->polymorphic = true;
929 edge->indirect_info->param_index = -1;
930 edge->indirect_info->otr_token
931 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
932 edge->indirect_info->otr_type = obj_type_ref_class (target);
933 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
934 edge->indirect_info->context = context;
937 edge->next_callee = indirect_calls;
938 if (indirect_calls)
939 indirect_calls->prev_callee = edge;
940 indirect_calls = edge;
942 return edge;
945 /* Remove the edge from the list of the callers of the callee. */
947 void
948 cgraph_edge::remove_callee (void)
950 gcc_assert (!indirect_unknown_callee);
951 if (prev_caller)
952 prev_caller->next_caller = next_caller;
953 if (next_caller)
954 next_caller->prev_caller = prev_caller;
955 if (!prev_caller)
956 callee->callers = next_caller;
959 /* Remove the edge from the list of the callees of the caller. */
961 void
962 cgraph_edge::remove_caller (void)
964 if (prev_callee)
965 prev_callee->next_callee = next_callee;
966 if (next_callee)
967 next_callee->prev_callee = prev_callee;
968 if (!prev_callee)
970 if (indirect_unknown_callee)
971 caller->indirect_calls = next_callee;
972 else
973 caller->callees = next_callee;
975 if (caller->call_site_hash)
976 caller->call_site_hash->remove_elt_with_hash (call_stmt,
977 htab_hash_pointer (call_stmt));
980 /* Put the edge onto the free list. */
982 void
983 symbol_table::free_edge (cgraph_edge *e)
985 int uid = e->uid;
987 if (e->indirect_info)
988 ggc_free (e->indirect_info);
990 /* Clear out the edge so we do not dangle pointers. */
991 memset (e, 0, sizeof (*e));
992 e->uid = uid;
993 NEXT_FREE_EDGE (e) = free_edges;
994 free_edges = e;
995 edges_count--;
998 /* Remove the edge in the cgraph. */
1000 void
1001 cgraph_edge::remove (void)
1003 /* Call all edge removal hooks. */
1004 symtab->call_edge_removal_hooks (this);
1006 if (!indirect_unknown_callee)
1007 /* Remove from callers list of the callee. */
1008 remove_callee ();
1010 /* Remove from callees list of the callers. */
1011 remove_caller ();
1013 /* Put the edge onto the free list. */
1014 symtab->free_edge (this);
1017 /* Set callee of call graph edge E and add it to the corresponding set of
1018 callers. */
1020 static void
1021 cgraph_set_edge_callee (cgraph_edge *e, cgraph_node *n)
1023 e->prev_caller = NULL;
1024 if (n->callers)
1025 n->callers->prev_caller = e;
1026 e->next_caller = n->callers;
1027 n->callers = e;
1028 e->callee = n;
1031 /* Turn edge into speculative call calling N2. Update
1032 the profile so the direct call is taken COUNT times
1033 with FREQUENCY.
1035 At clone materialization time, the indirect call E will
1036 be expanded as:
1038 if (call_dest == N2)
1039 n2 ();
1040 else
1041 call call_dest
1043 At this time the function just creates the direct call,
1044 the referencd representing the if conditional and attaches
1045 them all to the orginal indirect call statement.
1047 Return direct edge created. */
1049 cgraph_edge *
1050 cgraph_edge::make_speculative (cgraph_node *n2, gcov_type direct_count,
1051 int direct_frequency)
1053 cgraph_node *n = caller;
1054 ipa_ref *ref = NULL;
1055 cgraph_edge *e2;
1057 if (dump_file)
1059 fprintf (dump_file, "Indirect call -> speculative call"
1060 " %s/%i => %s/%i\n",
1061 xstrdup (n->name ()), n->order,
1062 xstrdup (n2->name ()), n2->order);
1064 speculative = true;
1065 e2 = n->create_edge (n2, call_stmt, direct_count, direct_frequency);
1066 initialize_inline_failed (e2);
1067 e2->speculative = true;
1068 if (TREE_NOTHROW (n2->decl))
1069 e2->can_throw_external = false;
1070 else
1071 e2->can_throw_external = can_throw_external;
1072 e2->lto_stmt_uid = lto_stmt_uid;
1073 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1074 count -= e2->count;
1075 frequency -= e2->frequency;
1076 symtab->call_edge_duplication_hooks (this, e2);
1077 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1078 ref->lto_stmt_uid = lto_stmt_uid;
1079 ref->speculative = speculative;
1080 n2->mark_address_taken ();
1081 return e2;
1084 /* Speculative call consist of three components:
1085 1) an indirect edge representing the original call
1086 2) an direct edge representing the new call
1087 3) ADDR_EXPR reference representing the speculative check.
1088 All three components are attached to single statement (the indirect
1089 call) and if one of them exists, all of them must exist.
1091 Given speculative call edge, return all three components.
1094 void
1095 cgraph_edge::speculative_call_info (cgraph_edge *&direct,
1096 cgraph_edge *&indirect,
1097 ipa_ref *&reference)
1099 ipa_ref *ref;
1100 int i;
1101 cgraph_edge *e2;
1102 cgraph_edge *e = this;
1104 if (!e->indirect_unknown_callee)
1105 for (e2 = e->caller->indirect_calls;
1106 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1107 e2 = e2->next_callee)
1109 else
1111 e2 = e;
1112 /* We can take advantage of the call stmt hash. */
1113 if (e2->call_stmt)
1115 e = e->caller->get_edge (e2->call_stmt);
1116 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1118 else
1119 for (e = e->caller->callees;
1120 e2->call_stmt != e->call_stmt
1121 || e2->lto_stmt_uid != e->lto_stmt_uid;
1122 e = e->next_callee)
1125 gcc_assert (e->speculative && e2->speculative);
1126 direct = e;
1127 indirect = e2;
1129 reference = NULL;
1130 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1131 if (ref->speculative
1132 && ((ref->stmt && ref->stmt == e->call_stmt)
1133 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1135 reference = ref;
1136 break;
1139 /* Speculative edge always consist of all three components - direct edge,
1140 indirect and reference. */
1142 gcc_assert (e && e2 && ref);
1145 /* Redirect callee of the edge to N. The function does not update underlying
1146 call expression. */
1148 void
1149 cgraph_edge::redirect_callee (cgraph_node *n)
1151 /* Remove from callers list of the current callee. */
1152 remove_callee ();
1154 /* Insert to callers list of the new callee. */
1155 cgraph_set_edge_callee (this, n);
1158 /* Speculative call edge turned out to be direct call to CALLE_DECL.
1159 Remove the speculative call sequence and return edge representing the call.
1160 It is up to caller to redirect the call as appropriate. */
1162 cgraph_edge *
1163 cgraph_edge::resolve_speculation (tree callee_decl)
1165 cgraph_edge *edge = this;
1166 cgraph_edge *e2;
1167 ipa_ref *ref;
1169 gcc_assert (edge->speculative);
1170 edge->speculative_call_info (e2, edge, ref);
1171 if (!callee_decl
1172 || !ref->referred->semantically_equivalent_p
1173 (symtab_node::get (callee_decl)))
1175 if (dump_file)
1177 if (callee_decl)
1179 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1180 "turned out to have contradicting known target ",
1181 xstrdup (edge->caller->name ()), edge->caller->order,
1182 xstrdup (e2->callee->name ()), e2->callee->order);
1183 print_generic_expr (dump_file, callee_decl, 0);
1184 fprintf (dump_file, "\n");
1186 else
1188 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1189 xstrdup (edge->caller->name ()), edge->caller->order,
1190 xstrdup (e2->callee->name ()), e2->callee->order);
1194 else
1196 cgraph_edge *tmp = edge;
1197 if (dump_file)
1198 fprintf (dump_file, "Speculative call turned into direct call.\n");
1199 edge = e2;
1200 e2 = tmp;
1201 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1202 in the functions inlined through it. */
1204 edge->count += e2->count;
1205 edge->frequency += e2->frequency;
1206 if (edge->frequency > CGRAPH_FREQ_MAX)
1207 edge->frequency = CGRAPH_FREQ_MAX;
1208 edge->speculative = false;
1209 e2->speculative = false;
1210 ref->remove_reference ();
1211 if (e2->indirect_unknown_callee || e2->inline_failed)
1212 e2->remove ();
1213 else
1214 e2->callee->remove_symbol_and_inline_clones ();
1215 if (edge->caller->call_site_hash)
1216 cgraph_update_edge_in_call_site_hash (edge);
1217 return edge;
1220 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1221 CALLEE. DELTA is an integer constant that is to be added to the this
1222 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1224 cgraph_edge *
1225 cgraph_edge::make_direct (cgraph_node *callee)
1227 cgraph_edge *edge = this;
1228 gcc_assert (indirect_unknown_callee);
1230 /* If we are redirecting speculative call, make it non-speculative. */
1231 if (indirect_unknown_callee && speculative)
1233 edge = edge->resolve_speculation (callee->decl);
1235 /* On successful speculation just return the pre existing direct edge. */
1236 if (!indirect_unknown_callee)
1237 return edge;
1240 indirect_unknown_callee = 0;
1241 ggc_free (indirect_info);
1242 indirect_info = NULL;
1244 /* Get the edge out of the indirect edge list. */
1245 if (prev_callee)
1246 prev_callee->next_callee = next_callee;
1247 if (next_callee)
1248 next_callee->prev_callee = prev_callee;
1249 if (!prev_callee)
1250 caller->indirect_calls = next_callee;
1252 /* Put it into the normal callee list */
1253 prev_callee = NULL;
1254 next_callee = caller->callees;
1255 if (caller->callees)
1256 caller->callees->prev_callee = edge;
1257 caller->callees = edge;
1259 /* Insert to callers list of the new callee. */
1260 cgraph_set_edge_callee (edge, callee);
1262 if (call_stmt)
1263 call_stmt_cannot_inline_p
1264 = !gimple_check_call_matching_types (call_stmt, callee->decl,
1265 false);
1267 /* We need to re-determine the inlining status of the edge. */
1268 initialize_inline_failed (edge);
1269 return edge;
1272 /* If necessary, change the function declaration in the call statement
1273 associated with E so that it corresponds to the edge callee. */
1275 gimple
1276 cgraph_edge::redirect_call_stmt_to_callee (void)
1278 cgraph_edge *e = this;
1280 tree decl = gimple_call_fndecl (e->call_stmt);
1281 tree lhs = gimple_call_lhs (e->call_stmt);
1282 gimple new_stmt;
1283 gimple_stmt_iterator gsi;
1284 #ifdef ENABLE_CHECKING
1285 cgraph_node *node;
1286 #endif
1288 if (e->speculative)
1290 cgraph_edge *e2;
1291 gimple new_stmt;
1292 ipa_ref *ref;
1294 e->speculative_call_info (e, e2, ref);
1295 /* If there already is an direct call (i.e. as a result of inliner's
1296 substitution), forget about speculating. */
1297 if (decl)
1298 e = e->resolve_speculation (decl);
1299 /* If types do not match, speculation was likely wrong.
1300 The direct edge was posisbly redirected to the clone with a different
1301 signature. We did not update the call statement yet, so compare it
1302 with the reference that still points to the proper type. */
1303 else if (!gimple_check_call_matching_types (e->call_stmt,
1304 ref->referred->decl,
1305 true))
1307 if (dump_file)
1308 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1309 "Type mismatch.\n",
1310 xstrdup (e->caller->name ()),
1311 e->caller->order,
1312 xstrdup (e->callee->name ()),
1313 e->callee->order);
1314 e = e->resolve_speculation ();
1315 /* We are producing the final function body and will throw away the
1316 callgraph edges really soon. Reset the counts/frequencies to
1317 keep verifier happy in the case of roundoff errors. */
1318 e->count = gimple_bb (e->call_stmt)->count;
1319 e->frequency = compute_call_stmt_bb_frequency
1320 (e->caller->decl, gimple_bb (e->call_stmt));
1322 /* Expand speculation into GIMPLE code. */
1323 else
1325 if (dump_file)
1326 fprintf (dump_file,
1327 "Expanding speculative call of %s/%i -> %s/%i count:"
1328 "%"PRId64"\n",
1329 xstrdup (e->caller->name ()),
1330 e->caller->order,
1331 xstrdup (e->callee->name ()),
1332 e->callee->order,
1333 (int64_t)e->count);
1334 gcc_assert (e2->speculative);
1335 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1336 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
1337 e->count || e2->count
1338 ? RDIV (e->count * REG_BR_PROB_BASE,
1339 e->count + e2->count)
1340 : e->frequency || e2->frequency
1341 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1342 e->frequency + e2->frequency)
1343 : REG_BR_PROB_BASE / 2,
1344 e->count, e->count + e2->count);
1345 e->speculative = false;
1346 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1347 false);
1349 /* Fix edges for BUILT_IN_CHKP_BNDRET calls attached to the
1350 processed call stmt. */
1351 if (gimple_call_with_bounds_p (new_stmt)
1352 && gimple_call_lhs (new_stmt)
1353 && chkp_retbnd_call_by_val (gimple_call_lhs (e2->call_stmt)))
1355 tree dresult = gimple_call_lhs (new_stmt);
1356 tree iresult = gimple_call_lhs (e2->call_stmt);
1357 gimple dbndret = chkp_retbnd_call_by_val (dresult);
1358 gimple ibndret = chkp_retbnd_call_by_val (iresult);
1359 struct cgraph_edge *iedge
1360 = e2->caller->cgraph_node::get_edge (ibndret);
1361 struct cgraph_edge *dedge;
1363 if (dbndret)
1365 dedge = iedge->caller->create_edge (iedge->callee,
1366 dbndret, e->count,
1367 e->frequency);
1368 dedge->frequency = compute_call_stmt_bb_frequency
1369 (dedge->caller->decl, gimple_bb (dedge->call_stmt));
1371 iedge->frequency = compute_call_stmt_bb_frequency
1372 (iedge->caller->decl, gimple_bb (iedge->call_stmt));
1375 e->frequency = compute_call_stmt_bb_frequency
1376 (e->caller->decl, gimple_bb (e->call_stmt));
1377 e2->frequency = compute_call_stmt_bb_frequency
1378 (e2->caller->decl, gimple_bb (e2->call_stmt));
1379 e2->speculative = false;
1380 ref->speculative = false;
1381 ref->stmt = NULL;
1382 /* Indirect edges are not both in the call site hash.
1383 get it updated. */
1384 if (e->caller->call_site_hash)
1385 cgraph_update_edge_in_call_site_hash (e2);
1386 pop_cfun ();
1387 /* Continue redirecting E to proper target. */
1391 if (e->indirect_unknown_callee
1392 || decl == e->callee->decl)
1393 return e->call_stmt;
1395 #ifdef ENABLE_CHECKING
1396 if (decl)
1398 node = cgraph_node::get (decl);
1399 gcc_assert (!node || !node->clone.combined_args_to_skip);
1401 #endif
1403 if (symtab->dump_file)
1405 fprintf (symtab->dump_file, "updating call of %s/%i -> %s/%i: ",
1406 xstrdup (e->caller->name ()), e->caller->order,
1407 xstrdup (e->callee->name ()), e->callee->order);
1408 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1409 if (e->callee->clone.combined_args_to_skip)
1411 fprintf (symtab->dump_file, " combined args to skip: ");
1412 dump_bitmap (symtab->dump_file,
1413 e->callee->clone.combined_args_to_skip);
1417 if (e->callee->clone.combined_args_to_skip)
1419 int lp_nr;
1421 new_stmt
1422 = gimple_call_copy_skip_args (e->call_stmt,
1423 e->callee->clone.combined_args_to_skip);
1424 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1425 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1427 if (gimple_vdef (new_stmt)
1428 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1429 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1431 gsi = gsi_for_stmt (e->call_stmt);
1432 gsi_replace (&gsi, new_stmt, false);
1433 /* We need to defer cleaning EH info on the new statement to
1434 fixup-cfg. We may not have dominator information at this point
1435 and thus would end up with unreachable blocks and have no way
1436 to communicate that we need to run CFG cleanup then. */
1437 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1438 if (lp_nr != 0)
1440 remove_stmt_from_eh_lp (e->call_stmt);
1441 add_stmt_to_eh_lp (new_stmt, lp_nr);
1444 else
1446 new_stmt = e->call_stmt;
1447 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1448 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1451 /* If the call becomes noreturn, remove the lhs. */
1452 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1454 if (TREE_CODE (lhs) == SSA_NAME)
1456 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1457 TREE_TYPE (lhs), NULL);
1458 var = get_or_create_ssa_default_def
1459 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1460 gimple set_stmt = gimple_build_assign (lhs, var);
1461 gsi = gsi_for_stmt (new_stmt);
1462 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1463 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1465 gimple_call_set_lhs (new_stmt, NULL_TREE);
1466 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1469 /* If new callee has no static chain, remove it. */
1470 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1472 gimple_call_set_chain (new_stmt, NULL);
1473 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1476 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1478 if (symtab->dump_file)
1480 fprintf (symtab->dump_file, " updated to:");
1481 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1483 return new_stmt;
1486 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1487 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1488 of OLD_STMT if it was previously call statement.
1489 If NEW_STMT is NULL, the call has been dropped without any
1490 replacement. */
1492 static void
1493 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1494 gimple old_stmt, tree old_call,
1495 gimple new_stmt)
1497 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1498 ? gimple_call_fndecl (new_stmt) : 0;
1500 /* We are seeing indirect calls, then there is nothing to update. */
1501 if (!new_call && !old_call)
1502 return;
1503 /* See if we turned indirect call into direct call or folded call to one builtin
1504 into different builtin. */
1505 if (old_call != new_call)
1507 cgraph_edge *e = node->get_edge (old_stmt);
1508 cgraph_edge *ne = NULL;
1509 gcov_type count;
1510 int frequency;
1512 if (e)
1514 /* See if the edge is already there and has the correct callee. It
1515 might be so because of indirect inlining has already updated
1516 it. We also might've cloned and redirected the edge. */
1517 if (new_call && e->callee)
1519 cgraph_node *callee = e->callee;
1520 while (callee)
1522 if (callee->decl == new_call
1523 || callee->former_clone_of == new_call)
1525 e->set_call_stmt (new_stmt);
1526 return;
1528 callee = callee->clone_of;
1532 /* Otherwise remove edge and create new one; we can't simply redirect
1533 since function has changed, so inline plan and other information
1534 attached to edge is invalid. */
1535 count = e->count;
1536 frequency = e->frequency;
1537 if (e->indirect_unknown_callee || e->inline_failed)
1538 e->remove ();
1539 else
1540 e->callee->remove_symbol_and_inline_clones ();
1542 else if (new_call)
1544 /* We are seeing new direct call; compute profile info based on BB. */
1545 basic_block bb = gimple_bb (new_stmt);
1546 count = bb->count;
1547 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1548 bb);
1551 if (new_call)
1553 ne = node->create_edge (cgraph_node::get_create (new_call),
1554 new_stmt, count, frequency);
1555 gcc_assert (ne->inline_failed);
1558 /* We only updated the call stmt; update pointer in cgraph edge.. */
1559 else if (old_stmt != new_stmt)
1560 node->get_edge (old_stmt)->set_call_stmt (new_stmt);
1563 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1564 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1565 of OLD_STMT before it was updated (updating can happen inplace). */
1567 void
1568 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1570 cgraph_node *orig = cgraph_node::get (cfun->decl);
1571 cgraph_node *node;
1573 gcc_checking_assert (orig);
1574 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1575 if (orig->clones)
1576 for (node = orig->clones; node != orig;)
1578 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1579 if (node->clones)
1580 node = node->clones;
1581 else if (node->next_sibling_clone)
1582 node = node->next_sibling_clone;
1583 else
1585 while (node != orig && !node->next_sibling_clone)
1586 node = node->clone_of;
1587 if (node != orig)
1588 node = node->next_sibling_clone;
1594 /* Remove all callees from the node. */
1596 void
1597 cgraph_node::remove_callees (void)
1599 cgraph_edge *e, *f;
1601 /* It is sufficient to remove the edges from the lists of callers of
1602 the callees. The callee list of the node can be zapped with one
1603 assignment. */
1604 for (e = callees; e; e = f)
1606 f = e->next_callee;
1607 symtab->call_edge_removal_hooks (e);
1608 if (!e->indirect_unknown_callee)
1609 e->remove_callee ();
1610 symtab->free_edge (e);
1612 for (e = indirect_calls; e; e = f)
1614 f = e->next_callee;
1615 symtab->call_edge_removal_hooks (e);
1616 if (!e->indirect_unknown_callee)
1617 e->remove_callee ();
1618 symtab->free_edge (e);
1620 indirect_calls = NULL;
1621 callees = NULL;
1622 if (call_site_hash)
1624 call_site_hash->empty ();
1625 call_site_hash = NULL;
1629 /* Remove all callers from the node. */
1631 void
1632 cgraph_node::remove_callers (void)
1634 cgraph_edge *e, *f;
1636 /* It is sufficient to remove the edges from the lists of callees of
1637 the callers. The caller list of the node can be zapped with one
1638 assignment. */
1639 for (e = callers; e; e = f)
1641 f = e->next_caller;
1642 symtab->call_edge_removal_hooks (e);
1643 e->remove_caller ();
1644 symtab->free_edge (e);
1646 callers = NULL;
1649 /* Helper function for cgraph_release_function_body and free_lang_data.
1650 It releases body from function DECL without having to inspect its
1651 possibly non-existent symtab node. */
1653 void
1654 release_function_body (tree decl)
1656 if (DECL_STRUCT_FUNCTION (decl))
1658 push_cfun (DECL_STRUCT_FUNCTION (decl));
1659 if (cfun->cfg
1660 && current_loops)
1662 cfun->curr_properties &= ~PROP_loops;
1663 loop_optimizer_finalize ();
1665 if (cfun->gimple_df)
1667 delete_tree_ssa ();
1668 delete_tree_cfg_annotations ();
1669 cfun->eh = NULL;
1671 if (cfun->cfg)
1673 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1674 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1675 clear_edges ();
1676 cfun->cfg = NULL;
1678 if (cfun->value_histograms)
1679 free_histograms ();
1680 pop_cfun ();
1681 gimple_set_body (decl, NULL);
1682 /* Struct function hangs a lot of data that would leak if we didn't
1683 removed all pointers to it. */
1684 ggc_free (DECL_STRUCT_FUNCTION (decl));
1685 DECL_STRUCT_FUNCTION (decl) = NULL;
1687 DECL_SAVED_TREE (decl) = NULL;
1690 /* Release memory used to represent body of function.
1691 Use this only for functions that are released before being translated to
1692 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1693 are free'd in final.c via free_after_compilation().
1694 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1696 void
1697 cgraph_node::release_body (bool keep_arguments)
1699 ipa_transforms_to_apply.release ();
1700 if (!used_as_abstract_origin && symtab->state != PARSING)
1702 DECL_RESULT (decl) = NULL;
1704 if (!keep_arguments)
1705 DECL_ARGUMENTS (decl) = NULL;
1707 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1708 of its associated function function declaration because it's
1709 needed to emit debug info later. */
1710 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1711 DECL_INITIAL (decl) = error_mark_node;
1712 release_function_body (decl);
1713 if (lto_file_data)
1714 lto_free_function_in_decl_state_for_node (this);
1717 /* Remove function from symbol table. */
1719 void
1720 cgraph_node::remove (void)
1722 cgraph_node *n;
1723 int uid = this->uid;
1725 symtab->call_cgraph_removal_hooks (this);
1726 remove_callers ();
1727 remove_callees ();
1728 ipa_transforms_to_apply.release ();
1730 /* Incremental inlining access removed nodes stored in the postorder list.
1732 force_output = false;
1733 forced_by_abi = false;
1734 for (n = nested; n; n = n->next_nested)
1735 n->origin = NULL;
1736 nested = NULL;
1737 if (origin)
1739 cgraph_node **node2 = &origin->nested;
1741 while (*node2 != this)
1742 node2 = &(*node2)->next_nested;
1743 *node2 = next_nested;
1745 unregister ();
1746 if (prev_sibling_clone)
1747 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1748 else if (clone_of)
1749 clone_of->clones = next_sibling_clone;
1750 if (next_sibling_clone)
1751 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1752 if (clones)
1754 cgraph_node *n, *next;
1756 if (clone_of)
1758 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1759 n->clone_of = clone_of;
1760 n->clone_of = clone_of;
1761 n->next_sibling_clone = clone_of->clones;
1762 if (clone_of->clones)
1763 clone_of->clones->prev_sibling_clone = n;
1764 clone_of->clones = clones;
1766 else
1768 /* We are removing node with clones. This makes clones inconsistent,
1769 but assume they will be removed subsequently and just keep clone
1770 tree intact. This can happen in unreachable function removal since
1771 we remove unreachable functions in random order, not by bottom-up
1772 walk of clone trees. */
1773 for (n = clones; n; n = next)
1775 next = n->next_sibling_clone;
1776 n->next_sibling_clone = NULL;
1777 n->prev_sibling_clone = NULL;
1778 n->clone_of = NULL;
1783 /* While all the clones are removed after being proceeded, the function
1784 itself is kept in the cgraph even after it is compiled. Check whether
1785 we are done with this body and reclaim it proactively if this is the case.
1787 if (symtab->state != LTO_STREAMING)
1789 n = cgraph_node::get (decl);
1790 if (!n
1791 || (!n->clones && !n->clone_of && !n->global.inlined_to
1792 && (symtab->global_info_ready
1793 && (TREE_ASM_WRITTEN (n->decl)
1794 || DECL_EXTERNAL (n->decl)
1795 || !n->analyzed
1796 || (!flag_wpa && n->in_other_partition)))))
1797 release_body ();
1800 decl = NULL;
1801 if (call_site_hash)
1803 call_site_hash->empty ();
1804 call_site_hash = NULL;
1807 if (instrumented_version)
1809 instrumented_version->instrumented_version = NULL;
1810 instrumented_version = NULL;
1813 symtab->release_symbol (this, uid);
1816 /* Likewise indicate that a node is having address taken. */
1818 void
1819 cgraph_node::mark_address_taken (void)
1821 /* Indirect inlining can figure out that all uses of the address are
1822 inlined. */
1823 if (global.inlined_to)
1825 gcc_assert (cfun->after_inlining);
1826 gcc_assert (callers->indirect_inlining_edge);
1827 return;
1829 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1830 IPA_REF_ADDR reference exists (and thus it should be set on node
1831 representing alias we take address of) and as a test whether address
1832 of the object was taken (and thus it should be set on node alias is
1833 referring to). We should remove the first use and the remove the
1834 following set. */
1835 address_taken = 1;
1836 cgraph_node *node = ultimate_alias_target ();
1837 node->address_taken = 1;
1840 /* Return local info for the compiled function. */
1842 cgraph_local_info *
1843 cgraph_node::local_info (tree decl)
1845 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1846 cgraph_node *node = get (decl);
1847 if (!node)
1848 return NULL;
1849 return &node->local;
1852 /* Return global info for the compiled function. */
1854 cgraph_global_info *
1855 cgraph_node::global_info (tree decl)
1857 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1858 && symtab->global_info_ready);
1859 cgraph_node *node = get (decl);
1860 if (!node)
1861 return NULL;
1862 return &node->global;
1865 /* Return local info for the compiled function. */
1867 cgraph_rtl_info *
1868 cgraph_node::rtl_info (tree decl)
1870 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1871 cgraph_node *node = get (decl);
1872 if (!node
1873 || (decl != current_function_decl
1874 && !TREE_ASM_WRITTEN (node->decl)))
1875 return NULL;
1876 return &node->rtl;
1879 /* Return a string describing the failure REASON. */
1881 const char*
1882 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1884 #undef DEFCIFCODE
1885 #define DEFCIFCODE(code, type, string) string,
1887 static const char *cif_string_table[CIF_N_REASONS] = {
1888 #include "cif-code.def"
1891 /* Signedness of an enum type is implementation defined, so cast it
1892 to unsigned before testing. */
1893 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1894 return cif_string_table[reason];
1897 /* Return a type describing the failure REASON. */
1899 cgraph_inline_failed_type_t
1900 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1902 #undef DEFCIFCODE
1903 #define DEFCIFCODE(code, type, string) type,
1905 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1906 #include "cif-code.def"
1909 /* Signedness of an enum type is implementation defined, so cast it
1910 to unsigned before testing. */
1911 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1912 return cif_type_table[reason];
1915 /* Names used to print out the availability enum. */
1916 const char * const cgraph_availability_names[] =
1917 {"unset", "not_available", "overwritable", "available", "local"};
1919 /* Output flags of edge E. */
1921 static void
1922 dump_edge_flags (FILE *f, struct cgraph_edge *edge)
1924 if (edge->speculative)
1925 fprintf (f, "(speculative) ");
1926 if (!edge->inline_failed)
1927 fprintf (f, "(inlined) ");
1928 if (edge->indirect_inlining_edge)
1929 fprintf (f, "(indirect_inlining) ");
1930 if (edge->count)
1931 fprintf (f, "(%"PRId64"x) ",
1932 (int64_t)edge->count);
1933 if (edge->frequency)
1934 fprintf (f, "(%.2f per call) ",
1935 edge->frequency / (double)CGRAPH_FREQ_BASE);
1936 if (edge->can_throw_external)
1937 fprintf (f, "(can throw external) ");
1940 /* Dump call graph node to file F. */
1942 void
1943 cgraph_node::dump (FILE *f)
1945 cgraph_edge *edge;
1947 dump_base (f);
1949 if (global.inlined_to)
1950 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1951 xstrdup (name ()),
1952 order,
1953 xstrdup (global.inlined_to->name ()),
1954 global.inlined_to->order);
1955 if (clone_of)
1956 fprintf (f, " Clone of %s/%i\n",
1957 clone_of->asm_name (),
1958 clone_of->order);
1959 if (symtab->function_flags_ready)
1960 fprintf (f, " Availability: %s\n",
1961 cgraph_availability_names [get_availability ()]);
1963 if (profile_id)
1964 fprintf (f, " Profile id: %i\n",
1965 profile_id);
1966 fprintf (f, " First run: %i\n", tp_first_run);
1967 fprintf (f, " Function flags:");
1968 if (count)
1969 fprintf (f, " executed %"PRId64"x",
1970 (int64_t)count);
1971 if (origin)
1972 fprintf (f, " nested in: %s", origin->asm_name ());
1973 if (gimple_has_body_p (decl))
1974 fprintf (f, " body");
1975 if (process)
1976 fprintf (f, " process");
1977 if (local.local)
1978 fprintf (f, " local");
1979 if (local.redefined_extern_inline)
1980 fprintf (f, " redefined_extern_inline");
1981 if (only_called_at_startup)
1982 fprintf (f, " only_called_at_startup");
1983 if (only_called_at_exit)
1984 fprintf (f, " only_called_at_exit");
1985 if (tm_clone)
1986 fprintf (f, " tm_clone");
1987 if (icf_merged)
1988 fprintf (f, " icf_merged");
1989 if (DECL_STATIC_CONSTRUCTOR (decl))
1990 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
1991 if (DECL_STATIC_DESTRUCTOR (decl))
1992 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
1994 fprintf (f, "\n");
1996 if (thunk.thunk_p)
1998 fprintf (f, " Thunk");
1999 if (thunk.alias)
2000 fprintf (f, " of %s (asm: %s)",
2001 lang_hooks.decl_printable_name (thunk.alias, 2),
2002 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2003 fprintf (f, " fixed offset %i virtual value %i has "
2004 "virtual offset %i)\n",
2005 (int)thunk.fixed_offset,
2006 (int)thunk.virtual_value,
2007 (int)thunk.virtual_offset_p);
2009 if (alias && thunk.alias
2010 && DECL_P (thunk.alias))
2012 fprintf (f, " Alias of %s",
2013 lang_hooks.decl_printable_name (thunk.alias, 2));
2014 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2015 fprintf (f, " (asm: %s)",
2016 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2017 fprintf (f, "\n");
2020 fprintf (f, " Called by: ");
2022 for (edge = callers; edge; edge = edge->next_caller)
2024 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2025 edge->caller->order);
2026 dump_edge_flags (f, edge);
2029 fprintf (f, "\n Calls: ");
2030 for (edge = callees; edge; edge = edge->next_callee)
2032 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2033 edge->callee->order);
2034 dump_edge_flags (f, edge);
2036 fprintf (f, "\n");
2038 for (edge = indirect_calls; edge; edge = edge->next_callee)
2040 if (edge->indirect_info->polymorphic)
2042 fprintf (f, " Polymorphic indirect call of type ");
2043 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2044 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2046 else
2047 fprintf (f, " Indirect call");
2048 dump_edge_flags (f, edge);
2049 if (edge->indirect_info->param_index != -1)
2051 fprintf (f, " of param:%i", edge->indirect_info->param_index);
2052 if (edge->indirect_info->agg_contents)
2053 fprintf (f, " loaded from %s %s at offset %i",
2054 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2055 edge->indirect_info->by_ref ? "passed by reference":"",
2056 (int)edge->indirect_info->offset);
2057 if (edge->indirect_info->vptr_changed)
2058 fprintf (f, " (vptr maybe changed)");
2060 fprintf (f, "\n");
2061 if (edge->indirect_info->polymorphic)
2062 edge->indirect_info->context.dump (f);
2065 if (instrumentation_clone)
2066 fprintf (f, " Is instrumented version.\n");
2067 else if (instrumented_version)
2068 fprintf (f, " Has instrumented version.\n");
2071 /* Dump call graph node NODE to stderr. */
2073 DEBUG_FUNCTION void
2074 cgraph_node::debug (void)
2076 dump (stderr);
2079 /* Dump the callgraph to file F. */
2081 void
2082 cgraph_node::dump_cgraph (FILE *f)
2084 cgraph_node *node;
2086 fprintf (f, "callgraph:\n\n");
2087 FOR_EACH_FUNCTION (node)
2088 node->dump (f);
2091 /* Return true when the DECL can possibly be inlined. */
2093 bool
2094 cgraph_function_possibly_inlined_p (tree decl)
2096 if (!symtab->global_info_ready)
2097 return !DECL_UNINLINABLE (decl);
2098 return DECL_POSSIBLY_INLINED (decl);
2101 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2102 void
2103 cgraph_node::unnest (void)
2105 cgraph_node **node2 = &origin->nested;
2106 gcc_assert (origin);
2108 while (*node2 != this)
2109 node2 = &(*node2)->next_nested;
2110 *node2 = next_nested;
2111 origin = NULL;
2114 /* Return function availability. See cgraph.h for description of individual
2115 return values. */
2116 enum availability
2117 cgraph_node::get_availability (void)
2119 enum availability avail;
2120 if (!analyzed)
2121 avail = AVAIL_NOT_AVAILABLE;
2122 else if (local.local)
2123 avail = AVAIL_LOCAL;
2124 else if (alias && weakref)
2125 ultimate_alias_target (&avail);
2126 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2127 avail = AVAIL_INTERPOSABLE;
2128 else if (!externally_visible)
2129 avail = AVAIL_AVAILABLE;
2130 /* Inline functions are safe to be analyzed even if their symbol can
2131 be overwritten at runtime. It is not meaningful to enforce any sane
2132 behaviour on replacing inline function by different body. */
2133 else if (DECL_DECLARED_INLINE_P (decl))
2134 avail = AVAIL_AVAILABLE;
2136 /* If the function can be overwritten, return OVERWRITABLE. Take
2137 care at least of two notable extensions - the COMDAT functions
2138 used to share template instantiations in C++ (this is symmetric
2139 to code cp_cannot_inline_tree_fn and probably shall be shared and
2140 the inlinability hooks completely eliminated).
2142 ??? Does the C++ one definition rule allow us to always return
2143 AVAIL_AVAILABLE here? That would be good reason to preserve this
2144 bit. */
2146 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2147 avail = AVAIL_INTERPOSABLE;
2148 else avail = AVAIL_AVAILABLE;
2150 return avail;
2153 /* Worker for cgraph_node_can_be_local_p. */
2154 static bool
2155 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2157 return !(!node->force_output
2158 && ((DECL_COMDAT (node->decl)
2159 && !node->forced_by_abi
2160 && !node->used_from_object_file_p ()
2161 && !node->same_comdat_group)
2162 || !node->externally_visible));
2165 /* Return true if cgraph_node can be made local for API change.
2166 Extern inline functions and C++ COMDAT functions can be made local
2167 at the expense of possible code size growth if function is used in multiple
2168 compilation units. */
2169 bool
2170 cgraph_node::can_be_local_p (void)
2172 return (!address_taken
2173 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2174 NULL, true));
2177 /* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
2178 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2179 skipped. */
2181 bool
2182 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2183 (cgraph_node *, void *),
2184 void *data,
2185 bool include_overwritable)
2187 cgraph_edge *e;
2188 ipa_ref *ref;
2190 if (callback (this, data))
2191 return true;
2192 for (e = callers; e; e = e->next_caller)
2193 if (e->caller->thunk.thunk_p
2194 && (include_overwritable
2195 || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2196 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2197 include_overwritable))
2198 return true;
2200 FOR_EACH_ALIAS (this, ref)
2202 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2203 if (include_overwritable
2204 || alias->get_availability () > AVAIL_INTERPOSABLE)
2205 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2206 include_overwritable))
2207 return true;
2209 return false;
2212 /* Call calback on function and aliases associated to the function.
2213 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2214 skipped. */
2216 bool
2217 cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2218 void *),
2219 void *data,
2220 bool include_overwritable)
2222 ipa_ref *ref;
2224 if (callback (this, data))
2225 return true;
2227 FOR_EACH_ALIAS (this, ref)
2229 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2230 if (include_overwritable
2231 || alias->get_availability () > AVAIL_INTERPOSABLE)
2232 if (alias->call_for_symbol_and_aliases (callback, data,
2233 include_overwritable))
2234 return true;
2236 return false;
2239 /* Worker to bring NODE local. */
2241 bool
2242 cgraph_node::make_local (cgraph_node *node, void *)
2244 gcc_checking_assert (node->can_be_local_p ());
2245 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2247 node->make_decl_local ();
2248 node->set_section (NULL);
2249 node->set_comdat_group (NULL);
2250 node->externally_visible = false;
2251 node->forced_by_abi = false;
2252 node->local.local = true;
2253 node->set_section (NULL);
2254 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2255 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2256 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2257 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2259 return false;
2262 /* Bring cgraph node local. */
2264 void
2265 cgraph_node::make_local (void)
2267 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2270 /* Worker to set nothrow flag. */
2272 static bool
2273 cgraph_set_nothrow_flag_1 (cgraph_node *node, void *data)
2275 cgraph_edge *e;
2277 TREE_NOTHROW (node->decl) = data != NULL;
2279 if (data != NULL)
2280 for (e = node->callers; e; e = e->next_caller)
2281 e->can_throw_external = false;
2282 return false;
2285 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2286 if any to NOTHROW. */
2288 void
2289 cgraph_node::set_nothrow_flag (bool nothrow)
2291 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2292 (void *)(size_t)nothrow, false);
2295 /* Worker to set const flag. */
2297 static bool
2298 cgraph_set_const_flag_1 (cgraph_node *node, void *data)
2300 /* Static constructors and destructors without a side effect can be
2301 optimized out. */
2302 if (data && !((size_t)data & 2))
2304 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2305 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2306 if (DECL_STATIC_DESTRUCTOR (node->decl))
2307 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2309 TREE_READONLY (node->decl) = data != NULL;
2310 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2311 return false;
2314 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2315 if any to READONLY. */
2317 void
2318 cgraph_node::set_const_flag (bool readonly, bool looping)
2320 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2321 (void *)(size_t)(readonly + (int)looping * 2),
2322 false);
2325 /* Worker to set pure flag. */
2327 static bool
2328 cgraph_set_pure_flag_1 (cgraph_node *node, void *data)
2330 /* Static constructors and destructors without a side effect can be
2331 optimized out. */
2332 if (data && !((size_t)data & 2))
2334 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2335 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2336 if (DECL_STATIC_DESTRUCTOR (node->decl))
2337 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2339 DECL_PURE_P (node->decl) = data != NULL;
2340 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2341 return false;
2344 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2345 if any to PURE. */
2347 void
2348 cgraph_node::set_pure_flag (bool pure, bool looping)
2350 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2351 (void *)(size_t)(pure + (int)looping * 2),
2352 false);
2355 /* Return true when cgraph_node can not return or throw and thus
2356 it is safe to ignore its side effects for IPA analysis. */
2358 bool
2359 cgraph_node::cannot_return_p (void)
2361 int flags = flags_from_decl_or_type (decl);
2362 if (!flag_exceptions)
2363 return (flags & ECF_NORETURN) != 0;
2364 else
2365 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2366 == (ECF_NORETURN | ECF_NOTHROW));
2369 /* Return true when call of edge can not lead to return from caller
2370 and thus it is safe to ignore its side effects for IPA analysis
2371 when computing side effects of the caller.
2372 FIXME: We could actually mark all edges that have no reaching
2373 patch to the exit block or throw to get better results. */
2374 bool
2375 cgraph_edge::cannot_lead_to_return_p (void)
2377 if (caller->cannot_return_p ())
2378 return true;
2379 if (indirect_unknown_callee)
2381 int flags = indirect_info->ecf_flags;
2382 if (!flag_exceptions)
2383 return (flags & ECF_NORETURN) != 0;
2384 else
2385 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2386 == (ECF_NORETURN | ECF_NOTHROW));
2388 else
2389 return callee->cannot_return_p ();
2392 /* Return true if the call can be hot. */
2394 bool
2395 cgraph_edge::maybe_hot_p (void)
2397 if (profile_info && flag_branch_probabilities
2398 && !maybe_hot_count_p (NULL, count))
2399 return false;
2400 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2401 || (callee
2402 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2403 return false;
2404 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2405 && (callee
2406 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2407 return false;
2408 if (optimize_size) return false;
2409 if (caller->frequency == NODE_FREQUENCY_HOT)
2410 return true;
2411 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE
2412 && frequency < CGRAPH_FREQ_BASE * 3 / 2)
2413 return false;
2414 if (flag_guess_branch_prob)
2416 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0
2417 || frequency <= (CGRAPH_FREQ_BASE
2418 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
2419 return false;
2421 return true;
2424 /* Return true when function can be removed from callgraph
2425 if all direct calls are eliminated. */
2427 bool
2428 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2430 gcc_assert (!global.inlined_to);
2431 /* Instrumentation clones should not be removed before
2432 instrumentation happens. New callers may appear after
2433 instrumentation. */
2434 if (instrumentation_clone
2435 && !chkp_function_instrumented_p (decl))
2436 return false;
2437 /* Extern inlines can always go, we will use the external definition. */
2438 if (DECL_EXTERNAL (decl))
2439 return true;
2440 /* When function is needed, we can not remove it. */
2441 if (force_output || used_from_other_partition)
2442 return false;
2443 if (DECL_STATIC_CONSTRUCTOR (decl)
2444 || DECL_STATIC_DESTRUCTOR (decl))
2445 return false;
2446 /* Only COMDAT functions can be removed if externally visible. */
2447 if (externally_visible
2448 && (!DECL_COMDAT (decl)
2449 || forced_by_abi
2450 || used_from_object_file_p ()))
2451 return false;
2452 return true;
2455 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2457 static bool
2458 nonremovable_p (cgraph_node *node, void *)
2460 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2463 /* Return true when function cgraph_node and its aliases can be removed from
2464 callgraph if all direct calls are eliminated. */
2466 bool
2467 cgraph_node::can_remove_if_no_direct_calls_p (void)
2469 /* Extern inlines can always go, we will use the external definition. */
2470 if (DECL_EXTERNAL (decl))
2471 return true;
2472 if (address_taken)
2473 return false;
2474 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2477 /* Return true when function cgraph_node can be expected to be removed
2478 from program when direct calls in this compilation unit are removed.
2480 As a special case COMDAT functions are
2481 cgraph_can_remove_if_no_direct_calls_p while the are not
2482 cgraph_only_called_directly_p (it is possible they are called from other
2483 unit)
2485 This function behaves as cgraph_only_called_directly_p because eliminating
2486 all uses of COMDAT function does not make it necessarily disappear from
2487 the program unless we are compiling whole program or we do LTO. In this
2488 case we know we win since dynamic linking will not really discard the
2489 linkonce section. */
2491 bool
2492 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2494 gcc_assert (!global.inlined_to);
2496 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2497 NULL, true))
2498 return false;
2499 if (!in_lto_p && !flag_whole_program)
2500 return only_called_directly_p ();
2501 else
2503 if (DECL_EXTERNAL (decl))
2504 return true;
2505 return can_remove_if_no_direct_calls_p ();
2510 /* Worker for cgraph_only_called_directly_p. */
2512 static bool
2513 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2515 return !node->only_called_directly_or_aliased_p ();
2518 /* Return true when function cgraph_node and all its aliases are only called
2519 directly.
2520 i.e. it is not externally visible, address was not taken and
2521 it is not used in any other non-standard way. */
2523 bool
2524 cgraph_node::only_called_directly_p (void)
2526 gcc_assert (ultimate_alias_target () == this);
2527 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2528 NULL, true);
2532 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2534 static bool
2535 collect_callers_of_node_1 (cgraph_node *node, void *data)
2537 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2538 cgraph_edge *cs;
2539 enum availability avail;
2540 node->ultimate_alias_target (&avail);
2542 if (avail > AVAIL_INTERPOSABLE)
2543 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2544 if (!cs->indirect_inlining_edge)
2545 redirect_callers->safe_push (cs);
2546 return false;
2549 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2550 cgraph_node (i.e. are not overwritable). */
2552 vec<cgraph_edge *>
2553 cgraph_node::collect_callers (void)
2555 vec<cgraph_edge *> redirect_callers = vNULL;
2556 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2557 &redirect_callers, false);
2558 return redirect_callers;
2561 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2563 static bool
2564 clone_of_p (cgraph_node *node, cgraph_node *node2)
2566 bool skipped_thunk = false;
2567 node = node->ultimate_alias_target ();
2568 node2 = node2->ultimate_alias_target ();
2570 /* There are no virtual clones of thunks so check former_clone_of or if we
2571 might have skipped thunks because this adjustments are no longer
2572 necessary. */
2573 while (node->thunk.thunk_p)
2575 if (node2->former_clone_of == node->decl)
2576 return true;
2577 if (!node->thunk.this_adjusting)
2578 return false;
2579 node = node->callees->callee->ultimate_alias_target ();
2580 skipped_thunk = true;
2583 if (skipped_thunk)
2585 if (!node2->clone.args_to_skip
2586 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2587 return false;
2588 if (node2->former_clone_of == node->decl)
2589 return true;
2590 else if (!node2->clone_of)
2591 return false;
2594 while (node != node2 && node2)
2595 node2 = node2->clone_of;
2596 return node2 != NULL;
2599 /* Verify edge E count and frequency. */
2601 static bool
2602 verify_edge_count_and_frequency (cgraph_edge *e)
2604 bool error_found = false;
2605 if (e->count < 0)
2607 error ("caller edge count is negative");
2608 error_found = true;
2610 if (e->frequency < 0)
2612 error ("caller edge frequency is negative");
2613 error_found = true;
2615 if (e->frequency > CGRAPH_FREQ_MAX)
2617 error ("caller edge frequency is too large");
2618 error_found = true;
2620 if (gimple_has_body_p (e->caller->decl)
2621 && !e->caller->global.inlined_to
2622 && !e->speculative
2623 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2624 Remove this once edges are actually removed from the function at that time. */
2625 && (e->frequency
2626 || (inline_edge_summary_vec.exists ()
2627 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
2628 || !inline_edge_summary (e)->predicate)))
2629 && (e->frequency
2630 != compute_call_stmt_bb_frequency (e->caller->decl,
2631 gimple_bb (e->call_stmt))))
2633 error ("caller edge frequency %i does not match BB frequency %i",
2634 e->frequency,
2635 compute_call_stmt_bb_frequency (e->caller->decl,
2636 gimple_bb (e->call_stmt)));
2637 error_found = true;
2639 return error_found;
2642 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2643 static void
2644 cgraph_debug_gimple_stmt (function *this_cfun, gimple stmt)
2646 bool fndecl_was_null = false;
2647 /* debug_gimple_stmt needs correct cfun */
2648 if (cfun != this_cfun)
2649 set_cfun (this_cfun);
2650 /* ...and an actual current_function_decl */
2651 if (!current_function_decl)
2653 current_function_decl = this_cfun->decl;
2654 fndecl_was_null = true;
2656 debug_gimple_stmt (stmt);
2657 if (fndecl_was_null)
2658 current_function_decl = NULL;
2661 /* Verify that call graph edge E corresponds to DECL from the associated
2662 statement. Return true if the verification should fail. */
2664 static bool
2665 verify_edge_corresponds_to_fndecl (cgraph_edge *e, tree decl)
2667 cgraph_node *node;
2669 if (!decl || e->callee->global.inlined_to)
2670 return false;
2671 if (symtab->state == LTO_STREAMING)
2672 return false;
2673 node = cgraph_node::get (decl);
2675 /* We do not know if a node from a different partition is an alias or what it
2676 aliases and therefore cannot do the former_clone_of check reliably. When
2677 body_removed is set, we have lost all information about what was alias or
2678 thunk of and also cannot proceed. */
2679 if (!node
2680 || node->body_removed
2681 || node->in_other_partition
2682 || node->icf_merged
2683 || e->callee->in_other_partition)
2684 return false;
2686 node = node->ultimate_alias_target ();
2688 /* Optimizers can redirect unreachable calls or calls triggering undefined
2689 behaviour to builtin_unreachable. */
2690 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2691 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2692 return false;
2694 if (e->callee->former_clone_of != node->decl
2695 && (node != e->callee->ultimate_alias_target ())
2696 && !clone_of_p (node, e->callee))
2697 return true;
2698 else
2699 return false;
2702 /* Verify cgraph nodes of given cgraph node. */
2703 DEBUG_FUNCTION void
2704 cgraph_node::verify_node (void)
2706 cgraph_edge *e;
2707 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2708 basic_block this_block;
2709 gimple_stmt_iterator gsi;
2710 bool error_found = false;
2712 if (seen_error ())
2713 return;
2715 timevar_push (TV_CGRAPH_VERIFY);
2716 error_found |= verify_base ();
2717 for (e = callees; e; e = e->next_callee)
2718 if (e->aux)
2720 error ("aux field set for edge %s->%s",
2721 identifier_to_locale (e->caller->name ()),
2722 identifier_to_locale (e->callee->name ()));
2723 error_found = true;
2725 if (count < 0)
2727 error ("execution count is negative");
2728 error_found = true;
2730 if (global.inlined_to && same_comdat_group)
2732 error ("inline clone in same comdat group list");
2733 error_found = true;
2735 if (!definition && !in_other_partition && local.local)
2737 error ("local symbols must be defined");
2738 error_found = true;
2740 if (global.inlined_to && externally_visible)
2742 error ("externally visible inline clone");
2743 error_found = true;
2745 if (global.inlined_to && address_taken)
2747 error ("inline clone with address taken");
2748 error_found = true;
2750 if (global.inlined_to && force_output)
2752 error ("inline clone is forced to output");
2753 error_found = true;
2755 for (e = indirect_calls; e; e = e->next_callee)
2757 if (e->aux)
2759 error ("aux field set for indirect edge from %s",
2760 identifier_to_locale (e->caller->name ()));
2761 error_found = true;
2763 if (!e->indirect_unknown_callee
2764 || !e->indirect_info)
2766 error ("An indirect edge from %s is not marked as indirect or has "
2767 "associated indirect_info, the corresponding statement is: ",
2768 identifier_to_locale (e->caller->name ()));
2769 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2770 error_found = true;
2773 bool check_comdat = comdat_local_p ();
2774 for (e = callers; e; e = e->next_caller)
2776 if (verify_edge_count_and_frequency (e))
2777 error_found = true;
2778 if (check_comdat
2779 && !in_same_comdat_group_p (e->caller))
2781 error ("comdat-local function called by %s outside its comdat",
2782 identifier_to_locale (e->caller->name ()));
2783 error_found = true;
2785 if (!e->inline_failed)
2787 if (global.inlined_to
2788 != (e->caller->global.inlined_to
2789 ? e->caller->global.inlined_to : e->caller))
2791 error ("inlined_to pointer is wrong");
2792 error_found = true;
2794 if (callers->next_caller)
2796 error ("multiple inline callers");
2797 error_found = true;
2800 else
2801 if (global.inlined_to)
2803 error ("inlined_to pointer set for noninline callers");
2804 error_found = true;
2807 for (e = indirect_calls; e; e = e->next_callee)
2808 if (verify_edge_count_and_frequency (e))
2809 error_found = true;
2810 if (!callers && global.inlined_to)
2812 error ("inlined_to pointer is set but no predecessors found");
2813 error_found = true;
2815 if (global.inlined_to == this)
2817 error ("inlined_to pointer refers to itself");
2818 error_found = true;
2821 if (clone_of)
2823 cgraph_node *n;
2824 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2825 if (n == this)
2826 break;
2827 if (!n)
2829 error ("cgraph_node has wrong clone_of");
2830 error_found = true;
2833 if (clones)
2835 cgraph_node *n;
2836 for (n = clones; n; n = n->next_sibling_clone)
2837 if (n->clone_of != this)
2838 break;
2839 if (n)
2841 error ("cgraph_node has wrong clone list");
2842 error_found = true;
2845 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2847 error ("cgraph_node is in clone list but it is not clone");
2848 error_found = true;
2850 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2852 error ("cgraph_node has wrong prev_clone pointer");
2853 error_found = true;
2855 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2857 error ("double linked list of clones corrupted");
2858 error_found = true;
2861 if (analyzed && alias)
2863 bool ref_found = false;
2864 int i;
2865 ipa_ref *ref = NULL;
2867 if (callees)
2869 error ("Alias has call edges");
2870 error_found = true;
2872 for (i = 0; iterate_reference (i, ref); i++)
2873 if (ref->use == IPA_REF_CHKP)
2875 else if (ref->use != IPA_REF_ALIAS)
2877 error ("Alias has non-alias reference");
2878 error_found = true;
2880 else if (ref_found)
2882 error ("Alias has more than one alias reference");
2883 error_found = true;
2885 else
2886 ref_found = true;
2887 if (!ref_found)
2889 error ("Analyzed alias has no reference");
2890 error_found = true;
2894 /* Check instrumented version reference. */
2895 if (instrumented_version
2896 && instrumented_version->instrumented_version != this)
2898 error ("Instrumentation clone does not reference original node");
2899 error_found = true;
2902 /* Cannot have orig_decl for not instrumented nodes. */
2903 if (!instrumentation_clone && orig_decl)
2905 error ("Not instrumented node has non-NULL original declaration");
2906 error_found = true;
2909 /* If original not instrumented node still exists then we may check
2910 original declaration is set properly. */
2911 if (instrumented_version
2912 && orig_decl
2913 && orig_decl != instrumented_version->decl)
2915 error ("Instrumented node has wrong original declaration");
2916 error_found = true;
2919 /* Check all nodes have chkp reference to their instrumented versions. */
2920 if (analyzed
2921 && instrumented_version
2922 && !instrumentation_clone)
2924 bool ref_found = false;
2925 int i;
2926 struct ipa_ref *ref;
2928 for (i = 0; iterate_reference (i, ref); i++)
2929 if (ref->use == IPA_REF_CHKP)
2931 if (ref_found)
2933 error ("Node has more than one chkp reference");
2934 error_found = true;
2936 if (ref->referred != instrumented_version)
2938 error ("Wrong node is referenced with chkp reference");
2939 error_found = true;
2941 ref_found = true;
2944 if (!ref_found)
2946 error ("Analyzed node has no reference to instrumented version");
2947 error_found = true;
2951 if (analyzed && thunk.thunk_p)
2953 if (!callees)
2955 error ("No edge out of thunk node");
2956 error_found = true;
2958 else if (callees->next_callee)
2960 error ("More than one edge out of thunk node");
2961 error_found = true;
2963 if (gimple_has_body_p (decl))
2965 error ("Thunk is not supposed to have body");
2966 error_found = true;
2968 if (thunk.add_pointer_bounds_args
2969 && !instrumented_version->semantically_equivalent_p (callees->callee))
2971 error ("Instrumentation thunk has wrong edge callee");
2972 error_found = true;
2975 else if (analyzed && gimple_has_body_p (decl)
2976 && !TREE_ASM_WRITTEN (decl)
2977 && (!DECL_EXTERNAL (decl) || global.inlined_to)
2978 && !flag_wpa)
2980 if (this_cfun->cfg)
2982 hash_set<gimple> stmts;
2983 int i;
2984 ipa_ref *ref = NULL;
2986 /* Reach the trees by walking over the CFG, and note the
2987 enclosing basic-blocks in the call edges. */
2988 FOR_EACH_BB_FN (this_block, this_cfun)
2990 for (gsi = gsi_start_phis (this_block);
2991 !gsi_end_p (gsi); gsi_next (&gsi))
2992 stmts.add (gsi_stmt (gsi));
2993 for (gsi = gsi_start_bb (this_block);
2994 !gsi_end_p (gsi);
2995 gsi_next (&gsi))
2997 gimple stmt = gsi_stmt (gsi);
2998 stmts.add (stmt);
2999 if (is_gimple_call (stmt))
3001 cgraph_edge *e = get_edge (stmt);
3002 tree decl = gimple_call_fndecl (stmt);
3003 if (e)
3005 if (e->aux)
3007 error ("shared call_stmt:");
3008 cgraph_debug_gimple_stmt (this_cfun, stmt);
3009 error_found = true;
3011 if (!e->indirect_unknown_callee)
3013 if (verify_edge_corresponds_to_fndecl (e, decl))
3015 error ("edge points to wrong declaration:");
3016 debug_tree (e->callee->decl);
3017 fprintf (stderr," Instead of:");
3018 debug_tree (decl);
3019 error_found = true;
3022 else if (decl)
3024 error ("an indirect edge with unknown callee "
3025 "corresponding to a call_stmt with "
3026 "a known declaration:");
3027 error_found = true;
3028 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3030 e->aux = (void *)1;
3032 else if (decl)
3034 error ("missing callgraph edge for call stmt:");
3035 cgraph_debug_gimple_stmt (this_cfun, stmt);
3036 error_found = true;
3041 for (i = 0; iterate_reference (i, ref); i++)
3042 if (ref->stmt && !stmts.contains (ref->stmt))
3044 error ("reference to dead statement");
3045 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3046 error_found = true;
3049 else
3050 /* No CFG available?! */
3051 gcc_unreachable ();
3053 for (e = callees; e; e = e->next_callee)
3055 if (!e->aux)
3057 error ("edge %s->%s has no corresponding call_stmt",
3058 identifier_to_locale (e->caller->name ()),
3059 identifier_to_locale (e->callee->name ()));
3060 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3061 error_found = true;
3063 e->aux = 0;
3065 for (e = indirect_calls; e; e = e->next_callee)
3067 if (!e->aux && !e->speculative)
3069 error ("an indirect edge from %s has no corresponding call_stmt",
3070 identifier_to_locale (e->caller->name ()));
3071 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3072 error_found = true;
3074 e->aux = 0;
3077 if (error_found)
3079 dump (stderr);
3080 internal_error ("verify_cgraph_node failed");
3082 timevar_pop (TV_CGRAPH_VERIFY);
3085 /* Verify whole cgraph structure. */
3086 DEBUG_FUNCTION void
3087 cgraph_node::verify_cgraph_nodes (void)
3089 cgraph_node *node;
3091 if (seen_error ())
3092 return;
3094 FOR_EACH_FUNCTION (node)
3095 node->verify ();
3098 /* Walk the alias chain to return the function cgraph_node is alias of.
3099 Walk through thunk, too.
3100 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3102 cgraph_node *
3103 cgraph_node::function_symbol (enum availability *availability)
3105 cgraph_node *node = this;
3109 node = node->ultimate_alias_target (availability);
3110 if (node->thunk.thunk_p)
3112 node = node->callees->callee;
3113 if (availability)
3115 enum availability a;
3116 a = node->get_availability ();
3117 if (a < *availability)
3118 *availability = a;
3120 node = node->ultimate_alias_target (availability);
3122 } while (node && node->thunk.thunk_p);
3123 return node;
3126 /* When doing LTO, read cgraph_node's body from disk if it is not already
3127 present. */
3129 bool
3130 cgraph_node::get_body (void)
3132 lto_file_decl_data *file_data;
3133 const char *data, *name;
3134 size_t len;
3135 tree decl = this->decl;
3137 if (DECL_RESULT (decl))
3138 return false;
3140 gcc_assert (in_lto_p);
3142 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3144 file_data = lto_file_data;
3145 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3147 /* We may have renamed the declaration, e.g., a static function. */
3148 name = lto_get_decl_name_mapping (file_data, name);
3150 data = lto_get_section_data (file_data, LTO_section_function_body,
3151 name, &len);
3152 if (!data)
3153 fatal_error ("%s: section %s is missing",
3154 file_data->file_name,
3155 name);
3157 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3159 lto_input_function_body (file_data, this, data);
3160 lto_stats.num_function_bodies++;
3161 lto_free_section_data (file_data, LTO_section_function_body, name,
3162 data, len);
3163 lto_free_function_in_decl_state_for_node (this);
3165 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3167 return true;
3170 /* Return the DECL_STRUCT_FUNCTION of the function. */
3172 struct function *
3173 cgraph_node::get_fun (void)
3175 cgraph_node *node = this;
3176 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3178 while (!fun && node->clone_of)
3180 node = node->clone_of;
3181 fun = DECL_STRUCT_FUNCTION (node->decl);
3184 return fun;
3187 /* Verify if the type of the argument matches that of the function
3188 declaration. If we cannot verify this or there is a mismatch,
3189 return false. */
3191 static bool
3192 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3194 tree parms, p;
3195 unsigned int i, nargs;
3197 /* Calls to internal functions always match their signature. */
3198 if (gimple_call_internal_p (stmt))
3199 return true;
3201 nargs = gimple_call_num_args (stmt);
3203 /* Get argument types for verification. */
3204 if (fndecl)
3205 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3206 else
3207 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3209 /* Verify if the type of the argument matches that of the function
3210 declaration. If we cannot verify this or there is a mismatch,
3211 return false. */
3212 if (fndecl && DECL_ARGUMENTS (fndecl))
3214 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3215 i < nargs;
3216 i++, p = DECL_CHAIN (p))
3218 tree arg;
3219 /* We cannot distinguish a varargs function from the case
3220 of excess parameters, still deferring the inlining decision
3221 to the callee is possible. */
3222 if (!p)
3223 break;
3224 arg = gimple_call_arg (stmt, i);
3225 if (p == error_mark_node
3226 || DECL_ARG_TYPE (p) == error_mark_node
3227 || arg == error_mark_node
3228 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3229 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3230 return false;
3232 if (args_count_match && p)
3233 return false;
3235 else if (parms)
3237 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3239 tree arg;
3240 /* If this is a varargs function defer inlining decision
3241 to callee. */
3242 if (!p)
3243 break;
3244 arg = gimple_call_arg (stmt, i);
3245 if (TREE_VALUE (p) == error_mark_node
3246 || arg == error_mark_node
3247 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3248 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3249 && !fold_convertible_p (TREE_VALUE (p), arg)))
3250 return false;
3253 else
3255 if (nargs != 0)
3256 return false;
3258 return true;
3261 /* Verify if the type of the argument and lhs of CALL_STMT matches
3262 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3263 true, the arg count needs to be the same.
3264 If we cannot verify this or there is a mismatch, return false. */
3266 bool
3267 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3268 bool args_count_match)
3270 tree lhs;
3272 if ((DECL_RESULT (callee)
3273 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3274 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3275 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3276 TREE_TYPE (lhs))
3277 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3278 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3279 return false;
3280 return true;
3283 /* Reset all state within cgraph.c so that we can rerun the compiler
3284 within the same process. For use by toplev::finalize. */
3286 void
3287 cgraph_c_finalize (void)
3289 symtab = NULL;
3291 x_cgraph_nodes_queue = NULL;
3293 cgraph_fnver_htab = NULL;
3294 version_info_node = NULL;
3297 #include "gt-cgraph.h"