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