PR rtl-optimization/82913
[official-gcc.git] / gcc / cgraphbuild.c
blobdd4bf9a7fa3bd35126fef36bc74b3f8b81970e4f
1 /* Callgraph construction.
2 Copyright (C) 2003-2017 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 "tree-pass.h"
28 #include "cgraph.h"
29 #include "gimple-fold.h"
30 #include "gimple-iterator.h"
31 #include "gimple-walk.h"
32 #include "ipa-utils.h"
33 #include "except.h"
35 /* Context of record_reference. */
36 struct record_reference_ctx
38 bool only_vars;
39 class varpool_node *varpool_node;
42 /* Walk tree and record all calls and references to functions/variables.
43 Called via walk_tree: TP is pointer to tree to be examined.
44 When DATA is non-null, record references to callgraph.
47 static tree
48 record_reference (tree *tp, int *walk_subtrees, void *data)
50 tree t = *tp;
51 tree decl;
52 record_reference_ctx *ctx = (record_reference_ctx *)data;
54 t = canonicalize_constructor_val (t, NULL);
55 if (!t)
56 t = *tp;
57 else if (t != *tp)
58 *tp = t;
60 switch (TREE_CODE (t))
62 case VAR_DECL:
63 case FUNCTION_DECL:
64 gcc_unreachable ();
65 break;
67 case FDESC_EXPR:
68 case ADDR_EXPR:
69 /* Record dereferences to the functions. This makes the
70 functions reachable unconditionally. */
71 decl = get_base_var (*tp);
72 if (TREE_CODE (decl) == FUNCTION_DECL)
74 cgraph_node *node = cgraph_node::get_create (decl);
75 if (!ctx->only_vars)
76 node->mark_address_taken ();
77 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
80 if (VAR_P (decl))
82 varpool_node *vnode = varpool_node::get_create (decl);
83 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
85 *walk_subtrees = 0;
86 break;
88 default:
89 /* Save some cycles by not walking types and declaration as we
90 won't find anything useful there anyway. */
91 if (IS_TYPE_OR_DECL_P (*tp))
93 *walk_subtrees = 0;
94 break;
96 break;
99 return NULL_TREE;
102 /* Record references to typeinfos in the type list LIST. */
104 static void
105 record_type_list (cgraph_node *node, tree list)
107 for (; list; list = TREE_CHAIN (list))
109 tree type = TREE_VALUE (list);
111 if (TYPE_P (type))
112 type = lookup_type_for_runtime (type);
113 STRIP_NOPS (type);
114 if (TREE_CODE (type) == ADDR_EXPR)
116 type = TREE_OPERAND (type, 0);
117 if (VAR_P (type))
119 varpool_node *vnode = varpool_node::get_create (type);
120 node->create_reference (vnode, IPA_REF_ADDR);
126 /* Record all references we will introduce by producing EH tables
127 for NODE. */
129 static void
130 record_eh_tables (cgraph_node *node, function *fun)
132 eh_region i;
134 if (DECL_FUNCTION_PERSONALITY (node->decl))
136 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
137 cgraph_node *per_node = cgraph_node::get_create (per_decl);
139 node->create_reference (per_node, IPA_REF_ADDR);
140 per_node->mark_address_taken ();
143 i = fun->eh->region_tree;
144 if (!i)
145 return;
147 while (1)
149 switch (i->type)
151 case ERT_CLEANUP:
152 case ERT_MUST_NOT_THROW:
153 break;
155 case ERT_TRY:
157 eh_catch c;
158 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
159 record_type_list (node, c->type_list);
161 break;
163 case ERT_ALLOWED_EXCEPTIONS:
164 record_type_list (node, i->u.allowed.type_list);
165 break;
167 /* If there are sub-regions, process them. */
168 if (i->inner)
169 i = i->inner;
170 /* If there are peers, process them. */
171 else if (i->next_peer)
172 i = i->next_peer;
173 /* Otherwise, step back up the tree to the next peer. */
174 else
178 i = i->outer;
179 if (i == NULL)
180 return;
182 while (i->next_peer == NULL);
183 i = i->next_peer;
188 /* Computes the frequency of the call statement so that it can be stored in
189 cgraph_edge. BB is the basic block of the call statement. */
191 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
193 return bb->count.to_cgraph_frequency
194 (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (decl))->count);
197 /* Mark address taken in STMT. */
199 static bool
200 mark_address (gimple *stmt, tree addr, tree, void *data)
202 addr = get_base_address (addr);
203 if (TREE_CODE (addr) == FUNCTION_DECL)
205 cgraph_node *node = cgraph_node::get_create (addr);
206 node->mark_address_taken ();
207 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
209 else if (addr && VAR_P (addr)
210 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
212 varpool_node *vnode = varpool_node::get_create (addr);
214 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
217 return false;
220 /* Mark load of T. */
222 static bool
223 mark_load (gimple *stmt, tree t, tree, void *data)
225 t = get_base_address (t);
226 if (t && TREE_CODE (t) == FUNCTION_DECL)
228 /* ??? This can happen on platforms with descriptors when these are
229 directly manipulated in the code. Pretend that it's an address. */
230 cgraph_node *node = cgraph_node::get_create (t);
231 node->mark_address_taken ();
232 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
234 else if (t && VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
236 varpool_node *vnode = varpool_node::get_create (t);
238 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
240 return false;
243 /* Mark store of T. */
245 static bool
246 mark_store (gimple *stmt, tree t, tree, void *data)
248 t = get_base_address (t);
249 if (t && VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
251 varpool_node *vnode = varpool_node::get_create (t);
253 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
255 return false;
258 /* Record all references from cgraph_node that are taken in statement STMT. */
260 void
261 cgraph_node::record_stmt_references (gimple *stmt)
263 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
264 mark_address);
267 /* Create cgraph edges for function calls.
268 Also look for functions and variables having addresses taken. */
270 namespace {
272 const pass_data pass_data_build_cgraph_edges =
274 GIMPLE_PASS, /* type */
275 "*build_cgraph_edges", /* name */
276 OPTGROUP_NONE, /* optinfo_flags */
277 TV_NONE, /* tv_id */
278 PROP_cfg, /* properties_required */
279 0, /* properties_provided */
280 0, /* properties_destroyed */
281 0, /* todo_flags_start */
282 0, /* todo_flags_finish */
285 class pass_build_cgraph_edges : public gimple_opt_pass
287 public:
288 pass_build_cgraph_edges (gcc::context *ctxt)
289 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
292 /* opt_pass methods: */
293 virtual unsigned int execute (function *);
295 }; // class pass_build_cgraph_edges
297 unsigned int
298 pass_build_cgraph_edges::execute (function *fun)
300 basic_block bb;
301 cgraph_node *node = cgraph_node::get (current_function_decl);
302 gimple_stmt_iterator gsi;
303 tree decl;
304 unsigned ix;
306 /* Create the callgraph edges and record the nodes referenced by the function.
307 body. */
308 FOR_EACH_BB_FN (bb, fun)
310 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
312 gimple *stmt = gsi_stmt (gsi);
313 tree decl;
315 if (is_gimple_debug (stmt))
316 continue;
318 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
320 int freq = compute_call_stmt_bb_frequency (current_function_decl,
321 bb);
322 decl = gimple_call_fndecl (call_stmt);
323 if (decl)
324 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
325 else if (gimple_call_internal_p (call_stmt))
327 else
328 node->create_indirect_edge (call_stmt,
329 gimple_call_flags (call_stmt),
330 bb->count, freq);
332 node->record_stmt_references (stmt);
333 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
335 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
336 node->create_reference (cgraph_node::get_create (fn),
337 IPA_REF_ADDR, stmt);
339 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
341 tree fn = gimple_omp_task_child_fn (stmt);
342 if (fn)
343 node->create_reference (cgraph_node::get_create (fn),
344 IPA_REF_ADDR, stmt);
345 fn = gimple_omp_task_copy_fn (stmt);
346 if (fn)
347 node->create_reference (cgraph_node::get_create (fn),
348 IPA_REF_ADDR, stmt);
351 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
352 node->record_stmt_references (gsi_stmt (gsi));
355 /* Look for initializers of constant variables and private statics. */
356 FOR_EACH_LOCAL_DECL (fun, ix, decl)
357 if (VAR_P (decl)
358 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
359 && !DECL_HAS_VALUE_EXPR_P (decl)
360 && TREE_TYPE (decl) != error_mark_node)
361 varpool_node::finalize_decl (decl);
362 record_eh_tables (node, fun);
364 return 0;
367 } // anon namespace
369 gimple_opt_pass *
370 make_pass_build_cgraph_edges (gcc::context *ctxt)
372 return new pass_build_cgraph_edges (ctxt);
375 /* Record references to functions and other variables present in the
376 initial value of DECL, a variable.
377 When ONLY_VARS is true, we mark needed only variables, not functions. */
379 void
380 record_references_in_initializer (tree decl, bool only_vars)
382 varpool_node *node = varpool_node::get_create (decl);
383 hash_set<tree> visited_nodes;
384 record_reference_ctx ctx = {false, NULL};
386 ctx.varpool_node = node;
387 ctx.only_vars = only_vars;
388 walk_tree (&DECL_INITIAL (decl), record_reference,
389 &ctx, &visited_nodes);
392 /* Rebuild cgraph edges for current function node. This needs to be run after
393 passes that don't update the cgraph. */
395 unsigned int
396 cgraph_edge::rebuild_edges (void)
398 basic_block bb;
399 cgraph_node *node = cgraph_node::get (current_function_decl);
400 gimple_stmt_iterator gsi;
402 node->remove_callees ();
403 node->remove_all_references ();
405 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ();
407 FOR_EACH_BB_FN (bb, cfun)
409 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
411 gimple *stmt = gsi_stmt (gsi);
412 tree decl;
414 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
416 int freq = compute_call_stmt_bb_frequency (current_function_decl,
417 bb);
418 decl = gimple_call_fndecl (call_stmt);
419 if (decl)
420 node->create_edge (cgraph_node::get_create (decl), call_stmt,
421 bb->count, freq);
422 else if (gimple_call_internal_p (call_stmt))
424 else
425 node->create_indirect_edge (call_stmt,
426 gimple_call_flags (call_stmt),
427 bb->count, freq);
429 node->record_stmt_references (stmt);
431 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
432 node->record_stmt_references (gsi_stmt (gsi));
434 record_eh_tables (node, cfun);
435 gcc_assert (!node->global.inlined_to);
437 if (node->instrumented_version
438 && !node->instrumentation_clone)
439 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
441 return 0;
444 /* Rebuild cgraph references for current function node. This needs to be run
445 after passes that don't update the cgraph. */
447 void
448 cgraph_edge::rebuild_references (void)
450 basic_block bb;
451 cgraph_node *node = cgraph_node::get (current_function_decl);
452 gimple_stmt_iterator gsi;
453 ipa_ref *ref = NULL;
454 int i;
456 /* Keep speculative references for further cgraph edge expansion. */
457 for (i = 0; node->iterate_reference (i, ref);)
458 if (!ref->speculative)
459 ref->remove_reference ();
460 else
461 i++;
463 FOR_EACH_BB_FN (bb, cfun)
465 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
466 node->record_stmt_references (gsi_stmt (gsi));
467 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
468 node->record_stmt_references (gsi_stmt (gsi));
470 record_eh_tables (node, cfun);
472 if (node->instrumented_version
473 && !node->instrumentation_clone)
474 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
477 namespace {
479 const pass_data pass_data_rebuild_cgraph_edges =
481 GIMPLE_PASS, /* type */
482 "*rebuild_cgraph_edges", /* name */
483 OPTGROUP_NONE, /* optinfo_flags */
484 TV_CGRAPH, /* tv_id */
485 PROP_cfg, /* properties_required */
486 0, /* properties_provided */
487 0, /* properties_destroyed */
488 0, /* todo_flags_start */
489 0, /* todo_flags_finish */
492 class pass_rebuild_cgraph_edges : public gimple_opt_pass
494 public:
495 pass_rebuild_cgraph_edges (gcc::context *ctxt)
496 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
499 /* opt_pass methods: */
500 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
501 virtual unsigned int execute (function *)
503 return cgraph_edge::rebuild_edges ();
506 }; // class pass_rebuild_cgraph_edges
508 } // anon namespace
510 gimple_opt_pass *
511 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
513 return new pass_rebuild_cgraph_edges (ctxt);
517 namespace {
519 const pass_data pass_data_remove_cgraph_callee_edges =
521 GIMPLE_PASS, /* type */
522 "*remove_cgraph_callee_edges", /* name */
523 OPTGROUP_NONE, /* optinfo_flags */
524 TV_NONE, /* tv_id */
525 0, /* properties_required */
526 0, /* properties_provided */
527 0, /* properties_destroyed */
528 0, /* todo_flags_start */
529 0, /* todo_flags_finish */
532 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
534 public:
535 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
536 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
539 /* opt_pass methods: */
540 opt_pass * clone () {
541 return new pass_remove_cgraph_callee_edges (m_ctxt);
543 virtual unsigned int execute (function *);
545 }; // class pass_remove_cgraph_callee_edges
547 unsigned int
548 pass_remove_cgraph_callee_edges::execute (function *)
550 cgraph_node *node = cgraph_node::get (current_function_decl);
551 node->remove_callees ();
552 node->remove_all_references ();
553 return 0;
556 } // anon namespace
558 gimple_opt_pass *
559 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
561 return new pass_remove_cgraph_callee_edges (ctxt);