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