OpenACC loop construct.
[official-gcc.git] / gcc / cgraphbuild.c
blob542fba438a3bfa78daf17af6dc722b4465b09ae6
1 /* Callgraph construction.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "pointer-set.h"
27 #include "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-fold.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimple-iterator.h"
35 #include "gimple-walk.h"
36 #include "langhooks.h"
37 #include "intl.h"
38 #include "tree-pass.h"
39 #include "ipa-utils.h"
40 #include "except.h"
41 #include "ipa-inline.h"
43 /* Context of record_reference. */
44 struct record_reference_ctx
46 bool only_vars;
47 class varpool_node *varpool_node;
50 /* Walk tree and record all calls and references to functions/variables.
51 Called via walk_tree: TP is pointer to tree to be examined.
52 When DATA is non-null, record references to callgraph.
55 static tree
56 record_reference (tree *tp, int *walk_subtrees, void *data)
58 tree t = *tp;
59 tree decl;
60 struct record_reference_ctx *ctx = (struct record_reference_ctx *)data;
62 t = canonicalize_constructor_val (t, NULL);
63 if (!t)
64 t = *tp;
65 else if (t != *tp)
66 *tp = t;
68 switch (TREE_CODE (t))
70 case VAR_DECL:
71 case FUNCTION_DECL:
72 gcc_unreachable ();
73 break;
75 case FDESC_EXPR:
76 case ADDR_EXPR:
77 /* Record dereferences to the functions. This makes the
78 functions reachable unconditionally. */
79 decl = get_base_var (*tp);
80 if (TREE_CODE (decl) == FUNCTION_DECL)
82 struct cgraph_node *node = cgraph_get_create_node (decl);
83 if (!ctx->only_vars)
84 cgraph_mark_address_taken_node (node);
85 ipa_record_reference (ctx->varpool_node,
86 node,
87 IPA_REF_ADDR, NULL);
90 if (TREE_CODE (decl) == VAR_DECL)
92 varpool_node *vnode = varpool_node_for_decl (decl);
93 ipa_record_reference (ctx->varpool_node,
94 vnode,
95 IPA_REF_ADDR, NULL);
97 *walk_subtrees = 0;
98 break;
100 default:
101 /* Save some cycles by not walking types and declaration as we
102 won't find anything useful there anyway. */
103 if (IS_TYPE_OR_DECL_P (*tp))
105 *walk_subtrees = 0;
106 break;
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 varpool_node *vnode = varpool_node_for_decl (type);
132 ipa_record_reference (node,
133 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->decl))
150 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
151 struct cgraph_node *per_node = cgraph_get_create_node (per_decl);
153 ipa_record_reference (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_FN
208 (DECL_STRUCT_FUNCTION (decl))->frequency;
209 int freq = bb->frequency;
211 if (profile_status_for_fn (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, tree, 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 node,
236 IPA_REF_ADDR, stmt);
238 else if (addr && TREE_CODE (addr) == VAR_DECL
239 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
241 varpool_node *vnode = varpool_node_for_decl (addr);
243 ipa_record_reference ((symtab_node *)data,
244 vnode,
245 IPA_REF_ADDR, stmt);
248 return false;
251 /* Mark load of T. */
253 static bool
254 mark_load (gimple stmt, tree t, tree, void *data)
256 t = get_base_address (t);
257 if (t && TREE_CODE (t) == FUNCTION_DECL)
259 /* ??? This can happen on platforms with descriptors when these are
260 directly manipulated in the code. Pretend that it's an address. */
261 struct cgraph_node *node = cgraph_get_create_node (t);
262 cgraph_mark_address_taken_node (node);
263 ipa_record_reference ((symtab_node *)data,
264 node,
265 IPA_REF_ADDR, stmt);
267 else if (t && TREE_CODE (t) == VAR_DECL
268 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
270 varpool_node *vnode = varpool_node_for_decl (t);
272 ipa_record_reference ((symtab_node *)data,
273 vnode,
274 IPA_REF_LOAD, stmt);
276 return false;
279 /* Mark store of T. */
281 static bool
282 mark_store (gimple stmt, tree t, tree, void *data)
284 t = get_base_address (t);
285 if (t && TREE_CODE (t) == VAR_DECL
286 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
288 varpool_node *vnode = varpool_node_for_decl (t);
290 ipa_record_reference ((symtab_node *)data,
291 vnode,
292 IPA_REF_STORE, stmt);
294 return false;
297 /* Record all references from NODE that are taken in statement STMT. */
298 void
299 ipa_record_stmt_references (struct cgraph_node *node, gimple stmt)
301 walk_stmt_load_store_addr_ops (stmt, node, mark_load, mark_store,
302 mark_address);
305 /* Create cgraph edges for function calls.
306 Also look for functions and variables having addresses taken. */
308 namespace {
310 const pass_data pass_data_build_cgraph_edges =
312 GIMPLE_PASS, /* type */
313 "*build_cgraph_edges", /* name */
314 OPTGROUP_NONE, /* optinfo_flags */
315 true, /* has_execute */
316 TV_NONE, /* tv_id */
317 PROP_cfg, /* properties_required */
318 0, /* properties_provided */
319 0, /* properties_destroyed */
320 0, /* todo_flags_start */
321 0, /* todo_flags_finish */
324 class pass_build_cgraph_edges : public gimple_opt_pass
326 public:
327 pass_build_cgraph_edges (gcc::context *ctxt)
328 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
331 /* opt_pass methods: */
332 virtual unsigned int execute (function *);
334 }; // class pass_build_cgraph_edges
336 unsigned int
337 pass_build_cgraph_edges::execute (function *fun)
339 basic_block bb;
340 struct cgraph_node *node = cgraph_get_node (current_function_decl);
341 struct pointer_set_t *visited_nodes = pointer_set_create ();
342 gimple_stmt_iterator gsi;
343 tree decl;
344 unsigned ix;
346 /* Create the callgraph edges and record the nodes referenced by the function.
347 body. */
348 FOR_EACH_BB_FN (bb, fun)
350 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
352 gimple stmt = gsi_stmt (gsi);
353 tree decl;
355 if (is_gimple_debug (stmt))
356 continue;
358 if (is_gimple_call (stmt))
360 int freq = compute_call_stmt_bb_frequency (current_function_decl,
361 bb);
362 decl = gimple_call_fndecl (stmt);
363 if (decl)
364 cgraph_create_edge (node, cgraph_get_create_node (decl),
365 stmt, bb->count, freq);
366 else if (gimple_call_internal_p (stmt))
368 else
369 cgraph_create_indirect_edge (node, stmt,
370 gimple_call_flags (stmt),
371 bb->count, freq);
373 ipa_record_stmt_references (node, stmt);
374 if (gimple_code (stmt) == GIMPLE_OACC_PARALLEL
375 && gimple_oacc_parallel_child_fn (stmt))
377 tree fn = gimple_oacc_parallel_child_fn (stmt);
378 ipa_record_reference (node,
379 cgraph_get_create_node (fn),
380 IPA_REF_ADDR, stmt);
382 else if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
383 && gimple_omp_parallel_child_fn (stmt))
385 tree fn = gimple_omp_parallel_child_fn (stmt);
386 ipa_record_reference (node,
387 cgraph_get_create_node (fn),
388 IPA_REF_ADDR, stmt);
390 else if (gimple_code (stmt) == GIMPLE_OMP_TASK)
392 tree fn = gimple_omp_task_child_fn (stmt);
393 if (fn)
394 ipa_record_reference (node,
395 cgraph_get_create_node (fn),
396 IPA_REF_ADDR, stmt);
397 fn = gimple_omp_task_copy_fn (stmt);
398 if (fn)
399 ipa_record_reference (node,
400 cgraph_get_create_node (fn),
401 IPA_REF_ADDR, stmt);
404 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
405 ipa_record_stmt_references (node, gsi_stmt (gsi));
408 /* Look for initializers of constant variables and private statics. */
409 FOR_EACH_LOCAL_DECL (fun, ix, decl)
410 if (TREE_CODE (decl) == VAR_DECL
411 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
412 && !DECL_HAS_VALUE_EXPR_P (decl))
413 varpool_finalize_decl (decl);
414 record_eh_tables (node, fun);
416 pointer_set_destroy (visited_nodes);
417 return 0;
420 } // anon namespace
422 gimple_opt_pass *
423 make_pass_build_cgraph_edges (gcc::context *ctxt)
425 return new pass_build_cgraph_edges (ctxt);
428 /* Record references to functions and other variables present in the
429 initial value of DECL, a variable.
430 When ONLY_VARS is true, we mark needed only variables, not functions. */
432 void
433 record_references_in_initializer (tree decl, bool only_vars)
435 struct pointer_set_t *visited_nodes = pointer_set_create ();
436 varpool_node *node = varpool_node_for_decl (decl);
437 struct record_reference_ctx ctx = {false, NULL};
439 ctx.varpool_node = node;
440 ctx.only_vars = only_vars;
441 walk_tree (&DECL_INITIAL (decl), record_reference,
442 &ctx, visited_nodes);
443 pointer_set_destroy (visited_nodes);
446 /* Rebuild cgraph edges for current function node. This needs to be run after
447 passes that don't update the cgraph. */
449 unsigned int
450 rebuild_cgraph_edges (void)
452 basic_block bb;
453 struct cgraph_node *node = cgraph_get_node (current_function_decl);
454 gimple_stmt_iterator gsi;
456 cgraph_node_remove_callees (node);
457 ipa_remove_all_references (&node->ref_list);
459 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
461 FOR_EACH_BB_FN (bb, cfun)
463 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
465 gimple stmt = gsi_stmt (gsi);
466 tree decl;
468 if (is_gimple_call (stmt))
470 int freq = compute_call_stmt_bb_frequency (current_function_decl,
471 bb);
472 decl = gimple_call_fndecl (stmt);
473 if (decl)
474 cgraph_create_edge (node, cgraph_get_create_node (decl), stmt,
475 bb->count, freq);
476 else if (gimple_call_internal_p (stmt))
478 else
479 cgraph_create_indirect_edge (node, stmt,
480 gimple_call_flags (stmt),
481 bb->count, freq);
483 ipa_record_stmt_references (node, stmt);
485 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
486 ipa_record_stmt_references (node, gsi_stmt (gsi));
488 record_eh_tables (node, cfun);
489 gcc_assert (!node->global.inlined_to);
491 return 0;
494 /* Rebuild cgraph edges for current function node. This needs to be run after
495 passes that don't update the cgraph. */
497 void
498 cgraph_rebuild_references (void)
500 basic_block bb;
501 struct cgraph_node *node = cgraph_get_node (current_function_decl);
502 gimple_stmt_iterator gsi;
503 struct ipa_ref *ref;
504 int i;
506 /* Keep speculative references for further cgraph edge expansion. */
507 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref);)
508 if (!ref->speculative)
509 ipa_remove_reference (ref);
510 else
511 i++;
513 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
515 FOR_EACH_BB_FN (bb, cfun)
517 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
518 ipa_record_stmt_references (node, gsi_stmt (gsi));
519 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
520 ipa_record_stmt_references (node, gsi_stmt (gsi));
522 record_eh_tables (node, cfun);
525 namespace {
527 const pass_data pass_data_rebuild_cgraph_edges =
529 GIMPLE_PASS, /* type */
530 "*rebuild_cgraph_edges", /* name */
531 OPTGROUP_NONE, /* optinfo_flags */
532 true, /* has_execute */
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 */
541 class pass_rebuild_cgraph_edges : public gimple_opt_pass
543 public:
544 pass_rebuild_cgraph_edges (gcc::context *ctxt)
545 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
548 /* opt_pass methods: */
549 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
550 virtual unsigned int execute (function *) { return rebuild_cgraph_edges (); }
552 }; // class pass_rebuild_cgraph_edges
554 } // anon namespace
556 gimple_opt_pass *
557 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
559 return new pass_rebuild_cgraph_edges (ctxt);
563 namespace {
565 const pass_data pass_data_remove_cgraph_callee_edges =
567 GIMPLE_PASS, /* type */
568 "*remove_cgraph_callee_edges", /* name */
569 OPTGROUP_NONE, /* optinfo_flags */
570 true, /* has_execute */
571 TV_NONE, /* tv_id */
572 0, /* properties_required */
573 0, /* properties_provided */
574 0, /* properties_destroyed */
575 0, /* todo_flags_start */
576 0, /* todo_flags_finish */
579 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
581 public:
582 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
583 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
586 /* opt_pass methods: */
587 opt_pass * clone () {
588 return new pass_remove_cgraph_callee_edges (m_ctxt);
590 virtual unsigned int execute (function *);
592 }; // class pass_remove_cgraph_callee_edges
594 unsigned int
595 pass_remove_cgraph_callee_edges::execute (function *)
597 struct cgraph_node *node = cgraph_get_node (current_function_decl);
598 cgraph_node_remove_callees (node);
599 ipa_remove_all_references (&node->ref_list);
600 return 0;
603 } // anon namespace
605 gimple_opt_pass *
606 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
608 return new pass_remove_cgraph_callee_edges (ctxt);