]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraph.c
Daily bump.
[thirdparty/gcc.git] / gcc / cgraph.c
CommitLineData
e72fcfe8 1/* Callgraph handling code.
82f331ff 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
JH
36
37 The callgraph at the moment does not represent indirect calls or calls
38 from other compilation unit. Flag NEEDED is set for each node that may
1f838355 39 be accessed in such an invisible way and it shall be considered an
18c6ada9
JH
40 entry point to the callgraph.
41
a418679d 42 Interprocedural information:
18c6ada9 43
a418679d 44 Callgraph is place to store data needed for interprocedural optimization.
1ae58c30 45 All data structures are divided into three components: local_info that
18c6ada9 46 is produced while analyzing the function, global_info that is result
1ae58c30 47 of global walking of the callgraph on the end of compilation and
18c6ada9
JH
48 rtl_info used by RTL backend to propagate data from already compiled
49 functions to their callers.
50
51 Inlining plans:
52
53 The function inlining information is decided in advance and maintained
54 in the callgraph as so called inline plan.
1ae58c30 55 For each inlined call, the callee's node is cloned to represent the
1ea7e6ad 56 new function copy produced by inliner.
1ae58c30
KH
57 Each inlined call gets a unique corresponding clone node of the callee
58 and the data structure is updated while inlining is performed, so
59 the clones are eliminated and their callee edges redirected to the
c22cacf3 60 caller.
18c6ada9
JH
61
62 Each edge has "inline_failed" field. When the field is set to NULL,
2b8a92de 63 the call will be inlined. When it is non-NULL it contains a reason
8a4a83ed 64 why inlining wasn't performed. */
18c6ada9 65
e72fcfe8
JH
66#include "config.h"
67#include "system.h"
68#include "coretypes.h"
69#include "tm.h"
70#include "tree.h"
cd9c7bd2 71#include "tree-inline.h"
e72fcfe8
JH
72#include "langhooks.h"
73#include "hashtab.h"
74#include "toplev.h"
75#include "flags.h"
76#include "ggc.h"
77#include "debug.h"
78#include "target.h"
cd9c7bd2 79#include "basic-block.h"
1c4a429a 80#include "cgraph.h"
e69529cd 81#include "output.h"
dc0bfe6a 82#include "intl.h"
726a989a 83#include "gimple.h"
ef330312 84#include "tree-dump.h"
7a388ee4 85#include "tree-flow.h"
a63f2942 86#include "value-prof.h"
f9417da1 87#include "except.h"
988d1653 88
2563c224
RG
89static void cgraph_node_remove_callers (struct cgraph_node *node);
90static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
91static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
92
e72fcfe8 93/* Hash table used to convert declarations into nodes. */
ed2df68b 94static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
266ad5c8
JH
95/* Hash table used to convert assembler names into nodes. */
96static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
e72fcfe8
JH
97
98/* The linked list of cgraph nodes. */
1c4a429a 99struct cgraph_node *cgraph_nodes;
e72fcfe8 100
1668aabc
JH
101/* Queue of cgraph nodes scheduled to be lowered. */
102struct cgraph_node *cgraph_nodes_queue;
103
f45e0ad1 104/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
c0220ea4 105 secondary queue used during optimization to accommodate passes that
50674e96 106 may generate new functions that need to be optimized and expanded. */
f45e0ad1 107struct cgraph_node *cgraph_new_nodes;
953ff289 108
e72fcfe8 109/* Number of nodes in existence. */
1c4a429a 110int cgraph_n_nodes;
e72fcfe8 111
b58b1157
JH
112/* Maximal uid used in cgraph nodes. */
113int cgraph_max_uid;
114
9088c1cc
MJ
115/* Maximal uid used in cgraph edges. */
116int cgraph_edge_max_uid;
117
6bad2617
TB
118/* Maximal pid used for profiling */
119int cgraph_max_pid;
120
dafc5b82
JH
121/* Set when whole unit has been analyzed so we can access global info. */
122bool cgraph_global_info_ready = false;
123
f45e0ad1
JH
124/* What state callgraph is in right now. */
125enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
126
cd9c7bd2
JH
127/* Set when the cgraph is fully build and the basic flags are computed. */
128bool cgraph_function_flags_ready = false;
129
474eccc6
ILT
130/* Linked list of cgraph asm nodes. */
131struct cgraph_asm_node *cgraph_asm_nodes;
132
133/* Last node in cgraph_asm_nodes. */
134static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
135
136/* The order index of the next cgraph node to be created. This is
137 used so that we can sort the cgraph nodes in order by when we saw
138 them, to support -fno-toplevel-reorder. */
139int cgraph_order;
140
9088c1cc
MJ
141/* List of hooks trigerred on cgraph_edge events. */
142struct cgraph_edge_hook_list {
143 cgraph_edge_hook hook;
144 void *data;
145 struct cgraph_edge_hook_list *next;
146};
147
148/* List of hooks trigerred on cgraph_node events. */
149struct cgraph_node_hook_list {
150 cgraph_node_hook hook;
151 void *data;
152 struct cgraph_node_hook_list *next;
153};
154
155/* List of hooks trigerred on events involving two cgraph_edges. */
156struct cgraph_2edge_hook_list {
157 cgraph_2edge_hook hook;
158 void *data;
159 struct cgraph_2edge_hook_list *next;
160};
161
162/* List of hooks trigerred on events involving two cgraph_nodes. */
163struct cgraph_2node_hook_list {
164 cgraph_2node_hook hook;
165 void *data;
166 struct cgraph_2node_hook_list *next;
167};
168
169/* List of hooks triggered when an edge is removed. */
170struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
171/* List of hooks triggered when a node is removed. */
172struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
173/* List of hooks triggered when an edge is duplicated. */
174struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
175/* List of hooks triggered when a node is duplicated. */
176struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
129a37fc
JH
177/* List of hooks triggered when an function is inserted. */
178struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
9088c1cc 179
2fb16412
MJ
180/* Head of a linked list of unused (freed) call graph nodes.
181 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
182static GTY(()) struct cgraph_node *free_nodes;
934cb78a
MJ
183/* Head of a linked list of unused (freed) call graph edges.
184 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
185static GTY(()) struct cgraph_edge *free_edges;
186
2fb16412
MJ
187/* Macros to access the next item in the list of free cgraph nodes and
188 edges. */
189#define NEXT_FREE_NODE(NODE) (NODE)->next
934cb78a 190#define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
9088c1cc
MJ
191
192/* Register HOOK to be called with DATA on each removed edge. */
193struct cgraph_edge_hook_list *
194cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
195{
196 struct cgraph_edge_hook_list *entry;
197 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
198
199 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
200 entry->hook = hook;
201 entry->data = data;
202 entry->next = NULL;
203 while (*ptr)
204 ptr = &(*ptr)->next;
205 *ptr = entry;
206 return entry;
207}
208
209/* Remove ENTRY from the list of hooks called on removing edges. */
210void
211cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
212{
213 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
214
215 while (*ptr != entry)
216 ptr = &(*ptr)->next;
217 *ptr = entry->next;
934cb78a 218 free (entry);
9088c1cc
MJ
219}
220
221/* Call all edge removal hooks. */
222static void
223cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
224{
225 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
226 while (entry)
227 {
228 entry->hook (e, entry->data);
229 entry = entry->next;
230 }
231}
232
233/* Register HOOK to be called with DATA on each removed node. */
234struct cgraph_node_hook_list *
235cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
236{
237 struct cgraph_node_hook_list *entry;
238 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
239
240 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
241 entry->hook = hook;
242 entry->data = data;
243 entry->next = NULL;
244 while (*ptr)
245 ptr = &(*ptr)->next;
246 *ptr = entry;
247 return entry;
248}
249
250/* Remove ENTRY from the list of hooks called on removing nodes. */
251void
252cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
253{
254 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
255
256 while (*ptr != entry)
257 ptr = &(*ptr)->next;
258 *ptr = entry->next;
934cb78a 259 free (entry);
9088c1cc
MJ
260}
261
262/* Call all node removal hooks. */
263static void
264cgraph_call_node_removal_hooks (struct cgraph_node *node)
265{
266 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
267 while (entry)
268 {
269 entry->hook (node, entry->data);
270 entry = entry->next;
271 }
272}
273
129a37fc
JH
274/* Register HOOK to be called with DATA on each removed node. */
275struct cgraph_node_hook_list *
276cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
277{
278 struct cgraph_node_hook_list *entry;
279 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
280
281 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
282 entry->hook = hook;
283 entry->data = data;
284 entry->next = NULL;
285 while (*ptr)
286 ptr = &(*ptr)->next;
287 *ptr = entry;
288 return entry;
289}
290
291/* Remove ENTRY from the list of hooks called on removing nodes. */
292void
293cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
294{
295 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
296
297 while (*ptr != entry)
298 ptr = &(*ptr)->next;
299 *ptr = entry->next;
934cb78a 300 free (entry);
129a37fc
JH
301}
302
303/* Call all node removal hooks. */
304void
305cgraph_call_function_insertion_hooks (struct cgraph_node *node)
306{
307 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
308 while (entry)
309 {
310 entry->hook (node, entry->data);
311 entry = entry->next;
312 }
313}
314
9088c1cc
MJ
315/* Register HOOK to be called with DATA on each duplicated edge. */
316struct cgraph_2edge_hook_list *
317cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
318{
319 struct cgraph_2edge_hook_list *entry;
320 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
321
322 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
323 entry->hook = hook;
324 entry->data = data;
325 entry->next = NULL;
326 while (*ptr)
327 ptr = &(*ptr)->next;
328 *ptr = entry;
329 return entry;
330}
331
332/* Remove ENTRY from the list of hooks called on duplicating edges. */
333void
334cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
335{
336 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
337
338 while (*ptr != entry)
339 ptr = &(*ptr)->next;
340 *ptr = entry->next;
934cb78a 341 free (entry);
9088c1cc
MJ
342}
343
344/* Call all edge duplication hooks. */
345static void
346cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
347 struct cgraph_edge *cs2)
348{
349 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
350 while (entry)
351 {
352 entry->hook (cs1, cs2, entry->data);
353 entry = entry->next;
354 }
355}
356
357/* Register HOOK to be called with DATA on each duplicated node. */
358struct cgraph_2node_hook_list *
359cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
360{
361 struct cgraph_2node_hook_list *entry;
362 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
363
364 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
365 entry->hook = hook;
366 entry->data = data;
367 entry->next = NULL;
368 while (*ptr)
369 ptr = &(*ptr)->next;
370 *ptr = entry;
371 return entry;
372}
373
374/* Remove ENTRY from the list of hooks called on duplicating nodes. */
375void
376cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
377{
378 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
379
380 while (*ptr != entry)
381 ptr = &(*ptr)->next;
382 *ptr = entry->next;
934cb78a 383 free (entry);
9088c1cc
MJ
384}
385
386/* Call all node duplication hooks. */
387static void
388cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
389 struct cgraph_node *node2)
390{
391 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
392 while (entry)
393 {
394 entry->hook (node1, node2, entry->data);
395 entry = entry->next;
396 }
397}
398
e72fcfe8
JH
399/* Returns a hash code for P. */
400
401static hashval_t
439f7bc3 402hash_node (const void *p)
e72fcfe8 403{
cceb1885 404 const struct cgraph_node *n = (const struct cgraph_node *) p;
6f312d18 405 return (hashval_t) DECL_UID (n->decl);
e72fcfe8
JH
406}
407
4537ec0c 408
6356f892 409/* Returns nonzero if P1 and P2 are equal. */
e72fcfe8
JH
410
411static int
439f7bc3 412eq_node (const void *p1, const void *p2)
e72fcfe8 413{
cceb1885
GDR
414 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
415 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
6f312d18 416 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
e72fcfe8
JH
417}
418
b2583345 419/* Allocate new callgraph node. */
0550e7b7 420
b2583345
JJ
421static inline struct cgraph_node *
422cgraph_allocate_node (void)
18c6ada9
JH
423{
424 struct cgraph_node *node;
425
2fb16412
MJ
426 if (free_nodes)
427 {
428 node = free_nodes;
429 free_nodes = NEXT_FREE_NODE (node);
430 }
431 else
432 {
433 node = GGC_CNEW (struct cgraph_node);
434 node->uid = cgraph_max_uid++;
435 }
436
b2583345
JJ
437 return node;
438}
439
440/* Allocate new callgraph node and insert it into basic data structures. */
441
442static struct cgraph_node *
443cgraph_create_node (void)
444{
445 struct cgraph_node *node = cgraph_allocate_node ();
446
18c6ada9 447 node->next = cgraph_nodes;
6bad2617 448 node->pid = -1;
474eccc6 449 node->order = cgraph_order++;
18c6ada9
JH
450 if (cgraph_nodes)
451 cgraph_nodes->previous = node;
452 node->previous = NULL;
670cd5c5 453 node->global.estimated_growth = INT_MIN;
18c6ada9
JH
454 cgraph_nodes = node;
455 cgraph_n_nodes++;
456 return node;
457}
458
e72fcfe8 459/* Return cgraph node assigned to DECL. Create new one when needed. */
0550e7b7 460
1c4a429a 461struct cgraph_node *
439f7bc3 462cgraph_node (tree decl)
e72fcfe8 463{
6f312d18 464 struct cgraph_node key, *node, **slot;
e72fcfe8 465
341c100f 466 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
988d1653 467
e72fcfe8 468 if (!cgraph_hash)
ed2df68b 469 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
e72fcfe8 470
6f312d18
ZW
471 key.decl = decl;
472
473 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
474
e72fcfe8 475 if (*slot)
6b02a499
JH
476 {
477 node = *slot;
b2583345
JJ
478 if (node->same_body_alias)
479 node = node->same_body;
6b02a499
JH
480 return node;
481 }
18c6ada9
JH
482
483 node = cgraph_create_node ();
e72fcfe8 484 node->decl = decl;
e72fcfe8 485 *slot = node;
5c2e00ee 486 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
e72fcfe8
JH
487 {
488 node->origin = cgraph_node (DECL_CONTEXT (decl));
489 node->next_nested = node->origin->nested;
490 node->origin->nested = node;
491 }
1aeaf0f7
JJ
492 if (assembler_name_hash)
493 {
494 void **aslot;
495 tree name = DECL_ASSEMBLER_NAME (decl);
496
497 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
498 decl_assembler_name_hash (name),
499 INSERT);
500 /* We can have multiple declarations with same assembler name. For C++
501 it is __builtin_strlen and strlen, for instance. Do we need to
502 record them all? Original implementation marked just first one
503 so lets hope for the best. */
504 if (*aslot == NULL)
505 *aslot = node;
506 }
e72fcfe8
JH
507 return node;
508}
509
b2583345
JJ
510/* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
511 Same body aliases are output whenever the body of DECL is output,
512 and cgraph_node (ALIAS) transparently returns cgraph_node (DECL). */
513
514bool
515cgraph_same_body_alias (tree alias, tree decl)
516{
517 struct cgraph_node key, *alias_node, *decl_node, **slot;
518
519 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
520 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
521 gcc_assert (!assembler_name_hash);
522
523#ifndef ASM_OUTPUT_DEF
524 /* If aliases aren't supported by the assembler, fail. */
525 return false;
526#endif
527
528 /* Comdat same body aliases are only supported when comdat groups
529 are supported and the symbols are weak. */
530 if (DECL_ONE_ONLY (decl) && (!HAVE_COMDAT_GROUP || !DECL_WEAK (decl)))
531 return false;
532
533 decl_node = cgraph_node (decl);
534
535 key.decl = alias;
536
537 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
538
539 /* If the cgraph_node has been already created, fail. */
540 if (*slot)
541 return false;
542
543 alias_node = cgraph_allocate_node ();
544 alias_node->decl = alias;
545 alias_node->same_body_alias = 1;
546 alias_node->same_body = decl_node;
547 alias_node->previous = NULL;
548 if (decl_node->same_body)
549 decl_node->same_body->previous = alias_node;
550 alias_node->next = decl_node->same_body;
551 decl_node->same_body = alias_node;
552 *slot = alias_node;
553 return true;
554}
555
f9329c35
DS
556/* Returns the cgraph node assigned to DECL or NULL if no cgraph node
557 is assigned. */
558
559struct cgraph_node *
560cgraph_get_node (tree decl)
561{
562 struct cgraph_node key, *node = NULL, **slot;
563
564 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
565
566 if (!cgraph_hash)
986ad133 567 return NULL;
f9329c35
DS
568
569 key.decl = decl;
570
571 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
572 NO_INSERT);
573
574 if (slot && *slot)
b2583345
JJ
575 {
576 node = *slot;
577 if (node->same_body_alias)
578 node = node->same_body;
579 }
f9329c35
DS
580 return node;
581}
582
ea99e0be
JH
583/* Insert already constructed node into hashtable. */
584
585void
586cgraph_insert_node_to_hashtable (struct cgraph_node *node)
587{
588 struct cgraph_node **slot;
589
590 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
591
592 gcc_assert (!*slot);
593 *slot = node;
594}
595
266ad5c8
JH
596/* Returns a hash code for P. */
597
598static hashval_t
599hash_node_by_assembler_name (const void *p)
600{
601 const struct cgraph_node *n = (const struct cgraph_node *) p;
602 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
603}
604
605/* Returns nonzero if P1 and P2 are equal. */
606
607static int
608eq_assembler_name (const void *p1, const void *p2)
609{
610 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
611 const_tree name = (const_tree)p2;
612 return (decl_assembler_name_equal (n1->decl, name));
613}
bedb9fc0
RH
614
615/* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
616 Return NULL if there's no such node. */
617
618struct cgraph_node *
619cgraph_node_for_asm (tree asmname)
620{
621 struct cgraph_node *node;
266ad5c8 622 void **slot;
bedb9fc0 623
266ad5c8
JH
624 if (!assembler_name_hash)
625 {
626 assembler_name_hash =
627 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
628 NULL);
629 for (node = cgraph_nodes; node; node = node->next)
630 if (!node->global.inlined_to)
631 {
632 tree name = DECL_ASSEMBLER_NAME (node->decl);
633 slot = htab_find_slot_with_hash (assembler_name_hash, name,
634 decl_assembler_name_hash (name),
635 INSERT);
636 /* We can have multiple declarations with same assembler name. For C++
637 it is __builtin_strlen and strlen, for instance. Do we need to
638 record them all? Original implementation marked just first one
639 so lets hope for the best. */
b2583345
JJ
640 if (!*slot)
641 *slot = node;
642 if (node->same_body)
643 {
644 struct cgraph_node *alias;
645
646 for (alias = node->same_body; alias; alias = alias->next)
647 {
648 hashval_t hash;
649 name = DECL_ASSEMBLER_NAME (alias->decl);
650 hash = decl_assembler_name_hash (name);
651 slot = htab_find_slot_with_hash (assembler_name_hash, name,
652 hash, INSERT);
653 if (!*slot)
654 *slot = alias;
655 }
656 }
266ad5c8
JH
657 }
658 }
659
660 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
661 decl_assembler_name_hash (asmname),
662 NO_INSERT);
bedb9fc0 663
266ad5c8 664 if (slot)
b2583345
JJ
665 {
666 node = (struct cgraph_node *) *slot;
667 if (node->same_body_alias)
668 node = node->same_body;
669 return node;
670 }
bedb9fc0
RH
671 return NULL;
672}
673
70d539ce
JH
674/* Returns a hash value for X (which really is a die_struct). */
675
676static hashval_t
677edge_hash (const void *x)
678{
741ac903 679 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
70d539ce
JH
680}
681
682/* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
683
684static int
685edge_eq (const void *x, const void *y)
686{
741ac903 687 return ((const struct cgraph_edge *) x)->call_stmt == y;
70d539ce
JH
688}
689
726a989a
RB
690
691/* Return the callgraph edge representing the GIMPLE_CALL statement
692 CALL_STMT. */
693
18c6ada9 694struct cgraph_edge *
726a989a 695cgraph_edge (struct cgraph_node *node, gimple call_stmt)
18c6ada9 696{
70d539ce
JH
697 struct cgraph_edge *e, *e2;
698 int n = 0;
699
700 if (node->call_site_hash)
f883e0a7
KG
701 return (struct cgraph_edge *)
702 htab_find_with_hash (node->call_site_hash, call_stmt,
52c76998 703 htab_hash_pointer (call_stmt));
18c6ada9
JH
704
705 /* This loop may turn out to be performance problem. In such case adding
706 hashtables into call nodes with very many edges is probably best
2b8a92de 707 solution. It is not good idea to add pointer into CALL_EXPR itself
18c6ada9
JH
708 because we want to make possible having multiple cgraph nodes representing
709 different clones of the same body before the body is actually cloned. */
710 for (e = node->callees; e; e= e->next_callee)
70d539ce
JH
711 {
712 if (e->call_stmt == call_stmt)
713 break;
714 n++;
715 }
726a989a 716
70d539ce
JH
717 if (n > 100)
718 {
719 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
720 for (e2 = node->callees; e2; e2 = e2->next_callee)
721 {
722 void **slot;
723 slot = htab_find_slot_with_hash (node->call_site_hash,
724 e2->call_stmt,
725 htab_hash_pointer (e2->call_stmt),
726 INSERT);
727 gcc_assert (!*slot);
728 *slot = e2;
729 }
730 }
726a989a 731
18c6ada9
JH
732 return e;
733}
734
726a989a 735
9187e02d 736/* Change field call_stmt of edge E to NEW_STMT. */
0550e7b7 737
70d539ce 738void
726a989a 739cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
70d539ce
JH
740{
741 if (e->caller->call_site_hash)
742 {
743 htab_remove_elt_with_hash (e->caller->call_site_hash,
744 e->call_stmt,
745 htab_hash_pointer (e->call_stmt));
746 }
747 e->call_stmt = new_stmt;
b6fa5b01 748 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
2505c5ed 749 e->can_throw_external = stmt_can_throw_external (new_stmt);
b6fa5b01 750 pop_cfun ();
70d539ce
JH
751 if (e->caller->call_site_hash)
752 {
753 void **slot;
754 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
755 e->call_stmt,
756 htab_hash_pointer
757 (e->call_stmt), INSERT);
758 gcc_assert (!*slot);
759 *slot = e;
760 }
761}
762
9b2a5ef7
RH
763/* Like cgraph_set_call_stmt but walk the clone tree and update all
764 clones sharing the same function body. */
9187e02d
JH
765
766void
767cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
768 gimple old_stmt, gimple new_stmt)
769{
770 struct cgraph_node *node;
771 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
772
773 if (edge)
774 cgraph_set_call_stmt (edge, new_stmt);
9b2a5ef7
RH
775
776 node = orig->clones;
777 if (node)
778 while (node != orig)
9187e02d
JH
779 {
780 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
781 if (edge)
782 cgraph_set_call_stmt (edge, new_stmt);
783 if (node->clones)
784 node = node->clones;
785 else if (node->next_sibling_clone)
786 node = node->next_sibling_clone;
787 else
788 {
789 while (node != orig && !node->next_sibling_clone)
790 node = node->clone_of;
791 if (node != orig)
792 node = node->next_sibling_clone;
793 }
794 }
795}
796
797/* Like cgraph_create_edge walk the clone tree and update all clones sharing
b8698a0f
L
798 same function body.
799
9187e02d 800 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
9b2a5ef7 801 frequencies of the clones. */
9187e02d
JH
802
803void
9b2a5ef7
RH
804cgraph_create_edge_including_clones (struct cgraph_node *orig,
805 struct cgraph_node *callee,
806 gimple stmt, gcov_type count,
807 int freq, int loop_depth,
9187e02d
JH
808 cgraph_inline_failed_t reason)
809{
810 struct cgraph_node *node;
9b2a5ef7 811 struct cgraph_edge *edge;
9187e02d 812
6ce2002b 813 if (!cgraph_edge (orig, stmt))
9b2a5ef7
RH
814 {
815 edge = cgraph_create_edge (orig, callee, stmt, count, freq, loop_depth);
816 edge->inline_failed = reason;
817 }
9187e02d 818
9b2a5ef7
RH
819 node = orig->clones;
820 if (node)
821 while (node != orig)
9187e02d
JH
822 {
823 /* It is possible that we already constant propagated into the clone
824 and turned indirect call into dirrect call. */
825 if (!cgraph_edge (node, stmt))
9b2a5ef7
RH
826 {
827 edge = cgraph_create_edge (node, callee, stmt, count,
828 freq, loop_depth);
829 edge->inline_failed = reason;
830 }
9187e02d
JH
831
832 if (node->clones)
833 node = node->clones;
834 else if (node->next_sibling_clone)
835 node = node->next_sibling_clone;
836 else
837 {
838 while (node != orig && !node->next_sibling_clone)
839 node = node->clone_of;
840 if (node != orig)
841 node = node->next_sibling_clone;
842 }
843 }
844}
845
3dc9eaa6
AN
846/* Give initial reasons why inlining would fail on EDGE. This gets either
847 nullified or usually overwritten by more precise reasons later. */
848
849static void
850initialize_inline_failed (struct cgraph_edge *e)
851{
852 struct cgraph_node *callee = e->callee;
853
854 if (!callee->analyzed)
855 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
856 else if (callee->local.redefined_extern_inline)
857 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
858 else if (!callee->local.inlinable)
859 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
d7f09764 860 else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
3dc9eaa6
AN
861 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
862 else
863 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
864}
865
e72fcfe8
JH
866/* Create edge from CALLER to CALLEE in the cgraph. */
867
18c6ada9
JH
868struct cgraph_edge *
869cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
726a989a 870 gimple call_stmt, gcov_type count, int freq, int nest)
e72fcfe8 871{
934cb78a 872 struct cgraph_edge *edge;
18c6ada9 873
d7f09764
DN
874
875 /* LTO does not actually have access to the call_stmt since these
876 have not been loaded yet. */
877 if (call_stmt)
878 {
b1d0a338 879#ifdef ENABLE_CHECKING
9f3f7d13
RG
880 /* This is rather pricely check possibly trigerring construction of
881 call stmt hashtable. */
882 gcc_assert (!cgraph_edge (caller, call_stmt));
18c6ada9
JH
883#endif
884
d7f09764
DN
885 gcc_assert (is_gimple_call (call_stmt));
886 }
b58b1157 887
934cb78a
MJ
888 if (free_edges)
889 {
890 edge = free_edges;
891 free_edges = NEXT_FREE_EDGE (edge);
892 }
893 else
894 {
895 edge = GGC_NEW (struct cgraph_edge);
896 edge->uid = cgraph_edge_max_uid++;
897 }
898
18c6ada9 899 edge->aux = NULL;
e72fcfe8
JH
900
901 edge->caller = caller;
902 edge->callee = callee;
e0704a46 903 edge->call_stmt = call_stmt;
b6fa5b01 904 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
9f3f7d13
RG
905 edge->can_throw_external
906 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
b6fa5b01 907 pop_cfun ();
2563c224 908 edge->prev_caller = NULL;
e72fcfe8 909 edge->next_caller = callee->callers;
2563c224
RG
910 if (callee->callers)
911 callee->callers->prev_caller = edge;
912 edge->prev_callee = NULL;
e72fcfe8 913 edge->next_callee = caller->callees;
2563c224
RG
914 if (caller->callees)
915 caller->callees->prev_callee = edge;
e72fcfe8
JH
916 caller->callees = edge;
917 callee->callers = edge;
e42922b1 918 edge->count = count;
45a80bb9
JH
919 gcc_assert (count >= 0);
920 edge->frequency = freq;
921 gcc_assert (freq >= 0);
922 gcc_assert (freq <= CGRAPH_FREQ_MAX);
e42922b1 923 edge->loop_nest = nest;
3e293154 924 edge->indirect_call = 0;
d7f09764
DN
925 edge->call_stmt_cannot_inline_p =
926 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
927 if (call_stmt && caller->call_site_hash)
70d539ce
JH
928 {
929 void **slot;
930 slot = htab_find_slot_with_hash (caller->call_site_hash,
931 edge->call_stmt,
932 htab_hash_pointer
933 (edge->call_stmt),
934 INSERT);
935 gcc_assert (!*slot);
936 *slot = edge;
937 }
3dc9eaa6
AN
938
939 initialize_inline_failed (edge);
940
e72fcfe8
JH
941 return edge;
942}
943
2563c224
RG
944/* Remove the edge E from the list of the callers of the callee. */
945
946static inline void
947cgraph_edge_remove_callee (struct cgraph_edge *e)
948{
949 if (e->prev_caller)
950 e->prev_caller->next_caller = e->next_caller;
951 if (e->next_caller)
952 e->next_caller->prev_caller = e->prev_caller;
953 if (!e->prev_caller)
954 e->callee->callers = e->next_caller;
955}
956
957/* Remove the edge E from the list of the callees of the caller. */
958
959static inline void
960cgraph_edge_remove_caller (struct cgraph_edge *e)
961{
962 if (e->prev_callee)
963 e->prev_callee->next_callee = e->next_callee;
964 if (e->next_callee)
965 e->next_callee->prev_callee = e->prev_callee;
966 if (!e->prev_callee)
967 e->caller->callees = e->next_callee;
70d539ce
JH
968 if (e->caller->call_site_hash)
969 htab_remove_elt_with_hash (e->caller->call_site_hash,
970 e->call_stmt,
971 htab_hash_pointer (e->call_stmt));
2563c224
RG
972}
973
934cb78a
MJ
974/* Put the edge onto the free list. */
975
976static void
977cgraph_free_edge (struct cgraph_edge *e)
978{
979 int uid = e->uid;
980
981 /* Clear out the edge so we do not dangle pointers. */
5c0466b5 982 memset (e, 0, sizeof (*e));
934cb78a
MJ
983 e->uid = uid;
984 NEXT_FREE_EDGE (e) = free_edges;
985 free_edges = e;
986}
987
2563c224 988/* Remove the edge E in the cgraph. */
e72fcfe8 989
cb967da5 990void
18c6ada9 991cgraph_remove_edge (struct cgraph_edge *e)
e72fcfe8 992{
934cb78a 993 /* Call all edge removal hooks. */
9088c1cc 994 cgraph_call_edge_removal_hooks (e);
934cb78a 995
2563c224
RG
996 /* Remove from callers list of the callee. */
997 cgraph_edge_remove_callee (e);
998
999 /* Remove from callees list of the callers. */
1000 cgraph_edge_remove_caller (e);
934cb78a
MJ
1001
1002 /* Put the edge onto the free list. */
1003 cgraph_free_edge (e);
e72fcfe8
JH
1004}
1005
18c6ada9
JH
1006/* Redirect callee of E to N. The function does not update underlying
1007 call expression. */
1008
1009void
1010cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1011{
2563c224
RG
1012 /* Remove from callers list of the current callee. */
1013 cgraph_edge_remove_callee (e);
18c6ada9 1014
2563c224
RG
1015 /* Insert to callers list of the new callee. */
1016 e->prev_caller = NULL;
1017 if (n->callers)
1018 n->callers->prev_caller = e;
18c6ada9
JH
1019 e->next_caller = n->callers;
1020 n->callers = e;
2563c224
RG
1021 e->callee = n;
1022}
1023
726a989a
RB
1024
1025/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
4b685e14
JH
1026 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1027 of OLD_STMT if it was previously call statement. */
2bafad93 1028
9187e02d
JH
1029static void
1030cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
4b685e14 1031 gimple old_stmt, tree old_call, gimple new_stmt)
2bafad93 1032{
4b685e14 1033 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
2bafad93 1034
4b685e14
JH
1035 /* We are seeing indirect calls, then there is nothing to update. */
1036 if (!new_call && !old_call)
1037 return;
1038 /* See if we turned indirect call into direct call or folded call to one builtin
1039 into different bultin. */
2bafad93
JJ
1040 if (old_call != new_call)
1041 {
1042 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1043 struct cgraph_edge *ne = NULL;
4b685e14
JH
1044 gcov_type count;
1045 int frequency;
1046 int loop_nest;
2bafad93
JJ
1047
1048 if (e)
1049 {
4b685e14
JH
1050 /* See if the call is already there. It might be because of indirect
1051 inlining already found it. */
1052 if (new_call && e->callee->decl == new_call)
1053 return;
1054
1055 /* Otherwise remove edge and create new one; we can't simply redirect
1056 since function has changed, so inline plan and other information
1057 attached to edge is invalid. */
4b685e14
JH
1058 count = e->count;
1059 frequency = e->frequency;
1060 loop_nest = e->loop_nest;
f8754107 1061 cgraph_remove_edge (e);
4b685e14
JH
1062 }
1063 else
1064 {
1065 /* We are seeing new direct call; compute profile info based on BB. */
1066 basic_block bb = gimple_bb (new_stmt);
1067 count = bb->count;
1068 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1069 bb);
1070 loop_nest = bb->loop_depth;
2bafad93 1071 }
2bafad93 1072
4b685e14
JH
1073 if (new_call)
1074 {
1075 ne = cgraph_create_edge (node, cgraph_node (new_call),
1076 new_stmt, count, frequency,
1077 loop_nest);
1078 gcc_assert (ne->inline_failed);
1079 }
2bafad93 1080 }
4b685e14
JH
1081 /* We only updated the call stmt; update pointer in cgraph edge.. */
1082 else if (old_stmt != new_stmt)
1083 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
2bafad93
JJ
1084}
1085
9187e02d 1086/* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
4b685e14
JH
1087 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1088 of OLD_STMT before it was updated (updating can happen inplace). */
9187e02d
JH
1089
1090void
4b685e14 1091cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
9187e02d
JH
1092{
1093 struct cgraph_node *orig = cgraph_node (cfun->decl);
1094 struct cgraph_node *node;
1095
4b685e14 1096 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
9187e02d
JH
1097 if (orig->clones)
1098 for (node = orig->clones; node != orig;)
1099 {
4b685e14 1100 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
9187e02d
JH
1101 if (node->clones)
1102 node = node->clones;
1103 else if (node->next_sibling_clone)
1104 node = node->next_sibling_clone;
1105 else
1106 {
1107 while (node != orig && !node->next_sibling_clone)
1108 node = node->clone_of;
1109 if (node != orig)
1110 node = node->next_sibling_clone;
1111 }
1112 }
1113}
1114
726a989a 1115
2563c224
RG
1116/* Remove all callees from the node. */
1117
1118void
1119cgraph_node_remove_callees (struct cgraph_node *node)
1120{
5c0466b5 1121 struct cgraph_edge *e, *f;
2563c224
RG
1122
1123 /* It is sufficient to remove the edges from the lists of callers of
1124 the callees. The callee list of the node can be zapped with one
1125 assignment. */
5c0466b5 1126 for (e = node->callees; e; e = f)
9088c1cc 1127 {
5c0466b5 1128 f = e->next_callee;
9088c1cc
MJ
1129 cgraph_call_edge_removal_hooks (e);
1130 cgraph_edge_remove_callee (e);
934cb78a 1131 cgraph_free_edge (e);
9088c1cc 1132 }
2563c224 1133 node->callees = NULL;
70d539ce
JH
1134 if (node->call_site_hash)
1135 {
1136 htab_delete (node->call_site_hash);
1137 node->call_site_hash = NULL;
1138 }
2563c224
RG
1139}
1140
1141/* Remove all callers from the node. */
1142
1143static void
1144cgraph_node_remove_callers (struct cgraph_node *node)
1145{
5c0466b5 1146 struct cgraph_edge *e, *f;
2563c224
RG
1147
1148 /* It is sufficient to remove the edges from the lists of callees of
1149 the callers. The caller list of the node can be zapped with one
1150 assignment. */
5c0466b5 1151 for (e = node->callers; e; e = f)
9088c1cc 1152 {
5c0466b5 1153 f = e->next_caller;
9088c1cc
MJ
1154 cgraph_call_edge_removal_hooks (e);
1155 cgraph_edge_remove_caller (e);
934cb78a 1156 cgraph_free_edge (e);
9088c1cc 1157 }
2563c224 1158 node->callers = NULL;
18c6ada9
JH
1159}
1160
3a40c18a
JH
1161/* Release memory used to represent body of function NODE. */
1162
1163void
1164cgraph_release_function_body (struct cgraph_node *node)
1165{
936fc9ba 1166 if (DECL_STRUCT_FUNCTION (node->decl))
3a40c18a
JH
1167 {
1168 tree old_decl = current_function_decl;
1169 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
936fc9ba
JH
1170 if (cfun->gimple_df)
1171 {
1172 current_function_decl = node->decl;
1173 delete_tree_ssa ();
1174 delete_tree_cfg_annotations ();
1175 cfun->eh = NULL;
1176 current_function_decl = old_decl;
1177 }
1178 if (cfun->cfg)
1179 {
1180 gcc_assert (dom_computed[0] == DOM_NONE);
1181 gcc_assert (dom_computed[1] == DOM_NONE);
1182 clear_edges ();
1183 }
a63f2942
JH
1184 if (cfun->value_histograms)
1185 free_histograms ();
1186 gcc_assert (!current_loops);
3a40c18a 1187 pop_cfun();
936fc9ba
JH
1188 gimple_set_body (node->decl, NULL);
1189 VEC_free (ipa_opt_pass, heap,
0e3776db 1190 node->ipa_transforms_to_apply);
936fc9ba
JH
1191 /* Struct function hangs a lot of data that would leak if we didn't
1192 removed all pointers to it. */
1193 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1194 DECL_STRUCT_FUNCTION (node->decl) = NULL;
3a40c18a
JH
1195 }
1196 DECL_SAVED_TREE (node->decl) = NULL;
6b20f353
DS
1197 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1198 of its associated function function declaration because it's
1199 needed to emit debug info later. */
1200 if (!node->abstract_and_needed)
1201 DECL_INITIAL (node->decl) = error_mark_node;
3a40c18a
JH
1202}
1203
b2583345
JJ
1204/* Remove same body alias node. */
1205
1206void
1207cgraph_remove_same_body_alias (struct cgraph_node *node)
1208{
1209 void **slot;
1210 int uid = node->uid;
1211
1212 gcc_assert (node->same_body_alias);
1213 if (node->previous)
1214 node->previous->next = node->next;
1215 else
1216 node->same_body->same_body = node->next;
1217 if (node->next)
1218 node->next->previous = node->previous;
1219 node->next = NULL;
1220 node->previous = NULL;
1221 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1222 if (*slot == node)
1223 htab_clear_slot (cgraph_hash, slot);
1224 if (assembler_name_hash)
1225 {
1226 tree name = DECL_ASSEMBLER_NAME (node->decl);
1227 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1228 decl_assembler_name_hash (name),
1229 NO_INSERT);
1230 if (slot && *slot == node)
1231 htab_clear_slot (assembler_name_hash, slot);
1232 }
1233
1234 /* Clear out the node to NULL all pointers and add the node to the free
1235 list. */
1236 memset (node, 0, sizeof(*node));
1237 node->uid = uid;
1238 NEXT_FREE_NODE (node) = free_nodes;
1239 free_nodes = node;
1240}
1241
18d13f34
JH
1242/* Remove the node from cgraph. */
1243
1244void
439f7bc3 1245cgraph_remove_node (struct cgraph_node *node)
18d13f34 1246{
2ee1067b 1247 void **slot;
4a76d91a 1248 bool kill_body = false;
ca30a539 1249 struct cgraph_node *n;
2fb16412 1250 int uid = node->uid;
18c6ada9 1251
9088c1cc 1252 cgraph_call_node_removal_hooks (node);
2563c224
RG
1253 cgraph_node_remove_callers (node);
1254 cgraph_node_remove_callees (node);
0e3776db
JH
1255 VEC_free (ipa_opt_pass, heap,
1256 node->ipa_transforms_to_apply);
266ad5c8 1257
96fc428c
JH
1258 /* Incremental inlining access removed nodes stored in the postorder list.
1259 */
1260 node->needed = node->reachable = false;
ca30a539
JH
1261 for (n = node->nested; n; n = n->next_nested)
1262 n->origin = NULL;
1263 node->nested = NULL;
18d13f34
JH
1264 if (node->origin)
1265 {
1266 struct cgraph_node **node2 = &node->origin->nested;
1267
1268 while (*node2 != node)
1269 node2 = &(*node2)->next_nested;
1270 *node2 = node->next_nested;
1271 }
1272 if (node->previous)
1273 node->previous->next = node->next;
1274 else
9b0436b7 1275 cgraph_nodes = node->next;
18d13f34
JH
1276 if (node->next)
1277 node->next->previous = node->previous;
96fc428c
JH
1278 node->next = NULL;
1279 node->previous = NULL;
6f312d18 1280 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
18c6ada9
JH
1281 if (*slot == node)
1282 {
9187e02d 1283 struct cgraph_node *next_inline_clone;
c22cacf3 1284
9187e02d
JH
1285 for (next_inline_clone = node->clones;
1286 next_inline_clone && next_inline_clone->decl != node->decl;
1287 next_inline_clone = next_inline_clone->next_sibling_clone)
1288 ;
1289
1290 /* If there is inline clone of the node being removed, we need
1291 to put it into the position of removed node and reorganize all
1292 other clones to be based on it. */
1293 if (next_inline_clone)
1294 {
1295 struct cgraph_node *n;
1296 struct cgraph_node *new_clones;
1297
1298 *slot = next_inline_clone;
1299
1300 /* Unlink inline clone from the list of clones of removed node. */
1301 if (next_inline_clone->next_sibling_clone)
1302 next_inline_clone->next_sibling_clone->prev_sibling_clone
1303 = next_inline_clone->prev_sibling_clone;
1304 if (next_inline_clone->prev_sibling_clone)
1305 {
1306 next_inline_clone->prev_sibling_clone->next_sibling_clone
1307 = next_inline_clone->next_sibling_clone;
1308 }
1309 else
1310 node->clones = next_inline_clone->next_sibling_clone;
1311
1312 new_clones = node->clones;
1313 node->clones = NULL;
1314
1315 /* Copy clone info. */
1316 next_inline_clone->clone = node->clone;
1317
1318 /* Now place it into clone tree at same level at NODE. */
1319 next_inline_clone->clone_of = node->clone_of;
1320 next_inline_clone->prev_sibling_clone = NULL;
1321 next_inline_clone->next_sibling_clone = NULL;
1322 if (node->clone_of)
1323 {
1324 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1325 node->clone_of->clones = next_inline_clone;
1326 }
1327
1328 /* Merge the clone list. */
1329 if (new_clones)
1330 {
1331 if (!next_inline_clone->clones)
1332 next_inline_clone->clones = new_clones;
1333 else
1334 {
1335 n = next_inline_clone->clones;
1336 while (n->next_sibling_clone)
1337 n = n->next_sibling_clone;
1338 n->next_sibling_clone = new_clones;
1339 new_clones->prev_sibling_clone = n;
1340 }
1341 }
1342
1343 /* Update clone_of pointers. */
1344 n = new_clones;
1345 while (n)
1346 {
1347 n->clone_of = next_inline_clone;
1348 n = n->next_sibling_clone;
1349 }
1350 }
18c6ada9
JH
1351 else
1352 {
c22cacf3 1353 htab_clear_slot (cgraph_hash, slot);
4a76d91a 1354 kill_body = true;
18c6ada9 1355 }
9187e02d 1356
18c6ada9
JH
1357 }
1358 else
9187e02d
JH
1359 gcc_assert (node->clone_of);
1360 if (node->prev_sibling_clone)
1361 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1362 else if (node->clone_of)
1363 node->clone_of->clones = node->next_sibling_clone;
1364 if (node->next_sibling_clone)
1365 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1366 if (node->clones)
18c6ada9 1367 {
9187e02d
JH
1368 struct cgraph_node *n;
1369
1370 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1371 n->clone_of = node->clone_of;
1372 n->clone_of = node->clone_of;
1373 n->next_sibling_clone = node->clone_of->clones;
1374 if (node->clone_of->clones)
1375 node->clone_of->clones->prev_sibling_clone = n;
1376 node->clone_of->clones = node->clones;
18c6ada9
JH
1377 }
1378
b2583345
JJ
1379 while (node->same_body)
1380 cgraph_remove_same_body_alias (node->same_body);
1381
c22cacf3 1382 /* While all the clones are removed after being proceeded, the function
4a76d91a
JH
1383 itself is kept in the cgraph even after it is compiled. Check whether
1384 we are done with this body and reclaim it proactively if this is the case.
1385 */
1386 if (!kill_body && *slot)
18c6ada9 1387 {
cceb1885 1388 struct cgraph_node *n = (struct cgraph_node *) *slot;
9187e02d 1389 if (!n->clones && !n->clone_of && !n->global.inlined_to
d63db217
JH
1390 && (cgraph_global_info_ready
1391 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
4a76d91a
JH
1392 kill_body = true;
1393 }
266ad5c8
JH
1394 if (assembler_name_hash)
1395 {
1396 tree name = DECL_ASSEMBLER_NAME (node->decl);
1397 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1398 decl_assembler_name_hash (name),
1399 NO_INSERT);
1400 /* Inline clones are not hashed. */
1401 if (slot && *slot == node)
1402 htab_clear_slot (assembler_name_hash, slot);
1403 }
18c6ada9 1404
7e8b322a 1405 if (kill_body)
3a40c18a 1406 cgraph_release_function_body (node);
96fc428c 1407 node->decl = NULL;
70d539ce
JH
1408 if (node->call_site_hash)
1409 {
1410 htab_delete (node->call_site_hash);
1411 node->call_site_hash = NULL;
1412 }
18c6ada9 1413 cgraph_n_nodes--;
2fb16412
MJ
1414
1415 /* Clear out the node to NULL all pointers and add the node to the free
1416 list. */
1417 memset (node, 0, sizeof(*node));
1418 node->uid = uid;
1419 NEXT_FREE_NODE (node) = free_nodes;
1420 free_nodes = node;
18d13f34
JH
1421}
1422
9187e02d
JH
1423/* Remove the node from cgraph. */
1424
1425void
1426cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1427{
1428 struct cgraph_edge *e, *next;
1429 for (e = node->callees; e; e = next)
1430 {
1431 next = e->next_callee;
1432 if (!e->inline_failed)
1433 cgraph_remove_node_and_inline_clones (e->callee);
1434 }
1435 cgraph_remove_node (node);
1436}
1437
8dafba3c
RH
1438/* Notify finalize_compilation_unit that given node is reachable. */
1439
1668aabc 1440void
8dafba3c 1441cgraph_mark_reachable_node (struct cgraph_node *node)
1668aabc 1442{
ba245151 1443 if (!node->reachable && node->local.finalized)
1668aabc 1444 {
ba245151 1445 notice_global_symbol (node->decl);
1668aabc 1446 node->reachable = 1;
45676d2b 1447 gcc_assert (!cgraph_global_info_ready);
e767b5be
JH
1448
1449 node->next_needed = cgraph_nodes_queue;
1450 cgraph_nodes_queue = node;
1668aabc
JH
1451 }
1452}
1453
8dafba3c
RH
1454/* Likewise indicate that a node is needed, i.e. reachable via some
1455 external means. */
1456
1457void
1458cgraph_mark_needed_node (struct cgraph_node *node)
1459{
1460 node->needed = 1;
b20996ff 1461 gcc_assert (!node->global.inlined_to);
8dafba3c
RH
1462 cgraph_mark_reachable_node (node);
1463}
18d13f34 1464
39ff5a96
JH
1465/* Likewise indicate that a node is having address taken. */
1466
1467void
1468cgraph_mark_address_taken_node (struct cgraph_node *node)
1469{
1470 node->address_taken = 1;
1471 cgraph_mark_needed_node (node);
1472}
1473
dafc5b82
JH
1474/* Return local info for the compiled function. */
1475
1476struct cgraph_local_info *
439f7bc3 1477cgraph_local_info (tree decl)
dafc5b82
JH
1478{
1479 struct cgraph_node *node;
c22cacf3 1480
341c100f 1481 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
dafc5b82
JH
1482 node = cgraph_node (decl);
1483 return &node->local;
1484}
1485
1486/* Return local info for the compiled function. */
1487
1488struct cgraph_global_info *
439f7bc3 1489cgraph_global_info (tree decl)
dafc5b82
JH
1490{
1491 struct cgraph_node *node;
c22cacf3 1492
341c100f 1493 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
dafc5b82
JH
1494 node = cgraph_node (decl);
1495 return &node->global;
1496}
1497
b255a036
JH
1498/* Return local info for the compiled function. */
1499
1500struct cgraph_rtl_info *
439f7bc3 1501cgraph_rtl_info (tree decl)
b255a036
JH
1502{
1503 struct cgraph_node *node;
c22cacf3 1504
341c100f 1505 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
b255a036
JH
1506 node = cgraph_node (decl);
1507 if (decl != current_function_decl
1508 && !TREE_ASM_WRITTEN (node->decl))
1509 return NULL;
1510 return &node->rtl;
1511}
1512
61a05df1
JH
1513/* Return a string describing the failure REASON. */
1514
1515const char*
1516cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1517{
1518#undef DEFCIFCODE
1519#define DEFCIFCODE(code, string) string,
1520
1521 static const char *cif_string_table[CIF_N_REASONS] = {
1522#include "cif-code.def"
1523 };
1524
1525 /* Signedness of an enum type is implementation defined, so cast it
1526 to unsigned before testing. */
1527 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1528 return cif_string_table[reason];
1529}
1530
a194aa56
JH
1531/* Return name of the node used in debug output. */
1532const char *
439f7bc3 1533cgraph_node_name (struct cgraph_node *node)
a194aa56 1534{
ae2bcd98 1535 return lang_hooks.decl_printable_name (node->decl, 2);
a194aa56 1536}
dafc5b82 1537
6b02a499 1538/* Names used to print out the availability enum. */
8a4a83ed 1539const char * const cgraph_availability_names[] =
fa10beec 1540 {"unset", "not_available", "overwritable", "available", "local"};
6b02a499 1541
c4e622b6
DN
1542
1543/* Dump call graph node NODE to file F. */
1544
18c6ada9
JH
1545void
1546dump_cgraph_node (FILE *f, struct cgraph_node *node)
1547{
1548 struct cgraph_edge *edge;
82f331ff
AO
1549 fprintf (f, "%s/%i(%i)", cgraph_node_name (node), node->uid,
1550 node->pid);
1551 dump_addr (f, " @", (void *)node);
18c6ada9
JH
1552 if (node->global.inlined_to)
1553 fprintf (f, " (inline copy in %s/%i)",
1554 cgraph_node_name (node->global.inlined_to),
1555 node->global.inlined_to->uid);
9187e02d
JH
1556 if (node->clone_of)
1557 fprintf (f, " (clone of %s/%i)",
1558 cgraph_node_name (node->clone_of),
1559 node->clone_of->uid);
6b02a499 1560 if (cgraph_function_flags_ready)
c22cacf3 1561 fprintf (f, " availability:%s",
8a4a83ed 1562 cgraph_availability_names [cgraph_function_body_availability (node)]);
e42922b1
JH
1563 if (node->count)
1564 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1565 (HOST_WIDEST_INT)node->count);
85057983
JH
1566 if (node->local.inline_summary.self_time)
1567 fprintf (f, " %i time, %i benefit", node->local.inline_summary.self_time,
1568 node->local.inline_summary.time_inlining_benefit);
1569 if (node->global.time && node->global.time
1570 != node->local.inline_summary.self_time)
1571 fprintf (f, " (%i after inlining)", node->global.time);
1572 if (node->local.inline_summary.self_size)
1573 fprintf (f, " %i size, %i benefit", node->local.inline_summary.self_size,
1574 node->local.inline_summary.size_inlining_benefit);
1575 if (node->global.size && node->global.size
1576 != node->local.inline_summary.self_size)
1577 fprintf (f, " (%i after inlining)", node->global.size);
95622280
JH
1578 if (node->local.inline_summary.estimated_self_stack_size)
1579 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
1580 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
ff28a94d 1581 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
18c6ada9
JH
1582 if (node->origin)
1583 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1584 if (node->needed)
1585 fprintf (f, " needed");
39ff5a96
JH
1586 if (node->address_taken)
1587 fprintf (f, " address_taken");
18c6ada9
JH
1588 else if (node->reachable)
1589 fprintf (f, " reachable");
39ecc018 1590 if (gimple_has_body_p (node->decl))
726a989a 1591 fprintf (f, " body");
257eb6e3
JH
1592 if (node->process)
1593 fprintf (f, " process");
18c6ada9
JH
1594 if (node->local.local)
1595 fprintf (f, " local");
e7d6beb0
JH
1596 if (node->local.externally_visible)
1597 fprintf (f, " externally_visible");
1598 if (node->local.finalized)
1599 fprintf (f, " finalized");
18c6ada9
JH
1600 if (node->local.disregard_inline_limits)
1601 fprintf (f, " always_inline");
1602 else if (node->local.inlinable)
1603 fprintf (f, " inlinable");
e7d6beb0
JH
1604 if (node->local.redefined_extern_inline)
1605 fprintf (f, " redefined_extern_inline");
18c6ada9
JH
1606 if (TREE_ASM_WRITTEN (node->decl))
1607 fprintf (f, " asm_written");
1608
1609 fprintf (f, "\n called by: ");
1610 for (edge = node->callers; edge; edge = edge->next_caller)
1611 {
1612 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1613 edge->caller->uid);
e42922b1
JH
1614 if (edge->count)
1615 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1616 (HOST_WIDEST_INT)edge->count);
45a80bb9
JH
1617 if (edge->frequency)
1618 fprintf (f, "(%.2f per call) ",
1619 edge->frequency / (double)CGRAPH_FREQ_BASE);
18c6ada9
JH
1620 if (!edge->inline_failed)
1621 fprintf(f, "(inlined) ");
3e293154
MJ
1622 if (edge->indirect_call)
1623 fprintf(f, "(indirect) ");
2505c5ed
JH
1624 if (edge->can_throw_external)
1625 fprintf(f, "(can throw external) ");
18c6ada9
JH
1626 }
1627
1628 fprintf (f, "\n calls: ");
1629 for (edge = node->callees; edge; edge = edge->next_callee)
1630 {
1631 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1632 edge->callee->uid);
1633 if (!edge->inline_failed)
1634 fprintf(f, "(inlined) ");
3e293154
MJ
1635 if (edge->indirect_call)
1636 fprintf(f, "(indirect) ");
6b02a499
JH
1637 if (edge->count)
1638 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1639 (HOST_WIDEST_INT)edge->count);
45a80bb9
JH
1640 if (edge->frequency)
1641 fprintf (f, "(%.2f per call) ",
1642 edge->frequency / (double)CGRAPH_FREQ_BASE);
6b02a499
JH
1643 if (edge->loop_nest)
1644 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
b6fa5b01
JH
1645 if (edge->can_throw_external)
1646 fprintf(f, "(can throw external) ");
18c6ada9
JH
1647 }
1648 fprintf (f, "\n");
1649}
1650
c4e622b6
DN
1651
1652/* Dump call graph node NODE to stderr. */
1653
1654void
1655debug_cgraph_node (struct cgraph_node *node)
1656{
1657 dump_cgraph_node (stderr, node);
1658}
1659
1660
1661/* Dump the callgraph to file F. */
e72fcfe8
JH
1662
1663void
439f7bc3 1664dump_cgraph (FILE *f)
e72fcfe8
JH
1665{
1666 struct cgraph_node *node;
1667
7d82fe7c 1668 fprintf (f, "callgraph:\n\n");
e72fcfe8 1669 for (node = cgraph_nodes; node; node = node->next)
18c6ada9 1670 dump_cgraph_node (f, node);
e72fcfe8 1671}
988d1653 1672
c4e622b6
DN
1673
1674/* Dump the call graph to stderr. */
1675
1676void
1677debug_cgraph (void)
1678{
1679 dump_cgraph (stderr);
1680}
1681
1682
fccc4eb2 1683/* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
c4e622b6 1684
fccc4eb2
JH
1685void
1686change_decl_assembler_name (tree decl, tree name)
1687{
266ad5c8 1688 gcc_assert (!assembler_name_hash);
fccc4eb2
JH
1689 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1690 {
1691 SET_DECL_ASSEMBLER_NAME (decl, name);
1692 return;
1693 }
1694 if (name == DECL_ASSEMBLER_NAME (decl))
1695 return;
1696
df964a18
JH
1697 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1698 && DECL_RTL_SET_P (decl))
d4ee4d25 1699 warning (0, "%D renamed after being referenced in assembly", decl);
fccc4eb2 1700
fccc4eb2 1701 SET_DECL_ASSEMBLER_NAME (decl, name);
e69529cd
JH
1702}
1703
474eccc6
ILT
1704/* Add a top-level asm statement to the list. */
1705
1706struct cgraph_asm_node *
1707cgraph_add_asm_node (tree asm_str)
1708{
1709 struct cgraph_asm_node *node;
1710
1711 node = GGC_CNEW (struct cgraph_asm_node);
1712 node->asm_str = asm_str;
1713 node->order = cgraph_order++;
1714 node->next = NULL;
1715 if (cgraph_asm_nodes == NULL)
1716 cgraph_asm_nodes = node;
1717 else
1718 cgraph_asm_last_node->next = node;
1719 cgraph_asm_last_node = node;
1720 return node;
1721}
1722
1bb17c21
JH
1723/* Return true when the DECL can possibly be inlined. */
1724bool
1725cgraph_function_possibly_inlined_p (tree decl)
1726{
1bb17c21 1727 if (!cgraph_global_info_ready)
e90acd93 1728 return !DECL_UNINLINABLE (decl);
6f312d18 1729 return DECL_POSSIBLY_INLINED (decl);
18c6ada9
JH
1730}
1731
1732/* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
1733struct cgraph_edge *
e42922b1 1734cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
d7f09764
DN
1735 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
1736 int freq_scale, int loop_nest, bool update_original)
18c6ada9 1737{
82d6e6fc 1738 struct cgraph_edge *new_edge;
45a80bb9 1739 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
0d63a740 1740 gcov_type freq;
e42922b1 1741
0d63a740
JH
1742 /* We do not want to ignore loop nest after frequency drops to 0. */
1743 if (!freq_scale)
1744 freq_scale = 1;
1745 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
45a80bb9
JH
1746 if (freq > CGRAPH_FREQ_MAX)
1747 freq = CGRAPH_FREQ_MAX;
82d6e6fc 1748 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
c22cacf3 1749 e->loop_nest + loop_nest);
18c6ada9 1750
82d6e6fc
KG
1751 new_edge->inline_failed = e->inline_failed;
1752 new_edge->indirect_call = e->indirect_call;
d7f09764 1753 new_edge->lto_stmt_uid = stmt_uid;
c5a4444c 1754 if (update_original)
d63f0fe5 1755 {
82d6e6fc 1756 e->count -= new_edge->count;
d63f0fe5
JH
1757 if (e->count < 0)
1758 e->count = 0;
1759 }
82d6e6fc
KG
1760 cgraph_call_edge_duplication_hooks (e, new_edge);
1761 return new_edge;
1bb17c21 1762}
e69529cd 1763
e42922b1 1764/* Create node representing clone of N executed COUNT times. Decrease
c22cacf3 1765 the execution counts from original node too.
c5a4444c
JH
1766
1767 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1768 function's profile to reflect the fact that part of execution is handled
1769 by node. */
18c6ada9 1770struct cgraph_node *
726a989a 1771cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq,
03ec7d01
JH
1772 int loop_nest, bool update_original,
1773 VEC(cgraph_edge_p,heap) *redirect_callers)
18c6ada9 1774{
82d6e6fc 1775 struct cgraph_node *new_node = cgraph_create_node ();
18c6ada9 1776 struct cgraph_edge *e;
06191a23 1777 gcov_type count_scale;
03ec7d01 1778 unsigned i;
18c6ada9 1779
82d6e6fc
KG
1780 new_node->decl = n->decl;
1781 new_node->origin = n->origin;
1782 if (new_node->origin)
18c6ada9 1783 {
82d6e6fc
KG
1784 new_node->next_nested = new_node->origin->nested;
1785 new_node->origin->nested = new_node;
18c6ada9 1786 }
82d6e6fc
KG
1787 new_node->analyzed = n->analyzed;
1788 new_node->local = n->local;
b20996ff 1789 new_node->local.externally_visible = false;
82d6e6fc
KG
1790 new_node->global = n->global;
1791 new_node->rtl = n->rtl;
82d6e6fc 1792 new_node->count = count;
9187e02d 1793 new_node->clone = n->clone;
e42922b1 1794 if (n->count)
52c76998
PY
1795 {
1796 if (new_node->count > n->count)
1797 count_scale = REG_BR_PROB_BASE;
1798 else
1799 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
1800 }
e42922b1
JH
1801 else
1802 count_scale = 0;
c5a4444c 1803 if (update_original)
d63f0fe5
JH
1804 {
1805 n->count -= count;
1806 if (n->count < 0)
1807 n->count = 0;
1808 }
18c6ada9 1809
03ec7d01
JH
1810 for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1811 {
1812 /* Redirect calls to the old version node to point to its new
1813 version. */
1814 cgraph_redirect_edge_callee (e, new_node);
1815 }
1816
1817
18c6ada9 1818 for (e = n->callees;e; e=e->next_callee)
d7f09764
DN
1819 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
1820 count_scale, freq, loop_nest, update_original);
18c6ada9 1821
9187e02d
JH
1822 new_node->next_sibling_clone = n->clones;
1823 if (n->clones)
1824 n->clones->prev_sibling_clone = new_node;
1825 n->clones = new_node;
1826 new_node->clone_of = n;
18c6ada9 1827
82d6e6fc
KG
1828 cgraph_call_node_duplication_hooks (n, new_node);
1829 return new_node;
18c6ada9 1830}
8f235343 1831
9187e02d
JH
1832/* Create a new name for omp child function. Returns an identifier. */
1833
1834static GTY(()) unsigned int clone_fn_id_num;
1835
1836static tree
1837clone_function_name (tree decl)
1838{
1839 tree name = DECL_ASSEMBLER_NAME (decl);
1840 size_t len = IDENTIFIER_LENGTH (name);
1841 char *tmp_name, *prefix;
1842
1843 prefix = XALLOCAVEC (char, len + strlen ("_clone") + 1);
1844 memcpy (prefix, IDENTIFIER_POINTER (name), len);
1845 strcpy (prefix + len, "_clone");
1846#ifndef NO_DOT_IN_LABEL
1847 prefix[len] = '.';
1848#elif !defined NO_DOLLAR_IN_LABEL
1849 prefix[len] = '$';
1850#endif
1851 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
1852 return get_identifier (tmp_name);
1853}
1854
1855/* Create callgraph node clone with new declaration. The actual body will
b8698a0f 1856 be copied later at compilation stage.
9187e02d
JH
1857
1858 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
1859 bitmap interface.
1860 */
1861struct cgraph_node *
1862cgraph_create_virtual_clone (struct cgraph_node *old_node,
1863 VEC(cgraph_edge_p,heap) *redirect_callers,
1864 VEC(ipa_replace_map_p,gc) *tree_map,
1865 bitmap args_to_skip)
1866{
1867 tree old_decl = old_node->decl;
1868 struct cgraph_node *new_node = NULL;
1869 tree new_decl;
1870 struct cgraph_node key, **slot;
9187e02d
JH
1871
1872 gcc_assert (tree_versionable_function_p (old_decl));
1873
1874 /* Make a new FUNCTION_DECL tree node */
1875 if (!args_to_skip)
1876 new_decl = copy_node (old_decl);
1877 else
1878 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
1879 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1880
1881 /* Generate a new name for the new version. */
1882 DECL_NAME (new_decl) = clone_function_name (old_decl);
1883 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1884 SET_DECL_RTL (new_decl, NULL);
1885
1886 new_node = cgraph_clone_node (old_node, old_node->count,
03ec7d01
JH
1887 CGRAPH_FREQ_BASE, 0, false,
1888 redirect_callers);
9187e02d
JH
1889 new_node->decl = new_decl;
1890 /* Update the properties.
1891 Make clone visible only within this translation unit. Make sure
1892 that is not weak also.
1893 ??? We cannot use COMDAT linkage because there is no
1894 ABI support for this. */
1895 DECL_EXTERNAL (new_node->decl) = 0;
fc26fae3 1896 DECL_COMDAT_GROUP (new_node->decl) = 0;
9187e02d
JH
1897 TREE_PUBLIC (new_node->decl) = 0;
1898 DECL_COMDAT (new_node->decl) = 0;
1899 DECL_WEAK (new_node->decl) = 0;
1900 new_node->clone.tree_map = tree_map;
1901 new_node->clone.args_to_skip = args_to_skip;
08ad1d6d
JH
1902 if (!args_to_skip)
1903 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
1904 else if (old_node->clone.combined_args_to_skip)
1905 {
1906 int newi = 0, oldi = 0;
1907 tree arg;
1908 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
1909 struct cgraph_node *orig_node;
1910 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
1911 ;
1912 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = TREE_CHAIN (arg), oldi++)
1913 {
1914 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
1915 {
1916 bitmap_set_bit (new_args_to_skip, oldi);
1917 continue;
1918 }
1919 if (bitmap_bit_p (args_to_skip, newi))
1920 bitmap_set_bit (new_args_to_skip, oldi);
1921 newi++;
1922 }
1923 new_node->clone.combined_args_to_skip = new_args_to_skip;
1924 }
1925 else
1926 new_node->clone.combined_args_to_skip = args_to_skip;
9187e02d
JH
1927 new_node->local.externally_visible = 0;
1928 new_node->local.local = 1;
1929 new_node->lowered = true;
1930 new_node->reachable = true;
1931
1932 key.decl = new_decl;
1933 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
1934 gcc_assert (!*slot);
1935 *slot = new_node;
1936 if (assembler_name_hash)
1937 {
1938 void **aslot;
1939 tree name = DECL_ASSEMBLER_NAME (new_decl);
1940
1941 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
1942 decl_assembler_name_hash (name),
1943 INSERT);
1944 gcc_assert (!*aslot);
1945 *aslot = new_node;
1946 }
03ec7d01 1947
9187e02d
JH
1948 return new_node;
1949}
1950
8f235343
JH
1951/* NODE is no longer nested function; update cgraph accordingly. */
1952void
1953cgraph_unnest_node (struct cgraph_node *node)
1954{
1955 struct cgraph_node **node2 = &node->origin->nested;
1956 gcc_assert (node->origin);
1957
1958 while (*node2 != node)
1959 node2 = &(*node2)->next_nested;
1960 *node2 = node->next_nested;
1961 node->origin = NULL;
1962}
6b02a499
JH
1963
1964/* Return function availability. See cgraph.h for description of individual
1965 return values. */
1966enum availability
1967cgraph_function_body_availability (struct cgraph_node *node)
1968{
1969 enum availability avail;
1970 gcc_assert (cgraph_function_flags_ready);
093c2329 1971 if (!node->analyzed)
6b02a499
JH
1972 avail = AVAIL_NOT_AVAILABLE;
1973 else if (node->local.local)
1974 avail = AVAIL_LOCAL;
d3ea650c 1975 else if (!node->local.externally_visible)
6b02a499 1976 avail = AVAIL_AVAILABLE;
4a371c8d
JH
1977 /* Inline functions are safe to be analyzed even if their sybol can
1978 be overwritten at runtime. It is not meaningful to enfore any sane
1979 behaviour on replacing inline function by different body. */
1980 else if (DECL_DECLARED_INLINE_P (node->decl))
1981 avail = AVAIL_AVAILABLE;
6b02a499
JH
1982
1983 /* If the function can be overwritten, return OVERWRITABLE. Take
1984 care at least of two notable extensions - the COMDAT functions
1985 used to share template instantiations in C++ (this is symmetric
1986 to code cp_cannot_inline_tree_fn and probably shall be shared and
ff5c4582 1987 the inlinability hooks completely eliminated).
6b02a499
JH
1988
1989 ??? Does the C++ one definition rule allow us to always return
1990 AVAIL_AVAILABLE here? That would be good reason to preserve this
4a371c8d
JH
1991 bit. */
1992
1993 else if (DECL_REPLACEABLE_P (node->decl) && !DECL_EXTERNAL (node->decl))
6b02a499
JH
1994 avail = AVAIL_OVERWRITABLE;
1995 else avail = AVAIL_AVAILABLE;
1996
1997 return avail;
1998}
1999
f45e0ad1
JH
2000/* Add the function FNDECL to the call graph.
2001 Unlike cgraph_finalize_function, this function is intended to be used
2002 by middle end and allows insertion of new function at arbitrary point
2003 of compilation. The function can be either in high, low or SSA form
2004 GIMPLE.
50674e96 2005
f45e0ad1 2006 The function is assumed to be reachable and have address taken (so no
b8698a0f 2007 API breaking optimizations are performed on it).
50674e96 2008
f45e0ad1
JH
2009 Main work done by this function is to enqueue the function for later
2010 processing to avoid need the passes to be re-entrant. */
953ff289
DN
2011
2012void
f45e0ad1 2013cgraph_add_new_function (tree fndecl, bool lowered)
953ff289 2014{
f45e0ad1
JH
2015 struct cgraph_node *node;
2016 switch (cgraph_state)
2017 {
2018 case CGRAPH_STATE_CONSTRUCTION:
fa10beec 2019 /* Just enqueue function to be processed at nearest occurrence. */
f45e0ad1
JH
2020 node = cgraph_node (fndecl);
2021 node->next_needed = cgraph_new_nodes;
2022 if (lowered)
2023 node->lowered = true;
2024 cgraph_new_nodes = node;
2025 break;
2026
2027 case CGRAPH_STATE_IPA:
7a388ee4 2028 case CGRAPH_STATE_IPA_SSA:
f45e0ad1
JH
2029 case CGRAPH_STATE_EXPANSION:
2030 /* Bring the function into finalized state and enqueue for later
2031 analyzing and compilation. */
2032 node = cgraph_node (fndecl);
2033 node->local.local = false;
2034 node->local.finalized = true;
2035 node->reachable = node->needed = true;
ff2c88a5
JH
2036 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2037 {
2038 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2039 current_function_decl = fndecl;
726a989a
RB
2040 gimple_register_cfg_hooks ();
2041 tree_lowering_passes (fndecl);
ff2c88a5
JH
2042 bitmap_obstack_initialize (NULL);
2043 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2044 execute_pass_list (pass_early_local_passes.pass.sub);
2045 bitmap_obstack_release (NULL);
2046 pop_cfun ();
2047 current_function_decl = NULL;
2048
2049 lowered = true;
2050 }
f45e0ad1
JH
2051 if (lowered)
2052 node->lowered = true;
2053 node->next_needed = cgraph_new_nodes;
2054 cgraph_new_nodes = node;
2055 break;
2056
2057 case CGRAPH_STATE_FINISHED:
2058 /* At the very end of compilation we have to do all the work up
2059 to expansion. */
2060 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2061 current_function_decl = fndecl;
726a989a 2062 gimple_register_cfg_hooks ();
f45e0ad1
JH
2063 if (!lowered)
2064 tree_lowering_passes (fndecl);
7a388ee4 2065 bitmap_obstack_initialize (NULL);
c72321c9 2066 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
8ddbbcae 2067 execute_pass_list (pass_early_local_passes.pass.sub);
7a388ee4 2068 bitmap_obstack_release (NULL);
f45e0ad1
JH
2069 tree_rest_of_compilation (fndecl);
2070 pop_cfun ();
2071 current_function_decl = NULL;
2072 break;
2073 }
f9417da1
RG
2074
2075 /* Set a personality if required and we already passed EH lowering. */
2076 if (lowered
2077 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2078 == eh_personality_lang))
2079 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
953ff289
DN
2080}
2081
a550d677
MJ
2082/* Return true if NODE can be made local for API change.
2083 Extern inline functions and C++ COMDAT functions can be made local
2084 at the expense of possible code size growth if function is used in multiple
2085 compilation units. */
2086bool
2087cgraph_node_can_be_local_p (struct cgraph_node *node)
2088{
3621d5ec
JH
2089 return (!node->needed
2090 && (DECL_COMDAT (node->decl) || !node->local.externally_visible));
a550d677
MJ
2091}
2092
2093/* Bring NODE local. */
2094void
2095cgraph_make_node_local (struct cgraph_node *node)
2096{
2097 gcc_assert (cgraph_node_can_be_local_p (node));
2098 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2099 {
2100 DECL_COMDAT (node->decl) = 0;
19751f1f 2101 DECL_COMDAT_GROUP (node->decl) = 0;
a550d677
MJ
2102 TREE_PUBLIC (node->decl) = 0;
2103 DECL_WEAK (node->decl) = 0;
2104 DECL_EXTERNAL (node->decl) = 0;
2105 node->local.externally_visible = false;
2106 node->local.local = true;
2107 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2108 }
2109}
2110
988d1653 2111#include "gt-cgraph.h"