* lto-symtab.c (lto_varpool_replace_node): Do not merge needed flags.
[official-gcc.git] / gcc / cgraphbuild.c
blob445a392110dce324636a4fe27c4d8e8b208641c8
1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "langhooks.h"
29 #include "pointer-set.h"
30 #include "cgraph.h"
31 #include "intl.h"
32 #include "gimple.h"
33 #include "tree-pass.h"
34 #include "ipa-utils.h"
35 #include "except.h"
36 #include "ipa-inline.h"
38 /* Context of record_reference. */
39 struct record_reference_ctx
41 bool only_vars;
42 struct varpool_node *varpool_node;
45 /* Walk tree and record all calls and references to functions/variables.
46 Called via walk_tree: TP is pointer to tree to be examined.
47 When DATA is non-null, record references to callgraph.
50 static tree
51 record_reference (tree *tp, int *walk_subtrees, void *data)
53 tree t = *tp;
54 tree decl;
55 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
57 t = canonicalize_constructor_val (t);
58 if (!t)
59 t = *tp;
60 else if (t != *tp)
61 *tp = t;
63 switch (TREE_CODE (t))
65 case VAR_DECL:
66 case FUNCTION_DECL:
67 gcc_unreachable ();
68 break;
70 case FDESC_EXPR:
71 case ADDR_EXPR:
72 /* Record dereferences to the functions. This makes the
73 functions reachable unconditionally. */
74 decl = get_base_var (*tp);
75 if (TREE_CODE (decl) == FUNCTION_DECL)
77 struct cgraph_node *node = cgraph_get_create_node (decl);
78 if (!ctx->only_vars)
79 cgraph_mark_address_taken_node (node);
80 ipa_record_reference ((symtab_node)ctx->varpool_node,
81 (symtab_node)node,
82 IPA_REF_ADDR, NULL);
85 if (TREE_CODE (decl) == VAR_DECL)
87 struct varpool_node *vnode = varpool_node (decl);
88 if (lang_hooks.callgraph.analyze_expr)
89 lang_hooks.callgraph.analyze_expr (&decl, walk_subtrees);
90 ipa_record_reference ((symtab_node)ctx->varpool_node,
91 (symtab_node)vnode,
92 IPA_REF_ADDR, NULL);
94 *walk_subtrees = 0;
95 break;
97 default:
98 /* Save some cycles by not walking types and declaration as we
99 won't find anything useful there anyway. */
100 if (IS_TYPE_OR_DECL_P (*tp))
102 *walk_subtrees = 0;
103 break;
106 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
107 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
108 break;
111 return NULL_TREE;
114 /* Record references to typeinfos in the type list LIST. */
116 static void
117 record_type_list (struct cgraph_node *node, tree list)
119 for (; list; list = TREE_CHAIN (list))
121 tree type = TREE_VALUE (list);
123 if (TYPE_P (type))
124 type = lookup_type_for_runtime (type);
125 STRIP_NOPS (type);
126 if (TREE_CODE (type) == ADDR_EXPR)
128 type = TREE_OPERAND (type, 0);
129 if (TREE_CODE (type) == VAR_DECL)
131 struct varpool_node *vnode = varpool_node (type);
132 ipa_record_reference ((symtab_node)node,
133 (symtab_node)vnode,
134 IPA_REF_ADDR, NULL);
140 /* Record all references we will introduce by producing EH tables
141 for NODE. */
143 static void
144 record_eh_tables (struct cgraph_node *node, struct function *fun)
146 eh_region i;
148 if (DECL_FUNCTION_PERSONALITY (node->symbol.decl))
150 struct cgraph_node *per_node;
152 per_node = cgraph_get_create_node (DECL_FUNCTION_PERSONALITY (node->symbol.decl));
153 ipa_record_reference ((symtab_node)node, (symtab_node)per_node, IPA_REF_ADDR, NULL);
154 cgraph_mark_address_taken_node (per_node);
157 i = fun->eh->region_tree;
158 if (!i)
159 return;
161 while (1)
163 switch (i->type)
165 case ERT_CLEANUP:
166 case ERT_MUST_NOT_THROW:
167 break;
169 case ERT_TRY:
171 eh_catch c;
172 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
173 record_type_list (node, c->type_list);
175 break;
177 case ERT_ALLOWED_EXCEPTIONS:
178 record_type_list (node, i->u.allowed.type_list);
179 break;
181 /* If there are sub-regions, process them. */
182 if (i->inner)
183 i = i->inner;
184 /* If there are peers, process them. */
185 else if (i->next_peer)
186 i = i->next_peer;
187 /* Otherwise, step back up the tree to the next peer. */
188 else
192 i = i->outer;
193 if (i == NULL)
194 return;
196 while (i->next_peer == NULL);
197 i = i->next_peer;
202 /* Computes the frequency of the call statement so that it can be stored in
203 cgraph_edge. BB is the basic block of the call statement. */
205 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
207 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
208 (DECL_STRUCT_FUNCTION (decl))->frequency;
209 int freq = bb->frequency;
211 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
212 return CGRAPH_FREQ_BASE;
214 if (!entry_freq)
215 entry_freq = 1, freq++;
217 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
218 if (freq > CGRAPH_FREQ_MAX)
219 freq = CGRAPH_FREQ_MAX;
221 return freq;
224 /* Mark address taken in STMT. */
226 static bool
227 mark_address (gimple stmt, tree addr, void *data)
229 addr = get_base_address (addr);
230 if (TREE_CODE (addr) == FUNCTION_DECL)
232 struct cgraph_node *node = cgraph_get_create_node (addr);
233 cgraph_mark_address_taken_node (node);
234 ipa_record_reference ((symtab_node)data,
235 (symtab_node)node,
236 IPA_REF_ADDR, stmt);
238 else if (addr && TREE_CODE (addr) == VAR_DECL
239 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
241 struct varpool_node *vnode = varpool_node (addr);
242 int walk_subtrees;
244 if (lang_hooks.callgraph.analyze_expr)
245 lang_hooks.callgraph.analyze_expr (&addr, &walk_subtrees);
246 ipa_record_reference ((symtab_node)data,
247 (symtab_node)vnode,
248 IPA_REF_ADDR, stmt);
251 return false;
254 /* Mark load of T. */
256 static bool
257 mark_load (gimple stmt, tree t, void *data)
259 t = get_base_address (t);
260 if (t && TREE_CODE (t) == FUNCTION_DECL)
262 /* ??? This can happen on platforms with descriptors when these are
263 directly manipulated in the code. Pretend that it's an address. */
264 struct cgraph_node *node = cgraph_get_create_node (t);
265 cgraph_mark_address_taken_node (node);
266 ipa_record_reference ((symtab_node)data,
267 (symtab_node)node,
268 IPA_REF_ADDR, stmt);
270 else if (t && TREE_CODE (t) == VAR_DECL
271 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
273 struct varpool_node *vnode = varpool_node (t);
274 int walk_subtrees;
276 if (lang_hooks.callgraph.analyze_expr)
277 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
278 ipa_record_reference ((symtab_node)data,
279 (symtab_node)vnode,
280 IPA_REF_LOAD, stmt);
282 return false;
285 /* Mark store of T. */
287 static bool
288 mark_store (gimple stmt, tree t, void *data)
290 t = get_base_address (t);
291 if (t && TREE_CODE (t) == VAR_DECL
292 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
294 struct varpool_node *vnode = varpool_node (t);
295 int walk_subtrees;
297 if (lang_hooks.callgraph.analyze_expr)
298 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
299 ipa_record_reference ((symtab_node)data,
300 (symtab_node)vnode,
301 IPA_REF_STORE, stmt);
303 return false;
306 /* Create cgraph edges for function calls.
307 Also look for functions and variables having addresses taken. */
309 static unsigned int
310 build_cgraph_edges (void)
312 basic_block bb;
313 struct cgraph_node *node = cgraph_get_node (current_function_decl);
314 struct pointer_set_t *visited_nodes = pointer_set_create ();
315 gimple_stmt_iterator gsi;
316 tree decl;
317 unsigned ix;
319 /* Create the callgraph edges and record the nodes referenced by the function.
320 body. */
321 FOR_EACH_BB (bb)
323 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
325 gimple stmt = gsi_stmt (gsi);
326 tree decl;
328 if (is_gimple_call (stmt))
330 int freq = compute_call_stmt_bb_frequency (current_function_decl,
331 bb);
332 decl = gimple_call_fndecl (stmt);
333 if (decl)
334 cgraph_create_edge (node, cgraph_get_create_node (decl),
335 stmt, bb->count, freq);
336 else
337 cgraph_create_indirect_edge (node, stmt,
338 gimple_call_flags (stmt),
339 bb->count, freq);
341 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
342 mark_store, mark_address);
343 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
344 && gimple_omp_parallel_child_fn (stmt))
346 tree fn = gimple_omp_parallel_child_fn (stmt);
347 ipa_record_reference ((symtab_node)node,
348 (symtab_node)cgraph_get_create_node (fn),
349 IPA_REF_ADDR, stmt);
351 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
353 tree fn = gimple_omp_task_child_fn (stmt);
354 if (fn)
355 ipa_record_reference ((symtab_node)node,
356 (symtab_node) cgraph_get_create_node (fn),
357 IPA_REF_ADDR, stmt);
358 fn = gimple_omp_task_copy_fn (stmt);
359 if (fn)
360 ipa_record_reference ((symtab_node)node,
361 (symtab_node)cgraph_get_create_node (fn),
362 IPA_REF_ADDR, stmt);
365 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
366 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
367 mark_load, mark_store, mark_address);
370 /* Look for initializers of constant variables and private statics. */
371 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
372 if (TREE_CODE (decl) == VAR_DECL
373 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
374 varpool_finalize_decl (decl);
375 record_eh_tables (node, cfun);
377 pointer_set_destroy (visited_nodes);
378 return 0;
381 struct gimple_opt_pass pass_build_cgraph_edges =
384 GIMPLE_PASS,
385 "*build_cgraph_edges", /* name */
386 NULL, /* gate */
387 build_cgraph_edges, /* execute */
388 NULL, /* sub */
389 NULL, /* next */
390 0, /* static_pass_number */
391 TV_NONE, /* tv_id */
392 PROP_cfg, /* properties_required */
393 0, /* properties_provided */
394 0, /* properties_destroyed */
395 0, /* todo_flags_start */
396 0 /* todo_flags_finish */
400 /* Record references to functions and other variables present in the
401 initial value of DECL, a variable.
402 When ONLY_VARS is true, we mark needed only variables, not functions. */
404 void
405 record_references_in_initializer (tree decl, bool only_vars)
407 struct pointer_set_t *visited_nodes = pointer_set_create ();
408 struct varpool_node *node = varpool_node (decl);
409 struct record_reference_ctx ctx = {false, NULL};
411 ctx.varpool_node = node;
412 ctx.only_vars = only_vars;
413 walk_tree (&DECL_INITIAL (decl), record_reference,
414 &ctx, visited_nodes);
415 pointer_set_destroy (visited_nodes);
418 /* Rebuild cgraph edges for current function node. This needs to be run after
419 passes that don't update the cgraph. */
421 unsigned int
422 rebuild_cgraph_edges (void)
424 basic_block bb;
425 struct cgraph_node *node = cgraph_get_node (current_function_decl);
426 gimple_stmt_iterator gsi;
428 cgraph_node_remove_callees (node);
429 ipa_remove_all_references (&node->symbol.ref_list);
431 node->count = ENTRY_BLOCK_PTR->count;
433 FOR_EACH_BB (bb)
435 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
437 gimple stmt = gsi_stmt (gsi);
438 tree decl;
440 if (is_gimple_call (stmt))
442 int freq = compute_call_stmt_bb_frequency (current_function_decl,
443 bb);
444 decl = gimple_call_fndecl (stmt);
445 if (decl)
446 cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
447 bb->count, freq);
448 else
449 cgraph_create_indirect_edge (node, stmt,
450 gimple_call_flags (stmt),
451 bb->count, freq);
453 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
454 mark_store, mark_address);
457 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
458 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
459 mark_load, mark_store, mark_address);
461 record_eh_tables (node, cfun);
462 gcc_assert (!node->global.inlined_to);
464 return 0;
467 /* Rebuild cgraph edges for current function node. This needs to be run after
468 passes that don't update the cgraph. */
470 void
471 cgraph_rebuild_references (void)
473 basic_block bb;
474 struct cgraph_node *node = cgraph_get_node (current_function_decl);
475 gimple_stmt_iterator gsi;
477 ipa_remove_all_references (&node->symbol.ref_list);
479 node->count = ENTRY_BLOCK_PTR->count;
481 FOR_EACH_BB (bb)
483 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
485 gimple stmt = gsi_stmt (gsi);
487 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
488 mark_store, mark_address);
491 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
492 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
493 mark_load, mark_store, mark_address);
495 record_eh_tables (node, cfun);
498 struct gimple_opt_pass pass_rebuild_cgraph_edges =
501 GIMPLE_PASS,
502 "*rebuild_cgraph_edges", /* name */
503 NULL, /* gate */
504 rebuild_cgraph_edges, /* execute */
505 NULL, /* sub */
506 NULL, /* next */
507 0, /* static_pass_number */
508 TV_CGRAPH, /* tv_id */
509 PROP_cfg, /* properties_required */
510 0, /* properties_provided */
511 0, /* properties_destroyed */
512 0, /* todo_flags_start */
513 0, /* todo_flags_finish */
518 static unsigned int
519 remove_cgraph_callee_edges (void)
521 cgraph_node_remove_callees (cgraph_get_node (current_function_decl));
522 return 0;
525 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
528 GIMPLE_PASS,
529 "*remove_cgraph_callee_edges", /* name */
530 NULL, /* gate */
531 remove_cgraph_callee_edges, /* execute */
532 NULL, /* sub */
533 NULL, /* next */
534 0, /* static_pass_number */
535 TV_NONE, /* tv_id */
536 0, /* properties_required */
537 0, /* properties_provided */
538 0, /* properties_destroyed */
539 0, /* todo_flags_start */
540 0, /* todo_flags_finish */