1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 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
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
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
24 #include "coretypes.h"
27 #include "tree-flow.h"
28 #include "langhooks.h"
29 #include "pointer-set.h"
32 #include "tree-gimple.h"
33 #include "tree-pass.h"
35 /* Walk tree and record all calls and references to functions/variables.
36 Called via walk_tree: TP is pointer to tree to be examined. */
39 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
43 switch (TREE_CODE (t
))
46 if (TREE_STATIC (t
) || DECL_EXTERNAL (t
))
48 varpool_mark_needed_node (varpool_node (t
));
49 if (lang_hooks
.callgraph
.analyze_expr
)
50 return lang_hooks
.callgraph
.analyze_expr (tp
, walk_subtrees
,
57 if (flag_unit_at_a_time
)
59 /* Record dereferences to the functions. This makes the
60 functions reachable unconditionally. */
61 tree decl
= TREE_OPERAND (*tp
, 0);
62 if (TREE_CODE (decl
) == FUNCTION_DECL
)
63 cgraph_mark_needed_node (cgraph_node (decl
));
68 /* Save some cycles by not walking types and declaration as we
69 won't find anything useful there anyway. */
70 if (IS_TYPE_OR_DECL_P (*tp
))
76 if ((unsigned int) TREE_CODE (t
) >= LAST_AND_UNUSED_TREE_CODE
)
77 return lang_hooks
.callgraph
.analyze_expr (tp
, walk_subtrees
, data
);
84 /* Give initial reasons why inlining would fail on all calls from
85 NODE. Those get either nullified or usually overwritten by more precise
89 initialize_inline_failed (struct cgraph_node
*node
)
91 struct cgraph_edge
*e
;
93 for (e
= node
->callers
; e
; e
= e
->next_caller
)
95 gcc_assert (!e
->callee
->global
.inlined_to
);
96 gcc_assert (e
->inline_failed
);
97 if (node
->local
.redefined_extern_inline
)
98 e
->inline_failed
= N_("redefined extern inline functions are not "
99 "considered for inlining");
100 else if (!node
->local
.inlinable
)
101 e
->inline_failed
= N_("function not inlinable");
103 e
->inline_failed
= N_("function not considered for inlining");
107 /* Create cgraph edges for function calls.
108 Also look for functions and variables having addresses taken. */
111 build_cgraph_edges (void)
114 struct cgraph_node
*node
= cgraph_node (current_function_decl
);
115 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
116 block_stmt_iterator bsi
;
119 /* Create the callgraph edges and record the nodes referenced by the function.
122 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
124 tree stmt
= bsi_stmt (bsi
);
125 tree call
= get_call_expr_in (stmt
);
128 if (call
&& (decl
= get_callee_fndecl (call
)))
130 cgraph_create_edge (node
, cgraph_node (decl
), stmt
,
133 walk_tree (&TREE_OPERAND (call
, 1),
134 record_reference
, node
, visited_nodes
);
135 if (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
)
136 walk_tree (&GIMPLE_STMT_OPERAND (stmt
, 0),
137 record_reference
, node
, visited_nodes
);
140 walk_tree (bsi_stmt_ptr (bsi
), record_reference
, node
, visited_nodes
);
143 /* Look for initializers of constant variables and private statics. */
144 for (step
= cfun
->unexpanded_var_list
;
146 step
= TREE_CHAIN (step
))
148 tree decl
= TREE_VALUE (step
);
149 if (TREE_CODE (decl
) == VAR_DECL
150 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
151 && flag_unit_at_a_time
)
152 varpool_finalize_decl (decl
);
153 else if (TREE_CODE (decl
) == VAR_DECL
&& DECL_INITIAL (decl
))
154 walk_tree (&DECL_INITIAL (decl
), record_reference
, node
, visited_nodes
);
157 pointer_set_destroy (visited_nodes
);
158 initialize_inline_failed (node
);
162 struct tree_opt_pass pass_build_cgraph_edges
=
166 build_cgraph_edges
, /* execute */
169 0, /* static_pass_number */
171 PROP_cfg
, /* properties_required */
172 0, /* properties_provided */
173 0, /* properties_destroyed */
174 0, /* todo_flags_start */
175 0, /* todo_flags_finish */
179 /* Record references to functions and other variables present in the
180 initial value of DECL, a variable. */
183 record_references_in_initializer (tree decl
)
185 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
186 walk_tree (&DECL_INITIAL (decl
), record_reference
, NULL
, visited_nodes
);
187 pointer_set_destroy (visited_nodes
);
190 /* Rebuild cgraph edges for current function node. This needs to be run after
191 passes that don't update the cgraph. */
194 rebuild_cgraph_edges (void)
197 struct cgraph_node
*node
= cgraph_node (current_function_decl
);
198 block_stmt_iterator bsi
;
200 cgraph_node_remove_callees (node
);
202 node
->count
= ENTRY_BLOCK_PTR
->count
;
205 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
207 tree stmt
= bsi_stmt (bsi
);
208 tree call
= get_call_expr_in (stmt
);
211 if (call
&& (decl
= get_callee_fndecl (call
)))
212 cgraph_create_edge (node
, cgraph_node (decl
), stmt
,
216 initialize_inline_failed (node
);
217 gcc_assert (!node
->global
.inlined_to
);
221 struct tree_opt_pass pass_rebuild_cgraph_edges
=
225 rebuild_cgraph_edges
, /* execute */
228 0, /* static_pass_number */
230 PROP_cfg
, /* properties_required */
231 0, /* properties_provided */
232 0, /* properties_destroyed */
233 0, /* todo_flags_start */
234 0, /* todo_flags_finish */