]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraph.c
re PR ipa/61144 (Invalid optimizations for extern vars with local weak definitions)
[thirdparty/gcc.git] / gcc / cgraph.c
CommitLineData
e72fcfe8 1/* Callgraph handling code.
23a5b65a 2 Copyright (C) 2003-2014 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
9c8305f8
JH
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
18c6ada9 25
e72fcfe8
JH
26#include "config.h"
27#include "system.h"
28#include "coretypes.h"
29#include "tm.h"
30#include "tree.h"
d8a2d370
DN
31#include "varasm.h"
32#include "calls.h"
33#include "print-tree.h"
cd9c7bd2 34#include "tree-inline.h"
e72fcfe8
JH
35#include "langhooks.h"
36#include "hashtab.h"
6e2830c3 37#include "hash-set.h"
e72fcfe8
JH
38#include "toplev.h"
39#include "flags.h"
e72fcfe8
JH
40#include "debug.h"
41#include "target.h"
1c4a429a 42#include "cgraph.h"
dc0bfe6a 43#include "intl.h"
2fb9a547
AM
44#include "tree-ssa-alias.h"
45#include "internal-fn.h"
46#include "tree-eh.h"
47#include "gimple-expr.h"
726a989a 48#include "gimple.h"
5be5c238 49#include "gimple-iterator.h"
7ee2468b
SB
50#include "timevar.h"
51#include "dumpfile.h"
442b4905
AM
52#include "gimple-ssa.h"
53#include "cgraph.h"
54#include "tree-cfg.h"
7a300452 55#include "tree-ssa.h"
a63f2942 56#include "value-prof.h"
f9417da1 57#include "except.h"
1da2ed5f 58#include "diagnostic-core.h"
715a4e08 59#include "rtl.h"
a940b4d9 60#include "ipa-utils.h"
99fecd47 61#include "lto-streamer.h"
e7f23018 62#include "ipa-inline.h"
7d776ee2 63#include "cfgloop.h"
9c8305f8 64#include "gimple-pretty-print.h"
23a04216
JH
65#include "expr.h"
66#include "tree-dfa.h"
988d1653 67
7ee2468b
SB
68/* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
69#include "tree-pass.h"
70
1668aabc 71/* Queue of cgraph nodes scheduled to be lowered. */
5e20cdc9 72symtab_node *x_cgraph_nodes_queue;
3dafb85c 73#define cgraph_nodes_queue ((cgraph_node *)x_cgraph_nodes_queue)
f45e0ad1 74
3dafb85c
ML
75/* Symbol table global context. */
76symbol_table *symtab;
cd9c7bd2 77
61502ca8 78/* List of hooks triggered on cgraph_edge events. */
9088c1cc
MJ
79struct cgraph_edge_hook_list {
80 cgraph_edge_hook hook;
81 void *data;
82 struct cgraph_edge_hook_list *next;
83};
84
61502ca8 85/* List of hooks triggered on cgraph_node events. */
9088c1cc
MJ
86struct cgraph_node_hook_list {
87 cgraph_node_hook hook;
88 void *data;
89 struct cgraph_node_hook_list *next;
90};
91
61502ca8 92/* List of hooks triggered on events involving two cgraph_edges. */
9088c1cc
MJ
93struct cgraph_2edge_hook_list {
94 cgraph_2edge_hook hook;
95 void *data;
96 struct cgraph_2edge_hook_list *next;
97};
98
61502ca8 99/* List of hooks triggered on events involving two cgraph_nodes. */
9088c1cc
MJ
100struct cgraph_2node_hook_list {
101 cgraph_2node_hook hook;
102 void *data;
103 struct cgraph_2node_hook_list *next;
104};
105
3649b9b7
ST
106/* Map a cgraph_node to cgraph_function_version_info using this htab.
107 The cgraph_function_version_info has a THIS_NODE field that is the
108 corresponding cgraph_node.. */
109
3dafb85c 110static GTY((param_is (cgraph_function_version_info))) htab_t
3649b9b7
ST
111 cgraph_fnver_htab = NULL;
112
113/* Hash function for cgraph_fnver_htab. */
114static hashval_t
115cgraph_fnver_htab_hash (const void *ptr)
116{
3dafb85c 117 int uid = ((const cgraph_function_version_info *)ptr)->this_node->uid;
3649b9b7
ST
118 return (hashval_t)(uid);
119}
120
121/* eq function for cgraph_fnver_htab. */
122static int
123cgraph_fnver_htab_eq (const void *p1, const void *p2)
124{
3dafb85c
ML
125 const cgraph_function_version_info *n1
126 = (const cgraph_function_version_info *)p1;
127 const cgraph_function_version_info *n2
128 = (const cgraph_function_version_info *)p2;
3649b9b7
ST
129
130 return n1->this_node->uid == n2->this_node->uid;
131}
132
133/* Mark as GC root all allocated nodes. */
134static GTY(()) struct cgraph_function_version_info *
135 version_info_node = NULL;
136
137/* Get the cgraph_function_version_info node corresponding to node. */
3dafb85c 138cgraph_function_version_info *
d52f5295 139cgraph_node::function_version (void)
3649b9b7 140{
3dafb85c
ML
141 cgraph_function_version_info *ret;
142 cgraph_function_version_info key;
d52f5295 143 key.this_node = this;
3649b9b7
ST
144
145 if (cgraph_fnver_htab == NULL)
146 return NULL;
147
3dafb85c 148 ret = (cgraph_function_version_info *)
3649b9b7
ST
149 htab_find (cgraph_fnver_htab, &key);
150
151 return ret;
152}
153
154/* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
155 corresponding to cgraph_node NODE. */
3dafb85c 156cgraph_function_version_info *
d52f5295 157cgraph_node::insert_new_function_version (void)
3649b9b7
ST
158{
159 void **slot;
160
161 version_info_node = NULL;
766090c2 162 version_info_node = ggc_cleared_alloc<cgraph_function_version_info> ();
d52f5295 163 version_info_node->this_node = this;
3649b9b7
ST
164
165 if (cgraph_fnver_htab == NULL)
166 cgraph_fnver_htab = htab_create_ggc (2, cgraph_fnver_htab_hash,
167 cgraph_fnver_htab_eq, NULL);
168
169 slot = htab_find_slot (cgraph_fnver_htab, version_info_node, INSERT);
170 gcc_assert (slot != NULL);
171 *slot = version_info_node;
172 return version_info_node;
173}
174
175/* Remove the cgraph_function_version_info and cgraph_node for DECL. This
176 DECL is a duplicate declaration. */
177void
d52f5295 178cgraph_node::delete_function_version (tree decl)
3649b9b7 179{
3dafb85c
ML
180 cgraph_node *decl_node = cgraph_node::get (decl);
181 cgraph_function_version_info *decl_v = NULL;
3649b9b7
ST
182
183 if (decl_node == NULL)
184 return;
185
d52f5295 186 decl_v = decl_node->function_version ();
3649b9b7
ST
187
188 if (decl_v == NULL)
189 return;
190
191 if (decl_v->prev != NULL)
192 decl_v->prev->next = decl_v->next;
193
194 if (decl_v->next != NULL)
195 decl_v->next->prev = decl_v->prev;
196
197 if (cgraph_fnver_htab != NULL)
198 htab_remove_elt (cgraph_fnver_htab, decl_v);
199
d52f5295 200 decl_node->remove ();
3649b9b7
ST
201}
202
203/* Record that DECL1 and DECL2 are semantically identical function
204 versions. */
205void
d52f5295 206cgraph_node::record_function_versions (tree decl1, tree decl2)
3649b9b7 207{
3dafb85c
ML
208 cgraph_node *decl1_node = cgraph_node::get_create (decl1);
209 cgraph_node *decl2_node = cgraph_node::get_create (decl2);
210 cgraph_function_version_info *decl1_v = NULL;
211 cgraph_function_version_info *decl2_v = NULL;
212 cgraph_function_version_info *before;
213 cgraph_function_version_info *after;
3649b9b7
ST
214
215 gcc_assert (decl1_node != NULL && decl2_node != NULL);
d52f5295
ML
216 decl1_v = decl1_node->function_version ();
217 decl2_v = decl2_node->function_version ();
3649b9b7
ST
218
219 if (decl1_v != NULL && decl2_v != NULL)
220 return;
221
222 if (decl1_v == NULL)
d52f5295 223 decl1_v = decl1_node->insert_new_function_version ();
3649b9b7
ST
224
225 if (decl2_v == NULL)
d52f5295 226 decl2_v = decl2_node->insert_new_function_version ();
3649b9b7
ST
227
228 /* Chain decl2_v and decl1_v. All semantically identical versions
229 will be chained together. */
230
231 before = decl1_v;
232 after = decl2_v;
233
234 while (before->next != NULL)
235 before = before->next;
236
237 while (after->prev != NULL)
238 after= after->prev;
239
240 before->next = after;
241 after->prev = before;
242}
243
9088c1cc 244/* Register HOOK to be called with DATA on each removed edge. */
3dafb85c
ML
245cgraph_edge_hook_list *
246symbol_table::add_edge_removal_hook (cgraph_edge_hook hook, void *data)
9088c1cc 247{
3dafb85c
ML
248 cgraph_edge_hook_list *entry;
249 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
9088c1cc 250
3dafb85c 251 entry = (cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
9088c1cc
MJ
252 entry->hook = hook;
253 entry->data = data;
254 entry->next = NULL;
255 while (*ptr)
256 ptr = &(*ptr)->next;
257 *ptr = entry;
258 return entry;
259}
260
261/* Remove ENTRY from the list of hooks called on removing edges. */
262void
3dafb85c 263symbol_table::remove_edge_removal_hook (cgraph_edge_hook_list *entry)
9088c1cc 264{
3dafb85c 265 cgraph_edge_hook_list **ptr = &m_first_edge_removal_hook;
9088c1cc
MJ
266
267 while (*ptr != entry)
268 ptr = &(*ptr)->next;
269 *ptr = entry->next;
934cb78a 270 free (entry);
9088c1cc
MJ
271}
272
273/* Call all edge removal hooks. */
3dafb85c
ML
274void
275symbol_table::call_edge_removal_hooks (cgraph_edge *e)
9088c1cc 276{
3dafb85c 277 cgraph_edge_hook_list *entry = m_first_edge_removal_hook;
9088c1cc
MJ
278 while (entry)
279 {
280 entry->hook (e, entry->data);
281 entry = entry->next;
282 }
283}
284
285/* Register HOOK to be called with DATA on each removed node. */
3dafb85c
ML
286cgraph_node_hook_list *
287symbol_table::add_cgraph_removal_hook (cgraph_node_hook hook, void *data)
9088c1cc 288{
3dafb85c
ML
289 cgraph_node_hook_list *entry;
290 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
9088c1cc 291
3dafb85c 292 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
9088c1cc
MJ
293 entry->hook = hook;
294 entry->data = data;
295 entry->next = NULL;
296 while (*ptr)
297 ptr = &(*ptr)->next;
298 *ptr = entry;
299 return entry;
300}
301
302/* Remove ENTRY from the list of hooks called on removing nodes. */
303void
3dafb85c 304symbol_table::remove_cgraph_removal_hook (cgraph_node_hook_list *entry)
9088c1cc 305{
3dafb85c 306 cgraph_node_hook_list **ptr = &m_first_cgraph_removal_hook;
9088c1cc
MJ
307
308 while (*ptr != entry)
309 ptr = &(*ptr)->next;
310 *ptr = entry->next;
934cb78a 311 free (entry);
9088c1cc
MJ
312}
313
314/* Call all node removal hooks. */
3dafb85c
ML
315void
316symbol_table::call_cgraph_removal_hooks (cgraph_node *node)
9088c1cc 317{
3dafb85c 318 cgraph_node_hook_list *entry = m_first_cgraph_removal_hook;
9088c1cc
MJ
319 while (entry)
320 {
321 entry->hook (node, entry->data);
322 entry = entry->next;
323 }
324}
325
3dafb85c
ML
326/* Call all node removal hooks. */
327void
328symbol_table::call_cgraph_insertion_hooks (cgraph_node *node)
329{
330 cgraph_node_hook_list *entry = m_first_cgraph_insertion_hook;
331 while (entry)
332 {
333 entry->hook (node, entry->data);
334 entry = entry->next;
335 }
336}
337
338
6544865a 339/* Register HOOK to be called with DATA on each inserted node. */
3dafb85c
ML
340cgraph_node_hook_list *
341symbol_table::add_cgraph_insertion_hook (cgraph_node_hook hook, void *data)
129a37fc 342{
3dafb85c
ML
343 cgraph_node_hook_list *entry;
344 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
129a37fc 345
3dafb85c 346 entry = (cgraph_node_hook_list *) xmalloc (sizeof (*entry));
129a37fc
JH
347 entry->hook = hook;
348 entry->data = data;
349 entry->next = NULL;
350 while (*ptr)
351 ptr = &(*ptr)->next;
352 *ptr = entry;
353 return entry;
354}
355
6544865a 356/* Remove ENTRY from the list of hooks called on inserted nodes. */
129a37fc 357void
3dafb85c 358symbol_table::remove_cgraph_insertion_hook (cgraph_node_hook_list *entry)
129a37fc 359{
3dafb85c 360 cgraph_node_hook_list **ptr = &m_first_cgraph_insertion_hook;
129a37fc
JH
361
362 while (*ptr != entry)
363 ptr = &(*ptr)->next;
364 *ptr = entry->next;
934cb78a 365 free (entry);
129a37fc
JH
366}
367
9088c1cc 368/* Register HOOK to be called with DATA on each duplicated edge. */
3dafb85c
ML
369cgraph_2edge_hook_list *
370symbol_table::add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
9088c1cc 371{
3dafb85c
ML
372 cgraph_2edge_hook_list *entry;
373 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
9088c1cc 374
3dafb85c 375 entry = (cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
9088c1cc
MJ
376 entry->hook = hook;
377 entry->data = data;
378 entry->next = NULL;
379 while (*ptr)
380 ptr = &(*ptr)->next;
381 *ptr = entry;
382 return entry;
383}
384
385/* Remove ENTRY from the list of hooks called on duplicating edges. */
386void
3dafb85c 387symbol_table::remove_edge_duplication_hook (cgraph_2edge_hook_list *entry)
9088c1cc 388{
3dafb85c 389 cgraph_2edge_hook_list **ptr = &m_first_edge_duplicated_hook;
9088c1cc
MJ
390
391 while (*ptr != entry)
392 ptr = &(*ptr)->next;
393 *ptr = entry->next;
934cb78a 394 free (entry);
9088c1cc
MJ
395}
396
397/* Call all edge duplication hooks. */
66a20fc2 398void
3dafb85c 399symbol_table::call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2)
9088c1cc 400{
3dafb85c 401 cgraph_2edge_hook_list *entry = m_first_edge_duplicated_hook;
9088c1cc
MJ
402 while (entry)
403 {
404 entry->hook (cs1, cs2, entry->data);
405 entry = entry->next;
406 }
407}
408
409/* Register HOOK to be called with DATA on each duplicated node. */
3dafb85c
ML
410cgraph_2node_hook_list *
411symbol_table::add_cgraph_duplication_hook (cgraph_2node_hook hook, void *data)
9088c1cc 412{
3dafb85c
ML
413 cgraph_2node_hook_list *entry;
414 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
9088c1cc 415
3dafb85c 416 entry = (cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
9088c1cc
MJ
417 entry->hook = hook;
418 entry->data = data;
419 entry->next = NULL;
420 while (*ptr)
421 ptr = &(*ptr)->next;
422 *ptr = entry;
423 return entry;
424}
425
426/* Remove ENTRY from the list of hooks called on duplicating nodes. */
427void
3dafb85c 428symbol_table::remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry)
9088c1cc 429{
3dafb85c 430 cgraph_2node_hook_list **ptr = &m_first_cgraph_duplicated_hook;
9088c1cc
MJ
431
432 while (*ptr != entry)
433 ptr = &(*ptr)->next;
434 *ptr = entry->next;
934cb78a 435 free (entry);
9088c1cc
MJ
436}
437
438/* Call all node duplication hooks. */
b0d0a291 439void
3dafb85c
ML
440symbol_table::call_cgraph_duplication_hooks (cgraph_node *node,
441 cgraph_node *node2)
9088c1cc 442{
3dafb85c 443 cgraph_2node_hook_list *entry = m_first_cgraph_duplicated_hook;
9088c1cc
MJ
444 while (entry)
445 {
3dafb85c 446 entry->hook (node, node2, entry->data);
9088c1cc
MJ
447 entry = entry->next;
448 }
449}
450
e72fcfe8 451/* Return cgraph node assigned to DECL. Create new one when needed. */
0550e7b7 452
d52f5295
ML
453cgraph_node *
454cgraph_node::create (tree decl)
e72fcfe8 455{
3dafb85c 456 cgraph_node *node = symtab->create_empty ();
341c100f 457 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
988d1653 458
67348ccc 459 node->decl = decl;
d52f5295 460 node->register_symbol ();
1ab24192 461
5c2e00ee 462 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
e72fcfe8 463 {
d52f5295 464 node->origin = cgraph_node::get_create (DECL_CONTEXT (decl));
e72fcfe8
JH
465 node->next_nested = node->origin->nested;
466 node->origin->nested = node;
467 }
468 return node;
469}
470
6f99e449
MJ
471/* Try to find a call graph node for declaration DECL and if it does not exist
472 or if it corresponds to an inline clone, create a new one. */
a358e188 473
d52f5295
ML
474cgraph_node *
475cgraph_node::get_create (tree decl)
a358e188 476{
3dafb85c 477 cgraph_node *first_clone = cgraph_node::get (decl);
a358e188 478
6f99e449
MJ
479 if (first_clone && !first_clone->global.inlined_to)
480 return first_clone;
a358e188 481
3dafb85c 482 cgraph_node *node = cgraph_node::create (decl);
6f99e449
MJ
483 if (first_clone)
484 {
485 first_clone->clone_of = node;
486 node->clones = first_clone;
3dafb85c 487 symtab->symtab_prevail_in_asm_name_hash (node);
aede2c10 488 node->decl->decl_with_vis.symtab_node = node;
6f99e449
MJ
489 if (dump_file)
490 fprintf (dump_file, "Introduced new external node "
491 "(%s/%i) and turned into root of the clone tree.\n",
fec39fa6 492 xstrdup (node->name ()), node->order);
6f99e449
MJ
493 }
494 else if (dump_file)
495 fprintf (dump_file, "Introduced new external node "
fec39fa6 496 "(%s/%i).\n", xstrdup (node->name ()),
6f99e449
MJ
497 node->order);
498 return node;
a358e188
MJ
499}
500
87e7b310 501/* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
073a8998 502 the function body is associated with (not necessarily cgraph_node (DECL). */
b2583345 503
d52f5295
ML
504cgraph_node *
505cgraph_node::create_alias (tree alias, tree target)
b2583345 506{
d52f5295 507 cgraph_node *alias_node;
b2583345 508
40a7fe1e
JH
509 gcc_assert (TREE_CODE (target) == FUNCTION_DECL
510 || TREE_CODE (target) == IDENTIFIER_NODE);
b2583345 511 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
d52f5295 512 alias_node = cgraph_node::get_create (alias);
67348ccc
DM
513 gcc_assert (!alias_node->definition);
514 alias_node->alias_target = target;
515 alias_node->definition = true;
516 alias_node->alias = true;
08346abd 517 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
67348ccc 518 alias_node->weakref = true;
6744a6ab
JH
519 return alias_node;
520}
521
051f8cc6 522/* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
a358e188 523 and NULL otherwise.
6744a6ab 524 Same body aliases are output whenever the body of DECL is output,
d52f5295
ML
525 and cgraph_node::get (ALIAS) transparently returns
526 cgraph_node::get (DECL). */
6744a6ab 527
3dafb85c 528cgraph_node *
d52f5295 529cgraph_node::create_same_body_alias (tree alias, tree decl)
6744a6ab 530{
3dafb85c 531 cgraph_node *n;
6744a6ab
JH
532#ifndef ASM_OUTPUT_DEF
533 /* If aliases aren't supported by the assembler, fail. */
051f8cc6 534 return NULL;
6744a6ab 535#endif
39e2db00
JH
536 /* Langhooks can create same body aliases of symbols not defined.
537 Those are useless. Drop them on the floor. */
3dafb85c 538 if (symtab->global_info_ready)
39e2db00 539 return NULL;
6744a6ab 540
d52f5295 541 n = cgraph_node::create_alias (alias, decl);
67348ccc 542 n->cpp_implicit_alias = true;
3dafb85c 543 if (symtab->cpp_implicit_aliases_done)
d52f5295 544 n->resolve_alias (cgraph_node::get (decl));
39e2db00 545 return n;
6744a6ab
JH
546}
547
051f8cc6 548/* Add thunk alias into callgraph. The alias declaration is ALIAS and it
61502ca8 549 aliases DECL with an adjustments made into the first parameter.
051f8cc6
JH
550 See comments in thunk_adjust for detail on the parameters. */
551
3dafb85c 552cgraph_node *
d52f5295
ML
553cgraph_node::create_thunk (tree alias, tree, bool this_adjusting,
554 HOST_WIDE_INT fixed_offset,
555 HOST_WIDE_INT virtual_value,
556 tree virtual_offset,
557 tree real_alias)
6744a6ab 558{
3dafb85c 559 cgraph_node *node;
6744a6ab 560
d52f5295 561 node = cgraph_node::get (alias);
6744a6ab 562 if (node)
d52f5295 563 node->reset ();
24d047a3 564 else
d52f5295 565 node = cgraph_node::create (alias);
77a74ed7 566 gcc_checking_assert (!virtual_offset
807e902e 567 || wi::eq_p (virtual_offset, virtual_value));
6744a6ab
JH
568 node->thunk.fixed_offset = fixed_offset;
569 node->thunk.this_adjusting = this_adjusting;
570 node->thunk.virtual_value = virtual_value;
571 node->thunk.virtual_offset_p = virtual_offset != NULL;
572 node->thunk.alias = real_alias;
573 node->thunk.thunk_p = true;
67348ccc 574 node->definition = true;
c47d0034 575
051f8cc6 576 return node;
b2583345
JJ
577}
578
bedb9fc0
RH
579/* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
580 Return NULL if there's no such node. */
581
d52f5295
ML
582cgraph_node *
583cgraph_node::get_for_asmname (tree asmname)
bedb9fc0 584{
1ab24192 585 /* We do not want to look at inline clones. */
3dafb85c 586 for (symtab_node *node = symtab_node::get_for_asmname (asmname);
5d59b5e1 587 node;
67348ccc 588 node = node->next_sharing_asm_name)
5d59b5e1 589 {
7de90a6c 590 cgraph_node *cn = dyn_cast <cgraph_node *> (node);
5d59b5e1
LC
591 if (cn && !cn->global.inlined_to)
592 return cn;
593 }
bedb9fc0
RH
594 return NULL;
595}
596
6bdf3519 597/* Returns a hash value for X (which really is a cgraph_edge). */
70d539ce
JH
598
599static hashval_t
600edge_hash (const void *x)
601{
3dafb85c 602 return htab_hash_pointer (((const cgraph_edge *) x)->call_stmt);
70d539ce
JH
603}
604
6bdf3519 605/* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
70d539ce
JH
606
607static int
608edge_eq (const void *x, const void *y)
609{
3dafb85c 610 return ((const cgraph_edge *) x)->call_stmt == y;
70d539ce
JH
611}
612
e33c6cd6
MJ
613/* Add call graph edge E to call site hash of its caller. */
614
042ae7d2 615static inline void
3dafb85c 616cgraph_update_edge_in_call_site_hash (cgraph_edge *e)
042ae7d2
JH
617{
618 void **slot;
619 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
620 e->call_stmt,
621 htab_hash_pointer (e->call_stmt),
622 INSERT);
623 *slot = e;
624}
625
626/* Add call graph edge E to call site hash of its caller. */
627
e33c6cd6 628static inline void
3dafb85c 629cgraph_add_edge_to_call_site_hash (cgraph_edge *e)
e33c6cd6
MJ
630{
631 void **slot;
042ae7d2
JH
632 /* There are two speculative edges for every statement (one direct,
633 one indirect); always hash the direct one. */
634 if (e->speculative && e->indirect_unknown_callee)
635 return;
e33c6cd6
MJ
636 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
637 e->call_stmt,
638 htab_hash_pointer (e->call_stmt),
639 INSERT);
042ae7d2
JH
640 if (*slot)
641 {
3dafb85c 642 gcc_assert (((cgraph_edge *)*slot)->speculative);
bfa3b50a
JH
643 if (e->callee)
644 *slot = e;
042ae7d2
JH
645 return;
646 }
647 gcc_assert (!*slot || e->speculative);
e33c6cd6
MJ
648 *slot = e;
649}
726a989a
RB
650
651/* Return the callgraph edge representing the GIMPLE_CALL statement
652 CALL_STMT. */
653
d52f5295
ML
654cgraph_edge *
655cgraph_node::get_edge (gimple call_stmt)
18c6ada9 656{
3dafb85c 657 cgraph_edge *e, *e2;
70d539ce
JH
658 int n = 0;
659
d52f5295 660 if (call_site_hash)
3dafb85c 661 return (cgraph_edge *)
d52f5295 662 htab_find_with_hash (call_site_hash, call_stmt,
52c76998 663 htab_hash_pointer (call_stmt));
18c6ada9
JH
664
665 /* This loop may turn out to be performance problem. In such case adding
666 hashtables into call nodes with very many edges is probably best
2b8a92de 667 solution. It is not good idea to add pointer into CALL_EXPR itself
18c6ada9
JH
668 because we want to make possible having multiple cgraph nodes representing
669 different clones of the same body before the body is actually cloned. */
d52f5295 670 for (e = callees; e; e = e->next_callee)
70d539ce
JH
671 {
672 if (e->call_stmt == call_stmt)
673 break;
674 n++;
675 }
726a989a 676
e33c6cd6 677 if (!e)
d52f5295 678 for (e = indirect_calls; e; e = e->next_callee)
e33c6cd6
MJ
679 {
680 if (e->call_stmt == call_stmt)
681 break;
682 n++;
683 }
684
70d539ce
JH
685 if (n > 100)
686 {
d52f5295
ML
687 call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
688 for (e2 = callees; e2; e2 = e2->next_callee)
e33c6cd6 689 cgraph_add_edge_to_call_site_hash (e2);
d52f5295 690 for (e2 = indirect_calls; e2; e2 = e2->next_callee)
e33c6cd6 691 cgraph_add_edge_to_call_site_hash (e2);
70d539ce 692 }
726a989a 693
18c6ada9
JH
694 return e;
695}
696
726a989a 697
3dafb85c 698/* Change field call_stmt of edge to NEW_STMT.
042ae7d2
JH
699 If UPDATE_SPECULATIVE and E is any component of speculative
700 edge, then update all components. */
0550e7b7 701
70d539ce 702void
3dafb85c 703cgraph_edge::set_call_stmt (gimple new_stmt, bool update_speculative)
70d539ce 704{
e33c6cd6
MJ
705 tree decl;
706
042ae7d2
JH
707 /* Speculative edges has three component, update all of them
708 when asked to. */
3dafb85c 709 if (update_speculative && speculative)
042ae7d2 710 {
3dafb85c
ML
711 cgraph_edge *direct, *indirect;
712 ipa_ref *ref;
042ae7d2 713
3dafb85c
ML
714 speculative_call_info (direct, indirect, ref);
715 direct->set_call_stmt (new_stmt, false);
716 indirect->set_call_stmt (new_stmt, false);
042ae7d2
JH
717 ref->stmt = new_stmt;
718 return;
719 }
720
721 /* Only direct speculative edges go to call_site_hash. */
3dafb85c
ML
722 if (caller->call_site_hash
723 && (!speculative || !indirect_unknown_callee))
70d539ce 724 {
3dafb85c
ML
725 htab_remove_elt_with_hash (caller->call_site_hash,
726 call_stmt,
727 htab_hash_pointer (call_stmt));
70d539ce 728 }
e33c6cd6 729
3dafb85c
ML
730 cgraph_edge *e = this;
731
732 call_stmt = new_stmt;
733 if (indirect_unknown_callee
e33c6cd6
MJ
734 && (decl = gimple_call_fndecl (new_stmt)))
735 {
736 /* Constant propagation (and possibly also inlining?) can turn an
737 indirect call into a direct one. */
3dafb85c 738 cgraph_node *new_callee = cgraph_node::get (decl);
e33c6cd6 739
a358e188 740 gcc_checking_assert (new_callee);
3dafb85c 741 e = make_direct (new_callee);
e33c6cd6
MJ
742 }
743
67348ccc 744 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
2505c5ed 745 e->can_throw_external = stmt_can_throw_external (new_stmt);
b6fa5b01 746 pop_cfun ();
70d539ce 747 if (e->caller->call_site_hash)
e33c6cd6 748 cgraph_add_edge_to_call_site_hash (e);
70d539ce
JH
749}
750
e33c6cd6
MJ
751/* Allocate a cgraph_edge structure and fill it with data according to the
752 parameters of which only CALLEE can be NULL (when creating an indirect call
753 edge). */
e72fcfe8 754
d52f5295 755cgraph_edge *
3dafb85c
ML
756symbol_table::create_edge (cgraph_node *caller, cgraph_node *callee,
757 gimple call_stmt, gcov_type count, int freq,
758 bool indir_unknown_callee)
e72fcfe8 759{
d52f5295 760 cgraph_edge *edge;
18c6ada9 761
d7f09764
DN
762 /* LTO does not actually have access to the call_stmt since these
763 have not been loaded yet. */
764 if (call_stmt)
765 {
61502ca8 766 /* This is a rather expensive check possibly triggering
77a74ed7 767 construction of call stmt hashtable. */
042ae7d2 768#ifdef ENABLE_CHECKING
3dafb85c 769 cgraph_edge *e;
d52f5295
ML
770 gcc_checking_assert (
771 !(e = caller->get_edge (call_stmt)) || e->speculative);
042ae7d2 772#endif
18c6ada9 773
d7f09764
DN
774 gcc_assert (is_gimple_call (call_stmt));
775 }
b58b1157 776
934cb78a
MJ
777 if (free_edges)
778 {
779 edge = free_edges;
780 free_edges = NEXT_FREE_EDGE (edge);
781 }
782 else
783 {
3dafb85c
ML
784 edge = ggc_alloc<cgraph_edge> ();
785 edge->uid = edges_max_uid++;
934cb78a
MJ
786 }
787
3dafb85c
ML
788 edges_count++;
789
18c6ada9 790 edge->aux = NULL;
e72fcfe8
JH
791 edge->caller = caller;
792 edge->callee = callee;
e33c6cd6
MJ
793 edge->prev_caller = NULL;
794 edge->next_caller = NULL;
795 edge->prev_callee = NULL;
796 edge->next_callee = NULL;
042ae7d2 797 edge->lto_stmt_uid = 0;
e33c6cd6
MJ
798
799 edge->count = count;
800 gcc_assert (count >= 0);
801 edge->frequency = freq;
802 gcc_assert (freq >= 0);
803 gcc_assert (freq <= CGRAPH_FREQ_MAX);
e33c6cd6 804
e0704a46 805 edge->call_stmt = call_stmt;
67348ccc 806 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
9f3f7d13
RG
807 edge->can_throw_external
808 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
b6fa5b01 809 pop_cfun ();
89faf322 810 if (call_stmt
67348ccc
DM
811 && callee && callee->decl
812 && !gimple_check_call_matching_types (call_stmt, callee->decl,
4de09b85 813 false))
89faf322
RG
814 edge->call_stmt_cannot_inline_p = true;
815 else
816 edge->call_stmt_cannot_inline_p = false;
e33c6cd6
MJ
817
818 edge->indirect_info = NULL;
819 edge->indirect_inlining_edge = 0;
5979aa54 820 edge->speculative = false;
51ce0547 821 edge->indirect_unknown_callee = indir_unknown_callee;
f9bb202b
JH
822 if (flag_devirtualize && call_stmt && DECL_STRUCT_FUNCTION (caller->decl))
823 edge->in_polymorphic_cdtor
824 = decl_maybe_in_construction_p (NULL, NULL, call_stmt,
825 caller->decl);
826 else
827 edge->in_polymorphic_cdtor = caller->thunk.thunk_p;
188c7d00
JH
828 if (call_stmt && caller->call_site_hash)
829 cgraph_add_edge_to_call_site_hash (edge);
e33c6cd6
MJ
830
831 return edge;
832}
833
d52f5295 834/* Create edge from a given function to CALLEE in the cgraph. */
e33c6cd6 835
3dafb85c
ML
836cgraph_edge *
837cgraph_node::create_edge (cgraph_node *callee,
d52f5295 838 gimple call_stmt, gcov_type count, int freq)
e33c6cd6 839{
3dafb85c
ML
840 cgraph_edge *edge = symtab->create_edge (this, callee, call_stmt, count,
841 freq, false);
e33c6cd6 842
e33c6cd6
MJ
843 initialize_inline_failed (edge);
844
e72fcfe8 845 edge->next_caller = callee->callers;
2563c224
RG
846 if (callee->callers)
847 callee->callers->prev_caller = edge;
d52f5295
ML
848 edge->next_callee = callees;
849 if (callees)
850 callees->prev_callee = edge;
851 callees = edge;
e72fcfe8 852 callee->callers = edge;
3dc9eaa6 853
e33c6cd6
MJ
854 return edge;
855}
856
ce47fda3
MJ
857/* Allocate cgraph_indirect_call_info and set its fields to default values. */
858
3dafb85c 859cgraph_indirect_call_info *
ce47fda3
MJ
860cgraph_allocate_init_indirect_info (void)
861{
3dafb85c 862 cgraph_indirect_call_info *ii;
ce47fda3 863
766090c2 864 ii = ggc_cleared_alloc<cgraph_indirect_call_info> ();
ce47fda3
MJ
865 ii->param_index = -1;
866 return ii;
867}
e33c6cd6
MJ
868
869/* Create an indirect edge with a yet-undetermined callee where the call
870 statement destination is a formal parameter of the caller with index
871 PARAM_INDEX. */
872
3dafb85c 873cgraph_edge *
d52f5295 874cgraph_node::create_indirect_edge (gimple call_stmt, int ecf_flags,
d34af022
IE
875 gcov_type count, int freq,
876 bool compute_indirect_info)
e33c6cd6 877{
3dafb85c
ML
878 cgraph_edge *edge = symtab->create_edge (this, NULL, call_stmt,
879 count, freq, true);
1d5755ef 880 tree target;
e33c6cd6 881
3dc9eaa6
AN
882 initialize_inline_failed (edge);
883
ce47fda3 884 edge->indirect_info = cgraph_allocate_init_indirect_info ();
5f902d76 885 edge->indirect_info->ecf_flags = ecf_flags;
e33c6cd6 886
1d5755ef 887 /* Record polymorphic call info. */
d34af022
IE
888 if (compute_indirect_info
889 && call_stmt
1d5755ef
JH
890 && (target = gimple_call_fn (call_stmt))
891 && virtual_method_call_p (target))
892 {
6f8091fc 893 ipa_polymorphic_call_context context (decl, target, call_stmt);
1d5755ef
JH
894
895 /* Only record types can have virtual calls. */
68377e53 896 edge->indirect_info->polymorphic = true;
1d5755ef 897 edge->indirect_info->param_index = -1;
6f8091fc
JH
898 edge->indirect_info->otr_token
899 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
900 edge->indirect_info->otr_type = obj_type_ref_class (target);
901 gcc_assert (TREE_CODE (edge->indirect_info->otr_type) == RECORD_TYPE);
ba392339 902 edge->indirect_info->context = context;
1d5755ef
JH
903 }
904
d52f5295
ML
905 edge->next_callee = indirect_calls;
906 if (indirect_calls)
907 indirect_calls->prev_callee = edge;
908 indirect_calls = edge;
e33c6cd6 909
e72fcfe8
JH
910 return edge;
911}
912
3dafb85c 913/* Remove the edge from the list of the callers of the callee. */
2563c224 914
3dafb85c
ML
915void
916cgraph_edge::remove_callee (void)
2563c224 917{
3dafb85c
ML
918 gcc_assert (!indirect_unknown_callee);
919 if (prev_caller)
920 prev_caller->next_caller = next_caller;
921 if (next_caller)
922 next_caller->prev_caller = prev_caller;
923 if (!prev_caller)
924 callee->callers = next_caller;
2563c224
RG
925}
926
3dafb85c 927/* Remove the edge from the list of the callees of the caller. */
2563c224 928
3dafb85c
ML
929void
930cgraph_edge::remove_caller (void)
2563c224 931{
3dafb85c
ML
932 if (prev_callee)
933 prev_callee->next_callee = next_callee;
934 if (next_callee)
935 next_callee->prev_callee = prev_callee;
936 if (!prev_callee)
e33c6cd6 937 {
3dafb85c
ML
938 if (indirect_unknown_callee)
939 caller->indirect_calls = next_callee;
e33c6cd6 940 else
3dafb85c 941 caller->callees = next_callee;
e33c6cd6 942 }
3dafb85c
ML
943 if (caller->call_site_hash)
944 htab_remove_elt_with_hash (caller->call_site_hash,
945 call_stmt,
946 htab_hash_pointer (call_stmt));
2563c224
RG
947}
948
934cb78a
MJ
949/* Put the edge onto the free list. */
950
3dafb85c
ML
951void
952symbol_table::free_edge (cgraph_edge *e)
934cb78a
MJ
953{
954 int uid = e->uid;
955
042ae7d2
JH
956 if (e->indirect_info)
957 ggc_free (e->indirect_info);
958
934cb78a 959 /* Clear out the edge so we do not dangle pointers. */
5c0466b5 960 memset (e, 0, sizeof (*e));
934cb78a
MJ
961 e->uid = uid;
962 NEXT_FREE_EDGE (e) = free_edges;
963 free_edges = e;
3dafb85c 964 edges_count--;
934cb78a
MJ
965}
966
3dafb85c 967/* Remove the edge in the cgraph. */
e72fcfe8 968
cb967da5 969void
3dafb85c 970cgraph_edge::remove (void)
e72fcfe8 971{
934cb78a 972 /* Call all edge removal hooks. */
3dafb85c 973 symtab->call_edge_removal_hooks (this);
934cb78a 974
3dafb85c 975 if (!indirect_unknown_callee)
e33c6cd6 976 /* Remove from callers list of the callee. */
3dafb85c 977 remove_callee ();
2563c224
RG
978
979 /* Remove from callees list of the callers. */
3dafb85c 980 remove_caller ();
934cb78a
MJ
981
982 /* Put the edge onto the free list. */
3dafb85c 983 symtab->free_edge (this);
e72fcfe8
JH
984}
985
e33c6cd6
MJ
986/* Set callee of call graph edge E and add it to the corresponding set of
987 callers. */
988
989static void
3dafb85c 990cgraph_set_edge_callee (cgraph_edge *e, cgraph_node *n)
e33c6cd6
MJ
991{
992 e->prev_caller = NULL;
993 if (n->callers)
994 n->callers->prev_caller = e;
995 e->next_caller = n->callers;
996 n->callers = e;
997 e->callee = n;
998}
999
3dafb85c 1000/* Turn edge into speculative call calling N2. Update
042ae7d2
JH
1001 the profile so the direct call is taken COUNT times
1002 with FREQUENCY.
1003
1004 At clone materialization time, the indirect call E will
1005 be expanded as:
1006
1007 if (call_dest == N2)
1008 n2 ();
1009 else
1010 call call_dest
1011
1012 At this time the function just creates the direct call,
1013 the referencd representing the if conditional and attaches
09ce3660 1014 them all to the orginal indirect call statement.
042ae7d2 1015
09ce3660
JH
1016 Return direct edge created. */
1017
3dafb85c
ML
1018cgraph_edge *
1019cgraph_edge::make_speculative (cgraph_node *n2, gcov_type direct_count,
1020 int direct_frequency)
042ae7d2 1021{
3dafb85c
ML
1022 cgraph_node *n = caller;
1023 ipa_ref *ref = NULL;
1024 cgraph_edge *e2;
042ae7d2
JH
1025
1026 if (dump_file)
1027 {
d5c3d3ef
JH
1028 fprintf (dump_file, "Indirect call -> speculative call"
1029 " %s/%i => %s/%i\n",
fec39fa6
TS
1030 xstrdup (n->name ()), n->order,
1031 xstrdup (n2->name ()), n2->order);
042ae7d2 1032 }
3dafb85c
ML
1033 speculative = true;
1034 e2 = n->create_edge (n2, call_stmt, direct_count, direct_frequency);
042ae7d2
JH
1035 initialize_inline_failed (e2);
1036 e2->speculative = true;
67348ccc 1037 if (TREE_NOTHROW (n2->decl))
bfa3b50a
JH
1038 e2->can_throw_external = false;
1039 else
3dafb85c
ML
1040 e2->can_throw_external = can_throw_external;
1041 e2->lto_stmt_uid = lto_stmt_uid;
f9bb202b 1042 e2->in_polymorphic_cdtor = in_polymorphic_cdtor;
3dafb85c
ML
1043 count -= e2->count;
1044 frequency -= e2->frequency;
1045 symtab->call_edge_duplication_hooks (this, e2);
1046 ref = n->create_reference (n2, IPA_REF_ADDR, call_stmt);
1047 ref->lto_stmt_uid = lto_stmt_uid;
1048 ref->speculative = speculative;
d52f5295 1049 n2->mark_address_taken ();
09ce3660 1050 return e2;
042ae7d2
JH
1051}
1052
1053/* Speculative call consist of three components:
1054 1) an indirect edge representing the original call
1055 2) an direct edge representing the new call
1056 3) ADDR_EXPR reference representing the speculative check.
1057 All three components are attached to single statement (the indirect
1058 call) and if one of them exists, all of them must exist.
1059
3dafb85c 1060 Given speculative call edge, return all three components.
042ae7d2
JH
1061 */
1062
1063void
3dafb85c
ML
1064cgraph_edge::speculative_call_info (cgraph_edge *&direct,
1065 cgraph_edge *&indirect,
1066 ipa_ref *&reference)
042ae7d2 1067{
3dafb85c 1068 ipa_ref *ref;
042ae7d2 1069 int i;
3dafb85c
ML
1070 cgraph_edge *e2;
1071 cgraph_edge *e = this;
042ae7d2
JH
1072
1073 if (!e->indirect_unknown_callee)
1074 for (e2 = e->caller->indirect_calls;
1075 e2->call_stmt != e->call_stmt || e2->lto_stmt_uid != e->lto_stmt_uid;
1076 e2 = e2->next_callee)
1077 ;
1078 else
1079 {
1080 e2 = e;
1081 /* We can take advantage of the call stmt hash. */
1082 if (e2->call_stmt)
1083 {
d52f5295 1084 e = e->caller->get_edge (e2->call_stmt);
09ce3660 1085 gcc_assert (e->speculative && !e->indirect_unknown_callee);
042ae7d2
JH
1086 }
1087 else
1088 for (e = e->caller->callees;
d0b66480
JH
1089 e2->call_stmt != e->call_stmt
1090 || e2->lto_stmt_uid != e->lto_stmt_uid;
042ae7d2
JH
1091 e = e->next_callee)
1092 ;
1093 }
1094 gcc_assert (e->speculative && e2->speculative);
d0b66480
JH
1095 direct = e;
1096 indirect = e2;
042ae7d2
JH
1097
1098 reference = NULL;
d122681a 1099 for (i = 0; e->caller->iterate_reference (i, ref); i++)
042ae7d2
JH
1100 if (ref->speculative
1101 && ((ref->stmt && ref->stmt == e->call_stmt)
57292ce9 1102 || (!ref->stmt && ref->lto_stmt_uid == e->lto_stmt_uid)))
042ae7d2
JH
1103 {
1104 reference = ref;
1105 break;
1106 }
d0b66480
JH
1107
1108 /* Speculative edge always consist of all three components - direct edge,
1109 indirect and reference. */
1110
1111 gcc_assert (e && e2 && ref);
042ae7d2
JH
1112}
1113
3dafb85c 1114/* Redirect callee of the edge to N. The function does not update underlying
18c6ada9
JH
1115 call expression. */
1116
1117void
3dafb85c 1118cgraph_edge::redirect_callee (cgraph_node *n)
18c6ada9 1119{
2563c224 1120 /* Remove from callers list of the current callee. */
3dafb85c 1121 remove_callee ();
18c6ada9 1122
2563c224 1123 /* Insert to callers list of the new callee. */
3dafb85c 1124 cgraph_set_edge_callee (this, n);
e33c6cd6
MJ
1125}
1126
3dafb85c 1127/* Speculative call edge turned out to be direct call to CALLE_DECL.
042ae7d2
JH
1128 Remove the speculative call sequence and return edge representing the call.
1129 It is up to caller to redirect the call as appropriate. */
1130
3dafb85c
ML
1131cgraph_edge *
1132cgraph_edge::resolve_speculation (tree callee_decl)
042ae7d2 1133{
3dafb85c
ML
1134 cgraph_edge *edge = this;
1135 cgraph_edge *e2;
1136 ipa_ref *ref;
042ae7d2
JH
1137
1138 gcc_assert (edge->speculative);
3dafb85c 1139 edge->speculative_call_info (e2, edge, ref);
123485ca 1140 if (!callee_decl
d52f5295
ML
1141 || !ref->referred->semantically_equivalent_p
1142 (symtab_node::get (callee_decl)))
042ae7d2
JH
1143 {
1144 if (dump_file)
1145 {
09ce3660
JH
1146 if (callee_decl)
1147 {
1148 fprintf (dump_file, "Speculative indirect call %s/%i => %s/%i has "
1149 "turned out to have contradicting known target ",
fec39fa6
TS
1150 xstrdup (edge->caller->name ()), edge->caller->order,
1151 xstrdup (e2->callee->name ()), e2->callee->order);
09ce3660
JH
1152 print_generic_expr (dump_file, callee_decl, 0);
1153 fprintf (dump_file, "\n");
1154 }
1155 else
1156 {
1157 fprintf (dump_file, "Removing speculative call %s/%i => %s/%i\n",
fec39fa6
TS
1158 xstrdup (edge->caller->name ()), edge->caller->order,
1159 xstrdup (e2->callee->name ()), e2->callee->order);
09ce3660 1160 }
042ae7d2
JH
1161 }
1162 }
1163 else
1164 {
3dafb85c 1165 cgraph_edge *tmp = edge;
042ae7d2
JH
1166 if (dump_file)
1167 fprintf (dump_file, "Speculative call turned into direct call.\n");
1168 edge = e2;
1169 e2 = tmp;
d0b66480
JH
1170 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1171 in the functions inlined through it. */
042ae7d2
JH
1172 }
1173 edge->count += e2->count;
1174 edge->frequency += e2->frequency;
634ab819
JH
1175 if (edge->frequency > CGRAPH_FREQ_MAX)
1176 edge->frequency = CGRAPH_FREQ_MAX;
042ae7d2
JH
1177 edge->speculative = false;
1178 e2->speculative = false;
d122681a 1179 ref->remove_reference ();
042ae7d2 1180 if (e2->indirect_unknown_callee || e2->inline_failed)
3dafb85c 1181 e2->remove ();
042ae7d2 1182 else
d52f5295 1183 e2->callee->remove_symbol_and_inline_clones ();
042ae7d2
JH
1184 if (edge->caller->call_site_hash)
1185 cgraph_update_edge_in_call_site_hash (edge);
042ae7d2
JH
1186 return edge;
1187}
1188
3dafb85c 1189/* Make an indirect edge with an unknown callee an ordinary edge leading to
ce47fda3
MJ
1190 CALLEE. DELTA is an integer constant that is to be added to the this
1191 pointer (first parameter) to compensate for skipping a thunk adjustment. */
e33c6cd6 1192
3dafb85c
ML
1193cgraph_edge *
1194cgraph_edge::make_direct (cgraph_node *callee)
e33c6cd6 1195{
3dafb85c
ML
1196 cgraph_edge *edge = this;
1197 gcc_assert (indirect_unknown_callee);
042ae7d2
JH
1198
1199 /* If we are redirecting speculative call, make it non-speculative. */
3dafb85c 1200 if (indirect_unknown_callee && speculative)
042ae7d2 1201 {
3dafb85c 1202 edge = edge->resolve_speculation (callee->decl);
042ae7d2
JH
1203
1204 /* On successful speculation just return the pre existing direct edge. */
3dafb85c 1205 if (!indirect_unknown_callee)
042ae7d2
JH
1206 return edge;
1207 }
1208
3dafb85c
ML
1209 indirect_unknown_callee = 0;
1210 ggc_free (indirect_info);
1211 indirect_info = NULL;
e33c6cd6
MJ
1212
1213 /* Get the edge out of the indirect edge list. */
3dafb85c
ML
1214 if (prev_callee)
1215 prev_callee->next_callee = next_callee;
1216 if (next_callee)
1217 next_callee->prev_callee = prev_callee;
1218 if (!prev_callee)
1219 caller->indirect_calls = next_callee;
e33c6cd6
MJ
1220
1221 /* Put it into the normal callee list */
3dafb85c
ML
1222 prev_callee = NULL;
1223 next_callee = caller->callees;
1224 if (caller->callees)
1225 caller->callees->prev_callee = edge;
1226 caller->callees = edge;
e33c6cd6
MJ
1227
1228 /* Insert to callers list of the new callee. */
1229 cgraph_set_edge_callee (edge, callee);
1230
3dafb85c
ML
1231 if (call_stmt)
1232 call_stmt_cannot_inline_p
1233 = !gimple_check_call_matching_types (call_stmt, callee->decl,
4de09b85 1234 false);
f2906a8e 1235
e33c6cd6
MJ
1236 /* We need to re-determine the inlining status of the edge. */
1237 initialize_inline_failed (edge);
042ae7d2 1238 return edge;
2563c224
RG
1239}
1240
66a20fc2
JH
1241/* If necessary, change the function declaration in the call statement
1242 associated with E so that it corresponds to the edge callee. */
1243
1244gimple
3dafb85c 1245cgraph_edge::redirect_call_stmt_to_callee (void)
66a20fc2 1246{
3dafb85c
ML
1247 cgraph_edge *e = this;
1248
66a20fc2 1249 tree decl = gimple_call_fndecl (e->call_stmt);
23a04216 1250 tree lhs = gimple_call_lhs (e->call_stmt);
66a20fc2
JH
1251 gimple new_stmt;
1252 gimple_stmt_iterator gsi;
1253#ifdef ENABLE_CHECKING
3dafb85c 1254 cgraph_node *node;
66a20fc2
JH
1255#endif
1256
042ae7d2
JH
1257 if (e->speculative)
1258 {
3dafb85c 1259 cgraph_edge *e2;
042ae7d2 1260 gimple new_stmt;
3dafb85c 1261 ipa_ref *ref;
042ae7d2 1262
3dafb85c 1263 e->speculative_call_info (e, e2, ref);
a21e735e
JH
1264 /* If there already is an direct call (i.e. as a result of inliner's
1265 substitution), forget about speculating. */
eefe9a99 1266 if (decl)
3dafb85c 1267 e = e->resolve_speculation (decl);
a21e735e
JH
1268 /* If types do not match, speculation was likely wrong.
1269 The direct edge was posisbly redirected to the clone with a different
1270 signature. We did not update the call statement yet, so compare it
1271 with the reference that still points to the proper type. */
1272 else if (!gimple_check_call_matching_types (e->call_stmt,
67348ccc 1273 ref->referred->decl,
eefe9a99 1274 true))
042ae7d2
JH
1275 {
1276 if (dump_file)
09ce3660
JH
1277 fprintf (dump_file, "Not expanding speculative call of %s/%i -> %s/%i\n"
1278 "Type mismatch.\n",
fec39fa6 1279 xstrdup (e->caller->name ()),
67348ccc 1280 e->caller->order,
fec39fa6 1281 xstrdup (e->callee->name ()),
67348ccc 1282 e->callee->order);
3dafb85c 1283 e = e->resolve_speculation ();
a21e735e
JH
1284 /* We are producing the final function body and will throw away the
1285 callgraph edges really soon. Reset the counts/frequencies to
1286 keep verifier happy in the case of roundoff errors. */
1287 e->count = gimple_bb (e->call_stmt)->count;
1288 e->frequency = compute_call_stmt_bb_frequency
67348ccc 1289 (e->caller->decl, gimple_bb (e->call_stmt));
09ce3660 1290 }
eefe9a99 1291 /* Expand speculation into GIMPLE code. */
09ce3660
JH
1292 else
1293 {
1294 if (dump_file)
a21e735e
JH
1295 fprintf (dump_file,
1296 "Expanding speculative call of %s/%i -> %s/%i count:"
a9243bfc 1297 "%"PRId64"\n",
fec39fa6 1298 xstrdup (e->caller->name ()),
67348ccc 1299 e->caller->order,
fec39fa6 1300 xstrdup (e->callee->name ()),
67348ccc 1301 e->callee->order,
a9243bfc 1302 (int64_t)e->count);
042ae7d2 1303 gcc_assert (e2->speculative);
67348ccc 1304 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
d52f5295 1305 new_stmt = gimple_ic (e->call_stmt, dyn_cast<cgraph_node *> (ref->referred),
042ae7d2
JH
1306 e->count || e2->count
1307 ? RDIV (e->count * REG_BR_PROB_BASE,
1308 e->count + e2->count)
1309 : e->frequency || e2->frequency
1310 ? RDIV (e->frequency * REG_BR_PROB_BASE,
1311 e->frequency + e2->frequency)
1312 : REG_BR_PROB_BASE / 2,
1313 e->count, e->count + e2->count);
1314 e->speculative = false;
d52f5295
ML
1315 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt,
1316 false);
a21e735e 1317 e->frequency = compute_call_stmt_bb_frequency
67348ccc 1318 (e->caller->decl, gimple_bb (e->call_stmt));
a21e735e 1319 e2->frequency = compute_call_stmt_bb_frequency
67348ccc 1320 (e2->caller->decl, gimple_bb (e2->call_stmt));
042ae7d2
JH
1321 e2->speculative = false;
1322 ref->speculative = false;
1323 ref->stmt = NULL;
1324 /* Indirect edges are not both in the call site hash.
1325 get it updated. */
1326 if (e->caller->call_site_hash)
1327 cgraph_update_edge_in_call_site_hash (e2);
1328 pop_cfun ();
1329 /* Continue redirecting E to proper target. */
1330 }
1331 }
1332
66a20fc2 1333 if (e->indirect_unknown_callee
67348ccc 1334 || decl == e->callee->decl)
66a20fc2
JH
1335 return e->call_stmt;
1336
1337#ifdef ENABLE_CHECKING
1338 if (decl)
1339 {
d52f5295 1340 node = cgraph_node::get (decl);
66a20fc2
JH
1341 gcc_assert (!node || !node->clone.combined_args_to_skip);
1342 }
1343#endif
1344
3dafb85c 1345 if (symtab->dump_file)
66a20fc2 1346 {
3dafb85c 1347 fprintf (symtab->dump_file, "updating call of %s/%i -> %s/%i: ",
fec39fa6
TS
1348 xstrdup (e->caller->name ()), e->caller->order,
1349 xstrdup (e->callee->name ()), e->callee->order);
3dafb85c 1350 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
66a20fc2
JH
1351 if (e->callee->clone.combined_args_to_skip)
1352 {
3dafb85c
ML
1353 fprintf (symtab->dump_file, " combined args to skip: ");
1354 dump_bitmap (symtab->dump_file,
66a20fc2
JH
1355 e->callee->clone.combined_args_to_skip);
1356 }
1357 }
1358
1359 if (e->callee->clone.combined_args_to_skip)
1360 {
1361 int lp_nr;
1362
1363 new_stmt
1364 = gimple_call_copy_skip_args (e->call_stmt,
1365 e->callee->clone.combined_args_to_skip);
67348ccc 1366 gimple_call_set_fndecl (new_stmt, e->callee->decl);
c52da5f7 1367 gimple_call_set_fntype (new_stmt, gimple_call_fntype (e->call_stmt));
66a20fc2
JH
1368
1369 if (gimple_vdef (new_stmt)
1370 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1371 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1372
1373 gsi = gsi_for_stmt (e->call_stmt);
1374 gsi_replace (&gsi, new_stmt, false);
1375 /* We need to defer cleaning EH info on the new statement to
1376 fixup-cfg. We may not have dominator information at this point
1377 and thus would end up with unreachable blocks and have no way
1378 to communicate that we need to run CFG cleanup then. */
1379 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
1380 if (lp_nr != 0)
1381 {
1382 remove_stmt_from_eh_lp (e->call_stmt);
1383 add_stmt_to_eh_lp (new_stmt, lp_nr);
1384 }
1385 }
1386 else
1387 {
1388 new_stmt = e->call_stmt;
67348ccc 1389 gimple_call_set_fndecl (new_stmt, e->callee->decl);
6a58ccca 1390 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
66a20fc2
JH
1391 }
1392
23a04216
JH
1393 /* If the call becomes noreturn, remove the lhs. */
1394 if (lhs && (gimple_call_flags (new_stmt) & ECF_NORETURN))
1395 {
1396 if (TREE_CODE (lhs) == SSA_NAME)
1397 {
45b62594
RB
1398 tree var = create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e->caller->decl),
1399 TREE_TYPE (lhs), NULL);
1400 var = get_or_create_ssa_default_def
1401 (DECL_STRUCT_FUNCTION (e->caller->decl), var);
1402 gimple set_stmt = gimple_build_assign (lhs, var);
23a04216 1403 gsi = gsi_for_stmt (new_stmt);
45b62594
RB
1404 gsi_insert_before_without_update (&gsi, set_stmt, GSI_SAME_STMT);
1405 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), set_stmt);
23a04216
JH
1406 }
1407 gimple_call_set_lhs (new_stmt, NULL_TREE);
1f91035f
JH
1408 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
1409 }
1410
1411 /* If new callee has no static chain, remove it. */
1412 if (gimple_call_chain (new_stmt) && !DECL_STATIC_CHAIN (e->callee->decl))
1413 {
1414 gimple_call_set_chain (new_stmt, NULL);
1415 update_stmt_fn (DECL_STRUCT_FUNCTION (e->caller->decl), new_stmt);
23a04216
JH
1416 }
1417
d52f5295 1418 e->caller->set_call_stmt_including_clones (e->call_stmt, new_stmt, false);
66a20fc2 1419
3dafb85c 1420 if (symtab->dump_file)
66a20fc2 1421 {
3dafb85c
ML
1422 fprintf (symtab->dump_file, " updated to:");
1423 print_gimple_stmt (symtab->dump_file, e->call_stmt, 0, dump_flags);
66a20fc2
JH
1424 }
1425 return new_stmt;
1426}
726a989a
RB
1427
1428/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
4b685e14 1429 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
a9d24544
JJ
1430 of OLD_STMT if it was previously call statement.
1431 If NEW_STMT is NULL, the call has been dropped without any
1432 replacement. */
2bafad93 1433
9187e02d 1434static void
3dafb85c 1435cgraph_update_edges_for_call_stmt_node (cgraph_node *node,
a9d24544
JJ
1436 gimple old_stmt, tree old_call,
1437 gimple new_stmt)
2bafad93 1438{
a9d24544
JJ
1439 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1440 ? gimple_call_fndecl (new_stmt) : 0;
2bafad93 1441
4b685e14
JH
1442 /* We are seeing indirect calls, then there is nothing to update. */
1443 if (!new_call && !old_call)
1444 return;
1445 /* See if we turned indirect call into direct call or folded call to one builtin
61502ca8 1446 into different builtin. */
2bafad93
JJ
1447 if (old_call != new_call)
1448 {
3dafb85c
ML
1449 cgraph_edge *e = node->get_edge (old_stmt);
1450 cgraph_edge *ne = NULL;
4b685e14
JH
1451 gcov_type count;
1452 int frequency;
2bafad93
JJ
1453
1454 if (e)
1455 {
e33c6cd6
MJ
1456 /* See if the edge is already there and has the correct callee. It
1457 might be so because of indirect inlining has already updated
97ba0040
JH
1458 it. We also might've cloned and redirected the edge. */
1459 if (new_call && e->callee)
1460 {
3dafb85c 1461 cgraph_node *callee = e->callee;
97ba0040
JH
1462 while (callee)
1463 {
67348ccc 1464 if (callee->decl == new_call
97ba0040 1465 || callee->former_clone_of == new_call)
eb14a79f 1466 {
3dafb85c 1467 e->set_call_stmt (new_stmt);
eb14a79f
ML
1468 return;
1469 }
97ba0040
JH
1470 callee = callee->clone_of;
1471 }
1472 }
4b685e14
JH
1473
1474 /* Otherwise remove edge and create new one; we can't simply redirect
1475 since function has changed, so inline plan and other information
1476 attached to edge is invalid. */
4b685e14
JH
1477 count = e->count;
1478 frequency = e->frequency;
2dbe8b70 1479 if (e->indirect_unknown_callee || e->inline_failed)
3dafb85c 1480 e->remove ();
2dbe8b70 1481 else
d52f5295 1482 e->callee->remove_symbol_and_inline_clones ();
4b685e14 1483 }
a9d24544 1484 else if (new_call)
4b685e14
JH
1485 {
1486 /* We are seeing new direct call; compute profile info based on BB. */
1487 basic_block bb = gimple_bb (new_stmt);
1488 count = bb->count;
1489 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1490 bb);
2bafad93 1491 }
2bafad93 1492
4b685e14
JH
1493 if (new_call)
1494 {
d52f5295
ML
1495 ne = node->create_edge (cgraph_node::get_create (new_call),
1496 new_stmt, count, frequency);
4b685e14
JH
1497 gcc_assert (ne->inline_failed);
1498 }
2bafad93 1499 }
4b685e14
JH
1500 /* We only updated the call stmt; update pointer in cgraph edge.. */
1501 else if (old_stmt != new_stmt)
3dafb85c 1502 node->get_edge (old_stmt)->set_call_stmt (new_stmt);
2bafad93
JJ
1503}
1504
9187e02d 1505/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
4b685e14
JH
1506 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1507 of OLD_STMT before it was updated (updating can happen inplace). */
9187e02d
JH
1508
1509void
4b685e14 1510cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
9187e02d 1511{
3dafb85c
ML
1512 cgraph_node *orig = cgraph_node::get (cfun->decl);
1513 cgraph_node *node;
9187e02d 1514
a358e188 1515 gcc_checking_assert (orig);
4b685e14 1516 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
9187e02d
JH
1517 if (orig->clones)
1518 for (node = orig->clones; node != orig;)
1519 {
4b685e14 1520 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
9187e02d
JH
1521 if (node->clones)
1522 node = node->clones;
1523 else if (node->next_sibling_clone)
1524 node = node->next_sibling_clone;
1525 else
1526 {
1527 while (node != orig && !node->next_sibling_clone)
1528 node = node->clone_of;
1529 if (node != orig)
1530 node = node->next_sibling_clone;
1531 }
1532 }
1533}
1534
726a989a 1535
2563c224
RG
1536/* Remove all callees from the node. */
1537
1538void
d52f5295 1539cgraph_node::remove_callees (void)
2563c224 1540{
3dafb85c 1541 cgraph_edge *e, *f;
2563c224
RG
1542
1543 /* It is sufficient to remove the edges from the lists of callers of
1544 the callees. The callee list of the node can be zapped with one
1545 assignment. */
d52f5295 1546 for (e = callees; e; e = f)
9088c1cc 1547 {
5c0466b5 1548 f = e->next_callee;
3dafb85c 1549 symtab->call_edge_removal_hooks (e);
e33c6cd6 1550 if (!e->indirect_unknown_callee)
3dafb85c
ML
1551 e->remove_callee ();
1552 symtab->free_edge (e);
9088c1cc 1553 }
d52f5295 1554 for (e = indirect_calls; e; e = f)
5f902d76
JH
1555 {
1556 f = e->next_callee;
3dafb85c 1557 symtab->call_edge_removal_hooks (e);
5f902d76 1558 if (!e->indirect_unknown_callee)
3dafb85c
ML
1559 e->remove_callee ();
1560 symtab->free_edge (e);
5f902d76 1561 }
d52f5295
ML
1562 indirect_calls = NULL;
1563 callees = NULL;
1564 if (call_site_hash)
70d539ce 1565 {
d52f5295
ML
1566 htab_delete (call_site_hash);
1567 call_site_hash = NULL;
70d539ce 1568 }
2563c224
RG
1569}
1570
1571/* Remove all callers from the node. */
1572
d52f5295
ML
1573void
1574cgraph_node::remove_callers (void)
2563c224 1575{
3dafb85c 1576 cgraph_edge *e, *f;
2563c224
RG
1577
1578 /* It is sufficient to remove the edges from the lists of callees of
1579 the callers. The caller list of the node can be zapped with one
1580 assignment. */
d52f5295 1581 for (e = callers; e; e = f)
9088c1cc 1582 {
5c0466b5 1583 f = e->next_caller;
3dafb85c
ML
1584 symtab->call_edge_removal_hooks (e);
1585 e->remove_caller ();
1586 symtab->free_edge (e);
9088c1cc 1587 }
d52f5295 1588 callers = NULL;
18c6ada9
JH
1589}
1590
49bde175
JH
1591/* Helper function for cgraph_release_function_body and free_lang_data.
1592 It releases body from function DECL without having to inspect its
1593 possibly non-existent symtab node. */
3a40c18a
JH
1594
1595void
49bde175 1596release_function_body (tree decl)
3a40c18a 1597{
49bde175 1598 if (DECL_STRUCT_FUNCTION (decl))
3a40c18a 1599 {
49bde175 1600 push_cfun (DECL_STRUCT_FUNCTION (decl));
7d776ee2
RG
1601 if (cfun->cfg
1602 && current_loops)
1603 {
1604 cfun->curr_properties &= ~PROP_loops;
1605 loop_optimizer_finalize ();
1606 }
936fc9ba
JH
1607 if (cfun->gimple_df)
1608 {
936fc9ba
JH
1609 delete_tree_ssa ();
1610 delete_tree_cfg_annotations ();
1611 cfun->eh = NULL;
936fc9ba
JH
1612 }
1613 if (cfun->cfg)
1614 {
e3f613cb
RB
1615 gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
1616 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS));
936fc9ba 1617 clear_edges ();
7bca81dc 1618 cfun->cfg = NULL;
936fc9ba 1619 }
a63f2942
JH
1620 if (cfun->value_histograms)
1621 free_histograms ();
c3284718 1622 pop_cfun ();
49bde175 1623 gimple_set_body (decl, NULL);
936fc9ba
JH
1624 /* Struct function hangs a lot of data that would leak if we didn't
1625 removed all pointers to it. */
49bde175
JH
1626 ggc_free (DECL_STRUCT_FUNCTION (decl));
1627 DECL_STRUCT_FUNCTION (decl) = NULL;
1628 }
1629 DECL_SAVED_TREE (decl) = NULL;
1630}
1631
d52f5295 1632/* Release memory used to represent body of function.
49bde175
JH
1633 Use this only for functions that are released before being translated to
1634 target code (i.e. RTL). Functions that are compiled to RTL and beyond
bf898b30
ML
1635 are free'd in final.c via free_after_compilation().
1636 KEEP_ARGUMENTS are useful only if you want to rebuild body as thunk. */
49bde175
JH
1637
1638void
bf898b30 1639cgraph_node::release_body (bool keep_arguments)
49bde175 1640{
d52f5295 1641 ipa_transforms_to_apply.release ();
3dafb85c 1642 if (!used_as_abstract_origin && symtab->state != PARSING)
49bde175 1643 {
d52f5295 1644 DECL_RESULT (decl) = NULL;
bf898b30
ML
1645
1646 if (!keep_arguments)
1647 DECL_ARGUMENTS (decl) = NULL;
3a40c18a 1648 }
6b20f353
DS
1649 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1650 of its associated function function declaration because it's
1651 needed to emit debug info later. */
d52f5295
ML
1652 if (!used_as_abstract_origin && DECL_INITIAL (decl))
1653 DECL_INITIAL (decl) = error_mark_node;
1654 release_function_body (decl);
1655 if (lto_file_data)
1656 lto_free_function_in_decl_state_for_node (this);
3a40c18a
JH
1657}
1658
d52f5295 1659/* Remove function from symbol table. */
18d13f34
JH
1660
1661void
d52f5295 1662cgraph_node::remove (void)
18d13f34 1663{
3dafb85c 1664 cgraph_node *n;
d52f5295 1665 int uid = this->uid;
18c6ada9 1666
3dafb85c 1667 symtab->call_cgraph_removal_hooks (this);
d52f5295
ML
1668 remove_callers ();
1669 remove_callees ();
1670 ipa_transforms_to_apply.release ();
266ad5c8 1671
96fc428c
JH
1672 /* Incremental inlining access removed nodes stored in the postorder list.
1673 */
d52f5295
ML
1674 force_output = false;
1675 forced_by_abi = false;
1676 for (n = nested; n; n = n->next_nested)
ca30a539 1677 n->origin = NULL;
d52f5295
ML
1678 nested = NULL;
1679 if (origin)
18d13f34 1680 {
3dafb85c 1681 cgraph_node **node2 = &origin->nested;
18d13f34 1682
d52f5295 1683 while (*node2 != this)
18d13f34 1684 node2 = &(*node2)->next_nested;
d52f5295 1685 *node2 = next_nested;
18d13f34 1686 }
d52f5295
ML
1687 unregister ();
1688 if (prev_sibling_clone)
1689 prev_sibling_clone->next_sibling_clone = next_sibling_clone;
1690 else if (clone_of)
1691 clone_of->clones = next_sibling_clone;
1692 if (next_sibling_clone)
1693 next_sibling_clone->prev_sibling_clone = prev_sibling_clone;
1694 if (clones)
18c6ada9 1695 {
3dafb85c 1696 cgraph_node *n, *next;
47cb0d7d 1697
d52f5295 1698 if (clone_of)
47cb0d7d 1699 {
d52f5295
ML
1700 for (n = clones; n->next_sibling_clone; n = n->next_sibling_clone)
1701 n->clone_of = clone_of;
1702 n->clone_of = clone_of;
1703 n->next_sibling_clone = clone_of->clones;
1704 if (clone_of->clones)
1705 clone_of->clones->prev_sibling_clone = n;
1706 clone_of->clones = clones;
47cb0d7d
JH
1707 }
1708 else
1709 {
66a20fc2 1710 /* We are removing node with clones. This makes clones inconsistent,
47cb0d7d
JH
1711 but assume they will be removed subsequently and just keep clone
1712 tree intact. This can happen in unreachable function removal since
1713 we remove unreachable functions in random order, not by bottom-up
1714 walk of clone trees. */
d52f5295 1715 for (n = clones; n; n = next)
47cb0d7d
JH
1716 {
1717 next = n->next_sibling_clone;
1718 n->next_sibling_clone = NULL;
1719 n->prev_sibling_clone = NULL;
1720 n->clone_of = NULL;
1721 }
1722 }
18c6ada9
JH
1723 }
1724
c22cacf3 1725 /* While all the clones are removed after being proceeded, the function
4a76d91a
JH
1726 itself is kept in the cgraph even after it is compiled. Check whether
1727 we are done with this body and reclaim it proactively if this is the case.
1728 */
3dafb85c 1729 if (symtab->state != LTO_STREAMING)
4f63dfc6 1730 {
d52f5295 1731 n = cgraph_node::get (decl);
4f63dfc6
JH
1732 if (!n
1733 || (!n->clones && !n->clone_of && !n->global.inlined_to
3dafb85c 1734 && (symtab->global_info_ready
67348ccc
DM
1735 && (TREE_ASM_WRITTEN (n->decl)
1736 || DECL_EXTERNAL (n->decl)
1737 || !n->analyzed
1738 || (!flag_wpa && n->in_other_partition)))))
d52f5295 1739 release_body ();
4f63dfc6 1740 }
1ab24192 1741
d52f5295
ML
1742 decl = NULL;
1743 if (call_site_hash)
70d539ce 1744 {
d52f5295
ML
1745 htab_delete (call_site_hash);
1746 call_site_hash = NULL;
70d539ce 1747 }
2fb16412 1748
3dafb85c 1749 symtab->release_symbol (this, uid);
18d13f34
JH
1750}
1751
39ff5a96
JH
1752/* Likewise indicate that a node is having address taken. */
1753
1754void
d52f5295 1755cgraph_node::mark_address_taken (void)
39ff5a96 1756{
4502fe8d
MJ
1757 /* Indirect inlining can figure out that all uses of the address are
1758 inlined. */
d52f5295 1759 if (global.inlined_to)
4502fe8d
MJ
1760 {
1761 gcc_assert (cfun->after_inlining);
d52f5295 1762 gcc_assert (callers->indirect_inlining_edge);
4502fe8d
MJ
1763 return;
1764 }
39e2db00
JH
1765 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1766 IPA_REF_ADDR reference exists (and thus it should be set on node
1767 representing alias we take address of) and as a test whether address
1768 of the object was taken (and thus it should be set on node alias is
1769 referring to). We should remove the first use and the remove the
1770 following set. */
d52f5295
ML
1771 address_taken = 1;
1772 cgraph_node *node = ultimate_alias_target ();
67348ccc 1773 node->address_taken = 1;
39ff5a96
JH
1774}
1775
dafc5b82
JH
1776/* Return local info for the compiled function. */
1777
3dafb85c
ML
1778cgraph_local_info *
1779cgraph_node::local_info (tree decl)
dafc5b82 1780{
341c100f 1781 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
3dafb85c 1782 cgraph_node *node = get (decl);
9f9ebcdf
MJ
1783 if (!node)
1784 return NULL;
dafc5b82
JH
1785 return &node->local;
1786}
1787
3dafb85c 1788/* Return global info for the compiled function. */
dafc5b82 1789
3dafb85c
ML
1790cgraph_global_info *
1791cgraph_node::global_info (tree decl)
dafc5b82 1792{
3dafb85c
ML
1793 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1794 && symtab->global_info_ready);
1795 cgraph_node *node = get (decl);
9f9ebcdf
MJ
1796 if (!node)
1797 return NULL;
dafc5b82
JH
1798 return &node->global;
1799}
1800
b255a036
JH
1801/* Return local info for the compiled function. */
1802
3dafb85c
ML
1803cgraph_rtl_info *
1804cgraph_node::rtl_info (tree decl)
b255a036 1805{
341c100f 1806 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
3dafb85c 1807 cgraph_node *node = get (decl);
9f9ebcdf
MJ
1808 if (!node
1809 || (decl != current_function_decl
67348ccc 1810 && !TREE_ASM_WRITTEN (node->decl)))
b255a036
JH
1811 return NULL;
1812 return &node->rtl;
1813}
1814
61a05df1
JH
1815/* Return a string describing the failure REASON. */
1816
1817const char*
1818cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1819{
1820#undef DEFCIFCODE
1cf11770 1821#define DEFCIFCODE(code, type, string) string,
61a05df1
JH
1822
1823 static const char *cif_string_table[CIF_N_REASONS] = {
1824#include "cif-code.def"
1825 };
1826
1827 /* Signedness of an enum type is implementation defined, so cast it
1828 to unsigned before testing. */
1829 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1830 return cif_string_table[reason];
1831}
1832
1cf11770
L
1833/* Return a type describing the failure REASON. */
1834
1835cgraph_inline_failed_type_t
1836cgraph_inline_failed_type (cgraph_inline_failed_t reason)
1837{
1838#undef DEFCIFCODE
1839#define DEFCIFCODE(code, type, string) type,
1840
1841 static cgraph_inline_failed_type_t cif_type_table[CIF_N_REASONS] = {
1842#include "cif-code.def"
1843 };
1844
1845 /* Signedness of an enum type is implementation defined, so cast it
1846 to unsigned before testing. */
1847 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1848 return cif_type_table[reason];
1849}
1850
6b02a499 1851/* Names used to print out the availability enum. */
8a4a83ed 1852const char * const cgraph_availability_names[] =
fa10beec 1853 {"unset", "not_available", "overwritable", "available", "local"};
6b02a499 1854
ba392339
JH
1855/* Output flags of edge E. */
1856
1857static void
1858dump_edge_flags (FILE *f, struct cgraph_edge *edge)
1859{
1860 if (edge->speculative)
1861 fprintf (f, "(speculative) ");
1862 if (!edge->inline_failed)
1863 fprintf (f, "(inlined) ");
1864 if (edge->indirect_inlining_edge)
1865 fprintf (f, "(indirect_inlining) ");
1866 if (edge->count)
1867 fprintf (f, "(%"PRId64"x) ",
1868 (int64_t)edge->count);
1869 if (edge->frequency)
1870 fprintf (f, "(%.2f per call) ",
1871 edge->frequency / (double)CGRAPH_FREQ_BASE);
1872 if (edge->can_throw_external)
1873 fprintf (f, "(can throw external) ");
1874}
c4e622b6 1875
d52f5295 1876/* Dump call graph node to file F. */
c4e622b6 1877
18c6ada9 1878void
d52f5295 1879cgraph_node::dump (FILE *f)
18c6ada9 1880{
3dafb85c 1881 cgraph_edge *edge;
e33c6cd6 1882
d52f5295 1883 dump_base (f);
8f940ee6 1884
d52f5295 1885 if (global.inlined_to)
8f940ee6 1886 fprintf (f, " Function %s/%i is inline copy in %s/%i\n",
d52f5295
ML
1887 xstrdup (name ()),
1888 order,
1889 xstrdup (global.inlined_to->name ()),
1890 global.inlined_to->order);
1891 if (clone_of)
8f940ee6 1892 fprintf (f, " Clone of %s/%i\n",
d52f5295
ML
1893 clone_of->asm_name (),
1894 clone_of->order);
3dafb85c 1895 if (symtab->function_flags_ready)
8f940ee6 1896 fprintf (f, " Availability: %s\n",
d52f5295 1897 cgraph_availability_names [get_availability ()]);
8f940ee6 1898
d52f5295 1899 if (profile_id)
634ab819 1900 fprintf (f, " Profile id: %i\n",
d52f5295
ML
1901 profile_id);
1902 fprintf (f, " First run: %i\n", tp_first_run);
8f940ee6 1903 fprintf (f, " Function flags:");
d52f5295 1904 if (count)
a9243bfc 1905 fprintf (f, " executed %"PRId64"x",
d52f5295
ML
1906 (int64_t)count);
1907 if (origin)
1908 fprintf (f, " nested in: %s", origin->asm_name ());
1909 if (gimple_has_body_p (decl))
726a989a 1910 fprintf (f, " body");
d52f5295 1911 if (process)
257eb6e3 1912 fprintf (f, " process");
d52f5295 1913 if (local.local)
18c6ada9 1914 fprintf (f, " local");
d52f5295 1915 if (local.redefined_extern_inline)
e7d6beb0 1916 fprintf (f, " redefined_extern_inline");
d52f5295 1917 if (only_called_at_startup)
844db5d0 1918 fprintf (f, " only_called_at_startup");
d52f5295 1919 if (only_called_at_exit)
844db5d0 1920 fprintf (f, " only_called_at_exit");
d52f5295 1921 if (tm_clone)
0a35513e 1922 fprintf (f, " tm_clone");
d52f5295
ML
1923 if (DECL_STATIC_CONSTRUCTOR (decl))
1924 fprintf (f," static_constructor (priority:%i)", get_init_priority ());
1925 if (DECL_STATIC_DESTRUCTOR (decl))
1926 fprintf (f," static_destructor (priority:%i)", get_fini_priority ());
18c6ada9 1927
c47d0034
JH
1928 fprintf (f, "\n");
1929
d52f5295 1930 if (thunk.thunk_p)
c47d0034 1931 {
40a7fe1e 1932 fprintf (f, " Thunk");
d52f5295 1933 if (thunk.alias)
40a7fe1e 1934 fprintf (f, " of %s (asm: %s)",
d52f5295
ML
1935 lang_hooks.decl_printable_name (thunk.alias, 2),
1936 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
40a7fe1e 1937 fprintf (f, " fixed offset %i virtual value %i has "
c47d0034 1938 "virtual offset %i)\n",
d52f5295
ML
1939 (int)thunk.fixed_offset,
1940 (int)thunk.virtual_value,
1941 (int)thunk.virtual_offset_p);
c47d0034 1942 }
d52f5295
ML
1943 if (alias && thunk.alias
1944 && DECL_P (thunk.alias))
39e2db00 1945 {
8f940ee6 1946 fprintf (f, " Alias of %s",
d52f5295
ML
1947 lang_hooks.decl_printable_name (thunk.alias, 2));
1948 if (DECL_ASSEMBLER_NAME_SET_P (thunk.alias))
39e2db00 1949 fprintf (f, " (asm: %s)",
d52f5295 1950 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk.alias)));
39e2db00
JH
1951 fprintf (f, "\n");
1952 }
c47d0034 1953
8f940ee6 1954 fprintf (f, " Called by: ");
c47d0034 1955
d52f5295 1956 for (edge = callers; edge; edge = edge->next_caller)
18c6ada9 1957 {
fec39fa6 1958 fprintf (f, "%s/%i ", edge->caller->asm_name (),
67348ccc 1959 edge->caller->order);
ba392339 1960 dump_edge_flags (f, edge);
18c6ada9
JH
1961 }
1962
8f940ee6 1963 fprintf (f, "\n Calls: ");
d52f5295 1964 for (edge = callees; edge; edge = edge->next_callee)
18c6ada9 1965 {
fec39fa6 1966 fprintf (f, "%s/%i ", edge->callee->asm_name (),
67348ccc 1967 edge->callee->order);
ba392339 1968 dump_edge_flags (f, edge);
18c6ada9
JH
1969 }
1970 fprintf (f, "\n");
6744a6ab 1971
d52f5295 1972 for (edge = indirect_calls; edge; edge = edge->next_callee)
ba392339
JH
1973 {
1974 if (edge->indirect_info->polymorphic)
1975 {
1976 fprintf (f, " Polymorphic indirect call of type ");
1977 print_generic_expr (f, edge->indirect_info->otr_type, TDF_SLIM);
1978 fprintf (f, " token:%i", (int) edge->indirect_info->otr_token);
1979 }
1980 else
1981 fprintf (f, " Indirect call");
1982 dump_edge_flags (f, edge);
1983 if (edge->indirect_info->param_index != -1)
1984 {
1985 fprintf (f, " of param:%i", edge->indirect_info->param_index);
1986 if (edge->indirect_info->agg_contents)
1987 fprintf (f, " loaded from %s %s at offset %i",
1988 edge->indirect_info->member_ptr ? "member ptr" : "aggregate",
1989 edge->indirect_info->by_ref ? "passed by reference":"",
1990 (int)edge->indirect_info->offset);
1991 }
1992 fprintf (f, "\n");
1993 if (edge->indirect_info->polymorphic)
1994 edge->indirect_info->context.dump (f);
1995 }
18c6ada9
JH
1996}
1997
c4e622b6
DN
1998/* Dump call graph node NODE to stderr. */
1999
24e47c76 2000DEBUG_FUNCTION void
d52f5295 2001cgraph_node::debug (void)
c4e622b6 2002{
d52f5295 2003 dump (stderr);
c4e622b6
DN
2004}
2005
c4e622b6 2006/* Dump the callgraph to file F. */
e72fcfe8
JH
2007
2008void
d52f5295 2009cgraph_node::dump_cgraph (FILE *f)
e72fcfe8 2010{
3dafb85c 2011 cgraph_node *node;
e72fcfe8 2012
7d82fe7c 2013 fprintf (f, "callgraph:\n\n");
65c70e6b 2014 FOR_EACH_FUNCTION (node)
d52f5295 2015 node->dump (f);
c4e622b6
DN
2016}
2017
1bb17c21 2018/* Return true when the DECL can possibly be inlined. */
d52f5295 2019
1bb17c21
JH
2020bool
2021cgraph_function_possibly_inlined_p (tree decl)
2022{
3dafb85c 2023 if (!symtab->global_info_ready)
e90acd93 2024 return !DECL_UNINLINABLE (decl);
6f312d18 2025 return DECL_POSSIBLY_INLINED (decl);
18c6ada9
JH
2026}
2027
d52f5295 2028/* cgraph_node is no longer nested function; update cgraph accordingly. */
8f235343 2029void
d52f5295 2030cgraph_node::unnest (void)
8f235343 2031{
3dafb85c 2032 cgraph_node **node2 = &origin->nested;
d52f5295 2033 gcc_assert (origin);
8f235343 2034
d52f5295 2035 while (*node2 != this)
8f235343 2036 node2 = &(*node2)->next_nested;
d52f5295
ML
2037 *node2 = next_nested;
2038 origin = NULL;
8f235343 2039}
6b02a499
JH
2040
2041/* Return function availability. See cgraph.h for description of individual
2042 return values. */
2043enum availability
d52f5295 2044cgraph_node::get_availability (void)
6b02a499
JH
2045{
2046 enum availability avail;
d52f5295 2047 if (!analyzed)
6b02a499 2048 avail = AVAIL_NOT_AVAILABLE;
d52f5295 2049 else if (local.local)
6b02a499 2050 avail = AVAIL_LOCAL;
d52f5295
ML
2051 else if (alias && weakref)
2052 ultimate_alias_target (&avail);
2053 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
2054 avail = AVAIL_INTERPOSABLE;
2055 else if (!externally_visible)
6b02a499 2056 avail = AVAIL_AVAILABLE;
61502ca8
NF
2057 /* Inline functions are safe to be analyzed even if their symbol can
2058 be overwritten at runtime. It is not meaningful to enforce any sane
4a371c8d 2059 behaviour on replacing inline function by different body. */
d52f5295 2060 else if (DECL_DECLARED_INLINE_P (decl))
4a371c8d 2061 avail = AVAIL_AVAILABLE;
6b02a499
JH
2062
2063 /* If the function can be overwritten, return OVERWRITABLE. Take
2064 care at least of two notable extensions - the COMDAT functions
2065 used to share template instantiations in C++ (this is symmetric
2066 to code cp_cannot_inline_tree_fn and probably shall be shared and
ff5c4582 2067 the inlinability hooks completely eliminated).
6b02a499
JH
2068
2069 ??? Does the C++ one definition rule allow us to always return
2070 AVAIL_AVAILABLE here? That would be good reason to preserve this
4a371c8d
JH
2071 bit. */
2072
d52f5295
ML
2073 else if (decl_replaceable_p (decl) && !DECL_EXTERNAL (decl))
2074 avail = AVAIL_INTERPOSABLE;
6b02a499
JH
2075 else avail = AVAIL_AVAILABLE;
2076
2077 return avail;
2078}
2079
be330ed4
JH
2080/* Worker for cgraph_node_can_be_local_p. */
2081static bool
3dafb85c 2082cgraph_node_cannot_be_local_p_1 (cgraph_node *node, void *)
be330ed4 2083{
67348ccc
DM
2084 return !(!node->force_output
2085 && ((DECL_COMDAT (node->decl)
2086 && !node->forced_by_abi
d52f5295 2087 && !node->used_from_object_file_p ()
67348ccc
DM
2088 && !node->same_comdat_group)
2089 || !node->externally_visible));
be330ed4
JH
2090}
2091
d52f5295 2092/* Return true if cgraph_node can be made local for API change.
a550d677
MJ
2093 Extern inline functions and C++ COMDAT functions can be made local
2094 at the expense of possible code size growth if function is used in multiple
2095 compilation units. */
2096bool
d52f5295 2097cgraph_node::can_be_local_p (void)
a550d677 2098{
d52f5295
ML
2099 return (!address_taken
2100 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1,
2101 NULL, true));
a550d677
MJ
2102}
2103
d52f5295 2104/* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
be330ed4
JH
2105 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2106 skipped. */
2107
2108bool
d52f5295
ML
2109cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback)
2110 (cgraph_node *, void *),
2111 void *data,
2112 bool include_overwritable)
be330ed4 2113{
3dafb85c
ML
2114 cgraph_edge *e;
2115 ipa_ref *ref;
be330ed4 2116
d52f5295 2117 if (callback (this, data))
be330ed4 2118 return true;
d52f5295 2119 for (e = callers; e; e = e->next_caller)
be330ed4
JH
2120 if (e->caller->thunk.thunk_p
2121 && (include_overwritable
d52f5295
ML
2122 || e->caller->get_availability () > AVAIL_INTERPOSABLE))
2123 if (e->caller->call_for_symbol_thunks_and_aliases (callback, data,
2124 include_overwritable))
9aa3f5c5 2125 return true;
e55637b7 2126
d52f5295 2127 FOR_EACH_ALIAS (this, ref)
e55637b7 2128 {
3dafb85c 2129 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
e55637b7 2130 if (include_overwritable
d52f5295
ML
2131 || alias->get_availability () > AVAIL_INTERPOSABLE)
2132 if (alias->call_for_symbol_thunks_and_aliases (callback, data,
2133 include_overwritable))
e55637b7
ML
2134 return true;
2135 }
be330ed4
JH
2136 return false;
2137}
2138
d52f5295 2139/* Call calback on function and aliases associated to the function.
be330ed4
JH
2140 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2141 skipped. */
2142
2143bool
d52f5295
ML
2144cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
2145 void *),
2146 void *data,
2147 bool include_overwritable)
a550d677 2148{
3dafb85c 2149 ipa_ref *ref;
be330ed4 2150
d52f5295 2151 if (callback (this, data))
be330ed4 2152 return true;
e55637b7 2153
d52f5295 2154 FOR_EACH_ALIAS (this, ref)
e55637b7 2155 {
3dafb85c 2156 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
e55637b7 2157 if (include_overwritable
d52f5295
ML
2158 || alias->get_availability () > AVAIL_INTERPOSABLE)
2159 if (alias->call_for_symbol_and_aliases (callback, data,
2160 include_overwritable))
e55637b7
ML
2161 return true;
2162 }
be330ed4
JH
2163 return false;
2164}
2165
2166/* Worker to bring NODE local. */
2167
d52f5295 2168bool
3dafb85c 2169cgraph_node::make_local (cgraph_node *node, void *)
be330ed4 2170{
d52f5295 2171 gcc_checking_assert (node->can_be_local_p ());
67348ccc 2172 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
a550d677 2173 {
d52f5295 2174 node->make_decl_local ();
da66d596 2175 node->set_section (NULL);
24d047a3 2176 node->set_comdat_group (NULL);
67348ccc
DM
2177 node->externally_visible = false;
2178 node->forced_by_abi = false;
a550d677 2179 node->local.local = true;
da66d596 2180 node->set_section (NULL);
67348ccc 2181 node->unique_name = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
da66d596 2182 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
67348ccc 2183 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
d52f5295 2184 gcc_assert (node->get_availability () == AVAIL_LOCAL);
a550d677 2185 }
be330ed4 2186 return false;
a550d677
MJ
2187}
2188
d52f5295 2189/* Bring cgraph node local. */
be330ed4
JH
2190
2191void
d52f5295 2192cgraph_node::make_local (void)
be330ed4 2193{
d52f5295 2194 call_for_symbol_thunks_and_aliases (cgraph_node::make_local, NULL, true);
be330ed4
JH
2195}
2196
2197/* Worker to set nothrow flag. */
2198
2199static bool
3dafb85c 2200cgraph_set_nothrow_flag_1 (cgraph_node *node, void *data)
be330ed4 2201{
3dafb85c 2202 cgraph_edge *e;
71fb4f92 2203
67348ccc 2204 TREE_NOTHROW (node->decl) = data != NULL;
71fb4f92
JH
2205
2206 if (data != NULL)
2207 for (e = node->callers; e; e = e->next_caller)
2208 e->can_throw_external = false;
be330ed4
JH
2209 return false;
2210}
2211
2212/* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
20cdc2be
JJ
2213 if any to NOTHROW. */
2214
2215void
d52f5295 2216cgraph_node::set_nothrow_flag (bool nothrow)
20cdc2be 2217{
d52f5295
ML
2218 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1,
2219 (void *)(size_t)nothrow, false);
20cdc2be
JJ
2220}
2221
be330ed4 2222/* Worker to set const flag. */
20cdc2be 2223
be330ed4 2224static bool
3dafb85c 2225cgraph_set_const_flag_1 (cgraph_node *node, void *data)
20cdc2be 2226{
530f3a1b
JH
2227 /* Static constructors and destructors without a side effect can be
2228 optimized out. */
be330ed4 2229 if (data && !((size_t)data & 2))
530f3a1b 2230 {
67348ccc
DM
2231 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2232 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2233 if (DECL_STATIC_DESTRUCTOR (node->decl))
2234 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
530f3a1b 2235 }
67348ccc
DM
2236 TREE_READONLY (node->decl) = data != NULL;
2237 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
be330ed4 2238 return false;
20cdc2be
JJ
2239}
2240
d52f5295 2241/* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
be330ed4 2242 if any to READONLY. */
20cdc2be
JJ
2243
2244void
d52f5295 2245cgraph_node::set_const_flag (bool readonly, bool looping)
20cdc2be 2246{
d52f5295
ML
2247 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1,
2248 (void *)(size_t)(readonly + (int)looping * 2),
be330ed4
JH
2249 false);
2250}
2251
2252/* Worker to set pure flag. */
2253
2254static bool
3dafb85c 2255cgraph_set_pure_flag_1 (cgraph_node *node, void *data)
be330ed4 2256{
a1ffba98 2257 /* Static constructors and destructors without a side effect can be
530f3a1b 2258 optimized out. */
be330ed4 2259 if (data && !((size_t)data & 2))
530f3a1b 2260 {
67348ccc
DM
2261 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2262 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2263 if (DECL_STATIC_DESTRUCTOR (node->decl))
2264 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
530f3a1b 2265 }
67348ccc
DM
2266 DECL_PURE_P (node->decl) = data != NULL;
2267 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
be330ed4 2268 return false;
20cdc2be
JJ
2269}
2270
d52f5295 2271/* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
be330ed4
JH
2272 if any to PURE. */
2273
2274void
d52f5295 2275cgraph_node::set_pure_flag (bool pure, bool looping)
fa5f5e27 2276{
d52f5295
ML
2277 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1,
2278 (void *)(size_t)(pure + (int)looping * 2),
2279 false);
be330ed4 2280}
844db5d0 2281
d52f5295 2282/* Return true when cgraph_node can not return or throw and thus
d56026c2
JH
2283 it is safe to ignore its side effects for IPA analysis. */
2284
2285bool
d52f5295 2286cgraph_node::cannot_return_p (void)
d56026c2 2287{
d52f5295 2288 int flags = flags_from_decl_or_type (decl);
d56026c2
JH
2289 if (!flag_exceptions)
2290 return (flags & ECF_NORETURN) != 0;
2291 else
2292 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2293 == (ECF_NORETURN | ECF_NOTHROW));
2294}
2295
3dafb85c 2296/* Return true when call of edge can not lead to return from caller
d56026c2
JH
2297 and thus it is safe to ignore its side effects for IPA analysis
2298 when computing side effects of the caller.
2299 FIXME: We could actually mark all edges that have no reaching
6626665f 2300 patch to the exit block or throw to get better results. */
d56026c2 2301bool
3dafb85c 2302cgraph_edge::cannot_lead_to_return_p (void)
d56026c2 2303{
3dafb85c 2304 if (caller->cannot_return_p ())
f10ea640 2305 return true;
3dafb85c 2306 if (indirect_unknown_callee)
d56026c2 2307 {
3dafb85c 2308 int flags = indirect_info->ecf_flags;
d56026c2
JH
2309 if (!flag_exceptions)
2310 return (flags & ECF_NORETURN) != 0;
2311 else
2312 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2313 == (ECF_NORETURN | ECF_NOTHROW));
2314 }
2315 else
3dafb85c 2316 return callee->cannot_return_p ();
d56026c2
JH
2317}
2318
d52f5295 2319/* Return true when function can be removed from callgraph
508e4757
JH
2320 if all direct calls are eliminated. */
2321
2322bool
d52f5295 2323cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
508e4757 2324{
d52f5295 2325 gcc_assert (!global.inlined_to);
530f3a1b 2326 /* Extern inlines can always go, we will use the external definition. */
d52f5295 2327 if (DECL_EXTERNAL (decl))
530f3a1b 2328 return true;
508e4757 2329 /* When function is needed, we can not remove it. */
d52f5295 2330 if (force_output || used_from_other_partition)
508e4757 2331 return false;
d52f5295
ML
2332 if (DECL_STATIC_CONSTRUCTOR (decl)
2333 || DECL_STATIC_DESTRUCTOR (decl))
530f3a1b 2334 return false;
508e4757 2335 /* Only COMDAT functions can be removed if externally visible. */
d52f5295
ML
2336 if (externally_visible
2337 && (!DECL_COMDAT (decl)
2338 || forced_by_abi
2339 || used_from_object_file_p ()))
508e4757 2340 return false;
508e4757
JH
2341 return true;
2342}
2343
9aa3f5c5
JH
2344/* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2345
2346static bool
3dafb85c 2347nonremovable_p (cgraph_node *node, void *)
9aa3f5c5 2348{
d52f5295 2349 return !node->can_remove_if_no_direct_calls_and_refs_p ();
9aa3f5c5
JH
2350}
2351
d52f5295
ML
2352/* Return true when function cgraph_node and its aliases can be removed from
2353 callgraph if all direct calls are eliminated. */
9aa3f5c5
JH
2354
2355bool
d52f5295 2356cgraph_node::can_remove_if_no_direct_calls_p (void)
9aa3f5c5
JH
2357{
2358 /* Extern inlines can always go, we will use the external definition. */
d52f5295 2359 if (DECL_EXTERNAL (decl))
9aa3f5c5 2360 return true;
d52f5295 2361 if (address_taken)
9aa3f5c5 2362 return false;
d52f5295 2363 return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
9aa3f5c5
JH
2364}
2365
d52f5295 2366/* Return true when function cgraph_node can be expected to be removed
09411461
JH
2367 from program when direct calls in this compilation unit are removed.
2368
2369 As a special case COMDAT functions are
2370 cgraph_can_remove_if_no_direct_calls_p while the are not
2371 cgraph_only_called_directly_p (it is possible they are called from other
2372 unit)
2373
2374 This function behaves as cgraph_only_called_directly_p because eliminating
61502ca8 2375 all uses of COMDAT function does not make it necessarily disappear from
09411461
JH
2376 the program unless we are compiling whole program or we do LTO. In this
2377 case we know we win since dynamic linking will not really discard the
2378 linkonce section. */
2379
2380bool
d52f5295 2381cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
09411461 2382{
d52f5295
ML
2383 gcc_assert (!global.inlined_to);
2384
2385 if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
2386 NULL, true))
09411461
JH
2387 return false;
2388 if (!in_lto_p && !flag_whole_program)
d52f5295 2389 return only_called_directly_p ();
09411461 2390 else
530f3a1b 2391 {
d52f5295 2392 if (DECL_EXTERNAL (decl))
530f3a1b 2393 return true;
d52f5295 2394 return can_remove_if_no_direct_calls_p ();
530f3a1b 2395 }
09411461
JH
2396}
2397
051f8cc6 2398
be330ed4
JH
2399/* Worker for cgraph_only_called_directly_p. */
2400
2401static bool
3dafb85c 2402cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
be330ed4 2403{
d52f5295 2404 return !node->only_called_directly_or_aliased_p ();
be330ed4
JH
2405}
2406
d52f5295 2407/* Return true when function cgraph_node and all its aliases are only called
be330ed4
JH
2408 directly.
2409 i.e. it is not externally visible, address was not taken and
2410 it is not used in any other non-standard way. */
2411
2412bool
d52f5295 2413cgraph_node::only_called_directly_p (void)
be330ed4 2414{
d52f5295
ML
2415 gcc_assert (ultimate_alias_target () == this);
2416 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
be330ed4
JH
2417 NULL, true);
2418}
2419
2420
2421/* Collect all callers of NODE. Worker for collect_callers_of_node. */
2422
2423static bool
3dafb85c 2424collect_callers_of_node_1 (cgraph_node *node, void *data)
be330ed4 2425{
d52f5295 2426 vec<cgraph_edge *> *redirect_callers = (vec<cgraph_edge *> *)data;
3dafb85c 2427 cgraph_edge *cs;
be330ed4 2428 enum availability avail;
d52f5295 2429 node->ultimate_alias_target (&avail);
be330ed4 2430
d52f5295 2431 if (avail > AVAIL_INTERPOSABLE)
be330ed4
JH
2432 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
2433 if (!cs->indirect_inlining_edge)
9771b263 2434 redirect_callers->safe_push (cs);
be330ed4
JH
2435 return false;
2436}
2437
d52f5295
ML
2438/* Collect all callers of cgraph_node and its aliases that are known to lead to
2439 cgraph_node (i.e. are not overwritable). */
be330ed4 2440
d52f5295
ML
2441vec<cgraph_edge *>
2442cgraph_node::collect_callers (void)
be330ed4 2443{
d52f5295
ML
2444 vec<cgraph_edge *> redirect_callers = vNULL;
2445 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1,
2446 &redirect_callers, false);
be330ed4
JH
2447 return redirect_callers;
2448}
2449
610c8ef0
MJ
2450/* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2451
9c8305f8 2452static bool
3dafb85c 2453clone_of_p (cgraph_node *node, cgraph_node *node2)
9c8305f8 2454{
610c8ef0 2455 bool skipped_thunk = false;
d52f5295
ML
2456 node = node->ultimate_alias_target ();
2457 node2 = node2->ultimate_alias_target ();
610c8ef0
MJ
2458
2459 /* There are no virtual clones of thunks so check former_clone_of or if we
2460 might have skipped thunks because this adjustments are no longer
2461 necessary. */
2462 while (node->thunk.thunk_p)
2463 {
2464 if (node2->former_clone_of == node->decl)
2465 return true;
2466 if (!node->thunk.this_adjusting)
2467 return false;
d52f5295 2468 node = node->callees->callee->ultimate_alias_target ();
610c8ef0
MJ
2469 skipped_thunk = true;
2470 }
2471
803d0ab0
MJ
2472 if (skipped_thunk)
2473 {
2474 if (!node2->clone.args_to_skip
2475 || !bitmap_bit_p (node2->clone.args_to_skip, 0))
2476 return false;
2477 if (node2->former_clone_of == node->decl)
2478 return true;
2479 else if (!node2->clone_of)
2480 return false;
2481 }
610c8ef0 2482
9c8305f8
JH
2483 while (node != node2 && node2)
2484 node2 = node2->clone_of;
2485 return node2 != NULL;
2486}
2487
2488/* Verify edge E count and frequency. */
2489
2490static bool
3dafb85c 2491verify_edge_count_and_frequency (cgraph_edge *e)
9c8305f8
JH
2492{
2493 bool error_found = false;
2494 if (e->count < 0)
2495 {
2496 error ("caller edge count is negative");
2497 error_found = true;
2498 }
2499 if (e->frequency < 0)
2500 {
2501 error ("caller edge frequency is negative");
2502 error_found = true;
2503 }
2504 if (e->frequency > CGRAPH_FREQ_MAX)
2505 {
2506 error ("caller edge frequency is too large");
2507 error_found = true;
2508 }
67348ccc 2509 if (gimple_has_body_p (e->caller->decl)
9c8305f8 2510 && !e->caller->global.inlined_to
042ae7d2 2511 && !e->speculative
9c8305f8 2512 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
073a8998 2513 Remove this once edges are actually removed from the function at that time. */
9c8305f8 2514 && (e->frequency
9771b263
DN
2515 || (inline_edge_summary_vec.exists ()
2516 && ((inline_edge_summary_vec.length () <= (unsigned) e->uid)
9c8305f8
JH
2517 || !inline_edge_summary (e)->predicate)))
2518 && (e->frequency
67348ccc 2519 != compute_call_stmt_bb_frequency (e->caller->decl,
9c8305f8
JH
2520 gimple_bb (e->call_stmt))))
2521 {
2522 error ("caller edge frequency %i does not match BB frequency %i",
2523 e->frequency,
67348ccc 2524 compute_call_stmt_bb_frequency (e->caller->decl,
9c8305f8
JH
2525 gimple_bb (e->call_stmt)));
2526 error_found = true;
2527 }
2528 return error_found;
2529}
2530
2531/* Switch to THIS_CFUN if needed and print STMT to stderr. */
2532static void
3dafb85c 2533cgraph_debug_gimple_stmt (function *this_cfun, gimple stmt)
9c8305f8 2534{
27f7e1c3 2535 bool fndecl_was_null = false;
9c8305f8
JH
2536 /* debug_gimple_stmt needs correct cfun */
2537 if (cfun != this_cfun)
2538 set_cfun (this_cfun);
27f7e1c3
AH
2539 /* ...and an actual current_function_decl */
2540 if (!current_function_decl)
2541 {
2542 current_function_decl = this_cfun->decl;
2543 fndecl_was_null = true;
2544 }
9c8305f8 2545 debug_gimple_stmt (stmt);
27f7e1c3
AH
2546 if (fndecl_was_null)
2547 current_function_decl = NULL;
9c8305f8
JH
2548}
2549
2550/* Verify that call graph edge E corresponds to DECL from the associated
2551 statement. Return true if the verification should fail. */
2552
2553static bool
3dafb85c 2554verify_edge_corresponds_to_fndecl (cgraph_edge *e, tree decl)
9c8305f8 2555{
3dafb85c 2556 cgraph_node *node;
9c8305f8
JH
2557
2558 if (!decl || e->callee->global.inlined_to)
2559 return false;
3dafb85c 2560 if (symtab->state == LTO_STREAMING)
ca0f62a8 2561 return false;
d52f5295 2562 node = cgraph_node::get (decl);
9c8305f8
JH
2563
2564 /* We do not know if a node from a different partition is an alias or what it
3d8d0043
MJ
2565 aliases and therefore cannot do the former_clone_of check reliably. When
2566 body_removed is set, we have lost all information about what was alias or
2567 thunk of and also cannot proceed. */
2568 if (!node
2569 || node->body_removed
2570 || node->in_other_partition
2571 || e->callee->in_other_partition)
9c8305f8 2572 return false;
9de6f6c3 2573
d52f5295
ML
2574 node = node->ultimate_alias_target ();
2575
9de6f6c3
JH
2576 /* Optimizers can redirect unreachable calls or calls triggering undefined
2577 behaviour to builtin_unreachable. */
2578 if (DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
2579 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_UNREACHABLE)
2580 return false;
9c8305f8 2581
67348ccc 2582 if (e->callee->former_clone_of != node->decl
d52f5295 2583 && (node != e->callee->ultimate_alias_target ())
610c8ef0 2584 && !clone_of_p (node, e->callee))
9c8305f8
JH
2585 return true;
2586 else
2587 return false;
2588}
2589
2590/* Verify cgraph nodes of given cgraph node. */
2591DEBUG_FUNCTION void
d52f5295 2592cgraph_node::verify_node (void)
9c8305f8 2593{
3dafb85c
ML
2594 cgraph_edge *e;
2595 function *this_cfun = DECL_STRUCT_FUNCTION (decl);
9c8305f8
JH
2596 basic_block this_block;
2597 gimple_stmt_iterator gsi;
2598 bool error_found = false;
2599
2600 if (seen_error ())
2601 return;
2602
2603 timevar_push (TV_CGRAPH_VERIFY);
d52f5295
ML
2604 error_found |= verify_base ();
2605 for (e = callees; e; e = e->next_callee)
9c8305f8
JH
2606 if (e->aux)
2607 {
2608 error ("aux field set for edge %s->%s",
fec39fa6
TS
2609 identifier_to_locale (e->caller->name ()),
2610 identifier_to_locale (e->callee->name ()));
9c8305f8
JH
2611 error_found = true;
2612 }
d52f5295 2613 if (count < 0)
9c8305f8
JH
2614 {
2615 error ("execution count is negative");
2616 error_found = true;
2617 }
d52f5295 2618 if (global.inlined_to && same_comdat_group)
65d630d4
JH
2619 {
2620 error ("inline clone in same comdat group list");
2621 error_found = true;
2622 }
d52f5295 2623 if (!definition && !in_other_partition && local.local)
e70670cf
JH
2624 {
2625 error ("local symbols must be defined");
2626 error_found = true;
2627 }
d52f5295 2628 if (global.inlined_to && externally_visible)
9c8305f8
JH
2629 {
2630 error ("externally visible inline clone");
2631 error_found = true;
2632 }
d52f5295 2633 if (global.inlined_to && address_taken)
9c8305f8
JH
2634 {
2635 error ("inline clone with address taken");
2636 error_found = true;
2637 }
d52f5295 2638 if (global.inlined_to && force_output)
9c8305f8
JH
2639 {
2640 error ("inline clone is forced to output");
2641 error_found = true;
2642 }
d52f5295 2643 for (e = indirect_calls; e; e = e->next_callee)
9c8305f8
JH
2644 {
2645 if (e->aux)
2646 {
2647 error ("aux field set for indirect edge from %s",
fec39fa6 2648 identifier_to_locale (e->caller->name ()));
9c8305f8
JH
2649 error_found = true;
2650 }
2651 if (!e->indirect_unknown_callee
2652 || !e->indirect_info)
2653 {
2654 error ("An indirect edge from %s is not marked as indirect or has "
2655 "associated indirect_info, the corresponding statement is: ",
fec39fa6 2656 identifier_to_locale (e->caller->name ()));
9c8305f8
JH
2657 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2658 error_found = true;
2659 }
2660 }
d52f5295
ML
2661 bool check_comdat = comdat_local_p ();
2662 for (e = callers; e; e = e->next_caller)
9c8305f8
JH
2663 {
2664 if (verify_edge_count_and_frequency (e))
2665 error_found = true;
1f26ac87 2666 if (check_comdat
d52f5295 2667 && !in_same_comdat_group_p (e->caller))
1f26ac87
JM
2668 {
2669 error ("comdat-local function called by %s outside its comdat",
2670 identifier_to_locale (e->caller->name ()));
2671 error_found = true;
2672 }
9c8305f8
JH
2673 if (!e->inline_failed)
2674 {
d52f5295 2675 if (global.inlined_to
9c8305f8
JH
2676 != (e->caller->global.inlined_to
2677 ? e->caller->global.inlined_to : e->caller))
2678 {
2679 error ("inlined_to pointer is wrong");
2680 error_found = true;
2681 }
d52f5295 2682 if (callers->next_caller)
9c8305f8
JH
2683 {
2684 error ("multiple inline callers");
2685 error_found = true;
2686 }
2687 }
2688 else
d52f5295 2689 if (global.inlined_to)
9c8305f8
JH
2690 {
2691 error ("inlined_to pointer set for noninline callers");
2692 error_found = true;
2693 }
2694 }
d52f5295 2695 for (e = indirect_calls; e; e = e->next_callee)
9c8305f8
JH
2696 if (verify_edge_count_and_frequency (e))
2697 error_found = true;
d52f5295 2698 if (!callers && global.inlined_to)
9c8305f8
JH
2699 {
2700 error ("inlined_to pointer is set but no predecessors found");
2701 error_found = true;
2702 }
d52f5295 2703 if (global.inlined_to == this)
9c8305f8
JH
2704 {
2705 error ("inlined_to pointer refers to itself");
2706 error_found = true;
2707 }
2708
d52f5295 2709 if (clone_of)
9c8305f8 2710 {
3dafb85c 2711 cgraph_node *n;
d52f5295
ML
2712 for (n = clone_of->clones; n; n = n->next_sibling_clone)
2713 if (n == this)
9c8305f8
JH
2714 break;
2715 if (!n)
2716 {
d52f5295 2717 error ("cgraph_node has wrong clone_of");
9c8305f8
JH
2718 error_found = true;
2719 }
2720 }
d52f5295 2721 if (clones)
9c8305f8 2722 {
3dafb85c 2723 cgraph_node *n;
d52f5295
ML
2724 for (n = clones; n; n = n->next_sibling_clone)
2725 if (n->clone_of != this)
9c8305f8
JH
2726 break;
2727 if (n)
2728 {
d52f5295 2729 error ("cgraph_node has wrong clone list");
9c8305f8
JH
2730 error_found = true;
2731 }
2732 }
d52f5295 2733 if ((prev_sibling_clone || next_sibling_clone) && !clone_of)
9c8305f8 2734 {
d52f5295 2735 error ("cgraph_node is in clone list but it is not clone");
9c8305f8
JH
2736 error_found = true;
2737 }
d52f5295 2738 if (!prev_sibling_clone && clone_of && clone_of->clones != this)
9c8305f8 2739 {
d52f5295 2740 error ("cgraph_node has wrong prev_clone pointer");
9c8305f8
JH
2741 error_found = true;
2742 }
d52f5295 2743 if (prev_sibling_clone && prev_sibling_clone->next_sibling_clone != this)
9c8305f8
JH
2744 {
2745 error ("double linked list of clones corrupted");
2746 error_found = true;
2747 }
2748
d52f5295 2749 if (analyzed && alias)
9c8305f8
JH
2750 {
2751 bool ref_found = false;
2752 int i;
3dafb85c 2753 ipa_ref *ref = NULL;
9c8305f8 2754
d52f5295 2755 if (callees)
9c8305f8
JH
2756 {
2757 error ("Alias has call edges");
2758 error_found = true;
2759 }
d52f5295 2760 for (i = 0; iterate_reference (i, ref); i++)
9c8305f8
JH
2761 if (ref->use != IPA_REF_ALIAS)
2762 {
2763 error ("Alias has non-alias reference");
2764 error_found = true;
2765 }
2766 else if (ref_found)
2767 {
2768 error ("Alias has more than one alias reference");
2769 error_found = true;
2770 }
2771 else
2772 ref_found = true;
2773 if (!ref_found)
2774 {
2775 error ("Analyzed alias has no reference");
2776 error_found = true;
2777 }
2778 }
d52f5295 2779 if (analyzed && thunk.thunk_p)
9c8305f8 2780 {
d52f5295 2781 if (!callees)
9c8305f8
JH
2782 {
2783 error ("No edge out of thunk node");
2784 error_found = true;
2785 }
d52f5295 2786 else if (callees->next_callee)
9c8305f8
JH
2787 {
2788 error ("More than one edge out of thunk node");
2789 error_found = true;
2790 }
d52f5295 2791 if (gimple_has_body_p (decl))
9c8305f8
JH
2792 {
2793 error ("Thunk is not supposed to have body");
2794 error_found = true;
2795 }
2796 }
d52f5295
ML
2797 else if (analyzed && gimple_has_body_p (decl)
2798 && !TREE_ASM_WRITTEN (decl)
2799 && (!DECL_EXTERNAL (decl) || global.inlined_to)
2800 && !flag_wpa)
9c8305f8
JH
2801 {
2802 if (this_cfun->cfg)
2803 {
6e2830c3 2804 hash_set<gimple> stmts;
71cafea9 2805 int i;
3dafb85c 2806 ipa_ref *ref = NULL;
71cafea9 2807
9c8305f8
JH
2808 /* Reach the trees by walking over the CFG, and note the
2809 enclosing basic-blocks in the call edges. */
2810 FOR_EACH_BB_FN (this_block, this_cfun)
71cafea9
JH
2811 {
2812 for (gsi = gsi_start_phis (this_block);
2813 !gsi_end_p (gsi); gsi_next (&gsi))
6e2830c3 2814 stmts.add (gsi_stmt (gsi));
71cafea9
JH
2815 for (gsi = gsi_start_bb (this_block);
2816 !gsi_end_p (gsi);
2817 gsi_next (&gsi))
2818 {
2819 gimple stmt = gsi_stmt (gsi);
6e2830c3 2820 stmts.add (stmt);
71cafea9
JH
2821 if (is_gimple_call (stmt))
2822 {
3dafb85c 2823 cgraph_edge *e = get_edge (stmt);
71cafea9
JH
2824 tree decl = gimple_call_fndecl (stmt);
2825 if (e)
2826 {
2827 if (e->aux)
2828 {
2829 error ("shared call_stmt:");
2830 cgraph_debug_gimple_stmt (this_cfun, stmt);
2831 error_found = true;
2832 }
2833 if (!e->indirect_unknown_callee)
2834 {
2835 if (verify_edge_corresponds_to_fndecl (e, decl))
2836 {
2837 error ("edge points to wrong declaration:");
67348ccc 2838 debug_tree (e->callee->decl);
71cafea9
JH
2839 fprintf (stderr," Instead of:");
2840 debug_tree (decl);
2841 error_found = true;
2842 }
2843 }
2844 else if (decl)
2845 {
2846 error ("an indirect edge with unknown callee "
2847 "corresponding to a call_stmt with "
2848 "a known declaration:");
2849 error_found = true;
2850 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2851 }
2852 e->aux = (void *)1;
2853 }
2854 else if (decl)
2855 {
2856 error ("missing callgraph edge for call stmt:");
2857 cgraph_debug_gimple_stmt (this_cfun, stmt);
2858 error_found = true;
2859 }
2860 }
2861 }
9c8305f8 2862 }
d52f5295 2863 for (i = 0; iterate_reference (i, ref); i++)
6e2830c3 2864 if (ref->stmt && !stmts.contains (ref->stmt))
71cafea9
JH
2865 {
2866 error ("reference to dead statement");
2867 cgraph_debug_gimple_stmt (this_cfun, ref->stmt);
2868 error_found = true;
2869 }
9c8305f8
JH
2870 }
2871 else
2872 /* No CFG available?! */
2873 gcc_unreachable ();
2874
d52f5295 2875 for (e = callees; e; e = e->next_callee)
9c8305f8
JH
2876 {
2877 if (!e->aux)
2878 {
2879 error ("edge %s->%s has no corresponding call_stmt",
fec39fa6
TS
2880 identifier_to_locale (e->caller->name ()),
2881 identifier_to_locale (e->callee->name ()));
9c8305f8
JH
2882 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2883 error_found = true;
2884 }
2885 e->aux = 0;
2886 }
d52f5295 2887 for (e = indirect_calls; e; e = e->next_callee)
9c8305f8 2888 {
042ae7d2 2889 if (!e->aux && !e->speculative)
9c8305f8
JH
2890 {
2891 error ("an indirect edge from %s has no corresponding call_stmt",
fec39fa6 2892 identifier_to_locale (e->caller->name ()));
9c8305f8
JH
2893 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
2894 error_found = true;
2895 }
2896 e->aux = 0;
2897 }
2898 }
2899 if (error_found)
2900 {
d52f5295 2901 dump (stderr);
9c8305f8
JH
2902 internal_error ("verify_cgraph_node failed");
2903 }
2904 timevar_pop (TV_CGRAPH_VERIFY);
2905}
2906
2907/* Verify whole cgraph structure. */
2908DEBUG_FUNCTION void
d52f5295 2909cgraph_node::verify_cgraph_nodes (void)
9c8305f8 2910{
3dafb85c 2911 cgraph_node *node;
9c8305f8
JH
2912
2913 if (seen_error ())
2914 return;
2915
2916 FOR_EACH_FUNCTION (node)
d52f5295 2917 node->verify ();
9c8305f8 2918}
48f4a6fa 2919
d52f5295 2920/* Walk the alias chain to return the function cgraph_node is alias of.
e70670cf
JH
2921 Walk through thunk, too.
2922 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
2923
d52f5295
ML
2924cgraph_node *
2925cgraph_node::function_symbol (enum availability *availability)
e70670cf 2926{
1113596f 2927 cgraph_node *node = this;
d52f5295 2928
e70670cf
JH
2929 do
2930 {
1113596f 2931 node = node->ultimate_alias_target (availability);
e70670cf
JH
2932 if (node->thunk.thunk_p)
2933 {
2934 node = node->callees->callee;
2935 if (availability)
2936 {
2937 enum availability a;
d52f5295 2938 a = node->get_availability ();
e70670cf
JH
2939 if (a < *availability)
2940 *availability = a;
2941 }
d52f5295 2942 node = node->ultimate_alias_target (availability);
e70670cf
JH
2943 }
2944 } while (node && node->thunk.thunk_p);
2945 return node;
2946}
2947
d52f5295
ML
2948/* When doing LTO, read cgraph_node's body from disk if it is not already
2949 present. */
a2e2a668
JH
2950
2951bool
d52f5295 2952cgraph_node::get_body (void)
a2e2a668 2953{
3dafb85c 2954 lto_file_decl_data *file_data;
a2e2a668
JH
2955 const char *data, *name;
2956 size_t len;
d52f5295 2957 tree decl = this->decl;
a2e2a668
JH
2958
2959 if (DECL_RESULT (decl))
2960 return false;
2961
2962 gcc_assert (in_lto_p);
2963
917dd9bf
JH
2964 timevar_push (TV_IPA_LTO_GIMPLE_IN);
2965
d52f5295 2966 file_data = lto_file_data;
a2e2a668
JH
2967 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2968
2969 /* We may have renamed the declaration, e.g., a static function. */
2970 name = lto_get_decl_name_mapping (file_data, name);
2971
2972 data = lto_get_section_data (file_data, LTO_section_function_body,
2973 name, &len);
2974 if (!data)
a2e2a668
JH
2975 fatal_error ("%s: section %s is missing",
2976 file_data->file_name,
2977 name);
a2e2a668
JH
2978
2979 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
2980
d52f5295 2981 lto_input_function_body (file_data, this, data);
a2e2a668
JH
2982 lto_stats.num_function_bodies++;
2983 lto_free_section_data (file_data, LTO_section_function_body, name,
2984 data, len);
d52f5295 2985 lto_free_function_in_decl_state_for_node (this);
917dd9bf
JH
2986
2987 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
2988
a2e2a668
JH
2989 return true;
2990}
2991
4484a35a
AM
2992/* Verify if the type of the argument matches that of the function
2993 declaration. If we cannot verify this or there is a mismatch,
2994 return false. */
2995
2996static bool
2997gimple_check_call_args (gimple stmt, tree fndecl, bool args_count_match)
2998{
2999 tree parms, p;
3000 unsigned int i, nargs;
3001
3002 /* Calls to internal functions always match their signature. */
3003 if (gimple_call_internal_p (stmt))
3004 return true;
3005
3006 nargs = gimple_call_num_args (stmt);
3007
3008 /* Get argument types for verification. */
3009 if (fndecl)
3010 parms = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3011 else
3012 parms = TYPE_ARG_TYPES (gimple_call_fntype (stmt));
3013
3014 /* Verify if the type of the argument matches that of the function
3015 declaration. If we cannot verify this or there is a mismatch,
3016 return false. */
3017 if (fndecl && DECL_ARGUMENTS (fndecl))
3018 {
3019 for (i = 0, p = DECL_ARGUMENTS (fndecl);
3020 i < nargs;
3021 i++, p = DECL_CHAIN (p))
3022 {
3023 tree arg;
3024 /* We cannot distinguish a varargs function from the case
3025 of excess parameters, still deferring the inlining decision
3026 to the callee is possible. */
3027 if (!p)
3028 break;
3029 arg = gimple_call_arg (stmt, i);
3030 if (p == error_mark_node
5147d10a 3031 || DECL_ARG_TYPE (p) == error_mark_node
4484a35a
AM
3032 || arg == error_mark_node
3033 || (!types_compatible_p (DECL_ARG_TYPE (p), TREE_TYPE (arg))
3034 && !fold_convertible_p (DECL_ARG_TYPE (p), arg)))
3035 return false;
3036 }
3037 if (args_count_match && p)
3038 return false;
3039 }
3040 else if (parms)
3041 {
3042 for (i = 0, p = parms; i < nargs; i++, p = TREE_CHAIN (p))
3043 {
3044 tree arg;
3045 /* If this is a varargs function defer inlining decision
3046 to callee. */
3047 if (!p)
3048 break;
3049 arg = gimple_call_arg (stmt, i);
3050 if (TREE_VALUE (p) == error_mark_node
3051 || arg == error_mark_node
3052 || TREE_CODE (TREE_VALUE (p)) == VOID_TYPE
3053 || (!types_compatible_p (TREE_VALUE (p), TREE_TYPE (arg))
3054 && !fold_convertible_p (TREE_VALUE (p), arg)))
3055 return false;
3056 }
3057 }
3058 else
3059 {
3060 if (nargs != 0)
3061 return false;
3062 }
3063 return true;
3064}
3065
3066/* Verify if the type of the argument and lhs of CALL_STMT matches
3067 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3068 true, the arg count needs to be the same.
3069 If we cannot verify this or there is a mismatch, return false. */
3070
3071bool
3072gimple_check_call_matching_types (gimple call_stmt, tree callee,
3073 bool args_count_match)
3074{
3075 tree lhs;
3076
3077 if ((DECL_RESULT (callee)
3078 && !DECL_BY_REFERENCE (DECL_RESULT (callee))
3079 && (lhs = gimple_call_lhs (call_stmt)) != NULL_TREE
3080 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee)),
3081 TREE_TYPE (lhs))
3082 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee)), lhs))
3083 || !gimple_check_call_args (call_stmt, callee, args_count_match))
3084 return false;
3085 return true;
3086}
3087
988d1653 3088#include "gt-cgraph.h"