1 /* Callgraph construction.
2 Copyright (C) 2003-2014 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 "pointer-set.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-fold.h"
31 #include "gimple-expr.h"
34 #include "gimple-iterator.h"
35 #include "gimple-walk.h"
36 #include "langhooks.h"
38 #include "tree-pass.h"
39 #include "ipa-utils.h"
41 #include "ipa-inline.h"
43 /* Context of record_reference. */
44 struct record_reference_ctx
47 class varpool_node
*varpool_node
;
50 /* Walk tree and record all calls and references to functions/variables.
51 Called via walk_tree: TP is pointer to tree to be examined.
52 When DATA is non-null, record references to callgraph.
56 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
60 struct record_reference_ctx
*ctx
= (struct record_reference_ctx
*)data
;
62 t
= canonicalize_constructor_val (t
, NULL
);
68 switch (TREE_CODE (t
))
77 /* Record dereferences to the functions. This makes the
78 functions reachable unconditionally. */
79 decl
= get_base_var (*tp
);
80 if (TREE_CODE (decl
) == FUNCTION_DECL
)
82 struct cgraph_node
*node
= cgraph_get_create_node (decl
);
84 cgraph_mark_address_taken_node (node
);
85 ipa_record_reference (ctx
->varpool_node
,
90 if (TREE_CODE (decl
) == VAR_DECL
)
92 varpool_node
*vnode
= varpool_node_for_decl (decl
);
93 ipa_record_reference (ctx
->varpool_node
,
101 /* Save some cycles by not walking types and declaration as we
102 won't find anything useful there anyway. */
103 if (IS_TYPE_OR_DECL_P (*tp
))
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 varpool_node
*vnode
= varpool_node_for_decl (type
);
132 ipa_record_reference (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
->decl
))
150 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
151 struct cgraph_node
*per_node
= cgraph_get_create_node (per_decl
);
153 ipa_record_reference (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_FN
208 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
209 int freq
= bb
->frequency
;
211 if (profile_status_for_fn (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
, tree
, 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 varpool_node
*vnode
= varpool_node_for_decl (addr
);
243 ipa_record_reference ((symtab_node
*)data
,
251 /* Mark load of T. */
254 mark_load (gimple stmt
, tree t
, tree
, void *data
)
256 t
= get_base_address (t
);
257 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
259 /* ??? This can happen on platforms with descriptors when these are
260 directly manipulated in the code. Pretend that it's an address. */
261 struct cgraph_node
*node
= cgraph_get_create_node (t
);
262 cgraph_mark_address_taken_node (node
);
263 ipa_record_reference ((symtab_node
*)data
,
267 else if (t
&& TREE_CODE (t
) == VAR_DECL
268 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
270 varpool_node
*vnode
= varpool_node_for_decl (t
);
272 ipa_record_reference ((symtab_node
*)data
,
279 /* Mark store of T. */
282 mark_store (gimple stmt
, tree t
, tree
, void *data
)
284 t
= get_base_address (t
);
285 if (t
&& TREE_CODE (t
) == VAR_DECL
286 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
288 varpool_node
*vnode
= varpool_node_for_decl (t
);
290 ipa_record_reference ((symtab_node
*)data
,
292 IPA_REF_STORE
, stmt
);
297 /* Record all references from NODE that are taken in statement STMT. */
299 ipa_record_stmt_references (struct cgraph_node
*node
, gimple stmt
)
301 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
, mark_store
,
305 /* Create cgraph edges for function calls.
306 Also look for functions and variables having addresses taken. */
310 const pass_data pass_data_build_cgraph_edges
=
312 GIMPLE_PASS
, /* type */
313 "*build_cgraph_edges", /* name */
314 OPTGROUP_NONE
, /* optinfo_flags */
315 true, /* has_execute */
317 PROP_cfg
, /* properties_required */
318 0, /* properties_provided */
319 0, /* properties_destroyed */
320 0, /* todo_flags_start */
321 0, /* todo_flags_finish */
324 class pass_build_cgraph_edges
: public gimple_opt_pass
327 pass_build_cgraph_edges (gcc::context
*ctxt
)
328 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
331 /* opt_pass methods: */
332 virtual unsigned int execute (function
*);
334 }; // class pass_build_cgraph_edges
337 pass_build_cgraph_edges::execute (function
*fun
)
340 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
341 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
342 gimple_stmt_iterator gsi
;
346 /* Create the callgraph edges and record the nodes referenced by the function.
348 FOR_EACH_BB_FN (bb
, fun
)
350 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
352 gimple stmt
= gsi_stmt (gsi
);
355 if (is_gimple_debug (stmt
))
358 if (is_gimple_call (stmt
))
360 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
362 decl
= gimple_call_fndecl (stmt
);
364 cgraph_create_edge (node
, cgraph_get_create_node (decl
),
365 stmt
, bb
->count
, freq
);
366 else if (gimple_call_internal_p (stmt
))
369 cgraph_create_indirect_edge (node
, stmt
,
370 gimple_call_flags (stmt
),
373 ipa_record_stmt_references (node
, stmt
);
374 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
375 && gimple_omp_parallel_child_fn (stmt
))
377 tree fn
= gimple_omp_parallel_child_fn (stmt
);
378 ipa_record_reference (node
,
379 cgraph_get_create_node (fn
),
382 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
384 tree fn
= gimple_omp_task_child_fn (stmt
);
386 ipa_record_reference (node
,
387 cgraph_get_create_node (fn
),
389 fn
= gimple_omp_task_copy_fn (stmt
);
391 ipa_record_reference (node
,
392 cgraph_get_create_node (fn
),
396 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
397 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
400 /* Look for initializers of constant variables and private statics. */
401 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
402 if (TREE_CODE (decl
) == VAR_DECL
403 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
404 && !DECL_HAS_VALUE_EXPR_P (decl
))
405 varpool_finalize_decl (decl
);
406 record_eh_tables (node
, fun
);
408 pointer_set_destroy (visited_nodes
);
415 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
417 return new pass_build_cgraph_edges (ctxt
);
420 /* Record references to functions and other variables present in the
421 initial value of DECL, a variable.
422 When ONLY_VARS is true, we mark needed only variables, not functions. */
425 record_references_in_initializer (tree decl
, bool only_vars
)
427 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
428 varpool_node
*node
= varpool_node_for_decl (decl
);
429 struct record_reference_ctx ctx
= {false, NULL
};
431 ctx
.varpool_node
= node
;
432 ctx
.only_vars
= only_vars
;
433 walk_tree (&DECL_INITIAL (decl
), record_reference
,
434 &ctx
, visited_nodes
);
435 pointer_set_destroy (visited_nodes
);
438 /* Rebuild cgraph edges for current function node. This needs to be run after
439 passes that don't update the cgraph. */
442 rebuild_cgraph_edges (void)
445 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
446 gimple_stmt_iterator gsi
;
448 cgraph_node_remove_callees (node
);
449 ipa_remove_all_references (&node
->ref_list
);
451 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
453 FOR_EACH_BB_FN (bb
, cfun
)
455 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
457 gimple stmt
= gsi_stmt (gsi
);
460 if (is_gimple_call (stmt
))
462 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
464 decl
= gimple_call_fndecl (stmt
);
466 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
468 else if (gimple_call_internal_p (stmt
))
471 cgraph_create_indirect_edge (node
, stmt
,
472 gimple_call_flags (stmt
),
475 ipa_record_stmt_references (node
, stmt
);
477 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
478 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
480 record_eh_tables (node
, cfun
);
481 gcc_assert (!node
->global
.inlined_to
);
486 /* Rebuild cgraph edges for current function node. This needs to be run after
487 passes that don't update the cgraph. */
490 cgraph_rebuild_references (void)
493 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
494 gimple_stmt_iterator gsi
;
498 /* Keep speculative references for further cgraph edge expansion. */
499 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
);)
500 if (!ref
->speculative
)
501 ipa_remove_reference (ref
);
505 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
507 FOR_EACH_BB_FN (bb
, cfun
)
509 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
510 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
511 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
512 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
514 record_eh_tables (node
, cfun
);
519 const pass_data pass_data_rebuild_cgraph_edges
=
521 GIMPLE_PASS
, /* type */
522 "*rebuild_cgraph_edges", /* name */
523 OPTGROUP_NONE
, /* optinfo_flags */
524 true, /* has_execute */
525 TV_CGRAPH
, /* tv_id */
526 PROP_cfg
, /* properties_required */
527 0, /* properties_provided */
528 0, /* properties_destroyed */
529 0, /* todo_flags_start */
530 0, /* todo_flags_finish */
533 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
536 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
537 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
540 /* opt_pass methods: */
541 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
542 virtual unsigned int execute (function
*) { return rebuild_cgraph_edges (); }
544 }; // class pass_rebuild_cgraph_edges
549 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
551 return new pass_rebuild_cgraph_edges (ctxt
);
557 const pass_data pass_data_remove_cgraph_callee_edges
=
559 GIMPLE_PASS
, /* type */
560 "*remove_cgraph_callee_edges", /* name */
561 OPTGROUP_NONE
, /* optinfo_flags */
562 true, /* has_execute */
564 0, /* properties_required */
565 0, /* properties_provided */
566 0, /* properties_destroyed */
567 0, /* todo_flags_start */
568 0, /* todo_flags_finish */
571 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
574 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
575 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
578 /* opt_pass methods: */
579 opt_pass
* clone () {
580 return new pass_remove_cgraph_callee_edges (m_ctxt
);
582 virtual unsigned int execute (function
*);
584 }; // class pass_remove_cgraph_callee_edges
587 pass_remove_cgraph_callee_edges::execute (function
*)
589 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
590 cgraph_node_remove_callees (node
);
591 ipa_remove_all_references (&node
->ref_list
);
598 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
600 return new pass_remove_cgraph_callee_edges (ctxt
);