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 "langhooks.h"
27 #include "pointer-set.h"
29 #include "tree-pass.h"
30 #include "ipa-utils.h"
32 #include "ipa-inline.h"
34 /* Context of record_reference. */
35 struct record_reference_ctx
38 struct varpool_node
*varpool_node
;
41 /* Walk tree and record all calls and references to functions/variables.
42 Called via walk_tree: TP is pointer to tree to be examined.
43 When DATA is non-null, record references to callgraph.
47 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
51 struct record_reference_ctx
*ctx
= (struct record_reference_ctx
*)data
;
53 t
= canonicalize_constructor_val (t
, NULL
);
59 switch (TREE_CODE (t
))
68 /* Record dereferences to the functions. This makes the
69 functions reachable unconditionally. */
70 decl
= get_base_var (*tp
);
71 if (TREE_CODE (decl
) == FUNCTION_DECL
)
73 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (decl
);
75 cgraph_mark_address_taken_node (node
);
76 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
81 if (TREE_CODE (decl
) == VAR_DECL
)
83 struct varpool_node
*vnode
= varpool_node_for_decl (decl
);
84 ipa_record_reference ((symtab_node
)ctx
->varpool_node
,
92 /* Save some cycles by not walking types and declaration as we
93 won't find anything useful there anyway. */
94 if (IS_TYPE_OR_DECL_P (*tp
))
105 /* Record references to typeinfos in the type list LIST. */
108 record_type_list (struct cgraph_node
*node
, tree list
)
110 for (; list
; list
= TREE_CHAIN (list
))
112 tree type
= TREE_VALUE (list
);
115 type
= lookup_type_for_runtime (type
);
117 if (TREE_CODE (type
) == ADDR_EXPR
)
119 type
= TREE_OPERAND (type
, 0);
120 if (TREE_CODE (type
) == VAR_DECL
)
122 struct varpool_node
*vnode
= varpool_node_for_decl (type
);
123 ipa_record_reference ((symtab_node
)node
,
131 /* Record all references we will introduce by producing EH tables
135 record_eh_tables (struct cgraph_node
*node
, struct function
*fun
)
139 if (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
))
141 struct cgraph_node
*per_node
;
143 per_node
= cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node
->symbol
.decl
));
144 ipa_record_reference ((symtab_node
)node
, (symtab_node
)per_node
, IPA_REF_ADDR
, NULL
);
145 cgraph_mark_address_taken_node (per_node
);
148 i
= fun
->eh
->region_tree
;
157 case ERT_MUST_NOT_THROW
:
163 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
164 record_type_list (node
, c
->type_list
);
168 case ERT_ALLOWED_EXCEPTIONS
:
169 record_type_list (node
, i
->u
.allowed
.type_list
);
172 /* If there are sub-regions, process them. */
175 /* If there are peers, process them. */
176 else if (i
->next_peer
)
178 /* Otherwise, step back up the tree to the next peer. */
187 while (i
->next_peer
== NULL
);
193 /* Computes the frequency of the call statement so that it can be stored in
194 cgraph_edge. BB is the basic block of the call statement. */
196 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
198 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FUNCTION
199 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
200 int freq
= bb
->frequency
;
202 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
203 return CGRAPH_FREQ_BASE
;
206 entry_freq
= 1, freq
++;
208 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
209 if (freq
> CGRAPH_FREQ_MAX
)
210 freq
= CGRAPH_FREQ_MAX
;
215 /* Mark address taken in STMT. */
218 mark_address (gimple stmt
, tree addr
, void *data
)
220 addr
= get_base_address (addr
);
221 if (TREE_CODE (addr
) == FUNCTION_DECL
)
223 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (addr
);
224 cgraph_mark_address_taken_node (node
);
225 ipa_record_reference ((symtab_node
)data
,
229 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
230 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
232 struct varpool_node
*vnode
= varpool_node_for_decl (addr
);
234 ipa_record_reference ((symtab_node
)data
,
242 /* Mark load of T. */
245 mark_load (gimple stmt
, tree t
, void *data
)
247 t
= get_base_address (t
);
248 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
250 /* ??? This can happen on platforms with descriptors when these are
251 directly manipulated in the code. Pretend that it's an address. */
252 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (t
);
253 cgraph_mark_address_taken_node (node
);
254 ipa_record_reference ((symtab_node
)data
,
258 else if (t
&& TREE_CODE (t
) == VAR_DECL
259 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
261 struct varpool_node
*vnode
= varpool_node_for_decl (t
);
263 ipa_record_reference ((symtab_node
)data
,
270 /* Mark store of T. */
273 mark_store (gimple stmt
, tree t
, void *data
)
275 t
= get_base_address (t
);
276 if (t
&& TREE_CODE (t
) == VAR_DECL
277 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
279 struct varpool_node
*vnode
= varpool_node_for_decl (t
);
281 ipa_record_reference ((symtab_node
)data
,
283 IPA_REF_STORE
, stmt
);
288 /* Record all references from NODE that are taken in statement STMT. */
290 ipa_record_stmt_references (struct cgraph_node
*node
, gimple stmt
)
292 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
, mark_store
,
296 /* Create cgraph edges for function calls.
297 Also look for functions and variables having addresses taken. */
300 build_cgraph_edges (void)
303 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
304 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
305 gimple_stmt_iterator gsi
;
309 /* Create the callgraph edges and record the nodes referenced by the function.
313 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
315 gimple stmt
= gsi_stmt (gsi
);
318 if (is_gimple_debug (stmt
))
321 if (is_gimple_call (stmt
))
323 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
325 decl
= gimple_call_fndecl (stmt
);
327 cgraph_create_edge (node
, cgraph_get_create_node (decl
),
328 stmt
, bb
->count
, freq
);
330 cgraph_create_indirect_edge (node
, stmt
,
331 gimple_call_flags (stmt
),
334 ipa_record_stmt_references (node
, stmt
);
335 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
336 && gimple_omp_parallel_child_fn (stmt
))
338 tree fn
= gimple_omp_parallel_child_fn (stmt
);
339 ipa_record_reference ((symtab_node
)node
,
340 (symtab_node
)cgraph_get_create_real_symbol_node (fn
),
343 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
345 tree fn
= gimple_omp_task_child_fn (stmt
);
347 ipa_record_reference ((symtab_node
)node
,
348 (symtab_node
) cgraph_get_create_real_symbol_node (fn
),
350 fn
= gimple_omp_task_copy_fn (stmt
);
352 ipa_record_reference ((symtab_node
)node
,
353 (symtab_node
)cgraph_get_create_real_symbol_node (fn
),
357 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
358 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
361 /* Look for initializers of constant variables and private statics. */
362 FOR_EACH_LOCAL_DECL (cfun
, ix
, decl
)
363 if (TREE_CODE (decl
) == VAR_DECL
364 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
365 && !DECL_HAS_VALUE_EXPR_P (decl
))
366 varpool_finalize_decl (decl
);
367 record_eh_tables (node
, cfun
);
369 pointer_set_destroy (visited_nodes
);
375 const pass_data pass_data_build_cgraph_edges
=
377 GIMPLE_PASS
, /* type */
378 "*build_cgraph_edges", /* name */
379 OPTGROUP_NONE
, /* optinfo_flags */
380 false, /* has_gate */
381 true, /* has_execute */
383 PROP_cfg
, /* properties_required */
384 0, /* properties_provided */
385 0, /* properties_destroyed */
386 0, /* todo_flags_start */
387 0, /* todo_flags_finish */
390 class pass_build_cgraph_edges
: public gimple_opt_pass
393 pass_build_cgraph_edges (gcc::context
*ctxt
)
394 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
397 /* opt_pass methods: */
398 unsigned int execute () { return build_cgraph_edges (); }
400 }; // class pass_build_cgraph_edges
405 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
407 return new pass_build_cgraph_edges (ctxt
);
410 /* Record references to functions and other variables present in the
411 initial value of DECL, a variable.
412 When ONLY_VARS is true, we mark needed only variables, not functions. */
415 record_references_in_initializer (tree decl
, bool only_vars
)
417 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
418 struct varpool_node
*node
= varpool_node_for_decl (decl
);
419 struct record_reference_ctx ctx
= {false, NULL
};
421 ctx
.varpool_node
= node
;
422 ctx
.only_vars
= only_vars
;
423 walk_tree (&DECL_INITIAL (decl
), record_reference
,
424 &ctx
, visited_nodes
);
425 pointer_set_destroy (visited_nodes
);
428 /* Rebuild cgraph edges for current function node. This needs to be run after
429 passes that don't update the cgraph. */
432 rebuild_cgraph_edges (void)
435 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
436 gimple_stmt_iterator gsi
;
438 cgraph_node_remove_callees (node
);
439 ipa_remove_all_references (&node
->symbol
.ref_list
);
441 node
->count
= ENTRY_BLOCK_PTR
->count
;
445 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
447 gimple stmt
= gsi_stmt (gsi
);
450 if (is_gimple_call (stmt
))
452 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
454 decl
= gimple_call_fndecl (stmt
);
456 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
459 cgraph_create_indirect_edge (node
, stmt
,
460 gimple_call_flags (stmt
),
463 ipa_record_stmt_references (node
, stmt
);
465 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
466 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
468 record_eh_tables (node
, cfun
);
469 gcc_assert (!node
->global
.inlined_to
);
474 /* Rebuild cgraph edges for current function node. This needs to be run after
475 passes that don't update the cgraph. */
478 cgraph_rebuild_references (void)
481 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
482 gimple_stmt_iterator gsi
;
486 /* Keep speculative references for further cgraph edge expansion. */
487 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
);)
488 if (!ref
->speculative
)
489 ipa_remove_reference (ref
);
493 node
->count
= ENTRY_BLOCK_PTR
->count
;
497 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
498 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
499 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
500 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
502 record_eh_tables (node
, cfun
);
507 const pass_data pass_data_rebuild_cgraph_edges
=
509 GIMPLE_PASS
, /* type */
510 "*rebuild_cgraph_edges", /* name */
511 OPTGROUP_NONE
, /* optinfo_flags */
512 false, /* has_gate */
513 true, /* has_execute */
514 TV_CGRAPH
, /* tv_id */
515 PROP_cfg
, /* properties_required */
516 0, /* properties_provided */
517 0, /* properties_destroyed */
518 0, /* todo_flags_start */
519 0, /* todo_flags_finish */
522 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
525 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
526 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
529 /* opt_pass methods: */
530 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
531 unsigned int execute () { return rebuild_cgraph_edges (); }
533 }; // class pass_rebuild_cgraph_edges
538 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
540 return new pass_rebuild_cgraph_edges (ctxt
);
545 remove_cgraph_callee_edges (void)
547 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
548 cgraph_node_remove_callees (node
);
549 ipa_remove_all_references (&node
->symbol
.ref_list
);
555 const pass_data pass_data_remove_cgraph_callee_edges
=
557 GIMPLE_PASS
, /* type */
558 "*remove_cgraph_callee_edges", /* name */
559 OPTGROUP_NONE
, /* optinfo_flags */
560 false, /* has_gate */
561 true, /* has_execute */
563 0, /* properties_required */
564 0, /* properties_provided */
565 0, /* properties_destroyed */
566 0, /* todo_flags_start */
567 0, /* todo_flags_finish */
570 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
573 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
574 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
577 /* opt_pass methods: */
578 opt_pass
* clone () {
579 return new pass_remove_cgraph_callee_edges (m_ctxt
);
581 unsigned int execute () { return remove_cgraph_callee_edges (); }
583 }; // class pass_remove_cgraph_callee_edges
588 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
590 return new pass_remove_cgraph_callee_edges (ctxt
);