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 "double-int.h"
35 #include "fold-const.h"
37 #include "hard-reg-set.h"
40 #include "dominance.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-fold.h"
46 #include "gimple-expr.h"
49 #include "gimple-iterator.h"
50 #include "gimple-walk.h"
51 #include "langhooks.h"
53 #include "tree-pass.h"
55 #include "plugin-api.h"
58 #include "ipa-utils.h"
60 #include "alloc-pool.h"
61 #include "symbol-summary.h"
63 #include "ipa-inline.h"
65 /* Context of record_reference. */
66 struct record_reference_ctx
69 class varpool_node
*varpool_node
;
72 /* Walk tree and record all calls and references to functions/variables.
73 Called via walk_tree: TP is pointer to tree to be examined.
74 When DATA is non-null, record references to callgraph.
78 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
82 record_reference_ctx
*ctx
= (record_reference_ctx
*)data
;
84 t
= canonicalize_constructor_val (t
, NULL
);
90 switch (TREE_CODE (t
))
99 /* Record dereferences to the functions. This makes the
100 functions reachable unconditionally. */
101 decl
= get_base_var (*tp
);
102 if (TREE_CODE (decl
) == FUNCTION_DECL
)
104 cgraph_node
*node
= cgraph_node::get_create (decl
);
106 node
->mark_address_taken ();
107 ctx
->varpool_node
->create_reference (node
, IPA_REF_ADDR
);
110 if (TREE_CODE (decl
) == VAR_DECL
)
112 varpool_node
*vnode
= varpool_node::get_create (decl
);
113 ctx
->varpool_node
->create_reference (vnode
, IPA_REF_ADDR
);
119 /* Save some cycles by not walking types and declaration as we
120 won't find anything useful there anyway. */
121 if (IS_TYPE_OR_DECL_P (*tp
))
132 /* Record references to typeinfos in the type list LIST. */
135 record_type_list (cgraph_node
*node
, tree list
)
137 for (; list
; list
= TREE_CHAIN (list
))
139 tree type
= TREE_VALUE (list
);
142 type
= lookup_type_for_runtime (type
);
144 if (TREE_CODE (type
) == ADDR_EXPR
)
146 type
= TREE_OPERAND (type
, 0);
147 if (TREE_CODE (type
) == VAR_DECL
)
149 varpool_node
*vnode
= varpool_node::get_create (type
);
150 node
->create_reference (vnode
, IPA_REF_ADDR
);
156 /* Record all references we will introduce by producing EH tables
160 record_eh_tables (cgraph_node
*node
, function
*fun
)
164 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
166 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
167 cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
169 node
->create_reference (per_node
, IPA_REF_ADDR
);
170 per_node
->mark_address_taken ();
173 i
= fun
->eh
->region_tree
;
182 case ERT_MUST_NOT_THROW
:
188 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
189 record_type_list (node
, c
->type_list
);
193 case ERT_ALLOWED_EXCEPTIONS
:
194 record_type_list (node
, i
->u
.allowed
.type_list
);
197 /* If there are sub-regions, process them. */
200 /* If there are peers, process them. */
201 else if (i
->next_peer
)
203 /* Otherwise, step back up the tree to the next peer. */
212 while (i
->next_peer
== NULL
);
218 /* Computes the frequency of the call statement so that it can be stored in
219 cgraph_edge. BB is the basic block of the call statement. */
221 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
223 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FN
224 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
225 int freq
= bb
->frequency
;
227 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
228 return CGRAPH_FREQ_BASE
;
231 entry_freq
= 1, freq
++;
233 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
234 if (freq
> CGRAPH_FREQ_MAX
)
235 freq
= CGRAPH_FREQ_MAX
;
240 /* Mark address taken in STMT. */
243 mark_address (gimple stmt
, tree addr
, tree
, void *data
)
245 addr
= get_base_address (addr
);
246 if (TREE_CODE (addr
) == FUNCTION_DECL
)
248 cgraph_node
*node
= cgraph_node::get_create (addr
);
249 node
->mark_address_taken ();
250 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
252 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
253 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
255 varpool_node
*vnode
= varpool_node::get_create (addr
);
257 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_ADDR
, stmt
);
263 /* Mark load of T. */
266 mark_load (gimple stmt
, tree t
, tree
, void *data
)
268 t
= get_base_address (t
);
269 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
271 /* ??? This can happen on platforms with descriptors when these are
272 directly manipulated in the code. Pretend that it's an address. */
273 cgraph_node
*node
= cgraph_node::get_create (t
);
274 node
->mark_address_taken ();
275 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
277 else if (t
&& TREE_CODE (t
) == VAR_DECL
278 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
280 varpool_node
*vnode
= varpool_node::get_create (t
);
282 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_LOAD
, stmt
);
287 /* Mark store of T. */
290 mark_store (gimple stmt
, tree t
, tree
, void *data
)
292 t
= get_base_address (t
);
293 if (t
&& TREE_CODE (t
) == VAR_DECL
294 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
296 varpool_node
*vnode
= varpool_node::get_create (t
);
298 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_STORE
, stmt
);
303 /* Record all references from cgraph_node that are taken in statement STMT. */
306 cgraph_node::record_stmt_references (gimple stmt
)
308 walk_stmt_load_store_addr_ops (stmt
, this, mark_load
, mark_store
,
312 /* Create cgraph edges for function calls.
313 Also look for functions and variables having addresses taken. */
317 const pass_data pass_data_build_cgraph_edges
=
319 GIMPLE_PASS
, /* type */
320 "*build_cgraph_edges", /* name */
321 OPTGROUP_NONE
, /* optinfo_flags */
323 PROP_cfg
, /* properties_required */
324 0, /* properties_provided */
325 0, /* properties_destroyed */
326 0, /* todo_flags_start */
327 0, /* todo_flags_finish */
330 class pass_build_cgraph_edges
: public gimple_opt_pass
333 pass_build_cgraph_edges (gcc::context
*ctxt
)
334 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
337 /* opt_pass methods: */
338 virtual unsigned int execute (function
*);
340 }; // class pass_build_cgraph_edges
343 pass_build_cgraph_edges::execute (function
*fun
)
346 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
347 gimple_stmt_iterator gsi
;
351 /* Create the callgraph edges and record the nodes referenced by the function.
353 FOR_EACH_BB_FN (bb
, fun
)
355 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
357 gimple stmt
= gsi_stmt (gsi
);
360 if (is_gimple_debug (stmt
))
363 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
365 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
367 decl
= gimple_call_fndecl (call_stmt
);
369 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
, bb
->count
, freq
);
370 else if (gimple_call_internal_p (call_stmt
))
373 node
->create_indirect_edge (call_stmt
,
374 gimple_call_flags (call_stmt
),
377 node
->record_stmt_references (stmt
);
378 if (gomp_parallel
*omp_par_stmt
= dyn_cast
<gomp_parallel
*> (stmt
))
380 tree fn
= gimple_omp_parallel_child_fn (omp_par_stmt
);
381 node
->create_reference (cgraph_node::get_create (fn
),
384 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
386 tree fn
= gimple_omp_task_child_fn (stmt
);
388 node
->create_reference (cgraph_node::get_create (fn
),
390 fn
= gimple_omp_task_copy_fn (stmt
);
392 node
->create_reference (cgraph_node::get_create (fn
),
396 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
397 node
->record_stmt_references (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_node::finalize_decl (decl
);
406 record_eh_tables (node
, fun
);
414 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
416 return new pass_build_cgraph_edges (ctxt
);
419 /* Record references to functions and other variables present in the
420 initial value of DECL, a variable.
421 When ONLY_VARS is true, we mark needed only variables, not functions. */
424 record_references_in_initializer (tree decl
, bool only_vars
)
426 varpool_node
*node
= varpool_node::get_create (decl
);
427 hash_set
<tree
> visited_nodes
;
428 record_reference_ctx ctx
= {false, NULL
};
430 ctx
.varpool_node
= node
;
431 ctx
.only_vars
= only_vars
;
432 walk_tree (&DECL_INITIAL (decl
), record_reference
,
433 &ctx
, &visited_nodes
);
436 /* Rebuild cgraph edges for current function node. This needs to be run after
437 passes that don't update the cgraph. */
440 cgraph_edge::rebuild_edges (void)
443 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
444 gimple_stmt_iterator gsi
;
446 node
->remove_callees ();
447 node
->remove_all_references ();
449 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
451 FOR_EACH_BB_FN (bb
, cfun
)
453 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
455 gimple stmt
= gsi_stmt (gsi
);
458 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
460 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
462 decl
= gimple_call_fndecl (call_stmt
);
464 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
466 else if (gimple_call_internal_p (call_stmt
))
469 node
->create_indirect_edge (call_stmt
,
470 gimple_call_flags (call_stmt
),
473 node
->record_stmt_references (stmt
);
475 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
476 node
->record_stmt_references (gsi_stmt (gsi
));
478 record_eh_tables (node
, cfun
);
479 gcc_assert (!node
->global
.inlined_to
);
481 if (node
->instrumented_version
482 && !node
->instrumentation_clone
)
483 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
488 /* Rebuild cgraph references for current function node. This needs to be run
489 after passes that don't update the cgraph. */
492 cgraph_edge::rebuild_references (void)
495 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
496 gimple_stmt_iterator gsi
;
500 /* Keep speculative references for further cgraph edge expansion. */
501 for (i
= 0; node
->iterate_reference (i
, ref
);)
502 if (!ref
->speculative
)
503 ref
->remove_reference ();
507 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
509 FOR_EACH_BB_FN (bb
, cfun
)
511 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
512 node
->record_stmt_references (gsi_stmt (gsi
));
513 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
514 node
->record_stmt_references (gsi_stmt (gsi
));
516 record_eh_tables (node
, cfun
);
518 if (node
->instrumented_version
519 && !node
->instrumentation_clone
)
520 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
525 const pass_data pass_data_rebuild_cgraph_edges
=
527 GIMPLE_PASS
, /* type */
528 "*rebuild_cgraph_edges", /* name */
529 OPTGROUP_NONE
, /* optinfo_flags */
530 TV_CGRAPH
, /* tv_id */
531 PROP_cfg
, /* properties_required */
532 0, /* properties_provided */
533 0, /* properties_destroyed */
534 0, /* todo_flags_start */
535 0, /* todo_flags_finish */
538 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
541 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
542 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
545 /* opt_pass methods: */
546 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
547 virtual unsigned int execute (function
*)
549 return cgraph_edge::rebuild_edges ();
552 }; // class pass_rebuild_cgraph_edges
557 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
559 return new pass_rebuild_cgraph_edges (ctxt
);
565 const pass_data pass_data_remove_cgraph_callee_edges
=
567 GIMPLE_PASS
, /* type */
568 "*remove_cgraph_callee_edges", /* name */
569 OPTGROUP_NONE
, /* optinfo_flags */
571 0, /* properties_required */
572 0, /* properties_provided */
573 0, /* properties_destroyed */
574 0, /* todo_flags_start */
575 0, /* todo_flags_finish */
578 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
581 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
582 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
585 /* opt_pass methods: */
586 opt_pass
* clone () {
587 return new pass_remove_cgraph_callee_edges (m_ctxt
);
589 virtual unsigned int execute (function
*);
591 }; // class pass_remove_cgraph_callee_edges
594 pass_remove_cgraph_callee_edges::execute (function
*)
596 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
597 node
->remove_callees ();
598 node
->remove_all_references ();
605 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
607 return new pass_remove_cgraph_callee_edges (ctxt
);