gcc/ada/
[official-gcc.git] / gcc / cgraphbuild.c
blob6726a348e9b0bd03bb5db4402acec7e3a5dfbe7a
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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "predict.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.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"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-walk.h"
42 #include "langhooks.h"
43 #include "intl.h"
44 #include "tree-pass.h"
45 #include "plugin-api.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "ipa-utils.h"
49 #include "except.h"
50 #include "alloc-pool.h"
51 #include "symbol-summary.h"
52 #include "ipa-prop.h"
53 #include "ipa-inline.h"
55 /* Context of record_reference. */
56 struct record_reference_ctx
58 bool only_vars;
59 class varpool_node *varpool_node;
62 /* Walk tree and record all calls and references to functions/variables.
63 Called via walk_tree: TP is pointer to tree to be examined.
64 When DATA is non-null, record references to callgraph.
67 static tree
68 record_reference (tree *tp, int *walk_subtrees, void *data)
70 tree t = *tp;
71 tree decl;
72 record_reference_ctx *ctx = (record_reference_ctx *)data;
74 t = canonicalize_constructor_val (t, NULL);
75 if (!t)
76 t = *tp;
77 else if (t != *tp)
78 *tp = t;
80 switch (TREE_CODE (t))
82 case VAR_DECL:
83 case FUNCTION_DECL:
84 gcc_unreachable ();
85 break;
87 case FDESC_EXPR:
88 case ADDR_EXPR:
89 /* Record dereferences to the functions. This makes the
90 functions reachable unconditionally. */
91 decl = get_base_var (*tp);
92 if (TREE_CODE (decl) == FUNCTION_DECL)
94 cgraph_node *node = cgraph_node::get_create (decl);
95 if (!ctx->only_vars)
96 node->mark_address_taken ();
97 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
100 if (TREE_CODE (decl) == VAR_DECL)
102 varpool_node *vnode = varpool_node::get_create (decl);
103 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
105 *walk_subtrees = 0;
106 break;
108 default:
109 /* Save some cycles by not walking types and declaration as we
110 won't find anything useful there anyway. */
111 if (IS_TYPE_OR_DECL_P (*tp))
113 *walk_subtrees = 0;
114 break;
116 break;
119 return NULL_TREE;
122 /* Record references to typeinfos in the type list LIST. */
124 static void
125 record_type_list (cgraph_node *node, tree list)
127 for (; list; list = TREE_CHAIN (list))
129 tree type = TREE_VALUE (list);
131 if (TYPE_P (type))
132 type = lookup_type_for_runtime (type);
133 STRIP_NOPS (type);
134 if (TREE_CODE (type) == ADDR_EXPR)
136 type = TREE_OPERAND (type, 0);
137 if (TREE_CODE (type) == VAR_DECL)
139 varpool_node *vnode = varpool_node::get_create (type);
140 node->create_reference (vnode, IPA_REF_ADDR);
146 /* Record all references we will introduce by producing EH tables
147 for NODE. */
149 static void
150 record_eh_tables (cgraph_node *node, function *fun)
152 eh_region i;
154 if (DECL_FUNCTION_PERSONALITY (node->decl))
156 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
157 cgraph_node *per_node = cgraph_node::get_create (per_decl);
159 node->create_reference (per_node, IPA_REF_ADDR);
160 per_node->mark_address_taken ();
163 i = fun->eh->region_tree;
164 if (!i)
165 return;
167 while (1)
169 switch (i->type)
171 case ERT_CLEANUP:
172 case ERT_MUST_NOT_THROW:
173 break;
175 case ERT_TRY:
177 eh_catch c;
178 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
179 record_type_list (node, c->type_list);
181 break;
183 case ERT_ALLOWED_EXCEPTIONS:
184 record_type_list (node, i->u.allowed.type_list);
185 break;
187 /* If there are sub-regions, process them. */
188 if (i->inner)
189 i = i->inner;
190 /* If there are peers, process them. */
191 else if (i->next_peer)
192 i = i->next_peer;
193 /* Otherwise, step back up the tree to the next peer. */
194 else
198 i = i->outer;
199 if (i == NULL)
200 return;
202 while (i->next_peer == NULL);
203 i = i->next_peer;
208 /* Computes the frequency of the call statement so that it can be stored in
209 cgraph_edge. BB is the basic block of the call statement. */
211 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
213 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
214 (DECL_STRUCT_FUNCTION (decl))->frequency;
215 int freq = bb->frequency;
217 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
218 return CGRAPH_FREQ_BASE;
220 if (!entry_freq)
221 entry_freq = 1, freq++;
223 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
224 if (freq > CGRAPH_FREQ_MAX)
225 freq = CGRAPH_FREQ_MAX;
227 return freq;
230 /* Mark address taken in STMT. */
232 static bool
233 mark_address (gimple stmt, tree addr, tree, void *data)
235 addr = get_base_address (addr);
236 if (TREE_CODE (addr) == FUNCTION_DECL)
238 cgraph_node *node = cgraph_node::get_create (addr);
239 node->mark_address_taken ();
240 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
242 else if (addr && TREE_CODE (addr) == VAR_DECL
243 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
245 varpool_node *vnode = varpool_node::get_create (addr);
247 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
250 return false;
253 /* Mark load of T. */
255 static bool
256 mark_load (gimple stmt, tree t, tree, void *data)
258 t = get_base_address (t);
259 if (t && TREE_CODE (t) == FUNCTION_DECL)
261 /* ??? This can happen on platforms with descriptors when these are
262 directly manipulated in the code. Pretend that it's an address. */
263 cgraph_node *node = cgraph_node::get_create (t);
264 node->mark_address_taken ();
265 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
267 else if (t && TREE_CODE (t) == VAR_DECL
268 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
270 varpool_node *vnode = varpool_node::get_create (t);
272 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
274 return false;
277 /* Mark store of T. */
279 static bool
280 mark_store (gimple stmt, tree t, tree, void *data)
282 t = get_base_address (t);
283 if (t && TREE_CODE (t) == VAR_DECL
284 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
286 varpool_node *vnode = varpool_node::get_create (t);
288 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
290 return false;
293 /* Record all references from cgraph_node that are taken in statement STMT. */
295 void
296 cgraph_node::record_stmt_references (gimple stmt)
298 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
299 mark_address);
302 /* Create cgraph edges for function calls.
303 Also look for functions and variables having addresses taken. */
305 namespace {
307 const pass_data pass_data_build_cgraph_edges =
309 GIMPLE_PASS, /* type */
310 "*build_cgraph_edges", /* name */
311 OPTGROUP_NONE, /* optinfo_flags */
312 TV_NONE, /* tv_id */
313 PROP_cfg, /* properties_required */
314 0, /* properties_provided */
315 0, /* properties_destroyed */
316 0, /* todo_flags_start */
317 0, /* todo_flags_finish */
320 class pass_build_cgraph_edges : public gimple_opt_pass
322 public:
323 pass_build_cgraph_edges (gcc::context *ctxt)
324 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
327 /* opt_pass methods: */
328 virtual unsigned int execute (function *);
330 }; // class pass_build_cgraph_edges
332 unsigned int
333 pass_build_cgraph_edges::execute (function *fun)
335 basic_block bb;
336 cgraph_node *node = cgraph_node::get (current_function_decl);
337 gimple_stmt_iterator gsi;
338 tree decl;
339 unsigned ix;
341 /* Create the callgraph edges and record the nodes referenced by the function.
342 body. */
343 FOR_EACH_BB_FN (bb, fun)
345 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
347 gimple stmt = gsi_stmt (gsi);
348 tree decl;
350 if (is_gimple_debug (stmt))
351 continue;
353 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
355 int freq = compute_call_stmt_bb_frequency (current_function_decl,
356 bb);
357 decl = gimple_call_fndecl (call_stmt);
358 if (decl)
359 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
360 else if (gimple_call_internal_p (call_stmt))
362 else
363 node->create_indirect_edge (call_stmt,
364 gimple_call_flags (call_stmt),
365 bb->count, freq);
367 node->record_stmt_references (stmt);
368 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
370 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
371 node->create_reference (cgraph_node::get_create (fn),
372 IPA_REF_ADDR, stmt);
374 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
376 tree fn = gimple_omp_task_child_fn (stmt);
377 if (fn)
378 node->create_reference (cgraph_node::get_create (fn),
379 IPA_REF_ADDR, stmt);
380 fn = gimple_omp_task_copy_fn (stmt);
381 if (fn)
382 node->create_reference (cgraph_node::get_create (fn),
383 IPA_REF_ADDR, stmt);
386 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
387 node->record_stmt_references (gsi_stmt (gsi));
390 /* Look for initializers of constant variables and private statics. */
391 FOR_EACH_LOCAL_DECL (fun, ix, decl)
392 if (TREE_CODE (decl) == VAR_DECL
393 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
394 && !DECL_HAS_VALUE_EXPR_P (decl))
395 varpool_node::finalize_decl (decl);
396 record_eh_tables (node, fun);
398 return 0;
401 } // anon namespace
403 gimple_opt_pass *
404 make_pass_build_cgraph_edges (gcc::context *ctxt)
406 return new pass_build_cgraph_edges (ctxt);
409 /* Record references to functions and other variables present in the
410 initial value of DECL, a variable.
411 When ONLY_VARS is true, we mark needed only variables, not functions. */
413 void
414 record_references_in_initializer (tree decl, bool only_vars)
416 varpool_node *node = varpool_node::get_create (decl);
417 hash_set<tree> visited_nodes;
418 record_reference_ctx ctx = {false, NULL};
420 ctx.varpool_node = node;
421 ctx.only_vars = only_vars;
422 walk_tree (&DECL_INITIAL (decl), record_reference,
423 &ctx, &visited_nodes);
426 /* Rebuild cgraph edges for current function node. This needs to be run after
427 passes that don't update the cgraph. */
429 unsigned int
430 cgraph_edge::rebuild_edges (void)
432 basic_block bb;
433 cgraph_node *node = cgraph_node::get (current_function_decl);
434 gimple_stmt_iterator gsi;
436 node->remove_callees ();
437 node->remove_all_references ();
439 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
441 FOR_EACH_BB_FN (bb, cfun)
443 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
445 gimple stmt = gsi_stmt (gsi);
446 tree decl;
448 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
450 int freq = compute_call_stmt_bb_frequency (current_function_decl,
451 bb);
452 decl = gimple_call_fndecl (call_stmt);
453 if (decl)
454 node->create_edge (cgraph_node::get_create (decl), call_stmt,
455 bb->count, freq);
456 else if (gimple_call_internal_p (call_stmt))
458 else
459 node->create_indirect_edge (call_stmt,
460 gimple_call_flags (call_stmt),
461 bb->count, freq);
463 node->record_stmt_references (stmt);
465 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
466 node->record_stmt_references (gsi_stmt (gsi));
468 record_eh_tables (node, cfun);
469 gcc_assert (!node->global.inlined_to);
471 if (node->instrumented_version
472 && !node->instrumentation_clone)
473 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
475 return 0;
478 /* Rebuild cgraph references for current function node. This needs to be run
479 after passes that don't update the cgraph. */
481 void
482 cgraph_edge::rebuild_references (void)
484 basic_block bb;
485 cgraph_node *node = cgraph_node::get (current_function_decl);
486 gimple_stmt_iterator gsi;
487 ipa_ref *ref = NULL;
488 int i;
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 ();
494 else
495 i++;
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);
508 if (node->instrumented_version
509 && !node->instrumentation_clone)
510 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
513 namespace {
515 const pass_data pass_data_rebuild_cgraph_edges =
517 GIMPLE_PASS, /* type */
518 "*rebuild_cgraph_edges", /* name */
519 OPTGROUP_NONE, /* optinfo_flags */
520 TV_CGRAPH, /* tv_id */
521 PROP_cfg, /* properties_required */
522 0, /* properties_provided */
523 0, /* properties_destroyed */
524 0, /* todo_flags_start */
525 0, /* todo_flags_finish */
528 class pass_rebuild_cgraph_edges : public gimple_opt_pass
530 public:
531 pass_rebuild_cgraph_edges (gcc::context *ctxt)
532 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
535 /* opt_pass methods: */
536 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
537 virtual unsigned int execute (function *)
539 return cgraph_edge::rebuild_edges ();
542 }; // class pass_rebuild_cgraph_edges
544 } // anon namespace
546 gimple_opt_pass *
547 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
549 return new pass_rebuild_cgraph_edges (ctxt);
553 namespace {
555 const pass_data pass_data_remove_cgraph_callee_edges =
557 GIMPLE_PASS, /* type */
558 "*remove_cgraph_callee_edges", /* name */
559 OPTGROUP_NONE, /* optinfo_flags */
560 TV_NONE, /* tv_id */
561 0, /* properties_required */
562 0, /* properties_provided */
563 0, /* properties_destroyed */
564 0, /* todo_flags_start */
565 0, /* todo_flags_finish */
568 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
570 public:
571 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
572 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
575 /* opt_pass methods: */
576 opt_pass * clone () {
577 return new pass_remove_cgraph_callee_edges (m_ctxt);
579 virtual unsigned int execute (function *);
581 }; // class pass_remove_cgraph_callee_edges
583 unsigned int
584 pass_remove_cgraph_callee_edges::execute (function *)
586 cgraph_node *node = cgraph_node::get (current_function_decl);
587 node->remove_callees ();
588 node->remove_all_references ();
589 return 0;
592 } // anon namespace
594 gimple_opt_pass *
595 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
597 return new pass_remove_cgraph_callee_edges (ctxt);