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 /* Expand all functions that must be output. */
425 #define NPREDECESORS(node) ((size_t) (node)->aux)
426 #define SET_NPREDECESORS(node, n) ((node)->aux = (void *) (size_t) (n))
428 /* Figure out what functions we want to assemble. */
431 cgraph_mark_functions_to_output ()
433 struct cgraph_node *node;
435 /* Figure out functions we want to assemble. */
436 for (node = cgraph_nodes; node; node = node->next)
438 tree decl = node->decl;
440 if (DECL_SAVED_TREE (decl)
442 || (DECL_UNINLINABLE (decl) && node->reachable)
443 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
444 && !TREE_ASM_WRITTEN (decl) && !node->origin
445 && !DECL_EXTERNAL (decl))
450 /* Expand function specified by NODE. */
452 cgraph_expand_function (node)
453 struct cgraph_node *node;
455 tree decl = node->decl;
457 announce_function (decl);
458 if (flag_inline_trees)
459 optimize_inline_calls (decl);
460 (*lang_hooks.callgraph.expand_function) (decl);
461 if (DECL_UNINLINABLE (decl))
462 DECL_SAVED_TREE (decl) = NULL;
463 current_function_decl = NULL;
467 /* Expand all functions that must be output.
469 Attempt to topologically sort the nodes so function is output when
470 all called functions are already assembled to allow data to be propagated
471 accross the callgraph. Use stack to get smaller distance between function
472 and it's callees (later we may use more sophisticated algorithm for
473 function reordering, we will likely want to use subsections to make output
474 functions to appear in top-down order, not bottom-up they are assembled). */
477 cgraph_expand_functions ()
479 struct cgraph_node *node;
480 struct cgraph_node **stack =
481 xcalloc (sizeof (struct cgraph_node *), cgraph_n_nodes);
483 struct cgraph_edge *edge;
485 cgraph_mark_functions_to_output ();
487 for (node = cgraph_nodes; node; node = node->next)
491 for (edge = node->callees; edge; edge = edge->next_callee)
492 if (edge->callee->output)
494 SET_NPREDECESORS (node, n);
496 stack[stack_size++] = node;
500 struct cgraph_node *minnode;
503 node = stack[--stack_size];
506 for (edge = node->callers; edge; edge = edge->next_caller)
507 if (edge->caller->output)
509 SET_NPREDECESORS (edge->caller,
510 NPREDECESORS (edge->caller) - 1);
511 if (!NPREDECESORS (edge->caller))
512 stack[stack_size++] = edge->caller;
514 if (!node->reachable)
516 cgraph_expand_function (node);
519 /* We found cycle. Break it and try again. */
520 for (node = cgraph_nodes; node; node = node->next)
523 || NPREDECESORS (minnode) > NPREDECESORS (node)))
527 stack[stack_size++] = minnode;
531 /* Perform simple optimizations based on callgraph. */
536 struct cgraph_node *node;
538 struct cgraph_edge *edge;
541 fprintf (stderr, "\n\nAssembling functions:");
543 /* Output everything.
544 ??? Our inline heuristic may decide to not inline functions previously
545 marked as inlinable thus adding new function bodies that must be output.
546 Later we should move all inlining decisions to callgraph code to make
548 cgraph_expand_functions ();
552 for (node = cgraph_nodes; node; node = node->next)
557 for (edge = node->callees; edge; edge = edge->next_callee)
558 if (!edge->callee->needed)
559 changed = edge->callee->needed = true;
562 cgraph_expand_functions ();