1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file contains basic routines manipulating call graph and variable pool
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
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node (in
32 contrast to tree DECL nodes where we can have multiple nodes for each
35 The mapping from declarations to call-graph nodes is done using hash table
36 based on DECL_ASSEMBLER_NAME, so it is essential for assembler name to
37 not change once the declaration is inserted into the call-graph.
38 The call-graph nodes are created lazily using cgraph_node function when
39 called for unknown declaration.
41 When built, there is one edge for each direct call. It is possible that
42 the reference will be later optimized out. The call-graph is built
43 conservatively in order to make conservative data flow analysis possible.
45 The callgraph at the moment does not represent indirect calls or calls
46 from other compilation unit. Flag NEEDED is set for each node that may
47 be accessed in such an invisible way and it shall be considered an
48 entry point to the callgraph.
50 Intraprocedural information:
52 Callgraph is place to store data needed for intraprocedural optimization.
53 All data structures are divided into three components: local_info that
54 is produced while analyzing the function, global_info that is result
55 of global walking of the callgraph on the end of compilation and
56 rtl_info used by RTL backend to propagate data from already compiled
57 functions to their callers.
61 The function inlining information is decided in advance and maintained
62 in the callgraph as so called inline plan.
63 For each inlined call, the callee's node is cloned to represent the
64 new function copy produced by inliner.
65 Each inlined call gets a unique corresponding clone node of the callee
66 and the data structure is updated while inlining is performed, so
67 the clones are eliminated and their callee edges redirected to the
70 Each edge has "inline_failed" field. When the field is set to NULL,
71 the call will be inlined. When it is non-NULL it contains a reason
72 why inlining wasn't performed.
75 The varpool data structure:
77 Varpool is used to maintain variables in similar manner as call-graph
78 is used for functions. Most of the API is symmetric replacing cgraph
79 function prefix by cgraph_varpool */
84 #include "coretypes.h"
87 #include "langhooks.h"
99 static void cgraph_node_remove_callers (struct cgraph_node *node);
100 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
101 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
103 /* Hash table used to convert declarations into nodes. */
104 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
106 /* The linked list of cgraph nodes. */
107 struct cgraph_node *cgraph_nodes;
109 /* Queue of cgraph nodes scheduled to be lowered. */
110 struct cgraph_node *cgraph_nodes_queue;
112 /* Number of nodes in existence. */
115 /* Maximal uid used in cgraph nodes. */
118 /* Set when whole unit has been analyzed so we can access global info. */
119 bool cgraph_global_info_ready = false;
121 /* Hash table used to convert declarations into nodes. */
122 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
124 /* Queue of cgraph nodes scheduled to be lowered and output. */
125 struct cgraph_varpool_node *cgraph_varpool_nodes_queue;
127 /* The linked list of cgraph varpool nodes. */
128 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_nodes;
130 static hashval_t hash_node (const void *);
131 static int eq_node (const void *, const void *);
133 /* Returns a hash code for P. */
136 hash_node (const void *p)
138 const struct cgraph_node *n = p;
139 return (hashval_t) DECL_UID (n->decl);
142 /* Returns nonzero if P1 and P2 are equal. */
145 eq_node (const void *p1, const void *p2)
147 const struct cgraph_node *n1 = p1, *n2 = p2;
148 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
151 /* Allocate new callgraph node and insert it into basic data structures. */
152 static struct cgraph_node *
153 cgraph_create_node (void)
155 struct cgraph_node *node;
157 node = ggc_alloc_cleared (sizeof (*node));
158 node->next = cgraph_nodes;
159 node->uid = cgraph_max_uid++;
161 cgraph_nodes->previous = node;
162 node->previous = NULL;
168 /* Return cgraph node assigned to DECL. Create new one when needed. */
170 cgraph_node (tree decl)
172 struct cgraph_node key, *node, **slot;
174 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
177 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
181 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
186 node = cgraph_create_node ();
189 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
191 node->origin = cgraph_node (DECL_CONTEXT (decl));
192 node->next_nested = node->origin->nested;
193 node->origin->nested = node;
198 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
201 decl_assembler_name_equal (tree decl, tree asmname)
203 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
205 if (decl_asmname == asmname)
208 /* If the target assembler name was set by the user, things are trickier.
209 We have a leading '*' to begin with. After that, it's arguable what
210 is the correct thing to do with -fleading-underscore. Arguably, we've
211 historically been doing the wrong thing in assemble_alias by always
212 printing the leading underscore. Since we're not changing that, make
213 sure user_label_prefix follows the '*' before matching. */
214 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
216 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
217 size_t ulp_len = strlen (user_label_prefix);
221 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
226 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
233 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
234 Return NULL if there's no such node. */
237 cgraph_node_for_asm (tree asmname)
239 struct cgraph_node *node;
241 for (node = cgraph_nodes; node ; node = node->next)
242 if (decl_assembler_name_equal (node->decl, asmname))
248 /* Return callgraph edge representing CALL_EXPR. */
250 cgraph_edge (struct cgraph_node *node, tree call_expr)
252 struct cgraph_edge *e;
254 /* This loop may turn out to be performance problem. In such case adding
255 hashtables into call nodes with very many edges is probably best
256 solution. It is not good idea to add pointer into CALL_EXPR itself
257 because we want to make possible having multiple cgraph nodes representing
258 different clones of the same body before the body is actually cloned. */
259 for (e = node->callees; e; e= e->next_callee)
260 if (e->call_expr == call_expr)
265 /* Create edge from CALLER to CALLEE in the cgraph. */
268 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
271 struct cgraph_edge *edge = ggc_alloc (sizeof (struct cgraph_edge));
272 #ifdef ENABLE_CHECKING
273 struct cgraph_edge *e;
275 for (e = caller->callees; e; e = e->next_callee)
276 gcc_assert (e->call_expr != call_expr);
279 gcc_assert (TREE_CODE (call_expr) == CALL_EXPR);
281 if (!DECL_SAVED_TREE (callee->decl))
282 edge->inline_failed = N_("function body not available");
283 else if (callee->local.redefined_extern_inline)
284 edge->inline_failed = N_("redefined extern inline functions are not "
285 "considered for inlining");
286 else if (callee->local.inlinable)
287 edge->inline_failed = N_("function not considered for inlining");
289 edge->inline_failed = N_("function not inlinable");
293 edge->caller = caller;
294 edge->callee = callee;
295 edge->call_expr = call_expr;
296 edge->prev_caller = NULL;
297 edge->next_caller = callee->callers;
299 callee->callers->prev_caller = edge;
300 edge->prev_callee = NULL;
301 edge->next_callee = caller->callees;
303 caller->callees->prev_callee = edge;
304 caller->callees = edge;
305 callee->callers = edge;
309 /* Remove the edge E from the list of the callers of the callee. */
312 cgraph_edge_remove_callee (struct cgraph_edge *e)
315 e->prev_caller->next_caller = e->next_caller;
317 e->next_caller->prev_caller = e->prev_caller;
319 e->callee->callers = e->next_caller;
322 /* Remove the edge E from the list of the callees of the caller. */
325 cgraph_edge_remove_caller (struct cgraph_edge *e)
328 e->prev_callee->next_callee = e->next_callee;
330 e->next_callee->prev_callee = e->prev_callee;
332 e->caller->callees = e->next_callee;
335 /* Remove the edge E in the cgraph. */
338 cgraph_remove_edge (struct cgraph_edge *e)
340 /* Remove from callers list of the callee. */
341 cgraph_edge_remove_callee (e);
343 /* Remove from callees list of the callers. */
344 cgraph_edge_remove_caller (e);
347 /* Redirect callee of E to N. The function does not update underlying
351 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
353 /* Remove from callers list of the current callee. */
354 cgraph_edge_remove_callee (e);
356 /* Insert to callers list of the new callee. */
357 e->prev_caller = NULL;
359 n->callers->prev_caller = e;
360 e->next_caller = n->callers;
365 /* Remove all callees from the node. */
368 cgraph_node_remove_callees (struct cgraph_node *node)
370 struct cgraph_edge *e;
372 /* It is sufficient to remove the edges from the lists of callers of
373 the callees. The callee list of the node can be zapped with one
375 for (e = node->callees; e; e = e->next_callee)
376 cgraph_edge_remove_callee (e);
377 node->callees = NULL;
380 /* Remove all callers from the node. */
383 cgraph_node_remove_callers (struct cgraph_node *node)
385 struct cgraph_edge *e;
387 /* It is sufficient to remove the edges from the lists of callees of
388 the callers. The caller list of the node can be zapped with one
390 for (e = node->callers; e; e = e->next_caller)
391 cgraph_edge_remove_caller (e);
392 node->callers = NULL;
395 /* Remove the node from cgraph. */
398 cgraph_remove_node (struct cgraph_node *node)
401 bool kill_body = false;
403 cgraph_node_remove_callers (node);
404 cgraph_node_remove_callees (node);
406 cgraph_remove_node (node->nested);
409 struct cgraph_node **node2 = &node->origin->nested;
411 while (*node2 != node)
412 node2 = &(*node2)->next_nested;
413 *node2 = node->next_nested;
416 node->previous->next = node->next;
418 cgraph_nodes = node->next;
420 node->next->previous = node->previous;
421 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
424 if (node->next_clone)
425 *slot = node->next_clone;
428 htab_clear_slot (cgraph_hash, slot);
434 struct cgraph_node *n;
436 for (n = *slot; n->next_clone != node; n = n->next_clone)
438 n->next_clone = node->next_clone;
441 /* While all the clones are removed after being proceeded, the function
442 itself is kept in the cgraph even after it is compiled. Check whether
443 we are done with this body and reclaim it proactively if this is the case.
445 if (!kill_body && *slot)
447 struct cgraph_node *n = *slot;
448 if (!n->next_clone && !n->global.inlined_to
449 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)))
453 if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
455 DECL_SAVED_TREE (node->decl) = NULL;
456 DECL_STRUCT_FUNCTION (node->decl) = NULL;
457 DECL_INITIAL (node->decl) = error_mark_node;
460 /* Do not free the structure itself so the walk over chain can continue. */
463 /* Notify finalize_compilation_unit that given node is reachable. */
466 cgraph_mark_reachable_node (struct cgraph_node *node)
468 if (!node->reachable && node->local.finalized)
470 notice_global_symbol (node->decl);
473 node->next_needed = cgraph_nodes_queue;
474 cgraph_nodes_queue = node;
478 /* Likewise indicate that a node is needed, i.e. reachable via some
482 cgraph_mark_needed_node (struct cgraph_node *node)
485 cgraph_mark_reachable_node (node);
488 /* Return local info for the compiled function. */
490 struct cgraph_local_info *
491 cgraph_local_info (tree decl)
493 struct cgraph_node *node;
495 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
496 node = cgraph_node (decl);
500 /* Return local info for the compiled function. */
502 struct cgraph_global_info *
503 cgraph_global_info (tree decl)
505 struct cgraph_node *node;
507 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
508 node = cgraph_node (decl);
509 return &node->global;
512 /* Return local info for the compiled function. */
514 struct cgraph_rtl_info *
515 cgraph_rtl_info (tree decl)
517 struct cgraph_node *node;
519 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
520 node = cgraph_node (decl);
521 if (decl != current_function_decl
522 && !TREE_ASM_WRITTEN (node->decl))
527 /* Return name of the node used in debug output. */
529 cgraph_node_name (struct cgraph_node *node)
531 return lang_hooks.decl_printable_name (node->decl, 2);
534 /* Dump given cgraph node. */
536 dump_cgraph_node (FILE *f, struct cgraph_node *node)
538 struct cgraph_edge *edge;
539 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
540 if (node->global.inlined_to)
541 fprintf (f, " (inline copy in %s/%i)",
542 cgraph_node_name (node->global.inlined_to),
543 node->global.inlined_to->uid);
544 if (node->local.self_insns)
545 fprintf (f, " %i insns", node->local.self_insns);
546 if (node->global.insns && node->global.insns != node->local.self_insns)
547 fprintf (f, " (%i after inlining)", node->global.insns);
549 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
551 fprintf (f, " needed");
552 else if (node->reachable)
553 fprintf (f, " reachable");
554 if (DECL_SAVED_TREE (node->decl))
555 fprintf (f, " tree");
557 fprintf (f, " output");
558 if (node->local.local)
559 fprintf (f, " local");
560 if (node->local.disregard_inline_limits)
561 fprintf (f, " always_inline");
562 else if (node->local.inlinable)
563 fprintf (f, " inlinable");
564 if (TREE_ASM_WRITTEN (node->decl))
565 fprintf (f, " asm_written");
567 fprintf (f, "\n called by: ");
568 for (edge = node->callers; edge; edge = edge->next_caller)
570 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
572 if (!edge->inline_failed)
573 fprintf(f, "(inlined) ");
576 fprintf (f, "\n calls: ");
577 for (edge = node->callees; edge; edge = edge->next_callee)
579 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
581 if (!edge->inline_failed)
582 fprintf(f, "(inlined) ");
587 /* Dump the callgraph. */
590 dump_cgraph (FILE *f)
592 struct cgraph_node *node;
594 fprintf (f, "callgraph:\n\n");
595 for (node = cgraph_nodes; node; node = node->next)
596 dump_cgraph_node (f, node);
599 /* Returns a hash code for P. */
602 hash_varpool_node (const void *p)
604 const struct cgraph_varpool_node *n = p;
605 return (hashval_t) DECL_UID (n->decl);
608 /* Returns nonzero if P1 and P2 are equal. */
611 eq_varpool_node (const void *p1, const void *p2)
613 const struct cgraph_varpool_node *n1 = p1, *n2 = p2;
614 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
617 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
618 struct cgraph_varpool_node *
619 cgraph_varpool_node (tree decl)
621 struct cgraph_varpool_node key, *node, **slot;
623 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
625 if (!cgraph_varpool_hash)
626 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
627 eq_varpool_node, NULL);
629 slot = (struct cgraph_varpool_node **)
630 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
633 node = ggc_alloc_cleared (sizeof (*node));
635 node->next = cgraph_varpool_nodes;
636 cgraph_varpool_nodes = node;
641 struct cgraph_varpool_node *
642 cgraph_varpool_node_for_asm (tree asmname)
644 struct cgraph_varpool_node *node;
646 for (node = cgraph_varpool_nodes; node ; node = node->next)
647 if (decl_assembler_name_equal (node->decl, asmname))
653 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
655 change_decl_assembler_name (tree decl, tree name)
657 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
659 SET_DECL_ASSEMBLER_NAME (decl, name);
662 if (name == DECL_ASSEMBLER_NAME (decl))
665 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
666 && DECL_RTL_SET_P (decl))
667 warning ("%D renamed after being referenced in assembly", decl);
669 SET_DECL_ASSEMBLER_NAME (decl, name);
672 /* Notify finalize_compilation_unit that given node is reachable
675 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
677 if (!node->needed && node->finalized)
679 node->next_needed = cgraph_varpool_nodes_queue;
680 cgraph_varpool_nodes_queue = node;
681 notice_global_symbol (node->decl);
687 cgraph_varpool_finalize_decl (tree decl)
689 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
691 /* The first declaration of a variable that comes through this function
692 decides whether it is global (in C, has external linkage)
693 or local (in C, has internal linkage). So do nothing more
694 if this function has already run. */
699 node->next_needed = cgraph_varpool_nodes_queue;
700 cgraph_varpool_nodes_queue = node;
701 notice_global_symbol (decl);
703 node->finalized = true;
705 if (/* Externally visible variables must be output. The exception are
706 COMDAT functions that must be output only when they are needed. */
707 (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
708 /* Function whose name is output to the assembler file must be produced.
709 It is possible to assemble the name later after finalizing the function
710 and the fact is noticed in assemble_name then. */
711 || (DECL_ASSEMBLER_NAME_SET_P (decl)
712 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
714 cgraph_varpool_mark_needed_node (node);
719 cgraph_varpool_assemble_pending_decls (void)
721 bool changed = false;
723 while (cgraph_varpool_nodes_queue)
725 tree decl = cgraph_varpool_nodes_queue->decl;
726 struct cgraph_varpool_node *node = cgraph_varpool_nodes_queue;
728 cgraph_varpool_nodes_queue = cgraph_varpool_nodes_queue->next_needed;
729 if (!TREE_ASM_WRITTEN (decl))
731 assemble_variable (decl, 0, 1, 0);
734 node->next_needed = NULL;
739 /* Return true when the DECL can possibly be inlined. */
741 cgraph_function_possibly_inlined_p (tree decl)
743 if (!cgraph_global_info_ready)
744 return (DECL_INLINE (decl) && !flag_really_no_inline);
745 return DECL_POSSIBLY_INLINED (decl);
748 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
750 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n, tree call_expr)
752 struct cgraph_edge *new = cgraph_create_edge (n, e->callee, call_expr);
754 new->inline_failed = e->inline_failed;
758 /* Create node representing clone of N. */
760 cgraph_clone_node (struct cgraph_node *n)
762 struct cgraph_node *new = cgraph_create_node ();
763 struct cgraph_edge *e;
766 new->origin = n->origin;
769 new->next_nested = new->origin->nested;
770 new->origin->nested = new;
772 new->analyzed = n->analyzed;
773 new->local = n->local;
774 new->global = n->global;
777 for (e = n->callees;e; e=e->next_callee)
778 cgraph_clone_edge (e, new, e->call_expr);
780 new->next_clone = n->next_clone;
786 /* NODE is no longer nested function; update cgraph accordingly. */
788 cgraph_unnest_node (struct cgraph_node *node)
790 struct cgraph_node **node2 = &node->origin->nested;
791 gcc_assert (node->origin);
793 while (*node2 != node)
794 node2 = &(*node2)->next_nested;
795 *node2 = node->next_nested;
798 #include "gt-cgraph.h"