* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / cgraphbuild.c
blob4dc4c2696c6d934e18b0e92cec28305eb1853113
1 /* Callgraph construction.
2 Copyright (C) 2003-2015 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 "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "predict.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "dominance.h"
33 #include "cfg.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
38 #include "gimple-expr.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-walk.h"
42 #include "langhooks.h"
43 #include "intl.h"
44 #include "tree-pass.h"
45 #include "cgraph.h"
46 #include "ipa-utils.h"
47 #include "except.h"
48 #include "alloc-pool.h"
49 #include "symbol-summary.h"
50 #include "ipa-prop.h"
51 #include "ipa-inline.h"
53 /* Context of record_reference. */
54 struct record_reference_ctx
56 bool only_vars;
57 class varpool_node *varpool_node;
60 /* Walk tree and record all calls and references to functions/variables.
61 Called via walk_tree: TP is pointer to tree to be examined.
62 When DATA is non-null, record references to callgraph.
65 static tree
66 record_reference (tree *tp, int *walk_subtrees, void *data)
68 tree t = *tp;
69 tree decl;
70 record_reference_ctx *ctx = (record_reference_ctx *)data;
72 t = canonicalize_constructor_val (t, NULL);
73 if (!t)
74 t = *tp;
75 else if (t != *tp)
76 *tp = t;
78 switch (TREE_CODE (t))
80 case VAR_DECL:
81 case FUNCTION_DECL:
82 gcc_unreachable ();
83 break;
85 case FDESC_EXPR:
86 case ADDR_EXPR:
87 /* Record dereferences to the functions. This makes the
88 functions reachable unconditionally. */
89 decl = get_base_var (*tp);
90 if (TREE_CODE (decl) == FUNCTION_DECL)
92 cgraph_node *node = cgraph_node::get_create (decl);
93 if (!ctx->only_vars)
94 node->mark_address_taken ();
95 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
98 if (TREE_CODE (decl) == VAR_DECL)
100 varpool_node *vnode = varpool_node::get_create (decl);
101 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
103 *walk_subtrees = 0;
104 break;
106 default:
107 /* Save some cycles by not walking types and declaration as we
108 won't find anything useful there anyway. */
109 if (IS_TYPE_OR_DECL_P (*tp))
111 *walk_subtrees = 0;
112 break;
114 break;
117 return NULL_TREE;
120 /* Record references to typeinfos in the type list LIST. */
122 static void
123 record_type_list (cgraph_node *node, tree list)
125 for (; list; list = TREE_CHAIN (list))
127 tree type = TREE_VALUE (list);
129 if (TYPE_P (type))
130 type = lookup_type_for_runtime (type);
131 STRIP_NOPS (type);
132 if (TREE_CODE (type) == ADDR_EXPR)
134 type = TREE_OPERAND (type, 0);
135 if (TREE_CODE (type) == VAR_DECL)
137 varpool_node *vnode = varpool_node::get_create (type);
138 node->create_reference (vnode, IPA_REF_ADDR);
144 /* Record all references we will introduce by producing EH tables
145 for NODE. */
147 static void
148 record_eh_tables (cgraph_node *node, function *fun)
150 eh_region i;
152 if (DECL_FUNCTION_PERSONALITY (node->decl))
154 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
155 cgraph_node *per_node = cgraph_node::get_create (per_decl);
157 node->create_reference (per_node, IPA_REF_ADDR);
158 per_node->mark_address_taken ();
161 i = fun->eh->region_tree;
162 if (!i)
163 return;
165 while (1)
167 switch (i->type)
169 case ERT_CLEANUP:
170 case ERT_MUST_NOT_THROW:
171 break;
173 case ERT_TRY:
175 eh_catch c;
176 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
177 record_type_list (node, c->type_list);
179 break;
181 case ERT_ALLOWED_EXCEPTIONS:
182 record_type_list (node, i->u.allowed.type_list);
183 break;
185 /* If there are sub-regions, process them. */
186 if (i->inner)
187 i = i->inner;
188 /* If there are peers, process them. */
189 else if (i->next_peer)
190 i = i->next_peer;
191 /* Otherwise, step back up the tree to the next peer. */
192 else
196 i = i->outer;
197 if (i == NULL)
198 return;
200 while (i->next_peer == NULL);
201 i = i->next_peer;
206 /* Computes the frequency of the call statement so that it can be stored in
207 cgraph_edge. BB is the basic block of the call statement. */
209 compute_call_stmt_bb_frequency (tree decl, basic_block bb)
211 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
212 (DECL_STRUCT_FUNCTION (decl))->frequency;
213 int freq = bb->frequency;
215 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
216 return CGRAPH_FREQ_BASE;
218 if (!entry_freq)
219 entry_freq = 1, freq++;
221 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
222 if (freq > CGRAPH_FREQ_MAX)
223 freq = CGRAPH_FREQ_MAX;
225 return freq;
228 /* Mark address taken in STMT. */
230 static bool
231 mark_address (gimple stmt, tree addr, tree, void *data)
233 addr = get_base_address (addr);
234 if (TREE_CODE (addr) == FUNCTION_DECL)
236 cgraph_node *node = cgraph_node::get_create (addr);
237 node->mark_address_taken ();
238 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
240 else if (addr && TREE_CODE (addr) == VAR_DECL
241 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
243 varpool_node *vnode = varpool_node::get_create (addr);
245 ((symtab_node *)data)->create_reference (vnode, 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 cgraph_node *node = cgraph_node::get_create (t);
262 node->mark_address_taken ();
263 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
265 else if (t && TREE_CODE (t) == VAR_DECL
266 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
268 varpool_node *vnode = varpool_node::get_create (t);
270 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
272 return false;
275 /* Mark store of T. */
277 static bool
278 mark_store (gimple stmt, tree t, tree, void *data)
280 t = get_base_address (t);
281 if (t && TREE_CODE (t) == VAR_DECL
282 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
284 varpool_node *vnode = varpool_node::get_create (t);
286 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
288 return false;
291 /* Record all references from cgraph_node that are taken in statement STMT. */
293 void
294 cgraph_node::record_stmt_references (gimple stmt)
296 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
297 mark_address);
300 /* Create cgraph edges for function calls.
301 Also look for functions and variables having addresses taken. */
303 namespace {
305 const pass_data pass_data_build_cgraph_edges =
307 GIMPLE_PASS, /* type */
308 "*build_cgraph_edges", /* name */
309 OPTGROUP_NONE, /* optinfo_flags */
310 TV_NONE, /* tv_id */
311 PROP_cfg, /* properties_required */
312 0, /* properties_provided */
313 0, /* properties_destroyed */
314 0, /* todo_flags_start */
315 0, /* todo_flags_finish */
318 class pass_build_cgraph_edges : public gimple_opt_pass
320 public:
321 pass_build_cgraph_edges (gcc::context *ctxt)
322 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
325 /* opt_pass methods: */
326 virtual unsigned int execute (function *);
328 }; // class pass_build_cgraph_edges
330 unsigned int
331 pass_build_cgraph_edges::execute (function *fun)
333 basic_block bb;
334 cgraph_node *node = cgraph_node::get (current_function_decl);
335 gimple_stmt_iterator gsi;
336 tree decl;
337 unsigned ix;
339 /* Create the callgraph edges and record the nodes referenced by the function.
340 body. */
341 FOR_EACH_BB_FN (bb, fun)
343 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
345 gimple stmt = gsi_stmt (gsi);
346 tree decl;
348 if (is_gimple_debug (stmt))
349 continue;
351 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
353 int freq = compute_call_stmt_bb_frequency (current_function_decl,
354 bb);
355 decl = gimple_call_fndecl (call_stmt);
356 if (decl)
357 node->create_edge (cgraph_node::get_create (decl), call_stmt, bb->count, freq);
358 else if (gimple_call_internal_p (call_stmt))
360 else
361 node->create_indirect_edge (call_stmt,
362 gimple_call_flags (call_stmt),
363 bb->count, freq);
365 node->record_stmt_references (stmt);
366 if (gomp_parallel *omp_par_stmt = dyn_cast <gomp_parallel *> (stmt))
368 tree fn = gimple_omp_parallel_child_fn (omp_par_stmt);
369 node->create_reference (cgraph_node::get_create (fn),
370 IPA_REF_ADDR, stmt);
372 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
374 tree fn = gimple_omp_task_child_fn (stmt);
375 if (fn)
376 node->create_reference (cgraph_node::get_create (fn),
377 IPA_REF_ADDR, stmt);
378 fn = gimple_omp_task_copy_fn (stmt);
379 if (fn)
380 node->create_reference (cgraph_node::get_create (fn),
381 IPA_REF_ADDR, stmt);
384 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
385 node->record_stmt_references (gsi_stmt (gsi));
388 /* Look for initializers of constant variables and private statics. */
389 FOR_EACH_LOCAL_DECL (fun, ix, decl)
390 if (TREE_CODE (decl) == VAR_DECL
391 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
392 && !DECL_HAS_VALUE_EXPR_P (decl))
393 varpool_node::finalize_decl (decl);
394 record_eh_tables (node, fun);
396 return 0;
399 } // anon namespace
401 gimple_opt_pass *
402 make_pass_build_cgraph_edges (gcc::context *ctxt)
404 return new pass_build_cgraph_edges (ctxt);
407 /* Record references to functions and other variables present in the
408 initial value of DECL, a variable.
409 When ONLY_VARS is true, we mark needed only variables, not functions. */
411 void
412 record_references_in_initializer (tree decl, bool only_vars)
414 varpool_node *node = varpool_node::get_create (decl);
415 hash_set<tree> visited_nodes;
416 record_reference_ctx ctx = {false, NULL};
418 ctx.varpool_node = node;
419 ctx.only_vars = only_vars;
420 walk_tree (&DECL_INITIAL (decl), record_reference,
421 &ctx, &visited_nodes);
424 /* Rebuild cgraph edges for current function node. This needs to be run after
425 passes that don't update the cgraph. */
427 unsigned int
428 cgraph_edge::rebuild_edges (void)
430 basic_block bb;
431 cgraph_node *node = cgraph_node::get (current_function_decl);
432 gimple_stmt_iterator gsi;
434 node->remove_callees ();
435 node->remove_all_references ();
437 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
439 FOR_EACH_BB_FN (bb, cfun)
441 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
443 gimple stmt = gsi_stmt (gsi);
444 tree decl;
446 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
448 int freq = compute_call_stmt_bb_frequency (current_function_decl,
449 bb);
450 decl = gimple_call_fndecl (call_stmt);
451 if (decl)
452 node->create_edge (cgraph_node::get_create (decl), call_stmt,
453 bb->count, freq);
454 else if (gimple_call_internal_p (call_stmt))
456 else
457 node->create_indirect_edge (call_stmt,
458 gimple_call_flags (call_stmt),
459 bb->count, freq);
461 node->record_stmt_references (stmt);
463 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
464 node->record_stmt_references (gsi_stmt (gsi));
466 record_eh_tables (node, cfun);
467 gcc_assert (!node->global.inlined_to);
469 if (node->instrumented_version
470 && !node->instrumentation_clone)
471 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
473 return 0;
476 /* Rebuild cgraph references for current function node. This needs to be run
477 after passes that don't update the cgraph. */
479 void
480 cgraph_edge::rebuild_references (void)
482 basic_block bb;
483 cgraph_node *node = cgraph_node::get (current_function_decl);
484 gimple_stmt_iterator gsi;
485 ipa_ref *ref = NULL;
486 int i;
488 /* Keep speculative references for further cgraph edge expansion. */
489 for (i = 0; node->iterate_reference (i, ref);)
490 if (!ref->speculative)
491 ref->remove_reference ();
492 else
493 i++;
495 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
497 FOR_EACH_BB_FN (bb, cfun)
499 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
500 node->record_stmt_references (gsi_stmt (gsi));
501 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
502 node->record_stmt_references (gsi_stmt (gsi));
504 record_eh_tables (node, cfun);
506 if (node->instrumented_version
507 && !node->instrumentation_clone)
508 node->create_reference (node->instrumented_version, IPA_REF_CHKP, NULL);
511 namespace {
513 const pass_data pass_data_rebuild_cgraph_edges =
515 GIMPLE_PASS, /* type */
516 "*rebuild_cgraph_edges", /* name */
517 OPTGROUP_NONE, /* optinfo_flags */
518 TV_CGRAPH, /* tv_id */
519 PROP_cfg, /* properties_required */
520 0, /* properties_provided */
521 0, /* properties_destroyed */
522 0, /* todo_flags_start */
523 0, /* todo_flags_finish */
526 class pass_rebuild_cgraph_edges : public gimple_opt_pass
528 public:
529 pass_rebuild_cgraph_edges (gcc::context *ctxt)
530 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
533 /* opt_pass methods: */
534 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
535 virtual unsigned int execute (function *)
537 return cgraph_edge::rebuild_edges ();
540 }; // class pass_rebuild_cgraph_edges
542 } // anon namespace
544 gimple_opt_pass *
545 make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
547 return new pass_rebuild_cgraph_edges (ctxt);
551 namespace {
553 const pass_data pass_data_remove_cgraph_callee_edges =
555 GIMPLE_PASS, /* type */
556 "*remove_cgraph_callee_edges", /* name */
557 OPTGROUP_NONE, /* optinfo_flags */
558 TV_NONE, /* tv_id */
559 0, /* properties_required */
560 0, /* properties_provided */
561 0, /* properties_destroyed */
562 0, /* todo_flags_start */
563 0, /* todo_flags_finish */
566 class pass_remove_cgraph_callee_edges : public gimple_opt_pass
568 public:
569 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
570 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
573 /* opt_pass methods: */
574 opt_pass * clone () {
575 return new pass_remove_cgraph_callee_edges (m_ctxt);
577 virtual unsigned int execute (function *);
579 }; // class pass_remove_cgraph_callee_edges
581 unsigned int
582 pass_remove_cgraph_callee_edges::execute (function *)
584 cgraph_node *node = cgraph_node::get (current_function_decl);
585 node->remove_callees ();
586 node->remove_all_references ();
587 return 0;
590 } // anon namespace
592 gimple_opt_pass *
593 make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
595 return new pass_remove_cgraph_callee_edges (ctxt);