2 Copyright (C) 2003-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file implements two related passes:
22 - pass_data_ipa_function_and_variable_visibility run just after
23 symbol table, references and callgraph are built
25 - pass_data_ipa_function_and_variable_visibility run as first
26 proper IPA pass (that is after early optimization, or, (with LTO)
27 as a first pass done at link-time.
29 Purpose of both passes is to set correctly visibility properties
30 of all symbols. This includes:
32 - Symbol privatization:
34 Some symbols that are declared public by frontend may be
35 turned local (either by -fwhole-program flag, by linker plugin feedback
38 - Discovery of local functions:
40 A local function is one whose calls can occur only in the current
41 compilation unit and all its calls are explicit, so we can change
42 its calling convention. We simply mark all static functions whose
43 address is not taken as local.
45 externally_visible flag is set for symbols that cannot be privatized.
46 For privatized symbols we clear TREE_PUBLIC flag and dismantle comdat
49 - Dismantling of comdat groups:
51 Comdat group represent a section that may be replaced by linker by
52 a different copy of the same section from other unit.
53 If we have resolution information (from linker plugin) and we know that
54 a given comdat gorup is prevailing, we can dismantle it and turn symbols
55 into normal symbols. If the resolution information says that the
56 section was previaled by copy from non-LTO code, we can also dismantle
57 it and turn all symbols into external.
61 Some symbols can be interposed by dynamic linker. Refering to these
62 symbols is expensive, since it needs to be overwritable by the dynamic
63 linker. In some cases we know that the interposition does not change
64 semantic and we can always refer to a local copy (as in the case of
65 inline function). In this case we produce a local alias and redirect
68 TODO: This should be done for references, too.
70 - Removal of static ocnstructors and destructors that have no side effects.
72 - Regularization of several oddities introduced by frontends that may
73 be impractical later in the optimization queue. */
77 #include "coretypes.h"
81 #include "gimple-expr.h"
82 #include "tree-pass.h"
86 #include "ipa-utils.h"
87 #include "stringpool.h"
90 /* Return true when NODE cannot be local. Worker for cgraph_local_node_p. */
93 non_local_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
95 return !(node
->only_called_directly_or_aliased_p ()
96 /* i386 would need update to output thunk with local calling
100 && !DECL_EXTERNAL (node
->decl
)
101 && !lookup_attribute ("noipa", DECL_ATTRIBUTES (node
->decl
))
102 && !node
->externally_visible
103 && !node
->used_from_other_partition
104 && !node
->in_other_partition
105 && node
->get_availability () >= AVAIL_AVAILABLE
);
108 /* Return true when function can be marked local. */
111 cgraph_node::local_p (void)
113 cgraph_node
*n
= ultimate_alias_target ();
116 return n
->callees
->callee
->local_p ();
117 return !n
->call_for_symbol_thunks_and_aliases (non_local_p
,
122 /* A helper for comdat_can_be_unshared_p. */
125 comdat_can_be_unshared_p_1 (symtab_node
*node
)
127 if (!node
->externally_visible
)
129 if (node
->address_can_be_compared_p ())
133 for (unsigned int i
= 0; node
->iterate_referring (i
, ref
); i
++)
134 if (ref
->address_matters_p ())
138 /* If the symbol is used in some weird way, better to not touch it. */
139 if (node
->force_output
)
142 /* Explicit instantiations needs to be output when possibly
144 if (node
->forced_by_abi
145 && TREE_PUBLIC (node
->decl
)
146 && (node
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
147 && !flag_whole_program
))
150 /* Non-readonly and volatile variables cannot be duplicated. */
151 if (is_a
<varpool_node
*> (node
)
152 && (!TREE_READONLY (node
->decl
)
153 || TREE_THIS_VOLATILE (node
->decl
)))
158 /* COMDAT functions must be shared only if they have address taken,
159 otherwise we can produce our own private implementation with
161 Return true when turning COMDAT function static cannot lead to wrong
162 code when the resulting object links with a library defining same COMDAT.
164 Virtual functions do have their addresses taken from the vtables,
165 but in C++ there is no way to compare their addresses for equality. */
168 comdat_can_be_unshared_p (symtab_node
*node
)
170 if (!comdat_can_be_unshared_p_1 (node
))
172 if (node
->same_comdat_group
)
176 /* If more than one function is in the same COMDAT group, it must
177 be shared even if just one function in the comdat group has
179 for (next
= node
->same_comdat_group
;
180 next
!= node
; next
= next
->same_comdat_group
)
181 if (!comdat_can_be_unshared_p_1 (next
))
187 /* Return true when function NODE should be considered externally visible. */
190 cgraph_externally_visible_p (struct cgraph_node
*node
,
193 while (node
->transparent_alias
&& node
->definition
)
194 node
= node
->get_alias_target ();
195 if (!node
->definition
)
197 if (!TREE_PUBLIC (node
->decl
)
198 || DECL_EXTERNAL (node
->decl
))
201 /* Do not try to localize built-in functions yet. One of problems is that we
202 end up mangling their asm for WHOPR that makes it impossible to call them
203 using the implicit built-in declarations anymore. Similarly this enables
204 us to remove them as unreachable before actual calls may appear during
205 expansion or folding. */
206 if (fndecl_built_in_p (node
->decl
))
209 /* If linker counts on us, we must preserve the function. */
210 if (node
->used_from_object_file_p ())
212 if (DECL_PRESERVE_P (node
->decl
))
214 if (lookup_attribute ("externally_visible",
215 DECL_ATTRIBUTES (node
->decl
)))
217 if (lookup_attribute ("noipa", DECL_ATTRIBUTES (node
->decl
)))
219 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
220 && lookup_attribute ("dllexport",
221 DECL_ATTRIBUTES (node
->decl
)))
224 /* Limitation of gas requires us to output targets of symver aliases as
225 global symbols. This is binutils PR 25295. */
227 FOR_EACH_ALIAS (node
, ref
)
228 if (ref
->referring
->symver
)
231 if (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
233 /* When doing LTO or whole program, we can bring COMDAT functions static.
234 This improves code quality and we know we will duplicate them at most twice
235 (in the case that we are not using plugin and link with object file
236 implementing same COMDAT) */
237 if (((in_lto_p
|| whole_program
) && !flag_incremental_link
)
238 && DECL_COMDAT (node
->decl
)
239 && comdat_can_be_unshared_p (node
))
242 /* When doing link time optimizations, hidden symbols become local. */
243 if ((in_lto_p
&& !flag_incremental_link
)
244 && (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
245 || DECL_VISIBILITY (node
->decl
) == VISIBILITY_INTERNAL
)
246 /* Be sure that node is defined in IR file, not in other object
247 file. In that case we don't set used_from_other_object_file. */
250 else if (!whole_program
)
253 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
259 /* Return true when variable should be considered externally visible. */
262 varpool_node::externally_visible_p (void)
264 while (transparent_alias
&& definition
)
265 return get_alias_target ()->externally_visible_p ();
266 if (DECL_EXTERNAL (decl
))
269 if (!TREE_PUBLIC (decl
))
272 /* If linker counts on us, we must preserve the function. */
273 if (used_from_object_file_p ())
276 /* Bringing TLS variables local may cause dynamic linker failures
277 on limits of static TLS vars. */
278 if (DECL_THREAD_LOCAL_P (decl
)
279 && (DECL_TLS_MODEL (decl
) != TLS_MODEL_EMULATED
280 && DECL_TLS_MODEL (decl
) != TLS_MODEL_INITIAL_EXEC
))
283 if (DECL_HARD_REGISTER (decl
))
285 if (DECL_PRESERVE_P (decl
))
287 if (lookup_attribute ("externally_visible",
288 DECL_ATTRIBUTES (decl
)))
290 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
291 && lookup_attribute ("dllexport",
292 DECL_ATTRIBUTES (decl
)))
295 /* Limitation of gas requires us to output targets of symver aliases as
296 global symbols. This is binutils PR 25295. */
298 FOR_EACH_ALIAS (this, ref
)
299 if (ref
->referring
->symver
)
302 if (resolution
== LDPR_PREVAILING_DEF_IRONLY
)
305 /* As a special case, the COMDAT virtual tables can be unshared.
306 In LTO mode turn vtables into static variables. The variable is readonly,
307 so this does not enable more optimization, but referring static var
308 is faster for dynamic linking. Also this match logic hidding vtables
309 from LTO symbol tables. */
310 if (((in_lto_p
|| flag_whole_program
) && !flag_incremental_link
)
311 && DECL_COMDAT (decl
)
312 && comdat_can_be_unshared_p (this))
315 /* When doing link time optimizations, hidden symbols become local. */
316 if (in_lto_p
&& !flag_incremental_link
317 && (DECL_VISIBILITY (decl
) == VISIBILITY_HIDDEN
318 || DECL_VISIBILITY (decl
) == VISIBILITY_INTERNAL
)
319 /* Be sure that node is defined in IR file, not in other object
320 file. In that case we don't set used_from_other_object_file. */
323 else if (!flag_whole_program
)
326 /* Do not attempt to privatize COMDATS by default.
327 This would break linking with C++ libraries sharing
330 FIXME: We can do so for readonly vars with no address taken and
331 possibly also for vtables since no direct pointer comparsion is done.
332 It might be interesting to do so to reduce linking overhead. */
333 if (DECL_COMDAT (decl
) || DECL_WEAK (decl
))
338 /* Return true if reference to NODE can be replaced by a local alias.
339 Local aliases save dynamic linking overhead and enable more optimizations.
343 can_replace_by_local_alias (symtab_node
*node
)
345 /* If aliases aren't supported, we can't do replacement. */
346 if (!TARGET_SUPPORTS_ALIASES
)
349 /* Weakrefs have a reason to be non-local. Be sure we do not replace
351 while (node
->transparent_alias
&& node
->definition
&& !node
->weakref
)
352 node
= node
->get_alias_target ();
356 return (node
->get_availability () > AVAIL_INTERPOSABLE
357 && !decl_binds_to_current_def_p (node
->decl
)
358 && !node
->can_be_discarded_p ());
361 /* Return true if we can replace reference to NODE by local alias
362 within a virtual table. Generally we can replace function pointers
363 and virtual table pointers. */
366 can_replace_by_local_alias_in_vtable (symtab_node
*node
)
368 if (is_a
<varpool_node
*> (node
)
369 && !DECL_VIRTUAL_P (node
->decl
))
371 return can_replace_by_local_alias (node
);
374 /* walk_tree callback that rewrites initializer references. */
377 update_vtable_references (tree
*tp
, int *walk_subtrees
,
378 void *data ATTRIBUTE_UNUSED
)
380 if (VAR_OR_FUNCTION_DECL_P (*tp
))
382 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp
)))
383 *tp
= symtab_node::get (*tp
)->noninterposable_alias ()->decl
;
386 else if (IS_TYPE_OR_DECL_P (*tp
))
391 /* In LTO we can remove COMDAT groups and weak symbols.
392 Either turn them into normal symbols or external symbol depending on
396 update_visibility_by_resolution_info (symtab_node
* node
)
400 if (!node
->externally_visible
401 || (!DECL_WEAK (node
->decl
) && !DECL_ONE_ONLY (node
->decl
))
402 || node
->resolution
== LDPR_UNKNOWN
)
405 define
= (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
406 || node
->resolution
== LDPR_PREVAILING_DEF
407 || node
->resolution
== LDPR_UNDEF
408 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
410 /* The linker decisions ought to agree in the whole group. */
411 if (node
->same_comdat_group
)
412 for (symtab_node
*next
= node
->same_comdat_group
;
413 next
!= node
; next
= next
->same_comdat_group
)
415 if (!next
->externally_visible
|| next
->transparent_alias
)
419 = define
== (next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
420 || next
->resolution
== LDPR_PREVAILING_DEF
421 || next
->resolution
== LDPR_UNDEF
422 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
423 gcc_assert (in_lto_p
|| same_def
);
428 if (node
->same_comdat_group
)
429 for (symtab_node
*next
= node
->same_comdat_group
;
430 next
!= node
; next
= next
->same_comdat_group
)
432 /* During incremental linking we need to keep symbol weak for future
433 linking. We can still drop definition if we know non-LTO world
435 if (!flag_incremental_link
)
437 DECL_WEAK (next
->decl
) = false;
438 next
->set_comdat_group (NULL
);
442 if (next
->externally_visible
)
443 DECL_EXTERNAL (next
->decl
) = true;
444 next
->set_comdat_group (NULL
);
448 /* During incremental linking we need to keep symbol weak for future
449 linking. We can still drop definition if we know non-LTO world prevails. */
450 if (!flag_incremental_link
)
452 DECL_WEAK (node
->decl
) = false;
453 node
->set_comdat_group (NULL
);
454 node
->dissolve_same_comdat_group_list ();
458 DECL_EXTERNAL (node
->decl
) = true;
459 node
->set_comdat_group (NULL
);
460 node
->dissolve_same_comdat_group_list ();
464 /* Try to get rid of weakref. */
467 optimize_weakref (symtab_node
*node
)
469 bool strip_weakref
= false;
470 bool static_alias
= false;
472 gcc_assert (node
->weakref
);
474 /* Weakrefs with no target defined cannot be optimized. */
477 symtab_node
*target
= node
->get_alias_target ();
479 /* Weakrefs to weakrefs can be optimized only if target can be. */
481 optimize_weakref (target
);
485 /* If we have definition of weakref's target and we know it binds locally,
486 we can turn weakref to static alias. */
487 if (TARGET_SUPPORTS_ALIASES
488 && target
->definition
&& decl_binds_to_current_def_p (target
->decl
))
489 strip_weakref
= static_alias
= true;
490 /* Otherwise we can turn weakref into transparent alias. This transformation
491 may break asm statements which directly refers to symbol name and expect
492 GNU as to translate it via .weakref directive. So do not optimize when
493 DECL_PRESERVED is set and .weakref is supported. */
494 else if ((!DECL_PRESERVE_P (target
->decl
)
495 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node
->decl
)))
496 && !DECL_WEAK (target
->decl
)
497 && !DECL_EXTERNAL (target
->decl
)
498 && ((target
->definition
&& !target
->can_be_discarded_p ())
499 || target
->resolution
!= LDPR_UNDEF
))
500 strip_weakref
= true;
503 node
->weakref
= false;
504 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node
->decl
)) = 0;
505 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
)) = NULL_TREE
;
506 DECL_ATTRIBUTES (node
->decl
) = remove_attribute ("weakref",
511 fprintf (dump_file
, "Optimizing weakref %s %s\n",
513 static_alias
? "as static alias" : "as transparent alias");
517 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
518 be sure it really clears the WEAK flag. */
519 TREE_PUBLIC (node
->decl
) = true;
520 node
->make_decl_local ();
521 node
->forced_by_abi
= false;
522 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
523 node
->externally_visible
= false;
524 gcc_assert (!DECL_WEAK (node
->decl
));
525 node
->transparent_alias
= false;
529 symtab
->change_decl_assembler_name
530 (node
->decl
, DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
531 node
->transparent_alias
= true;
532 node
->copy_visibility_from (target
);
534 gcc_assert (node
->alias
);
537 /* NODE is an externally visible definition, which we've discovered is
538 not needed externally. Make it local to this compilation. */
541 localize_node (bool whole_program
, symtab_node
*node
)
543 gcc_assert (whole_program
|| in_lto_p
|| !TREE_PUBLIC (node
->decl
));
545 /* It is possible that one comdat group contains both hidden and non-hidden
546 symbols. In this case we can privatize all hidden symbol but we need
547 to keep non-hidden exported. */
548 if (node
->same_comdat_group
549 && (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
550 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
))
553 for (next
= node
->same_comdat_group
;
554 next
!= node
; next
= next
->same_comdat_group
)
555 if (next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
556 || next
->resolution
== LDPR_PREVAILING_DEF
)
560 if (!node
->transparent_alias
)
562 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
563 node
->make_decl_local ();
564 if (!flag_incremental_link
)
565 node
->unique_name
|= true;
570 /* For similar reason do not privatize whole comdat when seeing comdat
571 local. Wait for non-comdat symbol to be privatized first. */
572 if (node
->comdat_local_p ())
575 if (node
->same_comdat_group
&& TREE_PUBLIC (node
->decl
))
577 for (symtab_node
*next
= node
->same_comdat_group
;
578 next
!= node
; next
= next
->same_comdat_group
)
580 next
->set_comdat_group (NULL
);
582 next
->set_section (NULL
);
583 if (!next
->transparent_alias
)
584 next
->make_decl_local ();
586 |= ((next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
587 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
588 && TREE_PUBLIC (next
->decl
)
589 && !flag_incremental_link
);
592 /* Now everything's localized, the grouping has no meaning, and
593 will cause crashes if we keep it around. */
594 node
->dissolve_same_comdat_group_list ();
598 |= ((node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
599 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
600 && TREE_PUBLIC (node
->decl
)
601 && !flag_incremental_link
);
603 if (TREE_PUBLIC (node
->decl
))
604 node
->set_comdat_group (NULL
);
605 if (DECL_COMDAT (node
->decl
) && !node
->alias
)
606 node
->set_section (NULL
);
607 if (!node
->transparent_alias
)
609 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
610 node
->make_decl_local ();
614 /* Decide on visibility of all symbols. */
617 function_and_variable_visibility (bool whole_program
)
619 struct cgraph_node
*node
;
622 /* All aliases should be processed at this point. */
623 gcc_checking_assert (!alias_pairs
|| !alias_pairs
->length ());
625 if (TARGET_SUPPORTS_ALIASES
)
627 FOR_EACH_DEFINED_FUNCTION (node
)
629 if (node
->get_availability () != AVAIL_INTERPOSABLE
630 || DECL_EXTERNAL (node
->decl
)
631 || node
->has_aliases_p ()
632 || lookup_attribute ("noipa", DECL_ATTRIBUTES (node
->decl
)))
635 cgraph_node
*alias
= 0;
636 cgraph_edge
*next_edge
;
637 for (cgraph_edge
*e
= node
->callees
; e
; e
= next_edge
)
639 next_edge
= e
->next_callee
;
640 /* Recursive function calls usually can't be interposed. */
642 if (!e
->recursive_p ())
648 = dyn_cast
<cgraph_node
*> (node
->noninterposable_alias ());
649 gcc_assert (alias
&& alias
!= node
);
652 e
->redirect_callee (alias
);
653 if (gimple_has_body_p (e
->caller
->decl
))
655 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
656 cgraph_edge::redirect_call_stmt_to_callee (e
);
663 FOR_EACH_FUNCTION (node
)
665 int flags
= flags_from_decl_or_type (node
->decl
);
667 /* Optimize away PURE and CONST constructors and destructors. */
669 && (DECL_STATIC_CONSTRUCTOR (node
->decl
)
670 || DECL_STATIC_DESTRUCTOR (node
->decl
))
671 && (flags
& (ECF_CONST
| ECF_PURE
))
672 && !(flags
& ECF_LOOPING_CONST_OR_PURE
)
673 && opt_for_fn (node
->decl
, optimize
))
675 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
676 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
679 /* Frontends and alias code marks nodes as needed before parsing
680 is finished. We may end up marking as node external nodes
681 where this flag is meaningless strip it. */
682 if (DECL_EXTERNAL (node
->decl
) || !node
->definition
)
684 node
->force_output
= 0;
685 node
->forced_by_abi
= 0;
688 /* C++ FE on lack of COMDAT support create local COMDAT functions
689 (that ought to be shared but cannot due to object format
690 limitations). It is necessary to keep the flag to make rest of C++ FE
691 happy. Clear the flag here to avoid confusion in middle-end. */
692 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
693 DECL_COMDAT (node
->decl
) = 0;
695 /* For external decls stop tracking same_comdat_group. It doesn't matter
696 what comdat group they are in when they won't be emitted in this TU.
698 An exception is LTO where we may end up with both external
699 and non-external declarations in the same comdat group in
700 the case declarations was not merged. */
701 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
) && !in_lto_p
)
705 for (symtab_node
*n
= node
->same_comdat_group
;
707 n
= n
->same_comdat_group
)
708 /* If at least one of same comdat group functions is external,
709 all of them have to be, otherwise it is a front-end bug. */
710 gcc_assert (DECL_EXTERNAL (n
->decl
));
712 node
->dissolve_same_comdat_group_list ();
714 gcc_assert ((!DECL_WEAK (node
->decl
)
715 && !DECL_COMDAT (node
->decl
))
716 || TREE_PUBLIC (node
->decl
)
718 || DECL_EXTERNAL (node
->decl
));
719 if (cgraph_externally_visible_p (node
, whole_program
))
721 gcc_assert (!node
->inlined_to
);
722 node
->externally_visible
= true;
726 node
->externally_visible
= false;
727 node
->forced_by_abi
= false;
729 if (!node
->externally_visible
730 && node
->definition
&& !node
->weakref
731 && !DECL_EXTERNAL (node
->decl
))
732 localize_node (whole_program
, node
);
735 && TREE_PUBLIC (node
->decl
))
737 struct cgraph_node
*decl_node
= node
;
739 decl_node
= decl_node
->callees
->callee
->function_symbol ();
741 /* Thunks have the same visibility as function they are attached to.
742 Make sure the C++ front end set this up properly. */
743 if (DECL_ONE_ONLY (decl_node
->decl
))
745 gcc_checking_assert (DECL_COMDAT (node
->decl
)
746 == DECL_COMDAT (decl_node
->decl
));
747 gcc_checking_assert (node
->in_same_comdat_group_p (decl_node
));
748 gcc_checking_assert (node
->same_comdat_group
);
750 node
->forced_by_abi
= decl_node
->forced_by_abi
;
751 if (DECL_EXTERNAL (decl_node
->decl
))
752 DECL_EXTERNAL (node
->decl
) = 1;
755 update_visibility_by_resolution_info (node
);
757 optimize_weakref (node
);
759 FOR_EACH_DEFINED_FUNCTION (node
)
762 node
->local
|= node
->local_p ();
764 /* If we know that function cannot be overwritten by a
765 different semantics and moreover its section cannot be
766 discarded, replace all direct calls by calls to an
767 noninterposable alias. This make dynamic linking cheaper and
768 enable more optimization.
770 TODO: We can also update virtual tables. */
772 && can_replace_by_local_alias (node
))
774 cgraph_node
*alias
= dyn_cast
<cgraph_node
*>
775 (node
->noninterposable_alias ());
777 if (alias
&& alias
!= node
)
779 while (node
->callers
)
781 struct cgraph_edge
*e
= node
->callers
;
783 e
->redirect_callee (alias
);
784 if (gimple_has_body_p (e
->caller
->decl
))
786 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
787 cgraph_edge::redirect_call_stmt_to_callee (e
);
794 FOR_EACH_VARIABLE (vnode
)
796 /* weak flag makes no sense on local variables. */
797 gcc_assert (!DECL_WEAK (vnode
->decl
)
799 || TREE_PUBLIC (vnode
->decl
)
800 || DECL_EXTERNAL (vnode
->decl
));
801 /* In several cases declarations cannot be common:
803 - when declaration has initializer
805 - when it has specific section
806 - when it resides in non-generic address space.
807 - if declaration is local, it will get into .local common section
808 so common flag is not needed. Frontends still produce these in
809 certain cases, such as for:
811 static int a __attribute__ ((common))
813 Canonicalize things here and clear the redundant flag. */
814 if (DECL_COMMON (vnode
->decl
)
815 && (!(TREE_PUBLIC (vnode
->decl
)
816 || DECL_EXTERNAL (vnode
->decl
))
817 || (DECL_INITIAL (vnode
->decl
)
818 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
819 || DECL_WEAK (vnode
->decl
)
820 || DECL_SECTION_NAME (vnode
->decl
) != NULL
821 || ! (ADDR_SPACE_GENERIC_P
822 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
823 DECL_COMMON (vnode
->decl
) = 0;
825 optimize_weakref (vnode
);
827 FOR_EACH_DEFINED_VARIABLE (vnode
)
829 if (!vnode
->definition
)
831 if (vnode
->externally_visible_p ())
832 vnode
->externally_visible
= true;
835 vnode
->externally_visible
= false;
836 vnode
->forced_by_abi
= false;
838 if (lookup_attribute ("no_reorder",
839 DECL_ATTRIBUTES (vnode
->decl
)))
840 vnode
->no_reorder
= 1;
842 if (!vnode
->externally_visible
843 && !vnode
->transparent_alias
844 && !DECL_EXTERNAL (vnode
->decl
))
845 localize_node (whole_program
, vnode
);
847 update_visibility_by_resolution_info (vnode
);
849 /* Update virtual tables to point to local aliases where possible. */
850 if (DECL_VIRTUAL_P (vnode
->decl
)
851 && !DECL_EXTERNAL (vnode
->decl
))
857 /* See if there is something to update. */
858 for (i
= 0; vnode
->iterate_reference (i
, ref
); i
++)
859 if (ref
->use
== IPA_REF_ADDR
860 && can_replace_by_local_alias_in_vtable (ref
->referred
))
867 hash_set
<tree
> visited_nodes
;
869 vnode
->get_constructor ();
870 walk_tree (&DECL_INITIAL (vnode
->decl
),
871 update_vtable_references
, NULL
, &visited_nodes
);
872 vnode
->remove_all_references ();
873 record_references_in_initializer (vnode
->decl
, false);
878 if (symtab
->state
>= IPA_SSA
)
880 FOR_EACH_VARIABLE (vnode
)
882 tree decl
= vnode
->decl
;
884 /* Upgrade TLS access model based on optimized visibility status,
885 unless it was specified explicitly or no references remain. */
886 if (DECL_THREAD_LOCAL_P (decl
)
887 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl
))
888 && vnode
->ref_list
.referring
.length ())
890 enum tls_model new_model
= decl_default_tls_model (decl
);
891 STATIC_ASSERT (TLS_MODEL_GLOBAL_DYNAMIC
< TLS_MODEL_LOCAL_DYNAMIC
);
892 STATIC_ASSERT (TLS_MODEL_INITIAL_EXEC
< TLS_MODEL_LOCAL_EXEC
);
893 /* We'd prefer to assert that recomputed model is not weaker than
894 what the front-end assigned, but cannot: see PR 107353. */
895 if (new_model
>= decl_tls_model (decl
))
896 set_decl_tls_model (decl
, new_model
);
903 fprintf (dump_file
, "\nMarking local functions:");
904 FOR_EACH_DEFINED_FUNCTION (node
)
906 fprintf (dump_file
, " %s", node
->dump_name ());
907 fprintf (dump_file
, "\n\n");
908 fprintf (dump_file
, "\nMarking externally visible functions:");
909 FOR_EACH_DEFINED_FUNCTION (node
)
910 if (node
->externally_visible
)
911 fprintf (dump_file
, " %s", node
->dump_name ());
912 fprintf (dump_file
, "\n\n");
913 fprintf (dump_file
, "\nMarking externally visible variables:");
914 FOR_EACH_DEFINED_VARIABLE (vnode
)
915 if (vnode
->externally_visible
)
916 fprintf (dump_file
, " %s", vnode
->dump_name ());
917 fprintf (dump_file
, "\n\n");
919 symtab
->function_flags_ready
= true;
923 /* Local function pass handling visibilities. This happens before LTO streaming
924 so in particular -fwhole-program should be ignored at this level. */
928 const pass_data pass_data_ipa_function_and_variable_visibility
=
930 SIMPLE_IPA_PASS
, /* type */
931 "visibility", /* name */
932 OPTGROUP_NONE
, /* optinfo_flags */
933 TV_CGRAPHOPT
, /* tv_id */
934 0, /* properties_required */
935 0, /* properties_provided */
936 0, /* properties_destroyed */
937 0, /* todo_flags_start */
938 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
941 /* Bring functions local at LTO time with -fwhole-program. */
944 whole_program_function_and_variable_visibility (void)
946 function_and_variable_visibility (flag_whole_program
);
947 if (optimize
|| in_lto_p
)
948 ipa_discover_variable_flags ();
956 const pass_data pass_data_ipa_whole_program_visibility
=
959 "whole-program", /* name */
960 OPTGROUP_NONE
, /* optinfo_flags */
961 TV_CGRAPHOPT
, /* tv_id */
962 0, /* properties_required */
963 0, /* properties_provided */
964 0, /* properties_destroyed */
965 0, /* todo_flags_start */
966 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
969 class pass_ipa_whole_program_visibility
: public ipa_opt_pass_d
972 pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
973 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility
, ctxt
,
974 NULL
, /* generate_summary */
975 NULL
, /* write_summary */
976 NULL
, /* read_summary */
977 NULL
, /* write_optimization_summary */
978 NULL
, /* read_optimization_summary */
979 NULL
, /* stmt_fixup */
980 0, /* function_transform_todo_flags_start */
981 NULL
, /* function_transform */
982 NULL
) /* variable_transform */
985 /* opt_pass methods: */
987 bool gate (function
*) final override
989 /* Do not re-run on ltrans stage. */
992 unsigned int execute (function
*) final override
994 return whole_program_function_and_variable_visibility ();
997 }; // class pass_ipa_whole_program_visibility
1002 make_pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
1004 return new pass_ipa_whole_program_visibility (ctxt
);
1007 class pass_ipa_function_and_variable_visibility
: public simple_ipa_opt_pass
1010 pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
1011 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility
,
1015 /* opt_pass methods: */
1016 unsigned int execute (function
*) final override
1018 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
1021 }; // class pass_ipa_function_and_variable_visibility
1023 simple_ipa_opt_pass
*
1024 make_pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
1026 return new pass_ipa_function_and_variable_visibility (ctxt
);