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