2005-06-30 J. D. Johnston <jjohnst@us.ibm.com>
[official-gcc.git] / gcc / cgraph.c
blob93648327cf9549a8f3bccad42e0c3968cb00024b
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 an 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 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.
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 "tree-inline.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "toplev.h"
91 #include "flags.h"
92 #include "ggc.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "basic-block.h"
96 #include "cgraph.h"
97 #include "varray.h"
98 #include "output.h"
99 #include "intl.h"
100 #include "tree-gimple.h"
102 static void cgraph_node_remove_callers (struct cgraph_node *node);
103 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
104 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
106 /* Hash table used to convert declarations into nodes. */
107 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
109 /* The linked list of cgraph nodes. */
110 struct cgraph_node *cgraph_nodes;
112 /* Queue of cgraph nodes scheduled to be lowered. */
113 struct cgraph_node *cgraph_nodes_queue;
115 /* Number of nodes in existence. */
116 int cgraph_n_nodes;
118 /* Maximal uid used in cgraph nodes. */
119 int cgraph_max_uid;
121 /* Set when whole unit has been analyzed so we can access global info. */
122 bool cgraph_global_info_ready = false;
124 /* Set when the cgraph is fully build and the basic flags are computed. */
125 bool cgraph_function_flags_ready = false;
127 /* Hash table used to convert declarations into nodes. */
128 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
130 /* Queue of cgraph nodes scheduled to be lowered and output. */
131 struct cgraph_varpool_node *cgraph_varpool_nodes_queue, *cgraph_varpool_first_unanalyzed_node;
134 /* The linked list of cgraph varpool nodes. */
135 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_nodes;
137 /* End of the varpool queue. Needs to be QTYed to work with PCH. */
138 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_last_needed_node;
140 static hashval_t hash_node (const void *);
141 static int eq_node (const void *, const void *);
143 /* Returns a hash code for P. */
145 static hashval_t
146 hash_node (const void *p)
148 const struct cgraph_node *n = p;
149 return (hashval_t) DECL_UID (n->decl);
152 /* Returns nonzero if P1 and P2 are equal. */
154 static int
155 eq_node (const void *p1, const void *p2)
157 const struct cgraph_node *n1 = p1, *n2 = p2;
158 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
161 /* Allocate new callgraph node and insert it into basic data structures. */
162 static struct cgraph_node *
163 cgraph_create_node (void)
165 struct cgraph_node *node;
167 node = ggc_alloc_cleared (sizeof (*node));
168 node->next = cgraph_nodes;
169 node->uid = cgraph_max_uid++;
170 if (cgraph_nodes)
171 cgraph_nodes->previous = node;
172 node->previous = NULL;
173 node->global.estimated_growth = INT_MIN;
174 cgraph_nodes = node;
175 cgraph_n_nodes++;
176 return node;
179 /* Return cgraph node assigned to DECL. Create new one when needed. */
180 struct cgraph_node *
181 cgraph_node (tree decl)
183 struct cgraph_node key, *node, **slot;
185 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
187 if (!cgraph_hash)
188 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
190 key.decl = decl;
192 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
194 if (*slot)
196 node = *slot;
197 if (!node->master_clone)
198 node->master_clone = node;
199 return node;
202 node = cgraph_create_node ();
203 node->decl = decl;
204 *slot = node;
205 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
207 node->origin = cgraph_node (DECL_CONTEXT (decl));
208 node->next_nested = node->origin->nested;
209 node->origin->nested = node;
210 node->master_clone = node;
212 return node;
215 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
217 static bool
218 decl_assembler_name_equal (tree decl, tree asmname)
220 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
222 if (decl_asmname == asmname)
223 return true;
225 /* If the target assembler name was set by the user, things are trickier.
226 We have a leading '*' to begin with. After that, it's arguable what
227 is the correct thing to do with -fleading-underscore. Arguably, we've
228 historically been doing the wrong thing in assemble_alias by always
229 printing the leading underscore. Since we're not changing that, make
230 sure user_label_prefix follows the '*' before matching. */
231 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
233 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
234 size_t ulp_len = strlen (user_label_prefix);
236 if (ulp_len == 0)
238 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
239 decl_str += ulp_len;
240 else
241 return false;
243 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
246 return false;
250 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
251 Return NULL if there's no such node. */
253 struct cgraph_node *
254 cgraph_node_for_asm (tree asmname)
256 struct cgraph_node *node;
258 for (node = cgraph_nodes; node ; node = node->next)
259 if (decl_assembler_name_equal (node->decl, asmname))
260 return node;
262 return NULL;
265 /* Return callgraph edge representing CALL_EXPR statement. */
266 struct cgraph_edge *
267 cgraph_edge (struct cgraph_node *node, tree call_stmt)
269 struct cgraph_edge *e;
271 /* This loop may turn out to be performance problem. In such case adding
272 hashtables into call nodes with very many edges is probably best
273 solution. It is not good idea to add pointer into CALL_EXPR itself
274 because we want to make possible having multiple cgraph nodes representing
275 different clones of the same body before the body is actually cloned. */
276 for (e = node->callees; e; e= e->next_callee)
277 if (e->call_stmt == call_stmt)
278 break;
279 return e;
282 /* Create edge from CALLER to CALLEE in the cgraph. */
284 struct cgraph_edge *
285 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
286 tree call_stmt, gcov_type count, int nest)
288 struct cgraph_edge *edge = ggc_alloc (sizeof (struct cgraph_edge));
289 #ifdef ENABLE_CHECKING
290 struct cgraph_edge *e;
292 for (e = caller->callees; e; e = e->next_callee)
293 gcc_assert (e->call_stmt != call_stmt);
294 #endif
296 gcc_assert (get_call_expr_in (call_stmt));
298 if (!DECL_SAVED_TREE (callee->decl))
299 edge->inline_failed = N_("function body not available");
300 else if (callee->local.redefined_extern_inline)
301 edge->inline_failed = N_("redefined extern inline functions are not "
302 "considered for inlining");
303 else if (callee->local.inlinable)
304 edge->inline_failed = N_("function not considered for inlining");
305 else
306 edge->inline_failed = N_("function not inlinable");
308 edge->aux = NULL;
310 edge->caller = caller;
311 edge->callee = callee;
312 edge->call_stmt = call_stmt;
313 edge->prev_caller = NULL;
314 edge->next_caller = callee->callers;
315 if (callee->callers)
316 callee->callers->prev_caller = edge;
317 edge->prev_callee = NULL;
318 edge->next_callee = caller->callees;
319 if (caller->callees)
320 caller->callees->prev_callee = edge;
321 caller->callees = edge;
322 callee->callers = edge;
323 edge->count = count;
324 edge->loop_nest = nest;
325 return edge;
328 /* Remove the edge E from the list of the callers of the callee. */
330 static inline void
331 cgraph_edge_remove_callee (struct cgraph_edge *e)
333 if (e->prev_caller)
334 e->prev_caller->next_caller = e->next_caller;
335 if (e->next_caller)
336 e->next_caller->prev_caller = e->prev_caller;
337 if (!e->prev_caller)
338 e->callee->callers = e->next_caller;
341 /* Remove the edge E from the list of the callees of the caller. */
343 static inline void
344 cgraph_edge_remove_caller (struct cgraph_edge *e)
346 if (e->prev_callee)
347 e->prev_callee->next_callee = e->next_callee;
348 if (e->next_callee)
349 e->next_callee->prev_callee = e->prev_callee;
350 if (!e->prev_callee)
351 e->caller->callees = e->next_callee;
354 /* Remove the edge E in the cgraph. */
356 void
357 cgraph_remove_edge (struct cgraph_edge *e)
359 /* Remove from callers list of the callee. */
360 cgraph_edge_remove_callee (e);
362 /* Remove from callees list of the callers. */
363 cgraph_edge_remove_caller (e);
366 /* Redirect callee of E to N. The function does not update underlying
367 call expression. */
369 void
370 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
372 /* Remove from callers list of the current callee. */
373 cgraph_edge_remove_callee (e);
375 /* Insert to callers list of the new callee. */
376 e->prev_caller = NULL;
377 if (n->callers)
378 n->callers->prev_caller = e;
379 e->next_caller = n->callers;
380 n->callers = e;
381 e->callee = n;
384 /* Remove all callees from the node. */
386 void
387 cgraph_node_remove_callees (struct cgraph_node *node)
389 struct cgraph_edge *e;
391 /* It is sufficient to remove the edges from the lists of callers of
392 the callees. The callee list of the node can be zapped with one
393 assignment. */
394 for (e = node->callees; e; e = e->next_callee)
395 cgraph_edge_remove_callee (e);
396 node->callees = NULL;
399 /* Remove all callers from the node. */
401 static void
402 cgraph_node_remove_callers (struct cgraph_node *node)
404 struct cgraph_edge *e;
406 /* It is sufficient to remove the edges from the lists of callees of
407 the callers. The caller list of the node can be zapped with one
408 assignment. */
409 for (e = node->callers; e; e = e->next_caller)
410 cgraph_edge_remove_caller (e);
411 node->callers = NULL;
414 /* Remove the node from cgraph. */
416 void
417 cgraph_remove_node (struct cgraph_node *node)
419 void **slot;
420 bool kill_body = false;
422 cgraph_node_remove_callers (node);
423 cgraph_node_remove_callees (node);
424 while (node->nested)
425 cgraph_remove_node (node->nested);
426 if (node->origin)
428 struct cgraph_node **node2 = &node->origin->nested;
430 while (*node2 != node)
431 node2 = &(*node2)->next_nested;
432 *node2 = node->next_nested;
434 if (node->previous)
435 node->previous->next = node->next;
436 else
437 cgraph_nodes = node->next;
438 if (node->next)
439 node->next->previous = node->previous;
440 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
441 if (*slot == node)
443 if (node->next_clone)
445 struct cgraph_node *new_node = node->next_clone;
446 struct cgraph_node *n;
448 /* Make the next clone be the master clone */
449 for (n = new_node; n; n = n->next_clone)
450 n->master_clone = new_node;
452 *slot = new_node;
453 node->next_clone->prev_clone = NULL;
455 else
457 htab_clear_slot (cgraph_hash, slot);
458 kill_body = true;
461 else
463 node->prev_clone->next_clone = node->next_clone;
464 if (node->next_clone)
465 node->next_clone->prev_clone = node->prev_clone;
468 /* While all the clones are removed after being proceeded, the function
469 itself is kept in the cgraph even after it is compiled. Check whether
470 we are done with this body and reclaim it proactively if this is the case.
472 if (!kill_body && *slot)
474 struct cgraph_node *n = *slot;
475 if (!n->next_clone && !n->global.inlined_to
476 && (cgraph_global_info_ready
477 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
478 kill_body = true;
481 if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
483 DECL_SAVED_TREE (node->decl) = NULL;
484 DECL_STRUCT_FUNCTION (node->decl) = NULL;
485 DECL_INITIAL (node->decl) = error_mark_node;
487 cgraph_n_nodes--;
488 /* Do not free the structure itself so the walk over chain can continue. */
491 /* Notify finalize_compilation_unit that given node is reachable. */
493 void
494 cgraph_mark_reachable_node (struct cgraph_node *node)
496 if (!node->reachable && node->local.finalized)
498 notice_global_symbol (node->decl);
499 node->reachable = 1;
500 gcc_assert (!cgraph_global_info_ready);
502 node->next_needed = cgraph_nodes_queue;
503 cgraph_nodes_queue = node;
507 /* Likewise indicate that a node is needed, i.e. reachable via some
508 external means. */
510 void
511 cgraph_mark_needed_node (struct cgraph_node *node)
513 node->needed = 1;
514 cgraph_mark_reachable_node (node);
517 /* Return local info for the compiled function. */
519 struct cgraph_local_info *
520 cgraph_local_info (tree decl)
522 struct cgraph_node *node;
524 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
525 node = cgraph_node (decl);
526 return &node->local;
529 /* Return local info for the compiled function. */
531 struct cgraph_global_info *
532 cgraph_global_info (tree decl)
534 struct cgraph_node *node;
536 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
537 node = cgraph_node (decl);
538 return &node->global;
541 /* Return local info for the compiled function. */
543 struct cgraph_rtl_info *
544 cgraph_rtl_info (tree decl)
546 struct cgraph_node *node;
548 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
549 node = cgraph_node (decl);
550 if (decl != current_function_decl
551 && !TREE_ASM_WRITTEN (node->decl))
552 return NULL;
553 return &node->rtl;
556 /* Return name of the node used in debug output. */
557 const char *
558 cgraph_node_name (struct cgraph_node *node)
560 return lang_hooks.decl_printable_name (node->decl, 2);
563 /* Return name of the node used in debug output. */
564 static const char *
565 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
567 return lang_hooks.decl_printable_name (node->decl, 2);
570 /* Names used to print out the availability enum. */
571 static const char * const availability_names[] =
572 {"unset", "not_available", "overwrittable", "available", "local"};
574 /* Dump given cgraph node. */
575 void
576 dump_cgraph_node (FILE *f, struct cgraph_node *node)
578 struct cgraph_edge *edge;
579 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
580 if (node->global.inlined_to)
581 fprintf (f, " (inline copy in %s/%i)",
582 cgraph_node_name (node->global.inlined_to),
583 node->global.inlined_to->uid);
584 if (cgraph_function_flags_ready)
585 fprintf (f, " availability:%s",
586 availability_names [cgraph_function_body_availability (node)]);
587 if (node->master_clone && node->master_clone->uid != node->uid)
588 fprintf (f, "(%i)", node->master_clone->uid);
589 if (node->count)
590 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
591 (HOST_WIDEST_INT)node->count);
592 if (node->local.self_insns)
593 fprintf (f, " %i insns", node->local.self_insns);
594 if (node->global.insns && node->global.insns != node->local.self_insns)
595 fprintf (f, " (%i after inlining)", node->global.insns);
596 if (node->origin)
597 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
598 if (node->needed)
599 fprintf (f, " needed");
600 else if (node->reachable)
601 fprintf (f, " reachable");
602 if (DECL_SAVED_TREE (node->decl))
603 fprintf (f, " tree");
604 if (node->output)
605 fprintf (f, " output");
606 if (node->local.local)
607 fprintf (f, " local");
608 if (node->local.externally_visible)
609 fprintf (f, " externally_visible");
610 if (node->local.finalized)
611 fprintf (f, " finalized");
612 if (node->local.disregard_inline_limits)
613 fprintf (f, " always_inline");
614 else if (node->local.inlinable)
615 fprintf (f, " inlinable");
616 if (node->local.redefined_extern_inline)
617 fprintf (f, " redefined_extern_inline");
618 if (TREE_ASM_WRITTEN (node->decl))
619 fprintf (f, " asm_written");
621 fprintf (f, "\n called by: ");
622 for (edge = node->callers; edge; edge = edge->next_caller)
624 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
625 edge->caller->uid);
626 if (edge->count)
627 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
628 (HOST_WIDEST_INT)edge->count);
629 if (!edge->inline_failed)
630 fprintf(f, "(inlined) ");
633 fprintf (f, "\n calls: ");
634 for (edge = node->callees; edge; edge = edge->next_callee)
636 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
637 edge->callee->uid);
638 if (!edge->inline_failed)
639 fprintf(f, "(inlined) ");
640 if (edge->count)
641 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
642 (HOST_WIDEST_INT)edge->count);
643 if (edge->loop_nest)
644 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
646 fprintf (f, "\n");
649 /* Dump the callgraph. */
651 void
652 dump_cgraph (FILE *f)
654 struct cgraph_node *node;
656 fprintf (f, "callgraph:\n\n");
657 for (node = cgraph_nodes; node; node = node->next)
658 dump_cgraph_node (f, node);
661 /* Dump given cgraph node. */
662 void
663 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
665 fprintf (f, "%s:", cgraph_varpool_node_name (node));
666 fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
667 if (DECL_INITIAL (node->decl))
668 fprintf (f, " initialized");
669 if (node->needed)
670 fprintf (f, " needed");
671 if (node->analyzed)
672 fprintf (f, " analyzed");
673 if (node->finalized)
674 fprintf (f, " finalized");
675 if (node->output)
676 fprintf (f, " output");
677 if (node->externally_visible)
678 fprintf (f, " externally_visible");
679 fprintf (f, "\n");
682 /* Dump the callgraph. */
684 void
685 dump_varpool (FILE *f)
687 struct cgraph_varpool_node *node;
689 fprintf (f, "variable pool:\n\n");
690 for (node = cgraph_varpool_nodes; node; node = node->next_needed)
691 dump_cgraph_varpool_node (f, node);
694 /* Returns a hash code for P. */
696 static hashval_t
697 hash_varpool_node (const void *p)
699 const struct cgraph_varpool_node *n = p;
700 return (hashval_t) DECL_UID (n->decl);
703 /* Returns nonzero if P1 and P2 are equal. */
705 static int
706 eq_varpool_node (const void *p1, const void *p2)
708 const struct cgraph_varpool_node *n1 = p1, *n2 = p2;
709 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
712 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
713 struct cgraph_varpool_node *
714 cgraph_varpool_node (tree decl)
716 struct cgraph_varpool_node key, *node, **slot;
718 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
720 if (!cgraph_varpool_hash)
721 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
722 eq_varpool_node, NULL);
723 key.decl = decl;
724 slot = (struct cgraph_varpool_node **)
725 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
726 if (*slot)
727 return *slot;
728 node = ggc_alloc_cleared (sizeof (*node));
729 node->decl = decl;
730 node->next = cgraph_varpool_nodes;
731 cgraph_varpool_nodes = node;
732 *slot = node;
733 return node;
736 struct cgraph_varpool_node *
737 cgraph_varpool_node_for_asm (tree asmname)
739 struct cgraph_varpool_node *node;
741 for (node = cgraph_varpool_nodes; node ; node = node->next)
742 if (decl_assembler_name_equal (node->decl, asmname))
743 return node;
745 return NULL;
748 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
749 void
750 change_decl_assembler_name (tree decl, tree name)
752 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
754 SET_DECL_ASSEMBLER_NAME (decl, name);
755 return;
757 if (name == DECL_ASSEMBLER_NAME (decl))
758 return;
760 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
761 && DECL_RTL_SET_P (decl))
762 warning (0, "%D renamed after being referenced in assembly", decl);
764 SET_DECL_ASSEMBLER_NAME (decl, name);
767 /* Helper function for finalization code - add node into lists so it will
768 be analyzed and compiled. */
769 void
770 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
772 if (cgraph_varpool_last_needed_node)
773 cgraph_varpool_last_needed_node->next_needed = node;
774 cgraph_varpool_last_needed_node = node;
775 node->next_needed = NULL;
776 if (!cgraph_varpool_nodes_queue)
777 cgraph_varpool_nodes_queue = node;
778 if (!cgraph_varpool_first_unanalyzed_node)
779 cgraph_varpool_first_unanalyzed_node = node;
780 notice_global_symbol (node->decl);
783 /* Reset the queue of needed nodes. */
784 void
785 cgraph_varpool_reset_queue (void)
787 cgraph_varpool_last_needed_node = NULL;
788 cgraph_varpool_nodes_queue = NULL;
789 cgraph_varpool_first_unanalyzed_node = NULL;
792 /* Notify finalize_compilation_unit that given node is reachable
793 or needed. */
794 void
795 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
797 if (!node->needed && node->finalized)
798 cgraph_varpool_enqueue_needed_node (node);
799 node->needed = 1;
802 /* Determine if variable DECL is needed. That is, visible to something
803 either outside this translation unit, something magic in the system
804 configury, or (if not doing unit-at-a-time) to something we haven't
805 seen yet. */
807 bool
808 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
810 /* If the user told us it is used, then it must be so. */
811 if (node->externally_visible
812 || lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
813 return true;
815 /* ??? If the assembler name is set by hand, it is possible to assemble
816 the name later after finalizing the function and the fact is noticed
817 in assemble_name then. This is arguably a bug. */
818 if (DECL_ASSEMBLER_NAME_SET_P (decl)
819 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
820 return true;
822 /* If we decided it was needed before, but at the time we didn't have
823 the definition available, then it's still needed. */
824 if (node->needed)
825 return true;
827 /* Externally visible variables must be output. The exception is
828 COMDAT variables that must be output only when they are needed. */
829 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
830 return true;
832 if (flag_unit_at_a_time)
833 return false;
835 /* If not doing unit at a time, then we'll only defer this function
836 if its marked for inlining. Otherwise we want to emit it now. */
838 /* We want to emit COMDAT variables only when absolutely necessary. */
839 if (DECL_COMDAT (decl))
840 return false;
841 return true;
844 void
845 cgraph_varpool_finalize_decl (tree decl)
847 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
849 /* The first declaration of a variable that comes through this function
850 decides whether it is global (in C, has external linkage)
851 or local (in C, has internal linkage). So do nothing more
852 if this function has already run. */
853 if (node->finalized)
855 if (cgraph_global_info_ready || !flag_unit_at_a_time)
856 cgraph_varpool_assemble_pending_decls ();
857 return;
859 if (node->needed)
860 cgraph_varpool_enqueue_needed_node (node);
861 node->finalized = true;
863 if (decide_is_variable_needed (node, decl))
864 cgraph_varpool_mark_needed_node (node);
865 /* Since we reclaim unreachable nodes at the end of every language
866 level unit, we need to be conservative about possible entry points
867 there. */
868 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
869 cgraph_varpool_mark_needed_node (node);
870 if (cgraph_global_info_ready || !flag_unit_at_a_time)
871 cgraph_varpool_assemble_pending_decls ();
874 /* Return true when the DECL can possibly be inlined. */
875 bool
876 cgraph_function_possibly_inlined_p (tree decl)
878 if (!cgraph_global_info_ready)
879 return (DECL_INLINE (decl) && !flag_really_no_inline);
880 return DECL_POSSIBLY_INLINED (decl);
883 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
884 struct cgraph_edge *
885 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
886 tree call_stmt, int count_scale, int loop_nest)
888 struct cgraph_edge *new;
890 new = cgraph_create_edge (n, e->callee, call_stmt,
891 e->count * count_scale / REG_BR_PROB_BASE,
892 e->loop_nest + loop_nest);
894 new->inline_failed = e->inline_failed;
895 e->count -= new->count;
896 return new;
899 /* Create node representing clone of N executed COUNT times. Decrease
900 the execution counts from original node too. */
901 struct cgraph_node *
902 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest)
904 struct cgraph_node *new = cgraph_create_node ();
905 struct cgraph_edge *e;
906 int count_scale;
908 new->decl = n->decl;
909 new->origin = n->origin;
910 if (new->origin)
912 new->next_nested = new->origin->nested;
913 new->origin->nested = new;
915 new->analyzed = n->analyzed;
916 new->local = n->local;
917 new->global = n->global;
918 new->rtl = n->rtl;
919 new->master_clone = n->master_clone;
920 new->count = count;
921 if (n->count)
922 count_scale = new->count * REG_BR_PROB_BASE / n->count;
923 else
924 count_scale = 0;
925 n->count -= count;
927 for (e = n->callees;e; e=e->next_callee)
928 cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest);
930 new->next_clone = n->next_clone;
931 new->prev_clone = n;
932 n->next_clone = new;
933 if (new->next_clone)
934 new->next_clone->prev_clone = new;
936 return new;
939 /* Return true if N is an master_clone, (see cgraph_master_clone). */
941 bool
942 cgraph_is_master_clone (struct cgraph_node *n)
944 return (n == cgraph_master_clone (n));
947 struct cgraph_node *
948 cgraph_master_clone (struct cgraph_node *n)
950 enum availability avail = cgraph_function_body_availability (n);
952 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
953 return NULL;
955 if (!n->master_clone)
956 n->master_clone = cgraph_node (n->decl);
958 return n->master_clone;
961 /* NODE is no longer nested function; update cgraph accordingly. */
962 void
963 cgraph_unnest_node (struct cgraph_node *node)
965 struct cgraph_node **node2 = &node->origin->nested;
966 gcc_assert (node->origin);
968 while (*node2 != node)
969 node2 = &(*node2)->next_nested;
970 *node2 = node->next_nested;
971 node->origin = NULL;
974 /* Return function availability. See cgraph.h for description of individual
975 return values. */
976 enum availability
977 cgraph_function_body_availability (struct cgraph_node *node)
979 enum availability avail;
980 gcc_assert (cgraph_function_flags_ready);
981 if (!node->local.finalized)
982 avail = AVAIL_NOT_AVAILABLE;
983 else if (node->local.local)
984 avail = AVAIL_LOCAL;
985 else if (node->local.externally_visible)
986 avail = AVAIL_AVAILABLE;
988 /* If the function can be overwritten, return OVERWRITABLE. Take
989 care at least of two notable extensions - the COMDAT functions
990 used to share template instantiations in C++ (this is symmetric
991 to code cp_cannot_inline_tree_fn and probably shall be shared and
992 the inlinability hooks completely eliminated).
994 ??? Does the C++ one definition rule allow us to always return
995 AVAIL_AVAILABLE here? That would be good reason to preserve this
996 hook Similarly deal with extern inline functions - this is again
997 necessary to get C++ shared functions having keyed templates
998 right and in the C extension documentation we probably should
999 document the requirement of both versions of function (extern
1000 inline and offline) having same side effect characteristics as
1001 good optimization is what this optimization is about. */
1003 else if (!(*targetm.binds_local_p) (node->decl)
1004 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1005 avail = AVAIL_OVERWRITABLE;
1006 else avail = AVAIL_AVAILABLE;
1008 return avail;
1011 /* Return variable availability. See cgraph.h for description of individual
1012 return values. */
1013 enum availability
1014 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1016 gcc_assert (cgraph_function_flags_ready);
1017 if (!node->finalized)
1018 return AVAIL_NOT_AVAILABLE;
1019 if (!TREE_PUBLIC (node->decl))
1020 return AVAIL_AVAILABLE;
1021 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1022 care of at least two notable extensions - the COMDAT variables
1023 used to share template instantiations in C++. */
1024 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1025 return AVAIL_OVERWRITABLE;
1026 return AVAIL_AVAILABLE;
1029 #include "gt-cgraph.h"