gcc/
[official-gcc.git] / gcc / ipa-visibility.c
blob1389dd9991d7f2bcfa83f4778441608f38e8cba9
1 /* IPA visibility pass
2 Copyright (C) 2003-2015 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 "alias.h"
80 #include "symtab.h"
81 #include "tree.h"
82 #include "plugin-api.h"
83 #include "hard-reg-set.h"
84 #include "function.h"
85 #include "ipa-ref.h"
86 #include "cgraph.h"
87 #include "tree-pass.h"
88 #include "calls.h"
89 #include "gimple-expr.h"
90 #include "varasm.h"
92 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
94 static bool
95 non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
97 return !(node->only_called_directly_or_aliased_p ()
98 /* i386 would need update to output thunk with locak calling
99 ocnvetions. */
100 && !node->thunk.thunk_p
101 && node->definition
102 && !DECL_EXTERNAL (node->decl)
103 && !node->externally_visible
104 && !node->used_from_other_partition
105 && !node->in_other_partition);
108 /* Return true when function can be marked local. */
110 bool
111 cgraph_node::local_p (void)
113 cgraph_node *n = ultimate_alias_target ();
115 if (n->thunk.thunk_p)
116 return n->callees->callee->local_p ();
117 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
118 NULL, true);
122 /* A helper for comdat_can_be_unshared_p. */
124 static bool
125 comdat_can_be_unshared_p_1 (symtab_node *node)
127 if (!node->externally_visible)
128 return true;
129 if (node->address_can_be_compared_p ())
131 struct ipa_ref *ref;
133 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
134 if (ref->address_matters_p ())
135 return false;
138 /* If the symbol is used in some weird way, better to not touch it. */
139 if (node->force_output)
140 return false;
142 /* Explicit instantiations needs to be output when possibly
143 used externally. */
144 if (node->forced_by_abi
145 && TREE_PUBLIC (node->decl)
146 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
147 && !flag_whole_program))
148 return false;
150 /* Non-readonly and volatile variables can not be duplicated. */
151 if (is_a <varpool_node *> (node)
152 && (!TREE_READONLY (node->decl)
153 || TREE_THIS_VOLATILE (node->decl)))
154 return false;
155 return true;
158 /* COMDAT functions must be shared only if they have address taken,
159 otherwise we can produce our own private implementation with
160 -fwhole-program.
161 Return true when turning COMDAT functoin static can not lead to wrong
162 code when the resulting object links with a library defining same COMDAT.
164 Virtual functions do have their addresses taken from the vtables,
165 but in C++ there is no way to compare their addresses for equality. */
167 static bool
168 comdat_can_be_unshared_p (symtab_node *node)
170 if (!comdat_can_be_unshared_p_1 (node))
171 return false;
172 if (node->same_comdat_group)
174 symtab_node *next;
176 /* If more than one function is in the same COMDAT group, it must
177 be shared even if just one function in the comdat group has
178 address taken. */
179 for (next = node->same_comdat_group;
180 next != node; next = next->same_comdat_group)
181 if (!comdat_can_be_unshared_p_1 (next))
182 return false;
184 return true;
187 /* Return true when function NODE should be considered externally visible. */
189 static bool
190 cgraph_externally_visible_p (struct cgraph_node *node,
191 bool whole_program)
193 if (!node->definition)
194 return false;
195 if (!TREE_PUBLIC (node->decl)
196 || DECL_EXTERNAL (node->decl))
197 return false;
199 /* Do not try to localize built-in functions yet. One of problems is that we
200 end up mangling their asm for WHOPR that makes it impossible to call them
201 using the implicit built-in declarations anymore. Similarly this enables
202 us to remove them as unreachable before actual calls may appear during
203 expansion or folding. */
204 if (DECL_BUILT_IN (node->decl))
205 return true;
207 /* If linker counts on us, we must preserve the function. */
208 if (node->used_from_object_file_p ())
209 return true;
210 if (DECL_PRESERVE_P (node->decl))
211 return true;
212 if (lookup_attribute ("externally_visible",
213 DECL_ATTRIBUTES (node->decl)))
214 return true;
215 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
216 && lookup_attribute ("dllexport",
217 DECL_ATTRIBUTES (node->decl)))
218 return true;
219 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
220 return false;
221 /* When doing LTO or whole program, we can bring COMDAT functoins static.
222 This improves code quality and we know we will duplicate them at most twice
223 (in the case that we are not using plugin and link with object file
224 implementing same COMDAT) */
225 if ((in_lto_p || whole_program)
226 && DECL_COMDAT (node->decl)
227 && comdat_can_be_unshared_p (node))
228 return false;
230 /* When doing link time optimizations, hidden symbols become local. */
231 if (in_lto_p
232 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
233 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
234 /* Be sure that node is defined in IR file, not in other object
235 file. In that case we don't set used_from_other_object_file. */
236 && node->definition)
238 else if (!whole_program)
239 return true;
241 if (MAIN_NAME_P (DECL_NAME (node->decl)))
242 return true;
244 if (node->instrumentation_clone
245 && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
246 return true;
248 return false;
251 /* Return true when variable should be considered externally visible. */
253 bool
254 varpool_node::externally_visible_p (void)
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)
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
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 bool
334 can_replace_by_local_alias (symtab_node *node)
336 return (node->get_availability () > AVAIL_INTERPOSABLE
337 && !decl_binds_to_current_def_p (node->decl)
338 && !node->can_be_discarded_p ());
341 /* Return true if we can replace refernece to NODE by local alias
342 within a virtual table. Generally we can replace function pointers
343 and virtual table pointers. */
345 bool
346 can_replace_by_local_alias_in_vtable (symtab_node *node)
348 if (is_a <varpool_node *> (node)
349 && !DECL_VIRTUAL_P (node->decl))
350 return false;
351 return can_replace_by_local_alias (node);
354 /* walk_tree callback that rewrites initializer references. */
356 static tree
357 update_vtable_references (tree *tp, int *walk_subtrees,
358 void *data ATTRIBUTE_UNUSED)
360 if (TREE_CODE (*tp) == VAR_DECL
361 || TREE_CODE (*tp) == FUNCTION_DECL)
363 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
364 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
365 *walk_subtrees = 0;
367 else if (IS_TYPE_OR_DECL_P (*tp))
368 *walk_subtrees = 0;
369 return NULL;
372 /* In LTO we can remove COMDAT groups and weak symbols.
373 Either turn them into normal symbols or external symbol depending on
374 resolution info. */
376 static void
377 update_visibility_by_resolution_info (symtab_node * node)
379 bool define;
381 if (!node->externally_visible
382 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
383 || node->resolution == LDPR_UNKNOWN)
384 return;
386 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
387 || node->resolution == LDPR_PREVAILING_DEF
388 || node->resolution == LDPR_UNDEF
389 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
391 /* The linker decisions ought to agree in the whole group. */
392 if (node->same_comdat_group)
393 for (symtab_node *next = node->same_comdat_group;
394 next != node; next = next->same_comdat_group)
396 if (!next->externally_visible)
397 continue;
399 bool same_def
400 = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
401 || next->resolution == LDPR_PREVAILING_DEF
402 || next->resolution == LDPR_UNDEF
403 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
404 gcc_assert (in_lto_p || same_def);
405 if (!same_def)
406 return;
409 if (node->same_comdat_group)
410 for (symtab_node *next = node->same_comdat_group;
411 next != node; next = next->same_comdat_group)
413 next->set_comdat_group (NULL);
414 DECL_WEAK (next->decl) = false;
415 if (next->externally_visible
416 && !define)
417 DECL_EXTERNAL (next->decl) = true;
419 node->set_comdat_group (NULL);
420 DECL_WEAK (node->decl) = false;
421 if (!define)
422 DECL_EXTERNAL (node->decl) = true;
423 node->dissolve_same_comdat_group_list ();
426 /* Decide on visibility of all symbols. */
428 static unsigned int
429 function_and_variable_visibility (bool whole_program)
431 struct cgraph_node *node;
432 varpool_node *vnode;
434 /* All aliases should be procssed at this point. */
435 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
437 FOR_EACH_FUNCTION (node)
439 int flags = flags_from_decl_or_type (node->decl);
441 /* Optimize away PURE and CONST constructors and destructors. */
442 if (optimize
443 && (flags & (ECF_CONST | ECF_PURE))
444 && !(flags & ECF_LOOPING_CONST_OR_PURE))
446 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
447 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
450 /* Frontends and alias code marks nodes as needed before parsing is finished.
451 We may end up marking as node external nodes where this flag is meaningless
452 strip it. */
453 if (DECL_EXTERNAL (node->decl) || !node->definition)
455 node->force_output = 0;
456 node->forced_by_abi = 0;
459 /* C++ FE on lack of COMDAT support create local COMDAT functions
460 (that ought to be shared but can not due to object format
461 limitations). It is necessary to keep the flag to make rest of C++ FE
462 happy. Clear the flag here to avoid confusion in middle-end. */
463 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
464 DECL_COMDAT (node->decl) = 0;
466 /* For external decls stop tracking same_comdat_group. It doesn't matter
467 what comdat group they are in when they won't be emitted in this TU. */
468 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
470 #ifdef ENABLE_CHECKING
471 symtab_node *n;
473 for (n = node->same_comdat_group;
474 n != node;
475 n = n->same_comdat_group)
476 /* If at least one of same comdat group functions is external,
477 all of them have to be, otherwise it is a front-end bug. */
478 gcc_assert (DECL_EXTERNAL (n->decl));
479 #endif
480 node->dissolve_same_comdat_group_list ();
482 gcc_assert ((!DECL_WEAK (node->decl)
483 && !DECL_COMDAT (node->decl))
484 || TREE_PUBLIC (node->decl)
485 || node->weakref
486 || DECL_EXTERNAL (node->decl));
487 if (cgraph_externally_visible_p (node, whole_program))
489 gcc_assert (!node->global.inlined_to);
490 node->externally_visible = true;
492 else
494 node->externally_visible = false;
495 node->forced_by_abi = false;
497 if (!node->externally_visible
498 && node->definition && !node->weakref
499 && !DECL_EXTERNAL (node->decl))
501 gcc_assert (whole_program || in_lto_p
502 || !TREE_PUBLIC (node->decl));
503 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
504 || node->unique_name
505 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
506 && TREE_PUBLIC (node->decl));
507 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
508 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
510 symtab_node *next = node;
512 /* Set all members of comdat group local. */
513 if (node->same_comdat_group)
514 for (next = node->same_comdat_group;
515 next != node;
516 next = next->same_comdat_group)
518 next->set_comdat_group (NULL);
519 if (!next->alias)
520 next->set_section (NULL);
521 next->make_decl_local ();
522 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
523 || next->unique_name
524 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
525 && TREE_PUBLIC (next->decl));
527 /* cgraph_externally_visible_p has already checked all other nodes
528 in the group and they will all be made local. We need to
529 dissolve the group at once so that the predicate does not
530 segfault though. */
531 node->dissolve_same_comdat_group_list ();
533 if (TREE_PUBLIC (node->decl))
534 node->set_comdat_group (NULL);
535 if (DECL_COMDAT (node->decl) && !node->alias)
536 node->set_section (NULL);
537 node->make_decl_local ();
540 if (node->thunk.thunk_p
541 && !node->thunk.add_pointer_bounds_args
542 && TREE_PUBLIC (node->decl))
544 struct cgraph_node *decl_node = node;
546 decl_node = decl_node->callees->callee->function_symbol ();
548 /* Thunks have the same visibility as function they are attached to.
549 Make sure the C++ front end set this up properly. */
550 if (DECL_ONE_ONLY (decl_node->decl))
552 gcc_checking_assert (DECL_COMDAT (node->decl)
553 == DECL_COMDAT (decl_node->decl));
554 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
555 gcc_checking_assert (node->same_comdat_group);
557 node->forced_by_abi = decl_node->forced_by_abi;
558 if (DECL_EXTERNAL (decl_node->decl))
559 DECL_EXTERNAL (node->decl) = 1;
562 update_visibility_by_resolution_info (node);
564 FOR_EACH_DEFINED_FUNCTION (node)
566 if (!node->local.local)
567 node->local.local |= node->local_p ();
569 /* If we know that function can not be overwritten by a different semantics
570 and moreover its section can not be discarded, replace all direct calls
571 by calls to an noninterposable alias. This make dynamic linking
572 cheaper and enable more optimization.
574 TODO: We can also update virtual tables. */
575 if (node->callers
576 && can_replace_by_local_alias (node))
578 cgraph_node *alias = dyn_cast<cgraph_node *>
579 (node->noninterposable_alias ());
581 if (alias && alias != node)
583 while (node->callers)
585 struct cgraph_edge *e = node->callers;
587 e->redirect_callee (alias);
588 if (gimple_has_body_p (e->caller->decl))
590 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
591 e->redirect_call_stmt_to_callee ();
592 pop_cfun ();
598 FOR_EACH_VARIABLE (vnode)
600 /* weak flag makes no sense on local variables. */
601 gcc_assert (!DECL_WEAK (vnode->decl)
602 || vnode->weakref
603 || TREE_PUBLIC (vnode->decl)
604 || DECL_EXTERNAL (vnode->decl));
605 /* In several cases declarations can not be common:
607 - when declaration has initializer
608 - when it is in weak
609 - when it has specific section
610 - when it resides in non-generic address space.
611 - if declaration is local, it will get into .local common section
612 so common flag is not needed. Frontends still produce these in
613 certain cases, such as for:
615 static int a __attribute__ ((common))
617 Canonicalize things here and clear the redundant flag. */
618 if (DECL_COMMON (vnode->decl)
619 && (!(TREE_PUBLIC (vnode->decl)
620 || DECL_EXTERNAL (vnode->decl))
621 || (DECL_INITIAL (vnode->decl)
622 && DECL_INITIAL (vnode->decl) != error_mark_node)
623 || DECL_WEAK (vnode->decl)
624 || DECL_SECTION_NAME (vnode->decl) != NULL
625 || ! (ADDR_SPACE_GENERIC_P
626 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
627 DECL_COMMON (vnode->decl) = 0;
629 FOR_EACH_DEFINED_VARIABLE (vnode)
631 if (!vnode->definition)
632 continue;
633 if (vnode->externally_visible_p ())
634 vnode->externally_visible = true;
635 else
637 vnode->externally_visible = false;
638 vnode->forced_by_abi = false;
640 if (lookup_attribute ("no_reorder",
641 DECL_ATTRIBUTES (vnode->decl)))
642 vnode->no_reorder = 1;
643 if (!vnode->externally_visible
644 && !vnode->weakref)
646 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
647 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
648 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
649 && TREE_PUBLIC (vnode->decl));
650 if (vnode->same_comdat_group && TREE_PUBLIC (vnode->decl))
652 symtab_node *next = vnode;
654 /* Set all members of comdat group local. */
655 if (vnode->same_comdat_group)
656 for (next = vnode->same_comdat_group;
657 next != vnode;
658 next = next->same_comdat_group)
660 next->set_comdat_group (NULL);
661 if (!next->alias)
662 next->set_section (NULL);
663 next->make_decl_local ();
664 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
665 || next->unique_name
666 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
667 && TREE_PUBLIC (next->decl));
669 vnode->dissolve_same_comdat_group_list ();
671 if (TREE_PUBLIC (vnode->decl))
672 vnode->set_comdat_group (NULL);
673 if (DECL_COMDAT (vnode->decl) && !vnode->alias)
674 vnode->set_section (NULL);
675 vnode->make_decl_local ();
676 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
678 update_visibility_by_resolution_info (vnode);
680 /* Update virtual tables to point to local aliases where possible. */
681 if (DECL_VIRTUAL_P (vnode->decl)
682 && !DECL_EXTERNAL (vnode->decl))
684 int i;
685 struct ipa_ref *ref;
686 bool found = false;
688 /* See if there is something to update. */
689 for (i = 0; vnode->iterate_referring (i, ref); i++)
690 if (ref->use == IPA_REF_ADDR
691 && can_replace_by_local_alias_in_vtable (ref->referred))
693 found = true;
694 break;
696 if (found)
698 hash_set<tree> visited_nodes;
700 vnode->get_constructor ();
701 walk_tree (&DECL_INITIAL (vnode->decl),
702 update_vtable_references, NULL, &visited_nodes);
703 vnode->remove_all_references ();
704 record_references_in_initializer (vnode->decl, false);
709 if (dump_file)
711 fprintf (dump_file, "\nMarking local functions:");
712 FOR_EACH_DEFINED_FUNCTION (node)
713 if (node->local.local)
714 fprintf (dump_file, " %s", node->name ());
715 fprintf (dump_file, "\n\n");
716 fprintf (dump_file, "\nMarking externally visible functions:");
717 FOR_EACH_DEFINED_FUNCTION (node)
718 if (node->externally_visible)
719 fprintf (dump_file, " %s", node->name ());
720 fprintf (dump_file, "\n\n");
721 fprintf (dump_file, "\nMarking externally visible variables:");
722 FOR_EACH_DEFINED_VARIABLE (vnode)
723 if (vnode->externally_visible)
724 fprintf (dump_file, " %s", vnode->name ());
725 fprintf (dump_file, "\n\n");
727 symtab->function_flags_ready = true;
728 return 0;
731 /* Local function pass handling visibilities. This happens before LTO streaming
732 so in particular -fwhole-program should be ignored at this level. */
734 namespace {
736 const pass_data pass_data_ipa_function_and_variable_visibility =
738 SIMPLE_IPA_PASS, /* type */
739 "visibility", /* name */
740 OPTGROUP_NONE, /* optinfo_flags */
741 TV_CGRAPHOPT, /* tv_id */
742 0, /* properties_required */
743 0, /* properties_provided */
744 0, /* properties_destroyed */
745 0, /* todo_flags_start */
746 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
749 /* Bring functions local at LTO time with -fwhole-program. */
751 static unsigned int
752 whole_program_function_and_variable_visibility (void)
754 function_and_variable_visibility (flag_whole_program);
755 if (optimize)
756 ipa_discover_readonly_nonaddressable_vars ();
757 return 0;
760 } // anon namespace
762 namespace {
764 const pass_data pass_data_ipa_whole_program_visibility =
766 IPA_PASS, /* type */
767 "whole-program", /* name */
768 OPTGROUP_NONE, /* optinfo_flags */
769 TV_CGRAPHOPT, /* tv_id */
770 0, /* properties_required */
771 0, /* properties_provided */
772 0, /* properties_destroyed */
773 0, /* todo_flags_start */
774 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
777 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
779 public:
780 pass_ipa_whole_program_visibility (gcc::context *ctxt)
781 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
782 NULL, /* generate_summary */
783 NULL, /* write_summary */
784 NULL, /* read_summary */
785 NULL, /* write_optimization_summary */
786 NULL, /* read_optimization_summary */
787 NULL, /* stmt_fixup */
788 0, /* function_transform_todo_flags_start */
789 NULL, /* function_transform */
790 NULL) /* variable_transform */
793 /* opt_pass methods: */
795 virtual bool gate (function *)
797 /* Do not re-run on ltrans stage. */
798 return !flag_ltrans;
800 virtual unsigned int execute (function *)
802 return whole_program_function_and_variable_visibility ();
805 }; // class pass_ipa_whole_program_visibility
807 } // anon namespace
809 ipa_opt_pass_d *
810 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
812 return new pass_ipa_whole_program_visibility (ctxt);
815 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
817 public:
818 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
819 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
820 ctxt)
823 /* opt_pass methods: */
824 virtual unsigned int execute (function *)
826 return function_and_variable_visibility (flag_whole_program && !flag_lto);
829 }; // class pass_ipa_function_and_variable_visibility
831 simple_ipa_opt_pass *
832 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
834 return new pass_ipa_function_and_variable_visibility (ctxt);