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"
27 #include "hard-reg-set.h"
29 #include "fold-const.h"
30 #include "internal-fn.h"
31 #include "gimple-fold.h"
32 #include "gimple-iterator.h"
33 #include "gimple-walk.h"
34 #include "langhooks.h"
36 #include "tree-pass.h"
38 #include "ipa-utils.h"
40 #include "alloc-pool.h"
41 #include "symbol-summary.h"
43 #include "ipa-inline.h"
45 /* Context of record_reference. */
46 struct record_reference_ctx
49 class varpool_node
*varpool_node
;
52 /* Walk tree and record all calls and references to functions/variables.
53 Called via walk_tree: TP is pointer to tree to be examined.
54 When DATA is non-null, record references to callgraph.
58 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
62 record_reference_ctx
*ctx
= (record_reference_ctx
*)data
;
64 t
= canonicalize_constructor_val (t
, NULL
);
70 switch (TREE_CODE (t
))
79 /* Record dereferences to the functions. This makes the
80 functions reachable unconditionally. */
81 decl
= get_base_var (*tp
);
82 if (TREE_CODE (decl
) == FUNCTION_DECL
)
84 cgraph_node
*node
= cgraph_node::get_create (decl
);
86 node
->mark_address_taken ();
87 ctx
->varpool_node
->create_reference (node
, IPA_REF_ADDR
);
90 if (TREE_CODE (decl
) == VAR_DECL
)
92 varpool_node
*vnode
= varpool_node::get_create (decl
);
93 ctx
->varpool_node
->create_reference (vnode
, IPA_REF_ADDR
);
99 /* Save some cycles by not walking types and declaration as we
100 won't find anything useful there anyway. */
101 if (IS_TYPE_OR_DECL_P (*tp
))
112 /* Record references to typeinfos in the type list LIST. */
115 record_type_list (cgraph_node
*node
, tree list
)
117 for (; list
; list
= TREE_CHAIN (list
))
119 tree type
= TREE_VALUE (list
);
122 type
= lookup_type_for_runtime (type
);
124 if (TREE_CODE (type
) == ADDR_EXPR
)
126 type
= TREE_OPERAND (type
, 0);
127 if (TREE_CODE (type
) == VAR_DECL
)
129 varpool_node
*vnode
= varpool_node::get_create (type
);
130 node
->create_reference (vnode
, IPA_REF_ADDR
);
136 /* Record all references we will introduce by producing EH tables
140 record_eh_tables (cgraph_node
*node
, function
*fun
)
144 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
146 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
147 cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
149 node
->create_reference (per_node
, IPA_REF_ADDR
);
150 per_node
->mark_address_taken ();
153 i
= fun
->eh
->region_tree
;
162 case ERT_MUST_NOT_THROW
:
168 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
169 record_type_list (node
, c
->type_list
);
173 case ERT_ALLOWED_EXCEPTIONS
:
174 record_type_list (node
, i
->u
.allowed
.type_list
);
177 /* If there are sub-regions, process them. */
180 /* If there are peers, process them. */
181 else if (i
->next_peer
)
183 /* Otherwise, step back up the tree to the next peer. */
192 while (i
->next_peer
== NULL
);
198 /* Computes the frequency of the call statement so that it can be stored in
199 cgraph_edge. BB is the basic block of the call statement. */
201 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
203 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FN
204 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
205 int freq
= bb
->frequency
;
207 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
208 return CGRAPH_FREQ_BASE
;
211 entry_freq
= 1, freq
++;
213 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
214 if (freq
> CGRAPH_FREQ_MAX
)
215 freq
= CGRAPH_FREQ_MAX
;
220 /* Mark address taken in STMT. */
223 mark_address (gimple stmt
, tree addr
, tree
, void *data
)
225 addr
= get_base_address (addr
);
226 if (TREE_CODE (addr
) == FUNCTION_DECL
)
228 cgraph_node
*node
= cgraph_node::get_create (addr
);
229 node
->mark_address_taken ();
230 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
232 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
233 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
235 varpool_node
*vnode
= varpool_node::get_create (addr
);
237 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_ADDR
, stmt
);
243 /* Mark load of T. */
246 mark_load (gimple stmt
, tree t
, tree
, void *data
)
248 t
= get_base_address (t
);
249 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
251 /* ??? This can happen on platforms with descriptors when these are
252 directly manipulated in the code. Pretend that it's an address. */
253 cgraph_node
*node
= cgraph_node::get_create (t
);
254 node
->mark_address_taken ();
255 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
257 else if (t
&& TREE_CODE (t
) == VAR_DECL
258 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
260 varpool_node
*vnode
= varpool_node::get_create (t
);
262 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_LOAD
, stmt
);
267 /* Mark store of T. */
270 mark_store (gimple stmt
, tree t
, tree
, void *data
)
272 t
= get_base_address (t
);
273 if (t
&& TREE_CODE (t
) == VAR_DECL
274 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
276 varpool_node
*vnode
= varpool_node::get_create (t
);
278 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_STORE
, stmt
);
283 /* Record all references from cgraph_node that are taken in statement STMT. */
286 cgraph_node::record_stmt_references (gimple stmt
)
288 walk_stmt_load_store_addr_ops (stmt
, this, mark_load
, mark_store
,
292 /* Create cgraph edges for function calls.
293 Also look for functions and variables having addresses taken. */
297 const pass_data pass_data_build_cgraph_edges
=
299 GIMPLE_PASS
, /* type */
300 "*build_cgraph_edges", /* name */
301 OPTGROUP_NONE
, /* optinfo_flags */
303 PROP_cfg
, /* properties_required */
304 0, /* properties_provided */
305 0, /* properties_destroyed */
306 0, /* todo_flags_start */
307 0, /* todo_flags_finish */
310 class pass_build_cgraph_edges
: public gimple_opt_pass
313 pass_build_cgraph_edges (gcc::context
*ctxt
)
314 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
317 /* opt_pass methods: */
318 virtual unsigned int execute (function
*);
320 }; // class pass_build_cgraph_edges
323 pass_build_cgraph_edges::execute (function
*fun
)
326 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
327 gimple_stmt_iterator gsi
;
331 /* Create the callgraph edges and record the nodes referenced by the function.
333 FOR_EACH_BB_FN (bb
, fun
)
335 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
337 gimple stmt
= gsi_stmt (gsi
);
340 if (is_gimple_debug (stmt
))
343 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
345 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
347 decl
= gimple_call_fndecl (call_stmt
);
349 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
, bb
->count
, freq
);
350 else if (gimple_call_internal_p (call_stmt
))
353 node
->create_indirect_edge (call_stmt
,
354 gimple_call_flags (call_stmt
),
357 node
->record_stmt_references (stmt
);
358 if (gomp_parallel
*omp_par_stmt
= dyn_cast
<gomp_parallel
*> (stmt
))
360 tree fn
= gimple_omp_parallel_child_fn (omp_par_stmt
);
361 node
->create_reference (cgraph_node::get_create (fn
),
364 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
366 tree fn
= gimple_omp_task_child_fn (stmt
);
368 node
->create_reference (cgraph_node::get_create (fn
),
370 fn
= gimple_omp_task_copy_fn (stmt
);
372 node
->create_reference (cgraph_node::get_create (fn
),
376 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
377 node
->record_stmt_references (gsi_stmt (gsi
));
380 /* Look for initializers of constant variables and private statics. */
381 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
382 if (TREE_CODE (decl
) == VAR_DECL
383 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
384 && !DECL_HAS_VALUE_EXPR_P (decl
))
385 varpool_node::finalize_decl (decl
);
386 record_eh_tables (node
, fun
);
394 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
396 return new pass_build_cgraph_edges (ctxt
);
399 /* Record references to functions and other variables present in the
400 initial value of DECL, a variable.
401 When ONLY_VARS is true, we mark needed only variables, not functions. */
404 record_references_in_initializer (tree decl
, bool only_vars
)
406 varpool_node
*node
= varpool_node::get_create (decl
);
407 hash_set
<tree
> visited_nodes
;
408 record_reference_ctx ctx
= {false, NULL
};
410 ctx
.varpool_node
= node
;
411 ctx
.only_vars
= only_vars
;
412 walk_tree (&DECL_INITIAL (decl
), record_reference
,
413 &ctx
, &visited_nodes
);
416 /* Rebuild cgraph edges for current function node. This needs to be run after
417 passes that don't update the cgraph. */
420 cgraph_edge::rebuild_edges (void)
423 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
424 gimple_stmt_iterator gsi
;
426 node
->remove_callees ();
427 node
->remove_all_references ();
429 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
431 FOR_EACH_BB_FN (bb
, cfun
)
433 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
435 gimple stmt
= gsi_stmt (gsi
);
438 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
440 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
442 decl
= gimple_call_fndecl (call_stmt
);
444 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
446 else if (gimple_call_internal_p (call_stmt
))
449 node
->create_indirect_edge (call_stmt
,
450 gimple_call_flags (call_stmt
),
453 node
->record_stmt_references (stmt
);
455 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
456 node
->record_stmt_references (gsi_stmt (gsi
));
458 record_eh_tables (node
, cfun
);
459 gcc_assert (!node
->global
.inlined_to
);
461 if (node
->instrumented_version
462 && !node
->instrumentation_clone
)
463 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
468 /* Rebuild cgraph references for current function node. This needs to be run
469 after passes that don't update the cgraph. */
472 cgraph_edge::rebuild_references (void)
475 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
476 gimple_stmt_iterator gsi
;
480 /* Keep speculative references for further cgraph edge expansion. */
481 for (i
= 0; node
->iterate_reference (i
, ref
);)
482 if (!ref
->speculative
)
483 ref
->remove_reference ();
487 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
489 FOR_EACH_BB_FN (bb
, cfun
)
491 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
492 node
->record_stmt_references (gsi_stmt (gsi
));
493 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
494 node
->record_stmt_references (gsi_stmt (gsi
));
496 record_eh_tables (node
, cfun
);
498 if (node
->instrumented_version
499 && !node
->instrumentation_clone
)
500 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
505 const pass_data pass_data_rebuild_cgraph_edges
=
507 GIMPLE_PASS
, /* type */
508 "*rebuild_cgraph_edges", /* name */
509 OPTGROUP_NONE
, /* optinfo_flags */
510 TV_CGRAPH
, /* tv_id */
511 PROP_cfg
, /* properties_required */
512 0, /* properties_provided */
513 0, /* properties_destroyed */
514 0, /* todo_flags_start */
515 0, /* todo_flags_finish */
518 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
521 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
522 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
525 /* opt_pass methods: */
526 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
527 virtual unsigned int execute (function
*)
529 return cgraph_edge::rebuild_edges ();
532 }; // class pass_rebuild_cgraph_edges
537 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
539 return new pass_rebuild_cgraph_edges (ctxt
);
545 const pass_data pass_data_remove_cgraph_callee_edges
=
547 GIMPLE_PASS
, /* type */
548 "*remove_cgraph_callee_edges", /* name */
549 OPTGROUP_NONE
, /* optinfo_flags */
551 0, /* properties_required */
552 0, /* properties_provided */
553 0, /* properties_destroyed */
554 0, /* todo_flags_start */
555 0, /* todo_flags_finish */
558 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
561 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
562 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
565 /* opt_pass methods: */
566 opt_pass
* clone () {
567 return new pass_remove_cgraph_callee_edges (m_ctxt
);
569 virtual unsigned int execute (function
*);
571 }; // class pass_remove_cgraph_callee_edges
574 pass_remove_cgraph_callee_edges::execute (function
*)
576 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
577 node
->remove_callees ();
578 node
->remove_all_references ();
585 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
587 return new pass_remove_cgraph_callee_edges (ctxt
);