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