Fix for ICE with -g on testcase with incomplete types.
[official-gcc.git] / gcc / cgraphbuild.c
blobb0815c987cb568d4d7a67edb7b01ee26131cfe8d
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 && TREE_TYPE (decl) != error_mark_node)
386 varpool_node::finalize_decl (decl);
387 record_eh_tables (node, fun);
389 return 0;
392 } // anon namespace
394 gimple_opt_pass *
395 make_pass_build_cgraph_edges (gcc::context *ctxt)
397 return new pass_build_cgraph_edges (ctxt);
400 /* Record references to functions and other variables present in the
401 initial value of DECL, a variable.
402 When ONLY_VARS is true, we mark needed only variables, not functions. */
404 void
405 record_references_in_initializer (tree decl, bool only_vars)
407 varpool_node *node = varpool_node::get_create (decl);
408 hash_set<tree> visited_nodes;
409 record_reference_ctx ctx = {false, NULL};
411 ctx.varpool_node = node;
412 ctx.only_vars = only_vars;
413 walk_tree (&DECL_INITIAL (decl), record_reference,
414 &ctx, &visited_nodes);
417 /* Rebuild cgraph edges for current function node. This needs to be run after
418 passes that don't update the cgraph. */
420 unsigned int
421 cgraph_edge::rebuild_edges (void)
423 basic_block bb;
424 cgraph_node *node = cgraph_node::get (current_function_decl);
425 gimple_stmt_iterator gsi;
427 node->remove_callees ();
428 node->remove_all_references ();
430 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
432 FOR_EACH_BB_FN (bb, cfun)
434 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
436 gimple *stmt = gsi_stmt (gsi);
437 tree decl;
439 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
441 int freq = compute_call_stmt_bb_frequency (current_function_decl,
442 bb);
443 decl = gimple_call_fndecl (call_stmt);
444 if (decl)
445 node->create_edge (cgraph_node::get_create (decl), call_stmt,
446 bb->count, freq);
447 else if (gimple_call_internal_p (call_stmt))
449 else
450 node->create_indirect_edge (call_stmt,
451 gimple_call_flags (call_stmt),
452 bb->count, freq);
454 node->record_stmt_references (stmt);
456 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
457 node->record_stmt_references (gsi_stmt (gsi));
459 record_eh_tables (node, cfun);
460 gcc_assert (!node->global.inlined_to);
462 if (node->instrumented_version
463 && !node->instrumentation_clone)
464 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
466 return 0;
469 /* Rebuild cgraph references for current function node. This needs to be run
470 after passes that don't update the cgraph. */
472 void
473 cgraph_edge::rebuild_references (void)
475 basic_block bb;
476 cgraph_node *node = cgraph_node::get (current_function_decl);
477 gimple_stmt_iterator gsi;
478 ipa_ref *ref = NULL;
479 int i;
481 /* Keep speculative references for further cgraph edge expansion. */
482 for (i = 0; node->iterate_reference (i, ref);)
483 if (!ref->speculative)
484 ref->remove_reference ();
485 else
486 i++;
488 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
490 FOR_EACH_BB_FN (bb, cfun)
492 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
493 node->record_stmt_references (gsi_stmt (gsi));
494 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
495 node->record_stmt_references (gsi_stmt (gsi));
497 record_eh_tables (node, cfun);
499 if (node->instrumented_version
500 && !node->instrumentation_clone)
501 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
504 namespace {
506 const pass_data pass_data_rebuild_cgraph_edges =
508 GIMPLE_PASS, /* type */
509 "*rebuild_cgraph_edges", /* name */
510 OPTGROUP_NONE, /* optinfo_flags */
511 TV_CGRAPH, /* tv_id */
512 PROP_cfg, /* properties_required */
513 0, /* properties_provided */
514 0, /* properties_destroyed */
515 0, /* todo_flags_start */
516 0, /* todo_flags_finish */
519 class pass_rebuild_cgraph_edges : public gimple_opt_pass
521 public:
522 pass_rebuild_cgraph_edges (gcc::context *ctxt)
523 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
526 /* opt_pass methods: */
527 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
528 virtual unsigned int execute (function *)
530 return cgraph_edge::rebuild_edges ();
533 }; // class pass_rebuild_cgraph_edges
535 } // anon namespace
537 gimple_opt_pass *
538 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
540 return new pass_rebuild_cgraph_edges (ctxt);
544 namespace {
546 const pass_data pass_data_remove_cgraph_callee_edges =
548 GIMPLE_PASS, /* type */
549 "*remove_cgraph_callee_edges", /* name */
550 OPTGROUP_NONE, /* optinfo_flags */
551 TV_NONE, /* tv_id */
552 0, /* properties_required */
553 0, /* properties_provided */
554 0, /* properties_destroyed */
555 0, /* todo_flags_start */
556 0, /* todo_flags_finish */
559 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
561 public:
562 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
563 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
566 /* opt_pass methods: */
567 opt_pass * clone () {
568 return new pass_remove_cgraph_callee_edges (m_ctxt);
570 virtual unsigned int execute (function *);
572 }; // class pass_remove_cgraph_callee_edges
574 unsigned int
575 pass_remove_cgraph_callee_edges::execute (function *)
577 cgraph_node *node = cgraph_node::get (current_function_decl);
578 node->remove_callees ();
579 node->remove_all_references ();
580 return 0;
583 } // anon namespace
585 gimple_opt_pass *
586 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
588 return new pass_remove_cgraph_callee_edges (ctxt);