gcc/testsuite
[official-gcc.git] / gcc / cgraphbuild.c
bloba04958f4d8c0a17e89f86ad6506d912834e0c100
1 /* Callgraph construction.
2 Copyright (C) 2003-2014 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 "tree.h"
26 #include "pointer-set.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-fold.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimple-iterator.h"
35 #include "gimple-walk.h"
36 #include "langhooks.h"
37 #include "intl.h"
38 #include "tree-pass.h"
39 #include "ipa-utils.h"
40 #include "except.h"
41 #include "ipa-inline.h"
43 /* Context of record_reference. */
44 struct record_reference_ctx
46 bool only_vars;
47 class varpool_node *varpool_node;
50 /* Walk tree and record all calls and references to functions/variables.
51 Called via walk_tree: TP is pointer to tree to be examined.
52 When DATA is non-null, record references to callgraph.
55 static tree
56 record_reference (tree *tp, int *walk_subtrees, void *data)
58 tree t = *tp;
59 tree decl;
60 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
62 t = canonicalize_constructor_val (t, NULL);
63 if (!t)
64 t = *tp;
65 else if (t != *tp)
66 *tp = t;
68 switch (TREE_CODE (t))
70 case VAR_DECL:
71 case FUNCTION_DECL:
72 gcc_unreachable ();
73 break;
75 case FDESC_EXPR:
76 case ADDR_EXPR:
77 /* Record dereferences to the functions. This makes the
78 functions reachable unconditionally. */
79 decl = get_base_var (*tp);
80 if (TREE_CODE (decl) == FUNCTION_DECL)
82 struct cgraph_node *node = cgraph_node::get_create (decl);
83 if (!ctx->only_vars)
84 node->mark_address_taken ();
85 ctx->varpool_node->add_reference (node, IPA_REF_ADDR);
88 if (TREE_CODE (decl) == VAR_DECL)
90 varpool_node *vnode = varpool_node::get_create (decl);
91 ctx->varpool_node->add_reference (vnode, IPA_REF_ADDR);
93 *walk_subtrees = 0;
94 break;
96 default:
97 /* Save some cycles by not walking types and declaration as we
98 won't find anything useful there anyway. */
99 if (IS_TYPE_OR_DECL_P (*tp))
101 *walk_subtrees = 0;
102 break;
104 break;
107 return NULL_TREE;
110 /* Record references to typeinfos in the type list LIST. */
112 static void
113 record_type_list (struct cgraph_node *node, tree list)
115 for (; list; list = TREE_CHAIN (list))
117 tree type = TREE_VALUE (list);
119 if (TYPE_P (type))
120 type = lookup_type_for_runtime (type);
121 STRIP_NOPS (type);
122 if (TREE_CODE (type) == ADDR_EXPR)
124 type = TREE_OPERAND (type, 0);
125 if (TREE_CODE (type) == VAR_DECL)
127 varpool_node *vnode = varpool_node::get_create (type);
128 node->add_reference (vnode, IPA_REF_ADDR);
134 /* Record all references we will introduce by producing EH tables
135 for NODE. */
137 static void
138 record_eh_tables (struct cgraph_node *node, struct function *fun)
140 eh_region i;
142 if (DECL_FUNCTION_PERSONALITY (node->decl))
144 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
145 struct cgraph_node *per_node = cgraph_node::get_create (per_decl);
147 node->add_reference (per_node, IPA_REF_ADDR);
148 per_node->mark_address_taken ();
151 i = fun->eh->region_tree;
152 if (!i)
153 return;
155 while (1)
157 switch (i->type)
159 case ERT_CLEANUP:
160 case ERT_MUST_NOT_THROW:
161 break;
163 case ERT_TRY:
165 eh_catch c;
166 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
167 record_type_list (node, c->type_list);
169 break;
171 case ERT_ALLOWED_EXCEPTIONS:
172 record_type_list (node, i->u.allowed.type_list);
173 break;
175 /* If there are sub-regions, process them. */
176 if (i->inner)
177 i = i->inner;
178 /* If there are peers, process them. */
179 else if (i->next_peer)
180 i = i->next_peer;
181 /* Otherwise, step back up the tree to the next peer. */
182 else
186 i = i->outer;
187 if (i == NULL)
188 return;
190 while (i->next_peer == NULL);
191 i = i->next_peer;
196 /* Computes the frequency of the call statement so that it can be stored in
197 cgraph_edge. BB is the basic block of the call statement. */
199 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
201 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
202 (DECL_STRUCT_FUNCTION (decl))->frequency;
203 int freq = bb->frequency;
205 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
206 return CGRAPH_FREQ_BASE;
208 if (!entry_freq)
209 entry_freq = 1, freq++;
211 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
212 if (freq > CGRAPH_FREQ_MAX)
213 freq = CGRAPH_FREQ_MAX;
215 return freq;
218 /* Mark address taken in STMT. */
220 static bool
221 mark_address (gimple stmt, tree addr, tree, void *data)
223 addr = get_base_address (addr);
224 if (TREE_CODE (addr) == FUNCTION_DECL)
226 struct cgraph_node *node = cgraph_node::get_create (addr);
227 node->mark_address_taken ();
228 ((symtab_node *)data)->add_reference (node, IPA_REF_ADDR, stmt);
230 else if (addr && TREE_CODE (addr) == VAR_DECL
231 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
233 varpool_node *vnode = varpool_node::get_create (addr);
235 ((symtab_node *)data)->add_reference (vnode, IPA_REF_ADDR, stmt);
238 return false;
241 /* Mark load of T. */
243 static bool
244 mark_load (gimple stmt, tree t, tree, void *data)
246 t = get_base_address (t);
247 if (t && TREE_CODE (t) == FUNCTION_DECL)
249 /* ??? This can happen on platforms with descriptors when these are
250 directly manipulated in the code. Pretend that it's an address. */
251 struct cgraph_node *node = cgraph_node::get_create (t);
252 node->mark_address_taken ();
253 ((symtab_node *)data)->add_reference (node, IPA_REF_ADDR, stmt);
255 else if (t && TREE_CODE (t) == VAR_DECL
256 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
258 varpool_node *vnode = varpool_node::get_create (t);
260 ((symtab_node *)data)->add_reference (vnode, IPA_REF_LOAD, stmt);
262 return false;
265 /* Mark store of T. */
267 static bool
268 mark_store (gimple stmt, tree t, tree, void *data)
270 t = get_base_address (t);
271 if (t && TREE_CODE (t) == VAR_DECL
272 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
274 varpool_node *vnode = varpool_node::get_create (t);
276 ((symtab_node *)data)->add_reference (vnode, IPA_REF_STORE, stmt);
278 return false;
281 /* Record all references from cgraph_node that are taken in statement STMT. */
283 void
284 cgraph_node::record_stmt_references (gimple stmt)
286 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
287 mark_address);
290 /* Create cgraph edges for function calls.
291 Also look for functions and variables having addresses taken. */
293 namespace {
295 const pass_data pass_data_build_cgraph_edges =
297 GIMPLE_PASS, /* type */
298 "*build_cgraph_edges", /* name */
299 OPTGROUP_NONE, /* optinfo_flags */
300 TV_NONE, /* tv_id */
301 PROP_cfg, /* properties_required */
302 0, /* properties_provided */
303 0, /* properties_destroyed */
304 0, /* todo_flags_start */
305 0, /* todo_flags_finish */
308 class pass_build_cgraph_edges : public gimple_opt_pass
310 public:
311 pass_build_cgraph_edges (gcc::context *ctxt)
312 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
315 /* opt_pass methods: */
316 virtual unsigned int execute (function *);
318 }; // class pass_build_cgraph_edges
320 unsigned int
321 pass_build_cgraph_edges::execute (function *fun)
323 basic_block bb;
324 struct cgraph_node *node = cgraph_node::get (current_function_decl);
325 gimple_stmt_iterator gsi;
326 tree decl;
327 unsigned ix;
329 /* Create the callgraph edges and record the nodes referenced by the function.
330 body. */
331 FOR_EACH_BB_FN (bb, fun)
333 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
335 gimple stmt = gsi_stmt (gsi);
336 tree decl;
338 if (is_gimple_debug (stmt))
339 continue;
341 if (is_gimple_call (stmt))
343 int freq = compute_call_stmt_bb_frequency (current_function_decl,
344 bb);
345 decl = gimple_call_fndecl (stmt);
346 if (decl)
347 node->create_edge (cgraph_node::get_create (decl),
348 stmt, bb->count, freq);
349 else if (gimple_call_internal_p (stmt))
351 else
352 node->create_indirect_edge (stmt,
353 gimple_call_flags (stmt),
354 bb->count, freq);
356 node->record_stmt_references (stmt);
357 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
358 && gimple_omp_parallel_child_fn (stmt))
360 tree fn = gimple_omp_parallel_child_fn (stmt);
361 node->add_reference (cgraph_node::get_create (fn),
362 IPA_REF_ADDR, stmt);
364 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
366 tree fn = gimple_omp_task_child_fn (stmt);
367 if (fn)
368 node->add_reference (cgraph_node::get_create (fn),
369 IPA_REF_ADDR, stmt);
370 fn = gimple_omp_task_copy_fn (stmt);
371 if (fn)
372 node->add_reference (cgraph_node::get_create (fn),
373 IPA_REF_ADDR, stmt);
376 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
377 node->record_stmt_references (gsi_stmt (gsi));
380 /* Look for initializers of constant variables and private statics. */
381 FOR_EACH_LOCAL_DECL (fun, ix, decl)
382 if (TREE_CODE (decl) == VAR_DECL
383 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
384 && !DECL_HAS_VALUE_EXPR_P (decl))
385 varpool_node::finalize_decl (decl);
386 record_eh_tables (node, fun);
388 return 0;
391 } // anon namespace
393 gimple_opt_pass *
394 make_pass_build_cgraph_edges (gcc::context *ctxt)
396 return new pass_build_cgraph_edges (ctxt);
399 /* Record references to functions and other variables present in the
400 initial value of DECL, a variable.
401 When ONLY_VARS is true, we mark needed only variables, not functions. */
403 void
404 record_references_in_initializer (tree decl, bool only_vars)
406 varpool_node *node = varpool_node::get_create (decl);
407 hash_set<tree> visited_nodes;
408 struct record_reference_ctx ctx = {false, NULL};
410 ctx.varpool_node = node;
411 ctx.only_vars = only_vars;
412 walk_tree (&DECL_INITIAL (decl), record_reference,
413 &ctx, &visited_nodes);
416 /* Rebuild cgraph edges for current function node. This needs to be run after
417 passes that don't update the cgraph. */
419 unsigned int
420 rebuild_cgraph_edges (void)
422 basic_block bb;
423 struct cgraph_node *node = cgraph_node::get (current_function_decl);
424 gimple_stmt_iterator gsi;
426 node->remove_callees ();
427 node->remove_all_references ();
429 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
431 FOR_EACH_BB_FN (bb, cfun)
433 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
435 gimple stmt = gsi_stmt (gsi);
436 tree decl;
438 if (is_gimple_call (stmt))
440 int freq = compute_call_stmt_bb_frequency (current_function_decl,
441 bb);
442 decl = gimple_call_fndecl (stmt);
443 if (decl)
444 node->create_edge (cgraph_node::get_create (decl), stmt,
445 bb->count, freq);
446 else if (gimple_call_internal_p (stmt))
448 else
449 node->create_indirect_edge (stmt,
450 gimple_call_flags (stmt),
451 bb->count, freq);
453 node->record_stmt_references (stmt);
455 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
456 node->record_stmt_references (gsi_stmt (gsi));
458 record_eh_tables (node, cfun);
459 gcc_assert (!node->global.inlined_to);
461 return 0;
464 /* Rebuild cgraph edges for current function node. This needs to be run after
465 passes that don't update the cgraph. */
467 void
468 cgraph_rebuild_references (void)
470 basic_block bb;
471 struct cgraph_node *node = cgraph_node::get (current_function_decl);
472 gimple_stmt_iterator gsi;
473 struct ipa_ref *ref = NULL;
474 int i;
476 /* Keep speculative references for further cgraph edge expansion. */
477 for (i = 0; node->iterate_reference (i, ref);)
478 if (!ref->speculative)
479 ref->remove_reference ();
480 else
481 i++;
483 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
485 FOR_EACH_BB_FN (bb, cfun)
487 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
488 node->record_stmt_references (gsi_stmt (gsi));
489 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
490 node->record_stmt_references (gsi_stmt (gsi));
492 record_eh_tables (node, cfun);
495 namespace {
497 const pass_data pass_data_rebuild_cgraph_edges =
499 GIMPLE_PASS, /* type */
500 "*rebuild_cgraph_edges", /* name */
501 OPTGROUP_NONE, /* optinfo_flags */
502 TV_CGRAPH, /* tv_id */
503 PROP_cfg, /* properties_required */
504 0, /* properties_provided */
505 0, /* properties_destroyed */
506 0, /* todo_flags_start */
507 0, /* todo_flags_finish */
510 class pass_rebuild_cgraph_edges : public gimple_opt_pass
512 public:
513 pass_rebuild_cgraph_edges (gcc::context *ctxt)
514 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
517 /* opt_pass methods: */
518 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
519 virtual unsigned int execute (function *) { return rebuild_cgraph_edges (); }
521 }; // class pass_rebuild_cgraph_edges
523 } // anon namespace
525 gimple_opt_pass *
526 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
528 return new pass_rebuild_cgraph_edges (ctxt);
532 namespace {
534 const pass_data pass_data_remove_cgraph_callee_edges =
536 GIMPLE_PASS, /* type */
537 "*remove_cgraph_callee_edges", /* name */
538 OPTGROUP_NONE, /* optinfo_flags */
539 TV_NONE, /* tv_id */
540 0, /* properties_required */
541 0, /* properties_provided */
542 0, /* properties_destroyed */
543 0, /* todo_flags_start */
544 0, /* todo_flags_finish */
547 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
549 public:
550 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
551 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
554 /* opt_pass methods: */
555 opt_pass * clone () {
556 return new pass_remove_cgraph_callee_edges (m_ctxt);
558 virtual unsigned int execute (function *);
560 }; // class pass_remove_cgraph_callee_edges
562 unsigned int
563 pass_remove_cgraph_callee_edges::execute (function *)
565 struct cgraph_node *node = cgraph_node::get (current_function_decl);
566 node->remove_callees ();
567 node->remove_all_references ();
568 return 0;
571 } // anon namespace
573 gimple_opt_pass *
574 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
576 return new pass_remove_cgraph_callee_edges (ctxt);