Daily bump.
[official-gcc.git] / gcc / cgraph.c
blob6fdd925a7dbc15e318547fc1f5bb3fc720188f9e
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 Interprocedural information:
52 Callgraph is place to store data needed for interprocedural 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 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 /* Returns a hash value for X (which really is a die_struct). */
298 static hashval_t
299 edge_hash (const void *x)
301 return htab_hash_pointer (((struct cgraph_edge *) x)->call_stmt);
304 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
306 static int
307 edge_eq (const void *x, const void *y)
309 return ((struct cgraph_edge *) x)->call_stmt == y;
312 /* Return callgraph edge representing CALL_EXPR statement. */
313 struct cgraph_edge *
314 cgraph_edge (struct cgraph_node *node, tree call_stmt)
316 struct cgraph_edge *e, *e2;
317 int n = 0;
319 if (node->call_site_hash)
320 return htab_find_with_hash (node->call_site_hash, call_stmt,
321 htab_hash_pointer (call_stmt));
323 /* This loop may turn out to be performance problem. In such case adding
324 hashtables into call nodes with very many edges is probably best
325 solution. It is not good idea to add pointer into CALL_EXPR itself
326 because we want to make possible having multiple cgraph nodes representing
327 different clones of the same body before the body is actually cloned. */
328 for (e = node->callees; e; e= e->next_callee)
330 if (e->call_stmt == call_stmt)
331 break;
332 n++;
334 if (n > 100)
336 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
337 for (e2 = node->callees; e2; e2 = e2->next_callee)
339 void **slot;
340 slot = htab_find_slot_with_hash (node->call_site_hash,
341 e2->call_stmt,
342 htab_hash_pointer (e2->call_stmt),
343 INSERT);
344 gcc_assert (!*slot);
345 *slot = e2;
348 return e;
351 /* Change call_smtt of edge E to NEW_STMT. */
352 void
353 cgraph_set_call_stmt (struct cgraph_edge *e, tree new_stmt)
355 if (e->caller->call_site_hash)
357 htab_remove_elt_with_hash (e->caller->call_site_hash,
358 e->call_stmt,
359 htab_hash_pointer (e->call_stmt));
361 e->call_stmt = new_stmt;
362 if (e->caller->call_site_hash)
364 void **slot;
365 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
366 e->call_stmt,
367 htab_hash_pointer
368 (e->call_stmt), INSERT);
369 gcc_assert (!*slot);
370 *slot = e;
374 /* Create edge from CALLER to CALLEE in the cgraph. */
376 struct cgraph_edge *
377 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
378 tree call_stmt, gcov_type count, int nest)
380 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
381 #ifdef ENABLE_CHECKING
382 struct cgraph_edge *e;
384 for (e = caller->callees; e; e = e->next_callee)
385 gcc_assert (e->call_stmt != call_stmt);
386 #endif
388 gcc_assert (get_call_expr_in (call_stmt));
390 if (!DECL_SAVED_TREE (callee->decl))
391 edge->inline_failed = N_("function body not available");
392 else if (callee->local.redefined_extern_inline)
393 edge->inline_failed = N_("redefined extern inline functions are not "
394 "considered for inlining");
395 else if (callee->local.inlinable)
396 edge->inline_failed = N_("function not considered for inlining");
397 else
398 edge->inline_failed = N_("function not inlinable");
400 edge->aux = NULL;
402 edge->caller = caller;
403 edge->callee = callee;
404 edge->call_stmt = call_stmt;
405 edge->prev_caller = NULL;
406 edge->next_caller = callee->callers;
407 if (callee->callers)
408 callee->callers->prev_caller = edge;
409 edge->prev_callee = NULL;
410 edge->next_callee = caller->callees;
411 if (caller->callees)
412 caller->callees->prev_callee = edge;
413 caller->callees = edge;
414 callee->callers = edge;
415 edge->count = count;
416 edge->loop_nest = nest;
417 if (caller->call_site_hash)
419 void **slot;
420 slot = htab_find_slot_with_hash (caller->call_site_hash,
421 edge->call_stmt,
422 htab_hash_pointer
423 (edge->call_stmt),
424 INSERT);
425 gcc_assert (!*slot);
426 *slot = edge;
428 return edge;
431 /* Remove the edge E from the list of the callers of the callee. */
433 static inline void
434 cgraph_edge_remove_callee (struct cgraph_edge *e)
436 if (e->prev_caller)
437 e->prev_caller->next_caller = e->next_caller;
438 if (e->next_caller)
439 e->next_caller->prev_caller = e->prev_caller;
440 if (!e->prev_caller)
441 e->callee->callers = e->next_caller;
444 /* Remove the edge E from the list of the callees of the caller. */
446 static inline void
447 cgraph_edge_remove_caller (struct cgraph_edge *e)
449 if (e->prev_callee)
450 e->prev_callee->next_callee = e->next_callee;
451 if (e->next_callee)
452 e->next_callee->prev_callee = e->prev_callee;
453 if (!e->prev_callee)
454 e->caller->callees = e->next_callee;
455 if (e->caller->call_site_hash)
456 htab_remove_elt_with_hash (e->caller->call_site_hash,
457 e->call_stmt,
458 htab_hash_pointer (e->call_stmt));
461 /* Remove the edge E in the cgraph. */
463 void
464 cgraph_remove_edge (struct cgraph_edge *e)
466 /* Remove from callers list of the callee. */
467 cgraph_edge_remove_callee (e);
469 /* Remove from callees list of the callers. */
470 cgraph_edge_remove_caller (e);
473 /* Redirect callee of E to N. The function does not update underlying
474 call expression. */
476 void
477 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
479 /* Remove from callers list of the current callee. */
480 cgraph_edge_remove_callee (e);
482 /* Insert to callers list of the new callee. */
483 e->prev_caller = NULL;
484 if (n->callers)
485 n->callers->prev_caller = e;
486 e->next_caller = n->callers;
487 n->callers = e;
488 e->callee = n;
491 /* Remove all callees from the node. */
493 void
494 cgraph_node_remove_callees (struct cgraph_node *node)
496 struct cgraph_edge *e;
498 /* It is sufficient to remove the edges from the lists of callers of
499 the callees. The callee list of the node can be zapped with one
500 assignment. */
501 for (e = node->callees; e; e = e->next_callee)
502 cgraph_edge_remove_callee (e);
503 node->callees = NULL;
504 if (node->call_site_hash)
506 htab_delete (node->call_site_hash);
507 node->call_site_hash = NULL;
511 /* Remove all callers from the node. */
513 static void
514 cgraph_node_remove_callers (struct cgraph_node *node)
516 struct cgraph_edge *e;
518 /* It is sufficient to remove the edges from the lists of callees of
519 the callers. The caller list of the node can be zapped with one
520 assignment. */
521 for (e = node->callers; e; e = e->next_caller)
522 cgraph_edge_remove_caller (e);
523 node->callers = NULL;
526 /* Remove the node from cgraph. */
528 void
529 cgraph_remove_node (struct cgraph_node *node)
531 void **slot;
532 bool kill_body = false;
534 cgraph_node_remove_callers (node);
535 cgraph_node_remove_callees (node);
536 /* Incremental inlining access removed nodes stored in the postorder list.
538 node->needed = node->reachable = false;
539 while (node->nested)
540 cgraph_remove_node (node->nested);
541 if (node->origin)
543 struct cgraph_node **node2 = &node->origin->nested;
545 while (*node2 != node)
546 node2 = &(*node2)->next_nested;
547 *node2 = node->next_nested;
549 if (node->previous)
550 node->previous->next = node->next;
551 else
552 cgraph_nodes = node->next;
553 if (node->next)
554 node->next->previous = node->previous;
555 node->next = NULL;
556 node->previous = NULL;
557 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
558 if (*slot == node)
560 if (node->next_clone)
562 struct cgraph_node *new_node = node->next_clone;
563 struct cgraph_node *n;
565 /* Make the next clone be the master clone */
566 for (n = new_node; n; n = n->next_clone)
567 n->master_clone = new_node;
569 *slot = new_node;
570 node->next_clone->prev_clone = NULL;
572 else
574 htab_clear_slot (cgraph_hash, slot);
575 kill_body = true;
578 else
580 node->prev_clone->next_clone = node->next_clone;
581 if (node->next_clone)
582 node->next_clone->prev_clone = node->prev_clone;
585 /* While all the clones are removed after being proceeded, the function
586 itself is kept in the cgraph even after it is compiled. Check whether
587 we are done with this body and reclaim it proactively if this is the case.
589 if (!kill_body && *slot)
591 struct cgraph_node *n = (struct cgraph_node *) *slot;
592 if (!n->next_clone && !n->global.inlined_to
593 && (cgraph_global_info_ready
594 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
595 kill_body = true;
598 if (kill_body && flag_unit_at_a_time)
600 DECL_SAVED_TREE (node->decl) = NULL;
601 DECL_STRUCT_FUNCTION (node->decl) = NULL;
602 DECL_INITIAL (node->decl) = error_mark_node;
604 node->decl = NULL;
605 if (node->call_site_hash)
607 htab_delete (node->call_site_hash);
608 node->call_site_hash = NULL;
610 cgraph_n_nodes--;
611 /* Do not free the structure itself so the walk over chain can continue. */
614 /* Notify finalize_compilation_unit that given node is reachable. */
616 void
617 cgraph_mark_reachable_node (struct cgraph_node *node)
619 if (!node->reachable && node->local.finalized)
621 notice_global_symbol (node->decl);
622 node->reachable = 1;
623 gcc_assert (!cgraph_global_info_ready);
625 node->next_needed = cgraph_nodes_queue;
626 cgraph_nodes_queue = node;
630 /* Likewise indicate that a node is needed, i.e. reachable via some
631 external means. */
633 void
634 cgraph_mark_needed_node (struct cgraph_node *node)
636 node->needed = 1;
637 cgraph_mark_reachable_node (node);
640 /* Return local info for the compiled function. */
642 struct cgraph_local_info *
643 cgraph_local_info (tree decl)
645 struct cgraph_node *node;
647 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
648 node = cgraph_node (decl);
649 return &node->local;
652 /* Return local info for the compiled function. */
654 struct cgraph_global_info *
655 cgraph_global_info (tree decl)
657 struct cgraph_node *node;
659 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
660 node = cgraph_node (decl);
661 return &node->global;
664 /* Return local info for the compiled function. */
666 struct cgraph_rtl_info *
667 cgraph_rtl_info (tree decl)
669 struct cgraph_node *node;
671 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
672 node = cgraph_node (decl);
673 if (decl != current_function_decl
674 && !TREE_ASM_WRITTEN (node->decl))
675 return NULL;
676 return &node->rtl;
679 /* Return name of the node used in debug output. */
680 const char *
681 cgraph_node_name (struct cgraph_node *node)
683 return lang_hooks.decl_printable_name (node->decl, 2);
686 /* Return name of the node used in debug output. */
687 static const char *
688 cgraph_varpool_node_name (struct cgraph_varpool_node *node)
690 return lang_hooks.decl_printable_name (node->decl, 2);
693 /* Names used to print out the availability enum. */
694 static const char * const availability_names[] =
695 {"unset", "not_available", "overwrittable", "available", "local"};
697 /* Dump given cgraph node. */
698 void
699 dump_cgraph_node (FILE *f, struct cgraph_node *node)
701 struct cgraph_edge *edge;
702 fprintf (f, "%s/%i:", cgraph_node_name (node), node->uid);
703 if (node->global.inlined_to)
704 fprintf (f, " (inline copy in %s/%i)",
705 cgraph_node_name (node->global.inlined_to),
706 node->global.inlined_to->uid);
707 if (cgraph_function_flags_ready)
708 fprintf (f, " availability:%s",
709 availability_names [cgraph_function_body_availability (node)]);
710 if (node->master_clone && node->master_clone->uid != node->uid)
711 fprintf (f, "(%i)", node->master_clone->uid);
712 if (node->count)
713 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
714 (HOST_WIDEST_INT)node->count);
715 if (node->local.self_insns)
716 fprintf (f, " %i insns", node->local.self_insns);
717 if (node->global.insns && node->global.insns != node->local.self_insns)
718 fprintf (f, " (%i after inlining)", node->global.insns);
719 if (node->local.estimated_self_stack_size)
720 fprintf (f, " %i bytes stack usage", (int)node->local.estimated_self_stack_size);
721 if (node->global.estimated_stack_size != node->local.estimated_self_stack_size)
722 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
723 if (node->origin)
724 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
725 if (node->needed)
726 fprintf (f, " needed");
727 else if (node->reachable)
728 fprintf (f, " reachable");
729 if (DECL_SAVED_TREE (node->decl))
730 fprintf (f, " tree");
731 if (node->output)
732 fprintf (f, " output");
733 if (node->local.local)
734 fprintf (f, " local");
735 if (node->local.externally_visible)
736 fprintf (f, " externally_visible");
737 if (node->local.finalized)
738 fprintf (f, " finalized");
739 if (node->local.disregard_inline_limits)
740 fprintf (f, " always_inline");
741 else if (node->local.inlinable)
742 fprintf (f, " inlinable");
743 if (node->local.redefined_extern_inline)
744 fprintf (f, " redefined_extern_inline");
745 if (TREE_ASM_WRITTEN (node->decl))
746 fprintf (f, " asm_written");
748 fprintf (f, "\n called by: ");
749 for (edge = node->callers; edge; edge = edge->next_caller)
751 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
752 edge->caller->uid);
753 if (edge->count)
754 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
755 (HOST_WIDEST_INT)edge->count);
756 if (!edge->inline_failed)
757 fprintf(f, "(inlined) ");
760 fprintf (f, "\n calls: ");
761 for (edge = node->callees; edge; edge = edge->next_callee)
763 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
764 edge->callee->uid);
765 if (!edge->inline_failed)
766 fprintf(f, "(inlined) ");
767 if (edge->count)
768 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
769 (HOST_WIDEST_INT)edge->count);
770 if (edge->loop_nest)
771 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
773 fprintf (f, "\n");
776 /* Dump the callgraph. */
778 void
779 dump_cgraph (FILE *f)
781 struct cgraph_node *node;
783 fprintf (f, "callgraph:\n\n");
784 for (node = cgraph_nodes; node; node = node->next)
785 dump_cgraph_node (f, node);
788 /* Dump given cgraph node. */
789 void
790 dump_cgraph_varpool_node (FILE *f, struct cgraph_varpool_node *node)
792 fprintf (f, "%s:", cgraph_varpool_node_name (node));
793 fprintf (f, " availability:%s",
794 cgraph_function_flags_ready
795 ? availability_names[cgraph_variable_initializer_availability (node)]
796 : "not-ready");
797 if (DECL_INITIAL (node->decl))
798 fprintf (f, " initialized");
799 if (node->needed)
800 fprintf (f, " needed");
801 if (node->analyzed)
802 fprintf (f, " analyzed");
803 if (node->finalized)
804 fprintf (f, " finalized");
805 if (node->output)
806 fprintf (f, " output");
807 if (node->externally_visible)
808 fprintf (f, " externally_visible");
809 fprintf (f, "\n");
812 /* Dump the callgraph. */
814 void
815 dump_varpool (FILE *f)
817 struct cgraph_varpool_node *node;
819 fprintf (f, "variable pool:\n\n");
820 for (node = cgraph_varpool_nodes; node; node = node->next_needed)
821 dump_cgraph_varpool_node (f, node);
824 /* Returns a hash code for P. */
826 static hashval_t
827 hash_varpool_node (const void *p)
829 const struct cgraph_varpool_node *n = (const struct cgraph_varpool_node *) p;
830 return (hashval_t) DECL_UID (n->decl);
833 /* Returns nonzero if P1 and P2 are equal. */
835 static int
836 eq_varpool_node (const void *p1, const void *p2)
838 const struct cgraph_varpool_node *n1 =
839 (const struct cgraph_varpool_node *) p1;
840 const struct cgraph_varpool_node *n2 =
841 (const struct cgraph_varpool_node *) p2;
842 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
845 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
846 struct cgraph_varpool_node *
847 cgraph_varpool_node (tree decl)
849 struct cgraph_varpool_node key, *node, **slot;
851 gcc_assert (DECL_P (decl) && TREE_CODE (decl) != FUNCTION_DECL);
853 if (!cgraph_varpool_hash)
854 cgraph_varpool_hash = htab_create_ggc (10, hash_varpool_node,
855 eq_varpool_node, NULL);
856 key.decl = decl;
857 slot = (struct cgraph_varpool_node **)
858 htab_find_slot (cgraph_varpool_hash, &key, INSERT);
859 if (*slot)
860 return *slot;
861 node = GGC_CNEW (struct cgraph_varpool_node);
862 node->decl = decl;
863 node->order = cgraph_order++;
864 node->next = cgraph_varpool_nodes;
865 cgraph_varpool_nodes = node;
866 *slot = node;
867 return node;
870 struct cgraph_varpool_node *
871 cgraph_varpool_node_for_asm (tree asmname)
873 struct cgraph_varpool_node *node;
875 for (node = cgraph_varpool_nodes; node ; node = node->next)
876 if (decl_assembler_name_equal (node->decl, asmname))
877 return node;
879 return NULL;
882 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
883 void
884 change_decl_assembler_name (tree decl, tree name)
886 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
888 SET_DECL_ASSEMBLER_NAME (decl, name);
889 return;
891 if (name == DECL_ASSEMBLER_NAME (decl))
892 return;
894 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
895 && DECL_RTL_SET_P (decl))
896 warning (0, "%D renamed after being referenced in assembly", decl);
898 SET_DECL_ASSEMBLER_NAME (decl, name);
901 /* Helper function for finalization code - add node into lists so it will
902 be analyzed and compiled. */
903 void
904 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node *node)
906 if (cgraph_varpool_last_needed_node)
907 cgraph_varpool_last_needed_node->next_needed = node;
908 cgraph_varpool_last_needed_node = node;
909 node->next_needed = NULL;
910 if (!cgraph_varpool_nodes_queue)
911 cgraph_varpool_nodes_queue = node;
912 if (!cgraph_varpool_first_unanalyzed_node)
913 cgraph_varpool_first_unanalyzed_node = node;
914 notice_global_symbol (node->decl);
917 /* Reset the queue of needed nodes. */
918 void
919 cgraph_varpool_reset_queue (void)
921 cgraph_varpool_last_needed_node = NULL;
922 cgraph_varpool_nodes_queue = NULL;
923 cgraph_varpool_first_unanalyzed_node = NULL;
926 /* Notify finalize_compilation_unit that given node is reachable
927 or needed. */
928 void
929 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node *node)
931 if (!node->needed && node->finalized
932 && !TREE_ASM_WRITTEN (node->decl))
933 cgraph_varpool_enqueue_needed_node (node);
934 node->needed = 1;
937 /* Determine if variable DECL is needed. That is, visible to something
938 either outside this translation unit, something magic in the system
939 configury, or (if not doing unit-at-a-time) to something we haven't
940 seen yet. */
942 bool
943 decide_is_variable_needed (struct cgraph_varpool_node *node, tree decl)
945 /* If the user told us it is used, then it must be so. */
946 if (node->externally_visible)
947 return true;
948 if (!flag_unit_at_a_time
949 && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
950 return true;
952 /* ??? If the assembler name is set by hand, it is possible to assemble
953 the name later after finalizing the function and the fact is noticed
954 in assemble_name then. This is arguably a bug. */
955 if (DECL_ASSEMBLER_NAME_SET_P (decl)
956 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
957 return true;
959 /* If we decided it was needed before, but at the time we didn't have
960 the definition available, then it's still needed. */
961 if (node->needed)
962 return true;
964 /* Externally visible variables must be output. The exception is
965 COMDAT variables that must be output only when they are needed. */
966 if (TREE_PUBLIC (decl) && !flag_whole_program && !DECL_COMDAT (decl)
967 && !DECL_EXTERNAL (decl))
968 return true;
970 /* When not reordering top level variables, we have to assume that
971 we are going to keep everything. */
972 if (flag_unit_at_a_time && flag_toplevel_reorder)
973 return false;
975 /* We want to emit COMDAT variables only when absolutely necessary. */
976 if (DECL_COMDAT (decl))
977 return false;
978 return true;
981 void
982 cgraph_varpool_finalize_decl (tree decl)
984 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
986 /* The first declaration of a variable that comes through this function
987 decides whether it is global (in C, has external linkage)
988 or local (in C, has internal linkage). So do nothing more
989 if this function has already run. */
990 if (node->finalized)
992 if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
993 cgraph_varpool_assemble_pending_decls ();
994 return;
996 if (node->needed)
997 cgraph_varpool_enqueue_needed_node (node);
998 node->finalized = true;
1000 if (decide_is_variable_needed (node, decl))
1001 cgraph_varpool_mark_needed_node (node);
1002 /* Since we reclaim unreachable nodes at the end of every language
1003 level unit, we need to be conservative about possible entry points
1004 there. */
1005 else if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
1006 cgraph_varpool_mark_needed_node (node);
1007 if (cgraph_global_info_ready || (!flag_unit_at_a_time && !flag_openmp))
1008 cgraph_varpool_assemble_pending_decls ();
1011 /* Add a top-level asm statement to the list. */
1013 struct cgraph_asm_node *
1014 cgraph_add_asm_node (tree asm_str)
1016 struct cgraph_asm_node *node;
1018 node = GGC_CNEW (struct cgraph_asm_node);
1019 node->asm_str = asm_str;
1020 node->order = cgraph_order++;
1021 node->next = NULL;
1022 if (cgraph_asm_nodes == NULL)
1023 cgraph_asm_nodes = node;
1024 else
1025 cgraph_asm_last_node->next = node;
1026 cgraph_asm_last_node = node;
1027 return node;
1030 /* Return true when the DECL can possibly be inlined. */
1031 bool
1032 cgraph_function_possibly_inlined_p (tree decl)
1034 if (!cgraph_global_info_ready)
1035 return (DECL_INLINE (decl) && !flag_really_no_inline);
1036 return DECL_POSSIBLY_INLINED (decl);
1039 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
1040 struct cgraph_edge *
1041 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
1042 tree call_stmt, gcov_type count_scale, int loop_nest,
1043 bool update_original)
1045 struct cgraph_edge *new;
1047 new = cgraph_create_edge (n, e->callee, call_stmt,
1048 e->count * count_scale / REG_BR_PROB_BASE,
1049 e->loop_nest + loop_nest);
1051 new->inline_failed = e->inline_failed;
1052 if (update_original)
1054 e->count -= new->count;
1055 if (e->count < 0)
1056 e->count = 0;
1058 return new;
1061 /* Create node representing clone of N executed COUNT times. Decrease
1062 the execution counts from original node too.
1064 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1065 function's profile to reflect the fact that part of execution is handled
1066 by node. */
1067 struct cgraph_node *
1068 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int loop_nest,
1069 bool update_original)
1071 struct cgraph_node *new = cgraph_create_node ();
1072 struct cgraph_edge *e;
1073 gcov_type count_scale;
1075 new->decl = n->decl;
1076 new->origin = n->origin;
1077 if (new->origin)
1079 new->next_nested = new->origin->nested;
1080 new->origin->nested = new;
1082 new->analyzed = n->analyzed;
1083 new->local = n->local;
1084 new->global = n->global;
1085 new->rtl = n->rtl;
1086 new->master_clone = n->master_clone;
1087 new->count = count;
1088 if (n->count)
1089 count_scale = new->count * REG_BR_PROB_BASE / n->count;
1090 else
1091 count_scale = 0;
1092 if (update_original)
1094 n->count -= count;
1095 if (n->count < 0)
1096 n->count = 0;
1099 for (e = n->callees;e; e=e->next_callee)
1100 cgraph_clone_edge (e, new, e->call_stmt, count_scale, loop_nest,
1101 update_original);
1103 new->next_clone = n->next_clone;
1104 new->prev_clone = n;
1105 n->next_clone = new;
1106 if (new->next_clone)
1107 new->next_clone->prev_clone = new;
1109 return new;
1112 /* Return true if N is an master_clone, (see cgraph_master_clone). */
1114 bool
1115 cgraph_is_master_clone (struct cgraph_node *n)
1117 return (n == cgraph_master_clone (n));
1120 struct cgraph_node *
1121 cgraph_master_clone (struct cgraph_node *n)
1123 enum availability avail = cgraph_function_body_availability (n);
1125 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1126 return NULL;
1128 if (!n->master_clone)
1129 n->master_clone = cgraph_node (n->decl);
1131 return n->master_clone;
1134 /* NODE is no longer nested function; update cgraph accordingly. */
1135 void
1136 cgraph_unnest_node (struct cgraph_node *node)
1138 struct cgraph_node **node2 = &node->origin->nested;
1139 gcc_assert (node->origin);
1141 while (*node2 != node)
1142 node2 = &(*node2)->next_nested;
1143 *node2 = node->next_nested;
1144 node->origin = NULL;
1147 /* Return function availability. See cgraph.h for description of individual
1148 return values. */
1149 enum availability
1150 cgraph_function_body_availability (struct cgraph_node *node)
1152 enum availability avail;
1153 gcc_assert (cgraph_function_flags_ready);
1154 if (!node->analyzed)
1155 avail = AVAIL_NOT_AVAILABLE;
1156 else if (node->local.local)
1157 avail = AVAIL_LOCAL;
1158 else if (node->local.externally_visible)
1159 avail = AVAIL_AVAILABLE;
1161 /* If the function can be overwritten, return OVERWRITABLE. Take
1162 care at least of two notable extensions - the COMDAT functions
1163 used to share template instantiations in C++ (this is symmetric
1164 to code cp_cannot_inline_tree_fn and probably shall be shared and
1165 the inlinability hooks completely eliminated).
1167 ??? Does the C++ one definition rule allow us to always return
1168 AVAIL_AVAILABLE here? That would be good reason to preserve this
1169 hook Similarly deal with extern inline functions - this is again
1170 necessary to get C++ shared functions having keyed templates
1171 right and in the C extension documentation we probably should
1172 document the requirement of both versions of function (extern
1173 inline and offline) having same side effect characteristics as
1174 good optimization is what this optimization is about. */
1176 else if (!(*targetm.binds_local_p) (node->decl)
1177 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1178 avail = AVAIL_OVERWRITABLE;
1179 else avail = AVAIL_AVAILABLE;
1181 return avail;
1184 /* Return variable availability. See cgraph.h for description of individual
1185 return values. */
1186 enum availability
1187 cgraph_variable_initializer_availability (struct cgraph_varpool_node *node)
1189 gcc_assert (cgraph_function_flags_ready);
1190 if (!node->finalized)
1191 return AVAIL_NOT_AVAILABLE;
1192 if (!TREE_PUBLIC (node->decl))
1193 return AVAIL_AVAILABLE;
1194 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1195 care of at least two notable extensions - the COMDAT variables
1196 used to share template instantiations in C++. */
1197 if (!(*targetm.binds_local_p) (node->decl) && !DECL_COMDAT (node->decl))
1198 return AVAIL_OVERWRITABLE;
1199 return AVAIL_AVAILABLE;
1203 /* Add the function FNDECL to the call graph. FNDECL is assumed to be
1204 in low GIMPLE form and ready to be processed by cgraph_finalize_function.
1206 When operating in unit-at-a-time, a new callgraph node is added to
1207 CGRAPH_EXPAND_QUEUE, which is processed after all the original
1208 functions in the call graph .
1210 When not in unit-at-a-time, the new callgraph node is added to
1211 CGRAPH_NODES_QUEUE for cgraph_assemble_pending_functions to
1212 process. */
1214 void
1215 cgraph_add_new_function (tree fndecl)
1217 struct cgraph_node *n = cgraph_node (fndecl);
1218 n->next_needed = cgraph_expand_queue;
1219 cgraph_expand_queue = n;
1222 #include "gt-cgraph.h"