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"
36 #include "ipa-inline.h"
38 /* Context of record_reference. */
39 struct record_reference_ctx
42 struct varpool_node
*varpool_node
;
45 /* Walk tree and record all calls and references to functions/variables.
46 Called via walk_tree: TP is pointer to tree to be examined.
47 When DATA is non-null, record references to callgraph.
51 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
55 struct record_reference_ctx
*ctx
= (struct record_reference_ctx
*)data
;
57 t
= canonicalize_constructor_val (t
);
63 switch (TREE_CODE (t
))
72 /* Record dereferences to the functions. This makes the
73 functions reachable unconditionally. */
74 decl
= get_base_var (*tp
);
75 if (TREE_CODE (decl
) == FUNCTION_DECL
)
77 struct cgraph_node
*node
= cgraph_get_create_node (decl
);
79 cgraph_mark_address_taken_node (node
);
80 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
85 if (TREE_CODE (decl
) == VAR_DECL
)
87 struct varpool_node
*vnode
= varpool_node (decl
);
88 if (lang_hooks
.callgraph
.analyze_expr
)
89 lang_hooks
.callgraph
.analyze_expr (&decl
, walk_subtrees
);
90 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
98 /* Save some cycles by not walking types and declaration as we
99 won't find anything useful there anyway. */
100 if (IS_TYPE_OR_DECL_P (*tp
))
106 if ((unsigned int) TREE_CODE (t
) >= LAST_AND_UNUSED_TREE_CODE
)
107 return lang_hooks
.callgraph
.analyze_expr (tp
, walk_subtrees
);
114 /* Record references to typeinfos in the type list LIST. */
117 record_type_list (struct cgraph_node
*node
, tree list
)
119 for (; list
; list
= TREE_CHAIN (list
))
121 tree type
= TREE_VALUE (list
);
124 type
= lookup_type_for_runtime (type
);
126 if (TREE_CODE (type
) == ADDR_EXPR
)
128 type
= TREE_OPERAND (type
, 0);
129 if (TREE_CODE (type
) == VAR_DECL
)
131 struct varpool_node
*vnode
= varpool_node (type
);
132 ipa_record_reference ((symtab_node
)node
,
140 /* Record all references we will introduce by producing EH tables
144 record_eh_tables (struct cgraph_node
*node
, struct function
*fun
)
148 if (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
))
150 struct cgraph_node
*per_node
;
152 per_node
= cgraph_get_create_node (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
));
153 ipa_record_reference ((symtab_node
)node
, (symtab_node
)per_node
, IPA_REF_ADDR
, NULL
);
154 cgraph_mark_address_taken_node (per_node
);
157 i
= fun
->eh
->region_tree
;
166 case ERT_MUST_NOT_THROW
:
172 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
173 record_type_list (node
, c
->type_list
);
177 case ERT_ALLOWED_EXCEPTIONS
:
178 record_type_list (node
, i
->u
.allowed
.type_list
);
181 /* If there are sub-regions, process them. */
184 /* If there are peers, process them. */
185 else if (i
->next_peer
)
187 /* Otherwise, step back up the tree to the next peer. */
196 while (i
->next_peer
== NULL
);
202 /* Computes the frequency of the call statement so that it can be stored in
203 cgraph_edge. BB is the basic block of the call statement. */
205 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
207 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FUNCTION
208 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
209 int freq
= bb
->frequency
;
211 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
212 return CGRAPH_FREQ_BASE
;
215 entry_freq
= 1, freq
++;
217 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
218 if (freq
> CGRAPH_FREQ_MAX
)
219 freq
= CGRAPH_FREQ_MAX
;
224 /* Mark address taken in STMT. */
227 mark_address (gimple stmt
, tree addr
, void *data
)
229 addr
= get_base_address (addr
);
230 if (TREE_CODE (addr
) == FUNCTION_DECL
)
232 struct cgraph_node
*node
= cgraph_get_create_node (addr
);
233 cgraph_mark_address_taken_node (node
);
234 ipa_record_reference ((symtab_node
)data
,
238 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
239 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
241 struct varpool_node
*vnode
= varpool_node (addr
);
244 if (lang_hooks
.callgraph
.analyze_expr
)
245 lang_hooks
.callgraph
.analyze_expr (&addr
, &walk_subtrees
);
246 ipa_record_reference ((symtab_node
)data
,
254 /* Mark load of T. */
257 mark_load (gimple stmt
, tree t
, void *data
)
259 t
= get_base_address (t
);
260 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
262 /* ??? This can happen on platforms with descriptors when these are
263 directly manipulated in the code. Pretend that it's an address. */
264 struct cgraph_node
*node
= cgraph_get_create_node (t
);
265 cgraph_mark_address_taken_node (node
);
266 ipa_record_reference ((symtab_node
)data
,
270 else if (t
&& TREE_CODE (t
) == VAR_DECL
271 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
273 struct varpool_node
*vnode
= varpool_node (t
);
276 if (lang_hooks
.callgraph
.analyze_expr
)
277 lang_hooks
.callgraph
.analyze_expr (&t
, &walk_subtrees
);
278 ipa_record_reference ((symtab_node
)data
,
285 /* Mark store of T. */
288 mark_store (gimple stmt
, tree t
, void *data
)
290 t
= get_base_address (t
);
291 if (t
&& TREE_CODE (t
) == VAR_DECL
292 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
294 struct varpool_node
*vnode
= varpool_node (t
);
297 if (lang_hooks
.callgraph
.analyze_expr
)
298 lang_hooks
.callgraph
.analyze_expr (&t
, &walk_subtrees
);
299 ipa_record_reference ((symtab_node
)data
,
301 IPA_REF_STORE
, stmt
);
306 /* Create cgraph edges for function calls.
307 Also look for functions and variables having addresses taken. */
310 build_cgraph_edges (void)
313 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
314 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
315 gimple_stmt_iterator gsi
;
319 /* Create the callgraph edges and record the nodes referenced by the function.
323 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
325 gimple stmt
= gsi_stmt (gsi
);
328 if (is_gimple_call (stmt
))
330 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
332 decl
= gimple_call_fndecl (stmt
);
334 cgraph_create_edge (node
, cgraph_get_create_node (decl
),
335 stmt
, bb
->count
, freq
);
337 cgraph_create_indirect_edge (node
, stmt
,
338 gimple_call_flags (stmt
),
341 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
342 mark_store
, mark_address
);
343 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
344 && gimple_omp_parallel_child_fn (stmt
))
346 tree fn
= gimple_omp_parallel_child_fn (stmt
);
347 ipa_record_reference ((symtab_node
)node
,
348 (symtab_node
)cgraph_get_create_node (fn
),
351 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
353 tree fn
= gimple_omp_task_child_fn (stmt
);
355 ipa_record_reference ((symtab_node
)node
,
356 (symtab_node
) cgraph_get_create_node (fn
),
358 fn
= gimple_omp_task_copy_fn (stmt
);
360 ipa_record_reference ((symtab_node
)node
,
361 (symtab_node
)cgraph_get_create_node (fn
),
365 for (gsi
= gsi_start (phi_nodes (bb
)); !gsi_end_p (gsi
); gsi_next (&gsi
))
366 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
367 mark_load
, mark_store
, mark_address
);
370 /* Look for initializers of constant variables and private statics. */
371 FOR_EACH_LOCAL_DECL (cfun
, ix
, decl
)
372 if (TREE_CODE (decl
) == VAR_DECL
373 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
)))
374 varpool_finalize_decl (decl
);
375 record_eh_tables (node
, cfun
);
377 pointer_set_destroy (visited_nodes
);
381 struct gimple_opt_pass pass_build_cgraph_edges
=
385 "*build_cgraph_edges", /* name */
387 build_cgraph_edges
, /* execute */
390 0, /* static_pass_number */
392 PROP_cfg
, /* properties_required */
393 0, /* properties_provided */
394 0, /* properties_destroyed */
395 0, /* todo_flags_start */
396 0 /* todo_flags_finish */
400 /* Record references to functions and other variables present in the
401 initial value of DECL, a variable.
402 When ONLY_VARS is true, we mark needed only variables, not functions. */
405 record_references_in_initializer (tree decl
, bool only_vars
)
407 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
408 struct varpool_node
*node
= varpool_node (decl
);
409 struct record_reference_ctx ctx
= {false, NULL
};
411 ctx
.varpool_node
= node
;
412 ctx
.only_vars
= only_vars
;
413 walk_tree (&DECL_INITIAL (decl
), record_reference
,
414 &ctx
, visited_nodes
);
415 pointer_set_destroy (visited_nodes
);
418 /* Rebuild cgraph edges for current function node. This needs to be run after
419 passes that don't update the cgraph. */
422 rebuild_cgraph_edges (void)
425 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
426 gimple_stmt_iterator gsi
;
428 cgraph_node_remove_callees (node
);
429 ipa_remove_all_references (&node
->symbol
.ref_list
);
431 node
->count
= ENTRY_BLOCK_PTR
->count
;
435 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
437 gimple stmt
= gsi_stmt (gsi
);
440 if (is_gimple_call (stmt
))
442 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
444 decl
= gimple_call_fndecl (stmt
);
446 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
449 cgraph_create_indirect_edge (node
, stmt
,
450 gimple_call_flags (stmt
),
453 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
454 mark_store
, mark_address
);
457 for (gsi
= gsi_start (phi_nodes (bb
)); !gsi_end_p (gsi
); gsi_next (&gsi
))
458 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
459 mark_load
, mark_store
, mark_address
);
461 record_eh_tables (node
, cfun
);
462 gcc_assert (!node
->global
.inlined_to
);
467 /* Rebuild cgraph edges for current function node. This needs to be run after
468 passes that don't update the cgraph. */
471 cgraph_rebuild_references (void)
474 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
475 gimple_stmt_iterator gsi
;
477 ipa_remove_all_references (&node
->symbol
.ref_list
);
479 node
->count
= ENTRY_BLOCK_PTR
->count
;
483 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
485 gimple stmt
= gsi_stmt (gsi
);
487 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
488 mark_store
, mark_address
);
491 for (gsi
= gsi_start (phi_nodes (bb
)); !gsi_end_p (gsi
); gsi_next (&gsi
))
492 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
493 mark_load
, mark_store
, mark_address
);
495 record_eh_tables (node
, cfun
);
498 struct gimple_opt_pass pass_rebuild_cgraph_edges
=
502 "*rebuild_cgraph_edges", /* name */
504 rebuild_cgraph_edges
, /* execute */
507 0, /* static_pass_number */
508 TV_CGRAPH
, /* tv_id */
509 PROP_cfg
, /* properties_required */
510 0, /* properties_provided */
511 0, /* properties_destroyed */
512 0, /* todo_flags_start */
513 0, /* todo_flags_finish */
519 remove_cgraph_callee_edges (void)
521 cgraph_node_remove_callees (cgraph_get_node (current_function_decl
));
525 struct gimple_opt_pass pass_remove_cgraph_callee_edges
=
529 "*remove_cgraph_callee_edges", /* name */
531 remove_cgraph_callee_edges
, /* execute */
534 0, /* static_pass_number */
536 0, /* properties_required */
537 0, /* properties_provided */
538 0, /* properties_destroyed */
539 0, /* todo_flags_start */
540 0, /* todo_flags_finish */