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"
25 #include "hard-reg-set.h"
28 #include "alloc-pool.h"
29 #include "tree-pass.h"
32 #include "fold-const.h"
33 #include "internal-fn.h"
34 #include "gimple-fold.h"
35 #include "gimple-iterator.h"
36 #include "gimple-walk.h"
37 #include "langhooks.h"
39 #include "ipa-utils.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 && TREE_TYPE (decl
) != error_mark_node
)
386 varpool_node::finalize_decl (decl
);
387 record_eh_tables (node
, fun
);
395 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
397 return new pass_build_cgraph_edges (ctxt
);
400 /* Record references to functions and other variables present in the
401 initial value of DECL, a variable.
402 When ONLY_VARS is true, we mark needed only variables, not functions. */
405 record_references_in_initializer (tree decl
, bool only_vars
)
407 varpool_node
*node
= varpool_node::get_create (decl
);
408 hash_set
<tree
> visited_nodes
;
409 record_reference_ctx ctx
= {false, NULL
};
411 ctx
.varpool_node
= node
;
412 ctx
.only_vars
= only_vars
;
413 walk_tree (&DECL_INITIAL (decl
), record_reference
,
414 &ctx
, &visited_nodes
);
417 /* Rebuild cgraph edges for current function node. This needs to be run after
418 passes that don't update the cgraph. */
421 cgraph_edge::rebuild_edges (void)
424 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
425 gimple_stmt_iterator gsi
;
427 node
->remove_callees ();
428 node
->remove_all_references ();
430 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
432 FOR_EACH_BB_FN (bb
, cfun
)
434 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
436 gimple
*stmt
= gsi_stmt (gsi
);
439 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
441 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
443 decl
= gimple_call_fndecl (call_stmt
);
445 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
447 else if (gimple_call_internal_p (call_stmt
))
450 node
->create_indirect_edge (call_stmt
,
451 gimple_call_flags (call_stmt
),
454 node
->record_stmt_references (stmt
);
456 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
457 node
->record_stmt_references (gsi_stmt (gsi
));
459 record_eh_tables (node
, cfun
);
460 gcc_assert (!node
->global
.inlined_to
);
462 if (node
->instrumented_version
463 && !node
->instrumentation_clone
)
464 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
469 /* Rebuild cgraph references for current function node. This needs to be run
470 after passes that don't update the cgraph. */
473 cgraph_edge::rebuild_references (void)
476 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
477 gimple_stmt_iterator gsi
;
481 /* Keep speculative references for further cgraph edge expansion. */
482 for (i
= 0; node
->iterate_reference (i
, ref
);)
483 if (!ref
->speculative
)
484 ref
->remove_reference ();
488 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
490 FOR_EACH_BB_FN (bb
, cfun
)
492 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
493 node
->record_stmt_references (gsi_stmt (gsi
));
494 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
495 node
->record_stmt_references (gsi_stmt (gsi
));
497 record_eh_tables (node
, cfun
);
499 if (node
->instrumented_version
500 && !node
->instrumentation_clone
)
501 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
506 const pass_data pass_data_rebuild_cgraph_edges
=
508 GIMPLE_PASS
, /* type */
509 "*rebuild_cgraph_edges", /* name */
510 OPTGROUP_NONE
, /* optinfo_flags */
511 TV_CGRAPH
, /* tv_id */
512 PROP_cfg
, /* properties_required */
513 0, /* properties_provided */
514 0, /* properties_destroyed */
515 0, /* todo_flags_start */
516 0, /* todo_flags_finish */
519 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
522 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
523 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
526 /* opt_pass methods: */
527 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
528 virtual unsigned int execute (function
*)
530 return cgraph_edge::rebuild_edges ();
533 }; // class pass_rebuild_cgraph_edges
538 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
540 return new pass_rebuild_cgraph_edges (ctxt
);
546 const pass_data pass_data_remove_cgraph_callee_edges
=
548 GIMPLE_PASS
, /* type */
549 "*remove_cgraph_callee_edges", /* name */
550 OPTGROUP_NONE
, /* optinfo_flags */
552 0, /* properties_required */
553 0, /* properties_provided */
554 0, /* properties_destroyed */
555 0, /* todo_flags_start */
556 0, /* todo_flags_finish */
559 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
562 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
563 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
566 /* opt_pass methods: */
567 opt_pass
* clone () {
568 return new pass_remove_cgraph_callee_edges (m_ctxt
);
570 virtual unsigned int execute (function
*);
572 }; // class pass_remove_cgraph_callee_edges
575 pass_remove_cgraph_callee_edges::execute (function
*)
577 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
578 node
->remove_callees ();
579 node
->remove_all_references ();
586 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
588 return new pass_remove_cgraph_callee_edges (ctxt
);