* testsuite/libmudflap.c/externs-1.c (main): Add return statement.
[official-gcc.git] / gcc / cgraph.c
blobb9b1b14f646e683555a8521f77a5330c4e18d8af
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains basic routines manipulating call graph and variable pool
24 The callgraph:
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
28 sharing.
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node (in
32 contrast to tree DECL nodes where we can have multiple nodes for each
33 function).
35 The mapping from declarations to call-graph nodes is done using hash table
36 based on DECL_ASSEMBLER_NAME, so it is essential for assembler name to
37 not change once the declaration is inserted into the call-graph.
38 The call-graph nodes are created lazily using cgraph_node function when
39 called for unknown declaration.
41 When built, there is one edge for each direct call. It is possible that
42 the reference will be later optimized out. The call-graph is built
43 conservatively in order to make conservative data flow analysis possible.
45 The callgraph at the moment does not represent indirect calls or calls
46 from other compilation unit. Flag NEEDED is set for each node that may
47 be accessed in such 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 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl)))
477 kill_body = true;
480 if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
482 DECL_SAVED_TREE (node->decl) = NULL;
483 DECL_STRUCT_FUNCTION (node->decl) = NULL;
484 DECL_INITIAL (node->decl) = error_mark_node;
486 cgraph_n_nodes--;
487 /* Do not free the structure itself so the walk over chain can continue. */
490 /* Notify finalize_compilation_unit that given node is reachable. */
492 void
493 cgraph_mark_reachable_node (struct cgraph_node *node)
495 if (!node->reachable && node->local.finalized)
497 notice_global_symbol (node->decl);
498 node->reachable = 1;
499 gcc_assert (!cgraph_global_info_ready);
501 node->next_needed = cgraph_nodes_queue;
502 cgraph_nodes_queue = node;
506 /* Likewise indicate that a node is needed, i.e. reachable via some
507 external means. */
509 void
510 cgraph_mark_needed_node (struct cgraph_node *node)
512 node->needed = 1;
513 cgraph_mark_reachable_node (node);
516 /* Return local info for the compiled function. */
518 struct cgraph_local_info *
519 cgraph_local_info (tree decl)
521 struct cgraph_node *node;
523 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
524 node = cgraph_node (decl);
525 return &node->local;
528 /* Return local info for the compiled function. */
530 struct cgraph_global_info *
531 cgraph_global_info (tree decl)
533 struct cgraph_node *node;
535 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
536 node = cgraph_node (decl);
537 return &node->global;
540 /* Return local info for the compiled function. */
542 struct cgraph_rtl_info *
543 cgraph_rtl_info (tree decl)
545 struct cgraph_node *node;
547 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
548 node = cgraph_node (decl);
549 if (decl != current_function_decl
550 && !TREE_ASM_WRITTEN (node->decl))
551 return NULL;
552 return &node->rtl;
555 /* Return name of the node used in debug output. */
556 const char *
557 cgraph_node_name (struct cgraph_node *node)
559 return lang_hooks.decl_printable_name (node->decl, 2);
562 /* Return name of the node used in debug output. */
563 static const char *
564 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
566 return lang_hooks.decl_printable_name (node->decl, 2);
569 /* Names used to print out the availability enum. */
570 static const char * const availability_names[] =
571 {"unset", "not_available", "overwrittable", "available", "local"};
573 /* Dump given cgraph node. */
574 void
575 dump_cgraph_node (FILE *f, struct cgraph_node *node)
577 struct cgraph_edge *edge;
578 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
579 if (node->global.inlined_to)
580 fprintf (f, " (inline copy in %s/%i)",
581 cgraph_node_name (node->global.inlined_to),
582 node->global.inlined_to->uid);
583 if (cgraph_function_flags_ready)
584 fprintf (f, " availability:%s",
585 availability_names [cgraph_function_body_availability (node)]);
586 if (node->master_clone && node->master_clone->uid != node->uid)
587 fprintf (f, "(%i)", node->master_clone->uid);
588 if (node->count)
589 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
590 (HOST_WIDEST_INT)node->count);
591 if (node->local.self_insns)
592 fprintf (f, " %i insns", node->local.self_insns);
593 if (node->global.insns && node->global.insns != node->local.self_insns)
594 fprintf (f, " (%i after inlining)", node->global.insns);
595 if (node->origin)
596 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
597 if (node->needed)
598 fprintf (f, " needed");
599 else if (node->reachable)
600 fprintf (f, " reachable");
601 if (DECL_SAVED_TREE (node->decl))
602 fprintf (f, " tree");
603 if (node->output)
604 fprintf (f, " output");
605 if (node->local.local)
606 fprintf (f, " local");
607 if (node->local.externally_visible)
608 fprintf (f, " externally_visible");
609 if (node->local.finalized)
610 fprintf (f, " finalized");
611 if (node->local.disregard_inline_limits)
612 fprintf (f, " always_inline");
613 else if (node->local.inlinable)
614 fprintf (f, " inlinable");
615 if (node->local.redefined_extern_inline)
616 fprintf (f, " redefined_extern_inline");
617 if (TREE_ASM_WRITTEN (node->decl))
618 fprintf (f, " asm_written");
620 fprintf (f, "\n called by: ");
621 for (edge = node->callers; edge; edge = edge->next_caller)
623 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
624 edge->caller->uid);
625 if (edge->count)
626 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
627 (HOST_WIDEST_INT)edge->count);
628 if (!edge->inline_failed)
629 fprintf(f, "(inlined) ");
632 fprintf (f, "\n calls: ");
633 for (edge = node->callees; edge; edge = edge->next_callee)
635 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
636 edge->callee->uid);
637 if (!edge->inline_failed)
638 fprintf(f, "(inlined) ");
639 if (edge->count)
640 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
641 (HOST_WIDEST_INT)edge->count);
642 if (edge->loop_nest)
643 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
645 fprintf (f, "\n");
648 /* Dump the callgraph. */
650 void
651 dump_cgraph (FILE *f)
653 struct cgraph_node *node;
655 fprintf (f, "callgraph:\n\n");
656 for (node = cgraph_nodes; node; node = node->next)
657 dump_cgraph_node (f, node);
660 /* Dump given cgraph node. */
661 void
662 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
664 fprintf (f, "%s:", cgraph_varpool_node_name (node));
665 fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
666 if (DECL_INITIAL (node->decl))
667 fprintf (f, " initialized");
668 if (node->needed)
669 fprintf (f, " needed");
670 if (node->analyzed)
671 fprintf (f, " analyzed");
672 if (node->finalized)
673 fprintf (f, " finalized");
674 if (node->output)
675 fprintf (f, " output");
676 if (node->externally_visible)
677 fprintf (f, " externally_visible");
678 fprintf (f, "\n");
681 /* Dump the callgraph. */
683 void
684 dump_varpool (FILE *f)
686 struct cgraph_varpool_node *node;
688 fprintf (f, "variable pool:\n\n");
689 for (node = cgraph_varpool_nodes; node; node = node->next_needed)
690 dump_cgraph_varpool_node (f, node);
693 /* Returns a hash code for P. */
695 static hashval_t
696 hash_varpool_node (const void *p)
698 const struct cgraph_varpool_node *n = p;
699 return (hashval_t) DECL_UID (n->decl);
702 /* Returns nonzero if P1 and P2 are equal. */
704 static int
705 eq_varpool_node (const void *p1, const void *p2)
707 const struct cgraph_varpool_node *n1 = p1, *n2 = p2;
708 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
711 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
712 struct cgraph_varpool_node *
713 cgraph_varpool_node (tree decl)
715 struct cgraph_varpool_node key, *node, **slot;
717 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
719 if (!cgraph_varpool_hash)
720 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
721 eq_varpool_node, NULL);
722 key.decl = decl;
723 slot = (struct cgraph_varpool_node **)
724 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
725 if (*slot)
726 return *slot;
727 node = ggc_alloc_cleared (sizeof (*node));
728 node->decl = decl;
729 node->next = cgraph_varpool_nodes;
730 cgraph_varpool_nodes = node;
731 *slot = node;
732 return node;
735 struct cgraph_varpool_node *
736 cgraph_varpool_node_for_asm (tree asmname)
738 struct cgraph_varpool_node *node;
740 for (node = cgraph_varpool_nodes; node ; node = node->next)
741 if (decl_assembler_name_equal (node->decl, asmname))
742 return node;
744 return NULL;
747 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
748 void
749 change_decl_assembler_name (tree decl, tree name)
751 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
753 SET_DECL_ASSEMBLER_NAME (decl, name);
754 return;
756 if (name == DECL_ASSEMBLER_NAME (decl))
757 return;
759 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
760 && DECL_RTL_SET_P (decl))
761 warning (0, "%D renamed after being referenced in assembly", decl);
763 SET_DECL_ASSEMBLER_NAME (decl, name);
766 /* Helper function for finalization code - add node into lists so it will
767 be analyzed and compiled. */
768 void
769 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
771 if (cgraph_varpool_last_needed_node)
772 cgraph_varpool_last_needed_node->next_needed = node;
773 cgraph_varpool_last_needed_node = node;
774 node->next_needed = NULL;
775 if (!cgraph_varpool_nodes_queue)
776 cgraph_varpool_nodes_queue = node;
777 if (!cgraph_varpool_first_unanalyzed_node)
778 cgraph_varpool_first_unanalyzed_node = node;
779 notice_global_symbol (node->decl);
782 /* Reset the queue of needed nodes. */
783 void
784 cgraph_varpool_reset_queue (void)
786 cgraph_varpool_last_needed_node = NULL;
787 cgraph_varpool_nodes_queue = NULL;
788 cgraph_varpool_first_unanalyzed_node = NULL;
791 /* Notify finalize_compilation_unit that given node is reachable
792 or needed. */
793 void
794 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
796 if (!node->needed && node->finalized)
797 cgraph_varpool_enqueue_needed_node (node);
798 node->needed = 1;
801 /* Determine if variable DECL is needed. That is, visible to something
802 either outside this translation unit, something magic in the system
803 configury, or (if not doing unit-at-a-time) to something we haven't
804 seen yet. */
806 bool
807 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
809 /* If the user told us it is used, then it must be so. */
810 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
811 return true;
813 /* ??? If the assembler name is set by hand, it is possible to assemble
814 the name later after finalizing the function and the fact is noticed
815 in assemble_name then. This is arguably a bug. */
816 if (DECL_ASSEMBLER_NAME_SET_P (decl)
817 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
818 return true;
820 /* If we decided it was needed before, but at the time we didn't have
821 the definition available, then it's still needed. */
822 if (node->needed)
823 return true;
825 /* Externally visible variables must be output. The exception is
826 COMDAT variables that must be output only when they are needed. */
827 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
828 return true;
830 if (flag_unit_at_a_time)
831 return false;
833 /* If not doing unit at a time, then we'll only defer this function
834 if its marked for inlining. Otherwise we want to emit it now. */
836 /* We want to emit COMDAT variables only when absolutely necessary. */
837 if (DECL_COMDAT (decl))
838 return false;
839 return true;
842 void
843 cgraph_varpool_finalize_decl (tree decl)
845 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
847 /* The first declaration of a variable that comes through this function
848 decides whether it is global (in C, has external linkage)
849 or local (in C, has internal linkage). So do nothing more
850 if this function has already run. */
851 if (node->finalized)
853 if (cgraph_global_info_ready || !flag_unit_at_a_time)
854 cgraph_varpool_assemble_pending_decls ();
855 return;
857 if (node->needed)
858 cgraph_varpool_enqueue_needed_node (node);
859 node->finalized = true;
861 if (decide_is_variable_needed (node, decl))
862 cgraph_varpool_mark_needed_node (node);
863 /* Since we reclaim unreachable nodes at the end of every language
864 level unit, we need to be conservative about possible entry points
865 there. */
866 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
867 cgraph_varpool_mark_needed_node (node);
868 if (cgraph_global_info_ready || !flag_unit_at_a_time)
869 cgraph_varpool_assemble_pending_decls ();
872 /* Return true when the DECL can possibly be inlined. */
873 bool
874 cgraph_function_possibly_inlined_p (tree decl)
876 if (!cgraph_global_info_ready)
877 return (DECL_INLINE (decl) && !flag_really_no_inline);
878 return DECL_POSSIBLY_INLINED (decl);
881 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
882 struct cgraph_edge *
883 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
884 tree call_stmt, int count_scale, int loop_nest)
886 struct cgraph_edge *new;
888 new = cgraph_create_edge (n, e->callee, call_stmt,
889 e->count * count_scale / REG_BR_PROB_BASE,
890 e->loop_nest + loop_nest);
892 new->inline_failed = e->inline_failed;
893 e->count -= new->count;
894 return new;
897 /* Create node representing clone of N executed COUNT times. Decrease
898 the execution counts from original node too. */
899 struct cgraph_node *
900 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest)
902 struct cgraph_node *new = cgraph_create_node ();
903 struct cgraph_edge *e;
904 int count_scale;
906 new->decl = n->decl;
907 new->origin = n->origin;
908 if (new->origin)
910 new->next_nested = new->origin->nested;
911 new->origin->nested = new;
913 new->analyzed = n->analyzed;
914 new->local = n->local;
915 new->global = n->global;
916 new->rtl = n->rtl;
917 new->master_clone = n->master_clone;
918 new->count = count;
919 if (n->count)
920 count_scale = new->count * REG_BR_PROB_BASE / n->count;
921 else
922 count_scale = 0;
923 n->count -= count;
925 for (e = n->callees;e; e=e->next_callee)
926 cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest);
928 new->next_clone = n->next_clone;
929 new->prev_clone = n;
930 n->next_clone = new;
931 if (new->next_clone)
932 new->next_clone->prev_clone = new;
934 return new;
937 /* Return true if N is an master_clone, (see cgraph_master_clone). */
939 bool
940 cgraph_is_master_clone (struct cgraph_node *n)
942 return (n == cgraph_master_clone (n));
945 struct cgraph_node *
946 cgraph_master_clone (struct cgraph_node *n)
948 enum availability avail = cgraph_function_body_availability (n);
950 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
951 return NULL;
953 if (!n->master_clone)
954 n->master_clone = cgraph_node (n->decl);
956 return n->master_clone;
959 /* NODE is no longer nested function; update cgraph accordingly. */
960 void
961 cgraph_unnest_node (struct cgraph_node *node)
963 struct cgraph_node **node2 = &node->origin->nested;
964 gcc_assert (node->origin);
966 while (*node2 != node)
967 node2 = &(*node2)->next_nested;
968 *node2 = node->next_nested;
969 node->origin = NULL;
972 /* Return function availability. See cgraph.h for description of individual
973 return values. */
974 enum availability
975 cgraph_function_body_availability (struct cgraph_node *node)
977 enum availability avail;
978 gcc_assert (cgraph_function_flags_ready);
979 if (!node->local.finalized)
980 avail = AVAIL_NOT_AVAILABLE;
981 else if (node->local.local)
982 avail = AVAIL_LOCAL;
983 else if (node->local.externally_visible)
984 avail = AVAIL_AVAILABLE;
986 /* If the function can be overwritten, return OVERWRITABLE. Take
987 care at least of two notable extensions - the COMDAT functions
988 used to share template instantiations in C++ (this is symmetric
989 to code cp_cannot_inline_tree_fn and probably shall be shared and
990 the inlinability hooks completely eliminated).
992 ??? Does the C++ one definition rule allow us to always return
993 AVAIL_AVAILABLE here? That would be good reason to preserve this
994 hook Similarly deal with extern inline functions - this is again
995 necessary to get C++ shared functions having keyed templates
996 right and in the C extension documentation we probably should
997 document the requirement of both versions of function (extern
998 inline and offline) having same side effect characteristics as
999 good optimization is what this optimization is about. */
1001 else if (!(*targetm.binds_local_p) (node->decl)
1002 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1003 avail = AVAIL_OVERWRITABLE;
1004 else avail = AVAIL_AVAILABLE;
1006 return avail;
1009 /* Return variable availability. See cgraph.h for description of individual
1010 return values. */
1011 enum availability
1012 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1014 gcc_assert (cgraph_function_flags_ready);
1015 if (!node->finalized)
1016 return AVAIL_NOT_AVAILABLE;
1017 if (!TREE_PUBLIC (node->decl))
1018 return AVAIL_AVAILABLE;
1019 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1020 care of at least two notable extensions - the COMDAT variables
1021 used to share template instantiations in C++. */
1022 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1023 return AVAIL_OVERWRITABLE;
1024 return AVAIL_AVAILABLE;
1027 #include "gt-cgraph.h"