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_for_decl (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_for_decl (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_for_decl (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_for_decl (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_for_decl (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 */
373 OPTGROUP_NONE
, /* optinfo_flags */
375 build_cgraph_edges
, /* execute */
378 0, /* static_pass_number */
380 PROP_cfg
, /* properties_required */
381 0, /* properties_provided */
382 0, /* properties_destroyed */
383 0, /* todo_flags_start */
384 0 /* todo_flags_finish */
388 /* Record references to functions and other variables present in the
389 initial value of DECL, a variable.
390 When ONLY_VARS is true, we mark needed only variables, not functions. */
393 record_references_in_initializer (tree decl
, bool only_vars
)
395 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
396 struct varpool_node
*node
= varpool_node_for_decl (decl
);
397 struct record_reference_ctx ctx
= {false, NULL
};
399 ctx
.varpool_node
= node
;
400 ctx
.only_vars
= only_vars
;
401 walk_tree (&DECL_INITIAL (decl
), record_reference
,
402 &ctx
, visited_nodes
);
403 pointer_set_destroy (visited_nodes
);
406 /* Rebuild cgraph edges for current function node. This needs to be run after
407 passes that don't update the cgraph. */
410 rebuild_cgraph_edges (void)
413 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
414 gimple_stmt_iterator gsi
;
416 cgraph_node_remove_callees (node
);
417 ipa_remove_all_references (&node
->symbol
.ref_list
);
419 node
->count
= ENTRY_BLOCK_PTR
->count
;
423 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
425 gimple stmt
= gsi_stmt (gsi
);
428 if (is_gimple_call (stmt
))
430 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
432 decl
= gimple_call_fndecl (stmt
);
434 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
437 cgraph_create_indirect_edge (node
, stmt
,
438 gimple_call_flags (stmt
),
441 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
442 mark_store
, mark_address
);
445 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
446 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
447 mark_load
, mark_store
, mark_address
);
449 record_eh_tables (node
, cfun
);
450 gcc_assert (!node
->global
.inlined_to
);
455 /* Rebuild cgraph edges for current function node. This needs to be run after
456 passes that don't update the cgraph. */
459 cgraph_rebuild_references (void)
462 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
463 gimple_stmt_iterator gsi
;
465 ipa_remove_all_references (&node
->symbol
.ref_list
);
467 node
->count
= ENTRY_BLOCK_PTR
->count
;
471 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
473 gimple stmt
= gsi_stmt (gsi
);
475 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
,
476 mark_store
, mark_address
);
479 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
480 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), node
,
481 mark_load
, mark_store
, mark_address
);
483 record_eh_tables (node
, cfun
);
486 struct gimple_opt_pass pass_rebuild_cgraph_edges
=
490 "*rebuild_cgraph_edges", /* name */
491 OPTGROUP_NONE
, /* optinfo_flags */
493 rebuild_cgraph_edges
, /* execute */
496 0, /* static_pass_number */
497 TV_CGRAPH
, /* tv_id */
498 PROP_cfg
, /* properties_required */
499 0, /* properties_provided */
500 0, /* properties_destroyed */
501 0, /* todo_flags_start */
502 0, /* todo_flags_finish */
508 remove_cgraph_callee_edges (void)
510 cgraph_node_remove_callees (cgraph_get_node (current_function_decl
));
514 struct gimple_opt_pass pass_remove_cgraph_callee_edges
=
518 "*remove_cgraph_callee_edges", /* name */
519 OPTGROUP_NONE
, /* optinfo_flags */
521 remove_cgraph_callee_edges
, /* execute */
524 0, /* static_pass_number */
526 0, /* properties_required */
527 0, /* properties_provided */
528 0, /* properties_destroyed */
529 0, /* todo_flags_start */
530 0, /* todo_flags_finish */