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