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