Import pre-release gcc-5.0 to new vendor branch
[dragonfly.git] / contrib / gcc-5.0 / gcc / ipa-visibility.c
1 /* IPA visibility pass
2    Copyright (C) 2003-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>.  */
19
20 /* This file implements two related passes: 
21
22      - pass_data_ipa_function_and_variable_visibility run just after
23        symbol table, references and callgraph are built
24
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.
28
29    Purpose of both passes is to set correctly visibility properties
30    of all symbols.  This includes:
31
32     - Symbol privatization:
33
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)
37
38     - Discovery of local functions:
39
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.
44
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.
48
49     - Dismantling of comdat groups:
50
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.
58
59     - Local aliases:
60
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.
67
68       TODO: This should be done for references, too.
69
70     - Removal of static ocnstructors and destructors that have no side effects.
71
72     - Regularization of several oddities introduced by frontends that may
73       be impractical later in the optimization queue.  */
74
75 #include "config.h"
76 #include "system.h"
77 #include "coretypes.h"
78 #include "tm.h"
79 #include "hash-set.h"
80 #include "machmode.h"
81 #include "vec.h"
82 #include "double-int.h"
83 #include "input.h"
84 #include "alias.h"
85 #include "symtab.h"
86 #include "wide-int.h"
87 #include "inchash.h"
88 #include "tree.h"
89 #include "hash-map.h"
90 #include "is-a.h"
91 #include "plugin-api.h"
92 #include "hard-reg-set.h"
93 #include "input.h"
94 #include "function.h"
95 #include "ipa-ref.h"
96 #include "cgraph.h"
97 #include "tree-pass.h"
98 #include "calls.h"
99 #include "gimple-expr.h"
100 #include "varasm.h"
101
102 /* Return true when NODE can not be local. Worker for cgraph_local_node_p.  */
103
104 bool
105 cgraph_node::non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
106 {
107    /* FIXME: Aliases can be local, but i386 gets thunks wrong then.  */
108    return !(node->only_called_directly_or_aliased_p ()
109             && !node->has_aliases_p ()
110             && node->definition
111             && !DECL_EXTERNAL (node->decl)
112             && !node->externally_visible
113             && !node->used_from_other_partition
114             && !node->in_other_partition);
115 }
116
117 /* Return true when function can be marked local.  */
118
119 bool
120 cgraph_node::local_p (void)
121 {
122    cgraph_node *n = ultimate_alias_target ();
123
124    /* FIXME: thunks can be considered local, but we need prevent i386
125       from attempting to change calling convention of them.  */
126    if (n->thunk.thunk_p)
127      return false;
128    return !n->call_for_symbol_thunks_and_aliases (cgraph_node::non_local_p,
129                                                 NULL, true);
130                                         
131 }
132
133 /* Return true when there is a reference to node and it is not vtable.  */
134
135 bool
136 symtab_node::address_taken_from_non_vtable_p (void)
137 {
138   int i;
139   struct ipa_ref *ref = NULL;
140
141   for (i = 0; iterate_referring (i, ref); i++)
142     if (ref->use == IPA_REF_ADDR)
143       {
144         varpool_node *node;
145         if (is_a <cgraph_node *> (ref->referring))
146           return true;
147         node = dyn_cast <varpool_node *> (ref->referring);
148         if (!DECL_VIRTUAL_P (node->decl))
149           return true;
150       }
151   return false;
152 }
153
154 /* A helper for comdat_can_be_unshared_p.  */
155
156 static bool
157 comdat_can_be_unshared_p_1 (symtab_node *node)
158 {
159   if (!node->externally_visible)
160     return true;
161   /* When address is taken, we don't know if equality comparison won't
162      break eventually. Exception are virutal functions, C++
163      constructors/destructors and vtables, where this is not possible by
164      language standard.  */
165   if (!DECL_VIRTUAL_P (node->decl)
166       && (TREE_CODE (node->decl) != FUNCTION_DECL
167           || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
168               && !DECL_CXX_DESTRUCTOR_P (node->decl)))
169       && node->address_taken_from_non_vtable_p ())
170     return false;
171
172   /* If the symbol is used in some weird way, better to not touch it.  */
173   if (node->force_output)
174     return false;
175
176   /* Explicit instantiations needs to be output when possibly
177      used externally.  */
178   if (node->forced_by_abi
179       && TREE_PUBLIC (node->decl)
180       && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
181           && !flag_whole_program))
182     return false;
183
184   /* Non-readonly and volatile variables can not be duplicated.  */
185   if (is_a <varpool_node *> (node)
186       && (!TREE_READONLY (node->decl)
187           || TREE_THIS_VOLATILE (node->decl)))
188     return false;
189   return true;
190 }
191
192 /* COMDAT functions must be shared only if they have address taken,
193    otherwise we can produce our own private implementation with
194    -fwhole-program.  
195    Return true when turning COMDAT functoin static can not lead to wrong
196    code when the resulting object links with a library defining same COMDAT.
197
198    Virtual functions do have their addresses taken from the vtables,
199    but in C++ there is no way to compare their addresses for equality.  */
200
201 static bool
202 comdat_can_be_unshared_p (symtab_node *node)
203 {
204   if (!comdat_can_be_unshared_p_1 (node))
205     return false;
206   if (node->same_comdat_group)
207     {
208       symtab_node *next;
209
210       /* If more than one function is in the same COMDAT group, it must
211          be shared even if just one function in the comdat group has
212          address taken.  */
213       for (next = node->same_comdat_group;
214            next != node; next = next->same_comdat_group)
215         if (!comdat_can_be_unshared_p_1 (next))
216           return false;
217     }
218   return true;
219 }
220
221 /* Return true when function NODE should be considered externally visible.  */
222
223 static bool
224 cgraph_externally_visible_p (struct cgraph_node *node,
225                              bool whole_program)
226 {
227   if (!node->definition)
228     return false;
229   if (!TREE_PUBLIC (node->decl)
230       || DECL_EXTERNAL (node->decl))
231     return false;
232
233   /* Do not try to localize built-in functions yet.  One of problems is that we
234      end up mangling their asm for WHOPR that makes it impossible to call them
235      using the implicit built-in declarations anymore.  Similarly this enables
236      us to remove them as unreachable before actual calls may appear during
237      expansion or folding.  */
238   if (DECL_BUILT_IN (node->decl))
239     return true;
240
241   /* If linker counts on us, we must preserve the function.  */
242   if (node->used_from_object_file_p ())
243     return true;
244   if (DECL_PRESERVE_P (node->decl))
245     return true;
246   if (lookup_attribute ("externally_visible",
247                         DECL_ATTRIBUTES (node->decl)))
248     return true;
249   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
250       && lookup_attribute ("dllexport",
251                            DECL_ATTRIBUTES (node->decl)))
252     return true;
253   if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
254     return false;
255   /* When doing LTO or whole program, we can bring COMDAT functoins static.
256      This improves code quality and we know we will duplicate them at most twice
257      (in the case that we are not using plugin and link with object file
258       implementing same COMDAT)  */
259   if ((in_lto_p || whole_program)
260       && DECL_COMDAT (node->decl)
261       && comdat_can_be_unshared_p (node))
262     return false;
263
264   /* When doing link time optimizations, hidden symbols become local.  */
265   if (in_lto_p
266       && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
267           || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
268       /* Be sure that node is defined in IR file, not in other object
269          file.  In that case we don't set used_from_other_object_file.  */
270       && node->definition)
271     ;
272   else if (!whole_program)
273     return true;
274
275   if (MAIN_NAME_P (DECL_NAME (node->decl)))
276     return true;
277
278   if (node->instrumentation_clone
279       && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
280     return true;
281
282   return false;
283 }
284
285 /* Return true when variable should be considered externally visible.  */
286
287 bool
288 varpool_node::externally_visible_p (void)
289 {
290   if (DECL_EXTERNAL (decl))
291     return true;
292
293   if (!TREE_PUBLIC (decl))
294     return false;
295
296   /* If linker counts on us, we must preserve the function.  */
297   if (used_from_object_file_p ())
298     return true;
299
300   /* Bringing TLS variables local may cause dynamic linker failures
301      on limits of static TLS vars.  */
302   if (DECL_THREAD_LOCAL_P (decl)
303       && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
304           && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
305     return true;
306
307   if (DECL_HARD_REGISTER (decl))
308     return true;
309   if (DECL_PRESERVE_P (decl))
310     return true;
311   if (lookup_attribute ("externally_visible",
312                         DECL_ATTRIBUTES (decl)))
313     return true;
314   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
315       && lookup_attribute ("dllexport",
316                            DECL_ATTRIBUTES (decl)))
317     return true;
318
319   /* See if we have linker information about symbol not being used or
320      if we need to make guess based on the declaration.
321
322      Even if the linker clams the symbol is unused, never bring internal
323      symbols that are declared by user as used or externally visible.
324      This is needed for i.e. references from asm statements.   */
325   if (used_from_object_file_p ())
326     return true;
327   if (resolution == LDPR_PREVAILING_DEF_IRONLY)
328     return false;
329
330   /* As a special case, the COMDAT virtual tables can be unshared.
331      In LTO mode turn vtables into static variables.  The variable is readonly,
332      so this does not enable more optimization, but referring static var
333      is faster for dynamic linking.  Also this match logic hidding vtables
334      from LTO symbol tables.  */
335   if ((in_lto_p || flag_whole_program)
336       && DECL_COMDAT (decl)
337       && comdat_can_be_unshared_p (this))
338     return false;
339
340   /* When doing link time optimizations, hidden symbols become local.  */
341   if (in_lto_p
342       && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
343           || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
344       /* Be sure that node is defined in IR file, not in other object
345          file.  In that case we don't set used_from_other_object_file.  */
346       && definition)
347     ;
348   else if (!flag_whole_program)
349     return true;
350
351   /* Do not attempt to privatize COMDATS by default.
352      This would break linking with C++ libraries sharing
353      inline definitions.
354
355      FIXME: We can do so for readonly vars with no address taken and
356      possibly also for vtables since no direct pointer comparsion is done.
357      It might be interesting to do so to reduce linking overhead.  */
358   if (DECL_COMDAT (decl) || DECL_WEAK (decl))
359     return true;
360   return false;
361 }
362
363 /* Return true if reference to NODE can be replaced by a local alias.
364    Local aliases save dynamic linking overhead and enable more optimizations.
365  */
366
367 bool
368 can_replace_by_local_alias (symtab_node *node)
369 {
370   return (node->get_availability () > AVAIL_INTERPOSABLE
371           && !decl_binds_to_current_def_p (node->decl)
372           && !node->can_be_discarded_p ());
373 }
374
375 /* Return true if we can replace refernece to NODE by local alias
376    within a virtual table.  Generally we can replace function pointers
377    and virtual table pointers.  */
378
379 bool
380 can_replace_by_local_alias_in_vtable (symtab_node *node)
381 {
382   if (is_a <varpool_node *> (node)
383       && !DECL_VIRTUAL_P (node->decl))
384     return false;
385   return can_replace_by_local_alias (node);
386 }
387
388 /* walk_tree callback that rewrites initializer references.   */
389
390 static tree
391 update_vtable_references (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
392 {
393   if (TREE_CODE (*tp) == VAR_DECL
394       || TREE_CODE (*tp) == FUNCTION_DECL)
395     {
396       if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
397         *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
398       *walk_subtrees = 0;
399     }
400   else if (IS_TYPE_OR_DECL_P (*tp))
401     *walk_subtrees = 0;
402   return NULL;
403 }
404
405 /* In LTO we can remove COMDAT groups and weak symbols.
406    Either turn them into normal symbols or external symbol depending on 
407    resolution info.  */
408
409 static void
410 update_visibility_by_resolution_info (symtab_node * node)
411 {
412   bool define;
413
414   if (!node->externally_visible
415       || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
416       || node->resolution == LDPR_UNKNOWN)
417     return;
418
419   define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
420             || node->resolution == LDPR_PREVAILING_DEF
421             || node->resolution == LDPR_UNDEF
422             || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
423
424   /* The linker decisions ought to agree in the whole group.  */
425   if (node->same_comdat_group)
426     for (symtab_node *next = node->same_comdat_group;
427          next != node; next = next->same_comdat_group)
428       gcc_assert (!next->externally_visible
429                   || define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
430                                 || next->resolution == LDPR_PREVAILING_DEF
431                                 || next->resolution == LDPR_UNDEF
432                                 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP));
433
434   if (node->same_comdat_group)
435     for (symtab_node *next = node->same_comdat_group;
436          next != node; next = next->same_comdat_group)
437       {
438         next->set_comdat_group (NULL);
439         DECL_WEAK (next->decl) = false;
440         if (next->externally_visible
441             && !define)
442           DECL_EXTERNAL (next->decl) = true;
443       }
444   node->set_comdat_group (NULL);
445   DECL_WEAK (node->decl) = false;
446   if (!define)
447     DECL_EXTERNAL (node->decl) = true;
448   node->dissolve_same_comdat_group_list ();
449 }
450
451 /* Decide on visibility of all symbols.  */
452
453 static unsigned int
454 function_and_variable_visibility (bool whole_program)
455 {
456   struct cgraph_node *node;
457   varpool_node *vnode;
458
459   /* All aliases should be procssed at this point.  */
460   gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
461
462   FOR_EACH_FUNCTION (node)
463     {
464       int flags = flags_from_decl_or_type (node->decl);
465
466       /* Optimize away PURE and CONST constructors and destructors.  */
467       if (optimize
468           && (flags & (ECF_CONST | ECF_PURE))
469           && !(flags & ECF_LOOPING_CONST_OR_PURE))
470         {
471           DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
472           DECL_STATIC_DESTRUCTOR (node->decl) = 0;
473         }
474
475       /* Frontends and alias code marks nodes as needed before parsing is finished.
476          We may end up marking as node external nodes where this flag is meaningless
477          strip it.  */
478       if (DECL_EXTERNAL (node->decl) || !node->definition)
479         {
480           node->force_output = 0;
481           node->forced_by_abi = 0;
482         }
483
484       /* C++ FE on lack of COMDAT support create local COMDAT functions
485          (that ought to be shared but can not due to object format
486          limitations).  It is necessary to keep the flag to make rest of C++ FE
487          happy.  Clear the flag here to avoid confusion in middle-end.  */
488       if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
489         DECL_COMDAT (node->decl) = 0;
490
491       /* For external decls stop tracking same_comdat_group. It doesn't matter
492          what comdat group they are in when they won't be emitted in this TU.  */
493       if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
494         {
495 #ifdef ENABLE_CHECKING
496           symtab_node *n;
497
498           for (n = node->same_comdat_group;
499                n != node;
500                n = n->same_comdat_group)
501               /* If at least one of same comdat group functions is external,
502                  all of them have to be, otherwise it is a front-end bug.  */
503               gcc_assert (DECL_EXTERNAL (n->decl));
504 #endif
505           node->dissolve_same_comdat_group_list ();
506         }
507       gcc_assert ((!DECL_WEAK (node->decl)
508                   && !DECL_COMDAT (node->decl))
509                   || TREE_PUBLIC (node->decl)
510                   || node->weakref
511                   || DECL_EXTERNAL (node->decl));
512       if (cgraph_externally_visible_p (node, whole_program))
513         {
514           gcc_assert (!node->global.inlined_to);
515           node->externally_visible = true;
516         }
517       else
518         {
519           node->externally_visible = false;
520           node->forced_by_abi = false;
521         }
522       if (!node->externally_visible
523           && node->definition && !node->weakref
524           && !DECL_EXTERNAL (node->decl))
525         {
526           gcc_assert (whole_program || in_lto_p
527                       || !TREE_PUBLIC (node->decl));
528           node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
529                                 || node->unique_name
530                                 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
531                                 && TREE_PUBLIC (node->decl));
532           node->resolution = LDPR_PREVAILING_DEF_IRONLY;
533           if (node->same_comdat_group && TREE_PUBLIC (node->decl))
534             {
535               symtab_node *next = node;
536
537               /* Set all members of comdat group local.  */
538               if (node->same_comdat_group)
539                 for (next = node->same_comdat_group;
540                      next != node;
541                      next = next->same_comdat_group)
542                 {
543                   next->set_comdat_group (NULL);
544                   if (!next->alias)
545                     next->set_section (NULL);
546                   next->make_decl_local ();
547                   next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
548                                         || next->unique_name
549                                         || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
550                                        && TREE_PUBLIC (next->decl));
551                 }
552               /* cgraph_externally_visible_p has already checked all other nodes
553                  in the group and they will all be made local.  We need to
554                  dissolve the group at once so that the predicate does not
555                  segfault though. */
556               node->dissolve_same_comdat_group_list ();
557             }
558           if (TREE_PUBLIC (node->decl))
559             node->set_comdat_group (NULL);
560           if (DECL_COMDAT (node->decl) && !node->alias)
561             node->set_section (NULL);
562           node->make_decl_local ();
563         }
564
565       if (node->thunk.thunk_p
566           && !node->thunk.add_pointer_bounds_args
567           && TREE_PUBLIC (node->decl))
568         {
569           struct cgraph_node *decl_node = node;
570
571           decl_node = decl_node->callees->callee->function_symbol ();
572
573           /* Thunks have the same visibility as function they are attached to.
574              Make sure the C++ front end set this up properly.  */
575           if (DECL_ONE_ONLY (decl_node->decl))
576             {
577               gcc_checking_assert (DECL_COMDAT (node->decl)
578                                    == DECL_COMDAT (decl_node->decl));
579               gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
580               gcc_checking_assert (node->same_comdat_group);
581             }
582           node->forced_by_abi = decl_node->forced_by_abi;
583           if (DECL_EXTERNAL (decl_node->decl))
584             DECL_EXTERNAL (node->decl) = 1;
585         }
586
587       update_visibility_by_resolution_info (node);
588     }
589   FOR_EACH_DEFINED_FUNCTION (node)
590     {
591       node->local.local |= node->local_p ();
592
593       /* If we know that function can not be overwritten by a different semantics
594          and moreover its section can not be discarded, replace all direct calls
595          by calls to an noninterposable alias.  This make dynamic linking
596          cheaper and enable more optimization.
597
598          TODO: We can also update virtual tables.  */
599       if (node->callers 
600           && can_replace_by_local_alias (node))
601         {
602           cgraph_node *alias = dyn_cast<cgraph_node *>
603             (node->noninterposable_alias ());
604
605           if (alias && alias != node)
606             {
607               while (node->callers)
608                 {
609                   struct cgraph_edge *e = node->callers;
610
611                   e->redirect_callee (alias);
612                   if (gimple_has_body_p (e->caller->decl))
613                     {
614                       push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
615                       e->redirect_call_stmt_to_callee ();
616                       pop_cfun ();
617                     }
618                 }
619             }
620         }
621     }
622   FOR_EACH_VARIABLE (vnode)
623     {
624       /* weak flag makes no sense on local variables.  */
625       gcc_assert (!DECL_WEAK (vnode->decl)
626                   || vnode->weakref
627                   || TREE_PUBLIC (vnode->decl)
628                   || DECL_EXTERNAL (vnode->decl));
629       /* In several cases declarations can not be common:
630
631          - when declaration has initializer
632          - when it is in weak
633          - when it has specific section
634          - when it resides in non-generic address space.
635          - if declaration is local, it will get into .local common section
636            so common flag is not needed.  Frontends still produce these in
637            certain cases, such as for:
638
639              static int a __attribute__ ((common))
640
641          Canonicalize things here and clear the redundant flag.  */
642       if (DECL_COMMON (vnode->decl)
643           && (!(TREE_PUBLIC (vnode->decl)
644               || DECL_EXTERNAL (vnode->decl))
645               || (DECL_INITIAL (vnode->decl)
646                   && DECL_INITIAL (vnode->decl) != error_mark_node)
647               || DECL_WEAK (vnode->decl)
648               || DECL_SECTION_NAME (vnode->decl) != NULL
649               || ! (ADDR_SPACE_GENERIC_P
650                     (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
651         DECL_COMMON (vnode->decl) = 0;
652     }
653   FOR_EACH_DEFINED_VARIABLE (vnode)
654     {
655       if (!vnode->definition)
656         continue;
657       if (vnode->externally_visible_p ())
658         vnode->externally_visible = true;
659       else
660         {
661           vnode->externally_visible = false;
662           vnode->forced_by_abi = false;
663         }
664       if (lookup_attribute ("no_reorder",
665                             DECL_ATTRIBUTES (vnode->decl)))
666         vnode->no_reorder = 1;
667       if (!vnode->externally_visible
668           && !vnode->weakref)
669         {
670           gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
671           vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
672                                        || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
673                                        && TREE_PUBLIC (vnode->decl));
674           if (vnode->same_comdat_group && TREE_PUBLIC (vnode->decl))
675             {
676               symtab_node *next = vnode;
677
678               /* Set all members of comdat group local.  */
679               if (vnode->same_comdat_group)
680                 for (next = vnode->same_comdat_group;
681                      next != vnode;
682                      next = next->same_comdat_group)
683                 {
684                   next->set_comdat_group (NULL);
685                   if (!next->alias)
686                     next->set_section (NULL);
687                   next->make_decl_local ();
688                   next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
689                                         || next->unique_name
690                                         || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
691                                        && TREE_PUBLIC (next->decl));
692                 }
693               vnode->dissolve_same_comdat_group_list ();
694             }
695           if (TREE_PUBLIC (vnode->decl))
696             vnode->set_comdat_group (NULL);
697           if (DECL_COMDAT (vnode->decl) && !vnode->alias)
698             vnode->set_section (NULL);
699           vnode->make_decl_local ();
700           vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
701         }
702       update_visibility_by_resolution_info (vnode);
703
704       /* Update virtual tables to point to local aliases where possible.  */
705       if (DECL_VIRTUAL_P (vnode->decl)
706           && !DECL_EXTERNAL (vnode->decl))
707         {
708           int i;
709           struct ipa_ref *ref;
710           bool found = false;
711
712           /* See if there is something to update.  */
713           for (i = 0; vnode->iterate_referring (i, ref); i++)
714             if (ref->use == IPA_REF_ADDR
715                 && can_replace_by_local_alias_in_vtable (ref->referred))
716               {
717                 found = true;
718                 break;
719               }
720           if (found)
721             {
722               hash_set<tree> visited_nodes;
723
724               vnode->get_constructor ();
725               walk_tree (&DECL_INITIAL (vnode->decl),
726                          update_vtable_references, NULL, &visited_nodes);
727               vnode->remove_all_references ();
728               record_references_in_initializer (vnode->decl, false);
729             }
730         }
731     }
732
733   if (dump_file)
734     {
735       fprintf (dump_file, "\nMarking local functions:");
736       FOR_EACH_DEFINED_FUNCTION (node)
737         if (node->local.local)
738           fprintf (dump_file, " %s", node->name ());
739       fprintf (dump_file, "\n\n");
740       fprintf (dump_file, "\nMarking externally visible functions:");
741       FOR_EACH_DEFINED_FUNCTION (node)
742         if (node->externally_visible)
743           fprintf (dump_file, " %s", node->name ());
744       fprintf (dump_file, "\n\n");
745       fprintf (dump_file, "\nMarking externally visible variables:");
746       FOR_EACH_DEFINED_VARIABLE (vnode)
747         if (vnode->externally_visible)
748           fprintf (dump_file, " %s", vnode->name ());
749       fprintf (dump_file, "\n\n");
750     }
751   symtab->function_flags_ready = true;
752   return 0;
753 }
754
755 /* Local function pass handling visibilities.  This happens before LTO streaming
756    so in particular -fwhole-program should be ignored at this level.  */
757
758 namespace {
759
760 const pass_data pass_data_ipa_function_and_variable_visibility =
761 {
762   SIMPLE_IPA_PASS, /* type */
763   "visibility", /* name */
764   OPTGROUP_NONE, /* optinfo_flags */
765   TV_CGRAPHOPT, /* tv_id */
766   0, /* properties_required */
767   0, /* properties_provided */
768   0, /* properties_destroyed */
769   0, /* todo_flags_start */
770   ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
771 };
772
773 /* Bring functions local at LTO time with -fwhole-program.  */
774
775 static unsigned int
776 whole_program_function_and_variable_visibility (void)
777 {
778   function_and_variable_visibility (flag_whole_program);
779   if (optimize)
780     ipa_discover_readonly_nonaddressable_vars ();
781   return 0;
782 }
783
784 } // anon namespace
785
786 namespace {
787
788 const pass_data pass_data_ipa_whole_program_visibility =
789 {
790   IPA_PASS, /* type */
791   "whole-program", /* name */
792   OPTGROUP_NONE, /* optinfo_flags */
793   TV_CGRAPHOPT, /* tv_id */
794   0, /* properties_required */
795   0, /* properties_provided */
796   0, /* properties_destroyed */
797   0, /* todo_flags_start */
798   ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
799 };
800
801 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
802 {
803 public:
804   pass_ipa_whole_program_visibility (gcc::context *ctxt)
805     : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
806                       NULL, /* generate_summary */
807                       NULL, /* write_summary */
808                       NULL, /* read_summary */
809                       NULL, /* write_optimization_summary */
810                       NULL, /* read_optimization_summary */
811                       NULL, /* stmt_fixup */
812                       0, /* function_transform_todo_flags_start */
813                       NULL, /* function_transform */
814                       NULL) /* variable_transform */
815   {}
816
817   /* opt_pass methods: */
818
819   virtual bool gate (function *)
820     {
821       /* Do not re-run on ltrans stage.  */
822       return !flag_ltrans;
823     }
824   virtual unsigned int execute (function *)
825     {
826       return whole_program_function_and_variable_visibility ();
827     }
828
829 }; // class pass_ipa_whole_program_visibility
830
831 } // anon namespace
832
833 ipa_opt_pass_d *
834 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
835 {
836   return new pass_ipa_whole_program_visibility (ctxt);
837 }
838
839 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
840 {
841 public:
842   pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
843     : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
844                            ctxt)
845   {}
846
847   /* opt_pass methods: */
848   virtual unsigned int execute (function *)
849     {
850       return function_and_variable_visibility (flag_whole_program && !flag_lto);
851     }
852
853 }; // class pass_ipa_function_and_variable_visibility
854
855 simple_ipa_opt_pass *
856 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
857 {
858   return new pass_ipa_function_and_variable_visibility (ctxt);
859 }