Debian Codegraph @

gcc/cgraph.cc

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