Concretize gimple_label_label
[official-gcc.git] / gcc / cgraphbuild.c
bloba97e397338d1c0e22a4e42cc543a800574179af7
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 "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-fold.h"
30 #include "gimple-expr.h"
31 #include "is-a.h"
32 #include "gimple.h"
33 #include "gimple-iterator.h"
34 #include "gimple-walk.h"
35 #include "langhooks.h"
36 #include "intl.h"
37 #include "tree-pass.h"
38 #include "ipa-utils.h"
39 #include "except.h"
40 #include "ipa-inline.h"
42 /* Context of record_reference. */
43 struct record_reference_ctx
45 bool only_vars;
46 class varpool_node *varpool_node;
49 /* Walk tree and record all calls and references to functions/variables.
50 Called via walk_tree: TP is pointer to tree to be examined.
51 When DATA is non-null, record references to callgraph.
54 static tree
55 record_reference (tree *tp, int *walk_subtrees, void *data)
57 tree t = *tp;
58 tree decl;
59 record_reference_ctx *ctx = (record_reference_ctx *)data;
61 t = canonicalize_constructor_val (t, NULL);
62 if (!t)
63 t = *tp;
64 else if (t != *tp)
65 *tp = t;
67 switch (TREE_CODE (t))
69 case VAR_DECL:
70 case FUNCTION_DECL:
71 gcc_unreachable ();
72 break;
74 case FDESC_EXPR:
75 case ADDR_EXPR:
76 /* Record dereferences to the functions. This makes the
77 functions reachable unconditionally. */
78 decl = get_base_var (*tp);
79 if (TREE_CODE (decl) == FUNCTION_DECL)
81 cgraph_node *node = cgraph_node::get_create (decl);
82 if (!ctx->only_vars)
83 node->mark_address_taken ();
84 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
87 if (TREE_CODE (decl) == VAR_DECL)
89 varpool_node *vnode = varpool_node::get_create (decl);
90 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
92 *walk_subtrees = 0;
93 break;
95 default:
96 /* Save some cycles by not walking types and declaration as we
97 won't find anything useful there anyway. */
98 if (IS_TYPE_OR_DECL_P (*tp))
100 *walk_subtrees = 0;
101 break;
103 break;
106 return NULL_TREE;
109 /* Record references to typeinfos in the type list LIST. */
111 static void
112 record_type_list (cgraph_node *node, tree list)
114 for (; list; list = TREE_CHAIN (list))
116 tree type = TREE_VALUE (list);
118 if (TYPE_P (type))
119 type = lookup_type_for_runtime (type);
120 STRIP_NOPS (type);
121 if (TREE_CODE (type) == ADDR_EXPR)
123 type = TREE_OPERAND (type, 0);
124 if (TREE_CODE (type) == VAR_DECL)
126 varpool_node *vnode = varpool_node::get_create (type);
127 node->create_reference (vnode, IPA_REF_ADDR);
133 /* Record all references we will introduce by producing EH tables
134 for NODE. */
136 static void
137 record_eh_tables (cgraph_node *node, function *fun)
139 eh_region i;
141 if (DECL_FUNCTION_PERSONALITY (node->decl))
143 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
144 cgraph_node *per_node = cgraph_node::get_create (per_decl);
146 node->create_reference (per_node, IPA_REF_ADDR);
147 per_node->mark_address_taken ();
150 i = fun->eh->region_tree;
151 if (!i)
152 return;
154 while (1)
156 switch (i->type)
158 case ERT_CLEANUP:
159 case ERT_MUST_NOT_THROW:
160 break;
162 case ERT_TRY:
164 eh_catch c;
165 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
166 record_type_list (node, c->type_list);
168 break;
170 case ERT_ALLOWED_EXCEPTIONS:
171 record_type_list (node, i->u.allowed.type_list);
172 break;
174 /* If there are sub-regions, process them. */
175 if (i->inner)
176 i = i->inner;
177 /* If there are peers, process them. */
178 else if (i->next_peer)
179 i = i->next_peer;
180 /* Otherwise, step back up the tree to the next peer. */
181 else
185 i = i->outer;
186 if (i == NULL)
187 return;
189 while (i->next_peer == NULL);
190 i = i->next_peer;
195 /* Computes the frequency of the call statement so that it can be stored in
196 cgraph_edge. BB is the basic block of the call statement. */
198 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
200 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
201 (DECL_STRUCT_FUNCTION (decl))->frequency;
202 int freq = bb->frequency;
204 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
205 return CGRAPH_FREQ_BASE;
207 if (!entry_freq)
208 entry_freq = 1, freq++;
210 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
211 if (freq > CGRAPH_FREQ_MAX)
212 freq = CGRAPH_FREQ_MAX;
214 return freq;
217 /* Mark address taken in STMT. */
219 static bool
220 mark_address (gimple stmt, tree addr, tree, void *data)
222 addr = get_base_address (addr);
223 if (TREE_CODE (addr) == FUNCTION_DECL)
225 cgraph_node *node = cgraph_node::get_create (addr);
226 node->mark_address_taken ();
227 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
229 else if (addr && TREE_CODE (addr) == VAR_DECL
230 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
232 varpool_node *vnode = varpool_node::get_create (addr);
234 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
237 return false;
240 /* Mark load of T. */
242 static bool
243 mark_load (gimple stmt, tree t, tree, void *data)
245 t = get_base_address (t);
246 if (t && TREE_CODE (t) == FUNCTION_DECL)
248 /* ??? This can happen on platforms with descriptors when these are
249 directly manipulated in the code. Pretend that it's an address. */
250 cgraph_node *node = cgraph_node::get_create (t);
251 node->mark_address_taken ();
252 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
254 else if (t && TREE_CODE (t) == VAR_DECL
255 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
257 varpool_node *vnode = varpool_node::get_create (t);
259 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
261 return false;
264 /* Mark store of T. */
266 static bool
267 mark_store (gimple stmt, tree t, tree, void *data)
269 t = get_base_address (t);
270 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_STORE, stmt);
277 return false;
280 /* Record all references from cgraph_node that are taken in statement STMT. */
282 void
283 cgraph_node::record_stmt_references (gimple stmt)
285 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
286 mark_address);
289 /* Create cgraph edges for function calls.
290 Also look for functions and variables having addresses taken. */
292 namespace {
294 const pass_data pass_data_build_cgraph_edges =
296 GIMPLE_PASS, /* type */
297 "*build_cgraph_edges", /* name */
298 OPTGROUP_NONE, /* optinfo_flags */
299 TV_NONE, /* tv_id */
300 PROP_cfg, /* properties_required */
301 0, /* properties_provided */
302 0, /* properties_destroyed */
303 0, /* todo_flags_start */
304 0, /* todo_flags_finish */
307 class pass_build_cgraph_edges : public gimple_opt_pass
309 public:
310 pass_build_cgraph_edges (gcc::context *ctxt)
311 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
314 /* opt_pass methods: */
315 virtual unsigned int execute (function *);
317 }; // class pass_build_cgraph_edges
319 unsigned int
320 pass_build_cgraph_edges::execute (function *fun)
322 basic_block bb;
323 cgraph_node *node = cgraph_node::get (current_function_decl);
324 gimple_stmt_iterator gsi;
325 tree decl;
326 unsigned ix;
328 /* Create the callgraph edges and record the nodes referenced by the function.
329 body. */
330 FOR_EACH_BB_FN (bb, fun)
332 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
334 gimple stmt = gsi_stmt (gsi);
335 tree decl;
337 if (is_gimple_debug (stmt))
338 continue;
340 if (gimple_call call_stmt = dyn_cast <gimple_call> (stmt))
342 int freq = compute_call_stmt_bb_frequency (current_function_decl,
343 bb);
344 decl = gimple_call_fndecl (call_stmt);
345 if (decl)
346 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
347 else if (gimple_call_internal_p (call_stmt))
349 else
350 node->create_indirect_edge (call_stmt,
351 gimple_call_flags (call_stmt),
352 bb->count, freq);
354 node->record_stmt_references (stmt);
355 if (gimple_omp_parallel omp_par_stmt =
356 dyn_cast <gimple_omp_parallel> (stmt))
358 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
359 node->create_reference (cgraph_node::get_create (fn),
360 IPA_REF_ADDR, stmt);
362 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
364 tree fn = gimple_omp_task_child_fn (stmt);
365 if (fn)
366 node->create_reference (cgraph_node::get_create (fn),
367 IPA_REF_ADDR, stmt);
368 fn = gimple_omp_task_copy_fn (stmt);
369 if (fn)
370 node->create_reference (cgraph_node::get_create (fn),
371 IPA_REF_ADDR, stmt);
374 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
375 node->record_stmt_references (gsi_stmt (gsi));
378 /* Look for initializers of constant variables and private statics. */
379 FOR_EACH_LOCAL_DECL (fun, ix, decl)
380 if (TREE_CODE (decl) == VAR_DECL
381 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
382 && !DECL_HAS_VALUE_EXPR_P (decl))
383 varpool_node::finalize_decl (decl);
384 record_eh_tables (node, fun);
386 return 0;
389 } // anon namespace
391 gimple_opt_pass *
392 make_pass_build_cgraph_edges (gcc::context *ctxt)
394 return new pass_build_cgraph_edges (ctxt);
397 /* Record references to functions and other variables present in the
398 initial value of DECL, a variable.
399 When ONLY_VARS is true, we mark needed only variables, not functions. */
401 void
402 record_references_in_initializer (tree decl, bool only_vars)
404 varpool_node *node = varpool_node::get_create (decl);
405 hash_set<tree> visited_nodes;
406 record_reference_ctx ctx = {false, NULL};
408 ctx.varpool_node = node;
409 ctx.only_vars = only_vars;
410 walk_tree (&DECL_INITIAL (decl), record_reference,
411 &ctx, &visited_nodes);
414 /* Rebuild cgraph edges for current function node. This needs to be run after
415 passes that don't update the cgraph. */
417 unsigned int
418 cgraph_edge::rebuild_edges (void)
420 basic_block bb;
421 cgraph_node *node = cgraph_node::get (current_function_decl);
422 gimple_stmt_iterator gsi;
424 node->remove_callees ();
425 node->remove_all_references ();
427 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
429 FOR_EACH_BB_FN (bb, cfun)
431 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
433 gimple stmt = gsi_stmt (gsi);
434 tree decl;
436 if (gimple_call call_stmt = dyn_cast <gimple_call> (stmt))
438 int freq = compute_call_stmt_bb_frequency (current_function_decl,
439 bb);
440 decl = gimple_call_fndecl (call_stmt);
441 if (decl)
442 node->create_edge (cgraph_node::get_create (decl), call_stmt,
443 bb->count, freq);
444 else if (gimple_call_internal_p (call_stmt))
446 else
447 node->create_indirect_edge (call_stmt,
448 gimple_call_flags (call_stmt),
449 bb->count, freq);
451 node->record_stmt_references (stmt);
453 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
454 node->record_stmt_references (gsi_stmt (gsi));
456 record_eh_tables (node, cfun);
457 gcc_assert (!node->global.inlined_to);
459 return 0;
462 /* Rebuild cgraph references for current function node. This needs to be run
463 after passes that don't update the cgraph. */
465 void
466 cgraph_edge::rebuild_references (void)
468 basic_block bb;
469 cgraph_node *node = cgraph_node::get (current_function_decl);
470 gimple_stmt_iterator gsi;
471 ipa_ref *ref = NULL;
472 int i;
474 /* Keep speculative references for further cgraph edge expansion. */
475 for (i = 0; node->iterate_reference (i, ref);)
476 if (!ref->speculative)
477 ref->remove_reference ();
478 else
479 i++;
481 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
483 FOR_EACH_BB_FN (bb, cfun)
485 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
486 node->record_stmt_references (gsi_stmt (gsi));
487 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
488 node->record_stmt_references (gsi_stmt (gsi));
490 record_eh_tables (node, cfun);
493 namespace {
495 const pass_data pass_data_rebuild_cgraph_edges =
497 GIMPLE_PASS, /* type */
498 "*rebuild_cgraph_edges", /* name */
499 OPTGROUP_NONE, /* optinfo_flags */
500 TV_CGRAPH, /* tv_id */
501 PROP_cfg, /* properties_required */
502 0, /* properties_provided */
503 0, /* properties_destroyed */
504 0, /* todo_flags_start */
505 0, /* todo_flags_finish */
508 class pass_rebuild_cgraph_edges : public gimple_opt_pass
510 public:
511 pass_rebuild_cgraph_edges (gcc::context *ctxt)
512 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
515 /* opt_pass methods: */
516 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
517 virtual unsigned int execute (function *)
519 return cgraph_edge::rebuild_edges ();
522 }; // class pass_rebuild_cgraph_edges
524 } // anon namespace
526 gimple_opt_pass *
527 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
529 return new pass_rebuild_cgraph_edges (ctxt);
533 namespace {
535 const pass_data pass_data_remove_cgraph_callee_edges =
537 GIMPLE_PASS, /* type */
538 "*remove_cgraph_callee_edges", /* name */
539 OPTGROUP_NONE, /* optinfo_flags */
540 TV_NONE, /* tv_id */
541 0, /* properties_required */
542 0, /* properties_provided */
543 0, /* properties_destroyed */
544 0, /* todo_flags_start */
545 0, /* todo_flags_finish */
548 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
550 public:
551 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
552 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
555 /* opt_pass methods: */
556 opt_pass * clone () {
557 return new pass_remove_cgraph_callee_edges (m_ctxt);
559 virtual unsigned int execute (function *);
561 }; // class pass_remove_cgraph_callee_edges
563 unsigned int
564 pass_remove_cgraph_callee_edges::execute (function *)
566 cgraph_node *node = cgraph_node::get (current_function_decl);
567 node->remove_callees ();
568 node->remove_all_references ();
569 return 0;
572 } // anon namespace
574 gimple_opt_pass *
575 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
577 return new pass_remove_cgraph_callee_edges (ctxt);