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