tree-core.h: Include symtab.h.
[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 "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"
44
45 /* Context of record_reference. */
46 struct record_reference_ctx
47 {
48 bool only_vars;
49 class varpool_node *varpool_node;
50 };
51
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.
55 */
56
57 static tree
58 record_reference (tree *tp, int *walk_subtrees, void *data)
59 {
60 tree t = *tp;
61 tree decl;
62 record_reference_ctx *ctx = (record_reference_ctx *)data;
63
64 t = canonicalize_constructor_val (t, NULL);
65 if (!t)
66 t = *tp;
67 else if (t != *tp)
68 *tp = t;
69
70 switch (TREE_CODE (t))
71 {
72 case VAR_DECL:
73 case FUNCTION_DECL:
74 gcc_unreachable ();
75 break;
76
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)
83 {
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);
88 }
89
90 if (TREE_CODE (decl) == VAR_DECL)
91 {
92 varpool_node *vnode = varpool_node::get_create (decl);
93 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
94 }
95 *walk_subtrees = 0;
96 break;
97
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))
102 {
103 *walk_subtrees = 0;
104 break;
105 }
106 break;
107 }
108
109 return NULL_TREE;
110 }
111
112 /* Record references to typeinfos in the type list LIST. */
113
114 static void
115 record_type_list (cgraph_node *node, tree list)
116 {
117 for (; list; list = TREE_CHAIN (list))
118 {
119 tree type = TREE_VALUE (list);
120
121 if (TYPE_P (type))
122 type = lookup_type_for_runtime (type);
123 STRIP_NOPS (type);
124 if (TREE_CODE (type) == ADDR_EXPR)
125 {
126 type = TREE_OPERAND (type, 0);
127 if (TREE_CODE (type) == VAR_DECL)
128 {
129 varpool_node *vnode = varpool_node::get_create (type);
130 node->create_reference (vnode, IPA_REF_ADDR);
131 }
132 }
133 }
134 }
135
136 /* Record all references we will introduce by producing EH tables
137 for NODE. */
138
139 static void
140 record_eh_tables (cgraph_node *node, function *fun)
141 {
142 eh_region i;
143
144 if (DECL_FUNCTION_PERSONALITY (node->decl))
145 {
146 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
147 cgraph_node *per_node = cgraph_node::get_create (per_decl);
148
149 node->create_reference (per_node, IPA_REF_ADDR);
150 per_node->mark_address_taken ();
151 }
152
153 i = fun->eh->region_tree;
154 if (!i)
155 return;
156
157 while (1)
158 {
159 switch (i->type)
160 {
161 case ERT_CLEANUP:
162 case ERT_MUST_NOT_THROW:
163 break;
164
165 case ERT_TRY:
166 {
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);
170 }
171 break;
172
173 case ERT_ALLOWED_EXCEPTIONS:
174 record_type_list (node, i->u.allowed.type_list);
175 break;
176 }
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
185 {
186 do
187 {
188 i = i->outer;
189 if (i == NULL)
190 return;
191 }
192 while (i->next_peer == NULL);
193 i = i->next_peer;
194 }
195 }
196 }
197
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. */
200 int
201 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
202 {
203 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
204 (DECL_STRUCT_FUNCTION (decl))->frequency;
205 int freq = bb->frequency;
206
207 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
208 return CGRAPH_FREQ_BASE;
209
210 if (!entry_freq)
211 entry_freq = 1, freq++;
212
213 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
214 if (freq > CGRAPH_FREQ_MAX)
215 freq = CGRAPH_FREQ_MAX;
216
217 return freq;
218 }
219
220 /* Mark address taken in STMT. */
221
222 static bool
223 mark_address (gimple stmt, tree addr, tree, void *data)
224 {
225 addr = get_base_address (addr);
226 if (TREE_CODE (addr) == FUNCTION_DECL)
227 {
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);
231 }
232 else if (addr && TREE_CODE (addr) == VAR_DECL
233 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
234 {
235 varpool_node *vnode = varpool_node::get_create (addr);
236
237 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
238 }
239
240 return false;
241 }
242
243 /* Mark load of T. */
244
245 static bool
246 mark_load (gimple stmt, tree t, tree, void *data)
247 {
248 t = get_base_address (t);
249 if (t && TREE_CODE (t) == FUNCTION_DECL)
250 {
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);
256 }
257 else if (t && TREE_CODE (t) == VAR_DECL
258 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
259 {
260 varpool_node *vnode = varpool_node::get_create (t);
261
262 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
263 }
264 return false;
265 }
266
267 /* Mark store of T. */
268
269 static bool
270 mark_store (gimple stmt, tree t, tree, void *data)
271 {
272 t = get_base_address (t);
273 if (t && TREE_CODE (t) == VAR_DECL
274 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
275 {
276 varpool_node *vnode = varpool_node::get_create (t);
277
278 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
279 }
280 return false;
281 }
282
283 /* Record all references from cgraph_node that are taken in statement STMT. */
284
285 void
286 cgraph_node::record_stmt_references (gimple stmt)
287 {
288 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
289 mark_address);
290 }
291
292 /* Create cgraph edges for function calls.
293 Also look for functions and variables having addresses taken. */
294
295 namespace {
296
297 const pass_data pass_data_build_cgraph_edges =
298 {
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 */
308 };
309
310 class pass_build_cgraph_edges : public gimple_opt_pass
311 {
312 public:
313 pass_build_cgraph_edges (gcc::context *ctxt)
314 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
315 {}
316
317 /* opt_pass methods: */
318 virtual unsigned int execute (function *);
319
320 }; // class pass_build_cgraph_edges
321
322 unsigned int
323 pass_build_cgraph_edges::execute (function *fun)
324 {
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;
330
331 /* Create the callgraph edges and record the nodes referenced by the function.
332 body. */
333 FOR_EACH_BB_FN (bb, fun)
334 {
335 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
336 {
337 gimple stmt = gsi_stmt (gsi);
338 tree decl;
339
340 if (is_gimple_debug (stmt))
341 continue;
342
343 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
344 {
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))
351 ;
352 else
353 node->create_indirect_edge (call_stmt,
354 gimple_call_flags (call_stmt),
355 bb->count, freq);
356 }
357 node->record_stmt_references (stmt);
358 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
359 {
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);
363 }
364 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
365 {
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);
374 }
375 }
376 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
377 node->record_stmt_references (gsi_stmt (gsi));
378 }
379
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);
387
388 return 0;
389 }
390
391 } // anon namespace
392
393 gimple_opt_pass *
394 make_pass_build_cgraph_edges (gcc::context *ctxt)
395 {
396 return new pass_build_cgraph_edges (ctxt);
397 }
398
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. */
402
403 void
404 record_references_in_initializer (tree decl, bool only_vars)
405 {
406 varpool_node *node = varpool_node::get_create (decl);
407 hash_set<tree> visited_nodes;
408 record_reference_ctx ctx = {false, NULL};
409
410 ctx.varpool_node = node;
411 ctx.only_vars = only_vars;
412 walk_tree (&DECL_INITIAL (decl), record_reference,
413 &ctx, &visited_nodes);
414 }
415
416 /* Rebuild cgraph edges for current function node. This needs to be run after
417 passes that don't update the cgraph. */
418
419 unsigned int
420 cgraph_edge::rebuild_edges (void)
421 {
422 basic_block bb;
423 cgraph_node *node = cgraph_node::get (current_function_decl);
424 gimple_stmt_iterator gsi;
425
426 node->remove_callees ();
427 node->remove_all_references ();
428
429 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
430
431 FOR_EACH_BB_FN (bb, cfun)
432 {
433 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
434 {
435 gimple stmt = gsi_stmt (gsi);
436 tree decl;
437
438 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
439 {
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))
447 ;
448 else
449 node->create_indirect_edge (call_stmt,
450 gimple_call_flags (call_stmt),
451 bb->count, freq);
452 }
453 node->record_stmt_references (stmt);
454 }
455 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
456 node->record_stmt_references (gsi_stmt (gsi));
457 }
458 record_eh_tables (node, cfun);
459 gcc_assert (!node->global.inlined_to);
460
461 if (node->instrumented_version
462 && !node->instrumentation_clone)
463 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
464
465 return 0;
466 }
467
468 /* Rebuild cgraph references for current function node. This needs to be run
469 after passes that don't update the cgraph. */
470
471 void
472 cgraph_edge::rebuild_references (void)
473 {
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;
479
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++;
486
487 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
488
489 FOR_EACH_BB_FN (bb, cfun)
490 {
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));
495 }
496 record_eh_tables (node, cfun);
497
498 if (node->instrumented_version
499 && !node->instrumentation_clone)
500 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
501 }
502
503 namespace {
504
505 const pass_data pass_data_rebuild_cgraph_edges =
506 {
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 */
516 };
517
518 class pass_rebuild_cgraph_edges : public gimple_opt_pass
519 {
520 public:
521 pass_rebuild_cgraph_edges (gcc::context *ctxt)
522 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
523 {}
524
525 /* opt_pass methods: */
526 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
527 virtual unsigned int execute (function *)
528 {
529 return cgraph_edge::rebuild_edges ();
530 }
531
532 }; // class pass_rebuild_cgraph_edges
533
534 } // anon namespace
535
536 gimple_opt_pass *
537 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
538 {
539 return new pass_rebuild_cgraph_edges (ctxt);
540 }
541
542
543 namespace {
544
545 const pass_data pass_data_remove_cgraph_callee_edges =
546 {
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 */
556 };
557
558 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
559 {
560 public:
561 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
562 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
563 {}
564
565 /* opt_pass methods: */
566 opt_pass * clone () {
567 return new pass_remove_cgraph_callee_edges (m_ctxt);
568 }
569 virtual unsigned int execute (function *);
570
571 }; // class pass_remove_cgraph_callee_edges
572
573 unsigned int
574 pass_remove_cgraph_callee_edges::execute (function *)
575 {
576 cgraph_node *node = cgraph_node::get (current_function_decl);
577 node->remove_callees ();
578 node->remove_all_references ();
579 return 0;
580 }
581
582 } // anon namespace
583
584 gimple_opt_pass *
585 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
586 {
587 return new pass_remove_cgraph_callee_edges (ctxt);
588 }