1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
37 /* Context of record_reference. */
38 struct record_reference_ctx
41 struct varpool_node *varpool_node;
44 /* Walk tree and record all calls and references to functions/variables.
45 Called via walk_tree: TP is pointer to tree to be examined.
46 When DATA is non-null, record references to callgraph.
50 record_reference (tree *tp, int *walk_subtrees, void *data)
54 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
56 t = canonicalize_constructor_val (t);
62 switch (TREE_CODE (t))
71 /* Record dereferences to the functions. This makes the
72 functions reachable unconditionally. */
73 decl = get_base_var (*tp);
74 if (TREE_CODE (decl) == FUNCTION_DECL)
76 struct cgraph_node *node = cgraph_get_create_node (decl);
78 cgraph_mark_address_taken_node (node);
79 ipa_record_reference (NULL, ctx->varpool_node, node, NULL,
83 if (TREE_CODE (decl) == VAR_DECL)
85 struct varpool_node *vnode = varpool_node (decl);
86 if (lang_hooks.callgraph.analyze_expr)
87 lang_hooks.callgraph.analyze_expr (&decl, walk_subtrees);
88 varpool_mark_needed_node (vnode);
89 if (vnode->alias && vnode->extra_name)
90 vnode = vnode->extra_name;
91 ipa_record_reference (NULL, ctx->varpool_node,
99 /* Save some cycles by not walking types and declaration as we
100 won't find anything useful there anyway. */
101 if (IS_TYPE_OR_DECL_P (*tp))
107 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
108 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
115 /* Record references to typeinfos in the type list LIST. */
118 record_type_list (struct cgraph_node *node, tree list)
120 for (; list; list = TREE_CHAIN (list))
122 tree type = TREE_VALUE (list);
125 type = lookup_type_for_runtime (type);
127 if (TREE_CODE (type) == ADDR_EXPR)
129 type = TREE_OPERAND (type, 0);
130 if (TREE_CODE (type) == VAR_DECL)
132 struct varpool_node *vnode = varpool_node (type);
133 varpool_mark_needed_node (vnode);
134 ipa_record_reference (node, NULL,
142 /* Record all references we will introduce by producing EH tables
146 record_eh_tables (struct cgraph_node *node, struct function *fun)
150 if (DECL_FUNCTION_PERSONALITY (node->decl))
151 ipa_record_reference (node, NULL,
152 cgraph_get_create_node (DECL_FUNCTION_PERSONALITY (node->decl)),
153 NULL, IPA_REF_ADDR, NULL);
155 i = fun->eh->region_tree;
164 case ERT_MUST_NOT_THROW:
170 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
171 record_type_list (node, c->type_list);
175 case ERT_ALLOWED_EXCEPTIONS:
176 record_type_list (node, i->u.allowed.type_list);
179 /* If there are sub-regions, process them. */
182 /* If there are peers, process them. */
183 else if (i->next_peer)
185 /* Otherwise, step back up the tree to the next peer. */
194 while (i->next_peer == NULL);
200 /* Reset inlining information of all incoming call edges of NODE. */
203 reset_inline_failed (struct cgraph_node *node)
205 struct cgraph_edge *e;
207 for (e = node->callers; e; e = e->next_caller)
209 e->callee->global.inlined_to = NULL;
211 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
212 else if (node->local.redefined_extern_inline)
213 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
214 else if (!node->local.inlinable)
215 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
216 else if (e->call_stmt_cannot_inline_p)
217 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
219 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
223 /* Computes the frequency of the call statement so that it can be stored in
224 cgraph_edge. BB is the basic block of the call statement. */
226 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
228 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
229 (DECL_STRUCT_FUNCTION (decl))->frequency;
230 int freq = bb->frequency;
232 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
233 return CGRAPH_FREQ_BASE;
236 entry_freq = 1, freq++;
238 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
239 if (freq > CGRAPH_FREQ_MAX)
240 freq = CGRAPH_FREQ_MAX;
245 /* Mark address taken in STMT. */
248 mark_address (gimple stmt, tree addr, void *data)
250 addr = get_base_address (addr);
251 if (TREE_CODE (addr) == FUNCTION_DECL)
253 struct cgraph_node *node = cgraph_get_create_node (addr);
254 cgraph_mark_address_taken_node (node);
255 ipa_record_reference ((struct cgraph_node *)data, NULL,
259 else if (addr && TREE_CODE (addr) == VAR_DECL
260 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
262 struct varpool_node *vnode = varpool_node (addr);
265 if (lang_hooks.callgraph.analyze_expr)
266 lang_hooks.callgraph.analyze_expr (&addr, &walk_subtrees);
267 varpool_mark_needed_node (vnode);
268 if (vnode->alias && vnode->extra_name)
269 vnode = vnode->extra_name;
270 ipa_record_reference ((struct cgraph_node *)data, NULL,
278 /* Mark load of T. */
281 mark_load (gimple stmt, tree t, void *data)
283 t = get_base_address (t);
284 if (t && TREE_CODE (t) == FUNCTION_DECL)
286 /* ??? This can happen on platforms with descriptors when these are
287 directly manipulated in the code. Pretend that it's an address. */
288 struct cgraph_node *node = cgraph_get_create_node (t);
289 cgraph_mark_address_taken_node (node);
290 ipa_record_reference ((struct cgraph_node *)data, NULL,
294 else if (t && TREE_CODE (t) == VAR_DECL
295 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
297 struct varpool_node *vnode = varpool_node (t);
300 if (lang_hooks.callgraph.analyze_expr)
301 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
302 varpool_mark_needed_node (vnode);
303 if (vnode->alias && vnode->extra_name)
304 vnode = vnode->extra_name;
305 ipa_record_reference ((struct cgraph_node *)data, NULL,
312 /* Mark store of T. */
315 mark_store (gimple stmt, tree t, void *data)
317 t = get_base_address (t);
318 if (t && TREE_CODE (t) == VAR_DECL
319 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
321 struct varpool_node *vnode = varpool_node (t);
324 if (lang_hooks.callgraph.analyze_expr)
325 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
326 varpool_mark_needed_node (vnode);
327 if (vnode->alias && vnode->extra_name)
328 vnode = vnode->extra_name;
329 ipa_record_reference ((struct cgraph_node *)data, NULL,
331 IPA_REF_STORE, stmt);
336 /* Create cgraph edges for function calls.
337 Also look for functions and variables having addresses taken. */
340 build_cgraph_edges (void)
343 struct cgraph_node *node = cgraph_get_node (current_function_decl);
344 struct pointer_set_t *visited_nodes = pointer_set_create ();
345 gimple_stmt_iterator gsi;
349 /* Create the callgraph edges and record the nodes referenced by the function.
353 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
355 gimple stmt = gsi_stmt (gsi);
358 if (is_gimple_call (stmt))
360 int freq = compute_call_stmt_bb_frequency (current_function_decl,
362 decl = gimple_call_fndecl (stmt);
364 cgraph_create_edge (node, cgraph_get_create_node (decl),
365 stmt, bb->count, freq, bb->loop_depth);
367 cgraph_create_indirect_edge (node, stmt,
368 gimple_call_flags (stmt),
372 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
373 mark_store, mark_address);
374 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
375 && gimple_omp_parallel_child_fn (stmt))
377 tree fn = gimple_omp_parallel_child_fn (stmt);
378 ipa_record_reference (node, NULL, cgraph_get_create_node (fn),
379 NULL, IPA_REF_ADDR, stmt);
381 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
383 tree fn = gimple_omp_task_child_fn (stmt);
385 ipa_record_reference (node, NULL, cgraph_get_create_node (fn),
386 NULL, IPA_REF_ADDR, stmt);
387 fn = gimple_omp_task_copy_fn (stmt);
389 ipa_record_reference (node, NULL, cgraph_get_create_node (fn),
390 NULL, IPA_REF_ADDR, stmt);
393 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
394 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
395 mark_load, mark_store, mark_address);
398 /* Look for initializers of constant variables and private statics. */
399 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
400 if (TREE_CODE (decl) == VAR_DECL
401 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
402 varpool_finalize_decl (decl);
403 record_eh_tables (node, cfun);
405 pointer_set_destroy (visited_nodes);
409 struct gimple_opt_pass pass_build_cgraph_edges =
413 "*build_cgraph_edges", /* name */
415 build_cgraph_edges, /* execute */
418 0, /* static_pass_number */
420 PROP_cfg, /* properties_required */
421 0, /* properties_provided */
422 0, /* properties_destroyed */
423 0, /* todo_flags_start */
424 0 /* todo_flags_finish */
428 /* Record references to functions and other variables present in the
429 initial value of DECL, a variable.
430 When ONLY_VARS is true, we mark needed only variables, not functions. */
433 record_references_in_initializer (tree decl, bool only_vars)
435 struct pointer_set_t *visited_nodes = pointer_set_create ();
436 struct varpool_node *node = varpool_node (decl);
437 struct record_reference_ctx ctx = {false, NULL};
439 ctx.varpool_node = node;
440 ctx.only_vars = only_vars;
441 walk_tree (&DECL_INITIAL (decl), record_reference,
442 &ctx, visited_nodes);
443 pointer_set_destroy (visited_nodes);
446 /* Rebuild cgraph edges for current function node. This needs to be run after
447 passes that don't update the cgraph. */
450 rebuild_cgraph_edges (void)
453 struct cgraph_node *node = cgraph_get_node (current_function_decl);
454 gimple_stmt_iterator gsi;
456 cgraph_node_remove_callees (node);
457 ipa_remove_all_references (&node->ref_list);
459 node->count = ENTRY_BLOCK_PTR->count;
463 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
465 gimple stmt = gsi_stmt (gsi);
468 if (is_gimple_call (stmt))
470 int freq = compute_call_stmt_bb_frequency (current_function_decl,
472 decl = gimple_call_fndecl (stmt);
474 cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
475 bb->count, freq, bb->loop_depth);
477 cgraph_create_indirect_edge (node, stmt,
478 gimple_call_flags (stmt),
482 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
483 mark_store, mark_address);
486 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
487 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
488 mark_load, mark_store, mark_address);
490 record_eh_tables (node, cfun);
491 gcc_assert (!node->global.inlined_to);
496 /* Rebuild cgraph edges for current function node. This needs to be run after
497 passes that don't update the cgraph. */
500 cgraph_rebuild_references (void)
503 struct cgraph_node *node = cgraph_get_node (current_function_decl);
504 gimple_stmt_iterator gsi;
506 ipa_remove_all_references (&node->ref_list);
508 node->count = ENTRY_BLOCK_PTR->count;
512 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
514 gimple stmt = gsi_stmt (gsi);
516 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
517 mark_store, mark_address);
520 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
521 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
522 mark_load, mark_store, mark_address);
524 record_eh_tables (node, cfun);
527 struct gimple_opt_pass pass_rebuild_cgraph_edges =
531 "*rebuild_cgraph_edges", /* name */
533 rebuild_cgraph_edges, /* execute */
536 0, /* static_pass_number */
537 TV_CGRAPH, /* tv_id */
538 PROP_cfg, /* properties_required */
539 0, /* properties_provided */
540 0, /* properties_destroyed */
541 0, /* todo_flags_start */
542 0, /* todo_flags_finish */
548 remove_cgraph_callee_edges (void)
550 cgraph_node_remove_callees (cgraph_get_node (current_function_decl));
554 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
558 "*remove_cgraph_callee_edges", /* name */
560 remove_cgraph_callee_edges, /* execute */
563 0, /* static_pass_number */
565 0, /* properties_required */
566 0, /* properties_provided */
567 0, /* properties_destroyed */
568 0, /* todo_flags_start */
569 0, /* todo_flags_finish */