Daily bump.
[official-gcc.git] / gcc / cgraphbuild.c
blobf2f04c34ca0333dcdb8fff53826e37f0fa688fc1
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "predict.h"
37 #include "hard-reg-set.h"
38 #include "input.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-fold.h"
46 #include "gimple-expr.h"
47 #include "is-a.h"
48 #include "gimple.h"
49 #include "gimple-iterator.h"
50 #include "gimple-walk.h"
51 #include "langhooks.h"
52 #include "intl.h"
53 #include "tree-pass.h"
54 #include "hash-map.h"
55 #include "plugin-api.h"
56 #include "ipa-ref.h"
57 #include "cgraph.h"
58 #include "ipa-utils.h"
59 #include "except.h"
60 #include "alloc-pool.h"
61 #include "symbol-summary.h"
62 #include "ipa-prop.h"
63 #include "ipa-inline.h"
65 /* Context of record_reference. */
66 struct record_reference_ctx
68 bool only_vars;
69 class varpool_node *varpool_node;
72 /* Walk tree and record all calls and references to functions/variables.
73 Called via walk_tree: TP is pointer to tree to be examined.
74 When DATA is non-null, record references to callgraph.
77 static tree
78 record_reference (tree *tp, int *walk_subtrees, void *data)
80 tree t = *tp;
81 tree decl;
82 record_reference_ctx *ctx = (record_reference_ctx *)data;
84 t = canonicalize_constructor_val (t, NULL);
85 if (!t)
86 t = *tp;
87 else if (t != *tp)
88 *tp = t;
90 switch (TREE_CODE (t))
92 case VAR_DECL:
93 case FUNCTION_DECL:
94 gcc_unreachable ();
95 break;
97 case FDESC_EXPR:
98 case ADDR_EXPR:
99 /* Record dereferences to the functions. This makes the
100 functions reachable unconditionally. */
101 decl = get_base_var (*tp);
102 if (TREE_CODE (decl) == FUNCTION_DECL)
104 cgraph_node *node = cgraph_node::get_create (decl);
105 if (!ctx->only_vars)
106 node->mark_address_taken ();
107 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
110 if (TREE_CODE (decl) == VAR_DECL)
112 varpool_node *vnode = varpool_node::get_create (decl);
113 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
115 *walk_subtrees = 0;
116 break;
118 default:
119 /* Save some cycles by not walking types and declaration as we
120 won't find anything useful there anyway. */
121 if (IS_TYPE_OR_DECL_P (*tp))
123 *walk_subtrees = 0;
124 break;
126 break;
129 return NULL_TREE;
132 /* Record references to typeinfos in the type list LIST. */
134 static void
135 record_type_list (cgraph_node *node, tree list)
137 for (; list; list = TREE_CHAIN (list))
139 tree type = TREE_VALUE (list);
141 if (TYPE_P (type))
142 type = lookup_type_for_runtime (type);
143 STRIP_NOPS (type);
144 if (TREE_CODE (type) == ADDR_EXPR)
146 type = TREE_OPERAND (type, 0);
147 if (TREE_CODE (type) == VAR_DECL)
149 varpool_node *vnode = varpool_node::get_create (type);
150 node->create_reference (vnode, IPA_REF_ADDR);
156 /* Record all references we will introduce by producing EH tables
157 for NODE. */
159 static void
160 record_eh_tables (cgraph_node *node, function *fun)
162 eh_region i;
164 if (DECL_FUNCTION_PERSONALITY (node->decl))
166 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
167 cgraph_node *per_node = cgraph_node::get_create (per_decl);
169 node->create_reference (per_node, IPA_REF_ADDR);
170 per_node->mark_address_taken ();
173 i = fun->eh->region_tree;
174 if (!i)
175 return;
177 while (1)
179 switch (i->type)
181 case ERT_CLEANUP:
182 case ERT_MUST_NOT_THROW:
183 break;
185 case ERT_TRY:
187 eh_catch c;
188 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
189 record_type_list (node, c->type_list);
191 break;
193 case ERT_ALLOWED_EXCEPTIONS:
194 record_type_list (node, i->u.allowed.type_list);
195 break;
197 /* If there are sub-regions, process them. */
198 if (i->inner)
199 i = i->inner;
200 /* If there are peers, process them. */
201 else if (i->next_peer)
202 i = i->next_peer;
203 /* Otherwise, step back up the tree to the next peer. */
204 else
208 i = i->outer;
209 if (i == NULL)
210 return;
212 while (i->next_peer == NULL);
213 i = i->next_peer;
218 /* Computes the frequency of the call statement so that it can be stored in
219 cgraph_edge. BB is the basic block of the call statement. */
221 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
223 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
224 (DECL_STRUCT_FUNCTION (decl))->frequency;
225 int freq = bb->frequency;
227 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
228 return CGRAPH_FREQ_BASE;
230 if (!entry_freq)
231 entry_freq = 1, freq++;
233 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
234 if (freq > CGRAPH_FREQ_MAX)
235 freq = CGRAPH_FREQ_MAX;
237 return freq;
240 /* Mark address taken in STMT. */
242 static bool
243 mark_address (gimple stmt, tree addr, tree, void *data)
245 addr = get_base_address (addr);
246 if (TREE_CODE (addr) == FUNCTION_DECL)
248 cgraph_node *node = cgraph_node::get_create (addr);
249 node->mark_address_taken ();
250 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
252 else if (addr && TREE_CODE (addr) == VAR_DECL
253 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
255 varpool_node *vnode = varpool_node::get_create (addr);
257 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
260 return false;
263 /* Mark load of T. */
265 static bool
266 mark_load (gimple stmt, tree t, tree, void *data)
268 t = get_base_address (t);
269 if (t && TREE_CODE (t) == FUNCTION_DECL)
271 /* ??? This can happen on platforms with descriptors when these are
272 directly manipulated in the code. Pretend that it's an address. */
273 cgraph_node *node = cgraph_node::get_create (t);
274 node->mark_address_taken ();
275 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
277 else if (t && TREE_CODE (t) == VAR_DECL
278 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
280 varpool_node *vnode = varpool_node::get_create (t);
282 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
284 return false;
287 /* Mark store of T. */
289 static bool
290 mark_store (gimple stmt, tree t, tree, void *data)
292 t = get_base_address (t);
293 if (t && TREE_CODE (t) == VAR_DECL
294 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
296 varpool_node *vnode = varpool_node::get_create (t);
298 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
300 return false;
303 /* Record all references from cgraph_node that are taken in statement STMT. */
305 void
306 cgraph_node::record_stmt_references (gimple stmt)
308 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
309 mark_address);
312 /* Create cgraph edges for function calls.
313 Also look for functions and variables having addresses taken. */
315 namespace {
317 const pass_data pass_data_build_cgraph_edges =
319 GIMPLE_PASS, /* type */
320 "*build_cgraph_edges", /* name */
321 OPTGROUP_NONE, /* optinfo_flags */
322 TV_NONE, /* tv_id */
323 PROP_cfg, /* properties_required */
324 0, /* properties_provided */
325 0, /* properties_destroyed */
326 0, /* todo_flags_start */
327 0, /* todo_flags_finish */
330 class pass_build_cgraph_edges : public gimple_opt_pass
332 public:
333 pass_build_cgraph_edges (gcc::context *ctxt)
334 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
337 /* opt_pass methods: */
338 virtual unsigned int execute (function *);
340 }; // class pass_build_cgraph_edges
342 unsigned int
343 pass_build_cgraph_edges::execute (function *fun)
345 basic_block bb;
346 cgraph_node *node = cgraph_node::get (current_function_decl);
347 gimple_stmt_iterator gsi;
348 tree decl;
349 unsigned ix;
351 /* Create the callgraph edges and record the nodes referenced by the function.
352 body. */
353 FOR_EACH_BB_FN (bb, fun)
355 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
357 gimple stmt = gsi_stmt (gsi);
358 tree decl;
360 if (is_gimple_debug (stmt))
361 continue;
363 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
365 int freq = compute_call_stmt_bb_frequency (current_function_decl,
366 bb);
367 decl = gimple_call_fndecl (call_stmt);
368 if (decl)
369 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
370 else if (gimple_call_internal_p (call_stmt))
372 else
373 node->create_indirect_edge (call_stmt,
374 gimple_call_flags (call_stmt),
375 bb->count, freq);
377 node->record_stmt_references (stmt);
378 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
380 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
381 node->create_reference (cgraph_node::get_create (fn),
382 IPA_REF_ADDR, stmt);
384 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
386 tree fn = gimple_omp_task_child_fn (stmt);
387 if (fn)
388 node->create_reference (cgraph_node::get_create (fn),
389 IPA_REF_ADDR, stmt);
390 fn = gimple_omp_task_copy_fn (stmt);
391 if (fn)
392 node->create_reference (cgraph_node::get_create (fn),
393 IPA_REF_ADDR, stmt);
396 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
397 node->record_stmt_references (gsi_stmt (gsi));
400 /* Look for initializers of constant variables and private statics. */
401 FOR_EACH_LOCAL_DECL (fun, ix, decl)
402 if (TREE_CODE (decl) == VAR_DECL
403 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
404 && !DECL_HAS_VALUE_EXPR_P (decl))
405 varpool_node::finalize_decl (decl);
406 record_eh_tables (node, fun);
408 return 0;
411 } // anon namespace
413 gimple_opt_pass *
414 make_pass_build_cgraph_edges (gcc::context *ctxt)
416 return new pass_build_cgraph_edges (ctxt);
419 /* Record references to functions and other variables present in the
420 initial value of DECL, a variable.
421 When ONLY_VARS is true, we mark needed only variables, not functions. */
423 void
424 record_references_in_initializer (tree decl, bool only_vars)
426 varpool_node *node = varpool_node::get_create (decl);
427 hash_set<tree> visited_nodes;
428 record_reference_ctx ctx = {false, NULL};
430 ctx.varpool_node = node;
431 ctx.only_vars = only_vars;
432 walk_tree (&DECL_INITIAL (decl), record_reference,
433 &ctx, &visited_nodes);
436 /* Rebuild cgraph edges for current function node. This needs to be run after
437 passes that don't update the cgraph. */
439 unsigned int
440 cgraph_edge::rebuild_edges (void)
442 basic_block bb;
443 cgraph_node *node = cgraph_node::get (current_function_decl);
444 gimple_stmt_iterator gsi;
446 node->remove_callees ();
447 node->remove_all_references ();
449 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
451 FOR_EACH_BB_FN (bb, cfun)
453 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
455 gimple stmt = gsi_stmt (gsi);
456 tree decl;
458 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
460 int freq = compute_call_stmt_bb_frequency (current_function_decl,
461 bb);
462 decl = gimple_call_fndecl (call_stmt);
463 if (decl)
464 node->create_edge (cgraph_node::get_create (decl), call_stmt,
465 bb->count, freq);
466 else if (gimple_call_internal_p (call_stmt))
468 else
469 node->create_indirect_edge (call_stmt,
470 gimple_call_flags (call_stmt),
471 bb->count, freq);
473 node->record_stmt_references (stmt);
475 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
476 node->record_stmt_references (gsi_stmt (gsi));
478 record_eh_tables (node, cfun);
479 gcc_assert (!node->global.inlined_to);
481 if (node->instrumented_version
482 && !node->instrumentation_clone)
483 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
485 return 0;
488 /* Rebuild cgraph references for current function node. This needs to be run
489 after passes that don't update the cgraph. */
491 void
492 cgraph_edge::rebuild_references (void)
494 basic_block bb;
495 cgraph_node *node = cgraph_node::get (current_function_decl);
496 gimple_stmt_iterator gsi;
497 ipa_ref *ref = NULL;
498 int i;
500 /* Keep speculative references for further cgraph edge expansion. */
501 for (i = 0; node->iterate_reference (i, ref);)
502 if (!ref->speculative)
503 ref->remove_reference ();
504 else
505 i++;
507 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
509 FOR_EACH_BB_FN (bb, cfun)
511 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
512 node->record_stmt_references (gsi_stmt (gsi));
513 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
514 node->record_stmt_references (gsi_stmt (gsi));
516 record_eh_tables (node, cfun);
518 if (node->instrumented_version
519 && !node->instrumentation_clone)
520 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
523 namespace {
525 const pass_data pass_data_rebuild_cgraph_edges =
527 GIMPLE_PASS, /* type */
528 "*rebuild_cgraph_edges", /* name */
529 OPTGROUP_NONE, /* optinfo_flags */
530 TV_CGRAPH, /* tv_id */
531 PROP_cfg, /* properties_required */
532 0, /* properties_provided */
533 0, /* properties_destroyed */
534 0, /* todo_flags_start */
535 0, /* todo_flags_finish */
538 class pass_rebuild_cgraph_edges : public gimple_opt_pass
540 public:
541 pass_rebuild_cgraph_edges (gcc::context *ctxt)
542 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
545 /* opt_pass methods: */
546 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
547 virtual unsigned int execute (function *)
549 return cgraph_edge::rebuild_edges ();
552 }; // class pass_rebuild_cgraph_edges
554 } // anon namespace
556 gimple_opt_pass *
557 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
559 return new pass_rebuild_cgraph_edges (ctxt);
563 namespace {
565 const pass_data pass_data_remove_cgraph_callee_edges =
567 GIMPLE_PASS, /* type */
568 "*remove_cgraph_callee_edges", /* name */
569 OPTGROUP_NONE, /* optinfo_flags */
570 TV_NONE, /* tv_id */
571 0, /* properties_required */
572 0, /* properties_provided */
573 0, /* properties_destroyed */
574 0, /* todo_flags_start */
575 0, /* todo_flags_finish */
578 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
580 public:
581 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
582 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
585 /* opt_pass methods: */
586 opt_pass * clone () {
587 return new pass_remove_cgraph_callee_edges (m_ctxt);
589 virtual unsigned int execute (function *);
591 }; // class pass_remove_cgraph_callee_edges
593 unsigned int
594 pass_remove_cgraph_callee_edges::execute (function *)
596 cgraph_node *node = cgraph_node::get (current_function_decl);
597 node->remove_callees ();
598 node->remove_all_references ();
599 return 0;
602 } // anon namespace
604 gimple_opt_pass *
605 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
607 return new pass_remove_cgraph_callee_edges (ctxt);