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