xref: /netbsd-src/external/gpl3/gcc/dist/gcc/cgraph.cc (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 /* Callgraph handling code.
2    Copyright (C) 2003-2022 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4 
5 This file is part of GCC.
6 
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.
11 
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.
16 
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/>.  */
20 
21 /*  This file contains basic routines manipulating call graph
22 
23     The call-graph is a data structure designed for inter-procedural
24     optimization.  It represents a multi-graph where nodes are functions
25     (symbols within symbol table) and edges are call sites. */
26 
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "backend.h"
31 #include "target.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "gimple.h"
35 #include "predict.h"
36 #include "alloc-pool.h"
37 #include "gimple-ssa.h"
38 #include "cgraph.h"
39 #include "lto-streamer.h"
40 #include "fold-const.h"
41 #include "varasm.h"
42 #include "calls.h"
43 #include "print-tree.h"
44 #include "langhooks.h"
45 #include "intl.h"
46 #include "tree-eh.h"
47 #include "gimple-iterator.h"
48 #include "tree-cfg.h"
49 #include "tree-ssa.h"
50 #include "value-prof.h"
51 #include "ipa-utils.h"
52 #include "symbol-summary.h"
53 #include "tree-vrp.h"
54 #include "ipa-prop.h"
55 #include "ipa-fnsummary.h"
56 #include "cfgloop.h"
57 #include "gimple-pretty-print.h"
58 #include "tree-dfa.h"
59 #include "profile.h"
60 #include "context.h"
61 #include "gimplify.h"
62 #include "stringpool.h"
63 #include "attribs.h"
64 #include "selftest.h"
65 #include "tree-into-ssa.h"
66 #include "ipa-inline.h"
67 #include "tree-nested.h"
68 #include "symtab-thunks.h"
69 #include "symtab-clones.h"
70 
71 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this.  */
72 #include "tree-pass.h"
73 
74 /* Queue of cgraph nodes scheduled to be lowered.  */
75 symtab_node *x_cgraph_nodes_queue;
76 #define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
77 
78 /* Symbol table global context.  */
79 symbol_table *symtab;
80 
81 /* List of hooks triggered on cgraph_edge events.  */
82 struct cgraph_edge_hook_list {
83   cgraph_edge_hook hook;
84   void *data;
85   struct cgraph_edge_hook_list *next;
86 };
87 
88 /* List of hooks triggered on cgraph_node events.  */
89 struct cgraph_node_hook_list {
90   cgraph_node_hook hook;
91   void *data;
92   struct cgraph_node_hook_list *next;
93 };
94 
95 /* List of hooks triggered on events involving two cgraph_edges.  */
96 struct cgraph_2edge_hook_list {
97   cgraph_2edge_hook hook;
98   void *data;
99   struct cgraph_2edge_hook_list *next;
100 };
101 
102 /* List of hooks triggered on events involving two cgraph_nodes.  */
103 struct cgraph_2node_hook_list {
104   cgraph_2node_hook hook;
105   void *data;
106   struct cgraph_2node_hook_list *next;
107 };
108 
109 /* Hash descriptor for cgraph_function_version_info.  */
110 
111 struct function_version_hasher : ggc_ptr_hash<cgraph_function_version_info>
112 {
113   static hashval_t hash (cgraph_function_version_info *);
114   static bool equal (cgraph_function_version_info *,
115 		     cgraph_function_version_info *);
116 };
117 
118 /* Map a cgraph_node to cgraph_function_version_info using this htab.
119    The cgraph_function_version_info has a THIS_NODE field that is the
120    corresponding cgraph_node..  */
121 
122 static GTY(()) hash_table<function_version_hasher> *cgraph_fnver_htab = NULL;
123 
124 /* Hash function for cgraph_fnver_htab.  */
125 hashval_t
hash(cgraph_function_version_info * ptr)126 function_version_hasher::hash (cgraph_function_version_info *ptr)
127 {
128   int uid = ptr->this_node->get_uid ();
129   return (hashval_t)(uid);
130 }
131 
132 /* eq function for cgraph_fnver_htab.  */
133 bool
equal(cgraph_function_version_info * n1,cgraph_function_version_info * n2)134 function_version_hasher::equal (cgraph_function_version_info *n1,
135 			       	cgraph_function_version_info *n2)
136 {
137   return n1->this_node->get_uid () == n2->this_node->get_uid ();
138 }
139 
140 /* Mark as GC root all allocated nodes.  */
141 static GTY(()) struct cgraph_function_version_info *
142   version_info_node = NULL;
143 
144 /* Return true if NODE's address can be compared.  */
145 
146 bool
address_can_be_compared_p()147 symtab_node::address_can_be_compared_p ()
148 {
149   /* Address of virtual tables and functions is never compared.  */
150   if (DECL_VIRTUAL_P (decl))
151     return false;
152   /* Address of C++ cdtors is never compared.  */
153   if (is_a <cgraph_node *> (this)
154       && (DECL_CXX_CONSTRUCTOR_P (decl)
155 	  || DECL_CXX_DESTRUCTOR_P (decl)))
156     return false;
157   /* Constant pool symbols addresses are never compared.
158      flag_merge_constants permits us to assume the same on readonly vars.  */
159   if (is_a <varpool_node *> (this)
160       && (DECL_IN_CONSTANT_POOL (decl)
161 	  || (flag_merge_constants >= 2
162 	      && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
163     return false;
164   return true;
165 }
166 
167 /* Get the cgraph_function_version_info node corresponding to node.  */
168 cgraph_function_version_info *
function_version(void)169 cgraph_node::function_version (void)
170 {
171   cgraph_function_version_info key;
172   key.this_node = this;
173 
174   if (cgraph_fnver_htab == NULL)
175     return NULL;
176 
177   return cgraph_fnver_htab->find (&key);
178 }
179 
180 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
181    corresponding to cgraph_node NODE.  */
182 cgraph_function_version_info *
insert_new_function_version(void)183 cgraph_node::insert_new_function_version (void)
184 {
185   version_info_node = NULL;
186   version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
187   version_info_node->this_node = this;
188 
189   if (cgraph_fnver_htab == NULL)
190     cgraph_fnver_htab = hash_table<function_version_hasher>::create_ggc (2);
191 
192   *cgraph_fnver_htab->find_slot (version_info_node, INSERT)
193     = version_info_node;
194   return version_info_node;
195 }
196 
197 /* Remove the cgraph_function_version_info node given by DECL_V.  */
198 static void
delete_function_version(cgraph_function_version_info * decl_v)199 delete_function_version (cgraph_function_version_info *decl_v)
200 {
201   if (decl_v == NULL)
202     return;
203 
204   if (version_info_node == decl_v)
205     version_info_node = NULL;
206 
207   if (decl_v->prev != NULL)
208     decl_v->prev->next = decl_v->next;
209 
210   if (decl_v->next != NULL)
211     decl_v->next->prev = decl_v->prev;
212 
213   if (cgraph_fnver_htab != NULL)
214     cgraph_fnver_htab->remove_elt (decl_v);
215 }
216 
217 /* Remove the cgraph_function_version_info and cgraph_node for DECL.  This
218    DECL is a duplicate declaration.  */
219 void
delete_function_version_by_decl(tree decl)220 cgraph_node::delete_function_version_by_decl (tree decl)
221 {
222   cgraph_node *decl_node = cgraph_node::get (decl);
223 
224   if (decl_node == NULL)
225     return;
226 
227   delete_function_version (decl_node->function_version ());
228 
229   decl_node->remove ();
230 }
231 
232 /* Record that DECL1 and DECL2 are semantically identical function
233    versions.  */
234 void
record_function_versions(tree decl1,tree decl2)235 cgraph_node::record_function_versions (tree decl1, tree decl2)
236 {
237   cgraph_node *decl1_node = cgraph_node::get_create (decl1);
238   cgraph_node *decl2_node = cgraph_node::get_create (decl2);
239   cgraph_function_version_info *decl1_v = NULL;
240   cgraph_function_version_info *decl2_v = NULL;
241   cgraph_function_version_info *before;
242   cgraph_function_version_info *after;
243 
244   gcc_assert (decl1_node != NULL && decl2_node != NULL);
245   decl1_v = decl1_node->function_version ();
246   decl2_v = decl2_node->function_version ();
247 
248   if (decl1_v != NULL && decl2_v != NULL)
249     return;
250 
251   if (decl1_v == NULL)
252     decl1_v = decl1_node->insert_new_function_version ();
253 
254   if (decl2_v == NULL)
255     decl2_v = decl2_node->insert_new_function_version ();
256 
257   /* Chain decl2_v and decl1_v.  All semantically identical versions
258      will be chained together.  */
259 
260   before = decl1_v;
261   after = decl2_v;
262 
263   while (before->next != NULL)
264     before = before->next;
265 
266   while (after->prev != NULL)
267     after= after->prev;
268 
269   before->next = after;
270   after->prev = before;
271 }
272 
273 /* Initialize callgraph dump file.  */
274 
275 void
initialize(void)276 symbol_table::initialize (void)
277 {
278   if (!dump_file)
279     dump_file = dump_begin (TDI_cgraph, NULL);
280 
281   if (!ipa_clones_dump_file)
282     ipa_clones_dump_file = dump_begin (TDI_clones, NULL);
283 }
284 
285 /* Allocate new callgraph node and insert it into basic data structures.  */
286 
287 cgraph_node *
create_empty(void)288 symbol_table::create_empty (void)
289 {
290   cgraph_count++;
291   return new (ggc_alloc<cgraph_node> ()) cgraph_node (cgraph_max_uid++);
292 }
293 
294 /* Register HOOK to be called with DATA on each removed edge.  */
295 cgraph_edge_hook_list *
add_edge_removal_hook(cgraph_edge_hook hook,void * data)296 symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
297 {
298   cgraph_edge_hook_list *entry;
299   cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
300 
301   entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
302   entry->hook = hook;
303   entry->data = data;
304   entry->next = NULL;
305   while (*ptr)
306     ptr = &(*ptr)->next;
307   *ptr = entry;
308   return entry;
309 }
310 
311 /* Remove ENTRY from the list of hooks called on removing edges.  */
312 void
remove_edge_removal_hook(cgraph_edge_hook_list * entry)313 symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
314 {
315   cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
316 
317   while (*ptr != entry)
318     ptr = &(*ptr)->next;
319   *ptr = entry->next;
320   free (entry);
321 }
322 
323 /* Call all edge removal hooks.  */
324 void
call_edge_removal_hooks(cgraph_edge * e)325 symbol_table::call_edge_removal_hooks (cgraph_edge *e)
326 {
327   cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
328   while (entry)
329   {
330     entry->hook (e, entry->data);
331     entry = entry->next;
332   }
333 }
334 
335 /* Register HOOK to be called with DATA on each removed node.  */
336 cgraph_node_hook_list *
add_cgraph_removal_hook(cgraph_node_hook hook,void * data)337 symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
338 {
339   cgraph_node_hook_list *entry;
340   cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
341 
342   entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
343   entry->hook = hook;
344   entry->data = data;
345   entry->next = NULL;
346   while (*ptr)
347     ptr = &(*ptr)->next;
348   *ptr = entry;
349   return entry;
350 }
351 
352 /* Remove ENTRY from the list of hooks called on removing nodes.  */
353 void
remove_cgraph_removal_hook(cgraph_node_hook_list * entry)354 symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
355 {
356   cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
357 
358   while (*ptr != entry)
359     ptr = &(*ptr)->next;
360   *ptr = entry->next;
361   free (entry);
362 }
363 
364 /* Call all node removal hooks.  */
365 void
call_cgraph_removal_hooks(cgraph_node * node)366 symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
367 {
368   cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
369   while (entry)
370   {
371     entry->hook (node, entry->data);
372     entry = entry->next;
373   }
374 }
375 
376 /* Call all node removal hooks.  */
377 void
call_cgraph_insertion_hooks(cgraph_node * node)378 symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
379 {
380   cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
381   while (entry)
382   {
383     entry->hook (node, entry->data);
384     entry = entry->next;
385   }
386 }
387 
388 
389 /* Register HOOK to be called with DATA on each inserted node.  */
390 cgraph_node_hook_list *
add_cgraph_insertion_hook(cgraph_node_hook hook,void * data)391 symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
392 {
393   cgraph_node_hook_list *entry;
394   cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
395 
396   entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
397   entry->hook = hook;
398   entry->data = data;
399   entry->next = NULL;
400   while (*ptr)
401     ptr = &(*ptr)->next;
402   *ptr = entry;
403   return entry;
404 }
405 
406 /* Remove ENTRY from the list of hooks called on inserted nodes.  */
407 void
remove_cgraph_insertion_hook(cgraph_node_hook_list * entry)408 symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
409 {
410   cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
411 
412   while (*ptr != entry)
413     ptr = &(*ptr)->next;
414   *ptr = entry->next;
415   free (entry);
416 }
417 
418 /* Register HOOK to be called with DATA on each duplicated edge.  */
419 cgraph_2edge_hook_list *
add_edge_duplication_hook(cgraph_2edge_hook hook,void * data)420 symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
421 {
422   cgraph_2edge_hook_list *entry;
423   cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
424 
425   entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
426   entry->hook = hook;
427   entry->data = data;
428   entry->next = NULL;
429   while (*ptr)
430     ptr = &(*ptr)->next;
431   *ptr = entry;
432   return entry;
433 }
434 
435 /* Remove ENTRY from the list of hooks called on duplicating edges.  */
436 void
remove_edge_duplication_hook(cgraph_2edge_hook_list * entry)437 symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
438 {
439   cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
440 
441   while (*ptr != entry)
442     ptr = &(*ptr)->next;
443   *ptr = entry->next;
444   free (entry);
445 }
446 
447 /* Call all edge duplication hooks.  */
448 void
call_edge_duplication_hooks(cgraph_edge * cs1,cgraph_edge * cs2)449 symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
450 {
451   cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
452   while (entry)
453   {
454     entry->hook (cs1, cs2, entry->data);
455     entry = entry->next;
456   }
457 }
458 
459 /* Register HOOK to be called with DATA on each duplicated node.  */
460 cgraph_2node_hook_list *
add_cgraph_duplication_hook(cgraph_2node_hook hook,void * data)461 symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
462 {
463   cgraph_2node_hook_list *entry;
464   cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
465 
466   entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
467   entry->hook = hook;
468   entry->data = data;
469   entry->next = NULL;
470   while (*ptr)
471     ptr = &(*ptr)->next;
472   *ptr = entry;
473   return entry;
474 }
475 
476 /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
477 void
remove_cgraph_duplication_hook(cgraph_2node_hook_list * entry)478 symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
479 {
480   cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
481 
482   while (*ptr != entry)
483     ptr = &(*ptr)->next;
484   *ptr = entry->next;
485   free (entry);
486 }
487 
488 /* Call all node duplication hooks.  */
489 void
call_cgraph_duplication_hooks(cgraph_node * node,cgraph_node * node2)490 symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
491 					     cgraph_node *node2)
492 {
493   cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
494   while (entry)
495   {
496     entry->hook (node, node2, entry->data);
497     entry = entry->next;
498   }
499 }
500 
501 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
502 
503 cgraph_node *
create(tree decl)504 cgraph_node::create (tree decl)
505 {
506   cgraph_node *node = symtab->create_empty ();
507   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
508 
509   node->decl = decl;
510   node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
511 
512   if ((flag_openacc || flag_openmp)
513       && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
514     {
515       node->offloadable = 1;
516       if (ENABLE_OFFLOADING)
517 	g->have_offload = true;
518     }
519 
520   if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
521     node->ifunc_resolver = true;
522 
523   node->register_symbol ();
524   maybe_record_nested_function (node);
525 
526   return node;
527 }
528 
529 /* Try to find a call graph node for declaration DECL and if it does not exist
530    or if it corresponds to an inline clone, create a new one.  */
531 
532 cgraph_node *
get_create(tree decl)533 cgraph_node::get_create (tree decl)
534 {
535   cgraph_node *first_clone = cgraph_node::get (decl);
536 
537   if (first_clone && !first_clone->inlined_to)
538     return first_clone;
539 
540   cgraph_node *node = cgraph_node::create (decl);
541   if (first_clone)
542     {
543       first_clone->clone_of = node;
544       node->clones = first_clone;
545       node->order = first_clone->order;
546       symtab->symtab_prevail_in_asm_name_hash (node);
547       node->decl->decl_with_vis.symtab_node = node;
548       if (dump_file)
549 	fprintf (dump_file, "Introduced new external node "
550 		 "(%s) and turned into root of the clone tree.\n",
551 		 node->dump_name ());
552     }
553   else if (dump_file)
554     fprintf (dump_file, "Introduced new external node "
555 	     "(%s).\n", node->dump_name ());
556   return node;
557 }
558 
559 /* Mark ALIAS as an alias to DECL.  DECL_NODE is cgraph node representing
560    the function body is associated with
561    (not necessarily cgraph_node (DECL)).  */
562 
563 cgraph_node *
create_alias(tree alias,tree target)564 cgraph_node::create_alias (tree alias, tree target)
565 {
566   cgraph_node *alias_node;
567 
568   gcc_assert (TREE_CODE (target) == FUNCTION_DECL
569 	      || TREE_CODE (target) == IDENTIFIER_NODE);
570   gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
571   alias_node = cgraph_node::get_create (alias);
572   gcc_assert (!alias_node->definition);
573   alias_node->alias_target = target;
574   alias_node->definition = true;
575   alias_node->alias = true;
576   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
577     alias_node->transparent_alias = alias_node->weakref = true;
578   if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias)))
579     alias_node->ifunc_resolver = true;
580   return alias_node;
581 }
582 
583 /* Attempt to mark ALIAS as an alias to DECL.  Return alias node if successful
584    and NULL otherwise.
585    Same body aliases are output whenever the body of DECL is output,
586    and cgraph_node::get (ALIAS) transparently returns
587    cgraph_node::get (DECL).  */
588 
589 cgraph_node *
create_same_body_alias(tree alias,tree decl)590 cgraph_node::create_same_body_alias (tree alias, tree decl)
591 {
592   cgraph_node *n;
593 
594   /* If aliases aren't supported by the assembler, fail.  */
595   if (!TARGET_SUPPORTS_ALIASES)
596     return NULL;
597 
598   /* Langhooks can create same body aliases of symbols not defined.
599      Those are useless. Drop them on the floor.  */
600   if (symtab->global_info_ready)
601     return NULL;
602 
603   n = cgraph_node::create_alias (alias, decl);
604   n->cpp_implicit_alias = true;
605   if (symtab->cpp_implicit_aliases_done)
606     n->resolve_alias (cgraph_node::get (decl));
607   return n;
608 }
609 
610 /* Add thunk alias into callgraph.  The alias declaration is ALIAS and it
611    aliases DECL with an adjustments made into the first parameter.
612    See comments in struct cgraph_thunk_info for detail on the parameters.  */
613 
614 cgraph_node *
create_thunk(tree alias,tree,bool this_adjusting,HOST_WIDE_INT fixed_offset,HOST_WIDE_INT virtual_value,HOST_WIDE_INT indirect_offset,tree virtual_offset,tree real_alias)615 cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
616 			   HOST_WIDE_INT fixed_offset,
617 			   HOST_WIDE_INT virtual_value,
618 			   HOST_WIDE_INT indirect_offset,
619 			   tree virtual_offset,
620 			   tree real_alias)
621 {
622   cgraph_node *node;
623 
624   node = cgraph_node::get (alias);
625   if (node)
626     node->reset ();
627   else
628     node = cgraph_node::create (alias);
629 
630   /* Make sure that if VIRTUAL_OFFSET is in sync with VIRTUAL_VALUE.  */
631   gcc_checking_assert (virtual_offset
632 		       ? virtual_value == wi::to_wide (virtual_offset)
633 		       : virtual_value == 0);
634 
635   node->thunk = true;
636   node->definition = true;
637 
638   thunk_info *i;
639   thunk_info local_info;
640   if (symtab->state < CONSTRUCTION)
641     i = &local_info;
642   else
643     i = thunk_info::get_create (node);
644   i->fixed_offset = fixed_offset;
645   i->virtual_value = virtual_value;
646   i->indirect_offset = indirect_offset;
647   i->alias = real_alias;
648   i->this_adjusting = this_adjusting;
649   i->virtual_offset_p = virtual_offset != NULL;
650   if (symtab->state < CONSTRUCTION)
651     i->register_early (node);
652 
653   return node;
654 }
655 
656 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
657    Return NULL if there's no such node.  */
658 
659 cgraph_node *
get_for_asmname(tree asmname)660 cgraph_node::get_for_asmname (tree asmname)
661 {
662   /* We do not want to look at inline clones.  */
663   for (symtab_node *node = symtab_node::get_for_asmname (asmname);
664        node;
665        node = node->next_sharing_asm_name)
666     {
667       cgraph_node *cn = dyn_cast <cgraph_node *> (node);
668       if (cn && !cn->inlined_to)
669 	return cn;
670     }
671   return NULL;
672 }
673 
674 /* Returns a hash value for X (which really is a cgraph_edge).  */
675 
676 hashval_t
hash(cgraph_edge * e)677 cgraph_edge_hasher::hash (cgraph_edge *e)
678 {
679   /* This is a really poor hash function, but it is what htab_hash_pointer
680      uses.  */
681   return (hashval_t) ((intptr_t)e->call_stmt >> 3);
682 }
683 
684 /* Returns a hash value for X (which really is a cgraph_edge).  */
685 
686 hashval_t
hash(gimple * call_stmt)687 cgraph_edge_hasher::hash (gimple *call_stmt)
688 {
689   /* This is a really poor hash function, but it is what htab_hash_pointer
690      uses.  */
691   return (hashval_t) ((intptr_t)call_stmt >> 3);
692 }
693 
694 /* Return nonzero if the call_stmt of cgraph_edge X is stmt *Y.  */
695 
696 inline bool
equal(cgraph_edge * x,gimple * y)697 cgraph_edge_hasher::equal (cgraph_edge *x, gimple *y)
698 {
699   return x->call_stmt == y;
700 }
701 
702 /* Add call graph edge E to call site hash of its caller.  */
703 
704 static inline void
cgraph_update_edge_in_call_site_hash(cgraph_edge * e)705 cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
706 {
707   gimple *call = e->call_stmt;
708   *e->caller->call_site_hash->find_slot_with_hash
709       (call, cgraph_edge_hasher::hash (call), INSERT) = e;
710 }
711 
712 /* Add call graph edge E to call site hash of its caller.  */
713 
714 static inline void
cgraph_add_edge_to_call_site_hash(cgraph_edge * e)715 cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
716 {
717   /* There are two speculative edges for every statement (one direct,
718      one indirect); always hash the direct one.  */
719   if (e->speculative && e->indirect_unknown_callee)
720     return;
721   cgraph_edge **slot = e->caller->call_site_hash->find_slot_with_hash
722       (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt), INSERT);
723   if (*slot)
724     {
725       gcc_assert (((cgraph_edge *)*slot)->speculative);
726       if (e->callee && (!e->prev_callee
727 			|| !e->prev_callee->speculative
728 			|| e->prev_callee->call_stmt != e->call_stmt))
729 	*slot = e;
730       return;
731     }
732   gcc_assert (!*slot || e->speculative);
733   *slot = e;
734 }
735 
736 /* Return the callgraph edge representing the GIMPLE_CALL statement
737    CALL_STMT.  */
738 
739 cgraph_edge *
get_edge(gimple * call_stmt)740 cgraph_node::get_edge (gimple *call_stmt)
741 {
742   cgraph_edge *e, *e2;
743   int n = 0;
744 
745   if (call_site_hash)
746     return call_site_hash->find_with_hash
747 	(call_stmt, cgraph_edge_hasher::hash (call_stmt));
748 
749   /* This loop may turn out to be performance problem.  In such case adding
750      hashtables into call nodes with very many edges is probably best
751      solution.  It is not good idea to add pointer into CALL_EXPR itself
752      because we want to make possible having multiple cgraph nodes representing
753      different clones of the same body before the body is actually cloned.  */
754   for (e = callees; e; e = e->next_callee)
755     {
756       if (e->call_stmt == call_stmt)
757 	break;
758       n++;
759     }
760 
761   if (!e)
762     for (e = indirect_calls; e; e = e->next_callee)
763       {
764 	if (e->call_stmt == call_stmt)
765 	  break;
766 	n++;
767       }
768 
769   if (n > 100)
770     {
771       call_site_hash = hash_table<cgraph_edge_hasher>::create_ggc (120);
772       for (e2 = callees; e2; e2 = e2->next_callee)
773 	cgraph_add_edge_to_call_site_hash (e2);
774       for (e2 = indirect_calls; e2; e2 = e2->next_callee)
775 	cgraph_add_edge_to_call_site_hash (e2);
776     }
777 
778   return e;
779 }
780 
781 
782 /* Change field call_stmt of edge E to NEW_STMT.  If UPDATE_SPECULATIVE and E
783    is any component of speculative edge, then update all components.
784    Speculations can be resolved in the process and EDGE can be removed and
785    deallocated.  Return the edge that now represents the call.  */
786 
787 cgraph_edge *
set_call_stmt(cgraph_edge * e,gcall * new_stmt,bool update_speculative)788 cgraph_edge::set_call_stmt (cgraph_edge *e, gcall *new_stmt,
789 			    bool update_speculative)
790 {
791   tree decl;
792 
793   cgraph_node *new_direct_callee = NULL;
794   if ((e->indirect_unknown_callee || e->speculative)
795       && (decl = gimple_call_fndecl (new_stmt)))
796     {
797       /* Constant propagation and especially inlining can turn an indirect call
798 	 into a direct one.  */
799       new_direct_callee = cgraph_node::get (decl);
800       gcc_checking_assert (new_direct_callee);
801     }
802 
803   /* Speculative edges has three component, update all of them
804      when asked to.  */
805   if (update_speculative && e->speculative
806       /* If we are about to resolve the speculation by calling make_direct
807 	 below, do not bother going over all the speculative edges now.  */
808       && !new_direct_callee)
809     {
810       cgraph_edge *direct, *indirect, *next;
811       ipa_ref *ref;
812       bool e_indirect = e->indirect_unknown_callee;
813       int n = 0;
814 
815       direct = e->first_speculative_call_target ();
816       indirect = e->speculative_call_indirect_edge ();
817 
818       gcall *old_stmt = direct->call_stmt;
819       for (cgraph_edge *d = direct; d; d = next)
820 	{
821 	  next = d->next_speculative_call_target ();
822 	  cgraph_edge *d2 = set_call_stmt (d, new_stmt, false);
823 	  gcc_assert (d2 == d);
824 	  n++;
825 	}
826       gcc_checking_assert (indirect->num_speculative_call_targets_p () == n);
827       for (unsigned int i = 0; e->caller->iterate_reference (i, ref); i++)
828 	if (ref->speculative && ref->stmt == old_stmt)
829 	  {
830 	    ref->stmt = new_stmt;
831 	    n--;
832 	  }
833 
834       indirect = set_call_stmt (indirect, new_stmt, false);
835       return e_indirect ? indirect : direct;
836     }
837 
838   if (new_direct_callee)
839     e = make_direct (e, new_direct_callee);
840 
841   /* Only direct speculative edges go to call_site_hash.  */
842   if (e->caller->call_site_hash
843       && (!e->speculative || !e->indirect_unknown_callee)
844       /* It is possible that edge was previously speculative.  In this case
845 	 we have different value in call stmt hash which needs preserving.  */
846       && e->caller->get_edge (e->call_stmt) == e)
847     e->caller->call_site_hash->remove_elt_with_hash
848       (e->call_stmt, cgraph_edge_hasher::hash (e->call_stmt));
849 
850   e->call_stmt = new_stmt;
851 
852   function *fun = DECL_STRUCT_FUNCTION (e->caller->decl);
853   e->can_throw_external = stmt_can_throw_external (fun, new_stmt);
854   /* Update call stite hash.  For speculative calls we only record the first
855      direct edge.  */
856   if (e->caller->call_site_hash
857       && (!e->speculative
858 	  || (e->callee
859 	      && (!e->prev_callee || !e->prev_callee->speculative
860 		  || e->prev_callee->call_stmt != e->call_stmt))
861 	  || (e->speculative && !e->callee)))
862     cgraph_add_edge_to_call_site_hash (e);
863   return e;
864 }
865 
866 /* Allocate a cgraph_edge structure and fill it with data according to the
867    parameters of which only CALLEE can be NULL (when creating an indirect call
868    edge).  CLONING_P should be set if properties that are copied from an
869    original edge should not be calculated.  */
870 
871 cgraph_edge *
create_edge(cgraph_node * caller,cgraph_node * callee,gcall * call_stmt,profile_count count,bool indir_unknown_callee,bool cloning_p)872 symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
873 			   gcall *call_stmt, profile_count count,
874 			   bool indir_unknown_callee, bool cloning_p)
875 {
876   cgraph_edge *edge;
877 
878   /* LTO does not actually have access to the call_stmt since these
879      have not been loaded yet.  */
880   if (call_stmt)
881     {
882       /* This is a rather expensive check possibly triggering
883 	 construction of call stmt hashtable.  */
884       cgraph_edge *e;
885       gcc_checking_assert (!(e = caller->get_edge (call_stmt))
886 			   || e->speculative);
887 
888       gcc_assert (is_gimple_call (call_stmt));
889     }
890 
891   edge = ggc_alloc<cgraph_edge> ();
892   edge->m_summary_id = -1;
893   edges_count++;
894 
895   gcc_assert (++edges_max_uid != 0);
896   edge->m_uid = edges_max_uid;
897   edge->aux = NULL;
898   edge->caller = caller;
899   edge->callee = callee;
900   edge->prev_caller = NULL;
901   edge->next_caller = NULL;
902   edge->prev_callee = NULL;
903   edge->next_callee = NULL;
904   edge->lto_stmt_uid = 0;
905   edge->speculative_id = 0;
906 
907   edge->count = count;
908   edge->call_stmt = call_stmt;
909   edge->indirect_info = NULL;
910   edge->indirect_inlining_edge = 0;
911   edge->speculative = false;
912   edge->indirect_unknown_callee = indir_unknown_callee;
913   if (call_stmt && caller->call_site_hash)
914     cgraph_add_edge_to_call_site_hash (edge);
915 
916   if (cloning_p)
917     return edge;
918 
919   edge->can_throw_external
920     = call_stmt ? stmt_can_throw_external (DECL_STRUCT_FUNCTION (caller->decl),
921 					   call_stmt) : false;
922   edge->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
923   edge->call_stmt_cannot_inline_p = false;
924 
925   if (opt_for_fn (edge->caller->decl, flag_devirtualize)
926       && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
927     edge->in_polymorphic_cdtor
928       = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
929 				      caller->decl);
930   else
931     edge->in_polymorphic_cdtor = caller->thunk;
932   if (callee)
933     caller->calls_declare_variant_alt |= callee->declare_variant_alt;
934 
935   if (callee && symtab->state != LTO_STREAMING
936       && edge->callee->comdat_local_p ())
937     edge->caller->calls_comdat_local = true;
938 
939   return edge;
940 }
941 
942 /* Create edge from a given function to CALLEE in the cgraph.  CLONING_P should
943    be set if properties that are copied from an original edge should not be
944    calculated.  */
945 
946 cgraph_edge *
create_edge(cgraph_node * callee,gcall * call_stmt,profile_count count,bool cloning_p)947 cgraph_node::create_edge (cgraph_node *callee,
948 			  gcall *call_stmt, profile_count count, bool cloning_p)
949 {
950   cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
951 					   false, cloning_p);
952 
953   if (!cloning_p)
954     initialize_inline_failed (edge);
955 
956   edge->next_caller = callee->callers;
957   if (callee->callers)
958     callee->callers->prev_caller = edge;
959   edge->next_callee = callees;
960   if (callees)
961     callees->prev_callee = edge;
962   callees = edge;
963   callee->callers = edge;
964 
965   return edge;
966 }
967 
968 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
969 
970 cgraph_indirect_call_info *
cgraph_allocate_init_indirect_info(void)971 cgraph_allocate_init_indirect_info (void)
972 {
973   cgraph_indirect_call_info *ii;
974 
975   ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
976   ii->param_index = -1;
977   return ii;
978 }
979 
980 /* Create an indirect edge with a yet-undetermined callee where the call
981    statement destination is a formal parameter of the caller with index
982    PARAM_INDEX. CLONING_P should be set if properties that are copied from an
983    original edge should not be calculated and indirect_info structure should
984    not be calculated.  */
985 
986 cgraph_edge *
create_indirect_edge(gcall * call_stmt,int ecf_flags,profile_count count,bool cloning_p)987 cgraph_node::create_indirect_edge (gcall *call_stmt, int ecf_flags,
988 				   profile_count count,
989 				   bool cloning_p)
990 {
991   cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt, count, true,
992 					   cloning_p);
993   tree target;
994 
995   if (!cloning_p)
996     initialize_inline_failed (edge);
997 
998   edge->indirect_info = cgraph_allocate_init_indirect_info ();
999   edge->indirect_info->ecf_flags = ecf_flags;
1000   edge->indirect_info->vptr_changed = true;
1001 
1002   /* Record polymorphic call info.  */
1003   if (!cloning_p
1004       && call_stmt
1005       && (target = gimple_call_fn (call_stmt))
1006       && virtual_method_call_p (target))
1007     {
1008       ipa_polymorphic_call_context context (decl, target, call_stmt);
1009 
1010       /* Only record types can have virtual calls.  */
1011       edge->indirect_info->polymorphic = true;
1012       edge->indirect_info->param_index = -1;
1013       edge->indirect_info->otr_token
1014 	 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
1015       edge->indirect_info->otr_type = obj_type_ref_class (target);
1016       gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
1017       edge->indirect_info->context = context;
1018     }
1019 
1020   edge->next_callee = indirect_calls;
1021   if (indirect_calls)
1022     indirect_calls->prev_callee = edge;
1023   indirect_calls = edge;
1024 
1025   return edge;
1026 }
1027 
1028 /* Remove the edge from the list of the callees of the caller.  */
1029 
1030 void
remove_caller(void)1031 cgraph_edge::remove_caller (void)
1032 {
1033   if (prev_callee)
1034     prev_callee->next_callee = next_callee;
1035   if (next_callee)
1036     next_callee->prev_callee = prev_callee;
1037   if (!prev_callee)
1038     {
1039       if (indirect_unknown_callee)
1040 	caller->indirect_calls = next_callee;
1041       else
1042 	caller->callees = next_callee;
1043     }
1044   if (caller->call_site_hash
1045       && this == caller->get_edge (call_stmt))
1046     caller->call_site_hash->remove_elt_with_hash
1047 	(call_stmt, cgraph_edge_hasher::hash (call_stmt));
1048 }
1049 
1050 /* Put the edge onto the free list.  */
1051 
1052 void
free_edge(cgraph_edge * e)1053 symbol_table::free_edge (cgraph_edge *e)
1054 {
1055   edges_count--;
1056   if (e->m_summary_id != -1)
1057     edge_released_summary_ids.safe_push (e->m_summary_id);
1058 
1059   if (e->indirect_info)
1060     ggc_free (e->indirect_info);
1061   ggc_free (e);
1062 }
1063 
1064 /* Remove the edge in the cgraph.  */
1065 
1066 void
remove(cgraph_edge * edge)1067 cgraph_edge::remove (cgraph_edge *edge)
1068 {
1069   /* Call all edge removal hooks.  */
1070   symtab->call_edge_removal_hooks (edge);
1071 
1072   if (!edge->indirect_unknown_callee)
1073     /* Remove from callers list of the callee.  */
1074     edge->remove_callee ();
1075 
1076   /* Remove from callees list of the callers.  */
1077   edge->remove_caller ();
1078 
1079   /* Put the edge onto the free list.  */
1080   symtab->free_edge (edge);
1081 }
1082 
1083 /* Turn edge into speculative call calling N2. Update
1084    the profile so the direct call is taken COUNT times
1085    with FREQUENCY.
1086 
1087    At clone materialization time, the indirect call E will
1088    be expanded as:
1089 
1090    if (call_dest == N2)
1091      n2 ();
1092    else
1093      call call_dest
1094 
1095    At this time the function just creates the direct call,
1096    the reference representing the if conditional and attaches
1097    them all to the original indirect call statement.
1098 
1099    speculative_id is used to link direct calls with their corresponding
1100    IPA_REF_ADDR references when representing speculative calls.
1101 
1102    Return direct edge created.  */
1103 
1104 cgraph_edge *
make_speculative(cgraph_node * n2,profile_count direct_count,unsigned int speculative_id)1105 cgraph_edge::make_speculative (cgraph_node *n2, profile_count direct_count,
1106 			       unsigned int speculative_id)
1107 {
1108   cgraph_node *n = caller;
1109   ipa_ref *ref = NULL;
1110   cgraph_edge *e2;
1111 
1112   if (dump_file)
1113     fprintf (dump_file, "Indirect call -> speculative call %s => %s\n",
1114 	     n->dump_name (), n2->dump_name ());
1115   speculative = true;
1116   e2 = n->create_edge (n2, call_stmt, direct_count);
1117   initialize_inline_failed (e2);
1118   e2->speculative = true;
1119   if (TREE_NOTHROW (n2->decl))
1120     e2->can_throw_external = false;
1121   else
1122     e2->can_throw_external = can_throw_external;
1123   e2->lto_stmt_uid = lto_stmt_uid;
1124   e2->speculative_id = speculative_id;
1125   e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
1126   indirect_info->num_speculative_call_targets++;
1127   count -= e2->count;
1128   symtab->call_edge_duplication_hooks (this, e2);
1129   ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1130   ref->lto_stmt_uid = lto_stmt_uid;
1131   ref->speculative_id = speculative_id;
1132   ref->speculative = speculative;
1133   n2->mark_address_taken ();
1134   return e2;
1135 }
1136 
1137 /* Speculative call consists of an indirect edge and one or more
1138    direct edge+ref pairs.
1139 
1140    Given an edge which is part of speculative call, return the first
1141    direct call edge in the speculative call sequence.  */
1142 
1143 cgraph_edge *
first_speculative_call_target()1144 cgraph_edge::first_speculative_call_target ()
1145 {
1146   cgraph_edge *e = this;
1147 
1148   gcc_checking_assert (e->speculative);
1149   if (e->callee)
1150     {
1151       while (e->prev_callee && e->prev_callee->speculative
1152 	     && e->prev_callee->call_stmt == e->call_stmt
1153 	     && e->prev_callee->lto_stmt_uid == e->lto_stmt_uid)
1154 	e = e->prev_callee;
1155       return e;
1156     }
1157   /* Call stmt site hash always points to the first target of the
1158      speculative call sequence.  */
1159   if (e->call_stmt)
1160     return e->caller->get_edge (e->call_stmt);
1161   for (cgraph_edge *e2 = e->caller->callees; true; e2 = e2->next_callee)
1162     if (e2->speculative
1163 	&& e->call_stmt == e2->call_stmt
1164 	&& e->lto_stmt_uid == e2->lto_stmt_uid)
1165       return e2;
1166 }
1167 
1168 /* We always maintain first direct edge in the call site hash, if one
1169    exists.  E is going to be removed.  See if it is first one and update
1170    hash accordingly.  INDIRECT is the indirect edge of speculative call.
1171    We assume that INDIRECT->num_speculative_call_targets_p () is already
1172    updated for removal of E.  */
1173 static void
update_call_stmt_hash_for_removing_direct_edge(cgraph_edge * e,cgraph_edge * indirect)1174 update_call_stmt_hash_for_removing_direct_edge (cgraph_edge *e,
1175 						cgraph_edge *indirect)
1176 {
1177   if (e->caller->call_site_hash)
1178     {
1179       if (e->caller->get_edge (e->call_stmt) != e)
1180 	;
1181       else if (!indirect->num_speculative_call_targets_p ())
1182 	cgraph_update_edge_in_call_site_hash (indirect);
1183       else
1184 	{
1185 	  gcc_checking_assert (e->next_callee && e->next_callee->speculative
1186 			       && e->next_callee->call_stmt == e->call_stmt);
1187 	  cgraph_update_edge_in_call_site_hash (e->next_callee);
1188 	}
1189     }
1190 }
1191 
1192 /* Speculative call EDGE turned out to be direct call to CALLEE_DECL.  Remove
1193    the speculative call sequence and return edge representing the call, the
1194    original EDGE can be removed and deallocated.  Return the edge that now
1195    represents the call.
1196 
1197    For "speculative" indirect call that contains multiple "speculative"
1198    targets (i.e. edge->indirect_info->num_speculative_call_targets > 1),
1199    decrease the count and only remove current direct edge.
1200 
1201    If no speculative direct call left to the speculative indirect call, remove
1202    the speculative of both the indirect call and corresponding direct edge.
1203 
1204    It is up to caller to iteratively resolve each "speculative" direct call and
1205    redirect the call as appropriate.  */
1206 
1207 cgraph_edge *
resolve_speculation(cgraph_edge * edge,tree callee_decl)1208 cgraph_edge::resolve_speculation (cgraph_edge *edge, tree callee_decl)
1209 {
1210   cgraph_edge *e2;
1211   ipa_ref *ref;
1212 
1213   gcc_assert (edge->speculative && (!callee_decl || edge->callee));
1214   if (!edge->callee)
1215     e2 = edge->first_speculative_call_target ();
1216   else
1217     e2 = edge;
1218   ref = e2->speculative_call_target_ref ();
1219   edge = edge->speculative_call_indirect_edge ();
1220   if (!callee_decl
1221       || !ref->referred->semantically_equivalent_p
1222 	   (symtab_node::get (callee_decl)))
1223     {
1224       if (dump_file)
1225 	{
1226 	  if (callee_decl)
1227 	    {
1228 	      fprintf (dump_file, "Speculative indirect call %s => %s has "
1229 		       "turned out to have contradicting known target ",
1230 		       edge->caller->dump_name (),
1231 		       e2->callee->dump_name ());
1232 	      print_generic_expr (dump_file, callee_decl);
1233 	      fprintf (dump_file, "\n");
1234 	    }
1235 	  else
1236 	    {
1237 	      fprintf (dump_file, "Removing speculative call %s => %s\n",
1238 		       edge->caller->dump_name (),
1239 		       e2->callee->dump_name ());
1240 	    }
1241 	}
1242     }
1243   else
1244     {
1245       cgraph_edge *tmp = edge;
1246       if (dump_file)
1247 	fprintf (dump_file, "Speculative call turned into direct call.\n");
1248       edge = e2;
1249       e2 = tmp;
1250       /* FIXME:  If EDGE is inlined, we should scale up the frequencies
1251 	 and counts in the functions inlined through it.  */
1252     }
1253   edge->count += e2->count;
1254   if (edge->num_speculative_call_targets_p ())
1255     {
1256       /* The indirect edge has multiple speculative targets, don't remove
1257 	 speculative until all related direct edges are resolved.  */
1258       edge->indirect_info->num_speculative_call_targets--;
1259       if (!edge->indirect_info->num_speculative_call_targets)
1260 	edge->speculative = false;
1261     }
1262   else
1263     edge->speculative = false;
1264   e2->speculative = false;
1265   update_call_stmt_hash_for_removing_direct_edge (e2, edge);
1266   ref->remove_reference ();
1267   if (e2->indirect_unknown_callee || e2->inline_failed)
1268     remove (e2);
1269   else
1270     e2->callee->remove_symbol_and_inline_clones ();
1271   return edge;
1272 }
1273 
1274 /* Return edge corresponding to speculative call to a given target.
1275    NULL if speculative call does not have one.  */
1276 
1277 cgraph_edge *
speculative_call_for_target(cgraph_node * target)1278 cgraph_edge::speculative_call_for_target (cgraph_node *target)
1279 {
1280   for (cgraph_edge *direct = first_speculative_call_target ();
1281        direct;
1282        direct = direct->next_speculative_call_target ())
1283     if (direct->speculative_call_target_ref ()
1284 	->referred->semantically_equivalent_p (target))
1285       return direct;
1286   return NULL;
1287 }
1288 
1289 /* Make an indirect or speculative EDGE with an unknown callee an ordinary edge
1290    leading to CALLEE.  Speculations can be resolved in the process and EDGE can
1291    be removed and deallocated.  Return the edge that now represents the
1292    call.  */
1293 
1294 cgraph_edge *
make_direct(cgraph_edge * edge,cgraph_node * callee)1295 cgraph_edge::make_direct (cgraph_edge *edge, cgraph_node *callee)
1296 {
1297   gcc_assert (edge->indirect_unknown_callee || edge->speculative);
1298 
1299   /* If we are redirecting speculative call, make it non-speculative.  */
1300   if (edge->speculative)
1301     {
1302       cgraph_edge *found = NULL;
1303       cgraph_edge *direct, *next;
1304 
1305       edge = edge->speculative_call_indirect_edge ();
1306 
1307       /* Look all speculative targets and remove all but one corresponding
1308 	 to callee (if it exists).  */
1309       for (direct = edge->first_speculative_call_target ();
1310 	   direct;
1311 	   direct = next)
1312 	{
1313 	  next = direct->next_speculative_call_target ();
1314 
1315 	  /* Compare ref not direct->callee.  Direct edge is possibly
1316 	     inlined or redirected.  */
1317 	  if (!direct->speculative_call_target_ref ()
1318 	       ->referred->semantically_equivalent_p (callee))
1319 	    edge = direct->resolve_speculation (direct, NULL);
1320 	  else
1321 	    {
1322 	      gcc_checking_assert (!found);
1323 	      found = direct;
1324 	    }
1325 	}
1326 
1327       /* On successful speculation just remove the indirect edge and
1328 	 return the pre existing direct edge.
1329 	 It is important to not remove it and redirect because the direct
1330 	 edge may be inlined or redirected.  */
1331       if (found)
1332 	{
1333 	  cgraph_edge *e2 = resolve_speculation (found, callee->decl);
1334 	  gcc_checking_assert (!found->speculative && e2 == found);
1335 	  return found;
1336 	}
1337       gcc_checking_assert (!edge->speculative);
1338     }
1339 
1340   edge->indirect_unknown_callee = 0;
1341   ggc_free (edge->indirect_info);
1342   edge->indirect_info = NULL;
1343 
1344   /* Get the edge out of the indirect edge list. */
1345   if (edge->prev_callee)
1346     edge->prev_callee->next_callee = edge->next_callee;
1347   if (edge->next_callee)
1348     edge->next_callee->prev_callee = edge->prev_callee;
1349   if (!edge->prev_callee)
1350     edge->caller->indirect_calls = edge->next_callee;
1351 
1352   /* Put it into the normal callee list */
1353   edge->prev_callee = NULL;
1354   edge->next_callee = edge->caller->callees;
1355   if (edge->caller->callees)
1356     edge->caller->callees->prev_callee = edge;
1357   edge->caller->callees = edge;
1358 
1359   /* Insert to callers list of the new callee.  */
1360   edge->set_callee (callee);
1361 
1362   /* We need to re-determine the inlining status of the edge.  */
1363   initialize_inline_failed (edge);
1364   return edge;
1365 }
1366 
1367 /* Redirect callee of the edge to N.  The function does not update underlying
1368    call expression.  */
1369 
1370 void
redirect_callee(cgraph_node * n)1371 cgraph_edge::redirect_callee (cgraph_node *n)
1372 {
1373   bool loc = callee->comdat_local_p ();
1374   /* Remove from callers list of the current callee.  */
1375   remove_callee ();
1376 
1377   /* Insert to callers list of the new callee.  */
1378   set_callee (n);
1379 
1380   if (!inline_failed)
1381     return;
1382   if (!loc && n->comdat_local_p ())
1383     {
1384       cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1385       to->calls_comdat_local = true;
1386     }
1387   else if (loc && !n->comdat_local_p ())
1388     {
1389       cgraph_node *to = caller->inlined_to ? caller->inlined_to : caller;
1390       gcc_checking_assert (to->calls_comdat_local);
1391       to->calls_comdat_local = to->check_calls_comdat_local_p ();
1392     }
1393 }
1394 
1395 /* If necessary, change the function declaration in the call statement
1396    associated with E so that it corresponds to the edge callee.  Speculations
1397    can be resolved in the process and EDGE can be removed and deallocated.
1398 
1399    The edge could be one of speculative direct call generated from speculative
1400    indirect call.  In this circumstance, decrease the speculative targets
1401    count (i.e. num_speculative_call_targets) and redirect call stmt to the
1402    corresponding i-th target.  If no speculative direct call left to the
1403    speculative indirect call, remove "speculative" of the indirect call and
1404    also redirect stmt to it's final direct target.
1405 
1406    When called from within tree-inline, KILLED_SSAs has to contain the pointer
1407    to killed_new_ssa_names within the copy_body_data structure and SSAs
1408    discovered to be useless (if LHS is removed) will be added to it, otherwise
1409    it needs to be NULL.
1410 
1411    It is up to caller to iteratively transform each "speculative"
1412    direct call as appropriate.  */
1413 
1414 gimple *
redirect_call_stmt_to_callee(cgraph_edge * e,hash_set<tree> * killed_ssas)1415 cgraph_edge::redirect_call_stmt_to_callee (cgraph_edge *e,
1416 					   hash_set <tree> *killed_ssas)
1417 {
1418   tree decl = gimple_call_fndecl (e->call_stmt);
1419   gcall *new_stmt;
1420   gimple_stmt_iterator gsi;
1421 
1422   if (e->speculative)
1423     {
1424       /* If there already is an direct call (i.e. as a result of inliner's
1425 	 substitution), forget about speculating.  */
1426       if (decl)
1427 	e = make_direct (e->speculative_call_indirect_edge (),
1428 			 cgraph_node::get (decl));
1429       else
1430 	{
1431 	  /* Be sure we redirect all speculative targets before poking
1432 	     abou tindirect edge.  */
1433 	  gcc_checking_assert (e->callee);
1434 	  cgraph_edge *indirect = e->speculative_call_indirect_edge ();
1435 	  gcall *new_stmt;
1436 	  ipa_ref *ref;
1437 
1438 	  /* Expand speculation into GIMPLE code.  */
1439 	  if (dump_file)
1440 	    {
1441 	      fprintf (dump_file,
1442 		       "Expanding speculative call of %s -> %s count: ",
1443 		       e->caller->dump_name (),
1444 		       e->callee->dump_name ());
1445 	      e->count.dump (dump_file);
1446 	      fprintf (dump_file, "\n");
1447 	    }
1448 	  push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1449 
1450 	  profile_count all = indirect->count;
1451 	  for (cgraph_edge *e2 = e->first_speculative_call_target ();
1452 	       e2;
1453 	       e2 = e2->next_speculative_call_target ())
1454 	    all = all + e2->count;
1455 	  profile_probability prob = e->count.probability_in (all);
1456 	  if (!prob.initialized_p ())
1457 	    prob = profile_probability::even ();
1458 	  ref = e->speculative_call_target_ref ();
1459 	  new_stmt = gimple_ic (e->call_stmt,
1460 				dyn_cast<cgraph_node *> (ref->referred),
1461 				prob);
1462 	  e->speculative = false;
1463 	  if (indirect->num_speculative_call_targets_p ())
1464 	    {
1465 	      /* The indirect edge has multiple speculative targets, don't
1466 		 remove speculative until all related direct edges are
1467 		 redirected.  */
1468 	      indirect->indirect_info->num_speculative_call_targets--;
1469 	      if (!indirect->indirect_info->num_speculative_call_targets)
1470 		indirect->speculative = false;
1471 	    }
1472 	  else
1473 	    indirect->speculative = false;
1474 	  /* Indirect edges are not both in the call site hash.
1475 	     get it updated.  */
1476 	  update_call_stmt_hash_for_removing_direct_edge (e, indirect);
1477 	  cgraph_edge::set_call_stmt (e, new_stmt, false);
1478 	  e->count = gimple_bb (e->call_stmt)->count;
1479 
1480 	  /* Once we are done with expanding the sequence, update also indirect
1481 	     call probability.  Until then the basic block accounts for the
1482 	     sum of indirect edge and all non-expanded speculations.  */
1483 	  if (!indirect->speculative)
1484 	    indirect->count = gimple_bb (indirect->call_stmt)->count;
1485 	  ref->speculative = false;
1486 	  ref->stmt = NULL;
1487 	  pop_cfun ();
1488 	  /* Continue redirecting E to proper target.  */
1489 	}
1490     }
1491 
1492 
1493   if (e->indirect_unknown_callee
1494       || decl == e->callee->decl)
1495     return e->call_stmt;
1496 
1497   if (decl && ipa_saved_clone_sources)
1498     {
1499       tree *p = ipa_saved_clone_sources->get (e->callee);
1500       if (p && decl == *p)
1501 	{
1502 	  gimple_call_set_fndecl (e->call_stmt, e->callee->decl);
1503 	  return e->call_stmt;
1504 	}
1505     }
1506   if (flag_checking && decl)
1507     {
1508       if (cgraph_node *node = cgraph_node::get (decl))
1509 	{
1510 	  clone_info *info = clone_info::get (node);
1511 	  gcc_assert (!info || !info->param_adjustments);
1512 	}
1513     }
1514 
1515   clone_info *callee_info = clone_info::get (e->callee);
1516   if (symtab->dump_file)
1517     {
1518       fprintf (symtab->dump_file, "updating call of %s -> %s: ",
1519 	       e->caller->dump_name (), e->callee->dump_name ());
1520       print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1521       if (callee_info && callee_info->param_adjustments)
1522 	callee_info->param_adjustments->dump (symtab->dump_file);
1523     }
1524 
1525   if (ipa_param_adjustments *padjs
1526 	 = callee_info ? callee_info->param_adjustments : NULL)
1527     {
1528       /* We need to defer cleaning EH info on the new statement to
1529 	 fixup-cfg.  We may not have dominator information at this point
1530 	 and thus would end up with unreachable blocks and have no way
1531 	 to communicate that we need to run CFG cleanup then.  */
1532       int lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1533       if (lp_nr != 0)
1534 	remove_stmt_from_eh_lp (e->call_stmt);
1535 
1536       tree old_fntype = gimple_call_fntype (e->call_stmt);
1537       new_stmt = padjs->modify_call (e, false, killed_ssas);
1538       cgraph_node *origin = e->callee;
1539       while (origin->clone_of)
1540 	origin = origin->clone_of;
1541 
1542       if ((origin->former_clone_of
1543 	   && old_fntype == TREE_TYPE (origin->former_clone_of))
1544 	  || old_fntype == TREE_TYPE (origin->decl))
1545 	gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1546       else
1547 	{
1548 	  tree new_fntype = padjs->build_new_function_type (old_fntype, true);
1549 	  gimple_call_set_fntype (new_stmt, new_fntype);
1550 	}
1551 
1552       if (lp_nr != 0)
1553 	add_stmt_to_eh_lp (new_stmt, lp_nr);
1554     }
1555   else
1556     {
1557       if (flag_checking
1558 	  && !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
1559 	ipa_verify_edge_has_no_modifications (e);
1560       new_stmt = e->call_stmt;
1561       gimple_call_set_fndecl (new_stmt, e->callee->decl);
1562       update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1563     }
1564 
1565   /* If changing the call to __cxa_pure_virtual or similar noreturn function,
1566      adjust gimple_call_fntype too.  */
1567   if (gimple_call_noreturn_p (new_stmt)
1568       && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (e->callee->decl)))
1569       && TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl))
1570       && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (e->callee->decl)))
1571 	  == void_type_node))
1572     gimple_call_set_fntype (new_stmt, TREE_TYPE (e->callee->decl));
1573 
1574   /* If the call becomes noreturn, remove the LHS if possible.  */
1575   tree lhs = gimple_call_lhs (new_stmt);
1576   if (lhs
1577       && gimple_call_noreturn_p (new_stmt)
1578       && (VOID_TYPE_P (TREE_TYPE (gimple_call_fntype (new_stmt)))
1579 	  || should_remove_lhs_p (lhs)))
1580     {
1581       if (TREE_CODE (lhs) == SSA_NAME)
1582 	{
1583 	  tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1584 					TREE_TYPE (lhs), NULL);
1585 	  var = get_or_create_ssa_default_def
1586 		  (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1587 	  gimple *set_stmt = gimple_build_assign (lhs, var);
1588 	  gsi = gsi_for_stmt (new_stmt);
1589 	  gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1590 	  update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
1591 	}
1592       gimple_call_set_lhs (new_stmt, NULL_TREE);
1593       update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1594     }
1595 
1596   /* If new callee has no static chain, remove it.  */
1597   if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1598     {
1599       gimple_call_set_chain (new_stmt, NULL);
1600       update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1601     }
1602 
1603   maybe_remove_unused_call_args (DECL_STRUCT_FUNCTION (e->caller->decl),
1604 				 new_stmt);
1605 
1606   e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
1607 
1608   if (symtab->dump_file)
1609     {
1610       fprintf (symtab->dump_file, "  updated to:");
1611       print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
1612     }
1613   return new_stmt;
1614 }
1615 
1616 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1617    OLD_STMT changed into NEW_STMT.  OLD_CALL is gimple_call_fndecl
1618    of OLD_STMT if it was previously call statement.
1619    If NEW_STMT is NULL, the call has been dropped without any
1620    replacement.  */
1621 
1622 static void
cgraph_update_edges_for_call_stmt_node(cgraph_node * node,gimple * old_stmt,tree old_call,gimple * new_stmt)1623 cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
1624 					gimple *old_stmt, tree old_call,
1625 					gimple *new_stmt)
1626 {
1627   tree new_call = (new_stmt && is_gimple_call (new_stmt))
1628 		  ? gimple_call_fndecl (new_stmt) : 0;
1629 
1630   /* We are seeing indirect calls, then there is nothing to update.  */
1631   if (!new_call && !old_call)
1632     return;
1633   /* See if we turned indirect call into direct call or folded call to one builtin
1634      into different builtin.  */
1635   if (old_call != new_call)
1636     {
1637       cgraph_edge *e = node->get_edge (old_stmt);
1638       cgraph_edge *ne = NULL;
1639       profile_count count;
1640 
1641       if (e)
1642 	{
1643 	  /* Keep calls marked as dead dead.  */
1644 	  if (new_stmt && is_gimple_call (new_stmt) && e->callee
1645 	      && fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
1646 	    {
1647 	      cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1648 					  as_a <gcall *> (new_stmt));
1649 	      return;
1650 	    }
1651 	  /* See if the edge is already there and has the correct callee.  It
1652 	     might be so because of indirect inlining has already updated
1653 	     it.  We also might've cloned and redirected the edge.  */
1654 	  if (new_call && e->callee)
1655 	    {
1656 	      cgraph_node *callee = e->callee;
1657 	      while (callee)
1658 		{
1659 		  if (callee->decl == new_call
1660 		      || callee->former_clone_of == new_call)
1661 		    {
1662 		      cgraph_edge::set_call_stmt (e, as_a <gcall *> (new_stmt));
1663 		      return;
1664 		    }
1665 		  callee = callee->clone_of;
1666 		}
1667 	    }
1668 
1669 	  /* Otherwise remove edge and create new one; we can't simply redirect
1670 	     since function has changed, so inline plan and other information
1671 	     attached to edge is invalid.  */
1672 	  count = e->count;
1673  	  if (e->indirect_unknown_callee || e->inline_failed)
1674 	    cgraph_edge::remove (e);
1675 	  else
1676 	    e->callee->remove_symbol_and_inline_clones ();
1677 	}
1678       else if (new_call)
1679 	{
1680 	  /* We are seeing new direct call; compute profile info based on BB.  */
1681 	  basic_block bb = gimple_bb (new_stmt);
1682 	  count = bb->count;
1683 	}
1684 
1685       if (new_call)
1686 	{
1687 	  ne = node->create_edge (cgraph_node::get_create (new_call),
1688 				  as_a <gcall *> (new_stmt), count);
1689 	  gcc_assert (ne->inline_failed);
1690 	}
1691     }
1692   /* We only updated the call stmt; update pointer in cgraph edge..  */
1693   else if (old_stmt != new_stmt)
1694     cgraph_edge::set_call_stmt (node->get_edge (old_stmt),
1695 				as_a <gcall *> (new_stmt));
1696 }
1697 
1698 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1699    OLD_STMT changed into NEW_STMT.  OLD_DECL is gimple_call_fndecl
1700    of OLD_STMT before it was updated (updating can happen inplace).  */
1701 
1702 void
cgraph_update_edges_for_call_stmt(gimple * old_stmt,tree old_decl,gimple * new_stmt)1703 cgraph_update_edges_for_call_stmt (gimple *old_stmt, tree old_decl,
1704 				   gimple *new_stmt)
1705 {
1706   cgraph_node *orig = cgraph_node::get (cfun->decl);
1707   cgraph_node *node;
1708 
1709   gcc_checking_assert (orig);
1710   cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1711   if (orig->clones)
1712     for (node = orig->clones; node != orig;)
1713       {
1714 	cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl,
1715 						new_stmt);
1716 	if (node->clones)
1717 	  node = node->clones;
1718 	else if (node->next_sibling_clone)
1719 	  node = node->next_sibling_clone;
1720 	else
1721 	  {
1722 	    while (node != orig && !node->next_sibling_clone)
1723 	      node = node->clone_of;
1724 	    if (node != orig)
1725 	      node = node->next_sibling_clone;
1726 	  }
1727       }
1728 }
1729 
1730 
1731 /* Remove all callees from the node.  */
1732 
1733 void
remove_callees(void)1734 cgraph_node::remove_callees (void)
1735 {
1736   cgraph_edge *e, *f;
1737 
1738   calls_comdat_local = false;
1739 
1740   /* It is sufficient to remove the edges from the lists of callers of
1741      the callees.  The callee list of the node can be zapped with one
1742      assignment.  */
1743   for (e = callees; e; e = f)
1744     {
1745       f = e->next_callee;
1746       symtab->call_edge_removal_hooks (e);
1747       if (!e->indirect_unknown_callee)
1748 	e->remove_callee ();
1749       symtab->free_edge (e);
1750     }
1751   for (e = indirect_calls; e; e = f)
1752     {
1753       f = e->next_callee;
1754       symtab->call_edge_removal_hooks (e);
1755       if (!e->indirect_unknown_callee)
1756 	e->remove_callee ();
1757       symtab->free_edge (e);
1758     }
1759   indirect_calls = NULL;
1760   callees = NULL;
1761   if (call_site_hash)
1762     {
1763       call_site_hash->empty ();
1764       call_site_hash = NULL;
1765     }
1766 }
1767 
1768 /* Remove all callers from the node.  */
1769 
1770 void
remove_callers(void)1771 cgraph_node::remove_callers (void)
1772 {
1773   cgraph_edge *e, *f;
1774 
1775   /* It is sufficient to remove the edges from the lists of callees of
1776      the callers.  The caller list of the node can be zapped with one
1777      assignment.  */
1778   for (e = callers; e; e = f)
1779     {
1780       f = e->next_caller;
1781       symtab->call_edge_removal_hooks (e);
1782       e->remove_caller ();
1783       symtab->free_edge (e);
1784     }
1785   callers = NULL;
1786 }
1787 
1788 /* Helper function for cgraph_release_function_body and free_lang_data.
1789    It releases body from function DECL without having to inspect its
1790    possibly non-existent symtab node.  */
1791 
1792 void
release_function_body(tree decl)1793 release_function_body (tree decl)
1794 {
1795   function *fn = DECL_STRUCT_FUNCTION (decl);
1796   if (fn)
1797     {
1798       if (fn->cfg
1799 	  && loops_for_fn (fn))
1800 	{
1801 	  fn->curr_properties &= ~PROP_loops;
1802 	  loop_optimizer_finalize (fn);
1803 	}
1804       if (fn->gimple_df)
1805 	{
1806 	  delete_tree_ssa (fn);
1807 	  fn->eh = NULL;
1808 	}
1809       if (fn->cfg)
1810 	{
1811 	  gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1812 	  gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1813 	  delete_tree_cfg_annotations (fn);
1814 	  free_cfg (fn);
1815 	  fn->cfg = NULL;
1816 	}
1817       if (fn->value_histograms)
1818 	free_histograms (fn);
1819       gimple_set_body (decl, NULL);
1820       /* Struct function hangs a lot of data that would leak if we didn't
1821 	 removed all pointers to it.   */
1822       ggc_free (fn);
1823       DECL_STRUCT_FUNCTION (decl) = NULL;
1824     }
1825   DECL_SAVED_TREE (decl) = NULL;
1826 }
1827 
1828 /* Release memory used to represent body of function.
1829    Use this only for functions that are released before being translated to
1830    target code (i.e. RTL).  Functions that are compiled to RTL and beyond
1831    are free'd in final.cc via free_after_compilation().
1832    KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk.  */
1833 
1834 void
release_body(bool keep_arguments)1835 cgraph_node::release_body (bool keep_arguments)
1836 {
1837   ipa_transforms_to_apply.release ();
1838   if (!used_as_abstract_origin && symtab->state != PARSING)
1839     {
1840       DECL_RESULT (decl) = NULL;
1841 
1842       if (!keep_arguments)
1843 	DECL_ARGUMENTS (decl) = NULL;
1844     }
1845   /* If the node is abstract and needed, then do not clear
1846      DECL_INITIAL of its associated function declaration because it's
1847      needed to emit debug info later.  */
1848   if (!used_as_abstract_origin && DECL_INITIAL (decl))
1849     DECL_INITIAL (decl) = error_mark_node;
1850   release_function_body (decl);
1851   if (lto_file_data)
1852     {
1853       lto_free_function_in_decl_state_for_node (this);
1854       lto_file_data = NULL;
1855     }
1856   if (flag_checking && clones)
1857     {
1858       /* It is invalid to release body before materializing clones except
1859 	 for thunks that don't really need a body.  Verify also that we do
1860 	 not leak pointers to the call statements.  */
1861       for (cgraph_node *node = clones; node;
1862 	   node = node->next_sibling_clone)
1863 	gcc_assert (node->thunk && !node->callees->call_stmt);
1864     }
1865   remove_callees ();
1866   remove_all_references ();
1867 }
1868 
1869 /* Remove function from symbol table.  */
1870 
1871 void
remove(void)1872 cgraph_node::remove (void)
1873 {
1874   bool clone_info_set = false;
1875   clone_info *info, saved_info;
1876   if (symtab->ipa_clones_dump_file && symtab->cloned_nodes.contains (this))
1877     fprintf (symtab->ipa_clones_dump_file,
1878 	     "Callgraph removal;%s;%d;%s;%d;%d\n", asm_name (), order,
1879 	     DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl),
1880 	     DECL_SOURCE_COLUMN (decl));
1881 
1882   if ((info = clone_info::get (this)) != NULL)
1883     {
1884       saved_info = *info;
1885       clone_info_set = true;
1886     }
1887   symtab->call_cgraph_removal_hooks (this);
1888   remove_callers ();
1889   remove_callees ();
1890   ipa_transforms_to_apply.release ();
1891   delete_function_version (function_version ());
1892 
1893   /* Incremental inlining access removed nodes stored in the postorder list.
1894      */
1895   force_output = false;
1896   forced_by_abi = false;
1897 
1898   unregister (clone_info_set ? &saved_info : NULL);
1899   if (prev_sibling_clone)
1900     prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1901   else if (clone_of)
1902     {
1903       clone_of->clones = next_sibling_clone;
1904       if (!clones)
1905 	{
1906 	  bool need_body = false;
1907 	  for (cgraph_node *n = clone_of; n; n = n->clone_of)
1908 	    if (n->analyzed || n->clones)
1909 	      {
1910 		need_body = true;
1911 		break;
1912 	      }
1913 	  if (!need_body)
1914 	    clone_of->release_body ();
1915 	}
1916     }
1917   if (next_sibling_clone)
1918     next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1919   if (clones)
1920     {
1921       cgraph_node *n, *next;
1922 
1923       if (clone_of)
1924 	{
1925 	  for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1926 	    n->clone_of = clone_of;
1927 	  n->clone_of = clone_of;
1928 	  n->next_sibling_clone = clone_of->clones;
1929 	  if (clone_of->clones)
1930 	    clone_of->clones->prev_sibling_clone = n;
1931 	  clone_of->clones = clones;
1932 	}
1933       else
1934 	{
1935 	  /* We are removing node with clones.  This makes clones inconsistent,
1936 	     but assume they will be removed subsequently and just keep clone
1937 	     tree intact.  This can happen in unreachable function removal since
1938 	     we remove unreachable functions in random order, not by bottom-up
1939 	     walk of clone trees.  */
1940 	  for (n = clones; n; n = next)
1941 	    {
1942 	       next = n->next_sibling_clone;
1943 	       n->next_sibling_clone = NULL;
1944 	       n->prev_sibling_clone = NULL;
1945 	       n->clone_of = NULL;
1946 	    }
1947 	}
1948     }
1949 
1950   /* While all the clones are removed after being proceeded, the function
1951      itself is kept in the cgraph even after it is compiled.  Check whether
1952      we are done with this body and reclaim it proactively if this is the case.
1953      */
1954   if (symtab->state != LTO_STREAMING)
1955     {
1956       cgraph_node *n = cgraph_node::get (decl);
1957       if (!n
1958 	  || (!n->clones && !n->clone_of && !n->inlined_to
1959 	      && ((symtab->global_info_ready || in_lto_p)
1960 		  && (TREE_ASM_WRITTEN (n->decl)
1961 		      || DECL_EXTERNAL (n->decl)
1962 		      || !n->analyzed
1963 		      || (!flag_wpa && n->in_other_partition)))))
1964 	release_body ();
1965     }
1966   else
1967     {
1968       lto_free_function_in_decl_state_for_node (this);
1969       lto_file_data = NULL;
1970     }
1971 
1972   decl = NULL;
1973   if (call_site_hash)
1974     {
1975       call_site_hash->empty ();
1976       call_site_hash = NULL;
1977     }
1978 
1979   symtab->release_symbol (this);
1980 }
1981 
1982 /* Likewise indicate that a node is having address taken.  */
1983 
1984 void
mark_address_taken(void)1985 cgraph_node::mark_address_taken (void)
1986 {
1987   /* Indirect inlining can figure out that all uses of the address are
1988      inlined.  */
1989   if (inlined_to)
1990     {
1991       gcc_assert (cfun->after_inlining);
1992       gcc_assert (callers->indirect_inlining_edge);
1993       return;
1994     }
1995   /* FIXME: address_taken flag is used both as a shortcut for testing whether
1996      IPA_REF_ADDR reference exists (and thus it should be set on node
1997      representing alias we take address of) and as a test whether address
1998      of the object was taken (and thus it should be set on node alias is
1999      referring to).  We should remove the first use and the remove the
2000      following set.  */
2001   address_taken = 1;
2002   cgraph_node *node = ultimate_alias_target ();
2003   node->address_taken = 1;
2004 }
2005 
2006 /* Return local info node for the compiled function.  */
2007 
2008 cgraph_node *
local_info_node(tree decl)2009 cgraph_node::local_info_node (tree decl)
2010 {
2011   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2012   cgraph_node *node = get (decl);
2013   if (!node)
2014     return NULL;
2015   return node->ultimate_alias_target ();
2016 }
2017 
2018 /* Return RTL info for the compiled function.  */
2019 
2020 cgraph_rtl_info *
rtl_info(const_tree decl)2021 cgraph_node::rtl_info (const_tree decl)
2022 {
2023   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2024   cgraph_node *node = get (decl);
2025   if (!node)
2026     return NULL;
2027   enum availability avail;
2028   node = node->ultimate_alias_target (&avail);
2029   if (decl != current_function_decl
2030       && (avail < AVAIL_AVAILABLE
2031 	  || (node->decl != current_function_decl
2032 	      && !TREE_ASM_WRITTEN (node->decl))))
2033     return NULL;
2034   /* Allocate if it doesn't exist.  */
2035   if (node->rtl == NULL)
2036     {
2037       node->rtl = ggc_cleared_alloc<cgraph_rtl_info> ();
2038       SET_HARD_REG_SET (node->rtl->function_used_regs);
2039     }
2040   return node->rtl;
2041 }
2042 
2043 /* Return a string describing the failure REASON.  */
2044 
2045 const char*
cgraph_inline_failed_string(cgraph_inline_failed_t reason)2046 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
2047 {
2048 #undef DEFCIFCODE
2049 #define DEFCIFCODE(code, type, string)	string,
2050 
2051   static const char *cif_string_table[CIF_N_REASONS] = {
2052 #include "cif-code.def"
2053   };
2054 
2055   /* Signedness of an enum type is implementation defined, so cast it
2056      to unsigned before testing. */
2057   gcc_assert ((unsigned) reason < CIF_N_REASONS);
2058   return cif_string_table[reason];
2059 }
2060 
2061 /* Return a type describing the failure REASON.  */
2062 
2063 cgraph_inline_failed_type_t
cgraph_inline_failed_type(cgraph_inline_failed_t reason)2064 cgraph_inline_failed_type (cgraph_inline_failed_t reason)
2065 {
2066 #undef DEFCIFCODE
2067 #define DEFCIFCODE(code, type, string)	type,
2068 
2069   static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
2070 #include "cif-code.def"
2071   };
2072 
2073   /* Signedness of an enum type is implementation defined, so cast it
2074      to unsigned before testing. */
2075   gcc_assert ((unsigned) reason < CIF_N_REASONS);
2076   return cif_type_table[reason];
2077 }
2078 
2079 /* Names used to print out the availability enum.  */
2080 const char * const cgraph_availability_names[] =
2081   {"unset", "not_available", "overwritable", "available", "local"};
2082 
2083 /* Output flags of edge to a file F.  */
2084 
2085 void
dump_edge_flags(FILE * f)2086 cgraph_edge::dump_edge_flags (FILE *f)
2087 {
2088   if (speculative)
2089     fprintf (f, "(speculative) ");
2090   if (!inline_failed)
2091     fprintf (f, "(inlined) ");
2092   if (call_stmt_cannot_inline_p)
2093     fprintf (f, "(call_stmt_cannot_inline_p) ");
2094   if (indirect_inlining_edge)
2095     fprintf (f, "(indirect_inlining) ");
2096   if (count.initialized_p ())
2097     {
2098       fprintf (f, "(");
2099       count.dump (f);
2100       fprintf (f, ",");
2101       fprintf (f, "%.2f per call) ", sreal_frequency ().to_double ());
2102     }
2103   if (can_throw_external)
2104     fprintf (f, "(can throw external) ");
2105 }
2106 
2107 /* Dump edge to stderr.  */
2108 
2109 void
debug(void)2110 cgraph_edge::debug (void)
2111 {
2112   fprintf (stderr, "%s -> %s ", caller->dump_asm_name (),
2113 	   callee == NULL ? "(null)" : callee->dump_asm_name ());
2114   dump_edge_flags (stderr);
2115   fprintf (stderr, "\n\n");
2116   caller->debug ();
2117   if (callee != NULL)
2118     callee->debug ();
2119 }
2120 
2121 /* Dump call graph node to file F.  */
2122 
2123 void
dump(FILE * f)2124 cgraph_node::dump (FILE *f)
2125 {
2126   cgraph_edge *edge;
2127 
2128   dump_base (f);
2129 
2130   if (inlined_to)
2131     fprintf (f, "  Function %s is inline copy in %s\n",
2132 	     dump_name (),
2133 	     inlined_to->dump_name ());
2134   if (clone_of)
2135     fprintf (f, "  Clone of %s\n", clone_of->dump_asm_name ());
2136   if (symtab->function_flags_ready)
2137     fprintf (f, "  Availability: %s\n",
2138 	     cgraph_availability_names [get_availability ()]);
2139 
2140   if (profile_id)
2141     fprintf (f, "  Profile id: %i\n",
2142 	     profile_id);
2143   if (unit_id)
2144     fprintf (f, "  Unit id: %i\n",
2145 	     unit_id);
2146   cgraph_function_version_info *vi = function_version ();
2147   if (vi != NULL)
2148     {
2149       fprintf (f, "  Version info: ");
2150       if (vi->prev != NULL)
2151 	{
2152 	  fprintf (f, "prev: ");
2153 	  fprintf (f, "%s ", vi->prev->this_node->dump_asm_name ());
2154 	}
2155       if (vi->next != NULL)
2156 	{
2157 	  fprintf (f, "next: ");
2158 	  fprintf (f, "%s ", vi->next->this_node->dump_asm_name ());
2159 	}
2160       if (vi->dispatcher_resolver != NULL_TREE)
2161 	fprintf (f, "dispatcher: %s",
2162 		 lang_hooks.decl_printable_name (vi->dispatcher_resolver, 2));
2163 
2164       fprintf (f, "\n");
2165     }
2166   fprintf (f, "  Function flags:");
2167   if (count.initialized_p ())
2168     {
2169       fprintf (f, " count:");
2170       count.dump (f);
2171     }
2172   if (tp_first_run > 0)
2173     fprintf (f, " first_run:%" PRId64, (int64_t) tp_first_run);
2174   if (cgraph_node *origin = nested_function_origin (this))
2175     fprintf (f, " nested in:%s", origin->dump_asm_name ());
2176   if (gimple_has_body_p (decl))
2177     fprintf (f, " body");
2178   if (process)
2179     fprintf (f, " process");
2180   if (local)
2181     fprintf (f, " local");
2182   if (redefined_extern_inline)
2183     fprintf (f, " redefined_extern_inline");
2184   if (only_called_at_startup)
2185     fprintf (f, " only_called_at_startup");
2186   if (only_called_at_exit)
2187     fprintf (f, " only_called_at_exit");
2188   if (tm_clone)
2189     fprintf (f, " tm_clone");
2190   if (calls_comdat_local)
2191     fprintf (f, " calls_comdat_local");
2192   if (icf_merged)
2193     fprintf (f, " icf_merged");
2194   if (merged_comdat)
2195     fprintf (f, " merged_comdat");
2196   if (merged_extern_inline)
2197     fprintf (f, " merged_extern_inline");
2198   if (split_part)
2199     fprintf (f, " split_part");
2200   if (indirect_call_target)
2201     fprintf (f, " indirect_call_target");
2202   if (nonfreeing_fn)
2203     fprintf (f, " nonfreeing_fn");
2204   if (DECL_STATIC_CONSTRUCTOR (decl))
2205     fprintf (f," static_constructor (priority:%i)", get_init_priority ());
2206   if (DECL_STATIC_DESTRUCTOR (decl))
2207     fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
2208   if (frequency == NODE_FREQUENCY_HOT)
2209     fprintf (f, " hot");
2210   if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2211     fprintf (f, " unlikely_executed");
2212   if (frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2213     fprintf (f, " executed_once");
2214   if (opt_for_fn (decl, optimize_size))
2215     fprintf (f, " optimize_size");
2216   if (parallelized_function)
2217     fprintf (f, " parallelized_function");
2218   if (DECL_IS_MALLOC (decl))
2219     fprintf (f, " decl_is_malloc");
2220   if (DECL_IS_OPERATOR_NEW_P (decl))
2221     fprintf (f, " %soperator_new",
2222 	     DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2223   if (DECL_IS_OPERATOR_DELETE_P (decl))
2224     fprintf (f, " %soperator_delete",
2225 	     DECL_IS_REPLACEABLE_OPERATOR (decl) ? "replaceable_" : "");
2226 
2227   if (DECL_STATIC_CHAIN (decl))
2228     fprintf (f, " static_chain");
2229 
2230   fprintf (f, "\n");
2231 
2232   if (thunk)
2233     {
2234       fprintf (f, "  Thunk");
2235       thunk_info::get (this)->dump (f);
2236     }
2237   else if (former_thunk_p ())
2238     {
2239       fprintf (f, "  Former thunk ");
2240       thunk_info::get (this)->dump (f);
2241     }
2242   else gcc_checking_assert (!thunk_info::get (this));
2243 
2244   fprintf (f, "  Called by: ");
2245 
2246   profile_count sum = profile_count::zero ();
2247   for (edge = callers; edge; edge = edge->next_caller)
2248     {
2249       fprintf (f, "%s ", edge->caller->dump_asm_name ());
2250       edge->dump_edge_flags (f);
2251       if (edge->count.initialized_p ())
2252 	sum += edge->count.ipa ();
2253     }
2254 
2255   fprintf (f, "\n  Calls: ");
2256   for (edge = callees; edge; edge = edge->next_callee)
2257     {
2258       fprintf (f, "%s ", edge->callee->dump_asm_name ());
2259       edge->dump_edge_flags (f);
2260     }
2261   fprintf (f, "\n");
2262 
2263   if (!body_removed && count.ipa ().initialized_p ())
2264     {
2265       bool ok = true;
2266       bool min = false;
2267       ipa_ref *ref;
2268 
2269       FOR_EACH_ALIAS (this, ref)
2270 	if (dyn_cast <cgraph_node *> (ref->referring)->count.initialized_p ())
2271 	  sum += dyn_cast <cgraph_node *> (ref->referring)->count.ipa ();
2272 
2273       if (inlined_to
2274 	  || (symtab->state < EXPANSION
2275 	      && ultimate_alias_target () == this && only_called_directly_p ()))
2276 	ok = !count.ipa ().differs_from_p (sum);
2277       else if (count.ipa () > profile_count::from_gcov_type (100)
2278 	       && count.ipa () < sum.apply_scale (99, 100))
2279 	ok = false, min = true;
2280       if (!ok)
2281 	{
2282 	  fprintf (f, "   Invalid sum of caller counts ");
2283 	  sum.dump (f);
2284 	  if (min)
2285 	    fprintf (f, ", should be at most ");
2286 	  else
2287 	    fprintf (f, ", should be ");
2288 	  count.ipa ().dump (f);
2289 	  fprintf (f, "\n");
2290 	}
2291     }
2292 
2293   for (edge = indirect_calls; edge; edge = edge->next_callee)
2294     {
2295       if (edge->indirect_info->polymorphic)
2296 	{
2297 	  fprintf (f, "   Polymorphic indirect call of type ");
2298 	  print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
2299 	  fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
2300 	}
2301       else
2302 	fprintf (f, "   Indirect call");
2303       edge->dump_edge_flags (f);
2304       if (edge->indirect_info->param_index != -1)
2305 	{
2306 	  fprintf (f, "of param:%i ", edge->indirect_info->param_index);
2307 	  if (edge->indirect_info->agg_contents)
2308 	   fprintf (f, "loaded from %s %s at offset %i ",
2309 		    edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
2310 		    edge->indirect_info->by_ref ? "passed by reference":"",
2311 		    (int)edge->indirect_info->offset);
2312 	  if (edge->indirect_info->vptr_changed)
2313 	    fprintf (f, "(vptr maybe changed) ");
2314 	}
2315       fprintf (f, "num speculative call targets: %i\n",
2316 	       edge->indirect_info->num_speculative_call_targets);
2317       if (edge->indirect_info->polymorphic)
2318 	edge->indirect_info->context.dump (f);
2319     }
2320 }
2321 
2322 /* Dump call graph node to file F in graphviz format.  */
2323 
2324 void
dump_graphviz(FILE * f)2325 cgraph_node::dump_graphviz (FILE *f)
2326 {
2327   cgraph_edge *edge;
2328 
2329   for (edge = callees; edge; edge = edge->next_callee)
2330     {
2331       cgraph_node *callee = edge->callee;
2332 
2333       fprintf (f, "\t\"%s\" -> \"%s\"\n", dump_name (), callee->dump_name ());
2334     }
2335 }
2336 
2337 
2338 /* Dump call graph node NODE to stderr.  */
2339 
2340 DEBUG_FUNCTION void
debug(void)2341 cgraph_node::debug (void)
2342 {
2343   dump (stderr);
2344 }
2345 
2346 /* Dump the callgraph to file F.  */
2347 
2348 void
dump_cgraph(FILE * f)2349 cgraph_node::dump_cgraph (FILE *f)
2350 {
2351   cgraph_node *node;
2352 
2353   fprintf (f, "callgraph:\n\n");
2354   FOR_EACH_FUNCTION (node)
2355     node->dump (f);
2356 }
2357 
2358 /* Return true when the DECL can possibly be inlined.  */
2359 
2360 bool
cgraph_function_possibly_inlined_p(tree decl)2361 cgraph_function_possibly_inlined_p (tree decl)
2362 {
2363   if (!symtab->global_info_ready)
2364     return !DECL_UNINLINABLE (decl);
2365   return DECL_POSSIBLY_INLINED (decl);
2366 }
2367 
2368 /* Return function availability.  See cgraph.h for description of individual
2369    return values.  */
2370 enum availability
get_availability(symtab_node * ref)2371 cgraph_node::get_availability (symtab_node *ref)
2372 {
2373   if (ref)
2374     {
2375       cgraph_node *cref = dyn_cast <cgraph_node *> (ref);
2376       if (cref)
2377 	ref = cref->inlined_to;
2378     }
2379   enum availability avail;
2380   if (!analyzed && !in_other_partition)
2381     avail = AVAIL_NOT_AVAILABLE;
2382   else if (local)
2383     avail = AVAIL_LOCAL;
2384   else if (inlined_to)
2385     avail = AVAIL_AVAILABLE;
2386   else if (transparent_alias)
2387     ultimate_alias_target (&avail, ref);
2388   else if (ifunc_resolver
2389 	   || lookup_attribute ("noipa", DECL_ATTRIBUTES (decl)))
2390     avail = AVAIL_INTERPOSABLE;
2391   else if (!externally_visible)
2392     avail = AVAIL_AVAILABLE;
2393   /* If this is a reference from symbol itself and there are no aliases, we
2394      may be sure that the symbol was not interposed by something else because
2395      the symbol itself would be unreachable otherwise.
2396 
2397      Also comdat groups are always resolved in groups.  */
2398   else if ((this == ref && !has_aliases_p ())
2399 	   || (ref && get_comdat_group ()
2400 	       && get_comdat_group () == ref->get_comdat_group ()))
2401     avail = AVAIL_AVAILABLE;
2402   /* Inline functions are safe to be analyzed even if their symbol can
2403      be overwritten at runtime.  It is not meaningful to enforce any sane
2404      behavior on replacing inline function by different body.  */
2405   else if (DECL_DECLARED_INLINE_P (decl))
2406     avail = AVAIL_AVAILABLE;
2407 
2408   /* If the function can be overwritten, return OVERWRITABLE.  Take
2409      care at least of two notable extensions - the COMDAT functions
2410      used to share template instantiations in C++ (this is symmetric
2411      to code cp_cannot_inline_tree_fn and probably shall be shared and
2412      the inlinability hooks completely eliminated).  */
2413 
2414   else if (decl_replaceable_p (decl, semantic_interposition)
2415 	   && !DECL_EXTERNAL (decl))
2416     avail = AVAIL_INTERPOSABLE;
2417   else avail = AVAIL_AVAILABLE;
2418 
2419   return avail;
2420 }
2421 
2422 /* Worker for cgraph_node_can_be_local_p.  */
2423 static bool
cgraph_node_cannot_be_local_p_1(cgraph_node * node,void *)2424 cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
2425 {
2426   return !(!node->force_output
2427 	   && !node->ifunc_resolver
2428 	   /* Limitation of gas requires us to output targets of symver aliases
2429 	      as global symbols.  This is binutils PR 25295.  */
2430 	   && !node->symver
2431 	   && ((DECL_COMDAT (node->decl)
2432 		&& !node->forced_by_abi
2433 		&& !node->used_from_object_file_p ()
2434 		&& !node->same_comdat_group)
2435 	       || !node->externally_visible));
2436 }
2437 
2438 /* Return true if cgraph_node can be made local for API change.
2439    Extern inline functions and C++ COMDAT functions can be made local
2440    at the expense of possible code size growth if function is used in multiple
2441    compilation units.  */
2442 bool
can_be_local_p(void)2443 cgraph_node::can_be_local_p (void)
2444 {
2445   return (!address_taken
2446 	  && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2447 						NULL, true));
2448 }
2449 
2450 /* Call callback on cgraph_node, thunks and aliases associated to cgraph_node.
2451    When INCLUDE_OVERWRITABLE is false, overwritable symbols are
2452    skipped.  When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
2453    skipped.  */
2454 bool
call_for_symbol_thunks_and_aliases(bool (* callback)(cgraph_node *,void *),void * data,bool include_overwritable,bool exclude_virtual_thunks)2455 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2456 						   (cgraph_node *, void *),
2457 						 void *data,
2458 						 bool include_overwritable,
2459 						 bool exclude_virtual_thunks)
2460 {
2461   cgraph_edge *e;
2462   ipa_ref *ref;
2463   enum availability avail = AVAIL_AVAILABLE;
2464 
2465   if (include_overwritable
2466       || (avail = get_availability ()) > AVAIL_INTERPOSABLE)
2467     {
2468       if (callback (this, data))
2469 	return true;
2470     }
2471   FOR_EACH_ALIAS (this, ref)
2472     {
2473       cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2474       if (include_overwritable
2475 	  || alias->get_availability () > AVAIL_INTERPOSABLE)
2476 	if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2477 						     include_overwritable,
2478 						     exclude_virtual_thunks))
2479 	  return true;
2480     }
2481   if (avail <= AVAIL_INTERPOSABLE)
2482     return false;
2483   for (e = callers; e; e = e->next_caller)
2484     if (e->caller->thunk
2485 	&& (include_overwritable
2486 	    || e->caller->get_availability () > AVAIL_INTERPOSABLE)
2487 	&& !(exclude_virtual_thunks
2488 	     && thunk_info::get (e->caller)->virtual_offset_p))
2489       if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2490 						       include_overwritable,
2491 						       exclude_virtual_thunks))
2492 	return true;
2493 
2494   return false;
2495 }
2496 
2497 /* Worker to bring NODE local.  */
2498 
2499 bool
make_local(cgraph_node * node,void *)2500 cgraph_node::make_local (cgraph_node *node, void *)
2501 {
2502   gcc_checking_assert (node->can_be_local_p ());
2503   if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2504     {
2505       node->make_decl_local ();
2506       node->set_section (NULL);
2507       node->set_comdat_group (NULL);
2508       node->externally_visible = false;
2509       node->forced_by_abi = false;
2510       node->local = true;
2511       node->set_section (NULL);
2512       node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
2513 			   || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
2514 			   && !flag_incremental_link);
2515       node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2516       gcc_assert (node->get_availability () == AVAIL_LOCAL);
2517     }
2518   return false;
2519 }
2520 
2521 /* Bring cgraph node local.  */
2522 
2523 void
make_local(void)2524 cgraph_node::make_local (void)
2525 {
2526   call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
2527 }
2528 
2529 /* Worker to set nothrow flag.  */
2530 
2531 static void
set_nothrow_flag_1(cgraph_node * node,bool nothrow,bool non_call,bool * changed)2532 set_nothrow_flag_1 (cgraph_node *node, bool nothrow, bool non_call,
2533 		    bool *changed)
2534 {
2535   cgraph_edge *e;
2536 
2537   if (nothrow && !TREE_NOTHROW (node->decl))
2538     {
2539       /* With non-call exceptions we can't say for sure if other function body
2540 	 was not possibly optimized to still throw.  */
2541       if (!non_call || node->binds_to_current_def_p ())
2542 	{
2543 	  TREE_NOTHROW (node->decl) = true;
2544 	  *changed = true;
2545 	  for (e = node->callers; e; e = e->next_caller)
2546 	    e->can_throw_external = false;
2547 	}
2548     }
2549   else if (!nothrow && TREE_NOTHROW (node->decl))
2550     {
2551       TREE_NOTHROW (node->decl) = false;
2552       *changed = true;
2553     }
2554   ipa_ref *ref;
2555   FOR_EACH_ALIAS (node, ref)
2556     {
2557       cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2558       if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2559 	set_nothrow_flag_1 (alias, nothrow, non_call, changed);
2560     }
2561   for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2562     if (e->caller->thunk
2563 	&& (!nothrow || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2564       set_nothrow_flag_1 (e->caller, nothrow, non_call, changed);
2565 }
2566 
2567 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2568    if any to NOTHROW.  */
2569 
2570 bool
set_nothrow_flag(bool nothrow)2571 cgraph_node::set_nothrow_flag (bool nothrow)
2572 {
2573   bool changed = false;
2574   bool non_call = opt_for_fn (decl, flag_non_call_exceptions);
2575 
2576   if (!nothrow || get_availability () > AVAIL_INTERPOSABLE)
2577     set_nothrow_flag_1 (this, nothrow, non_call, &changed);
2578   else
2579     {
2580       ipa_ref *ref;
2581 
2582       FOR_EACH_ALIAS (this, ref)
2583 	{
2584 	  cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2585 	  if (!nothrow || alias->get_availability () > AVAIL_INTERPOSABLE)
2586 	    set_nothrow_flag_1 (alias, nothrow, non_call, &changed);
2587 	}
2588     }
2589   return changed;
2590 }
2591 
2592 /* Worker to set malloc flag.  */
2593 static void
set_malloc_flag_1(cgraph_node * node,bool malloc_p,bool * changed)2594 set_malloc_flag_1 (cgraph_node *node, bool malloc_p, bool *changed)
2595 {
2596   if (malloc_p && !DECL_IS_MALLOC (node->decl))
2597     {
2598       DECL_IS_MALLOC (node->decl) = true;
2599       *changed = true;
2600     }
2601 
2602   ipa_ref *ref;
2603   FOR_EACH_ALIAS (node, ref)
2604     {
2605       cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2606       if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2607 	set_malloc_flag_1 (alias, malloc_p, changed);
2608     }
2609 
2610   for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2611     if (e->caller->thunk
2612 	&& (!malloc_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2613       set_malloc_flag_1 (e->caller, malloc_p, changed);
2614 }
2615 
2616 /* Set DECL_IS_MALLOC on NODE's decl and on NODE's aliases if any.  */
2617 
2618 bool
set_malloc_flag(bool malloc_p)2619 cgraph_node::set_malloc_flag (bool malloc_p)
2620 {
2621   bool changed = false;
2622 
2623   if (!malloc_p || get_availability () > AVAIL_INTERPOSABLE)
2624     set_malloc_flag_1 (this, malloc_p, &changed);
2625   else
2626     {
2627       ipa_ref *ref;
2628 
2629       FOR_EACH_ALIAS (this, ref)
2630 	{
2631 	  cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2632 	  if (!malloc_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2633 	    set_malloc_flag_1 (alias, malloc_p, &changed);
2634 	}
2635     }
2636   return changed;
2637 }
2638 
2639 /* Worker to set noreturng flag.  */
2640 static void
set_noreturn_flag_1(cgraph_node * node,bool noreturn_p,bool * changed)2641 set_noreturn_flag_1 (cgraph_node *node, bool noreturn_p, bool *changed)
2642 {
2643   if (noreturn_p && !TREE_THIS_VOLATILE (node->decl))
2644     {
2645       TREE_THIS_VOLATILE (node->decl) = true;
2646       *changed = true;
2647     }
2648 
2649   ipa_ref *ref;
2650   FOR_EACH_ALIAS (node, ref)
2651     {
2652       cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2653       if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2654 	set_noreturn_flag_1 (alias, noreturn_p, changed);
2655     }
2656 
2657   for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2658     if (e->caller->thunk
2659 	&& (!noreturn_p || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2660       set_noreturn_flag_1 (e->caller, noreturn_p, changed);
2661 }
2662 
2663 /* Set TREE_THIS_VOLATILE on NODE's decl and on NODE's aliases if any.  */
2664 
2665 bool
set_noreturn_flag(bool noreturn_p)2666 cgraph_node::set_noreturn_flag (bool noreturn_p)
2667 {
2668   bool changed = false;
2669 
2670   if (!noreturn_p || get_availability () > AVAIL_INTERPOSABLE)
2671     set_noreturn_flag_1 (this, noreturn_p, &changed);
2672   else
2673     {
2674       ipa_ref *ref;
2675 
2676       FOR_EACH_ALIAS (this, ref)
2677 	{
2678 	  cgraph_node *alias = dyn_cast<cgraph_node *> (ref->referring);
2679 	  if (!noreturn_p || alias->get_availability () > AVAIL_INTERPOSABLE)
2680 	    set_noreturn_flag_1 (alias, noreturn_p, &changed);
2681 	}
2682     }
2683   return changed;
2684 }
2685 
2686 /* Worker to set_const_flag.  */
2687 
2688 static void
set_const_flag_1(cgraph_node * node,bool set_const,bool looping,bool * changed)2689 set_const_flag_1 (cgraph_node *node, bool set_const, bool looping,
2690 		  bool *changed)
2691 {
2692   /* Static constructors and destructors without a side effect can be
2693      optimized out.  */
2694   if (set_const && !looping)
2695     {
2696       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2697 	{
2698 	  DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2699 	  *changed = true;
2700 	}
2701       if (DECL_STATIC_DESTRUCTOR (node->decl))
2702 	{
2703 	  DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2704 	  *changed = true;
2705 	}
2706     }
2707   if (!set_const)
2708     {
2709       if (TREE_READONLY (node->decl))
2710 	{
2711 	  TREE_READONLY (node->decl) = 0;
2712 	  DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2713 	  *changed = true;
2714 	}
2715     }
2716   else
2717     {
2718       /* Consider function:
2719 
2720 	 bool a(int *p)
2721 	 {
2722 	   return *p==*p;
2723 	 }
2724 
2725 	 During early optimization we will turn this into:
2726 
2727 	 bool a(int *p)
2728 	 {
2729 	   return true;
2730 	 }
2731 
2732 	 Now if this function will be detected as CONST however when interposed
2733 	 it may end up being just pure.  We always must assume the worst
2734 	 scenario here.  */
2735       if (TREE_READONLY (node->decl))
2736 	{
2737 	  if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2738 	    {
2739 	      DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2740 	      *changed = true;
2741 	    }
2742 	}
2743       else if (node->binds_to_current_def_p ())
2744 	{
2745 	  TREE_READONLY (node->decl) = true;
2746 	  DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2747 	  DECL_PURE_P (node->decl) = false;
2748 	  *changed = true;
2749 	}
2750       else
2751 	{
2752 	  if (dump_file && (dump_flags & TDF_DETAILS))
2753 	    fprintf (dump_file, "Dropping state to PURE because function does "
2754 		     "not bind to current def.\n");
2755 	  if (!DECL_PURE_P (node->decl))
2756 	    {
2757 	      DECL_PURE_P (node->decl) = true;
2758 	      DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2759 	      *changed = true;
2760 	    }
2761 	  else if (!looping && DECL_LOOPING_CONST_OR_PURE_P (node->decl))
2762 	    {
2763 	      DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2764 	      *changed = true;
2765 	    }
2766 	}
2767     }
2768 
2769   ipa_ref *ref;
2770   FOR_EACH_ALIAS (node, ref)
2771     {
2772       cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2773       if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2774 	set_const_flag_1 (alias, set_const, looping, changed);
2775     }
2776   for (cgraph_edge *e = node->callers; e; e = e->next_caller)
2777     if (e->caller->thunk
2778 	&& (!set_const || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2779       {
2780 	/* Virtual thunks access virtual offset in the vtable, so they can
2781 	   only be pure, never const.  */
2782 	if (set_const
2783 	    && (thunk_info::get (e->caller)->virtual_offset_p
2784 		|| !node->binds_to_current_def_p (e->caller)))
2785 	  *changed |= e->caller->set_pure_flag (true, looping);
2786 	else
2787 	  set_const_flag_1 (e->caller, set_const, looping, changed);
2788       }
2789 }
2790 
2791 /* If SET_CONST is true, mark function, aliases and thunks to be ECF_CONST.
2792    If SET_CONST if false, clear the flag.
2793 
2794    When setting the flag be careful about possible interposition and
2795    do not set the flag for functions that can be interposed and set pure
2796    flag for functions that can bind to other definition.
2797 
2798    Return true if any change was done. */
2799 
2800 bool
set_const_flag(bool set_const,bool looping)2801 cgraph_node::set_const_flag (bool set_const, bool looping)
2802 {
2803   bool changed = false;
2804   if (!set_const || get_availability () > AVAIL_INTERPOSABLE)
2805     set_const_flag_1 (this, set_const, looping, &changed);
2806   else
2807     {
2808       ipa_ref *ref;
2809 
2810       FOR_EACH_ALIAS (this, ref)
2811 	{
2812 	  cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2813 	  if (!set_const || alias->get_availability () > AVAIL_INTERPOSABLE)
2814 	    set_const_flag_1 (alias, set_const, looping, &changed);
2815 	}
2816     }
2817   return changed;
2818 }
2819 
2820 /* Info used by set_pure_flag_1.  */
2821 
2822 struct set_pure_flag_info
2823 {
2824   bool pure;
2825   bool looping;
2826   bool changed;
2827 };
2828 
2829 /* Worker to set_pure_flag.  */
2830 
2831 static bool
set_pure_flag_1(cgraph_node * node,void * data)2832 set_pure_flag_1 (cgraph_node *node, void *data)
2833 {
2834   struct set_pure_flag_info *info = (struct set_pure_flag_info *)data;
2835   /* Static constructors and destructors without a side effect can be
2836      optimized out.  */
2837   if (info->pure && !info->looping)
2838     {
2839       if (DECL_STATIC_CONSTRUCTOR (node->decl))
2840 	{
2841 	  DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2842 	  info->changed = true;
2843 	}
2844       if (DECL_STATIC_DESTRUCTOR (node->decl))
2845 	{
2846 	  DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2847 	  info->changed = true;
2848 	}
2849     }
2850   if (info->pure)
2851     {
2852       if (!DECL_PURE_P (node->decl) && !TREE_READONLY (node->decl))
2853 	{
2854 	  DECL_PURE_P (node->decl) = true;
2855 	  DECL_LOOPING_CONST_OR_PURE_P (node->decl) = info->looping;
2856 	  info->changed = true;
2857 	}
2858       else if (DECL_LOOPING_CONST_OR_PURE_P (node->decl)
2859 	       && !info->looping)
2860 	{
2861 	  DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2862 	  info->changed = true;
2863 	}
2864     }
2865   else
2866     {
2867       if (DECL_PURE_P (node->decl))
2868 	{
2869 	  DECL_PURE_P (node->decl) = false;
2870 	  DECL_LOOPING_CONST_OR_PURE_P (node->decl) = false;
2871 	  info->changed = true;
2872 	}
2873     }
2874   return false;
2875 }
2876 
2877 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2878    if any to PURE.
2879 
2880    When setting the flag, be careful about possible interposition.
2881    Return true if any change was done. */
2882 
2883 bool
set_pure_flag(bool pure,bool looping)2884 cgraph_node::set_pure_flag (bool pure, bool looping)
2885 {
2886   struct set_pure_flag_info info = {pure, looping, false};
2887   call_for_symbol_thunks_and_aliases (set_pure_flag_1, &info, !pure, true);
2888   return info.changed;
2889 }
2890 
2891 /* Return true when cgraph_node cannot return or throw and thus
2892    it is safe to ignore its side effects for IPA analysis.  */
2893 
2894 bool
cannot_return_p(void)2895 cgraph_node::cannot_return_p (void)
2896 {
2897   int flags = flags_from_decl_or_type (decl);
2898   if (!opt_for_fn (decl, flag_exceptions))
2899     return (flags & ECF_NORETURN) != 0;
2900   else
2901     return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2902 	     == (ECF_NORETURN | ECF_NOTHROW));
2903 }
2904 
2905 /* Return true when call of edge cannot lead to return from caller
2906    and thus it is safe to ignore its side effects for IPA analysis
2907    when computing side effects of the caller.
2908    FIXME: We could actually mark all edges that have no reaching
2909    patch to the exit block or throw to get better results.  */
2910 bool
cannot_lead_to_return_p(void)2911 cgraph_edge::cannot_lead_to_return_p (void)
2912 {
2913   if (caller->cannot_return_p ())
2914     return true;
2915   if (indirect_unknown_callee)
2916     {
2917       int flags = indirect_info->ecf_flags;
2918       if (!opt_for_fn (caller->decl, flag_exceptions))
2919 	return (flags & ECF_NORETURN) != 0;
2920       else
2921 	return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2922 		 == (ECF_NORETURN | ECF_NOTHROW));
2923     }
2924   else
2925     return callee->cannot_return_p ();
2926 }
2927 
2928 /* Return true if the edge may be considered hot.  */
2929 
2930 bool
maybe_hot_p(void)2931 cgraph_edge::maybe_hot_p (void)
2932 {
2933   if (!maybe_hot_count_p (NULL, count.ipa ()))
2934     return false;
2935   if (caller->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
2936       || (callee
2937 	  && callee->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED))
2938     return false;
2939   if (caller->frequency > NODE_FREQUENCY_UNLIKELY_EXECUTED
2940       && (callee
2941 	  && callee->frequency <= NODE_FREQUENCY_EXECUTED_ONCE))
2942     return false;
2943   if (opt_for_fn (caller->decl, optimize_size))
2944     return false;
2945   if (caller->frequency == NODE_FREQUENCY_HOT)
2946     return true;
2947   if (!count.initialized_p ())
2948     return true;
2949   cgraph_node *where = caller->inlined_to ? caller->inlined_to : caller;
2950   if (!where->count.initialized_p ())
2951     return false;
2952   if (caller->frequency == NODE_FREQUENCY_EXECUTED_ONCE)
2953     {
2954       if (count.apply_scale (2, 1) < where->count.apply_scale (3, 1))
2955 	return false;
2956     }
2957   else if (count.apply_scale (param_hot_bb_frequency_fraction , 1)
2958 	   < where->count)
2959     return false;
2960   return true;
2961 }
2962 
2963 /* Worker for cgraph_can_remove_if_no_direct_calls_p.  */
2964 
2965 static bool
nonremovable_p(cgraph_node * node,void *)2966 nonremovable_p (cgraph_node *node, void *)
2967 {
2968   return !node->can_remove_if_no_direct_calls_and_refs_p ();
2969 }
2970 
2971 /* Return true if whole comdat group can be removed if there are no direct
2972    calls to THIS.  */
2973 
2974 bool
can_remove_if_no_direct_calls_p(bool will_inline)2975 cgraph_node::can_remove_if_no_direct_calls_p (bool will_inline)
2976 {
2977   struct ipa_ref *ref;
2978 
2979   /* For local symbols or non-comdat group it is the same as
2980      can_remove_if_no_direct_calls_p.  */
2981   if (!externally_visible || !same_comdat_group)
2982     {
2983       if (DECL_EXTERNAL (decl))
2984 	return true;
2985       if (address_taken)
2986 	return false;
2987       return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
2988     }
2989 
2990   if (will_inline && address_taken)
2991     return false;
2992 
2993   /* Otherwise check if we can remove the symbol itself and then verify
2994      that only uses of the comdat groups are direct call to THIS
2995      or its aliases.   */
2996   if (!can_remove_if_no_direct_calls_and_refs_p ())
2997     return false;
2998 
2999   /* Check that all refs come from within the comdat group.  */
3000   for (int i = 0; iterate_referring (i, ref); i++)
3001     if (ref->referring->get_comdat_group () != get_comdat_group ())
3002       return false;
3003 
3004   struct cgraph_node *target = ultimate_alias_target ();
3005   for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3006        next != this; next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3007     {
3008       if (!externally_visible)
3009 	continue;
3010       if (!next->alias
3011 	  && !next->can_remove_if_no_direct_calls_and_refs_p ())
3012 	return false;
3013 
3014       /* If we see different symbol than THIS, be sure to check calls.  */
3015       if (next->ultimate_alias_target () != target)
3016 	for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3017 	  if (e->caller->get_comdat_group () != get_comdat_group ()
3018 	      || will_inline)
3019 	    return false;
3020 
3021       /* If function is not being inlined, we care only about
3022 	 references outside of the comdat group.  */
3023       if (!will_inline)
3024 	for (int i = 0; next->iterate_referring (i, ref); i++)
3025 	  if (ref->referring->get_comdat_group () != get_comdat_group ())
3026 	    return false;
3027     }
3028   return true;
3029 }
3030 
3031 /* Return true when function cgraph_node can be expected to be removed
3032    from program when direct calls in this compilation unit are removed.
3033 
3034    As a special case COMDAT functions are
3035    cgraph_can_remove_if_no_direct_calls_p while the are not
3036    cgraph_only_called_directly_p (it is possible they are called from other
3037    unit)
3038 
3039    This function behaves as cgraph_only_called_directly_p because eliminating
3040    all uses of COMDAT function does not make it necessarily disappear from
3041    the program unless we are compiling whole program or we do LTO.  In this
3042    case we know we win since dynamic linking will not really discard the
3043    linkonce section.  */
3044 
3045 bool
will_be_removed_from_program_if_no_direct_calls_p(bool will_inline)3046 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p
3047 	 (bool will_inline)
3048 {
3049   gcc_assert (!inlined_to);
3050   if (DECL_EXTERNAL (decl))
3051     return true;
3052 
3053   if (!in_lto_p && !flag_whole_program)
3054     {
3055       /* If the symbol is in comdat group, we need to verify that whole comdat
3056 	 group becomes unreachable.  Technically we could skip references from
3057 	 within the group, too.  */
3058       if (!only_called_directly_p ())
3059 	return false;
3060       if (same_comdat_group && externally_visible)
3061 	{
3062 	  struct cgraph_node *target = ultimate_alias_target ();
3063 
3064 	  if (will_inline && address_taken)
3065 	    return true;
3066 	  for (cgraph_node *next = dyn_cast<cgraph_node *> (same_comdat_group);
3067 	       next != this;
3068 	       next = dyn_cast<cgraph_node *> (next->same_comdat_group))
3069 	    {
3070 	      if (!externally_visible)
3071 		continue;
3072 	      if (!next->alias
3073 		  && !next->only_called_directly_p ())
3074 		return false;
3075 
3076 	      /* If we see different symbol than THIS,
3077 		 be sure to check calls.  */
3078 	      if (next->ultimate_alias_target () != target)
3079 		for (cgraph_edge *e = next->callers; e; e = e->next_caller)
3080 		  if (e->caller->get_comdat_group () != get_comdat_group ()
3081 		      || will_inline)
3082 		    return false;
3083 	    }
3084 	}
3085       return true;
3086     }
3087   else
3088     return can_remove_if_no_direct_calls_p (will_inline);
3089 }
3090 
3091 
3092 /* Worker for cgraph_only_called_directly_p.  */
3093 
3094 static bool
cgraph_not_only_called_directly_p_1(cgraph_node * node,void *)3095 cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
3096 {
3097   return !node->only_called_directly_or_aliased_p ();
3098 }
3099 
3100 /* Return true when function cgraph_node and all its aliases are only called
3101    directly.
3102    i.e. it is not externally visible, address was not taken and
3103    it is not used in any other non-standard way.  */
3104 
3105 bool
only_called_directly_p(void)3106 cgraph_node::only_called_directly_p (void)
3107 {
3108   gcc_assert (ultimate_alias_target () == this);
3109   return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
3110 				       NULL, true);
3111 }
3112 
3113 
3114 /* Collect all callers of NODE.  Worker for collect_callers_of_node.  */
3115 
3116 static bool
collect_callers_of_node_1(cgraph_node * node,void * data)3117 collect_callers_of_node_1 (cgraph_node *node, void *data)
3118 {
3119   vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3120   cgraph_edge *cs;
3121   enum availability avail;
3122   node->ultimate_alias_target (&avail);
3123 
3124   if (avail > AVAIL_INTERPOSABLE)
3125     for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3126       if (!cs->indirect_inlining_edge
3127 	  && !cs->caller->thunk)
3128 	redirect_callers->safe_push (cs);
3129   return false;
3130 }
3131 
3132 /* Collect all callers of cgraph_node and its aliases that are known to lead to
3133    cgraph_node (i.e. are not overwritable).  */
3134 
3135 auto_vec<cgraph_edge *>
collect_callers(void)3136 cgraph_node::collect_callers (void)
3137 {
3138   auto_vec<cgraph_edge *> redirect_callers;
3139   call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
3140 				    &redirect_callers, false);
3141   return redirect_callers;
3142 }
3143 
3144 
3145 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it.  Return
3146    optimistically true if this cannot be determined.  */
3147 
3148 static bool
clone_of_p(cgraph_node * node,cgraph_node * node2)3149 clone_of_p (cgraph_node *node, cgraph_node *node2)
3150 {
3151   node = node->ultimate_alias_target ();
3152   node2 = node2->ultimate_alias_target ();
3153 
3154   if (node2->clone_of == node
3155       || node2->former_clone_of == node->decl)
3156     return true;
3157 
3158   if (!node->thunk && !node->former_thunk_p ())
3159     {
3160       while (node2
3161 	     && node->decl != node2->decl
3162 	     && node->decl != node2->former_clone_of)
3163 	node2 = node2->clone_of;
3164       return node2 != NULL;
3165     }
3166 
3167   /* There are no virtual clones of thunks so check former_clone_of or if we
3168      might have skipped thunks because this adjustments are no longer
3169      necessary.  */
3170   while (node->thunk || node->former_thunk_p ())
3171     {
3172       if (!thunk_info::get (node)->this_adjusting)
3173 	return false;
3174       /* In case of instrumented expanded thunks, which can have multiple calls
3175 	 in them, we do not know how to continue and just have to be
3176 	 optimistic.  The same applies if all calls have already been inlined
3177 	 into the thunk.  */
3178       if (!node->callees || node->callees->next_callee)
3179 	return true;
3180       node = node->callees->callee->ultimate_alias_target ();
3181 
3182       clone_info *info = clone_info::get (node2);
3183       if (!info || !info->param_adjustments
3184 	  || info->param_adjustments->first_param_intact_p ())
3185 	return false;
3186       if (node2->former_clone_of == node->decl
3187 	  || node2->former_clone_of == node->former_clone_of)
3188 	return true;
3189 
3190       cgraph_node *n2 = node2;
3191       while (n2 && node->decl != n2->decl)
3192 	n2 = n2->clone_of;
3193       if (n2)
3194 	return true;
3195     }
3196 
3197   return false;
3198 }
3199 
3200 /* Verify edge count and frequency.  */
3201 
3202 bool
verify_count()3203 cgraph_edge::verify_count ()
3204 {
3205   bool error_found = false;
3206   if (!count.verify ())
3207     {
3208       error ("caller edge count invalid");
3209       error_found = true;
3210     }
3211   return error_found;
3212 }
3213 
3214 /* Switch to THIS_CFUN if needed and print STMT to stderr.  */
3215 static void
cgraph_debug_gimple_stmt(function * this_cfun,gimple * stmt)3216 cgraph_debug_gimple_stmt (function *this_cfun, gimple *stmt)
3217 {
3218   bool fndecl_was_null = false;
3219   /* debug_gimple_stmt needs correct cfun */
3220   if (cfun != this_cfun)
3221     set_cfun (this_cfun);
3222   /* ...and an actual current_function_decl */
3223   if (!current_function_decl)
3224     {
3225       current_function_decl = this_cfun->decl;
3226       fndecl_was_null = true;
3227     }
3228   debug_gimple_stmt (stmt);
3229   if (fndecl_was_null)
3230     current_function_decl = NULL;
3231 }
3232 
3233 /* Verify that call graph edge corresponds to DECL from the associated
3234    statement.  Return true if the verification should fail.  */
3235 
3236 bool
verify_corresponds_to_fndecl(tree decl)3237 cgraph_edge::verify_corresponds_to_fndecl (tree decl)
3238 {
3239   cgraph_node *node;
3240 
3241   if (!decl || callee->inlined_to)
3242     return false;
3243   if (symtab->state == LTO_STREAMING)
3244     return false;
3245   node = cgraph_node::get (decl);
3246 
3247   /* We do not know if a node from a different partition is an alias or what it
3248      aliases and therefore cannot do the former_clone_of check reliably.  When
3249      body_removed is set, we have lost all information about what was alias or
3250      thunk of and also cannot proceed.  */
3251   if (!node
3252       || node->body_removed
3253       || node->in_other_partition
3254       || callee->icf_merged
3255       || callee->in_other_partition)
3256     return false;
3257 
3258   node = node->ultimate_alias_target ();
3259 
3260   /* Optimizers can redirect unreachable calls or calls triggering undefined
3261      behavior to builtin_unreachable.  */
3262 
3263   if (fndecl_built_in_p (callee->decl, BUILT_IN_UNREACHABLE))
3264     return false;
3265 
3266   if (callee->former_clone_of != node->decl
3267       && (node != callee->ultimate_alias_target ())
3268       && !clone_of_p (node, callee))
3269     return true;
3270   else
3271     return false;
3272 }
3273 
3274 /* Disable warnings about missing quoting in GCC diagnostics for
3275    the verification errors.  Their format strings don't follow GCC
3276    diagnostic conventions and the calls are ultimately followed by
3277    one to internal_error.  */
3278 #if __GNUC__ >= 10
3279 #  pragma GCC diagnostic push
3280 #  pragma GCC diagnostic ignored "-Wformat-diag"
3281 #endif
3282 
3283 /* Verify consistency of speculative call in NODE corresponding to STMT
3284    and LTO_STMT_UID.  If INDIRECT is set, assume that it is the indirect
3285    edge of call sequence. Return true if error is found.
3286 
3287    This function is called to every component of indirect call (direct edges,
3288    indirect edge and refs).  To save duplicated work, do full testing only
3289    in that case.  */
3290 static bool
verify_speculative_call(struct cgraph_node * node,gimple * stmt,unsigned int lto_stmt_uid,struct cgraph_edge * indirect)3291 verify_speculative_call (struct cgraph_node *node, gimple *stmt,
3292 			 unsigned int lto_stmt_uid,
3293 			 struct cgraph_edge *indirect)
3294 {
3295   if (indirect == NULL)
3296     {
3297       for (indirect = node->indirect_calls; indirect;
3298 	   indirect = indirect->next_callee)
3299 	if (indirect->call_stmt == stmt
3300 	    && indirect->lto_stmt_uid == lto_stmt_uid)
3301 	  break;
3302       if (!indirect)
3303 	{
3304 	  error ("missing indirect call in speculative call sequence");
3305 	  return true;
3306 	}
3307       if (!indirect->speculative)
3308 	{
3309 	  error ("indirect call in speculative call sequence has no "
3310 		 "speculative flag");
3311 	  return true;
3312 	}
3313       return false;
3314     }
3315 
3316   /* Maximal number of targets.  We probably will never want to have more than
3317      this.  */
3318   const unsigned int num = 256;
3319   cgraph_edge *direct_calls[num];
3320   ipa_ref *refs[num];
3321 
3322   for (unsigned int i = 0; i < num; i++)
3323     {
3324       direct_calls[i] = NULL;
3325       refs[i] = NULL;
3326     }
3327 
3328   cgraph_edge *first_call = NULL;
3329   cgraph_edge *prev_call = NULL;
3330 
3331   for (cgraph_edge *direct = node->callees; direct;
3332        direct = direct->next_callee)
3333     if (direct->call_stmt == stmt && direct->lto_stmt_uid == lto_stmt_uid)
3334       {
3335 	if (!first_call)
3336 	  first_call = direct;
3337 	if (prev_call && direct != prev_call->next_callee)
3338 	  {
3339 	    error ("speculative edges are not adjacent");
3340 	    return true;
3341 	  }
3342 	prev_call = direct;
3343 	if (!direct->speculative)
3344 	  {
3345 	    error ("direct call to %s in speculative call sequence has no "
3346 		   "speculative flag", direct->callee->dump_name ());
3347 	    return true;
3348 	  }
3349 	if (direct->speculative_id >= num)
3350 	  {
3351 	    error ("direct call to %s in speculative call sequence has "
3352 		   "speculative_id %i out of range",
3353 		   direct->callee->dump_name (), direct->speculative_id);
3354 	    return true;
3355 	  }
3356 	if (direct_calls[direct->speculative_id])
3357 	  {
3358 	    error ("duplicate direct call to %s in speculative call sequence "
3359 		   "with speculative_id %i",
3360 		   direct->callee->dump_name (), direct->speculative_id);
3361 	    return true;
3362 	  }
3363 	direct_calls[direct->speculative_id] = direct;
3364       }
3365 
3366   if (first_call->call_stmt
3367       && first_call != node->get_edge (first_call->call_stmt))
3368     {
3369       error ("call stmt hash does not point to first direct edge of "
3370 	     "speculative call sequence");
3371       return true;
3372     }
3373 
3374   ipa_ref *ref;
3375   for (int i = 0; node->iterate_reference (i, ref); i++)
3376     if (ref->speculative
3377 	&& ref->stmt == stmt && ref->lto_stmt_uid == lto_stmt_uid)
3378       {
3379 	if (ref->speculative_id >= num)
3380 	  {
3381 	    error ("direct call to %s in speculative call sequence has "
3382 		   "speculative_id %i out of range",
3383 		   ref->referred->dump_name (), ref->speculative_id);
3384 	    return true;
3385 	  }
3386 	if (refs[ref->speculative_id])
3387 	  {
3388 	    error ("duplicate reference %s in speculative call sequence "
3389 		   "with speculative_id %i",
3390 		   ref->referred->dump_name (), ref->speculative_id);
3391 	    return true;
3392 	  }
3393 	refs[ref->speculative_id] = ref;
3394       }
3395 
3396   int num_targets = 0;
3397   for (unsigned int i = 0 ; i < num ; i++)
3398     {
3399       if (refs[i] && !direct_calls[i])
3400 	{
3401 	  error ("missing direct call for speculation %i", i);
3402 	  return true;
3403 	}
3404       if (!refs[i] && direct_calls[i])
3405 	{
3406 	  error ("missing ref for speculation %i", i);
3407 	  return true;
3408 	}
3409       if (refs[i] != NULL)
3410 	num_targets++;
3411     }
3412 
3413   if (num_targets != indirect->num_speculative_call_targets_p ())
3414     {
3415       error ("number of speculative targets %i mismatched with "
3416 	     "num_speculative_call_targets %i",
3417 	     num_targets,
3418 	     indirect->num_speculative_call_targets_p ());
3419       return true;
3420     }
3421   return false;
3422 }
3423 
3424 /* Verify cgraph nodes of given cgraph node.  */
3425 DEBUG_FUNCTION void
verify_node(void)3426 cgraph_node::verify_node (void)
3427 {
3428   cgraph_edge *e;
3429   function *this_cfun = DECL_STRUCT_FUNCTION (decl);
3430   basic_block this_block;
3431   gimple_stmt_iterator gsi;
3432   bool error_found = false;
3433   int i;
3434   ipa_ref *ref = NULL;
3435 
3436   if (seen_error ())
3437     return;
3438 
3439   timevar_push (TV_CGRAPH_VERIFY);
3440   error_found |= verify_base ();
3441   for (e = callees; e; e = e->next_callee)
3442     if (e->aux)
3443       {
3444 	error ("aux field set for edge %s->%s",
3445 	       identifier_to_locale (e->caller->name ()),
3446 	       identifier_to_locale (e->callee->name ()));
3447 	error_found = true;
3448       }
3449   if (!count.verify ())
3450     {
3451       error ("cgraph count invalid");
3452       error_found = true;
3453     }
3454   if (inlined_to && same_comdat_group)
3455     {
3456       error ("inline clone in same comdat group list");
3457       error_found = true;
3458     }
3459   if (inlined_to && !count.compatible_p (inlined_to->count))
3460     {
3461       error ("inline clone count is not compatible");
3462       count.debug ();
3463       inlined_to->count.debug ();
3464       error_found = true;
3465     }
3466   if (tp_first_run < 0)
3467     {
3468       error ("tp_first_run must be non-negative");
3469       error_found = true;
3470     }
3471   if (!definition && !in_other_partition && local)
3472     {
3473       error ("local symbols must be defined");
3474       error_found = true;
3475     }
3476   if (inlined_to && externally_visible)
3477     {
3478       error ("externally visible inline clone");
3479       error_found = true;
3480     }
3481   if (inlined_to && address_taken)
3482     {
3483       error ("inline clone with address taken");
3484       error_found = true;
3485     }
3486   if (inlined_to && force_output)
3487     {
3488       error ("inline clone is forced to output");
3489       error_found = true;
3490     }
3491   if (symtab->state != LTO_STREAMING)
3492     {
3493       if (calls_comdat_local && !same_comdat_group)
3494 	{
3495 	  error ("calls_comdat_local is set outside of a comdat group");
3496 	  error_found = true;
3497 	}
3498       if (!inlined_to && calls_comdat_local != check_calls_comdat_local_p ())
3499 	{
3500 	  error ("invalid calls_comdat_local flag");
3501 	  error_found = true;
3502 	}
3503     }
3504   if (DECL_IS_MALLOC (decl)
3505       && !POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3506     {
3507       error ("malloc attribute should be used for a function that "
3508 	     "returns a pointer");
3509       error_found = true;
3510     }
3511   if (definition
3512       && externally_visible
3513       /* For aliases in lto1 free_lang_data doesn't guarantee preservation
3514 	 of opt_for_fn (decl, flag_semantic_interposition).  See PR105399.  */
3515       && (!alias || !in_lto_p)
3516       && semantic_interposition
3517 	 != opt_for_fn (decl, flag_semantic_interposition))
3518     {
3519       error ("semantic interposition mismatch");
3520       error_found = true;
3521     }
3522   for (e = indirect_calls; e; e = e->next_callee)
3523     {
3524       if (e->aux)
3525 	{
3526 	  error ("aux field set for indirect edge from %s",
3527 		 identifier_to_locale (e->caller->name ()));
3528 	  error_found = true;
3529 	}
3530       if (!e->count.compatible_p (count))
3531 	{
3532 	  error ("edge count is not compatible with function count");
3533 	  e->count.debug ();
3534 	  count.debug ();
3535 	  error_found = true;
3536 	}
3537       if (!e->indirect_unknown_callee
3538 	  || !e->indirect_info)
3539 	{
3540 	  error ("An indirect edge from %s is not marked as indirect or has "
3541 		 "associated indirect_info, the corresponding statement is: ",
3542 		 identifier_to_locale (e->caller->name ()));
3543 	  cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3544 	  error_found = true;
3545 	}
3546       if (e->call_stmt && e->lto_stmt_uid)
3547 	{
3548 	  error ("edge has both call_stmt and lto_stmt_uid set");
3549 	  error_found = true;
3550 	}
3551     }
3552   bool check_comdat = comdat_local_p ();
3553   for (e = callers; e; e = e->next_caller)
3554     {
3555       if (e->verify_count ())
3556 	error_found = true;
3557       if (check_comdat
3558 	  && !in_same_comdat_group_p (e->caller))
3559 	{
3560 	  error ("comdat-local function called by %s outside its comdat",
3561 		 identifier_to_locale (e->caller->name ()));
3562 	  error_found = true;
3563 	}
3564       if (!e->inline_failed)
3565 	{
3566 	  if (inlined_to
3567 	      != (e->caller->inlined_to
3568 		  ? e->caller->inlined_to : e->caller))
3569 	    {
3570 	      error ("inlined_to pointer is wrong");
3571 	      error_found = true;
3572 	    }
3573 	  if (callers->next_caller)
3574 	    {
3575 	      error ("multiple inline callers");
3576 	      error_found = true;
3577 	    }
3578 	}
3579       else
3580 	if (inlined_to)
3581 	  {
3582 	    error ("inlined_to pointer set for noninline callers");
3583 	    error_found = true;
3584 	  }
3585     }
3586   for (e = callees; e; e = e->next_callee)
3587     {
3588       if (e->verify_count ())
3589 	error_found = true;
3590       if (!e->count.compatible_p (count))
3591 	{
3592 	  error ("edge count is not compatible with function count");
3593 	  e->count.debug ();
3594 	  count.debug ();
3595 	  error_found = true;
3596 	}
3597       if (gimple_has_body_p (e->caller->decl)
3598 	  && !e->caller->inlined_to
3599 	  && !e->speculative
3600 	  /* Optimized out calls are redirected to __builtin_unreachable.  */
3601 	  && (e->count.nonzero_p ()
3602 	      || ! e->callee->decl
3603 	      || !fndecl_built_in_p (e->callee->decl, BUILT_IN_UNREACHABLE))
3604 	  && count
3605 	      == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3606 	  && (!e->count.ipa_p ()
3607 	      && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3608 	{
3609 	  error ("caller edge count does not match BB count");
3610 	  fprintf (stderr, "edge count: ");
3611 	  e->count.dump (stderr);
3612 	  fprintf (stderr, "\n bb count: ");
3613 	  gimple_bb (e->call_stmt)->count.dump (stderr);
3614 	  fprintf (stderr, "\n");
3615 	  error_found = true;
3616 	}
3617       if (e->call_stmt && e->lto_stmt_uid)
3618 	{
3619 	  error ("edge has both call_stmt and lto_stmt_uid set");
3620 	  error_found = true;
3621 	}
3622       if (e->speculative
3623 	  && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3624 				      NULL))
3625 	error_found = true;
3626     }
3627   for (e = indirect_calls; e; e = e->next_callee)
3628     {
3629       if (e->verify_count ())
3630 	error_found = true;
3631       if (gimple_has_body_p (e->caller->decl)
3632 	  && !e->caller->inlined_to
3633 	  && !e->speculative
3634 	  && e->count.ipa_p ()
3635 	  && count
3636 	      == ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count
3637 	  && (!e->count.ipa_p ()
3638 	      && e->count.differs_from_p (gimple_bb (e->call_stmt)->count)))
3639 	{
3640 	  error ("indirect call count does not match BB count");
3641 	  fprintf (stderr, "edge count: ");
3642 	  e->count.dump (stderr);
3643 	  fprintf (stderr, "\n bb count: ");
3644 	  gimple_bb (e->call_stmt)->count.dump (stderr);
3645 	  fprintf (stderr, "\n");
3646 	  error_found = true;
3647 	}
3648       if (e->speculative
3649 	  && verify_speculative_call (e->caller, e->call_stmt, e->lto_stmt_uid,
3650 				      e))
3651 	error_found = true;
3652     }
3653   for (i = 0; iterate_reference (i, ref); i++)
3654     {
3655       if (ref->stmt && ref->lto_stmt_uid)
3656 	{
3657 	  error ("reference has both stmt and lto_stmt_uid set");
3658 	  error_found = true;
3659 	}
3660       if (ref->speculative
3661 	  && verify_speculative_call (this, ref->stmt,
3662 				      ref->lto_stmt_uid, NULL))
3663 	error_found = true;
3664     }
3665 
3666   if (!callers && inlined_to)
3667     {
3668       error ("inlined_to pointer is set but no predecessors found");
3669       error_found = true;
3670     }
3671   if (inlined_to == this)
3672     {
3673       error ("inlined_to pointer refers to itself");
3674       error_found = true;
3675     }
3676 
3677   if (clone_of)
3678     {
3679       cgraph_node *first_clone = clone_of->clones;
3680       if (first_clone != this)
3681 	{
3682 	  if (prev_sibling_clone->clone_of != clone_of)
3683 	    {
3684 	      error ("cgraph_node has wrong clone_of");
3685 	      error_found = true;
3686 	    }
3687 	}
3688     }
3689   if (clones)
3690     {
3691       cgraph_node *n;
3692       for (n = clones; n; n = n->next_sibling_clone)
3693 	if (n->clone_of != this)
3694 	  break;
3695       if (n)
3696 	{
3697 	  error ("cgraph_node has wrong clone list");
3698 	  error_found = true;
3699 	}
3700     }
3701   if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
3702     {
3703        error ("cgraph_node is in clone list but it is not clone");
3704        error_found = true;
3705     }
3706   if (!prev_sibling_clone && clone_of && clone_of->clones != this)
3707     {
3708       error ("cgraph_node has wrong prev_clone pointer");
3709       error_found = true;
3710     }
3711   if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
3712     {
3713       error ("double linked list of clones corrupted");
3714       error_found = true;
3715     }
3716 
3717   if (analyzed && alias)
3718     {
3719       bool ref_found = false;
3720       int i;
3721       ipa_ref *ref = NULL;
3722 
3723       if (callees)
3724 	{
3725 	  error ("Alias has call edges");
3726 	  error_found = true;
3727 	}
3728       for (i = 0; iterate_reference (i, ref); i++)
3729 	if (ref->use != IPA_REF_ALIAS)
3730 	  {
3731 	    error ("Alias has non-alias reference");
3732 	    error_found = true;
3733 	  }
3734 	else if (ref_found)
3735 	  {
3736 	    error ("Alias has more than one alias reference");
3737 	    error_found = true;
3738 	  }
3739 	else
3740 	  ref_found = true;
3741       if (!ref_found)
3742 	{
3743 	  error ("Analyzed alias has no reference");
3744 	  error_found = true;
3745 	}
3746     }
3747 
3748   if (analyzed && thunk)
3749     {
3750       if (!callees)
3751 	{
3752 	  error ("No edge out of thunk node");
3753 	  error_found = true;
3754 	}
3755       else if (callees->next_callee)
3756 	{
3757 	  error ("More than one edge out of thunk node");
3758 	  error_found = true;
3759 	}
3760       if (gimple_has_body_p (decl) && !inlined_to)
3761 	{
3762 	  error ("Thunk is not supposed to have body");
3763 	  error_found = true;
3764 	}
3765     }
3766   else if (analyzed && gimple_has_body_p (decl)
3767 	   && !TREE_ASM_WRITTEN (decl)
3768 	   && (!DECL_EXTERNAL (decl) || inlined_to)
3769 	   && !flag_wpa)
3770     {
3771       if (this_cfun->cfg)
3772 	{
3773 	  hash_set<gimple *> stmts;
3774 
3775 	  /* Reach the trees by walking over the CFG, and note the
3776 	     enclosing basic-blocks in the call edges.  */
3777 	  FOR_EACH_BB_FN (this_block, this_cfun)
3778 	    {
3779 	      for (gsi = gsi_start_phis (this_block);
3780 		   !gsi_end_p (gsi); gsi_next (&gsi))
3781 		stmts.add (gsi_stmt (gsi));
3782 	      for (gsi = gsi_start_bb (this_block);
3783 		   !gsi_end_p (gsi);
3784 		   gsi_next (&gsi))
3785 		{
3786 		  gimple *stmt = gsi_stmt (gsi);
3787 		  stmts.add (stmt);
3788 		  if (is_gimple_call (stmt))
3789 		    {
3790 		      cgraph_edge *e = get_edge (stmt);
3791 		      tree decl = gimple_call_fndecl (stmt);
3792 		      if (e)
3793 			{
3794 			  if (e->aux)
3795 			    {
3796 			      error ("shared call_stmt:");
3797 			      cgraph_debug_gimple_stmt (this_cfun, stmt);
3798 			      error_found = true;
3799 			    }
3800 			  if (!e->indirect_unknown_callee)
3801 			    {
3802 			      if (e->verify_corresponds_to_fndecl (decl))
3803 				{
3804 				  error ("edge points to wrong declaration:");
3805 				  debug_tree (e->callee->decl);
3806 				  fprintf (stderr," Instead of:");
3807 				  debug_tree (decl);
3808 				  error_found = true;
3809 				}
3810 			    }
3811 			  else if (decl)
3812 			    {
3813 			      error ("an indirect edge with unknown callee "
3814 				     "corresponding to a call_stmt with "
3815 				     "a known declaration:");
3816 			      error_found = true;
3817 			      cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3818 			    }
3819 			  e->aux = (void *)1;
3820 			}
3821 		      else if (decl)
3822 			{
3823 			  error ("missing callgraph edge for call stmt:");
3824 			  cgraph_debug_gimple_stmt (this_cfun, stmt);
3825 			  error_found = true;
3826 			}
3827 		    }
3828 		}
3829 	      }
3830 	    for (i = 0; iterate_reference (i, ref); i++)
3831 	      if (ref->stmt && !stmts.contains (ref->stmt))
3832 		{
3833 		  error ("reference to dead statement");
3834 		  cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
3835 		  error_found = true;
3836 		}
3837 	}
3838       else
3839 	/* No CFG available?!  */
3840 	gcc_unreachable ();
3841 
3842       for (e = callees; e; e = e->next_callee)
3843 	{
3844 	  if (!e->aux && !e->speculative)
3845 	    {
3846 	      error ("edge %s->%s has no corresponding call_stmt",
3847 		     identifier_to_locale (e->caller->name ()),
3848 		     identifier_to_locale (e->callee->name ()));
3849 	      cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3850 	      error_found = true;
3851 	    }
3852 	  e->aux = 0;
3853 	}
3854       for (e = indirect_calls; e; e = e->next_callee)
3855 	{
3856 	  if (!e->aux && !e->speculative)
3857 	    {
3858 	      error ("an indirect edge from %s has no corresponding call_stmt",
3859 		     identifier_to_locale (e->caller->name ()));
3860 	      cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
3861 	      error_found = true;
3862 	    }
3863 	  e->aux = 0;
3864 	}
3865     }
3866 
3867   if (nested_function_info *info = nested_function_info::get (this))
3868     {
3869       if (info->nested != NULL)
3870 	{
3871 	  for (cgraph_node *n = info->nested; n != NULL;
3872 	       n = next_nested_function (n))
3873 	    {
3874 	      nested_function_info *ninfo = nested_function_info::get (n);
3875 	      if (ninfo->origin == NULL)
3876 		{
3877 		  error ("missing origin for a node in a nested list");
3878 		  error_found = true;
3879 		}
3880 	      else if (ninfo->origin != this)
3881 		{
3882 		  error ("origin points to a different parent");
3883 		  error_found = true;
3884 		  break;
3885 		}
3886 	    }
3887 	}
3888       if (info->next_nested != NULL && info->origin == NULL)
3889 	{
3890 	  error ("missing origin for a node in a nested list");
3891 	  error_found = true;
3892 	}
3893     }
3894 
3895   if (error_found)
3896     {
3897       dump (stderr);
3898       internal_error ("verify_cgraph_node failed");
3899     }
3900   timevar_pop (TV_CGRAPH_VERIFY);
3901 }
3902 
3903 /* Verify whole cgraph structure.  */
3904 DEBUG_FUNCTION void
verify_cgraph_nodes(void)3905 cgraph_node::verify_cgraph_nodes (void)
3906 {
3907   cgraph_node *node;
3908 
3909   if (seen_error ())
3910     return;
3911 
3912   FOR_EACH_FUNCTION (node)
3913     node->verify ();
3914 }
3915 
3916 #if __GNUC__ >= 10
3917 #  pragma GCC diagnostic pop
3918 #endif
3919 
3920 /* Walk the alias chain to return the function cgraph_node is alias of.
3921    Walk through thunks, too.
3922    When AVAILABILITY is non-NULL, get minimal availability in the chain.
3923    When REF is non-NULL, assume that reference happens in symbol REF
3924    when determining the availability.  */
3925 
3926 cgraph_node *
function_symbol(enum availability * availability,struct symtab_node * ref)3927 cgraph_node::function_symbol (enum availability *availability,
3928 			      struct symtab_node *ref)
3929 {
3930   cgraph_node *node = ultimate_alias_target (availability, ref);
3931 
3932   while (node->thunk)
3933     {
3934       enum availability a;
3935 
3936       ref = node;
3937       node = node->callees->callee;
3938       node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3939       if (availability && a < *availability)
3940 	*availability = a;
3941     }
3942   return node;
3943 }
3944 
3945 /* Walk the alias chain to return the function cgraph_node is alias of.
3946    Walk through non virtual thunks, too.  Thus we return either a function
3947    or a virtual thunk node.
3948    When AVAILABILITY is non-NULL, get minimal availability in the chain.
3949    When REF is non-NULL, assume that reference happens in symbol REF
3950    when determining the availability.  */
3951 
3952 cgraph_node *
function_or_virtual_thunk_symbol(enum availability * availability,struct symtab_node * ref)3953 cgraph_node::function_or_virtual_thunk_symbol
3954 				(enum availability *availability,
3955 				 struct symtab_node *ref)
3956 {
3957   cgraph_node *node = ultimate_alias_target (availability, ref);
3958 
3959   while (node->thunk && !thunk_info::get (node)->virtual_offset_p)
3960     {
3961       enum availability a;
3962 
3963       ref = node;
3964       node = node->callees->callee;
3965       node = node->ultimate_alias_target (availability ? &a : NULL, ref);
3966       if (availability && a < *availability)
3967 	*availability = a;
3968     }
3969   return node;
3970 }
3971 
3972 /* When doing LTO, read cgraph_node's body from disk if it is not already
3973    present.  Also perform any necessary clone materializations.  */
3974 
3975 bool
get_untransformed_body()3976 cgraph_node::get_untransformed_body ()
3977 {
3978   lto_file_decl_data *file_data;
3979   const char *data, *name;
3980   size_t len;
3981   tree decl = this->decl;
3982 
3983   /* See if there is clone to be materialized.
3984      (inline clones does not need materialization, but we can be seeing
3985       an inline clone of real clone).  */
3986   cgraph_node *p = this;
3987   for (cgraph_node *c = clone_of; c; c = c->clone_of)
3988     {
3989       if (c->decl != decl)
3990 	p->materialize_clone ();
3991       p = c;
3992     }
3993 
3994   /* Check if body is already there.  Either we have gimple body or
3995      the function is thunk and in that case we set DECL_ARGUMENTS.  */
3996   if (DECL_ARGUMENTS (decl) || gimple_has_body_p (decl))
3997     return false;
3998 
3999   gcc_assert (in_lto_p && !DECL_RESULT (decl));
4000 
4001   timevar_push (TV_IPA_LTO_GIMPLE_IN);
4002 
4003   file_data = lto_file_data;
4004   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4005 
4006   /* We may have renamed the declaration, e.g., a static function.  */
4007   name = lto_get_decl_name_mapping (file_data, name);
4008   struct lto_in_decl_state *decl_state
4009 	 = lto_get_function_in_decl_state (file_data, decl);
4010 
4011   cgraph_node *origin = this;
4012   while (origin->clone_of)
4013     origin = origin->clone_of;
4014 
4015   int stream_order = origin->order - file_data->order_base;
4016   data = lto_get_section_data (file_data, LTO_section_function_body,
4017 			       name, stream_order, &len,
4018 			       decl_state->compressed);
4019   if (!data)
4020     fatal_error (input_location, "%s: section %s.%d is missing",
4021 		 file_data->file_name, name, stream_order);
4022 
4023   gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
4024 
4025   if (!quiet_flag)
4026     fprintf (stderr, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4027   lto_input_function_body (file_data, this, data);
4028   lto_stats.num_function_bodies++;
4029   lto_free_section_data (file_data, LTO_section_function_body, name,
4030 			 data, len, decl_state->compressed);
4031   lto_free_function_in_decl_state_for_node (this);
4032   /* Keep lto file data so ipa-inline-analysis knows about cross module
4033      inlining.  */
4034 
4035   timevar_pop (TV_IPA_LTO_GIMPLE_IN);
4036 
4037   return true;
4038 }
4039 
4040 /* Prepare function body.  When doing LTO, read cgraph_node's body from disk
4041    if it is not already present.  When some IPA transformations are scheduled,
4042    apply them.  */
4043 
4044 bool
get_body(void)4045 cgraph_node::get_body (void)
4046 {
4047   bool updated;
4048 
4049   updated = get_untransformed_body ();
4050 
4051   /* Getting transformed body makes no sense for inline clones;
4052      we should never use this on real clones because they are materialized
4053      early.
4054      TODO: Materializing clones here will likely lead to smaller LTRANS
4055      footprint. */
4056   gcc_assert (!inlined_to && !clone_of);
4057   if (ipa_transforms_to_apply.exists ())
4058     {
4059       opt_pass *saved_current_pass = current_pass;
4060       FILE *saved_dump_file = dump_file;
4061       const char *saved_dump_file_name = dump_file_name;
4062       dump_flags_t saved_dump_flags = dump_flags;
4063       dump_file_name = NULL;
4064       set_dump_file (NULL);
4065 
4066       push_cfun (DECL_STRUCT_FUNCTION (decl));
4067 
4068       update_ssa (TODO_update_ssa_only_virtuals);
4069       execute_all_ipa_transforms (true);
4070       cgraph_edge::rebuild_edges ();
4071       free_dominance_info (CDI_DOMINATORS);
4072       free_dominance_info (CDI_POST_DOMINATORS);
4073       pop_cfun ();
4074       updated = true;
4075 
4076       current_pass = saved_current_pass;
4077       set_dump_file (saved_dump_file);
4078       dump_file_name = saved_dump_file_name;
4079       dump_flags = saved_dump_flags;
4080     }
4081   return updated;
4082 }
4083 
4084 /* Return the DECL_STRUCT_FUNCTION of the function.  */
4085 
4086 struct function *
get_fun() const4087 cgraph_node::get_fun () const
4088 {
4089   const cgraph_node *node = this;
4090   struct function *fun = DECL_STRUCT_FUNCTION (node->decl);
4091 
4092   while (!fun && node->clone_of)
4093     {
4094       node = node->clone_of;
4095       fun = DECL_STRUCT_FUNCTION (node->decl);
4096     }
4097 
4098   return fun;
4099 }
4100 
4101 /* Reset all state within cgraph.cc so that we can rerun the compiler
4102    within the same process.  For use by toplev::finalize.  */
4103 
4104 void
cgraph_cc_finalize(void)4105 cgraph_cc_finalize (void)
4106 {
4107   nested_function_info::release ();
4108   thunk_info::release ();
4109   clone_info::release ();
4110   symtab = NULL;
4111 
4112   x_cgraph_nodes_queue = NULL;
4113 
4114   cgraph_fnver_htab = NULL;
4115   version_info_node = NULL;
4116 }
4117 
4118 /* A worker for call_for_symbol_and_aliases.  */
4119 
4120 bool
call_for_symbol_and_aliases_1(bool (* callback)(cgraph_node *,void *),void * data,bool include_overwritable)4121 cgraph_node::call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
4122 							      void *),
4123 					    void *data,
4124 					    bool include_overwritable)
4125 {
4126   ipa_ref *ref;
4127   FOR_EACH_ALIAS (this, ref)
4128     {
4129       cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
4130       if (include_overwritable
4131 	  || alias->get_availability () > AVAIL_INTERPOSABLE)
4132 	if (alias->call_for_symbol_and_aliases (callback, data,
4133 						include_overwritable))
4134 	  return true;
4135     }
4136   return false;
4137 }
4138 
4139 /* Return true if NODE has thunk.  */
4140 
4141 bool
has_thunk_p(cgraph_node * node,void *)4142 cgraph_node::has_thunk_p (cgraph_node *node, void *)
4143 {
4144   for (cgraph_edge *e = node->callers; e; e = e->next_caller)
4145     if (e->caller->thunk)
4146       return true;
4147   return false;
4148 }
4149 
4150 /* Expected frequency of executions within the function.  */
4151 
4152 sreal
sreal_frequency()4153 cgraph_edge::sreal_frequency ()
4154 {
4155   return count.to_sreal_scale (caller->inlined_to
4156 			       ? caller->inlined_to->count
4157 			       : caller->count);
4158 }
4159 
4160 
4161 /* During LTO stream in this can be used to check whether call can possibly
4162    be internal to the current translation unit.  */
4163 
4164 bool
possibly_call_in_translation_unit_p(void)4165 cgraph_edge::possibly_call_in_translation_unit_p (void)
4166 {
4167   gcc_checking_assert (in_lto_p && caller->prevailing_p ());
4168 
4169   /* While incremental linking we may end up getting function body later.  */
4170   if (flag_incremental_link == INCREMENTAL_LINK_LTO)
4171     return true;
4172 
4173   /* We may be smarter here and avoid streaming in indirect calls we can't
4174      track, but that would require arranging streaming the indirect call
4175      summary first.  */
4176   if (!callee)
4177     return true;
4178 
4179   /* If callee is local to the original translation unit, it will be
4180      defined.  */
4181   if (!TREE_PUBLIC (callee->decl) && !DECL_EXTERNAL (callee->decl))
4182     return true;
4183 
4184   /* Otherwise we need to lookup prevailing symbol (symbol table is not merged,
4185      yet) and see if it is a definition.  In fact we may also resolve aliases,
4186      but that is probably not too important.  */
4187   symtab_node *node = callee;
4188   for (int n = 10; node->previous_sharing_asm_name && n ; n--)
4189     node = node->previous_sharing_asm_name;
4190   if (node->previous_sharing_asm_name)
4191     node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (callee->decl));
4192   gcc_assert (TREE_PUBLIC (node->decl));
4193   return node->get_availability () >= AVAIL_INTERPOSABLE;
4194 }
4195 
4196 /* Return num_speculative_targets of this edge.  */
4197 
4198 int
num_speculative_call_targets_p(void)4199 cgraph_edge::num_speculative_call_targets_p (void)
4200 {
4201   return indirect_info ? indirect_info->num_speculative_call_targets : 0;
4202 }
4203 
4204 /* Check if function calls comdat local.  This is used to recompute
4205    calls_comdat_local flag after function transformations.  */
4206 bool
check_calls_comdat_local_p()4207 cgraph_node::check_calls_comdat_local_p ()
4208 {
4209   for (cgraph_edge *e = callees; e; e = e->next_callee)
4210     if (e->inline_failed
4211 	? e->callee->comdat_local_p ()
4212 	: e->callee->check_calls_comdat_local_p ())
4213       return true;
4214   return false;
4215 }
4216 
4217 /* Return true if this node represents a former, i.e. an expanded, thunk.  */
4218 
4219 bool
former_thunk_p(void)4220 cgraph_node::former_thunk_p (void)
4221 {
4222   if (thunk)
4223     return false;
4224   thunk_info *i = thunk_info::get (this);
4225   if (!i)
4226     return false;
4227   gcc_checking_assert (i->fixed_offset || i->virtual_offset_p
4228 		       || i->indirect_offset);
4229   return true;
4230 }
4231 
4232 /* A stashed copy of "symtab" for use by selftest::symbol_table_test.
4233    This needs to be a global so that it can be a GC root, and thus
4234    prevent the stashed copy from being garbage-collected if the GC runs
4235    during a symbol_table_test.  */
4236 
4237 symbol_table *saved_symtab;
4238 
4239 #if CHECKING_P
4240 
4241 namespace selftest {
4242 
4243 /* class selftest::symbol_table_test.  */
4244 
4245 /* Constructor.  Store the old value of symtab, and create a new one.  */
4246 
symbol_table_test()4247 symbol_table_test::symbol_table_test ()
4248 {
4249   gcc_assert (saved_symtab == NULL);
4250   saved_symtab = symtab;
4251   symtab = new (ggc_alloc<symbol_table> ()) symbol_table ();
4252 }
4253 
4254 /* Destructor.  Restore the old value of symtab.  */
4255 
~symbol_table_test()4256 symbol_table_test::~symbol_table_test ()
4257 {
4258   gcc_assert (saved_symtab != NULL);
4259   symtab = saved_symtab;
4260   saved_symtab = NULL;
4261 }
4262 
4263 /* Verify that symbol_table_test works.  */
4264 
4265 static void
test_symbol_table_test()4266 test_symbol_table_test ()
4267 {
4268   /* Simulate running two selftests involving symbol tables.  */
4269   for (int i = 0; i < 2; i++)
4270     {
4271       symbol_table_test stt;
4272       tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
4273 				   get_identifier ("test_decl"),
4274 				   build_function_type_list (void_type_node,
4275 							     NULL_TREE));
4276       cgraph_node *node = cgraph_node::get_create (test_decl);
4277       gcc_assert (node);
4278 
4279       /* Verify that the node has order 0 on both iterations,
4280 	 and thus that nodes have predictable dump names in selftests.  */
4281       ASSERT_EQ (node->order, 0);
4282       ASSERT_STREQ (node->dump_name (), "test_decl/0");
4283     }
4284 }
4285 
4286 /* Run all of the selftests within this file.  */
4287 
4288 void
cgraph_cc_tests()4289 cgraph_cc_tests ()
4290 {
4291   test_symbol_table_test ();
4292 }
4293 
4294 } // namespace selftest
4295 
4296 #endif /* CHECKING_P */
4297 
4298 #include "gt-cgraph.h"
4299