1 /* Callgraph construction.
2 Copyright (C) 2003-2024 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 "tree-pass.h"
29 #include "gimple-iterator.h"
30 #include "gimple-fold.h"
31 #include "gimple-walk.h"
32 #include "ipa-utils.h"
36 /* Context of record_reference. */
37 struct record_reference_ctx
40 struct varpool_node
*varpool_node
;
43 /* Walk tree and record all calls and references to functions/variables.
44 Called via walk_tree: TP is pointer to tree to be examined.
45 When DATA is non-null, record references to callgraph.
49 record_reference (tree
*tp
, int *walk_subtrees
, void *data
)
53 record_reference_ctx
*ctx
= (record_reference_ctx
*)data
;
55 t
= canonicalize_constructor_val (t
, NULL
);
61 switch (TREE_CODE (t
))
70 /* Record dereferences to the functions. This makes the
71 functions reachable unconditionally. */
72 decl
= get_base_var (*tp
);
73 if (TREE_CODE (decl
) == FUNCTION_DECL
)
75 cgraph_node
*node
= cgraph_node::get_create (decl
);
77 node
->mark_address_taken ();
78 ctx
->varpool_node
->create_reference (node
, IPA_REF_ADDR
);
83 /* Replace vars with their DECL_VALUE_EXPR if any.
84 This is normally done during gimplification, but
85 static var initializers are never gimplified. */
86 if (DECL_HAS_VALUE_EXPR_P (decl
))
89 for (p
= tp
; *p
!= decl
; p
= &TREE_OPERAND (*p
, 0))
91 *p
= unshare_expr (DECL_VALUE_EXPR (decl
));
92 return record_reference (tp
, walk_subtrees
, data
);
94 varpool_node
*vnode
= varpool_node::get_create (decl
);
95 ctx
->varpool_node
->create_reference (vnode
, IPA_REF_ADDR
);
101 /* Save some cycles by not walking types and declaration as we
102 won't find anything useful there anyway. */
103 if (IS_TYPE_OR_DECL_P (*tp
))
114 /* Record references to typeinfos in the type list LIST. */
117 record_type_list (cgraph_node
*node
, tree list
)
119 for (; list
; list
= TREE_CHAIN (list
))
121 tree type
= TREE_VALUE (list
);
124 type
= lookup_type_for_runtime (type
);
126 if (TREE_CODE (type
) == ADDR_EXPR
)
128 type
= TREE_OPERAND (type
, 0);
131 varpool_node
*vnode
= varpool_node::get_create (type
);
132 node
->create_reference (vnode
, IPA_REF_ADDR
);
138 /* Record all references we will introduce by producing EH tables
142 record_eh_tables (cgraph_node
*node
, function
*fun
)
146 if (DECL_FUNCTION_PERSONALITY (node
->decl
))
148 tree per_decl
= DECL_FUNCTION_PERSONALITY (node
->decl
);
149 cgraph_node
*per_node
= cgraph_node::get_create (per_decl
);
151 node
->create_reference (per_node
, IPA_REF_ADDR
);
152 per_node
->mark_address_taken ();
155 i
= fun
->eh
->region_tree
;
164 case ERT_MUST_NOT_THROW
:
170 for (c
= i
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
171 record_type_list (node
, c
->type_list
);
175 case ERT_ALLOWED_EXCEPTIONS
:
176 record_type_list (node
, i
->u
.allowed
.type_list
);
179 /* If there are sub-regions, process them. */
182 /* If there are peers, process them. */
183 else if (i
->next_peer
)
185 /* Otherwise, step back up the tree to the next peer. */
194 while (i
->next_peer
== NULL
);
200 /* Computes the frequency of the call statement so that it can be stored in
201 cgraph_edge. BB is the basic block of the call statement. */
203 compute_call_stmt_bb_frequency (tree decl
, basic_block bb
)
205 return bb
->count
.to_cgraph_frequency
206 (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl
))->count
);
209 /* Mark address taken in STMT. */
212 mark_address (gimple
*stmt
, tree addr
, tree
, void *data
)
214 addr
= get_base_address (addr
);
215 if (TREE_CODE (addr
) == FUNCTION_DECL
)
217 cgraph_node
*node
= cgraph_node::get_create (addr
);
218 node
->mark_address_taken ();
219 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
221 else if (addr
&& VAR_P (addr
)
222 && (TREE_STATIC (addr
) || DECL_EXTERNAL (addr
)))
224 varpool_node
*vnode
= varpool_node::get_create (addr
);
226 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_ADDR
, stmt
);
232 /* Mark load of T. */
235 mark_load (gimple
*stmt
, tree t
, tree
, void *data
)
237 t
= get_base_address (t
);
238 if (t
&& TREE_CODE (t
) == FUNCTION_DECL
)
240 /* ??? This can happen on platforms with descriptors when these are
241 directly manipulated in the code. Pretend that it's an address. */
242 cgraph_node
*node
= cgraph_node::get_create (t
);
243 node
->mark_address_taken ();
244 ((symtab_node
*)data
)->create_reference (node
, IPA_REF_ADDR
, stmt
);
246 else if (t
&& VAR_P (t
) && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
248 varpool_node
*vnode
= varpool_node::get_create (t
);
250 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_LOAD
, stmt
);
255 /* Mark store of T. */
258 mark_store (gimple
*stmt
, tree t
, tree
, void *data
)
260 t
= get_base_address (t
);
261 if (t
&& VAR_P (t
) && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
263 varpool_node
*vnode
= varpool_node::get_create (t
);
265 ((symtab_node
*)data
)->create_reference (vnode
, IPA_REF_STORE
, stmt
);
270 /* Record all references from cgraph_node that are taken in statement STMT. */
273 cgraph_node::record_stmt_references (gimple
*stmt
)
275 walk_stmt_load_store_addr_ops (stmt
, this, mark_load
, mark_store
,
279 /* Create cgraph edges for function calls.
280 Also look for functions and variables having addresses taken. */
284 const pass_data pass_data_build_cgraph_edges
=
286 GIMPLE_PASS
, /* type */
287 "*build_cgraph_edges", /* name */
288 OPTGROUP_NONE
, /* optinfo_flags */
290 PROP_cfg
, /* properties_required */
291 0, /* properties_provided */
292 0, /* properties_destroyed */
293 0, /* todo_flags_start */
294 0, /* todo_flags_finish */
297 class pass_build_cgraph_edges
: public gimple_opt_pass
300 pass_build_cgraph_edges (gcc::context
*ctxt
)
301 : gimple_opt_pass (pass_data_build_cgraph_edges
, ctxt
)
304 /* opt_pass methods: */
305 unsigned int execute (function
*) final override
;
307 }; // class pass_build_cgraph_edges
310 pass_build_cgraph_edges::execute (function
*fun
)
313 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
314 gimple_stmt_iterator gsi
;
318 /* Create the callgraph edges and record the nodes referenced by the function.
320 FOR_EACH_BB_FN (bb
, fun
)
322 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
324 gimple
*stmt
= gsi_stmt (gsi
);
327 if (is_gimple_debug (stmt
))
330 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
332 decl
= gimple_call_fndecl (call_stmt
);
334 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
, bb
->count
);
335 else if (gimple_call_internal_p (call_stmt
))
338 node
->create_indirect_edge (call_stmt
,
339 gimple_call_flags (call_stmt
),
342 node
->record_stmt_references (stmt
);
343 if (gomp_parallel
*omp_par_stmt
= dyn_cast
<gomp_parallel
*> (stmt
))
345 tree fn
= gimple_omp_parallel_child_fn (omp_par_stmt
);
346 node
->create_reference (cgraph_node::get_create (fn
),
349 if (gimple_code (stmt
) == GIMPLE_OMP_TASK
)
351 tree fn
= gimple_omp_task_child_fn (stmt
);
353 node
->create_reference (cgraph_node::get_create (fn
),
355 fn
= gimple_omp_task_copy_fn (stmt
);
357 node
->create_reference (cgraph_node::get_create (fn
),
361 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
362 node
->record_stmt_references (gsi_stmt (gsi
));
365 /* Look for initializers of constant variables and private statics. */
366 FOR_EACH_LOCAL_DECL (fun
, ix
, decl
)
368 && (TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
369 && !DECL_HAS_VALUE_EXPR_P (decl
)
370 && TREE_TYPE (decl
) != error_mark_node
)
371 varpool_node::finalize_decl (decl
);
372 record_eh_tables (node
, fun
);
380 make_pass_build_cgraph_edges (gcc::context
*ctxt
)
382 return new pass_build_cgraph_edges (ctxt
);
385 /* Record references to functions and other variables present in the
386 initial value of DECL, a variable.
387 When ONLY_VARS is true, we mark needed only variables, not functions. */
390 record_references_in_initializer (tree decl
, bool only_vars
)
392 varpool_node
*node
= varpool_node::get_create (decl
);
393 hash_set
<tree
> visited_nodes
;
394 record_reference_ctx ctx
= {false, NULL
};
396 ctx
.varpool_node
= node
;
397 ctx
.only_vars
= only_vars
;
398 walk_tree (&DECL_INITIAL (decl
), record_reference
,
399 &ctx
, &visited_nodes
);
402 /* Rebuild cgraph edges for current function node. This needs to be run after
403 passes that don't update the cgraph. */
406 cgraph_edge::rebuild_edges (void)
409 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
410 gimple_stmt_iterator gsi
;
412 node
->remove_callees ();
413 node
->remove_all_references ();
415 node
->count
= ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
;
417 FOR_EACH_BB_FN (bb
, cfun
)
419 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
421 gimple
*stmt
= gsi_stmt (gsi
);
424 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
426 decl
= gimple_call_fndecl (call_stmt
);
428 node
->create_edge (cgraph_node::get_create (decl
), call_stmt
,
430 else if (gimple_call_internal_p (call_stmt
))
433 node
->create_indirect_edge (call_stmt
,
434 gimple_call_flags (call_stmt
),
437 node
->record_stmt_references (stmt
);
439 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
440 node
->record_stmt_references (gsi_stmt (gsi
));
442 record_eh_tables (node
, cfun
);
443 gcc_assert (!node
->inlined_to
);
447 /* Rebuild cgraph references for current function node. This needs to be run
448 after passes that don't update the cgraph. */
451 cgraph_edge::rebuild_references (void)
454 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
455 gimple_stmt_iterator gsi
;
459 /* Keep speculative references for further cgraph edge expansion. */
460 for (i
= 0; node
->iterate_reference (i
, ref
);)
461 if (!ref
->speculative
)
462 ref
->remove_reference ();
466 FOR_EACH_BB_FN (bb
, cfun
)
468 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
469 node
->record_stmt_references (gsi_stmt (gsi
));
470 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
471 node
->record_stmt_references (gsi_stmt (gsi
));
473 record_eh_tables (node
, cfun
);
478 const pass_data pass_data_rebuild_cgraph_edges
=
480 GIMPLE_PASS
, /* type */
481 "*rebuild_cgraph_edges", /* name */
482 OPTGROUP_NONE
, /* optinfo_flags */
483 TV_CGRAPH
, /* tv_id */
484 PROP_cfg
, /* properties_required */
485 0, /* properties_provided */
486 0, /* properties_destroyed */
487 0, /* todo_flags_start */
488 0, /* todo_flags_finish */
491 class pass_rebuild_cgraph_edges
: public gimple_opt_pass
494 pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
495 : gimple_opt_pass (pass_data_rebuild_cgraph_edges
, ctxt
)
498 /* opt_pass methods: */
499 opt_pass
* clone () final override
501 return new pass_rebuild_cgraph_edges (m_ctxt
);
503 unsigned int execute (function
*) final override
505 return cgraph_edge::rebuild_edges ();
508 }; // class pass_rebuild_cgraph_edges
513 make_pass_rebuild_cgraph_edges (gcc::context
*ctxt
)
515 return new pass_rebuild_cgraph_edges (ctxt
);
521 const pass_data pass_data_remove_cgraph_callee_edges
=
523 GIMPLE_PASS
, /* type */
524 "*remove_cgraph_callee_edges", /* name */
525 OPTGROUP_NONE
, /* optinfo_flags */
527 0, /* properties_required */
528 0, /* properties_provided */
529 0, /* properties_destroyed */
530 0, /* todo_flags_start */
531 0, /* todo_flags_finish */
534 class pass_remove_cgraph_callee_edges
: public gimple_opt_pass
537 pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
538 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges
, ctxt
)
541 /* opt_pass methods: */
542 opt_pass
* clone () final override
{
543 return new pass_remove_cgraph_callee_edges (m_ctxt
);
545 unsigned int execute (function
*) final override
;
547 }; // class pass_remove_cgraph_callee_edges
550 pass_remove_cgraph_callee_edges::execute (function
*)
552 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
553 node
->remove_callees ();
554 node
->remove_all_references ();
561 make_pass_remove_cgraph_callee_edges (gcc::context
*ctxt
)
563 return new pass_remove_cgraph_callee_edges (ctxt
);