PR target/81369
[official-gcc.git] / gcc / ipa-visibility.c
blob17186e9d2e6156df07108ed3df6c5cced555b113
1 /* IPA visibility pass
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
9 version.
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
14 for more details.
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
36 or by other reasons)
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
47 group.
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.
59 - Local aliases:
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
66 calls to it.
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. */
75 #include "config.h"
76 #include "system.h"
77 #include "coretypes.h"
78 #include "tm.h"
79 #include "function.h"
80 #include "tree.h"
81 #include "gimple-expr.h"
82 #include "tree-pass.h"
83 #include "cgraph.h"
84 #include "calls.h"
85 #include "varasm.h"
87 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
89 static bool
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
94 convetions. */
95 && !node->thunk.thunk_p
96 && node->definition
97 && !DECL_EXTERNAL (node->decl)
98 && !node->externally_visible
99 && !node->used_from_other_partition
100 && !node->in_other_partition
101 && node->get_availability () >= AVAIL_AVAILABLE);
104 /* Return true when function can be marked local. */
106 bool
107 cgraph_node::local_p (void)
109 cgraph_node *n = ultimate_alias_target ();
111 if (n->thunk.thunk_p)
112 return n->callees->callee->local_p ();
113 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
114 NULL, true);
118 /* A helper for comdat_can_be_unshared_p. */
120 static bool
121 comdat_can_be_unshared_p_1 (symtab_node *node)
123 if (!node->externally_visible)
124 return true;
125 if (node->address_can_be_compared_p ())
127 struct ipa_ref *ref;
129 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
130 if (ref->address_matters_p ())
131 return false;
134 /* If the symbol is used in some weird way, better to not touch it. */
135 if (node->force_output)
136 return false;
138 /* Explicit instantiations needs to be output when possibly
139 used externally. */
140 if (node->forced_by_abi
141 && TREE_PUBLIC (node->decl)
142 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
143 && !flag_whole_program))
144 return false;
146 /* Non-readonly and volatile variables can not be duplicated. */
147 if (is_a <varpool_node *> (node)
148 && (!TREE_READONLY (node->decl)
149 || TREE_THIS_VOLATILE (node->decl)))
150 return false;
151 return true;
154 /* COMDAT functions must be shared only if they have address taken,
155 otherwise we can produce our own private implementation with
156 -fwhole-program.
157 Return true when turning COMDAT function static can not lead to wrong
158 code when the resulting object links with a library defining same COMDAT.
160 Virtual functions do have their addresses taken from the vtables,
161 but in C++ there is no way to compare their addresses for equality. */
163 static bool
164 comdat_can_be_unshared_p (symtab_node *node)
166 if (!comdat_can_be_unshared_p_1 (node))
167 return false;
168 if (node->same_comdat_group)
170 symtab_node *next;
172 /* If more than one function is in the same COMDAT group, it must
173 be shared even if just one function in the comdat group has
174 address taken. */
175 for (next = node->same_comdat_group;
176 next != node; next = next->same_comdat_group)
177 if (!comdat_can_be_unshared_p_1 (next))
178 return false;
180 return true;
183 /* Return true when function NODE should be considered externally visible. */
185 static bool
186 cgraph_externally_visible_p (struct cgraph_node *node,
187 bool whole_program)
189 while (node->transparent_alias && node->definition)
190 node = node->get_alias_target ();
191 if (!node->definition)
192 return false;
193 if (!TREE_PUBLIC (node->decl)
194 || DECL_EXTERNAL (node->decl))
195 return false;
197 /* Do not try to localize built-in functions yet. One of problems is that we
198 end up mangling their asm for WHOPR that makes it impossible to call them
199 using the implicit built-in declarations anymore. Similarly this enables
200 us to remove them as unreachable before actual calls may appear during
201 expansion or folding. */
202 if (DECL_BUILT_IN (node->decl))
203 return true;
205 /* If linker counts on us, we must preserve the function. */
206 if (node->used_from_object_file_p ())
207 return true;
208 if (DECL_PRESERVE_P (node->decl))
209 return true;
210 if (lookup_attribute ("externally_visible",
211 DECL_ATTRIBUTES (node->decl)))
212 return true;
213 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
214 && lookup_attribute ("dllexport",
215 DECL_ATTRIBUTES (node->decl)))
216 return true;
217 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
218 return false;
219 /* When doing LTO or whole program, we can bring COMDAT functoins static.
220 This improves code quality and we know we will duplicate them at most twice
221 (in the case that we are not using plugin and link with object file
222 implementing same COMDAT) */
223 if (((in_lto_p || whole_program) && !flag_incremental_link)
224 && DECL_COMDAT (node->decl)
225 && comdat_can_be_unshared_p (node))
226 return false;
228 /* When doing link time optimizations, hidden symbols become local. */
229 if ((in_lto_p && !flag_incremental_link)
230 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
231 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
232 /* Be sure that node is defined in IR file, not in other object
233 file. In that case we don't set used_from_other_object_file. */
234 && node->definition)
236 else if (!whole_program)
237 return true;
239 if (MAIN_NAME_P (DECL_NAME (node->decl)))
240 return true;
242 if (node->instrumentation_clone
243 && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
244 return true;
246 return false;
249 /* Return true when variable should be considered externally visible. */
251 bool
252 varpool_node::externally_visible_p (void)
254 while (transparent_alias && definition)
255 return get_alias_target ()->externally_visible_p ();
256 if (DECL_EXTERNAL (decl))
257 return true;
259 if (!TREE_PUBLIC (decl))
260 return false;
262 /* If linker counts on us, we must preserve the function. */
263 if (used_from_object_file_p ())
264 return true;
266 /* Bringing TLS variables local may cause dynamic linker failures
267 on limits of static TLS vars. */
268 if (DECL_THREAD_LOCAL_P (decl)
269 && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
270 && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
271 return true;
273 if (DECL_HARD_REGISTER (decl))
274 return true;
275 if (DECL_PRESERVE_P (decl))
276 return true;
277 if (lookup_attribute ("externally_visible",
278 DECL_ATTRIBUTES (decl)))
279 return true;
280 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
281 && lookup_attribute ("dllexport",
282 DECL_ATTRIBUTES (decl)))
283 return true;
285 /* See if we have linker information about symbol not being used or
286 if we need to make guess based on the declaration.
288 Even if the linker clams the symbol is unused, never bring internal
289 symbols that are declared by user as used or externally visible.
290 This is needed for i.e. references from asm statements. */
291 if (used_from_object_file_p ())
292 return true;
293 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
294 return false;
296 /* As a special case, the COMDAT virtual tables can be unshared.
297 In LTO mode turn vtables into static variables. The variable is readonly,
298 so this does not enable more optimization, but referring static var
299 is faster for dynamic linking. Also this match logic hidding vtables
300 from LTO symbol tables. */
301 if (((in_lto_p || flag_whole_program) && !flag_incremental_link)
302 && DECL_COMDAT (decl)
303 && comdat_can_be_unshared_p (this))
304 return false;
306 /* When doing link time optimizations, hidden symbols become local. */
307 if (in_lto_p && !flag_incremental_link
308 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
309 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
310 /* Be sure that node is defined in IR file, not in other object
311 file. In that case we don't set used_from_other_object_file. */
312 && definition)
314 else if (!flag_whole_program)
315 return true;
317 /* Do not attempt to privatize COMDATS by default.
318 This would break linking with C++ libraries sharing
319 inline definitions.
321 FIXME: We can do so for readonly vars with no address taken and
322 possibly also for vtables since no direct pointer comparsion is done.
323 It might be interesting to do so to reduce linking overhead. */
324 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
325 return true;
326 return false;
329 /* Return true if reference to NODE can be replaced by a local alias.
330 Local aliases save dynamic linking overhead and enable more optimizations.
333 static bool
334 can_replace_by_local_alias (symtab_node *node)
336 #ifndef ASM_OUTPUT_DEF
337 /* If aliases aren't supported, we can't do replacement. */
338 return false;
339 #endif
340 /* Weakrefs have a reason to be non-local. Be sure we do not replace
341 them. */
342 while (node->transparent_alias && node->definition && !node->weakref)
343 node = node->get_alias_target ();
344 if (node->weakref)
345 return false;
347 return (node->get_availability () > AVAIL_INTERPOSABLE
348 && !decl_binds_to_current_def_p (node->decl)
349 && !node->can_be_discarded_p ());
352 /* Return true if we can replace reference to NODE by local alias
353 within a virtual table. Generally we can replace function pointers
354 and virtual table pointers. */
356 static bool
357 can_replace_by_local_alias_in_vtable (symtab_node *node)
359 if (is_a <varpool_node *> (node)
360 && !DECL_VIRTUAL_P (node->decl))
361 return false;
362 return can_replace_by_local_alias (node);
365 /* walk_tree callback that rewrites initializer references. */
367 static tree
368 update_vtable_references (tree *tp, int *walk_subtrees,
369 void *data ATTRIBUTE_UNUSED)
371 if (VAR_OR_FUNCTION_DECL_P (*tp))
373 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
374 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
375 *walk_subtrees = 0;
377 else if (IS_TYPE_OR_DECL_P (*tp))
378 *walk_subtrees = 0;
379 return NULL;
382 /* In LTO we can remove COMDAT groups and weak symbols.
383 Either turn them into normal symbols or external symbol depending on
384 resolution info. */
386 static void
387 update_visibility_by_resolution_info (symtab_node * node)
389 bool define;
391 if (!node->externally_visible
392 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
393 || node->resolution == LDPR_UNKNOWN)
394 return;
396 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
397 || node->resolution == LDPR_PREVAILING_DEF
398 || node->resolution == LDPR_UNDEF
399 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
401 /* The linker decisions ought to agree in the whole group. */
402 if (node->same_comdat_group)
403 for (symtab_node *next = node->same_comdat_group;
404 next != node; next = next->same_comdat_group)
406 if (!next->externally_visible || next->transparent_alias)
407 continue;
409 bool same_def
410 = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
411 || next->resolution == LDPR_PREVAILING_DEF
412 || next->resolution == LDPR_UNDEF
413 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
414 gcc_assert (in_lto_p || same_def);
415 if (!same_def)
416 return;
419 if (node->same_comdat_group)
420 for (symtab_node *next = node->same_comdat_group;
421 next != node; next = next->same_comdat_group)
423 /* During incremental linking we need to keep symbol weak for future
424 linking. We can still drop definition if we know non-LTO world
425 prevails. */
426 if (!flag_incremental_link)
428 DECL_WEAK (next->decl) = false;
429 next->set_comdat_group (NULL);
431 if (!define)
433 if (next->externally_visible)
434 DECL_EXTERNAL (next->decl) = true;
435 next->set_comdat_group (NULL);
439 /* During incremental linking we need to keep symbol weak for future
440 linking. We can still drop definition if we know non-LTO world prevails. */
441 if (!flag_incremental_link)
443 DECL_WEAK (node->decl) = false;
444 node->set_comdat_group (NULL);
445 node->dissolve_same_comdat_group_list ();
447 if (!define)
449 DECL_EXTERNAL (node->decl) = true;
450 node->set_comdat_group (NULL);
451 node->dissolve_same_comdat_group_list ();
455 /* Try to get rid of weakref. */
457 static void
458 optimize_weakref (symtab_node *node)
460 #ifdef ASM_OUTPUT_DEF
461 bool aliases_supported = true;
462 #else
463 bool aliases_supported = false;
464 #endif
465 bool strip_weakref = false;
466 bool static_alias = false;
468 gcc_assert (node->weakref);
470 /* Weakrefs with no target defined can not be optimized. */
471 if (!node->analyzed)
472 return;
473 symtab_node *target = node->get_alias_target ();
475 /* Weakrefs to weakrefs can be optimized only if target can be. */
476 if (target->weakref)
477 optimize_weakref (target);
478 if (target->weakref)
479 return;
481 /* If we have definition of weakref's target and we know it binds locally,
482 we can turn weakref to static alias. */
483 if (target->definition && decl_binds_to_current_def_p (target->decl)
484 && aliases_supported)
485 strip_weakref = static_alias = true;
486 /* Otherwise we can turn weakref into transparent alias. This transformation
487 may break asm statements which directly refers to symbol name and expect
488 GNU as to translate it via .weakref directive. So do not optimize when
489 DECL_PRESERVED is set and .weakref is supported. */
490 else if ((!DECL_PRESERVE_P (target->decl)
491 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)))
492 && !DECL_WEAK (target->decl)
493 && !DECL_EXTERNAL (target->decl)
494 && ((target->definition && !target->can_be_discarded_p ())
495 || target->resolution != LDPR_UNDEF))
496 strip_weakref = true;
497 if (!strip_weakref)
498 return;
499 node->weakref = false;
500 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)) = 0;
501 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) = NULL_TREE;
502 DECL_ATTRIBUTES (node->decl) = remove_attribute ("weakref",
503 DECL_ATTRIBUTES
504 (node->decl));
506 if (dump_file)
507 fprintf (dump_file, "Optimizing weakref %s %s\n",
508 node->name(),
509 static_alias ? "as static alias" : "as transparent alias");
511 if (static_alias)
513 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
514 be sure it really clears the WEAK flag. */
515 TREE_PUBLIC (node->decl) = true;
516 node->make_decl_local ();
517 node->forced_by_abi = false;
518 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
519 node->externally_visible = false;
520 gcc_assert (!DECL_WEAK (node->decl));
521 node->transparent_alias = false;
523 else
525 symtab->change_decl_assembler_name
526 (node->decl, DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
527 node->transparent_alias = true;
528 node->copy_visibility_from (target);
530 gcc_assert (node->alias);
533 /* NODE is an externally visible definition, which we've discovered is
534 not needed externally. Make it local to this compilation. */
536 static void
537 localize_node (bool whole_program, symtab_node *node)
539 gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
541 /* It is possible that one comdat group contains both hidden and non-hidden
542 symbols. In this case we can privatize all hidden symbol but we need
543 to keep non-hidden exported. */
544 if (node->same_comdat_group
545 && node->resolution == LDPR_PREVAILING_DEF_IRONLY)
547 symtab_node *next;
548 for (next = node->same_comdat_group;
549 next != node; next = next->same_comdat_group)
550 if (next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
551 || next->resolution == LDPR_PREVAILING_DEF)
552 break;
553 if (node != next)
555 if (!node->transparent_alias)
557 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
558 node->make_decl_local ();
559 if (!flag_incremental_link)
560 node->unique_name |= true;
561 return;
565 /* For similar reason do not privatize whole comdat when seeing comdat
566 local. Wait for non-comdat symbol to be privatized first. */
567 if (node->comdat_local_p ())
568 return;
570 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
572 for (symtab_node *next = node->same_comdat_group;
573 next != node; next = next->same_comdat_group)
575 next->set_comdat_group (NULL);
576 if (!next->alias)
577 next->set_section (NULL);
578 if (!next->transparent_alias)
579 next->make_decl_local ();
580 next->unique_name
581 |= ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
582 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
583 && TREE_PUBLIC (next->decl)
584 && !flag_incremental_link);
587 /* Now everything's localized, the grouping has no meaning, and
588 will cause crashes if we keep it around. */
589 node->dissolve_same_comdat_group_list ();
592 node->unique_name
593 |= ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
594 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
595 && TREE_PUBLIC (node->decl)
596 && !flag_incremental_link);
598 if (TREE_PUBLIC (node->decl))
599 node->set_comdat_group (NULL);
600 if (DECL_COMDAT (node->decl) && !node->alias)
601 node->set_section (NULL);
602 if (!node->transparent_alias)
604 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
605 node->make_decl_local ();
609 /* Decide on visibility of all symbols. */
611 static unsigned int
612 function_and_variable_visibility (bool whole_program)
614 struct cgraph_node *node;
615 varpool_node *vnode;
617 /* All aliases should be procssed at this point. */
618 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
620 FOR_EACH_FUNCTION (node)
622 int flags = flags_from_decl_or_type (node->decl);
624 /* Optimize away PURE and CONST constructors and destructors. */
625 if (node->analyzed
626 && (DECL_STATIC_CONSTRUCTOR (node->decl)
627 || DECL_STATIC_DESTRUCTOR (node->decl))
628 && (flags & (ECF_CONST | ECF_PURE))
629 && !(flags & ECF_LOOPING_CONST_OR_PURE)
630 && opt_for_fn (node->decl, optimize))
632 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
633 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
636 /* Frontends and alias code marks nodes as needed before parsing
637 is finished. We may end up marking as node external nodes
638 where this flag is meaningless strip it. */
639 if (DECL_EXTERNAL (node->decl) || !node->definition)
641 node->force_output = 0;
642 node->forced_by_abi = 0;
645 /* C++ FE on lack of COMDAT support create local COMDAT functions
646 (that ought to be shared but can not due to object format
647 limitations). It is necessary to keep the flag to make rest of C++ FE
648 happy. Clear the flag here to avoid confusion in middle-end. */
649 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
650 DECL_COMDAT (node->decl) = 0;
652 /* For external decls stop tracking same_comdat_group. It doesn't matter
653 what comdat group they are in when they won't be emitted in this TU.
655 An exception is LTO where we may end up with both external
656 and non-external declarations in the same comdat group in
657 the case declarations was not merged. */
658 if (node->same_comdat_group && DECL_EXTERNAL (node->decl) && !in_lto_p)
660 if (flag_checking)
662 for (symtab_node *n = node->same_comdat_group;
663 n != node;
664 n = n->same_comdat_group)
665 /* If at least one of same comdat group functions is external,
666 all of them have to be, otherwise it is a front-end bug. */
667 gcc_assert (DECL_EXTERNAL (n->decl));
669 node->dissolve_same_comdat_group_list ();
671 gcc_assert ((!DECL_WEAK (node->decl)
672 && !DECL_COMDAT (node->decl))
673 || TREE_PUBLIC (node->decl)
674 || node->weakref
675 || DECL_EXTERNAL (node->decl));
676 if (cgraph_externally_visible_p (node, whole_program))
678 gcc_assert (!node->global.inlined_to);
679 node->externally_visible = true;
681 else
683 node->externally_visible = false;
684 node->forced_by_abi = false;
686 if (!node->externally_visible
687 && node->definition && !node->weakref
688 && !DECL_EXTERNAL (node->decl))
689 localize_node (whole_program, node);
691 if (node->thunk.thunk_p
692 && !node->thunk.add_pointer_bounds_args
693 && TREE_PUBLIC (node->decl))
695 struct cgraph_node *decl_node = node;
697 decl_node = decl_node->callees->callee->function_symbol ();
699 /* Thunks have the same visibility as function they are attached to.
700 Make sure the C++ front end set this up properly. */
701 if (DECL_ONE_ONLY (decl_node->decl))
703 gcc_checking_assert (DECL_COMDAT (node->decl)
704 == DECL_COMDAT (decl_node->decl));
705 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
706 gcc_checking_assert (node->same_comdat_group);
708 node->forced_by_abi = decl_node->forced_by_abi;
709 if (DECL_EXTERNAL (decl_node->decl))
710 DECL_EXTERNAL (node->decl) = 1;
713 update_visibility_by_resolution_info (node);
714 if (node->weakref)
715 optimize_weakref (node);
717 FOR_EACH_DEFINED_FUNCTION (node)
719 if (!node->local.local)
720 node->local.local |= node->local_p ();
722 /* If we know that function can not be overwritten by a
723 different semantics and moreover its section can not be
724 discarded, replace all direct calls by calls to an
725 noninterposable alias. This make dynamic linking cheaper and
726 enable more optimization.
728 TODO: We can also update virtual tables. */
729 if (node->callers
730 && can_replace_by_local_alias (node))
732 cgraph_node *alias = dyn_cast<cgraph_node *>
733 (node->noninterposable_alias ());
735 if (alias && alias != node)
737 while (node->callers)
739 struct cgraph_edge *e = node->callers;
741 e->redirect_callee (alias);
742 if (gimple_has_body_p (e->caller->decl))
744 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
745 e->redirect_call_stmt_to_callee ();
746 pop_cfun ();
752 FOR_EACH_VARIABLE (vnode)
754 /* weak flag makes no sense on local variables. */
755 gcc_assert (!DECL_WEAK (vnode->decl)
756 || vnode->weakref
757 || TREE_PUBLIC (vnode->decl)
758 || DECL_EXTERNAL (vnode->decl));
759 /* In several cases declarations can not be common:
761 - when declaration has initializer
762 - when it is in weak
763 - when it has specific section
764 - when it resides in non-generic address space.
765 - if declaration is local, it will get into .local common section
766 so common flag is not needed. Frontends still produce these in
767 certain cases, such as for:
769 static int a __attribute__ ((common))
771 Canonicalize things here and clear the redundant flag. */
772 if (DECL_COMMON (vnode->decl)
773 && (!(TREE_PUBLIC (vnode->decl)
774 || DECL_EXTERNAL (vnode->decl))
775 || (DECL_INITIAL (vnode->decl)
776 && DECL_INITIAL (vnode->decl) != error_mark_node)
777 || DECL_WEAK (vnode->decl)
778 || DECL_SECTION_NAME (vnode->decl) != NULL
779 || ! (ADDR_SPACE_GENERIC_P
780 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
781 DECL_COMMON (vnode->decl) = 0;
782 if (vnode->weakref)
783 optimize_weakref (vnode);
785 FOR_EACH_DEFINED_VARIABLE (vnode)
787 if (!vnode->definition)
788 continue;
789 if (vnode->externally_visible_p ())
790 vnode->externally_visible = true;
791 else
793 vnode->externally_visible = false;
794 vnode->forced_by_abi = false;
796 if (lookup_attribute ("no_reorder",
797 DECL_ATTRIBUTES (vnode->decl)))
798 vnode->no_reorder = 1;
800 if (!vnode->externally_visible
801 && !vnode->transparent_alias
802 && !DECL_EXTERNAL (vnode->decl))
803 localize_node (whole_program, vnode);
805 update_visibility_by_resolution_info (vnode);
807 /* Update virtual tables to point to local aliases where possible. */
808 if (DECL_VIRTUAL_P (vnode->decl)
809 && !DECL_EXTERNAL (vnode->decl))
811 int i;
812 struct ipa_ref *ref;
813 bool found = false;
815 /* See if there is something to update. */
816 for (i = 0; vnode->iterate_reference (i, ref); i++)
817 if (ref->use == IPA_REF_ADDR
818 && can_replace_by_local_alias_in_vtable (ref->referred))
820 found = true;
821 break;
823 if (found)
825 hash_set<tree> visited_nodes;
827 vnode->get_constructor ();
828 walk_tree (&DECL_INITIAL (vnode->decl),
829 update_vtable_references, NULL, &visited_nodes);
830 vnode->remove_all_references ();
831 record_references_in_initializer (vnode->decl, false);
836 if (dump_file)
838 fprintf (dump_file, "\nMarking local functions:");
839 FOR_EACH_DEFINED_FUNCTION (node)
840 if (node->local.local)
841 fprintf (dump_file, " %s", node->name ());
842 fprintf (dump_file, "\n\n");
843 fprintf (dump_file, "\nMarking externally visible functions:");
844 FOR_EACH_DEFINED_FUNCTION (node)
845 if (node->externally_visible)
846 fprintf (dump_file, " %s", node->name ());
847 fprintf (dump_file, "\n\n");
848 fprintf (dump_file, "\nMarking externally visible variables:");
849 FOR_EACH_DEFINED_VARIABLE (vnode)
850 if (vnode->externally_visible)
851 fprintf (dump_file, " %s", vnode->name ());
852 fprintf (dump_file, "\n\n");
854 symtab->function_flags_ready = true;
855 return 0;
858 /* Local function pass handling visibilities. This happens before LTO streaming
859 so in particular -fwhole-program should be ignored at this level. */
861 namespace {
863 const pass_data pass_data_ipa_function_and_variable_visibility =
865 SIMPLE_IPA_PASS, /* type */
866 "visibility", /* name */
867 OPTGROUP_NONE, /* optinfo_flags */
868 TV_CGRAPHOPT, /* tv_id */
869 0, /* properties_required */
870 0, /* properties_provided */
871 0, /* properties_destroyed */
872 0, /* todo_flags_start */
873 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
876 /* Bring functions local at LTO time with -fwhole-program. */
878 static unsigned int
879 whole_program_function_and_variable_visibility (void)
881 function_and_variable_visibility (flag_whole_program);
882 if (optimize || in_lto_p)
883 ipa_discover_readonly_nonaddressable_vars ();
884 return 0;
887 } // anon namespace
889 namespace {
891 const pass_data pass_data_ipa_whole_program_visibility =
893 IPA_PASS, /* type */
894 "whole-program", /* name */
895 OPTGROUP_NONE, /* optinfo_flags */
896 TV_CGRAPHOPT, /* tv_id */
897 0, /* properties_required */
898 0, /* properties_provided */
899 0, /* properties_destroyed */
900 0, /* todo_flags_start */
901 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
904 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
906 public:
907 pass_ipa_whole_program_visibility (gcc::context *ctxt)
908 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
909 NULL, /* generate_summary */
910 NULL, /* write_summary */
911 NULL, /* read_summary */
912 NULL, /* write_optimization_summary */
913 NULL, /* read_optimization_summary */
914 NULL, /* stmt_fixup */
915 0, /* function_transform_todo_flags_start */
916 NULL, /* function_transform */
917 NULL) /* variable_transform */
920 /* opt_pass methods: */
922 virtual bool gate (function *)
924 /* Do not re-run on ltrans stage. */
925 return !flag_ltrans;
927 virtual unsigned int execute (function *)
929 return whole_program_function_and_variable_visibility ();
932 }; // class pass_ipa_whole_program_visibility
934 } // anon namespace
936 ipa_opt_pass_d *
937 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
939 return new pass_ipa_whole_program_visibility (ctxt);
942 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
944 public:
945 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
946 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
947 ctxt)
950 /* opt_pass methods: */
951 virtual unsigned int execute (function *)
953 return function_and_variable_visibility (flag_whole_program && !flag_lto);
956 }; // class pass_ipa_function_and_variable_visibility
958 simple_ipa_opt_pass *
959 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
961 return new pass_ipa_function_and_variable_visibility (ctxt);