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"
37 /* Hash table used to convert declarations into nodes. */
38 static htab_t cgraph_hash = 0;
40 /* The linked list of cgraph nodes. */
41 struct cgraph_node *cgraph_nodes;
43 /* Number of nodes in existence. */
46 /* Set when whole unit has been analyzed so we can access global info. */
47 bool cgraph_global_info_ready = false;
49 static struct cgraph_edge *create_edge PARAMS ((struct cgraph_node *,
50 struct cgraph_node *));
51 static void remove_edge PARAMS ((struct cgraph_node *, struct cgraph_node *));
52 static hashval_t hash_node PARAMS ((const PTR));
53 static int eq_node PARAMS ((const PTR, const PTR));
55 /* Returns a hash code for P. */
62 htab_hash_pointer (DECL_ASSEMBLER_NAME
63 (((struct cgraph_node *) p)->decl));
66 /* Returns non-zero if P1 and P2 are equal. */
73 return ((DECL_ASSEMBLER_NAME (((struct cgraph_node *) p1)->decl)) ==
74 DECL_ASSEMBLER_NAME ((tree) p2));
77 /* Return cgraph node assigned to DECL. Create new one when needed. */
82 struct cgraph_node *node;
83 struct cgraph_node **slot;
86 cgraph_hash = htab_create (10, hash_node, eq_node, NULL);
89 (struct cgraph_node **) htab_find_slot_with_hash (cgraph_hash, decl,
95 node = xcalloc (sizeof (*node), 1);
97 node->next = cgraph_nodes;
101 if (DECL_CONTEXT (decl))
103 node->origin = cgraph_node (DECL_CONTEXT (decl));
104 node->next_nested = node->origin->nested;
105 node->origin->nested = node;
110 /* Create edge from CALLER to CALLEE in the cgraph. */
112 static struct cgraph_edge *
113 create_edge (caller, callee)
114 struct cgraph_node *caller, *callee;
116 struct cgraph_edge *edge = xmalloc (sizeof (struct cgraph_edge));
118 edge->caller = caller;
119 edge->callee = callee;
120 edge->next_caller = callee->callers;
121 edge->next_callee = caller->callees;
122 caller->callees = edge;
123 callee->callers = edge;
127 /* Remove the edge from CALLER to CALLEE in the cgraph. */
130 remove_edge (caller, callee)
131 struct cgraph_node *caller, *callee;
133 struct cgraph_edge **edge, **edge2;
135 for (edge = &callee->callers; *edge && (*edge)->caller != caller;
136 edge = &((*edge)->next_caller))
140 *edge = (*edge)->next_caller;
141 for (edge2 = &caller->callees; *edge2 && (*edge2)->callee != callee;
142 edge2 = &(*edge2)->next_callee)
146 *edge2 = (*edge2)->next_callee;
149 /* Record call from CALLER to CALLEE */
152 cgraph_record_call (caller, callee)
155 return create_edge (cgraph_node (caller), cgraph_node (callee));
159 cgraph_remove_call (caller, callee)
162 remove_edge (cgraph_node (caller), cgraph_node (callee));
165 /* Return true when CALLER_DECL calls CALLEE_DECL. */
168 cgraph_calls_p (caller_decl, callee_decl)
169 tree caller_decl, callee_decl;
171 struct cgraph_node *caller = cgraph_node (caller_decl);
172 struct cgraph_node *callee = cgraph_node (callee_decl);
173 struct cgraph_edge *edge;
175 for (edge = callee->callers; edge && (edge)->caller != caller;
176 edge = (edge->next_caller))
181 /* Return local info for the compiled function. */
183 struct cgraph_local_info *
184 cgraph_local_info (decl)
187 struct cgraph_node *node;
188 if (TREE_CODE (decl) != FUNCTION_DECL)
190 node = cgraph_node (decl);
194 /* Return local info for the compiled function. */
196 struct cgraph_global_info *
197 cgraph_global_info (decl)
200 struct cgraph_node *node;
201 if (TREE_CODE (decl) != FUNCTION_DECL || !cgraph_global_info_ready)
203 node = cgraph_node (decl);
204 return &node->global;
208 /* Dump the callgraph. */
214 struct cgraph_node *node;
216 fprintf (f, "\nCallgraph:\n\n");
217 for (node = cgraph_nodes; node; node = node->next)
219 struct cgraph_edge *edge;
220 fprintf (f, "%s", IDENTIFIER_POINTER (DECL_NAME (node->decl)));
222 fprintf (f, " nested in: %s",
223 IDENTIFIER_POINTER (DECL_NAME (node->origin->decl)));
225 fprintf (f, " needed");
226 else if (node->reachable)
227 fprintf (f, " reachable");
228 if (DECL_SAVED_TREE (node->decl))
229 fprintf (f, " tree");
231 fprintf (f, "\n called by :");
232 for (edge = node->callers; edge; edge = edge->next_caller)
234 IDENTIFIER_POINTER (DECL_NAME (edge->caller->decl)));
236 fprintf (f, "\n calls: ");
237 for (edge = node->callees; edge; edge = edge->next_callee)
239 IDENTIFIER_POINTER (DECL_NAME (edge->callee->decl)));