2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cgraphbuild.c
blob5282bdc3bc865a16c558171a87bc6a1b95c601d9
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "predict.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-fold.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "langhooks.h"
46 #include "intl.h"
47 #include "tree-pass.h"
48 #include "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "ipa-utils.h"
52 #include "except.h"
53 #include "alloc-pool.h"
54 #include "symbol-summary.h"
55 #include "ipa-prop.h"
56 #include "ipa-inline.h"
58 /* Context of record_reference. */
59 struct record_reference_ctx
61 bool only_vars;
62 class varpool_node *varpool_node;
65 /* Walk tree and record all calls and references to functions/variables.
66 Called via walk_tree: TP is pointer to tree to be examined.
67 When DATA is non-null, record references to callgraph.
70 static tree
71 record_reference (tree *tp, int *walk_subtrees, void *data)
73 tree t = *tp;
74 tree decl;
75 record_reference_ctx *ctx = (record_reference_ctx *)data;
77 t = canonicalize_constructor_val (t, NULL);
78 if (!t)
79 t = *tp;
80 else if (t != *tp)
81 *tp = t;
83 switch (TREE_CODE (t))
85 case VAR_DECL:
86 case FUNCTION_DECL:
87 gcc_unreachable ();
88 break;
90 case FDESC_EXPR:
91 case ADDR_EXPR:
92 /* Record dereferences to the functions. This makes the
93 functions reachable unconditionally. */
94 decl = get_base_var (*tp);
95 if (TREE_CODE (decl) == FUNCTION_DECL)
97 cgraph_node *node = cgraph_node::get_create (decl);
98 if (!ctx->only_vars)
99 node->mark_address_taken ();
100 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
103 if (TREE_CODE (decl) == VAR_DECL)
105 varpool_node *vnode = varpool_node::get_create (decl);
106 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
108 *walk_subtrees = 0;
109 break;
111 default:
112 /* Save some cycles by not walking types and declaration as we
113 won't find anything useful there anyway. */
114 if (IS_TYPE_OR_DECL_P (*tp))
116 *walk_subtrees = 0;
117 break;
119 break;
122 return NULL_TREE;
125 /* Record references to typeinfos in the type list LIST. */
127 static void
128 record_type_list (cgraph_node *node, tree list)
130 for (; list; list = TREE_CHAIN (list))
132 tree type = TREE_VALUE (list);
134 if (TYPE_P (type))
135 type = lookup_type_for_runtime (type);
136 STRIP_NOPS (type);
137 if (TREE_CODE (type) == ADDR_EXPR)
139 type = TREE_OPERAND (type, 0);
140 if (TREE_CODE (type) == VAR_DECL)
142 varpool_node *vnode = varpool_node::get_create (type);
143 node->create_reference (vnode, IPA_REF_ADDR);
149 /* Record all references we will introduce by producing EH tables
150 for NODE. */
152 static void
153 record_eh_tables (cgraph_node *node, function *fun)
155 eh_region i;
157 if (DECL_FUNCTION_PERSONALITY (node->decl))
159 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
160 cgraph_node *per_node = cgraph_node::get_create (per_decl);
162 node->create_reference (per_node, IPA_REF_ADDR);
163 per_node->mark_address_taken ();
166 i = fun->eh->region_tree;
167 if (!i)
168 return;
170 while (1)
172 switch (i->type)
174 case ERT_CLEANUP:
175 case ERT_MUST_NOT_THROW:
176 break;
178 case ERT_TRY:
180 eh_catch c;
181 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
182 record_type_list (node, c->type_list);
184 break;
186 case ERT_ALLOWED_EXCEPTIONS:
187 record_type_list (node, i->u.allowed.type_list);
188 break;
190 /* If there are sub-regions, process them. */
191 if (i->inner)
192 i = i->inner;
193 /* If there are peers, process them. */
194 else if (i->next_peer)
195 i = i->next_peer;
196 /* Otherwise, step back up the tree to the next peer. */
197 else
201 i = i->outer;
202 if (i == NULL)
203 return;
205 while (i->next_peer == NULL);
206 i = i->next_peer;
211 /* Computes the frequency of the call statement so that it can be stored in
212 cgraph_edge. BB is the basic block of the call statement. */
214 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
216 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
217 (DECL_STRUCT_FUNCTION (decl))->frequency;
218 int freq = bb->frequency;
220 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
221 return CGRAPH_FREQ_BASE;
223 if (!entry_freq)
224 entry_freq = 1, freq++;
226 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
227 if (freq > CGRAPH_FREQ_MAX)
228 freq = CGRAPH_FREQ_MAX;
230 return freq;
233 /* Mark address taken in STMT. */
235 static bool
236 mark_address (gimple stmt, tree addr, tree, void *data)
238 addr = get_base_address (addr);
239 if (TREE_CODE (addr) == FUNCTION_DECL)
241 cgraph_node *node = cgraph_node::get_create (addr);
242 node->mark_address_taken ();
243 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
245 else if (addr && TREE_CODE (addr) == VAR_DECL
246 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
248 varpool_node *vnode = varpool_node::get_create (addr);
250 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
253 return false;
256 /* Mark load of T. */
258 static bool
259 mark_load (gimple stmt, tree t, tree, void *data)
261 t = get_base_address (t);
262 if (t && TREE_CODE (t) == FUNCTION_DECL)
264 /* ??? This can happen on platforms with descriptors when these are
265 directly manipulated in the code. Pretend that it's an address. */
266 cgraph_node *node = cgraph_node::get_create (t);
267 node->mark_address_taken ();
268 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
270 else if (t && TREE_CODE (t) == VAR_DECL
271 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
273 varpool_node *vnode = varpool_node::get_create (t);
275 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
277 return false;
280 /* Mark store of T. */
282 static bool
283 mark_store (gimple stmt, tree t, tree, void *data)
285 t = get_base_address (t);
286 if (t && TREE_CODE (t) == VAR_DECL
287 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
289 varpool_node *vnode = varpool_node::get_create (t);
291 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
293 return false;
296 /* Record all references from cgraph_node that are taken in statement STMT. */
298 void
299 cgraph_node::record_stmt_references (gimple stmt)
301 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
302 mark_address);
305 /* Create cgraph edges for function calls.
306 Also look for functions and variables having addresses taken. */
308 namespace {
310 const pass_data pass_data_build_cgraph_edges =
312 GIMPLE_PASS, /* type */
313 "*build_cgraph_edges", /* name */
314 OPTGROUP_NONE, /* optinfo_flags */
315 TV_NONE, /* tv_id */
316 PROP_cfg, /* properties_required */
317 0, /* properties_provided */
318 0, /* properties_destroyed */
319 0, /* todo_flags_start */
320 0, /* todo_flags_finish */
323 class pass_build_cgraph_edges : public gimple_opt_pass
325 public:
326 pass_build_cgraph_edges (gcc::context *ctxt)
327 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
330 /* opt_pass methods: */
331 virtual unsigned int execute (function *);
333 }; // class pass_build_cgraph_edges
335 unsigned int
336 pass_build_cgraph_edges::execute (function *fun)
338 basic_block bb;
339 cgraph_node *node = cgraph_node::get (current_function_decl);
340 gimple_stmt_iterator gsi;
341 tree decl;
342 unsigned ix;
344 /* Create the callgraph edges and record the nodes referenced by the function.
345 body. */
346 FOR_EACH_BB_FN (bb, fun)
348 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
350 gimple stmt = gsi_stmt (gsi);
351 tree decl;
353 if (is_gimple_debug (stmt))
354 continue;
356 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
358 int freq = compute_call_stmt_bb_frequency (current_function_decl,
359 bb);
360 decl = gimple_call_fndecl (call_stmt);
361 if (decl)
362 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
363 else if (gimple_call_internal_p (call_stmt))
365 else
366 node->create_indirect_edge (call_stmt,
367 gimple_call_flags (call_stmt),
368 bb->count, freq);
370 node->record_stmt_references (stmt);
371 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
373 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
374 node->create_reference (cgraph_node::get_create (fn),
375 IPA_REF_ADDR, stmt);
377 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
379 tree fn = gimple_omp_task_child_fn (stmt);
380 if (fn)
381 node->create_reference (cgraph_node::get_create (fn),
382 IPA_REF_ADDR, stmt);
383 fn = gimple_omp_task_copy_fn (stmt);
384 if (fn)
385 node->create_reference (cgraph_node::get_create (fn),
386 IPA_REF_ADDR, stmt);
389 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
390 node->record_stmt_references (gsi_stmt (gsi));
393 /* Look for initializers of constant variables and private statics. */
394 FOR_EACH_LOCAL_DECL (fun, ix, decl)
395 if (TREE_CODE (decl) == VAR_DECL
396 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
397 && !DECL_HAS_VALUE_EXPR_P (decl))
398 varpool_node::finalize_decl (decl);
399 record_eh_tables (node, fun);
401 return 0;
404 } // anon namespace
406 gimple_opt_pass *
407 make_pass_build_cgraph_edges (gcc::context *ctxt)
409 return new pass_build_cgraph_edges (ctxt);
412 /* Record references to functions and other variables present in the
413 initial value of DECL, a variable.
414 When ONLY_VARS is true, we mark needed only variables, not functions. */
416 void
417 record_references_in_initializer (tree decl, bool only_vars)
419 varpool_node *node = varpool_node::get_create (decl);
420 hash_set<tree> visited_nodes;
421 record_reference_ctx ctx = {false, NULL};
423 ctx.varpool_node = node;
424 ctx.only_vars = only_vars;
425 walk_tree (&DECL_INITIAL (decl), record_reference,
426 &ctx, &visited_nodes);
429 /* Rebuild cgraph edges for current function node. This needs to be run after
430 passes that don't update the cgraph. */
432 unsigned int
433 cgraph_edge::rebuild_edges (void)
435 basic_block bb;
436 cgraph_node *node = cgraph_node::get (current_function_decl);
437 gimple_stmt_iterator gsi;
439 node->remove_callees ();
440 node->remove_all_references ();
442 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
444 FOR_EACH_BB_FN (bb, cfun)
446 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
448 gimple stmt = gsi_stmt (gsi);
449 tree decl;
451 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
453 int freq = compute_call_stmt_bb_frequency (current_function_decl,
454 bb);
455 decl = gimple_call_fndecl (call_stmt);
456 if (decl)
457 node->create_edge (cgraph_node::get_create (decl), call_stmt,
458 bb->count, freq);
459 else if (gimple_call_internal_p (call_stmt))
461 else
462 node->create_indirect_edge (call_stmt,
463 gimple_call_flags (call_stmt),
464 bb->count, freq);
466 node->record_stmt_references (stmt);
468 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
469 node->record_stmt_references (gsi_stmt (gsi));
471 record_eh_tables (node, cfun);
472 gcc_assert (!node->global.inlined_to);
474 if (node->instrumented_version
475 && !node->instrumentation_clone)
476 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
478 return 0;
481 /* Rebuild cgraph references for current function node. This needs to be run
482 after passes that don't update the cgraph. */
484 void
485 cgraph_edge::rebuild_references (void)
487 basic_block bb;
488 cgraph_node *node = cgraph_node::get (current_function_decl);
489 gimple_stmt_iterator gsi;
490 ipa_ref *ref = NULL;
491 int i;
493 /* Keep speculative references for further cgraph edge expansion. */
494 for (i = 0; node->iterate_reference (i, ref);)
495 if (!ref->speculative)
496 ref->remove_reference ();
497 else
498 i++;
500 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
502 FOR_EACH_BB_FN (bb, cfun)
504 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
505 node->record_stmt_references (gsi_stmt (gsi));
506 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
507 node->record_stmt_references (gsi_stmt (gsi));
509 record_eh_tables (node, cfun);
511 if (node->instrumented_version
512 && !node->instrumentation_clone)
513 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
516 namespace {
518 const pass_data pass_data_rebuild_cgraph_edges =
520 GIMPLE_PASS, /* type */
521 "*rebuild_cgraph_edges", /* name */
522 OPTGROUP_NONE, /* optinfo_flags */
523 TV_CGRAPH, /* tv_id */
524 PROP_cfg, /* properties_required */
525 0, /* properties_provided */
526 0, /* properties_destroyed */
527 0, /* todo_flags_start */
528 0, /* todo_flags_finish */
531 class pass_rebuild_cgraph_edges : public gimple_opt_pass
533 public:
534 pass_rebuild_cgraph_edges (gcc::context *ctxt)
535 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
538 /* opt_pass methods: */
539 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
540 virtual unsigned int execute (function *)
542 return cgraph_edge::rebuild_edges ();
545 }; // class pass_rebuild_cgraph_edges
547 } // anon namespace
549 gimple_opt_pass *
550 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
552 return new pass_rebuild_cgraph_edges (ctxt);
556 namespace {
558 const pass_data pass_data_remove_cgraph_callee_edges =
560 GIMPLE_PASS, /* type */
561 "*remove_cgraph_callee_edges", /* name */
562 OPTGROUP_NONE, /* optinfo_flags */
563 TV_NONE, /* tv_id */
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
573 public:
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 virtual unsigned int execute (function *);
584 }; // class pass_remove_cgraph_callee_edges
586 unsigned int
587 pass_remove_cgraph_callee_edges::execute (function *)
589 cgraph_node *node = cgraph_node::get (current_function_decl);
590 node->remove_callees ();
591 node->remove_all_references ();
592 return 0;
595 } // anon namespace
597 gimple_opt_pass *
598 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
600 return new pass_remove_cgraph_callee_edges (ctxt);