[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / cgraphbuild.c
blob33b01be83acfdd456514aaaf9cbb66ed1664ca86
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 "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "hard-reg-set.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "internal-fn.h"
31 #include "gimple-fold.h"
32 #include "gimple-iterator.h"
33 #include "gimple-walk.h"
34 #include "langhooks.h"
35 #include "intl.h"
36 #include "tree-pass.h"
37 #include "cgraph.h"
38 #include "ipa-utils.h"
39 #include "except.h"
40 #include "alloc-pool.h"
41 #include "symbol-summary.h"
42 #include "ipa-prop.h"
43 #include "ipa-inline.h"
45 /* Context of record_reference. */
46 struct record_reference_ctx
48 bool only_vars;
49 class varpool_node *varpool_node;
52 /* Walk tree and record all calls and references to functions/variables.
53 Called via walk_tree: TP is pointer to tree to be examined.
54 When DATA is non-null, record references to callgraph.
57 static tree
58 record_reference (tree *tp, int *walk_subtrees, void *data)
60 tree t = *tp;
61 tree decl;
62 record_reference_ctx *ctx = (record_reference_ctx *)data;
64 t = canonicalize_constructor_val (t, NULL);
65 if (!t)
66 t = *tp;
67 else if (t != *tp)
68 *tp = t;
70 switch (TREE_CODE (t))
72 case VAR_DECL:
73 case FUNCTION_DECL:
74 gcc_unreachable ();
75 break;
77 case FDESC_EXPR:
78 case ADDR_EXPR:
79 /* Record dereferences to the functions. This makes the
80 functions reachable unconditionally. */
81 decl = get_base_var (*tp);
82 if (TREE_CODE (decl) == FUNCTION_DECL)
84 cgraph_node *node = cgraph_node::get_create (decl);
85 if (!ctx->only_vars)
86 node->mark_address_taken ();
87 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
90 if (TREE_CODE (decl) == VAR_DECL)
92 varpool_node *vnode = varpool_node::get_create (decl);
93 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
95 *walk_subtrees = 0;
96 break;
98 default:
99 /* Save some cycles by not walking types and declaration as we
100 won't find anything useful there anyway. */
101 if (IS_TYPE_OR_DECL_P (*tp))
103 *walk_subtrees = 0;
104 break;
106 break;
109 return NULL_TREE;
112 /* Record references to typeinfos in the type list LIST. */
114 static void
115 record_type_list (cgraph_node *node, tree list)
117 for (; list; list = TREE_CHAIN (list))
119 tree type = TREE_VALUE (list);
121 if (TYPE_P (type))
122 type = lookup_type_for_runtime (type);
123 STRIP_NOPS (type);
124 if (TREE_CODE (type) == ADDR_EXPR)
126 type = TREE_OPERAND (type, 0);
127 if (TREE_CODE (type) == VAR_DECL)
129 varpool_node *vnode = varpool_node::get_create (type);
130 node->create_reference (vnode, IPA_REF_ADDR);
136 /* Record all references we will introduce by producing EH tables
137 for NODE. */
139 static void
140 record_eh_tables (cgraph_node *node, function *fun)
142 eh_region i;
144 if (DECL_FUNCTION_PERSONALITY (node->decl))
146 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
147 cgraph_node *per_node = cgraph_node::get_create (per_decl);
149 node->create_reference (per_node, IPA_REF_ADDR);
150 per_node->mark_address_taken ();
153 i = fun->eh->region_tree;
154 if (!i)
155 return;
157 while (1)
159 switch (i->type)
161 case ERT_CLEANUP:
162 case ERT_MUST_NOT_THROW:
163 break;
165 case ERT_TRY:
167 eh_catch c;
168 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
169 record_type_list (node, c->type_list);
171 break;
173 case ERT_ALLOWED_EXCEPTIONS:
174 record_type_list (node, i->u.allowed.type_list);
175 break;
177 /* If there are sub-regions, process them. */
178 if (i->inner)
179 i = i->inner;
180 /* If there are peers, process them. */
181 else if (i->next_peer)
182 i = i->next_peer;
183 /* Otherwise, step back up the tree to the next peer. */
184 else
188 i = i->outer;
189 if (i == NULL)
190 return;
192 while (i->next_peer == NULL);
193 i = i->next_peer;
198 /* Computes the frequency of the call statement so that it can be stored in
199 cgraph_edge. BB is the basic block of the call statement. */
201 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
203 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
204 (DECL_STRUCT_FUNCTION (decl))->frequency;
205 int freq = bb->frequency;
207 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
208 return CGRAPH_FREQ_BASE;
210 if (!entry_freq)
211 entry_freq = 1, freq++;
213 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
214 if (freq > CGRAPH_FREQ_MAX)
215 freq = CGRAPH_FREQ_MAX;
217 return freq;
220 /* Mark address taken in STMT. */
222 static bool
223 mark_address (gimple *stmt, tree addr, tree, void *data)
225 addr = get_base_address (addr);
226 if (TREE_CODE (addr) == FUNCTION_DECL)
228 cgraph_node *node = cgraph_node::get_create (addr);
229 node->mark_address_taken ();
230 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
232 else if (addr && TREE_CODE (addr) == VAR_DECL
233 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
235 varpool_node *vnode = varpool_node::get_create (addr);
237 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
240 return false;
243 /* Mark load of T. */
245 static bool
246 mark_load (gimple *stmt, tree t, tree, void *data)
248 t = get_base_address (t);
249 if (t && TREE_CODE (t) == FUNCTION_DECL)
251 /* ??? This can happen on platforms with descriptors when these are
252 directly manipulated in the code. Pretend that it's an address. */
253 cgraph_node *node = cgraph_node::get_create (t);
254 node->mark_address_taken ();
255 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
257 else if (t && TREE_CODE (t) == VAR_DECL
258 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
260 varpool_node *vnode = varpool_node::get_create (t);
262 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
264 return false;
267 /* Mark store of T. */
269 static bool
270 mark_store (gimple *stmt, tree t, tree, void *data)
272 t = get_base_address (t);
273 if (t && TREE_CODE (t) == VAR_DECL
274 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
276 varpool_node *vnode = varpool_node::get_create (t);
278 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
280 return false;
283 /* Record all references from cgraph_node that are taken in statement STMT. */
285 void
286 cgraph_node::record_stmt_references (gimple *stmt)
288 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
289 mark_address);
292 /* Create cgraph edges for function calls.
293 Also look for functions and variables having addresses taken. */
295 namespace {
297 const pass_data pass_data_build_cgraph_edges =
299 GIMPLE_PASS, /* type */
300 "*build_cgraph_edges", /* name */
301 OPTGROUP_NONE, /* optinfo_flags */
302 TV_NONE, /* tv_id */
303 PROP_cfg, /* properties_required */
304 0, /* properties_provided */
305 0, /* properties_destroyed */
306 0, /* todo_flags_start */
307 0, /* todo_flags_finish */
310 class pass_build_cgraph_edges : public gimple_opt_pass
312 public:
313 pass_build_cgraph_edges (gcc::context *ctxt)
314 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
317 /* opt_pass methods: */
318 virtual unsigned int execute (function *);
320 }; // class pass_build_cgraph_edges
322 unsigned int
323 pass_build_cgraph_edges::execute (function *fun)
325 basic_block bb;
326 cgraph_node *node = cgraph_node::get (current_function_decl);
327 gimple_stmt_iterator gsi;
328 tree decl;
329 unsigned ix;
331 /* Create the callgraph edges and record the nodes referenced by the function.
332 body. */
333 FOR_EACH_BB_FN (bb, fun)
335 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
337 gimple *stmt = gsi_stmt (gsi);
338 tree decl;
340 if (is_gimple_debug (stmt))
341 continue;
343 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
345 int freq = compute_call_stmt_bb_frequency (current_function_decl,
346 bb);
347 decl = gimple_call_fndecl (call_stmt);
348 if (decl)
349 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
350 else if (gimple_call_internal_p (call_stmt))
352 else
353 node->create_indirect_edge (call_stmt,
354 gimple_call_flags (call_stmt),
355 bb->count, freq);
357 node->record_stmt_references (stmt);
358 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
360 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
361 node->create_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->create_reference (cgraph_node::get_create (fn),
369 IPA_REF_ADDR, stmt);
370 fn = gimple_omp_task_copy_fn (stmt);
371 if (fn)
372 node->create_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 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 cgraph_edge::rebuild_edges (void)
422 basic_block bb;
423 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 (gcall *call_stmt = dyn_cast <gcall *> (stmt))
440 int freq = compute_call_stmt_bb_frequency (current_function_decl,
441 bb);
442 decl = gimple_call_fndecl (call_stmt);
443 if (decl)
444 node->create_edge (cgraph_node::get_create (decl), call_stmt,
445 bb->count, freq);
446 else if (gimple_call_internal_p (call_stmt))
448 else
449 node->create_indirect_edge (call_stmt,
450 gimple_call_flags (call_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 if (node->instrumented_version
462 && !node->instrumentation_clone)
463 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
465 return 0;
468 /* Rebuild cgraph references for current function node. This needs to be run
469 after passes that don't update the cgraph. */
471 void
472 cgraph_edge::rebuild_references (void)
474 basic_block bb;
475 cgraph_node *node = cgraph_node::get (current_function_decl);
476 gimple_stmt_iterator gsi;
477 ipa_ref *ref = NULL;
478 int i;
480 /* Keep speculative references for further cgraph edge expansion. */
481 for (i = 0; node->iterate_reference (i, ref);)
482 if (!ref->speculative)
483 ref->remove_reference ();
484 else
485 i++;
487 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
489 FOR_EACH_BB_FN (bb, cfun)
491 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
492 node->record_stmt_references (gsi_stmt (gsi));
493 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
494 node->record_stmt_references (gsi_stmt (gsi));
496 record_eh_tables (node, cfun);
498 if (node->instrumented_version
499 && !node->instrumentation_clone)
500 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
503 namespace {
505 const pass_data pass_data_rebuild_cgraph_edges =
507 GIMPLE_PASS, /* type */
508 "*rebuild_cgraph_edges", /* name */
509 OPTGROUP_NONE, /* optinfo_flags */
510 TV_CGRAPH, /* tv_id */
511 PROP_cfg, /* properties_required */
512 0, /* properties_provided */
513 0, /* properties_destroyed */
514 0, /* todo_flags_start */
515 0, /* todo_flags_finish */
518 class pass_rebuild_cgraph_edges : public gimple_opt_pass
520 public:
521 pass_rebuild_cgraph_edges (gcc::context *ctxt)
522 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
525 /* opt_pass methods: */
526 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
527 virtual unsigned int execute (function *)
529 return cgraph_edge::rebuild_edges ();
532 }; // class pass_rebuild_cgraph_edges
534 } // anon namespace
536 gimple_opt_pass *
537 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
539 return new pass_rebuild_cgraph_edges (ctxt);
543 namespace {
545 const pass_data pass_data_remove_cgraph_callee_edges =
547 GIMPLE_PASS, /* type */
548 "*remove_cgraph_callee_edges", /* name */
549 OPTGROUP_NONE, /* optinfo_flags */
550 TV_NONE, /* tv_id */
551 0, /* properties_required */
552 0, /* properties_provided */
553 0, /* properties_destroyed */
554 0, /* todo_flags_start */
555 0, /* todo_flags_finish */
558 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
560 public:
561 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
562 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
565 /* opt_pass methods: */
566 opt_pass * clone () {
567 return new pass_remove_cgraph_callee_edges (m_ctxt);
569 virtual unsigned int execute (function *);
571 }; // class pass_remove_cgraph_callee_edges
573 unsigned int
574 pass_remove_cgraph_callee_edges::execute (function *)
576 cgraph_node *node = cgraph_node::get (current_function_decl);
577 node->remove_callees ();
578 node->remove_all_references ();
579 return 0;
582 } // anon namespace
584 gimple_opt_pass *
585 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
587 return new pass_remove_cgraph_callee_edges (ctxt);