1 /* Callgraph construction.
2 Copyright (C) 2003-2013 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"
27 #include "langhooks.h"
28 #include "pointer-set.h"
30 #include "tree-pass.h"
31 #include "ipa-utils.h"
33 #include "ipa-inline.h"
35 /* Context of record_reference. */
36 struct record_reference_ctx
39 struct varpool_node
*varpool_node
;
42 /* Walk tree and record all calls and references to functions/variables.
43 Called via walk_tree: TP is pointer to tree to be examined.
44 When DATA is non-null, record references to callgraph.
48 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
52 struct record_reference_ctx
*ctx
= (struct record_reference_ctx
*)data
;
54 t
= canonicalize_constructor_val (t
, NULL
);
60 switch (TREE_CODE (t
))
69 /* Record dereferences to the functions. This makes the
70 functions reachable unconditionally. */
71 decl
= get_base_var (*tp
);
72 if (TREE_CODE (decl
) == FUNCTION_DECL
)
74 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (decl
);
76 cgraph_mark_address_taken_node (node
);
77 ipa_record_reference (ctx
->varpool_node
,
82 if (TREE_CODE (decl
) == VAR_DECL
)
84 struct varpool_node
*vnode
= varpool_node_for_decl (decl
);
85 ipa_record_reference (ctx
->varpool_node
,
93 /* Save some cycles by not walking types and declaration as we
94 won't find anything useful there anyway. */
95 if (IS_TYPE_OR_DECL_P (*tp
))
106 /* Record references to typeinfos in the type list LIST. */
109 record_type_list (struct cgraph_node
*node
, tree list
)
111 for (; list
; list
= TREE_CHAIN (list
))
113 tree type
= TREE_VALUE (list
);
116 type
= lookup_type_for_runtime (type
);
118 if (TREE_CODE (type
) == ADDR_EXPR
)
120 type
= TREE_OPERAND (type
, 0);
121 if (TREE_CODE (type
) == VAR_DECL
)
123 struct varpool_node
*vnode
= varpool_node_for_decl (type
);
124 ipa_record_reference (node
,
132 /* Record all references we will introduce by producing EH tables
136 record_eh_tables (struct cgraph_node
*node
, struct function
*fun
)
140 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
142 struct cgraph_node
*per_node
;
144 per_node
= cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node
->decl
));
145 ipa_record_reference (node
, per_node
, IPA_REF_ADDR
, NULL
);
146 cgraph_mark_address_taken_node (per_node
);
149 i
= fun
->eh
->region_tree
;
158 case ERT_MUST_NOT_THROW
:
164 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
165 record_type_list (node
, c
->type_list
);
169 case ERT_ALLOWED_EXCEPTIONS
:
170 record_type_list (node
, i
->u
.allowed
.type_list
);
173 /* If there are sub-regions, process them. */
176 /* If there are peers, process them. */
177 else if (i
->next_peer
)
179 /* Otherwise, step back up the tree to the next peer. */
188 while (i
->next_peer
== NULL
);
194 /* Computes the frequency of the call statement so that it can be stored in
195 cgraph_edge. BB is the basic block of the call statement. */
197 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
199 int entry_freq
= ENTRY_BLOCK_PTR_FOR_FUNCTION
200 (DECL_STRUCT_FUNCTION (decl
))->frequency
;
201 int freq
= bb
->frequency
;
203 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl
)) == PROFILE_ABSENT
)
204 return CGRAPH_FREQ_BASE
;
207 entry_freq
= 1, freq
++;
209 freq
= freq
* CGRAPH_FREQ_BASE
/ entry_freq
;
210 if (freq
> CGRAPH_FREQ_MAX
)
211 freq
= CGRAPH_FREQ_MAX
;
216 /* Mark address taken in STMT. */
219 mark_address (gimple stmt
, tree addr
, void *data
)
221 addr
= get_base_address (addr
);
222 if (TREE_CODE (addr
) == FUNCTION_DECL
)
224 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (addr
);
225 cgraph_mark_address_taken_node (node
);
226 ipa_record_reference ((symtab_node
)data
,
230 else if (addr
&& TREE_CODE (addr
) == VAR_DECL
231 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
233 struct varpool_node
*vnode
= varpool_node_for_decl (addr
);
235 ipa_record_reference ((symtab_node
)data
,
243 /* Mark load of T. */
246 mark_load (gimple stmt
, tree t
, 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 struct cgraph_node
*node
= cgraph_get_create_real_symbol_node (t
);
254 cgraph_mark_address_taken_node (node
);
255 ipa_record_reference ((symtab_node
)data
,
259 else if (t
&& TREE_CODE (t
) == VAR_DECL
260 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
262 struct varpool_node
*vnode
= varpool_node_for_decl (t
);
264 ipa_record_reference ((symtab_node
)data
,
271 /* Mark store of T. */
274 mark_store (gimple stmt
, tree t
, void *data
)
276 t
= get_base_address (t
);
277 if (t
&& TREE_CODE (t
) == VAR_DECL
278 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
280 struct varpool_node
*vnode
= varpool_node_for_decl (t
);
282 ipa_record_reference ((symtab_node
)data
,
284 IPA_REF_STORE
, stmt
);
289 /* Record all references from NODE that are taken in statement STMT. */
291 ipa_record_stmt_references (struct cgraph_node
*node
, gimple stmt
)
293 walk_stmt_load_store_addr_ops (stmt
, node
, mark_load
, mark_store
,
297 /* Create cgraph edges for function calls.
298 Also look for functions and variables having addresses taken. */
301 build_cgraph_edges (void)
304 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
305 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
306 gimple_stmt_iterator gsi
;
310 /* Create the callgraph edges and record the nodes referenced by the function.
314 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
316 gimple stmt
= gsi_stmt (gsi
);
319 if (is_gimple_debug (stmt
))
322 if (is_gimple_call (stmt
))
324 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
326 decl
= gimple_call_fndecl (stmt
);
328 cgraph_create_edge (node
, cgraph_get_create_node (decl
),
329 stmt
, bb
->count
, freq
);
331 cgraph_create_indirect_edge (node
, stmt
,
332 gimple_call_flags (stmt
),
335 ipa_record_stmt_references (node
, stmt
);
336 if (gimple_code (stmt
) == GIMPLE_OMP_PARALLEL
337 && gimple_omp_parallel_child_fn (stmt
))
339 tree fn
= gimple_omp_parallel_child_fn (stmt
);
340 ipa_record_reference (node
,
341 cgraph_get_create_real_symbol_node (fn
),
344 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
346 tree fn
= gimple_omp_task_child_fn (stmt
);
348 ipa_record_reference (node
,
349 cgraph_get_create_real_symbol_node (fn
),
351 fn
= gimple_omp_task_copy_fn (stmt
);
353 ipa_record_reference (node
,
354 cgraph_get_create_real_symbol_node (fn
),
358 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
359 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
362 /* Look for initializers of constant variables and private statics. */
363 FOR_EACH_LOCAL_DECL (cfun
, ix
, decl
)
364 if (TREE_CODE (decl
) == VAR_DECL
365 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
366 && !DECL_HAS_VALUE_EXPR_P (decl
))
367 varpool_finalize_decl (decl
);
368 record_eh_tables (node
, cfun
);
370 pointer_set_destroy (visited_nodes
);
376 const pass_data pass_data_build_cgraph_edges
=
378 GIMPLE_PASS
, /* type */
379 "*build_cgraph_edges", /* name */
380 OPTGROUP_NONE
, /* optinfo_flags */
381 false, /* has_gate */
382 true, /* has_execute */
384 PROP_cfg
, /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
388 0, /* todo_flags_finish */
391 class pass_build_cgraph_edges
: public gimple_opt_pass
394 pass_build_cgraph_edges (gcc::context
*ctxt
)
395 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
398 /* opt_pass methods: */
399 unsigned int execute () { return build_cgraph_edges (); }
401 }; // class pass_build_cgraph_edges
406 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
408 return new pass_build_cgraph_edges (ctxt
);
411 /* Record references to functions and other variables present in the
412 initial value of DECL, a variable.
413 When ONLY_VARS is true, we mark needed only variables, not functions. */
416 record_references_in_initializer (tree decl
, bool only_vars
)
418 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
419 struct varpool_node
*node
= varpool_node_for_decl (decl
);
420 struct record_reference_ctx ctx
= {false, NULL
};
422 ctx
.varpool_node
= node
;
423 ctx
.only_vars
= only_vars
;
424 walk_tree (&DECL_INITIAL (decl
), record_reference
,
425 &ctx
, visited_nodes
);
426 pointer_set_destroy (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 rebuild_cgraph_edges (void)
436 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
437 gimple_stmt_iterator gsi
;
439 cgraph_node_remove_callees (node
);
440 ipa_remove_all_references (&node
->ref_list
);
442 node
->count
= ENTRY_BLOCK_PTR
->count
;
446 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
448 gimple stmt
= gsi_stmt (gsi
);
451 if (is_gimple_call (stmt
))
453 int freq
= compute_call_stmt_bb_frequency (current_function_decl
,
455 decl
= gimple_call_fndecl (stmt
);
457 cgraph_create_edge (node
, cgraph_get_create_node (decl
), stmt
,
460 cgraph_create_indirect_edge (node
, stmt
,
461 gimple_call_flags (stmt
),
464 ipa_record_stmt_references (node
, stmt
);
466 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
467 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
469 record_eh_tables (node
, cfun
);
470 gcc_assert (!node
->global
.inlined_to
);
475 /* Rebuild cgraph edges for current function node. This needs to be run after
476 passes that don't update the cgraph. */
479 cgraph_rebuild_references (void)
482 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
483 gimple_stmt_iterator gsi
;
487 /* Keep speculative references for further cgraph edge expansion. */
488 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
);)
489 if (!ref
->speculative
)
490 ipa_remove_reference (ref
);
494 node
->count
= ENTRY_BLOCK_PTR
->count
;
498 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
499 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
500 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
501 ipa_record_stmt_references (node
, gsi_stmt (gsi
));
503 record_eh_tables (node
, cfun
);
508 const pass_data pass_data_rebuild_cgraph_edges
=
510 GIMPLE_PASS
, /* type */
511 "*rebuild_cgraph_edges", /* name */
512 OPTGROUP_NONE
, /* optinfo_flags */
513 false, /* has_gate */
514 true, /* has_execute */
515 TV_CGRAPH
, /* tv_id */
516 PROP_cfg
, /* properties_required */
517 0, /* properties_provided */
518 0, /* properties_destroyed */
519 0, /* todo_flags_start */
520 0, /* todo_flags_finish */
523 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
526 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
527 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
530 /* opt_pass methods: */
531 opt_pass
* clone () { return new pass_rebuild_cgraph_edges (m_ctxt
); }
532 unsigned int execute () { return rebuild_cgraph_edges (); }
534 }; // class pass_rebuild_cgraph_edges
539 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
541 return new pass_rebuild_cgraph_edges (ctxt
);
546 remove_cgraph_callee_edges (void)
548 struct cgraph_node
*node
= cgraph_get_node (current_function_decl
);
549 cgraph_node_remove_callees (node
);
550 ipa_remove_all_references (&node
->ref_list
);
556 const pass_data pass_data_remove_cgraph_callee_edges
=
558 GIMPLE_PASS
, /* type */
559 "*remove_cgraph_callee_edges", /* name */
560 OPTGROUP_NONE
, /* optinfo_flags */
561 false, /* has_gate */
562 true, /* has_execute */
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 unsigned int execute () { return remove_cgraph_callee_edges (); }
584 }; // class pass_remove_cgraph_callee_edges
589 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
591 return new pass_remove_cgraph_callee_edges (ctxt
);