1 /* Callgraph handling code.
2 Copyright (C) 2003 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
24 #include "coretypes.h"
27 #include "tree-inline.h"
28 #include "langhooks.h"
38 /* The known declarations must not get garbage collected. Callgraph
39 datastructures should not get saved via PCH code since this would
40 make it difficult to extend into intra-module optimizer later. So
41 we store only the references into the array to prevent gabrage
42 collector from deleting live data. */
43 static GTY(()) varray_type known_fns;
45 /* Hash table used to convert declarations into nodes. */
46 static htab_t cgraph_hash = 0;
48 /* The linked list of cgraph nodes. */
49 struct cgraph_node *cgraph_nodes;
51 /* Queue of cgraph nodes scheduled to be lowered. */
52 struct cgraph_node *cgraph_nodes_queue;
54 /* Number of nodes in existence. */
57 /* Set when whole unit has been analyzed so we can access global info. */
58 bool cgraph_global_info_ready = false;
60 static struct cgraph_edge *create_edge PARAMS ((struct cgraph_node *,
61 struct cgraph_node *));
62 static void cgraph_remove_edge PARAMS ((struct cgraph_node *, struct cgraph_node *));
63 static hashval_t hash_node PARAMS ((const void *));
64 static int eq_node PARAMS ((const void *, const void *));
66 /* Returns a hash code for P. */
73 htab_hash_pointer (DECL_ASSEMBLER_NAME
74 (((struct cgraph_node *) p)->decl));
77 /* Returns nonzero if P1 and P2 are equal. */
84 return ((DECL_ASSEMBLER_NAME (((struct cgraph_node *) p1)->decl)) ==
88 /* Return cgraph node assigned to DECL. Create new one when needed. */
93 struct cgraph_node *node;
94 struct cgraph_node **slot;
96 if (TREE_CODE (decl) != FUNCTION_DECL)
101 cgraph_hash = htab_create (10, hash_node, eq_node, NULL);
102 VARRAY_TREE_INIT (known_fns, 32, "known_fns");
106 (struct cgraph_node **) htab_find_slot_with_hash (cgraph_hash,
107 DECL_ASSEMBLER_NAME (decl),
113 node = xcalloc (sizeof (*node), 1);
115 node->next = cgraph_nodes;
117 cgraph_nodes->previous = node;
118 node->previous = NULL;
122 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
124 node->origin = cgraph_node (DECL_CONTEXT (decl));
125 node->next_nested = node->origin->nested;
126 node->origin->nested = node;
128 VARRAY_PUSH_TREE (known_fns, decl);
132 /* Try to find existing function for identifier ID. */
134 cgraph_node_for_identifier (id)
137 struct cgraph_node **slot;
139 if (TREE_CODE (id) != IDENTIFIER_NODE)
144 cgraph_hash = htab_create (10, hash_node, eq_node, NULL);
145 VARRAY_TREE_INIT (known_fns, 32, "known_fns");
149 (struct cgraph_node **) htab_find_slot_with_hash (cgraph_hash, id,
150 htab_hash_pointer (id), 0);
156 /* Create edge from CALLER to CALLEE in the cgraph. */
158 static struct cgraph_edge *
159 create_edge (caller, callee)
160 struct cgraph_node *caller, *callee;
162 struct cgraph_edge *edge = xmalloc (sizeof (struct cgraph_edge));
164 edge->caller = caller;
165 edge->callee = callee;
166 edge->next_caller = callee->callers;
167 edge->next_callee = caller->callees;
168 caller->callees = edge;
169 callee->callers = edge;
173 /* Remove the edge from CALLER to CALLEE in the cgraph. */
176 cgraph_remove_edge (caller, callee)
177 struct cgraph_node *caller, *callee;
179 struct cgraph_edge **edge, **edge2;
181 for (edge = &callee->callers; *edge && (*edge)->caller != caller;
182 edge = &((*edge)->next_caller))
186 *edge = (*edge)->next_caller;
187 for (edge2 = &caller->callees; *edge2 && (*edge2)->callee != callee;
188 edge2 = &(*edge2)->next_callee)
192 *edge2 = (*edge2)->next_callee;
195 /* Remove the node from cgraph. */
198 cgraph_remove_node (node)
199 struct cgraph_node *node;
201 while (node->callers)
202 cgraph_remove_edge (node->callers->caller, node);
203 while (node->callees)
204 cgraph_remove_edge (node, node->callees->callee);
206 cgraph_remove_node (node->nested);
209 struct cgraph_node **node2 = &node->origin->nested;
211 while (*node2 != node)
212 node2 = &(*node2)->next_nested;
213 *node2 = node->next_nested;
216 node->previous->next = node->next;
220 node->next->previous = node->previous;
221 DECL_SAVED_TREE (node->decl) = NULL;
222 /* Do not free the structure itself so the walk over chain can continue. */
225 /* Notify finalize_compilation_unit that given node is reachable
228 cgraph_mark_needed_node (node, needed)
229 struct cgraph_node *node;
236 if (!node->reachable)
239 if (DECL_SAVED_TREE (node->decl))
241 node->aux = cgraph_nodes_queue;
242 cgraph_nodes_queue = node;
248 /* Record call from CALLER to CALLEE */
251 cgraph_record_call (caller, callee)
254 return create_edge (cgraph_node (caller), cgraph_node (callee));
258 cgraph_remove_call (caller, callee)
261 cgraph_remove_edge (cgraph_node (caller), cgraph_node (callee));
264 /* Return true when CALLER_DECL calls CALLEE_DECL. */
267 cgraph_calls_p (caller_decl, callee_decl)
268 tree caller_decl, callee_decl;
270 struct cgraph_node *caller = cgraph_node (caller_decl);
271 struct cgraph_node *callee = cgraph_node (callee_decl);
272 struct cgraph_edge *edge;
274 for (edge = callee->callers; edge && (edge)->caller != caller;
275 edge = (edge->next_caller))
280 /* Return local info for the compiled function. */
282 struct cgraph_local_info *
283 cgraph_local_info (decl)
286 struct cgraph_node *node;
287 if (TREE_CODE (decl) != FUNCTION_DECL)
289 node = cgraph_node (decl);
293 /* Return local info for the compiled function. */
295 struct cgraph_global_info *
296 cgraph_global_info (decl)
299 struct cgraph_node *node;
300 if (TREE_CODE (decl) != FUNCTION_DECL || !cgraph_global_info_ready)
302 node = cgraph_node (decl);
303 return &node->global;
306 /* Return local info for the compiled function. */
308 struct cgraph_rtl_info *
309 cgraph_rtl_info (decl)
312 struct cgraph_node *node;
313 if (TREE_CODE (decl) != FUNCTION_DECL)
315 node = cgraph_node (decl);
316 if (decl != current_function_decl
317 && !TREE_ASM_WRITTEN (node->decl))
323 /* Dump the callgraph. */
329 struct cgraph_node *node;
331 fprintf (f, "\nCallgraph:\n\n");
332 for (node = cgraph_nodes; node; node = node->next)
334 struct cgraph_edge *edge;
335 fprintf (f, "%s", IDENTIFIER_POINTER (DECL_NAME (node->decl)));
337 fprintf (f, " nested in: %s",
338 IDENTIFIER_POINTER (DECL_NAME (node->origin->decl)));
340 fprintf (f, " needed");
341 else if (node->reachable)
342 fprintf (f, " reachable");
343 if (DECL_SAVED_TREE (node->decl))
344 fprintf (f, " tree");
346 fprintf (f, "\n called by :");
347 for (edge = node->callers; edge; edge = edge->next_caller)
349 IDENTIFIER_POINTER (DECL_NAME (edge->caller->decl)));
351 fprintf (f, "\n calls: ");
352 for (edge = node->callees; edge; edge = edge->next_callee)
354 IDENTIFIER_POINTER (DECL_NAME (edge->callee->decl)));
359 #include "gt-cgraph.h"