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