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"
86 #include "ipa-utils.h"
88 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
91 non_local_p (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
93 return !(node
->only_called_directly_or_aliased_p ()
94 /* i386 would need update to output thunk with local calling
96 && !node
->thunk
.thunk_p
98 && !DECL_EXTERNAL (node
->decl
)
99 && !node
->externally_visible
100 && !node
->used_from_other_partition
101 && !node
->in_other_partition
102 && node
->get_availability () >= AVAIL_AVAILABLE
);
105 /* Return true when function can be marked local. */
108 cgraph_node::local_p (void)
110 cgraph_node
*n
= ultimate_alias_target ();
112 if (n
->thunk
.thunk_p
)
113 return n
->callees
->callee
->local_p ();
114 return !n
->call_for_symbol_thunks_and_aliases (non_local_p
,
119 /* A helper for comdat_can_be_unshared_p. */
122 comdat_can_be_unshared_p_1 (symtab_node
*node
)
124 if (!node
->externally_visible
)
126 if (node
->address_can_be_compared_p ())
130 for (unsigned int i
= 0; node
->iterate_referring (i
, ref
); i
++)
131 if (ref
->address_matters_p ())
135 /* If the symbol is used in some weird way, better to not touch it. */
136 if (node
->force_output
)
139 /* Explicit instantiations needs to be output when possibly
141 if (node
->forced_by_abi
142 && TREE_PUBLIC (node
->decl
)
143 && (node
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
144 && !flag_whole_program
))
147 /* Non-readonly and volatile variables can not be duplicated. */
148 if (is_a
<varpool_node
*> (node
)
149 && (!TREE_READONLY (node
->decl
)
150 || TREE_THIS_VOLATILE (node
->decl
)))
155 /* COMDAT functions must be shared only if they have address taken,
156 otherwise we can produce our own private implementation with
158 Return true when turning COMDAT function static can not lead to wrong
159 code when the resulting object links with a library defining same COMDAT.
161 Virtual functions do have their addresses taken from the vtables,
162 but in C++ there is no way to compare their addresses for equality. */
165 comdat_can_be_unshared_p (symtab_node
*node
)
167 if (!comdat_can_be_unshared_p_1 (node
))
169 if (node
->same_comdat_group
)
173 /* If more than one function is in the same COMDAT group, it must
174 be shared even if just one function in the comdat group has
176 for (next
= node
->same_comdat_group
;
177 next
!= node
; next
= next
->same_comdat_group
)
178 if (!comdat_can_be_unshared_p_1 (next
))
184 /* Return true when function NODE should be considered externally visible. */
187 cgraph_externally_visible_p (struct cgraph_node
*node
,
190 while (node
->transparent_alias
&& node
->definition
)
191 node
= node
->get_alias_target ();
192 if (!node
->definition
)
194 if (!TREE_PUBLIC (node
->decl
)
195 || DECL_EXTERNAL (node
->decl
))
198 /* Do not try to localize built-in functions yet. One of problems is that we
199 end up mangling their asm for WHOPR that makes it impossible to call them
200 using the implicit built-in declarations anymore. Similarly this enables
201 us to remove them as unreachable before actual calls may appear during
202 expansion or folding. */
203 if (DECL_BUILT_IN (node
->decl
))
206 /* If linker counts on us, we must preserve the function. */
207 if (node
->used_from_object_file_p ())
209 if (DECL_PRESERVE_P (node
->decl
))
211 if (lookup_attribute ("externally_visible",
212 DECL_ATTRIBUTES (node
->decl
)))
214 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
215 && lookup_attribute ("dllexport",
216 DECL_ATTRIBUTES (node
->decl
)))
218 if (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
220 /* When doing LTO or whole program, we can bring COMDAT functoins static.
221 This improves code quality and we know we will duplicate them at most twice
222 (in the case that we are not using plugin and link with object file
223 implementing same COMDAT) */
224 if (((in_lto_p
|| whole_program
) && !flag_incremental_link
)
225 && DECL_COMDAT (node
->decl
)
226 && comdat_can_be_unshared_p (node
))
229 /* When doing link time optimizations, hidden symbols become local. */
230 if ((in_lto_p
&& !flag_incremental_link
)
231 && (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
232 || DECL_VISIBILITY (node
->decl
) == VISIBILITY_INTERNAL
)
233 /* Be sure that node is defined in IR file, not in other object
234 file. In that case we don't set used_from_other_object_file. */
237 else if (!whole_program
)
240 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
243 if (node
->instrumentation_clone
244 && MAIN_NAME_P (DECL_NAME (node
->orig_decl
)))
250 /* Return true when variable should be considered externally visible. */
253 varpool_node::externally_visible_p (void)
255 while (transparent_alias
&& definition
)
256 return get_alias_target ()->externally_visible_p ();
257 if (DECL_EXTERNAL (decl
))
260 if (!TREE_PUBLIC (decl
))
263 /* If linker counts on us, we must preserve the function. */
264 if (used_from_object_file_p ())
267 /* Bringing TLS variables local may cause dynamic linker failures
268 on limits of static TLS vars. */
269 if (DECL_THREAD_LOCAL_P (decl
)
270 && (DECL_TLS_MODEL (decl
) != TLS_MODEL_EMULATED
271 && DECL_TLS_MODEL (decl
) != TLS_MODEL_INITIAL_EXEC
))
274 if (DECL_HARD_REGISTER (decl
))
276 if (DECL_PRESERVE_P (decl
))
278 if (lookup_attribute ("externally_visible",
279 DECL_ATTRIBUTES (decl
)))
281 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
282 && lookup_attribute ("dllexport",
283 DECL_ATTRIBUTES (decl
)))
286 /* See if we have linker information about symbol not being used or
287 if we need to make guess based on the declaration.
289 Even if the linker clams the symbol is unused, never bring internal
290 symbols that are declared by user as used or externally visible.
291 This is needed for i.e. references from asm statements. */
292 if (used_from_object_file_p ())
294 if (resolution
== LDPR_PREVAILING_DEF_IRONLY
)
297 /* As a special case, the COMDAT virtual tables can be unshared.
298 In LTO mode turn vtables into static variables. The variable is readonly,
299 so this does not enable more optimization, but referring static var
300 is faster for dynamic linking. Also this match logic hidding vtables
301 from LTO symbol tables. */
302 if (((in_lto_p
|| flag_whole_program
) && !flag_incremental_link
)
303 && DECL_COMDAT (decl
)
304 && comdat_can_be_unshared_p (this))
307 /* When doing link time optimizations, hidden symbols become local. */
308 if (in_lto_p
&& !flag_incremental_link
309 && (DECL_VISIBILITY (decl
) == VISIBILITY_HIDDEN
310 || DECL_VISIBILITY (decl
) == VISIBILITY_INTERNAL
)
311 /* Be sure that node is defined in IR file, not in other object
312 file. In that case we don't set used_from_other_object_file. */
315 else if (!flag_whole_program
)
318 /* Do not attempt to privatize COMDATS by default.
319 This would break linking with C++ libraries sharing
322 FIXME: We can do so for readonly vars with no address taken and
323 possibly also for vtables since no direct pointer comparsion is done.
324 It might be interesting to do so to reduce linking overhead. */
325 if (DECL_COMDAT (decl
) || DECL_WEAK (decl
))
330 /* Return true if reference to NODE can be replaced by a local alias.
331 Local aliases save dynamic linking overhead and enable more optimizations.
335 can_replace_by_local_alias (symtab_node
*node
)
337 #ifndef ASM_OUTPUT_DEF
338 /* If aliases aren't supported, we can't do replacement. */
341 /* Weakrefs have a reason to be non-local. Be sure we do not replace
343 while (node
->transparent_alias
&& node
->definition
&& !node
->weakref
)
344 node
= node
->get_alias_target ();
348 return (node
->get_availability () > AVAIL_INTERPOSABLE
349 && !decl_binds_to_current_def_p (node
->decl
)
350 && !node
->can_be_discarded_p ());
353 /* Return true if we can replace reference to NODE by local alias
354 within a virtual table. Generally we can replace function pointers
355 and virtual table pointers. */
358 can_replace_by_local_alias_in_vtable (symtab_node
*node
)
360 if (is_a
<varpool_node
*> (node
)
361 && !DECL_VIRTUAL_P (node
->decl
))
363 return can_replace_by_local_alias (node
);
366 /* walk_tree callback that rewrites initializer references. */
369 update_vtable_references (tree
*tp
, int *walk_subtrees
,
370 void *data ATTRIBUTE_UNUSED
)
372 if (VAR_OR_FUNCTION_DECL_P (*tp
))
374 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp
)))
375 *tp
= symtab_node::get (*tp
)->noninterposable_alias ()->decl
;
378 else if (IS_TYPE_OR_DECL_P (*tp
))
383 /* In LTO we can remove COMDAT groups and weak symbols.
384 Either turn them into normal symbols or external symbol depending on
388 update_visibility_by_resolution_info (symtab_node
* node
)
392 if (!node
->externally_visible
393 || (!DECL_WEAK (node
->decl
) && !DECL_ONE_ONLY (node
->decl
))
394 || node
->resolution
== LDPR_UNKNOWN
)
397 define
= (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
398 || node
->resolution
== LDPR_PREVAILING_DEF
399 || node
->resolution
== LDPR_UNDEF
400 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
402 /* The linker decisions ought to agree in the whole group. */
403 if (node
->same_comdat_group
)
404 for (symtab_node
*next
= node
->same_comdat_group
;
405 next
!= node
; next
= next
->same_comdat_group
)
407 if (!next
->externally_visible
|| next
->transparent_alias
)
411 = define
== (next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
412 || next
->resolution
== LDPR_PREVAILING_DEF
413 || next
->resolution
== LDPR_UNDEF
414 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
415 gcc_assert (in_lto_p
|| same_def
);
420 if (node
->same_comdat_group
)
421 for (symtab_node
*next
= node
->same_comdat_group
;
422 next
!= node
; next
= next
->same_comdat_group
)
424 /* During incremental linking we need to keep symbol weak for future
425 linking. We can still drop definition if we know non-LTO world
427 if (!flag_incremental_link
)
429 DECL_WEAK (next
->decl
) = false;
430 next
->set_comdat_group (NULL
);
434 if (next
->externally_visible
)
435 DECL_EXTERNAL (next
->decl
) = true;
436 next
->set_comdat_group (NULL
);
440 /* During incremental linking we need to keep symbol weak for future
441 linking. We can still drop definition if we know non-LTO world prevails. */
442 if (!flag_incremental_link
)
444 DECL_WEAK (node
->decl
) = false;
445 node
->set_comdat_group (NULL
);
446 node
->dissolve_same_comdat_group_list ();
450 DECL_EXTERNAL (node
->decl
) = true;
451 node
->set_comdat_group (NULL
);
452 node
->dissolve_same_comdat_group_list ();
456 /* Try to get rid of weakref. */
459 optimize_weakref (symtab_node
*node
)
461 #ifdef ASM_OUTPUT_DEF
462 bool aliases_supported
= true;
464 bool aliases_supported
= false;
466 bool strip_weakref
= false;
467 bool static_alias
= false;
469 gcc_assert (node
->weakref
);
471 /* Weakrefs with no target defined can not be optimized. */
474 symtab_node
*target
= node
->get_alias_target ();
476 /* Weakrefs to weakrefs can be optimized only if target can be. */
478 optimize_weakref (target
);
482 /* If we have definition of weakref's target and we know it binds locally,
483 we can turn weakref to static alias. */
484 if (target
->definition
&& decl_binds_to_current_def_p (target
->decl
)
485 && aliases_supported
)
486 strip_weakref
= static_alias
= true;
487 /* Otherwise we can turn weakref into transparent alias. This transformation
488 may break asm statements which directly refers to symbol name and expect
489 GNU as to translate it via .weakref directive. So do not optimize when
490 DECL_PRESERVED is set and .weakref is supported. */
491 else if ((!DECL_PRESERVE_P (target
->decl
)
492 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node
->decl
)))
493 && !DECL_WEAK (target
->decl
)
494 && !DECL_EXTERNAL (target
->decl
)
495 && ((target
->definition
&& !target
->can_be_discarded_p ())
496 || target
->resolution
!= LDPR_UNDEF
))
497 strip_weakref
= true;
500 node
->weakref
= false;
501 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node
->decl
)) = 0;
502 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
)) = NULL_TREE
;
503 DECL_ATTRIBUTES (node
->decl
) = remove_attribute ("weakref",
508 fprintf (dump_file
, "Optimizing weakref %s %s\n",
510 static_alias
? "as static alias" : "as transparent alias");
514 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
515 be sure it really clears the WEAK flag. */
516 TREE_PUBLIC (node
->decl
) = true;
517 node
->make_decl_local ();
518 node
->forced_by_abi
= false;
519 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
520 node
->externally_visible
= false;
521 gcc_assert (!DECL_WEAK (node
->decl
));
522 node
->transparent_alias
= false;
526 symtab
->change_decl_assembler_name
527 (node
->decl
, DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
528 node
->transparent_alias
= true;
529 node
->copy_visibility_from (target
);
531 gcc_assert (node
->alias
);
534 /* NODE is an externally visible definition, which we've discovered is
535 not needed externally. Make it local to this compilation. */
538 localize_node (bool whole_program
, symtab_node
*node
)
540 gcc_assert (whole_program
|| in_lto_p
|| !TREE_PUBLIC (node
->decl
));
542 /* It is possible that one comdat group contains both hidden and non-hidden
543 symbols. In this case we can privatize all hidden symbol but we need
544 to keep non-hidden exported. */
545 if (node
->same_comdat_group
546 && node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
549 for (next
= node
->same_comdat_group
;
550 next
!= node
; next
= next
->same_comdat_group
)
551 if (next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
552 || next
->resolution
== LDPR_PREVAILING_DEF
)
556 if (!node
->transparent_alias
)
558 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
559 node
->make_decl_local ();
560 if (!flag_incremental_link
)
561 node
->unique_name
|= true;
566 /* For similar reason do not privatize whole comdat when seeing comdat
567 local. Wait for non-comdat symbol to be privatized first. */
568 if (node
->comdat_local_p ())
571 if (node
->same_comdat_group
&& TREE_PUBLIC (node
->decl
))
573 for (symtab_node
*next
= node
->same_comdat_group
;
574 next
!= node
; next
= next
->same_comdat_group
)
576 next
->set_comdat_group (NULL
);
578 next
->set_section (NULL
);
579 if (!next
->transparent_alias
)
580 next
->make_decl_local ();
582 |= ((next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
583 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
584 && TREE_PUBLIC (next
->decl
)
585 && !flag_incremental_link
);
588 /* Now everything's localized, the grouping has no meaning, and
589 will cause crashes if we keep it around. */
590 node
->dissolve_same_comdat_group_list ();
594 |= ((node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
595 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
596 && TREE_PUBLIC (node
->decl
)
597 && !flag_incremental_link
);
599 if (TREE_PUBLIC (node
->decl
))
600 node
->set_comdat_group (NULL
);
601 if (DECL_COMDAT (node
->decl
) && !node
->alias
)
602 node
->set_section (NULL
);
603 if (!node
->transparent_alias
)
605 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
606 node
->make_decl_local ();
610 /* Decide on visibility of all symbols. */
613 function_and_variable_visibility (bool whole_program
)
615 struct cgraph_node
*node
;
618 /* All aliases should be processed at this point. */
619 gcc_checking_assert (!alias_pairs
|| !alias_pairs
->length ());
621 #ifdef ASM_OUTPUT_DEF
622 FOR_EACH_DEFINED_FUNCTION (node
)
624 if (node
->get_availability () != AVAIL_INTERPOSABLE
625 || DECL_EXTERNAL (node
->decl
)
626 || node
->has_aliases_p ())
629 cgraph_node
*alias
= 0;
630 for (cgraph_edge
*e
= node
->callees
; e
; e
= e
->next_callee
)
632 /* Recursive function calls usually can't be interposed. */
634 if (!e
->recursive_p ())
639 alias
= dyn_cast
<cgraph_node
*> (node
->noninterposable_alias ());
640 gcc_assert (alias
&& alias
!= node
);
643 e
->redirect_callee (alias
);
644 if (gimple_has_body_p (e
->caller
->decl
))
646 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
647 e
->redirect_call_stmt_to_callee ();
654 FOR_EACH_FUNCTION (node
)
656 int flags
= flags_from_decl_or_type (node
->decl
);
658 /* Optimize away PURE and CONST constructors and destructors. */
660 && (DECL_STATIC_CONSTRUCTOR (node
->decl
)
661 || DECL_STATIC_DESTRUCTOR (node
->decl
))
662 && (flags
& (ECF_CONST
| ECF_PURE
))
663 && !(flags
& ECF_LOOPING_CONST_OR_PURE
)
664 && opt_for_fn (node
->decl
, optimize
))
666 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
667 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
670 /* Frontends and alias code marks nodes as needed before parsing
671 is finished. We may end up marking as node external nodes
672 where this flag is meaningless strip it. */
673 if (DECL_EXTERNAL (node
->decl
) || !node
->definition
)
675 node
->force_output
= 0;
676 node
->forced_by_abi
= 0;
679 /* C++ FE on lack of COMDAT support create local COMDAT functions
680 (that ought to be shared but can not due to object format
681 limitations). It is necessary to keep the flag to make rest of C++ FE
682 happy. Clear the flag here to avoid confusion in middle-end. */
683 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
684 DECL_COMDAT (node
->decl
) = 0;
686 /* For external decls stop tracking same_comdat_group. It doesn't matter
687 what comdat group they are in when they won't be emitted in this TU.
689 An exception is LTO where we may end up with both external
690 and non-external declarations in the same comdat group in
691 the case declarations was not merged. */
692 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
) && !in_lto_p
)
696 for (symtab_node
*n
= node
->same_comdat_group
;
698 n
= n
->same_comdat_group
)
699 /* If at least one of same comdat group functions is external,
700 all of them have to be, otherwise it is a front-end bug. */
701 gcc_assert (DECL_EXTERNAL (n
->decl
));
703 node
->dissolve_same_comdat_group_list ();
705 gcc_assert ((!DECL_WEAK (node
->decl
)
706 && !DECL_COMDAT (node
->decl
))
707 || TREE_PUBLIC (node
->decl
)
709 || DECL_EXTERNAL (node
->decl
));
710 if (cgraph_externally_visible_p (node
, whole_program
))
712 gcc_assert (!node
->global
.inlined_to
);
713 node
->externally_visible
= true;
717 node
->externally_visible
= false;
718 node
->forced_by_abi
= false;
720 if (!node
->externally_visible
721 && node
->definition
&& !node
->weakref
722 && !DECL_EXTERNAL (node
->decl
))
723 localize_node (whole_program
, node
);
725 if (node
->thunk
.thunk_p
726 && !node
->thunk
.add_pointer_bounds_args
727 && TREE_PUBLIC (node
->decl
))
729 struct cgraph_node
*decl_node
= node
;
731 decl_node
= decl_node
->callees
->callee
->function_symbol ();
733 /* Thunks have the same visibility as function they are attached to.
734 Make sure the C++ front end set this up properly. */
735 if (DECL_ONE_ONLY (decl_node
->decl
))
737 gcc_checking_assert (DECL_COMDAT (node
->decl
)
738 == DECL_COMDAT (decl_node
->decl
));
739 gcc_checking_assert (node
->in_same_comdat_group_p (decl_node
));
740 gcc_checking_assert (node
->same_comdat_group
);
742 node
->forced_by_abi
= decl_node
->forced_by_abi
;
743 if (DECL_EXTERNAL (decl_node
->decl
))
744 DECL_EXTERNAL (node
->decl
) = 1;
747 update_visibility_by_resolution_info (node
);
749 optimize_weakref (node
);
751 FOR_EACH_DEFINED_FUNCTION (node
)
753 if (!node
->local
.local
)
754 node
->local
.local
|= node
->local_p ();
756 /* If we know that function can not be overwritten by a
757 different semantics and moreover its section can not be
758 discarded, replace all direct calls by calls to an
759 noninterposable alias. This make dynamic linking cheaper and
760 enable more optimization.
762 TODO: We can also update virtual tables. */
764 && can_replace_by_local_alias (node
))
766 cgraph_node
*alias
= dyn_cast
<cgraph_node
*>
767 (node
->noninterposable_alias ());
769 if (alias
&& alias
!= node
)
771 while (node
->callers
)
773 struct cgraph_edge
*e
= node
->callers
;
775 e
->redirect_callee (alias
);
776 if (gimple_has_body_p (e
->caller
->decl
))
778 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
779 e
->redirect_call_stmt_to_callee ();
786 FOR_EACH_VARIABLE (vnode
)
788 /* weak flag makes no sense on local variables. */
789 gcc_assert (!DECL_WEAK (vnode
->decl
)
791 || TREE_PUBLIC (vnode
->decl
)
792 || DECL_EXTERNAL (vnode
->decl
));
793 /* In several cases declarations can not be common:
795 - when declaration has initializer
797 - when it has specific section
798 - when it resides in non-generic address space.
799 - if declaration is local, it will get into .local common section
800 so common flag is not needed. Frontends still produce these in
801 certain cases, such as for:
803 static int a __attribute__ ((common))
805 Canonicalize things here and clear the redundant flag. */
806 if (DECL_COMMON (vnode
->decl
)
807 && (!(TREE_PUBLIC (vnode
->decl
)
808 || DECL_EXTERNAL (vnode
->decl
))
809 || (DECL_INITIAL (vnode
->decl
)
810 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
811 || DECL_WEAK (vnode
->decl
)
812 || DECL_SECTION_NAME (vnode
->decl
) != NULL
813 || ! (ADDR_SPACE_GENERIC_P
814 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
815 DECL_COMMON (vnode
->decl
) = 0;
817 optimize_weakref (vnode
);
819 FOR_EACH_DEFINED_VARIABLE (vnode
)
821 if (!vnode
->definition
)
823 if (vnode
->externally_visible_p ())
824 vnode
->externally_visible
= true;
827 vnode
->externally_visible
= false;
828 vnode
->forced_by_abi
= false;
830 if (lookup_attribute ("no_reorder",
831 DECL_ATTRIBUTES (vnode
->decl
)))
832 vnode
->no_reorder
= 1;
834 if (!vnode
->externally_visible
835 && !vnode
->transparent_alias
836 && !DECL_EXTERNAL (vnode
->decl
))
837 localize_node (whole_program
, vnode
);
839 update_visibility_by_resolution_info (vnode
);
841 /* Update virtual tables to point to local aliases where possible. */
842 if (DECL_VIRTUAL_P (vnode
->decl
)
843 && !DECL_EXTERNAL (vnode
->decl
))
849 /* See if there is something to update. */
850 for (i
= 0; vnode
->iterate_reference (i
, ref
); i
++)
851 if (ref
->use
== IPA_REF_ADDR
852 && can_replace_by_local_alias_in_vtable (ref
->referred
))
859 hash_set
<tree
> visited_nodes
;
861 vnode
->get_constructor ();
862 walk_tree (&DECL_INITIAL (vnode
->decl
),
863 update_vtable_references
, NULL
, &visited_nodes
);
864 vnode
->remove_all_references ();
865 record_references_in_initializer (vnode
->decl
, false);
872 fprintf (dump_file
, "\nMarking local functions:");
873 FOR_EACH_DEFINED_FUNCTION (node
)
874 if (node
->local
.local
)
875 fprintf (dump_file
, " %s", node
->name ());
876 fprintf (dump_file
, "\n\n");
877 fprintf (dump_file
, "\nMarking externally visible functions:");
878 FOR_EACH_DEFINED_FUNCTION (node
)
879 if (node
->externally_visible
)
880 fprintf (dump_file
, " %s", node
->name ());
881 fprintf (dump_file
, "\n\n");
882 fprintf (dump_file
, "\nMarking externally visible variables:");
883 FOR_EACH_DEFINED_VARIABLE (vnode
)
884 if (vnode
->externally_visible
)
885 fprintf (dump_file
, " %s", vnode
->name ());
886 fprintf (dump_file
, "\n\n");
888 symtab
->function_flags_ready
= true;
892 /* Local function pass handling visibilities. This happens before LTO streaming
893 so in particular -fwhole-program should be ignored at this level. */
897 const pass_data pass_data_ipa_function_and_variable_visibility
=
899 SIMPLE_IPA_PASS
, /* type */
900 "visibility", /* name */
901 OPTGROUP_NONE
, /* optinfo_flags */
902 TV_CGRAPHOPT
, /* tv_id */
903 0, /* properties_required */
904 0, /* properties_provided */
905 0, /* properties_destroyed */
906 0, /* todo_flags_start */
907 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
910 /* Bring functions local at LTO time with -fwhole-program. */
913 whole_program_function_and_variable_visibility (void)
915 function_and_variable_visibility (flag_whole_program
);
916 if (optimize
|| in_lto_p
)
917 ipa_discover_readonly_nonaddressable_vars ();
925 const pass_data pass_data_ipa_whole_program_visibility
=
928 "whole-program", /* name */
929 OPTGROUP_NONE
, /* optinfo_flags */
930 TV_CGRAPHOPT
, /* tv_id */
931 0, /* properties_required */
932 0, /* properties_provided */
933 0, /* properties_destroyed */
934 0, /* todo_flags_start */
935 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
938 class pass_ipa_whole_program_visibility
: public ipa_opt_pass_d
941 pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
942 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility
, ctxt
,
943 NULL
, /* generate_summary */
944 NULL
, /* write_summary */
945 NULL
, /* read_summary */
946 NULL
, /* write_optimization_summary */
947 NULL
, /* read_optimization_summary */
948 NULL
, /* stmt_fixup */
949 0, /* function_transform_todo_flags_start */
950 NULL
, /* function_transform */
951 NULL
) /* variable_transform */
954 /* opt_pass methods: */
956 virtual bool gate (function
*)
958 /* Do not re-run on ltrans stage. */
961 virtual unsigned int execute (function
*)
963 return whole_program_function_and_variable_visibility ();
966 }; // class pass_ipa_whole_program_visibility
971 make_pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
973 return new pass_ipa_whole_program_visibility (ctxt
);
976 class pass_ipa_function_and_variable_visibility
: public simple_ipa_opt_pass
979 pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
980 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility
,
984 /* opt_pass methods: */
985 virtual unsigned int execute (function
*)
987 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
990 }; // class pass_ipa_function_and_variable_visibility
992 simple_ipa_opt_pass
*
993 make_pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
995 return new pass_ipa_function_and_variable_visibility (ctxt
);