* cgraph.h: Flatten. Remove all include files.
[official-gcc.git] / gcc / cgraphbuild.c
blob51ec01bf0329e5c3c53b1c90899db45b81fed67b
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_OMP_PARALLEL
372 && gimple_omp_parallel_child_fn (stmt))
374 tree fn = gimple_omp_parallel_child_fn (stmt);
375 node->create_reference (cgraph_node::get_create (fn),
376 IPA_REF_ADDR, stmt);
378 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
380 tree fn = gimple_omp_task_child_fn (stmt);
381 if (fn)
382 node->create_reference (cgraph_node::get_create (fn),
383 IPA_REF_ADDR, stmt);
384 fn = gimple_omp_task_copy_fn (stmt);
385 if (fn)
386 node->create_reference (cgraph_node::get_create (fn),
387 IPA_REF_ADDR, stmt);
390 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
391 node->record_stmt_references (gsi_stmt (gsi));
394 /* Look for initializers of constant variables and private statics. */
395 FOR_EACH_LOCAL_DECL (fun, ix, decl)
396 if (TREE_CODE (decl) == VAR_DECL
397 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
398 && !DECL_HAS_VALUE_EXPR_P (decl))
399 varpool_node::finalize_decl (decl);
400 record_eh_tables (node, fun);
402 return 0;
405 } // anon namespace
407 gimple_opt_pass *
408 make_pass_build_cgraph_edges (gcc::context *ctxt)
410 return new pass_build_cgraph_edges (ctxt);
413 /* Record references to functions and other variables present in the
414 initial value of DECL, a variable.
415 When ONLY_VARS is true, we mark needed only variables, not functions. */
417 void
418 record_references_in_initializer (tree decl, bool only_vars)
420 varpool_node *node = varpool_node::get_create (decl);
421 hash_set<tree> visited_nodes;
422 record_reference_ctx ctx = {false, NULL};
424 ctx.varpool_node = node;
425 ctx.only_vars = only_vars;
426 walk_tree (&DECL_INITIAL (decl), record_reference,
427 &ctx, &visited_nodes);
430 /* Rebuild cgraph edges for current function node. This needs to be run after
431 passes that don't update the cgraph. */
433 unsigned int
434 cgraph_edge::rebuild_edges (void)
436 basic_block bb;
437 cgraph_node *node = cgraph_node::get (current_function_decl);
438 gimple_stmt_iterator gsi;
440 node->remove_callees ();
441 node->remove_all_references ();
443 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
445 FOR_EACH_BB_FN (bb, cfun)
447 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
449 gimple stmt = gsi_stmt (gsi);
450 tree decl;
452 if (is_gimple_call (stmt))
454 int freq = compute_call_stmt_bb_frequency (current_function_decl,
455 bb);
456 decl = gimple_call_fndecl (stmt);
457 if (decl)
458 node->create_edge (cgraph_node::get_create (decl), stmt,
459 bb->count, freq);
460 else if (gimple_call_internal_p (stmt))
462 else
463 node->create_indirect_edge (stmt,
464 gimple_call_flags (stmt),
465 bb->count, freq);
467 node->record_stmt_references (stmt);
469 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
470 node->record_stmt_references (gsi_stmt (gsi));
472 record_eh_tables (node, cfun);
473 gcc_assert (!node->global.inlined_to);
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);
509 namespace {
511 const pass_data pass_data_rebuild_cgraph_edges =
513 GIMPLE_PASS, /* type */
514 "*rebuild_cgraph_edges", /* name */
515 OPTGROUP_NONE, /* optinfo_flags */
516 TV_CGRAPH, /* tv_id */
517 PROP_cfg, /* properties_required */
518 0, /* properties_provided */
519 0, /* properties_destroyed */
520 0, /* todo_flags_start */
521 0, /* todo_flags_finish */
524 class pass_rebuild_cgraph_edges : public gimple_opt_pass
526 public:
527 pass_rebuild_cgraph_edges (gcc::context *ctxt)
528 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
531 /* opt_pass methods: */
532 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
533 virtual unsigned int execute (function *)
535 return cgraph_edge::rebuild_edges ();
538 }; // class pass_rebuild_cgraph_edges
540 } // anon namespace
542 gimple_opt_pass *
543 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
545 return new pass_rebuild_cgraph_edges (ctxt);
549 namespace {
551 const pass_data pass_data_remove_cgraph_callee_edges =
553 GIMPLE_PASS, /* type */
554 "*remove_cgraph_callee_edges", /* name */
555 OPTGROUP_NONE, /* optinfo_flags */
556 TV_NONE, /* tv_id */
557 0, /* properties_required */
558 0, /* properties_provided */
559 0, /* properties_destroyed */
560 0, /* todo_flags_start */
561 0, /* todo_flags_finish */
564 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
566 public:
567 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
568 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
571 /* opt_pass methods: */
572 opt_pass * clone () {
573 return new pass_remove_cgraph_callee_edges (m_ctxt);
575 virtual unsigned int execute (function *);
577 }; // class pass_remove_cgraph_callee_edges
579 unsigned int
580 pass_remove_cgraph_callee_edges::execute (function *)
582 cgraph_node *node = cgraph_node::get (current_function_decl);
583 node->remove_callees ();
584 node->remove_all_references ();
585 return 0;
588 } // anon namespace
590 gimple_opt_pass *
591 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
593 return new pass_remove_cgraph_callee_edges (ctxt);