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