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"
36 /* The cgraph data strutcture.
37 Each function decl has assigned cgraph_node listing calees and callers. */
42 struct cgraph_edge *callees;
43 struct cgraph_edge *callers;
44 struct cgraph_node *next;
45 /* For nested functions points to function the node is nested in. */
46 struct cgraph_node *origin;
47 /* Points to first nested function, if any. */
48 struct cgraph_node *nested;
49 /* Pointer to the next function with same origin, if any. */
50 struct cgraph_node *next_nested;
53 /* Set when function must be output - it is externally visible
54 or it's address is taken. */
56 /* Set when function is reachable by call from other function
57 that is eighter reachable or needed. */
59 /* Set when the frontend has been asked to lower representation of this
60 function into trees. Callees lists are not available when lowered
63 /* Set when function is scheduled to be assembled. */
69 struct cgraph_node *caller, *callee;
70 struct cgraph_edge *next_caller;
71 struct cgraph_edge *next_callee;
74 /* Hash table used to convert declarations into nodes. */
75 static htab_t cgraph_hash = 0;
77 /* The linked list of cgraph nodes. */
78 static struct cgraph_node *cgraph_nodes;
80 /* Number of nodes in existence. */
81 static int cgraph_n_nodes;
83 static struct cgraph_node *cgraph_node PARAMS ((tree decl));
84 static struct cgraph_edge *create_edge PARAMS ((struct cgraph_node *,
85 struct cgraph_node *));
86 static void remove_edge PARAMS ((struct cgraph_node *, struct cgraph_node *));
87 static struct cgraph_edge *record_call PARAMS ((tree, tree));
88 static tree record_call_1 PARAMS ((tree *, int *, void *));
89 static hashval_t hash_node PARAMS ((const PTR));
90 static int eq_node PARAMS ((const PTR, const PTR));
91 static struct cgraph_node *cgraph_node PARAMS ((tree));
92 static void cgraph_expand_functions PARAMS ((void));
93 static void cgraph_mark_functions_to_output PARAMS ((void));
94 static void cgraph_expand_function PARAMS ((struct cgraph_node *));
95 static void cgraph_mark_needed_node PARAMS ((struct cgraph_node *, int));
97 /* Returns a hash code for P. */
104 htab_hash_pointer (DECL_ASSEMBLER_NAME
105 (((struct cgraph_node *) p)->decl));
108 /* Returns non-zero if P1 and P2 are equal. */
115 return ((DECL_ASSEMBLER_NAME (((struct cgraph_node *) p1)->decl)) ==
116 DECL_ASSEMBLER_NAME ((tree) p2));
119 /* Return cgraph node assigned to DECL. Create new one when needed. */
120 static struct cgraph_node *
124 struct cgraph_node *node;
125 struct cgraph_node **slot;
128 cgraph_hash = htab_create (10, hash_node, eq_node, NULL);
131 (struct cgraph_node **) htab_find_slot_with_hash (cgraph_hash, decl,
137 node = xcalloc (sizeof (*node), 1);
139 node->next = cgraph_nodes;
143 if (DECL_CONTEXT (decl))
145 node->origin = cgraph_node (DECL_CONTEXT (decl));
146 node->next_nested = node->origin->nested;
147 node->origin->nested = node;
152 /* Create edge from CALLER to CALLEE in the cgraph. */
154 static struct cgraph_edge *
155 create_edge (caller, callee)
156 struct cgraph_node *caller, *callee;
158 struct cgraph_edge *edge = xmalloc (sizeof (struct cgraph_edge));
160 edge->caller = caller;
161 edge->callee = callee;
162 edge->next_caller = callee->callers;
163 edge->next_callee = caller->callees;
164 caller->callees = edge;
165 callee->callers = edge;
169 /* Remove the edge from CALLER to CALLEE in the cgraph. */
172 remove_edge (caller, callee)
173 struct cgraph_node *caller, *callee;
175 struct cgraph_edge **edge, **edge2;
177 for (edge = &callee->callers; *edge && (*edge)->caller != caller;
178 edge = &((*edge)->next_caller))
182 *edge = (*edge)->next_caller;
183 for (edge2 = &caller->callees; *edge2 && (*edge2)->callee != callee;
184 edge2 = &(*edge2)->next_callee)
188 *edge2 = (*edge2)->next_callee;
191 /* Record call from CALLER to CALLEE */
193 static struct cgraph_edge *
194 record_call (caller, callee)
197 return create_edge (cgraph_node (caller), cgraph_node (callee));
201 cgraph_remove_call (caller, callee)
204 remove_edge (cgraph_node (caller), cgraph_node (callee));
207 /* Return true when CALLER_DECL calls CALLEE_DECL. */
210 cgraph_calls_p (caller_decl, callee_decl)
211 tree caller_decl, callee_decl;
213 struct cgraph_node *caller = cgraph_node (caller_decl);
214 struct cgraph_node *callee = cgraph_node (callee_decl);
215 struct cgraph_edge *edge;
217 for (edge = callee->callers; edge && (edge)->caller != caller;
218 edge = (edge->next_caller))
223 /* Walk tree and record all calls. Called via walk_tree. */
225 record_call_1 (tp, walk_subtrees, data)
230 /* Record dereferences to the functions. This makes the functions
231 reachable unconditionally. */
232 if (TREE_CODE (*tp) == ADDR_EXPR)
234 tree decl = TREE_OPERAND (*tp, 0);
235 if (TREE_CODE (decl) == FUNCTION_DECL)
236 cgraph_mark_needed_node (cgraph_node (decl), 1);
238 else if (TREE_CODE (*tp) == CALL_EXPR)
240 tree decl = TREE_OPERAND (*tp, 0);
241 if (TREE_CODE (decl) == ADDR_EXPR)
242 decl = TREE_OPERAND (decl, 0);
243 if (TREE_CODE (decl) == FUNCTION_DECL)
245 if (DECL_BUILT_IN (decl))
247 record_call (data, decl);
248 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data, NULL);
255 /* Create cgraph edges for function calles via BODY. */
258 cgraph_create_edges (decl, body)
262 walk_tree (&body, record_call_1, decl, NULL);
265 /* Analyze function once it is parsed. Set up the local information
266 available - create cgraph edges for function calles via BODY. */
269 cgraph_finalize_function (decl, body)
271 tree body ATTRIBUTE_UNUSED;
273 struct cgraph_node *node = cgraph_node (decl);
277 /* Set TREE_UNINLINABLE flag. */
278 tree_inlinable_function_p (decl);
280 (*debug_hooks->deferred_inline_function) (decl);
283 /* Dump the callgraph. */
289 struct cgraph_node *node;
291 fprintf (f, "\nCallgraph:\n\n");
292 for (node = cgraph_nodes; node; node = node->next)
294 struct cgraph_edge *edge;
295 fprintf (f, "%s", IDENTIFIER_POINTER (DECL_NAME (node->decl)));
297 fprintf (f, " nested in: %s",
298 IDENTIFIER_POINTER (DECL_NAME (node->origin->decl)));
300 fprintf (f, " needed");
301 else if (node->reachable)
302 fprintf (f, " reachable");
303 if (DECL_SAVED_TREE (node->decl))
304 fprintf (f, " tree");
306 fprintf (f, "\n called by :");
307 for (edge = node->callers; edge; edge = edge->next_caller)
309 IDENTIFIER_POINTER (DECL_NAME (edge->caller->decl)));
311 fprintf (f, "\n calls: ");
312 for (edge = node->callees; edge; edge = edge->next_callee)
314 IDENTIFIER_POINTER (DECL_NAME (edge->callee->decl)));
319 static struct cgraph_node *queue = NULL;
321 /* Notify finalize_compilation_unit that given node is reachable
324 cgraph_mark_needed_node (node, needed)
325 struct cgraph_node *node;
330 if (DECL_SAVED_TREE (node->decl))
331 announce_function (node->decl);
334 if (!node->reachable)
337 if (DECL_SAVED_TREE (node->decl))
345 /* Analyze the whole compilation unit once it is parsed completely. */
348 cgraph_finalize_compilation_unit ()
350 struct cgraph_node *node;
351 struct cgraph_edge *edge;
353 /* Collect entry points to the unit. */
356 fprintf (stderr, "\n\nUnit entry points:");
358 for (node = cgraph_nodes; node; node = node->next)
360 tree decl = node->decl;
362 if (!DECL_SAVED_TREE (decl))
364 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
365 || (DECL_ASSEMBLER_NAME_SET_P (decl)
366 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
368 cgraph_mark_needed_node (node, 1);
372 /* Propagate reachability flag and lower representation of all reachable
373 functions. In the future, lowering will introduce new functions and
374 new entry points on the way (by template instantiation and virtual
375 method table generation for instance). */
378 tree decl = queue->decl;
382 if (node->lowered || !node->reachable || !DECL_SAVED_TREE (decl))
385 /* At the moment frontend automatically emits all nested functions. */
388 struct cgraph_node *node2;
390 for (node2 = node->nested; node2; node2 = node2->next_nested)
391 if (!node2->reachable)
392 cgraph_mark_needed_node (node2, 0);
395 if (lang_hooks.callgraph.lower_function)
396 (*lang_hooks.callgraph.lower_function) (decl);
397 /* First kill forward declaration so reverse inling works properly. */
398 cgraph_create_edges (decl, DECL_SAVED_TREE (decl));
400 for (edge = node->callees; edge; edge = edge->next_callee)
402 if (!edge->callee->reachable)
403 cgraph_mark_needed_node (edge->callee, 0);
405 node->lowered = true;
408 fprintf (stderr, "\n\nReclaiming functions:");
410 for (node = cgraph_nodes; node; node = node->next)
412 tree decl = node->decl;
414 if (!node->reachable && DECL_SAVED_TREE (decl))
416 DECL_SAVED_TREE (decl) = NULL;
417 announce_function (decl);
423 /* Figure out what functions we want to assemble. */
426 cgraph_mark_functions_to_output ()
428 struct cgraph_node *node;
430 /* Figure out functions we want to assemble. */
431 for (node = cgraph_nodes; node; node = node->next)
433 tree decl = node->decl;
435 if (DECL_SAVED_TREE (decl)
437 || (DECL_UNINLINABLE (decl) && node->reachable)
438 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
439 && !TREE_ASM_WRITTEN (decl) && !node->origin
440 && !DECL_EXTERNAL (decl))
445 /* Expand function specified by NODE. */
447 cgraph_expand_function (node)
448 struct cgraph_node *node;
450 tree decl = node->decl;
452 announce_function (decl);
453 if (flag_inline_trees)
454 optimize_inline_calls (decl);
455 (*lang_hooks.callgraph.expand_function) (decl);
456 if (DECL_UNINLINABLE (decl))
457 DECL_SAVED_TREE (decl) = NULL;
458 current_function_decl = NULL;
462 /* Expand all functions that must be output.
464 Attempt to topologically sort the nodes so function is output when
465 all called functions are already assembled to allow data to be propagated
466 accross the callgraph. Use stack to get smaller distance between function
467 and it's callees (later we may use more sophisticated algorithm for
468 function reordering, we will likely want to use subsections to make output
469 functions to appear in top-down order, not bottom-up they are assembled). */
472 cgraph_expand_functions ()
474 struct cgraph_node *node, *node2;
475 struct cgraph_node **stack =
476 xcalloc (sizeof (struct cgraph_node *), cgraph_n_nodes);
477 struct cgraph_node **order =
478 xcalloc (sizeof (struct cgraph_node *), cgraph_n_nodes);
481 struct cgraph_edge *edge, last;
484 cgraph_mark_functions_to_output ();
486 /* We have to deal with cycles nicely, so use depth first traversal
487 algorithm. Ignore the fact that some functions won't need to be output
488 and put them into order as well, so we get dependencies right trought inlined
490 for (node = cgraph_nodes; node; node = node->next)
492 for (node = cgraph_nodes; node; node = node->next)
493 if (node->output && !node->aux)
499 node->aux = node->callers;
502 while (node2->aux != &last)
505 if (edge->next_caller)
506 node2->aux = edge->next_caller;
509 if (!edge->caller->aux)
511 if (!edge->caller->callers)
512 edge->caller->aux = &last;
514 edge->caller->aux = edge->caller->callers;
515 stack[stack_size++] = node2;
516 node2 = edge->caller;
520 if (node2->aux == &last)
522 order[order_pos++] = node2;
524 node2 = stack[--stack_size];
530 for (i = order_pos - 1; i >=0; i--)
535 if (!node->reachable)
538 cgraph_expand_function (node);
545 /* Perform simple optimizations based on callgraph. */
550 struct cgraph_node *node;
552 struct cgraph_edge *edge;
555 fprintf (stderr, "\n\nAssembling functions:");
557 /* Output everything.
558 ??? Our inline heuristic may decide to not inline functions previously
559 marked as inlinable thus adding new function bodies that must be output.
560 Later we should move all inlining decisions to callgraph code to make
562 cgraph_expand_functions ();
566 for (node = cgraph_nodes; node; node = node->next)
571 for (edge = node->callees; edge; edge = edge->next_callee)
572 if (!edge->callee->needed)
573 changed = edge->callee->needed = true;
576 cgraph_expand_functions ();