]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cgraphbuild.c
Automated conversion of passes to C++ classes
[thirdparty/gcc.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2 Copyright (C) 2003-2013 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 "tree-flow.h"
27 #include "langhooks.h"
28 #include "pointer-set.h"
29 #include "cgraph.h"
30 #include "intl.h"
31 #include "gimple.h"
32 #include "tree-pass.h"
33 #include "ipa-utils.h"
34 #include "except.h"
35 #include "ipa-inline.h"
36
37 /* Context of record_reference. */
38 struct record_reference_ctx
39 {
40 bool only_vars;
41 struct varpool_node *varpool_node;
42 };
43
44 /* Walk tree and record all calls and references to functions/variables.
45 Called via walk_tree: TP is pointer to tree to be examined.
46 When DATA is non-null, record references to callgraph.
47 */
48
49 static tree
50 record_reference (tree *tp, int *walk_subtrees, void *data)
51 {
52 tree t = *tp;
53 tree decl;
54 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
55
56 t = canonicalize_constructor_val (t, NULL);
57 if (!t)
58 t = *tp;
59 else if (t != *tp)
60 *tp = t;
61
62 switch (TREE_CODE (t))
63 {
64 case VAR_DECL:
65 case FUNCTION_DECL:
66 gcc_unreachable ();
67 break;
68
69 case FDESC_EXPR:
70 case ADDR_EXPR:
71 /* Record dereferences to the functions. This makes the
72 functions reachable unconditionally. */
73 decl = get_base_var (*tp);
74 if (TREE_CODE (decl) == FUNCTION_DECL)
75 {
76 struct cgraph_node *node = cgraph_get_create_real_symbol_node (decl);
77 if (!ctx->only_vars)
78 cgraph_mark_address_taken_node (node);
79 ipa_record_reference ((symtab_node)ctx->varpool_node,
80 (symtab_node)node,
81 IPA_REF_ADDR, NULL);
82 }
83
84 if (TREE_CODE (decl) == VAR_DECL)
85 {
86 struct varpool_node *vnode = varpool_node_for_decl (decl);
87 ipa_record_reference ((symtab_node)ctx->varpool_node,
88 (symtab_node)vnode,
89 IPA_REF_ADDR, NULL);
90 }
91 *walk_subtrees = 0;
92 break;
93
94 default:
95 /* Save some cycles by not walking types and declaration as we
96 won't find anything useful there anyway. */
97 if (IS_TYPE_OR_DECL_P (*tp))
98 {
99 *walk_subtrees = 0;
100 break;
101 }
102 break;
103 }
104
105 return NULL_TREE;
106 }
107
108 /* Record references to typeinfos in the type list LIST. */
109
110 static void
111 record_type_list (struct cgraph_node *node, tree list)
112 {
113 for (; list; list = TREE_CHAIN (list))
114 {
115 tree type = TREE_VALUE (list);
116
117 if (TYPE_P (type))
118 type = lookup_type_for_runtime (type);
119 STRIP_NOPS (type);
120 if (TREE_CODE (type) == ADDR_EXPR)
121 {
122 type = TREE_OPERAND (type, 0);
123 if (TREE_CODE (type) == VAR_DECL)
124 {
125 struct varpool_node *vnode = varpool_node_for_decl (type);
126 ipa_record_reference ((symtab_node)node,
127 (symtab_node)vnode,
128 IPA_REF_ADDR, NULL);
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->symbol.decl))
143 {
144 struct cgraph_node *per_node;
145
146 per_node = cgraph_get_create_real_symbol_node (DECL_FUNCTION_PERSONALITY (node->symbol.decl));
147 ipa_record_reference ((symtab_node)node, (symtab_node)per_node, IPA_REF_ADDR, NULL);
148 cgraph_mark_address_taken_node (per_node);
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_FUNCTION
202 (DECL_STRUCT_FUNCTION (decl))->frequency;
203 int freq = bb->frequency;
204
205 if (profile_status_for_function (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, void *data)
222 {
223 addr = get_base_address (addr);
224 if (TREE_CODE (addr) == FUNCTION_DECL)
225 {
226 struct cgraph_node *node = cgraph_get_create_real_symbol_node (addr);
227 cgraph_mark_address_taken_node (node);
228 ipa_record_reference ((symtab_node)data,
229 (symtab_node)node,
230 IPA_REF_ADDR, stmt);
231 }
232 else if (addr && TREE_CODE (addr) == VAR_DECL
233 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
234 {
235 struct varpool_node *vnode = varpool_node_for_decl (addr);
236
237 ipa_record_reference ((symtab_node)data,
238 (symtab_node)vnode,
239 IPA_REF_ADDR, stmt);
240 }
241
242 return false;
243 }
244
245 /* Mark load of T. */
246
247 static bool
248 mark_load (gimple stmt, tree t, void *data)
249 {
250 t = get_base_address (t);
251 if (t && TREE_CODE (t) == FUNCTION_DECL)
252 {
253 /* ??? This can happen on platforms with descriptors when these are
254 directly manipulated in the code. Pretend that it's an address. */
255 struct cgraph_node *node = cgraph_get_create_real_symbol_node (t);
256 cgraph_mark_address_taken_node (node);
257 ipa_record_reference ((symtab_node)data,
258 (symtab_node)node,
259 IPA_REF_ADDR, stmt);
260 }
261 else if (t && TREE_CODE (t) == VAR_DECL
262 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
263 {
264 struct varpool_node *vnode = varpool_node_for_decl (t);
265
266 ipa_record_reference ((symtab_node)data,
267 (symtab_node)vnode,
268 IPA_REF_LOAD, stmt);
269 }
270 return false;
271 }
272
273 /* Mark store of T. */
274
275 static bool
276 mark_store (gimple stmt, tree t, void *data)
277 {
278 t = get_base_address (t);
279 if (t && TREE_CODE (t) == VAR_DECL
280 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
281 {
282 struct varpool_node *vnode = varpool_node_for_decl (t);
283
284 ipa_record_reference ((symtab_node)data,
285 (symtab_node)vnode,
286 IPA_REF_STORE, stmt);
287 }
288 return false;
289 }
290
291 /* Record all references from NODE that are taken in statement STMT. */
292 void
293 ipa_record_stmt_references (struct cgraph_node *node, gimple stmt)
294 {
295 walk_stmt_load_store_addr_ops (stmt, node, mark_load, mark_store,
296 mark_address);
297 }
298
299 /* Create cgraph edges for function calls.
300 Also look for functions and variables having addresses taken. */
301
302 static unsigned int
303 build_cgraph_edges (void)
304 {
305 basic_block bb;
306 struct cgraph_node *node = cgraph_get_node (current_function_decl);
307 struct pointer_set_t *visited_nodes = pointer_set_create ();
308 gimple_stmt_iterator gsi;
309 tree decl;
310 unsigned ix;
311
312 /* Create the callgraph edges and record the nodes referenced by the function.
313 body. */
314 FOR_EACH_BB (bb)
315 {
316 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
317 {
318 gimple stmt = gsi_stmt (gsi);
319 tree decl;
320
321 if (is_gimple_call (stmt))
322 {
323 int freq = compute_call_stmt_bb_frequency (current_function_decl,
324 bb);
325 decl = gimple_call_fndecl (stmt);
326 if (decl)
327 cgraph_create_edge (node, cgraph_get_create_node (decl),
328 stmt, bb->count, freq);
329 else
330 cgraph_create_indirect_edge (node, stmt,
331 gimple_call_flags (stmt),
332 bb->count, freq);
333 }
334 ipa_record_stmt_references (node, stmt);
335 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
336 && gimple_omp_parallel_child_fn (stmt))
337 {
338 tree fn = gimple_omp_parallel_child_fn (stmt);
339 ipa_record_reference ((symtab_node)node,
340 (symtab_node)cgraph_get_create_real_symbol_node (fn),
341 IPA_REF_ADDR, stmt);
342 }
343 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
344 {
345 tree fn = gimple_omp_task_child_fn (stmt);
346 if (fn)
347 ipa_record_reference ((symtab_node)node,
348 (symtab_node) cgraph_get_create_real_symbol_node (fn),
349 IPA_REF_ADDR, stmt);
350 fn = gimple_omp_task_copy_fn (stmt);
351 if (fn)
352 ipa_record_reference ((symtab_node)node,
353 (symtab_node)cgraph_get_create_real_symbol_node (fn),
354 IPA_REF_ADDR, stmt);
355 }
356 }
357 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
358 ipa_record_stmt_references (node, gsi_stmt (gsi));
359 }
360
361 /* Look for initializers of constant variables and private statics. */
362 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
363 if (TREE_CODE (decl) == VAR_DECL
364 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
365 && !DECL_HAS_VALUE_EXPR_P (decl))
366 varpool_finalize_decl (decl);
367 record_eh_tables (node, cfun);
368
369 pointer_set_destroy (visited_nodes);
370 return 0;
371 }
372
373 namespace {
374
375 const pass_data pass_data_build_cgraph_edges =
376 {
377 GIMPLE_PASS, /* type */
378 "*build_cgraph_edges", /* name */
379 OPTGROUP_NONE, /* optinfo_flags */
380 false, /* has_gate */
381 true, /* has_execute */
382 TV_NONE, /* tv_id */
383 PROP_cfg, /* properties_required */
384 0, /* properties_provided */
385 0, /* properties_destroyed */
386 0, /* todo_flags_start */
387 0, /* todo_flags_finish */
388 };
389
390 class pass_build_cgraph_edges : public gimple_opt_pass
391 {
392 public:
393 pass_build_cgraph_edges(gcc::context *ctxt)
394 : gimple_opt_pass(pass_data_build_cgraph_edges, ctxt)
395 {}
396
397 /* opt_pass methods: */
398 unsigned int execute () { return build_cgraph_edges (); }
399
400 }; // class pass_build_cgraph_edges
401
402 } // anon namespace
403
404 gimple_opt_pass *
405 make_pass_build_cgraph_edges (gcc::context *ctxt)
406 {
407 return new pass_build_cgraph_edges (ctxt);
408 }
409
410 /* Record references to functions and other variables present in the
411 initial value of DECL, a variable.
412 When ONLY_VARS is true, we mark needed only variables, not functions. */
413
414 void
415 record_references_in_initializer (tree decl, bool only_vars)
416 {
417 struct pointer_set_t *visited_nodes = pointer_set_create ();
418 struct varpool_node *node = varpool_node_for_decl (decl);
419 struct record_reference_ctx ctx = {false, NULL};
420
421 ctx.varpool_node = node;
422 ctx.only_vars = only_vars;
423 walk_tree (&DECL_INITIAL (decl), record_reference,
424 &ctx, visited_nodes);
425 pointer_set_destroy (visited_nodes);
426 }
427
428 /* Rebuild cgraph edges for current function node. This needs to be run after
429 passes that don't update the cgraph. */
430
431 unsigned int
432 rebuild_cgraph_edges (void)
433 {
434 basic_block bb;
435 struct cgraph_node *node = cgraph_get_node (current_function_decl);
436 gimple_stmt_iterator gsi;
437
438 cgraph_node_remove_callees (node);
439 ipa_remove_all_references (&node->symbol.ref_list);
440
441 node->count = ENTRY_BLOCK_PTR->count;
442
443 FOR_EACH_BB (bb)
444 {
445 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
446 {
447 gimple stmt = gsi_stmt (gsi);
448 tree decl;
449
450 if (is_gimple_call (stmt))
451 {
452 int freq = compute_call_stmt_bb_frequency (current_function_decl,
453 bb);
454 decl = gimple_call_fndecl (stmt);
455 if (decl)
456 cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
457 bb->count, freq);
458 else
459 cgraph_create_indirect_edge (node, stmt,
460 gimple_call_flags (stmt),
461 bb->count, freq);
462 }
463 ipa_record_stmt_references (node, stmt);
464 }
465 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
466 ipa_record_stmt_references (node, gsi_stmt (gsi));
467 }
468 record_eh_tables (node, cfun);
469 gcc_assert (!node->global.inlined_to);
470
471 return 0;
472 }
473
474 /* Rebuild cgraph edges for current function node. This needs to be run after
475 passes that don't update the cgraph. */
476
477 void
478 cgraph_rebuild_references (void)
479 {
480 basic_block bb;
481 struct cgraph_node *node = cgraph_get_node (current_function_decl);
482 gimple_stmt_iterator gsi;
483
484 ipa_remove_all_references (&node->symbol.ref_list);
485
486 node->count = ENTRY_BLOCK_PTR->count;
487
488 FOR_EACH_BB (bb)
489 {
490 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
491 ipa_record_stmt_references (node, gsi_stmt (gsi));
492 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
493 ipa_record_stmt_references (node, 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 false, /* has_gate */
506 true, /* has_execute */
507 TV_CGRAPH, /* tv_id */
508 PROP_cfg, /* properties_required */
509 0, /* properties_provided */
510 0, /* properties_destroyed */
511 0, /* todo_flags_start */
512 0, /* todo_flags_finish */
513 };
514
515 class pass_rebuild_cgraph_edges : public gimple_opt_pass
516 {
517 public:
518 pass_rebuild_cgraph_edges(gcc::context *ctxt)
519 : gimple_opt_pass(pass_data_rebuild_cgraph_edges, ctxt)
520 {}
521
522 /* opt_pass methods: */
523 opt_pass * clone () { return new pass_rebuild_cgraph_edges (ctxt_); }
524 unsigned int execute () { return rebuild_cgraph_edges (); }
525
526 }; // class pass_rebuild_cgraph_edges
527
528 } // anon namespace
529
530 gimple_opt_pass *
531 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
532 {
533 return new pass_rebuild_cgraph_edges (ctxt);
534 }
535
536
537 static unsigned int
538 remove_cgraph_callee_edges (void)
539 {
540 cgraph_node_remove_callees (cgraph_get_node (current_function_decl));
541 return 0;
542 }
543
544 namespace {
545
546 const pass_data pass_data_remove_cgraph_callee_edges =
547 {
548 GIMPLE_PASS, /* type */
549 "*remove_cgraph_callee_edges", /* name */
550 OPTGROUP_NONE, /* optinfo_flags */
551 false, /* has_gate */
552 true, /* has_execute */
553 TV_NONE, /* tv_id */
554 0, /* properties_required */
555 0, /* properties_provided */
556 0, /* properties_destroyed */
557 0, /* todo_flags_start */
558 0, /* todo_flags_finish */
559 };
560
561 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
562 {
563 public:
564 pass_remove_cgraph_callee_edges(gcc::context *ctxt)
565 : gimple_opt_pass(pass_data_remove_cgraph_callee_edges, ctxt)
566 {}
567
568 /* opt_pass methods: */
569 opt_pass * clone () {
570 return new pass_remove_cgraph_callee_edges (ctxt_);
571 }
572 unsigned int execute () { return remove_cgraph_callee_edges (); }
573
574 }; // class pass_remove_cgraph_callee_edges
575
576 } // anon namespace
577
578 gimple_opt_pass *
579 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
580 {
581 return new pass_remove_cgraph_callee_edges (ctxt);
582 }