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