* config/c4x/c4x.h (INITIALIZE_TRAMPOLINE): Replace 'tramp' with 'TRAMP' in
[official-gcc.git] / gcc / cgraph.c
blob5e44b55d8d14761afa4e99432a31080c04dffb91
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004 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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 /* This file contains basic routines manipulating call graph and variable pool
24 The callgraph:
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
28 sharing.
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node (in
32 contrast to tree DECL nodes where we can have multiple nodes for each
33 function).
35 The mapping from declarations to call-graph nodes is done using hash table
36 based on DECL_ASSEMBLER_NAME, so it is essential for assembler name to
37 not change once the declaration is inserted into the call-graph.
38 The call-graph nodes are created lazily using cgraph_node function when
39 called for unknown declaration.
41 When built, there is one edge for each direct call. It is possible that
42 the reference will be later optimized out. The call-graph is built
43 conservatively in order to make conservative data flow analysis possible.
45 The callgraph at the moment does not represent indirect calls or calls
46 from other compilation unit. Flag NEEDED is set for each node that may
47 be accessed in such a invisible way and it shall be considered an
48 entry point to the callgraph.
50 Intraprocedural information:
52 Callgraph is place to store data needed for intraprocedural optimization.
53 All data structures are divided into three components: local_info that
54 is produced while analyzing the function, global_info that is result
55 of global walking of the callgraph on the end of compilation and
56 rtl_info used by RTL backend to propagate data from already compiled
57 functions to their callers.
59 Inlining plans:
61 The function inlining information is decided in advance and maintained
62 in the callgraph as so called inline plan.
63 For each inlined call, the callee's node is cloned to represent the
64 new function copy produced by inlininer.
65 Each inlined call gets a unique corresponding clone node of the callee
66 and the data structure is updated while inlining is performed, so
67 the clones are eliminated and their callee edges redirected to the
68 caller.
70 Each edge has "inline_failed" field. When the field is set to NULL,
71 the call will be inlined. When it is non-NULL it contains a reason
72 why inlining wasn't performed.
75 The varpool data structure:
77 Varpool is used to maintain variables in similar manner as call-graph
78 is used for functions. Most of the API is symmetric replacing cgraph
79 function prefix by cgraph_varpool */
82 #include "config.h"
83 #include "system.h"
84 #include "coretypes.h"
85 #include "tm.h"
86 #include "tree.h"
87 #include "langhooks.h"
88 #include "hashtab.h"
89 #include "toplev.h"
90 #include "flags.h"
91 #include "ggc.h"
92 #include "debug.h"
93 #include "target.h"
94 #include "cgraph.h"
95 #include "varray.h"
96 #include "output.h"
97 #include "intl.h"
99 /* Hash table used to convert declarations into nodes. */
100 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
102 /* We destructively update the callgraph during inlining, thus we need to
103 keep a separate table with information on whether inlining happened.
104 ??? Do this with a bit in the DECL instead of a hash table. */
105 htab_t cgraph_inline_hash;
107 /* The linked list of cgraph nodes. */
108 struct cgraph_node *cgraph_nodes;
110 /* Queue of cgraph nodes scheduled to be lowered. */
111 struct cgraph_node *cgraph_nodes_queue;
113 /* Number of nodes in existence. */
114 int cgraph_n_nodes;
116 /* Maximal uid used in cgraph nodes. */
117 int cgraph_max_uid;
119 /* Set when whole unit has been analyzed so we can access global info. */
120 bool cgraph_global_info_ready = false;
122 /* Hash table used to convert declarations into nodes. */
123 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
125 /* Queue of cgraph nodes scheduled to be lowered and output. */
126 struct cgraph_varpool_node *cgraph_varpool_nodes_queue;
128 /* Number of nodes in existence. */
129 int cgraph_varpool_n_nodes;
131 /* The linked list of cgraph varpool nodes. */
132 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_nodes;
134 static hashval_t hash_node (const void *);
135 static int eq_node (const void *, const void *);
137 /* Returns a hash code for P. */
139 static hashval_t
140 hash_node (const void *p)
142 return ((hashval_t) DECL_UID (((struct cgraph_node *) p)->decl));
145 /* Returns nonzero if P1 and P2 are equal. */
147 static int
148 eq_node (const void *p1, const void *p2)
150 return (DECL_UID (((struct cgraph_node *) p1)->decl) == (unsigned int)p2);
153 /* Allocate new callgraph node and insert it into basic data structures. */
154 static struct cgraph_node *
155 cgraph_create_node (void)
157 struct cgraph_node *node;
159 node = ggc_alloc_cleared (sizeof (*node));
160 node->next = cgraph_nodes;
161 node->uid = cgraph_max_uid++;
162 if (cgraph_nodes)
163 cgraph_nodes->previous = node;
164 node->previous = NULL;
165 cgraph_nodes = node;
166 cgraph_n_nodes++;
167 return node;
170 /* Return cgraph node assigned to DECL. Create new one when needed. */
171 struct cgraph_node *
172 cgraph_node (tree decl)
174 struct cgraph_node *node;
175 struct cgraph_node **slot;
177 if (TREE_CODE (decl) != FUNCTION_DECL)
178 abort ();
180 if (!cgraph_hash)
181 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
183 slot = (struct cgraph_node **)
184 htab_find_slot_with_hash (cgraph_hash,
185 (void *)DECL_UID (decl),
186 (hashval_t)DECL_UID (decl),
187 INSERT);
188 if (*slot)
189 return *slot;
191 node = cgraph_create_node ();
192 node->decl = decl;
193 *slot = node;
194 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
196 node->origin = cgraph_node (DECL_CONTEXT (decl));
197 node->next_nested = node->origin->nested;
198 node->origin->nested = node;
200 return node;
203 /* Return callgraph edge representing CALL_EXPR. */
204 struct cgraph_edge *
205 cgraph_edge (struct cgraph_node *node, tree call_expr)
207 struct cgraph_edge *e;
209 /* This loop may turn out to be performance problem. In such case adding
210 hashtables into call nodes with very many edges is probably best
211 solution. It is not good idea to add pointer into CALL_EXPR itself
212 because we want to make possible having multiple cgraph nodes representing
213 different clones of the same body before the body is actually cloned. */
214 for (e = node->callees; e; e= e->next_callee)
215 if (e->call_expr == call_expr)
216 break;
217 return e;
220 /* Create edge from CALLER to CALLEE in the cgraph. */
222 struct cgraph_edge *
223 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
224 tree call_expr)
226 struct cgraph_edge *edge = ggc_alloc (sizeof (struct cgraph_edge));
227 #ifdef ENABLE_CHECKING
228 struct cgraph_edge *e;
230 for (e = caller->callees; e; e = e->next_callee)
231 if (e->call_expr == call_expr)
232 abort ();
233 #endif
235 if (TREE_CODE (call_expr) != CALL_EXPR)
236 abort ();
238 if (!DECL_SAVED_TREE (callee->decl))
239 edge->inline_failed = N_("function body not available");
240 else if (callee->local.redefined_extern_inline)
241 edge->inline_failed = N_("redefined extern inline functions are not "
242 "considered for inlining");
243 else if (callee->local.inlinable)
244 edge->inline_failed = N_("function not considered for inlining");
245 else
246 edge->inline_failed = N_("function not inlinable");
248 edge->aux = NULL;
250 edge->caller = caller;
251 edge->callee = callee;
252 edge->call_expr = call_expr;
253 edge->next_caller = callee->callers;
254 edge->next_callee = caller->callees;
255 caller->callees = edge;
256 callee->callers = edge;
257 return edge;
260 /* Remove the edge E the cgraph. */
262 void
263 cgraph_remove_edge (struct cgraph_edge *e)
265 struct cgraph_edge **edge, **edge2;
267 for (edge = &e->callee->callers; *edge && *edge != e;
268 edge = &((*edge)->next_caller))
269 continue;
270 if (!*edge)
271 abort ();
272 *edge = (*edge)->next_caller;
273 for (edge2 = &e->caller->callees; *edge2 && *edge2 != e;
274 edge2 = &(*edge2)->next_callee)
275 continue;
276 if (!*edge2)
277 abort ();
278 *edge2 = (*edge2)->next_callee;
281 /* Redirect callee of E to N. The function does not update underlying
282 call expression. */
284 void
285 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
287 struct cgraph_edge **edge;
289 for (edge = &e->callee->callers; *edge && *edge != e;
290 edge = &((*edge)->next_caller))
291 continue;
292 if (!*edge)
293 abort ();
294 *edge = (*edge)->next_caller;
295 e->callee = n;
296 e->next_caller = n->callers;
297 n->callers = e;
300 /* Remove the node from cgraph. */
302 void
303 cgraph_remove_node (struct cgraph_node *node)
305 void **slot;
306 bool check_dead = 1;
308 while (node->callers)
309 cgraph_remove_edge (node->callers);
310 while (node->callees)
311 cgraph_remove_edge (node->callees);
312 while (node->nested)
313 cgraph_remove_node (node->nested);
314 if (node->origin)
316 struct cgraph_node **node2 = &node->origin->nested;
318 while (*node2 != node)
319 node2 = &(*node2)->next_nested;
320 *node2 = node->next_nested;
322 if (node->previous)
323 node->previous->next = node->next;
324 else
325 cgraph_nodes = node->next;
326 if (node->next)
327 node->next->previous = node->previous;
328 slot =
329 htab_find_slot_with_hash (cgraph_hash,
330 (void *)DECL_UID (node->decl),
331 (hashval_t)DECL_UID (node->decl),
332 NO_INSERT);
333 if (*slot == node)
335 if (node->next_clone)
336 *slot = node->next_clone;
337 else
339 htab_clear_slot (cgraph_hash, slot);
340 if (!dump_enabled_p (TDI_all))
342 DECL_SAVED_TREE (node->decl) = NULL;
343 DECL_STRUCT_FUNCTION (node->decl) = NULL;
345 check_dead = false;
348 else
350 struct cgraph_node *n;
352 for (n = *slot; n->next_clone != node; n = n->next_clone)
353 continue;
354 n->next_clone = node->next_clone;
357 /* Work out whether we still need a function body (either there is inline
358 clone or there is out of line function whose body is not written). */
359 if (check_dead && flag_unit_at_a_time)
361 struct cgraph_node *n;
363 for (n = *slot; n; n = n->next_clone)
364 if (n->global.inlined_to
365 || (!n->global.inlined_to
366 && !TREE_ASM_WRITTEN (n->decl) && !DECL_EXTERNAL (n->decl)))
367 break;
368 if (!n && !dump_enabled_p (TDI_all))
370 DECL_SAVED_TREE (node->decl) = NULL;
371 DECL_STRUCT_FUNCTION (node->decl) = NULL;
374 cgraph_n_nodes--;
375 /* Do not free the structure itself so the walk over chain can continue. */
378 /* Notify finalize_compilation_unit that given node is reachable. */
380 void
381 cgraph_mark_reachable_node (struct cgraph_node *node)
383 if (!node->reachable && node->local.finalized)
385 notice_global_symbol (node->decl);
386 node->reachable = 1;
388 node->next_needed = cgraph_nodes_queue;
389 cgraph_nodes_queue = node;
393 /* Likewise indicate that a node is needed, i.e. reachable via some
394 external means. */
396 void
397 cgraph_mark_needed_node (struct cgraph_node *node)
399 node->needed = 1;
400 cgraph_mark_reachable_node (node);
403 /* Return true when CALLER_DECL calls CALLEE_DECL. */
405 bool
406 cgraph_calls_p (tree caller_decl, tree callee_decl)
408 struct cgraph_node *caller = cgraph_node (caller_decl);
409 struct cgraph_node *callee = cgraph_node (callee_decl);
410 struct cgraph_edge *edge;
412 for (edge = callee->callers; edge && (edge)->caller != caller;
413 edge = (edge->next_caller))
414 continue;
415 return edge != NULL;
418 /* Return local info for the compiled function. */
420 struct cgraph_local_info *
421 cgraph_local_info (tree decl)
423 struct cgraph_node *node;
424 if (TREE_CODE (decl) != FUNCTION_DECL)
425 abort ();
426 node = cgraph_node (decl);
427 return &node->local;
430 /* Return local info for the compiled function. */
432 struct cgraph_global_info *
433 cgraph_global_info (tree decl)
435 struct cgraph_node *node;
436 if (TREE_CODE (decl) != FUNCTION_DECL || !cgraph_global_info_ready)
437 abort ();
438 node = cgraph_node (decl);
439 return &node->global;
442 /* Return local info for the compiled function. */
444 struct cgraph_rtl_info *
445 cgraph_rtl_info (tree decl)
447 struct cgraph_node *node;
448 if (TREE_CODE (decl) != FUNCTION_DECL)
449 abort ();
450 node = cgraph_node (decl);
451 if (decl != current_function_decl
452 && !TREE_ASM_WRITTEN (node->decl))
453 return NULL;
454 return &node->rtl;
457 /* Return name of the node used in debug output. */
458 const char *
459 cgraph_node_name (struct cgraph_node *node)
461 return lang_hooks.decl_printable_name (node->decl, 2);
464 /* Dump given cgraph node. */
465 void
466 dump_cgraph_node (FILE *f, struct cgraph_node *node)
468 struct cgraph_edge *edge;
469 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
470 if (node->global.inlined_to)
471 fprintf (f, " (inline copy in %s/%i)",
472 cgraph_node_name (node->global.inlined_to),
473 node->global.inlined_to->uid);
474 if (node->local.self_insns)
475 fprintf (f, " %i insns", node->local.self_insns);
476 if (node->global.insns && node->global.insns != node->local.self_insns)
477 fprintf (f, " (%i after inlining)", node->global.insns);
478 if (node->origin)
479 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
480 if (node->needed)
481 fprintf (f, " needed");
482 else if (node->reachable)
483 fprintf (f, " reachable");
484 if (DECL_SAVED_TREE (node->decl))
485 fprintf (f, " tree");
486 if (node->output)
487 fprintf (f, " output");
489 if (node->local.local)
490 fprintf (f, " local");
491 if (node->local.disregard_inline_limits)
492 fprintf (f, " always_inline");
493 else if (node->local.inlinable)
494 fprintf (f, " inlinable");
495 if (TREE_ASM_WRITTEN (node->decl))
496 fprintf (f, " asm_written");
498 fprintf (f, "\n called by: ");
499 for (edge = node->callers; edge; edge = edge->next_caller)
501 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
502 edge->caller->uid);
503 if (!edge->inline_failed)
504 fprintf(f, "(inlined) ");
507 fprintf (f, "\n calls: ");
508 for (edge = node->callees; edge; edge = edge->next_callee)
510 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
511 edge->callee->uid);
512 if (!edge->inline_failed)
513 fprintf(f, "(inlined) ");
515 fprintf (f, "\n");
518 /* Dump the callgraph. */
520 void
521 dump_cgraph (FILE *f)
523 struct cgraph_node *node;
525 fprintf (f, "callgraph:\n\n");
526 for (node = cgraph_nodes; node; node = node->next)
527 dump_cgraph_node (f, node);
530 /* Returns a hash code for P. */
532 static hashval_t
533 cgraph_varpool_hash_node (const void *p)
535 return ((hashval_t) DECL_UID (((struct cgraph_varpool_node *) p)->decl));
538 /* Returns nonzero if P1 and P2 are equal. */
540 static int
541 eq_cgraph_varpool_node (const void *p1, const void *p2)
543 return (DECL_UID (((struct cgraph_varpool_node *) p1)->decl)
544 == (unsigned int) p2);
548 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
549 struct cgraph_varpool_node *
550 cgraph_varpool_node (tree decl)
552 struct cgraph_varpool_node *node;
553 struct cgraph_varpool_node **slot;
555 if (!DECL_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
556 abort ();
558 if (!cgraph_varpool_hash)
559 cgraph_varpool_hash = htab_create_ggc (10, cgraph_varpool_hash_node,
560 eq_cgraph_varpool_node, NULL);
561 slot = (struct cgraph_varpool_node **)
562 htab_find_slot_with_hash (cgraph_varpool_hash,
563 (void *)DECL_UID (decl),
564 (hashval_t)DECL_UID (decl),
565 INSERT);
566 if (*slot)
567 return *slot;
568 node = ggc_alloc_cleared (sizeof (*node));
569 node->decl = decl;
570 cgraph_varpool_n_nodes++;
571 cgraph_varpool_nodes = node;
572 *slot = node;
573 return node;
576 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
577 void
578 change_decl_assembler_name (tree decl, tree name)
580 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
582 SET_DECL_ASSEMBLER_NAME (decl, name);
583 return;
585 if (name == DECL_ASSEMBLER_NAME (decl))
586 return;
588 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
589 && DECL_RTL_SET_P (decl))
590 warning ("%D renamed after being referenced in assembly", decl);
592 SET_DECL_ASSEMBLER_NAME (decl, name);
595 /* Notify finalize_compilation_unit that given node is reachable
596 or needed. */
597 void
598 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
600 if (!node->needed && node->finalized)
602 node->next_needed = cgraph_varpool_nodes_queue;
603 cgraph_varpool_nodes_queue = node;
604 notice_global_symbol (node->decl);
606 node->needed = 1;
609 void
610 cgraph_varpool_finalize_decl (tree decl)
612 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
614 /* The first declaration of a variable that comes through this function
615 decides whether it is global (in C, has external linkage)
616 or local (in C, has internal linkage). So do nothing more
617 if this function has already run. */
618 if (node->finalized)
619 return;
620 if (node->needed)
622 node->next_needed = cgraph_varpool_nodes_queue;
623 cgraph_varpool_nodes_queue = node;
624 notice_global_symbol (decl);
626 node->finalized = true;
628 if (/* Externally visible variables must be output. The exception are
629 COMDAT functions that must be output only when they are needed. */
630 (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
631 /* Function whose name is output to the assembler file must be produced.
632 It is possible to assemble the name later after finalizing the function
633 and the fact is noticed in assemble_name then. */
634 || (DECL_ASSEMBLER_NAME_SET_P (decl)
635 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
637 cgraph_varpool_mark_needed_node (node);
641 bool
642 cgraph_varpool_assemble_pending_decls (void)
644 bool changed = false;
646 while (cgraph_varpool_nodes_queue)
648 tree decl = cgraph_varpool_nodes_queue->decl;
649 struct cgraph_varpool_node *node = cgraph_varpool_nodes_queue;
651 cgraph_varpool_nodes_queue = cgraph_varpool_nodes_queue->next_needed;
652 if (!TREE_ASM_WRITTEN (decl))
654 assemble_variable (decl, 0, 1, 0);
655 changed = true;
657 node->next_needed = NULL;
659 return changed;
662 /* Return true when the DECL can possibly be inlined. */
663 bool
664 cgraph_function_possibly_inlined_p (tree decl)
666 if (!cgraph_global_info_ready)
667 return (DECL_INLINE (decl) && !flag_really_no_inline);
668 if (!cgraph_inline_hash)
669 return false;
670 return (htab_find_slot (cgraph_inline_hash, decl, NO_INSERT) != NULL);
673 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
674 struct cgraph_edge *
675 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n, tree call_expr)
677 struct cgraph_edge *new = cgraph_create_edge (n, e->callee, call_expr);
679 new->inline_failed = e->inline_failed;
680 return new;
683 /* Create node representing clone of N. */
684 struct cgraph_node *
685 cgraph_clone_node (struct cgraph_node *n)
687 struct cgraph_node *new = cgraph_create_node ();
688 struct cgraph_edge *e;
690 new->decl = n->decl;
691 new->origin = n->origin;
692 if (new->origin)
694 new->next_nested = new->origin->nested;
695 new->origin->nested = new;
697 new->analyzed = n->analyzed;
698 new->local = n->local;
699 new->global = n->global;
700 new->rtl = n->rtl;
702 for (e = n->callees;e; e=e->next_callee)
703 cgraph_clone_edge (e, new, e->call_expr);
705 new->next_clone = n->next_clone;
706 n->next_clone = new;
708 return new;
710 #include "gt-cgraph.h"