fixing all errors
[official-gcc.git] / gcc / cgraphbuild.c
blob6b8116a612a53bc1de2c2c8625a890f3705d6707
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"
37 /* Context of record_reference. */
38 struct record_reference_ctx
40 bool only_vars;
41 struct varpool_node *varpool_node;
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.
49 static tree
50 record_reference (tree *tp, int *walk_subtrees, void *data)
52 tree t = *tp;
53 tree decl;
54 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
56 switch (TREE_CODE (t))
58 case VAR_DECL:
59 case FUNCTION_DECL:
60 gcc_unreachable ();
61 break;
63 case FDESC_EXPR:
64 case ADDR_EXPR:
65 /* Record dereferences to the functions. This makes the
66 functions reachable unconditionally. */
67 decl = get_base_var (*tp);
68 if (TREE_CODE (decl) == FUNCTION_DECL)
70 if (!ctx->only_vars)
71 cgraph_mark_address_taken_node (cgraph_node (decl));
72 ipa_record_reference (NULL, ctx->varpool_node,
73 cgraph_node (decl), NULL,
74 IPA_REF_ADDR, NULL);
77 if (TREE_CODE (decl) == VAR_DECL)
79 struct varpool_node *vnode = varpool_node (decl);
80 if (lang_hooks.callgraph.analyze_expr)
81 lang_hooks.callgraph.analyze_expr (&decl, walk_subtrees);
82 varpool_mark_needed_node (vnode);
83 if (vnode->alias && vnode->extra_name)
84 vnode = vnode->extra_name;
85 ipa_record_reference (NULL, ctx->varpool_node,
86 NULL, vnode,
87 IPA_REF_ADDR, NULL);
89 *walk_subtrees = 0;
90 break;
92 default:
93 /* Save some cycles by not walking types and declaration as we
94 won't find anything useful there anyway. */
95 if (IS_TYPE_OR_DECL_P (*tp))
97 *walk_subtrees = 0;
98 break;
101 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
102 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
103 break;
106 return NULL_TREE;
109 /* Record references to typeinfos in the type list LIST. */
111 static void
112 record_type_list (struct cgraph_node *node, tree list)
114 for (; list; list = TREE_CHAIN (list))
116 tree type = TREE_VALUE (list);
118 if (TYPE_P (type))
119 type = lookup_type_for_runtime (type);
120 STRIP_NOPS (type);
121 if (TREE_CODE (type) == ADDR_EXPR)
123 type = TREE_OPERAND (type, 0);
124 if (TREE_CODE (type) == VAR_DECL)
126 struct varpool_node *vnode = varpool_node (type);
127 varpool_mark_needed_node (vnode);
128 ipa_record_reference (node, NULL,
129 NULL, vnode,
130 IPA_REF_ADDR, NULL);
136 /* Record all references we will introduce by producing EH tables
137 for NODE. */
139 static void
140 record_eh_tables (struct cgraph_node *node, struct function *fun)
142 eh_region i;
144 if (DECL_FUNCTION_PERSONALITY (node->decl))
145 ipa_record_reference (node, NULL,
146 cgraph_node (DECL_FUNCTION_PERSONALITY (node->decl)),
147 NULL, IPA_REF_ADDR, NULL);
149 i = fun->eh->region_tree;
150 if (!i)
151 return;
153 while (1)
155 switch (i->type)
157 case ERT_CLEANUP:
158 case ERT_MUST_NOT_THROW:
159 break;
161 case ERT_TRY:
163 eh_catch c;
164 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
165 record_type_list (node, c->type_list);
167 break;
169 case ERT_ALLOWED_EXCEPTIONS:
170 record_type_list (node, i->u.allowed.type_list);
171 break;
173 /* If there are sub-regions, process them. */
174 if (i->inner)
175 i = i->inner;
176 /* If there are peers, process them. */
177 else if (i->next_peer)
178 i = i->next_peer;
179 /* Otherwise, step back up the tree to the next peer. */
180 else
184 i = i->outer;
185 if (i == NULL)
186 return;
188 while (i->next_peer == NULL);
189 i = i->next_peer;
194 /* Reset inlining information of all incoming call edges of NODE. */
196 void
197 reset_inline_failed (struct cgraph_node *node)
199 struct cgraph_edge *e;
201 for (e = node->callers; e; e = e->next_caller)
203 e->callee->global.inlined_to = NULL;
204 if (!node->analyzed)
205 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
206 else if (node->local.redefined_extern_inline)
207 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
208 else if (!node->local.inlinable)
209 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
210 else if (e->call_stmt_cannot_inline_p)
211 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
212 else
213 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
217 /* Computes the frequency of the call statement so that it can be stored in
218 cgraph_edge. BB is the basic block of the call statement. */
220 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
222 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
223 (DECL_STRUCT_FUNCTION (decl))->frequency;
224 int freq = bb->frequency;
226 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
227 return CGRAPH_FREQ_BASE;
229 if (!entry_freq)
230 entry_freq = 1, freq++;
232 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
233 if (freq > CGRAPH_FREQ_MAX)
234 freq = CGRAPH_FREQ_MAX;
236 return freq;
239 /* Mark address taken in STMT. */
241 static bool
242 mark_address (gimple stmt, tree addr, void *data)
244 addr = get_base_address (addr);
245 if (TREE_CODE (addr) == FUNCTION_DECL)
247 struct cgraph_node *node = cgraph_node (addr);
248 cgraph_mark_address_taken_node (node);
249 ipa_record_reference ((struct cgraph_node *)data, NULL,
250 node, NULL,
251 IPA_REF_ADDR, stmt);
253 else if (addr && TREE_CODE (addr) == VAR_DECL
254 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
256 struct varpool_node *vnode = varpool_node (addr);
257 int walk_subtrees;
259 if (lang_hooks.callgraph.analyze_expr)
260 lang_hooks.callgraph.analyze_expr (&addr, &walk_subtrees);
261 varpool_mark_needed_node (vnode);
262 if (vnode->alias && vnode->extra_name)
263 vnode = vnode->extra_name;
264 ipa_record_reference ((struct cgraph_node *)data, NULL,
265 NULL, vnode,
266 IPA_REF_ADDR, stmt);
269 return false;
272 /* Mark load of T. */
274 static bool
275 mark_load (gimple stmt, tree t, void *data)
277 t = get_base_address (t);
278 if (t && TREE_CODE (t) == FUNCTION_DECL)
280 /* ??? This can happen on platforms with descriptors when these are
281 directly manipulated in the code. Pretend that it's an address. */
282 struct cgraph_node *node = cgraph_node (t);
283 cgraph_mark_address_taken_node (node);
284 ipa_record_reference ((struct cgraph_node *)data, NULL,
285 node, NULL,
286 IPA_REF_ADDR, stmt);
288 else if (t && TREE_CODE (t) == VAR_DECL
289 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
291 struct varpool_node *vnode = varpool_node (t);
292 int walk_subtrees;
294 if (lang_hooks.callgraph.analyze_expr)
295 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
296 varpool_mark_needed_node (vnode);
297 if (vnode->alias && vnode->extra_name)
298 vnode = vnode->extra_name;
299 ipa_record_reference ((struct cgraph_node *)data, NULL,
300 NULL, vnode,
301 IPA_REF_LOAD, stmt);
303 return false;
306 /* Mark store of T. */
308 static bool
309 mark_store (gimple stmt, tree t, void *data)
311 t = get_base_address (t);
312 if (t && TREE_CODE (t) == VAR_DECL
313 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
315 struct varpool_node *vnode = varpool_node (t);
316 int walk_subtrees;
318 if (lang_hooks.callgraph.analyze_expr)
319 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
320 varpool_mark_needed_node (vnode);
321 if (vnode->alias && vnode->extra_name)
322 vnode = vnode->extra_name;
323 ipa_record_reference ((struct cgraph_node *)data, NULL,
324 NULL, vnode,
325 IPA_REF_STORE, stmt);
327 return false;
330 /* Create cgraph edges for function calls.
331 Also look for functions and variables having addresses taken. */
333 static unsigned int
334 build_cgraph_edges (void)
336 basic_block bb;
337 struct cgraph_node *node = cgraph_node (current_function_decl);
338 struct pointer_set_t *visited_nodes = pointer_set_create ();
339 gimple_stmt_iterator gsi;
340 tree decl;
341 unsigned ix;
343 /* Create the callgraph edges and record the nodes referenced by the function.
344 body. */
345 FOR_EACH_BB (bb)
347 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
349 gimple stmt = gsi_stmt (gsi);
350 tree decl;
352 if (is_gimple_call (stmt))
354 int freq = compute_call_stmt_bb_frequency (current_function_decl,
355 bb);
356 decl = gimple_call_fndecl (stmt);
357 if (decl)
358 cgraph_create_edge (node, cgraph_node (decl), stmt,
359 bb->count, freq,
360 bb->loop_depth);
361 else
362 cgraph_create_indirect_edge (node, stmt,
363 gimple_call_flags (stmt),
364 bb->count, freq,
365 bb->loop_depth);
367 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
368 mark_store, mark_address);
369 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
370 && gimple_omp_parallel_child_fn (stmt))
372 tree fn = gimple_omp_parallel_child_fn (stmt);
373 ipa_record_reference (node, NULL, cgraph_node (fn),
374 NULL, IPA_REF_ADDR, stmt);
376 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
378 tree fn = gimple_omp_task_child_fn (stmt);
379 if (fn)
380 ipa_record_reference (node, NULL, cgraph_node (fn),
381 NULL, IPA_REF_ADDR, stmt);
382 fn = gimple_omp_task_copy_fn (stmt);
383 if (fn)
384 ipa_record_reference (node, NULL, cgraph_node (fn),
385 NULL, IPA_REF_ADDR, stmt);
388 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
389 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
390 mark_load, mark_store, mark_address);
393 /* Look for initializers of constant variables and private statics. */
394 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
395 if (TREE_CODE (decl) == VAR_DECL
396 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
397 varpool_finalize_decl (decl);
398 record_eh_tables (node, cfun);
400 pointer_set_destroy (visited_nodes);
401 return 0;
404 struct gimple_opt_pass pass_build_cgraph_edges =
407 GIMPLE_PASS,
408 "*build_cgraph_edges", /* name */
409 NULL, /* gate */
410 build_cgraph_edges, /* execute */
411 NULL, /* sub */
412 NULL, /* next */
413 0, /* static_pass_number */
414 TV_NONE, /* tv_id */
415 PROP_cfg, /* properties_required */
416 0, /* properties_provided */
417 0, /* properties_destroyed */
418 0, /* todo_flags_start */
419 0 /* todo_flags_finish */
423 /* Record references to functions and other variables present in the
424 initial value of DECL, a variable.
425 When ONLY_VARS is true, we mark needed only variables, not functions. */
427 void
428 record_references_in_initializer (tree decl, bool only_vars)
430 struct pointer_set_t *visited_nodes = pointer_set_create ();
431 struct varpool_node *node = varpool_node (decl);
432 struct record_reference_ctx ctx = {false, NULL};
434 ctx.varpool_node = node;
435 ctx.only_vars = only_vars;
436 walk_tree (&DECL_INITIAL (decl), record_reference,
437 &ctx, visited_nodes);
438 pointer_set_destroy (visited_nodes);
441 /* Rebuild cgraph edges for current function node. This needs to be run after
442 passes that don't update the cgraph. */
444 unsigned int
445 rebuild_cgraph_edges (void)
447 basic_block bb;
448 struct cgraph_node *node = cgraph_node (current_function_decl);
449 gimple_stmt_iterator gsi;
451 cgraph_node_remove_callees (node);
452 ipa_remove_all_references (&node->ref_list);
454 node->count = ENTRY_BLOCK_PTR->count;
456 FOR_EACH_BB (bb)
458 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
460 gimple stmt = gsi_stmt (gsi);
461 tree decl;
463 if (is_gimple_call (stmt))
465 int freq = compute_call_stmt_bb_frequency (current_function_decl,
466 bb);
467 decl = gimple_call_fndecl (stmt);
468 if (decl)
469 cgraph_create_edge (node, cgraph_node (decl), stmt,
470 bb->count, freq,
471 bb->loop_depth);
472 else
473 cgraph_create_indirect_edge (node, stmt,
474 gimple_call_flags (stmt),
475 bb->count, freq,
476 bb->loop_depth);
478 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
479 mark_store, mark_address);
482 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
483 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
484 mark_load, mark_store, mark_address);
486 record_eh_tables (node, cfun);
487 gcc_assert (!node->global.inlined_to);
489 return 0;
492 /* Rebuild cgraph edges for current function node. This needs to be run after
493 passes that don't update the cgraph. */
495 void
496 cgraph_rebuild_references (void)
498 basic_block bb;
499 struct cgraph_node *node = cgraph_node (current_function_decl);
500 gimple_stmt_iterator gsi;
502 ipa_remove_all_references (&node->ref_list);
504 node->count = ENTRY_BLOCK_PTR->count;
506 FOR_EACH_BB (bb)
508 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
510 gimple stmt = gsi_stmt (gsi);
512 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
513 mark_store, mark_address);
516 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
517 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
518 mark_load, mark_store, mark_address);
520 record_eh_tables (node, cfun);
523 struct gimple_opt_pass pass_rebuild_cgraph_edges =
526 GIMPLE_PASS,
527 "*rebuild_cgraph_edges", /* name */
528 NULL, /* gate */
529 rebuild_cgraph_edges, /* execute */
530 NULL, /* sub */
531 NULL, /* next */
532 0, /* static_pass_number */
533 TV_CGRAPH, /* tv_id */
534 PROP_cfg, /* properties_required */
535 0, /* properties_provided */
536 0, /* properties_destroyed */
537 0, /* todo_flags_start */
538 0, /* todo_flags_finish */
543 static unsigned int
544 remove_cgraph_callee_edges (void)
546 cgraph_node_remove_callees (cgraph_node (current_function_decl));
547 return 0;
550 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
553 GIMPLE_PASS,
554 "*remove_cgraph_callee_edges", /* name */
555 NULL, /* gate */
556 remove_cgraph_callee_edges, /* execute */
557 NULL, /* sub */
558 NULL, /* next */
559 0, /* static_pass_number */
560 TV_NONE, /* tv_id */
561 0, /* properties_required */
562 0, /* properties_provided */
563 0, /* properties_destroyed */
564 0, /* todo_flags_start */
565 0, /* todo_flags_finish */