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