1 /* Callgraph construction.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "tree-flow.h"
27 #include "langhooks.h"
28 #include "pointer-set.h"
32 #include "tree-pass.h"
33 #include "ipa-utils.h"
35 #include "ipa-inline.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
, NULL
);
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_real_symbol_node (decl
);
78 cgraph_mark_address_taken_node (node
);
79 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
84 if (TREE_CODE (decl
) == VAR_DECL
)
86 struct varpool_node
*vnode
= varpool_node_for_decl (decl
);
87 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
95 /* Save some cycles by not walking types and declaration as we
96 won't find anything useful there anyway. */
97 if (IS_TYPE_OR_DECL_P (*tp
))
108 /* Record references to typeinfos in the type list LIST. */
111 record_type_list (struct cgraph_node
*node
, tree list
)
113 for (; list
; list
= TREE_CHAIN (list
))
115 tree type
= TREE_VALUE (list
);
118 type
= lookup_type_for_runtime (type
);
120 if (TREE_CODE (type
) == ADDR_EXPR
)
122 type
= TREE_OPERAND (type
, 0);
123 if (TREE_CODE (type
) == VAR_DECL
)
125 struct varpool_node
*vnode
= varpool_node_for_decl (type
);
126 ipa_record_reference ((symtab_node
)node
,
134 /* Record all references we will introduce by producing EH tables
138 record_eh_tables (struct cgraph_node
*node
, struct function
*fun
)
142 if (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
))
144 struct cgraph_node
*per_node
;
146 per_node
= cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
));
147 ipa_record_reference ((symtab_node
)node
, (symtab_node
)per_node
, IPA_REF_ADDR
, NULL
);
148 cgraph_mark_address_taken_node (per_node
);
151 i
= fun
->eh
->region_tree
;
160 case ERT_MUST_NOT_THROW
:
166 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
167 record_type_list (node
, c
->type_list
);
171 case ERT_ALLOWED_EXCEPTIONS
:
172 record_type_list (node
, i
->u
.allowed
.type_list
);
175 /* If there are sub-regions, process them. */
178 /* If there are peers, process them. */
179 else if (i
->next_peer
)
181 /* Otherwise, step back up the tree to the next peer. */
190 while (i
->next_peer
== NULL
);
196 /* Computes the frequency of the call statement so that it can be stored in
197 cgraph_edge. BB is the basic block of the call statement. */
199 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
201 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FUNCTION
202 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
203 int freq
= bb
->frequency
;
205 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
206 return CGRAPH_FREQ_BASE
;
209 entry_freq
= 1, freq
++;
211 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
212 if (freq
> CGRAPH_FREQ_MAX
)
213 freq
= CGRAPH_FREQ_MAX
;
218 /* Mark address taken in STMT. */
221 mark_address (gimple stmt
, tree addr
, void *data
)
223 addr
= get_base_address (addr
);
224 if (TREE_CODE (addr
) == FUNCTION_DECL
)
226 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (addr
);
227 cgraph_mark_address_taken_node (node
);
228 ipa_record_reference ((symtab_node
)data
,
232 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
233 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
235 struct varpool_node
*vnode
= varpool_node_for_decl (addr
);
237 ipa_record_reference ((symtab_node
)data
,
245 /* Mark load of T. */
248 mark_load (gimple stmt
, tree t
, void *data
)
250 t
= get_base_address (t
);
251 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
253 /* ??? This can happen on platforms with descriptors when these are
254 directly manipulated in the code. Pretend that it's an address. */
255 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (t
);
256 cgraph_mark_address_taken_node (node
);
257 ipa_record_reference ((symtab_node
)data
,
261 else if (t
&& TREE_CODE (t
) == VAR_DECL
262 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
264 struct varpool_node
*vnode
= varpool_node_for_decl (t
);
266 ipa_record_reference ((symtab_node
)data
,
273 /* Mark store of T. */
276 mark_store (gimple stmt
, tree t
, void *data
)
278 t
= get_base_address (t
);
279 if (t
&& TREE_CODE (t
) == VAR_DECL
280 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
282 struct varpool_node
*vnode
= varpool_node_for_decl (t
);
284 ipa_record_reference ((symtab_node
)data
,
286 IPA_REF_STORE
, stmt
);
291 /* Record all references from NODE that are taken in statement STMT. */
293 ipa_record_stmt_references (struct cgraph_node
*node
, gimple stmt
)
295 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
, mark_store
,
299 /* Create cgraph edges for function calls.
300 Also look for functions and variables having addresses taken. */
303 build_cgraph_edges (void)
306 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
307 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
308 gimple_stmt_iterator gsi
;
312 /* Create the callgraph edges and record the nodes referenced by the function.
316 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
318 gimple stmt
= gsi_stmt (gsi
);
321 if (is_gimple_debug (stmt
))
324 if (is_gimple_call (stmt
))
326 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
328 decl
= gimple_call_fndecl (stmt
);
330 cgraph_create_edge (node
, cgraph_get_create_node (decl
),
331 stmt
, bb
->count
, freq
);
333 cgraph_create_indirect_edge (node
, stmt
,
334 gimple_call_flags (stmt
),
337 ipa_record_stmt_references (node
, stmt
);
338 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
339 && gimple_omp_parallel_child_fn (stmt
))
341 tree fn
= gimple_omp_parallel_child_fn (stmt
);
342 ipa_record_reference ((symtab_node
)node
,
343 (symtab_node
)cgraph_get_create_real_symbol_node (fn
),
346 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
348 tree fn
= gimple_omp_task_child_fn (stmt
);
350 ipa_record_reference ((symtab_node
)node
,
351 (symtab_node
) cgraph_get_create_real_symbol_node (fn
),
353 fn
= gimple_omp_task_copy_fn (stmt
);
355 ipa_record_reference ((symtab_node
)node
,
356 (symtab_node
)cgraph_get_create_real_symbol_node (fn
),
360 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
361 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
364 /* Look for initializers of constant variables and private statics. */
365 FOR_EACH_LOCAL_DECL (cfun
, ix
, decl
)
366 if (TREE_CODE (decl
) == VAR_DECL
367 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
368 && !DECL_HAS_VALUE_EXPR_P (decl
))
369 varpool_finalize_decl (decl
);
370 record_eh_tables (node
, cfun
);
372 pointer_set_destroy (visited_nodes
);
378 const pass_data pass_data_build_cgraph_edges
=
380 GIMPLE_PASS
, /* type */
381 "*build_cgraph_edges", /* name */
382 OPTGROUP_NONE
, /* optinfo_flags */
383 false, /* has_gate */
384 true, /* has_execute */
386 PROP_cfg
, /* properties_required */
387 0, /* properties_provided */
388 0, /* properties_destroyed */
389 0, /* todo_flags_start */
390 0, /* todo_flags_finish */
393 class pass_build_cgraph_edges
: public gimple_opt_pass
396 pass_build_cgraph_edges(gcc::context
*ctxt
)
397 : gimple_opt_pass(pass_data_build_cgraph_edges
, ctxt
)
400 /* opt_pass methods: */
401 unsigned int execute () { return build_cgraph_edges (); }
403 }; // class pass_build_cgraph_edges
408 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
410 return new pass_build_cgraph_edges (ctxt
);
413 /* Record references to functions and other variables present in the
414 initial value of DECL, a variable.
415 When ONLY_VARS is true, we mark needed only variables, not functions. */
418 record_references_in_initializer (tree decl
, bool only_vars
)
420 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
421 struct varpool_node
*node
= varpool_node_for_decl (decl
);
422 struct record_reference_ctx ctx
= {false, NULL
};
424 ctx
.varpool_node
= node
;
425 ctx
.only_vars
= only_vars
;
426 walk_tree (&DECL_INITIAL (decl
), record_reference
,
427 &ctx
, visited_nodes
);
428 pointer_set_destroy (visited_nodes
);
431 /* Rebuild cgraph edges for current function node. This needs to be run after
432 passes that don't update the cgraph. */
435 rebuild_cgraph_edges (void)
438 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
439 gimple_stmt_iterator gsi
;
441 cgraph_node_remove_callees (node
);
442 ipa_remove_all_references (&node
->symbol
.ref_list
);
444 node
->count
= ENTRY_BLOCK_PTR
->count
;
448 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
450 gimple stmt
= gsi_stmt (gsi
);
453 if (is_gimple_call (stmt
))
455 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
457 decl
= gimple_call_fndecl (stmt
);
459 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
462 cgraph_create_indirect_edge (node
, stmt
,
463 gimple_call_flags (stmt
),
466 ipa_record_stmt_references (node
, stmt
);
468 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
469 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
471 record_eh_tables (node
, cfun
);
472 gcc_assert (!node
->global
.inlined_to
);
477 /* Rebuild cgraph edges for current function node. This needs to be run after
478 passes that don't update the cgraph. */
481 cgraph_rebuild_references (void)
484 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
485 gimple_stmt_iterator gsi
;
489 /* Keep speculative references for further cgraph edge expansion. */
490 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
);)
491 if (!ref
->speculative
)
492 ipa_remove_reference (ref
);
496 node
->count
= ENTRY_BLOCK_PTR
->count
;
500 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
501 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
502 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
503 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
505 record_eh_tables (node
, cfun
);
510 const pass_data pass_data_rebuild_cgraph_edges
=
512 GIMPLE_PASS
, /* type */
513 "*rebuild_cgraph_edges", /* name */
514 OPTGROUP_NONE
, /* optinfo_flags */
515 false, /* has_gate */
516 true, /* has_execute */
517 TV_CGRAPH
, /* tv_id */
518 PROP_cfg
, /* properties_required */
519 0, /* properties_provided */
520 0, /* properties_destroyed */
521 0, /* todo_flags_start */
522 0, /* todo_flags_finish */
525 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
528 pass_rebuild_cgraph_edges(gcc::context
*ctxt
)
529 : gimple_opt_pass(pass_data_rebuild_cgraph_edges
, ctxt
)
532 /* opt_pass methods: */
533 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (ctxt_
); }
534 unsigned int execute () { return rebuild_cgraph_edges (); }
536 }; // class pass_rebuild_cgraph_edges
541 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
543 return new pass_rebuild_cgraph_edges (ctxt
);
548 remove_cgraph_callee_edges (void)
550 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
551 cgraph_node_remove_callees (node
);
552 ipa_remove_all_references (&node
->symbol
.ref_list
);
558 const pass_data pass_data_remove_cgraph_callee_edges
=
560 GIMPLE_PASS
, /* type */
561 "*remove_cgraph_callee_edges", /* name */
562 OPTGROUP_NONE
, /* optinfo_flags */
563 false, /* has_gate */
564 true, /* has_execute */
566 0, /* properties_required */
567 0, /* properties_provided */
568 0, /* properties_destroyed */
569 0, /* todo_flags_start */
570 0, /* todo_flags_finish */
573 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
576 pass_remove_cgraph_callee_edges(gcc::context
*ctxt
)
577 : gimple_opt_pass(pass_data_remove_cgraph_callee_edges
, ctxt
)
580 /* opt_pass methods: */
581 opt_pass
* clone () {
582 return new pass_remove_cgraph_callee_edges (ctxt_
);
584 unsigned int execute () { return remove_cgraph_callee_edges (); }
586 }; // class pass_remove_cgraph_callee_edges
591 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
593 return new pass_remove_cgraph_callee_edges (ctxt
);