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