lto-symtab.c (lto_varpool_replace_node): Do not merge needed flags.
[gcc.git] / gcc / cgraphbuild.c
1 /* Callgraph construction.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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"
37
38 /* Context of record_reference. */
39 struct record_reference_ctx
40 {
41 bool only_vars;
42 struct varpool_node *varpool_node;
43 };
44
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.
48 */
49
50 static tree
51 record_reference (tree *tp, int *walk_subtrees, void *data)
52 {
53 tree t = *tp;
54 tree decl;
55 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
56
57 t = canonicalize_constructor_val (t);
58 if (!t)
59 t = *tp;
60 else if (t != *tp)
61 *tp = t;
62
63 switch (TREE_CODE (t))
64 {
65 case VAR_DECL:
66 case FUNCTION_DECL:
67 gcc_unreachable ();
68 break;
69
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)
76 {
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);
83 }
84
85 if (TREE_CODE (decl) == VAR_DECL)
86 {
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);
93 }
94 *walk_subtrees = 0;
95 break;
96
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))
101 {
102 *walk_subtrees = 0;
103 break;
104 }
105
106 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
107 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
108 break;
109 }
110
111 return NULL_TREE;
112 }
113
114 /* Record references to typeinfos in the type list LIST. */
115
116 static void
117 record_type_list (struct cgraph_node *node, tree list)
118 {
119 for (; list; list = TREE_CHAIN (list))
120 {
121 tree type = TREE_VALUE (list);
122
123 if (TYPE_P (type))
124 type = lookup_type_for_runtime (type);
125 STRIP_NOPS (type);
126 if (TREE_CODE (type) == ADDR_EXPR)
127 {
128 type = TREE_OPERAND (type, 0);
129 if (TREE_CODE (type) == VAR_DECL)
130 {
131 struct varpool_node *vnode = varpool_node (type);
132 ipa_record_reference ((symtab_node)node,
133 (symtab_node)vnode,
134 IPA_REF_ADDR, NULL);
135 }
136 }
137 }
138 }
139
140 /* Record all references we will introduce by producing EH tables
141 for NODE. */
142
143 static void
144 record_eh_tables (struct cgraph_node *node, struct function *fun)
145 {
146 eh_region i;
147
148 if (DECL_FUNCTION_PERSONALITY (node->symbol.decl))
149 {
150 struct cgraph_node *per_node;
151
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);
155 }
156
157 i = fun->eh->region_tree;
158 if (!i)
159 return;
160
161 while (1)
162 {
163 switch (i->type)
164 {
165 case ERT_CLEANUP:
166 case ERT_MUST_NOT_THROW:
167 break;
168
169 case ERT_TRY:
170 {
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);
174 }
175 break;
176
177 case ERT_ALLOWED_EXCEPTIONS:
178 record_type_list (node, i->u.allowed.type_list);
179 break;
180 }
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
189 {
190 do
191 {
192 i = i->outer;
193 if (i == NULL)
194 return;
195 }
196 while (i->next_peer == NULL);
197 i = i->next_peer;
198 }
199 }
200 }
201
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. */
204 int
205 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
206 {
207 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
208 (DECL_STRUCT_FUNCTION (decl))->frequency;
209 int freq = bb->frequency;
210
211 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
212 return CGRAPH_FREQ_BASE;
213
214 if (!entry_freq)
215 entry_freq = 1, freq++;
216
217 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
218 if (freq > CGRAPH_FREQ_MAX)
219 freq = CGRAPH_FREQ_MAX;
220
221 return freq;
222 }
223
224 /* Mark address taken in STMT. */
225
226 static bool
227 mark_address (gimple stmt, tree addr, void *data)
228 {
229 addr = get_base_address (addr);
230 if (TREE_CODE (addr) == FUNCTION_DECL)
231 {
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);
237 }
238 else if (addr && TREE_CODE (addr) == VAR_DECL
239 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
240 {
241 struct varpool_node *vnode = varpool_node (addr);
242 int walk_subtrees;
243
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);
249 }
250
251 return false;
252 }
253
254 /* Mark load of T. */
255
256 static bool
257 mark_load (gimple stmt, tree t, void *data)
258 {
259 t = get_base_address (t);
260 if (t && TREE_CODE (t) == FUNCTION_DECL)
261 {
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);
269 }
270 else if (t && TREE_CODE (t) == VAR_DECL
271 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
272 {
273 struct varpool_node *vnode = varpool_node (t);
274 int walk_subtrees;
275
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);
281 }
282 return false;
283 }
284
285 /* Mark store of T. */
286
287 static bool
288 mark_store (gimple stmt, tree t, void *data)
289 {
290 t = get_base_address (t);
291 if (t && TREE_CODE (t) == VAR_DECL
292 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
293 {
294 struct varpool_node *vnode = varpool_node (t);
295 int walk_subtrees;
296
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);
302 }
303 return false;
304 }
305
306 /* Create cgraph edges for function calls.
307 Also look for functions and variables having addresses taken. */
308
309 static unsigned int
310 build_cgraph_edges (void)
311 {
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;
318
319 /* Create the callgraph edges and record the nodes referenced by the function.
320 body. */
321 FOR_EACH_BB (bb)
322 {
323 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
324 {
325 gimple stmt = gsi_stmt (gsi);
326 tree decl;
327
328 if (is_gimple_call (stmt))
329 {
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);
340 }
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))
345 {
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);
350 }
351 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
352 {
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);
363 }
364 }
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);
368 }
369
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);
376
377 pointer_set_destroy (visited_nodes);
378 return 0;
379 }
380
381 struct gimple_opt_pass pass_build_cgraph_edges =
382 {
383 {
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 */
397 }
398 };
399
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. */
403
404 void
405 record_references_in_initializer (tree decl, bool only_vars)
406 {
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};
410
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);
416 }
417
418 /* Rebuild cgraph edges for current function node. This needs to be run after
419 passes that don't update the cgraph. */
420
421 unsigned int
422 rebuild_cgraph_edges (void)
423 {
424 basic_block bb;
425 struct cgraph_node *node = cgraph_get_node (current_function_decl);
426 gimple_stmt_iterator gsi;
427
428 cgraph_node_remove_callees (node);
429 ipa_remove_all_references (&node->symbol.ref_list);
430
431 node->count = ENTRY_BLOCK_PTR->count;
432
433 FOR_EACH_BB (bb)
434 {
435 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
436 {
437 gimple stmt = gsi_stmt (gsi);
438 tree decl;
439
440 if (is_gimple_call (stmt))
441 {
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);
452 }
453 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
454 mark_store, mark_address);
455
456 }
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);
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_get_node (current_function_decl);
475 gimple_stmt_iterator gsi;
476
477 ipa_remove_all_references (&node->symbol.ref_list);
478
479 node->count = ENTRY_BLOCK_PTR->count;
480
481 FOR_EACH_BB (bb)
482 {
483 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
484 {
485 gimple stmt = gsi_stmt (gsi);
486
487 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
488 mark_store, mark_address);
489
490 }
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);
494 }
495 record_eh_tables (node, cfun);
496 }
497
498 struct gimple_opt_pass pass_rebuild_cgraph_edges =
499 {
500 {
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 */
514 }
515 };
516
517
518 static unsigned int
519 remove_cgraph_callee_edges (void)
520 {
521 cgraph_node_remove_callees (cgraph_get_node (current_function_decl));
522 return 0;
523 }
524
525 struct gimple_opt_pass pass_remove_cgraph_callee_edges =
526 {
527 {
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 */
541 }
542 };