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