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