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 static struct cgraph_edge *create_edge PARAMS ((struct cgraph_node *,
47 struct cgraph_node *));
48 static void remove_edge PARAMS ((struct cgraph_node *, struct cgraph_node *));
49 static hashval_t hash_node PARAMS ((const PTR));
50 static int eq_node PARAMS ((const PTR, const PTR));
52 /* Returns a hash code for P. */
59 htab_hash_pointer (DECL_ASSEMBLER_NAME
60 (((struct cgraph_node *) p)->decl));
63 /* Returns non-zero if P1 and P2 are equal. */
70 return ((DECL_ASSEMBLER_NAME (((struct cgraph_node *) p1)->decl)) ==
71 DECL_ASSEMBLER_NAME ((tree) p2));
74 /* Return cgraph node assigned to DECL. Create new one when needed. */
79 struct cgraph_node *node;
80 struct cgraph_node **slot;
83 cgraph_hash = htab_create (10, hash_node, eq_node, NULL);
86 (struct cgraph_node **) htab_find_slot_with_hash (cgraph_hash, decl,
92 node = xcalloc (sizeof (*node), 1);
94 node->next = cgraph_nodes;
98 if (DECL_CONTEXT (decl))
100 node->origin = cgraph_node (DECL_CONTEXT (decl));
101 node->next_nested = node->origin->nested;
102 node->origin->nested = node;
107 /* Create edge from CALLER to CALLEE in the cgraph. */
109 static struct cgraph_edge *
110 create_edge (caller, callee)
111 struct cgraph_node *caller, *callee;
113 struct cgraph_edge *edge = xmalloc (sizeof (struct cgraph_edge));
115 edge->caller = caller;
116 edge->callee = callee;
117 edge->next_caller = callee->callers;
118 edge->next_callee = caller->callees;
119 caller->callees = edge;
120 callee->callers = edge;
124 /* Remove the edge from CALLER to CALLEE in the cgraph. */
127 remove_edge (caller, callee)
128 struct cgraph_node *caller, *callee;
130 struct cgraph_edge **edge, **edge2;
132 for (edge = &callee->callers; *edge && (*edge)->caller != caller;
133 edge = &((*edge)->next_caller))
137 *edge = (*edge)->next_caller;
138 for (edge2 = &caller->callees; *edge2 && (*edge2)->callee != callee;
139 edge2 = &(*edge2)->next_callee)
143 *edge2 = (*edge2)->next_callee;
146 /* Record call from CALLER to CALLEE */
149 cgraph_record_call (caller, callee)
152 return create_edge (cgraph_node (caller), cgraph_node (callee));
156 cgraph_remove_call (caller, callee)
159 remove_edge (cgraph_node (caller), cgraph_node (callee));
162 /* Return true when CALLER_DECL calls CALLEE_DECL. */
165 cgraph_calls_p (caller_decl, callee_decl)
166 tree caller_decl, callee_decl;
168 struct cgraph_node *caller = cgraph_node (caller_decl);
169 struct cgraph_node *callee = cgraph_node (callee_decl);
170 struct cgraph_edge *edge;
172 for (edge = callee->callers; edge && (edge)->caller != caller;
173 edge = (edge->next_caller))
178 /* Dump the callgraph. */
184 struct cgraph_node *node;
186 fprintf (f, "\nCallgraph:\n\n");
187 for (node = cgraph_nodes; node; node = node->next)
189 struct cgraph_edge *edge;
190 fprintf (f, "%s", IDENTIFIER_POINTER (DECL_NAME (node->decl)));
192 fprintf (f, " nested in: %s",
193 IDENTIFIER_POINTER (DECL_NAME (node->origin->decl)));
195 fprintf (f, " needed");
196 else if (node->reachable)
197 fprintf (f, " reachable");
198 if (DECL_SAVED_TREE (node->decl))
199 fprintf (f, " tree");
201 fprintf (f, "\n called by :");
202 for (edge = node->callers; edge; edge = edge->next_caller)
204 IDENTIFIER_POINTER (DECL_NAME (edge->caller->decl)));
206 fprintf (f, "\n calls: ");
207 for (edge = node->callees; edge; edge = edge->next_callee)
209 IDENTIFIER_POINTER (DECL_NAME (edge->callee->decl)));