2016-09-25 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-visibility.c
blobe4c3f7c51109895a1a46df411c44c2d941dda5f1
1 /* IPA visibility pass
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file implements two related passes:
22 - pass_data_ipa_function_and_variable_visibility run just after
23 symbol table, references and callgraph are built
25 - pass_data_ipa_function_and_variable_visibility run as first
26 proper IPA pass (that is after early optimization, or, (with LTO)
27 as a first pass done at link-time.
29 Purpose of both passes is to set correctly visibility properties
30 of all symbols. This includes:
32 - Symbol privatization:
34 Some symbols that are declared public by frontend may be
35 turned local (either by -fwhole-program flag, by linker plugin feedback
36 or by other reasons)
38 - Discovery of local functions:
40 A local function is one whose calls can occur only in the current
41 compilation unit and all its calls are explicit, so we can change
42 its calling convention. We simply mark all static functions whose
43 address is not taken as local.
45 externally_visible flag is set for symbols that can not be privatized.
46 For privatized symbols we clear TREE_PUBLIC flag and dismantle comdat
47 group.
49 - Dismantling of comdat groups:
51 Comdat group represent a section that may be replaced by linker by
52 a different copy of the same section from other unit.
53 If we have resolution information (from linker plugin) and we know that
54 a given comdat gorup is prevailing, we can dismantle it and turn symbols
55 into normal symbols. If the resolution information says that the
56 section was previaled by copy from non-LTO code, we can also dismantle
57 it and turn all symbols into external.
59 - Local aliases:
61 Some symbols can be interposed by dynamic linker. Refering to these
62 symbols is expensive, since it needs to be overwritable by the dynamic
63 linker. In some cases we know that the interposition does not change
64 semantic and we can always refer to a local copy (as in the case of
65 inline function). In this case we produce a local alias and redirect
66 calls to it.
68 TODO: This should be done for references, too.
70 - Removal of static ocnstructors and destructors that have no side effects.
72 - Regularization of several oddities introduced by frontends that may
73 be impractical later in the optimization queue. */
75 #include "config.h"
76 #include "system.h"
77 #include "coretypes.h"
78 #include "tm.h"
79 #include "function.h"
80 #include "tree.h"
81 #include "gimple-expr.h"
82 #include "tree-pass.h"
83 #include "cgraph.h"
84 #include "calls.h"
85 #include "varasm.h"
87 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
89 static bool
90 non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
92 return !(node->only_called_directly_or_aliased_p ()
93 /* i386 would need update to output thunk with locak calling
94 ocnvetions. */
95 && !node->thunk.thunk_p
96 && node->definition
97 && !DECL_EXTERNAL (node->decl)
98 && !node->externally_visible
99 && !node->used_from_other_partition
100 && !node->in_other_partition);
103 /* Return true when function can be marked local. */
105 bool
106 cgraph_node::local_p (void)
108 cgraph_node *n = ultimate_alias_target ();
110 if (n->thunk.thunk_p)
111 return n->callees->callee->local_p ();
112 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
113 NULL, true);
117 /* A helper for comdat_can_be_unshared_p. */
119 static bool
120 comdat_can_be_unshared_p_1 (symtab_node *node)
122 if (!node->externally_visible)
123 return true;
124 if (node->address_can_be_compared_p ())
126 struct ipa_ref *ref;
128 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
129 if (ref->address_matters_p ())
130 return false;
133 /* If the symbol is used in some weird way, better to not touch it. */
134 if (node->force_output)
135 return false;
137 /* Explicit instantiations needs to be output when possibly
138 used externally. */
139 if (node->forced_by_abi
140 && TREE_PUBLIC (node->decl)
141 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
142 && !flag_whole_program))
143 return false;
145 /* Non-readonly and volatile variables can not be duplicated. */
146 if (is_a <varpool_node *> (node)
147 && (!TREE_READONLY (node->decl)
148 || TREE_THIS_VOLATILE (node->decl)))
149 return false;
150 return true;
153 /* COMDAT functions must be shared only if they have address taken,
154 otherwise we can produce our own private implementation with
155 -fwhole-program.
156 Return true when turning COMDAT functoin static can not lead to wrong
157 code when the resulting object links with a library defining same COMDAT.
159 Virtual functions do have their addresses taken from the vtables,
160 but in C++ there is no way to compare their addresses for equality. */
162 static bool
163 comdat_can_be_unshared_p (symtab_node *node)
165 if (!comdat_can_be_unshared_p_1 (node))
166 return false;
167 if (node->same_comdat_group)
169 symtab_node *next;
171 /* If more than one function is in the same COMDAT group, it must
172 be shared even if just one function in the comdat group has
173 address taken. */
174 for (next = node->same_comdat_group;
175 next != node; next = next->same_comdat_group)
176 if (!comdat_can_be_unshared_p_1 (next))
177 return false;
179 return true;
182 /* Return true when function NODE should be considered externally visible. */
184 static bool
185 cgraph_externally_visible_p (struct cgraph_node *node,
186 bool whole_program)
188 while (node->transparent_alias && node->definition)
189 node = node->get_alias_target ();
190 if (!node->definition)
191 return false;
192 if (!TREE_PUBLIC (node->decl)
193 || DECL_EXTERNAL (node->decl))
194 return false;
196 /* Do not try to localize built-in functions yet. One of problems is that we
197 end up mangling their asm for WHOPR that makes it impossible to call them
198 using the implicit built-in declarations anymore. Similarly this enables
199 us to remove them as unreachable before actual calls may appear during
200 expansion or folding. */
201 if (DECL_BUILT_IN (node->decl))
202 return true;
204 /* If linker counts on us, we must preserve the function. */
205 if (node->used_from_object_file_p ())
206 return true;
207 if (DECL_PRESERVE_P (node->decl))
208 return true;
209 if (lookup_attribute ("externally_visible",
210 DECL_ATTRIBUTES (node->decl)))
211 return true;
212 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
213 && lookup_attribute ("dllexport",
214 DECL_ATTRIBUTES (node->decl)))
215 return true;
216 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
217 return false;
218 /* When doing LTO or whole program, we can bring COMDAT functoins static.
219 This improves code quality and we know we will duplicate them at most twice
220 (in the case that we are not using plugin and link with object file
221 implementing same COMDAT) */
222 if (((in_lto_p || whole_program) && !flag_incremental_link)
223 && DECL_COMDAT (node->decl)
224 && comdat_can_be_unshared_p (node))
225 return false;
227 /* When doing link time optimizations, hidden symbols become local. */
228 if ((in_lto_p && !flag_incremental_link)
229 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
230 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
231 /* Be sure that node is defined in IR file, not in other object
232 file. In that case we don't set used_from_other_object_file. */
233 && node->definition)
235 else if (!whole_program)
236 return true;
238 if (MAIN_NAME_P (DECL_NAME (node->decl)))
239 return true;
241 if (node->instrumentation_clone
242 && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
243 return true;
245 return false;
248 /* Return true when variable should be considered externally visible. */
250 bool
251 varpool_node::externally_visible_p (void)
253 while (transparent_alias && definition)
254 return get_alias_target ()->externally_visible_p ();
255 if (DECL_EXTERNAL (decl))
256 return true;
258 if (!TREE_PUBLIC (decl))
259 return false;
261 /* If linker counts on us, we must preserve the function. */
262 if (used_from_object_file_p ())
263 return true;
265 /* Bringing TLS variables local may cause dynamic linker failures
266 on limits of static TLS vars. */
267 if (DECL_THREAD_LOCAL_P (decl)
268 && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
269 && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
270 return true;
272 if (DECL_HARD_REGISTER (decl))
273 return true;
274 if (DECL_PRESERVE_P (decl))
275 return true;
276 if (lookup_attribute ("externally_visible",
277 DECL_ATTRIBUTES (decl)))
278 return true;
279 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
280 && lookup_attribute ("dllexport",
281 DECL_ATTRIBUTES (decl)))
282 return true;
284 /* See if we have linker information about symbol not being used or
285 if we need to make guess based on the declaration.
287 Even if the linker clams the symbol is unused, never bring internal
288 symbols that are declared by user as used or externally visible.
289 This is needed for i.e. references from asm statements. */
290 if (used_from_object_file_p ())
291 return true;
292 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
293 return false;
295 /* As a special case, the COMDAT virtual tables can be unshared.
296 In LTO mode turn vtables into static variables. The variable is readonly,
297 so this does not enable more optimization, but referring static var
298 is faster for dynamic linking. Also this match logic hidding vtables
299 from LTO symbol tables. */
300 if (((in_lto_p || flag_whole_program) && !flag_incremental_link)
301 && DECL_COMDAT (decl)
302 && comdat_can_be_unshared_p (this))
303 return false;
305 /* When doing link time optimizations, hidden symbols become local. */
306 if (in_lto_p && !flag_incremental_link
307 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
308 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
309 /* Be sure that node is defined in IR file, not in other object
310 file. In that case we don't set used_from_other_object_file. */
311 && definition)
313 else if (!flag_whole_program)
314 return true;
316 /* Do not attempt to privatize COMDATS by default.
317 This would break linking with C++ libraries sharing
318 inline definitions.
320 FIXME: We can do so for readonly vars with no address taken and
321 possibly also for vtables since no direct pointer comparsion is done.
322 It might be interesting to do so to reduce linking overhead. */
323 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
324 return true;
325 return false;
328 /* Return true if reference to NODE can be replaced by a local alias.
329 Local aliases save dynamic linking overhead and enable more optimizations.
332 static bool
333 can_replace_by_local_alias (symtab_node *node)
335 #ifndef ASM_OUTPUT_DEF
336 /* If aliases aren't supported, we can't do replacement. */
337 return false;
338 #endif
339 /* Weakrefs have a reason to be non-local. Be sure we do not replace
340 them. */
341 while (node->transparent_alias && node->definition && !node->weakref)
342 node = node->get_alias_target ();
343 if (node->weakref)
344 return false;
346 return (node->get_availability () > AVAIL_INTERPOSABLE
347 && !decl_binds_to_current_def_p (node->decl)
348 && !node->can_be_discarded_p ());
351 /* Return true if we can replace reference to NODE by local alias
352 within a virtual table. Generally we can replace function pointers
353 and virtual table pointers. */
355 static bool
356 can_replace_by_local_alias_in_vtable (symtab_node *node)
358 if (is_a <varpool_node *> (node)
359 && !DECL_VIRTUAL_P (node->decl))
360 return false;
361 return can_replace_by_local_alias (node);
364 /* walk_tree callback that rewrites initializer references. */
366 static tree
367 update_vtable_references (tree *tp, int *walk_subtrees,
368 void *data ATTRIBUTE_UNUSED)
370 if (TREE_CODE (*tp) == VAR_DECL
371 || TREE_CODE (*tp) == FUNCTION_DECL)
373 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
374 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
375 *walk_subtrees = 0;
377 else if (IS_TYPE_OR_DECL_P (*tp))
378 *walk_subtrees = 0;
379 return NULL;
382 /* In LTO we can remove COMDAT groups and weak symbols.
383 Either turn them into normal symbols or external symbol depending on
384 resolution info. */
386 static void
387 update_visibility_by_resolution_info (symtab_node * node)
389 bool define;
391 if (!node->externally_visible
392 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
393 || node->resolution == LDPR_UNKNOWN)
394 return;
396 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
397 || node->resolution == LDPR_PREVAILING_DEF
398 || node->resolution == LDPR_UNDEF
399 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
401 /* The linker decisions ought to agree in the whole group. */
402 if (node->same_comdat_group)
403 for (symtab_node *next = node->same_comdat_group;
404 next != node; next = next->same_comdat_group)
406 if (!next->externally_visible || next->transparent_alias)
407 continue;
409 bool same_def
410 = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
411 || next->resolution == LDPR_PREVAILING_DEF
412 || next->resolution == LDPR_UNDEF
413 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
414 gcc_assert (in_lto_p || same_def);
415 if (!same_def)
416 return;
419 if (node->same_comdat_group)
420 for (symtab_node *next = node->same_comdat_group;
421 next != node; next = next->same_comdat_group)
423 /* During incremental linking we need to keep symbol weak for future
424 linking. We can still drop definition if we know non-LTO world
425 prevails. */
426 if (!flag_incremental_link)
428 DECL_WEAK (next->decl) = false;
429 next->set_comdat_group (NULL);
431 if (!define)
433 if (next->externally_visible)
434 DECL_EXTERNAL (next->decl) = true;
435 next->set_comdat_group (NULL);
439 /* During incremental linking we need to keep symbol weak for future
440 linking. We can still drop definition if we know non-LTO world prevails. */
441 if (!flag_incremental_link)
443 DECL_WEAK (node->decl) = false;
444 node->set_comdat_group (NULL);
445 node->dissolve_same_comdat_group_list ();
447 if (!define)
449 DECL_EXTERNAL (node->decl) = true;
450 node->set_comdat_group (NULL);
451 node->dissolve_same_comdat_group_list ();
455 /* Try to get rid of weakref. */
457 static void
458 optimize_weakref (symtab_node *node)
460 #ifdef ASM_OUTPUT_DEF
461 bool aliases_supported = true;
462 #else
463 bool aliases_supported = false;
464 #endif
465 bool strip_weakref = false;
466 bool static_alias = false;
468 gcc_assert (node->weakref);
470 /* Weakrefs with no target defined can not be optimized. */
471 if (!node->analyzed)
472 return;
473 symtab_node *target = node->get_alias_target ();
475 /* Weakrefs to weakrefs can be optimized only if target can be. */
476 if (target->weakref)
477 optimize_weakref (target);
478 if (target->weakref)
479 return;
481 /* If we have definition of weakref's target and we know it binds locally,
482 we can turn weakref to static alias. */
483 if (target->definition && decl_binds_to_current_def_p (target->decl)
484 && aliases_supported)
485 strip_weakref = static_alias = true;
486 /* Otherwise we can turn weakref into transparent alias. This transformation
487 may break asm statements which directly refers to symbol name and expect
488 GNU as to translate it via .weakref directive. So do not optimize when
489 DECL_PRESERVED is set and .weakref is supported. */
490 else if ((!DECL_PRESERVE_P (target->decl)
491 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)))
492 && !DECL_WEAK (target->decl)
493 && !DECL_EXTERNAL (target->decl)
494 && ((target->definition && !target->can_be_discarded_p ())
495 || target->resolution != LDPR_UNDEF))
496 strip_weakref = true;
497 if (!strip_weakref)
498 return;
499 node->weakref = false;
500 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)) = 0;
501 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) = NULL_TREE;
502 DECL_ATTRIBUTES (node->decl) = remove_attribute ("weakref",
503 DECL_ATTRIBUTES
504 (node->decl));
506 if (dump_file)
507 fprintf (dump_file, "Optimizing weakref %s %s\n",
508 node->name(),
509 static_alias ? "as static alias" : "as transparent alias");
511 if (static_alias)
513 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
514 be sure it really clears the WEAK flag. */
515 TREE_PUBLIC (node->decl) = true;
516 node->make_decl_local ();
517 node->forced_by_abi = false;
518 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
519 node->externally_visible = false;
520 gcc_assert (!DECL_WEAK (node->decl));
521 node->transparent_alias = false;
523 else
525 symtab->change_decl_assembler_name
526 (node->decl, DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
527 node->transparent_alias = true;
528 node->copy_visibility_from (target);
530 gcc_assert (node->alias);
533 /* Decide on visibility of all symbols. */
535 static unsigned int
536 function_and_variable_visibility (bool whole_program)
538 struct cgraph_node *node;
539 varpool_node *vnode;
541 /* All aliases should be procssed at this point. */
542 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
544 FOR_EACH_FUNCTION (node)
546 int flags = flags_from_decl_or_type (node->decl);
548 /* Optimize away PURE and CONST constructors and destructors. */
549 if (optimize
550 && (flags & (ECF_CONST | ECF_PURE))
551 && !(flags & ECF_LOOPING_CONST_OR_PURE))
553 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
554 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
557 /* Frontends and alias code marks nodes as needed before parsing is finished.
558 We may end up marking as node external nodes where this flag is meaningless
559 strip it. */
560 if (DECL_EXTERNAL (node->decl) || !node->definition)
562 node->force_output = 0;
563 node->forced_by_abi = 0;
566 /* C++ FE on lack of COMDAT support create local COMDAT functions
567 (that ought to be shared but can not due to object format
568 limitations). It is necessary to keep the flag to make rest of C++ FE
569 happy. Clear the flag here to avoid confusion in middle-end. */
570 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
571 DECL_COMDAT (node->decl) = 0;
573 /* For external decls stop tracking same_comdat_group. It doesn't matter
574 what comdat group they are in when they won't be emitted in this TU.
576 An exception is LTO where we may end up with both external
577 and non-external declarations in the same comdat group in
578 the case declarations was not merged. */
579 if (node->same_comdat_group && DECL_EXTERNAL (node->decl) && !in_lto_p)
581 if (flag_checking)
583 for (symtab_node *n = node->same_comdat_group;
584 n != node;
585 n = n->same_comdat_group)
586 /* If at least one of same comdat group functions is external,
587 all of them have to be, otherwise it is a front-end bug. */
588 gcc_assert (DECL_EXTERNAL (n->decl));
590 node->dissolve_same_comdat_group_list ();
592 gcc_assert ((!DECL_WEAK (node->decl)
593 && !DECL_COMDAT (node->decl))
594 || TREE_PUBLIC (node->decl)
595 || node->weakref
596 || DECL_EXTERNAL (node->decl));
597 if (cgraph_externally_visible_p (node, whole_program))
599 gcc_assert (!node->global.inlined_to);
600 node->externally_visible = true;
602 else
604 node->externally_visible = false;
605 node->forced_by_abi = false;
607 if (!node->externally_visible
608 && node->definition && !node->weakref
609 && !DECL_EXTERNAL (node->decl))
611 gcc_assert (whole_program || in_lto_p
612 || !TREE_PUBLIC (node->decl));
613 node->unique_name |= ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
614 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
615 && TREE_PUBLIC (node->decl)
616 && !flag_incremental_link);
617 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
618 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
620 symtab_node *next = node;
622 /* Set all members of comdat group local. */
623 if (node->same_comdat_group)
624 for (next = node->same_comdat_group;
625 next != node;
626 next = next->same_comdat_group)
628 next->set_comdat_group (NULL);
629 if (!next->alias)
630 next->set_section (NULL);
631 if (!next->transparent_alias)
632 next->make_decl_local ();
633 next->unique_name |= ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
634 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
635 && TREE_PUBLIC (next->decl)
636 && !flag_incremental_link);
638 /* cgraph_externally_visible_p has already checked all other nodes
639 in the group and they will all be made local. We need to
640 dissolve the group at once so that the predicate does not
641 segfault though. */
642 node->dissolve_same_comdat_group_list ();
644 if (TREE_PUBLIC (node->decl))
645 node->set_comdat_group (NULL);
646 if (DECL_COMDAT (node->decl) && !node->alias)
647 node->set_section (NULL);
648 if (!node->transparent_alias)
649 node->make_decl_local ();
652 if (node->thunk.thunk_p
653 && !node->thunk.add_pointer_bounds_args
654 && TREE_PUBLIC (node->decl))
656 struct cgraph_node *decl_node = node;
658 decl_node = decl_node->callees->callee->function_symbol ();
660 /* Thunks have the same visibility as function they are attached to.
661 Make sure the C++ front end set this up properly. */
662 if (DECL_ONE_ONLY (decl_node->decl))
664 gcc_checking_assert (DECL_COMDAT (node->decl)
665 == DECL_COMDAT (decl_node->decl));
666 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
667 gcc_checking_assert (node->same_comdat_group);
669 node->forced_by_abi = decl_node->forced_by_abi;
670 if (DECL_EXTERNAL (decl_node->decl))
671 DECL_EXTERNAL (node->decl) = 1;
674 update_visibility_by_resolution_info (node);
675 if (node->weakref)
676 optimize_weakref (node);
678 FOR_EACH_DEFINED_FUNCTION (node)
680 if (!node->local.local)
681 node->local.local |= node->local_p ();
683 /* If we know that function can not be overwritten by a
684 different semantics and moreover its section can not be
685 discarded, replace all direct calls by calls to an
686 noninterposable alias. This make dynamic linking cheaper and
687 enable more optimization.
689 TODO: We can also update virtual tables. */
690 if (node->callers
691 && can_replace_by_local_alias (node))
693 cgraph_node *alias = dyn_cast<cgraph_node *>
694 (node->noninterposable_alias ());
696 if (alias && alias != node)
698 while (node->callers)
700 struct cgraph_edge *e = node->callers;
702 e->redirect_callee (alias);
703 if (gimple_has_body_p (e->caller->decl))
705 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
706 e->redirect_call_stmt_to_callee ();
707 pop_cfun ();
713 FOR_EACH_VARIABLE (vnode)
715 /* weak flag makes no sense on local variables. */
716 gcc_assert (!DECL_WEAK (vnode->decl)
717 || vnode->weakref
718 || TREE_PUBLIC (vnode->decl)
719 || DECL_EXTERNAL (vnode->decl));
720 /* In several cases declarations can not be common:
722 - when declaration has initializer
723 - when it is in weak
724 - when it has specific section
725 - when it resides in non-generic address space.
726 - if declaration is local, it will get into .local common section
727 so common flag is not needed. Frontends still produce these in
728 certain cases, such as for:
730 static int a __attribute__ ((common))
732 Canonicalize things here and clear the redundant flag. */
733 if (DECL_COMMON (vnode->decl)
734 && (!(TREE_PUBLIC (vnode->decl)
735 || DECL_EXTERNAL (vnode->decl))
736 || (DECL_INITIAL (vnode->decl)
737 && DECL_INITIAL (vnode->decl) != error_mark_node)
738 || DECL_WEAK (vnode->decl)
739 || DECL_SECTION_NAME (vnode->decl) != NULL
740 || ! (ADDR_SPACE_GENERIC_P
741 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
742 DECL_COMMON (vnode->decl) = 0;
743 if (vnode->weakref)
744 optimize_weakref (vnode);
746 FOR_EACH_DEFINED_VARIABLE (vnode)
748 if (!vnode->definition)
749 continue;
750 if (vnode->externally_visible_p ())
751 vnode->externally_visible = true;
752 else
754 vnode->externally_visible = false;
755 vnode->forced_by_abi = false;
757 if (lookup_attribute ("no_reorder",
758 DECL_ATTRIBUTES (vnode->decl)))
759 vnode->no_reorder = 1;
760 if (!vnode->externally_visible
761 && !vnode->transparent_alias)
763 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
764 vnode->unique_name |= ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
765 || vnode->resolution
766 == LDPR_PREVAILING_DEF_IRONLY_EXP)
767 && TREE_PUBLIC (vnode->decl)
768 && !flag_incremental_link);
769 if (vnode->same_comdat_group && TREE_PUBLIC (vnode->decl))
771 symtab_node *next = vnode;
773 /* Set all members of comdat group local. */
774 if (vnode->same_comdat_group)
775 for (next = vnode->same_comdat_group;
776 next != vnode;
777 next = next->same_comdat_group)
779 next->set_comdat_group (NULL);
780 if (!next->alias)
781 next->set_section (NULL);
782 if (!next->transparent_alias)
784 next->make_decl_local ();
785 next->unique_name |= ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
786 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
787 && TREE_PUBLIC (next->decl)
788 && !flag_incremental_link);
791 vnode->dissolve_same_comdat_group_list ();
793 if (TREE_PUBLIC (vnode->decl))
794 vnode->set_comdat_group (NULL);
795 if (DECL_COMDAT (vnode->decl) && !vnode->alias)
796 vnode->set_section (NULL);
797 if (!vnode->transparent_alias)
799 vnode->make_decl_local ();
800 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
803 update_visibility_by_resolution_info (vnode);
805 /* Update virtual tables to point to local aliases where possible. */
806 if (DECL_VIRTUAL_P (vnode->decl)
807 && !DECL_EXTERNAL (vnode->decl))
809 int i;
810 struct ipa_ref *ref;
811 bool found = false;
813 /* See if there is something to update. */
814 for (i = 0; vnode->iterate_reference (i, ref); i++)
815 if (ref->use == IPA_REF_ADDR
816 && can_replace_by_local_alias_in_vtable (ref->referred))
818 found = true;
819 break;
821 if (found)
823 hash_set<tree> visited_nodes;
825 vnode->get_constructor ();
826 walk_tree (&DECL_INITIAL (vnode->decl),
827 update_vtable_references, NULL, &visited_nodes);
828 vnode->remove_all_references ();
829 record_references_in_initializer (vnode->decl, false);
834 if (dump_file)
836 fprintf (dump_file, "\nMarking local functions:");
837 FOR_EACH_DEFINED_FUNCTION (node)
838 if (node->local.local)
839 fprintf (dump_file, " %s", node->name ());
840 fprintf (dump_file, "\n\n");
841 fprintf (dump_file, "\nMarking externally visible functions:");
842 FOR_EACH_DEFINED_FUNCTION (node)
843 if (node->externally_visible)
844 fprintf (dump_file, " %s", node->name ());
845 fprintf (dump_file, "\n\n");
846 fprintf (dump_file, "\nMarking externally visible variables:");
847 FOR_EACH_DEFINED_VARIABLE (vnode)
848 if (vnode->externally_visible)
849 fprintf (dump_file, " %s", vnode->name ());
850 fprintf (dump_file, "\n\n");
852 symtab->function_flags_ready = true;
853 return 0;
856 /* Local function pass handling visibilities. This happens before LTO streaming
857 so in particular -fwhole-program should be ignored at this level. */
859 namespace {
861 const pass_data pass_data_ipa_function_and_variable_visibility =
863 SIMPLE_IPA_PASS, /* type */
864 "visibility", /* name */
865 OPTGROUP_NONE, /* optinfo_flags */
866 TV_CGRAPHOPT, /* tv_id */
867 0, /* properties_required */
868 0, /* properties_provided */
869 0, /* properties_destroyed */
870 0, /* todo_flags_start */
871 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
874 /* Bring functions local at LTO time with -fwhole-program. */
876 static unsigned int
877 whole_program_function_and_variable_visibility (void)
879 function_and_variable_visibility (flag_whole_program);
880 if (optimize)
881 ipa_discover_readonly_nonaddressable_vars ();
882 return 0;
885 } // anon namespace
887 namespace {
889 const pass_data pass_data_ipa_whole_program_visibility =
891 IPA_PASS, /* type */
892 "whole-program", /* name */
893 OPTGROUP_NONE, /* optinfo_flags */
894 TV_CGRAPHOPT, /* tv_id */
895 0, /* properties_required */
896 0, /* properties_provided */
897 0, /* properties_destroyed */
898 0, /* todo_flags_start */
899 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
902 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
904 public:
905 pass_ipa_whole_program_visibility (gcc::context *ctxt)
906 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
907 NULL, /* generate_summary */
908 NULL, /* write_summary */
909 NULL, /* read_summary */
910 NULL, /* write_optimization_summary */
911 NULL, /* read_optimization_summary */
912 NULL, /* stmt_fixup */
913 0, /* function_transform_todo_flags_start */
914 NULL, /* function_transform */
915 NULL) /* variable_transform */
918 /* opt_pass methods: */
920 virtual bool gate (function *)
922 /* Do not re-run on ltrans stage. */
923 return !flag_ltrans;
925 virtual unsigned int execute (function *)
927 return whole_program_function_and_variable_visibility ();
930 }; // class pass_ipa_whole_program_visibility
932 } // anon namespace
934 ipa_opt_pass_d *
935 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
937 return new pass_ipa_whole_program_visibility (ctxt);
940 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
942 public:
943 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
944 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
945 ctxt)
948 /* opt_pass methods: */
949 virtual unsigned int execute (function *)
951 return function_and_variable_visibility (flag_whole_program && !flag_lto);
954 }; // class pass_ipa_function_and_variable_visibility
956 simple_ipa_opt_pass *
957 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
959 return new pass_ipa_function_and_variable_visibility (ctxt);