OSDN Git Service

* cgraph.c (hash_node, eq_node, cgraph_node, cgraph_remove_node)
[pf3gnuchains/gcc-fork.git] / gcc / cgraph.c
1 /* Callgraph handling code.
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22 /*  This file contains basic routines manipulating call graph and variable pool
23   
24 The callgraph:
25
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.
29
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).
34
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.
40     
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.
44
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.
49
50     Intraprocedural information:
51
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.
58
59     Inlining plans:
60
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. 
69
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.
73
74
75 The varpool data structure:
76
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  */
80
81
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"
98
99 /* Hash table used to convert declarations into nodes.  */
100 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
101
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;
106
107 /* The linked list of cgraph nodes.  */
108 struct cgraph_node *cgraph_nodes;
109
110 /* Queue of cgraph nodes scheduled to be lowered.  */
111 struct cgraph_node *cgraph_nodes_queue;
112
113 /* Number of nodes in existence.  */
114 int cgraph_n_nodes;
115
116 /* Maximal uid used in cgraph nodes.  */
117 int cgraph_max_uid;
118
119 /* Set when whole unit has been analyzed so we can access global info.  */
120 bool cgraph_global_info_ready = false;
121
122 /* Hash table used to convert declarations into nodes.  */
123 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
124
125 /* Queue of cgraph nodes scheduled to be lowered and output.  */
126 struct cgraph_varpool_node *cgraph_varpool_nodes_queue;
127
128 /* Number of nodes in existence.  */
129 int cgraph_varpool_n_nodes;
130
131 /* The linked list of cgraph varpool nodes.  */
132 static GTY(())  struct cgraph_varpool_node *cgraph_varpool_nodes;
133
134 static hashval_t hash_node (const void *);
135 static int eq_node (const void *, const void *);
136
137 /* Returns a hash code for P.  */
138
139 static hashval_t
140 hash_node (const void *p)
141 {
142   return htab_hash_pointer (((struct cgraph_node *) p)->decl);
143 }
144
145 /* Returns nonzero if P1 and P2 are equal.  */
146
147 static int
148 eq_node (const void *p1, const void *p2)
149 {
150   return (void *)((struct cgraph_node *) p1)->decl == p2;
151 }
152
153 /* Allocate new callgraph node and insert it into basic data structures.  */
154 static struct cgraph_node *
155 cgraph_create_node (void)
156 {
157   struct cgraph_node *node;
158
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;
168 }
169
170 /* Return cgraph node assigned to DECL.  Create new one when needed.  */
171 struct cgraph_node *
172 cgraph_node (tree decl)
173 {
174   struct cgraph_node *node;
175   struct cgraph_node **slot;
176
177   if (TREE_CODE (decl) != FUNCTION_DECL)
178     abort ();
179
180   if (!cgraph_hash)
181     cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
182
183   slot = (struct cgraph_node **)
184     htab_find_slot_with_hash (cgraph_hash, decl,
185                               htab_hash_pointer (decl), INSERT);
186   if (*slot)
187     return *slot;
188
189   node = cgraph_create_node ();
190   node->decl = decl;
191   *slot = node;
192   if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
193     {
194       node->origin = cgraph_node (DECL_CONTEXT (decl));
195       node->next_nested = node->origin->nested;
196       node->origin->nested = node;
197     }
198   return node;
199 }
200
201 /* Return callgraph edge representing CALL_EXPR.  */
202 struct cgraph_edge *
203 cgraph_edge (struct cgraph_node *node, tree call_expr)
204 {
205   struct cgraph_edge *e;
206
207   /* This loop may turn out to be performance problem.  In such case adding
208      hashtables into call nodes with very many edges is probably best
209      solution.  It is not good idea to add pointer into CALL_EXPR itself
210      because we want to make possible having multiple cgraph nodes representing
211      different clones of the same body before the body is actually cloned.  */
212   for (e = node->callees; e; e= e->next_callee)
213     if (e->call_expr == call_expr)
214       break;
215   return e;
216 }
217
218 /* Create edge from CALLER to CALLEE in the cgraph.  */
219
220 struct cgraph_edge *
221 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
222                     tree call_expr)
223 {
224   struct cgraph_edge *edge = ggc_alloc (sizeof (struct cgraph_edge));
225 #ifdef ENABLE_CHECKING
226   struct cgraph_edge *e;
227
228   for (e = caller->callees; e; e = e->next_callee)
229     if (e->call_expr == call_expr)
230       abort ();
231 #endif
232
233   if (TREE_CODE (call_expr) != CALL_EXPR)
234     abort ();
235
236   if (!DECL_SAVED_TREE (callee->decl))
237     edge->inline_failed = N_("function body not available");
238   else if (callee->local.redefined_extern_inline)
239     edge->inline_failed = N_("redefined extern inline functions are not "
240                              "considered for inlining");
241   else if (callee->local.inlinable)
242     edge->inline_failed = N_("function not considered for inlining");
243   else
244     edge->inline_failed = N_("function not inlinable");
245
246   edge->aux = NULL;
247
248   edge->caller = caller;
249   edge->callee = callee;
250   edge->call_expr = call_expr;
251   edge->next_caller = callee->callers;
252   edge->next_callee = caller->callees;
253   caller->callees = edge;
254   callee->callers = edge;
255   return edge;
256 }
257
258 /* Remove the edge E the cgraph.  */
259
260 void
261 cgraph_remove_edge (struct cgraph_edge *e)
262 {
263   struct cgraph_edge **edge, **edge2;
264
265   for (edge = &e->callee->callers; *edge && *edge != e;
266        edge = &((*edge)->next_caller))
267     continue;
268   if (!*edge)
269     abort ();
270   *edge = (*edge)->next_caller;
271   for (edge2 = &e->caller->callees; *edge2 && *edge2 != e;
272        edge2 = &(*edge2)->next_callee)
273     continue;
274   if (!*edge2)
275     abort ();
276   *edge2 = (*edge2)->next_callee;
277 }
278
279 /* Redirect callee of E to N.  The function does not update underlying
280    call expression.  */
281
282 void
283 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
284 {
285   struct cgraph_edge **edge;
286
287   for (edge = &e->callee->callers; *edge && *edge != e;
288        edge = &((*edge)->next_caller))
289     continue;
290   if (!*edge)
291     abort ();
292   *edge = (*edge)->next_caller;
293   e->callee = n;
294   e->next_caller = n->callers;
295   n->callers = e;
296 }
297
298 /* Remove the node from cgraph.  */
299
300 void
301 cgraph_remove_node (struct cgraph_node *node)
302 {
303   void **slot;
304   bool check_dead = 1;
305
306   while (node->callers)
307     cgraph_remove_edge (node->callers);
308   while (node->callees)
309     cgraph_remove_edge (node->callees);
310   while (node->nested)
311     cgraph_remove_node (node->nested);
312   if (node->origin)
313     {
314       struct cgraph_node **node2 = &node->origin->nested;
315
316       while (*node2 != node)
317         node2 = &(*node2)->next_nested;
318       *node2 = node->next_nested;
319     }
320   if (node->previous)
321     node->previous->next = node->next;
322   else
323     cgraph_nodes = node->next;
324   if (node->next)
325     node->next->previous = node->previous;
326   slot = 
327     htab_find_slot_with_hash (cgraph_hash, node->decl,
328                               htab_hash_pointer (node->decl), NO_INSERT);
329   if (*slot == node)
330     {
331       if (node->next_clone)
332         *slot = node->next_clone;
333       else
334         {
335           htab_clear_slot (cgraph_hash, slot);
336           if (!dump_enabled_p (TDI_all))
337             {
338               DECL_SAVED_TREE (node->decl) = NULL;
339               DECL_STRUCT_FUNCTION (node->decl) = NULL;
340             }
341           check_dead = false;
342         }
343     }
344   else
345     {
346       struct cgraph_node *n;
347
348       for (n = *slot; n->next_clone != node; n = n->next_clone)
349         continue;
350       n->next_clone = node->next_clone;
351     }
352
353   /* Work out whether we still need a function body (either there is inline
354      clone or there is out of line function whose body is not written).  */
355   if (check_dead && flag_unit_at_a_time)
356     {
357       struct cgraph_node *n;
358
359       for (n = *slot; n; n = n->next_clone)
360         if (n->global.inlined_to
361             || (!n->global.inlined_to
362                 && !TREE_ASM_WRITTEN (n->decl) && !DECL_EXTERNAL (n->decl)))
363           break;
364       if (!n && !dump_enabled_p (TDI_all))
365         {
366           DECL_SAVED_TREE (node->decl) = NULL;
367           DECL_STRUCT_FUNCTION (node->decl) = NULL;
368         }
369     }
370   cgraph_n_nodes--;
371   /* Do not free the structure itself so the walk over chain can continue.  */
372 }
373
374 /* Notify finalize_compilation_unit that given node is reachable.  */
375
376 void
377 cgraph_mark_reachable_node (struct cgraph_node *node)
378 {
379   if (!node->reachable && node->local.finalized)
380     {
381       notice_global_symbol (node->decl);
382       node->reachable = 1;
383
384       node->next_needed = cgraph_nodes_queue;
385       cgraph_nodes_queue = node;
386     }
387 }
388
389 /* Likewise indicate that a node is needed, i.e. reachable via some
390    external means.  */
391
392 void
393 cgraph_mark_needed_node (struct cgraph_node *node)
394 {
395   node->needed = 1;
396   cgraph_mark_reachable_node (node);
397 }
398
399 /* Return true when CALLER_DECL calls CALLEE_DECL.  */
400
401 bool
402 cgraph_calls_p (tree caller_decl, tree callee_decl)
403 {
404   struct cgraph_node *caller = cgraph_node (caller_decl);
405   struct cgraph_node *callee = cgraph_node (callee_decl);
406   struct cgraph_edge *edge;
407
408   for (edge = callee->callers; edge && (edge)->caller != caller;
409        edge = (edge->next_caller))
410     continue;
411   return edge != NULL;
412 }
413
414 /* Return local info for the compiled function.  */
415
416 struct cgraph_local_info *
417 cgraph_local_info (tree decl)
418 {
419   struct cgraph_node *node;
420   if (TREE_CODE (decl) != FUNCTION_DECL)
421     abort ();
422   node = cgraph_node (decl);
423   return &node->local;
424 }
425
426 /* Return local info for the compiled function.  */
427
428 struct cgraph_global_info *
429 cgraph_global_info (tree decl)
430 {
431   struct cgraph_node *node;
432   if (TREE_CODE (decl) != FUNCTION_DECL || !cgraph_global_info_ready)
433     abort ();
434   node = cgraph_node (decl);
435   return &node->global;
436 }
437
438 /* Return local info for the compiled function.  */
439
440 struct cgraph_rtl_info *
441 cgraph_rtl_info (tree decl)
442 {
443   struct cgraph_node *node;
444   if (TREE_CODE (decl) != FUNCTION_DECL)
445     abort ();
446   node = cgraph_node (decl);
447   if (decl != current_function_decl
448       && !TREE_ASM_WRITTEN (node->decl))
449     return NULL;
450   return &node->rtl;
451 }
452
453 /* Return name of the node used in debug output.  */
454 const char *
455 cgraph_node_name (struct cgraph_node *node)
456 {
457   return lang_hooks.decl_printable_name (node->decl, 2);
458 }
459
460 /* Dump given cgraph node.  */
461 void
462 dump_cgraph_node (FILE *f, struct cgraph_node *node)
463 {
464   struct cgraph_edge *edge;
465   fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
466   if (node->global.inlined_to)
467     fprintf (f, " (inline copy in %s/%i)",
468              cgraph_node_name (node->global.inlined_to),
469              node->global.inlined_to->uid);
470   if (node->local.self_insns)
471     fprintf (f, " %i insns", node->local.self_insns);
472   if (node->global.insns && node->global.insns != node->local.self_insns)
473     fprintf (f, " (%i after inlining)", node->global.insns);
474   if (node->origin)
475     fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
476   if (node->needed)
477     fprintf (f, " needed");
478   else if (node->reachable)
479     fprintf (f, " reachable");
480   if (DECL_SAVED_TREE (node->decl))
481     fprintf (f, " tree");
482   if (node->output)
483     fprintf (f, " output");
484
485   if (node->local.local)
486     fprintf (f, " local");
487   if (node->local.disregard_inline_limits)
488     fprintf (f, " always_inline");
489   else if (node->local.inlinable)
490     fprintf (f, " inlinable");
491   if (TREE_ASM_WRITTEN (node->decl))
492     fprintf (f, " asm_written");
493
494   fprintf (f, "\n  called by: ");
495   for (edge = node->callers; edge; edge = edge->next_caller)
496     {
497       fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
498                edge->caller->uid);
499       if (!edge->inline_failed)
500         fprintf(f, "(inlined) ");
501     }
502
503   fprintf (f, "\n  calls: ");
504   for (edge = node->callees; edge; edge = edge->next_callee)
505     {
506       fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
507                edge->callee->uid);
508       if (!edge->inline_failed)
509         fprintf(f, "(inlined) ");
510     }
511   fprintf (f, "\n");
512 }
513
514 /* Dump the callgraph.  */
515
516 void
517 dump_cgraph (FILE *f)
518 {
519   struct cgraph_node *node;
520
521   fprintf (f, "callgraph:\n\n");
522   for (node = cgraph_nodes; node; node = node->next)
523     dump_cgraph_node (f, node);
524 }
525
526 /* Returns a hash code for P.  */
527
528 static hashval_t
529 cgraph_varpool_hash_node (const void *p)
530 {
531   return htab_hash_pointer (((struct cgraph_varpool_node *) p)->decl);
532 }
533
534 /* Returns nonzero if P1 and P2 are equal.  */
535
536 static int
537 eq_cgraph_varpool_node (const void *p1, const void *p2)
538 {
539   return (void *)((struct cgraph_varpool_node *) p1)->decl == p2;
540 }
541
542 /* Return cgraph_varpool node assigned to DECL.  Create new one when needed.  */
543 struct cgraph_varpool_node *
544 cgraph_varpool_node (tree decl)
545 {
546   struct cgraph_varpool_node *node;
547   struct cgraph_varpool_node **slot;
548
549   if (!DECL_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
550     abort ();
551
552   if (!cgraph_varpool_hash)
553     cgraph_varpool_hash = htab_create_ggc (10, cgraph_varpool_hash_node,
554                                            eq_cgraph_varpool_node, NULL);
555   slot = (struct cgraph_varpool_node **)
556     htab_find_slot_with_hash (cgraph_varpool_hash, decl,
557                               htab_hash_pointer (decl), INSERT);
558   if (*slot)
559     return *slot;
560   node = ggc_alloc_cleared (sizeof (*node));
561   node->decl = decl;
562   cgraph_varpool_n_nodes++;
563   cgraph_varpool_nodes = node;
564   *slot = node;
565   return node;
566 }
567
568 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables.  */
569 void
570 change_decl_assembler_name (tree decl, tree name)
571 {
572   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
573     {
574       SET_DECL_ASSEMBLER_NAME (decl, name);
575       return;
576     }
577   if (name == DECL_ASSEMBLER_NAME (decl))
578     return;
579
580   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
581       && DECL_RTL_SET_P (decl))
582     warning ("%D renamed after being referenced in assembly", decl);
583
584   SET_DECL_ASSEMBLER_NAME (decl, name);
585 }
586
587 /* Notify finalize_compilation_unit that given node is reachable
588    or needed.  */
589 void
590 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
591 {
592   if (!node->needed && node->finalized)
593     {
594       node->next_needed = cgraph_varpool_nodes_queue;
595       cgraph_varpool_nodes_queue = node;
596       notice_global_symbol (node->decl);
597     }
598   node->needed = 1;
599 }
600
601 void
602 cgraph_varpool_finalize_decl (tree decl)
603 {
604   struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
605  
606   /* The first declaration of a variable that comes through this function
607      decides whether it is global (in C, has external linkage)
608      or local (in C, has internal linkage).  So do nothing more
609      if this function has already run.  */
610   if (node->finalized)
611     return;
612   if (node->needed)
613     {
614       node->next_needed = cgraph_varpool_nodes_queue;
615       cgraph_varpool_nodes_queue = node;
616       notice_global_symbol (decl);
617     }
618   node->finalized = true;
619
620   if (/* Externally visible variables must be output.  The exception are
621          COMDAT functions that must be output only when they are needed.  */
622       (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
623       /* Function whose name is output to the assembler file must be produced.
624          It is possible to assemble the name later after finalizing the function
625          and the fact is noticed in assemble_name then.  */
626       || (DECL_ASSEMBLER_NAME_SET_P (decl)
627           && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))))
628     {
629       cgraph_varpool_mark_needed_node (node);
630     }
631 }
632
633 bool
634 cgraph_varpool_assemble_pending_decls (void)
635 {
636   bool changed = false;
637
638   while (cgraph_varpool_nodes_queue)
639     {
640       tree decl = cgraph_varpool_nodes_queue->decl;
641       struct cgraph_varpool_node *node = cgraph_varpool_nodes_queue;
642
643       cgraph_varpool_nodes_queue = cgraph_varpool_nodes_queue->next_needed;
644       if (!TREE_ASM_WRITTEN (decl))
645         {
646           assemble_variable (decl, 0, 1, 0);
647           changed = true;
648         }
649       node->next_needed = NULL;
650     }
651   return changed;
652 }
653
654 /* Return true when the DECL can possibly be inlined.  */
655 bool
656 cgraph_function_possibly_inlined_p (tree decl)
657 {
658   if (!cgraph_global_info_ready)
659     return (DECL_INLINE (decl) && !flag_really_no_inline);
660   if (!cgraph_inline_hash)
661     return false;
662   return (htab_find_slot (cgraph_inline_hash, decl, NO_INSERT) != NULL);
663 }
664
665 /* Create clone of E in the node N represented by CALL_EXPR the callgraph.  */
666 struct cgraph_edge *
667 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n, tree call_expr)
668 {
669   struct cgraph_edge *new = cgraph_create_edge (n, e->callee, call_expr);
670
671   new->inline_failed = e->inline_failed;
672   return new;
673 }
674
675 /* Create node representing clone of N.  */
676 struct cgraph_node *
677 cgraph_clone_node (struct cgraph_node *n)
678 {
679   struct cgraph_node *new = cgraph_create_node ();
680   struct cgraph_edge *e;
681
682   new->decl = n->decl;
683   new->origin = n->origin;
684   if (new->origin)
685     {
686       new->next_nested = new->origin->nested;
687       new->origin->nested = new;
688     }
689   new->analyzed = n->analyzed;
690   new->local = n->local;
691   new->global = n->global;
692   new->rtl = n->rtl;
693
694   for (e = n->callees;e; e=e->next_callee)
695     cgraph_clone_edge (e, new, e->call_expr);
696
697   new->next_clone = n->next_clone;
698   n->next_clone = new;
699
700   return new;
701 }
702 #include "gt-cgraph.h"