decl.c (value_annotation_hasher::handle_cache_entry): Delete.
[gcc.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "predict.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
38 #include "gimple-expr.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-walk.h"
42 #include "langhooks.h"
43 #include "intl.h"
44 #include "tree-pass.h"
45 #include "plugin-api.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "ipa-utils.h"
49 #include "except.h"
50 #include "alloc-pool.h"
51 #include "symbol-summary.h"
52 #include "ipa-prop.h"
53 #include "ipa-inline.h"
54
55 /* Context of record_reference. */
56 struct record_reference_ctx
57 {
58 bool only_vars;
59 class varpool_node *varpool_node;
60 };
61
62 /* Walk tree and record all calls and references to functions/variables.
63 Called via walk_tree: TP is pointer to tree to be examined.
64 When DATA is non-null, record references to callgraph.
65 */
66
67 static tree
68 record_reference (tree *tp, int *walk_subtrees, void *data)
69 {
70 tree t = *tp;
71 tree decl;
72 record_reference_ctx *ctx = (record_reference_ctx *)data;
73
74 t = canonicalize_constructor_val (t, NULL);
75 if (!t)
76 t = *tp;
77 else if (t != *tp)
78 *tp = t;
79
80 switch (TREE_CODE (t))
81 {
82 case VAR_DECL:
83 case FUNCTION_DECL:
84 gcc_unreachable ();
85 break;
86
87 case FDESC_EXPR:
88 case ADDR_EXPR:
89 /* Record dereferences to the functions. This makes the
90 functions reachable unconditionally. */
91 decl = get_base_var (*tp);
92 if (TREE_CODE (decl) == FUNCTION_DECL)
93 {
94 cgraph_node *node = cgraph_node::get_create (decl);
95 if (!ctx->only_vars)
96 node->mark_address_taken ();
97 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
98 }
99
100 if (TREE_CODE (decl) == VAR_DECL)
101 {
102 varpool_node *vnode = varpool_node::get_create (decl);
103 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
104 }
105 *walk_subtrees = 0;
106 break;
107
108 default:
109 /* Save some cycles by not walking types and declaration as we
110 won't find anything useful there anyway. */
111 if (IS_TYPE_OR_DECL_P (*tp))
112 {
113 *walk_subtrees = 0;
114 break;
115 }
116 break;
117 }
118
119 return NULL_TREE;
120 }
121
122 /* Record references to typeinfos in the type list LIST. */
123
124 static void
125 record_type_list (cgraph_node *node, tree list)
126 {
127 for (; list; list = TREE_CHAIN (list))
128 {
129 tree type = TREE_VALUE (list);
130
131 if (TYPE_P (type))
132 type = lookup_type_for_runtime (type);
133 STRIP_NOPS (type);
134 if (TREE_CODE (type) == ADDR_EXPR)
135 {
136 type = TREE_OPERAND (type, 0);
137 if (TREE_CODE (type) == VAR_DECL)
138 {
139 varpool_node *vnode = varpool_node::get_create (type);
140 node->create_reference (vnode, IPA_REF_ADDR);
141 }
142 }
143 }
144 }
145
146 /* Record all references we will introduce by producing EH tables
147 for NODE. */
148
149 static void
150 record_eh_tables (cgraph_node *node, function *fun)
151 {
152 eh_region i;
153
154 if (DECL_FUNCTION_PERSONALITY (node->decl))
155 {
156 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
157 cgraph_node *per_node = cgraph_node::get_create (per_decl);
158
159 node->create_reference (per_node, IPA_REF_ADDR);
160 per_node->mark_address_taken ();
161 }
162
163 i = fun->eh->region_tree;
164 if (!i)
165 return;
166
167 while (1)
168 {
169 switch (i->type)
170 {
171 case ERT_CLEANUP:
172 case ERT_MUST_NOT_THROW:
173 break;
174
175 case ERT_TRY:
176 {
177 eh_catch c;
178 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
179 record_type_list (node, c->type_list);
180 }
181 break;
182
183 case ERT_ALLOWED_EXCEPTIONS:
184 record_type_list (node, i->u.allowed.type_list);
185 break;
186 }
187 /* If there are sub-regions, process them. */
188 if (i->inner)
189 i = i->inner;
190 /* If there are peers, process them. */
191 else if (i->next_peer)
192 i = i->next_peer;
193 /* Otherwise, step back up the tree to the next peer. */
194 else
195 {
196 do
197 {
198 i = i->outer;
199 if (i == NULL)
200 return;
201 }
202 while (i->next_peer == NULL);
203 i = i->next_peer;
204 }
205 }
206 }
207
208 /* Computes the frequency of the call statement so that it can be stored in
209 cgraph_edge. BB is the basic block of the call statement. */
210 int
211 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
212 {
213 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
214 (DECL_STRUCT_FUNCTION (decl))->frequency;
215 int freq = bb->frequency;
216
217 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
218 return CGRAPH_FREQ_BASE;
219
220 if (!entry_freq)
221 entry_freq = 1, freq++;
222
223 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
224 if (freq > CGRAPH_FREQ_MAX)
225 freq = CGRAPH_FREQ_MAX;
226
227 return freq;
228 }
229
230 /* Mark address taken in STMT. */
231
232 static bool
233 mark_address (gimple stmt, tree addr, tree, void *data)
234 {
235 addr = get_base_address (addr);
236 if (TREE_CODE (addr) == FUNCTION_DECL)
237 {
238 cgraph_node *node = cgraph_node::get_create (addr);
239 node->mark_address_taken ();
240 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
241 }
242 else if (addr && TREE_CODE (addr) == VAR_DECL
243 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
244 {
245 varpool_node *vnode = varpool_node::get_create (addr);
246
247 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
248 }
249
250 return false;
251 }
252
253 /* Mark load of T. */
254
255 static bool
256 mark_load (gimple stmt, tree t, tree, void *data)
257 {
258 t = get_base_address (t);
259 if (t && TREE_CODE (t) == FUNCTION_DECL)
260 {
261 /* ??? This can happen on platforms with descriptors when these are
262 directly manipulated in the code. Pretend that it's an address. */
263 cgraph_node *node = cgraph_node::get_create (t);
264 node->mark_address_taken ();
265 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
266 }
267 else if (t && TREE_CODE (t) == VAR_DECL
268 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
269 {
270 varpool_node *vnode = varpool_node::get_create (t);
271
272 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
273 }
274 return false;
275 }
276
277 /* Mark store of T. */
278
279 static bool
280 mark_store (gimple stmt, tree t, tree, void *data)
281 {
282 t = get_base_address (t);
283 if (t && TREE_CODE (t) == VAR_DECL
284 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
285 {
286 varpool_node *vnode = varpool_node::get_create (t);
287
288 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
289 }
290 return false;
291 }
292
293 /* Record all references from cgraph_node that are taken in statement STMT. */
294
295 void
296 cgraph_node::record_stmt_references (gimple stmt)
297 {
298 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
299 mark_address);
300 }
301
302 /* Create cgraph edges for function calls.
303 Also look for functions and variables having addresses taken. */
304
305 namespace {
306
307 const pass_data pass_data_build_cgraph_edges =
308 {
309 GIMPLE_PASS, /* type */
310 "*build_cgraph_edges", /* name */
311 OPTGROUP_NONE, /* optinfo_flags */
312 TV_NONE, /* tv_id */
313 PROP_cfg, /* properties_required */
314 0, /* properties_provided */
315 0, /* properties_destroyed */
316 0, /* todo_flags_start */
317 0, /* todo_flags_finish */
318 };
319
320 class pass_build_cgraph_edges : public gimple_opt_pass
321 {
322 public:
323 pass_build_cgraph_edges (gcc::context *ctxt)
324 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
325 {}
326
327 /* opt_pass methods: */
328 virtual unsigned int execute (function *);
329
330 }; // class pass_build_cgraph_edges
331
332 unsigned int
333 pass_build_cgraph_edges::execute (function *fun)
334 {
335 basic_block bb;
336 cgraph_node *node = cgraph_node::get (current_function_decl);
337 gimple_stmt_iterator gsi;
338 tree decl;
339 unsigned ix;
340
341 /* Create the callgraph edges and record the nodes referenced by the function.
342 body. */
343 FOR_EACH_BB_FN (bb, fun)
344 {
345 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
346 {
347 gimple stmt = gsi_stmt (gsi);
348 tree decl;
349
350 if (is_gimple_debug (stmt))
351 continue;
352
353 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
354 {
355 int freq = compute_call_stmt_bb_frequency (current_function_decl,
356 bb);
357 decl = gimple_call_fndecl (call_stmt);
358 if (decl)
359 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
360 else if (gimple_call_internal_p (call_stmt))
361 ;
362 else
363 node->create_indirect_edge (call_stmt,
364 gimple_call_flags (call_stmt),
365 bb->count, freq);
366 }
367 node->record_stmt_references (stmt);
368 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
369 {
370 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
371 node->create_reference (cgraph_node::get_create (fn),
372 IPA_REF_ADDR, stmt);
373 }
374 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
375 {
376 tree fn = gimple_omp_task_child_fn (stmt);
377 if (fn)
378 node->create_reference (cgraph_node::get_create (fn),
379 IPA_REF_ADDR, stmt);
380 fn = gimple_omp_task_copy_fn (stmt);
381 if (fn)
382 node->create_reference (cgraph_node::get_create (fn),
383 IPA_REF_ADDR, stmt);
384 }
385 }
386 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
387 node->record_stmt_references (gsi_stmt (gsi));
388 }
389
390 /* Look for initializers of constant variables and private statics. */
391 FOR_EACH_LOCAL_DECL (fun, ix, decl)
392 if (TREE_CODE (decl) == VAR_DECL
393 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
394 && !DECL_HAS_VALUE_EXPR_P (decl))
395 varpool_node::finalize_decl (decl);
396 record_eh_tables (node, fun);
397
398 return 0;
399 }
400
401 } // anon namespace
402
403 gimple_opt_pass *
404 make_pass_build_cgraph_edges (gcc::context *ctxt)
405 {
406 return new pass_build_cgraph_edges (ctxt);
407 }
408
409 /* Record references to functions and other variables present in the
410 initial value of DECL, a variable.
411 When ONLY_VARS is true, we mark needed only variables, not functions. */
412
413 void
414 record_references_in_initializer (tree decl, bool only_vars)
415 {
416 varpool_node *node = varpool_node::get_create (decl);
417 hash_set<tree> visited_nodes;
418 record_reference_ctx ctx = {false, NULL};
419
420 ctx.varpool_node = node;
421 ctx.only_vars = only_vars;
422 walk_tree (&DECL_INITIAL (decl), record_reference,
423 &ctx, &visited_nodes);
424 }
425
426 /* Rebuild cgraph edges for current function node. This needs to be run after
427 passes that don't update the cgraph. */
428
429 unsigned int
430 cgraph_edge::rebuild_edges (void)
431 {
432 basic_block bb;
433 cgraph_node *node = cgraph_node::get (current_function_decl);
434 gimple_stmt_iterator gsi;
435
436 node->remove_callees ();
437 node->remove_all_references ();
438
439 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
440
441 FOR_EACH_BB_FN (bb, cfun)
442 {
443 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
444 {
445 gimple stmt = gsi_stmt (gsi);
446 tree decl;
447
448 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
449 {
450 int freq = compute_call_stmt_bb_frequency (current_function_decl,
451 bb);
452 decl = gimple_call_fndecl (call_stmt);
453 if (decl)
454 node->create_edge (cgraph_node::get_create (decl), call_stmt,
455 bb->count, freq);
456 else if (gimple_call_internal_p (call_stmt))
457 ;
458 else
459 node->create_indirect_edge (call_stmt,
460 gimple_call_flags (call_stmt),
461 bb->count, freq);
462 }
463 node->record_stmt_references (stmt);
464 }
465 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
466 node->record_stmt_references (gsi_stmt (gsi));
467 }
468 record_eh_tables (node, cfun);
469 gcc_assert (!node->global.inlined_to);
470
471 if (node->instrumented_version
472 && !node->instrumentation_clone)
473 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
474
475 return 0;
476 }
477
478 /* Rebuild cgraph references for current function node. This needs to be run
479 after passes that don't update the cgraph. */
480
481 void
482 cgraph_edge::rebuild_references (void)
483 {
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;
489
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++;
496
497 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
498
499 FOR_EACH_BB_FN (bb, cfun)
500 {
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));
505 }
506 record_eh_tables (node, cfun);
507
508 if (node->instrumented_version
509 && !node->instrumentation_clone)
510 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
511 }
512
513 namespace {
514
515 const pass_data pass_data_rebuild_cgraph_edges =
516 {
517 GIMPLE_PASS, /* type */
518 "*rebuild_cgraph_edges", /* name */
519 OPTGROUP_NONE, /* optinfo_flags */
520 TV_CGRAPH, /* tv_id */
521 PROP_cfg, /* properties_required */
522 0, /* properties_provided */
523 0, /* properties_destroyed */
524 0, /* todo_flags_start */
525 0, /* todo_flags_finish */
526 };
527
528 class pass_rebuild_cgraph_edges : public gimple_opt_pass
529 {
530 public:
531 pass_rebuild_cgraph_edges (gcc::context *ctxt)
532 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
533 {}
534
535 /* opt_pass methods: */
536 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
537 virtual unsigned int execute (function *)
538 {
539 return cgraph_edge::rebuild_edges ();
540 }
541
542 }; // class pass_rebuild_cgraph_edges
543
544 } // anon namespace
545
546 gimple_opt_pass *
547 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
548 {
549 return new pass_rebuild_cgraph_edges (ctxt);
550 }
551
552
553 namespace {
554
555 const pass_data pass_data_remove_cgraph_callee_edges =
556 {
557 GIMPLE_PASS, /* type */
558 "*remove_cgraph_callee_edges", /* name */
559 OPTGROUP_NONE, /* optinfo_flags */
560 TV_NONE, /* tv_id */
561 0, /* properties_required */
562 0, /* properties_provided */
563 0, /* properties_destroyed */
564 0, /* todo_flags_start */
565 0, /* todo_flags_finish */
566 };
567
568 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
569 {
570 public:
571 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
572 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
573 {}
574
575 /* opt_pass methods: */
576 opt_pass * clone () {
577 return new pass_remove_cgraph_callee_edges (m_ctxt);
578 }
579 virtual unsigned int execute (function *);
580
581 }; // class pass_remove_cgraph_callee_edges
582
583 unsigned int
584 pass_remove_cgraph_callee_edges::execute (function *)
585 {
586 cgraph_node *node = cgraph_node::get (current_function_decl);
587 node->remove_callees ();
588 node->remove_all_references ();
589 return 0;
590 }
591
592 } // anon namespace
593
594 gimple_opt_pass *
595 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
596 {
597 return new pass_remove_cgraph_callee_edges (ctxt);
598 }