]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphbuild.c
re PR fortran/47339 (Fortran 2003/2008: Valid NAMELIST rejected; Fortran 95: Invalid...
[thirdparty/gcc.git] / gcc / cgraphbuild.c
CommitLineData
2dee695b 1/* Callgraph construction.
d652f226 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
726a989a 3 Free Software Foundation, Inc.
2dee695b
JH
4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
2dee695b
JH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
2dee695b
JH
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"
726a989a 32#include "gimple.h"
2dee695b 33#include "tree-pass.h"
85912441 34#include "ipa-utils.h"
de61f467 35#include "except.h"
85912441
JH
36
37/* Context of record_reference. */
38struct record_reference_ctx
39{
40 bool only_vars;
369451ec 41 struct varpool_node *varpool_node;
85912441 42};
2dee695b
JH
43
44/* Walk tree and record all calls and references to functions/variables.
b8698a0f 45 Called via walk_tree: TP is pointer to tree to be examined.
625f802c
JH
46 When DATA is non-null, record references to callgraph.
47 */
2dee695b
JH
48
49static tree
625f802c 50record_reference (tree *tp, int *walk_subtrees, void *data)
2dee695b
JH
51{
52 tree t = *tp;
7e8b322a 53 tree decl;
85912441 54 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
2dee695b
JH
55
56 switch (TREE_CODE (t))
57 {
58 case VAR_DECL:
85912441
JH
59 case FUNCTION_DECL:
60 gcc_unreachable ();
2dee695b
JH
61 break;
62
63 case FDESC_EXPR:
64 case ADDR_EXPR:
7e8b322a
JH
65 /* Record dereferences to the functions. This makes the
66 functions reachable unconditionally. */
85912441 67 decl = get_base_var (*tp);
369451ec
JH
68 if (TREE_CODE (decl) == FUNCTION_DECL)
69 {
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);
75 }
85912441
JH
76
77 if (TREE_CODE (decl) == VAR_DECL)
78 {
369451ec 79 struct varpool_node *vnode = varpool_node (decl);
85912441
JH
80 if (lang_hooks.callgraph.analyze_expr)
81 lang_hooks.callgraph.analyze_expr (&decl, walk_subtrees);
369451ec
JH
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);
85912441
JH
88 }
89 *walk_subtrees = 0;
2dee695b
JH
90 break;
91
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))
96 {
97 *walk_subtrees = 0;
98 break;
99 }
100
101 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
c4e622b6 102 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees);
2dee695b
JH
103 break;
104 }
105
106 return NULL_TREE;
107}
108
de61f467
JH
109/* Record references to typeinfos in the type list LIST. */
110
111static void
112record_type_list (struct cgraph_node *node, tree list)
113{
114 for (; list; list = TREE_CHAIN (list))
115 {
116 tree type = TREE_VALUE (list);
117
118 if (TYPE_P (type))
119 type = lookup_type_for_runtime (type);
120 STRIP_NOPS (type);
121 if (TREE_CODE (type) == ADDR_EXPR)
122 {
123 type = TREE_OPERAND (type, 0);
124 if (TREE_CODE (type) == VAR_DECL)
125 {
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);
131 }
132 }
133 }
134}
135
136/* Record all references we will introduce by producing EH tables
137 for NODE. */
138
139static void
140record_eh_tables (struct cgraph_node *node, struct function *fun)
141{
142 eh_region i;
143
144 i = fun->eh->region_tree;
145 if (!i)
146 return;
147
148 while (1)
149 {
150 switch (i->type)
151 {
152 case ERT_CLEANUP:
153 case ERT_MUST_NOT_THROW:
154 break;
155
156 case ERT_TRY:
157 {
158 eh_catch c;
159 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
160 record_type_list (node, c->type_list);
161 }
162 break;
163
164 case ERT_ALLOWED_EXCEPTIONS:
165 record_type_list (node, i->u.allowed.type_list);
166 break;
167 }
168 /* If there are sub-regions, process them. */
169 if (i->inner)
170 i = i->inner;
171 /* If there are peers, process them. */
172 else if (i->next_peer)
173 i = i->next_peer;
174 /* Otherwise, step back up the tree to the next peer. */
175 else
176 {
177 do
178 {
179 i = i->outer;
180 if (i == NULL)
181 return;
182 }
183 while (i->next_peer == NULL);
184 i = i->next_peer;
185 }
186 }
187}
188
d7f09764
DN
189/* Reset inlining information of all incoming call edges of NODE. */
190
191void
192reset_inline_failed (struct cgraph_node *node)
193{
194 struct cgraph_edge *e;
195
196 for (e = node->callers; e; e = e->next_caller)
197 {
198 e->callee->global.inlined_to = NULL;
199 if (!node->analyzed)
200 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
201 else if (node->local.redefined_extern_inline)
202 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
203 else if (!node->local.inlinable)
204 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
205 else if (e->call_stmt_cannot_inline_p)
206 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
207 else
208 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
209 }
210}
211
3e293154
MJ
212/* Computes the frequency of the call statement so that it can be stored in
213 cgraph_edge. BB is the basic block of the call statement. */
214int
9187e02d 215compute_call_stmt_bb_frequency (tree decl, basic_block bb)
3e293154 216{
0d63a740
JH
217 int entry_freq = ENTRY_BLOCK_PTR_FOR_FUNCTION
218 (DECL_STRUCT_FUNCTION (decl))->frequency;
c0ee0021 219 int freq = bb->frequency;
3e293154 220
9187e02d
JH
221 if (profile_status_for_function (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
222 return CGRAPH_FREQ_BASE;
223
3e293154 224 if (!entry_freq)
c0ee0021 225 entry_freq = 1, freq++;
3e293154 226
c0ee0021 227 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
3e293154
MJ
228 if (freq > CGRAPH_FREQ_MAX)
229 freq = CGRAPH_FREQ_MAX;
230
231 return freq;
232}
233
85912441
JH
234/* Mark address taken in STMT. */
235
236static bool
3ebb5ca6 237mark_address (gimple stmt, tree addr, void *data)
85912441 238{
e9615971 239 addr = get_base_address (addr);
85912441
JH
240 if (TREE_CODE (addr) == FUNCTION_DECL)
241 {
242 struct cgraph_node *node = cgraph_node (addr);
243 cgraph_mark_address_taken_node (node);
369451ec
JH
244 ipa_record_reference ((struct cgraph_node *)data, NULL,
245 node, NULL,
246 IPA_REF_ADDR, stmt);
85912441 247 }
e9615971
RG
248 else if (addr && TREE_CODE (addr) == VAR_DECL
249 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
85912441 250 {
e9615971
RG
251 struct varpool_node *vnode = varpool_node (addr);
252 int walk_subtrees;
85912441 253
e9615971
RG
254 if (lang_hooks.callgraph.analyze_expr)
255 lang_hooks.callgraph.analyze_expr (&addr, &walk_subtrees);
256 varpool_mark_needed_node (vnode);
257 if (vnode->alias && vnode->extra_name)
258 vnode = vnode->extra_name;
259 ipa_record_reference ((struct cgraph_node *)data, NULL,
260 NULL, vnode,
261 IPA_REF_ADDR, stmt);
85912441
JH
262 }
263
264 return false;
265}
266
267/* Mark load of T. */
268
269static bool
3ebb5ca6 270mark_load (gimple stmt, tree t, void *data)
85912441
JH
271{
272 t = get_base_address (t);
3ebb5ca6
EB
273 if (t && TREE_CODE (t) == FUNCTION_DECL)
274 {
275 /* ??? This can happen on platforms with descriptors when these are
276 directly manipulated in the code. Pretend that it's an address. */
277 struct cgraph_node *node = cgraph_node (t);
278 cgraph_mark_address_taken_node (node);
279 ipa_record_reference ((struct cgraph_node *)data, NULL,
280 node, NULL,
281 IPA_REF_ADDR, stmt);
282 }
283 else if (t && TREE_CODE (t) == VAR_DECL
284 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
85912441
JH
285 {
286 struct varpool_node *vnode = varpool_node (t);
287 int walk_subtrees;
288
289 if (lang_hooks.callgraph.analyze_expr)
290 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
291 varpool_mark_needed_node (vnode);
369451ec
JH
292 if (vnode->alias && vnode->extra_name)
293 vnode = vnode->extra_name;
294 ipa_record_reference ((struct cgraph_node *)data, NULL,
295 NULL, vnode,
296 IPA_REF_LOAD, stmt);
85912441
JH
297 }
298 return false;
299}
300
301/* Mark store of T. */
302
303static bool
3ebb5ca6 304mark_store (gimple stmt, tree t, void *data)
85912441
JH
305{
306 t = get_base_address (t);
70f34814 307 if (t && TREE_CODE (t) == VAR_DECL
85912441
JH
308 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
309 {
310 struct varpool_node *vnode = varpool_node (t);
311 int walk_subtrees;
312
313 if (lang_hooks.callgraph.analyze_expr)
314 lang_hooks.callgraph.analyze_expr (&t, &walk_subtrees);
315 varpool_mark_needed_node (vnode);
369451ec
JH
316 if (vnode->alias && vnode->extra_name)
317 vnode = vnode->extra_name;
318 ipa_record_reference ((struct cgraph_node *)data, NULL,
319 NULL, vnode,
3ebb5ca6 320 IPA_REF_STORE, stmt);
85912441
JH
321 }
322 return false;
323}
324
2dee695b
JH
325/* Create cgraph edges for function calls.
326 Also look for functions and variables having addresses taken. */
327
328static unsigned int
329build_cgraph_edges (void)
330{
331 basic_block bb;
332 struct cgraph_node *node = cgraph_node (current_function_decl);
333 struct pointer_set_t *visited_nodes = pointer_set_create ();
726a989a 334 gimple_stmt_iterator gsi;
c021f10b
NF
335 tree decl;
336 unsigned ix;
2dee695b
JH
337
338 /* Create the callgraph edges and record the nodes referenced by the function.
339 body. */
340 FOR_EACH_BB (bb)
85912441
JH
341 {
342 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
343 {
344 gimple stmt = gsi_stmt (gsi);
345 tree decl;
346
5f902d76
JH
347 if (is_gimple_call (stmt))
348 {
349 int freq = compute_call_stmt_bb_frequency (current_function_decl,
350 bb);
351 decl = gimple_call_fndecl (stmt);
352 if (decl)
353 cgraph_create_edge (node, cgraph_node (decl), stmt,
354 bb->count, freq,
355 bb->loop_depth);
356 else
357 cgraph_create_indirect_edge (node, stmt,
358 gimple_call_flags (stmt),
359 bb->count, freq,
360 bb->loop_depth);
361 }
85912441
JH
362 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
363 mark_store, mark_address);
364 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
365 && gimple_omp_parallel_child_fn (stmt))
366 {
367 tree fn = gimple_omp_parallel_child_fn (stmt);
8c380037
JH
368 ipa_record_reference (node, NULL, cgraph_node (fn),
369 NULL, IPA_REF_ADDR, stmt);
85912441
JH
370 }
371 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
372 {
373 tree fn = gimple_omp_task_child_fn (stmt);
374 if (fn)
8c380037
JH
375 ipa_record_reference (node, NULL, cgraph_node (fn),
376 NULL, IPA_REF_ADDR, stmt);
85912441
JH
377 fn = gimple_omp_task_copy_fn (stmt);
378 if (fn)
8c380037
JH
379 ipa_record_reference (node, NULL, cgraph_node (fn),
380 NULL, IPA_REF_ADDR, stmt);
85912441
JH
381 }
382 }
383 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
384 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
385 mark_load, mark_store, mark_address);
386 }
2dee695b
JH
387
388 /* Look for initializers of constant variables and private statics. */
c021f10b
NF
389 FOR_EACH_LOCAL_DECL (cfun, ix, decl)
390 if (TREE_CODE (decl) == VAR_DECL
391 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
392 varpool_finalize_decl (decl);
de61f467 393 record_eh_tables (node, cfun);
2dee695b
JH
394
395 pointer_set_destroy (visited_nodes);
2dee695b
JH
396 return 0;
397}
398
8ddbbcae 399struct gimple_opt_pass pass_build_cgraph_edges =
2dee695b 400{
8ddbbcae
JH
401 {
402 GIMPLE_PASS,
e0a42b0f 403 "*build_cgraph_edges", /* name */
2dee695b
JH
404 NULL, /* gate */
405 build_cgraph_edges, /* execute */
406 NULL, /* sub */
407 NULL, /* next */
408 0, /* static_pass_number */
7072a650 409 TV_NONE, /* tv_id */
2dee695b
JH
410 PROP_cfg, /* properties_required */
411 0, /* properties_provided */
412 0, /* properties_destroyed */
413 0, /* todo_flags_start */
8ddbbcae
JH
414 0 /* todo_flags_finish */
415 }
2dee695b
JH
416};
417
418/* Record references to functions and other variables present in the
b8698a0f 419 initial value of DECL, a variable.
625f802c 420 When ONLY_VARS is true, we mark needed only variables, not functions. */
2dee695b
JH
421
422void
625f802c 423record_references_in_initializer (tree decl, bool only_vars)
2dee695b
JH
424{
425 struct pointer_set_t *visited_nodes = pointer_set_create ();
369451ec
JH
426 struct varpool_node *node = varpool_node (decl);
427 struct record_reference_ctx ctx = {false, NULL};
85912441 428
369451ec 429 ctx.varpool_node = node;
85912441 430 ctx.only_vars = only_vars;
b8698a0f 431 walk_tree (&DECL_INITIAL (decl), record_reference,
85912441 432 &ctx, visited_nodes);
2dee695b
JH
433 pointer_set_destroy (visited_nodes);
434}
435
436/* Rebuild cgraph edges for current function node. This needs to be run after
437 passes that don't update the cgraph. */
438
917948d3 439unsigned int
2dee695b
JH
440rebuild_cgraph_edges (void)
441{
442 basic_block bb;
443 struct cgraph_node *node = cgraph_node (current_function_decl);
726a989a 444 gimple_stmt_iterator gsi;
2dee695b
JH
445
446 cgraph_node_remove_callees (node);
369451ec 447 ipa_remove_all_references (&node->ref_list);
2dee695b
JH
448
449 node->count = ENTRY_BLOCK_PTR->count;
450
451 FOR_EACH_BB (bb)
85912441
JH
452 {
453 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
454 {
455 gimple stmt = gsi_stmt (gsi);
456 tree decl;
457
5f902d76
JH
458 if (is_gimple_call (stmt))
459 {
460 int freq = compute_call_stmt_bb_frequency (current_function_decl,
461 bb);
462 decl = gimple_call_fndecl (stmt);
463 if (decl)
464 cgraph_create_edge (node, cgraph_node (decl), stmt,
465 bb->count, freq,
466 bb->loop_depth);
467 else
468 cgraph_create_indirect_edge (node, stmt,
469 gimple_call_flags (stmt),
470 bb->count, freq,
471 bb->loop_depth);
472 }
85912441
JH
473 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
474 mark_store, mark_address);
475
476 }
477 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
478 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
479 mark_load, mark_store, mark_address);
480 }
de61f467 481 record_eh_tables (node, cfun);
2dee695b 482 gcc_assert (!node->global.inlined_to);
3dc9eaa6 483
2dee695b
JH
484 return 0;
485}
486
99b766fc
JH
487/* Rebuild cgraph edges for current function node. This needs to be run after
488 passes that don't update the cgraph. */
489
490void
491cgraph_rebuild_references (void)
492{
493 basic_block bb;
494 struct cgraph_node *node = cgraph_node (current_function_decl);
495 gimple_stmt_iterator gsi;
496
497 ipa_remove_all_references (&node->ref_list);
498
499 node->count = ENTRY_BLOCK_PTR->count;
500
501 FOR_EACH_BB (bb)
502 {
503 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
504 {
505 gimple stmt = gsi_stmt (gsi);
506
507 walk_stmt_load_store_addr_ops (stmt, node, mark_load,
508 mark_store, mark_address);
509
510 }
511 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
512 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), node,
513 mark_load, mark_store, mark_address);
514 }
515 record_eh_tables (node, cfun);
516}
517
8ddbbcae 518struct gimple_opt_pass pass_rebuild_cgraph_edges =
2dee695b 519{
8ddbbcae
JH
520 {
521 GIMPLE_PASS,
e0a42b0f 522 "*rebuild_cgraph_edges", /* name */
2dee695b
JH
523 NULL, /* gate */
524 rebuild_cgraph_edges, /* execute */
525 NULL, /* sub */
526 NULL, /* next */
527 0, /* static_pass_number */
a222c01a 528 TV_CGRAPH, /* tv_id */
2dee695b
JH
529 PROP_cfg, /* properties_required */
530 0, /* properties_provided */
531 0, /* properties_destroyed */
532 0, /* todo_flags_start */
533 0, /* todo_flags_finish */
8ddbbcae 534 }
2dee695b 535};
133f9369
MJ
536
537
538static unsigned int
539remove_cgraph_callee_edges (void)
540{
541 cgraph_node_remove_callees (cgraph_node (current_function_decl));
542 return 0;
543}
544
545struct gimple_opt_pass pass_remove_cgraph_callee_edges =
546{
547 {
548 GIMPLE_PASS,
e0a42b0f 549 "*remove_cgraph_callee_edges", /* name */
133f9369
MJ
550 NULL, /* gate */
551 remove_cgraph_callee_edges, /* execute */
552 NULL, /* sub */
553 NULL, /* next */
554 0, /* static_pass_number */
7072a650 555 TV_NONE, /* tv_id */
133f9369
MJ
556 0, /* properties_required */
557 0, /* properties_provided */
558 0, /* properties_destroyed */
559 0, /* todo_flags_start */
560 0, /* todo_flags_finish */
561 }
562};