2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / cgraph.c
blobab45f04c7cc40c61f8e74e32b3471d4d3bd010df
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"
101 #include "tree-dump.h"
103 static void cgraph_node_remove_callers (struct cgraph_node *node);
104 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
105 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
107 /* Hash table used to convert declarations into nodes. */
108 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
110 /* The linked list of cgraph nodes. */
111 struct cgraph_node *cgraph_nodes;
113 /* Queue of cgraph nodes scheduled to be lowered. */
114 struct cgraph_node *cgraph_nodes_queue;
116 /* Number of nodes in existence. */
117 int cgraph_n_nodes;
119 /* Maximal uid used in cgraph nodes. */
120 int cgraph_max_uid;
122 /* Set when whole unit has been analyzed so we can access global info. */
123 bool cgraph_global_info_ready = false;
125 /* Set when the cgraph is fully build and the basic flags are computed. */
126 bool cgraph_function_flags_ready = false;
128 /* Hash table used to convert declarations into nodes. */
129 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
131 /* Queue of cgraph nodes scheduled to be lowered and output. */
132 struct cgraph_varpool_node *cgraph_varpool_nodes_queue, *cgraph_varpool_first_unanalyzed_node;
135 /* The linked list of cgraph varpool nodes. */
136 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_nodes;
138 /* End of the varpool queue. Needs to be QTYed to work with PCH. */
139 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_last_needed_node;
141 static hashval_t hash_node (const void *);
142 static int eq_node (const void *, const void *);
144 /* Returns a hash code for P. */
146 static hashval_t
147 hash_node (const void *p)
149 const struct cgraph_node *n = (const struct cgraph_node *) p;
150 return (hashval_t) DECL_UID (n->decl);
153 /* Returns nonzero if P1 and P2 are equal. */
155 static int
156 eq_node (const void *p1, const void *p2)
158 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
159 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
160 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
163 /* Allocate new callgraph node and insert it into basic data structures. */
164 static struct cgraph_node *
165 cgraph_create_node (void)
167 struct cgraph_node *node;
169 node = GGC_CNEW (struct cgraph_node);
170 node->next = cgraph_nodes;
171 node->uid = cgraph_max_uid++;
172 if (cgraph_nodes)
173 cgraph_nodes->previous = node;
174 node->previous = NULL;
175 node->global.estimated_growth = INT_MIN;
176 cgraph_nodes = node;
177 cgraph_n_nodes++;
178 return node;
181 /* Return cgraph node assigned to DECL. Create new one when needed. */
182 struct cgraph_node *
183 cgraph_node (tree decl)
185 struct cgraph_node key, *node, **slot;
187 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
189 if (!cgraph_hash)
190 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
192 key.decl = decl;
194 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
196 if (*slot)
198 node = *slot;
199 if (!node->master_clone)
200 node->master_clone = node;
201 return node;
204 node = cgraph_create_node ();
205 node->decl = decl;
206 *slot = node;
207 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
209 node->origin = cgraph_node (DECL_CONTEXT (decl));
210 node->next_nested = node->origin->nested;
211 node->origin->nested = node;
212 node->master_clone = node;
214 return node;
217 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
219 static bool
220 decl_assembler_name_equal (tree decl, tree asmname)
222 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
224 if (decl_asmname == asmname)
225 return true;
227 /* If the target assembler name was set by the user, things are trickier.
228 We have a leading '*' to begin with. After that, it's arguable what
229 is the correct thing to do with -fleading-underscore. Arguably, we've
230 historically been doing the wrong thing in assemble_alias by always
231 printing the leading underscore. Since we're not changing that, make
232 sure user_label_prefix follows the '*' before matching. */
233 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
235 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
236 size_t ulp_len = strlen (user_label_prefix);
238 if (ulp_len == 0)
240 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
241 decl_str += ulp_len;
242 else
243 return false;
245 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
248 return false;
252 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
253 Return NULL if there's no such node. */
255 struct cgraph_node *
256 cgraph_node_for_asm (tree asmname)
258 struct cgraph_node *node;
260 for (node = cgraph_nodes; node ; node = node->next)
261 if (decl_assembler_name_equal (node->decl, asmname))
262 return node;
264 return NULL;
267 /* Return callgraph edge representing CALL_EXPR statement. */
268 struct cgraph_edge *
269 cgraph_edge (struct cgraph_node *node, tree call_stmt)
271 struct cgraph_edge *e;
273 /* This loop may turn out to be performance problem. In such case adding
274 hashtables into call nodes with very many edges is probably best
275 solution. It is not good idea to add pointer into CALL_EXPR itself
276 because we want to make possible having multiple cgraph nodes representing
277 different clones of the same body before the body is actually cloned. */
278 for (e = node->callees; e; e= e->next_callee)
279 if (e->call_stmt == call_stmt)
280 break;
281 return e;
284 /* Create edge from CALLER to CALLEE in the cgraph. */
286 struct cgraph_edge *
287 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
288 tree call_stmt, gcov_type count, int nest)
290 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
291 #ifdef ENABLE_CHECKING
292 struct cgraph_edge *e;
294 for (e = caller->callees; e; e = e->next_callee)
295 gcc_assert (e->call_stmt != call_stmt);
296 #endif
298 gcc_assert (get_call_expr_in (call_stmt));
300 if (!DECL_SAVED_TREE (callee->decl))
301 edge->inline_failed = N_("function body not available");
302 else if (callee->local.redefined_extern_inline)
303 edge->inline_failed = N_("redefined extern inline functions are not "
304 "considered for inlining");
305 else if (callee->local.inlinable)
306 edge->inline_failed = N_("function not considered for inlining");
307 else
308 edge->inline_failed = N_("function not inlinable");
310 edge->aux = NULL;
312 edge->caller = caller;
313 edge->callee = callee;
314 edge->call_stmt = call_stmt;
315 edge->prev_caller = NULL;
316 edge->next_caller = callee->callers;
317 if (callee->callers)
318 callee->callers->prev_caller = edge;
319 edge->prev_callee = NULL;
320 edge->next_callee = caller->callees;
321 if (caller->callees)
322 caller->callees->prev_callee = edge;
323 caller->callees = edge;
324 callee->callers = edge;
325 edge->count = count;
326 edge->loop_nest = nest;
327 return edge;
330 /* Remove the edge E from the list of the callers of the callee. */
332 static inline void
333 cgraph_edge_remove_callee (struct cgraph_edge *e)
335 if (e->prev_caller)
336 e->prev_caller->next_caller = e->next_caller;
337 if (e->next_caller)
338 e->next_caller->prev_caller = e->prev_caller;
339 if (!e->prev_caller)
340 e->callee->callers = e->next_caller;
343 /* Remove the edge E from the list of the callees of the caller. */
345 static inline void
346 cgraph_edge_remove_caller (struct cgraph_edge *e)
348 if (e->prev_callee)
349 e->prev_callee->next_callee = e->next_callee;
350 if (e->next_callee)
351 e->next_callee->prev_callee = e->prev_callee;
352 if (!e->prev_callee)
353 e->caller->callees = e->next_callee;
356 /* Remove the edge E in the cgraph. */
358 void
359 cgraph_remove_edge (struct cgraph_edge *e)
361 /* Remove from callers list of the callee. */
362 cgraph_edge_remove_callee (e);
364 /* Remove from callees list of the callers. */
365 cgraph_edge_remove_caller (e);
368 /* Redirect callee of E to N. The function does not update underlying
369 call expression. */
371 void
372 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
374 /* Remove from callers list of the current callee. */
375 cgraph_edge_remove_callee (e);
377 /* Insert to callers list of the new callee. */
378 e->prev_caller = NULL;
379 if (n->callers)
380 n->callers->prev_caller = e;
381 e->next_caller = n->callers;
382 n->callers = e;
383 e->callee = n;
386 /* Remove all callees from the node. */
388 void
389 cgraph_node_remove_callees (struct cgraph_node *node)
391 struct cgraph_edge *e;
393 /* It is sufficient to remove the edges from the lists of callers of
394 the callees. The callee list of the node can be zapped with one
395 assignment. */
396 for (e = node->callees; e; e = e->next_callee)
397 cgraph_edge_remove_callee (e);
398 node->callees = NULL;
401 /* Remove all callers from the node. */
403 static void
404 cgraph_node_remove_callers (struct cgraph_node *node)
406 struct cgraph_edge *e;
408 /* It is sufficient to remove the edges from the lists of callees of
409 the callers. The caller list of the node can be zapped with one
410 assignment. */
411 for (e = node->callers; e; e = e->next_caller)
412 cgraph_edge_remove_caller (e);
413 node->callers = NULL;
416 /* Remove the node from cgraph. */
418 void
419 cgraph_remove_node (struct cgraph_node *node)
421 void **slot;
422 bool kill_body = false;
424 cgraph_node_remove_callers (node);
425 cgraph_node_remove_callees (node);
426 while (node->nested)
427 cgraph_remove_node (node->nested);
428 if (node->origin)
430 struct cgraph_node **node2 = &node->origin->nested;
432 while (*node2 != node)
433 node2 = &(*node2)->next_nested;
434 *node2 = node->next_nested;
436 if (node->previous)
437 node->previous->next = node->next;
438 else
439 cgraph_nodes = node->next;
440 if (node->next)
441 node->next->previous = node->previous;
442 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
443 if (*slot == node)
445 if (node->next_clone)
447 struct cgraph_node *new_node = node->next_clone;
448 struct cgraph_node *n;
450 /* Make the next clone be the master clone */
451 for (n = new_node; n; n = n->next_clone)
452 n->master_clone = new_node;
454 *slot = new_node;
455 node->next_clone->prev_clone = NULL;
457 else
459 htab_clear_slot (cgraph_hash, slot);
460 kill_body = true;
463 else
465 node->prev_clone->next_clone = node->next_clone;
466 if (node->next_clone)
467 node->next_clone->prev_clone = node->prev_clone;
470 /* While all the clones are removed after being proceeded, the function
471 itself is kept in the cgraph even after it is compiled. Check whether
472 we are done with this body and reclaim it proactively if this is the case.
474 if (!kill_body && *slot)
476 struct cgraph_node *n = (struct cgraph_node *) *slot;
477 if (!n->next_clone && !n->global.inlined_to
478 && (cgraph_global_info_ready
479 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
480 kill_body = true;
483 if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
485 DECL_SAVED_TREE (node->decl) = NULL;
486 DECL_STRUCT_FUNCTION (node->decl) = NULL;
487 DECL_INITIAL (node->decl) = error_mark_node;
489 cgraph_n_nodes--;
490 /* Do not free the structure itself so the walk over chain can continue. */
493 /* Notify finalize_compilation_unit that given node is reachable. */
495 void
496 cgraph_mark_reachable_node (struct cgraph_node *node)
498 if (!node->reachable && node->local.finalized)
500 notice_global_symbol (node->decl);
501 node->reachable = 1;
502 gcc_assert (!cgraph_global_info_ready);
504 node->next_needed = cgraph_nodes_queue;
505 cgraph_nodes_queue = node;
509 /* Likewise indicate that a node is needed, i.e. reachable via some
510 external means. */
512 void
513 cgraph_mark_needed_node (struct cgraph_node *node)
515 node->needed = 1;
516 cgraph_mark_reachable_node (node);
519 /* Return local info for the compiled function. */
521 struct cgraph_local_info *
522 cgraph_local_info (tree decl)
524 struct cgraph_node *node;
526 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
527 node = cgraph_node (decl);
528 return &node->local;
531 /* Return local info for the compiled function. */
533 struct cgraph_global_info *
534 cgraph_global_info (tree decl)
536 struct cgraph_node *node;
538 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
539 node = cgraph_node (decl);
540 return &node->global;
543 /* Return local info for the compiled function. */
545 struct cgraph_rtl_info *
546 cgraph_rtl_info (tree decl)
548 struct cgraph_node *node;
550 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
551 node = cgraph_node (decl);
552 if (decl != current_function_decl
553 && !TREE_ASM_WRITTEN (node->decl))
554 return NULL;
555 return &node->rtl;
558 /* Return name of the node used in debug output. */
559 const char *
560 cgraph_node_name (struct cgraph_node *node)
562 return lang_hooks.decl_printable_name (node->decl, 2);
565 /* Return name of the node used in debug output. */
566 static const char *
567 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
569 return lang_hooks.decl_printable_name (node->decl, 2);
572 /* Names used to print out the availability enum. */
573 static const char * const availability_names[] =
574 {"unset", "not_available", "overwrittable", "available", "local"};
576 /* Dump given cgraph node. */
577 void
578 dump_cgraph_node (FILE *f, struct cgraph_node *node)
580 struct cgraph_edge *edge;
581 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
582 if (node->global.inlined_to)
583 fprintf (f, " (inline copy in %s/%i)",
584 cgraph_node_name (node->global.inlined_to),
585 node->global.inlined_to->uid);
586 if (cgraph_function_flags_ready)
587 fprintf (f, " availability:%s",
588 availability_names [cgraph_function_body_availability (node)]);
589 if (node->master_clone && node->master_clone->uid != node->uid)
590 fprintf (f, "(%i)", node->master_clone->uid);
591 if (node->count)
592 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
593 (HOST_WIDEST_INT)node->count);
594 if (node->local.self_insns)
595 fprintf (f, " %i insns", node->local.self_insns);
596 if (node->global.insns && node->global.insns != node->local.self_insns)
597 fprintf (f, " (%i after inlining)", node->global.insns);
598 if (node->origin)
599 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
600 if (node->needed)
601 fprintf (f, " needed");
602 else if (node->reachable)
603 fprintf (f, " reachable");
604 if (DECL_SAVED_TREE (node->decl))
605 fprintf (f, " tree");
606 if (node->output)
607 fprintf (f, " output");
608 if (node->local.local)
609 fprintf (f, " local");
610 if (node->local.externally_visible)
611 fprintf (f, " externally_visible");
612 if (node->local.finalized)
613 fprintf (f, " finalized");
614 if (node->local.disregard_inline_limits)
615 fprintf (f, " always_inline");
616 else if (node->local.inlinable)
617 fprintf (f, " inlinable");
618 if (node->local.redefined_extern_inline)
619 fprintf (f, " redefined_extern_inline");
620 if (TREE_ASM_WRITTEN (node->decl))
621 fprintf (f, " asm_written");
623 fprintf (f, "\n called by: ");
624 for (edge = node->callers; edge; edge = edge->next_caller)
626 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
627 edge->caller->uid);
628 if (edge->count)
629 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
630 (HOST_WIDEST_INT)edge->count);
631 if (!edge->inline_failed)
632 fprintf(f, "(inlined) ");
635 fprintf (f, "\n calls: ");
636 for (edge = node->callees; edge; edge = edge->next_callee)
638 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
639 edge->callee->uid);
640 if (!edge->inline_failed)
641 fprintf(f, "(inlined) ");
642 if (edge->count)
643 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
644 (HOST_WIDEST_INT)edge->count);
645 if (edge->loop_nest)
646 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
648 fprintf (f, "\n");
651 /* Dump the callgraph. */
653 void
654 dump_cgraph (FILE *f)
656 struct cgraph_node *node;
658 fprintf (f, "callgraph:\n\n");
659 for (node = cgraph_nodes; node; node = node->next)
660 dump_cgraph_node (f, node);
663 /* Dump given cgraph node. */
664 void
665 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
667 fprintf (f, "%s:", cgraph_varpool_node_name (node));
668 fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
669 if (DECL_INITIAL (node->decl))
670 fprintf (f, " initialized");
671 if (node->needed)
672 fprintf (f, " needed");
673 if (node->analyzed)
674 fprintf (f, " analyzed");
675 if (node->finalized)
676 fprintf (f, " finalized");
677 if (node->output)
678 fprintf (f, " output");
679 if (node->externally_visible)
680 fprintf (f, " externally_visible");
681 fprintf (f, "\n");
684 /* Dump the callgraph. */
686 void
687 dump_varpool (FILE *f)
689 struct cgraph_varpool_node *node;
691 fprintf (f, "variable pool:\n\n");
692 for (node = cgraph_varpool_nodes; node; node = node->next_needed)
693 dump_cgraph_varpool_node (f, node);
696 /* Returns a hash code for P. */
698 static hashval_t
699 hash_varpool_node (const void *p)
701 const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
702 return (hashval_t) DECL_UID (n->decl);
705 /* Returns nonzero if P1 and P2 are equal. */
707 static int
708 eq_varpool_node (const void *p1, const void *p2)
710 const struct cgraph_varpool_node *n1 =
711 (const struct cgraph_varpool_node *) p1;
712 const struct cgraph_varpool_node *n2 =
713 (const struct cgraph_varpool_node *) p2;
714 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
717 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
718 struct cgraph_varpool_node *
719 cgraph_varpool_node (tree decl)
721 struct cgraph_varpool_node key, *node, **slot;
723 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
725 if (!cgraph_varpool_hash)
726 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
727 eq_varpool_node, NULL);
728 key.decl = decl;
729 slot = (struct cgraph_varpool_node **)
730 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
731 if (*slot)
732 return *slot;
733 node = GGC_CNEW (struct cgraph_varpool_node);
734 node->decl = decl;
735 node->next = cgraph_varpool_nodes;
736 cgraph_varpool_nodes = node;
737 *slot = node;
738 return node;
741 struct cgraph_varpool_node *
742 cgraph_varpool_node_for_asm (tree asmname)
744 struct cgraph_varpool_node *node;
746 for (node = cgraph_varpool_nodes; node ; node = node->next)
747 if (decl_assembler_name_equal (node->decl, asmname))
748 return node;
750 return NULL;
753 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
754 void
755 change_decl_assembler_name (tree decl, tree name)
757 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
759 SET_DECL_ASSEMBLER_NAME (decl, name);
760 return;
762 if (name == DECL_ASSEMBLER_NAME (decl))
763 return;
765 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
766 && DECL_RTL_SET_P (decl))
767 warning (0, "%D renamed after being referenced in assembly", decl);
769 SET_DECL_ASSEMBLER_NAME (decl, name);
772 /* Helper function for finalization code - add node into lists so it will
773 be analyzed and compiled. */
774 void
775 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
777 if (cgraph_varpool_last_needed_node)
778 cgraph_varpool_last_needed_node->next_needed = node;
779 cgraph_varpool_last_needed_node = node;
780 node->next_needed = NULL;
781 if (!cgraph_varpool_nodes_queue)
782 cgraph_varpool_nodes_queue = node;
783 if (!cgraph_varpool_first_unanalyzed_node)
784 cgraph_varpool_first_unanalyzed_node = node;
785 notice_global_symbol (node->decl);
788 /* Reset the queue of needed nodes. */
789 void
790 cgraph_varpool_reset_queue (void)
792 cgraph_varpool_last_needed_node = NULL;
793 cgraph_varpool_nodes_queue = NULL;
794 cgraph_varpool_first_unanalyzed_node = NULL;
797 /* Notify finalize_compilation_unit that given node is reachable
798 or needed. */
799 void
800 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
802 if (!node->needed && node->finalized)
803 cgraph_varpool_enqueue_needed_node (node);
804 node->needed = 1;
807 /* Determine if variable DECL is needed. That is, visible to something
808 either outside this translation unit, something magic in the system
809 configury, or (if not doing unit-at-a-time) to something we haven't
810 seen yet. */
812 bool
813 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
815 /* If the user told us it is used, then it must be so. */
816 if (node->externally_visible
817 || lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
818 return true;
820 /* ??? If the assembler name is set by hand, it is possible to assemble
821 the name later after finalizing the function and the fact is noticed
822 in assemble_name then. This is arguably a bug. */
823 if (DECL_ASSEMBLER_NAME_SET_P (decl)
824 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
825 return true;
827 /* If we decided it was needed before, but at the time we didn't have
828 the definition available, then it's still needed. */
829 if (node->needed)
830 return true;
832 /* Externally visible variables must be output. The exception is
833 COMDAT variables that must be output only when they are needed. */
834 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
835 return true;
837 if (flag_unit_at_a_time)
838 return false;
840 /* If not doing unit at a time, then we'll only defer this function
841 if its marked for inlining. Otherwise we want to emit it now. */
843 /* We want to emit COMDAT variables only when absolutely necessary. */
844 if (DECL_COMDAT (decl))
845 return false;
846 return true;
849 void
850 cgraph_varpool_finalize_decl (tree decl)
852 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
854 /* The first declaration of a variable that comes through this function
855 decides whether it is global (in C, has external linkage)
856 or local (in C, has internal linkage). So do nothing more
857 if this function has already run. */
858 if (node->finalized)
860 if (cgraph_global_info_ready || !flag_unit_at_a_time)
861 cgraph_varpool_assemble_pending_decls ();
862 return;
864 if (node->needed)
865 cgraph_varpool_enqueue_needed_node (node);
866 node->finalized = true;
868 if (decide_is_variable_needed (node, decl))
869 cgraph_varpool_mark_needed_node (node);
870 /* Since we reclaim unreachable nodes at the end of every language
871 level unit, we need to be conservative about possible entry points
872 there. */
873 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
874 cgraph_varpool_mark_needed_node (node);
875 if (cgraph_global_info_ready || !flag_unit_at_a_time)
876 cgraph_varpool_assemble_pending_decls ();
879 /* Return true when the DECL can possibly be inlined. */
880 bool
881 cgraph_function_possibly_inlined_p (tree decl)
883 if (!cgraph_global_info_ready)
884 return (DECL_INLINE (decl) && !flag_really_no_inline);
885 return DECL_POSSIBLY_INLINED (decl);
888 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
889 struct cgraph_edge *
890 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
891 tree call_stmt, gcov_type count_scale, int loop_nest,
892 bool update_original)
894 struct cgraph_edge *new;
896 new = cgraph_create_edge (n, e->callee, call_stmt,
897 e->count * count_scale / REG_BR_PROB_BASE,
898 e->loop_nest + loop_nest);
900 new->inline_failed = e->inline_failed;
901 if (update_original)
903 e->count -= new->count;
904 if (e->count < 0)
905 e->count = 0;
907 return new;
910 /* Create node representing clone of N executed COUNT times. Decrease
911 the execution counts from original node too.
913 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
914 function's profile to reflect the fact that part of execution is handled
915 by node. */
916 struct cgraph_node *
917 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
918 bool update_original)
920 struct cgraph_node *new = cgraph_create_node ();
921 struct cgraph_edge *e;
922 gcov_type count_scale;
924 new->decl = n->decl;
925 new->origin = n->origin;
926 if (new->origin)
928 new->next_nested = new->origin->nested;
929 new->origin->nested = new;
931 new->analyzed = n->analyzed;
932 new->local = n->local;
933 new->global = n->global;
934 new->rtl = n->rtl;
935 new->master_clone = n->master_clone;
936 new->count = count;
937 if (n->count)
938 count_scale = new->count * REG_BR_PROB_BASE / n->count;
939 else
940 count_scale = 0;
941 if (update_original)
943 n->count -= count;
944 if (n->count < 0)
945 n->count = 0;
948 for (e = n->callees;e; e=e->next_callee)
949 cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
950 update_original);
952 new->next_clone = n->next_clone;
953 new->prev_clone = n;
954 n->next_clone = new;
955 if (new->next_clone)
956 new->next_clone->prev_clone = new;
958 return new;
961 /* Return true if N is an master_clone, (see cgraph_master_clone). */
963 bool
964 cgraph_is_master_clone (struct cgraph_node *n)
966 return (n == cgraph_master_clone (n));
969 struct cgraph_node *
970 cgraph_master_clone (struct cgraph_node *n)
972 enum availability avail = cgraph_function_body_availability (n);
974 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
975 return NULL;
977 if (!n->master_clone)
978 n->master_clone = cgraph_node (n->decl);
980 return n->master_clone;
983 /* NODE is no longer nested function; update cgraph accordingly. */
984 void
985 cgraph_unnest_node (struct cgraph_node *node)
987 struct cgraph_node **node2 = &node->origin->nested;
988 gcc_assert (node->origin);
990 while (*node2 != node)
991 node2 = &(*node2)->next_nested;
992 *node2 = node->next_nested;
993 node->origin = NULL;
996 /* Return function availability. See cgraph.h for description of individual
997 return values. */
998 enum availability
999 cgraph_function_body_availability (struct cgraph_node *node)
1001 enum availability avail;
1002 gcc_assert (cgraph_function_flags_ready);
1003 if (!node->analyzed)
1004 avail = AVAIL_NOT_AVAILABLE;
1005 else if (node->local.local)
1006 avail = AVAIL_LOCAL;
1007 else if (node->local.externally_visible)
1008 avail = AVAIL_AVAILABLE;
1010 /* If the function can be overwritten, return OVERWRITABLE. Take
1011 care at least of two notable extensions - the COMDAT functions
1012 used to share template instantiations in C++ (this is symmetric
1013 to code cp_cannot_inline_tree_fn and probably shall be shared and
1014 the inlinability hooks completely eliminated).
1016 ??? Does the C++ one definition rule allow us to always return
1017 AVAIL_AVAILABLE here? That would be good reason to preserve this
1018 hook Similarly deal with extern inline functions - this is again
1019 necessary to get C++ shared functions having keyed templates
1020 right and in the C extension documentation we probably should
1021 document the requirement of both versions of function (extern
1022 inline and offline) having same side effect characteristics as
1023 good optimization is what this optimization is about. */
1025 else if (!(*targetm.binds_local_p) (node->decl)
1026 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1027 avail = AVAIL_OVERWRITABLE;
1028 else avail = AVAIL_AVAILABLE;
1030 return avail;
1033 /* Return variable availability. See cgraph.h for description of individual
1034 return values. */
1035 enum availability
1036 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1038 gcc_assert (cgraph_function_flags_ready);
1039 if (!node->finalized)
1040 return AVAIL_NOT_AVAILABLE;
1041 if (!TREE_PUBLIC (node->decl))
1042 return AVAIL_AVAILABLE;
1043 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1044 care of at least two notable extensions - the COMDAT variables
1045 used to share template instantiations in C++. */
1046 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1047 return AVAIL_OVERWRITABLE;
1048 return AVAIL_AVAILABLE;
1051 #include "gt-cgraph.h"