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