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