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"
46 #include "ipa-utils.h"
48 #include "alloc-pool.h"
49 #include "symbol-summary.h"
51 #include "ipa-inline.h"
53 /* Context of record_reference. */
54 struct record_reference_ctx
57 class varpool_node
*varpool_node
;
60 /* Walk tree and record all calls and references to functions/variables.
61 Called via walk_tree: TP is pointer to tree to be examined.
62 When DATA is non-null, record references to callgraph.
66 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
70 record_reference_ctx
*ctx
= (record_reference_ctx
*)data
;
72 t
= canonicalize_constructor_val (t
, NULL
);
78 switch (TREE_CODE (t
))
87 /* Record dereferences to the functions. This makes the
88 functions reachable unconditionally. */
89 decl
= get_base_var (*tp
);
90 if (TREE_CODE (decl
) == FUNCTION_DECL
)
92 cgraph_node
*node
= cgraph_node::get_create (decl
);
94 node
->mark_address_taken ();
95 ctx
->varpool_node
->create_reference (node
, IPA_REF_ADDR
);
98 if (TREE_CODE (decl
) == VAR_DECL
)
100 varpool_node
*vnode
= varpool_node::get_create (decl
);
101 ctx
->varpool_node
->create_reference (vnode
, IPA_REF_ADDR
);
107 /* Save some cycles by not walking types and declaration as we
108 won't find anything useful there anyway. */
109 if (IS_TYPE_OR_DECL_P (*tp
))
120 /* Record references to typeinfos in the type list LIST. */
123 record_type_list (cgraph_node
*node
, tree list
)
125 for (; list
; list
= TREE_CHAIN (list
))
127 tree type
= TREE_VALUE (list
);
130 type
= lookup_type_for_runtime (type
);
132 if (TREE_CODE (type
) == ADDR_EXPR
)
134 type
= TREE_OPERAND (type
, 0);
135 if (TREE_CODE (type
) == VAR_DECL
)
137 varpool_node
*vnode
= varpool_node::get_create (type
);
138 node
->create_reference (vnode
, IPA_REF_ADDR
);
144 /* Record all references we will introduce by producing EH tables
148 record_eh_tables (cgraph_node
*node
, function
*fun
)
152 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
154 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
155 cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
157 node
->create_reference (per_node
, IPA_REF_ADDR
);
158 per_node
->mark_address_taken ();
161 i
= fun
->eh
->region_tree
;
170 case ERT_MUST_NOT_THROW
:
176 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
177 record_type_list (node
, c
->type_list
);
181 case ERT_ALLOWED_EXCEPTIONS
:
182 record_type_list (node
, i
->u
.allowed
.type_list
);
185 /* If there are sub-regions, process them. */
188 /* If there are peers, process them. */
189 else if (i
->next_peer
)
191 /* Otherwise, step back up the tree to the next peer. */
200 while (i
->next_peer
== NULL
);
206 /* Computes the frequency of the call statement so that it can be stored in
207 cgraph_edge. BB is the basic block of the call statement. */
209 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
211 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FN
212 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
213 int freq
= bb
->frequency
;
215 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
216 return CGRAPH_FREQ_BASE
;
219 entry_freq
= 1, freq
++;
221 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
222 if (freq
> CGRAPH_FREQ_MAX
)
223 freq
= CGRAPH_FREQ_MAX
;
228 /* Mark address taken in STMT. */
231 mark_address (gimple stmt
, tree addr
, tree
, void *data
)
233 addr
= get_base_address (addr
);
234 if (TREE_CODE (addr
) == FUNCTION_DECL
)
236 cgraph_node
*node
= cgraph_node::get_create (addr
);
237 node
->mark_address_taken ();
238 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
240 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
241 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
243 varpool_node
*vnode
= varpool_node::get_create (addr
);
245 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_ADDR
, stmt
);
251 /* Mark load of T. */
254 mark_load (gimple stmt
, tree t
, tree
, void *data
)
256 t
= get_base_address (t
);
257 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
259 /* ??? This can happen on platforms with descriptors when these are
260 directly manipulated in the code. Pretend that it's an address. */
261 cgraph_node
*node
= cgraph_node::get_create (t
);
262 node
->mark_address_taken ();
263 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
265 else if (t
&& TREE_CODE (t
) == VAR_DECL
266 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
268 varpool_node
*vnode
= varpool_node::get_create (t
);
270 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_LOAD
, stmt
);
275 /* Mark store of T. */
278 mark_store (gimple stmt
, tree t
, tree
, void *data
)
280 t
= get_base_address (t
);
281 if (t
&& TREE_CODE (t
) == VAR_DECL
282 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
284 varpool_node
*vnode
= varpool_node::get_create (t
);
286 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_STORE
, stmt
);
291 /* Record all references from cgraph_node that are taken in statement STMT. */
294 cgraph_node::record_stmt_references (gimple stmt
)
296 walk_stmt_load_store_addr_ops (stmt
, this, mark_load
, mark_store
,
300 /* Create cgraph edges for function calls.
301 Also look for functions and variables having addresses taken. */
305 const pass_data pass_data_build_cgraph_edges
=
307 GIMPLE_PASS
, /* type */
308 "*build_cgraph_edges", /* name */
309 OPTGROUP_NONE
, /* optinfo_flags */
311 PROP_cfg
, /* properties_required */
312 0, /* properties_provided */
313 0, /* properties_destroyed */
314 0, /* todo_flags_start */
315 0, /* todo_flags_finish */
318 class pass_build_cgraph_edges
: public gimple_opt_pass
321 pass_build_cgraph_edges (gcc::context
*ctxt
)
322 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
325 /* opt_pass methods: */
326 virtual unsigned int execute (function
*);
328 }; // class pass_build_cgraph_edges
331 pass_build_cgraph_edges::execute (function
*fun
)
334 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
335 gimple_stmt_iterator gsi
;
339 /* Create the callgraph edges and record the nodes referenced by the function.
341 FOR_EACH_BB_FN (bb
, fun
)
343 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
345 gimple stmt
= gsi_stmt (gsi
);
348 if (is_gimple_debug (stmt
))
351 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
353 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
355 decl
= gimple_call_fndecl (call_stmt
);
357 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
, bb
->count
, freq
);
358 else if (gimple_call_internal_p (call_stmt
))
361 node
->create_indirect_edge (call_stmt
,
362 gimple_call_flags (call_stmt
),
365 node
->record_stmt_references (stmt
);
366 if (gomp_parallel
*omp_par_stmt
= dyn_cast
<gomp_parallel
*> (stmt
))
368 tree fn
= gimple_omp_parallel_child_fn (omp_par_stmt
);
369 node
->create_reference (cgraph_node::get_create (fn
),
372 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
374 tree fn
= gimple_omp_task_child_fn (stmt
);
376 node
->create_reference (cgraph_node::get_create (fn
),
378 fn
= gimple_omp_task_copy_fn (stmt
);
380 node
->create_reference (cgraph_node::get_create (fn
),
384 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
385 node
->record_stmt_references (gsi_stmt (gsi
));
388 /* Look for initializers of constant variables and private statics. */
389 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
390 if (TREE_CODE (decl
) == VAR_DECL
391 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
392 && !DECL_HAS_VALUE_EXPR_P (decl
))
393 varpool_node::finalize_decl (decl
);
394 record_eh_tables (node
, fun
);
402 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
404 return new pass_build_cgraph_edges (ctxt
);
407 /* Record references to functions and other variables present in the
408 initial value of DECL, a variable.
409 When ONLY_VARS is true, we mark needed only variables, not functions. */
412 record_references_in_initializer (tree decl
, bool only_vars
)
414 varpool_node
*node
= varpool_node::get_create (decl
);
415 hash_set
<tree
> visited_nodes
;
416 record_reference_ctx ctx
= {false, NULL
};
418 ctx
.varpool_node
= node
;
419 ctx
.only_vars
= only_vars
;
420 walk_tree (&DECL_INITIAL (decl
), record_reference
,
421 &ctx
, &visited_nodes
);
424 /* Rebuild cgraph edges for current function node. This needs to be run after
425 passes that don't update the cgraph. */
428 cgraph_edge::rebuild_edges (void)
431 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
432 gimple_stmt_iterator gsi
;
434 node
->remove_callees ();
435 node
->remove_all_references ();
437 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
439 FOR_EACH_BB_FN (bb
, cfun
)
441 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
443 gimple stmt
= gsi_stmt (gsi
);
446 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
448 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
450 decl
= gimple_call_fndecl (call_stmt
);
452 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
454 else if (gimple_call_internal_p (call_stmt
))
457 node
->create_indirect_edge (call_stmt
,
458 gimple_call_flags (call_stmt
),
461 node
->record_stmt_references (stmt
);
463 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
464 node
->record_stmt_references (gsi_stmt (gsi
));
466 record_eh_tables (node
, cfun
);
467 gcc_assert (!node
->global
.inlined_to
);
469 if (node
->instrumented_version
470 && !node
->instrumentation_clone
)
471 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
476 /* Rebuild cgraph references for current function node. This needs to be run
477 after passes that don't update the cgraph. */
480 cgraph_edge::rebuild_references (void)
483 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
484 gimple_stmt_iterator gsi
;
488 /* Keep speculative references for further cgraph edge expansion. */
489 for (i
= 0; node
->iterate_reference (i
, ref
);)
490 if (!ref
->speculative
)
491 ref
->remove_reference ();
495 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
497 FOR_EACH_BB_FN (bb
, cfun
)
499 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
500 node
->record_stmt_references (gsi_stmt (gsi
));
501 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
502 node
->record_stmt_references (gsi_stmt (gsi
));
504 record_eh_tables (node
, cfun
);
506 if (node
->instrumented_version
507 && !node
->instrumentation_clone
)
508 node
->create_reference (node
->instrumented_version
, IPA_REF_CHKP
, NULL
);
513 const pass_data pass_data_rebuild_cgraph_edges
=
515 GIMPLE_PASS
, /* type */
516 "*rebuild_cgraph_edges", /* name */
517 OPTGROUP_NONE
, /* optinfo_flags */
518 TV_CGRAPH
, /* tv_id */
519 PROP_cfg
, /* properties_required */
520 0, /* properties_provided */
521 0, /* properties_destroyed */
522 0, /* todo_flags_start */
523 0, /* todo_flags_finish */
526 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
529 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
530 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
533 /* opt_pass methods: */
534 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
535 virtual unsigned int execute (function
*)
537 return cgraph_edge::rebuild_edges ();
540 }; // class pass_rebuild_cgraph_edges
545 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
547 return new pass_rebuild_cgraph_edges (ctxt
);
553 const pass_data pass_data_remove_cgraph_callee_edges
=
555 GIMPLE_PASS
, /* type */
556 "*remove_cgraph_callee_edges", /* name */
557 OPTGROUP_NONE
, /* optinfo_flags */
559 0, /* properties_required */
560 0, /* properties_provided */
561 0, /* properties_destroyed */
562 0, /* todo_flags_start */
563 0, /* todo_flags_finish */
566 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
569 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
570 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
573 /* opt_pass methods: */
574 opt_pass
* clone () {
575 return new pass_remove_cgraph_callee_edges (m_ctxt
);
577 virtual unsigned int execute (function
*);
579 }; // class pass_remove_cgraph_callee_edges
582 pass_remove_cgraph_callee_edges::execute (function
*)
584 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
585 node
->remove_callees ();
586 node
->remove_all_references ();
593 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
595 return new pass_remove_cgraph_callee_edges (ctxt
);