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"
34 #include "ipa-utils.h"
36 /* Context of record_reference. */
37 struct record_reference_ctx
40 struct varpool_node *varpool_node;
43 /* Walk tree and record all calls and references to functions/variables.
44 Called via walk_tree: TP is pointer to tree to be examined.
45 When DATA is non-null, record references to callgraph.
49 record_reference (tree *tp, int *walk_subtrees, void *data)
53 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
55 switch (TREE_CODE (t))
64 /* Record dereferences to the functions. This makes the
65 functions reachable unconditionally. */
66 decl = get_base_var (*tp);
67 if (TREE_CODE (decl) == FUNCTION_DECL)
70 cgraph_mark_address_taken_node (cgraph_node (decl));
71 ipa_record_reference (NULL, ctx->varpool_node,
72 cgraph_node (decl), NULL,
76 if (TREE_CODE (decl) == VAR_DECL)
78 struct varpool_node *vnode = varpool_node (decl);
79 if (lang_hooks.callgraph.analyze_expr)
80 lang_hooks.callgraph.analyze_expr (&decl, walk_subtrees);
81 varpool_mark_needed_node (vnode);
82 if (vnode->alias && vnode->extra_name)
83 vnode = vnode->extra_name;
84 ipa_record_reference (NULL, ctx->varpool_node,
92 /* Save some cycles by not walking types and declaration as we
93 won't find anything useful there anyway. */
94 if (IS_TYPE_OR_DECL_P (*tp))
100 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
101 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
108 /* Reset inlining information of all incoming call edges of NODE. */
111 reset_inline_failed (struct cgraph_node *node)
113 struct cgraph_edge *e;
115 for (e = node->callers; e; e = e->next_caller)
117 e->callee->global.inlined_to = NULL;
119 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
120 else if (node->local.redefined_extern_inline)
121 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
122 else if (!node->local.inlinable)
123 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
124 else if (e->call_stmt_cannot_inline_p)
125 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
127 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
131 /* Computes the frequency of the call statement so that it can be stored in
132 cgraph_edge. BB is the basic block of the call statement. */
134 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
136 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
137 (DECL_STRUCT_FUNCTION (decl))->frequency;
138 int freq = bb->frequency;
140 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
141 return CGRAPH_FREQ_BASE;
144 entry_freq = 1, freq++;
146 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
147 if (freq > CGRAPH_FREQ_MAX)
148 freq = CGRAPH_FREQ_MAX;
153 /* Mark address taken in STMT. */
156 mark_address (gimple stmt ATTRIBUTE_UNUSED, tree addr,
157 void *data ATTRIBUTE_UNUSED)
159 if (TREE_CODE (addr) == FUNCTION_DECL)
161 struct cgraph_node *node = cgraph_node (addr);
162 cgraph_mark_address_taken_node (node);
163 ipa_record_reference ((struct cgraph_node *)data, NULL,
169 addr = get_base_address (addr);
170 if (addr && TREE_CODE (addr) == VAR_DECL
171 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
173 struct varpool_node *vnode = varpool_node (addr);
176 if (lang_hooks.callgraph.analyze_expr)
177 lang_hooks.callgraph.analyze_expr (&addr, &walk_subtrees);
178 varpool_mark_needed_node (vnode);
179 if (vnode->alias && vnode->extra_name)
180 vnode = vnode->extra_name;
181 ipa_record_reference ((struct cgraph_node *)data, NULL,
190 /* Mark load of T. */
193 mark_load (gimple stmt ATTRIBUTE_UNUSED, tree t,
194 void *data ATTRIBUTE_UNUSED)
196 t = get_base_address (t);
197 if (TREE_CODE (t) == VAR_DECL
198 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
200 struct varpool_node *vnode = varpool_node (t);
203 if (lang_hooks.callgraph.analyze_expr)
204 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
205 varpool_mark_needed_node (vnode);
206 if (vnode->alias && vnode->extra_name)
207 vnode = vnode->extra_name;
208 ipa_record_reference ((struct cgraph_node *)data, NULL,
215 /* Mark store of T. */
218 mark_store (gimple stmt ATTRIBUTE_UNUSED, tree t,
219 void *data ATTRIBUTE_UNUSED)
221 t = get_base_address (t);
222 if (TREE_CODE (t) == VAR_DECL
223 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
225 struct varpool_node *vnode = varpool_node (t);
228 if (lang_hooks.callgraph.analyze_expr)
229 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
230 varpool_mark_needed_node (vnode);
231 if (vnode->alias && vnode->extra_name)
232 vnode = vnode->extra_name;
233 ipa_record_reference ((struct cgraph_node *)data, NULL,
235 IPA_REF_STORE, NULL);
240 /* Create cgraph edges for function calls.
241 Also look for functions and variables having addresses taken. */
244 build_cgraph_edges (void)
247 struct cgraph_node *node = cgraph_node (current_function_decl);
248 struct pointer_set_t *visited_nodes = pointer_set_create ();
249 gimple_stmt_iterator gsi;
252 /* Create the callgraph edges and record the nodes referenced by the function.
256 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
258 gimple stmt = gsi_stmt (gsi);
261 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
262 cgraph_create_edge (node, cgraph_node (decl), stmt,
264 compute_call_stmt_bb_frequency
265 (current_function_decl, bb),
267 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
268 mark_store, mark_address);
269 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
270 && gimple_omp_parallel_child_fn (stmt))
272 tree fn = gimple_omp_parallel_child_fn (stmt);
273 cgraph_mark_needed_node (cgraph_node (fn));
275 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
277 tree fn = gimple_omp_task_child_fn (stmt);
279 cgraph_mark_needed_node (cgraph_node (fn));
280 fn = gimple_omp_task_copy_fn (stmt);
282 cgraph_mark_needed_node (cgraph_node (fn));
285 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
286 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
287 mark_load, mark_store, mark_address);
290 /* Look for initializers of constant variables and private statics. */
291 for (step = cfun->local_decls;
293 step = TREE_CHAIN (step))
295 tree decl = TREE_VALUE (step);
296 if (TREE_CODE (decl) == VAR_DECL
297 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
298 varpool_finalize_decl (decl);
301 pointer_set_destroy (visited_nodes);
305 struct gimple_opt_pass pass_build_cgraph_edges =
309 "*build_cgraph_edges", /* name */
311 build_cgraph_edges, /* execute */
314 0, /* static_pass_number */
316 PROP_cfg, /* properties_required */
317 0, /* properties_provided */
318 0, /* properties_destroyed */
319 0, /* todo_flags_start */
320 0 /* todo_flags_finish */
324 /* Record references to functions and other variables present in the
325 initial value of DECL, a variable.
326 When ONLY_VARS is true, we mark needed only variables, not functions. */
329 record_references_in_initializer (tree decl, bool only_vars)
331 struct pointer_set_t *visited_nodes = pointer_set_create ();
332 struct varpool_node *node = varpool_node (decl);
333 struct record_reference_ctx ctx = {false, NULL};
335 ctx.varpool_node = node;
336 ctx.only_vars = only_vars;
337 walk_tree (&DECL_INITIAL (decl), record_reference,
338 &ctx, visited_nodes);
339 pointer_set_destroy (visited_nodes);
342 /* Rebuild cgraph edges for current function node. This needs to be run after
343 passes that don't update the cgraph. */
346 rebuild_cgraph_edges (void)
349 struct cgraph_node *node = cgraph_node (current_function_decl);
350 gimple_stmt_iterator gsi;
352 cgraph_node_remove_callees (node);
353 ipa_remove_all_references (&node->ref_list);
355 node->count = ENTRY_BLOCK_PTR->count;
359 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
361 gimple stmt = gsi_stmt (gsi);
364 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
365 cgraph_create_edge (node, cgraph_node (decl), stmt,
367 compute_call_stmt_bb_frequency
368 (current_function_decl, bb),
370 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
371 mark_store, mark_address);
374 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
375 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
376 mark_load, mark_store, mark_address);
378 gcc_assert (!node->global.inlined_to);
383 struct gimple_opt_pass pass_rebuild_cgraph_edges =
387 "*rebuild_cgraph_edges", /* name */
389 rebuild_cgraph_edges, /* execute */
392 0, /* static_pass_number */
394 PROP_cfg, /* properties_required */
395 0, /* properties_provided */
396 0, /* properties_destroyed */
397 0, /* todo_flags_start */
398 0, /* todo_flags_finish */
404 remove_cgraph_callee_edges (void)
406 cgraph_node_remove_callees (cgraph_node (current_function_decl));
410 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
414 "*remove_cgraph_callee_edges", /* name */
416 remove_cgraph_callee_edges, /* execute */
419 0, /* static_pass_number */
421 0, /* properties_required */
422 0, /* properties_provided */
423 0, /* properties_destroyed */
424 0, /* todo_flags_start */
425 0, /* todo_flags_finish */