2 Copyright (C) 2003-2017 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 can not 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"
87 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
90 non_local_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
92 return !(node
->only_called_directly_or_aliased_p ()
93 /* i386 would need update to output thunk with local calling
95 && !node
->thunk
.thunk_p
97 && !DECL_EXTERNAL (node
->decl
)
98 && !node
->externally_visible
99 && !node
->used_from_other_partition
100 && !node
->in_other_partition
);
103 /* Return true when function can be marked local. */
106 cgraph_node::local_p (void)
108 cgraph_node
*n
= ultimate_alias_target ();
110 if (n
->thunk
.thunk_p
)
111 return n
->callees
->callee
->local_p ();
112 return !n
->call_for_symbol_thunks_and_aliases (non_local_p
,
117 /* A helper for comdat_can_be_unshared_p. */
120 comdat_can_be_unshared_p_1 (symtab_node
*node
)
122 if (!node
->externally_visible
)
124 if (node
->address_can_be_compared_p ())
128 for (unsigned int i
= 0; node
->iterate_referring (i
, ref
); i
++)
129 if (ref
->address_matters_p ())
133 /* If the symbol is used in some weird way, better to not touch it. */
134 if (node
->force_output
)
137 /* Explicit instantiations needs to be output when possibly
139 if (node
->forced_by_abi
140 && TREE_PUBLIC (node
->decl
)
141 && (node
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
142 && !flag_whole_program
))
145 /* Non-readonly and volatile variables can not be duplicated. */
146 if (is_a
<varpool_node
*> (node
)
147 && (!TREE_READONLY (node
->decl
)
148 || TREE_THIS_VOLATILE (node
->decl
)))
153 /* COMDAT functions must be shared only if they have address taken,
154 otherwise we can produce our own private implementation with
156 Return true when turning COMDAT function static can not lead to wrong
157 code when the resulting object links with a library defining same COMDAT.
159 Virtual functions do have their addresses taken from the vtables,
160 but in C++ there is no way to compare their addresses for equality. */
163 comdat_can_be_unshared_p (symtab_node
*node
)
165 if (!comdat_can_be_unshared_p_1 (node
))
167 if (node
->same_comdat_group
)
171 /* If more than one function is in the same COMDAT group, it must
172 be shared even if just one function in the comdat group has
174 for (next
= node
->same_comdat_group
;
175 next
!= node
; next
= next
->same_comdat_group
)
176 if (!comdat_can_be_unshared_p_1 (next
))
182 /* Return true when function NODE should be considered externally visible. */
185 cgraph_externally_visible_p (struct cgraph_node
*node
,
188 while (node
->transparent_alias
&& node
->definition
)
189 node
= node
->get_alias_target ();
190 if (!node
->definition
)
192 if (!TREE_PUBLIC (node
->decl
)
193 || DECL_EXTERNAL (node
->decl
))
196 /* Do not try to localize built-in functions yet. One of problems is that we
197 end up mangling their asm for WHOPR that makes it impossible to call them
198 using the implicit built-in declarations anymore. Similarly this enables
199 us to remove them as unreachable before actual calls may appear during
200 expansion or folding. */
201 if (DECL_BUILT_IN (node
->decl
))
204 /* If linker counts on us, we must preserve the function. */
205 if (node
->used_from_object_file_p ())
207 if (DECL_PRESERVE_P (node
->decl
))
209 if (lookup_attribute ("externally_visible",
210 DECL_ATTRIBUTES (node
->decl
)))
212 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
213 && lookup_attribute ("dllexport",
214 DECL_ATTRIBUTES (node
->decl
)))
216 if (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
218 /* When doing LTO or whole program, we can bring COMDAT functoins static.
219 This improves code quality and we know we will duplicate them at most twice
220 (in the case that we are not using plugin and link with object file
221 implementing same COMDAT) */
222 if (((in_lto_p
|| whole_program
) && !flag_incremental_link
)
223 && DECL_COMDAT (node
->decl
)
224 && comdat_can_be_unshared_p (node
))
227 /* When doing link time optimizations, hidden symbols become local. */
228 if ((in_lto_p
&& !flag_incremental_link
)
229 && (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
230 || DECL_VISIBILITY (node
->decl
) == VISIBILITY_INTERNAL
)
231 /* Be sure that node is defined in IR file, not in other object
232 file. In that case we don't set used_from_other_object_file. */
235 else if (!whole_program
)
238 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
241 if (node
->instrumentation_clone
242 && MAIN_NAME_P (DECL_NAME (node
->orig_decl
)))
248 /* Return true when variable should be considered externally visible. */
251 varpool_node::externally_visible_p (void)
253 while (transparent_alias
&& definition
)
254 return get_alias_target ()->externally_visible_p ();
255 if (DECL_EXTERNAL (decl
))
258 if (!TREE_PUBLIC (decl
))
261 /* If linker counts on us, we must preserve the function. */
262 if (used_from_object_file_p ())
265 /* Bringing TLS variables local may cause dynamic linker failures
266 on limits of static TLS vars. */
267 if (DECL_THREAD_LOCAL_P (decl
)
268 && (DECL_TLS_MODEL (decl
) != TLS_MODEL_EMULATED
269 && DECL_TLS_MODEL (decl
) != TLS_MODEL_INITIAL_EXEC
))
272 if (DECL_HARD_REGISTER (decl
))
274 if (DECL_PRESERVE_P (decl
))
276 if (lookup_attribute ("externally_visible",
277 DECL_ATTRIBUTES (decl
)))
279 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
280 && lookup_attribute ("dllexport",
281 DECL_ATTRIBUTES (decl
)))
284 /* See if we have linker information about symbol not being used or
285 if we need to make guess based on the declaration.
287 Even if the linker clams the symbol is unused, never bring internal
288 symbols that are declared by user as used or externally visible.
289 This is needed for i.e. references from asm statements. */
290 if (used_from_object_file_p ())
292 if (resolution
== LDPR_PREVAILING_DEF_IRONLY
)
295 /* As a special case, the COMDAT virtual tables can be unshared.
296 In LTO mode turn vtables into static variables. The variable is readonly,
297 so this does not enable more optimization, but referring static var
298 is faster for dynamic linking. Also this match logic hidding vtables
299 from LTO symbol tables. */
300 if (((in_lto_p
|| flag_whole_program
) && !flag_incremental_link
)
301 && DECL_COMDAT (decl
)
302 && comdat_can_be_unshared_p (this))
305 /* When doing link time optimizations, hidden symbols become local. */
306 if (in_lto_p
&& !flag_incremental_link
307 && (DECL_VISIBILITY (decl
) == VISIBILITY_HIDDEN
308 || DECL_VISIBILITY (decl
) == VISIBILITY_INTERNAL
)
309 /* Be sure that node is defined in IR file, not in other object
310 file. In that case we don't set used_from_other_object_file. */
313 else if (!flag_whole_program
)
316 /* Do not attempt to privatize COMDATS by default.
317 This would break linking with C++ libraries sharing
320 FIXME: We can do so for readonly vars with no address taken and
321 possibly also for vtables since no direct pointer comparsion is done.
322 It might be interesting to do so to reduce linking overhead. */
323 if (DECL_COMDAT (decl
) || DECL_WEAK (decl
))
328 /* Return true if reference to NODE can be replaced by a local alias.
329 Local aliases save dynamic linking overhead and enable more optimizations.
333 can_replace_by_local_alias (symtab_node
*node
)
335 #ifndef ASM_OUTPUT_DEF
336 /* If aliases aren't supported, we can't do replacement. */
339 /* Weakrefs have a reason to be non-local. Be sure we do not replace
341 while (node
->transparent_alias
&& node
->definition
&& !node
->weakref
)
342 node
= node
->get_alias_target ();
346 return (node
->get_availability () > AVAIL_INTERPOSABLE
347 && !decl_binds_to_current_def_p (node
->decl
)
348 && !node
->can_be_discarded_p ());
351 /* Return true if we can replace reference to NODE by local alias
352 within a virtual table. Generally we can replace function pointers
353 and virtual table pointers. */
356 can_replace_by_local_alias_in_vtable (symtab_node
*node
)
358 if (is_a
<varpool_node
*> (node
)
359 && !DECL_VIRTUAL_P (node
->decl
))
361 return can_replace_by_local_alias (node
);
364 /* walk_tree callback that rewrites initializer references. */
367 update_vtable_references (tree
*tp
, int *walk_subtrees
,
368 void *data ATTRIBUTE_UNUSED
)
370 if (VAR_OR_FUNCTION_DECL_P (*tp
))
372 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp
)))
373 *tp
= symtab_node::get (*tp
)->noninterposable_alias ()->decl
;
376 else if (IS_TYPE_OR_DECL_P (*tp
))
381 /* In LTO we can remove COMDAT groups and weak symbols.
382 Either turn them into normal symbols or external symbol depending on
386 update_visibility_by_resolution_info (symtab_node
* node
)
390 if (!node
->externally_visible
391 || (!DECL_WEAK (node
->decl
) && !DECL_ONE_ONLY (node
->decl
))
392 || node
->resolution
== LDPR_UNKNOWN
)
395 define
= (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
396 || node
->resolution
== LDPR_PREVAILING_DEF
397 || node
->resolution
== LDPR_UNDEF
398 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
400 /* The linker decisions ought to agree in the whole group. */
401 if (node
->same_comdat_group
)
402 for (symtab_node
*next
= node
->same_comdat_group
;
403 next
!= node
; next
= next
->same_comdat_group
)
405 if (!next
->externally_visible
|| next
->transparent_alias
)
409 = define
== (next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
410 || next
->resolution
== LDPR_PREVAILING_DEF
411 || next
->resolution
== LDPR_UNDEF
412 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
413 gcc_assert (in_lto_p
|| same_def
);
418 if (node
->same_comdat_group
)
419 for (symtab_node
*next
= node
->same_comdat_group
;
420 next
!= node
; next
= next
->same_comdat_group
)
422 /* During incremental linking we need to keep symbol weak for future
423 linking. We can still drop definition if we know non-LTO world
425 if (!flag_incremental_link
)
427 DECL_WEAK (next
->decl
) = false;
428 next
->set_comdat_group (NULL
);
432 if (next
->externally_visible
)
433 DECL_EXTERNAL (next
->decl
) = true;
434 next
->set_comdat_group (NULL
);
438 /* During incremental linking we need to keep symbol weak for future
439 linking. We can still drop definition if we know non-LTO world prevails. */
440 if (!flag_incremental_link
)
442 DECL_WEAK (node
->decl
) = false;
443 node
->set_comdat_group (NULL
);
444 node
->dissolve_same_comdat_group_list ();
448 DECL_EXTERNAL (node
->decl
) = true;
449 node
->set_comdat_group (NULL
);
450 node
->dissolve_same_comdat_group_list ();
454 /* Try to get rid of weakref. */
457 optimize_weakref (symtab_node
*node
)
459 #ifdef ASM_OUTPUT_DEF
460 bool aliases_supported
= true;
462 bool aliases_supported
= false;
464 bool strip_weakref
= false;
465 bool static_alias
= false;
467 gcc_assert (node
->weakref
);
469 /* Weakrefs with no target defined can not be optimized. */
472 symtab_node
*target
= node
->get_alias_target ();
474 /* Weakrefs to weakrefs can be optimized only if target can be. */
476 optimize_weakref (target
);
480 /* If we have definition of weakref's target and we know it binds locally,
481 we can turn weakref to static alias. */
482 if (target
->definition
&& decl_binds_to_current_def_p (target
->decl
)
483 && aliases_supported
)
484 strip_weakref
= static_alias
= true;
485 /* Otherwise we can turn weakref into transparent alias. This transformation
486 may break asm statements which directly refers to symbol name and expect
487 GNU as to translate it via .weakref directive. So do not optimize when
488 DECL_PRESERVED is set and .weakref is supported. */
489 else if ((!DECL_PRESERVE_P (target
->decl
)
490 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node
->decl
)))
491 && !DECL_WEAK (target
->decl
)
492 && !DECL_EXTERNAL (target
->decl
)
493 && ((target
->definition
&& !target
->can_be_discarded_p ())
494 || target
->resolution
!= LDPR_UNDEF
))
495 strip_weakref
= true;
498 node
->weakref
= false;
499 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node
->decl
)) = 0;
500 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
)) = NULL_TREE
;
501 DECL_ATTRIBUTES (node
->decl
) = remove_attribute ("weakref",
506 fprintf (dump_file
, "Optimizing weakref %s %s\n",
508 static_alias
? "as static alias" : "as transparent alias");
512 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
513 be sure it really clears the WEAK flag. */
514 TREE_PUBLIC (node
->decl
) = true;
515 node
->make_decl_local ();
516 node
->forced_by_abi
= false;
517 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
518 node
->externally_visible
= false;
519 gcc_assert (!DECL_WEAK (node
->decl
));
520 node
->transparent_alias
= false;
524 symtab
->change_decl_assembler_name
525 (node
->decl
, DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
526 node
->transparent_alias
= true;
527 node
->copy_visibility_from (target
);
529 gcc_assert (node
->alias
);
532 /* NODE is an externally visible definition, which we've discovered is
533 not needed externally. Make it local to this compilation. */
536 localize_node (bool whole_program
, symtab_node
*node
)
538 gcc_assert (whole_program
|| in_lto_p
|| !TREE_PUBLIC (node
->decl
));
540 /* It is possible that one comdat group contains both hidden and non-hidden
541 symbols. In this case we can privatize all hidden symbol but we need
542 to keep non-hidden exported. */
543 if (node
->same_comdat_group
544 && node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
547 for (next
= node
->same_comdat_group
;
548 next
!= node
; next
= next
->same_comdat_group
)
549 if (next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
550 || next
->resolution
== LDPR_PREVAILING_DEF
)
554 if (!node
->transparent_alias
)
556 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
557 node
->make_decl_local ();
558 if (!flag_incremental_link
)
559 node
->unique_name
|= true;
564 /* For similar reason do not privatize whole comdat when seeing comdat
565 local. Wait for non-comdat symbol to be privatized first. */
566 if (node
->comdat_local_p ())
569 if (node
->same_comdat_group
&& TREE_PUBLIC (node
->decl
))
571 for (symtab_node
*next
= node
->same_comdat_group
;
572 next
!= node
; next
= next
->same_comdat_group
)
574 next
->set_comdat_group (NULL
);
576 next
->set_section (NULL
);
577 if (!next
->transparent_alias
)
578 next
->make_decl_local ();
580 |= ((next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
581 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
582 && TREE_PUBLIC (next
->decl
)
583 && !flag_incremental_link
);
586 /* Now everything's localized, the grouping has no meaning, and
587 will cause crashes if we keep it around. */
588 node
->dissolve_same_comdat_group_list ();
592 |= ((node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
593 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
594 && TREE_PUBLIC (node
->decl
)
595 && !flag_incremental_link
);
597 if (TREE_PUBLIC (node
->decl
))
598 node
->set_comdat_group (NULL
);
599 if (DECL_COMDAT (node
->decl
) && !node
->alias
)
600 node
->set_section (NULL
);
601 if (!node
->transparent_alias
)
603 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
604 node
->make_decl_local ();
608 /* Decide on visibility of all symbols. */
611 function_and_variable_visibility (bool whole_program
)
613 struct cgraph_node
*node
;
616 /* All aliases should be procssed at this point. */
617 gcc_checking_assert (!alias_pairs
|| !alias_pairs
->length ());
619 FOR_EACH_FUNCTION (node
)
621 int flags
= flags_from_decl_or_type (node
->decl
);
623 /* Optimize away PURE and CONST constructors and destructors. */
625 && (flags
& (ECF_CONST
| ECF_PURE
))
626 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
628 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
629 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
632 /* Frontends and alias code marks nodes as needed before parsing
633 is finished. We may end up marking as node external nodes
634 where this flag is meaningless strip it. */
635 if (DECL_EXTERNAL (node
->decl
) || !node
->definition
)
637 node
->force_output
= 0;
638 node
->forced_by_abi
= 0;
641 /* C++ FE on lack of COMDAT support create local COMDAT functions
642 (that ought to be shared but can not due to object format
643 limitations). It is necessary to keep the flag to make rest of C++ FE
644 happy. Clear the flag here to avoid confusion in middle-end. */
645 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
646 DECL_COMDAT (node
->decl
) = 0;
648 /* For external decls stop tracking same_comdat_group. It doesn't matter
649 what comdat group they are in when they won't be emitted in this TU.
651 An exception is LTO where we may end up with both external
652 and non-external declarations in the same comdat group in
653 the case declarations was not merged. */
654 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
) && !in_lto_p
)
658 for (symtab_node
*n
= node
->same_comdat_group
;
660 n
= n
->same_comdat_group
)
661 /* If at least one of same comdat group functions is external,
662 all of them have to be, otherwise it is a front-end bug. */
663 gcc_assert (DECL_EXTERNAL (n
->decl
));
665 node
->dissolve_same_comdat_group_list ();
667 gcc_assert ((!DECL_WEAK (node
->decl
)
668 && !DECL_COMDAT (node
->decl
))
669 || TREE_PUBLIC (node
->decl
)
671 || DECL_EXTERNAL (node
->decl
));
672 if (cgraph_externally_visible_p (node
, whole_program
))
674 gcc_assert (!node
->global
.inlined_to
);
675 node
->externally_visible
= true;
679 node
->externally_visible
= false;
680 node
->forced_by_abi
= false;
682 if (!node
->externally_visible
683 && node
->definition
&& !node
->weakref
684 && !DECL_EXTERNAL (node
->decl
))
685 localize_node (whole_program
, node
);
687 if (node
->thunk
.thunk_p
688 && !node
->thunk
.add_pointer_bounds_args
689 && TREE_PUBLIC (node
->decl
))
691 struct cgraph_node
*decl_node
= node
;
693 decl_node
= decl_node
->callees
->callee
->function_symbol ();
695 /* Thunks have the same visibility as function they are attached to.
696 Make sure the C++ front end set this up properly. */
697 if (DECL_ONE_ONLY (decl_node
->decl
))
699 gcc_checking_assert (DECL_COMDAT (node
->decl
)
700 == DECL_COMDAT (decl_node
->decl
));
701 gcc_checking_assert (node
->in_same_comdat_group_p (decl_node
));
702 gcc_checking_assert (node
->same_comdat_group
);
704 node
->forced_by_abi
= decl_node
->forced_by_abi
;
705 if (DECL_EXTERNAL (decl_node
->decl
))
706 DECL_EXTERNAL (node
->decl
) = 1;
709 update_visibility_by_resolution_info (node
);
711 optimize_weakref (node
);
713 FOR_EACH_DEFINED_FUNCTION (node
)
715 if (!node
->local
.local
)
716 node
->local
.local
|= node
->local_p ();
718 /* If we know that function can not be overwritten by a
719 different semantics and moreover its section can not be
720 discarded, replace all direct calls by calls to an
721 noninterposable alias. This make dynamic linking cheaper and
722 enable more optimization.
724 TODO: We can also update virtual tables. */
726 && can_replace_by_local_alias (node
))
728 cgraph_node
*alias
= dyn_cast
<cgraph_node
*>
729 (node
->noninterposable_alias ());
731 if (alias
&& alias
!= node
)
733 while (node
->callers
)
735 struct cgraph_edge
*e
= node
->callers
;
737 e
->redirect_callee (alias
);
738 if (gimple_has_body_p (e
->caller
->decl
))
740 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
741 e
->redirect_call_stmt_to_callee ();
748 FOR_EACH_VARIABLE (vnode
)
750 /* weak flag makes no sense on local variables. */
751 gcc_assert (!DECL_WEAK (vnode
->decl
)
753 || TREE_PUBLIC (vnode
->decl
)
754 || DECL_EXTERNAL (vnode
->decl
));
755 /* In several cases declarations can not be common:
757 - when declaration has initializer
759 - when it has specific section
760 - when it resides in non-generic address space.
761 - if declaration is local, it will get into .local common section
762 so common flag is not needed. Frontends still produce these in
763 certain cases, such as for:
765 static int a __attribute__ ((common))
767 Canonicalize things here and clear the redundant flag. */
768 if (DECL_COMMON (vnode
->decl
)
769 && (!(TREE_PUBLIC (vnode
->decl
)
770 || DECL_EXTERNAL (vnode
->decl
))
771 || (DECL_INITIAL (vnode
->decl
)
772 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
773 || DECL_WEAK (vnode
->decl
)
774 || DECL_SECTION_NAME (vnode
->decl
) != NULL
775 || ! (ADDR_SPACE_GENERIC_P
776 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
777 DECL_COMMON (vnode
->decl
) = 0;
779 optimize_weakref (vnode
);
781 FOR_EACH_DEFINED_VARIABLE (vnode
)
783 if (!vnode
->definition
)
785 if (vnode
->externally_visible_p ())
786 vnode
->externally_visible
= true;
789 vnode
->externally_visible
= false;
790 vnode
->forced_by_abi
= false;
792 if (lookup_attribute ("no_reorder",
793 DECL_ATTRIBUTES (vnode
->decl
)))
794 vnode
->no_reorder
= 1;
796 if (!vnode
->externally_visible
797 && !vnode
->transparent_alias
798 && !DECL_EXTERNAL (vnode
->decl
))
799 localize_node (whole_program
, vnode
);
801 update_visibility_by_resolution_info (vnode
);
803 /* Update virtual tables to point to local aliases where possible. */
804 if (DECL_VIRTUAL_P (vnode
->decl
)
805 && !DECL_EXTERNAL (vnode
->decl
))
811 /* See if there is something to update. */
812 for (i
= 0; vnode
->iterate_reference (i
, ref
); i
++)
813 if (ref
->use
== IPA_REF_ADDR
814 && can_replace_by_local_alias_in_vtable (ref
->referred
))
821 hash_set
<tree
> visited_nodes
;
823 vnode
->get_constructor ();
824 walk_tree (&DECL_INITIAL (vnode
->decl
),
825 update_vtable_references
, NULL
, &visited_nodes
);
826 vnode
->remove_all_references ();
827 record_references_in_initializer (vnode
->decl
, false);
834 fprintf (dump_file
, "\nMarking local functions:");
835 FOR_EACH_DEFINED_FUNCTION (node
)
836 if (node
->local
.local
)
837 fprintf (dump_file
, " %s", node
->name ());
838 fprintf (dump_file
, "\n\n");
839 fprintf (dump_file
, "\nMarking externally visible functions:");
840 FOR_EACH_DEFINED_FUNCTION (node
)
841 if (node
->externally_visible
)
842 fprintf (dump_file
, " %s", node
->name ());
843 fprintf (dump_file
, "\n\n");
844 fprintf (dump_file
, "\nMarking externally visible variables:");
845 FOR_EACH_DEFINED_VARIABLE (vnode
)
846 if (vnode
->externally_visible
)
847 fprintf (dump_file
, " %s", vnode
->name ());
848 fprintf (dump_file
, "\n\n");
850 symtab
->function_flags_ready
= true;
854 /* Local function pass handling visibilities. This happens before LTO streaming
855 so in particular -fwhole-program should be ignored at this level. */
859 const pass_data pass_data_ipa_function_and_variable_visibility
=
861 SIMPLE_IPA_PASS
, /* type */
862 "visibility", /* name */
863 OPTGROUP_NONE
, /* optinfo_flags */
864 TV_CGRAPHOPT
, /* tv_id */
865 0, /* properties_required */
866 0, /* properties_provided */
867 0, /* properties_destroyed */
868 0, /* todo_flags_start */
869 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
872 /* Bring functions local at LTO time with -fwhole-program. */
875 whole_program_function_and_variable_visibility (void)
877 function_and_variable_visibility (flag_whole_program
);
879 ipa_discover_readonly_nonaddressable_vars ();
887 const pass_data pass_data_ipa_whole_program_visibility
=
890 "whole-program", /* name */
891 OPTGROUP_NONE
, /* optinfo_flags */
892 TV_CGRAPHOPT
, /* tv_id */
893 0, /* properties_required */
894 0, /* properties_provided */
895 0, /* properties_destroyed */
896 0, /* todo_flags_start */
897 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
900 class pass_ipa_whole_program_visibility
: public ipa_opt_pass_d
903 pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
904 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility
, ctxt
,
905 NULL
, /* generate_summary */
906 NULL
, /* write_summary */
907 NULL
, /* read_summary */
908 NULL
, /* write_optimization_summary */
909 NULL
, /* read_optimization_summary */
910 NULL
, /* stmt_fixup */
911 0, /* function_transform_todo_flags_start */
912 NULL
, /* function_transform */
913 NULL
) /* variable_transform */
916 /* opt_pass methods: */
918 virtual bool gate (function
*)
920 /* Do not re-run on ltrans stage. */
923 virtual unsigned int execute (function
*)
925 return whole_program_function_and_variable_visibility ();
928 }; // class pass_ipa_whole_program_visibility
933 make_pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
935 return new pass_ipa_whole_program_visibility (ctxt
);
938 class pass_ipa_function_and_variable_visibility
: public simple_ipa_opt_pass
941 pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
942 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility
,
946 /* opt_pass methods: */
947 virtual unsigned int execute (function
*)
949 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
952 }; // class pass_ipa_function_and_variable_visibility
954 simple_ipa_opt_pass
*
955 make_pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
957 return new pass_ipa_function_and_variable_visibility (ctxt
);