svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / cgraphbuild.c
blob6a46bc7c75b2e6aae30586fe43a33060b726e8d7
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 "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "machmode.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 "hash-map.h"
49 #include "plugin-api.h"
50 #include "ipa-ref.h"
51 #include "cgraph.h"
52 #include "ipa-utils.h"
53 #include "except.h"
54 #include "alloc-pool.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 (is_gimple_call (stmt))
358 int freq = compute_call_stmt_bb_frequency (current_function_decl,
359 bb);
360 decl = gimple_call_fndecl (stmt);
361 if (decl)
362 node->create_edge (cgraph_node::get_create (decl), stmt, bb->count, freq);
363 else if (gimple_call_internal_p (stmt))
365 else
366 node->create_indirect_edge (stmt,
367 gimple_call_flags (stmt),
368 bb->count, freq);
370 node->record_stmt_references (stmt);
371 if (gimple_code (stmt) == GIMPLE_OACC_PARALLEL
372 && gimple_oacc_parallel_child_fn (stmt))
374 tree fn = gimple_oacc_parallel_child_fn (stmt);
375 node->create_reference (cgraph_node::get_create (fn),
376 IPA_REF_ADDR, stmt);
378 else if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
379 && gimple_omp_parallel_child_fn (stmt))
381 tree fn = gimple_omp_parallel_child_fn (stmt);
382 node->create_reference (cgraph_node::get_create (fn),
383 IPA_REF_ADDR, stmt);
385 else if (gimple_code (stmt) == GIMPLE_OMP_TASK)
387 tree fn = gimple_omp_task_child_fn (stmt);
388 if (fn)
389 node->create_reference (cgraph_node::get_create (fn),
390 IPA_REF_ADDR, stmt);
391 fn = gimple_omp_task_copy_fn (stmt);
392 if (fn)
393 node->create_reference (cgraph_node::get_create (fn),
394 IPA_REF_ADDR, stmt);
397 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
398 node->record_stmt_references (gsi_stmt (gsi));
401 /* Look for initializers of constant variables and private statics. */
402 FOR_EACH_LOCAL_DECL (fun, ix, decl)
403 if (TREE_CODE (decl) == VAR_DECL
404 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
405 && !DECL_HAS_VALUE_EXPR_P (decl))
406 varpool_node::finalize_decl (decl);
407 record_eh_tables (node, fun);
409 return 0;
412 } // anon namespace
414 gimple_opt_pass *
415 make_pass_build_cgraph_edges (gcc::context *ctxt)
417 return new pass_build_cgraph_edges (ctxt);
420 /* Record references to functions and other variables present in the
421 initial value of DECL, a variable.
422 When ONLY_VARS is true, we mark needed only variables, not functions. */
424 void
425 record_references_in_initializer (tree decl, bool only_vars)
427 varpool_node *node = varpool_node::get_create (decl);
428 hash_set<tree> visited_nodes;
429 record_reference_ctx ctx = {false, NULL};
431 ctx.varpool_node = node;
432 ctx.only_vars = only_vars;
433 walk_tree (&DECL_INITIAL (decl), record_reference,
434 &ctx, &visited_nodes);
437 /* Rebuild cgraph edges for current function node. This needs to be run after
438 passes that don't update the cgraph. */
440 unsigned int
441 cgraph_edge::rebuild_edges (void)
443 basic_block bb;
444 cgraph_node *node = cgraph_node::get (current_function_decl);
445 gimple_stmt_iterator gsi;
447 node->remove_callees ();
448 node->remove_all_references ();
450 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
452 FOR_EACH_BB_FN (bb, cfun)
454 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
456 gimple stmt = gsi_stmt (gsi);
457 tree decl;
459 if (is_gimple_call (stmt))
461 int freq = compute_call_stmt_bb_frequency (current_function_decl,
462 bb);
463 decl = gimple_call_fndecl (stmt);
464 if (decl)
465 node->create_edge (cgraph_node::get_create (decl), stmt,
466 bb->count, freq);
467 else if (gimple_call_internal_p (stmt))
469 else
470 node->create_indirect_edge (stmt,
471 gimple_call_flags (stmt),
472 bb->count, freq);
474 node->record_stmt_references (stmt);
476 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
477 node->record_stmt_references (gsi_stmt (gsi));
479 record_eh_tables (node, cfun);
480 gcc_assert (!node->global.inlined_to);
482 return 0;
485 /* Rebuild cgraph references for current function node. This needs to be run
486 after passes that don't update the cgraph. */
488 void
489 cgraph_edge::rebuild_references (void)
491 basic_block bb;
492 cgraph_node *node = cgraph_node::get (current_function_decl);
493 gimple_stmt_iterator gsi;
494 ipa_ref *ref = NULL;
495 int i;
497 /* Keep speculative references for further cgraph edge expansion. */
498 for (i = 0; node->iterate_reference (i, ref);)
499 if (!ref->speculative)
500 ref->remove_reference ();
501 else
502 i++;
504 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
506 FOR_EACH_BB_FN (bb, cfun)
508 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
509 node->record_stmt_references (gsi_stmt (gsi));
510 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
511 node->record_stmt_references (gsi_stmt (gsi));
513 record_eh_tables (node, cfun);
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);