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