1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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/>. */
24 #include "coretypes.h"
27 #include "tree-flow.h"
28 #include "langhooks.h"
29 #include "pointer-set.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.
37 When DATA is non-null, record references to callgraph.
41 record_reference (tree *tp, int *walk_subtrees, void *data)
45 bool do_callgraph = data != NULL;
47 switch (TREE_CODE (t))
50 if (TREE_STATIC (t) || DECL_EXTERNAL (t))
52 varpool_mark_needed_node (varpool_node (t));
53 if (lang_hooks.callgraph.analyze_expr)
54 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
60 /* Record dereferences to the functions. This makes the
61 functions reachable unconditionally. */
62 decl = TREE_OPERAND (*tp, 0);
63 if (TREE_CODE (decl) == FUNCTION_DECL && do_callgraph)
64 cgraph_mark_address_taken_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);
84 /* Reset inlining information of all incoming call edges of NODE. */
87 reset_inline_failed (struct cgraph_node *node)
89 struct cgraph_edge *e;
91 for (e = node->callers; e; e = e->next_caller)
93 e->callee->global.inlined_to = NULL;
95 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
96 else if (node->local.redefined_extern_inline)
97 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
98 else if (!node->local.inlinable)
99 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
100 else if (e->call_stmt_cannot_inline_p)
101 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
103 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
107 /* Computes the frequency of the call statement so that it can be stored in
108 cgraph_edge. BB is the basic block of the call statement. */
110 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
112 int entry_freq = ENTRY_BLOCK_PTR->frequency;
113 int freq = bb->frequency;
115 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
116 return CGRAPH_FREQ_BASE;
119 entry_freq = 1, freq++;
121 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
122 if (freq > CGRAPH_FREQ_MAX)
123 freq = CGRAPH_FREQ_MAX;
128 /* Create cgraph edges for function calls.
129 Also look for functions and variables having addresses taken. */
132 build_cgraph_edges (void)
135 struct cgraph_node *node = cgraph_node (current_function_decl);
136 struct pointer_set_t *visited_nodes = pointer_set_create ();
137 gimple_stmt_iterator gsi;
140 /* Create the callgraph edges and record the nodes referenced by the function.
143 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
145 gimple stmt = gsi_stmt (gsi);
148 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
151 size_t n = gimple_call_num_args (stmt);
152 cgraph_create_edge (node, cgraph_node (decl), stmt,
153 bb->count, compute_call_stmt_bb_frequency (current_function_decl, bb),
155 for (i = 0; i < n; i++)
156 walk_tree (gimple_call_arg_ptr (stmt, i), record_reference,
157 node, visited_nodes);
158 if (gimple_call_lhs (stmt))
159 walk_tree (gimple_call_lhs_ptr (stmt), record_reference, node,
164 struct walk_stmt_info wi;
165 memset (&wi, 0, sizeof (wi));
167 wi.pset = visited_nodes;
168 walk_gimple_op (stmt, record_reference, &wi);
169 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
170 && gimple_omp_parallel_child_fn (stmt))
172 tree fn = gimple_omp_parallel_child_fn (stmt);
173 cgraph_mark_needed_node (cgraph_node (fn));
175 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
177 tree fn = gimple_omp_task_child_fn (stmt);
179 cgraph_mark_needed_node (cgraph_node (fn));
180 fn = gimple_omp_task_copy_fn (stmt);
182 cgraph_mark_needed_node (cgraph_node (fn));
187 /* Look for initializers of constant variables and private statics. */
188 for (step = cfun->local_decls;
190 step = TREE_CHAIN (step))
192 tree decl = TREE_VALUE (step);
193 if (TREE_CODE (decl) == VAR_DECL
194 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
195 varpool_finalize_decl (decl);
196 else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
197 walk_tree (&DECL_INITIAL (decl), record_reference, node, visited_nodes);
200 pointer_set_destroy (visited_nodes);
204 struct gimple_opt_pass pass_build_cgraph_edges =
210 build_cgraph_edges, /* execute */
213 0, /* static_pass_number */
215 PROP_cfg, /* properties_required */
216 0, /* properties_provided */
217 0, /* properties_destroyed */
218 0, /* todo_flags_start */
219 0 /* todo_flags_finish */
223 /* Record references to functions and other variables present in the
224 initial value of DECL, a variable.
225 When ONLY_VARS is true, we mark needed only variables, not functions. */
228 record_references_in_initializer (tree decl, bool only_vars)
230 struct pointer_set_t *visited_nodes = pointer_set_create ();
231 walk_tree (&DECL_INITIAL (decl), record_reference,
232 only_vars ? NULL : decl, visited_nodes);
233 pointer_set_destroy (visited_nodes);
236 /* Rebuild cgraph edges for current function node. This needs to be run after
237 passes that don't update the cgraph. */
240 rebuild_cgraph_edges (void)
243 struct cgraph_node *node = cgraph_node (current_function_decl);
244 gimple_stmt_iterator gsi;
246 cgraph_node_remove_callees (node);
248 node->count = ENTRY_BLOCK_PTR->count;
251 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
253 gimple stmt = gsi_stmt (gsi);
256 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
257 cgraph_create_edge (node, cgraph_node (decl), stmt,
259 compute_call_stmt_bb_frequency
260 (current_function_decl, bb),
264 gcc_assert (!node->global.inlined_to);
269 struct gimple_opt_pass pass_rebuild_cgraph_edges =
275 rebuild_cgraph_edges, /* execute */
278 0, /* static_pass_number */
280 PROP_cfg, /* properties_required */
281 0, /* properties_provided */
282 0, /* properties_destroyed */
283 0, /* todo_flags_start */
284 0, /* todo_flags_finish */
290 remove_cgraph_callee_edges (void)
292 cgraph_node_remove_callees (cgraph_node (current_function_decl));
296 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
302 remove_cgraph_callee_edges, /* execute */
305 0, /* static_pass_number */
307 0, /* properties_required */
308 0, /* properties_provided */
309 0, /* properties_destroyed */
310 0, /* todo_flags_start */
311 0, /* todo_flags_finish */