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