Remove unused debug_str_hash_forced from dwarf2out.
[official-gcc.git] / gcc / cgraph.c
blob91805e30b73651c14f405fb2aa30a1a2ae9d2f17
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 || double_int_equal_p
617 (tree_to_double_int (virtual_offset),
618 shwi_to_double_int (virtual_value)));
619 node->thunk.fixed_offset = fixed_offset;
620 node->thunk.this_adjusting = this_adjusting;
621 node->thunk.virtual_value = virtual_value;
622 node->thunk.virtual_offset_p = virtual_offset != NULL;
623 node->thunk.alias = real_alias;
624 node->thunk.thunk_p = true;
625 return node;
628 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
629 is assigned. */
631 struct cgraph_node *
632 cgraph_get_node_or_alias (const_tree decl)
634 struct cgraph_node key, *node = NULL, **slot;
636 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
638 if (!cgraph_hash)
639 return NULL;
641 key.decl = CONST_CAST2 (tree, const_tree, decl);
643 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
644 NO_INSERT);
646 if (slot && *slot)
647 node = *slot;
648 return node;
651 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
652 is assigned. */
654 struct cgraph_node *
655 cgraph_get_node (const_tree decl)
657 struct cgraph_node key, *node = NULL, **slot;
659 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
661 if (!cgraph_hash)
662 return NULL;
664 key.decl = CONST_CAST2 (tree, const_tree, decl);
666 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key,
667 NO_INSERT);
669 if (slot && *slot)
671 node = *slot;
672 if (node->same_body_alias)
673 node = node->same_body;
675 return node;
678 /* Insert already constructed node into hashtable. */
680 void
681 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
683 struct cgraph_node **slot;
685 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
687 gcc_assert (!*slot);
688 *slot = node;
691 /* Returns a hash code for P. */
693 static hashval_t
694 hash_node_by_assembler_name (const void *p)
696 const struct cgraph_node *n = (const struct cgraph_node *) p;
697 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
700 /* Returns nonzero if P1 and P2 are equal. */
702 static int
703 eq_assembler_name (const void *p1, const void *p2)
705 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
706 const_tree name = (const_tree)p2;
707 return (decl_assembler_name_equal (n1->decl, name));
710 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
711 Return NULL if there's no such node. */
713 struct cgraph_node *
714 cgraph_node_for_asm (tree asmname)
716 struct cgraph_node *node;
717 void **slot;
719 if (!assembler_name_hash)
721 assembler_name_hash =
722 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
723 NULL);
724 for (node = cgraph_nodes; node; node = node->next)
725 if (!node->global.inlined_to)
727 tree name = DECL_ASSEMBLER_NAME (node->decl);
728 slot = htab_find_slot_with_hash (assembler_name_hash, name,
729 decl_assembler_name_hash (name),
730 INSERT);
731 /* We can have multiple declarations with same assembler name. For C++
732 it is __builtin_strlen and strlen, for instance. Do we need to
733 record them all? Original implementation marked just first one
734 so lets hope for the best. */
735 if (!*slot)
736 *slot = node;
737 if (node->same_body)
739 struct cgraph_node *alias;
741 for (alias = node->same_body; alias; alias = alias->next)
743 hashval_t hash;
744 name = DECL_ASSEMBLER_NAME (alias->decl);
745 hash = decl_assembler_name_hash (name);
746 slot = htab_find_slot_with_hash (assembler_name_hash, name,
747 hash, INSERT);
748 if (!*slot)
749 *slot = alias;
755 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
756 decl_assembler_name_hash (asmname),
757 NO_INSERT);
759 if (slot)
761 node = (struct cgraph_node *) *slot;
762 if (node->same_body_alias)
763 node = node->same_body;
764 return node;
766 return NULL;
769 /* Returns a hash value for X (which really is a die_struct). */
771 static hashval_t
772 edge_hash (const void *x)
774 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
777 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
779 static int
780 edge_eq (const void *x, const void *y)
782 return ((const struct cgraph_edge *) x)->call_stmt == y;
785 /* Add call graph edge E to call site hash of its caller. */
787 static inline void
788 cgraph_add_edge_to_call_site_hash (struct cgraph_edge *e)
790 void **slot;
791 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
792 e->call_stmt,
793 htab_hash_pointer (e->call_stmt),
794 INSERT);
795 gcc_assert (!*slot);
796 *slot = e;
799 /* Return the callgraph edge representing the GIMPLE_CALL statement
800 CALL_STMT. */
802 struct cgraph_edge *
803 cgraph_edge (struct cgraph_node *node, gimple call_stmt)
805 struct cgraph_edge *e, *e2;
806 int n = 0;
808 if (node->call_site_hash)
809 return (struct cgraph_edge *)
810 htab_find_with_hash (node->call_site_hash, call_stmt,
811 htab_hash_pointer (call_stmt));
813 /* This loop may turn out to be performance problem. In such case adding
814 hashtables into call nodes with very many edges is probably best
815 solution. It is not good idea to add pointer into CALL_EXPR itself
816 because we want to make possible having multiple cgraph nodes representing
817 different clones of the same body before the body is actually cloned. */
818 for (e = node->callees; e; e = e->next_callee)
820 if (e->call_stmt == call_stmt)
821 break;
822 n++;
825 if (!e)
826 for (e = node->indirect_calls; e; e = e->next_callee)
828 if (e->call_stmt == call_stmt)
829 break;
830 n++;
833 if (n > 100)
835 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
836 for (e2 = node->callees; e2; e2 = e2->next_callee)
837 cgraph_add_edge_to_call_site_hash (e2);
838 for (e2 = node->indirect_calls; e2; e2 = e2->next_callee)
839 cgraph_add_edge_to_call_site_hash (e2);
842 return e;
846 /* Change field call_stmt of edge E to NEW_STMT. */
848 void
849 cgraph_set_call_stmt (struct cgraph_edge *e, gimple new_stmt)
851 tree decl;
853 if (e->caller->call_site_hash)
855 htab_remove_elt_with_hash (e->caller->call_site_hash,
856 e->call_stmt,
857 htab_hash_pointer (e->call_stmt));
860 e->call_stmt = new_stmt;
861 if (e->indirect_unknown_callee
862 && (decl = gimple_call_fndecl (new_stmt)))
864 /* Constant propagation (and possibly also inlining?) can turn an
865 indirect call into a direct one. */
866 struct cgraph_node *new_callee = cgraph_get_node (decl);
868 gcc_checking_assert (new_callee);
869 cgraph_make_edge_direct (e, new_callee, 0);
872 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
873 e->can_throw_external = stmt_can_throw_external (new_stmt);
874 pop_cfun ();
875 if (e->caller->call_site_hash)
876 cgraph_add_edge_to_call_site_hash (e);
879 /* Like cgraph_set_call_stmt but walk the clone tree and update all
880 clones sharing the same function body. */
882 void
883 cgraph_set_call_stmt_including_clones (struct cgraph_node *orig,
884 gimple old_stmt, gimple new_stmt)
886 struct cgraph_node *node;
887 struct cgraph_edge *edge = cgraph_edge (orig, old_stmt);
889 if (edge)
890 cgraph_set_call_stmt (edge, new_stmt);
892 node = orig->clones;
893 if (node)
894 while (node != orig)
896 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
897 if (edge)
898 cgraph_set_call_stmt (edge, new_stmt);
899 if (node->clones)
900 node = node->clones;
901 else if (node->next_sibling_clone)
902 node = node->next_sibling_clone;
903 else
905 while (node != orig && !node->next_sibling_clone)
906 node = node->clone_of;
907 if (node != orig)
908 node = node->next_sibling_clone;
913 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
914 same function body. If clones already have edge for OLD_STMT; only
915 update the edge same way as cgraph_set_call_stmt_including_clones does.
917 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
918 frequencies of the clones. */
920 void
921 cgraph_create_edge_including_clones (struct cgraph_node *orig,
922 struct cgraph_node *callee,
923 gimple old_stmt,
924 gimple stmt, gcov_type count,
925 int freq,
926 cgraph_inline_failed_t reason)
928 struct cgraph_node *node;
929 struct cgraph_edge *edge;
931 if (!cgraph_edge (orig, stmt))
933 edge = cgraph_create_edge (orig, callee, stmt, count, freq);
934 edge->inline_failed = reason;
937 node = orig->clones;
938 if (node)
939 while (node != orig)
941 struct cgraph_edge *edge = cgraph_edge (node, old_stmt);
943 /* It is possible that clones already contain the edge while
944 master didn't. Either we promoted indirect call into direct
945 call in the clone or we are processing clones of unreachable
946 master where edges has been removed. */
947 if (edge)
948 cgraph_set_call_stmt (edge, stmt);
949 else if (!cgraph_edge (node, stmt))
951 edge = cgraph_create_edge (node, callee, stmt, count,
952 freq);
953 edge->inline_failed = reason;
956 if (node->clones)
957 node = node->clones;
958 else if (node->next_sibling_clone)
959 node = node->next_sibling_clone;
960 else
962 while (node != orig && !node->next_sibling_clone)
963 node = node->clone_of;
964 if (node != orig)
965 node = node->next_sibling_clone;
970 /* Allocate a cgraph_edge structure and fill it with data according to the
971 parameters of which only CALLEE can be NULL (when creating an indirect call
972 edge). */
974 static struct cgraph_edge *
975 cgraph_create_edge_1 (struct cgraph_node *caller, struct cgraph_node *callee,
976 gimple call_stmt, gcov_type count, int freq)
978 struct cgraph_edge *edge;
980 /* LTO does not actually have access to the call_stmt since these
981 have not been loaded yet. */
982 if (call_stmt)
984 /* This is a rather expensive check possibly triggering
985 construction of call stmt hashtable. */
986 gcc_checking_assert (!cgraph_edge (caller, call_stmt));
988 gcc_assert (is_gimple_call (call_stmt));
991 if (free_edges)
993 edge = free_edges;
994 free_edges = NEXT_FREE_EDGE (edge);
996 else
998 edge = ggc_alloc_cgraph_edge ();
999 edge->uid = cgraph_edge_max_uid++;
1002 edge->aux = NULL;
1003 edge->caller = caller;
1004 edge->callee = callee;
1005 edge->prev_caller = NULL;
1006 edge->next_caller = NULL;
1007 edge->prev_callee = NULL;
1008 edge->next_callee = NULL;
1010 edge->count = count;
1011 gcc_assert (count >= 0);
1012 edge->frequency = freq;
1013 gcc_assert (freq >= 0);
1014 gcc_assert (freq <= CGRAPH_FREQ_MAX);
1016 edge->call_stmt = call_stmt;
1017 push_cfun (DECL_STRUCT_FUNCTION (caller->decl));
1018 edge->can_throw_external
1019 = call_stmt ? stmt_can_throw_external (call_stmt) : false;
1020 pop_cfun ();
1021 edge->call_stmt_cannot_inline_p =
1022 (call_stmt ? gimple_call_cannot_inline_p (call_stmt) : false);
1023 if (call_stmt && caller->call_site_hash)
1024 cgraph_add_edge_to_call_site_hash (edge);
1026 edge->indirect_info = NULL;
1027 edge->indirect_inlining_edge = 0;
1029 return edge;
1032 /* Create edge from CALLER to CALLEE in the cgraph. */
1034 struct cgraph_edge *
1035 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
1036 gimple call_stmt, gcov_type count, int freq)
1038 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, callee, call_stmt,
1039 count, freq);
1041 edge->indirect_unknown_callee = 0;
1042 initialize_inline_failed (edge);
1044 edge->next_caller = callee->callers;
1045 if (callee->callers)
1046 callee->callers->prev_caller = edge;
1047 edge->next_callee = caller->callees;
1048 if (caller->callees)
1049 caller->callees->prev_callee = edge;
1050 caller->callees = edge;
1051 callee->callers = edge;
1053 return edge;
1056 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1058 struct cgraph_indirect_call_info *
1059 cgraph_allocate_init_indirect_info (void)
1061 struct cgraph_indirect_call_info *ii;
1063 ii = ggc_alloc_cleared_cgraph_indirect_call_info ();
1064 ii->param_index = -1;
1065 return ii;
1068 /* Create an indirect edge with a yet-undetermined callee where the call
1069 statement destination is a formal parameter of the caller with index
1070 PARAM_INDEX. */
1072 struct cgraph_edge *
1073 cgraph_create_indirect_edge (struct cgraph_node *caller, gimple call_stmt,
1074 int ecf_flags,
1075 gcov_type count, int freq)
1077 struct cgraph_edge *edge = cgraph_create_edge_1 (caller, NULL, call_stmt,
1078 count, freq);
1080 edge->indirect_unknown_callee = 1;
1081 initialize_inline_failed (edge);
1083 edge->indirect_info = cgraph_allocate_init_indirect_info ();
1084 edge->indirect_info->ecf_flags = ecf_flags;
1086 edge->next_callee = caller->indirect_calls;
1087 if (caller->indirect_calls)
1088 caller->indirect_calls->prev_callee = edge;
1089 caller->indirect_calls = edge;
1091 return edge;
1094 /* Remove the edge E from the list of the callers of the callee. */
1096 static inline void
1097 cgraph_edge_remove_callee (struct cgraph_edge *e)
1099 gcc_assert (!e->indirect_unknown_callee);
1100 if (e->prev_caller)
1101 e->prev_caller->next_caller = e->next_caller;
1102 if (e->next_caller)
1103 e->next_caller->prev_caller = e->prev_caller;
1104 if (!e->prev_caller)
1105 e->callee->callers = e->next_caller;
1108 /* Remove the edge E from the list of the callees of the caller. */
1110 static inline void
1111 cgraph_edge_remove_caller (struct cgraph_edge *e)
1113 if (e->prev_callee)
1114 e->prev_callee->next_callee = e->next_callee;
1115 if (e->next_callee)
1116 e->next_callee->prev_callee = e->prev_callee;
1117 if (!e->prev_callee)
1119 if (e->indirect_unknown_callee)
1120 e->caller->indirect_calls = e->next_callee;
1121 else
1122 e->caller->callees = e->next_callee;
1124 if (e->caller->call_site_hash)
1125 htab_remove_elt_with_hash (e->caller->call_site_hash,
1126 e->call_stmt,
1127 htab_hash_pointer (e->call_stmt));
1130 /* Put the edge onto the free list. */
1132 static void
1133 cgraph_free_edge (struct cgraph_edge *e)
1135 int uid = e->uid;
1137 /* Clear out the edge so we do not dangle pointers. */
1138 memset (e, 0, sizeof (*e));
1139 e->uid = uid;
1140 NEXT_FREE_EDGE (e) = free_edges;
1141 free_edges = e;
1144 /* Remove the edge E in the cgraph. */
1146 void
1147 cgraph_remove_edge (struct cgraph_edge *e)
1149 /* Call all edge removal hooks. */
1150 cgraph_call_edge_removal_hooks (e);
1152 if (!e->indirect_unknown_callee)
1153 /* Remove from callers list of the callee. */
1154 cgraph_edge_remove_callee (e);
1156 /* Remove from callees list of the callers. */
1157 cgraph_edge_remove_caller (e);
1159 /* Put the edge onto the free list. */
1160 cgraph_free_edge (e);
1163 /* Set callee of call graph edge E and add it to the corresponding set of
1164 callers. */
1166 static void
1167 cgraph_set_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1169 e->prev_caller = NULL;
1170 if (n->callers)
1171 n->callers->prev_caller = e;
1172 e->next_caller = n->callers;
1173 n->callers = e;
1174 e->callee = n;
1177 /* Redirect callee of E to N. The function does not update underlying
1178 call expression. */
1180 void
1181 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
1183 /* Remove from callers list of the current callee. */
1184 cgraph_edge_remove_callee (e);
1186 /* Insert to callers list of the new callee. */
1187 cgraph_set_edge_callee (e, n);
1190 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1191 CALLEE. DELTA is an integer constant that is to be added to the this
1192 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1194 void
1195 cgraph_make_edge_direct (struct cgraph_edge *edge, struct cgraph_node *callee,
1196 HOST_WIDE_INT delta)
1198 edge->indirect_unknown_callee = 0;
1199 edge->indirect_info->thunk_delta = delta;
1201 /* Get the edge out of the indirect edge list. */
1202 if (edge->prev_callee)
1203 edge->prev_callee->next_callee = edge->next_callee;
1204 if (edge->next_callee)
1205 edge->next_callee->prev_callee = edge->prev_callee;
1206 if (!edge->prev_callee)
1207 edge->caller->indirect_calls = edge->next_callee;
1209 /* Put it into the normal callee list */
1210 edge->prev_callee = NULL;
1211 edge->next_callee = edge->caller->callees;
1212 if (edge->caller->callees)
1213 edge->caller->callees->prev_callee = edge;
1214 edge->caller->callees = edge;
1216 /* Insert to callers list of the new callee. */
1217 cgraph_set_edge_callee (edge, callee);
1219 /* We need to re-determine the inlining status of the edge. */
1220 initialize_inline_failed (edge);
1224 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1225 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1226 of OLD_STMT if it was previously call statement. */
1228 static void
1229 cgraph_update_edges_for_call_stmt_node (struct cgraph_node *node,
1230 gimple old_stmt, tree old_call, gimple new_stmt)
1232 tree new_call = (is_gimple_call (new_stmt)) ? gimple_call_fndecl (new_stmt) : 0;
1234 /* We are seeing indirect calls, then there is nothing to update. */
1235 if (!new_call && !old_call)
1236 return;
1237 /* See if we turned indirect call into direct call or folded call to one builtin
1238 into different builtin. */
1239 if (old_call != new_call)
1241 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
1242 struct cgraph_edge *ne = NULL;
1243 gcov_type count;
1244 int frequency;
1246 if (e)
1248 /* See if the edge is already there and has the correct callee. It
1249 might be so because of indirect inlining has already updated
1250 it. We also might've cloned and redirected the edge. */
1251 if (new_call && e->callee)
1253 struct cgraph_node *callee = e->callee;
1254 while (callee)
1256 if (callee->decl == new_call
1257 || callee->former_clone_of == new_call)
1258 return;
1259 callee = callee->clone_of;
1263 /* Otherwise remove edge and create new one; we can't simply redirect
1264 since function has changed, so inline plan and other information
1265 attached to edge is invalid. */
1266 count = e->count;
1267 frequency = e->frequency;
1268 cgraph_remove_edge (e);
1270 else
1272 /* We are seeing new direct call; compute profile info based on BB. */
1273 basic_block bb = gimple_bb (new_stmt);
1274 count = bb->count;
1275 frequency = compute_call_stmt_bb_frequency (current_function_decl,
1276 bb);
1279 if (new_call)
1281 ne = cgraph_create_edge (node, cgraph_get_create_node (new_call),
1282 new_stmt, count, frequency);
1283 gcc_assert (ne->inline_failed);
1286 /* We only updated the call stmt; update pointer in cgraph edge.. */
1287 else if (old_stmt != new_stmt)
1288 cgraph_set_call_stmt (cgraph_edge (node, old_stmt), new_stmt);
1291 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1292 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1293 of OLD_STMT before it was updated (updating can happen inplace). */
1295 void
1296 cgraph_update_edges_for_call_stmt (gimple old_stmt, tree old_decl, gimple new_stmt)
1298 struct cgraph_node *orig = cgraph_get_node (cfun->decl);
1299 struct cgraph_node *node;
1301 gcc_checking_assert (orig);
1302 cgraph_update_edges_for_call_stmt_node (orig, old_stmt, old_decl, new_stmt);
1303 if (orig->clones)
1304 for (node = orig->clones; node != orig;)
1306 cgraph_update_edges_for_call_stmt_node (node, old_stmt, old_decl, new_stmt);
1307 if (node->clones)
1308 node = node->clones;
1309 else if (node->next_sibling_clone)
1310 node = node->next_sibling_clone;
1311 else
1313 while (node != orig && !node->next_sibling_clone)
1314 node = node->clone_of;
1315 if (node != orig)
1316 node = node->next_sibling_clone;
1322 /* Remove all callees from the node. */
1324 void
1325 cgraph_node_remove_callees (struct cgraph_node *node)
1327 struct cgraph_edge *e, *f;
1329 /* It is sufficient to remove the edges from the lists of callers of
1330 the callees. The callee list of the node can be zapped with one
1331 assignment. */
1332 for (e = node->callees; e; e = f)
1334 f = e->next_callee;
1335 cgraph_call_edge_removal_hooks (e);
1336 if (!e->indirect_unknown_callee)
1337 cgraph_edge_remove_callee (e);
1338 cgraph_free_edge (e);
1340 for (e = node->indirect_calls; e; e = f)
1342 f = e->next_callee;
1343 cgraph_call_edge_removal_hooks (e);
1344 if (!e->indirect_unknown_callee)
1345 cgraph_edge_remove_callee (e);
1346 cgraph_free_edge (e);
1348 node->indirect_calls = NULL;
1349 node->callees = NULL;
1350 if (node->call_site_hash)
1352 htab_delete (node->call_site_hash);
1353 node->call_site_hash = NULL;
1357 /* Remove all callers from the node. */
1359 static void
1360 cgraph_node_remove_callers (struct cgraph_node *node)
1362 struct cgraph_edge *e, *f;
1364 /* It is sufficient to remove the edges from the lists of callees of
1365 the callers. The caller list of the node can be zapped with one
1366 assignment. */
1367 for (e = node->callers; e; e = f)
1369 f = e->next_caller;
1370 cgraph_call_edge_removal_hooks (e);
1371 cgraph_edge_remove_caller (e);
1372 cgraph_free_edge (e);
1374 node->callers = NULL;
1377 /* Release memory used to represent body of function NODE. */
1379 void
1380 cgraph_release_function_body (struct cgraph_node *node)
1382 if (DECL_STRUCT_FUNCTION (node->decl))
1384 tree old_decl = current_function_decl;
1385 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1386 if (cfun->gimple_df)
1388 current_function_decl = node->decl;
1389 delete_tree_ssa ();
1390 delete_tree_cfg_annotations ();
1391 cfun->eh = NULL;
1392 current_function_decl = old_decl;
1394 if (cfun->cfg)
1396 gcc_assert (dom_computed[0] == DOM_NONE);
1397 gcc_assert (dom_computed[1] == DOM_NONE);
1398 clear_edges ();
1400 if (cfun->value_histograms)
1401 free_histograms ();
1402 gcc_assert (!current_loops);
1403 pop_cfun();
1404 gimple_set_body (node->decl, NULL);
1405 VEC_free (ipa_opt_pass, heap,
1406 node->ipa_transforms_to_apply);
1407 /* Struct function hangs a lot of data that would leak if we didn't
1408 removed all pointers to it. */
1409 ggc_free (DECL_STRUCT_FUNCTION (node->decl));
1410 DECL_STRUCT_FUNCTION (node->decl) = NULL;
1412 DECL_SAVED_TREE (node->decl) = NULL;
1413 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1414 of its associated function function declaration because it's
1415 needed to emit debug info later. */
1416 if (!node->abstract_and_needed)
1417 DECL_INITIAL (node->decl) = error_mark_node;
1420 /* Remove same body alias node. */
1422 void
1423 cgraph_remove_same_body_alias (struct cgraph_node *node)
1425 void **slot;
1426 int uid = node->uid;
1428 gcc_assert (node->same_body_alias);
1429 if (node->previous)
1430 node->previous->next = node->next;
1431 else
1432 node->same_body->same_body = node->next;
1433 if (node->next)
1434 node->next->previous = node->previous;
1435 node->next = NULL;
1436 node->previous = NULL;
1437 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1438 if (*slot == node)
1439 htab_clear_slot (cgraph_hash, slot);
1440 if (assembler_name_hash)
1442 tree name = DECL_ASSEMBLER_NAME (node->decl);
1443 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1444 decl_assembler_name_hash (name),
1445 NO_INSERT);
1446 if (slot && *slot == node)
1447 htab_clear_slot (assembler_name_hash, slot);
1450 /* Clear out the node to NULL all pointers and add the node to the free
1451 list. */
1452 memset (node, 0, sizeof(*node));
1453 node->uid = uid;
1454 NEXT_FREE_NODE (node) = free_nodes;
1455 free_nodes = node;
1458 /* Remove the node from cgraph. */
1460 void
1461 cgraph_remove_node (struct cgraph_node *node)
1463 void **slot;
1464 bool kill_body = false;
1465 struct cgraph_node *n;
1466 int uid = node->uid;
1468 cgraph_call_node_removal_hooks (node);
1469 cgraph_node_remove_callers (node);
1470 cgraph_node_remove_callees (node);
1471 ipa_remove_all_references (&node->ref_list);
1472 ipa_remove_all_refering (&node->ref_list);
1473 VEC_free (ipa_opt_pass, heap,
1474 node->ipa_transforms_to_apply);
1476 /* Incremental inlining access removed nodes stored in the postorder list.
1478 node->needed = node->reachable = false;
1479 for (n = node->nested; n; n = n->next_nested)
1480 n->origin = NULL;
1481 node->nested = NULL;
1482 if (node->origin)
1484 struct cgraph_node **node2 = &node->origin->nested;
1486 while (*node2 != node)
1487 node2 = &(*node2)->next_nested;
1488 *node2 = node->next_nested;
1490 if (node->previous)
1491 node->previous->next = node->next;
1492 else
1493 cgraph_nodes = node->next;
1494 if (node->next)
1495 node->next->previous = node->previous;
1496 node->next = NULL;
1497 node->previous = NULL;
1498 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
1499 if (*slot == node)
1501 struct cgraph_node *next_inline_clone;
1503 for (next_inline_clone = node->clones;
1504 next_inline_clone && next_inline_clone->decl != node->decl;
1505 next_inline_clone = next_inline_clone->next_sibling_clone)
1508 /* If there is inline clone of the node being removed, we need
1509 to put it into the position of removed node and reorganize all
1510 other clones to be based on it. */
1511 if (next_inline_clone)
1513 struct cgraph_node *n;
1514 struct cgraph_node *new_clones;
1516 *slot = next_inline_clone;
1518 /* Unlink inline clone from the list of clones of removed node. */
1519 if (next_inline_clone->next_sibling_clone)
1520 next_inline_clone->next_sibling_clone->prev_sibling_clone
1521 = next_inline_clone->prev_sibling_clone;
1522 if (next_inline_clone->prev_sibling_clone)
1524 gcc_assert (node->clones != next_inline_clone);
1525 next_inline_clone->prev_sibling_clone->next_sibling_clone
1526 = next_inline_clone->next_sibling_clone;
1528 else
1530 gcc_assert (node->clones == next_inline_clone);
1531 node->clones = next_inline_clone->next_sibling_clone;
1534 new_clones = node->clones;
1535 node->clones = NULL;
1537 /* Copy clone info. */
1538 next_inline_clone->clone = node->clone;
1540 /* Now place it into clone tree at same level at NODE. */
1541 next_inline_clone->clone_of = node->clone_of;
1542 next_inline_clone->prev_sibling_clone = NULL;
1543 next_inline_clone->next_sibling_clone = NULL;
1544 if (node->clone_of)
1546 if (node->clone_of->clones)
1547 node->clone_of->clones->prev_sibling_clone = next_inline_clone;
1548 next_inline_clone->next_sibling_clone = node->clone_of->clones;
1549 node->clone_of->clones = next_inline_clone;
1552 /* Merge the clone list. */
1553 if (new_clones)
1555 if (!next_inline_clone->clones)
1556 next_inline_clone->clones = new_clones;
1557 else
1559 n = next_inline_clone->clones;
1560 while (n->next_sibling_clone)
1561 n = n->next_sibling_clone;
1562 n->next_sibling_clone = new_clones;
1563 new_clones->prev_sibling_clone = n;
1567 /* Update clone_of pointers. */
1568 n = new_clones;
1569 while (n)
1571 n->clone_of = next_inline_clone;
1572 n = n->next_sibling_clone;
1575 else
1577 htab_clear_slot (cgraph_hash, slot);
1578 kill_body = true;
1582 if (node->prev_sibling_clone)
1583 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1584 else if (node->clone_of)
1585 node->clone_of->clones = node->next_sibling_clone;
1586 if (node->next_sibling_clone)
1587 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1588 if (node->clones)
1590 struct cgraph_node *n, *next;
1592 if (node->clone_of)
1594 for (n = node->clones; n->next_sibling_clone; n = n->next_sibling_clone)
1595 n->clone_of = node->clone_of;
1596 n->clone_of = node->clone_of;
1597 n->next_sibling_clone = node->clone_of->clones;
1598 if (node->clone_of->clones)
1599 node->clone_of->clones->prev_sibling_clone = n;
1600 node->clone_of->clones = node->clones;
1602 else
1604 /* We are removing node with clones. this makes clones inconsistent,
1605 but assume they will be removed subsequently and just keep clone
1606 tree intact. This can happen in unreachable function removal since
1607 we remove unreachable functions in random order, not by bottom-up
1608 walk of clone trees. */
1609 for (n = node->clones; n; n = next)
1611 next = n->next_sibling_clone;
1612 n->next_sibling_clone = NULL;
1613 n->prev_sibling_clone = NULL;
1614 n->clone_of = NULL;
1619 while (node->same_body)
1620 cgraph_remove_same_body_alias (node->same_body);
1622 if (node->same_comdat_group)
1624 struct cgraph_node *prev;
1625 for (prev = node->same_comdat_group;
1626 prev->same_comdat_group != node;
1627 prev = prev->same_comdat_group)
1629 if (node->same_comdat_group == prev)
1630 prev->same_comdat_group = NULL;
1631 else
1632 prev->same_comdat_group = node->same_comdat_group;
1633 node->same_comdat_group = NULL;
1636 /* While all the clones are removed after being proceeded, the function
1637 itself is kept in the cgraph even after it is compiled. Check whether
1638 we are done with this body and reclaim it proactively if this is the case.
1640 if (!kill_body && *slot)
1642 struct cgraph_node *n = (struct cgraph_node *) *slot;
1643 if (!n->clones && !n->clone_of && !n->global.inlined_to
1644 && (cgraph_global_info_ready
1645 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)
1646 || n->in_other_partition)))
1647 kill_body = true;
1649 if (assembler_name_hash)
1651 tree name = DECL_ASSEMBLER_NAME (node->decl);
1652 slot = htab_find_slot_with_hash (assembler_name_hash, name,
1653 decl_assembler_name_hash (name),
1654 NO_INSERT);
1655 /* Inline clones are not hashed. */
1656 if (slot && *slot == node)
1657 htab_clear_slot (assembler_name_hash, slot);
1660 if (kill_body)
1661 cgraph_release_function_body (node);
1662 node->decl = NULL;
1663 if (node->call_site_hash)
1665 htab_delete (node->call_site_hash);
1666 node->call_site_hash = NULL;
1668 cgraph_n_nodes--;
1670 /* Clear out the node to NULL all pointers and add the node to the free
1671 list. */
1672 memset (node, 0, sizeof(*node));
1673 node->uid = uid;
1674 NEXT_FREE_NODE (node) = free_nodes;
1675 free_nodes = node;
1678 /* Remove the node from cgraph. */
1680 void
1681 cgraph_remove_node_and_inline_clones (struct cgraph_node *node)
1683 struct cgraph_edge *e, *next;
1684 for (e = node->callees; e; e = next)
1686 next = e->next_callee;
1687 if (!e->inline_failed)
1688 cgraph_remove_node_and_inline_clones (e->callee);
1690 cgraph_remove_node (node);
1693 /* Notify finalize_compilation_unit that given node is reachable. */
1695 void
1696 cgraph_mark_reachable_node (struct cgraph_node *node)
1698 if (!node->reachable && node->local.finalized)
1700 if (cgraph_global_info_ready)
1702 /* Verify that function does not appear to be needed out of blue
1703 during the optimization process. This can happen for extern
1704 inlines when bodies was removed after inlining. */
1705 gcc_assert ((node->analyzed || node->in_other_partition
1706 || DECL_EXTERNAL (node->decl)));
1708 else
1709 notice_global_symbol (node->decl);
1710 node->reachable = 1;
1712 node->next_needed = cgraph_nodes_queue;
1713 cgraph_nodes_queue = node;
1717 /* Likewise indicate that a node is needed, i.e. reachable via some
1718 external means. */
1720 void
1721 cgraph_mark_needed_node (struct cgraph_node *node)
1723 node->needed = 1;
1724 gcc_assert (!node->global.inlined_to);
1725 cgraph_mark_reachable_node (node);
1728 /* Likewise indicate that a node is having address taken. */
1730 void
1731 cgraph_mark_address_taken_node (struct cgraph_node *node)
1733 gcc_assert (!node->global.inlined_to);
1734 cgraph_mark_reachable_node (node);
1735 node->address_taken = 1;
1738 /* Return local info for the compiled function. */
1740 struct cgraph_local_info *
1741 cgraph_local_info (tree decl)
1743 struct cgraph_node *node;
1745 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1746 node = cgraph_get_node (decl);
1747 if (!node)
1748 return NULL;
1749 return &node->local;
1752 /* Return local info for the compiled function. */
1754 struct cgraph_global_info *
1755 cgraph_global_info (tree decl)
1757 struct cgraph_node *node;
1759 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
1760 node = cgraph_get_node (decl);
1761 if (!node)
1762 return NULL;
1763 return &node->global;
1766 /* Return local info for the compiled function. */
1768 struct cgraph_rtl_info *
1769 cgraph_rtl_info (tree decl)
1771 struct cgraph_node *node;
1773 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1774 node = cgraph_get_node (decl);
1775 if (!node
1776 || (decl != current_function_decl
1777 && !TREE_ASM_WRITTEN (node->decl)))
1778 return NULL;
1779 return &node->rtl;
1782 /* Return a string describing the failure REASON. */
1784 const char*
1785 cgraph_inline_failed_string (cgraph_inline_failed_t reason)
1787 #undef DEFCIFCODE
1788 #define DEFCIFCODE(code, string) string,
1790 static const char *cif_string_table[CIF_N_REASONS] = {
1791 #include "cif-code.def"
1794 /* Signedness of an enum type is implementation defined, so cast it
1795 to unsigned before testing. */
1796 gcc_assert ((unsigned) reason < CIF_N_REASONS);
1797 return cif_string_table[reason];
1800 /* Return name of the node used in debug output. */
1801 const char *
1802 cgraph_node_name (struct cgraph_node *node)
1804 return lang_hooks.decl_printable_name (node->decl, 2);
1807 /* Names used to print out the availability enum. */
1808 const char * const cgraph_availability_names[] =
1809 {"unset", "not_available", "overwritable", "available", "local"};
1812 /* Dump call graph node NODE to file F. */
1814 void
1815 dump_cgraph_node (FILE *f, struct cgraph_node *node)
1817 struct cgraph_edge *edge;
1818 int indirect_calls_count = 0;
1820 fprintf (f, "%s/%i", cgraph_node_name (node), node->uid);
1821 dump_addr (f, " @", (void *)node);
1822 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
1823 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1824 if (node->global.inlined_to)
1825 fprintf (f, " (inline copy in %s/%i)",
1826 cgraph_node_name (node->global.inlined_to),
1827 node->global.inlined_to->uid);
1828 if (node->same_comdat_group)
1829 fprintf (f, " (same comdat group as %s/%i)",
1830 cgraph_node_name (node->same_comdat_group),
1831 node->same_comdat_group->uid);
1832 if (node->clone_of)
1833 fprintf (f, " (clone of %s/%i)",
1834 cgraph_node_name (node->clone_of),
1835 node->clone_of->uid);
1836 if (cgraph_function_flags_ready)
1837 fprintf (f, " availability:%s",
1838 cgraph_availability_names [cgraph_function_body_availability (node)]);
1839 if (node->analyzed)
1840 fprintf (f, " analyzed");
1841 if (node->in_other_partition)
1842 fprintf (f, " in_other_partition");
1843 if (node->count)
1844 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
1845 (HOST_WIDEST_INT)node->count);
1846 if (node->origin)
1847 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
1848 if (node->needed)
1849 fprintf (f, " needed");
1850 if (node->address_taken)
1851 fprintf (f, " address_taken");
1852 else if (node->reachable)
1853 fprintf (f, " reachable");
1854 else if (node->reachable_from_other_partition)
1855 fprintf (f, " reachable_from_other_partition");
1856 if (gimple_has_body_p (node->decl))
1857 fprintf (f, " body");
1858 if (node->process)
1859 fprintf (f, " process");
1860 if (node->local.local)
1861 fprintf (f, " local");
1862 if (node->local.externally_visible)
1863 fprintf (f, " externally_visible");
1864 if (node->resolution != LDPR_UNKNOWN)
1865 fprintf (f, " %s",
1866 ld_plugin_symbol_resolution_names[(int)node->resolution]);
1867 if (node->local.finalized)
1868 fprintf (f, " finalized");
1869 if (node->local.redefined_extern_inline)
1870 fprintf (f, " redefined_extern_inline");
1871 if (TREE_ASM_WRITTEN (node->decl))
1872 fprintf (f, " asm_written");
1873 if (node->only_called_at_startup)
1874 fprintf (f, " only_called_at_startup");
1875 if (node->only_called_at_exit)
1876 fprintf (f, " only_called_at_exit");
1878 fprintf (f, "\n called by: ");
1879 for (edge = node->callers; edge; edge = edge->next_caller)
1881 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
1882 edge->caller->uid);
1883 if (edge->count)
1884 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1885 (HOST_WIDEST_INT)edge->count);
1886 if (edge->frequency)
1887 fprintf (f, "(%.2f per call) ",
1888 edge->frequency / (double)CGRAPH_FREQ_BASE);
1889 if (!edge->inline_failed)
1890 fprintf(f, "(inlined) ");
1891 if (edge->indirect_inlining_edge)
1892 fprintf(f, "(indirect_inlining) ");
1893 if (edge->can_throw_external)
1894 fprintf(f, "(can throw external) ");
1897 fprintf (f, "\n calls: ");
1898 for (edge = node->callees; edge; edge = edge->next_callee)
1900 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
1901 edge->callee->uid);
1902 if (!edge->inline_failed)
1903 fprintf(f, "(inlined) ");
1904 if (edge->indirect_inlining_edge)
1905 fprintf(f, "(indirect_inlining) ");
1906 if (edge->count)
1907 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
1908 (HOST_WIDEST_INT)edge->count);
1909 if (edge->frequency)
1910 fprintf (f, "(%.2f per call) ",
1911 edge->frequency / (double)CGRAPH_FREQ_BASE);
1912 if (edge->can_throw_external)
1913 fprintf(f, "(can throw external) ");
1915 fprintf (f, "\n");
1916 fprintf (f, " References: ");
1917 ipa_dump_references (f, &node->ref_list);
1918 fprintf (f, " Refering this function: ");
1919 ipa_dump_refering (f, &node->ref_list);
1921 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1922 indirect_calls_count++;
1923 if (indirect_calls_count)
1924 fprintf (f, " has %i outgoing edges for indirect calls.\n",
1925 indirect_calls_count);
1927 if (node->same_body)
1929 struct cgraph_node *n;
1930 fprintf (f, " aliases & thunks:");
1931 for (n = node->same_body; n; n = n->next)
1933 fprintf (f, " %s/%i", cgraph_node_name (n), n->uid);
1934 if (n->thunk.thunk_p)
1936 fprintf (f, " (thunk of %s fixed offset %i virtual value %i has "
1937 "virtual offset %i",
1938 lang_hooks.decl_printable_name (n->thunk.alias, 2),
1939 (int)n->thunk.fixed_offset,
1940 (int)n->thunk.virtual_value,
1941 (int)n->thunk.virtual_offset_p);
1942 fprintf (f, ")");
1944 if (DECL_ASSEMBLER_NAME_SET_P (n->decl))
1945 fprintf (f, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1947 fprintf (f, "\n");
1952 /* Dump call graph node NODE to stderr. */
1954 DEBUG_FUNCTION void
1955 debug_cgraph_node (struct cgraph_node *node)
1957 dump_cgraph_node (stderr, node);
1961 /* Dump the callgraph to file F. */
1963 void
1964 dump_cgraph (FILE *f)
1966 struct cgraph_node *node;
1968 fprintf (f, "callgraph:\n\n");
1969 for (node = cgraph_nodes; node; node = node->next)
1970 dump_cgraph_node (f, node);
1974 /* Dump the call graph to stderr. */
1976 DEBUG_FUNCTION void
1977 debug_cgraph (void)
1979 dump_cgraph (stderr);
1983 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
1985 void
1986 change_decl_assembler_name (tree decl, tree name)
1988 struct cgraph_node *node;
1989 void **slot;
1990 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1991 SET_DECL_ASSEMBLER_NAME (decl, name);
1992 else
1994 if (name == DECL_ASSEMBLER_NAME (decl))
1995 return;
1997 if (assembler_name_hash
1998 && TREE_CODE (decl) == FUNCTION_DECL
1999 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2001 tree old_name = DECL_ASSEMBLER_NAME (decl);
2002 slot = htab_find_slot_with_hash (assembler_name_hash, old_name,
2003 decl_assembler_name_hash (old_name),
2004 NO_INSERT);
2005 /* Inline clones are not hashed. */
2006 if (slot && *slot == node)
2007 htab_clear_slot (assembler_name_hash, slot);
2009 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
2010 && DECL_RTL_SET_P (decl))
2011 warning (0, "%D renamed after being referenced in assembly", decl);
2013 SET_DECL_ASSEMBLER_NAME (decl, name);
2015 if (assembler_name_hash
2016 && TREE_CODE (decl) == FUNCTION_DECL
2017 && (node = cgraph_get_node_or_alias (decl)) != NULL)
2019 slot = htab_find_slot_with_hash (assembler_name_hash, name,
2020 decl_assembler_name_hash (name),
2021 INSERT);
2022 gcc_assert (!*slot);
2023 *slot = node;
2027 /* Add a top-level asm statement to the list. */
2029 struct cgraph_asm_node *
2030 cgraph_add_asm_node (tree asm_str)
2032 struct cgraph_asm_node *node;
2034 node = ggc_alloc_cleared_cgraph_asm_node ();
2035 node->asm_str = asm_str;
2036 node->order = cgraph_order++;
2037 node->next = NULL;
2038 if (cgraph_asm_nodes == NULL)
2039 cgraph_asm_nodes = node;
2040 else
2041 cgraph_asm_last_node->next = node;
2042 cgraph_asm_last_node = node;
2043 return node;
2046 /* Return true when the DECL can possibly be inlined. */
2047 bool
2048 cgraph_function_possibly_inlined_p (tree decl)
2050 if (!cgraph_global_info_ready)
2051 return !DECL_UNINLINABLE (decl);
2052 return DECL_POSSIBLY_INLINED (decl);
2055 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2056 struct cgraph_edge *
2057 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
2058 gimple call_stmt, unsigned stmt_uid, gcov_type count_scale,
2059 int freq_scale, bool update_original)
2061 struct cgraph_edge *new_edge;
2062 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
2063 gcov_type freq;
2065 /* We do not want to ignore loop nest after frequency drops to 0. */
2066 if (!freq_scale)
2067 freq_scale = 1;
2068 freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
2069 if (freq > CGRAPH_FREQ_MAX)
2070 freq = CGRAPH_FREQ_MAX;
2072 if (e->indirect_unknown_callee)
2074 tree decl;
2076 if (call_stmt && (decl = gimple_call_fndecl (call_stmt)))
2078 struct cgraph_node *callee = cgraph_get_node (decl);
2079 gcc_checking_assert (callee);
2080 new_edge = cgraph_create_edge (n, callee, call_stmt, count, freq);
2082 else
2084 new_edge = cgraph_create_indirect_edge (n, call_stmt,
2085 e->indirect_info->ecf_flags,
2086 count, freq);
2087 *new_edge->indirect_info = *e->indirect_info;
2090 else
2092 new_edge = cgraph_create_edge (n, e->callee, call_stmt, count, freq);
2093 if (e->indirect_info)
2095 new_edge->indirect_info
2096 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2097 *new_edge->indirect_info = *e->indirect_info;
2101 new_edge->inline_failed = e->inline_failed;
2102 new_edge->indirect_inlining_edge = e->indirect_inlining_edge;
2103 new_edge->lto_stmt_uid = stmt_uid;
2104 /* Clone flags that depend on call_stmt availability manually. */
2105 new_edge->can_throw_external = e->can_throw_external;
2106 new_edge->call_stmt_cannot_inline_p = e->call_stmt_cannot_inline_p;
2107 if (update_original)
2109 e->count -= new_edge->count;
2110 if (e->count < 0)
2111 e->count = 0;
2113 cgraph_call_edge_duplication_hooks (e, new_edge);
2114 return new_edge;
2117 /* Create node representing clone of N executed COUNT times. Decrease
2118 the execution counts from original node too.
2119 The new clone will have decl set to DECL that may or may not be the same
2120 as decl of N.
2122 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2123 function's profile to reflect the fact that part of execution is handled
2124 by node. */
2125 struct cgraph_node *
2126 cgraph_clone_node (struct cgraph_node *n, tree decl, gcov_type count, int freq,
2127 bool update_original,
2128 VEC(cgraph_edge_p,heap) *redirect_callers)
2130 struct cgraph_node *new_node = cgraph_create_node_1 ();
2131 struct cgraph_edge *e;
2132 gcov_type count_scale;
2133 unsigned i;
2135 new_node->decl = decl;
2136 new_node->origin = n->origin;
2137 if (new_node->origin)
2139 new_node->next_nested = new_node->origin->nested;
2140 new_node->origin->nested = new_node;
2142 new_node->analyzed = n->analyzed;
2143 new_node->local = n->local;
2144 new_node->local.externally_visible = false;
2145 new_node->local.local = true;
2146 new_node->global = n->global;
2147 new_node->rtl = n->rtl;
2148 new_node->count = count;
2149 new_node->frequency = n->frequency;
2150 new_node->clone = n->clone;
2151 new_node->clone.tree_map = 0;
2152 if (n->count)
2154 if (new_node->count > n->count)
2155 count_scale = REG_BR_PROB_BASE;
2156 else
2157 count_scale = new_node->count * REG_BR_PROB_BASE / n->count;
2159 else
2160 count_scale = 0;
2161 if (update_original)
2163 n->count -= count;
2164 if (n->count < 0)
2165 n->count = 0;
2168 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2170 /* Redirect calls to the old version node to point to its new
2171 version. */
2172 cgraph_redirect_edge_callee (e, new_node);
2176 for (e = n->callees;e; e=e->next_callee)
2177 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2178 count_scale, freq, update_original);
2180 for (e = n->indirect_calls; e; e = e->next_callee)
2181 cgraph_clone_edge (e, new_node, e->call_stmt, e->lto_stmt_uid,
2182 count_scale, freq, update_original);
2183 ipa_clone_references (new_node, NULL, &n->ref_list);
2185 new_node->next_sibling_clone = n->clones;
2186 if (n->clones)
2187 n->clones->prev_sibling_clone = new_node;
2188 n->clones = new_node;
2189 new_node->clone_of = n;
2191 cgraph_call_node_duplication_hooks (n, new_node);
2192 if (n->decl != decl)
2194 struct cgraph_node **slot;
2195 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, new_node, INSERT);
2196 gcc_assert (!*slot);
2197 *slot = new_node;
2198 if (assembler_name_hash)
2200 void **aslot;
2201 tree name = DECL_ASSEMBLER_NAME (decl);
2203 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
2204 decl_assembler_name_hash (name),
2205 INSERT);
2206 gcc_assert (!*aslot);
2207 *aslot = new_node;
2210 return new_node;
2213 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2215 static GTY(()) unsigned int clone_fn_id_num;
2217 tree
2218 clone_function_name (tree decl, const char *suffix)
2220 tree name = DECL_ASSEMBLER_NAME (decl);
2221 size_t len = IDENTIFIER_LENGTH (name);
2222 char *tmp_name, *prefix;
2224 prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
2225 memcpy (prefix, IDENTIFIER_POINTER (name), len);
2226 strcpy (prefix + len + 1, suffix);
2227 #ifndef NO_DOT_IN_LABEL
2228 prefix[len] = '.';
2229 #elif !defined NO_DOLLAR_IN_LABEL
2230 prefix[len] = '$';
2231 #else
2232 prefix[len] = '_';
2233 #endif
2234 ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
2235 return get_identifier (tmp_name);
2238 /* Create callgraph node clone with new declaration. The actual body will
2239 be copied later at compilation stage.
2241 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2242 bitmap interface.
2244 struct cgraph_node *
2245 cgraph_create_virtual_clone (struct cgraph_node *old_node,
2246 VEC(cgraph_edge_p,heap) *redirect_callers,
2247 VEC(ipa_replace_map_p,gc) *tree_map,
2248 bitmap args_to_skip,
2249 const char * suffix)
2251 tree old_decl = old_node->decl;
2252 struct cgraph_node *new_node = NULL;
2253 tree new_decl;
2254 size_t i;
2255 struct ipa_replace_map *map;
2257 if (!flag_wpa)
2258 gcc_checking_assert (tree_versionable_function_p (old_decl));
2260 gcc_assert (old_node->local.can_change_signature || !args_to_skip);
2262 /* Make a new FUNCTION_DECL tree node */
2263 if (!args_to_skip)
2264 new_decl = copy_node (old_decl);
2265 else
2266 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2267 DECL_STRUCT_FUNCTION (new_decl) = NULL;
2269 /* Generate a new name for the new version. */
2270 DECL_NAME (new_decl) = clone_function_name (old_decl, suffix);
2271 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2272 SET_DECL_RTL (new_decl, NULL);
2274 new_node = cgraph_clone_node (old_node, new_decl, old_node->count,
2275 CGRAPH_FREQ_BASE, false,
2276 redirect_callers);
2277 /* Update the properties.
2278 Make clone visible only within this translation unit. Make sure
2279 that is not weak also.
2280 ??? We cannot use COMDAT linkage because there is no
2281 ABI support for this. */
2282 DECL_EXTERNAL (new_node->decl) = 0;
2283 if (DECL_ONE_ONLY (old_decl))
2284 DECL_SECTION_NAME (new_node->decl) = NULL;
2285 DECL_COMDAT_GROUP (new_node->decl) = 0;
2286 TREE_PUBLIC (new_node->decl) = 0;
2287 DECL_COMDAT (new_node->decl) = 0;
2288 DECL_WEAK (new_node->decl) = 0;
2289 DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
2290 DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
2291 new_node->clone.tree_map = tree_map;
2292 new_node->clone.args_to_skip = args_to_skip;
2293 FOR_EACH_VEC_ELT (ipa_replace_map_p, tree_map, i, map)
2295 tree var = map->new_tree;
2297 STRIP_NOPS (var);
2298 if (TREE_CODE (var) != ADDR_EXPR)
2299 continue;
2300 var = get_base_var (var);
2301 if (!var)
2302 continue;
2304 /* Record references of the future statement initializing the constant
2305 argument. */
2306 if (TREE_CODE (var) == FUNCTION_DECL)
2308 struct cgraph_node *ref_node = cgraph_get_node (var);
2309 gcc_checking_assert (ref_node);
2310 ipa_record_reference (new_node, NULL, ref_node, NULL, IPA_REF_ADDR,
2311 NULL);
2313 else if (TREE_CODE (var) == VAR_DECL)
2314 ipa_record_reference (new_node, NULL, NULL, varpool_node (var),
2315 IPA_REF_ADDR, NULL);
2317 if (!args_to_skip)
2318 new_node->clone.combined_args_to_skip = old_node->clone.combined_args_to_skip;
2319 else if (old_node->clone.combined_args_to_skip)
2321 int newi = 0, oldi = 0;
2322 tree arg;
2323 bitmap new_args_to_skip = BITMAP_GGC_ALLOC ();
2324 struct cgraph_node *orig_node;
2325 for (orig_node = old_node; orig_node->clone_of; orig_node = orig_node->clone_of)
2327 for (arg = DECL_ARGUMENTS (orig_node->decl); arg; arg = DECL_CHAIN (arg), oldi++)
2329 if (bitmap_bit_p (old_node->clone.combined_args_to_skip, oldi))
2331 bitmap_set_bit (new_args_to_skip, oldi);
2332 continue;
2334 if (bitmap_bit_p (args_to_skip, newi))
2335 bitmap_set_bit (new_args_to_skip, oldi);
2336 newi++;
2338 new_node->clone.combined_args_to_skip = new_args_to_skip;
2340 else
2341 new_node->clone.combined_args_to_skip = args_to_skip;
2342 new_node->local.externally_visible = 0;
2343 new_node->local.local = 1;
2344 new_node->lowered = true;
2345 new_node->reachable = true;
2348 return new_node;
2351 /* NODE is no longer nested function; update cgraph accordingly. */
2352 void
2353 cgraph_unnest_node (struct cgraph_node *node)
2355 struct cgraph_node **node2 = &node->origin->nested;
2356 gcc_assert (node->origin);
2358 while (*node2 != node)
2359 node2 = &(*node2)->next_nested;
2360 *node2 = node->next_nested;
2361 node->origin = NULL;
2364 /* Return function availability. See cgraph.h for description of individual
2365 return values. */
2366 enum availability
2367 cgraph_function_body_availability (struct cgraph_node *node)
2369 enum availability avail;
2370 gcc_assert (cgraph_function_flags_ready);
2371 if (!node->analyzed)
2372 avail = AVAIL_NOT_AVAILABLE;
2373 else if (node->local.local)
2374 avail = AVAIL_LOCAL;
2375 else if (!node->local.externally_visible)
2376 avail = AVAIL_AVAILABLE;
2377 /* Inline functions are safe to be analyzed even if their symbol can
2378 be overwritten at runtime. It is not meaningful to enforce any sane
2379 behaviour on replacing inline function by different body. */
2380 else if (DECL_DECLARED_INLINE_P (node->decl))
2381 avail = AVAIL_AVAILABLE;
2383 /* If the function can be overwritten, return OVERWRITABLE. Take
2384 care at least of two notable extensions - the COMDAT functions
2385 used to share template instantiations in C++ (this is symmetric
2386 to code cp_cannot_inline_tree_fn and probably shall be shared and
2387 the inlinability hooks completely eliminated).
2389 ??? Does the C++ one definition rule allow us to always return
2390 AVAIL_AVAILABLE here? That would be good reason to preserve this
2391 bit. */
2393 else if (decl_replaceable_p (node->decl) && !DECL_EXTERNAL (node->decl))
2394 avail = AVAIL_OVERWRITABLE;
2395 else avail = AVAIL_AVAILABLE;
2397 return avail;
2400 /* Add the function FNDECL to the call graph.
2401 Unlike cgraph_finalize_function, this function is intended to be used
2402 by middle end and allows insertion of new function at arbitrary point
2403 of compilation. The function can be either in high, low or SSA form
2404 GIMPLE.
2406 The function is assumed to be reachable and have address taken (so no
2407 API breaking optimizations are performed on it).
2409 Main work done by this function is to enqueue the function for later
2410 processing to avoid need the passes to be re-entrant. */
2412 void
2413 cgraph_add_new_function (tree fndecl, bool lowered)
2415 struct cgraph_node *node;
2416 switch (cgraph_state)
2418 case CGRAPH_STATE_CONSTRUCTION:
2419 /* Just enqueue function to be processed at nearest occurrence. */
2420 node = cgraph_create_node (fndecl);
2421 node->next_needed = cgraph_new_nodes;
2422 if (lowered)
2423 node->lowered = true;
2424 cgraph_new_nodes = node;
2425 break;
2427 case CGRAPH_STATE_IPA:
2428 case CGRAPH_STATE_IPA_SSA:
2429 case CGRAPH_STATE_EXPANSION:
2430 /* Bring the function into finalized state and enqueue for later
2431 analyzing and compilation. */
2432 node = cgraph_get_create_node (fndecl);
2433 node->local.local = false;
2434 node->local.finalized = true;
2435 node->reachable = node->needed = true;
2436 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
2438 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2439 current_function_decl = fndecl;
2440 gimple_register_cfg_hooks ();
2441 tree_lowering_passes (fndecl);
2442 bitmap_obstack_initialize (NULL);
2443 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2444 execute_pass_list (pass_early_local_passes.pass.sub);
2445 bitmap_obstack_release (NULL);
2446 pop_cfun ();
2447 current_function_decl = NULL;
2449 lowered = true;
2451 if (lowered)
2452 node->lowered = true;
2453 node->next_needed = cgraph_new_nodes;
2454 cgraph_new_nodes = node;
2455 break;
2457 case CGRAPH_STATE_FINISHED:
2458 /* At the very end of compilation we have to do all the work up
2459 to expansion. */
2460 node = cgraph_create_node (fndecl);
2461 if (lowered)
2462 node->lowered = true;
2463 cgraph_analyze_function (node);
2464 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2465 current_function_decl = fndecl;
2466 gimple_register_cfg_hooks ();
2467 bitmap_obstack_initialize (NULL);
2468 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
2469 execute_pass_list (pass_early_local_passes.pass.sub);
2470 bitmap_obstack_release (NULL);
2471 tree_rest_of_compilation (fndecl);
2472 pop_cfun ();
2473 current_function_decl = NULL;
2474 break;
2477 /* Set a personality if required and we already passed EH lowering. */
2478 if (lowered
2479 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
2480 == eh_personality_lang))
2481 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
2484 /* Return true if NODE can be made local for API change.
2485 Extern inline functions and C++ COMDAT functions can be made local
2486 at the expense of possible code size growth if function is used in multiple
2487 compilation units. */
2488 bool
2489 cgraph_node_can_be_local_p (struct cgraph_node *node)
2491 return (!node->needed && !node->address_taken
2492 && ((DECL_COMDAT (node->decl) && !node->same_comdat_group)
2493 || !node->local.externally_visible));
2496 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2497 but other code such as notice_global_symbol generates rtl. */
2498 void
2499 cgraph_make_decl_local (tree decl)
2501 rtx rtl, symbol;
2503 if (TREE_CODE (decl) == VAR_DECL)
2504 DECL_COMMON (decl) = 0;
2505 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
2507 if (DECL_COMDAT (decl))
2509 /* It is possible that we are linking against library defining same COMDAT
2510 function. To avoid conflict we need to rename our local name of the
2511 function just in the case WHOPR partitioning decide to make it hidden
2512 to avoid cross partition references. */
2513 if (flag_wpa)
2515 const char *old_name;
2517 old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2518 if (TREE_CODE (decl) == FUNCTION_DECL)
2520 struct cgraph_node *node = cgraph_get_node_or_alias (decl);
2521 change_decl_assembler_name (decl,
2522 clone_function_name (decl, "local"));
2523 if (node->local.lto_file_data)
2524 lto_record_renamed_decl (node->local.lto_file_data,
2525 old_name,
2526 IDENTIFIER_POINTER
2527 (DECL_ASSEMBLER_NAME (decl)));
2529 else if (TREE_CODE (decl) == VAR_DECL)
2531 struct varpool_node *vnode = varpool_get_node (decl);
2532 /* change_decl_assembler_name will warn here on vtables because
2533 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2534 SET_DECL_ASSEMBLER_NAME (decl,
2535 clone_function_name (decl, "local"));
2536 if (vnode->lto_file_data)
2537 lto_record_renamed_decl (vnode->lto_file_data,
2538 old_name,
2539 IDENTIFIER_POINTER
2540 (DECL_ASSEMBLER_NAME (decl)));
2543 DECL_SECTION_NAME (decl) = 0;
2544 DECL_COMDAT (decl) = 0;
2546 DECL_COMDAT_GROUP (decl) = 0;
2547 DECL_WEAK (decl) = 0;
2548 DECL_EXTERNAL (decl) = 0;
2549 TREE_PUBLIC (decl) = 0;
2550 if (!DECL_RTL_SET_P (decl))
2551 return;
2553 /* Update rtl flags. */
2554 make_decl_rtl (decl);
2556 rtl = DECL_RTL (decl);
2557 if (!MEM_P (rtl))
2558 return;
2560 symbol = XEXP (rtl, 0);
2561 if (GET_CODE (symbol) != SYMBOL_REF)
2562 return;
2564 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
2567 /* Bring NODE local. */
2568 void
2569 cgraph_make_node_local (struct cgraph_node *node)
2571 gcc_assert (cgraph_node_can_be_local_p (node));
2572 if (DECL_COMDAT (node->decl) || DECL_EXTERNAL (node->decl))
2574 struct cgraph_node *alias;
2575 cgraph_make_decl_local (node->decl);
2577 for (alias = node->same_body; alias; alias = alias->next)
2578 cgraph_make_decl_local (alias->decl);
2580 node->local.externally_visible = false;
2581 node->local.local = true;
2582 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
2583 gcc_assert (cgraph_function_body_availability (node) == AVAIL_LOCAL);
2587 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2588 if any to NOTHROW. */
2590 void
2591 cgraph_set_nothrow_flag (struct cgraph_node *node, bool nothrow)
2593 struct cgraph_node *alias;
2594 TREE_NOTHROW (node->decl) = nothrow;
2595 for (alias = node->same_body; alias; alias = alias->next)
2596 TREE_NOTHROW (alias->decl) = nothrow;
2599 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2600 if any to READONLY. */
2602 void
2603 cgraph_set_const_flag (struct cgraph_node *node, bool readonly, bool looping)
2605 struct cgraph_node *alias;
2606 /* Static constructors and destructors without a side effect can be
2607 optimized out. */
2608 if (!looping && readonly)
2610 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2611 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2612 if (DECL_STATIC_DESTRUCTOR (node->decl))
2613 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2615 TREE_READONLY (node->decl) = readonly;
2616 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2617 for (alias = node->same_body; alias; alias = alias->next)
2619 TREE_READONLY (alias->decl) = readonly;
2620 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2624 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2625 if any to PURE. */
2627 void
2628 cgraph_set_pure_flag (struct cgraph_node *node, bool pure, bool looping)
2630 struct cgraph_node *alias;
2631 /* Static constructors and destructors without a side effect can be
2632 optimized out. */
2633 if (!looping && pure)
2635 if (DECL_STATIC_CONSTRUCTOR (node->decl))
2636 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
2637 if (DECL_STATIC_DESTRUCTOR (node->decl))
2638 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
2640 DECL_PURE_P (node->decl) = pure;
2641 DECL_LOOPING_CONST_OR_PURE_P (node->decl) = looping;
2642 for (alias = node->same_body; alias; alias = alias->next)
2644 DECL_PURE_P (alias->decl) = pure;
2645 DECL_LOOPING_CONST_OR_PURE_P (alias->decl) = looping;
2649 /* See if the frequency of NODE can be updated based on frequencies of its
2650 callers. */
2651 bool
2652 cgraph_propagate_frequency (struct cgraph_node *node)
2654 bool maybe_unlikely_executed = true, maybe_executed_once = true;
2655 bool only_called_at_startup = true;
2656 bool only_called_at_exit = true;
2657 bool changed = false;
2658 struct cgraph_edge *edge;
2660 if (!node->local.local)
2661 return false;
2662 gcc_assert (node->analyzed);
2663 if (dump_file && (dump_flags & TDF_DETAILS))
2664 fprintf (dump_file, "Processing frequency %s\n", cgraph_node_name (node));
2666 for (edge = node->callers;
2667 edge && (maybe_unlikely_executed || maybe_executed_once
2668 || only_called_at_startup || only_called_at_exit);
2669 edge = edge->next_caller)
2671 if (edge->caller != node)
2673 only_called_at_startup &= edge->caller->only_called_at_startup;
2674 /* It makes sense to put main() together with the static constructors.
2675 It will be executed for sure, but rest of functions called from
2676 main are definitely not at startup only. */
2677 if (MAIN_NAME_P (DECL_NAME (edge->caller->decl)))
2678 only_called_at_startup = 0;
2679 only_called_at_exit &= edge->caller->only_called_at_exit;
2681 if (!edge->frequency)
2682 continue;
2683 switch (edge->caller->frequency)
2685 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
2686 break;
2687 case NODE_FREQUENCY_EXECUTED_ONCE:
2688 if (dump_file && (dump_flags & TDF_DETAILS))
2689 fprintf (dump_file, " Called by %s that is executed once\n",
2690 cgraph_node_name (edge->caller));
2691 maybe_unlikely_executed = false;
2692 if (inline_edge_summary (edge)->loop_depth)
2694 maybe_executed_once = false;
2695 if (dump_file && (dump_flags & TDF_DETAILS))
2696 fprintf (dump_file, " Called in loop\n");
2698 break;
2699 case NODE_FREQUENCY_HOT:
2700 case NODE_FREQUENCY_NORMAL:
2701 if (dump_file && (dump_flags & TDF_DETAILS))
2702 fprintf (dump_file, " Called by %s that is normal or hot\n",
2703 cgraph_node_name (edge->caller));
2704 maybe_unlikely_executed = false;
2705 maybe_executed_once = false;
2706 break;
2709 if ((only_called_at_startup && !only_called_at_exit)
2710 && !node->only_called_at_startup)
2712 node->only_called_at_startup = true;
2713 if (dump_file)
2714 fprintf (dump_file, "Node %s promoted to only called at startup.\n",
2715 cgraph_node_name (node));
2716 changed = true;
2718 if ((only_called_at_exit && !only_called_at_startup)
2719 && !node->only_called_at_exit)
2721 node->only_called_at_exit = true;
2722 if (dump_file)
2723 fprintf (dump_file, "Node %s promoted to only called at exit.\n",
2724 cgraph_node_name (node));
2725 changed = true;
2727 /* These come either from profile or user hints; never update them. */
2728 if (node->frequency == NODE_FREQUENCY_HOT
2729 || node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2730 return changed;
2731 if (maybe_unlikely_executed)
2733 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
2734 if (dump_file)
2735 fprintf (dump_file, "Node %s promoted to unlikely executed.\n",
2736 cgraph_node_name (node));
2737 changed = true;
2739 else if (maybe_executed_once && node->frequency != NODE_FREQUENCY_EXECUTED_ONCE)
2741 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
2742 if (dump_file)
2743 fprintf (dump_file, "Node %s promoted to executed once.\n",
2744 cgraph_node_name (node));
2745 changed = true;
2747 return changed;
2750 /* Return true when NODE can not return or throw and thus
2751 it is safe to ignore its side effects for IPA analysis. */
2753 bool
2754 cgraph_node_cannot_return (struct cgraph_node *node)
2756 int flags = flags_from_decl_or_type (node->decl);
2757 if (!flag_exceptions)
2758 return (flags & ECF_NORETURN) != 0;
2759 else
2760 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2761 == (ECF_NORETURN | ECF_NOTHROW));
2764 /* Return true when call of E can not lead to return from caller
2765 and thus it is safe to ignore its side effects for IPA analysis
2766 when computing side effects of the caller.
2767 FIXME: We could actually mark all edges that have no reaching
2768 patch to EXIT_BLOCK_PTR or throw to get better results. */
2769 bool
2770 cgraph_edge_cannot_lead_to_return (struct cgraph_edge *e)
2772 if (cgraph_node_cannot_return (e->caller))
2773 return true;
2774 if (e->indirect_unknown_callee)
2776 int flags = e->indirect_info->ecf_flags;
2777 if (!flag_exceptions)
2778 return (flags & ECF_NORETURN) != 0;
2779 else
2780 return ((flags & (ECF_NORETURN | ECF_NOTHROW))
2781 == (ECF_NORETURN | ECF_NOTHROW));
2783 else
2784 return cgraph_node_cannot_return (e->callee);
2787 /* Return true when function NODE can be removed from callgraph
2788 if all direct calls are eliminated. */
2790 bool
2791 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node *node)
2793 gcc_assert (!node->global.inlined_to);
2794 /* Extern inlines can always go, we will use the external definition. */
2795 if (DECL_EXTERNAL (node->decl))
2796 return true;
2797 /* When function is needed, we can not remove it. */
2798 if (node->needed || node->reachable_from_other_partition)
2799 return false;
2800 if (DECL_STATIC_CONSTRUCTOR (node->decl)
2801 || DECL_STATIC_DESTRUCTOR (node->decl))
2802 return false;
2803 /* Only COMDAT functions can be removed if externally visible. */
2804 if (node->local.externally_visible
2805 && (!DECL_COMDAT (node->decl)
2806 || cgraph_used_from_object_file_p (node)))
2807 return false;
2808 return true;
2811 /* Return true when function NODE can be expected to be removed
2812 from program when direct calls in this compilation unit are removed.
2814 As a special case COMDAT functions are
2815 cgraph_can_remove_if_no_direct_calls_p while the are not
2816 cgraph_only_called_directly_p (it is possible they are called from other
2817 unit)
2819 This function behaves as cgraph_only_called_directly_p because eliminating
2820 all uses of COMDAT function does not make it necessarily disappear from
2821 the program unless we are compiling whole program or we do LTO. In this
2822 case we know we win since dynamic linking will not really discard the
2823 linkonce section. */
2825 bool
2826 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node *node)
2828 gcc_assert (!node->global.inlined_to);
2829 if (cgraph_used_from_object_file_p (node))
2830 return false;
2831 if (!in_lto_p && !flag_whole_program)
2832 return cgraph_only_called_directly_p (node);
2833 else
2835 if (DECL_EXTERNAL (node->decl))
2836 return true;
2837 return cgraph_can_remove_if_no_direct_calls_p (node);
2841 /* Return true when RESOLUTION indicate that linker will use
2842 the symbol from non-LTO object files. */
2844 bool
2845 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2847 return (resolution == LDPR_PREVAILING_DEF
2848 || resolution == LDPR_PREEMPTED_REG
2849 || resolution == LDPR_RESOLVED_EXEC
2850 || resolution == LDPR_RESOLVED_DYN);
2853 /* Return true when NODE is known to be used from other (non-LTO) object file.
2854 Known only when doing LTO via linker plugin. */
2856 bool
2857 cgraph_used_from_object_file_p (struct cgraph_node *node)
2859 struct cgraph_node *alias;
2861 gcc_assert (!node->global.inlined_to);
2862 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
2863 return false;
2864 if (resolution_used_from_other_file_p (node->resolution))
2865 return true;
2866 for (alias = node->same_body; alias; alias = alias->next)
2867 if (TREE_PUBLIC (alias->decl)
2868 && resolution_used_from_other_file_p (alias->resolution))
2869 return true;
2870 return false;
2873 #include "gt-cgraph.h"