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"
26 #include "pointer-set.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-fold.h"
31 #include "gimple-expr.h"
34 #include "gimple-iterator.h"
35 #include "gimple-walk.h"
36 #include "langhooks.h"
38 #include "tree-pass.h"
39 #include "ipa-utils.h"
41 #include "ipa-inline.h"
43 /* Context of record_reference. */
44 struct record_reference_ctx
47 class varpool_node
*varpool_node
;
50 /* Walk tree and record all calls and references to functions/variables.
51 Called via walk_tree: TP is pointer to tree to be examined.
52 When DATA is non-null, record references to callgraph.
56 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
60 struct record_reference_ctx
*ctx
= (struct record_reference_ctx
*)data
;
62 t
= canonicalize_constructor_val (t
, NULL
);
68 switch (TREE_CODE (t
))
77 /* Record dereferences to the functions. This makes the
78 functions reachable unconditionally. */
79 decl
= get_base_var (*tp
);
80 if (TREE_CODE (decl
) == FUNCTION_DECL
)
82 struct cgraph_node
*node
= cgraph_node::get_create (decl
);
84 node
->mark_address_taken ();
85 ctx
->varpool_node
->add_reference (node
, IPA_REF_ADDR
);
88 if (TREE_CODE (decl
) == VAR_DECL
)
90 varpool_node
*vnode
= varpool_node::get_create (decl
);
91 ctx
->varpool_node
->add_reference (vnode
, IPA_REF_ADDR
);
97 /* Save some cycles by not walking types and declaration as we
98 won't find anything useful there anyway. */
99 if (IS_TYPE_OR_DECL_P (*tp
))
110 /* Record references to typeinfos in the type list LIST. */
113 record_type_list (struct cgraph_node
*node
, tree list
)
115 for (; list
; list
= TREE_CHAIN (list
))
117 tree type
= TREE_VALUE (list
);
120 type
= lookup_type_for_runtime (type
);
122 if (TREE_CODE (type
) == ADDR_EXPR
)
124 type
= TREE_OPERAND (type
, 0);
125 if (TREE_CODE (type
) == VAR_DECL
)
127 varpool_node
*vnode
= varpool_node::get_create (type
);
128 node
->add_reference (vnode
, IPA_REF_ADDR
);
134 /* Record all references we will introduce by producing EH tables
138 record_eh_tables (struct cgraph_node
*node
, struct function
*fun
)
142 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
144 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
145 struct cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
147 node
->add_reference (per_node
, IPA_REF_ADDR
);
148 per_node
->mark_address_taken ();
151 i
= fun
->eh
->region_tree
;
160 case ERT_MUST_NOT_THROW
:
166 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
167 record_type_list (node
, c
->type_list
);
171 case ERT_ALLOWED_EXCEPTIONS
:
172 record_type_list (node
, i
->u
.allowed
.type_list
);
175 /* If there are sub-regions, process them. */
178 /* If there are peers, process them. */
179 else if (i
->next_peer
)
181 /* Otherwise, step back up the tree to the next peer. */
190 while (i
->next_peer
== NULL
);
196 /* Computes the frequency of the call statement so that it can be stored in
197 cgraph_edge. BB is the basic block of the call statement. */
199 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
201 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FN
202 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
203 int freq
= bb
->frequency
;
205 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
206 return CGRAPH_FREQ_BASE
;
209 entry_freq
= 1, freq
++;
211 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
212 if (freq
> CGRAPH_FREQ_MAX
)
213 freq
= CGRAPH_FREQ_MAX
;
218 /* Mark address taken in STMT. */
221 mark_address (gimple stmt
, tree addr
, tree
, void *data
)
223 addr
= get_base_address (addr
);
224 if (TREE_CODE (addr
) == FUNCTION_DECL
)
226 struct cgraph_node
*node
= cgraph_node::get_create (addr
);
227 node
->mark_address_taken ();
228 ((symtab_node
*)data
)->add_reference (node
, IPA_REF_ADDR
, stmt
);
230 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
231 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
233 varpool_node
*vnode
= varpool_node::get_create (addr
);
235 ((symtab_node
*)data
)->add_reference (vnode
, IPA_REF_ADDR
, stmt
);
241 /* Mark load of T. */
244 mark_load (gimple stmt
, tree t
, tree
, void *data
)
246 t
= get_base_address (t
);
247 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
249 /* ??? This can happen on platforms with descriptors when these are
250 directly manipulated in the code. Pretend that it's an address. */
251 struct cgraph_node
*node
= cgraph_node::get_create (t
);
252 node
->mark_address_taken ();
253 ((symtab_node
*)data
)->add_reference (node
, IPA_REF_ADDR
, stmt
);
255 else if (t
&& TREE_CODE (t
) == VAR_DECL
256 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
258 varpool_node
*vnode
= varpool_node::get_create (t
);
260 ((symtab_node
*)data
)->add_reference (vnode
, IPA_REF_LOAD
, stmt
);
265 /* Mark store of T. */
268 mark_store (gimple stmt
, tree t
, tree
, void *data
)
270 t
= get_base_address (t
);
271 if (t
&& TREE_CODE (t
) == VAR_DECL
272 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
274 varpool_node
*vnode
= varpool_node::get_create (t
);
276 ((symtab_node
*)data
)->add_reference (vnode
, IPA_REF_STORE
, stmt
);
281 /* Record all references from cgraph_node that are taken in statement STMT. */
284 cgraph_node::record_stmt_references (gimple stmt
)
286 walk_stmt_load_store_addr_ops (stmt
, this, mark_load
, mark_store
,
290 /* Create cgraph edges for function calls.
291 Also look for functions and variables having addresses taken. */
295 const pass_data pass_data_build_cgraph_edges
=
297 GIMPLE_PASS
, /* type */
298 "*build_cgraph_edges", /* name */
299 OPTGROUP_NONE
, /* optinfo_flags */
301 PROP_cfg
, /* properties_required */
302 0, /* properties_provided */
303 0, /* properties_destroyed */
304 0, /* todo_flags_start */
305 0, /* todo_flags_finish */
308 class pass_build_cgraph_edges
: public gimple_opt_pass
311 pass_build_cgraph_edges (gcc::context
*ctxt
)
312 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
315 /* opt_pass methods: */
316 virtual unsigned int execute (function
*);
318 }; // class pass_build_cgraph_edges
321 pass_build_cgraph_edges::execute (function
*fun
)
324 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
325 gimple_stmt_iterator gsi
;
329 /* Create the callgraph edges and record the nodes referenced by the function.
331 FOR_EACH_BB_FN (bb
, fun
)
333 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
335 gimple stmt
= gsi_stmt (gsi
);
338 if (is_gimple_debug (stmt
))
341 if (is_gimple_call (stmt
))
343 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
345 decl
= gimple_call_fndecl (stmt
);
347 node
->create_edge (cgraph_node::get_create (decl
),
348 stmt
, bb
->count
, freq
);
349 else if (gimple_call_internal_p (stmt
))
352 node
->create_indirect_edge (stmt
,
353 gimple_call_flags (stmt
),
356 node
->record_stmt_references (stmt
);
357 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
358 && gimple_omp_parallel_child_fn (stmt
))
360 tree fn
= gimple_omp_parallel_child_fn (stmt
);
361 node
->add_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
->add_reference (cgraph_node::get_create (fn
),
370 fn
= gimple_omp_task_copy_fn (stmt
);
372 node
->add_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 struct 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 rebuild_cgraph_edges (void)
423 struct 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 (is_gimple_call (stmt
))
440 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
442 decl
= gimple_call_fndecl (stmt
);
444 node
->create_edge (cgraph_node::get_create (decl
), stmt
,
446 else if (gimple_call_internal_p (stmt
))
449 node
->create_indirect_edge (stmt
,
450 gimple_call_flags (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
);
464 /* Rebuild cgraph edges for current function node. This needs to be run after
465 passes that don't update the cgraph. */
468 cgraph_rebuild_references (void)
471 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
472 gimple_stmt_iterator gsi
;
473 struct ipa_ref
*ref
= NULL
;
476 /* Keep speculative references for further cgraph edge expansion. */
477 for (i
= 0; node
->iterate_reference (i
, ref
);)
478 if (!ref
->speculative
)
479 ref
->remove_reference ();
483 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
485 FOR_EACH_BB_FN (bb
, cfun
)
487 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
488 node
->record_stmt_references (gsi_stmt (gsi
));
489 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
490 node
->record_stmt_references (gsi_stmt (gsi
));
492 record_eh_tables (node
, cfun
);
497 const pass_data pass_data_rebuild_cgraph_edges
=
499 GIMPLE_PASS
, /* type */
500 "*rebuild_cgraph_edges", /* name */
501 OPTGROUP_NONE
, /* optinfo_flags */
502 TV_CGRAPH
, /* tv_id */
503 PROP_cfg
, /* properties_required */
504 0, /* properties_provided */
505 0, /* properties_destroyed */
506 0, /* todo_flags_start */
507 0, /* todo_flags_finish */
510 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
513 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
514 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
517 /* opt_pass methods: */
518 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
519 virtual unsigned int execute (function
*) { return rebuild_cgraph_edges (); }
521 }; // class pass_rebuild_cgraph_edges
526 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
528 return new pass_rebuild_cgraph_edges (ctxt
);
534 const pass_data pass_data_remove_cgraph_callee_edges
=
536 GIMPLE_PASS
, /* type */
537 "*remove_cgraph_callee_edges", /* name */
538 OPTGROUP_NONE
, /* optinfo_flags */
540 0, /* properties_required */
541 0, /* properties_provided */
542 0, /* properties_destroyed */
543 0, /* todo_flags_start */
544 0, /* todo_flags_finish */
547 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
550 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
551 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
554 /* opt_pass methods: */
555 opt_pass
* clone () {
556 return new pass_remove_cgraph_callee_edges (m_ctxt
);
558 virtual unsigned int execute (function
*);
560 }; // class pass_remove_cgraph_callee_edges
563 pass_remove_cgraph_callee_edges::execute (function
*)
565 struct cgraph_node
*node
= cgraph_node::get (current_function_decl
);
566 node
->remove_callees ();
567 node
->remove_all_references ();
574 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
576 return new pass_remove_cgraph_callee_edges (ctxt
);