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