1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
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
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node.
33 The mapping from declarations to call-graph nodes is done using hash table
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
37 The callgraph at the moment does not represent indirect calls or calls
38 from other compilation unit. Flag NEEDED is set for each node that may
39 be accessed in such an invisible way and it shall be considered an
40 entry point to the callgraph.
42 Interprocedural information:
44 Callgraph is place to store data needed for interprocedural optimization.
45 All data structures are divided into three components: local_info that
46 is produced while analyzing the function, global_info that is result
47 of global walking of the callgraph on the end of compilation and
48 rtl_info used by RTL backend to propagate data from already compiled
49 functions to their callers.
53 The function inlining information is decided in advance and maintained
54 in the callgraph as so called inline plan.
55 For each inlined call, the callee's node is cloned to represent the
56 new function copy produced by inliner.
57 Each inlined call gets a unique corresponding clone node of the callee
58 and the data structure is updated while inlining is performed, so
59 the clones are eliminated and their callee edges redirected to the
62 Each edge has "inline_failed" field. When the field is set to NULL,
63 the call will be inlined. When it is non-NULL it contains a reason
64 why inlining wasn't performed. */
68 #include "coretypes.h"
71 #include "tree-inline.h"
72 #include "langhooks.h"
79 #include "basic-block.h"
84 #include "tree-gimple.h"
85 #include "tree-dump.h"
86 #include "tree-flow.h"
88 static void cgraph_node_remove_callers (struct cgraph_node *node);
89 static inline void cgraph_edge_remove_caller (struct cgraph_edge *e);
90 static inline void cgraph_edge_remove_callee (struct cgraph_edge *e);
92 /* Hash table used to convert declarations into nodes. */
93 static GTY((param_is (struct cgraph_node))) htab_t cgraph_hash;
95 /* The linked list of cgraph nodes. */
96 struct cgraph_node *cgraph_nodes;
98 /* Queue of cgraph nodes scheduled to be lowered. */
99 struct cgraph_node *cgraph_nodes_queue;
101 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
102 secondary queue used during optimization to accommodate passes that
103 may generate new functions that need to be optimized and expanded. */
104 struct cgraph_node *cgraph_new_nodes;
106 /* Number of nodes in existence. */
109 /* Maximal uid used in cgraph nodes. */
112 /* Maximal uid used in cgraph edges. */
113 int cgraph_edge_max_uid;
115 /* Maximal pid used for profiling */
118 /* Set when whole unit has been analyzed so we can access global info. */
119 bool cgraph_global_info_ready = false;
121 /* What state callgraph is in right now. */
122 enum cgraph_state cgraph_state = CGRAPH_STATE_CONSTRUCTION;
124 /* Set when the cgraph is fully build and the basic flags are computed. */
125 bool cgraph_function_flags_ready = false;
127 /* Linked list of cgraph asm nodes. */
128 struct cgraph_asm_node *cgraph_asm_nodes;
130 /* Last node in cgraph_asm_nodes. */
131 static GTY(()) struct cgraph_asm_node *cgraph_asm_last_node;
133 /* The order index of the next cgraph node to be created. This is
134 used so that we can sort the cgraph nodes in order by when we saw
135 them, to support -fno-toplevel-reorder. */
138 /* List of hooks trigerred on cgraph_edge events. */
139 struct cgraph_edge_hook_list {
140 cgraph_edge_hook hook;
142 struct cgraph_edge_hook_list *next;
145 /* List of hooks trigerred on cgraph_node events. */
146 struct cgraph_node_hook_list {
147 cgraph_node_hook hook;
149 struct cgraph_node_hook_list *next;
152 /* List of hooks trigerred on events involving two cgraph_edges. */
153 struct cgraph_2edge_hook_list {
154 cgraph_2edge_hook hook;
156 struct cgraph_2edge_hook_list *next;
159 /* List of hooks trigerred on events involving two cgraph_nodes. */
160 struct cgraph_2node_hook_list {
161 cgraph_2node_hook hook;
163 struct cgraph_2node_hook_list *next;
166 /* List of hooks triggered when an edge is removed. */
167 struct cgraph_edge_hook_list *first_cgraph_edge_removal_hook;
168 /* List of hooks triggered when a node is removed. */
169 struct cgraph_node_hook_list *first_cgraph_node_removal_hook;
170 /* List of hooks triggered when an edge is duplicated. */
171 struct cgraph_2edge_hook_list *first_cgraph_edge_duplicated_hook;
172 /* List of hooks triggered when a node is duplicated. */
173 struct cgraph_2node_hook_list *first_cgraph_node_duplicated_hook;
176 /* Register HOOK to be called with DATA on each removed edge. */
177 struct cgraph_edge_hook_list *
178 cgraph_add_edge_removal_hook (cgraph_edge_hook hook, void *data)
180 struct cgraph_edge_hook_list *entry;
181 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
183 entry = (struct cgraph_edge_hook_list *) xmalloc (sizeof (*entry));
193 /* Remove ENTRY from the list of hooks called on removing edges. */
195 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *entry)
197 struct cgraph_edge_hook_list **ptr = &first_cgraph_edge_removal_hook;
199 while (*ptr != entry)
204 /* Call all edge removal hooks. */
206 cgraph_call_edge_removal_hooks (struct cgraph_edge *e)
208 struct cgraph_edge_hook_list *entry = first_cgraph_edge_removal_hook;
211 entry->hook (e, entry->data);
216 /* Register HOOK to be called with DATA on each removed node. */
217 struct cgraph_node_hook_list *
218 cgraph_add_node_removal_hook (cgraph_node_hook hook, void *data)
220 struct cgraph_node_hook_list *entry;
221 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
223 entry = (struct cgraph_node_hook_list *) xmalloc (sizeof (*entry));
233 /* Remove ENTRY from the list of hooks called on removing nodes. */
235 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *entry)
237 struct cgraph_node_hook_list **ptr = &first_cgraph_node_removal_hook;
239 while (*ptr != entry)
244 /* Call all node removal hooks. */
246 cgraph_call_node_removal_hooks (struct cgraph_node *node)
248 struct cgraph_node_hook_list *entry = first_cgraph_node_removal_hook;
251 entry->hook (node, entry->data);
256 /* Register HOOK to be called with DATA on each duplicated edge. */
257 struct cgraph_2edge_hook_list *
258 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook, void *data)
260 struct cgraph_2edge_hook_list *entry;
261 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
263 entry = (struct cgraph_2edge_hook_list *) xmalloc (sizeof (*entry));
273 /* Remove ENTRY from the list of hooks called on duplicating edges. */
275 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *entry)
277 struct cgraph_2edge_hook_list **ptr = &first_cgraph_edge_duplicated_hook;
279 while (*ptr != entry)
284 /* Call all edge duplication hooks. */
286 cgraph_call_edge_duplication_hooks (struct cgraph_edge *cs1,
287 struct cgraph_edge *cs2)
289 struct cgraph_2edge_hook_list *entry = first_cgraph_edge_duplicated_hook;
292 entry->hook (cs1, cs2, entry->data);
297 /* Register HOOK to be called with DATA on each duplicated node. */
298 struct cgraph_2node_hook_list *
299 cgraph_add_node_duplication_hook (cgraph_2node_hook hook, void *data)
301 struct cgraph_2node_hook_list *entry;
302 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
304 entry = (struct cgraph_2node_hook_list *) xmalloc (sizeof (*entry));
314 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
316 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *entry)
318 struct cgraph_2node_hook_list **ptr = &first_cgraph_node_duplicated_hook;
320 while (*ptr != entry)
325 /* Call all node duplication hooks. */
327 cgraph_call_node_duplication_hooks (struct cgraph_node *node1,
328 struct cgraph_node *node2)
330 struct cgraph_2node_hook_list *entry = first_cgraph_node_duplicated_hook;
333 entry->hook (node1, node2, entry->data);
338 /* Returns a hash code for P. */
341 hash_node (const void *p)
343 const struct cgraph_node *n = (const struct cgraph_node *) p;
344 return (hashval_t) DECL_UID (n->decl);
347 /* Returns nonzero if P1 and P2 are equal. */
350 eq_node (const void *p1, const void *p2)
352 const struct cgraph_node *n1 = (const struct cgraph_node *) p1;
353 const struct cgraph_node *n2 = (const struct cgraph_node *) p2;
354 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
357 /* Allocate new callgraph node and insert it into basic data structures. */
359 static struct cgraph_node *
360 cgraph_create_node (void)
362 struct cgraph_node *node;
364 node = GGC_CNEW (struct cgraph_node);
365 node->next = cgraph_nodes;
366 node->uid = cgraph_max_uid++;
368 node->order = cgraph_order++;
370 cgraph_nodes->previous = node;
371 node->previous = NULL;
372 node->global.estimated_growth = INT_MIN;
378 /* Return cgraph node assigned to DECL. Create new one when needed. */
381 cgraph_node (tree decl)
383 struct cgraph_node key, *node, **slot;
385 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
388 cgraph_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
392 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, &key, INSERT);
397 if (!node->master_clone)
398 node->master_clone = node;
402 node = cgraph_create_node ();
405 if (DECL_CONTEXT (decl) && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
407 node->origin = cgraph_node (DECL_CONTEXT (decl));
408 node->next_nested = node->origin->nested;
409 node->origin->nested = node;
410 node->master_clone = node;
415 /* Insert already constructed node into hashtable. */
418 cgraph_insert_node_to_hashtable (struct cgraph_node *node)
420 struct cgraph_node **slot;
422 slot = (struct cgraph_node **) htab_find_slot (cgraph_hash, node, INSERT);
429 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
430 Return NULL if there's no such node. */
433 cgraph_node_for_asm (tree asmname)
435 struct cgraph_node *node;
437 for (node = cgraph_nodes; node ; node = node->next)
438 if (decl_assembler_name_equal (node->decl, asmname))
444 /* Returns a hash value for X (which really is a die_struct). */
447 edge_hash (const void *x)
449 return htab_hash_pointer (((const struct cgraph_edge *) x)->call_stmt);
452 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
455 edge_eq (const void *x, const void *y)
457 return ((const struct cgraph_edge *) x)->call_stmt == y;
460 /* Return callgraph edge representing CALL_EXPR statement. */
462 cgraph_edge (struct cgraph_node *node, tree call_stmt)
464 struct cgraph_edge *e, *e2;
467 if (node->call_site_hash)
468 return (struct cgraph_edge *)
469 htab_find_with_hash (node->call_site_hash, call_stmt,
470 htab_hash_pointer (call_stmt));
472 /* This loop may turn out to be performance problem. In such case adding
473 hashtables into call nodes with very many edges is probably best
474 solution. It is not good idea to add pointer into CALL_EXPR itself
475 because we want to make possible having multiple cgraph nodes representing
476 different clones of the same body before the body is actually cloned. */
477 for (e = node->callees; e; e= e->next_callee)
479 if (e->call_stmt == call_stmt)
485 node->call_site_hash = htab_create_ggc (120, edge_hash, edge_eq, NULL);
486 for (e2 = node->callees; e2; e2 = e2->next_callee)
489 slot = htab_find_slot_with_hash (node->call_site_hash,
491 htab_hash_pointer (e2->call_stmt),
500 /* Change call_stmt of edge E to NEW_STMT. */
503 cgraph_set_call_stmt (struct cgraph_edge *e, tree new_stmt)
505 if (e->caller->call_site_hash)
507 htab_remove_elt_with_hash (e->caller->call_site_hash,
509 htab_hash_pointer (e->call_stmt));
511 e->call_stmt = new_stmt;
512 if (e->caller->call_site_hash)
515 slot = htab_find_slot_with_hash (e->caller->call_site_hash,
518 (e->call_stmt), INSERT);
524 /* Create edge from CALLER to CALLEE in the cgraph. */
527 cgraph_create_edge (struct cgraph_node *caller, struct cgraph_node *callee,
528 tree call_stmt, gcov_type count, int freq, int nest)
530 struct cgraph_edge *edge = GGC_NEW (struct cgraph_edge);
531 #ifdef ENABLE_CHECKING
532 struct cgraph_edge *e;
534 for (e = caller->callees; e; e = e->next_callee)
535 gcc_assert (e->call_stmt != call_stmt);
538 gcc_assert (get_call_expr_in (call_stmt));
540 if (!DECL_SAVED_TREE (callee->decl))
541 edge->inline_failed = N_("function body not available");
542 else if (callee->local.redefined_extern_inline)
543 edge->inline_failed = N_("redefined extern inline functions are not "
544 "considered for inlining");
545 else if (callee->local.inlinable)
546 edge->inline_failed = N_("function not considered for inlining");
548 edge->inline_failed = N_("function not inlinable");
552 edge->caller = caller;
553 edge->callee = callee;
554 edge->call_stmt = call_stmt;
555 edge->prev_caller = NULL;
556 edge->next_caller = callee->callers;
558 callee->callers->prev_caller = edge;
559 edge->prev_callee = NULL;
560 edge->next_callee = caller->callees;
562 caller->callees->prev_callee = edge;
563 caller->callees = edge;
564 callee->callers = edge;
566 gcc_assert (count >= 0);
567 edge->frequency = freq;
568 gcc_assert (freq >= 0);
569 gcc_assert (freq <= CGRAPH_FREQ_MAX);
570 edge->loop_nest = nest;
571 edge->uid = cgraph_edge_max_uid++;
572 if (caller->call_site_hash)
575 slot = htab_find_slot_with_hash (caller->call_site_hash,
586 /* Remove the edge E from the list of the callers of the callee. */
589 cgraph_edge_remove_callee (struct cgraph_edge *e)
592 e->prev_caller->next_caller = e->next_caller;
594 e->next_caller->prev_caller = e->prev_caller;
596 e->callee->callers = e->next_caller;
599 /* Remove the edge E from the list of the callees of the caller. */
602 cgraph_edge_remove_caller (struct cgraph_edge *e)
605 e->prev_callee->next_callee = e->next_callee;
607 e->next_callee->prev_callee = e->prev_callee;
609 e->caller->callees = e->next_callee;
610 if (e->caller->call_site_hash)
611 htab_remove_elt_with_hash (e->caller->call_site_hash,
613 htab_hash_pointer (e->call_stmt));
616 /* Remove the edge E in the cgraph. */
619 cgraph_remove_edge (struct cgraph_edge *e)
621 cgraph_call_edge_removal_hooks (e);
622 /* Remove from callers list of the callee. */
623 cgraph_edge_remove_callee (e);
625 /* Remove from callees list of the callers. */
626 cgraph_edge_remove_caller (e);
629 /* Redirect callee of E to N. The function does not update underlying
633 cgraph_redirect_edge_callee (struct cgraph_edge *e, struct cgraph_node *n)
635 /* Remove from callers list of the current callee. */
636 cgraph_edge_remove_callee (e);
638 /* Insert to callers list of the new callee. */
639 e->prev_caller = NULL;
641 n->callers->prev_caller = e;
642 e->next_caller = n->callers;
647 /* Update or remove corresponding cgraph edge if a call OLD_CALL
648 in OLD_STMT changed into NEW_STMT. */
651 cgraph_update_edges_for_call_stmt (tree old_stmt, tree old_call,
654 tree new_call = get_call_expr_in (new_stmt);
655 struct cgraph_node *node = cgraph_node (cfun->decl);
657 if (old_call != new_call)
659 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
660 struct cgraph_edge *ne = NULL;
665 gcov_type count = e->count;
666 int frequency = e->frequency;
667 int loop_nest = e->loop_nest;
669 cgraph_remove_edge (e);
672 new_decl = get_callee_fndecl (new_call);
675 ne = cgraph_create_edge (node, cgraph_node (new_decl),
676 new_stmt, count, frequency,
678 gcc_assert (ne->inline_failed);
683 else if (old_stmt != new_stmt)
685 struct cgraph_edge *e = cgraph_edge (node, old_stmt);
688 cgraph_set_call_stmt (e, new_stmt);
692 /* Remove all callees from the node. */
695 cgraph_node_remove_callees (struct cgraph_node *node)
697 struct cgraph_edge *e;
699 /* It is sufficient to remove the edges from the lists of callers of
700 the callees. The callee list of the node can be zapped with one
702 for (e = node->callees; e; e = e->next_callee)
704 cgraph_call_edge_removal_hooks (e);
705 cgraph_edge_remove_callee (e);
707 node->callees = NULL;
708 if (node->call_site_hash)
710 htab_delete (node->call_site_hash);
711 node->call_site_hash = NULL;
715 /* Remove all callers from the node. */
718 cgraph_node_remove_callers (struct cgraph_node *node)
720 struct cgraph_edge *e;
722 /* It is sufficient to remove the edges from the lists of callees of
723 the callers. The caller list of the node can be zapped with one
725 for (e = node->callers; e; e = e->next_caller)
727 cgraph_call_edge_removal_hooks (e);
728 cgraph_edge_remove_caller (e);
730 node->callers = NULL;
733 /* Release memory used to represent body of function NODE. */
736 cgraph_release_function_body (struct cgraph_node *node)
738 if (DECL_STRUCT_FUNCTION (node->decl)
739 && DECL_STRUCT_FUNCTION (node->decl)->gimple_df)
741 tree old_decl = current_function_decl;
742 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
743 current_function_decl = node->decl;
745 delete_tree_cfg_annotations ();
747 current_function_decl = old_decl;
750 DECL_SAVED_TREE (node->decl) = NULL;
751 DECL_STRUCT_FUNCTION (node->decl) = NULL;
752 DECL_INITIAL (node->decl) = error_mark_node;
755 /* Remove the node from cgraph. */
758 cgraph_remove_node (struct cgraph_node *node)
761 bool kill_body = false;
763 cgraph_call_node_removal_hooks (node);
764 cgraph_node_remove_callers (node);
765 cgraph_node_remove_callees (node);
766 /* Incremental inlining access removed nodes stored in the postorder list.
768 node->needed = node->reachable = false;
770 cgraph_remove_node (node->nested);
773 struct cgraph_node **node2 = &node->origin->nested;
775 while (*node2 != node)
776 node2 = &(*node2)->next_nested;
777 *node2 = node->next_nested;
780 node->previous->next = node->next;
782 cgraph_nodes = node->next;
784 node->next->previous = node->previous;
786 node->previous = NULL;
787 slot = htab_find_slot (cgraph_hash, node, NO_INSERT);
790 if (node->next_clone)
792 struct cgraph_node *new_node = node->next_clone;
793 struct cgraph_node *n;
795 /* Make the next clone be the master clone */
796 for (n = new_node; n; n = n->next_clone)
797 n->master_clone = new_node;
800 node->next_clone->prev_clone = NULL;
804 htab_clear_slot (cgraph_hash, slot);
810 node->prev_clone->next_clone = node->next_clone;
811 if (node->next_clone)
812 node->next_clone->prev_clone = node->prev_clone;
815 /* While all the clones are removed after being proceeded, the function
816 itself is kept in the cgraph even after it is compiled. Check whether
817 we are done with this body and reclaim it proactively if this is the case.
819 if (!kill_body && *slot)
821 struct cgraph_node *n = (struct cgraph_node *) *slot;
822 if (!n->next_clone && !n->global.inlined_to
823 && (cgraph_global_info_ready
824 && (TREE_ASM_WRITTEN (n->decl) || DECL_EXTERNAL (n->decl))))
828 if (kill_body && flag_unit_at_a_time)
829 cgraph_release_function_body (node);
831 if (node->call_site_hash)
833 htab_delete (node->call_site_hash);
834 node->call_site_hash = NULL;
837 /* Do not free the structure itself so the walk over chain can continue. */
840 /* Notify finalize_compilation_unit that given node is reachable. */
843 cgraph_mark_reachable_node (struct cgraph_node *node)
845 if (!node->reachable && node->local.finalized)
847 notice_global_symbol (node->decl);
849 gcc_assert (!cgraph_global_info_ready);
851 node->next_needed = cgraph_nodes_queue;
852 cgraph_nodes_queue = node;
856 /* Likewise indicate that a node is needed, i.e. reachable via some
860 cgraph_mark_needed_node (struct cgraph_node *node)
863 cgraph_mark_reachable_node (node);
866 /* Return local info for the compiled function. */
868 struct cgraph_local_info *
869 cgraph_local_info (tree decl)
871 struct cgraph_node *node;
873 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
874 node = cgraph_node (decl);
878 /* Return local info for the compiled function. */
880 struct cgraph_global_info *
881 cgraph_global_info (tree decl)
883 struct cgraph_node *node;
885 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL && cgraph_global_info_ready);
886 node = cgraph_node (decl);
887 return &node->global;
890 /* Return local info for the compiled function. */
892 struct cgraph_rtl_info *
893 cgraph_rtl_info (tree decl)
895 struct cgraph_node *node;
897 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
898 node = cgraph_node (decl);
899 if (decl != current_function_decl
900 && !TREE_ASM_WRITTEN (node->decl))
905 /* Return name of the node used in debug output. */
907 cgraph_node_name (struct cgraph_node *node)
909 return lang_hooks.decl_printable_name (node->decl, 2);
912 /* Names used to print out the availability enum. */
913 const char * const cgraph_availability_names[] =
914 {"unset", "not_available", "overwritable", "available", "local"};
917 /* Dump call graph node NODE to file F. */
920 dump_cgraph_node (FILE *f, struct cgraph_node *node)
922 struct cgraph_edge *edge;
923 fprintf (f, "%s/%i(%i):", cgraph_node_name (node), node->uid, node->pid);
924 if (node->global.inlined_to)
925 fprintf (f, " (inline copy in %s/%i)",
926 cgraph_node_name (node->global.inlined_to),
927 node->global.inlined_to->uid);
928 if (cgraph_function_flags_ready)
929 fprintf (f, " availability:%s",
930 cgraph_availability_names [cgraph_function_body_availability (node)]);
931 if (node->master_clone && node->master_clone->uid != node->uid)
932 fprintf (f, "(%i)", node->master_clone->uid);
934 fprintf (f, " executed "HOST_WIDEST_INT_PRINT_DEC"x",
935 (HOST_WIDEST_INT)node->count);
936 if (node->local.inline_summary.self_insns)
937 fprintf (f, " %i insns", node->local.inline_summary.self_insns);
938 if (node->global.insns && node->global.insns
939 != node->local.inline_summary.self_insns)
940 fprintf (f, " (%i after inlining)", node->global.insns);
941 if (node->local.inline_summary.estimated_self_stack_size)
942 fprintf (f, " %i bytes stack usage", (int)node->local.inline_summary.estimated_self_stack_size);
943 if (node->global.estimated_stack_size != node->local.inline_summary.estimated_self_stack_size)
944 fprintf (f, " %i bytes after inlining", (int)node->global.estimated_stack_size);
946 fprintf (f, " nested in: %s", cgraph_node_name (node->origin));
948 fprintf (f, " needed");
949 else if (node->reachable)
950 fprintf (f, " reachable");
951 if (DECL_SAVED_TREE (node->decl))
952 fprintf (f, " tree");
954 fprintf (f, " output");
955 if (node->local.local)
956 fprintf (f, " local");
957 if (node->local.externally_visible)
958 fprintf (f, " externally_visible");
959 if (node->local.finalized)
960 fprintf (f, " finalized");
961 if (node->local.disregard_inline_limits)
962 fprintf (f, " always_inline");
963 else if (node->local.inlinable)
964 fprintf (f, " inlinable");
965 if (node->local.redefined_extern_inline)
966 fprintf (f, " redefined_extern_inline");
967 if (TREE_ASM_WRITTEN (node->decl))
968 fprintf (f, " asm_written");
970 fprintf (f, "\n called by: ");
971 for (edge = node->callers; edge; edge = edge->next_caller)
973 fprintf (f, "%s/%i ", cgraph_node_name (edge->caller),
976 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
977 (HOST_WIDEST_INT)edge->count);
979 fprintf (f, "(%.2f per call) ",
980 edge->frequency / (double)CGRAPH_FREQ_BASE);
981 if (!edge->inline_failed)
982 fprintf(f, "(inlined) ");
985 fprintf (f, "\n calls: ");
986 for (edge = node->callees; edge; edge = edge->next_callee)
988 fprintf (f, "%s/%i ", cgraph_node_name (edge->callee),
990 if (!edge->inline_failed)
991 fprintf(f, "(inlined) ");
993 fprintf (f, "("HOST_WIDEST_INT_PRINT_DEC"x) ",
994 (HOST_WIDEST_INT)edge->count);
996 fprintf (f, "(%.2f per call) ",
997 edge->frequency / (double)CGRAPH_FREQ_BASE);
999 fprintf (f, "(nested in %i loops) ", edge->loop_nest);
1005 /* Dump call graph node NODE to stderr. */
1008 debug_cgraph_node (struct cgraph_node *node)
1010 dump_cgraph_node (stderr, node);
1014 /* Dump the callgraph to file F. */
1017 dump_cgraph (FILE *f)
1019 struct cgraph_node *node;
1021 fprintf (f, "callgraph:\n\n");
1022 for (node = cgraph_nodes; node; node = node->next)
1023 dump_cgraph_node (f, node);
1027 /* Dump the call graph to stderr. */
1032 dump_cgraph (stderr);
1036 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
1039 change_decl_assembler_name (tree decl, tree name)
1041 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
1043 SET_DECL_ASSEMBLER_NAME (decl, name);
1046 if (name == DECL_ASSEMBLER_NAME (decl))
1049 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
1050 && DECL_RTL_SET_P (decl))
1051 warning (0, "%D renamed after being referenced in assembly", decl);
1053 SET_DECL_ASSEMBLER_NAME (decl, name);
1056 /* Add a top-level asm statement to the list. */
1058 struct cgraph_asm_node *
1059 cgraph_add_asm_node (tree asm_str)
1061 struct cgraph_asm_node *node;
1063 node = GGC_CNEW (struct cgraph_asm_node);
1064 node->asm_str = asm_str;
1065 node->order = cgraph_order++;
1067 if (cgraph_asm_nodes == NULL)
1068 cgraph_asm_nodes = node;
1070 cgraph_asm_last_node->next = node;
1071 cgraph_asm_last_node = node;
1075 /* Return true when the DECL can possibly be inlined. */
1077 cgraph_function_possibly_inlined_p (tree decl)
1079 if (!cgraph_global_info_ready)
1080 return (DECL_INLINE (decl) && !flag_really_no_inline);
1081 return DECL_POSSIBLY_INLINED (decl);
1084 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
1085 struct cgraph_edge *
1086 cgraph_clone_edge (struct cgraph_edge *e, struct cgraph_node *n,
1087 tree call_stmt, gcov_type count_scale, int freq_scale,
1088 int loop_nest, bool update_original)
1090 struct cgraph_edge *new;
1091 gcov_type count = e->count * count_scale / REG_BR_PROB_BASE;
1092 gcov_type freq = e->frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
1094 if (freq > CGRAPH_FREQ_MAX)
1095 freq = CGRAPH_FREQ_MAX;
1096 new = cgraph_create_edge (n, e->callee, call_stmt, count, freq,
1097 e->loop_nest + loop_nest);
1099 new->inline_failed = e->inline_failed;
1100 if (update_original)
1102 e->count -= new->count;
1106 cgraph_call_edge_duplication_hooks (e, new);
1110 /* Create node representing clone of N executed COUNT times. Decrease
1111 the execution counts from original node too.
1113 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
1114 function's profile to reflect the fact that part of execution is handled
1116 struct cgraph_node *
1117 cgraph_clone_node (struct cgraph_node *n, gcov_type count, int freq, int loop_nest,
1118 bool update_original)
1120 struct cgraph_node *new = cgraph_create_node ();
1121 struct cgraph_edge *e;
1122 gcov_type count_scale;
1124 new->decl = n->decl;
1125 new->origin = n->origin;
1128 new->next_nested = new->origin->nested;
1129 new->origin->nested = new;
1131 new->analyzed = n->analyzed;
1132 new->local = n->local;
1133 new->global = n->global;
1135 new->master_clone = n->master_clone;
1138 count_scale = new->count * REG_BR_PROB_BASE / n->count;
1141 if (update_original)
1148 for (e = n->callees;e; e=e->next_callee)
1149 cgraph_clone_edge (e, new, e->call_stmt, count_scale, freq, loop_nest,
1152 new->next_clone = n->next_clone;
1153 new->prev_clone = n;
1154 n->next_clone = new;
1155 if (new->next_clone)
1156 new->next_clone->prev_clone = new;
1158 cgraph_call_node_duplication_hooks (n, new);
1162 /* Return true if N is an master_clone, (see cgraph_master_clone). */
1165 cgraph_is_master_clone (struct cgraph_node *n)
1167 return (n == cgraph_master_clone (n));
1170 struct cgraph_node *
1171 cgraph_master_clone (struct cgraph_node *n)
1173 enum availability avail = cgraph_function_body_availability (n);
1175 if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
1178 if (!n->master_clone)
1179 n->master_clone = cgraph_node (n->decl);
1181 return n->master_clone;
1184 /* NODE is no longer nested function; update cgraph accordingly. */
1186 cgraph_unnest_node (struct cgraph_node *node)
1188 struct cgraph_node **node2 = &node->origin->nested;
1189 gcc_assert (node->origin);
1191 while (*node2 != node)
1192 node2 = &(*node2)->next_nested;
1193 *node2 = node->next_nested;
1194 node->origin = NULL;
1197 /* Return function availability. See cgraph.h for description of individual
1200 cgraph_function_body_availability (struct cgraph_node *node)
1202 enum availability avail;
1203 gcc_assert (cgraph_function_flags_ready);
1204 if (!node->analyzed)
1205 avail = AVAIL_NOT_AVAILABLE;
1206 else if (node->local.local)
1207 avail = AVAIL_LOCAL;
1208 else if (node->local.externally_visible)
1209 avail = AVAIL_AVAILABLE;
1211 /* If the function can be overwritten, return OVERWRITABLE. Take
1212 care at least of two notable extensions - the COMDAT functions
1213 used to share template instantiations in C++ (this is symmetric
1214 to code cp_cannot_inline_tree_fn and probably shall be shared and
1215 the inlinability hooks completely eliminated).
1217 ??? Does the C++ one definition rule allow us to always return
1218 AVAIL_AVAILABLE here? That would be good reason to preserve this
1219 hook Similarly deal with extern inline functions - this is again
1220 necessary to get C++ shared functions having keyed templates
1221 right and in the C extension documentation we probably should
1222 document the requirement of both versions of function (extern
1223 inline and offline) having same side effect characteristics as
1224 good optimization is what this optimization is about. */
1226 else if (!(*targetm.binds_local_p) (node->decl)
1227 && !DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl))
1228 avail = AVAIL_OVERWRITABLE;
1229 else avail = AVAIL_AVAILABLE;
1234 /* Add the function FNDECL to the call graph.
1235 Unlike cgraph_finalize_function, this function is intended to be used
1236 by middle end and allows insertion of new function at arbitrary point
1237 of compilation. The function can be either in high, low or SSA form
1240 The function is assumed to be reachable and have address taken (so no
1241 API breaking optimizations are performed on it).
1243 Main work done by this function is to enqueue the function for later
1244 processing to avoid need the passes to be re-entrant. */
1247 cgraph_add_new_function (tree fndecl, bool lowered)
1249 struct cgraph_node *node;
1250 switch (cgraph_state)
1252 case CGRAPH_STATE_CONSTRUCTION:
1253 /* Just enqueue function to be processed at nearest occurrence. */
1254 node = cgraph_node (fndecl);
1255 node->next_needed = cgraph_new_nodes;
1257 node->lowered = true;
1258 cgraph_new_nodes = node;
1261 case CGRAPH_STATE_IPA:
1262 case CGRAPH_STATE_IPA_SSA:
1263 case CGRAPH_STATE_EXPANSION:
1264 /* Bring the function into finalized state and enqueue for later
1265 analyzing and compilation. */
1266 node = cgraph_node (fndecl);
1267 node->local.local = false;
1268 node->local.finalized = true;
1269 node->reachable = node->needed = true;
1270 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
1272 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1273 current_function_decl = fndecl;
1274 tree_register_cfg_hooks ();
1275 tree_lowering_passes (fndecl);
1276 bitmap_obstack_initialize (NULL);
1277 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
1278 execute_pass_list (pass_early_local_passes.pass.sub);
1279 bitmap_obstack_release (NULL);
1281 current_function_decl = NULL;
1286 node->lowered = true;
1287 node->next_needed = cgraph_new_nodes;
1288 cgraph_new_nodes = node;
1291 case CGRAPH_STATE_FINISHED:
1292 /* At the very end of compilation we have to do all the work up
1294 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
1295 current_function_decl = fndecl;
1296 tree_register_cfg_hooks ();
1298 tree_lowering_passes (fndecl);
1299 bitmap_obstack_initialize (NULL);
1300 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)) && optimize)
1301 execute_pass_list (pass_early_local_passes.pass.sub);
1302 bitmap_obstack_release (NULL);
1303 tree_rest_of_compilation (fndecl);
1305 current_function_decl = NULL;
1310 #include "gt-cgraph.h"