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