1 /* Callgraph handling code.
2 Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 #include "lto-streamer.h"
34 #include "omp-offload.h"
36 #include "stringpool.h"
39 const char * const tls_model_names
[]={"none", "emulated",
40 "global-dynamic", "local-dynamic",
41 "initial-exec", "local-exec"};
43 /* List of hooks triggered on varpool_node events. */
44 struct varpool_node_hook_list
{
45 varpool_node_hook hook
;
47 struct varpool_node_hook_list
*next
;
50 /* Register HOOK to be called with DATA on each removed node. */
51 varpool_node_hook_list
*
52 symbol_table::add_varpool_removal_hook (varpool_node_hook hook
, void *data
)
54 varpool_node_hook_list
*entry
;
55 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
57 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
67 /* Remove ENTRY from the list of hooks called on removing nodes. */
69 symbol_table::remove_varpool_removal_hook (varpool_node_hook_list
*entry
)
71 varpool_node_hook_list
**ptr
= &m_first_varpool_removal_hook
;
79 /* Call all node removal hooks. */
81 symbol_table::call_varpool_removal_hooks (varpool_node
*node
)
83 varpool_node_hook_list
*entry
= m_first_varpool_removal_hook
;
86 entry
->hook (node
, entry
->data
);
91 /* Register HOOK to be called with DATA on each inserted node. */
92 varpool_node_hook_list
*
93 symbol_table::add_varpool_insertion_hook (varpool_node_hook hook
, void *data
)
95 varpool_node_hook_list
*entry
;
96 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
98 entry
= (varpool_node_hook_list
*) xmalloc (sizeof (*entry
));
108 /* Remove ENTRY from the list of hooks called on inserted nodes. */
110 symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list
*entry
)
112 varpool_node_hook_list
**ptr
= &m_first_varpool_insertion_hook
;
114 while (*ptr
!= entry
)
120 /* Call all node insertion hooks. */
122 symbol_table::call_varpool_insertion_hooks (varpool_node
*node
)
124 varpool_node_hook_list
*entry
= m_first_varpool_insertion_hook
;
127 entry
->hook (node
, entry
->data
);
132 /* Allocate new callgraph node and insert it into basic data structures. */
135 varpool_node::create_empty (void)
137 return new (ggc_alloc
<varpool_node
> ()) varpool_node ();
140 /* Return varpool node assigned to DECL. Create new one when needed. */
142 varpool_node::get_create (tree decl
)
144 varpool_node
*node
= varpool_node::get (decl
);
145 gcc_checking_assert (VAR_P (decl
));
149 node
= varpool_node::create_empty ();
152 if ((flag_openacc
|| flag_openmp
)
153 && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
)))
155 node
->offloadable
= 1;
156 if (ENABLE_OFFLOADING
&& !DECL_EXTERNAL (decl
))
158 g
->have_offload
= true;
160 vec_safe_push (offload_vars
, decl
);
164 node
->register_symbol ();
168 /* Remove variable from symbol table. */
171 varpool_node::remove (void)
173 symtab
->call_varpool_removal_hooks (this);
176 lto_free_function_in_decl_state_for_node (this);
177 lto_file_data
= NULL
;
180 /* When streaming we can have multiple nodes associated with decl. */
181 if (symtab
->state
== LTO_STREAMING
)
183 /* Keep constructor when it may be used for folding. We remove
184 references to external variables before final compilation. */
185 else if (DECL_INITIAL (decl
) && DECL_INITIAL (decl
) != error_mark_node
186 && !ctor_useable_for_folding_p ())
187 remove_initializer ();
193 /* Remove node initializer when it is no longer needed. */
195 varpool_node::remove_initializer (void)
197 if (DECL_INITIAL (decl
)
198 && !DECL_IN_CONSTANT_POOL (decl
)
199 /* Keep vtables for BINFO folding. */
200 && !DECL_VIRTUAL_P (decl
)
201 /* FIXME: http://gcc.gnu.org/PR55395 */
202 && debug_info_level
== DINFO_LEVEL_NONE
203 /* When doing declaration merging we have duplicate
204 entries for given decl. Do not attempt to remove
205 the bodies, or we will end up removing
207 && symtab
->state
!= LTO_STREAMING
)
208 DECL_INITIAL (decl
) = error_mark_node
;
211 /* Dump given varpool node to F. */
213 varpool_node::dump (FILE *f
)
216 fprintf (f
, " Availability: %s\n",
217 symtab
->function_flags_ready
218 ? cgraph_availability_names
[get_availability ()]
220 fprintf (f
, " Varpool flags:");
221 if (DECL_INITIAL (decl
))
222 fprintf (f
, " initialized");
224 fprintf (f
, " output");
225 if (used_by_single_function
)
226 fprintf (f
, " used-by-single-function");
227 if (TREE_READONLY (decl
))
228 fprintf (f
, " read-only");
229 if (ctor_useable_for_folding_p ())
230 fprintf (f
, " const-value-known");
232 fprintf (f
, " write-only");
234 fprintf (f
, " tls-%s", tls_model_names
[tls_model
]);
239 /* Dump given varpool node to stderr. */
240 void varpool_node::debug (void)
242 varpool_node::dump (stderr
);
245 /* Dump the variable pool to F. */
247 varpool_node::dump_varpool (FILE *f
)
251 fprintf (f
, "variable pool:\n\n");
252 FOR_EACH_VARIABLE (node
)
256 /* Dump the variable pool to stderr. */
259 varpool_node::debug_varpool (void)
261 dump_varpool (stderr
);
264 /* Given an assembler name, lookup node. */
266 varpool_node::get_for_asmname (tree asmname
)
268 if (symtab_node
*node
= symtab_node::get_for_asmname (asmname
))
269 return dyn_cast
<varpool_node
*> (node
);
274 /* When doing LTO, read variable's constructor from disk if
275 it is not already present. */
278 varpool_node::get_constructor (void)
280 lto_file_decl_data
*file_data
;
281 const char *data
, *name
;
284 if (DECL_INITIAL (decl
) != error_mark_node
287 return DECL_INITIAL (decl
);
289 timevar_push (TV_IPA_LTO_CTORS_IN
);
291 file_data
= lto_file_data
;
292 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
294 /* We may have renamed the declaration, e.g., a static function. */
295 name
= lto_get_decl_name_mapping (file_data
, name
);
296 struct lto_in_decl_state
*decl_state
297 = lto_get_function_in_decl_state (file_data
, decl
);
299 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
300 name
, order
- file_data
->order_base
,
301 &len
, decl_state
->compressed
);
303 fatal_error (input_location
, "%s: section %s.%d is missing",
304 file_data
->file_name
,
305 name
, order
- file_data
->order_base
);
308 fprintf (stderr
, " in:%s", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
309 lto_input_variable_constructor (file_data
, this, data
);
310 gcc_assert (DECL_INITIAL (decl
) != error_mark_node
);
311 lto_stats
.num_function_bodies
++;
312 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
313 data
, len
, decl_state
->compressed
);
314 lto_free_function_in_decl_state_for_node (this);
315 timevar_pop (TV_IPA_LTO_CTORS_IN
);
316 return DECL_INITIAL (decl
);
319 /* Return true if variable has constructor that can be used for folding. */
322 varpool_node::ctor_useable_for_folding_p (void)
324 varpool_node
*real_node
= this;
326 if (real_node
->alias
&& real_node
->definition
)
327 real_node
= ultimate_alias_target ();
329 if (TREE_CODE (decl
) == CONST_DECL
330 || DECL_IN_CONSTANT_POOL (decl
))
332 if (TREE_THIS_VOLATILE (decl
))
335 /* Avoid attempts to load constructors that was not streamed. */
336 if (in_lto_p
&& DECL_INITIAL (real_node
->decl
) == error_mark_node
337 && real_node
->body_removed
)
340 /* If we do not have a constructor, we can't use it. */
341 if (DECL_INITIAL (real_node
->decl
) == error_mark_node
342 && !real_node
->lto_file_data
)
345 /* Vtables are defined by their types and must match no matter of interposition
347 if (DECL_VIRTUAL_P (decl
))
349 /* The C++ front end creates VAR_DECLs for vtables of typeinfo
350 classes not defined in the current TU so that it can refer
351 to them from typeinfo objects. Avoid returning NULL_TREE. */
352 return DECL_INITIAL (real_node
->decl
) != NULL
;
355 /* An alias of a read-only variable is also read-only, since the variable
356 is stored in read-only memory. We also accept read-only aliases of
357 non-read-only locations assuming that the user knows what he is asking
359 if (!TREE_READONLY (decl
) && !TREE_READONLY (real_node
->decl
))
362 /* Variables declared 'const' without an initializer
363 have zero as the initializer if they may not be
364 overridden at link or run time.
366 It is actually requirement for C++ compiler to optimize const variables
367 consistently. As a GNU extension, do not enforce this rule for user defined
368 weak variables, so we support interposition on:
369 static const int dummy = 0;
370 extern const int foo __attribute__((__weak__, __alias__("dummy")));
372 if ((!DECL_INITIAL (real_node
->decl
)
373 || (DECL_WEAK (decl
) && !DECL_COMDAT (decl
)))
374 && (DECL_EXTERNAL (decl
) || decl_replaceable_p (decl
)))
377 /* Variables declared `const' with an initializer are considered
378 to not be overwritable with different initializer by default.
380 ??? Previously we behaved so for scalar variables but not for array
385 /* If DECLARATION is constant variable and its initial value is known
386 (so we can do constant folding), return its constructor (DECL_INITIAL).
387 This may be an expression or NULL when DECL is initialized to 0.
388 Return ERROR_MARK_NODE otherwise.
390 In LTO this may actually trigger reading the constructor from disk.
391 For this reason varpool_ctor_useable_for_folding_p should be used when
392 the actual constructor value is not needed. */
395 ctor_for_folding (tree decl
)
397 varpool_node
*node
, *real_node
;
400 if (!VAR_P (decl
) && TREE_CODE (decl
) != CONST_DECL
)
401 return error_mark_node
;
403 if (TREE_CODE (decl
) == CONST_DECL
404 || DECL_IN_CONSTANT_POOL (decl
))
405 return DECL_INITIAL (decl
);
407 if (TREE_THIS_VOLATILE (decl
))
408 return error_mark_node
;
410 /* Do not care about automatic variables. Those are never initialized
411 anyway, because gimplifier expands the code. */
412 if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
414 gcc_assert (!TREE_PUBLIC (decl
));
415 return error_mark_node
;
418 gcc_assert (VAR_P (decl
));
420 real_node
= node
= varpool_node::get (decl
);
423 real_node
= node
->ultimate_alias_target ();
424 real_decl
= real_node
->decl
;
429 /* See if we are dealing with alias.
430 In most cases alias is just alternative symbol pointing to a given
431 constructor. This allows us to use interposition rules of DECL
432 constructor of REAL_NODE. However weakrefs are special by being just
433 alternative name of their target (if defined). */
434 if (decl
!= real_decl
)
436 gcc_assert (!DECL_INITIAL (decl
)
437 || (node
->alias
&& node
->get_alias_target () == real_node
)
438 || DECL_INITIAL (decl
) == error_mark_node
);
439 while (node
->transparent_alias
&& node
->analyzed
)
441 node
= node
->get_alias_target ();
446 if ((!DECL_VIRTUAL_P (real_decl
)
447 || DECL_INITIAL (real_decl
) == error_mark_node
448 || !DECL_INITIAL (real_decl
))
449 && (!node
|| !node
->ctor_useable_for_folding_p ()))
450 return error_mark_node
;
452 /* OK, we can return constructor. See if we need to fetch it from disk
454 if (DECL_INITIAL (real_decl
) != error_mark_node
456 return DECL_INITIAL (real_decl
);
457 return real_node
->get_constructor ();
460 /* Add the variable DECL to the varpool.
461 Unlike finalize_decl function is intended to be used
462 by middle end and allows insertion of new variable at arbitrary point
465 varpool_node::add (tree decl
)
468 varpool_node::finalize_decl (decl
);
469 node
= varpool_node::get_create (decl
);
470 symtab
->call_varpool_insertion_hooks (node
);
471 if (node
->externally_visible_p ())
472 node
->externally_visible
= true;
473 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
474 node
->no_reorder
= 1;
477 /* Return variable availability. See cgraph.h for description of individual
480 varpool_node::get_availability (symtab_node
*ref
)
482 if (!definition
&& !in_other_partition
)
483 return AVAIL_NOT_AVAILABLE
;
484 if (!TREE_PUBLIC (decl
))
485 return AVAIL_AVAILABLE
;
486 if (DECL_IN_CONSTANT_POOL (decl
)
487 || DECL_VIRTUAL_P (decl
))
488 return AVAIL_AVAILABLE
;
489 if (transparent_alias
&& definition
)
491 enum availability avail
;
493 ultimate_alias_target (&avail
, ref
);
496 /* If this is a reference from symbol itself and there are no aliases, we
497 may be sure that the symbol was not interposed by something else because
498 the symbol itself would be unreachable otherwise. */
499 if ((this == ref
&& !has_aliases_p ())
500 || (ref
&& get_comdat_group ()
501 && get_comdat_group () == ref
->get_comdat_group ()))
502 return AVAIL_AVAILABLE
;
503 /* If the variable can be overwritten, return OVERWRITABLE. Takes
504 care of at least one notable extension - the COMDAT variables
505 used to share template instantiations in C++. */
506 if (decl_replaceable_p (decl
)
507 || DECL_EXTERNAL (decl
))
508 return AVAIL_INTERPOSABLE
;
509 return AVAIL_AVAILABLE
;
513 varpool_node::analyze (void)
515 /* When reading back varpool at LTO time, we re-construct the queue in order
516 to have "needed" list right by inserting all needed nodes into varpool.
517 We however don't want to re-analyze already analyzed nodes. */
520 gcc_assert (!in_lto_p
|| symtab
->function_flags_ready
);
521 /* Compute the alignment early so function body expanders are
522 already informed about increased alignment. */
523 align_variable (decl
, 0);
526 resolve_alias (varpool_node::get (alias_target
));
527 else if (DECL_INITIAL (decl
))
528 record_references_in_initializer (decl
, analyzed
);
532 /* Assemble thunks and aliases associated to varpool node. */
535 varpool_node::assemble_aliases (void)
539 FOR_EACH_ALIAS (this, ref
)
541 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
543 do_assemble_symver (alias
->decl
,
544 DECL_ASSEMBLER_NAME (decl
));
545 else if (!alias
->transparent_alias
)
546 do_assemble_alias (alias
->decl
,
547 DECL_ASSEMBLER_NAME (decl
));
548 alias
->assemble_aliases ();
552 /* Output one variable, if necessary. Return whether we output it. */
555 varpool_node::assemble_decl (void)
557 /* Aliases are output when their target is produced or by
562 /* Constant pool is output from RTL land when the reference
563 survive till this level. */
564 if (DECL_IN_CONSTANT_POOL (decl
) && TREE_ASM_WRITTEN (decl
))
567 /* Decls with VALUE_EXPR should not be in the varpool at all. They
568 are not real variables, but just info for debugging and codegen.
569 Unfortunately at the moment emutls is not updating varpool correctly
570 after turning real vars into value_expr vars. */
571 if (DECL_HAS_VALUE_EXPR_P (decl
)
572 && !targetm
.have_tls
)
575 /* Hard register vars do not need to be output. */
576 if (DECL_HARD_REGISTER (decl
))
579 gcc_checking_assert (!TREE_ASM_WRITTEN (decl
)
581 && !DECL_HAS_VALUE_EXPR_P (decl
));
583 if (!in_other_partition
584 && !DECL_EXTERNAL (decl
))
587 assemble_variable (decl
, 0, 1, 0);
588 gcc_assert (TREE_ASM_WRITTEN (decl
));
589 gcc_assert (definition
);
591 /* After the parser has generated debugging information, augment
592 this information with any new location/etc information that may
593 have become available after the compilation proper. */
594 debug_hooks
->late_global_decl (decl
);
601 /* Add NODE to queue starting at FIRST.
602 The queue is linked via AUX pointers and terminated by pointer to 1. */
605 enqueue_node (varpool_node
*node
, varpool_node
**first
)
609 gcc_checking_assert (*first
);
614 /* Optimization of function bodies might've rendered some variables as
615 unnecessary so we want to avoid these from being compiled. Re-do
616 reachability starting from variables that are either externally visible
617 or was referred from the asm output routines. */
620 symbol_table::remove_unreferenced_decls (void)
622 varpool_node
*next
, *node
;
623 varpool_node
*first
= (varpool_node
*)(void *)1;
626 hash_set
<varpool_node
*> referenced
;
632 fprintf (dump_file
, "Trivially needed variables:");
633 FOR_EACH_DEFINED_VARIABLE (node
)
636 && (!node
->can_remove_if_no_refs_p ()
637 /* We just expanded all function bodies. See if any of
638 them needed the variable. */
639 || DECL_RTL_SET_P (node
->decl
)))
641 enqueue_node (node
, &first
);
643 fprintf (dump_file
, " %s", node
->dump_asm_name ());
646 while (first
!= (varpool_node
*)(void *)1)
649 first
= (varpool_node
*)first
->aux
;
651 if (node
->same_comdat_group
)
654 for (next
= node
->same_comdat_group
;
656 next
= next
->same_comdat_group
)
658 varpool_node
*vnext
= dyn_cast
<varpool_node
*> (next
);
659 if (vnext
&& vnext
->analyzed
&& !next
->comdat_local_p ())
660 enqueue_node (vnext
, &first
);
663 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
665 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (ref
->referred
);
667 && !vnode
->in_other_partition
668 && (!DECL_EXTERNAL (ref
->referred
->decl
)
671 enqueue_node (vnode
, &first
);
674 referenced
.add (vnode
);
675 while (vnode
&& vnode
->alias
&& vnode
->definition
)
677 vnode
= vnode
->get_alias_target ();
678 referenced
.add (vnode
);
684 fprintf (dump_file
, "\nRemoving variables:");
685 for (node
= first_defined_variable (); node
; node
= next
)
687 next
= next_defined_variable (node
);
688 if (!node
->aux
&& !node
->no_reorder
)
691 fprintf (dump_file
, " %s", node
->dump_asm_name ());
692 if (referenced
.contains(node
))
693 node
->remove_initializer ();
700 fprintf (dump_file
, "\n");
703 /* For variables in named sections make sure get_variable_section
704 is called before we switch to those sections. Then section
705 conflicts between read-only and read-only requiring relocations
706 sections can be resolved. */
708 varpool_node::finalize_named_section_flags (void)
710 if (!TREE_ASM_WRITTEN (decl
)
712 && !in_other_partition
713 && !DECL_EXTERNAL (decl
)
715 && !DECL_HAS_VALUE_EXPR_P (decl
)
717 get_variable_section (decl
, false);
720 /* Output all variables enqueued to be assembled. */
722 symbol_table::output_variables (void)
724 bool changed
= false;
730 remove_unreferenced_decls ();
732 timevar_push (TV_VAROUT
);
734 FOR_EACH_DEFINED_VARIABLE (node
)
736 /* Handled in output_in_order. */
737 if (node
->no_reorder
)
740 node
->finalize_named_section_flags ();
743 /* There is a similar loop in output_in_order. Please keep them in sync. */
744 FOR_EACH_VARIABLE (node
)
746 /* Handled in output_in_order. */
747 if (node
->no_reorder
)
749 if (DECL_HARD_REGISTER (node
->decl
)
750 || DECL_HAS_VALUE_EXPR_P (node
->decl
))
752 if (node
->definition
)
753 changed
|= node
->assemble_decl ();
755 assemble_undefined_decl (node
->decl
);
757 timevar_pop (TV_VAROUT
);
761 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
762 Extra name aliases are output whenever DECL is output. */
765 varpool_node::create_alias (tree alias
, tree decl
)
767 varpool_node
*alias_node
;
769 gcc_assert (VAR_P (decl
));
770 gcc_assert (VAR_P (alias
));
771 alias_node
= varpool_node::get_create (alias
);
772 alias_node
->alias
= true;
773 alias_node
->definition
= true;
774 alias_node
->alias_target
= decl
;
775 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
776 alias_node
->weakref
= alias_node
->transparent_alias
= true;
780 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
781 Extra name aliases are output whenever DECL is output. */
784 varpool_node::create_extra_name_alias (tree alias
, tree decl
)
786 varpool_node
*alias_node
;
788 /* If aliases aren't supported by the assembler, fail. */
789 if (!TARGET_SUPPORTS_ALIASES
)
792 alias_node
= varpool_node::create_alias (alias
, decl
);
793 alias_node
->cpp_implicit_alias
= true;
795 /* Extra name alias mechanism creates aliases really late
796 via DECL_ASSEMBLER_NAME mechanism.
797 This is unfortunate because they are not going through the
798 standard channels. Ensure they get output. */
799 if (symtab
->cpp_implicit_aliases_done
)
800 alias_node
->resolve_alias (varpool_node::get_create (decl
));
804 /* Worker for call_for_symbol_and_aliases. */
807 varpool_node::call_for_symbol_and_aliases_1 (bool (*callback
) (varpool_node
*,
810 bool include_overwritable
)
814 FOR_EACH_ALIAS (this, ref
)
816 varpool_node
*alias
= dyn_cast
<varpool_node
*> (ref
->referring
);
817 if (include_overwritable
818 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
819 if (alias
->call_for_symbol_and_aliases (callback
, data
,
820 include_overwritable
))