]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraph.c
uses_allocator.cc: New.
[thirdparty/gcc.git] / gcc / cgraph.c
CommitLineData
e72fcfe8 1/* Callgraph handling code.
4139c7ef 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2bafad93 3 Free Software Foundation, Inc.
e72fcfe8
JH
4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
e72fcfe8
JH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
e72fcfe8 21
8a4a83ed 22/* This file contains basic routines manipulating call graph
c22cacf3 23
18c6ada9
JH
24The callgraph:
25
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
28 sharing.
29
30 The call-graph consist of nodes and edges represented via linked lists.
0550e7b7 31 Each function (external or not) corresponds to the unique node.
18c6ada9
JH
32
33 The mapping from declarations to call-graph nodes is done using hash table
0550e7b7
JH
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
18c6ada9 36
e33c6cd6
MJ
37 The callgraph at the moment does not represent all indirect calls or calls
38 from other compilation units. Flag NEEDED is set for each node that may be
39 accessed in such an invisible way and it shall be considered an entry point
40 to the callgraph.
41
42 On the other hand, the callgraph currently does contain some edges for
43 indirect calls with unknown callees which can be accessed through
44 indirect_calls field of a node. It should be noted however that at the
45 moment only calls which are potential candidates for indirect inlining are
46 added there.
18c6ada9 47
a418679d 48 Interprocedural information:
18c6ada9 49
a418679d 50 Callgraph is place to store data needed for interprocedural optimization.
1ae58c30 51 All data structures are divided into three components: local_info that
18c6ada9 52 is produced while analyzing the function, global_info that is result
1ae58c30 53 of global walking of the callgraph on the end of compilation and
18c6ada9
JH
54 rtl_info used by RTL backend to propagate data from already compiled
55 functions to their callers.
56
e33c6cd6
MJ
57 Moreover, each node has a uid which can be used to keep information in
58 on-the-side arrays. UIDs are reused and therefore reasonably dense.
59
18c6ada9
JH
60 Inlining plans:
61
62 The function inlining information is decided in advance and maintained
63 in the callgraph as so called inline plan.
1ae58c30 64 For each inlined call, the callee's node is cloned to represent the
1ea7e6ad 65 new function copy produced by inliner.
1ae58c30
KH
66 Each inlined call gets a unique corresponding clone node of the callee
67 and the data structure is updated while inlining is performed, so
68 the clones are eliminated and their callee edges redirected to the
c22cacf3 69 caller.
18c6ada9
JH
70
71 Each edge has "inline_failed" field. When the field is set to NULL,
2b8a92de 72 the call will be inlined. When it is non-NULL it contains a reason
8a4a83ed 73 why inlining wasn't performed. */
18c6ada9 74
e72fcfe8
JH
75#include "config.h"
76#include "system.h"
77#include "coretypes.h"
78#include "tm.h"
79#include "tree.h"
cd9c7bd2 80#include "tree-inline.h"
e72fcfe8
JH
81#include "langhooks.h"
82#include "hashtab.h"
83#include "toplev.h"
84#include "flags.h"
85#include "ggc.h"
86#include "debug.h"
87#include "target.h"
cd9c7bd2 88#include "basic-block.h"
1c4a429a 89#include "cgraph.h"
e69529cd 90#include "output.h"
dc0bfe6a 91#include "intl.h"
726a989a 92#include "gimple.h"
ef330312 93#include "tree-dump.h"
7a388ee4 94#include "tree-flow.h"
a63f2942 95#include "value-prof.h"
f9417da1 96#include "except.h"
1da2ed5f 97#include "diagnostic-core.h"
715a4e08 98#include "rtl.h"
a940b4d9 99#include "ipa-utils.h"
99fecd47 100#include "lto-streamer.h"
e7f23018 101#include "ipa-inline.h"
988d1653 102
671769c3
JH
103const char * const ld_plugin_symbol_resolution_names[]=
104{
105 "",
106 "undef",
107 "prevailing_def",
108 "prevailing_def_ironly",
109 "preempted_reg",
110 "preempted_ir",
111 "resolved_ir",
112 "resolved_exec",
113 "resolved_dyn"
114};
115
2563c224
RG
116static void cgraph_node_remove_callers (struct cgraph_node *node);
117static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
118static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
119
e72fcfe8 120/* Hash table used to convert declarations into nodes. */
ed2df68b 121static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
266ad5c8
JH
122/* Hash table used to convert assembler names into nodes. */
123static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
e72fcfe8
JH
124
125/* The linked list of cgraph nodes. */
1c4a429a 126struct cgraph_node *cgraph_nodes;
e72fcfe8 127
1668aabc
JH
128/* Queue of cgraph nodes scheduled to be lowered. */
129struct cgraph_node *cgraph_nodes_queue;
130
f45e0ad1 131/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
c0220ea4 132 secondary queue used during optimization to accommodate passes that
50674e96 133 may generate new functions that need to be optimized and expanded. */
f45e0ad1 134struct cgraph_node *cgraph_new_nodes;
953ff289 135
e72fcfe8 136/* Number of nodes in existence. */
1c4a429a 137int cgraph_n_nodes;
e72fcfe8 138
b58b1157
JH
139/* Maximal uid used in cgraph nodes. */
140int cgraph_max_uid;
141
9088c1cc
MJ
142/* Maximal uid used in cgraph edges. */
143int cgraph_edge_max_uid;
144
dafc5b82
JH
145/* Set when whole unit has been analyzed so we can access global info. */
146bool cgraph_global_info_ready = false;
147
f45e0ad1
JH
148/* What state callgraph is in right now. */
149enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
150
cd9c7bd2
JH
151/* Set when the cgraph is fully build and the basic flags are computed. */
152bool cgraph_function_flags_ready = false;
153
474eccc6
ILT
154/* Linked list of cgraph asm nodes. */
155struct cgraph_asm_node *cgraph_asm_nodes;
156
157/* Last node in cgraph_asm_nodes. */
158static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
159
160/* The order index of the next cgraph node to be created. This is
161 used so that we can sort the cgraph nodes in order by when we saw
162 them, to support -fno-toplevel-reorder. */
163int cgraph_order;
164
61502ca8 165/* List of hooks triggered on cgraph_edge events. */
9088c1cc
MJ
166struct cgraph_edge_hook_list {
167 cgraph_edge_hook hook;
168 void *data;
169 struct cgraph_edge_hook_list *next;
170};
171
61502ca8 172/* List of hooks triggered on cgraph_node events. */
9088c1cc
MJ
173struct cgraph_node_hook_list {
174 cgraph_node_hook hook;
175 void *data;
176 struct cgraph_node_hook_list *next;
177};
178
61502ca8 179/* List of hooks triggered on events involving two cgraph_edges. */
9088c1cc
MJ
180struct cgraph_2edge_hook_list {
181 cgraph_2edge_hook hook;
182 void *data;
183 struct cgraph_2edge_hook_list *next;
184};
185
61502ca8 186/* List of hooks triggered on events involving two cgraph_nodes. */
9088c1cc
MJ
187struct cgraph_2node_hook_list {
188 cgraph_2node_hook hook;
189 void *data;
190 struct cgraph_2node_hook_list *next;
191};
192
193/* List of hooks triggered when an edge is removed. */
194struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
195/* List of hooks triggered when a node is removed. */
196struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
197/* List of hooks triggered when an edge is duplicated. */
198struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
199/* List of hooks triggered when a node is duplicated. */
200struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
129a37fc
JH
201/* List of hooks triggered when an function is inserted. */
202struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
9088c1cc 203
2fb16412
MJ
204/* Head of a linked list of unused (freed) call graph nodes.
205 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
206static GTY(()) struct cgraph_node *free_nodes;
934cb78a
MJ
207/* Head of a linked list of unused (freed) call graph edges.
208 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
209static GTY(()) struct cgraph_edge *free_edges;
210
2fb16412
MJ
211/* Macros to access the next item in the list of free cgraph nodes and
212 edges. */
213#define NEXT_FREE_NODE(NODE) (NODE)->next
934cb78a 214#define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
9088c1cc
MJ
215
216/* Register HOOK to be called with DATA on each removed edge. */
217struct cgraph_edge_hook_list *
218cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
219{
220 struct cgraph_edge_hook_list *entry;
221 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
222
223 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
224 entry->hook = hook;
225 entry->data = data;
226 entry->next = NULL;
227 while (*ptr)
228 ptr = &(*ptr)->next;
229 *ptr = entry;
230 return entry;
231}
232
233/* Remove ENTRY from the list of hooks called on removing edges. */
234void
235cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
236{
237 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
238
239 while (*ptr != entry)
240 ptr = &(*ptr)->next;
241 *ptr = entry->next;
934cb78a 242 free (entry);
9088c1cc
MJ
243}
244
245/* Call all edge removal hooks. */
246static void
247cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
248{
249 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
250 while (entry)
251 {
252 entry->hook (e, entry->data);
253 entry = entry->next;
254 }
255}
256
257/* Register HOOK to be called with DATA on each removed node. */
258struct cgraph_node_hook_list *
259cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
260{
261 struct cgraph_node_hook_list *entry;
262 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
263
264 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
265 entry->hook = hook;
266 entry->data = data;
267 entry->next = NULL;
268 while (*ptr)
269 ptr = &(*ptr)->next;
270 *ptr = entry;
271 return entry;
272}
273
274/* Remove ENTRY from the list of hooks called on removing nodes. */
275void
276cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
277{
278 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
279
280 while (*ptr != entry)
281 ptr = &(*ptr)->next;
282 *ptr = entry->next;
934cb78a 283 free (entry);
9088c1cc
MJ
284}
285
286/* Call all node removal hooks. */
287static void
288cgraph_call_node_removal_hooks (struct cgraph_node *node)
289{
290 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
291 while (entry)
292 {
293 entry->hook (node, entry->data);
294 entry = entry->next;
295 }
296}
297
6544865a 298/* Register HOOK to be called with DATA on each inserted node. */
129a37fc
JH
299struct cgraph_node_hook_list *
300cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
301{
302 struct cgraph_node_hook_list *entry;
303 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
304
305 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
306 entry->hook = hook;
307 entry->data = data;
308 entry->next = NULL;
309 while (*ptr)
310 ptr = &(*ptr)->next;
311 *ptr = entry;
312 return entry;
313}
314
6544865a 315/* Remove ENTRY from the list of hooks called on inserted nodes. */
129a37fc
JH
316void
317cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
318{
319 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
320
321 while (*ptr != entry)
322 ptr = &(*ptr)->next;
323 *ptr = entry->next;
934cb78a 324 free (entry);
129a37fc
JH
325}
326
6544865a 327/* Call all node insertion hooks. */
129a37fc
JH
328void
329cgraph_call_function_insertion_hooks (struct cgraph_node *node)
330{
331 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
332 while (entry)
333 {
334 entry->hook (node, entry->data);
335 entry = entry->next;
336 }
337}
338
9088c1cc
MJ
339/* Register HOOK to be called with DATA on each duplicated edge. */
340struct cgraph_2edge_hook_list *
341cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
342{
343 struct cgraph_2edge_hook_list *entry;
344 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
345
346 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
347 entry->hook = hook;
348 entry->data = data;
349 entry->next = NULL;
350 while (*ptr)
351 ptr = &(*ptr)->next;
352 *ptr = entry;
353 return entry;
354}
355
356/* Remove ENTRY from the list of hooks called on duplicating edges. */
357void
358cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
359{
360 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
361
362 while (*ptr != entry)
363 ptr = &(*ptr)->next;
364 *ptr = entry->next;
934cb78a 365 free (entry);
9088c1cc
MJ
366}
367
368/* Call all edge duplication hooks. */
369static void
370cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
371 struct cgraph_edge *cs2)
372{
373 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
374 while (entry)
375 {
376 entry->hook (cs1, cs2, entry->data);
377 entry = entry->next;
378 }
379}
380
381/* Register HOOK to be called with DATA on each duplicated node. */
382struct cgraph_2node_hook_list *
383cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
384{
385 struct cgraph_2node_hook_list *entry;
386 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
387
388 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
389 entry->hook = hook;
390 entry->data = data;
391 entry->next = NULL;
392 while (*ptr)
393 ptr = &(*ptr)->next;
394 *ptr = entry;
395 return entry;
396}
397
398/* Remove ENTRY from the list of hooks called on duplicating nodes. */
399void
400cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
401{
402 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
403
404 while (*ptr != entry)
405 ptr = &(*ptr)->next;
406 *ptr = entry->next;
934cb78a 407 free (entry);
9088c1cc
MJ
408}
409
410/* Call all node duplication hooks. */
411static void
412cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
413 struct cgraph_node *node2)
414{
415 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
416 while (entry)
417 {
418 entry->hook (node1, node2, entry->data);
419 entry = entry->next;
420 }
421}
422
e72fcfe8
JH
423/* Returns a hash code for P. */
424
425static hashval_t
439f7bc3 426hash_node (const void *p)
e72fcfe8 427{
cceb1885 428 const struct cgraph_node *n = (const struct cgraph_node *) p;
6f312d18 429 return (hashval_t) DECL_UID (n->decl);
e72fcfe8
JH
430}
431
4537ec0c 432
6356f892 433/* Returns nonzero if P1 and P2 are equal. */
e72fcfe8
JH
434
435static int
439f7bc3 436eq_node (const void *p1, const void *p2)
e72fcfe8 437{
cceb1885
GDR
438 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
439 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
6f312d18 440 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
e72fcfe8
JH
441}
442
b2583345 443/* Allocate new callgraph node. */
0550e7b7 444
b2583345
JJ
445static inline struct cgraph_node *
446cgraph_allocate_node (void)
18c6ada9
JH
447{
448 struct cgraph_node *node;
449
2fb16412
MJ
450 if (free_nodes)
451 {
452 node = free_nodes;
453 free_nodes = NEXT_FREE_NODE (node);
454 }
455 else
456 {
a9429e29 457 node = ggc_alloc_cleared_cgraph_node ();
2fb16412
MJ
458 node->uid = cgraph_max_uid++;
459 }
460
b2583345
JJ
461 return node;
462}
463
464/* Allocate new callgraph node and insert it into basic data structures. */
465
466static struct cgraph_node *
a358e188 467cgraph_create_node_1 (void)
b2583345
JJ
468{
469 struct cgraph_node *node = cgraph_allocate_node ();
470
18c6ada9 471 node->next = cgraph_nodes;
474eccc6 472 node->order = cgraph_order++;
18c6ada9
JH
473 if (cgraph_nodes)
474 cgraph_nodes->previous = node;
475 node->previous = NULL;
5fefcf92 476 node->frequency = NODE_FREQUENCY_NORMAL;
db0bf14f 477 node->count_materialization_scale = REG_BR_PROB_BASE;
369451ec 478 ipa_empty_ref_list (&node->ref_list);
18c6ada9
JH
479 cgraph_nodes = node;
480 cgraph_n_nodes++;
481 return node;
482}
483
e72fcfe8 484/* Return cgraph node assigned to DECL. Create new one when needed. */
0550e7b7 485
1c4a429a 486struct cgraph_node *
a358e188 487cgraph_create_node (tree decl)
e72fcfe8 488{
6f312d18 489 struct cgraph_node key, *node, **slot;
e72fcfe8 490
341c100f 491 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
988d1653 492
e72fcfe8 493 if (!cgraph_hash)
ed2df68b 494 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
e72fcfe8 495
6f312d18 496 key.decl = decl;
6f312d18 497 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
a358e188 498 gcc_assert (!*slot);
6f312d18 499
a358e188 500 node = cgraph_create_node_1 ();
e72fcfe8 501 node->decl = decl;
e72fcfe8 502 *slot = node;
5c2e00ee 503 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
e72fcfe8 504 {
a358e188 505 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
e72fcfe8
JH
506 node->next_nested = node->origin->nested;
507 node->origin->nested = node;
508 }
1aeaf0f7
JJ
509 if (assembler_name_hash)
510 {
511 void **aslot;
512 tree name = DECL_ASSEMBLER_NAME (decl);
513
514 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
515 decl_assembler_name_hash (name),
516 INSERT);
517 /* We can have multiple declarations with same assembler name. For C++
518 it is __builtin_strlen and strlen, for instance. Do we need to
519 record them all? Original implementation marked just first one
520 so lets hope for the best. */
521 if (*aslot == NULL)
522 *aslot = node;
523 }
e72fcfe8
JH
524 return node;
525}
526
a358e188
MJ
527/* Try to find a call graph node for declaration DECL and if it does not exist,
528 create it. */
529
530struct cgraph_node *
531cgraph_get_create_node (tree decl)
532{
533 struct cgraph_node *node;
534
535 node = cgraph_get_node (decl);
536 if (node)
537 return node;
538
539 return cgraph_create_node (decl);
540}
541
87e7b310
JH
542/* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
543 the function body is associated with (not neccesarily cgraph_node (DECL). */
b2583345 544
6744a6ab 545static struct cgraph_node *
87e7b310 546cgraph_same_body_alias_1 (struct cgraph_node *decl_node, tree alias, tree decl)
b2583345 547{
87e7b310 548 struct cgraph_node key, *alias_node, **slot;
b2583345
JJ
549
550 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
551 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
b2583345
JJ
552
553 key.decl = alias;
554
555 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
556
557 /* If the cgraph_node has been already created, fail. */
558 if (*slot)
6744a6ab 559 return NULL;
b2583345
JJ
560
561 alias_node = cgraph_allocate_node ();
562 alias_node->decl = alias;
563 alias_node->same_body_alias = 1;
564 alias_node->same_body = decl_node;
565 alias_node->previous = NULL;
566 if (decl_node->same_body)
567 decl_node->same_body->previous = alias_node;
568 alias_node->next = decl_node->same_body;
6744a6ab 569 alias_node->thunk.alias = decl;
b2583345
JJ
570 decl_node->same_body = alias_node;
571 *slot = alias_node;
6744a6ab
JH
572 return alias_node;
573}
574
051f8cc6 575/* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
a358e188 576 and NULL otherwise.
6744a6ab 577 Same body aliases are output whenever the body of DECL is output,
a358e188 578 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
6744a6ab 579
051f8cc6 580struct cgraph_node *
87e7b310 581cgraph_same_body_alias (struct cgraph_node *decl_node, tree alias, tree decl)
6744a6ab
JH
582{
583#ifndef ASM_OUTPUT_DEF
584 /* If aliases aren't supported by the assembler, fail. */
051f8cc6 585 return NULL;
6744a6ab
JH
586#endif
587
588 /*gcc_assert (!assembler_name_hash);*/
589
87e7b310 590 return cgraph_same_body_alias_1 (decl_node, alias, decl);
6744a6ab
JH
591}
592
051f8cc6 593/* Add thunk alias into callgraph. The alias declaration is ALIAS and it
61502ca8 594 aliases DECL with an adjustments made into the first parameter.
051f8cc6
JH
595 See comments in thunk_adjust for detail on the parameters. */
596
597struct cgraph_node *
c47d0034
JH
598cgraph_add_thunk (struct cgraph_node *decl_node ATTRIBUTE_UNUSED,
599 tree alias, tree decl,
87e7b310 600 bool this_adjusting,
6744a6ab
JH
601 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
602 tree virtual_offset,
603 tree real_alias)
604{
c47d0034 605 struct cgraph_node *node;
6744a6ab 606
c47d0034 607 node = cgraph_get_node (alias);
6744a6ab
JH
608 if (node)
609 {
610 gcc_assert (node->local.finalized);
be330ed4
JH
611 gcc_assert (!node->alias);
612 gcc_assert (!node->thunk.thunk_p);
6744a6ab
JH
613 cgraph_remove_node (node);
614 }
615
c47d0034 616 node = cgraph_create_node (alias);
77a74ed7 617 gcc_checking_assert (!virtual_offset
e562bf36
RG
618 || double_int_equal_p
619 (tree_to_double_int (virtual_offset),
620 shwi_to_double_int (virtual_value)));
6744a6ab
JH
621 node->thunk.fixed_offset = fixed_offset;
622 node->thunk.this_adjusting = this_adjusting;
623 node->thunk.virtual_value = virtual_value;
624 node->thunk.virtual_offset_p = virtual_offset != NULL;
625 node->thunk.alias = real_alias;
626 node->thunk.thunk_p = true;
c47d0034
JH
627 node->local.finalized = true;
628
629 if (cgraph_decide_is_function_needed (node, decl))
630 cgraph_mark_needed_node (node);
631
632 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
633 || (DECL_VIRTUAL_P (decl)
634 && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
635 cgraph_mark_reachable_node (node);
051f8cc6 636 return node;
b2583345
JJ
637}
638
e10aaec0
JH
639/* Returns the cgraph node assigned to DECL or NULL if no cgraph node
640 is assigned. */
641
642struct cgraph_node *
051f8cc6 643cgraph_get_node_or_alias (const_tree decl)
e10aaec0
JH
644{
645 struct cgraph_node key, *node = NULL, **slot;
646
647 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
648
649 if (!cgraph_hash)
650 return NULL;
651
051f8cc6 652 key.decl = CONST_CAST2 (tree, const_tree, decl);
e10aaec0
JH
653
654 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
655 NO_INSERT);
656
657 if (slot && *slot)
658 node = *slot;
659 return node;
660}
661
f9329c35
DS
662/* Returns the cgraph node assigned to DECL or NULL if no cgraph node
663 is assigned. */
664
665struct cgraph_node *
051f8cc6 666cgraph_get_node (const_tree decl)
f9329c35
DS
667{
668 struct cgraph_node key, *node = NULL, **slot;
669
670 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
671
672 if (!cgraph_hash)
986ad133 673 return NULL;
f9329c35 674
051f8cc6 675 key.decl = CONST_CAST2 (tree, const_tree, decl);
f9329c35
DS
676
677 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
678 NO_INSERT);
679
680 if (slot && *slot)
b2583345
JJ
681 {
682 node = *slot;
683 if (node->same_body_alias)
684 node = node->same_body;
685 }
f9329c35
DS
686 return node;
687}
688
ea99e0be
JH
689/* Insert already constructed node into hashtable. */
690
691void
692cgraph_insert_node_to_hashtable (struct cgraph_node *node)
693{
694 struct cgraph_node **slot;
695
696 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
697
698 gcc_assert (!*slot);
699 *slot = node;
700}
701
266ad5c8
JH
702/* Returns a hash code for P. */
703
704static hashval_t
705hash_node_by_assembler_name (const void *p)
706{
707 const struct cgraph_node *n = (const struct cgraph_node *) p;
708 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
709}
710
711/* Returns nonzero if P1 and P2 are equal. */
712
713static int
714eq_assembler_name (const void *p1, const void *p2)
715{
716 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
717 const_tree name = (const_tree)p2;
718 return (decl_assembler_name_equal (n1->decl, name));
719}
bedb9fc0
RH
720
721/* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
722 Return NULL if there's no such node. */
723
724struct cgraph_node *
725cgraph_node_for_asm (tree asmname)
726{
727 struct cgraph_node *node;
266ad5c8 728 void **slot;
bedb9fc0 729
266ad5c8
JH
730 if (!assembler_name_hash)
731 {
732 assembler_name_hash =
733 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
734 NULL);
735 for (node = cgraph_nodes; node; node = node->next)
736 if (!node->global.inlined_to)
737 {
738 tree name = DECL_ASSEMBLER_NAME (node->decl);
739 slot = htab_find_slot_with_hash (assembler_name_hash, name,
740 decl_assembler_name_hash (name),
741 INSERT);
742 /* We can have multiple declarations with same assembler name. For C++
743 it is __builtin_strlen and strlen, for instance. Do we need to
744 record them all? Original implementation marked just first one
745 so lets hope for the best. */
b2583345
JJ
746 if (!*slot)
747 *slot = node;
748 if (node->same_body)
749 {
750 struct cgraph_node *alias;
751
752 for (alias = node->same_body; alias; alias = alias->next)
753 {
754 hashval_t hash;
755 name = DECL_ASSEMBLER_NAME (alias->decl);
756 hash = decl_assembler_name_hash (name);
757 slot = htab_find_slot_with_hash (assembler_name_hash, name,
758 hash, INSERT);
759 if (!*slot)
760 *slot = alias;
761 }
762 }
266ad5c8
JH
763 }
764 }
765
766 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
767 decl_assembler_name_hash (asmname),
768 NO_INSERT);
bedb9fc0 769
266ad5c8 770 if (slot)
b2583345
JJ
771 {
772 node = (struct cgraph_node *) *slot;
773 if (node->same_body_alias)
774 node = node->same_body;
775 return node;
776 }
bedb9fc0
RH
777 return NULL;
778}
779
70d539ce
JH
780/* Returns a hash value for X (which really is a die_struct). */
781
782static hashval_t
783edge_hash (const void *x)
784{
741ac903 785 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
70d539ce
JH
786}
787
788/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
789
790static int
791edge_eq (const void *x, const void *y)
792{
741ac903 793 return ((const struct cgraph_edge *) x)->call_stmt == y;
70d539ce
JH
794}
795
e33c6cd6
MJ
796/* Add call graph edge E to call site hash of its caller. */
797
798static inline void
799cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
800{
801 void **slot;
802 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
803 e->call_stmt,
804 htab_hash_pointer (e->call_stmt),
805 INSERT);
806 gcc_assert (!*slot);
807 *slot = e;
808}
726a989a
RB
809
810/* Return the callgraph edge representing the GIMPLE_CALL statement
811 CALL_STMT. */
812
18c6ada9 813struct cgraph_edge *
726a989a 814cgraph_edge (struct cgraph_node *node, gimple call_stmt)
18c6ada9 815{
70d539ce
JH
816 struct cgraph_edge *e, *e2;
817 int n = 0;
818
819 if (node->call_site_hash)
f883e0a7
KG
820 return (struct cgraph_edge *)
821 htab_find_with_hash (node->call_site_hash, call_stmt,
52c76998 822 htab_hash_pointer (call_stmt));
18c6ada9
JH
823
824 /* This loop may turn out to be performance problem. In such case adding
825 hashtables into call nodes with very many edges is probably best
2b8a92de 826 solution. It is not good idea to add pointer into CALL_EXPR itself
18c6ada9
JH
827 because we want to make possible having multiple cgraph nodes representing
828 different clones of the same body before the body is actually cloned. */
e33c6cd6 829 for (e = node->callees; e; e = e->next_callee)
70d539ce
JH
830 {
831 if (e->call_stmt == call_stmt)
832 break;
833 n++;
834 }
726a989a 835
e33c6cd6
MJ
836 if (!e)
837 for (e = node->indirect_calls; e; e = e->next_callee)
838 {
839 if (e->call_stmt == call_stmt)
840 break;
841 n++;
842 }
843
70d539ce
JH
844 if (n > 100)
845 {
846 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
847 for (e2 = node->callees; e2; e2 = e2->next_callee)
e33c6cd6
MJ
848 cgraph_add_edge_to_call_site_hash (e2);
849 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
850 cgraph_add_edge_to_call_site_hash (e2);
70d539ce 851 }
726a989a 852
18c6ada9
JH
853 return e;
854}
855
726a989a 856
9187e02d 857/* Change field call_stmt of edge E to NEW_STMT. */
0550e7b7 858
70d539ce 859void
726a989a 860cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
70d539ce 861{
e33c6cd6
MJ
862 tree decl;
863
70d539ce
JH
864 if (e->caller->call_site_hash)
865 {
866 htab_remove_elt_with_hash (e->caller->call_site_hash,
867 e->call_stmt,
868 htab_hash_pointer (e->call_stmt));
869 }
e33c6cd6 870
70d539ce 871 e->call_stmt = new_stmt;
e33c6cd6
MJ
872 if (e->indirect_unknown_callee
873 && (decl = gimple_call_fndecl (new_stmt)))
874 {
875 /* Constant propagation (and possibly also inlining?) can turn an
876 indirect call into a direct one. */
a358e188 877 struct cgraph_node *new_callee = cgraph_get_node (decl);
e33c6cd6 878
a358e188 879 gcc_checking_assert (new_callee);
ce47fda3 880 cgraph_make_edge_direct (e, new_callee, 0);
e33c6cd6
MJ
881 }
882
b6fa5b01 883 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
2505c5ed 884 e->can_throw_external = stmt_can_throw_external (new_stmt);
b6fa5b01 885 pop_cfun ();
70d539ce 886 if (e->caller->call_site_hash)
e33c6cd6 887 cgraph_add_edge_to_call_site_hash (e);
70d539ce
JH
888}
889
9b2a5ef7
RH
890/* Like cgraph_set_call_stmt but walk the clone tree and update all
891 clones sharing the same function body. */
9187e02d
JH
892
893void
894cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
895 gimple old_stmt, gimple new_stmt)
896{
897 struct cgraph_node *node;
898 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
899
900 if (edge)
901 cgraph_set_call_stmt (edge, new_stmt);
9b2a5ef7
RH
902
903 node = orig->clones;
904 if (node)
905 while (node != orig)
9187e02d
JH
906 {
907 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
908 if (edge)
909 cgraph_set_call_stmt (edge, new_stmt);
910 if (node->clones)
911 node = node->clones;
912 else if (node->next_sibling_clone)
913 node = node->next_sibling_clone;
914 else
915 {
916 while (node != orig && !node->next_sibling_clone)
917 node = node->clone_of;
918 if (node != orig)
919 node = node->next_sibling_clone;
920 }
921 }
922}
923
924/* Like cgraph_create_edge walk the clone tree and update all clones sharing
47cb0d7d
JH
925 same function body. If clones already have edge for OLD_STMT; only
926 update the edge same way as cgraph_set_call_stmt_including_clones does.
b8698a0f 927
9187e02d 928 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
9b2a5ef7 929 frequencies of the clones. */
9187e02d
JH
930
931void
9b2a5ef7
RH
932cgraph_create_edge_including_clones (struct cgraph_node *orig,
933 struct cgraph_node *callee,
47cb0d7d 934 gimple old_stmt,
9b2a5ef7 935 gimple stmt, gcov_type count,
898b8927 936 int freq,
9187e02d
JH
937 cgraph_inline_failed_t reason)
938{
939 struct cgraph_node *node;
9b2a5ef7 940 struct cgraph_edge *edge;
9187e02d 941
6ce2002b 942 if (!cgraph_edge (orig, stmt))
9b2a5ef7 943 {
898b8927 944 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
9b2a5ef7
RH
945 edge->inline_failed = reason;
946 }
9187e02d 947
9b2a5ef7
RH
948 node = orig->clones;
949 if (node)
950 while (node != orig)
9187e02d 951 {
47cb0d7d
JH
952 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
953
954 /* It is possible that clones already contain the edge while
955 master didn't. Either we promoted indirect call into direct
956 call in the clone or we are processing clones of unreachable
61502ca8 957 master where edges has been removed. */
47cb0d7d
JH
958 if (edge)
959 cgraph_set_call_stmt (edge, stmt);
960 else if (!cgraph_edge (node, stmt))
9b2a5ef7
RH
961 {
962 edge = cgraph_create_edge (node, callee, stmt, count,
898b8927 963 freq);
9b2a5ef7
RH
964 edge->inline_failed = reason;
965 }
9187e02d
JH
966
967 if (node->clones)
968 node = node->clones;
969 else if (node->next_sibling_clone)
970 node = node->next_sibling_clone;
971 else
972 {
973 while (node != orig && !node->next_sibling_clone)
974 node = node->clone_of;
975 if (node != orig)
976 node = node->next_sibling_clone;
977 }
978 }
979}
980
e33c6cd6
MJ
981/* Allocate a cgraph_edge structure and fill it with data according to the
982 parameters of which only CALLEE can be NULL (when creating an indirect call
983 edge). */
e72fcfe8 984
e33c6cd6
MJ
985static struct cgraph_edge *
986cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
898b8927 987 gimple call_stmt, gcov_type count, int freq)
e72fcfe8 988{
934cb78a 989 struct cgraph_edge *edge;
18c6ada9 990
d7f09764
DN
991 /* LTO does not actually have access to the call_stmt since these
992 have not been loaded yet. */
993 if (call_stmt)
994 {
61502ca8 995 /* This is a rather expensive check possibly triggering
77a74ed7
NF
996 construction of call stmt hashtable. */
997 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
18c6ada9 998
d7f09764
DN
999 gcc_assert (is_gimple_call (call_stmt));
1000 }
b58b1157 1001
934cb78a
MJ
1002 if (free_edges)
1003 {
1004 edge = free_edges;
1005 free_edges = NEXT_FREE_EDGE (edge);
1006 }
1007 else
1008 {
a9429e29 1009 edge = ggc_alloc_cgraph_edge ();
934cb78a
MJ
1010 edge->uid = cgraph_edge_max_uid++;
1011 }
1012
18c6ada9 1013 edge->aux = NULL;
e72fcfe8
JH
1014 edge->caller = caller;
1015 edge->callee = callee;
e33c6cd6
MJ
1016 edge->prev_caller = NULL;
1017 edge->next_caller = NULL;
1018 edge->prev_callee = NULL;
1019 edge->next_callee = NULL;
1020
1021 edge->count = count;
1022 gcc_assert (count >= 0);
1023 edge->frequency = freq;
1024 gcc_assert (freq >= 0);
1025 gcc_assert (freq <= CGRAPH_FREQ_MAX);
e33c6cd6 1026
e0704a46 1027 edge->call_stmt = call_stmt;
b6fa5b01 1028 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
9f3f7d13
RG
1029 edge->can_throw_external
1030 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
b6fa5b01 1031 pop_cfun ();
e33c6cd6
MJ
1032 edge->call_stmt_cannot_inline_p =
1033 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1034 if (call_stmt && caller->call_site_hash)
1035 cgraph_add_edge_to_call_site_hash (edge);
1036
1037 edge->indirect_info = NULL;
1038 edge->indirect_inlining_edge = 0;
1039
1040 return edge;
1041}
1042
1043/* Create edge from CALLER to CALLEE in the cgraph. */
1044
1045struct cgraph_edge *
1046cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
898b8927 1047 gimple call_stmt, gcov_type count, int freq)
e33c6cd6
MJ
1048{
1049 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
898b8927 1050 count, freq);
e33c6cd6
MJ
1051
1052 edge->indirect_unknown_callee = 0;
1053 initialize_inline_failed (edge);
1054
e72fcfe8 1055 edge->next_caller = callee->callers;
2563c224
RG
1056 if (callee->callers)
1057 callee->callers->prev_caller = edge;
e72fcfe8 1058 edge->next_callee = caller->callees;
2563c224
RG
1059 if (caller->callees)
1060 caller->callees->prev_callee = edge;
e72fcfe8
JH
1061 caller->callees = edge;
1062 callee->callers = edge;
3dc9eaa6 1063
e33c6cd6
MJ
1064 return edge;
1065}
1066
ce47fda3
MJ
1067/* Allocate cgraph_indirect_call_info and set its fields to default values. */
1068
1069struct cgraph_indirect_call_info *
1070cgraph_allocate_init_indirect_info (void)
1071{
1072 struct cgraph_indirect_call_info *ii;
1073
1074 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1075 ii->param_index = -1;
1076 return ii;
1077}
e33c6cd6
MJ
1078
1079/* Create an indirect edge with a yet-undetermined callee where the call
1080 statement destination is a formal parameter of the caller with index
1081 PARAM_INDEX. */
1082
1083struct cgraph_edge *
1084cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
5f902d76 1085 int ecf_flags,
898b8927 1086 gcov_type count, int freq)
e33c6cd6
MJ
1087{
1088 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
898b8927 1089 count, freq);
e33c6cd6
MJ
1090
1091 edge->indirect_unknown_callee = 1;
3dc9eaa6
AN
1092 initialize_inline_failed (edge);
1093
ce47fda3 1094 edge->indirect_info = cgraph_allocate_init_indirect_info ();
5f902d76 1095 edge->indirect_info->ecf_flags = ecf_flags;
e33c6cd6
MJ
1096
1097 edge->next_callee = caller->indirect_calls;
1098 if (caller->indirect_calls)
1099 caller->indirect_calls->prev_callee = edge;
1100 caller->indirect_calls = edge;
1101
e72fcfe8
JH
1102 return edge;
1103}
1104
2563c224
RG
1105/* Remove the edge E from the list of the callers of the callee. */
1106
1107static inline void
1108cgraph_edge_remove_callee (struct cgraph_edge *e)
1109{
e33c6cd6 1110 gcc_assert (!e->indirect_unknown_callee);
2563c224
RG
1111 if (e->prev_caller)
1112 e->prev_caller->next_caller = e->next_caller;
1113 if (e->next_caller)
1114 e->next_caller->prev_caller = e->prev_caller;
1115 if (!e->prev_caller)
1116 e->callee->callers = e->next_caller;
1117}
1118
1119/* Remove the edge E from the list of the callees of the caller. */
1120
1121static inline void
1122cgraph_edge_remove_caller (struct cgraph_edge *e)
1123{
1124 if (e->prev_callee)
1125 e->prev_callee->next_callee = e->next_callee;
1126 if (e->next_callee)
1127 e->next_callee->prev_callee = e->prev_callee;
1128 if (!e->prev_callee)
e33c6cd6
MJ
1129 {
1130 if (e->indirect_unknown_callee)
1131 e->caller->indirect_calls = e->next_callee;
1132 else
1133 e->caller->callees = e->next_callee;
1134 }
70d539ce
JH
1135 if (e->caller->call_site_hash)
1136 htab_remove_elt_with_hash (e->caller->call_site_hash,
1137 e->call_stmt,
1138 htab_hash_pointer (e->call_stmt));
2563c224
RG
1139}
1140
934cb78a
MJ
1141/* Put the edge onto the free list. */
1142
1143static void
1144cgraph_free_edge (struct cgraph_edge *e)
1145{
1146 int uid = e->uid;
1147
1148 /* Clear out the edge so we do not dangle pointers. */
5c0466b5 1149 memset (e, 0, sizeof (*e));
934cb78a
MJ
1150 e->uid = uid;
1151 NEXT_FREE_EDGE (e) = free_edges;
1152 free_edges = e;
1153}
1154
2563c224 1155/* Remove the edge E in the cgraph. */
e72fcfe8 1156
cb967da5 1157void
18c6ada9 1158cgraph_remove_edge (struct cgraph_edge *e)
e72fcfe8 1159{
934cb78a 1160 /* Call all edge removal hooks. */
9088c1cc 1161 cgraph_call_edge_removal_hooks (e);
934cb78a 1162
e33c6cd6
MJ
1163 if (!e->indirect_unknown_callee)
1164 /* Remove from callers list of the callee. */
1165 cgraph_edge_remove_callee (e);
2563c224
RG
1166
1167 /* Remove from callees list of the callers. */
1168 cgraph_edge_remove_caller (e);
934cb78a
MJ
1169
1170 /* Put the edge onto the free list. */
1171 cgraph_free_edge (e);
e72fcfe8
JH
1172}
1173
e33c6cd6
MJ
1174/* Set callee of call graph edge E and add it to the corresponding set of
1175 callers. */
1176
1177static void
1178cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1179{
1180 e->prev_caller = NULL;
1181 if (n->callers)
1182 n->callers->prev_caller = e;
1183 e->next_caller = n->callers;
1184 n->callers = e;
1185 e->callee = n;
1186}
1187
18c6ada9
JH
1188/* Redirect callee of E to N. The function does not update underlying
1189 call expression. */
1190
1191void
1192cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1193{
2563c224
RG
1194 /* Remove from callers list of the current callee. */
1195 cgraph_edge_remove_callee (e);
18c6ada9 1196
2563c224 1197 /* Insert to callers list of the new callee. */
e33c6cd6
MJ
1198 cgraph_set_edge_callee (e, n);
1199}
1200
1201/* Make an indirect EDGE with an unknown callee an ordinary edge leading to
ce47fda3
MJ
1202 CALLEE. DELTA is an integer constant that is to be added to the this
1203 pointer (first parameter) to compensate for skipping a thunk adjustment. */
e33c6cd6
MJ
1204
1205void
ceeffab0 1206cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
ce47fda3 1207 HOST_WIDE_INT delta)
e33c6cd6
MJ
1208{
1209 edge->indirect_unknown_callee = 0;
ceeffab0 1210 edge->indirect_info->thunk_delta = delta;
e33c6cd6
MJ
1211
1212 /* Get the edge out of the indirect edge list. */
1213 if (edge->prev_callee)
1214 edge->prev_callee->next_callee = edge->next_callee;
1215 if (edge->next_callee)
1216 edge->next_callee->prev_callee = edge->prev_callee;
1217 if (!edge->prev_callee)
1218 edge->caller->indirect_calls = edge->next_callee;
1219
1220 /* Put it into the normal callee list */
1221 edge->prev_callee = NULL;
1222 edge->next_callee = edge->caller->callees;
1223 if (edge->caller->callees)
1224 edge->caller->callees->prev_callee = edge;
1225 edge->caller->callees = edge;
1226
1227 /* Insert to callers list of the new callee. */
1228 cgraph_set_edge_callee (edge, callee);
1229
1230 /* We need to re-determine the inlining status of the edge. */
1231 initialize_inline_failed (edge);
2563c224
RG
1232}
1233
726a989a
RB
1234
1235/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
4b685e14 1236 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
a9d24544
JJ
1237 of OLD_STMT if it was previously call statement.
1238 If NEW_STMT is NULL, the call has been dropped without any
1239 replacement. */
2bafad93 1240
9187e02d
JH
1241static void
1242cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
a9d24544
JJ
1243 gimple old_stmt, tree old_call,
1244 gimple new_stmt)
2bafad93 1245{
a9d24544
JJ
1246 tree new_call = (new_stmt && is_gimple_call (new_stmt))
1247 ? gimple_call_fndecl (new_stmt) : 0;
2bafad93 1248
4b685e14
JH
1249 /* We are seeing indirect calls, then there is nothing to update. */
1250 if (!new_call && !old_call)
1251 return;
1252 /* See if we turned indirect call into direct call or folded call to one builtin
61502ca8 1253 into different builtin. */
2bafad93
JJ
1254 if (old_call != new_call)
1255 {
1256 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1257 struct cgraph_edge *ne = NULL;
4b685e14
JH
1258 gcov_type count;
1259 int frequency;
2bafad93
JJ
1260
1261 if (e)
1262 {
e33c6cd6
MJ
1263 /* See if the edge is already there and has the correct callee. It
1264 might be so because of indirect inlining has already updated
97ba0040
JH
1265 it. We also might've cloned and redirected the edge. */
1266 if (new_call && e->callee)
1267 {
1268 struct cgraph_node *callee = e->callee;
1269 while (callee)
1270 {
1271 if (callee->decl == new_call
1272 || callee->former_clone_of == new_call)
1273 return;
1274 callee = callee->clone_of;
1275 }
1276 }
4b685e14
JH
1277
1278 /* Otherwise remove edge and create new one; we can't simply redirect
1279 since function has changed, so inline plan and other information
1280 attached to edge is invalid. */
4b685e14
JH
1281 count = e->count;
1282 frequency = e->frequency;
f8754107 1283 cgraph_remove_edge (e);
4b685e14 1284 }
a9d24544 1285 else if (new_call)
4b685e14
JH
1286 {
1287 /* We are seeing new direct call; compute profile info based on BB. */
1288 basic_block bb = gimple_bb (new_stmt);
1289 count = bb->count;
1290 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1291 bb);
2bafad93 1292 }
2bafad93 1293
4b685e14
JH
1294 if (new_call)
1295 {
a358e188 1296 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
898b8927 1297 new_stmt, count, frequency);
4b685e14
JH
1298 gcc_assert (ne->inline_failed);
1299 }
2bafad93 1300 }
4b685e14
JH
1301 /* We only updated the call stmt; update pointer in cgraph edge.. */
1302 else if (old_stmt != new_stmt)
1303 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
2bafad93
JJ
1304}
1305
9187e02d 1306/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
4b685e14
JH
1307 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1308 of OLD_STMT before it was updated (updating can happen inplace). */
9187e02d
JH
1309
1310void
4b685e14 1311cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
9187e02d 1312{
a358e188 1313 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
9187e02d
JH
1314 struct cgraph_node *node;
1315
a358e188 1316 gcc_checking_assert (orig);
4b685e14 1317 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
9187e02d
JH
1318 if (orig->clones)
1319 for (node = orig->clones; node != orig;)
1320 {
4b685e14 1321 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
9187e02d
JH
1322 if (node->clones)
1323 node = node->clones;
1324 else if (node->next_sibling_clone)
1325 node = node->next_sibling_clone;
1326 else
1327 {
1328 while (node != orig && !node->next_sibling_clone)
1329 node = node->clone_of;
1330 if (node != orig)
1331 node = node->next_sibling_clone;
1332 }
1333 }
1334}
1335
726a989a 1336
2563c224
RG
1337/* Remove all callees from the node. */
1338
1339void
1340cgraph_node_remove_callees (struct cgraph_node *node)
1341{
5c0466b5 1342 struct cgraph_edge *e, *f;
2563c224
RG
1343
1344 /* It is sufficient to remove the edges from the lists of callers of
1345 the callees. The callee list of the node can be zapped with one
1346 assignment. */
5c0466b5 1347 for (e = node->callees; e; e = f)
9088c1cc 1348 {
5c0466b5 1349 f = e->next_callee;
9088c1cc 1350 cgraph_call_edge_removal_hooks (e);
e33c6cd6
MJ
1351 if (!e->indirect_unknown_callee)
1352 cgraph_edge_remove_callee (e);
934cb78a 1353 cgraph_free_edge (e);
9088c1cc 1354 }
5f902d76
JH
1355 for (e = node->indirect_calls; e; e = f)
1356 {
1357 f = e->next_callee;
1358 cgraph_call_edge_removal_hooks (e);
1359 if (!e->indirect_unknown_callee)
1360 cgraph_edge_remove_callee (e);
1361 cgraph_free_edge (e);
1362 }
1363 node->indirect_calls = NULL;
2563c224 1364 node->callees = NULL;
70d539ce
JH
1365 if (node->call_site_hash)
1366 {
1367 htab_delete (node->call_site_hash);
1368 node->call_site_hash = NULL;
1369 }
2563c224
RG
1370}
1371
1372/* Remove all callers from the node. */
1373
1374static void
1375cgraph_node_remove_callers (struct cgraph_node *node)
1376{
5c0466b5 1377 struct cgraph_edge *e, *f;
2563c224
RG
1378
1379 /* It is sufficient to remove the edges from the lists of callees of
1380 the callers. The caller list of the node can be zapped with one
1381 assignment. */
5c0466b5 1382 for (e = node->callers; e; e = f)
9088c1cc 1383 {
5c0466b5 1384 f = e->next_caller;
9088c1cc
MJ
1385 cgraph_call_edge_removal_hooks (e);
1386 cgraph_edge_remove_caller (e);
934cb78a 1387 cgraph_free_edge (e);
9088c1cc 1388 }
2563c224 1389 node->callers = NULL;
18c6ada9
JH
1390}
1391
3a40c18a
JH
1392/* Release memory used to represent body of function NODE. */
1393
1394void
1395cgraph_release_function_body (struct cgraph_node *node)
1396{
936fc9ba 1397 if (DECL_STRUCT_FUNCTION (node->decl))
3a40c18a
JH
1398 {
1399 tree old_decl = current_function_decl;
1400 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
936fc9ba
JH
1401 if (cfun->gimple_df)
1402 {
1403 current_function_decl = node->decl;
1404 delete_tree_ssa ();
1405 delete_tree_cfg_annotations ();
1406 cfun->eh = NULL;
1407 current_function_decl = old_decl;
1408 }
1409 if (cfun->cfg)
1410 {
1411 gcc_assert (dom_computed[0] == DOM_NONE);
1412 gcc_assert (dom_computed[1] == DOM_NONE);
1413 clear_edges ();
1414 }
a63f2942
JH
1415 if (cfun->value_histograms)
1416 free_histograms ();
1417 gcc_assert (!current_loops);
3a40c18a 1418 pop_cfun();
936fc9ba
JH
1419 gimple_set_body (node->decl, NULL);
1420 VEC_free (ipa_opt_pass, heap,
0e3776db 1421 node->ipa_transforms_to_apply);
936fc9ba
JH
1422 /* Struct function hangs a lot of data that would leak if we didn't
1423 removed all pointers to it. */
1424 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1425 DECL_STRUCT_FUNCTION (node->decl) = NULL;
3a40c18a
JH
1426 }
1427 DECL_SAVED_TREE (node->decl) = NULL;
6b20f353
DS
1428 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1429 of its associated function function declaration because it's
1430 needed to emit debug info later. */
1431 if (!node->abstract_and_needed)
1432 DECL_INITIAL (node->decl) = error_mark_node;
3a40c18a
JH
1433}
1434
b2583345
JJ
1435/* Remove same body alias node. */
1436
1437void
1438cgraph_remove_same_body_alias (struct cgraph_node *node)
1439{
1440 void **slot;
1441 int uid = node->uid;
1442
1443 gcc_assert (node->same_body_alias);
1444 if (node->previous)
1445 node->previous->next = node->next;
1446 else
1447 node->same_body->same_body = node->next;
1448 if (node->next)
1449 node->next->previous = node->previous;
1450 node->next = NULL;
1451 node->previous = NULL;
1452 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1453 if (*slot == node)
1454 htab_clear_slot (cgraph_hash, slot);
1455 if (assembler_name_hash)
1456 {
1457 tree name = DECL_ASSEMBLER_NAME (node->decl);
1458 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1459 decl_assembler_name_hash (name),
1460 NO_INSERT);
1461 if (slot && *slot == node)
1462 htab_clear_slot (assembler_name_hash, slot);
1463 }
1464
1465 /* Clear out the node to NULL all pointers and add the node to the free
1466 list. */
1467 memset (node, 0, sizeof(*node));
1468 node->uid = uid;
1469 NEXT_FREE_NODE (node) = free_nodes;
1470 free_nodes = node;
1471}
1472
18d13f34
JH
1473/* Remove the node from cgraph. */
1474
1475void
439f7bc3 1476cgraph_remove_node (struct cgraph_node *node)
18d13f34 1477{
2ee1067b 1478 void **slot;
4a76d91a 1479 bool kill_body = false;
ca30a539 1480 struct cgraph_node *n;
2fb16412 1481 int uid = node->uid;
18c6ada9 1482
9088c1cc 1483 cgraph_call_node_removal_hooks (node);
2563c224
RG
1484 cgraph_node_remove_callers (node);
1485 cgraph_node_remove_callees (node);
369451ec
JH
1486 ipa_remove_all_references (&node->ref_list);
1487 ipa_remove_all_refering (&node->ref_list);
0e3776db
JH
1488 VEC_free (ipa_opt_pass, heap,
1489 node->ipa_transforms_to_apply);
266ad5c8 1490
96fc428c
JH
1491 /* Incremental inlining access removed nodes stored in the postorder list.
1492 */
1493 node->needed = node->reachable = false;
ca30a539
JH
1494 for (n = node->nested; n; n = n->next_nested)
1495 n->origin = NULL;
1496 node->nested = NULL;
18d13f34
JH
1497 if (node->origin)
1498 {
1499 struct cgraph_node **node2 = &node->origin->nested;
1500
1501 while (*node2 != node)
1502 node2 = &(*node2)->next_nested;
1503 *node2 = node->next_nested;
1504 }
1505 if (node->previous)
1506 node->previous->next = node->next;
1507 else
9b0436b7 1508 cgraph_nodes = node->next;
18d13f34
JH
1509 if (node->next)
1510 node->next->previous = node->previous;
96fc428c
JH
1511 node->next = NULL;
1512 node->previous = NULL;
6f312d18 1513 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
18c6ada9
JH
1514 if (*slot == node)
1515 {
9187e02d 1516 struct cgraph_node *next_inline_clone;
c22cacf3 1517
9187e02d
JH
1518 for (next_inline_clone = node->clones;
1519 next_inline_clone && next_inline_clone->decl != node->decl;
1520 next_inline_clone = next_inline_clone->next_sibling_clone)
1521 ;
1522
1523 /* If there is inline clone of the node being removed, we need
1524 to put it into the position of removed node and reorganize all
1525 other clones to be based on it. */
1526 if (next_inline_clone)
1527 {
1528 struct cgraph_node *n;
1529 struct cgraph_node *new_clones;
1530
1531 *slot = next_inline_clone;
1532
1533 /* Unlink inline clone from the list of clones of removed node. */
1534 if (next_inline_clone->next_sibling_clone)
1535 next_inline_clone->next_sibling_clone->prev_sibling_clone
1536 = next_inline_clone->prev_sibling_clone;
1537 if (next_inline_clone->prev_sibling_clone)
1538 {
47cb0d7d 1539 gcc_assert (node->clones != next_inline_clone);
9187e02d
JH
1540 next_inline_clone->prev_sibling_clone->next_sibling_clone
1541 = next_inline_clone->next_sibling_clone;
1542 }
1543 else
47cb0d7d
JH
1544 {
1545 gcc_assert (node->clones == next_inline_clone);
1546 node->clones = next_inline_clone->next_sibling_clone;
1547 }
9187e02d
JH
1548
1549 new_clones = node->clones;
1550 node->clones = NULL;
1551
1552 /* Copy clone info. */
1553 next_inline_clone->clone = node->clone;
1554
1555 /* Now place it into clone tree at same level at NODE. */
1556 next_inline_clone->clone_of = node->clone_of;
1557 next_inline_clone->prev_sibling_clone = NULL;
1558 next_inline_clone->next_sibling_clone = NULL;
1559 if (node->clone_of)
1560 {
47cb0d7d
JH
1561 if (node->clone_of->clones)
1562 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
9187e02d
JH
1563 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1564 node->clone_of->clones = next_inline_clone;
1565 }
1566
1567 /* Merge the clone list. */
1568 if (new_clones)
1569 {
1570 if (!next_inline_clone->clones)
1571 next_inline_clone->clones = new_clones;
1572 else
1573 {
1574 n = next_inline_clone->clones;
1575 while (n->next_sibling_clone)
1576 n = n->next_sibling_clone;
1577 n->next_sibling_clone = new_clones;
1578 new_clones->prev_sibling_clone = n;
1579 }
1580 }
1581
1582 /* Update clone_of pointers. */
1583 n = new_clones;
1584 while (n)
1585 {
1586 n->clone_of = next_inline_clone;
1587 n = n->next_sibling_clone;
1588 }
1589 }
18c6ada9
JH
1590 else
1591 {
c22cacf3 1592 htab_clear_slot (cgraph_hash, slot);
4a76d91a 1593 kill_body = true;
18c6ada9 1594 }
9187e02d 1595
18c6ada9 1596 }
9187e02d
JH
1597 if (node->prev_sibling_clone)
1598 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1599 else if (node->clone_of)
1600 node->clone_of->clones = node->next_sibling_clone;
1601 if (node->next_sibling_clone)
1602 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1603 if (node->clones)
18c6ada9 1604 {
47cb0d7d
JH
1605 struct cgraph_node *n, *next;
1606
1607 if (node->clone_of)
1608 {
1609 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1610 n->clone_of = node->clone_of;
1611 n->clone_of = node->clone_of;
1612 n->next_sibling_clone = node->clone_of->clones;
1613 if (node->clone_of->clones)
1614 node->clone_of->clones->prev_sibling_clone = n;
1615 node->clone_of->clones = node->clones;
1616 }
1617 else
1618 {
1619 /* We are removing node with clones. this makes clones inconsistent,
1620 but assume they will be removed subsequently and just keep clone
1621 tree intact. This can happen in unreachable function removal since
1622 we remove unreachable functions in random order, not by bottom-up
1623 walk of clone trees. */
1624 for (n = node->clones; n; n = next)
1625 {
1626 next = n->next_sibling_clone;
1627 n->next_sibling_clone = NULL;
1628 n->prev_sibling_clone = NULL;
1629 n->clone_of = NULL;
1630 }
1631 }
18c6ada9
JH
1632 }
1633
b2583345
JJ
1634 while (node->same_body)
1635 cgraph_remove_same_body_alias (node->same_body);
1636
b66887e4
JJ
1637 if (node->same_comdat_group)
1638 {
1639 struct cgraph_node *prev;
1640 for (prev = node->same_comdat_group;
1641 prev->same_comdat_group != node;
1642 prev = prev->same_comdat_group)
1643 ;
1644 if (node->same_comdat_group == prev)
1645 prev->same_comdat_group = NULL;
1646 else
1647 prev->same_comdat_group = node->same_comdat_group;
1648 node->same_comdat_group = NULL;
1649 }
1650
c22cacf3 1651 /* While all the clones are removed after being proceeded, the function
4a76d91a
JH
1652 itself is kept in the cgraph even after it is compiled. Check whether
1653 we are done with this body and reclaim it proactively if this is the case.
1654 */
1655 if (!kill_body && *slot)
18c6ada9 1656 {
cceb1885 1657 struct cgraph_node *n = (struct cgraph_node *) *slot;
9187e02d 1658 if (!n->clones && !n->clone_of && !n->global.inlined_to
d63db217 1659 && (cgraph_global_info_ready
a837268b
JH
1660 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1661 || n->in_other_partition)))
4a76d91a
JH
1662 kill_body = true;
1663 }
266ad5c8
JH
1664 if (assembler_name_hash)
1665 {
1666 tree name = DECL_ASSEMBLER_NAME (node->decl);
1667 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1668 decl_assembler_name_hash (name),
1669 NO_INSERT);
1670 /* Inline clones are not hashed. */
1671 if (slot && *slot == node)
1672 htab_clear_slot (assembler_name_hash, slot);
1673 }
18c6ada9 1674
7e8b322a 1675 if (kill_body)
3a40c18a 1676 cgraph_release_function_body (node);
96fc428c 1677 node->decl = NULL;
70d539ce
JH
1678 if (node->call_site_hash)
1679 {
1680 htab_delete (node->call_site_hash);
1681 node->call_site_hash = NULL;
1682 }
18c6ada9 1683 cgraph_n_nodes--;
2fb16412
MJ
1684
1685 /* Clear out the node to NULL all pointers and add the node to the free
1686 list. */
1687 memset (node, 0, sizeof(*node));
1688 node->uid = uid;
1689 NEXT_FREE_NODE (node) = free_nodes;
1690 free_nodes = node;
18d13f34
JH
1691}
1692
9187e02d
JH
1693/* Remove the node from cgraph. */
1694
1695void
1696cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1697{
1698 struct cgraph_edge *e, *next;
1699 for (e = node->callees; e; e = next)
1700 {
1701 next = e->next_callee;
1702 if (!e->inline_failed)
1703 cgraph_remove_node_and_inline_clones (e->callee);
1704 }
1705 cgraph_remove_node (node);
1706}
1707
8dafba3c
RH
1708/* Notify finalize_compilation_unit that given node is reachable. */
1709
1668aabc 1710void
8dafba3c 1711cgraph_mark_reachable_node (struct cgraph_node *node)
1668aabc 1712{
ba245151 1713 if (!node->reachable && node->local.finalized)
1668aabc 1714 {
a2acdf1f
JH
1715 if (cgraph_global_info_ready)
1716 {
1717 /* Verify that function does not appear to be needed out of blue
1718 during the optimization process. This can happen for extern
1719 inlines when bodies was removed after inlining. */
7baeea85
JH
1720 gcc_assert ((node->analyzed || node->in_other_partition
1721 || DECL_EXTERNAL (node->decl)));
a2acdf1f
JH
1722 }
1723 else
1724 notice_global_symbol (node->decl);
1668aabc 1725 node->reachable = 1;
e767b5be
JH
1726
1727 node->next_needed = cgraph_nodes_queue;
1728 cgraph_nodes_queue = node;
1668aabc
JH
1729 }
1730}
1731
8dafba3c
RH
1732/* Likewise indicate that a node is needed, i.e. reachable via some
1733 external means. */
1734
1735void
1736cgraph_mark_needed_node (struct cgraph_node *node)
1737{
1738 node->needed = 1;
b20996ff 1739 gcc_assert (!node->global.inlined_to);
8dafba3c
RH
1740 cgraph_mark_reachable_node (node);
1741}
18d13f34 1742
39ff5a96
JH
1743/* Likewise indicate that a node is having address taken. */
1744
1745void
1746cgraph_mark_address_taken_node (struct cgraph_node *node)
1747{
530f3a1b 1748 gcc_assert (!node->global.inlined_to);
bd3cdcc0 1749 cgraph_mark_reachable_node (node);
39ff5a96 1750 node->address_taken = 1;
39ff5a96
JH
1751}
1752
dafc5b82
JH
1753/* Return local info for the compiled function. */
1754
1755struct cgraph_local_info *
439f7bc3 1756cgraph_local_info (tree decl)
dafc5b82
JH
1757{
1758 struct cgraph_node *node;
c22cacf3 1759
341c100f 1760 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
9f9ebcdf
MJ
1761 node = cgraph_get_node (decl);
1762 if (!node)
1763 return NULL;
dafc5b82
JH
1764 return &node->local;
1765}
1766
1767/* Return local info for the compiled function. */
1768
1769struct cgraph_global_info *
439f7bc3 1770cgraph_global_info (tree decl)
dafc5b82
JH
1771{
1772 struct cgraph_node *node;
c22cacf3 1773
341c100f 1774 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
9f9ebcdf
MJ
1775 node = cgraph_get_node (decl);
1776 if (!node)
1777 return NULL;
dafc5b82
JH
1778 return &node->global;
1779}
1780
b255a036
JH
1781/* Return local info for the compiled function. */
1782
1783struct cgraph_rtl_info *
439f7bc3 1784cgraph_rtl_info (tree decl)
b255a036
JH
1785{
1786 struct cgraph_node *node;
c22cacf3 1787
341c100f 1788 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
9f9ebcdf
MJ
1789 node = cgraph_get_node (decl);
1790 if (!node
1791 || (decl != current_function_decl
1792 && !TREE_ASM_WRITTEN (node->decl)))
b255a036
JH
1793 return NULL;
1794 return &node->rtl;
1795}
1796
61a05df1
JH
1797/* Return a string describing the failure REASON. */
1798
1799const char*
1800cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1801{
1802#undef DEFCIFCODE
1803#define DEFCIFCODE(code, string) string,
1804
1805 static const char *cif_string_table[CIF_N_REASONS] = {
1806#include "cif-code.def"
1807 };
1808
1809 /* Signedness of an enum type is implementation defined, so cast it
1810 to unsigned before testing. */
1811 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1812 return cif_string_table[reason];
1813}
1814
a194aa56
JH
1815/* Return name of the node used in debug output. */
1816const char *
439f7bc3 1817cgraph_node_name (struct cgraph_node *node)
a194aa56 1818{
ae2bcd98 1819 return lang_hooks.decl_printable_name (node->decl, 2);
a194aa56 1820}
dafc5b82 1821
6b02a499 1822/* Names used to print out the availability enum. */
8a4a83ed 1823const char * const cgraph_availability_names[] =
fa10beec 1824 {"unset", "not_available", "overwritable", "available", "local"};
6b02a499 1825
c4e622b6
DN
1826
1827/* Dump call graph node NODE to file F. */
1828
18c6ada9
JH
1829void
1830dump_cgraph_node (FILE *f, struct cgraph_node *node)
1831{
1832 struct cgraph_edge *edge;
e33c6cd6
MJ
1833 int indirect_calls_count = 0;
1834
903d1e67 1835 fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
82f331ff 1836 dump_addr (f, " @", (void *)node);
0115e6c7
JH
1837 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1838 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
18c6ada9
JH
1839 if (node->global.inlined_to)
1840 fprintf (f, " (inline copy in %s/%i)",
1841 cgraph_node_name (node->global.inlined_to),
1842 node->global.inlined_to->uid);
844db5d0
JH
1843 if (node->same_comdat_group)
1844 fprintf (f, " (same comdat group as %s/%i)",
1845 cgraph_node_name (node->same_comdat_group),
1846 node->same_comdat_group->uid);
9187e02d
JH
1847 if (node->clone_of)
1848 fprintf (f, " (clone of %s/%i)",
1849 cgraph_node_name (node->clone_of),
1850 node->clone_of->uid);
6b02a499 1851 if (cgraph_function_flags_ready)
c22cacf3 1852 fprintf (f, " availability:%s",
8a4a83ed 1853 cgraph_availability_names [cgraph_function_body_availability (node)]);
a837268b
JH
1854 if (node->analyzed)
1855 fprintf (f, " analyzed");
1856 if (node->in_other_partition)
1857 fprintf (f, " in_other_partition");
e42922b1
JH
1858 if (node->count)
1859 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1860 (HOST_WIDEST_INT)node->count);
18c6ada9
JH
1861 if (node->origin)
1862 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1863 if (node->needed)
1864 fprintf (f, " needed");
39ff5a96
JH
1865 if (node->address_taken)
1866 fprintf (f, " address_taken");
18c6ada9
JH
1867 else if (node->reachable)
1868 fprintf (f, " reachable");
a837268b
JH
1869 else if (node->reachable_from_other_partition)
1870 fprintf (f, " reachable_from_other_partition");
39ecc018 1871 if (gimple_has_body_p (node->decl))
726a989a 1872 fprintf (f, " body");
257eb6e3
JH
1873 if (node->process)
1874 fprintf (f, " process");
18c6ada9
JH
1875 if (node->local.local)
1876 fprintf (f, " local");
e7d6beb0
JH
1877 if (node->local.externally_visible)
1878 fprintf (f, " externally_visible");
671769c3
JH
1879 if (node->resolution != LDPR_UNKNOWN)
1880 fprintf (f, " %s",
1881 ld_plugin_symbol_resolution_names[(int)node->resolution]);
e7d6beb0
JH
1882 if (node->local.finalized)
1883 fprintf (f, " finalized");
e7d6beb0
JH
1884 if (node->local.redefined_extern_inline)
1885 fprintf (f, " redefined_extern_inline");
18c6ada9
JH
1886 if (TREE_ASM_WRITTEN (node->decl))
1887 fprintf (f, " asm_written");
844db5d0
JH
1888 if (node->only_called_at_startup)
1889 fprintf (f, " only_called_at_startup");
1890 if (node->only_called_at_exit)
1891 fprintf (f, " only_called_at_exit");
18c6ada9 1892
c47d0034
JH
1893 fprintf (f, "\n");
1894
1895 if (node->thunk.thunk_p)
1896 {
1897 fprintf (f, " thunk of %s (asm: %s) fixed offset %i virtual value %i has "
1898 "virtual offset %i)\n",
1899 lang_hooks.decl_printable_name (node->thunk.alias, 2),
1900 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->thunk.alias)),
1901 (int)node->thunk.fixed_offset,
1902 (int)node->thunk.virtual_value,
1903 (int)node->thunk.virtual_offset_p);
1904 }
1905
1906 fprintf (f, " called by: ");
1907
18c6ada9
JH
1908 for (edge = node->callers; edge; edge = edge->next_caller)
1909 {
1910 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1911 edge->caller->uid);
e42922b1
JH
1912 if (edge->count)
1913 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1914 (HOST_WIDEST_INT)edge->count);
45a80bb9
JH
1915 if (edge->frequency)
1916 fprintf (f, "(%.2f per call) ",
1917 edge->frequency / (double)CGRAPH_FREQ_BASE);
18c6ada9
JH
1918 if (!edge->inline_failed)
1919 fprintf(f, "(inlined) ");
e33c6cd6
MJ
1920 if (edge->indirect_inlining_edge)
1921 fprintf(f, "(indirect_inlining) ");
2505c5ed
JH
1922 if (edge->can_throw_external)
1923 fprintf(f, "(can throw external) ");
18c6ada9
JH
1924 }
1925
1926 fprintf (f, "\n calls: ");
1927 for (edge = node->callees; edge; edge = edge->next_callee)
1928 {
1929 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1930 edge->callee->uid);
1931 if (!edge->inline_failed)
1932 fprintf(f, "(inlined) ");
e33c6cd6
MJ
1933 if (edge->indirect_inlining_edge)
1934 fprintf(f, "(indirect_inlining) ");
6b02a499
JH
1935 if (edge->count)
1936 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1937 (HOST_WIDEST_INT)edge->count);
45a80bb9
JH
1938 if (edge->frequency)
1939 fprintf (f, "(%.2f per call) ",
1940 edge->frequency / (double)CGRAPH_FREQ_BASE);
b6fa5b01
JH
1941 if (edge->can_throw_external)
1942 fprintf(f, "(can throw external) ");
18c6ada9
JH
1943 }
1944 fprintf (f, "\n");
369451ec
JH
1945 fprintf (f, " References: ");
1946 ipa_dump_references (f, &node->ref_list);
1947 fprintf (f, " Refering this function: ");
1948 ipa_dump_refering (f, &node->ref_list);
6744a6ab 1949
e33c6cd6
MJ
1950 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1951 indirect_calls_count++;
1952 if (indirect_calls_count)
1953 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1954 indirect_calls_count);
1955
6744a6ab
JH
1956 if (node->same_body)
1957 {
1958 struct cgraph_node *n;
c47d0034 1959 fprintf (f, " aliases:");
6744a6ab
JH
1960 for (n = node->same_body; n; n = n->next)
1961 {
1962 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
2942c502
JH
1963 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1964 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
6744a6ab
JH
1965 }
1966 fprintf (f, "\n");
1967 }
18c6ada9
JH
1968}
1969
c4e622b6
DN
1970
1971/* Dump call graph node NODE to stderr. */
1972
24e47c76 1973DEBUG_FUNCTION void
c4e622b6
DN
1974debug_cgraph_node (struct cgraph_node *node)
1975{
1976 dump_cgraph_node (stderr, node);
1977}
1978
1979
1980/* Dump the callgraph to file F. */
e72fcfe8
JH
1981
1982void
439f7bc3 1983dump_cgraph (FILE *f)
e72fcfe8
JH
1984{
1985 struct cgraph_node *node;
1986
7d82fe7c 1987 fprintf (f, "callgraph:\n\n");
e72fcfe8 1988 for (node = cgraph_nodes; node; node = node->next)
18c6ada9 1989 dump_cgraph_node (f, node);
e72fcfe8 1990}
988d1653 1991
c4e622b6
DN
1992
1993/* Dump the call graph to stderr. */
1994
24e47c76 1995DEBUG_FUNCTION void
c4e622b6
DN
1996debug_cgraph (void)
1997{
1998 dump_cgraph (stderr);
1999}
2000
2001
fccc4eb2 2002/* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
c4e622b6 2003
fccc4eb2
JH
2004void
2005change_decl_assembler_name (tree decl, tree name)
2006{
99fecd47
JH
2007 struct cgraph_node *node;
2008 void **slot;
fccc4eb2 2009 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
99fecd47
JH
2010 SET_DECL_ASSEMBLER_NAME (decl, name);
2011 else
fccc4eb2 2012 {
99fecd47
JH
2013 if (name == DECL_ASSEMBLER_NAME (decl))
2014 return;
fccc4eb2 2015
99fecd47
JH
2016 if (assembler_name_hash
2017 && TREE_CODE (decl) == FUNCTION_DECL
2018 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2019 {
2020 tree old_name = DECL_ASSEMBLER_NAME (decl);
2021 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2022 decl_assembler_name_hash (old_name),
2023 NO_INSERT);
2024 /* Inline clones are not hashed. */
2025 if (slot && *slot == node)
2026 htab_clear_slot (assembler_name_hash, slot);
2027 }
2028 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2029 && DECL_RTL_SET_P (decl))
2030 warning (0, "%D renamed after being referenced in assembly", decl);
fccc4eb2 2031
99fecd47
JH
2032 SET_DECL_ASSEMBLER_NAME (decl, name);
2033 }
2034 if (assembler_name_hash
2035 && TREE_CODE (decl) == FUNCTION_DECL
2036 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2037 {
2038 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2039 decl_assembler_name_hash (name),
2040 INSERT);
2041 gcc_assert (!*slot);
2042 *slot = node;
2043 }
e69529cd
JH
2044}
2045
474eccc6
ILT
2046/* Add a top-level asm statement to the list. */
2047
2048struct cgraph_asm_node *
2049cgraph_add_asm_node (tree asm_str)
2050{
2051 struct cgraph_asm_node *node;
2052
a9429e29 2053 node = ggc_alloc_cleared_cgraph_asm_node ();
474eccc6
ILT
2054 node->asm_str = asm_str;
2055 node->order = cgraph_order++;
2056 node->next = NULL;
2057 if (cgraph_asm_nodes == NULL)
2058 cgraph_asm_nodes = node;
2059 else
2060 cgraph_asm_last_node->next = node;
2061 cgraph_asm_last_node = node;
2062 return node;
2063}
2064
1bb17c21
JH
2065/* Return true when the DECL can possibly be inlined. */
2066bool
2067cgraph_function_possibly_inlined_p (tree decl)
2068{
1bb17c21 2069 if (!cgraph_global_info_ready)
e90acd93 2070 return !DECL_UNINLINABLE (decl);
6f312d18 2071 return DECL_POSSIBLY_INLINED (decl);
18c6ada9
JH
2072}
2073
2074/* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2075struct cgraph_edge *
e42922b1 2076cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
d7f09764 2077 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
898b8927 2078 int freq_scale, bool update_original)
18c6ada9 2079{
82d6e6fc 2080 struct cgraph_edge *new_edge;
45a80bb9 2081 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
0d63a740 2082 gcov_type freq;
e42922b1 2083
0d63a740
JH
2084 /* We do not want to ignore loop nest after frequency drops to 0. */
2085 if (!freq_scale)
2086 freq_scale = 1;
2087 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
45a80bb9
JH
2088 if (freq > CGRAPH_FREQ_MAX)
2089 freq = CGRAPH_FREQ_MAX;
e33c6cd6
MJ
2090
2091 if (e->indirect_unknown_callee)
2092 {
2093 tree decl;
2094
2095 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2096 {
a358e188
MJ
2097 struct cgraph_node *callee = cgraph_get_node (decl);
2098 gcc_checking_assert (callee);
898b8927 2099 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
e33c6cd6
MJ
2100 }
2101 else
2102 {
5f902d76
JH
2103 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2104 e->indirect_info->ecf_flags,
898b8927 2105 count, freq);
b258210c 2106 *new_edge->indirect_info = *e->indirect_info;
e33c6cd6
MJ
2107 }
2108 }
2109 else
ceeffab0 2110 {
898b8927 2111 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
ceeffab0
MJ
2112 if (e->indirect_info)
2113 {
2114 new_edge->indirect_info
2115 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2116 *new_edge->indirect_info = *e->indirect_info;
2117 }
2118 }
18c6ada9 2119
82d6e6fc 2120 new_edge->inline_failed = e->inline_failed;
e33c6cd6 2121 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
d7f09764 2122 new_edge->lto_stmt_uid = stmt_uid;
41c8e948
RG
2123 /* Clone flags that depend on call_stmt availability manually. */
2124 new_edge->can_throw_external = e->can_throw_external;
2125 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
c5a4444c 2126 if (update_original)
d63f0fe5 2127 {
82d6e6fc 2128 e->count -= new_edge->count;
d63f0fe5
JH
2129 if (e->count < 0)
2130 e->count = 0;
2131 }
82d6e6fc
KG
2132 cgraph_call_edge_duplication_hooks (e, new_edge);
2133 return new_edge;
1bb17c21 2134}
e69529cd 2135
74605a11 2136
e42922b1 2137/* Create node representing clone of N executed COUNT times. Decrease
c22cacf3 2138 the execution counts from original node too.
ccbbf8a2
JH
2139 The new clone will have decl set to DECL that may or may not be the same
2140 as decl of N.
c5a4444c
JH
2141
2142 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2143 function's profile to reflect the fact that part of execution is handled
74605a11
JH
2144 by node.
2145 When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
2146 the new clone. Otherwise the caller is responsible for doing so later. */
2147
18c6ada9 2148struct cgraph_node *
91fbf0c7 2149cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
898b8927 2150 bool update_original,
74605a11
JH
2151 VEC(cgraph_edge_p,heap) *redirect_callers,
2152 bool call_duplication_hook)
18c6ada9 2153{
a358e188 2154 struct cgraph_node *new_node = cgraph_create_node_1 ();
18c6ada9 2155 struct cgraph_edge *e;
06191a23 2156 gcov_type count_scale;
03ec7d01 2157 unsigned i;
18c6ada9 2158
91fbf0c7 2159 new_node->decl = decl;
82d6e6fc
KG
2160 new_node->origin = n->origin;
2161 if (new_node->origin)
18c6ada9 2162 {
82d6e6fc
KG
2163 new_node->next_nested = new_node->origin->nested;
2164 new_node->origin->nested = new_node;
18c6ada9 2165 }
82d6e6fc
KG
2166 new_node->analyzed = n->analyzed;
2167 new_node->local = n->local;
b20996ff 2168 new_node->local.externally_visible = false;
e65bb9be 2169 new_node->local.local = true;
82d6e6fc
KG
2170 new_node->global = n->global;
2171 new_node->rtl = n->rtl;
82d6e6fc 2172 new_node->count = count;
5fefcf92 2173 new_node->frequency = n->frequency;
9187e02d 2174 new_node->clone = n->clone;
88cc1e04 2175 new_node->clone.tree_map = 0;
e42922b1 2176 if (n->count)
52c76998
PY
2177 {
2178 if (new_node->count > n->count)
2179 count_scale = REG_BR_PROB_BASE;
2180 else
2181 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2182 }
e42922b1
JH
2183 else
2184 count_scale = 0;
c5a4444c 2185 if (update_original)
d63f0fe5
JH
2186 {
2187 n->count -= count;
2188 if (n->count < 0)
2189 n->count = 0;
2190 }
18c6ada9 2191
ac47786e 2192 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
03ec7d01
JH
2193 {
2194 /* Redirect calls to the old version node to point to its new
2195 version. */
2196 cgraph_redirect_edge_callee (e, new_node);
2197 }
2198
2199
18c6ada9 2200 for (e = n->callees;e; e=e->next_callee)
d7f09764 2201 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
898b8927 2202 count_scale, freq, update_original);
18c6ada9 2203
e33c6cd6
MJ
2204 for (e = n->indirect_calls; e; e = e->next_callee)
2205 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
898b8927 2206 count_scale, freq, update_original);
369451ec 2207 ipa_clone_references (new_node, NULL, &n->ref_list);
e33c6cd6 2208
9187e02d
JH
2209 new_node->next_sibling_clone = n->clones;
2210 if (n->clones)
2211 n->clones->prev_sibling_clone = new_node;
2212 n->clones = new_node;
2213 new_node->clone_of = n;
18c6ada9 2214
91fbf0c7
JH
2215 if (n->decl != decl)
2216 {
2217 struct cgraph_node **slot;
2218 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2219 gcc_assert (!*slot);
2220 *slot = new_node;
2221 if (assembler_name_hash)
2222 {
2223 void **aslot;
2224 tree name = DECL_ASSEMBLER_NAME (decl);
2225
2226 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2227 decl_assembler_name_hash (name),
2228 INSERT);
2229 gcc_assert (!*aslot);
2230 *aslot = new_node;
2231 }
2232 }
74605a11
JH
2233
2234 if (call_duplication_hook)
2235 cgraph_call_node_duplication_hooks (n, new_node);
82d6e6fc 2236 return new_node;
18c6ada9 2237}
8f235343 2238
036546e5 2239/* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
9187e02d
JH
2240
2241static GTY(()) unsigned int clone_fn_id_num;
2242
036546e5
JH
2243tree
2244clone_function_name (tree decl, const char *suffix)
9187e02d
JH
2245{
2246 tree name = DECL_ASSEMBLER_NAME (decl);
2247 size_t len = IDENTIFIER_LENGTH (name);
2248 char *tmp_name, *prefix;
2249
036546e5 2250 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
9187e02d 2251 memcpy (prefix, IDENTIFIER_POINTER (name), len);
036546e5 2252 strcpy (prefix + len + 1, suffix);
9187e02d
JH
2253#ifndef NO_DOT_IN_LABEL
2254 prefix[len] = '.';
2255#elif !defined NO_DOLLAR_IN_LABEL
2256 prefix[len] = '$';
036546e5
JH
2257#else
2258 prefix[len] = '_';
9187e02d
JH
2259#endif
2260 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2261 return get_identifier (tmp_name);
2262}
2263
2264/* Create callgraph node clone with new declaration. The actual body will
b8698a0f 2265 be copied later at compilation stage.
9187e02d
JH
2266
2267 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2268 bitmap interface.
2269 */
2270struct cgraph_node *
2271cgraph_create_virtual_clone (struct cgraph_node *old_node,
2272 VEC(cgraph_edge_p,heap) *redirect_callers,
2273 VEC(ipa_replace_map_p,gc) *tree_map,
036546e5
JH
2274 bitmap args_to_skip,
2275 const char * suffix)
9187e02d
JH
2276{
2277 tree old_decl = old_node->decl;
2278 struct cgraph_node *new_node = NULL;
2279 tree new_decl;
a940b4d9
JH
2280 size_t i;
2281 struct ipa_replace_map *map;
9187e02d 2282
36576655 2283 if (!flag_wpa)
77a74ed7 2284 gcc_checking_assert (tree_versionable_function_p (old_decl));
9187e02d 2285
61e03ffc
JH
2286 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2287
9187e02d
JH
2288 /* Make a new FUNCTION_DECL tree node */
2289 if (!args_to_skip)
2290 new_decl = copy_node (old_decl);
2291 else
2292 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2293 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2294
2295 /* Generate a new name for the new version. */
036546e5 2296 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
9187e02d
JH
2297 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2298 SET_DECL_RTL (new_decl, NULL);
2299
91fbf0c7 2300 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
898b8927 2301 CGRAPH_FREQ_BASE, false,
74605a11 2302 redirect_callers, false);
9187e02d
JH
2303 /* Update the properties.
2304 Make clone visible only within this translation unit. Make sure
2305 that is not weak also.
2306 ??? We cannot use COMDAT linkage because there is no
2307 ABI support for this. */
2308 DECL_EXTERNAL (new_node->decl) = 0;
087fa34b
JJ
2309 if (DECL_ONE_ONLY (old_decl))
2310 DECL_SECTION_NAME (new_node->decl) = NULL;
fc26fae3 2311 DECL_COMDAT_GROUP (new_node->decl) = 0;
9187e02d
JH
2312 TREE_PUBLIC (new_node->decl) = 0;
2313 DECL_COMDAT (new_node->decl) = 0;
2314 DECL_WEAK (new_node->decl) = 0;
dd8352ee
JH
2315 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
2316 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
9187e02d
JH
2317 new_node->clone.tree_map = tree_map;
2318 new_node->clone.args_to_skip = args_to_skip;
ac47786e 2319 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
a940b4d9
JH
2320 {
2321 tree var = map->new_tree;
2322
2323 STRIP_NOPS (var);
2324 if (TREE_CODE (var) != ADDR_EXPR)
2325 continue;
2326 var = get_base_var (var);
2327 if (!var)
2328 continue;
2329
2330 /* Record references of the future statement initializing the constant
2331 argument. */
2332 if (TREE_CODE (var) == FUNCTION_DECL)
a358e188
MJ
2333 {
2334 struct cgraph_node *ref_node = cgraph_get_node (var);
2335 gcc_checking_assert (ref_node);
2336 ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2337 NULL);
2338 }
a940b4d9
JH
2339 else if (TREE_CODE (var) == VAR_DECL)
2340 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2341 IPA_REF_ADDR, NULL);
2342 }
08ad1d6d
JH
2343 if (!args_to_skip)
2344 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2345 else if (old_node->clone.combined_args_to_skip)
2346 {
2347 int newi = 0, oldi = 0;
2348 tree arg;
2349 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2350 struct cgraph_node *orig_node;
2351 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2352 ;
910ad8de 2353 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
08ad1d6d
JH
2354 {
2355 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2356 {
2357 bitmap_set_bit (new_args_to_skip, oldi);
2358 continue;
2359 }
2360 if (bitmap_bit_p (args_to_skip, newi))
2361 bitmap_set_bit (new_args_to_skip, oldi);
2362 newi++;
2363 }
2364 new_node->clone.combined_args_to_skip = new_args_to_skip;
2365 }
2366 else
2367 new_node->clone.combined_args_to_skip = args_to_skip;
9187e02d
JH
2368 new_node->local.externally_visible = 0;
2369 new_node->local.local = 1;
2370 new_node->lowered = true;
2371 new_node->reachable = true;
2372
74605a11
JH
2373 cgraph_call_node_duplication_hooks (old_node, new_node);
2374
03ec7d01 2375
9187e02d
JH
2376 return new_node;
2377}
2378
8f235343
JH
2379/* NODE is no longer nested function; update cgraph accordingly. */
2380void
2381cgraph_unnest_node (struct cgraph_node *node)
2382{
2383 struct cgraph_node **node2 = &node->origin->nested;
2384 gcc_assert (node->origin);
2385
2386 while (*node2 != node)
2387 node2 = &(*node2)->next_nested;
2388 *node2 = node->next_nested;
2389 node->origin = NULL;
2390}
6b02a499
JH
2391
2392/* Return function availability. See cgraph.h for description of individual
2393 return values. */
2394enum availability
2395cgraph_function_body_availability (struct cgraph_node *node)
2396{
2397 enum availability avail;
2398 gcc_assert (cgraph_function_flags_ready);
093c2329 2399 if (!node->analyzed)
6b02a499
JH
2400 avail = AVAIL_NOT_AVAILABLE;
2401 else if (node->local.local)
2402 avail = AVAIL_LOCAL;
d3ea650c 2403 else if (!node->local.externally_visible)
6b02a499 2404 avail = AVAIL_AVAILABLE;
61502ca8
NF
2405 /* Inline functions are safe to be analyzed even if their symbol can
2406 be overwritten at runtime. It is not meaningful to enforce any sane
4a371c8d
JH
2407 behaviour on replacing inline function by different body. */
2408 else if (DECL_DECLARED_INLINE_P (node->decl))
2409 avail = AVAIL_AVAILABLE;
6b02a499
JH
2410
2411 /* If the function can be overwritten, return OVERWRITABLE. Take
2412 care at least of two notable extensions - the COMDAT functions
2413 used to share template instantiations in C++ (this is symmetric
2414 to code cp_cannot_inline_tree_fn and probably shall be shared and
ff5c4582 2415 the inlinability hooks completely eliminated).
6b02a499
JH
2416
2417 ??? Does the C++ one definition rule allow us to always return
2418 AVAIL_AVAILABLE here? That would be good reason to preserve this
4a371c8d
JH
2419 bit. */
2420
051f8cc6 2421 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
6b02a499
JH
2422 avail = AVAIL_OVERWRITABLE;
2423 else avail = AVAIL_AVAILABLE;
2424
2425 return avail;
2426}
2427
f45e0ad1
JH
2428/* Add the function FNDECL to the call graph.
2429 Unlike cgraph_finalize_function, this function is intended to be used
2430 by middle end and allows insertion of new function at arbitrary point
2431 of compilation. The function can be either in high, low or SSA form
2432 GIMPLE.
50674e96 2433
f45e0ad1 2434 The function is assumed to be reachable and have address taken (so no
b8698a0f 2435 API breaking optimizations are performed on it).
50674e96 2436
f45e0ad1
JH
2437 Main work done by this function is to enqueue the function for later
2438 processing to avoid need the passes to be re-entrant. */
953ff289
DN
2439
2440void
f45e0ad1 2441cgraph_add_new_function (tree fndecl, bool lowered)
953ff289 2442{
f45e0ad1
JH
2443 struct cgraph_node *node;
2444 switch (cgraph_state)
2445 {
2446 case CGRAPH_STATE_CONSTRUCTION:
fa10beec 2447 /* Just enqueue function to be processed at nearest occurrence. */
a358e188 2448 node = cgraph_create_node (fndecl);
f45e0ad1
JH
2449 node->next_needed = cgraph_new_nodes;
2450 if (lowered)
2451 node->lowered = true;
2452 cgraph_new_nodes = node;
2453 break;
2454
2455 case CGRAPH_STATE_IPA:
7a388ee4 2456 case CGRAPH_STATE_IPA_SSA:
f45e0ad1
JH
2457 case CGRAPH_STATE_EXPANSION:
2458 /* Bring the function into finalized state and enqueue for later
2459 analyzing and compilation. */
a358e188 2460 node = cgraph_get_create_node (fndecl);
f45e0ad1
JH
2461 node->local.local = false;
2462 node->local.finalized = true;
2463 node->reachable = node->needed = true;
ff2c88a5
JH
2464 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2465 {
2466 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2467 current_function_decl = fndecl;
726a989a
RB
2468 gimple_register_cfg_hooks ();
2469 tree_lowering_passes (fndecl);
ff2c88a5
JH
2470 bitmap_obstack_initialize (NULL);
2471 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2472 execute_pass_list (pass_early_local_passes.pass.sub);
2473 bitmap_obstack_release (NULL);
2474 pop_cfun ();
2475 current_function_decl = NULL;
2476
2477 lowered = true;
2478 }
f45e0ad1
JH
2479 if (lowered)
2480 node->lowered = true;
2481 node->next_needed = cgraph_new_nodes;
2482 cgraph_new_nodes = node;
2483 break;
2484
2485 case CGRAPH_STATE_FINISHED:
2486 /* At the very end of compilation we have to do all the work up
2487 to expansion. */
a358e188 2488 node = cgraph_create_node (fndecl);
322dd859
MJ
2489 if (lowered)
2490 node->lowered = true;
2491 cgraph_analyze_function (node);
f45e0ad1
JH
2492 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2493 current_function_decl = fndecl;
726a989a 2494 gimple_register_cfg_hooks ();
7a388ee4 2495 bitmap_obstack_initialize (NULL);
c72321c9 2496 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
8ddbbcae 2497 execute_pass_list (pass_early_local_passes.pass.sub);
7a388ee4 2498 bitmap_obstack_release (NULL);
f45e0ad1
JH
2499 tree_rest_of_compilation (fndecl);
2500 pop_cfun ();
2501 current_function_decl = NULL;
2502 break;
2503 }
f9417da1
RG
2504
2505 /* Set a personality if required and we already passed EH lowering. */
2506 if (lowered
2507 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2508 == eh_personality_lang))
2509 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
953ff289
DN
2510}
2511
be330ed4
JH
2512/* Worker for cgraph_node_can_be_local_p. */
2513static bool
2514cgraph_node_cannot_be_local_p_1 (struct cgraph_node *node,
2515 void *data ATTRIBUTE_UNUSED)
2516{
2517 return !(!node->needed
2518 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2519 || !node->local.externally_visible));
2520}
2521
a550d677
MJ
2522/* Return true if NODE can be made local for API change.
2523 Extern inline functions and C++ COMDAT functions can be made local
2524 at the expense of possible code size growth if function is used in multiple
2525 compilation units. */
2526bool
2527cgraph_node_can_be_local_p (struct cgraph_node *node)
2528{
be330ed4
JH
2529 return (!node->address_taken
2530 && !cgraph_for_node_and_aliases (node,
2531 cgraph_node_cannot_be_local_p_1,
2532 NULL, true));
a550d677
MJ
2533}
2534
715a4e08
AM
2535/* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2536 but other code such as notice_global_symbol generates rtl. */
2537void
2538cgraph_make_decl_local (tree decl)
2539{
2540 rtx rtl, symbol;
2541
2542 if (TREE_CODE (decl) == VAR_DECL)
2543 DECL_COMMON (decl) = 0;
5dde3b01
JH
2544 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2545
2546 if (DECL_COMDAT (decl))
715a4e08 2547 {
99fecd47
JH
2548 /* It is possible that we are linking against library defining same COMDAT
2549 function. To avoid conflict we need to rename our local name of the
2550 function just in the case WHOPR partitioning decide to make it hidden
2551 to avoid cross partition references. */
2552 if (flag_wpa)
2553 {
2554 const char *old_name;
2555
2556 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2557 if (TREE_CODE (decl) == FUNCTION_DECL)
2558 {
2559 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2560 change_decl_assembler_name (decl,
2561 clone_function_name (decl, "local"));
2562 if (node->local.lto_file_data)
2563 lto_record_renamed_decl (node->local.lto_file_data,
2564 old_name,
2565 IDENTIFIER_POINTER
2566 (DECL_ASSEMBLER_NAME (decl)));
2567 }
2568 else if (TREE_CODE (decl) == VAR_DECL)
2569 {
2570 struct varpool_node *vnode = varpool_get_node (decl);
2571 /* change_decl_assembler_name will warn here on vtables because
2572 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2573 SET_DECL_ASSEMBLER_NAME (decl,
2574 clone_function_name (decl, "local"));
2575 if (vnode->lto_file_data)
2576 lto_record_renamed_decl (vnode->lto_file_data,
2577 old_name,
2578 IDENTIFIER_POINTER
2579 (DECL_ASSEMBLER_NAME (decl)));
2580 }
2581 }
5dde3b01 2582 DECL_SECTION_NAME (decl) = 0;
715a4e08 2583 DECL_COMDAT (decl) = 0;
715a4e08 2584 }
5dde3b01
JH
2585 DECL_COMDAT_GROUP (decl) = 0;
2586 DECL_WEAK (decl) = 0;
2587 DECL_EXTERNAL (decl) = 0;
715a4e08
AM
2588 TREE_PUBLIC (decl) = 0;
2589 if (!DECL_RTL_SET_P (decl))
2590 return;
2591
2592 /* Update rtl flags. */
2593 make_decl_rtl (decl);
2594
2595 rtl = DECL_RTL (decl);
2596 if (!MEM_P (rtl))
2597 return;
2598
2599 symbol = XEXP (rtl, 0);
2600 if (GET_CODE (symbol) != SYMBOL_REF)
2601 return;
2602
2603 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2604}
2605
be330ed4
JH
2606/* Call calback on NODE, thunks and aliases asociated to NODE.
2607 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2608 skipped. */
2609
2610bool
2611cgraph_for_node_thunks_and_aliases (struct cgraph_node *node,
2612 bool (*callback) (struct cgraph_node *, void *),
2613 void *data,
2614 bool include_overwritable)
2615{
2616 struct cgraph_edge *e;
2617 struct cgraph_node *alias;
2618
2619 if (callback (node, data))
2620 return true;
2621 for (alias = node->same_body; alias; alias = alias->next)
2622 if (callback (alias, data))
2623 return true;
2624 for (e = node->callers; e; e = e->next_caller)
2625 if (e->caller->thunk.thunk_p
2626 && (include_overwritable
2627 || cgraph_function_body_availability (e->caller) > AVAIL_OVERWRITABLE))
2628 cgraph_for_node_thunks_and_aliases (e->caller, callback, data, include_overwritable);
2629 return false;
2630}
2631
2632/* Call calback on NODE and aliases asociated to NODE.
2633 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2634 skipped. */
2635
2636bool
2637cgraph_for_node_and_aliases (struct cgraph_node *node,
2638 bool (*callback) (struct cgraph_node *, void *),
2639 void *data,
2640 bool include_overwritable ATTRIBUTE_UNUSED)
a550d677 2641{
be330ed4
JH
2642 struct cgraph_node *alias;
2643
2644 if (callback (node, data))
2645 return true;
2646 for (alias = node->same_body; alias; alias = alias->next)
2647 if (callback (alias, data))
2648 return true;
2649 return false;
2650}
2651
2652/* Worker to bring NODE local. */
2653
2654static bool
2655cgraph_make_node_local_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2656{
2657 gcc_checking_assert (cgraph_node_can_be_local_p (node));
a550d677
MJ
2658 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2659 {
715a4e08
AM
2660 cgraph_make_decl_local (node->decl);
2661
a550d677
MJ
2662 node->local.externally_visible = false;
2663 node->local.local = true;
051f8cc6 2664 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
a550d677
MJ
2665 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2666 }
be330ed4 2667 return false;
a550d677
MJ
2668}
2669
be330ed4
JH
2670/* Bring NODE local. */
2671
2672void
2673cgraph_make_node_local (struct cgraph_node *node)
2674{
2675 cgraph_for_node_thunks_and_aliases (node, cgraph_make_node_local_1,
2676 NULL, true);
2677}
2678
2679/* Worker to set nothrow flag. */
2680
2681static bool
2682cgraph_set_nothrow_flag_1 (struct cgraph_node *node, void *data)
2683{
71fb4f92
JH
2684 struct cgraph_edge *e;
2685
be330ed4 2686 TREE_NOTHROW (node->decl) = data != NULL;
71fb4f92
JH
2687
2688 if (data != NULL)
2689 for (e = node->callers; e; e = e->next_caller)
2690 e->can_throw_external = false;
be330ed4
JH
2691 return false;
2692}
2693
2694/* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
20cdc2be
JJ
2695 if any to NOTHROW. */
2696
2697void
2698cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2699{
be330ed4
JH
2700 cgraph_for_node_thunks_and_aliases (node, cgraph_set_nothrow_flag_1,
2701 (void *)(size_t)nothrow, false);
20cdc2be
JJ
2702}
2703
be330ed4 2704/* Worker to set const flag. */
20cdc2be 2705
be330ed4
JH
2706static bool
2707cgraph_set_const_flag_1 (struct cgraph_node *node, void *data)
20cdc2be 2708{
530f3a1b
JH
2709 /* Static constructors and destructors without a side effect can be
2710 optimized out. */
be330ed4 2711 if (data && !((size_t)data & 2))
530f3a1b
JH
2712 {
2713 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2714 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2715 if (DECL_STATIC_DESTRUCTOR (node->decl))
2716 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2717 }
be330ed4
JH
2718 TREE_READONLY (node->decl) = data != NULL;
2719 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2720 return false;
20cdc2be
JJ
2721}
2722
be330ed4
JH
2723/* Set TREE_READONLY on NODE's decl and on aliases of NODE
2724 if any to READONLY. */
20cdc2be
JJ
2725
2726void
be330ed4 2727cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
20cdc2be 2728{
be330ed4
JH
2729 cgraph_for_node_thunks_and_aliases (node, cgraph_set_const_flag_1,
2730 (void *)(size_t)(readonly + (int)looping * 2),
2731 false);
2732}
2733
2734/* Worker to set pure flag. */
2735
2736static bool
2737cgraph_set_pure_flag_1 (struct cgraph_node *node, void *data)
2738{
2739 /* Static pureructors and destructors without a side effect can be
530f3a1b 2740 optimized out. */
be330ed4 2741 if (data && !((size_t)data & 2))
530f3a1b
JH
2742 {
2743 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2744 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2745 if (DECL_STATIC_DESTRUCTOR (node->decl))
2746 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2747 }
be330ed4
JH
2748 DECL_PURE_P (node->decl) = data != NULL;
2749 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = ((size_t)data & 2) != 0;
2750 return false;
20cdc2be
JJ
2751}
2752
be330ed4
JH
2753/* Set DECL_PURE_P on NODE's decl and on aliases of NODE
2754 if any to PURE. */
2755
2756void
2757cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
fa5f5e27 2758{
be330ed4
JH
2759 cgraph_for_node_thunks_and_aliases (node, cgraph_set_pure_flag_1,
2760 (void *)(size_t)(pure + (int)looping * 2),
2761 false);
2762}
844db5d0 2763
be330ed4 2764/* Data used by cgraph_propagate_frequency. */
844db5d0 2765
be330ed4
JH
2766struct cgraph_propagate_frequency_data
2767{
2768 bool maybe_unlikely_executed;
2769 bool maybe_executed_once;
2770 bool only_called_at_startup;
2771 bool only_called_at_exit;
2772};
2773
2774/* Worker for cgraph_propagate_frequency_1. */
2775
2776static bool
2777cgraph_propagate_frequency_1 (struct cgraph_node *node, void *data)
2778{
2779 struct cgraph_propagate_frequency_data *d;
2780 struct cgraph_edge *edge;
2781
2782 d = (struct cgraph_propagate_frequency_data *)data;
fa5f5e27 2783 for (edge = node->callers;
be330ed4
JH
2784 edge && (d->maybe_unlikely_executed || d->maybe_executed_once
2785 || d->only_called_at_startup || d->only_called_at_exit);
fa5f5e27
JH
2786 edge = edge->next_caller)
2787 {
844db5d0
JH
2788 if (edge->caller != node)
2789 {
be330ed4 2790 d->only_called_at_startup &= edge->caller->only_called_at_startup;
61502ca8 2791 /* It makes sense to put main() together with the static constructors.
844db5d0 2792 It will be executed for sure, but rest of functions called from
61502ca8 2793 main are definitely not at startup only. */
844db5d0 2794 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
be330ed4
JH
2795 d->only_called_at_startup = 0;
2796 d->only_called_at_exit &= edge->caller->only_called_at_exit;
844db5d0 2797 }
fa5f5e27
JH
2798 if (!edge->frequency)
2799 continue;
2800 switch (edge->caller->frequency)
2801 {
2802 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2803 break;
2804 case NODE_FREQUENCY_EXECUTED_ONCE:
2805 if (dump_file && (dump_flags & TDF_DETAILS))
844db5d0 2806 fprintf (dump_file, " Called by %s that is executed once\n",
87e7b310 2807 cgraph_node_name (edge->caller));
be330ed4 2808 d->maybe_unlikely_executed = false;
898b8927 2809 if (inline_edge_summary (edge)->loop_depth)
fa5f5e27 2810 {
be330ed4 2811 d->maybe_executed_once = false;
fa5f5e27
JH
2812 if (dump_file && (dump_flags & TDF_DETAILS))
2813 fprintf (dump_file, " Called in loop\n");
2814 }
2815 break;
2816 case NODE_FREQUENCY_HOT:
2817 case NODE_FREQUENCY_NORMAL:
2818 if (dump_file && (dump_flags & TDF_DETAILS))
844db5d0 2819 fprintf (dump_file, " Called by %s that is normal or hot\n",
87e7b310 2820 cgraph_node_name (edge->caller));
be330ed4
JH
2821 d->maybe_unlikely_executed = false;
2822 d->maybe_executed_once = false;
fa5f5e27
JH
2823 break;
2824 }
2825 }
be330ed4
JH
2826 return edge != NULL;
2827}
2828
2829/* See if the frequency of NODE can be updated based on frequencies of its
2830 callers. */
2831bool
2832cgraph_propagate_frequency (struct cgraph_node *node)
2833{
2834 struct cgraph_propagate_frequency_data d = {true, true, true, true};
2835 bool changed = false;
2836
2837 if (!node->local.local)
2838 return false;
2839 gcc_assert (node->analyzed);
2840 if (dump_file && (dump_flags & TDF_DETAILS))
2841 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2842
2843 cgraph_for_node_and_aliases (node, cgraph_propagate_frequency_1, &d, true);
2844
2845 if ((d.only_called_at_startup && !d.only_called_at_exit)
844db5d0
JH
2846 && !node->only_called_at_startup)
2847 {
2848 node->only_called_at_startup = true;
fa5f5e27 2849 if (dump_file)
844db5d0
JH
2850 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2851 cgraph_node_name (node));
2852 changed = true;
2853 }
be330ed4 2854 if ((d.only_called_at_exit && !d.only_called_at_startup)
844db5d0
JH
2855 && !node->only_called_at_exit)
2856 {
2857 node->only_called_at_exit = true;
fa5f5e27 2858 if (dump_file)
844db5d0
JH
2859 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2860 cgraph_node_name (node));
2861 changed = true;
2862 }
2863 /* These come either from profile or user hints; never update them. */
2864 if (node->frequency == NODE_FREQUENCY_HOT
2865 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2866 return changed;
be330ed4 2867 if (d.maybe_unlikely_executed)
844db5d0
JH
2868 {
2869 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2870 if (dump_file)
2871 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2872 cgraph_node_name (node));
2873 changed = true;
2874 }
be330ed4 2875 else if (d.maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
844db5d0
JH
2876 {
2877 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2878 if (dump_file)
2879 fprintf (dump_file, "Node %s promoted to executed once.\n",
2880 cgraph_node_name (node));
2881 changed = true;
2882 }
2883 return changed;
fa5f5e27
JH
2884}
2885
d56026c2
JH
2886/* Return true when NODE can not return or throw and thus
2887 it is safe to ignore its side effects for IPA analysis. */
2888
2889bool
2890cgraph_node_cannot_return (struct cgraph_node *node)
2891{
2892 int flags = flags_from_decl_or_type (node->decl);
2893 if (!flag_exceptions)
2894 return (flags & ECF_NORETURN) != 0;
2895 else
2896 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2897 == (ECF_NORETURN | ECF_NOTHROW));
2898}
2899
2900/* Return true when call of E can not lead to return from caller
2901 and thus it is safe to ignore its side effects for IPA analysis
2902 when computing side effects of the caller.
2903 FIXME: We could actually mark all edges that have no reaching
2904 patch to EXIT_BLOCK_PTR or throw to get better results. */
2905bool
2906cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2907{
f10ea640
JH
2908 if (cgraph_node_cannot_return (e->caller))
2909 return true;
d56026c2
JH
2910 if (e->indirect_unknown_callee)
2911 {
2912 int flags = e->indirect_info->ecf_flags;
2913 if (!flag_exceptions)
2914 return (flags & ECF_NORETURN) != 0;
2915 else
2916 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2917 == (ECF_NORETURN | ECF_NOTHROW));
2918 }
2919 else
2920 return cgraph_node_cannot_return (e->callee);
2921}
2922
508e4757
JH
2923/* Return true when function NODE can be removed from callgraph
2924 if all direct calls are eliminated. */
2925
2926bool
2927cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2928{
530f3a1b
JH
2929 gcc_assert (!node->global.inlined_to);
2930 /* Extern inlines can always go, we will use the external definition. */
2931 if (DECL_EXTERNAL (node->decl))
2932 return true;
508e4757
JH
2933 /* When function is needed, we can not remove it. */
2934 if (node->needed || node->reachable_from_other_partition)
2935 return false;
530f3a1b
JH
2936 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2937 || DECL_STATIC_DESTRUCTOR (node->decl))
2938 return false;
508e4757
JH
2939 /* Only COMDAT functions can be removed if externally visible. */
2940 if (node->local.externally_visible
051f8cc6
JH
2941 && (!DECL_COMDAT (node->decl)
2942 || cgraph_used_from_object_file_p (node)))
508e4757 2943 return false;
508e4757
JH
2944 return true;
2945}
2946
61502ca8 2947/* Return true when function NODE can be expected to be removed
09411461
JH
2948 from program when direct calls in this compilation unit are removed.
2949
2950 As a special case COMDAT functions are
2951 cgraph_can_remove_if_no_direct_calls_p while the are not
2952 cgraph_only_called_directly_p (it is possible they are called from other
2953 unit)
2954
2955 This function behaves as cgraph_only_called_directly_p because eliminating
61502ca8 2956 all uses of COMDAT function does not make it necessarily disappear from
09411461
JH
2957 the program unless we are compiling whole program or we do LTO. In this
2958 case we know we win since dynamic linking will not really discard the
2959 linkonce section. */
2960
2961bool
2962cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2963{
530f3a1b 2964 gcc_assert (!node->global.inlined_to);
051f8cc6 2965 if (cgraph_used_from_object_file_p (node))
09411461
JH
2966 return false;
2967 if (!in_lto_p && !flag_whole_program)
2968 return cgraph_only_called_directly_p (node);
2969 else
530f3a1b
JH
2970 {
2971 if (DECL_EXTERNAL (node->decl))
2972 return true;
2973 return cgraph_can_remove_if_no_direct_calls_p (node);
2974 }
09411461
JH
2975}
2976
051f8cc6 2977/* Return true when RESOLUTION indicate that linker will use
61502ca8 2978 the symbol from non-LTO object files. */
051f8cc6
JH
2979
2980bool
2981resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2982{
2983 return (resolution == LDPR_PREVAILING_DEF
2984 || resolution == LDPR_PREEMPTED_REG
2985 || resolution == LDPR_RESOLVED_EXEC
2986 || resolution == LDPR_RESOLVED_DYN);
2987}
2988
be330ed4 2989
051f8cc6
JH
2990/* Return true when NODE is known to be used from other (non-LTO) object file.
2991 Known only when doing LTO via linker plugin. */
2992
2993bool
2994cgraph_used_from_object_file_p (struct cgraph_node *node)
2995{
530f3a1b
JH
2996 gcc_assert (!node->global.inlined_to);
2997 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
051f8cc6
JH
2998 return false;
2999 if (resolution_used_from_other_file_p (node->resolution))
3000 return true;
051f8cc6
JH
3001 return false;
3002}
3003
be330ed4
JH
3004/* Worker for cgraph_only_called_directly_p. */
3005
3006static bool
3007cgraph_not_only_called_directly_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3008{
3009 return !cgraph_only_called_directly_or_aliased_p (node);
3010}
3011
3012/* Return true when function NODE and all its aliases are only called
3013 directly.
3014 i.e. it is not externally visible, address was not taken and
3015 it is not used in any other non-standard way. */
3016
3017bool
3018cgraph_only_called_directly_p (struct cgraph_node *node)
3019{
3020 gcc_assert (cgraph_function_or_thunk_node (node, NULL) == node);
3021 return !cgraph_for_node_and_aliases (node, cgraph_not_only_called_directly_p_1,
3022 NULL, true);
3023}
3024
3025
3026/* Collect all callers of NODE. Worker for collect_callers_of_node. */
3027
3028static bool
3029collect_callers_of_node_1 (struct cgraph_node *node, void *data)
3030{
3031 VEC (cgraph_edge_p, heap) ** redirect_callers = (VEC (cgraph_edge_p, heap) **)data;
3032 struct cgraph_edge *cs;
3033 enum availability avail;
3034 cgraph_function_or_thunk_node (node, &avail);
3035
3036 if (avail > AVAIL_OVERWRITABLE)
3037 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
3038 if (!cs->indirect_inlining_edge)
3039 VEC_safe_push (cgraph_edge_p, heap, *redirect_callers, cs);
3040 return false;
3041}
3042
3043/* Collect all callers of NODE and its aliases that are known to lead to NODE
3044 (i.e. are not overwritable). */
3045
3046VEC (cgraph_edge_p, heap) *
3047collect_callers_of_node (struct cgraph_node *node)
3048{
3049 VEC (cgraph_edge_p, heap) * redirect_callers = NULL;
3050 cgraph_for_node_and_aliases (node, collect_callers_of_node_1,
3051 &redirect_callers, false);
3052 return redirect_callers;
3053}
3054
988d1653 3055#include "gt-cgraph.h"