1 /* Callgraph construction.
2 Copyright (C) 2003-2014 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"
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"
49 #include "plugin-api.h"
52 #include "ipa-utils.h"
54 #include "alloc-pool.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 (is_gimple_call (stmt
))
358 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
360 decl
= gimple_call_fndecl (stmt
);
362 node
->create_edge (cgraph_node::get_create (decl
), stmt
, bb
->count
, freq
);
363 else if (gimple_call_internal_p (stmt
))
366 node
->create_indirect_edge (stmt
,
367 gimple_call_flags (stmt
),
370 node
->record_stmt_references (stmt
);
371 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
372 && gimple_omp_parallel_child_fn (stmt
))
374 tree fn
= gimple_omp_parallel_child_fn (stmt
);
375 node
->create_reference (cgraph_node::get_create (fn
),
378 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
380 tree fn
= gimple_omp_task_child_fn (stmt
);
382 node
->create_reference (cgraph_node::get_create (fn
),
384 fn
= gimple_omp_task_copy_fn (stmt
);
386 node
->create_reference (cgraph_node::get_create (fn
),
390 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
391 node
->record_stmt_references (gsi_stmt (gsi
));
394 /* Look for initializers of constant variables and private statics. */
395 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
396 if (TREE_CODE (decl
) == VAR_DECL
397 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
398 && !DECL_HAS_VALUE_EXPR_P (decl
))
399 varpool_node::finalize_decl (decl
);
400 record_eh_tables (node
, fun
);
408 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
410 return new pass_build_cgraph_edges (ctxt
);
413 /* Record references to functions and other variables present in the
414 initial value of DECL, a variable.
415 When ONLY_VARS is true, we mark needed only variables, not functions. */
418 record_references_in_initializer (tree decl
, bool only_vars
)
420 varpool_node
*node
= varpool_node::get_create (decl
);
421 hash_set
<tree
> visited_nodes
;
422 record_reference_ctx ctx
= {false, NULL
};
424 ctx
.varpool_node
= node
;
425 ctx
.only_vars
= only_vars
;
426 walk_tree (&DECL_INITIAL (decl
), record_reference
,
427 &ctx
, &visited_nodes
);
430 /* Rebuild cgraph edges for current function node. This needs to be run after
431 passes that don't update the cgraph. */
434 cgraph_edge::rebuild_edges (void)
437 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
438 gimple_stmt_iterator gsi
;
440 node
->remove_callees ();
441 node
->remove_all_references ();
443 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
445 FOR_EACH_BB_FN (bb
, cfun
)
447 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
449 gimple stmt
= gsi_stmt (gsi
);
452 if (is_gimple_call (stmt
))
454 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
456 decl
= gimple_call_fndecl (stmt
);
458 node
->create_edge (cgraph_node::get_create (decl
), stmt
,
460 else if (gimple_call_internal_p (stmt
))
463 node
->create_indirect_edge (stmt
,
464 gimple_call_flags (stmt
),
467 node
->record_stmt_references (stmt
);
469 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
470 node
->record_stmt_references (gsi_stmt (gsi
));
472 record_eh_tables (node
, cfun
);
473 gcc_assert (!node
->global
.inlined_to
);
478 /* Rebuild cgraph references for current function node. This needs to be run
479 after passes that don't update the cgraph. */
482 cgraph_edge::rebuild_references (void)
485 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
486 gimple_stmt_iterator gsi
;
490 /* Keep speculative references for further cgraph edge expansion. */
491 for (i
= 0; node
->iterate_reference (i
, ref
);)
492 if (!ref
->speculative
)
493 ref
->remove_reference ();
497 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
499 FOR_EACH_BB_FN (bb
, cfun
)
501 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
502 node
->record_stmt_references (gsi_stmt (gsi
));
503 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
504 node
->record_stmt_references (gsi_stmt (gsi
));
506 record_eh_tables (node
, cfun
);
511 const pass_data pass_data_rebuild_cgraph_edges
=
513 GIMPLE_PASS
, /* type */
514 "*rebuild_cgraph_edges", /* name */
515 OPTGROUP_NONE
, /* optinfo_flags */
516 TV_CGRAPH
, /* tv_id */
517 PROP_cfg
, /* properties_required */
518 0, /* properties_provided */
519 0, /* properties_destroyed */
520 0, /* todo_flags_start */
521 0, /* todo_flags_finish */
524 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
527 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
528 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
531 /* opt_pass methods: */
532 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
533 virtual unsigned int execute (function
*)
535 return cgraph_edge::rebuild_edges ();
538 }; // class pass_rebuild_cgraph_edges
543 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
545 return new pass_rebuild_cgraph_edges (ctxt
);
551 const pass_data pass_data_remove_cgraph_callee_edges
=
553 GIMPLE_PASS
, /* type */
554 "*remove_cgraph_callee_edges", /* name */
555 OPTGROUP_NONE
, /* optinfo_flags */
557 0, /* properties_required */
558 0, /* properties_provided */
559 0, /* properties_destroyed */
560 0, /* todo_flags_start */
561 0, /* todo_flags_finish */
564 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
567 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
568 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
571 /* opt_pass methods: */
572 opt_pass
* clone () {
573 return new pass_remove_cgraph_callee_edges (m_ctxt
);
575 virtual unsigned int execute (function
*);
577 }; // class pass_remove_cgraph_callee_edges
580 pass_remove_cgraph_callee_edges::execute (function
*)
582 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
583 node
->remove_callees ();
584 node
->remove_all_references ();
591 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
593 return new pass_remove_cgraph_callee_edges (ctxt
);