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