]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphbuild.c
IPA C++ refactoring 4/N
[thirdparty/gcc.git] / gcc / cgraphbuild.c
CommitLineData
e7c352d1 1/* Callgraph construction.
3aea1f79 2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
e7c352d1 3 Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
e7c352d1 10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
e7c352d1 20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "tree.h"
bc61cadb 26#include "basic-block.h"
27#include "tree-ssa-alias.h"
28#include "internal-fn.h"
29#include "gimple-fold.h"
30#include "gimple-expr.h"
31#include "is-a.h"
b23fb4cb 32#include "gimple.h"
dcf1a1ec 33#include "gimple-iterator.h"
34#include "gimple-walk.h"
e7c352d1 35#include "langhooks.h"
e7c352d1 36#include "intl.h"
e7c352d1 37#include "tree-pass.h"
63b0bea7 38#include "ipa-utils.h"
12221745 39#include "except.h"
cbd7f5a0 40#include "ipa-inline.h"
63b0bea7 41
42/* Context of record_reference. */
43struct record_reference_ctx
44{
45 bool only_vars;
098f44bc 46 class varpool_node *varpool_node;
63b0bea7 47};
e7c352d1 48
49/* Walk tree and record all calls and references to functions/variables.
48e1416a 50 Called via walk_tree: TP is pointer to tree to be examined.
85276fc7 51 When DATA is non-null, record references to callgraph.
52 */
e7c352d1 53
54static tree
85276fc7 55record_reference (tree *tp, int *walk_subtrees, void *data)
e7c352d1 56{
57 tree t = *tp;
6329636b 58 tree decl;
35ee1c66 59 record_reference_ctx *ctx = (record_reference_ctx *)data;
e7c352d1 60
a65b88bf 61 t = canonicalize_constructor_val (t, NULL);
bb903e9c 62 if (!t)
63 t = *tp;
64 else if (t != *tp)
65 *tp = t;
66
e7c352d1 67 switch (TREE_CODE (t))
68 {
69 case VAR_DECL:
63b0bea7 70 case FUNCTION_DECL:
71 gcc_unreachable ();
e7c352d1 72 break;
73
74 case FDESC_EXPR:
75 case ADDR_EXPR:
6329636b 76 /* Record dereferences to the functions. This makes the
77 functions reachable unconditionally. */
63b0bea7 78 decl = get_base_var (*tp);
8d810329 79 if (TREE_CODE (decl) == FUNCTION_DECL)
80 {
35ee1c66 81 cgraph_node *node = cgraph_node::get_create (decl);
8d810329 82 if (!ctx->only_vars)
415d1b9a 83 node->mark_address_taken ();
35ee1c66 84 ctx->varpool_node->create_reference (node, IPA_REF_ADDR);
8d810329 85 }
63b0bea7 86
87 if (TREE_CODE (decl) == VAR_DECL)
88 {
97221fd7 89 varpool_node *vnode = varpool_node::get_create (decl);
35ee1c66 90 ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
63b0bea7 91 }
92 *walk_subtrees = 0;
e7c352d1 93 break;
94
95 default:
96 /* Save some cycles by not walking types and declaration as we
97 won't find anything useful there anyway. */
98 if (IS_TYPE_OR_DECL_P (*tp))
99 {
100 *walk_subtrees = 0;
101 break;
102 }
e7c352d1 103 break;
104 }
105
106 return NULL_TREE;
107}
108
12221745 109/* Record references to typeinfos in the type list LIST. */
110
111static void
35ee1c66 112record_type_list (cgraph_node *node, tree list)
12221745 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 {
97221fd7 126 varpool_node *vnode = varpool_node::get_create (type);
35ee1c66 127 node->create_reference (vnode, IPA_REF_ADDR);
12221745 128 }
129 }
130 }
131}
132
133/* Record all references we will introduce by producing EH tables
134 for NODE. */
135
136static void
35ee1c66 137record_eh_tables (cgraph_node *node, function *fun)
12221745 138{
139 eh_region i;
140
02774f2d 141 if (DECL_FUNCTION_PERSONALITY (node->decl))
9be42e8c 142 {
593ce529 143 tree per_decl = DECL_FUNCTION_PERSONALITY (node->decl);
35ee1c66 144 cgraph_node *per_node = cgraph_node::get_create (per_decl);
9be42e8c 145
35ee1c66 146 node->create_reference (per_node, IPA_REF_ADDR);
415d1b9a 147 per_node->mark_address_taken ();
9be42e8c 148 }
b13addbf 149
12221745 150 i = fun->eh->region_tree;
151 if (!i)
152 return;
153
154 while (1)
155 {
156 switch (i->type)
157 {
158 case ERT_CLEANUP:
159 case ERT_MUST_NOT_THROW:
160 break;
161
162 case ERT_TRY:
163 {
164 eh_catch c;
165 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
166 record_type_list (node, c->type_list);
167 }
168 break;
169
170 case ERT_ALLOWED_EXCEPTIONS:
171 record_type_list (node, i->u.allowed.type_list);
172 break;
173 }
174 /* If there are sub-regions, process them. */
175 if (i->inner)
176 i = i->inner;
177 /* If there are peers, process them. */
178 else if (i->next_peer)
179 i = i->next_peer;
180 /* Otherwise, step back up the tree to the next peer. */
181 else
182 {
183 do
184 {
185 i = i->outer;
186 if (i == NULL)
187 return;
188 }
189 while (i->next_peer == NULL);
190 i = i->next_peer;
191 }
192 }
193}
194
f8daee9b 195/* Computes the frequency of the call statement so that it can be stored in
196 cgraph_edge. BB is the basic block of the call statement. */
197int
ccf4ab6b 198compute_call_stmt_bb_frequency (tree decl, basic_block bb)
f8daee9b 199{
34154e27 200 int entry_freq = ENTRY_BLOCK_PTR_FOR_FN
e2d3f422 201 (DECL_STRUCT_FUNCTION (decl))->frequency;
9f694a82 202 int freq = bb->frequency;
f8daee9b 203
3bedbae3 204 if (profile_status_for_fn (DECL_STRUCT_FUNCTION (decl)) == PROFILE_ABSENT)
ccf4ab6b 205 return CGRAPH_FREQ_BASE;
206
f8daee9b 207 if (!entry_freq)
9f694a82 208 entry_freq = 1, freq++;
f8daee9b 209
9f694a82 210 freq = freq * CGRAPH_FREQ_BASE / entry_freq;
f8daee9b 211 if (freq > CGRAPH_FREQ_MAX)
212 freq = CGRAPH_FREQ_MAX;
213
214 return freq;
215}
216
63b0bea7 217/* Mark address taken in STMT. */
218
219static bool
5b26a9e3 220mark_address (gimple stmt, tree addr, tree, void *data)
63b0bea7 221{
10ac3b61 222 addr = get_base_address (addr);
63b0bea7 223 if (TREE_CODE (addr) == FUNCTION_DECL)
224 {
35ee1c66 225 cgraph_node *node = cgraph_node::get_create (addr);
415d1b9a 226 node->mark_address_taken ();
35ee1c66 227 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
63b0bea7 228 }
10ac3b61 229 else if (addr && TREE_CODE (addr) == VAR_DECL
230 && (TREE_STATIC (addr) || DECL_EXTERNAL (addr)))
63b0bea7 231 {
97221fd7 232 varpool_node *vnode = varpool_node::get_create (addr);
63b0bea7 233
35ee1c66 234 ((symtab_node *)data)->create_reference (vnode, IPA_REF_ADDR, stmt);
63b0bea7 235 }
236
237 return false;
238}
239
240/* Mark load of T. */
241
242static bool
5b26a9e3 243mark_load (gimple stmt, tree t, tree, void *data)
63b0bea7 244{
245 t = get_base_address (t);
4c06d6f7 246 if (t && TREE_CODE (t) == FUNCTION_DECL)
247 {
248 /* ??? This can happen on platforms with descriptors when these are
249 directly manipulated in the code. Pretend that it's an address. */
35ee1c66 250 cgraph_node *node = cgraph_node::get_create (t);
415d1b9a 251 node->mark_address_taken ();
35ee1c66 252 ((symtab_node *)data)->create_reference (node, IPA_REF_ADDR, stmt);
4c06d6f7 253 }
254 else if (t && TREE_CODE (t) == VAR_DECL
255 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
63b0bea7 256 {
97221fd7 257 varpool_node *vnode = varpool_node::get_create (t);
63b0bea7 258
35ee1c66 259 ((symtab_node *)data)->create_reference (vnode, IPA_REF_LOAD, stmt);
63b0bea7 260 }
261 return false;
262}
263
264/* Mark store of T. */
265
266static bool
5b26a9e3 267mark_store (gimple stmt, tree t, tree, void *data)
63b0bea7 268{
269 t = get_base_address (t);
182cf5a9 270 if (t && TREE_CODE (t) == VAR_DECL
63b0bea7 271 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
272 {
97221fd7 273 varpool_node *vnode = varpool_node::get_create (t);
63b0bea7 274
35ee1c66 275 ((symtab_node *)data)->create_reference (vnode, IPA_REF_STORE, stmt);
63b0bea7 276 }
277 return false;
278}
279
415d1b9a 280/* Record all references from cgraph_node that are taken in statement STMT. */
281
7d9f258f 282void
415d1b9a 283cgraph_node::record_stmt_references (gimple stmt)
7d9f258f 284{
415d1b9a 285 walk_stmt_load_store_addr_ops (stmt, this, mark_load, mark_store,
7d9f258f 286 mark_address);
287}
288
e7c352d1 289/* Create cgraph edges for function calls.
290 Also look for functions and variables having addresses taken. */
291
65b0537f 292namespace {
293
294const pass_data pass_data_build_cgraph_edges =
295{
296 GIMPLE_PASS, /* type */
297 "*build_cgraph_edges", /* name */
298 OPTGROUP_NONE, /* optinfo_flags */
65b0537f 299 TV_NONE, /* tv_id */
300 PROP_cfg, /* properties_required */
301 0, /* properties_provided */
302 0, /* properties_destroyed */
303 0, /* todo_flags_start */
304 0, /* todo_flags_finish */
305};
306
307class pass_build_cgraph_edges : public gimple_opt_pass
308{
309public:
310 pass_build_cgraph_edges (gcc::context *ctxt)
311 : gimple_opt_pass (pass_data_build_cgraph_edges, ctxt)
312 {}
313
314 /* opt_pass methods: */
315 virtual unsigned int execute (function *);
316
317}; // class pass_build_cgraph_edges
318
319unsigned int
320pass_build_cgraph_edges::execute (function *fun)
e7c352d1 321{
322 basic_block bb;
35ee1c66 323 cgraph_node *node = cgraph_node::get (current_function_decl);
75a70cf9 324 gimple_stmt_iterator gsi;
2ab2ce89 325 tree decl;
326 unsigned ix;
e7c352d1 327
328 /* Create the callgraph edges and record the nodes referenced by the function.
329 body. */
65b0537f 330 FOR_EACH_BB_FN (bb, fun)
63b0bea7 331 {
332 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
333 {
334 gimple stmt = gsi_stmt (gsi);
335 tree decl;
336
9df17e79 337 if (is_gimple_debug (stmt))
338 continue;
339
f8b7e3ec 340 if (is_gimple_call (stmt))
341 {
342 int freq = compute_call_stmt_bb_frequency (current_function_decl,
343 bb);
344 decl = gimple_call_fndecl (stmt);
345 if (decl)
35ee1c66 346 node->create_edge (cgraph_node::get_create (decl), stmt, bb->count, freq);
9978ce6d 347 else if (gimple_call_internal_p (stmt))
348 ;
f8b7e3ec 349 else
415d1b9a 350 node->create_indirect_edge (stmt,
351 gimple_call_flags (stmt),
352 bb->count, freq);
f8b7e3ec 353 }
415d1b9a 354 node->record_stmt_references (stmt);
63b0bea7 355 if (gimple_code (stmt) == GIMPLE_OMP_PARALLEL
356 && gimple_omp_parallel_child_fn (stmt))
357 {
358 tree fn = gimple_omp_parallel_child_fn (stmt);
35ee1c66 359 node->create_reference (cgraph_node::get_create (fn),
51ce5652 360 IPA_REF_ADDR, stmt);
63b0bea7 361 }
362 if (gimple_code (stmt) == GIMPLE_OMP_TASK)
363 {
364 tree fn = gimple_omp_task_child_fn (stmt);
365 if (fn)
35ee1c66 366 node->create_reference (cgraph_node::get_create (fn),
51ce5652 367 IPA_REF_ADDR, stmt);
63b0bea7 368 fn = gimple_omp_task_copy_fn (stmt);
369 if (fn)
35ee1c66 370 node->create_reference (cgraph_node::get_create (fn),
51ce5652 371 IPA_REF_ADDR, stmt);
63b0bea7 372 }
373 }
e3a19533 374 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
415d1b9a 375 node->record_stmt_references (gsi_stmt (gsi));
63b0bea7 376 }
e7c352d1 377
378 /* Look for initializers of constant variables and private statics. */
65b0537f 379 FOR_EACH_LOCAL_DECL (fun, ix, decl)
2ab2ce89 380 if (TREE_CODE (decl) == VAR_DECL
3d1c0354 381 && (TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
382 && !DECL_HAS_VALUE_EXPR_P (decl))
97221fd7 383 varpool_node::finalize_decl (decl);
65b0537f 384 record_eh_tables (node, fun);
e7c352d1 385
e7c352d1 386 return 0;
387}
388
cbe8bda8 389} // anon namespace
390
391gimple_opt_pass *
392make_pass_build_cgraph_edges (gcc::context *ctxt)
393{
394 return new pass_build_cgraph_edges (ctxt);
395}
396
e7c352d1 397/* Record references to functions and other variables present in the
48e1416a 398 initial value of DECL, a variable.
85276fc7 399 When ONLY_VARS is true, we mark needed only variables, not functions. */
e7c352d1 400
401void
85276fc7 402record_references_in_initializer (tree decl, bool only_vars)
e7c352d1 403{
97221fd7 404 varpool_node *node = varpool_node::get_create (decl);
431205b7 405 hash_set<tree> visited_nodes;
35ee1c66 406 record_reference_ctx ctx = {false, NULL};
63b0bea7 407
8d810329 408 ctx.varpool_node = node;
63b0bea7 409 ctx.only_vars = only_vars;
48e1416a 410 walk_tree (&DECL_INITIAL (decl), record_reference,
431205b7 411 &ctx, &visited_nodes);
e7c352d1 412}
413
414/* Rebuild cgraph edges for current function node. This needs to be run after
415 passes that don't update the cgraph. */
416
79acaae1 417unsigned int
35ee1c66 418cgraph_edge::rebuild_edges (void)
e7c352d1 419{
420 basic_block bb;
35ee1c66 421 cgraph_node *node = cgraph_node::get (current_function_decl);
75a70cf9 422 gimple_stmt_iterator gsi;
e7c352d1 423
415d1b9a 424 node->remove_callees ();
51ce5652 425 node->remove_all_references ();
e7c352d1 426
34154e27 427 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
e7c352d1 428
fc00614f 429 FOR_EACH_BB_FN (bb, cfun)
63b0bea7 430 {
431 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
432 {
433 gimple stmt = gsi_stmt (gsi);
434 tree decl;
435
f8b7e3ec 436 if (is_gimple_call (stmt))
437 {
438 int freq = compute_call_stmt_bb_frequency (current_function_decl,
439 bb);
440 decl = gimple_call_fndecl (stmt);
441 if (decl)
415d1b9a 442 node->create_edge (cgraph_node::get_create (decl), stmt,
443 bb->count, freq);
9978ce6d 444 else if (gimple_call_internal_p (stmt))
445 ;
f8b7e3ec 446 else
415d1b9a 447 node->create_indirect_edge (stmt,
448 gimple_call_flags (stmt),
449 bb->count, freq);
f8b7e3ec 450 }
415d1b9a 451 node->record_stmt_references (stmt);
63b0bea7 452 }
e3a19533 453 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
415d1b9a 454 node->record_stmt_references (gsi_stmt (gsi));
63b0bea7 455 }
12221745 456 record_eh_tables (node, cfun);
e7c352d1 457 gcc_assert (!node->global.inlined_to);
69428266 458
e7c352d1 459 return 0;
460}
461
35ee1c66 462/* Rebuild cgraph references for current function node. This needs to be run
463 after passes that don't update the cgraph. */
ea7e866e 464
465void
35ee1c66 466cgraph_edge::rebuild_references (void)
ea7e866e 467{
468 basic_block bb;
35ee1c66 469 cgraph_node *node = cgraph_node::get (current_function_decl);
ea7e866e 470 gimple_stmt_iterator gsi;
35ee1c66 471 ipa_ref *ref = NULL;
4d044066 472 int i;
473
474 /* Keep speculative references for further cgraph edge expansion. */
51ce5652 475 for (i = 0; node->iterate_reference (i, ref);)
4d044066 476 if (!ref->speculative)
51ce5652 477 ref->remove_reference ();
4d044066 478 else
479 i++;
ea7e866e 480
34154e27 481 node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
ea7e866e 482
fc00614f 483 FOR_EACH_BB_FN (bb, cfun)
ea7e866e 484 {
485 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
415d1b9a 486 node->record_stmt_references (gsi_stmt (gsi));
e3a19533 487 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
415d1b9a 488 node->record_stmt_references (gsi_stmt (gsi));
ea7e866e 489 }
490 record_eh_tables (node, cfun);
491}
492
cbe8bda8 493namespace {
494
495const pass_data pass_data_rebuild_cgraph_edges =
e7c352d1 496{
cbe8bda8 497 GIMPLE_PASS, /* type */
498 "*rebuild_cgraph_edges", /* name */
499 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 500 TV_CGRAPH, /* tv_id */
501 PROP_cfg, /* properties_required */
502 0, /* properties_provided */
503 0, /* properties_destroyed */
504 0, /* todo_flags_start */
505 0, /* todo_flags_finish */
e7c352d1 506};
ba3a7ba0 507
cbe8bda8 508class pass_rebuild_cgraph_edges : public gimple_opt_pass
509{
510public:
9af5ce0c 511 pass_rebuild_cgraph_edges (gcc::context *ctxt)
512 : gimple_opt_pass (pass_data_rebuild_cgraph_edges, ctxt)
cbe8bda8 513 {}
514
515 /* opt_pass methods: */
ae84f584 516 opt_pass * clone () { return new pass_rebuild_cgraph_edges (m_ctxt); }
35ee1c66 517 virtual unsigned int execute (function *)
518 {
519 return cgraph_edge::rebuild_edges ();
520 }
cbe8bda8 521
522}; // class pass_rebuild_cgraph_edges
523
524} // anon namespace
525
526gimple_opt_pass *
527make_pass_rebuild_cgraph_edges (gcc::context *ctxt)
528{
529 return new pass_rebuild_cgraph_edges (ctxt);
530}
531
ba3a7ba0 532
cbe8bda8 533namespace {
534
535const pass_data pass_data_remove_cgraph_callee_edges =
ba3a7ba0 536{
cbe8bda8 537 GIMPLE_PASS, /* type */
538 "*remove_cgraph_callee_edges", /* name */
539 OPTGROUP_NONE, /* optinfo_flags */
cbe8bda8 540 TV_NONE, /* tv_id */
541 0, /* properties_required */
542 0, /* properties_provided */
543 0, /* properties_destroyed */
544 0, /* todo_flags_start */
545 0, /* todo_flags_finish */
ba3a7ba0 546};
cbe8bda8 547
548class pass_remove_cgraph_callee_edges : public gimple_opt_pass
549{
550public:
9af5ce0c 551 pass_remove_cgraph_callee_edges (gcc::context *ctxt)
552 : gimple_opt_pass (pass_data_remove_cgraph_callee_edges, ctxt)
cbe8bda8 553 {}
554
555 /* opt_pass methods: */
556 opt_pass * clone () {
ae84f584 557 return new pass_remove_cgraph_callee_edges (m_ctxt);
cbe8bda8 558 }
65b0537f 559 virtual unsigned int execute (function *);
cbe8bda8 560
561}; // class pass_remove_cgraph_callee_edges
562
65b0537f 563unsigned int
564pass_remove_cgraph_callee_edges::execute (function *)
565{
35ee1c66 566 cgraph_node *node = cgraph_node::get (current_function_decl);
415d1b9a 567 node->remove_callees ();
51ce5652 568 node->remove_all_references ();
65b0537f 569 return 0;
570}
571
cbe8bda8 572} // anon namespace
573
574gimple_opt_pass *
575make_pass_remove_cgraph_callee_edges (gcc::context *ctxt)
576{
577 return new pass_remove_cgraph_callee_edges (ctxt);
578}