1 /* Callgraph construction.
2 Copyright (C) 2003-2015 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"
28 #include "fold-const.h"
30 #include "hard-reg-set.h"
32 #include "dominance.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
38 #include "gimple-expr.h"
40 #include "gimple-iterator.h"
41 #include "gimple-walk.h"
42 #include "langhooks.h"
44 #include "tree-pass.h"
45 #include "plugin-api.h"
48 #include "ipa-utils.h"
50 #include "alloc-pool.h"
51 #include "symbol-summary.h"
53 #include "ipa-inline.h"
55 /* Context of record_reference. */
56 struct record_reference_ctx
59 class varpool_node
*varpool_node
;
62 /* Walk tree and record all calls and references to functions/variables.
63 Called via walk_tree: TP is pointer to tree to be examined.
64 When DATA is non-null, record references to callgraph.
68 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
72 record_reference_ctx
*ctx
= (record_reference_ctx
*)data
;
74 t
= canonicalize_constructor_val (t
, NULL
);
80 switch (TREE_CODE (t
))
89 /* Record dereferences to the functions. This makes the
90 functions reachable unconditionally. */
91 decl
= get_base_var (*tp
);
92 if (TREE_CODE (decl
) == FUNCTION_DECL
)
94 cgraph_node
*node
= cgraph_node::get_create (decl
);
96 node
->mark_address_taken ();
97 ctx
->varpool_node
->create_reference (node
, IPA_REF_ADDR
);
100 if (TREE_CODE (decl
) == VAR_DECL
)
102 varpool_node
*vnode
= varpool_node::get_create (decl
);
103 ctx
->varpool_node
->create_reference (vnode
, IPA_REF_ADDR
);
109 /* Save some cycles by not walking types and declaration as we
110 won't find anything useful there anyway. */
111 if (IS_TYPE_OR_DECL_P (*tp
))
122 /* Record references to typeinfos in the type list LIST. */
125 record_type_list (cgraph_node
*node
, tree list
)
127 for (; list
; list
= TREE_CHAIN (list
))
129 tree type
= TREE_VALUE (list
);
132 type
= lookup_type_for_runtime (type
);
134 if (TREE_CODE (type
) == ADDR_EXPR
)
136 type
= TREE_OPERAND (type
, 0);
137 if (TREE_CODE (type
) == VAR_DECL
)
139 varpool_node
*vnode
= varpool_node::get_create (type
);
140 node
->create_reference (vnode
, IPA_REF_ADDR
);
146 /* Record all references we will introduce by producing EH tables
150 record_eh_tables (cgraph_node
*node
, function
*fun
)
154 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
156 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
157 cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
159 node
->create_reference (per_node
, IPA_REF_ADDR
);
160 per_node
->mark_address_taken ();
163 i
= fun
->eh
->region_tree
;
172 case ERT_MUST_NOT_THROW
:
178 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
179 record_type_list (node
, c
->type_list
);
183 case ERT_ALLOWED_EXCEPTIONS
:
184 record_type_list (node
, i
->u
.allowed
.type_list
);
187 /* If there are sub-regions, process them. */
190 /* If there are peers, process them. */
191 else if (i
->next_peer
)
193 /* Otherwise, step back up the tree to the next peer. */
202 while (i
->next_peer
== NULL
);
208 /* Computes the frequency of the call statement so that it can be stored in
209 cgraph_edge. BB is the basic block of the call statement. */
211 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
213 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FN
214 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
215 int freq
= bb
->frequency
;
217 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
218 return CGRAPH_FREQ_BASE
;
221 entry_freq
= 1, freq
++;
223 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
224 if (freq
> CGRAPH_FREQ_MAX
)
225 freq
= CGRAPH_FREQ_MAX
;
230 /* Mark address taken in STMT. */
233 mark_address (gimple stmt
, tree addr
, tree
, void *data
)
235 addr
= get_base_address (addr
);
236 if (TREE_CODE (addr
) == FUNCTION_DECL
)
238 cgraph_node
*node
= cgraph_node::get_create (addr
);
239 node
->mark_address_taken ();
240 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
242 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
243 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
245 varpool_node
*vnode
= varpool_node::get_create (addr
);
247 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_ADDR
, stmt
);
253 /* Mark load of T. */
256 mark_load (gimple stmt
, tree t
, tree
, void *data
)
258 t
= get_base_address (t
);
259 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
261 /* ??? This can happen on platforms with descriptors when these are
262 directly manipulated in the code. Pretend that it's an address. */
263 cgraph_node
*node
= cgraph_node::get_create (t
);
264 node
->mark_address_taken ();
265 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
267 else if (t
&& TREE_CODE (t
) == VAR_DECL
268 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
270 varpool_node
*vnode
= varpool_node::get_create (t
);
272 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_LOAD
, stmt
);
277 /* Mark store of T. */
280 mark_store (gimple stmt
, tree t
, tree
, void *data
)
282 t
= get_base_address (t
);
283 if (t
&& TREE_CODE (t
) == VAR_DECL
284 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
286 varpool_node
*vnode
= varpool_node::get_create (t
);
288 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_STORE
, stmt
);
293 /* Record all references from cgraph_node that are taken in statement STMT. */
296 cgraph_node::record_stmt_references (gimple stmt
)
298 walk_stmt_load_store_addr_ops (stmt
, this, mark_load
, mark_store
,
302 /* Create cgraph edges for function calls.
303 Also look for functions and variables having addresses taken. */
307 const pass_data pass_data_build_cgraph_edges
=
309 GIMPLE_PASS
, /* type */
310 "*build_cgraph_edges", /* name */
311 OPTGROUP_NONE
, /* optinfo_flags */
313 PROP_cfg
, /* properties_required */
314 0, /* properties_provided */
315 0, /* properties_destroyed */
316 0, /* todo_flags_start */
317 0, /* todo_flags_finish */
320 class pass_build_cgraph_edges
: public gimple_opt_pass
323 pass_build_cgraph_edges (gcc::context
*ctxt
)
324 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
327 /* opt_pass methods: */
328 virtual unsigned int execute (function
*);
330 }; // class pass_build_cgraph_edges
333 pass_build_cgraph_edges::execute (function
*fun
)
336 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
337 gimple_stmt_iterator gsi
;
341 /* Create the callgraph edges and record the nodes referenced by the function.
343 FOR_EACH_BB_FN (bb
, fun
)
345 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
347 gimple stmt
= gsi_stmt (gsi
);
350 if (is_gimple_debug (stmt
))
353 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
355 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
357 decl
= gimple_call_fndecl (call_stmt
);
359 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
, bb
->count
, freq
);
360 else if (gimple_call_internal_p (call_stmt
))
363 node
->create_indirect_edge (call_stmt
,
364 gimple_call_flags (call_stmt
),
367 node
->record_stmt_references (stmt
);
368 if (gomp_parallel
*omp_par_stmt
= dyn_cast
<gomp_parallel
*> (stmt
))
370 tree fn
= gimple_omp_parallel_child_fn (omp_par_stmt
);
371 node
->create_reference (cgraph_node::get_create (fn
),
374 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
376 tree fn
= gimple_omp_task_child_fn (stmt
);
378 node
->create_reference (cgraph_node::get_create (fn
),
380 fn
= gimple_omp_task_copy_fn (stmt
);
382 node
->create_reference (cgraph_node::get_create (fn
),
386 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
387 node
->record_stmt_references (gsi_stmt (gsi
));
390 /* Look for initializers of constant variables and private statics. */
391 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
392 if (TREE_CODE (decl
) == VAR_DECL
393 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
394 && !DECL_HAS_VALUE_EXPR_P (decl
))
395 varpool_node::finalize_decl (decl
);
396 record_eh_tables (node
, fun
);
404 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
406 return new pass_build_cgraph_edges (ctxt
);
409 /* Record references to functions and other variables present in the
410 initial value of DECL, a variable.
411 When ONLY_VARS is true, we mark needed only variables, not functions. */
414 record_references_in_initializer (tree decl
, bool only_vars
)
416 varpool_node
*node
= varpool_node::get_create (decl
);
417 hash_set
<tree
> visited_nodes
;
418 record_reference_ctx ctx
= {false, NULL
};
420 ctx
.varpool_node
= node
;
421 ctx
.only_vars
= only_vars
;
422 walk_tree (&DECL_INITIAL (decl
), record_reference
,
423 &ctx
, &visited_nodes
);
426 /* Rebuild cgraph edges for current function node. This needs to be run after
427 passes that don't update the cgraph. */
430 cgraph_edge::rebuild_edges (void)
433 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
434 gimple_stmt_iterator gsi
;
436 node
->remove_callees ();
437 node
->remove_all_references ();
439 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
441 FOR_EACH_BB_FN (bb
, cfun
)
443 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
445 gimple stmt
= gsi_stmt (gsi
);
448 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
450 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
452 decl
= gimple_call_fndecl (call_stmt
);
454 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
456 else if (gimple_call_internal_p (call_stmt
))
459 node
->create_indirect_edge (call_stmt
,
460 gimple_call_flags (call_stmt
),
463 node
->record_stmt_references (stmt
);
465 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
466 node
->record_stmt_references (gsi_stmt (gsi
));
468 record_eh_tables (node
, cfun
);
469 gcc_assert (!node
->global
.inlined_to
);
471 if (node
->instrumented_version
472 && !node
->instrumentation_clone
)
473 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
478 /* Rebuild cgraph references for current function node. This needs to be run
479 after passes that don't update the cgraph. */
482 cgraph_edge::rebuild_references (void)
485 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
486 gimple_stmt_iterator gsi
;
490 /* Keep speculative references for further cgraph edge expansion. */
491 for (i
= 0; node
->iterate_reference (i
, ref
);)
492 if (!ref
->speculative
)
493 ref
->remove_reference ();
497 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
499 FOR_EACH_BB_FN (bb
, cfun
)
501 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
502 node
->record_stmt_references (gsi_stmt (gsi
));
503 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
504 node
->record_stmt_references (gsi_stmt (gsi
));
506 record_eh_tables (node
, cfun
);
508 if (node
->instrumented_version
509 && !node
->instrumentation_clone
)
510 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
515 const pass_data pass_data_rebuild_cgraph_edges
=
517 GIMPLE_PASS
, /* type */
518 "*rebuild_cgraph_edges", /* name */
519 OPTGROUP_NONE
, /* optinfo_flags */
520 TV_CGRAPH
, /* tv_id */
521 PROP_cfg
, /* properties_required */
522 0, /* properties_provided */
523 0, /* properties_destroyed */
524 0, /* todo_flags_start */
525 0, /* todo_flags_finish */
528 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
531 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
532 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
535 /* opt_pass methods: */
536 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
537 virtual unsigned int execute (function
*)
539 return cgraph_edge::rebuild_edges ();
542 }; // class pass_rebuild_cgraph_edges
547 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
549 return new pass_rebuild_cgraph_edges (ctxt
);
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 */
561 0, /* properties_required */
562 0, /* properties_provided */
563 0, /* properties_destroyed */
564 0, /* todo_flags_start */
565 0, /* todo_flags_finish */
568 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
571 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
572 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
575 /* opt_pass methods: */
576 opt_pass
* clone () {
577 return new pass_remove_cgraph_callee_edges (m_ctxt
);
579 virtual unsigned int execute (function
*);
581 }; // class pass_remove_cgraph_callee_edges
584 pass_remove_cgraph_callee_edges::execute (function
*)
586 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
587 node
->remove_callees ();
588 node
->remove_all_references ();
595 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
597 return new pass_remove_cgraph_callee_edges (ctxt
);