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"
29 #include "fold-const.h"
31 #include "hard-reg-set.h"
34 #include "dominance.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-fold.h"
40 #include "gimple-expr.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "langhooks.h"
47 #include "tree-pass.h"
48 #include "plugin-api.h"
51 #include "ipa-utils.h"
53 #include "alloc-pool.h"
54 #include "symbol-summary.h"
56 #include "ipa-inline.h"
58 /* Context of record_reference. */
59 struct record_reference_ctx
62 class varpool_node
*varpool_node
;
65 /* Walk tree and record all calls and references to functions/variables.
66 Called via walk_tree: TP is pointer to tree to be examined.
67 When DATA is non-null, record references to callgraph.
71 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
75 record_reference_ctx
*ctx
= (record_reference_ctx
*)data
;
77 t
= canonicalize_constructor_val (t
, NULL
);
83 switch (TREE_CODE (t
))
92 /* Record dereferences to the functions. This makes the
93 functions reachable unconditionally. */
94 decl
= get_base_var (*tp
);
95 if (TREE_CODE (decl
) == FUNCTION_DECL
)
97 cgraph_node
*node
= cgraph_node::get_create (decl
);
99 node
->mark_address_taken ();
100 ctx
->varpool_node
->create_reference (node
, IPA_REF_ADDR
);
103 if (TREE_CODE (decl
) == VAR_DECL
)
105 varpool_node
*vnode
= varpool_node::get_create (decl
);
106 ctx
->varpool_node
->create_reference (vnode
, IPA_REF_ADDR
);
112 /* Save some cycles by not walking types and declaration as we
113 won't find anything useful there anyway. */
114 if (IS_TYPE_OR_DECL_P (*tp
))
125 /* Record references to typeinfos in the type list LIST. */
128 record_type_list (cgraph_node
*node
, tree list
)
130 for (; list
; list
= TREE_CHAIN (list
))
132 tree type
= TREE_VALUE (list
);
135 type
= lookup_type_for_runtime (type
);
137 if (TREE_CODE (type
) == ADDR_EXPR
)
139 type
= TREE_OPERAND (type
, 0);
140 if (TREE_CODE (type
) == VAR_DECL
)
142 varpool_node
*vnode
= varpool_node::get_create (type
);
143 node
->create_reference (vnode
, IPA_REF_ADDR
);
149 /* Record all references we will introduce by producing EH tables
153 record_eh_tables (cgraph_node
*node
, function
*fun
)
157 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
159 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
160 cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
162 node
->create_reference (per_node
, IPA_REF_ADDR
);
163 per_node
->mark_address_taken ();
166 i
= fun
->eh
->region_tree
;
175 case ERT_MUST_NOT_THROW
:
181 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
182 record_type_list (node
, c
->type_list
);
186 case ERT_ALLOWED_EXCEPTIONS
:
187 record_type_list (node
, i
->u
.allowed
.type_list
);
190 /* If there are sub-regions, process them. */
193 /* If there are peers, process them. */
194 else if (i
->next_peer
)
196 /* Otherwise, step back up the tree to the next peer. */
205 while (i
->next_peer
== NULL
);
211 /* Computes the frequency of the call statement so that it can be stored in
212 cgraph_edge. BB is the basic block of the call statement. */
214 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
216 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FN
217 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
218 int freq
= bb
->frequency
;
220 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
221 return CGRAPH_FREQ_BASE
;
224 entry_freq
= 1, freq
++;
226 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
227 if (freq
> CGRAPH_FREQ_MAX
)
228 freq
= CGRAPH_FREQ_MAX
;
233 /* Mark address taken in STMT. */
236 mark_address (gimple stmt
, tree addr
, tree
, void *data
)
238 addr
= get_base_address (addr
);
239 if (TREE_CODE (addr
) == FUNCTION_DECL
)
241 cgraph_node
*node
= cgraph_node::get_create (addr
);
242 node
->mark_address_taken ();
243 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
245 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
246 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
248 varpool_node
*vnode
= varpool_node::get_create (addr
);
250 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_ADDR
, stmt
);
256 /* Mark load of T. */
259 mark_load (gimple stmt
, tree t
, tree
, void *data
)
261 t
= get_base_address (t
);
262 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
264 /* ??? This can happen on platforms with descriptors when these are
265 directly manipulated in the code. Pretend that it's an address. */
266 cgraph_node
*node
= cgraph_node::get_create (t
);
267 node
->mark_address_taken ();
268 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
270 else if (t
&& TREE_CODE (t
) == VAR_DECL
271 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
273 varpool_node
*vnode
= varpool_node::get_create (t
);
275 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_LOAD
, stmt
);
280 /* Mark store of T. */
283 mark_store (gimple stmt
, tree t
, tree
, void *data
)
285 t
= get_base_address (t
);
286 if (t
&& TREE_CODE (t
) == VAR_DECL
287 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
289 varpool_node
*vnode
= varpool_node::get_create (t
);
291 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_STORE
, stmt
);
296 /* Record all references from cgraph_node that are taken in statement STMT. */
299 cgraph_node::record_stmt_references (gimple stmt
)
301 walk_stmt_load_store_addr_ops (stmt
, this, 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 */
316 PROP_cfg
, /* properties_required */
317 0, /* properties_provided */
318 0, /* properties_destroyed */
319 0, /* todo_flags_start */
320 0, /* todo_flags_finish */
323 class pass_build_cgraph_edges
: public gimple_opt_pass
326 pass_build_cgraph_edges (gcc::context
*ctxt
)
327 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
330 /* opt_pass methods: */
331 virtual unsigned int execute (function
*);
333 }; // class pass_build_cgraph_edges
336 pass_build_cgraph_edges::execute (function
*fun
)
339 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
340 gimple_stmt_iterator gsi
;
344 /* Create the callgraph edges and record the nodes referenced by the function.
346 FOR_EACH_BB_FN (bb
, fun
)
348 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
350 gimple stmt
= gsi_stmt (gsi
);
353 if (is_gimple_debug (stmt
))
356 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
358 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
360 decl
= gimple_call_fndecl (call_stmt
);
362 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
, bb
->count
, freq
);
363 else if (gimple_call_internal_p (call_stmt
))
366 node
->create_indirect_edge (call_stmt
,
367 gimple_call_flags (call_stmt
),
370 node
->record_stmt_references (stmt
);
371 if (gomp_parallel
*omp_par_stmt
= dyn_cast
<gomp_parallel
*> (stmt
))
373 tree fn
= gimple_omp_parallel_child_fn (omp_par_stmt
);
374 node
->create_reference (cgraph_node::get_create (fn
),
377 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
379 tree fn
= gimple_omp_task_child_fn (stmt
);
381 node
->create_reference (cgraph_node::get_create (fn
),
383 fn
= gimple_omp_task_copy_fn (stmt
);
385 node
->create_reference (cgraph_node::get_create (fn
),
389 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
390 node
->record_stmt_references (gsi_stmt (gsi
));
393 /* Look for initializers of constant variables and private statics. */
394 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
395 if (TREE_CODE (decl
) == VAR_DECL
396 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
397 && !DECL_HAS_VALUE_EXPR_P (decl
))
398 varpool_node::finalize_decl (decl
);
399 record_eh_tables (node
, fun
);
407 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
409 return new pass_build_cgraph_edges (ctxt
);
412 /* Record references to functions and other variables present in the
413 initial value of DECL, a variable.
414 When ONLY_VARS is true, we mark needed only variables, not functions. */
417 record_references_in_initializer (tree decl
, bool only_vars
)
419 varpool_node
*node
= varpool_node::get_create (decl
);
420 hash_set
<tree
> visited_nodes
;
421 record_reference_ctx ctx
= {false, NULL
};
423 ctx
.varpool_node
= node
;
424 ctx
.only_vars
= only_vars
;
425 walk_tree (&DECL_INITIAL (decl
), record_reference
,
426 &ctx
, &visited_nodes
);
429 /* Rebuild cgraph edges for current function node. This needs to be run after
430 passes that don't update the cgraph. */
433 cgraph_edge::rebuild_edges (void)
436 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
437 gimple_stmt_iterator gsi
;
439 node
->remove_callees ();
440 node
->remove_all_references ();
442 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
444 FOR_EACH_BB_FN (bb
, cfun
)
446 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
448 gimple stmt
= gsi_stmt (gsi
);
451 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
453 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
455 decl
= gimple_call_fndecl (call_stmt
);
457 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
459 else if (gimple_call_internal_p (call_stmt
))
462 node
->create_indirect_edge (call_stmt
,
463 gimple_call_flags (call_stmt
),
466 node
->record_stmt_references (stmt
);
468 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
469 node
->record_stmt_references (gsi_stmt (gsi
));
471 record_eh_tables (node
, cfun
);
472 gcc_assert (!node
->global
.inlined_to
);
474 if (node
->instrumented_version
475 && !node
->instrumentation_clone
)
476 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
481 /* Rebuild cgraph references for current function node. This needs to be run
482 after passes that don't update the cgraph. */
485 cgraph_edge::rebuild_references (void)
488 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
489 gimple_stmt_iterator gsi
;
493 /* Keep speculative references for further cgraph edge expansion. */
494 for (i
= 0; node
->iterate_reference (i
, ref
);)
495 if (!ref
->speculative
)
496 ref
->remove_reference ();
500 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
502 FOR_EACH_BB_FN (bb
, cfun
)
504 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
505 node
->record_stmt_references (gsi_stmt (gsi
));
506 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
507 node
->record_stmt_references (gsi_stmt (gsi
));
509 record_eh_tables (node
, cfun
);
511 if (node
->instrumented_version
512 && !node
->instrumentation_clone
)
513 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
518 const pass_data pass_data_rebuild_cgraph_edges
=
520 GIMPLE_PASS
, /* type */
521 "*rebuild_cgraph_edges", /* name */
522 OPTGROUP_NONE
, /* optinfo_flags */
523 TV_CGRAPH
, /* tv_id */
524 PROP_cfg
, /* properties_required */
525 0, /* properties_provided */
526 0, /* properties_destroyed */
527 0, /* todo_flags_start */
528 0, /* todo_flags_finish */
531 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
534 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
535 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
538 /* opt_pass methods: */
539 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
540 virtual unsigned int execute (function
*)
542 return cgraph_edge::rebuild_edges ();
545 }; // class pass_rebuild_cgraph_edges
550 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
552 return new pass_rebuild_cgraph_edges (ctxt
);
558 const pass_data pass_data_remove_cgraph_callee_edges
=
560 GIMPLE_PASS
, /* type */
561 "*remove_cgraph_callee_edges", /* name */
562 OPTGROUP_NONE
, /* optinfo_flags */
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 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
590 node
->remove_callees ();
591 node
->remove_all_references ();
598 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
600 return new pass_remove_cgraph_callee_edges (ctxt
);