* doc/invoke.texi: Add cpu_type power6.
[official-gcc.git] / gcc / cgraph.c
blob04ff09452cdd2ae53b0faf9dd5833e665c8236df
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 /* Queue of cgraph nodes scheduled to be expanded. This is a
117 secondary queue used during optimization to accommodate passes that
118 may generate new functions that need to be optimized and expanded. */
119 struct cgraph_node *cgraph_expand_queue;
121 /* Number of nodes in existence. */
122 int cgraph_n_nodes;
124 /* Maximal uid used in cgraph nodes. */
125 int cgraph_max_uid;
127 /* Set when whole unit has been analyzed so we can access global info. */
128 bool cgraph_global_info_ready = false;
130 /* Set when the cgraph is fully build and the basic flags are computed. */
131 bool cgraph_function_flags_ready = false;
133 /* Hash table used to convert declarations into nodes. */
134 static GTY((param_is (struct cgraph_varpool_node))) htab_t cgraph_varpool_hash;
136 /* Queue of cgraph nodes scheduled to be lowered and output. */
137 struct cgraph_varpool_node *cgraph_varpool_nodes_queue, *cgraph_varpool_first_unanalyzed_node;
139 /* The linked list of cgraph varpool nodes. */
140 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_nodes;
142 /* End of the varpool queue. Needs to be QTYed to work with PCH. */
143 static GTY(()) struct cgraph_varpool_node *cgraph_varpool_last_needed_node;
145 /* Linked list of cgraph asm nodes. */
146 struct cgraph_asm_node *cgraph_asm_nodes;
148 /* Last node in cgraph_asm_nodes. */
149 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
151 /* The order index of the next cgraph node to be created. This is
152 used so that we can sort the cgraph nodes in order by when we saw
153 them, to support -fno-toplevel-reorder. */
154 int cgraph_order;
156 static hashval_t hash_node (const void *);
157 static int eq_node (const void *, const void *);
159 /* Returns a hash code for P. */
161 static hashval_t
162 hash_node (const void *p)
164 const struct cgraph_node *n = (const struct cgraph_node *) p;
165 return (hashval_t) DECL_UID (n->decl);
168 /* Returns nonzero if P1 and P2 are equal. */
170 static int
171 eq_node (const void *p1, const void *p2)
173 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
174 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
175 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
178 /* Allocate new callgraph node and insert it into basic data structures. */
179 static struct cgraph_node *
180 cgraph_create_node (void)
182 struct cgraph_node *node;
184 node = GGC_CNEW (struct cgraph_node);
185 node->next = cgraph_nodes;
186 node->uid = cgraph_max_uid++;
187 node->order = cgraph_order++;
188 if (cgraph_nodes)
189 cgraph_nodes->previous = node;
190 node->previous = NULL;
191 node->global.estimated_growth = INT_MIN;
192 cgraph_nodes = node;
193 cgraph_n_nodes++;
194 return node;
197 /* Return cgraph node assigned to DECL. Create new one when needed. */
198 struct cgraph_node *
199 cgraph_node (tree decl)
201 struct cgraph_node key, *node, **slot;
203 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
205 if (!cgraph_hash)
206 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
208 key.decl = decl;
210 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
212 if (*slot)
214 node = *slot;
215 if (!node->master_clone)
216 node->master_clone = node;
217 return node;
220 node = cgraph_create_node ();
221 node->decl = decl;
222 *slot = node;
223 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
225 node->origin = cgraph_node (DECL_CONTEXT (decl));
226 node->next_nested = node->origin->nested;
227 node->origin->nested = node;
228 node->master_clone = node;
230 return node;
233 /* Insert already constructed node into hashtable. */
235 void
236 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
238 struct cgraph_node **slot;
240 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
242 gcc_assert (!*slot);
243 *slot = node;
246 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
248 static bool
249 decl_assembler_name_equal (tree decl, tree asmname)
251 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
253 if (decl_asmname == asmname)
254 return true;
256 /* If the target assembler name was set by the user, things are trickier.
257 We have a leading '*' to begin with. After that, it's arguable what
258 is the correct thing to do with -fleading-underscore. Arguably, we've
259 historically been doing the wrong thing in assemble_alias by always
260 printing the leading underscore. Since we're not changing that, make
261 sure user_label_prefix follows the '*' before matching. */
262 if (IDENTIFIER_POINTER (decl_asmname)[0] == '*')
264 const char *decl_str = IDENTIFIER_POINTER (decl_asmname) + 1;
265 size_t ulp_len = strlen (user_label_prefix);
267 if (ulp_len == 0)
269 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
270 decl_str += ulp_len;
271 else
272 return false;
274 return strcmp (decl_str, IDENTIFIER_POINTER (asmname)) == 0;
277 return false;
281 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
282 Return NULL if there's no such node. */
284 struct cgraph_node *
285 cgraph_node_for_asm (tree asmname)
287 struct cgraph_node *node;
289 for (node = cgraph_nodes; node ; node = node->next)
290 if (decl_assembler_name_equal (node->decl, asmname))
291 return node;
293 return NULL;
296 /* Return callgraph edge representing CALL_EXPR statement. */
297 struct cgraph_edge *
298 cgraph_edge (struct cgraph_node *node, tree call_stmt)
300 struct cgraph_edge *e;
302 /* This loop may turn out to be performance problem. In such case adding
303 hashtables into call nodes with very many edges is probably best
304 solution. It is not good idea to add pointer into CALL_EXPR itself
305 because we want to make possible having multiple cgraph nodes representing
306 different clones of the same body before the body is actually cloned. */
307 for (e = node->callees; e; e= e->next_callee)
308 if (e->call_stmt == call_stmt)
309 break;
310 return e;
313 /* Create edge from CALLER to CALLEE in the cgraph. */
315 struct cgraph_edge *
316 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
317 tree call_stmt, gcov_type count, int nest)
319 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
320 #ifdef ENABLE_CHECKING
321 struct cgraph_edge *e;
323 for (e = caller->callees; e; e = e->next_callee)
324 gcc_assert (e->call_stmt != call_stmt);
325 #endif
327 gcc_assert (get_call_expr_in (call_stmt));
329 if (!DECL_SAVED_TREE (callee->decl))
330 edge->inline_failed = N_("function body not available");
331 else if (callee->local.redefined_extern_inline)
332 edge->inline_failed = N_("redefined extern inline functions are not "
333 "considered for inlining");
334 else if (callee->local.inlinable)
335 edge->inline_failed = N_("function not considered for inlining");
336 else
337 edge->inline_failed = N_("function not inlinable");
339 edge->aux = NULL;
341 edge->caller = caller;
342 edge->callee = callee;
343 edge->call_stmt = call_stmt;
344 edge->prev_caller = NULL;
345 edge->next_caller = callee->callers;
346 if (callee->callers)
347 callee->callers->prev_caller = edge;
348 edge->prev_callee = NULL;
349 edge->next_callee = caller->callees;
350 if (caller->callees)
351 caller->callees->prev_callee = edge;
352 caller->callees = edge;
353 callee->callers = edge;
354 edge->count = count;
355 edge->loop_nest = nest;
356 return edge;
359 /* Remove the edge E from the list of the callers of the callee. */
361 static inline void
362 cgraph_edge_remove_callee (struct cgraph_edge *e)
364 if (e->prev_caller)
365 e->prev_caller->next_caller = e->next_caller;
366 if (e->next_caller)
367 e->next_caller->prev_caller = e->prev_caller;
368 if (!e->prev_caller)
369 e->callee->callers = e->next_caller;
372 /* Remove the edge E from the list of the callees of the caller. */
374 static inline void
375 cgraph_edge_remove_caller (struct cgraph_edge *e)
377 if (e->prev_callee)
378 e->prev_callee->next_callee = e->next_callee;
379 if (e->next_callee)
380 e->next_callee->prev_callee = e->prev_callee;
381 if (!e->prev_callee)
382 e->caller->callees = e->next_callee;
385 /* Remove the edge E in the cgraph. */
387 void
388 cgraph_remove_edge (struct cgraph_edge *e)
390 /* Remove from callers list of the callee. */
391 cgraph_edge_remove_callee (e);
393 /* Remove from callees list of the callers. */
394 cgraph_edge_remove_caller (e);
397 /* Redirect callee of E to N. The function does not update underlying
398 call expression. */
400 void
401 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
403 /* Remove from callers list of the current callee. */
404 cgraph_edge_remove_callee (e);
406 /* Insert to callers list of the new callee. */
407 e->prev_caller = NULL;
408 if (n->callers)
409 n->callers->prev_caller = e;
410 e->next_caller = n->callers;
411 n->callers = e;
412 e->callee = n;
415 /* Remove all callees from the node. */
417 void
418 cgraph_node_remove_callees (struct cgraph_node *node)
420 struct cgraph_edge *e;
422 /* It is sufficient to remove the edges from the lists of callers of
423 the callees. The callee list of the node can be zapped with one
424 assignment. */
425 for (e = node->callees; e; e = e->next_callee)
426 cgraph_edge_remove_callee (e);
427 node->callees = NULL;
430 /* Remove all callers from the node. */
432 static void
433 cgraph_node_remove_callers (struct cgraph_node *node)
435 struct cgraph_edge *e;
437 /* It is sufficient to remove the edges from the lists of callees of
438 the callers. The caller list of the node can be zapped with one
439 assignment. */
440 for (e = node->callers; e; e = e->next_caller)
441 cgraph_edge_remove_caller (e);
442 node->callers = NULL;
445 /* Remove the node from cgraph. */
447 void
448 cgraph_remove_node (struct cgraph_node *node)
450 void **slot;
451 bool kill_body = false;
453 cgraph_node_remove_callers (node);
454 cgraph_node_remove_callees (node);
455 while (node->nested)
456 cgraph_remove_node (node->nested);
457 if (node->origin)
459 struct cgraph_node **node2 = &node->origin->nested;
461 while (*node2 != node)
462 node2 = &(*node2)->next_nested;
463 *node2 = node->next_nested;
465 if (node->previous)
466 node->previous->next = node->next;
467 else
468 cgraph_nodes = node->next;
469 if (node->next)
470 node->next->previous = node->previous;
471 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
472 if (*slot == node)
474 if (node->next_clone)
476 struct cgraph_node *new_node = node->next_clone;
477 struct cgraph_node *n;
479 /* Make the next clone be the master clone */
480 for (n = new_node; n; n = n->next_clone)
481 n->master_clone = new_node;
483 *slot = new_node;
484 node->next_clone->prev_clone = NULL;
486 else
488 htab_clear_slot (cgraph_hash, slot);
489 kill_body = true;
492 else
494 node->prev_clone->next_clone = node->next_clone;
495 if (node->next_clone)
496 node->next_clone->prev_clone = node->prev_clone;
499 /* While all the clones are removed after being proceeded, the function
500 itself is kept in the cgraph even after it is compiled. Check whether
501 we are done with this body and reclaim it proactively if this is the case.
503 if (!kill_body && *slot)
505 struct cgraph_node *n = (struct cgraph_node *) *slot;
506 if (!n->next_clone && !n->global.inlined_to
507 && (cgraph_global_info_ready
508 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
509 kill_body = true;
512 if (kill_body && !dump_enabled_p (TDI_tree_all) && flag_unit_at_a_time)
514 DECL_SAVED_TREE (node->decl) = NULL;
515 DECL_STRUCT_FUNCTION (node->decl) = NULL;
516 DECL_INITIAL (node->decl) = error_mark_node;
518 cgraph_n_nodes--;
519 /* Do not free the structure itself so the walk over chain can continue. */
522 /* Notify finalize_compilation_unit that given node is reachable. */
524 void
525 cgraph_mark_reachable_node (struct cgraph_node *node)
527 if (!node->reachable && node->local.finalized)
529 notice_global_symbol (node->decl);
530 node->reachable = 1;
531 gcc_assert (!cgraph_global_info_ready);
533 node->next_needed = cgraph_nodes_queue;
534 cgraph_nodes_queue = node;
538 /* Likewise indicate that a node is needed, i.e. reachable via some
539 external means. */
541 void
542 cgraph_mark_needed_node (struct cgraph_node *node)
544 node->needed = 1;
545 cgraph_mark_reachable_node (node);
548 /* Return local info for the compiled function. */
550 struct cgraph_local_info *
551 cgraph_local_info (tree decl)
553 struct cgraph_node *node;
555 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
556 node = cgraph_node (decl);
557 return &node->local;
560 /* Return local info for the compiled function. */
562 struct cgraph_global_info *
563 cgraph_global_info (tree decl)
565 struct cgraph_node *node;
567 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
568 node = cgraph_node (decl);
569 return &node->global;
572 /* Return local info for the compiled function. */
574 struct cgraph_rtl_info *
575 cgraph_rtl_info (tree decl)
577 struct cgraph_node *node;
579 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
580 node = cgraph_node (decl);
581 if (decl != current_function_decl
582 && !TREE_ASM_WRITTEN (node->decl))
583 return NULL;
584 return &node->rtl;
587 /* Return name of the node used in debug output. */
588 const char *
589 cgraph_node_name (struct cgraph_node *node)
591 return lang_hooks.decl_printable_name (node->decl, 2);
594 /* Return name of the node used in debug output. */
595 static const char *
596 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
598 return lang_hooks.decl_printable_name (node->decl, 2);
601 /* Names used to print out the availability enum. */
602 static const char * const availability_names[] =
603 {"unset", "not_available", "overwrittable", "available", "local"};
605 /* Dump given cgraph node. */
606 void
607 dump_cgraph_node (FILE *f, struct cgraph_node *node)
609 struct cgraph_edge *edge;
610 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
611 if (node->global.inlined_to)
612 fprintf (f, " (inline copy in %s/%i)",
613 cgraph_node_name (node->global.inlined_to),
614 node->global.inlined_to->uid);
615 if (cgraph_function_flags_ready)
616 fprintf (f, " availability:%s",
617 availability_names [cgraph_function_body_availability (node)]);
618 if (node->master_clone && node->master_clone->uid != node->uid)
619 fprintf (f, "(%i)", node->master_clone->uid);
620 if (node->count)
621 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
622 (HOST_WIDEST_INT)node->count);
623 if (node->local.self_insns)
624 fprintf (f, " %i insns", node->local.self_insns);
625 if (node->global.insns && node->global.insns != node->local.self_insns)
626 fprintf (f, " (%i after inlining)", node->global.insns);
627 if (node->origin)
628 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
629 if (node->needed)
630 fprintf (f, " needed");
631 else if (node->reachable)
632 fprintf (f, " reachable");
633 if (DECL_SAVED_TREE (node->decl))
634 fprintf (f, " tree");
635 if (node->output)
636 fprintf (f, " output");
637 if (node->local.local)
638 fprintf (f, " local");
639 if (node->local.externally_visible)
640 fprintf (f, " externally_visible");
641 if (node->local.finalized)
642 fprintf (f, " finalized");
643 if (node->local.disregard_inline_limits)
644 fprintf (f, " always_inline");
645 else if (node->local.inlinable)
646 fprintf (f, " inlinable");
647 if (node->local.redefined_extern_inline)
648 fprintf (f, " redefined_extern_inline");
649 if (TREE_ASM_WRITTEN (node->decl))
650 fprintf (f, " asm_written");
652 fprintf (f, "\n called by: ");
653 for (edge = node->callers; edge; edge = edge->next_caller)
655 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
656 edge->caller->uid);
657 if (edge->count)
658 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
659 (HOST_WIDEST_INT)edge->count);
660 if (!edge->inline_failed)
661 fprintf(f, "(inlined) ");
664 fprintf (f, "\n calls: ");
665 for (edge = node->callees; edge; edge = edge->next_callee)
667 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
668 edge->callee->uid);
669 if (!edge->inline_failed)
670 fprintf(f, "(inlined) ");
671 if (edge->count)
672 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
673 (HOST_WIDEST_INT)edge->count);
674 if (edge->loop_nest)
675 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
677 fprintf (f, "\n");
680 /* Dump the callgraph. */
682 void
683 dump_cgraph (FILE *f)
685 struct cgraph_node *node;
687 fprintf (f, "callgraph:\n\n");
688 for (node = cgraph_nodes; node; node = node->next)
689 dump_cgraph_node (f, node);
692 /* Dump given cgraph node. */
693 void
694 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
696 fprintf (f, "%s:", cgraph_varpool_node_name (node));
697 fprintf (f, " availability:%s", availability_names [cgraph_variable_initializer_availability (node)]);
698 if (DECL_INITIAL (node->decl))
699 fprintf (f, " initialized");
700 if (node->needed)
701 fprintf (f, " needed");
702 if (node->analyzed)
703 fprintf (f, " analyzed");
704 if (node->finalized)
705 fprintf (f, " finalized");
706 if (node->output)
707 fprintf (f, " output");
708 if (node->externally_visible)
709 fprintf (f, " externally_visible");
710 fprintf (f, "\n");
713 /* Dump the callgraph. */
715 void
716 dump_varpool (FILE *f)
718 struct cgraph_varpool_node *node;
720 fprintf (f, "variable pool:\n\n");
721 for (node = cgraph_varpool_nodes; node; node = node->next_needed)
722 dump_cgraph_varpool_node (f, node);
725 /* Returns a hash code for P. */
727 static hashval_t
728 hash_varpool_node (const void *p)
730 const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
731 return (hashval_t) DECL_UID (n->decl);
734 /* Returns nonzero if P1 and P2 are equal. */
736 static int
737 eq_varpool_node (const void *p1, const void *p2)
739 const struct cgraph_varpool_node *n1 =
740 (const struct cgraph_varpool_node *) p1;
741 const struct cgraph_varpool_node *n2 =
742 (const struct cgraph_varpool_node *) p2;
743 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
746 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
747 struct cgraph_varpool_node *
748 cgraph_varpool_node (tree decl)
750 struct cgraph_varpool_node key, *node, **slot;
752 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
754 if (!cgraph_varpool_hash)
755 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
756 eq_varpool_node, NULL);
757 key.decl = decl;
758 slot = (struct cgraph_varpool_node **)
759 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
760 if (*slot)
761 return *slot;
762 node = GGC_CNEW (struct cgraph_varpool_node);
763 node->decl = decl;
764 node->order = cgraph_order++;
765 node->next = cgraph_varpool_nodes;
766 cgraph_varpool_nodes = node;
767 *slot = node;
768 return node;
771 struct cgraph_varpool_node *
772 cgraph_varpool_node_for_asm (tree asmname)
774 struct cgraph_varpool_node *node;
776 for (node = cgraph_varpool_nodes; node ; node = node->next)
777 if (decl_assembler_name_equal (node->decl, asmname))
778 return node;
780 return NULL;
783 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
784 void
785 change_decl_assembler_name (tree decl, tree name)
787 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
789 SET_DECL_ASSEMBLER_NAME (decl, name);
790 return;
792 if (name == DECL_ASSEMBLER_NAME (decl))
793 return;
795 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
796 && DECL_RTL_SET_P (decl))
797 warning (0, "%D renamed after being referenced in assembly", decl);
799 SET_DECL_ASSEMBLER_NAME (decl, name);
802 /* Helper function for finalization code - add node into lists so it will
803 be analyzed and compiled. */
804 void
805 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
807 if (cgraph_varpool_last_needed_node)
808 cgraph_varpool_last_needed_node->next_needed = node;
809 cgraph_varpool_last_needed_node = node;
810 node->next_needed = NULL;
811 if (!cgraph_varpool_nodes_queue)
812 cgraph_varpool_nodes_queue = node;
813 if (!cgraph_varpool_first_unanalyzed_node)
814 cgraph_varpool_first_unanalyzed_node = node;
815 notice_global_symbol (node->decl);
818 /* Reset the queue of needed nodes. */
819 void
820 cgraph_varpool_reset_queue (void)
822 cgraph_varpool_last_needed_node = NULL;
823 cgraph_varpool_nodes_queue = NULL;
824 cgraph_varpool_first_unanalyzed_node = NULL;
827 /* Notify finalize_compilation_unit that given node is reachable
828 or needed. */
829 void
830 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
832 if (!node->needed && node->finalized)
833 cgraph_varpool_enqueue_needed_node (node);
834 node->needed = 1;
837 /* Determine if variable DECL is needed. That is, visible to something
838 either outside this translation unit, something magic in the system
839 configury, or (if not doing unit-at-a-time) to something we haven't
840 seen yet. */
842 bool
843 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
845 /* If the user told us it is used, then it must be so. */
846 if (node->externally_visible
847 || lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
848 return true;
850 /* ??? If the assembler name is set by hand, it is possible to assemble
851 the name later after finalizing the function and the fact is noticed
852 in assemble_name then. This is arguably a bug. */
853 if (DECL_ASSEMBLER_NAME_SET_P (decl)
854 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
855 return true;
857 /* If we decided it was needed before, but at the time we didn't have
858 the definition available, then it's still needed. */
859 if (node->needed)
860 return true;
862 /* Externally visible variables must be output. The exception is
863 COMDAT variables that must be output only when they are needed. */
864 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
865 return true;
867 /* When not reordering top level variables, we have to assume that
868 we are going to keep everything. */
869 if (flag_unit_at_a_time && flag_toplevel_reorder)
870 return false;
872 /* We want to emit COMDAT variables only when absolutely necessary. */
873 if (DECL_COMDAT (decl))
874 return false;
875 return true;
878 void
879 cgraph_varpool_finalize_decl (tree decl)
881 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
883 /* The first declaration of a variable that comes through this function
884 decides whether it is global (in C, has external linkage)
885 or local (in C, has internal linkage). So do nothing more
886 if this function has already run. */
887 if (node->finalized)
889 if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
890 cgraph_varpool_assemble_pending_decls ();
891 return;
893 if (node->needed)
894 cgraph_varpool_enqueue_needed_node (node);
895 node->finalized = true;
897 if (decide_is_variable_needed (node, decl))
898 cgraph_varpool_mark_needed_node (node);
899 /* Since we reclaim unreachable nodes at the end of every language
900 level unit, we need to be conservative about possible entry points
901 there. */
902 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
903 cgraph_varpool_mark_needed_node (node);
904 if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
905 cgraph_varpool_assemble_pending_decls ();
908 /* Add a top-level asm statement to the list. */
910 struct cgraph_asm_node *
911 cgraph_add_asm_node (tree asm_str)
913 struct cgraph_asm_node *node;
915 node = GGC_CNEW (struct cgraph_asm_node);
916 node->asm_str = asm_str;
917 node->order = cgraph_order++;
918 node->next = NULL;
919 if (cgraph_asm_nodes == NULL)
920 cgraph_asm_nodes = node;
921 else
922 cgraph_asm_last_node->next = node;
923 cgraph_asm_last_node = node;
924 return node;
927 /* Return true when the DECL can possibly be inlined. */
928 bool
929 cgraph_function_possibly_inlined_p (tree decl)
931 if (!cgraph_global_info_ready)
932 return (DECL_INLINE (decl) && !flag_really_no_inline);
933 return DECL_POSSIBLY_INLINED (decl);
936 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
937 struct cgraph_edge *
938 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
939 tree call_stmt, gcov_type count_scale, int loop_nest,
940 bool update_original)
942 struct cgraph_edge *new;
944 new = cgraph_create_edge (n, e->callee, call_stmt,
945 e->count * count_scale / REG_BR_PROB_BASE,
946 e->loop_nest + loop_nest);
948 new->inline_failed = e->inline_failed;
949 if (update_original)
951 e->count -= new->count;
952 if (e->count < 0)
953 e->count = 0;
955 return new;
958 /* Create node representing clone of N executed COUNT times. Decrease
959 the execution counts from original node too.
961 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
962 function's profile to reflect the fact that part of execution is handled
963 by node. */
964 struct cgraph_node *
965 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
966 bool update_original)
968 struct cgraph_node *new = cgraph_create_node ();
969 struct cgraph_edge *e;
970 gcov_type count_scale;
972 new->decl = n->decl;
973 new->origin = n->origin;
974 if (new->origin)
976 new->next_nested = new->origin->nested;
977 new->origin->nested = new;
979 new->analyzed = n->analyzed;
980 new->local = n->local;
981 new->global = n->global;
982 new->rtl = n->rtl;
983 new->master_clone = n->master_clone;
984 new->count = count;
985 if (n->count)
986 count_scale = new->count * REG_BR_PROB_BASE / n->count;
987 else
988 count_scale = 0;
989 if (update_original)
991 n->count -= count;
992 if (n->count < 0)
993 n->count = 0;
996 for (e = n->callees;e; e=e->next_callee)
997 cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
998 update_original);
1000 new->next_clone = n->next_clone;
1001 new->prev_clone = n;
1002 n->next_clone = new;
1003 if (new->next_clone)
1004 new->next_clone->prev_clone = new;
1006 return new;
1009 /* Return true if N is an master_clone, (see cgraph_master_clone). */
1011 bool
1012 cgraph_is_master_clone (struct cgraph_node *n)
1014 return (n == cgraph_master_clone (n));
1017 struct cgraph_node *
1018 cgraph_master_clone (struct cgraph_node *n)
1020 enum availability avail = cgraph_function_body_availability (n);
1022 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1023 return NULL;
1025 if (!n->master_clone)
1026 n->master_clone = cgraph_node (n->decl);
1028 return n->master_clone;
1031 /* NODE is no longer nested function; update cgraph accordingly. */
1032 void
1033 cgraph_unnest_node (struct cgraph_node *node)
1035 struct cgraph_node **node2 = &node->origin->nested;
1036 gcc_assert (node->origin);
1038 while (*node2 != node)
1039 node2 = &(*node2)->next_nested;
1040 *node2 = node->next_nested;
1041 node->origin = NULL;
1044 /* Return function availability. See cgraph.h for description of individual
1045 return values. */
1046 enum availability
1047 cgraph_function_body_availability (struct cgraph_node *node)
1049 enum availability avail;
1050 gcc_assert (cgraph_function_flags_ready);
1051 if (!node->analyzed)
1052 avail = AVAIL_NOT_AVAILABLE;
1053 else if (node->local.local)
1054 avail = AVAIL_LOCAL;
1055 else if (node->local.externally_visible)
1056 avail = AVAIL_AVAILABLE;
1058 /* If the function can be overwritten, return OVERWRITABLE. Take
1059 care at least of two notable extensions - the COMDAT functions
1060 used to share template instantiations in C++ (this is symmetric
1061 to code cp_cannot_inline_tree_fn and probably shall be shared and
1062 the inlinability hooks completely eliminated).
1064 ??? Does the C++ one definition rule allow us to always return
1065 AVAIL_AVAILABLE here? That would be good reason to preserve this
1066 hook Similarly deal with extern inline functions - this is again
1067 necessary to get C++ shared functions having keyed templates
1068 right and in the C extension documentation we probably should
1069 document the requirement of both versions of function (extern
1070 inline and offline) having same side effect characteristics as
1071 good optimization is what this optimization is about. */
1073 else if (!(*targetm.binds_local_p) (node->decl)
1074 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1075 avail = AVAIL_OVERWRITABLE;
1076 else avail = AVAIL_AVAILABLE;
1078 return avail;
1081 /* Return variable availability. See cgraph.h for description of individual
1082 return values. */
1083 enum availability
1084 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1086 gcc_assert (cgraph_function_flags_ready);
1087 if (!node->finalized)
1088 return AVAIL_NOT_AVAILABLE;
1089 if (!TREE_PUBLIC (node->decl))
1090 return AVAIL_AVAILABLE;
1091 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1092 care of at least two notable extensions - the COMDAT variables
1093 used to share template instantiations in C++. */
1094 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1095 return AVAIL_OVERWRITABLE;
1096 return AVAIL_AVAILABLE;
1100 /* Add the function FNDECL to the call graph. FNDECL is assumed to be
1101 in low GIMPLE form and ready to be processed by cgraph_finalize_function.
1103 When operating in unit-at-a-time, a new callgraph node is added to
1104 CGRAPH_EXPAND_QUEUE, which is processed after all the original
1105 functions in the call graph .
1107 When not in unit-at-a-time, the new callgraph node is added to
1108 CGRAPH_NODES_QUEUE for cgraph_assemble_pending_functions to
1109 process. */
1111 void
1112 cgraph_add_new_function (tree fndecl)
1114 struct cgraph_node *n = cgraph_node (fndecl);
1115 n->next_needed = cgraph_expand_queue;
1116 cgraph_expand_queue = n;
1119 #include "gt-cgraph.h"