* tree.c (find_tree_t, find_tree): Remove.
[official-gcc.git] / gcc / cgraph.c
blob10a22334771407e0deba772e60dff63dfd8f4906
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;
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 /* Linked list of cgraph asm nodes. */
141 struct cgraph_asm_node *cgraph_asm_nodes;
143 /* Last node in cgraph_asm_nodes. */
144 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
146 /* The order index of the next cgraph node to be created. This is
147 used so that we can sort the cgraph nodes in order by when we saw
148 them, to support -fno-toplevel-reorder. */
149 int cgraph_order;
151 static hashval_t hash_node (const void *);
152 static int eq_node (const void *, const void *);
154 /* Returns a hash code for P. */
156 static hashval_t
157 hash_node (const void *p)
159 const struct cgraph_node *n = (const struct cgraph_node *) p;
160 return (hashval_t) DECL_UID (n->decl);
163 /* Returns nonzero if P1 and P2 are equal. */
165 static int
166 eq_node (const void *p1, const void *p2)
168 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
169 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
170 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
173 /* Allocate new callgraph node and insert it into basic data structures. */
174 static struct cgraph_node *
175 cgraph_create_node (void)
177 struct cgraph_node *node;
179 node = GGC_CNEW (struct cgraph_node);
180 node->next = cgraph_nodes;
181 node->uid = cgraph_max_uid++;
182 node->order = cgraph_order++;
183 if (cgraph_nodes)
184 cgraph_nodes->previous = node;
185 node->previous = NULL;
186 node->global.estimated_growth = INT_MIN;
187 cgraph_nodes = node;
188 cgraph_n_nodes++;
189 return node;
192 /* Return cgraph node assigned to DECL. Create new one when needed. */
193 struct cgraph_node *
194 cgraph_node (tree decl)
196 struct cgraph_node key, *node, **slot;
198 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
200 if (!cgraph_hash)
201 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
203 key.decl = decl;
205 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
207 if (*slot)
209 node = *slot;
210 if (!node->master_clone)
211 node->master_clone = node;
212 return node;
215 node = cgraph_create_node ();
216 node->decl = decl;
217 *slot = node;
218 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
220 node->origin = cgraph_node (DECL_CONTEXT (decl));
221 node->next_nested = node->origin->nested;
222 node->origin->nested = node;
223 node->master_clone = node;
225 return node;
228 /* Insert already constructed node into hashtable. */
230 void
231 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
233 struct cgraph_node **slot;
235 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
237 gcc_assert (!*slot);
238 *slot = node;
241 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
243 static bool
244 decl_assembler_name_equal (tree decl, tree asmname)
246 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
248 if (decl_asmname == asmname)
249 return true;
251 /* If the target assembler name was set by the user, things are trickier.
252 We have a leading '*' to begin with. After that, it's arguable what
253 is the correct thing to do with -fleading-underscore. Arguably, we've
254 historically been doing the wrong thing in assemble_alias by always
255 printing the leading underscore. Since we're not changing that, make
256 sure user_label_prefix follows the '*' before matching. */
257 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
259 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
260 size_t ulp_len = strlen (user_label_prefix);
262 if (ulp_len == 0)
264 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
265 decl_str += ulp_len;
266 else
267 return false;
269 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
272 return false;
276 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
277 Return NULL if there's no such node. */
279 struct cgraph_node *
280 cgraph_node_for_asm (tree asmname)
282 struct cgraph_node *node;
284 for (node = cgraph_nodes; node ; node = node->next)
285 if (decl_assembler_name_equal (node->decl, asmname))
286 return node;
288 return NULL;
291 /* Return callgraph edge representing CALL_EXPR statement. */
292 struct cgraph_edge *
293 cgraph_edge (struct cgraph_node *node, tree call_stmt)
295 struct cgraph_edge *e;
297 /* This loop may turn out to be performance problem. In such case adding
298 hashtables into call nodes with very many edges is probably best
299 solution. It is not good idea to add pointer into CALL_EXPR itself
300 because we want to make possible having multiple cgraph nodes representing
301 different clones of the same body before the body is actually cloned. */
302 for (e = node->callees; e; e= e->next_callee)
303 if (e->call_stmt == call_stmt)
304 break;
305 return e;
308 /* Create edge from CALLER to CALLEE in the cgraph. */
310 struct cgraph_edge *
311 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
312 tree call_stmt, gcov_type count, int nest)
314 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
315 #ifdef ENABLE_CHECKING
316 struct cgraph_edge *e;
318 for (e = caller->callees; e; e = e->next_callee)
319 gcc_assert (e->call_stmt != call_stmt);
320 #endif
322 gcc_assert (get_call_expr_in (call_stmt));
324 if (!DECL_SAVED_TREE (callee->decl))
325 edge->inline_failed = N_("function body not available");
326 else if (callee->local.redefined_extern_inline)
327 edge->inline_failed = N_("redefined extern inline functions are not "
328 "considered for inlining");
329 else if (callee->local.inlinable)
330 edge->inline_failed = N_("function not considered for inlining");
331 else
332 edge->inline_failed = N_("function not inlinable");
334 edge->aux = NULL;
336 edge->caller = caller;
337 edge->callee = callee;
338 edge->call_stmt = call_stmt;
339 edge->prev_caller = NULL;
340 edge->next_caller = callee->callers;
341 if (callee->callers)
342 callee->callers->prev_caller = edge;
343 edge->prev_callee = NULL;
344 edge->next_callee = caller->callees;
345 if (caller->callees)
346 caller->callees->prev_callee = edge;
347 caller->callees = edge;
348 callee->callers = edge;
349 edge->count = count;
350 edge->loop_nest = nest;
351 return edge;
354 /* Remove the edge E from the list of the callers of the callee. */
356 static inline void
357 cgraph_edge_remove_callee (struct cgraph_edge *e)
359 if (e->prev_caller)
360 e->prev_caller->next_caller = e->next_caller;
361 if (e->next_caller)
362 e->next_caller->prev_caller = e->prev_caller;
363 if (!e->prev_caller)
364 e->callee->callers = e->next_caller;
367 /* Remove the edge E from the list of the callees of the caller. */
369 static inline void
370 cgraph_edge_remove_caller (struct cgraph_edge *e)
372 if (e->prev_callee)
373 e->prev_callee->next_callee = e->next_callee;
374 if (e->next_callee)
375 e->next_callee->prev_callee = e->prev_callee;
376 if (!e->prev_callee)
377 e->caller->callees = e->next_callee;
380 /* Remove the edge E in the cgraph. */
382 void
383 cgraph_remove_edge (struct cgraph_edge *e)
385 /* Remove from callers list of the callee. */
386 cgraph_edge_remove_callee (e);
388 /* Remove from callees list of the callers. */
389 cgraph_edge_remove_caller (e);
392 /* Redirect callee of E to N. The function does not update underlying
393 call expression. */
395 void
396 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
398 /* Remove from callers list of the current callee. */
399 cgraph_edge_remove_callee (e);
401 /* Insert to callers list of the new callee. */
402 e->prev_caller = NULL;
403 if (n->callers)
404 n->callers->prev_caller = e;
405 e->next_caller = n->callers;
406 n->callers = e;
407 e->callee = n;
410 /* Remove all callees from the node. */
412 void
413 cgraph_node_remove_callees (struct cgraph_node *node)
415 struct cgraph_edge *e;
417 /* It is sufficient to remove the edges from the lists of callers of
418 the callees. The callee list of the node can be zapped with one
419 assignment. */
420 for (e = node->callees; e; e = e->next_callee)
421 cgraph_edge_remove_callee (e);
422 node->callees = NULL;
425 /* Remove all callers from the node. */
427 static void
428 cgraph_node_remove_callers (struct cgraph_node *node)
430 struct cgraph_edge *e;
432 /* It is sufficient to remove the edges from the lists of callees of
433 the callers. The caller list of the node can be zapped with one
434 assignment. */
435 for (e = node->callers; e; e = e->next_caller)
436 cgraph_edge_remove_caller (e);
437 node->callers = NULL;
440 /* Remove the node from cgraph. */
442 void
443 cgraph_remove_node (struct cgraph_node *node)
445 void **slot;
446 bool kill_body = false;
448 cgraph_node_remove_callers (node);
449 cgraph_node_remove_callees (node);
450 while (node->nested)
451 cgraph_remove_node (node->nested);
452 if (node->origin)
454 struct cgraph_node **node2 = &node->origin->nested;
456 while (*node2 != node)
457 node2 = &(*node2)->next_nested;
458 *node2 = node->next_nested;
460 if (node->previous)
461 node->previous->next = node->next;
462 else
463 cgraph_nodes = node->next;
464 if (node->next)
465 node->next->previous = node->previous;
466 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
467 if (*slot == node)
469 if (node->next_clone)
471 struct cgraph_node *new_node = node->next_clone;
472 struct cgraph_node *n;
474 /* Make the next clone be the master clone */
475 for (n = new_node; n; n = n->next_clone)
476 n->master_clone = new_node;
478 *slot = new_node;
479 node->next_clone->prev_clone = NULL;
481 else
483 htab_clear_slot (cgraph_hash, slot);
484 kill_body = true;
487 else
489 node->prev_clone->next_clone = node->next_clone;
490 if (node->next_clone)
491 node->next_clone->prev_clone = node->prev_clone;
494 /* While all the clones are removed after being proceeded, the function
495 itself is kept in the cgraph even after it is compiled. Check whether
496 we are done with this body and reclaim it proactively if this is the case.
498 if (!kill_body && *slot)
500 struct cgraph_node *n = (struct cgraph_node *) *slot;
501 if (!n->next_clone && !n->global.inlined_to
502 && (cgraph_global_info_ready
503 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
504 kill_body = true;
507 if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
509 DECL_SAVED_TREE (node->decl) = NULL;
510 DECL_STRUCT_FUNCTION (node->decl) = NULL;
511 DECL_INITIAL (node->decl) = error_mark_node;
513 cgraph_n_nodes--;
514 /* Do not free the structure itself so the walk over chain can continue. */
517 /* Notify finalize_compilation_unit that given node is reachable. */
519 void
520 cgraph_mark_reachable_node (struct cgraph_node *node)
522 if (!node->reachable && node->local.finalized)
524 notice_global_symbol (node->decl);
525 node->reachable = 1;
526 gcc_assert (!cgraph_global_info_ready);
528 node->next_needed = cgraph_nodes_queue;
529 cgraph_nodes_queue = node;
533 /* Likewise indicate that a node is needed, i.e. reachable via some
534 external means. */
536 void
537 cgraph_mark_needed_node (struct cgraph_node *node)
539 node->needed = 1;
540 cgraph_mark_reachable_node (node);
543 /* Return local info for the compiled function. */
545 struct cgraph_local_info *
546 cgraph_local_info (tree decl)
548 struct cgraph_node *node;
550 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
551 node = cgraph_node (decl);
552 return &node->local;
555 /* Return local info for the compiled function. */
557 struct cgraph_global_info *
558 cgraph_global_info (tree decl)
560 struct cgraph_node *node;
562 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
563 node = cgraph_node (decl);
564 return &node->global;
567 /* Return local info for the compiled function. */
569 struct cgraph_rtl_info *
570 cgraph_rtl_info (tree decl)
572 struct cgraph_node *node;
574 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
575 node = cgraph_node (decl);
576 if (decl != current_function_decl
577 && !TREE_ASM_WRITTEN (node->decl))
578 return NULL;
579 return &node->rtl;
582 /* Return name of the node used in debug output. */
583 const char *
584 cgraph_node_name (struct cgraph_node *node)
586 return lang_hooks.decl_printable_name (node->decl, 2);
589 /* Return name of the node used in debug output. */
590 static const char *
591 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
593 return lang_hooks.decl_printable_name (node->decl, 2);
596 /* Names used to print out the availability enum. */
597 static const char * const availability_names[] =
598 {"unset", "not_available", "overwrittable", "available", "local"};
600 /* Dump given cgraph node. */
601 void
602 dump_cgraph_node (FILE *f, struct cgraph_node *node)
604 struct cgraph_edge *edge;
605 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
606 if (node->global.inlined_to)
607 fprintf (f, " (inline copy in %s/%i)",
608 cgraph_node_name (node->global.inlined_to),
609 node->global.inlined_to->uid);
610 if (cgraph_function_flags_ready)
611 fprintf (f, " availability:%s",
612 availability_names [cgraph_function_body_availability (node)]);
613 if (node->master_clone && node->master_clone->uid != node->uid)
614 fprintf (f, "(%i)", node->master_clone->uid);
615 if (node->count)
616 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
617 (HOST_WIDEST_INT)node->count);
618 if (node->local.self_insns)
619 fprintf (f, " %i insns", node->local.self_insns);
620 if (node->global.insns && node->global.insns != node->local.self_insns)
621 fprintf (f, " (%i after inlining)", node->global.insns);
622 if (node->origin)
623 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
624 if (node->needed)
625 fprintf (f, " needed");
626 else if (node->reachable)
627 fprintf (f, " reachable");
628 if (DECL_SAVED_TREE (node->decl))
629 fprintf (f, " tree");
630 if (node->output)
631 fprintf (f, " output");
632 if (node->local.local)
633 fprintf (f, " local");
634 if (node->local.externally_visible)
635 fprintf (f, " externally_visible");
636 if (node->local.finalized)
637 fprintf (f, " finalized");
638 if (node->local.disregard_inline_limits)
639 fprintf (f, " always_inline");
640 else if (node->local.inlinable)
641 fprintf (f, " inlinable");
642 if (node->local.redefined_extern_inline)
643 fprintf (f, " redefined_extern_inline");
644 if (TREE_ASM_WRITTEN (node->decl))
645 fprintf (f, " asm_written");
647 fprintf (f, "\n called by: ");
648 for (edge = node->callers; edge; edge = edge->next_caller)
650 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
651 edge->caller->uid);
652 if (edge->count)
653 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
654 (HOST_WIDEST_INT)edge->count);
655 if (!edge->inline_failed)
656 fprintf(f, "(inlined) ");
659 fprintf (f, "\n calls: ");
660 for (edge = node->callees; edge; edge = edge->next_callee)
662 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
663 edge->callee->uid);
664 if (!edge->inline_failed)
665 fprintf(f, "(inlined) ");
666 if (edge->count)
667 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
668 (HOST_WIDEST_INT)edge->count);
669 if (edge->loop_nest)
670 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
672 fprintf (f, "\n");
675 /* Dump the callgraph. */
677 void
678 dump_cgraph (FILE *f)
680 struct cgraph_node *node;
682 fprintf (f, "callgraph:\n\n");
683 for (node = cgraph_nodes; node; node = node->next)
684 dump_cgraph_node (f, node);
687 /* Dump given cgraph node. */
688 void
689 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
691 fprintf (f, "%s:", cgraph_varpool_node_name (node));
692 fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
693 if (DECL_INITIAL (node->decl))
694 fprintf (f, " initialized");
695 if (node->needed)
696 fprintf (f, " needed");
697 if (node->analyzed)
698 fprintf (f, " analyzed");
699 if (node->finalized)
700 fprintf (f, " finalized");
701 if (node->output)
702 fprintf (f, " output");
703 if (node->externally_visible)
704 fprintf (f, " externally_visible");
705 fprintf (f, "\n");
708 /* Dump the callgraph. */
710 void
711 dump_varpool (FILE *f)
713 struct cgraph_varpool_node *node;
715 fprintf (f, "variable pool:\n\n");
716 for (node = cgraph_varpool_nodes; node; node = node->next_needed)
717 dump_cgraph_varpool_node (f, node);
720 /* Returns a hash code for P. */
722 static hashval_t
723 hash_varpool_node (const void *p)
725 const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
726 return (hashval_t) DECL_UID (n->decl);
729 /* Returns nonzero if P1 and P2 are equal. */
731 static int
732 eq_varpool_node (const void *p1, const void *p2)
734 const struct cgraph_varpool_node *n1 =
735 (const struct cgraph_varpool_node *) p1;
736 const struct cgraph_varpool_node *n2 =
737 (const struct cgraph_varpool_node *) p2;
738 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
741 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
742 struct cgraph_varpool_node *
743 cgraph_varpool_node (tree decl)
745 struct cgraph_varpool_node key, *node, **slot;
747 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
749 if (!cgraph_varpool_hash)
750 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
751 eq_varpool_node, NULL);
752 key.decl = decl;
753 slot = (struct cgraph_varpool_node **)
754 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
755 if (*slot)
756 return *slot;
757 node = GGC_CNEW (struct cgraph_varpool_node);
758 node->decl = decl;
759 node->order = cgraph_order++;
760 node->next = cgraph_varpool_nodes;
761 cgraph_varpool_nodes = node;
762 *slot = node;
763 return node;
766 struct cgraph_varpool_node *
767 cgraph_varpool_node_for_asm (tree asmname)
769 struct cgraph_varpool_node *node;
771 for (node = cgraph_varpool_nodes; node ; node = node->next)
772 if (decl_assembler_name_equal (node->decl, asmname))
773 return node;
775 return NULL;
778 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
779 void
780 change_decl_assembler_name (tree decl, tree name)
782 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
784 SET_DECL_ASSEMBLER_NAME (decl, name);
785 return;
787 if (name == DECL_ASSEMBLER_NAME (decl))
788 return;
790 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
791 && DECL_RTL_SET_P (decl))
792 warning (0, "%D renamed after being referenced in assembly", decl);
794 SET_DECL_ASSEMBLER_NAME (decl, name);
797 /* Helper function for finalization code - add node into lists so it will
798 be analyzed and compiled. */
799 void
800 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
802 if (cgraph_varpool_last_needed_node)
803 cgraph_varpool_last_needed_node->next_needed = node;
804 cgraph_varpool_last_needed_node = node;
805 node->next_needed = NULL;
806 if (!cgraph_varpool_nodes_queue)
807 cgraph_varpool_nodes_queue = node;
808 if (!cgraph_varpool_first_unanalyzed_node)
809 cgraph_varpool_first_unanalyzed_node = node;
810 notice_global_symbol (node->decl);
813 /* Reset the queue of needed nodes. */
814 void
815 cgraph_varpool_reset_queue (void)
817 cgraph_varpool_last_needed_node = NULL;
818 cgraph_varpool_nodes_queue = NULL;
819 cgraph_varpool_first_unanalyzed_node = NULL;
822 /* Notify finalize_compilation_unit that given node is reachable
823 or needed. */
824 void
825 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
827 if (!node->needed && node->finalized)
828 cgraph_varpool_enqueue_needed_node (node);
829 node->needed = 1;
832 /* Determine if variable DECL is needed. That is, visible to something
833 either outside this translation unit, something magic in the system
834 configury, or (if not doing unit-at-a-time) to something we haven't
835 seen yet. */
837 bool
838 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
840 /* If the user told us it is used, then it must be so. */
841 if (node->externally_visible
842 || lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
843 return true;
845 /* ??? If the assembler name is set by hand, it is possible to assemble
846 the name later after finalizing the function and the fact is noticed
847 in assemble_name then. This is arguably a bug. */
848 if (DECL_ASSEMBLER_NAME_SET_P (decl)
849 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
850 return true;
852 /* If we decided it was needed before, but at the time we didn't have
853 the definition available, then it's still needed. */
854 if (node->needed)
855 return true;
857 /* Externally visible variables must be output. The exception is
858 COMDAT variables that must be output only when they are needed. */
859 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
860 return true;
862 /* When not reordering top level variables, we have to assume that
863 we are going to keep everything. */
864 if (flag_unit_at_a_time && flag_toplevel_reorder)
865 return false;
867 /* We want to emit COMDAT variables only when absolutely necessary. */
868 if (DECL_COMDAT (decl))
869 return false;
870 return true;
873 void
874 cgraph_varpool_finalize_decl (tree decl)
876 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
878 /* The first declaration of a variable that comes through this function
879 decides whether it is global (in C, has external linkage)
880 or local (in C, has internal linkage). So do nothing more
881 if this function has already run. */
882 if (node->finalized)
884 if (cgraph_global_info_ready || !flag_unit_at_a_time)
885 cgraph_varpool_assemble_pending_decls ();
886 return;
888 if (node->needed)
889 cgraph_varpool_enqueue_needed_node (node);
890 node->finalized = true;
892 if (decide_is_variable_needed (node, decl))
893 cgraph_varpool_mark_needed_node (node);
894 /* Since we reclaim unreachable nodes at the end of every language
895 level unit, we need to be conservative about possible entry points
896 there. */
897 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
898 cgraph_varpool_mark_needed_node (node);
899 if (cgraph_global_info_ready || !flag_unit_at_a_time)
900 cgraph_varpool_assemble_pending_decls ();
903 /* Add a top-level asm statement to the list. */
905 struct cgraph_asm_node *
906 cgraph_add_asm_node (tree asm_str)
908 struct cgraph_asm_node *node;
910 node = GGC_CNEW (struct cgraph_asm_node);
911 node->asm_str = asm_str;
912 node->order = cgraph_order++;
913 node->next = NULL;
914 if (cgraph_asm_nodes == NULL)
915 cgraph_asm_nodes = node;
916 else
917 cgraph_asm_last_node->next = node;
918 cgraph_asm_last_node = node;
919 return node;
922 /* Return true when the DECL can possibly be inlined. */
923 bool
924 cgraph_function_possibly_inlined_p (tree decl)
926 if (!cgraph_global_info_ready)
927 return (DECL_INLINE (decl) && !flag_really_no_inline);
928 return DECL_POSSIBLY_INLINED (decl);
931 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
932 struct cgraph_edge *
933 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
934 tree call_stmt, gcov_type count_scale, int loop_nest,
935 bool update_original)
937 struct cgraph_edge *new;
939 new = cgraph_create_edge (n, e->callee, call_stmt,
940 e->count * count_scale / REG_BR_PROB_BASE,
941 e->loop_nest + loop_nest);
943 new->inline_failed = e->inline_failed;
944 if (update_original)
946 e->count -= new->count;
947 if (e->count < 0)
948 e->count = 0;
950 return new;
953 /* Create node representing clone of N executed COUNT times. Decrease
954 the execution counts from original node too.
956 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
957 function's profile to reflect the fact that part of execution is handled
958 by node. */
959 struct cgraph_node *
960 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
961 bool update_original)
963 struct cgraph_node *new = cgraph_create_node ();
964 struct cgraph_edge *e;
965 gcov_type count_scale;
967 new->decl = n->decl;
968 new->origin = n->origin;
969 if (new->origin)
971 new->next_nested = new->origin->nested;
972 new->origin->nested = new;
974 new->analyzed = n->analyzed;
975 new->local = n->local;
976 new->global = n->global;
977 new->rtl = n->rtl;
978 new->master_clone = n->master_clone;
979 new->count = count;
980 if (n->count)
981 count_scale = new->count * REG_BR_PROB_BASE / n->count;
982 else
983 count_scale = 0;
984 if (update_original)
986 n->count -= count;
987 if (n->count < 0)
988 n->count = 0;
991 for (e = n->callees;e; e=e->next_callee)
992 cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
993 update_original);
995 new->next_clone = n->next_clone;
996 new->prev_clone = n;
997 n->next_clone = new;
998 if (new->next_clone)
999 new->next_clone->prev_clone = new;
1001 return new;
1004 /* Return true if N is an master_clone, (see cgraph_master_clone). */
1006 bool
1007 cgraph_is_master_clone (struct cgraph_node *n)
1009 return (n == cgraph_master_clone (n));
1012 struct cgraph_node *
1013 cgraph_master_clone (struct cgraph_node *n)
1015 enum availability avail = cgraph_function_body_availability (n);
1017 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1018 return NULL;
1020 if (!n->master_clone)
1021 n->master_clone = cgraph_node (n->decl);
1023 return n->master_clone;
1026 /* NODE is no longer nested function; update cgraph accordingly. */
1027 void
1028 cgraph_unnest_node (struct cgraph_node *node)
1030 struct cgraph_node **node2 = &node->origin->nested;
1031 gcc_assert (node->origin);
1033 while (*node2 != node)
1034 node2 = &(*node2)->next_nested;
1035 *node2 = node->next_nested;
1036 node->origin = NULL;
1039 /* Return function availability. See cgraph.h for description of individual
1040 return values. */
1041 enum availability
1042 cgraph_function_body_availability (struct cgraph_node *node)
1044 enum availability avail;
1045 gcc_assert (cgraph_function_flags_ready);
1046 if (!node->analyzed)
1047 avail = AVAIL_NOT_AVAILABLE;
1048 else if (node->local.local)
1049 avail = AVAIL_LOCAL;
1050 else if (node->local.externally_visible)
1051 avail = AVAIL_AVAILABLE;
1053 /* If the function can be overwritten, return OVERWRITABLE. Take
1054 care at least of two notable extensions - the COMDAT functions
1055 used to share template instantiations in C++ (this is symmetric
1056 to code cp_cannot_inline_tree_fn and probably shall be shared and
1057 the inlinability hooks completely eliminated).
1059 ??? Does the C++ one definition rule allow us to always return
1060 AVAIL_AVAILABLE here? That would be good reason to preserve this
1061 hook Similarly deal with extern inline functions - this is again
1062 necessary to get C++ shared functions having keyed templates
1063 right and in the C extension documentation we probably should
1064 document the requirement of both versions of function (extern
1065 inline and offline) having same side effect characteristics as
1066 good optimization is what this optimization is about. */
1068 else if (!(*targetm.binds_local_p) (node->decl)
1069 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1070 avail = AVAIL_OVERWRITABLE;
1071 else avail = AVAIL_AVAILABLE;
1073 return avail;
1076 /* Return variable availability. See cgraph.h for description of individual
1077 return values. */
1078 enum availability
1079 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1081 gcc_assert (cgraph_function_flags_ready);
1082 if (!node->finalized)
1083 return AVAIL_NOT_AVAILABLE;
1084 if (!TREE_PUBLIC (node->decl))
1085 return AVAIL_AVAILABLE;
1086 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1087 care of at least two notable extensions - the COMDAT variables
1088 used to share template instantiations in C++. */
1089 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1090 return AVAIL_OVERWRITABLE;
1091 return AVAIL_AVAILABLE;
1094 #include "gt-cgraph.h"