new tests
[official-gcc.git] / gcc / cgraph.c
blob2086bfe9b0bb883e5de698e11070a3a29a5c66a5
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
24 The callgraph:
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.
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node.
33 The mapping from declarations to call-graph nodes is done using hash table
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
37 The callgraph at the moment does not represent all indirect calls or calls
38 from other compilation units. Flag NEEDED is set for each node that may be
39 accessed in such an invisible way and it shall be considered an entry point
40 to the callgraph.
42 On the other hand, the callgraph currently does contain some edges for
43 indirect calls with unknown callees which can be accessed through
44 indirect_calls field of a node. It should be noted however that at the
45 moment only calls which are potential candidates for indirect inlining are
46 added there.
48 Interprocedural information:
50 Callgraph is place to store data needed for interprocedural optimization.
51 All data structures are divided into three components: local_info that
52 is produced while analyzing the function, global_info that is result
53 of global walking of the callgraph on the end of compilation and
54 rtl_info used by RTL backend to propagate data from already compiled
55 functions to their callers.
57 Moreover, each node has a uid which can be used to keep information in
58 on-the-side arrays. UIDs are reused and therefore reasonably dense.
60 Inlining plans:
62 The function inlining information is decided in advance and maintained
63 in the callgraph as so called inline plan.
64 For each inlined call, the callee's node is cloned to represent the
65 new function copy produced by inliner.
66 Each inlined call gets a unique corresponding clone node of the callee
67 and the data structure is updated while inlining is performed, so
68 the clones are eliminated and their callee edges redirected to the
69 caller.
71 Each edge has "inline_failed" field. When the field is set to NULL,
72 the call will be inlined. When it is non-NULL it contains a reason
73 why inlining wasn't performed. */
75 #include "config.h"
76 #include "system.h"
77 #include "coretypes.h"
78 #include "tm.h"
79 #include "tree.h"
80 #include "tree-inline.h"
81 #include "langhooks.h"
82 #include "hashtab.h"
83 #include "toplev.h"
84 #include "flags.h"
85 #include "ggc.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "basic-block.h"
89 #include "cgraph.h"
90 #include "output.h"
91 #include "intl.h"
92 #include "gimple.h"
93 #include "tree-dump.h"
94 #include "tree-flow.h"
95 #include "value-prof.h"
96 #include "except.h"
97 #include "diagnostic-core.h"
98 #include "rtl.h"
99 #include "ipa-utils.h"
100 #include "lto-streamer.h"
101 #include "ipa-inline.h"
103 const char * const ld_plugin_symbol_resolution_names[]=
106 "undef",
107 "prevailing_def",
108 "prevailing_def_ironly",
109 "preempted_reg",
110 "preempted_ir",
111 "resolved_ir",
112 "resolved_exec",
113 "resolved_dyn"
116 static void cgraph_node_remove_callers (struct cgraph_node *node);
117 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
118 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
120 /* Hash table used to convert declarations into nodes. */
121 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
122 /* Hash table used to convert assembler names into nodes. */
123 static GTY((param_is (struct cgraph_node))) htab_t assembler_name_hash;
125 /* The linked list of cgraph nodes. */
126 struct cgraph_node *cgraph_nodes;
128 /* Queue of cgraph nodes scheduled to be lowered. */
129 struct cgraph_node *cgraph_nodes_queue;
131 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
132 secondary queue used during optimization to accommodate passes that
133 may generate new functions that need to be optimized and expanded. */
134 struct cgraph_node *cgraph_new_nodes;
136 /* Number of nodes in existence. */
137 int cgraph_n_nodes;
139 /* Maximal uid used in cgraph nodes. */
140 int cgraph_max_uid;
142 /* Maximal uid used in cgraph edges. */
143 int cgraph_edge_max_uid;
145 /* Set when whole unit has been analyzed so we can access global info. */
146 bool cgraph_global_info_ready = false;
148 /* What state callgraph is in right now. */
149 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
151 /* Set when the cgraph is fully build and the basic flags are computed. */
152 bool cgraph_function_flags_ready = false;
154 /* Linked list of cgraph asm nodes. */
155 struct cgraph_asm_node *cgraph_asm_nodes;
157 /* Last node in cgraph_asm_nodes. */
158 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
160 /* The order index of the next cgraph node to be created. This is
161 used so that we can sort the cgraph nodes in order by when we saw
162 them, to support -fno-toplevel-reorder. */
163 int cgraph_order;
165 /* List of hooks triggered on cgraph_edge events. */
166 struct cgraph_edge_hook_list {
167 cgraph_edge_hook hook;
168 void *data;
169 struct cgraph_edge_hook_list *next;
172 /* List of hooks triggered on cgraph_node events. */
173 struct cgraph_node_hook_list {
174 cgraph_node_hook hook;
175 void *data;
176 struct cgraph_node_hook_list *next;
179 /* List of hooks triggered on events involving two cgraph_edges. */
180 struct cgraph_2edge_hook_list {
181 cgraph_2edge_hook hook;
182 void *data;
183 struct cgraph_2edge_hook_list *next;
186 /* List of hooks triggered on events involving two cgraph_nodes. */
187 struct cgraph_2node_hook_list {
188 cgraph_2node_hook hook;
189 void *data;
190 struct cgraph_2node_hook_list *next;
193 /* List of hooks triggered when an edge is removed. */
194 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
195 /* List of hooks triggered when a node is removed. */
196 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
197 /* List of hooks triggered when an edge is duplicated. */
198 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
199 /* List of hooks triggered when a node is duplicated. */
200 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
201 /* List of hooks triggered when an function is inserted. */
202 struct cgraph_node_hook_list *first_cgraph_function_insertion_hook;
204 /* Head of a linked list of unused (freed) call graph nodes.
205 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
206 static GTY(()) struct cgraph_node *free_nodes;
207 /* Head of a linked list of unused (freed) call graph edges.
208 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
209 static GTY(()) struct cgraph_edge *free_edges;
211 /* Macros to access the next item in the list of free cgraph nodes and
212 edges. */
213 #define NEXT_FREE_NODE(NODE) (NODE)->next
214 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
216 /* Register HOOK to be called with DATA on each removed edge. */
217 struct cgraph_edge_hook_list *
218 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
220 struct cgraph_edge_hook_list *entry;
221 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
223 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
224 entry->hook = hook;
225 entry->data = data;
226 entry->next = NULL;
227 while (*ptr)
228 ptr = &(*ptr)->next;
229 *ptr = entry;
230 return entry;
233 /* Remove ENTRY from the list of hooks called on removing edges. */
234 void
235 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
237 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
239 while (*ptr != entry)
240 ptr = &(*ptr)->next;
241 *ptr = entry->next;
242 free (entry);
245 /* Call all edge removal hooks. */
246 static void
247 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
249 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
250 while (entry)
252 entry->hook (e, entry->data);
253 entry = entry->next;
257 /* Register HOOK to be called with DATA on each removed node. */
258 struct cgraph_node_hook_list *
259 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
261 struct cgraph_node_hook_list *entry;
262 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
264 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
265 entry->hook = hook;
266 entry->data = data;
267 entry->next = NULL;
268 while (*ptr)
269 ptr = &(*ptr)->next;
270 *ptr = entry;
271 return entry;
274 /* Remove ENTRY from the list of hooks called on removing nodes. */
275 void
276 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
278 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
280 while (*ptr != entry)
281 ptr = &(*ptr)->next;
282 *ptr = entry->next;
283 free (entry);
286 /* Call all node removal hooks. */
287 static void
288 cgraph_call_node_removal_hooks (struct cgraph_node *node)
290 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
291 while (entry)
293 entry->hook (node, entry->data);
294 entry = entry->next;
298 /* Register HOOK to be called with DATA on each inserted node. */
299 struct cgraph_node_hook_list *
300 cgraph_add_function_insertion_hook (cgraph_node_hook hook, void *data)
302 struct cgraph_node_hook_list *entry;
303 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
305 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
306 entry->hook = hook;
307 entry->data = data;
308 entry->next = NULL;
309 while (*ptr)
310 ptr = &(*ptr)->next;
311 *ptr = entry;
312 return entry;
315 /* Remove ENTRY from the list of hooks called on inserted nodes. */
316 void
317 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *entry)
319 struct cgraph_node_hook_list **ptr = &first_cgraph_function_insertion_hook;
321 while (*ptr != entry)
322 ptr = &(*ptr)->next;
323 *ptr = entry->next;
324 free (entry);
327 /* Call all node insertion hooks. */
328 void
329 cgraph_call_function_insertion_hooks (struct cgraph_node *node)
331 struct cgraph_node_hook_list *entry = first_cgraph_function_insertion_hook;
332 while (entry)
334 entry->hook (node, entry->data);
335 entry = entry->next;
339 /* Register HOOK to be called with DATA on each duplicated edge. */
340 struct cgraph_2edge_hook_list *
341 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
343 struct cgraph_2edge_hook_list *entry;
344 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
346 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
347 entry->hook = hook;
348 entry->data = data;
349 entry->next = NULL;
350 while (*ptr)
351 ptr = &(*ptr)->next;
352 *ptr = entry;
353 return entry;
356 /* Remove ENTRY from the list of hooks called on duplicating edges. */
357 void
358 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
360 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
362 while (*ptr != entry)
363 ptr = &(*ptr)->next;
364 *ptr = entry->next;
365 free (entry);
368 /* Call all edge duplication hooks. */
369 static void
370 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
371 struct cgraph_edge *cs2)
373 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
374 while (entry)
376 entry->hook (cs1, cs2, entry->data);
377 entry = entry->next;
381 /* Register HOOK to be called with DATA on each duplicated node. */
382 struct cgraph_2node_hook_list *
383 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
385 struct cgraph_2node_hook_list *entry;
386 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
388 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
389 entry->hook = hook;
390 entry->data = data;
391 entry->next = NULL;
392 while (*ptr)
393 ptr = &(*ptr)->next;
394 *ptr = entry;
395 return entry;
398 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
399 void
400 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
402 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
404 while (*ptr != entry)
405 ptr = &(*ptr)->next;
406 *ptr = entry->next;
407 free (entry);
410 /* Call all node duplication hooks. */
411 static void
412 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
413 struct cgraph_node *node2)
415 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
416 while (entry)
418 entry->hook (node1, node2, entry->data);
419 entry = entry->next;
423 /* Returns a hash code for P. */
425 static hashval_t
426 hash_node (const void *p)
428 const struct cgraph_node *n = (const struct cgraph_node *) p;
429 return (hashval_t) DECL_UID (n->decl);
433 /* Returns nonzero if P1 and P2 are equal. */
435 static int
436 eq_node (const void *p1, const void *p2)
438 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
439 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
440 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
443 /* Allocate new callgraph node. */
445 static inline struct cgraph_node *
446 cgraph_allocate_node (void)
448 struct cgraph_node *node;
450 if (free_nodes)
452 node = free_nodes;
453 free_nodes = NEXT_FREE_NODE (node);
455 else
457 node = ggc_alloc_cleared_cgraph_node ();
458 node->uid = cgraph_max_uid++;
461 return node;
464 /* Allocate new callgraph node and insert it into basic data structures. */
466 static struct cgraph_node *
467 cgraph_create_node_1 (void)
469 struct cgraph_node *node = cgraph_allocate_node ();
471 node->next = cgraph_nodes;
472 node->order = cgraph_order++;
473 if (cgraph_nodes)
474 cgraph_nodes->previous = node;
475 node->previous = NULL;
476 node->frequency = NODE_FREQUENCY_NORMAL;
477 node->count_materialization_scale = REG_BR_PROB_BASE;
478 ipa_empty_ref_list (&node->ref_list);
479 cgraph_nodes = node;
480 cgraph_n_nodes++;
481 return node;
484 /* Return cgraph node assigned to DECL. Create new one when needed. */
486 struct cgraph_node *
487 cgraph_create_node (tree decl)
489 struct cgraph_node key, *node, **slot;
491 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
493 if (!cgraph_hash)
494 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
496 key.decl = decl;
497 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
498 gcc_assert (!*slot);
500 node = cgraph_create_node_1 ();
501 node->decl = decl;
502 *slot = node;
503 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
505 node->origin = cgraph_get_create_node (DECL_CONTEXT (decl));
506 node->next_nested = node->origin->nested;
507 node->origin->nested = node;
509 if (assembler_name_hash)
511 void **aslot;
512 tree name = DECL_ASSEMBLER_NAME (decl);
514 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
515 decl_assembler_name_hash (name),
516 INSERT);
517 /* We can have multiple declarations with same assembler name. For C++
518 it is __builtin_strlen and strlen, for instance. Do we need to
519 record them all? Original implementation marked just first one
520 so lets hope for the best. */
521 if (*aslot == NULL)
522 *aslot = node;
524 return node;
527 /* Try to find a call graph node for declaration DECL and if it does not exist,
528 create it. */
530 struct cgraph_node *
531 cgraph_get_create_node (tree decl)
533 struct cgraph_node *node;
535 node = cgraph_get_node (decl);
536 if (node)
537 return node;
539 return cgraph_create_node (decl);
542 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
543 the function body is associated with (not neccesarily cgraph_node (DECL). */
545 static struct cgraph_node *
546 cgraph_same_body_alias_1 (struct cgraph_node *decl_node, tree alias, tree decl)
548 struct cgraph_node key, *alias_node, **slot;
550 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
551 gcc_assert (TREE_CODE (alias) == FUNCTION_DECL);
553 key.decl = alias;
555 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
557 /* If the cgraph_node has been already created, fail. */
558 if (*slot)
559 return NULL;
561 alias_node = cgraph_allocate_node ();
562 alias_node->decl = alias;
563 alias_node->same_body_alias = 1;
564 alias_node->same_body = decl_node;
565 alias_node->previous = NULL;
566 if (decl_node->same_body)
567 decl_node->same_body->previous = alias_node;
568 alias_node->next = decl_node->same_body;
569 alias_node->thunk.alias = decl;
570 decl_node->same_body = alias_node;
571 *slot = alias_node;
572 return alias_node;
575 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
576 and NULL otherwise.
577 Same body aliases are output whenever the body of DECL is output,
578 and cgraph_get_node (ALIAS) transparently returns cgraph_get_node (DECL). */
580 struct cgraph_node *
581 cgraph_same_body_alias (struct cgraph_node *decl_node, tree alias, tree decl)
583 #ifndef ASM_OUTPUT_DEF
584 /* If aliases aren't supported by the assembler, fail. */
585 return NULL;
586 #endif
588 /*gcc_assert (!assembler_name_hash);*/
590 return cgraph_same_body_alias_1 (decl_node, alias, decl);
593 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
594 aliases DECL with an adjustments made into the first parameter.
595 See comments in thunk_adjust for detail on the parameters. */
597 struct cgraph_node *
598 cgraph_add_thunk (struct cgraph_node *decl_node, tree alias, tree decl,
599 bool this_adjusting,
600 HOST_WIDE_INT fixed_offset, HOST_WIDE_INT virtual_value,
601 tree virtual_offset,
602 tree real_alias)
604 struct cgraph_node *node = cgraph_get_node (alias);
606 if (node)
608 gcc_assert (node->local.finalized);
609 gcc_assert (!node->same_body);
610 cgraph_remove_node (node);
613 node = cgraph_same_body_alias_1 (decl_node, alias, decl);
614 gcc_assert (node);
615 gcc_checking_assert (!virtual_offset
616 || tree_int_cst_equal (virtual_offset,
617 size_int (virtual_value)));
618 node->thunk.fixed_offset = fixed_offset;
619 node->thunk.this_adjusting = this_adjusting;
620 node->thunk.virtual_value = virtual_value;
621 node->thunk.virtual_offset_p = virtual_offset != NULL;
622 node->thunk.alias = real_alias;
623 node->thunk.thunk_p = true;
624 return node;
627 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
628 is assigned. */
630 struct cgraph_node *
631 cgraph_get_node_or_alias (const_tree decl)
633 struct cgraph_node key, *node = NULL, **slot;
635 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
637 if (!cgraph_hash)
638 return NULL;
640 key.decl = CONST_CAST2 (tree, const_tree, decl);
642 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
643 NO_INSERT);
645 if (slot && *slot)
646 node = *slot;
647 return node;
650 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
651 is assigned. */
653 struct cgraph_node *
654 cgraph_get_node (const_tree decl)
656 struct cgraph_node key, *node = NULL, **slot;
658 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
660 if (!cgraph_hash)
661 return NULL;
663 key.decl = CONST_CAST2 (tree, const_tree, decl);
665 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
666 NO_INSERT);
668 if (slot && *slot)
670 node = *slot;
671 if (node->same_body_alias)
672 node = node->same_body;
674 return node;
677 /* Insert already constructed node into hashtable. */
679 void
680 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
682 struct cgraph_node **slot;
684 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
686 gcc_assert (!*slot);
687 *slot = node;
690 /* Returns a hash code for P. */
692 static hashval_t
693 hash_node_by_assembler_name (const void *p)
695 const struct cgraph_node *n = (const struct cgraph_node *) p;
696 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
699 /* Returns nonzero if P1 and P2 are equal. */
701 static int
702 eq_assembler_name (const void *p1, const void *p2)
704 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
705 const_tree name = (const_tree)p2;
706 return (decl_assembler_name_equal (n1->decl, name));
709 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
710 Return NULL if there's no such node. */
712 struct cgraph_node *
713 cgraph_node_for_asm (tree asmname)
715 struct cgraph_node *node;
716 void **slot;
718 if (!assembler_name_hash)
720 assembler_name_hash =
721 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
722 NULL);
723 for (node = cgraph_nodes; node; node = node->next)
724 if (!node->global.inlined_to)
726 tree name = DECL_ASSEMBLER_NAME (node->decl);
727 slot = htab_find_slot_with_hash (assembler_name_hash, name,
728 decl_assembler_name_hash (name),
729 INSERT);
730 /* We can have multiple declarations with same assembler name. For C++
731 it is __builtin_strlen and strlen, for instance. Do we need to
732 record them all? Original implementation marked just first one
733 so lets hope for the best. */
734 if (!*slot)
735 *slot = node;
736 if (node->same_body)
738 struct cgraph_node *alias;
740 for (alias = node->same_body; alias; alias = alias->next)
742 hashval_t hash;
743 name = DECL_ASSEMBLER_NAME (alias->decl);
744 hash = decl_assembler_name_hash (name);
745 slot = htab_find_slot_with_hash (assembler_name_hash, name,
746 hash, INSERT);
747 if (!*slot)
748 *slot = alias;
754 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
755 decl_assembler_name_hash (asmname),
756 NO_INSERT);
758 if (slot)
760 node = (struct cgraph_node *) *slot;
761 if (node->same_body_alias)
762 node = node->same_body;
763 return node;
765 return NULL;
768 /* Returns a hash value for X (which really is a die_struct). */
770 static hashval_t
771 edge_hash (const void *x)
773 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
776 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
778 static int
779 edge_eq (const void *x, const void *y)
781 return ((const struct cgraph_edge *) x)->call_stmt == y;
784 /* Add call graph edge E to call site hash of its caller. */
786 static inline void
787 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
789 void **slot;
790 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
791 e->call_stmt,
792 htab_hash_pointer (e->call_stmt),
793 INSERT);
794 gcc_assert (!*slot);
795 *slot = e;
798 /* Return the callgraph edge representing the GIMPLE_CALL statement
799 CALL_STMT. */
801 struct cgraph_edge *
802 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
804 struct cgraph_edge *e, *e2;
805 int n = 0;
807 if (node->call_site_hash)
808 return (struct cgraph_edge *)
809 htab_find_with_hash (node->call_site_hash, call_stmt,
810 htab_hash_pointer (call_stmt));
812 /* This loop may turn out to be performance problem. In such case adding
813 hashtables into call nodes with very many edges is probably best
814 solution. It is not good idea to add pointer into CALL_EXPR itself
815 because we want to make possible having multiple cgraph nodes representing
816 different clones of the same body before the body is actually cloned. */
817 for (e = node->callees; e; e = e->next_callee)
819 if (e->call_stmt == call_stmt)
820 break;
821 n++;
824 if (!e)
825 for (e = node->indirect_calls; e; e = e->next_callee)
827 if (e->call_stmt == call_stmt)
828 break;
829 n++;
832 if (n > 100)
834 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
835 for (e2 = node->callees; e2; e2 = e2->next_callee)
836 cgraph_add_edge_to_call_site_hash (e2);
837 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
838 cgraph_add_edge_to_call_site_hash (e2);
841 return e;
845 /* Change field call_stmt of edge E to NEW_STMT. */
847 void
848 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
850 tree decl;
852 if (e->caller->call_site_hash)
854 htab_remove_elt_with_hash (e->caller->call_site_hash,
855 e->call_stmt,
856 htab_hash_pointer (e->call_stmt));
859 e->call_stmt = new_stmt;
860 if (e->indirect_unknown_callee
861 && (decl = gimple_call_fndecl (new_stmt)))
863 /* Constant propagation (and possibly also inlining?) can turn an
864 indirect call into a direct one. */
865 struct cgraph_node *new_callee = cgraph_get_node (decl);
867 gcc_checking_assert (new_callee);
868 cgraph_make_edge_direct (e, new_callee, 0);
871 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
872 e->can_throw_external = stmt_can_throw_external (new_stmt);
873 pop_cfun ();
874 if (e->caller->call_site_hash)
875 cgraph_add_edge_to_call_site_hash (e);
878 /* Like cgraph_set_call_stmt but walk the clone tree and update all
879 clones sharing the same function body. */
881 void
882 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
883 gimple old_stmt, gimple new_stmt)
885 struct cgraph_node *node;
886 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
888 if (edge)
889 cgraph_set_call_stmt (edge, new_stmt);
891 node = orig->clones;
892 if (node)
893 while (node != orig)
895 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
896 if (edge)
897 cgraph_set_call_stmt (edge, new_stmt);
898 if (node->clones)
899 node = node->clones;
900 else if (node->next_sibling_clone)
901 node = node->next_sibling_clone;
902 else
904 while (node != orig && !node->next_sibling_clone)
905 node = node->clone_of;
906 if (node != orig)
907 node = node->next_sibling_clone;
912 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
913 same function body. If clones already have edge for OLD_STMT; only
914 update the edge same way as cgraph_set_call_stmt_including_clones does.
916 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
917 frequencies of the clones. */
919 void
920 cgraph_create_edge_including_clones (struct cgraph_node *orig,
921 struct cgraph_node *callee,
922 gimple old_stmt,
923 gimple stmt, gcov_type count,
924 int freq,
925 cgraph_inline_failed_t reason)
927 struct cgraph_node *node;
928 struct cgraph_edge *edge;
930 if (!cgraph_edge (orig, stmt))
932 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
933 edge->inline_failed = reason;
936 node = orig->clones;
937 if (node)
938 while (node != orig)
940 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
942 /* It is possible that clones already contain the edge while
943 master didn't. Either we promoted indirect call into direct
944 call in the clone or we are processing clones of unreachable
945 master where edges has been removed. */
946 if (edge)
947 cgraph_set_call_stmt (edge, stmt);
948 else if (!cgraph_edge (node, stmt))
950 edge = cgraph_create_edge (node, callee, stmt, count,
951 freq);
952 edge->inline_failed = reason;
955 if (node->clones)
956 node = node->clones;
957 else if (node->next_sibling_clone)
958 node = node->next_sibling_clone;
959 else
961 while (node != orig && !node->next_sibling_clone)
962 node = node->clone_of;
963 if (node != orig)
964 node = node->next_sibling_clone;
969 /* Allocate a cgraph_edge structure and fill it with data according to the
970 parameters of which only CALLEE can be NULL (when creating an indirect call
971 edge). */
973 static struct cgraph_edge *
974 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
975 gimple call_stmt, gcov_type count, int freq)
977 struct cgraph_edge *edge;
979 /* LTO does not actually have access to the call_stmt since these
980 have not been loaded yet. */
981 if (call_stmt)
983 /* This is a rather expensive check possibly triggering
984 construction of call stmt hashtable. */
985 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
987 gcc_assert (is_gimple_call (call_stmt));
990 if (free_edges)
992 edge = free_edges;
993 free_edges = NEXT_FREE_EDGE (edge);
995 else
997 edge = ggc_alloc_cgraph_edge ();
998 edge->uid = cgraph_edge_max_uid++;
1001 edge->aux = NULL;
1002 edge->caller = caller;
1003 edge->callee = callee;
1004 edge->prev_caller = NULL;
1005 edge->next_caller = NULL;
1006 edge->prev_callee = NULL;
1007 edge->next_callee = NULL;
1009 edge->count = count;
1010 gcc_assert (count >= 0);
1011 edge->frequency = freq;
1012 gcc_assert (freq >= 0);
1013 gcc_assert (freq <= CGRAPH_FREQ_MAX);
1015 edge->call_stmt = call_stmt;
1016 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1017 edge->can_throw_external
1018 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1019 pop_cfun ();
1020 edge->call_stmt_cannot_inline_p =
1021 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1022 if (call_stmt && caller->call_site_hash)
1023 cgraph_add_edge_to_call_site_hash (edge);
1025 edge->indirect_info = NULL;
1026 edge->indirect_inlining_edge = 0;
1028 return edge;
1031 /* Create edge from CALLER to CALLEE in the cgraph. */
1033 struct cgraph_edge *
1034 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1035 gimple call_stmt, gcov_type count, int freq)
1037 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1038 count, freq);
1040 edge->indirect_unknown_callee = 0;
1041 initialize_inline_failed (edge);
1043 edge->next_caller = callee->callers;
1044 if (callee->callers)
1045 callee->callers->prev_caller = edge;
1046 edge->next_callee = caller->callees;
1047 if (caller->callees)
1048 caller->callees->prev_callee = edge;
1049 caller->callees = edge;
1050 callee->callers = edge;
1052 return edge;
1055 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1057 struct cgraph_indirect_call_info *
1058 cgraph_allocate_init_indirect_info (void)
1060 struct cgraph_indirect_call_info *ii;
1062 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1063 ii->param_index = -1;
1064 return ii;
1067 /* Create an indirect edge with a yet-undetermined callee where the call
1068 statement destination is a formal parameter of the caller with index
1069 PARAM_INDEX. */
1071 struct cgraph_edge *
1072 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1073 int ecf_flags,
1074 gcov_type count, int freq)
1076 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1077 count, freq);
1079 edge->indirect_unknown_callee = 1;
1080 initialize_inline_failed (edge);
1082 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1083 edge->indirect_info->ecf_flags = ecf_flags;
1085 edge->next_callee = caller->indirect_calls;
1086 if (caller->indirect_calls)
1087 caller->indirect_calls->prev_callee = edge;
1088 caller->indirect_calls = edge;
1090 return edge;
1093 /* Remove the edge E from the list of the callers of the callee. */
1095 static inline void
1096 cgraph_edge_remove_callee (struct cgraph_edge *e)
1098 gcc_assert (!e->indirect_unknown_callee);
1099 if (e->prev_caller)
1100 e->prev_caller->next_caller = e->next_caller;
1101 if (e->next_caller)
1102 e->next_caller->prev_caller = e->prev_caller;
1103 if (!e->prev_caller)
1104 e->callee->callers = e->next_caller;
1107 /* Remove the edge E from the list of the callees of the caller. */
1109 static inline void
1110 cgraph_edge_remove_caller (struct cgraph_edge *e)
1112 if (e->prev_callee)
1113 e->prev_callee->next_callee = e->next_callee;
1114 if (e->next_callee)
1115 e->next_callee->prev_callee = e->prev_callee;
1116 if (!e->prev_callee)
1118 if (e->indirect_unknown_callee)
1119 e->caller->indirect_calls = e->next_callee;
1120 else
1121 e->caller->callees = e->next_callee;
1123 if (e->caller->call_site_hash)
1124 htab_remove_elt_with_hash (e->caller->call_site_hash,
1125 e->call_stmt,
1126 htab_hash_pointer (e->call_stmt));
1129 /* Put the edge onto the free list. */
1131 static void
1132 cgraph_free_edge (struct cgraph_edge *e)
1134 int uid = e->uid;
1136 /* Clear out the edge so we do not dangle pointers. */
1137 memset (e, 0, sizeof (*e));
1138 e->uid = uid;
1139 NEXT_FREE_EDGE (e) = free_edges;
1140 free_edges = e;
1143 /* Remove the edge E in the cgraph. */
1145 void
1146 cgraph_remove_edge (struct cgraph_edge *e)
1148 /* Call all edge removal hooks. */
1149 cgraph_call_edge_removal_hooks (e);
1151 if (!e->indirect_unknown_callee)
1152 /* Remove from callers list of the callee. */
1153 cgraph_edge_remove_callee (e);
1155 /* Remove from callees list of the callers. */
1156 cgraph_edge_remove_caller (e);
1158 /* Put the edge onto the free list. */
1159 cgraph_free_edge (e);
1162 /* Set callee of call graph edge E and add it to the corresponding set of
1163 callers. */
1165 static void
1166 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1168 e->prev_caller = NULL;
1169 if (n->callers)
1170 n->callers->prev_caller = e;
1171 e->next_caller = n->callers;
1172 n->callers = e;
1173 e->callee = n;
1176 /* Redirect callee of E to N. The function does not update underlying
1177 call expression. */
1179 void
1180 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1182 /* Remove from callers list of the current callee. */
1183 cgraph_edge_remove_callee (e);
1185 /* Insert to callers list of the new callee. */
1186 cgraph_set_edge_callee (e, n);
1189 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1190 CALLEE. DELTA is an integer constant that is to be added to the this
1191 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1193 void
1194 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1195 HOST_WIDE_INT delta)
1197 edge->indirect_unknown_callee = 0;
1198 edge->indirect_info->thunk_delta = delta;
1200 /* Get the edge out of the indirect edge list. */
1201 if (edge->prev_callee)
1202 edge->prev_callee->next_callee = edge->next_callee;
1203 if (edge->next_callee)
1204 edge->next_callee->prev_callee = edge->prev_callee;
1205 if (!edge->prev_callee)
1206 edge->caller->indirect_calls = edge->next_callee;
1208 /* Put it into the normal callee list */
1209 edge->prev_callee = NULL;
1210 edge->next_callee = edge->caller->callees;
1211 if (edge->caller->callees)
1212 edge->caller->callees->prev_callee = edge;
1213 edge->caller->callees = edge;
1215 /* Insert to callers list of the new callee. */
1216 cgraph_set_edge_callee (edge, callee);
1218 /* We need to re-determine the inlining status of the edge. */
1219 initialize_inline_failed (edge);
1223 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1224 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1225 of OLD_STMT if it was previously call statement. */
1227 static void
1228 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1229 gimple old_stmt, tree old_call, gimple new_stmt)
1231 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1233 /* We are seeing indirect calls, then there is nothing to update. */
1234 if (!new_call && !old_call)
1235 return;
1236 /* See if we turned indirect call into direct call or folded call to one builtin
1237 into different builtin. */
1238 if (old_call != new_call)
1240 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1241 struct cgraph_edge *ne = NULL;
1242 gcov_type count;
1243 int frequency;
1245 if (e)
1247 /* See if the edge is already there and has the correct callee. It
1248 might be so because of indirect inlining has already updated
1249 it. We also might've cloned and redirected the edge. */
1250 if (new_call && e->callee)
1252 struct cgraph_node *callee = e->callee;
1253 while (callee)
1255 if (callee->decl == new_call
1256 || callee->former_clone_of == new_call)
1257 return;
1258 callee = callee->clone_of;
1262 /* Otherwise remove edge and create new one; we can't simply redirect
1263 since function has changed, so inline plan and other information
1264 attached to edge is invalid. */
1265 count = e->count;
1266 frequency = e->frequency;
1267 cgraph_remove_edge (e);
1269 else
1271 /* We are seeing new direct call; compute profile info based on BB. */
1272 basic_block bb = gimple_bb (new_stmt);
1273 count = bb->count;
1274 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1275 bb);
1278 if (new_call)
1280 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1281 new_stmt, count, frequency);
1282 gcc_assert (ne->inline_failed);
1285 /* We only updated the call stmt; update pointer in cgraph edge.. */
1286 else if (old_stmt != new_stmt)
1287 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1290 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1291 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1292 of OLD_STMT before it was updated (updating can happen inplace). */
1294 void
1295 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1297 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1298 struct cgraph_node *node;
1300 gcc_checking_assert (orig);
1301 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1302 if (orig->clones)
1303 for (node = orig->clones; node != orig;)
1305 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1306 if (node->clones)
1307 node = node->clones;
1308 else if (node->next_sibling_clone)
1309 node = node->next_sibling_clone;
1310 else
1312 while (node != orig && !node->next_sibling_clone)
1313 node = node->clone_of;
1314 if (node != orig)
1315 node = node->next_sibling_clone;
1321 /* Remove all callees from the node. */
1323 void
1324 cgraph_node_remove_callees (struct cgraph_node *node)
1326 struct cgraph_edge *e, *f;
1328 /* It is sufficient to remove the edges from the lists of callers of
1329 the callees. The callee list of the node can be zapped with one
1330 assignment. */
1331 for (e = node->callees; e; e = f)
1333 f = e->next_callee;
1334 cgraph_call_edge_removal_hooks (e);
1335 if (!e->indirect_unknown_callee)
1336 cgraph_edge_remove_callee (e);
1337 cgraph_free_edge (e);
1339 for (e = node->indirect_calls; e; e = f)
1341 f = e->next_callee;
1342 cgraph_call_edge_removal_hooks (e);
1343 if (!e->indirect_unknown_callee)
1344 cgraph_edge_remove_callee (e);
1345 cgraph_free_edge (e);
1347 node->indirect_calls = NULL;
1348 node->callees = NULL;
1349 if (node->call_site_hash)
1351 htab_delete (node->call_site_hash);
1352 node->call_site_hash = NULL;
1356 /* Remove all callers from the node. */
1358 static void
1359 cgraph_node_remove_callers (struct cgraph_node *node)
1361 struct cgraph_edge *e, *f;
1363 /* It is sufficient to remove the edges from the lists of callees of
1364 the callers. The caller list of the node can be zapped with one
1365 assignment. */
1366 for (e = node->callers; e; e = f)
1368 f = e->next_caller;
1369 cgraph_call_edge_removal_hooks (e);
1370 cgraph_edge_remove_caller (e);
1371 cgraph_free_edge (e);
1373 node->callers = NULL;
1376 /* Release memory used to represent body of function NODE. */
1378 void
1379 cgraph_release_function_body (struct cgraph_node *node)
1381 if (DECL_STRUCT_FUNCTION (node->decl))
1383 tree old_decl = current_function_decl;
1384 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1385 if (cfun->gimple_df)
1387 current_function_decl = node->decl;
1388 delete_tree_ssa ();
1389 delete_tree_cfg_annotations ();
1390 cfun->eh = NULL;
1391 current_function_decl = old_decl;
1393 if (cfun->cfg)
1395 gcc_assert (dom_computed[0] == DOM_NONE);
1396 gcc_assert (dom_computed[1] == DOM_NONE);
1397 clear_edges ();
1399 if (cfun->value_histograms)
1400 free_histograms ();
1401 gcc_assert (!current_loops);
1402 pop_cfun();
1403 gimple_set_body (node->decl, NULL);
1404 VEC_free (ipa_opt_pass, heap,
1405 node->ipa_transforms_to_apply);
1406 /* Struct function hangs a lot of data that would leak if we didn't
1407 removed all pointers to it. */
1408 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1409 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1411 DECL_SAVED_TREE (node->decl) = NULL;
1412 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1413 of its associated function function declaration because it's
1414 needed to emit debug info later. */
1415 if (!node->abstract_and_needed)
1416 DECL_INITIAL (node->decl) = error_mark_node;
1419 /* Remove same body alias node. */
1421 void
1422 cgraph_remove_same_body_alias (struct cgraph_node *node)
1424 void **slot;
1425 int uid = node->uid;
1427 gcc_assert (node->same_body_alias);
1428 if (node->previous)
1429 node->previous->next = node->next;
1430 else
1431 node->same_body->same_body = node->next;
1432 if (node->next)
1433 node->next->previous = node->previous;
1434 node->next = NULL;
1435 node->previous = NULL;
1436 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1437 if (*slot == node)
1438 htab_clear_slot (cgraph_hash, slot);
1439 if (assembler_name_hash)
1441 tree name = DECL_ASSEMBLER_NAME (node->decl);
1442 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1443 decl_assembler_name_hash (name),
1444 NO_INSERT);
1445 if (slot && *slot == node)
1446 htab_clear_slot (assembler_name_hash, slot);
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;
1457 /* Remove the node from cgraph. */
1459 void
1460 cgraph_remove_node (struct cgraph_node *node)
1462 void **slot;
1463 bool kill_body = false;
1464 struct cgraph_node *n;
1465 int uid = node->uid;
1467 cgraph_call_node_removal_hooks (node);
1468 cgraph_node_remove_callers (node);
1469 cgraph_node_remove_callees (node);
1470 ipa_remove_all_references (&node->ref_list);
1471 ipa_remove_all_refering (&node->ref_list);
1472 VEC_free (ipa_opt_pass, heap,
1473 node->ipa_transforms_to_apply);
1475 /* Incremental inlining access removed nodes stored in the postorder list.
1477 node->needed = node->reachable = false;
1478 for (n = node->nested; n; n = n->next_nested)
1479 n->origin = NULL;
1480 node->nested = NULL;
1481 if (node->origin)
1483 struct cgraph_node **node2 = &node->origin->nested;
1485 while (*node2 != node)
1486 node2 = &(*node2)->next_nested;
1487 *node2 = node->next_nested;
1489 if (node->previous)
1490 node->previous->next = node->next;
1491 else
1492 cgraph_nodes = node->next;
1493 if (node->next)
1494 node->next->previous = node->previous;
1495 node->next = NULL;
1496 node->previous = NULL;
1497 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1498 if (*slot == node)
1500 struct cgraph_node *next_inline_clone;
1502 for (next_inline_clone = node->clones;
1503 next_inline_clone && next_inline_clone->decl != node->decl;
1504 next_inline_clone = next_inline_clone->next_sibling_clone)
1507 /* If there is inline clone of the node being removed, we need
1508 to put it into the position of removed node and reorganize all
1509 other clones to be based on it. */
1510 if (next_inline_clone)
1512 struct cgraph_node *n;
1513 struct cgraph_node *new_clones;
1515 *slot = next_inline_clone;
1517 /* Unlink inline clone from the list of clones of removed node. */
1518 if (next_inline_clone->next_sibling_clone)
1519 next_inline_clone->next_sibling_clone->prev_sibling_clone
1520 = next_inline_clone->prev_sibling_clone;
1521 if (next_inline_clone->prev_sibling_clone)
1523 gcc_assert (node->clones != next_inline_clone);
1524 next_inline_clone->prev_sibling_clone->next_sibling_clone
1525 = next_inline_clone->next_sibling_clone;
1527 else
1529 gcc_assert (node->clones == next_inline_clone);
1530 node->clones = next_inline_clone->next_sibling_clone;
1533 new_clones = node->clones;
1534 node->clones = NULL;
1536 /* Copy clone info. */
1537 next_inline_clone->clone = node->clone;
1539 /* Now place it into clone tree at same level at NODE. */
1540 next_inline_clone->clone_of = node->clone_of;
1541 next_inline_clone->prev_sibling_clone = NULL;
1542 next_inline_clone->next_sibling_clone = NULL;
1543 if (node->clone_of)
1545 if (node->clone_of->clones)
1546 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1547 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1548 node->clone_of->clones = next_inline_clone;
1551 /* Merge the clone list. */
1552 if (new_clones)
1554 if (!next_inline_clone->clones)
1555 next_inline_clone->clones = new_clones;
1556 else
1558 n = next_inline_clone->clones;
1559 while (n->next_sibling_clone)
1560 n = n->next_sibling_clone;
1561 n->next_sibling_clone = new_clones;
1562 new_clones->prev_sibling_clone = n;
1566 /* Update clone_of pointers. */
1567 n = new_clones;
1568 while (n)
1570 n->clone_of = next_inline_clone;
1571 n = n->next_sibling_clone;
1574 else
1576 htab_clear_slot (cgraph_hash, slot);
1577 kill_body = true;
1581 if (node->prev_sibling_clone)
1582 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1583 else if (node->clone_of)
1584 node->clone_of->clones = node->next_sibling_clone;
1585 if (node->next_sibling_clone)
1586 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1587 if (node->clones)
1589 struct cgraph_node *n, *next;
1591 if (node->clone_of)
1593 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1594 n->clone_of = node->clone_of;
1595 n->clone_of = node->clone_of;
1596 n->next_sibling_clone = node->clone_of->clones;
1597 if (node->clone_of->clones)
1598 node->clone_of->clones->prev_sibling_clone = n;
1599 node->clone_of->clones = node->clones;
1601 else
1603 /* We are removing node with clones. this makes clones inconsistent,
1604 but assume they will be removed subsequently and just keep clone
1605 tree intact. This can happen in unreachable function removal since
1606 we remove unreachable functions in random order, not by bottom-up
1607 walk of clone trees. */
1608 for (n = node->clones; n; n = next)
1610 next = n->next_sibling_clone;
1611 n->next_sibling_clone = NULL;
1612 n->prev_sibling_clone = NULL;
1613 n->clone_of = NULL;
1618 while (node->same_body)
1619 cgraph_remove_same_body_alias (node->same_body);
1621 if (node->same_comdat_group)
1623 struct cgraph_node *prev;
1624 for (prev = node->same_comdat_group;
1625 prev->same_comdat_group != node;
1626 prev = prev->same_comdat_group)
1628 if (node->same_comdat_group == prev)
1629 prev->same_comdat_group = NULL;
1630 else
1631 prev->same_comdat_group = node->same_comdat_group;
1632 node->same_comdat_group = NULL;
1635 /* While all the clones are removed after being proceeded, the function
1636 itself is kept in the cgraph even after it is compiled. Check whether
1637 we are done with this body and reclaim it proactively if this is the case.
1639 if (!kill_body && *slot)
1641 struct cgraph_node *n = (struct cgraph_node *) *slot;
1642 if (!n->clones && !n->clone_of && !n->global.inlined_to
1643 && (cgraph_global_info_ready
1644 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1645 || n->in_other_partition)))
1646 kill_body = true;
1648 if (assembler_name_hash)
1650 tree name = DECL_ASSEMBLER_NAME (node->decl);
1651 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1652 decl_assembler_name_hash (name),
1653 NO_INSERT);
1654 /* Inline clones are not hashed. */
1655 if (slot && *slot == node)
1656 htab_clear_slot (assembler_name_hash, slot);
1659 if (kill_body)
1660 cgraph_release_function_body (node);
1661 node->decl = NULL;
1662 if (node->call_site_hash)
1664 htab_delete (node->call_site_hash);
1665 node->call_site_hash = NULL;
1667 cgraph_n_nodes--;
1669 /* Clear out the node to NULL all pointers and add the node to the free
1670 list. */
1671 memset (node, 0, sizeof(*node));
1672 node->uid = uid;
1673 NEXT_FREE_NODE (node) = free_nodes;
1674 free_nodes = node;
1677 /* Remove the node from cgraph. */
1679 void
1680 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1682 struct cgraph_edge *e, *next;
1683 for (e = node->callees; e; e = next)
1685 next = e->next_callee;
1686 if (!e->inline_failed)
1687 cgraph_remove_node_and_inline_clones (e->callee);
1689 cgraph_remove_node (node);
1692 /* Notify finalize_compilation_unit that given node is reachable. */
1694 void
1695 cgraph_mark_reachable_node (struct cgraph_node *node)
1697 if (!node->reachable && node->local.finalized)
1699 if (cgraph_global_info_ready)
1701 /* Verify that function does not appear to be needed out of blue
1702 during the optimization process. This can happen for extern
1703 inlines when bodies was removed after inlining. */
1704 gcc_assert ((node->analyzed || node->in_other_partition
1705 || DECL_EXTERNAL (node->decl)));
1707 else
1708 notice_global_symbol (node->decl);
1709 node->reachable = 1;
1711 node->next_needed = cgraph_nodes_queue;
1712 cgraph_nodes_queue = node;
1716 /* Likewise indicate that a node is needed, i.e. reachable via some
1717 external means. */
1719 void
1720 cgraph_mark_needed_node (struct cgraph_node *node)
1722 node->needed = 1;
1723 gcc_assert (!node->global.inlined_to);
1724 cgraph_mark_reachable_node (node);
1727 /* Likewise indicate that a node is having address taken. */
1729 void
1730 cgraph_mark_address_taken_node (struct cgraph_node *node)
1732 gcc_assert (!node->global.inlined_to);
1733 cgraph_mark_reachable_node (node);
1734 node->address_taken = 1;
1737 /* Return local info for the compiled function. */
1739 struct cgraph_local_info *
1740 cgraph_local_info (tree decl)
1742 struct cgraph_node *node;
1744 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1745 node = cgraph_get_node (decl);
1746 if (!node)
1747 return NULL;
1748 return &node->local;
1751 /* Return local info for the compiled function. */
1753 struct cgraph_global_info *
1754 cgraph_global_info (tree decl)
1756 struct cgraph_node *node;
1758 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1759 node = cgraph_get_node (decl);
1760 if (!node)
1761 return NULL;
1762 return &node->global;
1765 /* Return local info for the compiled function. */
1767 struct cgraph_rtl_info *
1768 cgraph_rtl_info (tree decl)
1770 struct cgraph_node *node;
1772 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1773 node = cgraph_get_node (decl);
1774 if (!node
1775 || (decl != current_function_decl
1776 && !TREE_ASM_WRITTEN (node->decl)))
1777 return NULL;
1778 return &node->rtl;
1781 /* Return a string describing the failure REASON. */
1783 const char*
1784 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1786 #undef DEFCIFCODE
1787 #define DEFCIFCODE(code, string) string,
1789 static const char *cif_string_table[CIF_N_REASONS] = {
1790 #include "cif-code.def"
1793 /* Signedness of an enum type is implementation defined, so cast it
1794 to unsigned before testing. */
1795 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1796 return cif_string_table[reason];
1799 /* Return name of the node used in debug output. */
1800 const char *
1801 cgraph_node_name (struct cgraph_node *node)
1803 return lang_hooks.decl_printable_name (node->decl, 2);
1806 /* Names used to print out the availability enum. */
1807 const char * const cgraph_availability_names[] =
1808 {"unset", "not_available", "overwritable", "available", "local"};
1811 /* Dump call graph node NODE to file F. */
1813 void
1814 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1816 struct cgraph_edge *edge;
1817 int indirect_calls_count = 0;
1819 fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
1820 dump_addr (f, " @", (void *)node);
1821 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1822 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1823 if (node->global.inlined_to)
1824 fprintf (f, " (inline copy in %s/%i)",
1825 cgraph_node_name (node->global.inlined_to),
1826 node->global.inlined_to->uid);
1827 if (node->same_comdat_group)
1828 fprintf (f, " (same comdat group as %s/%i)",
1829 cgraph_node_name (node->same_comdat_group),
1830 node->same_comdat_group->uid);
1831 if (node->clone_of)
1832 fprintf (f, " (clone of %s/%i)",
1833 cgraph_node_name (node->clone_of),
1834 node->clone_of->uid);
1835 if (cgraph_function_flags_ready)
1836 fprintf (f, " availability:%s",
1837 cgraph_availability_names [cgraph_function_body_availability (node)]);
1838 if (node->analyzed)
1839 fprintf (f, " analyzed");
1840 if (node->in_other_partition)
1841 fprintf (f, " in_other_partition");
1842 if (node->count)
1843 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1844 (HOST_WIDEST_INT)node->count);
1845 if (node->origin)
1846 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1847 if (node->needed)
1848 fprintf (f, " needed");
1849 if (node->address_taken)
1850 fprintf (f, " address_taken");
1851 else if (node->reachable)
1852 fprintf (f, " reachable");
1853 else if (node->reachable_from_other_partition)
1854 fprintf (f, " reachable_from_other_partition");
1855 if (gimple_has_body_p (node->decl))
1856 fprintf (f, " body");
1857 if (node->process)
1858 fprintf (f, " process");
1859 if (node->local.local)
1860 fprintf (f, " local");
1861 if (node->local.externally_visible)
1862 fprintf (f, " externally_visible");
1863 if (node->resolution != LDPR_UNKNOWN)
1864 fprintf (f, " %s",
1865 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1866 if (node->local.finalized)
1867 fprintf (f, " finalized");
1868 if (node->local.redefined_extern_inline)
1869 fprintf (f, " redefined_extern_inline");
1870 if (TREE_ASM_WRITTEN (node->decl))
1871 fprintf (f, " asm_written");
1872 if (node->only_called_at_startup)
1873 fprintf (f, " only_called_at_startup");
1874 if (node->only_called_at_exit)
1875 fprintf (f, " only_called_at_exit");
1877 fprintf (f, "\n called by: ");
1878 for (edge = node->callers; edge; edge = edge->next_caller)
1880 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1881 edge->caller->uid);
1882 if (edge->count)
1883 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1884 (HOST_WIDEST_INT)edge->count);
1885 if (edge->frequency)
1886 fprintf (f, "(%.2f per call) ",
1887 edge->frequency / (double)CGRAPH_FREQ_BASE);
1888 if (!edge->inline_failed)
1889 fprintf(f, "(inlined) ");
1890 if (edge->indirect_inlining_edge)
1891 fprintf(f, "(indirect_inlining) ");
1892 if (edge->can_throw_external)
1893 fprintf(f, "(can throw external) ");
1896 fprintf (f, "\n calls: ");
1897 for (edge = node->callees; edge; edge = edge->next_callee)
1899 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1900 edge->callee->uid);
1901 if (!edge->inline_failed)
1902 fprintf(f, "(inlined) ");
1903 if (edge->indirect_inlining_edge)
1904 fprintf(f, "(indirect_inlining) ");
1905 if (edge->count)
1906 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1907 (HOST_WIDEST_INT)edge->count);
1908 if (edge->frequency)
1909 fprintf (f, "(%.2f per call) ",
1910 edge->frequency / (double)CGRAPH_FREQ_BASE);
1911 if (edge->can_throw_external)
1912 fprintf(f, "(can throw external) ");
1914 fprintf (f, "\n");
1915 fprintf (f, " References: ");
1916 ipa_dump_references (f, &node->ref_list);
1917 fprintf (f, " Refering this function: ");
1918 ipa_dump_refering (f, &node->ref_list);
1920 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1921 indirect_calls_count++;
1922 if (indirect_calls_count)
1923 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1924 indirect_calls_count);
1926 if (node->same_body)
1928 struct cgraph_node *n;
1929 fprintf (f, " aliases & thunks:");
1930 for (n = node->same_body; n; n = n->next)
1932 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1933 if (n->thunk.thunk_p)
1935 fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1936 "virtual offset %i",
1937 lang_hooks.decl_printable_name (n->thunk.alias, 2),
1938 (int)n->thunk.fixed_offset,
1939 (int)n->thunk.virtual_value,
1940 (int)n->thunk.virtual_offset_p);
1941 fprintf (f, ")");
1943 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1944 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1946 fprintf (f, "\n");
1951 /* Dump call graph node NODE to stderr. */
1953 DEBUG_FUNCTION void
1954 debug_cgraph_node (struct cgraph_node *node)
1956 dump_cgraph_node (stderr, node);
1960 /* Dump the callgraph to file F. */
1962 void
1963 dump_cgraph (FILE *f)
1965 struct cgraph_node *node;
1967 fprintf (f, "callgraph:\n\n");
1968 for (node = cgraph_nodes; node; node = node->next)
1969 dump_cgraph_node (f, node);
1973 /* Dump the call graph to stderr. */
1975 DEBUG_FUNCTION void
1976 debug_cgraph (void)
1978 dump_cgraph (stderr);
1982 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
1984 void
1985 change_decl_assembler_name (tree decl, tree name)
1987 struct cgraph_node *node;
1988 void **slot;
1989 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1990 SET_DECL_ASSEMBLER_NAME (decl, name);
1991 else
1993 if (name == DECL_ASSEMBLER_NAME (decl))
1994 return;
1996 if (assembler_name_hash
1997 && TREE_CODE (decl) == FUNCTION_DECL
1998 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2000 tree old_name = DECL_ASSEMBLER_NAME (decl);
2001 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2002 decl_assembler_name_hash (old_name),
2003 NO_INSERT);
2004 /* Inline clones are not hashed. */
2005 if (slot && *slot == node)
2006 htab_clear_slot (assembler_name_hash, slot);
2008 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2009 && DECL_RTL_SET_P (decl))
2010 warning (0, "%D renamed after being referenced in assembly", decl);
2012 SET_DECL_ASSEMBLER_NAME (decl, name);
2014 if (assembler_name_hash
2015 && TREE_CODE (decl) == FUNCTION_DECL
2016 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2018 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2019 decl_assembler_name_hash (name),
2020 INSERT);
2021 gcc_assert (!*slot);
2022 *slot = node;
2026 /* Add a top-level asm statement to the list. */
2028 struct cgraph_asm_node *
2029 cgraph_add_asm_node (tree asm_str)
2031 struct cgraph_asm_node *node;
2033 node = ggc_alloc_cleared_cgraph_asm_node ();
2034 node->asm_str = asm_str;
2035 node->order = cgraph_order++;
2036 node->next = NULL;
2037 if (cgraph_asm_nodes == NULL)
2038 cgraph_asm_nodes = node;
2039 else
2040 cgraph_asm_last_node->next = node;
2041 cgraph_asm_last_node = node;
2042 return node;
2045 /* Return true when the DECL can possibly be inlined. */
2046 bool
2047 cgraph_function_possibly_inlined_p (tree decl)
2049 if (!cgraph_global_info_ready)
2050 return !DECL_UNINLINABLE (decl);
2051 return DECL_POSSIBLY_INLINED (decl);
2054 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2055 struct cgraph_edge *
2056 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2057 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2058 int freq_scale, bool update_original)
2060 struct cgraph_edge *new_edge;
2061 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2062 gcov_type freq;
2064 /* We do not want to ignore loop nest after frequency drops to 0. */
2065 if (!freq_scale)
2066 freq_scale = 1;
2067 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2068 if (freq > CGRAPH_FREQ_MAX)
2069 freq = CGRAPH_FREQ_MAX;
2071 if (e->indirect_unknown_callee)
2073 tree decl;
2075 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2077 struct cgraph_node *callee = cgraph_get_node (decl);
2078 gcc_checking_assert (callee);
2079 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
2081 else
2083 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2084 e->indirect_info->ecf_flags,
2085 count, freq);
2086 *new_edge->indirect_info = *e->indirect_info;
2089 else
2091 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
2092 if (e->indirect_info)
2094 new_edge->indirect_info
2095 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2096 *new_edge->indirect_info = *e->indirect_info;
2100 new_edge->inline_failed = e->inline_failed;
2101 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2102 new_edge->lto_stmt_uid = stmt_uid;
2103 /* Clone flags that depend on call_stmt availability manually. */
2104 new_edge->can_throw_external = e->can_throw_external;
2105 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2106 if (update_original)
2108 e->count -= new_edge->count;
2109 if (e->count < 0)
2110 e->count = 0;
2112 cgraph_call_edge_duplication_hooks (e, new_edge);
2113 return new_edge;
2116 /* Create node representing clone of N executed COUNT times. Decrease
2117 the execution counts from original node too.
2118 The new clone will have decl set to DECL that may or may not be the same
2119 as decl of N.
2121 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2122 function's profile to reflect the fact that part of execution is handled
2123 by node. */
2124 struct cgraph_node *
2125 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2126 bool update_original,
2127 VEC(cgraph_edge_p,heap) *redirect_callers)
2129 struct cgraph_node *new_node = cgraph_create_node_1 ();
2130 struct cgraph_edge *e;
2131 gcov_type count_scale;
2132 unsigned i;
2134 new_node->decl = decl;
2135 new_node->origin = n->origin;
2136 if (new_node->origin)
2138 new_node->next_nested = new_node->origin->nested;
2139 new_node->origin->nested = new_node;
2141 new_node->analyzed = n->analyzed;
2142 new_node->local = n->local;
2143 new_node->local.externally_visible = false;
2144 new_node->local.local = true;
2145 new_node->global = n->global;
2146 new_node->rtl = n->rtl;
2147 new_node->count = count;
2148 new_node->frequency = n->frequency;
2149 new_node->clone = n->clone;
2150 new_node->clone.tree_map = 0;
2151 if (n->count)
2153 if (new_node->count > n->count)
2154 count_scale = REG_BR_PROB_BASE;
2155 else
2156 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2158 else
2159 count_scale = 0;
2160 if (update_original)
2162 n->count -= count;
2163 if (n->count < 0)
2164 n->count = 0;
2167 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2169 /* Redirect calls to the old version node to point to its new
2170 version. */
2171 cgraph_redirect_edge_callee (e, new_node);
2175 for (e = n->callees;e; e=e->next_callee)
2176 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2177 count_scale, freq, update_original);
2179 for (e = n->indirect_calls; e; e = e->next_callee)
2180 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2181 count_scale, freq, update_original);
2182 ipa_clone_references (new_node, NULL, &n->ref_list);
2184 new_node->next_sibling_clone = n->clones;
2185 if (n->clones)
2186 n->clones->prev_sibling_clone = new_node;
2187 n->clones = new_node;
2188 new_node->clone_of = n;
2190 cgraph_call_node_duplication_hooks (n, new_node);
2191 if (n->decl != decl)
2193 struct cgraph_node **slot;
2194 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2195 gcc_assert (!*slot);
2196 *slot = new_node;
2197 if (assembler_name_hash)
2199 void **aslot;
2200 tree name = DECL_ASSEMBLER_NAME (decl);
2202 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2203 decl_assembler_name_hash (name),
2204 INSERT);
2205 gcc_assert (!*aslot);
2206 *aslot = new_node;
2209 return new_node;
2212 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2214 static GTY(()) unsigned int clone_fn_id_num;
2216 tree
2217 clone_function_name (tree decl, const char *suffix)
2219 tree name = DECL_ASSEMBLER_NAME (decl);
2220 size_t len = IDENTIFIER_LENGTH (name);
2221 char *tmp_name, *prefix;
2223 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2224 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2225 strcpy (prefix + len + 1, suffix);
2226 #ifndef NO_DOT_IN_LABEL
2227 prefix[len] = '.';
2228 #elif !defined NO_DOLLAR_IN_LABEL
2229 prefix[len] = '$';
2230 #else
2231 prefix[len] = '_';
2232 #endif
2233 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2234 return get_identifier (tmp_name);
2237 /* Create callgraph node clone with new declaration. The actual body will
2238 be copied later at compilation stage.
2240 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2241 bitmap interface.
2243 struct cgraph_node *
2244 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2245 VEC(cgraph_edge_p,heap) *redirect_callers,
2246 VEC(ipa_replace_map_p,gc) *tree_map,
2247 bitmap args_to_skip,
2248 const char * suffix)
2250 tree old_decl = old_node->decl;
2251 struct cgraph_node *new_node = NULL;
2252 tree new_decl;
2253 size_t i;
2254 struct ipa_replace_map *map;
2256 if (!flag_wpa)
2257 gcc_checking_assert (tree_versionable_function_p (old_decl));
2259 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2261 /* Make a new FUNCTION_DECL tree node */
2262 if (!args_to_skip)
2263 new_decl = copy_node (old_decl);
2264 else
2265 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2266 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2268 /* Generate a new name for the new version. */
2269 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2270 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2271 SET_DECL_RTL (new_decl, NULL);
2273 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2274 CGRAPH_FREQ_BASE, false,
2275 redirect_callers);
2276 /* Update the properties.
2277 Make clone visible only within this translation unit. Make sure
2278 that is not weak also.
2279 ??? We cannot use COMDAT linkage because there is no
2280 ABI support for this. */
2281 DECL_EXTERNAL (new_node->decl) = 0;
2282 if (DECL_ONE_ONLY (old_decl))
2283 DECL_SECTION_NAME (new_node->decl) = NULL;
2284 DECL_COMDAT_GROUP (new_node->decl) = 0;
2285 TREE_PUBLIC (new_node->decl) = 0;
2286 DECL_COMDAT (new_node->decl) = 0;
2287 DECL_WEAK (new_node->decl) = 0;
2288 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
2289 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
2290 new_node->clone.tree_map = tree_map;
2291 new_node->clone.args_to_skip = args_to_skip;
2292 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2294 tree var = map->new_tree;
2296 STRIP_NOPS (var);
2297 if (TREE_CODE (var) != ADDR_EXPR)
2298 continue;
2299 var = get_base_var (var);
2300 if (!var)
2301 continue;
2303 /* Record references of the future statement initializing the constant
2304 argument. */
2305 if (TREE_CODE (var) == FUNCTION_DECL)
2307 struct cgraph_node *ref_node = cgraph_get_node (var);
2308 gcc_checking_assert (ref_node);
2309 ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2310 NULL);
2312 else if (TREE_CODE (var) == VAR_DECL)
2313 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2314 IPA_REF_ADDR, NULL);
2316 if (!args_to_skip)
2317 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2318 else if (old_node->clone.combined_args_to_skip)
2320 int newi = 0, oldi = 0;
2321 tree arg;
2322 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2323 struct cgraph_node *orig_node;
2324 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2326 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2328 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2330 bitmap_set_bit (new_args_to_skip, oldi);
2331 continue;
2333 if (bitmap_bit_p (args_to_skip, newi))
2334 bitmap_set_bit (new_args_to_skip, oldi);
2335 newi++;
2337 new_node->clone.combined_args_to_skip = new_args_to_skip;
2339 else
2340 new_node->clone.combined_args_to_skip = args_to_skip;
2341 new_node->local.externally_visible = 0;
2342 new_node->local.local = 1;
2343 new_node->lowered = true;
2344 new_node->reachable = true;
2347 return new_node;
2350 /* NODE is no longer nested function; update cgraph accordingly. */
2351 void
2352 cgraph_unnest_node (struct cgraph_node *node)
2354 struct cgraph_node **node2 = &node->origin->nested;
2355 gcc_assert (node->origin);
2357 while (*node2 != node)
2358 node2 = &(*node2)->next_nested;
2359 *node2 = node->next_nested;
2360 node->origin = NULL;
2363 /* Return function availability. See cgraph.h for description of individual
2364 return values. */
2365 enum availability
2366 cgraph_function_body_availability (struct cgraph_node *node)
2368 enum availability avail;
2369 gcc_assert (cgraph_function_flags_ready);
2370 if (!node->analyzed)
2371 avail = AVAIL_NOT_AVAILABLE;
2372 else if (node->local.local)
2373 avail = AVAIL_LOCAL;
2374 else if (!node->local.externally_visible)
2375 avail = AVAIL_AVAILABLE;
2376 /* Inline functions are safe to be analyzed even if their symbol can
2377 be overwritten at runtime. It is not meaningful to enforce any sane
2378 behaviour on replacing inline function by different body. */
2379 else if (DECL_DECLARED_INLINE_P (node->decl))
2380 avail = AVAIL_AVAILABLE;
2382 /* If the function can be overwritten, return OVERWRITABLE. Take
2383 care at least of two notable extensions - the COMDAT functions
2384 used to share template instantiations in C++ (this is symmetric
2385 to code cp_cannot_inline_tree_fn and probably shall be shared and
2386 the inlinability hooks completely eliminated).
2388 ??? Does the C++ one definition rule allow us to always return
2389 AVAIL_AVAILABLE here? That would be good reason to preserve this
2390 bit. */
2392 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2393 avail = AVAIL_OVERWRITABLE;
2394 else avail = AVAIL_AVAILABLE;
2396 return avail;
2399 /* Add the function FNDECL to the call graph.
2400 Unlike cgraph_finalize_function, this function is intended to be used
2401 by middle end and allows insertion of new function at arbitrary point
2402 of compilation. The function can be either in high, low or SSA form
2403 GIMPLE.
2405 The function is assumed to be reachable and have address taken (so no
2406 API breaking optimizations are performed on it).
2408 Main work done by this function is to enqueue the function for later
2409 processing to avoid need the passes to be re-entrant. */
2411 void
2412 cgraph_add_new_function (tree fndecl, bool lowered)
2414 struct cgraph_node *node;
2415 switch (cgraph_state)
2417 case CGRAPH_STATE_CONSTRUCTION:
2418 /* Just enqueue function to be processed at nearest occurrence. */
2419 node = cgraph_create_node (fndecl);
2420 node->next_needed = cgraph_new_nodes;
2421 if (lowered)
2422 node->lowered = true;
2423 cgraph_new_nodes = node;
2424 break;
2426 case CGRAPH_STATE_IPA:
2427 case CGRAPH_STATE_IPA_SSA:
2428 case CGRAPH_STATE_EXPANSION:
2429 /* Bring the function into finalized state and enqueue for later
2430 analyzing and compilation. */
2431 node = cgraph_get_create_node (fndecl);
2432 node->local.local = false;
2433 node->local.finalized = true;
2434 node->reachable = node->needed = true;
2435 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2437 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2438 current_function_decl = fndecl;
2439 gimple_register_cfg_hooks ();
2440 tree_lowering_passes (fndecl);
2441 bitmap_obstack_initialize (NULL);
2442 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2443 execute_pass_list (pass_early_local_passes.pass.sub);
2444 bitmap_obstack_release (NULL);
2445 pop_cfun ();
2446 current_function_decl = NULL;
2448 lowered = true;
2450 if (lowered)
2451 node->lowered = true;
2452 node->next_needed = cgraph_new_nodes;
2453 cgraph_new_nodes = node;
2454 break;
2456 case CGRAPH_STATE_FINISHED:
2457 /* At the very end of compilation we have to do all the work up
2458 to expansion. */
2459 node = cgraph_create_node (fndecl);
2460 if (lowered)
2461 node->lowered = true;
2462 cgraph_analyze_function (node);
2463 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2464 current_function_decl = fndecl;
2465 gimple_register_cfg_hooks ();
2466 bitmap_obstack_initialize (NULL);
2467 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2468 execute_pass_list (pass_early_local_passes.pass.sub);
2469 bitmap_obstack_release (NULL);
2470 tree_rest_of_compilation (fndecl);
2471 pop_cfun ();
2472 current_function_decl = NULL;
2473 break;
2476 /* Set a personality if required and we already passed EH lowering. */
2477 if (lowered
2478 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2479 == eh_personality_lang))
2480 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2483 /* Return true if NODE can be made local for API change.
2484 Extern inline functions and C++ COMDAT functions can be made local
2485 at the expense of possible code size growth if function is used in multiple
2486 compilation units. */
2487 bool
2488 cgraph_node_can_be_local_p (struct cgraph_node *node)
2490 return (!node->needed && !node->address_taken
2491 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2492 || !node->local.externally_visible));
2495 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2496 but other code such as notice_global_symbol generates rtl. */
2497 void
2498 cgraph_make_decl_local (tree decl)
2500 rtx rtl, symbol;
2502 if (TREE_CODE (decl) == VAR_DECL)
2503 DECL_COMMON (decl) = 0;
2504 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2506 if (DECL_COMDAT (decl))
2508 /* It is possible that we are linking against library defining same COMDAT
2509 function. To avoid conflict we need to rename our local name of the
2510 function just in the case WHOPR partitioning decide to make it hidden
2511 to avoid cross partition references. */
2512 if (flag_wpa)
2514 const char *old_name;
2516 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2517 if (TREE_CODE (decl) == FUNCTION_DECL)
2519 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2520 change_decl_assembler_name (decl,
2521 clone_function_name (decl, "local"));
2522 if (node->local.lto_file_data)
2523 lto_record_renamed_decl (node->local.lto_file_data,
2524 old_name,
2525 IDENTIFIER_POINTER
2526 (DECL_ASSEMBLER_NAME (decl)));
2528 else if (TREE_CODE (decl) == VAR_DECL)
2530 struct varpool_node *vnode = varpool_get_node (decl);
2531 /* change_decl_assembler_name will warn here on vtables because
2532 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2533 SET_DECL_ASSEMBLER_NAME (decl,
2534 clone_function_name (decl, "local"));
2535 if (vnode->lto_file_data)
2536 lto_record_renamed_decl (vnode->lto_file_data,
2537 old_name,
2538 IDENTIFIER_POINTER
2539 (DECL_ASSEMBLER_NAME (decl)));
2542 DECL_SECTION_NAME (decl) = 0;
2543 DECL_COMDAT (decl) = 0;
2545 DECL_COMDAT_GROUP (decl) = 0;
2546 DECL_WEAK (decl) = 0;
2547 DECL_EXTERNAL (decl) = 0;
2548 TREE_PUBLIC (decl) = 0;
2549 if (!DECL_RTL_SET_P (decl))
2550 return;
2552 /* Update rtl flags. */
2553 make_decl_rtl (decl);
2555 rtl = DECL_RTL (decl);
2556 if (!MEM_P (rtl))
2557 return;
2559 symbol = XEXP (rtl, 0);
2560 if (GET_CODE (symbol) != SYMBOL_REF)
2561 return;
2563 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2566 /* Bring NODE local. */
2567 void
2568 cgraph_make_node_local (struct cgraph_node *node)
2570 gcc_assert (cgraph_node_can_be_local_p (node));
2571 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2573 struct cgraph_node *alias;
2574 cgraph_make_decl_local (node->decl);
2576 for (alias = node->same_body; alias; alias = alias->next)
2577 cgraph_make_decl_local (alias->decl);
2579 node->local.externally_visible = false;
2580 node->local.local = true;
2581 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2582 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2586 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2587 if any to NOTHROW. */
2589 void
2590 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2592 struct cgraph_node *alias;
2593 TREE_NOTHROW (node->decl) = nothrow;
2594 for (alias = node->same_body; alias; alias = alias->next)
2595 TREE_NOTHROW (alias->decl) = nothrow;
2598 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2599 if any to READONLY. */
2601 void
2602 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2604 struct cgraph_node *alias;
2605 /* Static constructors and destructors without a side effect can be
2606 optimized out. */
2607 if (!looping && readonly)
2609 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2610 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2611 if (DECL_STATIC_DESTRUCTOR (node->decl))
2612 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2614 TREE_READONLY (node->decl) = readonly;
2615 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2616 for (alias = node->same_body; alias; alias = alias->next)
2618 TREE_READONLY (alias->decl) = readonly;
2619 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2623 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2624 if any to PURE. */
2626 void
2627 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2629 struct cgraph_node *alias;
2630 /* Static constructors and destructors without a side effect can be
2631 optimized out. */
2632 if (!looping && pure)
2634 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2635 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2636 if (DECL_STATIC_DESTRUCTOR (node->decl))
2637 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2639 DECL_PURE_P (node->decl) = pure;
2640 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2641 for (alias = node->same_body; alias; alias = alias->next)
2643 DECL_PURE_P (alias->decl) = pure;
2644 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2648 /* See if the frequency of NODE can be updated based on frequencies of its
2649 callers. */
2650 bool
2651 cgraph_propagate_frequency (struct cgraph_node *node)
2653 bool maybe_unlikely_executed = true, maybe_executed_once = true;
2654 bool only_called_at_startup = true;
2655 bool only_called_at_exit = true;
2656 bool changed = false;
2657 struct cgraph_edge *edge;
2659 if (!node->local.local)
2660 return false;
2661 gcc_assert (node->analyzed);
2662 if (dump_file && (dump_flags & TDF_DETAILS))
2663 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2665 for (edge = node->callers;
2666 edge && (maybe_unlikely_executed || maybe_executed_once
2667 || only_called_at_startup || only_called_at_exit);
2668 edge = edge->next_caller)
2670 if (edge->caller != node)
2672 only_called_at_startup &= edge->caller->only_called_at_startup;
2673 /* It makes sense to put main() together with the static constructors.
2674 It will be executed for sure, but rest of functions called from
2675 main are definitely not at startup only. */
2676 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2677 only_called_at_startup = 0;
2678 only_called_at_exit &= edge->caller->only_called_at_exit;
2680 if (!edge->frequency)
2681 continue;
2682 switch (edge->caller->frequency)
2684 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2685 break;
2686 case NODE_FREQUENCY_EXECUTED_ONCE:
2687 if (dump_file && (dump_flags & TDF_DETAILS))
2688 fprintf (dump_file, " Called by %s that is executed once\n",
2689 cgraph_node_name (edge->caller));
2690 maybe_unlikely_executed = false;
2691 if (inline_edge_summary (edge)->loop_depth)
2693 maybe_executed_once = false;
2694 if (dump_file && (dump_flags & TDF_DETAILS))
2695 fprintf (dump_file, " Called in loop\n");
2697 break;
2698 case NODE_FREQUENCY_HOT:
2699 case NODE_FREQUENCY_NORMAL:
2700 if (dump_file && (dump_flags & TDF_DETAILS))
2701 fprintf (dump_file, " Called by %s that is normal or hot\n",
2702 cgraph_node_name (edge->caller));
2703 maybe_unlikely_executed = false;
2704 maybe_executed_once = false;
2705 break;
2708 if ((only_called_at_startup && !only_called_at_exit)
2709 && !node->only_called_at_startup)
2711 node->only_called_at_startup = true;
2712 if (dump_file)
2713 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2714 cgraph_node_name (node));
2715 changed = true;
2717 if ((only_called_at_exit && !only_called_at_startup)
2718 && !node->only_called_at_exit)
2720 node->only_called_at_exit = true;
2721 if (dump_file)
2722 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2723 cgraph_node_name (node));
2724 changed = true;
2726 /* These come either from profile or user hints; never update them. */
2727 if (node->frequency == NODE_FREQUENCY_HOT
2728 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2729 return changed;
2730 if (maybe_unlikely_executed)
2732 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2733 if (dump_file)
2734 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2735 cgraph_node_name (node));
2736 changed = true;
2738 else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2740 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2741 if (dump_file)
2742 fprintf (dump_file, "Node %s promoted to executed once.\n",
2743 cgraph_node_name (node));
2744 changed = true;
2746 return changed;
2749 /* Return true when NODE can not return or throw and thus
2750 it is safe to ignore its side effects for IPA analysis. */
2752 bool
2753 cgraph_node_cannot_return (struct cgraph_node *node)
2755 int flags = flags_from_decl_or_type (node->decl);
2756 if (!flag_exceptions)
2757 return (flags & ECF_NORETURN) != 0;
2758 else
2759 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2760 == (ECF_NORETURN | ECF_NOTHROW));
2763 /* Return true when call of E can not lead to return from caller
2764 and thus it is safe to ignore its side effects for IPA analysis
2765 when computing side effects of the caller.
2766 FIXME: We could actually mark all edges that have no reaching
2767 patch to EXIT_BLOCK_PTR or throw to get better results. */
2768 bool
2769 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2771 if (cgraph_node_cannot_return (e->caller))
2772 return true;
2773 if (e->indirect_unknown_callee)
2775 int flags = e->indirect_info->ecf_flags;
2776 if (!flag_exceptions)
2777 return (flags & ECF_NORETURN) != 0;
2778 else
2779 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2780 == (ECF_NORETURN | ECF_NOTHROW));
2782 else
2783 return cgraph_node_cannot_return (e->callee);
2786 /* Return true when function NODE can be removed from callgraph
2787 if all direct calls are eliminated. */
2789 bool
2790 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2792 gcc_assert (!node->global.inlined_to);
2793 /* Extern inlines can always go, we will use the external definition. */
2794 if (DECL_EXTERNAL (node->decl))
2795 return true;
2796 /* When function is needed, we can not remove it. */
2797 if (node->needed || node->reachable_from_other_partition)
2798 return false;
2799 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2800 || DECL_STATIC_DESTRUCTOR (node->decl))
2801 return false;
2802 /* Only COMDAT functions can be removed if externally visible. */
2803 if (node->local.externally_visible
2804 && (!DECL_COMDAT (node->decl)
2805 || cgraph_used_from_object_file_p (node)))
2806 return false;
2807 return true;
2810 /* Return true when function NODE can be expected to be removed
2811 from program when direct calls in this compilation unit are removed.
2813 As a special case COMDAT functions are
2814 cgraph_can_remove_if_no_direct_calls_p while the are not
2815 cgraph_only_called_directly_p (it is possible they are called from other
2816 unit)
2818 This function behaves as cgraph_only_called_directly_p because eliminating
2819 all uses of COMDAT function does not make it necessarily disappear from
2820 the program unless we are compiling whole program or we do LTO. In this
2821 case we know we win since dynamic linking will not really discard the
2822 linkonce section. */
2824 bool
2825 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2827 gcc_assert (!node->global.inlined_to);
2828 if (cgraph_used_from_object_file_p (node))
2829 return false;
2830 if (!in_lto_p && !flag_whole_program)
2831 return cgraph_only_called_directly_p (node);
2832 else
2834 if (DECL_EXTERNAL (node->decl))
2835 return true;
2836 return cgraph_can_remove_if_no_direct_calls_p (node);
2840 /* Return true when RESOLUTION indicate that linker will use
2841 the symbol from non-LTO object files. */
2843 bool
2844 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2846 return (resolution == LDPR_PREVAILING_DEF
2847 || resolution == LDPR_PREEMPTED_REG
2848 || resolution == LDPR_RESOLVED_EXEC
2849 || resolution == LDPR_RESOLVED_DYN);
2852 /* Return true when NODE is known to be used from other (non-LTO) object file.
2853 Known only when doing LTO via linker plugin. */
2855 bool
2856 cgraph_used_from_object_file_p (struct cgraph_node *node)
2858 struct cgraph_node *alias;
2860 gcc_assert (!node->global.inlined_to);
2861 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2862 return false;
2863 if (resolution_used_from_other_file_p (node->resolution))
2864 return true;
2865 for (alias = node->same_body; alias; alias = alias->next)
2866 if (TREE_PUBLIC (alias->decl)
2867 && resolution_used_from_other_file_p (alias->resolution))
2868 return true;
2869 return false;
2872 #include "gt-cgraph.h"