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