PR debug/66535
[official-gcc.git] / gcc / cgraph.c
blob95907fb51241f12135637eaa62f7b58ba7a1461d
1 /* Callgraph handling code.
2 Copyright (C) 2003-2015 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 "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "varasm.h"
36 #include "calls.h"
37 #include "print-tree.h"
38 #include "tree-inline.h"
39 #include "langhooks.h"
40 #include "toplev.h"
41 #include "flags.h"
42 #include "debug.h"
43 #include "target.h"
44 #include "predict.h"
45 #include "dominance.h"
46 #include "cfg.h"
47 #include "basic-block.h"
48 #include "is-a.h"
49 #include "plugin-api.h"
50 #include "hard-reg-set.h"
51 #include "function.h"
52 #include "ipa-ref.h"
53 #include "cgraph.h"
54 #include "intl.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "tree-eh.h"
58 #include "gimple-expr.h"
59 #include "gimple.h"
60 #include "gimple-iterator.h"
61 #include "timevar.h"
62 #include "dumpfile.h"
63 #include "gimple-ssa.h"
64 #include "tree-cfg.h"
65 #include "tree-ssa.h"
66 #include "value-prof.h"
67 #include "except.h"
68 #include "diagnostic-core.h"
69 #include "rtl.h"
70 #include "ipa-utils.h"
71 #include "lto-streamer.h"
72 #include "alloc-pool.h"
73 #include "symbol-summary.h"
74 #include "ipa-prop.h"
75 #include "ipa-inline.h"
76 #include "cfgloop.h"
77 #include "gimple-pretty-print.h"
78 #include "insn-config.h"
79 #include "expmed.h"
80 #include "dojump.h"
81 #include "explow.h"
82 #include "emit-rtl.h"
83 #include "stmt.h"
84 #include "expr.h"
85 #include "tree-dfa.h"
86 #include "profile.h"
87 #include "params.h"
88 #include "tree-chkp.h"
89 #include "context.h"
91 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
92 #include "tree-pass.h"
94 /* Queue of cgraph nodes scheduled to be lowered. */
95 symtab_node *x_cgraph_nodes_queue;
96 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
98 /* Symbol table global context. */
99 symbol_table *symtab;
101 /* List of hooks triggered on cgraph_edge events. */
102 struct cgraph_edge_hook_list {
103 cgraph_edge_hook hook;
104 void *data;
105 struct cgraph_edge_hook_list *next;
108 /* List of hooks triggered on cgraph_node events. */
109 struct cgraph_node_hook_list {
110 cgraph_node_hook hook;
111 void *data;
112 struct cgraph_node_hook_list *next;
115 /* List of hooks triggered on events involving two cgraph_edges. */
116 struct cgraph_2edge_hook_list {
117 cgraph_2edge_hook hook;
118 void *data;
119 struct cgraph_2edge_hook_list *next;
122 /* List of hooks triggered on events involving two cgraph_nodes. */
123 struct cgraph_2node_hook_list {
124 cgraph_2node_hook hook;
125 void *data;
126 struct cgraph_2node_hook_list *next;
129 /* Hash descriptor for cgraph_function_version_info. */
131 struct function_version_hasher : ggc_hasher<cgraph_function_version_info *>
133 static hashval_t hash (cgraph_function_version_info *);
134 static bool equal (cgraph_function_version_info *,
135 cgraph_function_version_info *);
138 /* Map a cgraph_node to cgraph_function_version_info using this htab.
139 The cgraph_function_version_info has a THIS_NODE field that is the
140 corresponding cgraph_node.. */
142 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
144 /* Hash function for cgraph_fnver_htab. */
145 hashval_t
146 function_version_hasher::hash (cgraph_function_version_info *ptr)
148 int uid = ptr->this_node->uid;
149 return (hashval_t)(uid);
152 /* eq function for cgraph_fnver_htab. */
153 bool
154 function_version_hasher::equal (cgraph_function_version_info *n1,
155 cgraph_function_version_info *n2)
157 return n1->this_node->uid == n2->this_node->uid;
160 /* Mark as GC root all allocated nodes. */
161 static GTY(()) struct cgraph_function_version_info *
162 version_info_node = NULL;
164 /* Get the cgraph_function_version_info node corresponding to node. */
165 cgraph_function_version_info *
166 cgraph_node::function_version (void)
168 cgraph_function_version_info key;
169 key.this_node = this;
171 if (cgraph_fnver_htab == NULL)
172 return NULL;
174 return cgraph_fnver_htab->find (&key);
177 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
178 corresponding to cgraph_node NODE. */
179 cgraph_function_version_info *
180 cgraph_node::insert_new_function_version (void)
182 version_info_node = NULL;
183 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
184 version_info_node->this_node = this;
186 if (cgraph_fnver_htab == NULL)
187 cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
189 *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
190 = version_info_node;
191 return version_info_node;
194 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
195 DECL is a duplicate declaration. */
196 void
197 cgraph_node::delete_function_version (tree decl)
199 cgraph_node *decl_node = cgraph_node::get (decl);
200 cgraph_function_version_info *decl_v = NULL;
202 if (decl_node == NULL)
203 return;
205 decl_v = decl_node->function_version ();
207 if (decl_v == NULL)
208 return;
210 if (decl_v->prev != NULL)
211 decl_v->prev->next = decl_v->next;
213 if (decl_v->next != NULL)
214 decl_v->next->prev = decl_v->prev;
216 if (cgraph_fnver_htab != NULL)
217 cgraph_fnver_htab->remove_elt (decl_v);
219 decl_node->remove ();
222 /* Record that DECL1 and DECL2 are semantically identical function
223 versions. */
224 void
225 cgraph_node::record_function_versions (tree decl1, tree decl2)
227 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
228 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
229 cgraph_function_version_info *decl1_v = NULL;
230 cgraph_function_version_info *decl2_v = NULL;
231 cgraph_function_version_info *before;
232 cgraph_function_version_info *after;
234 gcc_assert (decl1_node != NULL && decl2_node != NULL);
235 decl1_v = decl1_node->function_version ();
236 decl2_v = decl2_node->function_version ();
238 if (decl1_v != NULL && decl2_v != NULL)
239 return;
241 if (decl1_v == NULL)
242 decl1_v = decl1_node->insert_new_function_version ();
244 if (decl2_v == NULL)
245 decl2_v = decl2_node->insert_new_function_version ();
247 /* Chain decl2_v and decl1_v. All semantically identical versions
248 will be chained together. */
250 before = decl1_v;
251 after = decl2_v;
253 while (before->next != NULL)
254 before = before->next;
256 while (after->prev != NULL)
257 after= after->prev;
259 before->next = after;
260 after->prev = before;
263 /* Initialize callgraph dump file. */
265 void
266 symbol_table::initialize (void)
268 if (!dump_file)
269 dump_file = dump_begin (TDI_cgraph, NULL);
272 /* Allocate new callgraph node and insert it into basic data structures. */
274 cgraph_node *
275 symbol_table::create_empty (void)
277 cgraph_node *node = allocate_cgraph_symbol ();
279 node->type = SYMTAB_FUNCTION;
280 node->frequency = NODE_FREQUENCY_NORMAL;
281 node->count_materialization_scale = REG_BR_PROB_BASE;
282 cgraph_count++;
284 return node;
287 /* Register HOOK to be called with DATA on each removed edge. */
288 cgraph_edge_hook_list *
289 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
291 cgraph_edge_hook_list *entry;
292 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
294 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
295 entry->hook = hook;
296 entry->data = data;
297 entry->next = NULL;
298 while (*ptr)
299 ptr = &(*ptr)->next;
300 *ptr = entry;
301 return entry;
304 /* Remove ENTRY from the list of hooks called on removing edges. */
305 void
306 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
308 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
310 while (*ptr != entry)
311 ptr = &(*ptr)->next;
312 *ptr = entry->next;
313 free (entry);
316 /* Call all edge removal hooks. */
317 void
318 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
320 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
321 while (entry)
323 entry->hook (e, entry->data);
324 entry = entry->next;
328 /* Register HOOK to be called with DATA on each removed node. */
329 cgraph_node_hook_list *
330 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
332 cgraph_node_hook_list *entry;
333 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
335 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
336 entry->hook = hook;
337 entry->data = data;
338 entry->next = NULL;
339 while (*ptr)
340 ptr = &(*ptr)->next;
341 *ptr = entry;
342 return entry;
345 /* Remove ENTRY from the list of hooks called on removing nodes. */
346 void
347 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
349 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
351 while (*ptr != entry)
352 ptr = &(*ptr)->next;
353 *ptr = entry->next;
354 free (entry);
357 /* Call all node removal hooks. */
358 void
359 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
361 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
362 while (entry)
364 entry->hook (node, entry->data);
365 entry = entry->next;
369 /* Call all node removal hooks. */
370 void
371 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
373 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
374 while (entry)
376 entry->hook (node, entry->data);
377 entry = entry->next;
382 /* Register HOOK to be called with DATA on each inserted node. */
383 cgraph_node_hook_list *
384 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
386 cgraph_node_hook_list *entry;
387 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
389 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
390 entry->hook = hook;
391 entry->data = data;
392 entry->next = NULL;
393 while (*ptr)
394 ptr = &(*ptr)->next;
395 *ptr = entry;
396 return entry;
399 /* Remove ENTRY from the list of hooks called on inserted nodes. */
400 void
401 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
403 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
405 while (*ptr != entry)
406 ptr = &(*ptr)->next;
407 *ptr = entry->next;
408 free (entry);
411 /* Register HOOK to be called with DATA on each duplicated edge. */
412 cgraph_2edge_hook_list *
413 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
415 cgraph_2edge_hook_list *entry;
416 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
418 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
419 entry->hook = hook;
420 entry->data = data;
421 entry->next = NULL;
422 while (*ptr)
423 ptr = &(*ptr)->next;
424 *ptr = entry;
425 return entry;
428 /* Remove ENTRY from the list of hooks called on duplicating edges. */
429 void
430 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
432 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
434 while (*ptr != entry)
435 ptr = &(*ptr)->next;
436 *ptr = entry->next;
437 free (entry);
440 /* Call all edge duplication hooks. */
441 void
442 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
444 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
445 while (entry)
447 entry->hook (cs1, cs2, entry->data);
448 entry = entry->next;
452 /* Register HOOK to be called with DATA on each duplicated node. */
453 cgraph_2node_hook_list *
454 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
456 cgraph_2node_hook_list *entry;
457 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
459 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
460 entry->hook = hook;
461 entry->data = data;
462 entry->next = NULL;
463 while (*ptr)
464 ptr = &(*ptr)->next;
465 *ptr = entry;
466 return entry;
469 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
470 void
471 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
473 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
475 while (*ptr != entry)
476 ptr = &(*ptr)->next;
477 *ptr = entry->next;
478 free (entry);
481 /* Call all node duplication hooks. */
482 void
483 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
484 cgraph_node *node2)
486 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
487 while (entry)
489 entry->hook (node, node2, entry->data);
490 entry = entry->next;
494 /* Return cgraph node assigned to DECL. Create new one when needed. */
496 cgraph_node *
497 cgraph_node::create (tree decl)
499 cgraph_node *node = symtab->create_empty ();
500 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
502 node->decl = decl;
504 if ((flag_openacc || flag_openmp)
505 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
507 node->offloadable = 1;
508 #ifdef ENABLE_OFFLOADING
509 g->have_offload = true;
510 #endif
513 node->register_symbol ();
515 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
517 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
518 node->next_nested = node->origin->nested;
519 node->origin->nested = node;
521 return node;
524 /* Try to find a call graph node for declaration DECL and if it does not exist
525 or if it corresponds to an inline clone, create a new one. */
527 cgraph_node *
528 cgraph_node::get_create (tree decl)
530 cgraph_node *first_clone = cgraph_node::get (decl);
532 if (first_clone && !first_clone->global.inlined_to)
533 return first_clone;
535 cgraph_node *node = cgraph_node::create (decl);
536 if (first_clone)
538 first_clone->clone_of = node;
539 node->clones = first_clone;
540 symtab->symtab_prevail_in_asm_name_hash (node);
541 node->decl->decl_with_vis.symtab_node = node;
542 if (dump_file)
543 fprintf (dump_file, "Introduced new external node "
544 "(%s/%i) and turned into root of the clone tree.\n",
545 node->name (), node->order);
547 else if (dump_file)
548 fprintf (dump_file, "Introduced new external node "
549 "(%s/%i).\n", node->name (), node->order);
550 return node;
553 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
554 the function body is associated with (not necessarily cgraph_node (DECL). */
556 cgraph_node *
557 cgraph_node::create_alias (tree alias, tree target)
559 cgraph_node *alias_node;
561 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
562 || TREE_CODE (target) == IDENTIFIER_NODE);
563 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
564 alias_node = cgraph_node::get_create (alias);
565 gcc_assert (!alias_node->definition);
566 alias_node->alias_target = target;
567 alias_node->definition = true;
568 alias_node->alias = true;
569 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
570 alias_node->weakref = true;
571 return alias_node;
574 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
575 and NULL otherwise.
576 Same body aliases are output whenever the body of DECL is output,
577 and cgraph_node::get (ALIAS) transparently returns
578 cgraph_node::get (DECL). */
580 cgraph_node *
581 cgraph_node::create_same_body_alias (tree alias, tree decl)
583 cgraph_node *n;
584 #ifndef ASM_OUTPUT_DEF
585 /* If aliases aren't supported by the assembler, fail. */
586 return NULL;
587 #endif
588 /* Langhooks can create same body aliases of symbols not defined.
589 Those are useless. Drop them on the floor. */
590 if (symtab->global_info_ready)
591 return NULL;
593 n = cgraph_node::create_alias (alias, decl);
594 n->cpp_implicit_alias = true;
595 if (symtab->cpp_implicit_aliases_done)
596 n->resolve_alias (cgraph_node::get (decl));
597 return n;
600 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
601 aliases DECL with an adjustments made into the first parameter.
602 See comments in thunk_adjust for detail on the parameters. */
604 cgraph_node *
605 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
606 HOST_WIDE_INT fixed_offset,
607 HOST_WIDE_INT virtual_value,
608 tree virtual_offset,
609 tree real_alias)
611 cgraph_node *node;
613 node = cgraph_node::get (alias);
614 if (node)
615 node->reset ();
616 else
617 node = cgraph_node::create (alias);
618 gcc_checking_assert (!virtual_offset
619 || wi::eq_p (virtual_offset, virtual_value));
620 node->thunk.fixed_offset = fixed_offset;
621 node->thunk.this_adjusting = this_adjusting;
622 node->thunk.virtual_value = virtual_value;
623 node->thunk.virtual_offset_p = virtual_offset != NULL;
624 node->thunk.alias = real_alias;
625 node->thunk.thunk_p = true;
626 node->definition = true;
628 return node;
631 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
632 Return NULL if there's no such node. */
634 cgraph_node *
635 cgraph_node::get_for_asmname (tree asmname)
637 /* We do not want to look at inline clones. */
638 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
639 node;
640 node = node->next_sharing_asm_name)
642 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
643 if (cn && !cn->global.inlined_to)
644 return cn;
646 return NULL;
649 /* Returns a hash value for X (which really is a cgraph_edge). */
651 hashval_t
652 cgraph_edge_hasher::hash (cgraph_edge *e)
654 /* This is a really poor hash function, but it is what htab_hash_pointer
655 uses. */
656 return (hashval_t) ((intptr_t)e->call_stmt >> 3);
659 /* Returns a hash value for X (which really is a cgraph_edge). */
661 hashval_t
662 cgraph_edge_hasher::hash (gimple call_stmt)
664 /* This is a really poor hash function, but it is what htab_hash_pointer
665 uses. */
666 return (hashval_t) ((intptr_t)call_stmt >> 3);
669 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
671 inline bool
672 cgraph_edge_hasher::equal (cgraph_edge *x, gimple y)
674 return x->call_stmt == y;
677 /* Add call graph edge E to call site hash of its caller. */
679 static inline void
680 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
682 gimple call = e->call_stmt;
683 *e->caller->call_site_hash->find_slot_with_hash
684 (call, cgraph_edge_hasher::hash (call), INSERT) = e;
687 /* Add call graph edge E to call site hash of its caller. */
689 static inline void
690 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
692 /* There are two speculative edges for every statement (one direct,
693 one indirect); always hash the direct one. */
694 if (e->speculative && e->indirect_unknown_callee)
695 return;
696 cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
697 (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
698 if (*slot)
700 gcc_assert (((cgraph_edge *)*slot)->speculative);
701 if (e->callee)
702 *slot = e;
703 return;
705 gcc_assert (!*slot || e->speculative);
706 *slot = e;
709 /* Return the callgraph edge representing the GIMPLE_CALL statement
710 CALL_STMT. */
712 cgraph_edge *
713 cgraph_node::get_edge (gimple call_stmt)
715 cgraph_edge *e, *e2;
716 int n = 0;
718 if (call_site_hash)
719 return call_site_hash->find_with_hash
720 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
722 /* This loop may turn out to be performance problem. In such case adding
723 hashtables into call nodes with very many edges is probably best
724 solution. It is not good idea to add pointer into CALL_EXPR itself
725 because we want to make possible having multiple cgraph nodes representing
726 different clones of the same body before the body is actually cloned. */
727 for (e = callees; e; e = e->next_callee)
729 if (e->call_stmt == call_stmt)
730 break;
731 n++;
734 if (!e)
735 for (e = indirect_calls; e; e = e->next_callee)
737 if (e->call_stmt == call_stmt)
738 break;
739 n++;
742 if (n > 100)
744 call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
745 for (e2 = callees; e2; e2 = e2->next_callee)
746 cgraph_add_edge_to_call_site_hash (e2);
747 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
748 cgraph_add_edge_to_call_site_hash (e2);
751 return e;
755 /* Change field call_stmt of edge to NEW_STMT.
756 If UPDATE_SPECULATIVE and E is any component of speculative
757 edge, then update all components. */
759 void
760 cgraph_edge::set_call_stmt (gcall *new_stmt, bool update_speculative)
762 tree decl;
764 /* Speculative edges has three component, update all of them
765 when asked to. */
766 if (update_speculative && speculative)
768 cgraph_edge *direct, *indirect;
769 ipa_ref *ref;
771 speculative_call_info (direct, indirect, ref);
772 direct->set_call_stmt (new_stmt, false);
773 indirect->set_call_stmt (new_stmt, false);
774 ref->stmt = new_stmt;
775 return;
778 /* Only direct speculative edges go to call_site_hash. */
779 if (caller->call_site_hash
780 && (!speculative || !indirect_unknown_callee))
782 caller->call_site_hash->remove_elt_with_hash
783 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
786 cgraph_edge *e = this;
788 call_stmt = new_stmt;
789 if (indirect_unknown_callee
790 && (decl = gimple_call_fndecl (new_stmt)))
792 /* Constant propagation (and possibly also inlining?) can turn an
793 indirect call into a direct one. */
794 cgraph_node *new_callee = cgraph_node::get (decl);
796 gcc_checking_assert (new_callee);
797 e = make_direct (new_callee);
800 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
801 e->can_throw_external = stmt_can_throw_external (new_stmt);
802 pop_cfun ();
803 if (e->caller->call_site_hash)
804 cgraph_add_edge_to_call_site_hash (e);
807 /* Allocate a cgraph_edge structure and fill it with data according to the
808 parameters of which only CALLEE can be NULL (when creating an indirect call
809 edge). */
811 cgraph_edge *
812 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
813 gcall *call_stmt, gcov_type count, int freq,
814 bool indir_unknown_callee)
816 cgraph_edge *edge;
818 /* LTO does not actually have access to the call_stmt since these
819 have not been loaded yet. */
820 if (call_stmt)
822 /* This is a rather expensive check possibly triggering
823 construction of call stmt hashtable. */
824 #ifdef ENABLE_CHECKING
825 cgraph_edge *e;
826 gcc_checking_assert (
827 !(e = caller->get_edge (call_stmt)) || e->speculative);
828 #endif
830 gcc_assert (is_gimple_call (call_stmt));
833 if (free_edges)
835 edge = free_edges;
836 free_edges = NEXT_FREE_EDGE (edge);
838 else
840 edge = ggc_alloc<cgraph_edge> ();
841 edge->uid = edges_max_uid++;
844 edges_count++;
846 edge->aux = NULL;
847 edge->caller = caller;
848 edge->callee = callee;
849 edge->prev_caller = NULL;
850 edge->next_caller = NULL;
851 edge->prev_callee = NULL;
852 edge->next_callee = NULL;
853 edge->lto_stmt_uid = 0;
855 edge->count = count;
856 gcc_assert (count >= 0);
857 edge->frequency = freq;
858 gcc_assert (freq >= 0);
859 gcc_assert (freq <= CGRAPH_FREQ_MAX);
861 edge->call_stmt = call_stmt;
862 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
863 edge->can_throw_external
864 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
865 pop_cfun ();
866 if (call_stmt
867 && callee && callee->decl
868 && !gimple_check_call_matching_types (call_stmt, callee->decl,
869 false))
870 edge->call_stmt_cannot_inline_p = true;
871 else
872 edge->call_stmt_cannot_inline_p = false;
874 edge->indirect_info = NULL;
875 edge->indirect_inlining_edge = 0;
876 edge->speculative = false;
877 edge->indirect_unknown_callee = indir_unknown_callee;
878 if (opt_for_fn (edge->caller->decl, flag_devirtualize)
879 && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
880 edge->in_polymorphic_cdtor
881 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
882 caller->decl);
883 else
884 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
885 if (call_stmt && caller->call_site_hash)
886 cgraph_add_edge_to_call_site_hash (edge);
888 return edge;
891 /* Create edge from a given function to CALLEE in the cgraph. */
893 cgraph_edge *
894 cgraph_node::create_edge (cgraph_node *callee,
895 gcall *call_stmt, gcov_type count, int freq)
897 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
898 freq, false);
900 initialize_inline_failed (edge);
902 edge->next_caller = callee->callers;
903 if (callee->callers)
904 callee->callers->prev_caller = edge;
905 edge->next_callee = callees;
906 if (callees)
907 callees->prev_callee = edge;
908 callees = edge;
909 callee->callers = edge;
911 return edge;
914 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
916 cgraph_indirect_call_info *
917 cgraph_allocate_init_indirect_info (void)
919 cgraph_indirect_call_info *ii;
921 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
922 ii->param_index = -1;
923 return ii;
926 /* Create an indirect edge with a yet-undetermined callee where the call
927 statement destination is a formal parameter of the caller with index
928 PARAM_INDEX. */
930 cgraph_edge *
931 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
932 gcov_type count, int freq,
933 bool compute_indirect_info)
935 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt,
936 count, freq, true);
937 tree target;
939 initialize_inline_failed (edge);
941 edge->indirect_info = cgraph_allocate_init_indirect_info ();
942 edge->indirect_info->ecf_flags = ecf_flags;
943 edge->indirect_info->vptr_changed = true;
945 /* Record polymorphic call info. */
946 if (compute_indirect_info
947 && call_stmt
948 && (target = gimple_call_fn (call_stmt))
949 && virtual_method_call_p (target))
951 ipa_polymorphic_call_context context (decl, target, call_stmt);
953 /* Only record types can have virtual calls. */
954 edge->indirect_info->polymorphic = true;
955 edge->indirect_info->param_index = -1;
956 edge->indirect_info->otr_token
957 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
958 edge->indirect_info->otr_type = obj_type_ref_class (target);
959 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
960 edge->indirect_info->context = context;
963 edge->next_callee = indirect_calls;
964 if (indirect_calls)
965 indirect_calls->prev_callee = edge;
966 indirect_calls = edge;
968 return edge;
971 /* Remove the edge from the list of the callees of the caller. */
973 void
974 cgraph_edge::remove_caller (void)
976 if (prev_callee)
977 prev_callee->next_callee = next_callee;
978 if (next_callee)
979 next_callee->prev_callee = prev_callee;
980 if (!prev_callee)
982 if (indirect_unknown_callee)
983 caller->indirect_calls = next_callee;
984 else
985 caller->callees = next_callee;
987 if (caller->call_site_hash)
988 caller->call_site_hash->remove_elt_with_hash
989 (call_stmt, cgraph_edge_hasher::hash (call_stmt));
992 /* Put the edge onto the free list. */
994 void
995 symbol_table::free_edge (cgraph_edge *e)
997 int uid = e->uid;
999 if (e->indirect_info)
1000 ggc_free (e->indirect_info);
1002 /* Clear out the edge so we do not dangle pointers. */
1003 memset (e, 0, sizeof (*e));
1004 e->uid = uid;
1005 NEXT_FREE_EDGE (e) = free_edges;
1006 free_edges = e;
1007 edges_count--;
1010 /* Remove the edge in the cgraph. */
1012 void
1013 cgraph_edge::remove (void)
1015 /* Call all edge removal hooks. */
1016 symtab->call_edge_removal_hooks (this);
1018 if (!indirect_unknown_callee)
1019 /* Remove from callers list of the callee. */
1020 remove_callee ();
1022 /* Remove from callees list of the callers. */
1023 remove_caller ();
1025 /* Put the edge onto the free list. */
1026 symtab->free_edge (this);
1029 /* Turn edge into speculative call calling N2. Update
1030 the profile so the direct call is taken COUNT times
1031 with FREQUENCY.
1033 At clone materialization time, the indirect call E will
1034 be expanded as:
1036 if (call_dest == N2)
1037 n2 ();
1038 else
1039 call call_dest
1041 At this time the function just creates the direct call,
1042 the referencd representing the if conditional and attaches
1043 them all to the orginal indirect call statement.
1045 Return direct edge created. */
1047 cgraph_edge *
1048 cgraph_edge::make_speculative (cgraph_node *n2, gcov_type direct_count,
1049 int direct_frequency)
1051 cgraph_node *n = caller;
1052 ipa_ref *ref = NULL;
1053 cgraph_edge *e2;
1055 if (dump_file)
1057 fprintf (dump_file, "Indirect call -> speculative call"
1058 " %s/%i => %s/%i\n",
1059 xstrdup_for_dump (n->name ()), n->order,
1060 xstrdup_for_dump (n2->name ()), n2->order);
1062 speculative = true;
1063 e2 = n->create_edge (n2, call_stmt, direct_count, direct_frequency);
1064 initialize_inline_failed (e2);
1065 e2->speculative = true;
1066 if (TREE_NOTHROW (n2->decl))
1067 e2->can_throw_external = false;
1068 else
1069 e2->can_throw_external = can_throw_external;
1070 e2->lto_stmt_uid = lto_stmt_uid;
1071 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1072 count -= e2->count;
1073 frequency -= e2->frequency;
1074 symtab->call_edge_duplication_hooks (this, e2);
1075 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1076 ref->lto_stmt_uid = lto_stmt_uid;
1077 ref->speculative = speculative;
1078 n2->mark_address_taken ();
1079 return e2;
1082 /* Speculative call consist of three components:
1083 1) an indirect edge representing the original call
1084 2) an direct edge representing the new call
1085 3) ADDR_EXPR reference representing the speculative check.
1086 All three components are attached to single statement (the indirect
1087 call) and if one of them exists, all of them must exist.
1089 Given speculative call edge, return all three components.
1092 void
1093 cgraph_edge::speculative_call_info (cgraph_edge *&direct,
1094 cgraph_edge *&indirect,
1095 ipa_ref *&reference)
1097 ipa_ref *ref;
1098 int i;
1099 cgraph_edge *e2;
1100 cgraph_edge *e = this;
1102 if (!e->indirect_unknown_callee)
1103 for (e2 = e->caller->indirect_calls;
1104 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1105 e2 = e2->next_callee)
1107 else
1109 e2 = e;
1110 /* We can take advantage of the call stmt hash. */
1111 if (e2->call_stmt)
1113 e = e->caller->get_edge (e2->call_stmt);
1114 gcc_assert (e->speculative && !e->indirect_unknown_callee);
1116 else
1117 for (e = e->caller->callees;
1118 e2->call_stmt != e->call_stmt
1119 || e2->lto_stmt_uid != e->lto_stmt_uid;
1120 e = e->next_callee)
1123 gcc_assert (e->speculative && e2->speculative);
1124 direct = e;
1125 indirect = e2;
1127 reference = NULL;
1128 for (i = 0; e->caller->iterate_reference (i, ref); i++)
1129 if (ref->speculative
1130 && ((ref->stmt && ref->stmt == e->call_stmt)
1131 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
1133 reference = ref;
1134 break;
1137 /* Speculative edge always consist of all three components - direct edge,
1138 indirect and reference. */
1140 gcc_assert (e && e2 && ref);
1143 /* Speculative call edge turned out to be direct call to CALLE_DECL.
1144 Remove the speculative call sequence and return edge representing the call.
1145 It is up to caller to redirect the call as appropriate. */
1147 cgraph_edge *
1148 cgraph_edge::resolve_speculation (tree callee_decl)
1150 cgraph_edge *edge = this;
1151 cgraph_edge *e2;
1152 ipa_ref *ref;
1154 gcc_assert (edge->speculative);
1155 edge->speculative_call_info (e2, edge, ref);
1156 if (!callee_decl
1157 || !ref->referred->semantically_equivalent_p
1158 (symtab_node::get (callee_decl)))
1160 if (dump_file)
1162 if (callee_decl)
1164 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1165 "turned out to have contradicting known target ",
1166 xstrdup_for_dump (edge->caller->name ()),
1167 edge->caller->order,
1168 xstrdup_for_dump (e2->callee->name ()),
1169 e2->callee->order);
1170 print_generic_expr (dump_file, callee_decl, 0);
1171 fprintf (dump_file, "\n");
1173 else
1175 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
1176 xstrdup_for_dump (edge->caller->name ()),
1177 edge->caller->order,
1178 xstrdup_for_dump (e2->callee->name ()),
1179 e2->callee->order);
1183 else
1185 cgraph_edge *tmp = edge;
1186 if (dump_file)
1187 fprintf (dump_file, "Speculative call turned into direct call.\n");
1188 edge = e2;
1189 e2 = tmp;
1190 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1191 in the functions inlined through it. */
1193 edge->count += e2->count;
1194 edge->frequency += e2->frequency;
1195 if (edge->frequency > CGRAPH_FREQ_MAX)
1196 edge->frequency = CGRAPH_FREQ_MAX;
1197 edge->speculative = false;
1198 e2->speculative = false;
1199 ref->remove_reference ();
1200 if (e2->indirect_unknown_callee || e2->inline_failed)
1201 e2->remove ();
1202 else
1203 e2->callee->remove_symbol_and_inline_clones ();
1204 if (edge->caller->call_site_hash)
1205 cgraph_update_edge_in_call_site_hash (edge);
1206 return edge;
1209 /* Make an indirect edge with an unknown callee an ordinary edge leading to
1210 CALLEE. DELTA is an integer constant that is to be added to the this
1211 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1213 cgraph_edge *
1214 cgraph_edge::make_direct (cgraph_node *callee)
1216 cgraph_edge *edge = this;
1217 gcc_assert (indirect_unknown_callee);
1219 /* If we are redirecting speculative call, make it non-speculative. */
1220 if (indirect_unknown_callee && speculative)
1222 edge = edge->resolve_speculation (callee->decl);
1224 /* On successful speculation just return the pre existing direct edge. */
1225 if (!indirect_unknown_callee)
1226 return edge;
1229 indirect_unknown_callee = 0;
1230 ggc_free (indirect_info);
1231 indirect_info = NULL;
1233 /* Get the edge out of the indirect edge list. */
1234 if (prev_callee)
1235 prev_callee->next_callee = next_callee;
1236 if (next_callee)
1237 next_callee->prev_callee = prev_callee;
1238 if (!prev_callee)
1239 caller->indirect_calls = next_callee;
1241 /* Put it into the normal callee list */
1242 prev_callee = NULL;
1243 next_callee = caller->callees;
1244 if (caller->callees)
1245 caller->callees->prev_callee = edge;
1246 caller->callees = edge;
1248 /* Insert to callers list of the new callee. */
1249 edge->set_callee (callee);
1251 if (call_stmt)
1252 call_stmt_cannot_inline_p
1253 = !gimple_check_call_matching_types (call_stmt, callee->decl,
1254 false);
1256 /* We need to re-determine the inlining status of the edge. */
1257 initialize_inline_failed (edge);
1258 return edge;
1261 /* If necessary, change the function declaration in the call statement
1262 associated with E so that it corresponds to the edge callee. */
1264 gimple
1265 cgraph_edge::redirect_call_stmt_to_callee (void)
1267 cgraph_edge *e = this;
1269 tree decl = gimple_call_fndecl (e->call_stmt);
1270 tree lhs = gimple_call_lhs (e->call_stmt);
1271 gcall *new_stmt;
1272 gimple_stmt_iterator gsi;
1273 bool skip_bounds = false;
1274 #ifdef ENABLE_CHECKING
1275 cgraph_node *node;
1276 #endif
1278 if (e->speculative)
1280 cgraph_edge *e2;
1281 gcall *new_stmt;
1282 ipa_ref *ref;
1284 e->speculative_call_info (e, e2, ref);
1285 /* If there already is an direct call (i.e. as a result of inliner's
1286 substitution), forget about speculating. */
1287 if (decl)
1288 e = e->resolve_speculation (decl);
1289 /* If types do not match, speculation was likely wrong.
1290 The direct edge was posisbly redirected to the clone with a different
1291 signature. We did not update the call statement yet, so compare it
1292 with the reference that still points to the proper type. */
1293 else if (!gimple_check_call_matching_types (e->call_stmt,
1294 ref->referred->decl,
1295 true))
1297 if (dump_file)
1298 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1299 "Type mismatch.\n",
1300 xstrdup_for_dump (e->caller->name ()),
1301 e->caller->order,
1302 xstrdup_for_dump (e->callee->name ()),
1303 e->callee->order);
1304 e = e->resolve_speculation ();
1305 /* We are producing the final function body and will throw away the
1306 callgraph edges really soon. Reset the counts/frequencies to
1307 keep verifier happy in the case of roundoff errors. */
1308 e->count = gimple_bb (e->call_stmt)->count;
1309 e->frequency = compute_call_stmt_bb_frequency
1310 (e->caller->decl, gimple_bb (e->call_stmt));
1312 /* Expand speculation into GIMPLE code. */
1313 else
1315 if (dump_file)
1316 fprintf (dump_file,
1317 "Expanding speculative call of %s/%i -> %s/%i count:"
1318 "%" PRId64"\n",
1319 xstrdup_for_dump (e->caller->name ()),
1320 e->caller->order,
1321 xstrdup_for_dump (e->callee->name ()),
1322 e->callee->order,
1323 (int64_t)e->count);
1324 gcc_assert (e2->speculative);
1325 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1326 new_stmt = gimple_ic (e->call_stmt,
1327 dyn_cast<cgraph_node *> (ref->referred),
1328 e->count || e2->count
1329 ? RDIV (e->count * REG_BR_PROB_BASE,
1330 e->count + e2->count)
1331 : e->frequency || e2->frequency
1332 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1333 e->frequency + e2->frequency)
1334 : REG_BR_PROB_BASE / 2,
1335 e->count, e->count + e2->count);
1336 e->speculative = false;
1337 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1338 false);
1340 /* Fix edges for BUILT_IN_CHKP_BNDRET calls attached to the
1341 processed call stmt. */
1342 if (gimple_call_with_bounds_p (new_stmt)
1343 && gimple_call_lhs (new_stmt)
1344 && chkp_retbnd_call_by_val (gimple_call_lhs (e2->call_stmt)))
1346 tree dresult = gimple_call_lhs (new_stmt);
1347 tree iresult = gimple_call_lhs (e2->call_stmt);
1348 gcall *dbndret = chkp_retbnd_call_by_val (dresult);
1349 gcall *ibndret = chkp_retbnd_call_by_val (iresult);
1350 struct cgraph_edge *iedge
1351 = e2->caller->cgraph_node::get_edge (ibndret);
1352 struct cgraph_edge *dedge;
1354 if (dbndret)
1356 dedge = iedge->caller->create_edge (iedge->callee,
1357 dbndret, e->count,
1358 e->frequency);
1359 dedge->frequency = compute_call_stmt_bb_frequency
1360 (dedge->caller->decl, gimple_bb (dedge->call_stmt));
1362 iedge->frequency = compute_call_stmt_bb_frequency
1363 (iedge->caller->decl, gimple_bb (iedge->call_stmt));
1366 e->frequency = compute_call_stmt_bb_frequency
1367 (e->caller->decl, gimple_bb (e->call_stmt));
1368 e2->frequency = compute_call_stmt_bb_frequency
1369 (e2->caller->decl, gimple_bb (e2->call_stmt));
1370 e2->speculative = false;
1371 ref->speculative = false;
1372 ref->stmt = NULL;
1373 /* Indirect edges are not both in the call site hash.
1374 get it updated. */
1375 if (e->caller->call_site_hash)
1376 cgraph_update_edge_in_call_site_hash (e2);
1377 pop_cfun ();
1378 /* Continue redirecting E to proper target. */
1382 /* We might propagate instrumented function pointer into
1383 not instrumented function and vice versa. In such a
1384 case we need to either fix function declaration or
1385 remove bounds from call statement. */
1386 if (flag_check_pointer_bounds && e->callee)
1387 skip_bounds = chkp_redirect_edge (e);
1389 if (e->indirect_unknown_callee
1390 || (decl == e->callee->decl
1391 && !skip_bounds))
1392 return e->call_stmt;
1394 #ifdef ENABLE_CHECKING
1395 if (decl)
1397 node = cgraph_node::get (decl);
1398 gcc_assert (!node || !node->clone.combined_args_to_skip);
1400 #endif
1402 if (symtab->dump_file)
1404 fprintf (symtab->dump_file, "updating call of %s/%i -> %s/%i: ",
1405 xstrdup_for_dump (e->caller->name ()), e->caller->order,
1406 xstrdup_for_dump (e->callee->name ()), e->callee->order);
1407 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1408 if (e->callee->clone.combined_args_to_skip)
1410 fprintf (symtab->dump_file, " combined args to skip: ");
1411 dump_bitmap (symtab->dump_file,
1412 e->callee->clone.combined_args_to_skip);
1416 if (e->callee->clone.combined_args_to_skip
1417 || skip_bounds)
1419 int lp_nr;
1421 new_stmt = e->call_stmt;
1422 if (e->callee->clone.combined_args_to_skip)
1423 new_stmt
1424 = gimple_call_copy_skip_args (new_stmt,
1425 e->callee->clone.combined_args_to_skip);
1426 if (skip_bounds)
1427 new_stmt = chkp_copy_call_skip_bounds (new_stmt);
1429 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1430 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
1432 if (gimple_vdef (new_stmt)
1433 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1434 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1436 gsi = gsi_for_stmt (e->call_stmt);
1437 gsi_replace (&gsi, new_stmt, false);
1438 /* We need to defer cleaning EH info on the new statement to
1439 fixup-cfg. We may not have dominator information at this point
1440 and thus would end up with unreachable blocks and have no way
1441 to communicate that we need to run CFG cleanup then. */
1442 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1443 if (lp_nr != 0)
1445 remove_stmt_from_eh_lp (e->call_stmt);
1446 add_stmt_to_eh_lp (new_stmt, lp_nr);
1449 else
1451 new_stmt = e->call_stmt;
1452 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1453 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1456 /* If the call becomes noreturn, remove the LHS if possible. */
1457 if (lhs
1458 && (gimple_call_flags (new_stmt) & ECF_NORETURN)
1459 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
1461 if (TREE_CODE (lhs) == SSA_NAME)
1463 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1464 TREE_TYPE (lhs), NULL);
1465 var = get_or_create_ssa_default_def
1466 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1467 gimple set_stmt = gimple_build_assign (lhs, var);
1468 gsi = gsi_for_stmt (new_stmt);
1469 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1470 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1472 gimple_call_set_lhs (new_stmt, NULL_TREE);
1473 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1476 /* If new callee has no static chain, remove it. */
1477 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1479 gimple_call_set_chain (new_stmt, NULL);
1480 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1483 maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1484 new_stmt);
1486 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1488 if (symtab->dump_file)
1490 fprintf (symtab->dump_file, " updated to:");
1491 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1493 return new_stmt;
1496 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1497 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1498 of OLD_STMT if it was previously call statement.
1499 If NEW_STMT is NULL, the call has been dropped without any
1500 replacement. */
1502 static void
1503 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1504 gimple old_stmt, tree old_call,
1505 gimple new_stmt)
1507 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1508 ? gimple_call_fndecl (new_stmt) : 0;
1510 /* We are seeing indirect calls, then there is nothing to update. */
1511 if (!new_call && !old_call)
1512 return;
1513 /* See if we turned indirect call into direct call or folded call to one builtin
1514 into different builtin. */
1515 if (old_call != new_call)
1517 cgraph_edge *e = node->get_edge (old_stmt);
1518 cgraph_edge *ne = NULL;
1519 gcov_type count;
1520 int frequency;
1522 if (e)
1524 /* Keep calls marked as dead dead. */
1525 if (new_stmt && is_gimple_call (new_stmt) && e->callee
1526 && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
1527 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
1529 node->get_edge (old_stmt)->set_call_stmt
1530 (as_a <gcall *> (new_stmt));
1531 return;
1533 /* See if the edge is already there and has the correct callee. It
1534 might be so because of indirect inlining has already updated
1535 it. We also might've cloned and redirected the edge. */
1536 if (new_call && e->callee)
1538 cgraph_node *callee = e->callee;
1539 while (callee)
1541 if (callee->decl == new_call
1542 || callee->former_clone_of == new_call)
1544 e->set_call_stmt (as_a <gcall *> (new_stmt));
1545 return;
1547 callee = callee->clone_of;
1551 /* Otherwise remove edge and create new one; we can't simply redirect
1552 since function has changed, so inline plan and other information
1553 attached to edge is invalid. */
1554 count = e->count;
1555 frequency = e->frequency;
1556 if (e->indirect_unknown_callee || e->inline_failed)
1557 e->remove ();
1558 else
1559 e->callee->remove_symbol_and_inline_clones ();
1561 else if (new_call)
1563 /* We are seeing new direct call; compute profile info based on BB. */
1564 basic_block bb = gimple_bb (new_stmt);
1565 count = bb->count;
1566 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1567 bb);
1570 if (new_call)
1572 ne = node->create_edge (cgraph_node::get_create (new_call),
1573 as_a <gcall *> (new_stmt), count,
1574 frequency);
1575 gcc_assert (ne->inline_failed);
1578 /* We only updated the call stmt; update pointer in cgraph edge.. */
1579 else if (old_stmt != new_stmt)
1580 node->get_edge (old_stmt)->set_call_stmt (as_a <gcall *> (new_stmt));
1583 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1584 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1585 of OLD_STMT before it was updated (updating can happen inplace). */
1587 void
1588 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1590 cgraph_node *orig = cgraph_node::get (cfun->decl);
1591 cgraph_node *node;
1593 gcc_checking_assert (orig);
1594 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1595 if (orig->clones)
1596 for (node = orig->clones; node != orig;)
1598 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1599 if (node->clones)
1600 node = node->clones;
1601 else if (node->next_sibling_clone)
1602 node = node->next_sibling_clone;
1603 else
1605 while (node != orig && !node->next_sibling_clone)
1606 node = node->clone_of;
1607 if (node != orig)
1608 node = node->next_sibling_clone;
1614 /* Remove all callees from the node. */
1616 void
1617 cgraph_node::remove_callees (void)
1619 cgraph_edge *e, *f;
1621 /* It is sufficient to remove the edges from the lists of callers of
1622 the callees. The callee list of the node can be zapped with one
1623 assignment. */
1624 for (e = callees; e; e = f)
1626 f = e->next_callee;
1627 symtab->call_edge_removal_hooks (e);
1628 if (!e->indirect_unknown_callee)
1629 e->remove_callee ();
1630 symtab->free_edge (e);
1632 for (e = indirect_calls; e; e = f)
1634 f = e->next_callee;
1635 symtab->call_edge_removal_hooks (e);
1636 if (!e->indirect_unknown_callee)
1637 e->remove_callee ();
1638 symtab->free_edge (e);
1640 indirect_calls = NULL;
1641 callees = NULL;
1642 if (call_site_hash)
1644 call_site_hash->empty ();
1645 call_site_hash = NULL;
1649 /* Remove all callers from the node. */
1651 void
1652 cgraph_node::remove_callers (void)
1654 cgraph_edge *e, *f;
1656 /* It is sufficient to remove the edges from the lists of callees of
1657 the callers. The caller list of the node can be zapped with one
1658 assignment. */
1659 for (e = callers; e; e = f)
1661 f = e->next_caller;
1662 symtab->call_edge_removal_hooks (e);
1663 e->remove_caller ();
1664 symtab->free_edge (e);
1666 callers = NULL;
1669 /* Helper function for cgraph_release_function_body and free_lang_data.
1670 It releases body from function DECL without having to inspect its
1671 possibly non-existent symtab node. */
1673 void
1674 release_function_body (tree decl)
1676 if (DECL_STRUCT_FUNCTION (decl))
1678 if (DECL_STRUCT_FUNCTION (decl)->cfg
1679 || DECL_STRUCT_FUNCTION (decl)->gimple_df)
1681 push_cfun (DECL_STRUCT_FUNCTION (decl));
1682 if (cfun->cfg
1683 && current_loops)
1685 cfun->curr_properties &= ~PROP_loops;
1686 loop_optimizer_finalize ();
1688 if (cfun->gimple_df)
1690 delete_tree_ssa ();
1691 delete_tree_cfg_annotations ();
1692 cfun->eh = NULL;
1694 if (cfun->cfg)
1696 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1697 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
1698 clear_edges ();
1699 cfun->cfg = NULL;
1701 if (cfun->value_histograms)
1702 free_histograms ();
1703 pop_cfun ();
1705 gimple_set_body (decl, NULL);
1706 /* Struct function hangs a lot of data that would leak if we didn't
1707 removed all pointers to it. */
1708 ggc_free (DECL_STRUCT_FUNCTION (decl));
1709 DECL_STRUCT_FUNCTION (decl) = NULL;
1711 DECL_SAVED_TREE (decl) = NULL;
1714 /* Release memory used to represent body of function.
1715 Use this only for functions that are released before being translated to
1716 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1717 are free'd in final.c via free_after_compilation().
1718 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
1720 void
1721 cgraph_node::release_body (bool keep_arguments)
1723 ipa_transforms_to_apply.release ();
1724 if (!used_as_abstract_origin && symtab->state != PARSING)
1726 DECL_RESULT (decl) = NULL;
1728 if (!keep_arguments)
1729 DECL_ARGUMENTS (decl) = NULL;
1731 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1732 of its associated function function declaration because it's
1733 needed to emit debug info later. */
1734 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1735 DECL_INITIAL (decl) = error_mark_node;
1736 release_function_body (decl);
1737 if (lto_file_data)
1739 lto_free_function_in_decl_state_for_node (this);
1740 lto_file_data = NULL;
1744 /* Remove function from symbol table. */
1746 void
1747 cgraph_node::remove (void)
1749 cgraph_node *n;
1750 int uid = this->uid;
1752 symtab->call_cgraph_removal_hooks (this);
1753 remove_callers ();
1754 remove_callees ();
1755 ipa_transforms_to_apply.release ();
1757 /* Incremental inlining access removed nodes stored in the postorder list.
1759 force_output = false;
1760 forced_by_abi = false;
1761 for (n = nested; n; n = n->next_nested)
1762 n->origin = NULL;
1763 nested = NULL;
1764 if (origin)
1766 cgraph_node **node2 = &origin->nested;
1768 while (*node2 != this)
1769 node2 = &(*node2)->next_nested;
1770 *node2 = next_nested;
1772 unregister ();
1773 if (prev_sibling_clone)
1774 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1775 else if (clone_of)
1776 clone_of->clones = next_sibling_clone;
1777 if (next_sibling_clone)
1778 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1779 if (clones)
1781 cgraph_node *n, *next;
1783 if (clone_of)
1785 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1786 n->clone_of = clone_of;
1787 n->clone_of = clone_of;
1788 n->next_sibling_clone = clone_of->clones;
1789 if (clone_of->clones)
1790 clone_of->clones->prev_sibling_clone = n;
1791 clone_of->clones = clones;
1793 else
1795 /* We are removing node with clones. This makes clones inconsistent,
1796 but assume they will be removed subsequently and just keep clone
1797 tree intact. This can happen in unreachable function removal since
1798 we remove unreachable functions in random order, not by bottom-up
1799 walk of clone trees. */
1800 for (n = clones; n; n = next)
1802 next = n->next_sibling_clone;
1803 n->next_sibling_clone = NULL;
1804 n->prev_sibling_clone = NULL;
1805 n->clone_of = NULL;
1810 /* While all the clones are removed after being proceeded, the function
1811 itself is kept in the cgraph even after it is compiled. Check whether
1812 we are done with this body and reclaim it proactively if this is the case.
1814 if (symtab->state != LTO_STREAMING)
1816 n = cgraph_node::get (decl);
1817 if (!n
1818 || (!n->clones && !n->clone_of && !n->global.inlined_to
1819 && ((symtab->global_info_ready || in_lto_p)
1820 && (TREE_ASM_WRITTEN (n->decl)
1821 || DECL_EXTERNAL (n->decl)
1822 || !n->analyzed
1823 || (!flag_wpa && n->in_other_partition)))))
1824 release_body ();
1826 else
1828 lto_free_function_in_decl_state_for_node (this);
1829 lto_file_data = NULL;
1832 decl = NULL;
1833 if (call_site_hash)
1835 call_site_hash->empty ();
1836 call_site_hash = NULL;
1839 if (instrumented_version)
1841 instrumented_version->instrumented_version = NULL;
1842 instrumented_version = NULL;
1845 symtab->release_symbol (this, uid);
1848 /* Likewise indicate that a node is having address taken. */
1850 void
1851 cgraph_node::mark_address_taken (void)
1853 /* Indirect inlining can figure out that all uses of the address are
1854 inlined. */
1855 if (global.inlined_to)
1857 gcc_assert (cfun->after_inlining);
1858 gcc_assert (callers->indirect_inlining_edge);
1859 return;
1861 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1862 IPA_REF_ADDR reference exists (and thus it should be set on node
1863 representing alias we take address of) and as a test whether address
1864 of the object was taken (and thus it should be set on node alias is
1865 referring to). We should remove the first use and the remove the
1866 following set. */
1867 address_taken = 1;
1868 cgraph_node *node = ultimate_alias_target ();
1869 node->address_taken = 1;
1872 /* Return local info for the compiled function. */
1874 cgraph_local_info *
1875 cgraph_node::local_info (tree decl)
1877 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1878 cgraph_node *node = get (decl);
1879 if (!node)
1880 return NULL;
1881 return &node->ultimate_alias_target ()->local;
1884 /* Return local info for the compiled function. */
1886 cgraph_rtl_info *
1887 cgraph_node::rtl_info (tree decl)
1889 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1890 cgraph_node *node = get (decl);
1891 if (!node)
1892 return NULL;
1893 node = node->ultimate_alias_target ();
1894 if (node->decl != current_function_decl
1895 && !TREE_ASM_WRITTEN (node->decl))
1896 return NULL;
1897 return &node->ultimate_alias_target ()->rtl;
1900 /* Return a string describing the failure REASON. */
1902 const char*
1903 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1905 #undef DEFCIFCODE
1906 #define DEFCIFCODE(code, type, string) string,
1908 static const char *cif_string_table[CIF_N_REASONS] = {
1909 #include "cif-code.def"
1912 /* Signedness of an enum type is implementation defined, so cast it
1913 to unsigned before testing. */
1914 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1915 return cif_string_table[reason];
1918 /* Return a type describing the failure REASON. */
1920 cgraph_inline_failed_type_t
1921 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1923 #undef DEFCIFCODE
1924 #define DEFCIFCODE(code, type, string) type,
1926 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1927 #include "cif-code.def"
1930 /* Signedness of an enum type is implementation defined, so cast it
1931 to unsigned before testing. */
1932 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1933 return cif_type_table[reason];
1936 /* Names used to print out the availability enum. */
1937 const char * const cgraph_availability_names[] =
1938 {"unset", "not_available", "overwritable", "available", "local"};
1940 /* Output flags of edge to a file F. */
1942 void
1943 cgraph_edge::dump_edge_flags (FILE *f)
1945 if (speculative)
1946 fprintf (f, "(speculative) ");
1947 if (!inline_failed)
1948 fprintf (f, "(inlined) ");
1949 if (indirect_inlining_edge)
1950 fprintf (f, "(indirect_inlining) ");
1951 if (count)
1952 fprintf (f, "(%" PRId64"x) ", (int64_t)count);
1953 if (frequency)
1954 fprintf (f, "(%.2f per call) ", frequency / (double)CGRAPH_FREQ_BASE);
1955 if (can_throw_external)
1956 fprintf (f, "(can throw external) ");
1959 /* Dump call graph node to file F. */
1961 void
1962 cgraph_node::dump (FILE *f)
1964 cgraph_edge *edge;
1966 dump_base (f);
1968 if (global.inlined_to)
1969 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
1970 xstrdup_for_dump (name ()),
1971 order,
1972 xstrdup_for_dump (global.inlined_to->name ()),
1973 global.inlined_to->order);
1974 if (clone_of)
1975 fprintf (f, " Clone of %s/%i\n",
1976 clone_of->asm_name (),
1977 clone_of->order);
1978 if (symtab->function_flags_ready)
1979 fprintf (f, " Availability: %s\n",
1980 cgraph_availability_names [get_availability ()]);
1982 if (profile_id)
1983 fprintf (f, " Profile id: %i\n",
1984 profile_id);
1985 fprintf (f, " First run: %i\n", tp_first_run);
1986 fprintf (f, " Function flags:");
1987 if (count)
1988 fprintf (f, " executed %" PRId64"x",
1989 (int64_t)count);
1990 if (origin)
1991 fprintf (f, " nested in: %s", origin->asm_name ());
1992 if (gimple_has_body_p (decl))
1993 fprintf (f, " body");
1994 if (process)
1995 fprintf (f, " process");
1996 if (local.local)
1997 fprintf (f, " local");
1998 if (local.redefined_extern_inline)
1999 fprintf (f, " redefined_extern_inline");
2000 if (only_called_at_startup)
2001 fprintf (f, " only_called_at_startup");
2002 if (only_called_at_exit)
2003 fprintf (f, " only_called_at_exit");
2004 if (tm_clone)
2005 fprintf (f, " tm_clone");
2006 if (icf_merged)
2007 fprintf (f, " icf_merged");
2008 if (nonfreeing_fn)
2009 fprintf (f, " nonfreeing_fn");
2010 if (DECL_STATIC_CONSTRUCTOR (decl))
2011 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2012 if (DECL_STATIC_DESTRUCTOR (decl))
2013 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2014 if (frequency == NODE_FREQUENCY_HOT)
2015 fprintf (f, " hot");
2016 if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2017 fprintf (f, " unlikely_executed");
2018 if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2019 fprintf (f, " executed_once");
2020 if (only_called_at_startup)
2021 fprintf (f, " only_called_at_startup");
2022 if (only_called_at_exit)
2023 fprintf (f, " only_called_at_exit");
2024 if (opt_for_fn (decl, optimize_size))
2025 fprintf (f, " optimize_size");
2026 if (parallelized_function)
2027 fprintf (f, " parallelized_function");
2029 fprintf (f, "\n");
2031 if (thunk.thunk_p)
2033 fprintf (f, " Thunk");
2034 if (thunk.alias)
2035 fprintf (f, " of %s (asm: %s)",
2036 lang_hooks.decl_printable_name (thunk.alias, 2),
2037 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2038 fprintf (f, " fixed offset %i virtual value %i has "
2039 "virtual offset %i)\n",
2040 (int)thunk.fixed_offset,
2041 (int)thunk.virtual_value,
2042 (int)thunk.virtual_offset_p);
2044 if (alias && thunk.alias
2045 && DECL_P (thunk.alias))
2047 fprintf (f, " Alias of %s",
2048 lang_hooks.decl_printable_name (thunk.alias, 2));
2049 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
2050 fprintf (f, " (asm: %s)",
2051 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
2052 fprintf (f, "\n");
2055 fprintf (f, " Called by: ");
2057 for (edge = callers; edge; edge = edge->next_caller)
2059 fprintf (f, "%s/%i ", edge->caller->asm_name (),
2060 edge->caller->order);
2061 edge->dump_edge_flags (f);
2064 fprintf (f, "\n Calls: ");
2065 for (edge = callees; edge; edge = edge->next_callee)
2067 fprintf (f, "%s/%i ", edge->callee->asm_name (),
2068 edge->callee->order);
2069 edge->dump_edge_flags (f);
2071 fprintf (f, "\n");
2073 for (edge = indirect_calls; edge; edge = edge->next_callee)
2075 if (edge->indirect_info->polymorphic)
2077 fprintf (f, " Polymorphic indirect call of type ");
2078 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2079 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2081 else
2082 fprintf (f, " Indirect call");
2083 edge->dump_edge_flags (f);
2084 if (edge->indirect_info->param_index != -1)
2086 fprintf (f, " of param:%i", edge->indirect_info->param_index);
2087 if (edge->indirect_info->agg_contents)
2088 fprintf (f, " loaded from %s %s at offset %i",
2089 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2090 edge->indirect_info->by_ref ? "passed by reference":"",
2091 (int)edge->indirect_info->offset);
2092 if (edge->indirect_info->vptr_changed)
2093 fprintf (f, " (vptr maybe changed)");
2095 fprintf (f, "\n");
2096 if (edge->indirect_info->polymorphic)
2097 edge->indirect_info->context.dump (f);
2100 if (instrumentation_clone)
2101 fprintf (f, " Is instrumented version.\n");
2102 else if (instrumented_version)
2103 fprintf (f, " Has instrumented version.\n");
2106 /* Dump call graph node NODE to stderr. */
2108 DEBUG_FUNCTION void
2109 cgraph_node::debug (void)
2111 dump (stderr);
2114 /* Dump the callgraph to file F. */
2116 void
2117 cgraph_node::dump_cgraph (FILE *f)
2119 cgraph_node *node;
2121 fprintf (f, "callgraph:\n\n");
2122 FOR_EACH_FUNCTION (node)
2123 node->dump (f);
2126 /* Return true when the DECL can possibly be inlined. */
2128 bool
2129 cgraph_function_possibly_inlined_p (tree decl)
2131 if (!symtab->global_info_ready)
2132 return !DECL_UNINLINABLE (decl);
2133 return DECL_POSSIBLY_INLINED (decl);
2136 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2137 void
2138 cgraph_node::unnest (void)
2140 cgraph_node **node2 = &origin->nested;
2141 gcc_assert (origin);
2143 while (*node2 != this)
2144 node2 = &(*node2)->next_nested;
2145 *node2 = next_nested;
2146 origin = NULL;
2149 /* Return function availability. See cgraph.h for description of individual
2150 return values. */
2151 enum availability
2152 cgraph_node::get_availability (void)
2154 enum availability avail;
2155 if (!analyzed)
2156 avail = AVAIL_NOT_AVAILABLE;
2157 else if (local.local)
2158 avail = AVAIL_LOCAL;
2159 else if (alias && weakref)
2160 ultimate_alias_target (&avail);
2161 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2162 avail = AVAIL_INTERPOSABLE;
2163 else if (!externally_visible)
2164 avail = AVAIL_AVAILABLE;
2165 /* Inline functions are safe to be analyzed even if their symbol can
2166 be overwritten at runtime. It is not meaningful to enforce any sane
2167 behaviour on replacing inline function by different body. */
2168 else if (DECL_DECLARED_INLINE_P (decl))
2169 avail = AVAIL_AVAILABLE;
2171 /* If the function can be overwritten, return OVERWRITABLE. Take
2172 care at least of two notable extensions - the COMDAT functions
2173 used to share template instantiations in C++ (this is symmetric
2174 to code cp_cannot_inline_tree_fn and probably shall be shared and
2175 the inlinability hooks completely eliminated).
2177 ??? Does the C++ one definition rule allow us to always return
2178 AVAIL_AVAILABLE here? That would be good reason to preserve this
2179 bit. */
2181 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2182 avail = AVAIL_INTERPOSABLE;
2183 else avail = AVAIL_AVAILABLE;
2185 return avail;
2188 /* Worker for cgraph_node_can_be_local_p. */
2189 static bool
2190 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2192 return !(!node->force_output
2193 && ((DECL_COMDAT (node->decl)
2194 && !node->forced_by_abi
2195 && !node->used_from_object_file_p ()
2196 && !node->same_comdat_group)
2197 || !node->externally_visible));
2200 /* Return true if cgraph_node can be made local for API change.
2201 Extern inline functions and C++ COMDAT functions can be made local
2202 at the expense of possible code size growth if function is used in multiple
2203 compilation units. */
2204 bool
2205 cgraph_node::can_be_local_p (void)
2207 return (!address_taken
2208 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2209 NULL, true));
2212 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2213 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2214 skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2215 skipped. */
2216 bool
2217 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2218 (cgraph_node *, void *),
2219 void *data,
2220 bool include_overwritable,
2221 bool exclude_virtual_thunks)
2223 cgraph_edge *e;
2224 ipa_ref *ref;
2226 if (callback (this, data))
2227 return true;
2228 FOR_EACH_ALIAS (this, ref)
2230 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2231 if (include_overwritable
2232 || alias->get_availability () > AVAIL_INTERPOSABLE)
2233 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2234 include_overwritable,
2235 exclude_virtual_thunks))
2236 return true;
2238 for (e = callers; e; e = e->next_caller)
2239 if (e->caller->thunk.thunk_p
2240 && (include_overwritable
2241 || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2242 && !(exclude_virtual_thunks
2243 && e->caller->thunk.virtual_offset_p))
2244 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2245 include_overwritable,
2246 exclude_virtual_thunks))
2247 return true;
2249 return false;
2252 /* Worker to bring NODE local. */
2254 bool
2255 cgraph_node::make_local (cgraph_node *node, void *)
2257 gcc_checking_assert (node->can_be_local_p ());
2258 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2260 node->make_decl_local ();
2261 node->set_section (NULL);
2262 node->set_comdat_group (NULL);
2263 node->externally_visible = false;
2264 node->forced_by_abi = false;
2265 node->local.local = true;
2266 node->set_section (NULL);
2267 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
2268 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
2269 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2270 gcc_assert (node->get_availability () == AVAIL_LOCAL);
2272 return false;
2275 /* Bring cgraph node local. */
2277 void
2278 cgraph_node::make_local (void)
2280 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2283 /* Worker to set nothrow flag. */
2285 static bool
2286 cgraph_set_nothrow_flag_1 (cgraph_node *node, void *data)
2288 cgraph_edge *e;
2290 TREE_NOTHROW (node->decl) = data != NULL;
2292 if (data != NULL)
2293 for (e = node->callers; e; e = e->next_caller)
2294 e->can_throw_external = false;
2295 return false;
2298 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2299 if any to NOTHROW. */
2301 void
2302 cgraph_node::set_nothrow_flag (bool nothrow)
2304 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2305 (void *)(size_t)nothrow, false);
2308 /* Worker to set const flag. */
2310 static bool
2311 cgraph_set_const_flag_1 (cgraph_node *node, void *data)
2313 /* Static constructors and destructors without a side effect can be
2314 optimized out. */
2315 if (data && !((size_t)data & 2))
2317 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2318 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2319 if (DECL_STATIC_DESTRUCTOR (node->decl))
2320 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2322 TREE_READONLY (node->decl) = data != NULL;
2323 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2324 return false;
2327 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2328 if any to READONLY. */
2330 void
2331 cgraph_node::set_const_flag (bool readonly, bool looping)
2333 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2334 (void *)(size_t)(readonly + (int)looping * 2),
2335 false, true);
2338 /* Worker to set pure flag. */
2340 static bool
2341 cgraph_set_pure_flag_1 (cgraph_node *node, void *data)
2343 /* Static constructors and destructors without a side effect can be
2344 optimized out. */
2345 if (data && !((size_t)data & 2))
2347 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2348 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2349 if (DECL_STATIC_DESTRUCTOR (node->decl))
2350 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2352 DECL_PURE_P (node->decl) = data != NULL;
2353 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2354 return false;
2357 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2358 if any to PURE. */
2360 void
2361 cgraph_node::set_pure_flag (bool pure, bool looping)
2363 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2364 (void *)(size_t)(pure + (int)looping * 2),
2365 false, true);
2368 /* Return true when cgraph_node can not return or throw and thus
2369 it is safe to ignore its side effects for IPA analysis. */
2371 bool
2372 cgraph_node::cannot_return_p (void)
2374 int flags = flags_from_decl_or_type (decl);
2375 if (!opt_for_fn (decl, flag_exceptions))
2376 return (flags & ECF_NORETURN) != 0;
2377 else
2378 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2379 == (ECF_NORETURN | ECF_NOTHROW));
2382 /* Return true when call of edge can not lead to return from caller
2383 and thus it is safe to ignore its side effects for IPA analysis
2384 when computing side effects of the caller.
2385 FIXME: We could actually mark all edges that have no reaching
2386 patch to the exit block or throw to get better results. */
2387 bool
2388 cgraph_edge::cannot_lead_to_return_p (void)
2390 if (caller->cannot_return_p ())
2391 return true;
2392 if (indirect_unknown_callee)
2394 int flags = indirect_info->ecf_flags;
2395 if (!opt_for_fn (caller->decl, flag_exceptions))
2396 return (flags & ECF_NORETURN) != 0;
2397 else
2398 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2399 == (ECF_NORETURN | ECF_NOTHROW));
2401 else
2402 return callee->cannot_return_p ();
2405 /* Return true if the call can be hot. */
2407 bool
2408 cgraph_edge::maybe_hot_p (void)
2410 /* TODO: Export profile_status from cfun->cfg to cgraph_node. */
2411 if (profile_info
2412 && opt_for_fn (caller->decl, flag_branch_probabilities)
2413 && !maybe_hot_count_p (NULL, count))
2414 return false;
2415 if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2416 || (callee
2417 && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2418 return false;
2419 if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2420 && (callee
2421 && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2422 return false;
2423 if (opt_for_fn (caller->decl, optimize_size))
2424 return false;
2425 if (caller->frequency == NODE_FREQUENCY_HOT)
2426 return true;
2427 if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE
2428 && frequency < CGRAPH_FREQ_BASE * 3 / 2)
2429 return false;
2430 if (opt_for_fn (caller->decl, flag_guess_branch_prob))
2432 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0
2433 || frequency <= (CGRAPH_FREQ_BASE
2434 / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
2435 return false;
2437 return true;
2440 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2442 static bool
2443 nonremovable_p (cgraph_node *node, void *)
2445 return !node->can_remove_if_no_direct_calls_and_refs_p ();
2448 /* Return true if whole comdat group can be removed if there are no direct
2449 calls to THIS. */
2451 bool
2452 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2454 struct ipa_ref *ref;
2456 /* For local symbols or non-comdat group it is the same as
2457 can_remove_if_no_direct_calls_p. */
2458 if (!externally_visible || !same_comdat_group)
2460 if (DECL_EXTERNAL (decl))
2461 return true;
2462 if (address_taken)
2463 return false;
2464 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2467 if (will_inline && address_taken)
2468 return false;
2470 /* Otheriwse check if we can remove the symbol itself and then verify
2471 that only uses of the comdat groups are direct call to THIS
2472 or its aliases. */
2473 if (!can_remove_if_no_direct_calls_and_refs_p ())
2474 return false;
2476 /* Check that all refs come from within the comdat group. */
2477 for (int i = 0; iterate_referring (i, ref); i++)
2478 if (ref->referring->get_comdat_group () != get_comdat_group ())
2479 return false;
2481 struct cgraph_node *target = ultimate_alias_target ();
2482 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2483 next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2485 if (!externally_visible)
2486 continue;
2487 if (!next->alias
2488 && !next->can_remove_if_no_direct_calls_and_refs_p ())
2489 return false;
2491 /* If we see different symbol than THIS, be sure to check calls. */
2492 if (next->ultimate_alias_target () != target)
2493 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2494 if (e->caller->get_comdat_group () != get_comdat_group ()
2495 || will_inline)
2496 return false;
2498 /* If function is not being inlined, we care only about
2499 references outside of the comdat group. */
2500 if (!will_inline)
2501 for (int i = 0; next->iterate_referring (i, ref); i++)
2502 if (ref->referring->get_comdat_group () != get_comdat_group ())
2503 return false;
2505 return true;
2508 /* Return true when function cgraph_node can be expected to be removed
2509 from program when direct calls in this compilation unit are removed.
2511 As a special case COMDAT functions are
2512 cgraph_can_remove_if_no_direct_calls_p while the are not
2513 cgraph_only_called_directly_p (it is possible they are called from other
2514 unit)
2516 This function behaves as cgraph_only_called_directly_p because eliminating
2517 all uses of COMDAT function does not make it necessarily disappear from
2518 the program unless we are compiling whole program or we do LTO. In this
2519 case we know we win since dynamic linking will not really discard the
2520 linkonce section. */
2522 bool
2523 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
2524 (bool will_inline)
2526 gcc_assert (!global.inlined_to);
2527 if (DECL_EXTERNAL (decl))
2528 return true;
2530 if (!in_lto_p && !flag_whole_program)
2532 /* If the symbol is in comdat group, we need to verify that whole comdat
2533 group becomes unreachable. Technically we could skip references from
2534 within the group, too. */
2535 if (!only_called_directly_p ())
2536 return false;
2537 if (same_comdat_group && externally_visible)
2539 struct cgraph_node *target = ultimate_alias_target ();
2541 if (will_inline && address_taken)
2542 return true;
2543 for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
2544 next != this;
2545 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
2547 if (!externally_visible)
2548 continue;
2549 if (!next->alias
2550 && !next->only_called_directly_p ())
2551 return false;
2553 /* If we see different symbol than THIS,
2554 be sure to check calls. */
2555 if (next->ultimate_alias_target () != target)
2556 for (cgraph_edge *e = next->callers; e; e = e->next_caller)
2557 if (e->caller->get_comdat_group () != get_comdat_group ()
2558 || will_inline)
2559 return false;
2562 return true;
2564 else
2565 return can_remove_if_no_direct_calls_p (will_inline);
2569 /* Worker for cgraph_only_called_directly_p. */
2571 static bool
2572 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
2574 return !node->only_called_directly_or_aliased_p ();
2577 /* Return true when function cgraph_node and all its aliases are only called
2578 directly.
2579 i.e. it is not externally visible, address was not taken and
2580 it is not used in any other non-standard way. */
2582 bool
2583 cgraph_node::only_called_directly_p (void)
2585 gcc_assert (ultimate_alias_target () == this);
2586 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
2587 NULL, true);
2591 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2593 static bool
2594 collect_callers_of_node_1 (cgraph_node *node, void *data)
2596 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
2597 cgraph_edge *cs;
2598 enum availability avail;
2599 node->ultimate_alias_target (&avail);
2601 if (avail > AVAIL_INTERPOSABLE)
2602 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2603 if (!cs->indirect_inlining_edge)
2604 redirect_callers->safe_push (cs);
2605 return false;
2608 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2609 cgraph_node (i.e. are not overwritable). */
2611 vec<cgraph_edge *>
2612 cgraph_node::collect_callers (void)
2614 vec<cgraph_edge *> redirect_callers = vNULL;
2615 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2616 &redirect_callers, false);
2617 return redirect_callers;
2620 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2622 static bool
2623 clone_of_p (cgraph_node *node, cgraph_node *node2)
2625 bool skipped_thunk = false;
2626 node = node->ultimate_alias_target ();
2627 node2 = node2->ultimate_alias_target ();
2629 /* There are no virtual clones of thunks so check former_clone_of or if we
2630 might have skipped thunks because this adjustments are no longer
2631 necessary. */
2632 while (node->thunk.thunk_p)
2634 if (node2->former_clone_of == node->decl)
2635 return true;
2636 if (!node->thunk.this_adjusting)
2637 return false;
2638 node = node->callees->callee->ultimate_alias_target ();
2639 skipped_thunk = true;
2642 if (skipped_thunk)
2644 if (!node2->clone.args_to_skip
2645 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2646 return false;
2647 if (node2->former_clone_of == node->decl)
2648 return true;
2649 else if (!node2->clone_of)
2650 return false;
2653 while (node != node2 && node2)
2654 node2 = node2->clone_of;
2655 return node2 != NULL;
2658 /* Verify edge count and frequency. */
2660 bool
2661 cgraph_edge::verify_count_and_frequency ()
2663 bool error_found = false;
2664 if (count < 0)
2666 error ("caller edge count is negative");
2667 error_found = true;
2669 if (frequency < 0)
2671 error ("caller edge frequency is negative");
2672 error_found = true;
2674 if (frequency > CGRAPH_FREQ_MAX)
2676 error ("caller edge frequency is too large");
2677 error_found = true;
2679 return error_found;
2682 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2683 static void
2684 cgraph_debug_gimple_stmt (function *this_cfun, gimple stmt)
2686 bool fndecl_was_null = false;
2687 /* debug_gimple_stmt needs correct cfun */
2688 if (cfun != this_cfun)
2689 set_cfun (this_cfun);
2690 /* ...and an actual current_function_decl */
2691 if (!current_function_decl)
2693 current_function_decl = this_cfun->decl;
2694 fndecl_was_null = true;
2696 debug_gimple_stmt (stmt);
2697 if (fndecl_was_null)
2698 current_function_decl = NULL;
2701 /* Verify that call graph edge corresponds to DECL from the associated
2702 statement. Return true if the verification should fail. */
2704 bool
2705 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
2707 cgraph_node *node;
2709 if (!decl || callee->global.inlined_to)
2710 return false;
2711 if (symtab->state == LTO_STREAMING)
2712 return false;
2713 node = cgraph_node::get (decl);
2715 /* We do not know if a node from a different partition is an alias or what it
2716 aliases and therefore cannot do the former_clone_of check reliably. When
2717 body_removed is set, we have lost all information about what was alias or
2718 thunk of and also cannot proceed. */
2719 if (!node
2720 || node->body_removed
2721 || node->in_other_partition
2722 || callee->icf_merged
2723 || callee->in_other_partition)
2724 return false;
2726 node = node->ultimate_alias_target ();
2728 /* Optimizers can redirect unreachable calls or calls triggering undefined
2729 behaviour to builtin_unreachable. */
2730 if (DECL_BUILT_IN_CLASS (callee->decl) == BUILT_IN_NORMAL
2731 && DECL_FUNCTION_CODE (callee->decl) == BUILT_IN_UNREACHABLE)
2732 return false;
2734 if (callee->former_clone_of != node->decl
2735 && (node != callee->ultimate_alias_target ())
2736 && !clone_of_p (node, callee))
2737 return true;
2738 else
2739 return false;
2742 /* Verify cgraph nodes of given cgraph node. */
2743 DEBUG_FUNCTION void
2744 cgraph_node::verify_node (void)
2746 cgraph_edge *e;
2747 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
2748 basic_block this_block;
2749 gimple_stmt_iterator gsi;
2750 bool error_found = false;
2752 if (seen_error ())
2753 return;
2755 timevar_push (TV_CGRAPH_VERIFY);
2756 error_found |= verify_base ();
2757 for (e = callees; e; e = e->next_callee)
2758 if (e->aux)
2760 error ("aux field set for edge %s->%s",
2761 identifier_to_locale (e->caller->name ()),
2762 identifier_to_locale (e->callee->name ()));
2763 error_found = true;
2765 if (count < 0)
2767 error ("execution count is negative");
2768 error_found = true;
2770 if (global.inlined_to && same_comdat_group)
2772 error ("inline clone in same comdat group list");
2773 error_found = true;
2775 if (!definition && !in_other_partition && local.local)
2777 error ("local symbols must be defined");
2778 error_found = true;
2780 if (global.inlined_to && externally_visible)
2782 error ("externally visible inline clone");
2783 error_found = true;
2785 if (global.inlined_to && address_taken)
2787 error ("inline clone with address taken");
2788 error_found = true;
2790 if (global.inlined_to && force_output)
2792 error ("inline clone is forced to output");
2793 error_found = true;
2795 for (e = indirect_calls; e; e = e->next_callee)
2797 if (e->aux)
2799 error ("aux field set for indirect edge from %s",
2800 identifier_to_locale (e->caller->name ()));
2801 error_found = true;
2803 if (!e->indirect_unknown_callee
2804 || !e->indirect_info)
2806 error ("An indirect edge from %s is not marked as indirect or has "
2807 "associated indirect_info, the corresponding statement is: ",
2808 identifier_to_locale (e->caller->name ()));
2809 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2810 error_found = true;
2813 bool check_comdat = comdat_local_p ();
2814 for (e = callers; e; e = e->next_caller)
2816 if (e->verify_count_and_frequency ())
2817 error_found = true;
2818 if (check_comdat
2819 && !in_same_comdat_group_p (e->caller))
2821 error ("comdat-local function called by %s outside its comdat",
2822 identifier_to_locale (e->caller->name ()));
2823 error_found = true;
2825 if (!e->inline_failed)
2827 if (global.inlined_to
2828 != (e->caller->global.inlined_to
2829 ? e->caller->global.inlined_to : e->caller))
2831 error ("inlined_to pointer is wrong");
2832 error_found = true;
2834 if (callers->next_caller)
2836 error ("multiple inline callers");
2837 error_found = true;
2840 else
2841 if (global.inlined_to)
2843 error ("inlined_to pointer set for noninline callers");
2844 error_found = true;
2847 for (e = callees; e; e = e->next_callee)
2849 if (e->verify_count_and_frequency ())
2850 error_found = true;
2851 if (gimple_has_body_p (e->caller->decl)
2852 && !e->caller->global.inlined_to
2853 && !e->speculative
2854 /* Optimized out calls are redirected to __builtin_unreachable. */
2855 && (e->frequency
2856 || e->callee->decl
2857 != builtin_decl_implicit (BUILT_IN_UNREACHABLE))
2858 && (e->frequency
2859 != compute_call_stmt_bb_frequency (e->caller->decl,
2860 gimple_bb (e->call_stmt))))
2862 error ("caller edge frequency %i does not match BB frequency %i",
2863 e->frequency,
2864 compute_call_stmt_bb_frequency (e->caller->decl,
2865 gimple_bb (e->call_stmt)));
2866 error_found = true;
2869 for (e = indirect_calls; e; e = e->next_callee)
2871 if (e->verify_count_and_frequency ())
2872 error_found = true;
2873 if (gimple_has_body_p (e->caller->decl)
2874 && !e->caller->global.inlined_to
2875 && !e->speculative
2876 && (e->frequency
2877 != compute_call_stmt_bb_frequency (e->caller->decl,
2878 gimple_bb (e->call_stmt))))
2880 error ("indirect call frequency %i does not match BB frequency %i",
2881 e->frequency,
2882 compute_call_stmt_bb_frequency (e->caller->decl,
2883 gimple_bb (e->call_stmt)));
2884 error_found = true;
2887 if (!callers && global.inlined_to)
2889 error ("inlined_to pointer is set but no predecessors found");
2890 error_found = true;
2892 if (global.inlined_to == this)
2894 error ("inlined_to pointer refers to itself");
2895 error_found = true;
2898 if (clone_of)
2900 cgraph_node *n;
2901 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2902 if (n == this)
2903 break;
2904 if (!n)
2906 error ("cgraph_node has wrong clone_of");
2907 error_found = true;
2910 if (clones)
2912 cgraph_node *n;
2913 for (n = clones; n; n = n->next_sibling_clone)
2914 if (n->clone_of != this)
2915 break;
2916 if (n)
2918 error ("cgraph_node has wrong clone list");
2919 error_found = true;
2922 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
2924 error ("cgraph_node is in clone list but it is not clone");
2925 error_found = true;
2927 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
2929 error ("cgraph_node has wrong prev_clone pointer");
2930 error_found = true;
2932 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
2934 error ("double linked list of clones corrupted");
2935 error_found = true;
2938 if (analyzed && alias)
2940 bool ref_found = false;
2941 int i;
2942 ipa_ref *ref = NULL;
2944 if (callees)
2946 error ("Alias has call edges");
2947 error_found = true;
2949 for (i = 0; iterate_reference (i, ref); i++)
2950 if (ref->use == IPA_REF_CHKP)
2952 else if (ref->use != IPA_REF_ALIAS)
2954 error ("Alias has non-alias reference");
2955 error_found = true;
2957 else if (ref_found)
2959 error ("Alias has more than one alias reference");
2960 error_found = true;
2962 else
2963 ref_found = true;
2964 if (!ref_found)
2966 error ("Analyzed alias has no reference");
2967 error_found = true;
2971 /* Check instrumented version reference. */
2972 if (instrumented_version
2973 && instrumented_version->instrumented_version != this)
2975 error ("Instrumentation clone does not reference original node");
2976 error_found = true;
2979 /* Cannot have orig_decl for not instrumented nodes. */
2980 if (!instrumentation_clone && orig_decl)
2982 error ("Not instrumented node has non-NULL original declaration");
2983 error_found = true;
2986 /* If original not instrumented node still exists then we may check
2987 original declaration is set properly. */
2988 if (instrumented_version
2989 && orig_decl
2990 && orig_decl != instrumented_version->decl)
2992 error ("Instrumented node has wrong original declaration");
2993 error_found = true;
2996 /* Check all nodes have chkp reference to their instrumented versions. */
2997 if (analyzed
2998 && instrumented_version
2999 && !instrumentation_clone)
3001 bool ref_found = false;
3002 int i;
3003 struct ipa_ref *ref;
3005 for (i = 0; iterate_reference (i, ref); i++)
3006 if (ref->use == IPA_REF_CHKP)
3008 if (ref_found)
3010 error ("Node has more than one chkp reference");
3011 error_found = true;
3013 if (ref->referred != instrumented_version)
3015 error ("Wrong node is referenced with chkp reference");
3016 error_found = true;
3018 ref_found = true;
3021 if (!ref_found)
3023 error ("Analyzed node has no reference to instrumented version");
3024 error_found = true;
3028 if (instrumentation_clone
3029 && DECL_BUILT_IN_CLASS (decl) == NOT_BUILT_IN)
3031 tree name = DECL_ASSEMBLER_NAME (decl);
3032 tree orig_name = DECL_ASSEMBLER_NAME (orig_decl);
3034 if (!IDENTIFIER_TRANSPARENT_ALIAS (name)
3035 || TREE_CHAIN (name) != orig_name)
3037 error ("Alias chain for instrumented node is broken");
3038 error_found = true;
3042 if (analyzed && thunk.thunk_p)
3044 if (!callees)
3046 error ("No edge out of thunk node");
3047 error_found = true;
3049 else if (callees->next_callee)
3051 error ("More than one edge out of thunk node");
3052 error_found = true;
3054 if (gimple_has_body_p (decl))
3056 error ("Thunk is not supposed to have body");
3057 error_found = true;
3059 if (thunk.add_pointer_bounds_args
3060 && !instrumented_version->semantically_equivalent_p (callees->callee))
3062 error ("Instrumentation thunk has wrong edge callee");
3063 error_found = true;
3066 else if (analyzed && gimple_has_body_p (decl)
3067 && !TREE_ASM_WRITTEN (decl)
3068 && (!DECL_EXTERNAL (decl) || global.inlined_to)
3069 && !flag_wpa)
3071 if (this_cfun->cfg)
3073 hash_set<gimple> stmts;
3074 int i;
3075 ipa_ref *ref = NULL;
3077 /* Reach the trees by walking over the CFG, and note the
3078 enclosing basic-blocks in the call edges. */
3079 FOR_EACH_BB_FN (this_block, this_cfun)
3081 for (gsi = gsi_start_phis (this_block);
3082 !gsi_end_p (gsi); gsi_next (&gsi))
3083 stmts.add (gsi_stmt (gsi));
3084 for (gsi = gsi_start_bb (this_block);
3085 !gsi_end_p (gsi);
3086 gsi_next (&gsi))
3088 gimple stmt = gsi_stmt (gsi);
3089 stmts.add (stmt);
3090 if (is_gimple_call (stmt))
3092 cgraph_edge *e = get_edge (stmt);
3093 tree decl = gimple_call_fndecl (stmt);
3094 if (e)
3096 if (e->aux)
3098 error ("shared call_stmt:");
3099 cgraph_debug_gimple_stmt (this_cfun, stmt);
3100 error_found = true;
3102 if (!e->indirect_unknown_callee)
3104 if (e->verify_corresponds_to_fndecl (decl))
3106 error ("edge points to wrong declaration:");
3107 debug_tree (e->callee->decl);
3108 fprintf (stderr," Instead of:");
3109 debug_tree (decl);
3110 error_found = true;
3113 else if (decl)
3115 error ("an indirect edge with unknown callee "
3116 "corresponding to a call_stmt with "
3117 "a known declaration:");
3118 error_found = true;
3119 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3121 e->aux = (void *)1;
3123 else if (decl)
3125 error ("missing callgraph edge for call stmt:");
3126 cgraph_debug_gimple_stmt (this_cfun, stmt);
3127 error_found = true;
3132 for (i = 0; iterate_reference (i, ref); i++)
3133 if (ref->stmt && !stmts.contains (ref->stmt))
3135 error ("reference to dead statement");
3136 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3137 error_found = true;
3140 else
3141 /* No CFG available?! */
3142 gcc_unreachable ();
3144 for (e = callees; e; e = e->next_callee)
3146 if (!e->aux)
3148 error ("edge %s->%s has no corresponding call_stmt",
3149 identifier_to_locale (e->caller->name ()),
3150 identifier_to_locale (e->callee->name ()));
3151 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3152 error_found = true;
3154 e->aux = 0;
3156 for (e = indirect_calls; e; e = e->next_callee)
3158 if (!e->aux && !e->speculative)
3160 error ("an indirect edge from %s has no corresponding call_stmt",
3161 identifier_to_locale (e->caller->name ()));
3162 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3163 error_found = true;
3165 e->aux = 0;
3168 if (error_found)
3170 dump (stderr);
3171 internal_error ("verify_cgraph_node failed");
3173 timevar_pop (TV_CGRAPH_VERIFY);
3176 /* Verify whole cgraph structure. */
3177 DEBUG_FUNCTION void
3178 cgraph_node::verify_cgraph_nodes (void)
3180 cgraph_node *node;
3182 if (seen_error ())
3183 return;
3185 FOR_EACH_FUNCTION (node)
3186 node->verify ();
3189 /* Walk the alias chain to return the function cgraph_node is alias of.
3190 Walk through thunks, too.
3191 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3193 cgraph_node *
3194 cgraph_node::function_symbol (enum availability *availability)
3196 cgraph_node *node = ultimate_alias_target (availability);
3198 while (node->thunk.thunk_p)
3200 node = node->callees->callee;
3201 if (availability)
3203 enum availability a;
3204 a = node->get_availability ();
3205 if (a < *availability)
3206 *availability = a;
3208 node = node->ultimate_alias_target (availability);
3210 return node;
3213 /* Walk the alias chain to return the function cgraph_node is alias of.
3214 Walk through non virtual thunks, too. Thus we return either a function
3215 or a virtual thunk node.
3216 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3218 cgraph_node *
3219 cgraph_node::function_or_virtual_thunk_symbol
3220 (enum availability *availability)
3222 cgraph_node *node = ultimate_alias_target (availability);
3224 while (node->thunk.thunk_p && !node->thunk.virtual_offset_p)
3226 node = node->callees->callee;
3227 if (availability)
3229 enum availability a;
3230 a = node->get_availability ();
3231 if (a < *availability)
3232 *availability = a;
3234 node = node->ultimate_alias_target (availability);
3236 return node;
3239 /* When doing LTO, read cgraph_node's body from disk if it is not already
3240 present. */
3242 bool
3243 cgraph_node::get_untransformed_body (void)
3245 lto_file_decl_data *file_data;
3246 const char *data, *name;
3247 size_t len;
3248 tree decl = this->decl;
3250 if (DECL_RESULT (decl))
3251 return false;
3253 gcc_assert (in_lto_p);
3255 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3257 file_data = lto_file_data;
3258 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3260 /* We may have renamed the declaration, e.g., a static function. */
3261 name = lto_get_decl_name_mapping (file_data, name);
3263 data = lto_get_section_data (file_data, LTO_section_function_body,
3264 name, &len);
3265 if (!data)
3266 fatal_error (input_location, "%s: section %s is missing",
3267 file_data->file_name,
3268 name);
3270 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
3272 lto_input_function_body (file_data, this, data);
3273 lto_stats.num_function_bodies++;
3274 lto_free_section_data (file_data, LTO_section_function_body, name,
3275 data, len);
3276 lto_free_function_in_decl_state_for_node (this);
3277 /* Keep lto file data so ipa-inline-analysis knows about cross module
3278 inlining. */
3280 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3282 return true;
3285 /* Prepare function body. When doing LTO, read cgraph_node's body from disk
3286 if it is not already present. When some IPA transformations are scheduled,
3287 apply them. */
3289 bool
3290 cgraph_node::get_body (void)
3292 bool updated;
3294 updated = get_untransformed_body ();
3296 /* Getting transformed body makes no sense for inline clones;
3297 we should never use this on real clones becuase they are materialized
3298 early.
3299 TODO: Materializing clones here will likely lead to smaller LTRANS
3300 footprint. */
3301 gcc_assert (!global.inlined_to && !clone_of);
3302 if (ipa_transforms_to_apply.exists ())
3304 opt_pass *saved_current_pass = current_pass;
3305 FILE *saved_dump_file = dump_file;
3306 int saved_dump_flags = dump_flags;
3308 push_cfun (DECL_STRUCT_FUNCTION (decl));
3309 execute_all_ipa_transforms ();
3310 cgraph_edge::rebuild_edges ();
3311 free_dominance_info (CDI_DOMINATORS);
3312 free_dominance_info (CDI_POST_DOMINATORS);
3313 pop_cfun ();
3314 updated = true;
3316 current_pass = saved_current_pass;
3317 dump_file = saved_dump_file;
3318 dump_flags = saved_dump_flags;
3320 return updated;
3323 /* Return the DECL_STRUCT_FUNCTION of the function. */
3325 struct function *
3326 cgraph_node::get_fun (void)
3328 cgraph_node *node = this;
3329 struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
3331 while (!fun && node->clone_of)
3333 node = node->clone_of;
3334 fun = DECL_STRUCT_FUNCTION (node->decl);
3337 return fun;
3340 /* Verify if the type of the argument matches that of the function
3341 declaration. If we cannot verify this or there is a mismatch,
3342 return false. */
3344 static bool
3345 gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
3347 tree parms, p;
3348 unsigned int i, nargs;
3350 /* Calls to internal functions always match their signature. */
3351 if (gimple_call_internal_p (stmt))
3352 return true;
3354 nargs = gimple_call_num_args (stmt);
3356 /* Get argument types for verification. */
3357 if (fndecl)
3358 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3359 else
3360 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3362 /* Verify if the type of the argument matches that of the function
3363 declaration. If we cannot verify this or there is a mismatch,
3364 return false. */
3365 if (fndecl && DECL_ARGUMENTS (fndecl))
3367 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3368 i < nargs;
3369 i++, p = DECL_CHAIN (p))
3371 tree arg;
3372 /* We cannot distinguish a varargs function from the case
3373 of excess parameters, still deferring the inlining decision
3374 to the callee is possible. */
3375 if (!p)
3376 break;
3377 arg = gimple_call_arg (stmt, i);
3378 if (p == error_mark_node
3379 || DECL_ARG_TYPE (p) == error_mark_node
3380 || arg == error_mark_node
3381 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3382 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3383 return false;
3385 if (args_count_match && p)
3386 return false;
3388 else if (parms)
3390 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3392 tree arg;
3393 /* If this is a varargs function defer inlining decision
3394 to callee. */
3395 if (!p)
3396 break;
3397 arg = gimple_call_arg (stmt, i);
3398 if (TREE_VALUE (p) == error_mark_node
3399 || arg == error_mark_node
3400 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3401 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3402 && !fold_convertible_p (TREE_VALUE (p), arg)))
3403 return false;
3406 else
3408 if (nargs != 0)
3409 return false;
3411 return true;
3414 /* Verify if the type of the argument and lhs of CALL_STMT matches
3415 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3416 true, the arg count needs to be the same.
3417 If we cannot verify this or there is a mismatch, return false. */
3419 bool
3420 gimple_check_call_matching_types (gimple call_stmt, tree callee,
3421 bool args_count_match)
3423 tree lhs;
3425 if ((DECL_RESULT (callee)
3426 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3427 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3428 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3429 TREE_TYPE (lhs))
3430 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3431 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3432 return false;
3433 return true;
3436 /* Reset all state within cgraph.c so that we can rerun the compiler
3437 within the same process. For use by toplev::finalize. */
3439 void
3440 cgraph_c_finalize (void)
3442 symtab = NULL;
3444 x_cgraph_nodes_queue = NULL;
3446 cgraph_fnver_htab = NULL;
3447 version_info_node = NULL;
3450 /* A wroker for call_for_symbol_and_aliases. */
3452 bool
3453 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
3454 void *),
3455 void *data,
3456 bool include_overwritable)
3458 ipa_ref *ref;
3459 FOR_EACH_ALIAS (this, ref)
3461 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
3462 if (include_overwritable
3463 || alias->get_availability () > AVAIL_INTERPOSABLE)
3464 if (alias->call_for_symbol_and_aliases (callback, data,
3465 include_overwritable))
3466 return true;
3468 return false;
3471 /* Return true if NODE has thunk. */
3473 bool
3474 cgraph_node::has_thunk_p (cgraph_node *node, void *)
3476 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3477 if (e->caller->thunk.thunk_p)
3478 return true;
3479 return false;
3482 #include "gt-cgraph.h"