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
, NULL
);
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 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
96 /* Save some cycles by not walking types and declaration as we
97 won't find anything useful there anyway. */
98 if (IS_TYPE_OR_DECL_P (*tp
))
109 /* Record references to typeinfos in the type list LIST. */
112 record_type_list (struct cgraph_node
*node
, tree list
)
114 for (; list
; list
= TREE_CHAIN (list
))
116 tree type
= TREE_VALUE (list
);
119 type
= lookup_type_for_runtime (type
);
121 if (TREE_CODE (type
) == ADDR_EXPR
)
123 type
= TREE_OPERAND (type
, 0);
124 if (TREE_CODE (type
) == VAR_DECL
)
126 struct varpool_node
*vnode
= varpool_node (type
);
127 ipa_record_reference ((symtab_node
)node
,
135 /* Record all references we will introduce by producing EH tables
139 record_eh_tables (struct cgraph_node
*node
, struct function
*fun
)
143 if (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
))
145 struct cgraph_node
*per_node
;
147 per_node
= cgraph_get_create_node (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
));
148 ipa_record_reference ((symtab_node
)node
, (symtab_node
)per_node
, IPA_REF_ADDR
, NULL
);
149 cgraph_mark_address_taken_node (per_node
);
152 i
= fun
->eh
->region_tree
;
161 case ERT_MUST_NOT_THROW
:
167 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
168 record_type_list (node
, c
->type_list
);
172 case ERT_ALLOWED_EXCEPTIONS
:
173 record_type_list (node
, i
->u
.allowed
.type_list
);
176 /* If there are sub-regions, process them. */
179 /* If there are peers, process them. */
180 else if (i
->next_peer
)
182 /* Otherwise, step back up the tree to the next peer. */
191 while (i
->next_peer
== NULL
);
197 /* Computes the frequency of the call statement so that it can be stored in
198 cgraph_edge. BB is the basic block of the call statement. */
200 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
202 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FUNCTION
203 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
204 int freq
= bb
->frequency
;
206 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
207 return CGRAPH_FREQ_BASE
;
210 entry_freq
= 1, freq
++;
212 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
213 if (freq
> CGRAPH_FREQ_MAX
)
214 freq
= CGRAPH_FREQ_MAX
;
219 /* Mark address taken in STMT. */
222 mark_address (gimple stmt
, tree addr
, void *data
)
224 addr
= get_base_address (addr
);
225 if (TREE_CODE (addr
) == FUNCTION_DECL
)
227 struct cgraph_node
*node
= cgraph_get_create_node (addr
);
228 cgraph_mark_address_taken_node (node
);
229 ipa_record_reference ((symtab_node
)data
,
233 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
234 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
236 struct varpool_node
*vnode
= varpool_node (addr
);
238 ipa_record_reference ((symtab_node
)data
,
246 /* Mark load of T. */
249 mark_load (gimple stmt
, tree t
, void *data
)
251 t
= get_base_address (t
);
252 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
254 /* ??? This can happen on platforms with descriptors when these are
255 directly manipulated in the code. Pretend that it's an address. */
256 struct cgraph_node
*node
= cgraph_get_create_node (t
);
257 cgraph_mark_address_taken_node (node
);
258 ipa_record_reference ((symtab_node
)data
,
262 else if (t
&& TREE_CODE (t
) == VAR_DECL
263 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
265 struct varpool_node
*vnode
= varpool_node (t
);
267 ipa_record_reference ((symtab_node
)data
,
274 /* Mark store of T. */
277 mark_store (gimple stmt
, tree t
, void *data
)
279 t
= get_base_address (t
);
280 if (t
&& TREE_CODE (t
) == VAR_DECL
281 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
283 struct varpool_node
*vnode
= varpool_node (t
);
285 ipa_record_reference ((symtab_node
)data
,
287 IPA_REF_STORE
, stmt
);
292 /* Create cgraph edges for function calls.
293 Also look for functions and variables having addresses taken. */
296 build_cgraph_edges (void)
299 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
300 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
301 gimple_stmt_iterator gsi
;
305 /* Create the callgraph edges and record the nodes referenced by the function.
309 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
311 gimple stmt
= gsi_stmt (gsi
);
314 if (is_gimple_call (stmt
))
316 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
318 decl
= gimple_call_fndecl (stmt
);
320 cgraph_create_edge (node
, cgraph_get_create_node (decl
),
321 stmt
, bb
->count
, freq
);
323 cgraph_create_indirect_edge (node
, stmt
,
324 gimple_call_flags (stmt
),
327 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
328 mark_store
, mark_address
);
329 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
330 && gimple_omp_parallel_child_fn (stmt
))
332 tree fn
= gimple_omp_parallel_child_fn (stmt
);
333 ipa_record_reference ((symtab_node
)node
,
334 (symtab_node
)cgraph_get_create_node (fn
),
337 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
339 tree fn
= gimple_omp_task_child_fn (stmt
);
341 ipa_record_reference ((symtab_node
)node
,
342 (symtab_node
) cgraph_get_create_node (fn
),
344 fn
= gimple_omp_task_copy_fn (stmt
);
346 ipa_record_reference ((symtab_node
)node
,
347 (symtab_node
)cgraph_get_create_node (fn
),
351 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
352 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
353 mark_load
, mark_store
, mark_address
);
356 /* Look for initializers of constant variables and private statics. */
357 FOR_EACH_LOCAL_DECL (cfun
, ix
, decl
)
358 if (TREE_CODE (decl
) == VAR_DECL
359 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
360 && !DECL_HAS_VALUE_EXPR_P (decl
))
361 varpool_finalize_decl (decl
);
362 record_eh_tables (node
, cfun
);
364 pointer_set_destroy (visited_nodes
);
368 struct gimple_opt_pass pass_build_cgraph_edges
=
372 "*build_cgraph_edges", /* name */
374 build_cgraph_edges
, /* execute */
377 0, /* static_pass_number */
379 PROP_cfg
, /* properties_required */
380 0, /* properties_provided */
381 0, /* properties_destroyed */
382 0, /* todo_flags_start */
383 0 /* todo_flags_finish */
387 /* Record references to functions and other variables present in the
388 initial value of DECL, a variable.
389 When ONLY_VARS is true, we mark needed only variables, not functions. */
392 record_references_in_initializer (tree decl
, bool only_vars
)
394 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
395 struct varpool_node
*node
= varpool_node (decl
);
396 struct record_reference_ctx ctx
= {false, NULL
};
398 ctx
.varpool_node
= node
;
399 ctx
.only_vars
= only_vars
;
400 walk_tree (&DECL_INITIAL (decl
), record_reference
,
401 &ctx
, visited_nodes
);
402 pointer_set_destroy (visited_nodes
);
405 /* Rebuild cgraph edges for current function node. This needs to be run after
406 passes that don't update the cgraph. */
409 rebuild_cgraph_edges (void)
412 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
413 gimple_stmt_iterator gsi
;
415 cgraph_node_remove_callees (node
);
416 ipa_remove_all_references (&node
->symbol
.ref_list
);
418 node
->count
= ENTRY_BLOCK_PTR
->count
;
422 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
424 gimple stmt
= gsi_stmt (gsi
);
427 if (is_gimple_call (stmt
))
429 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
431 decl
= gimple_call_fndecl (stmt
);
433 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
436 cgraph_create_indirect_edge (node
, stmt
,
437 gimple_call_flags (stmt
),
440 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
441 mark_store
, mark_address
);
444 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
445 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
446 mark_load
, mark_store
, mark_address
);
448 record_eh_tables (node
, cfun
);
449 gcc_assert (!node
->global
.inlined_to
);
454 /* Rebuild cgraph edges for current function node. This needs to be run after
455 passes that don't update the cgraph. */
458 cgraph_rebuild_references (void)
461 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
462 gimple_stmt_iterator gsi
;
464 ipa_remove_all_references (&node
->symbol
.ref_list
);
466 node
->count
= ENTRY_BLOCK_PTR
->count
;
470 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
472 gimple stmt
= gsi_stmt (gsi
);
474 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
475 mark_store
, mark_address
);
478 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
479 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
480 mark_load
, mark_store
, mark_address
);
482 record_eh_tables (node
, cfun
);
485 struct gimple_opt_pass pass_rebuild_cgraph_edges
=
489 "*rebuild_cgraph_edges", /* name */
491 rebuild_cgraph_edges
, /* execute */
494 0, /* static_pass_number */
495 TV_CGRAPH
, /* tv_id */
496 PROP_cfg
, /* properties_required */
497 0, /* properties_provided */
498 0, /* properties_destroyed */
499 0, /* todo_flags_start */
500 0, /* todo_flags_finish */
506 remove_cgraph_callee_edges (void)
508 cgraph_node_remove_callees (cgraph_get_node (current_function_decl
));
512 struct gimple_opt_pass pass_remove_cgraph_callee_edges
=
516 "*remove_cgraph_callee_edges", /* name */
518 remove_cgraph_callee_edges
, /* execute */
521 0, /* static_pass_number */
523 0, /* properties_required */
524 0, /* properties_provided */
525 0, /* properties_destroyed */
526 0, /* todo_flags_start */
527 0, /* todo_flags_finish */