libstdc++: Refactor loops in std::__platform_semaphore
[official-gcc.git] / gcc / ipa-visibility.cc
blob21f0c47f388e29b03058890c00d099e9df4c7548
1 /* IPA visibility pass
2 Copyright (C) 2003-2024 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 cannot 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"
86 #include "ipa-utils.h"
87 #include "stringpool.h"
88 #include "attribs.h"
90 /* Return true when NODE cannot be local. Worker for cgraph_local_node_p. */
92 static bool
93 non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
95 return !(node->only_called_directly_or_aliased_p ()
96 /* i386 would need update to output thunk with local calling
97 conventions. */
98 && !node->thunk
99 && node->definition
100 && !DECL_EXTERNAL (node->decl)
101 && !lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl))
102 && !node->externally_visible
103 && !node->used_from_other_partition
104 && !node->in_other_partition
105 && node->get_availability () >= AVAIL_AVAILABLE
106 && !DECL_STATIC_CONSTRUCTOR (node->decl)
107 && !DECL_STATIC_DESTRUCTOR (node->decl));
110 /* Return true when function can be marked local. */
112 bool
113 cgraph_node::local_p (void)
115 cgraph_node *n = ultimate_alias_target ();
117 if (n->thunk)
118 return n->callees->callee->local_p ();
119 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
120 NULL, true);
123 /* A helper for comdat_can_be_unshared_p. */
125 static bool
126 comdat_can_be_unshared_p_1 (symtab_node *node)
128 if (!node->externally_visible)
129 return true;
130 if (node->address_can_be_compared_p ())
132 struct ipa_ref *ref;
134 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
135 if (ref->address_matters_p ())
136 return false;
139 /* If the symbol is used in some weird way, better to not touch it. */
140 if (node->force_output)
141 return false;
143 /* Explicit instantiations needs to be output when possibly
144 used externally. */
145 if (node->forced_by_abi
146 && TREE_PUBLIC (node->decl)
147 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
148 && !flag_whole_program))
149 return false;
151 /* Non-readonly and volatile variables cannot be duplicated. */
152 if (is_a <varpool_node *> (node)
153 && (!TREE_READONLY (node->decl)
154 || TREE_THIS_VOLATILE (node->decl)))
155 return false;
156 return true;
159 /* COMDAT functions must be shared only if they have address taken,
160 otherwise we can produce our own private implementation with
161 -fwhole-program.
162 Return true when turning COMDAT function static cannot lead to wrong
163 code when the resulting object links with a library defining same COMDAT.
165 Virtual functions do have their addresses taken from the vtables,
166 but in C++ there is no way to compare their addresses for equality. */
168 static bool
169 comdat_can_be_unshared_p (symtab_node *node)
171 if (!comdat_can_be_unshared_p_1 (node))
172 return false;
173 if (node->same_comdat_group)
175 symtab_node *next;
177 /* If more than one function is in the same COMDAT group, it must
178 be shared even if just one function in the comdat group has
179 address taken. */
180 for (next = node->same_comdat_group;
181 next != node; next = next->same_comdat_group)
182 if (!comdat_can_be_unshared_p_1 (next))
183 return false;
185 return true;
188 /* Return true when function NODE should be considered externally visible. */
190 static bool
191 cgraph_externally_visible_p (struct cgraph_node *node,
192 bool whole_program)
194 while (node->transparent_alias && node->definition)
195 node = node->get_alias_target ();
196 if (!node->definition)
197 return false;
198 if (!TREE_PUBLIC (node->decl)
199 || DECL_EXTERNAL (node->decl))
200 return false;
202 /* Do not try to localize built-in functions yet. One of problems is that we
203 end up mangling their asm for WHOPR that makes it impossible to call them
204 using the implicit built-in declarations anymore. Similarly this enables
205 us to remove them as unreachable before actual calls may appear during
206 expansion or folding. */
207 if (fndecl_built_in_p (node->decl))
208 return true;
210 /* If linker counts on us, we must preserve the function. */
211 if (node->used_from_object_file_p ())
212 return true;
213 if (DECL_PRESERVE_P (node->decl))
214 return true;
215 if (lookup_attribute ("externally_visible",
216 DECL_ATTRIBUTES (node->decl)))
217 return true;
218 if (lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)))
219 return true;
220 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
221 && lookup_attribute ("dllexport",
222 DECL_ATTRIBUTES (node->decl)))
223 return true;
225 /* Limitation of gas requires us to output targets of symver aliases as
226 global symbols. This is binutils PR 25295. */
227 ipa_ref *ref;
228 FOR_EACH_ALIAS (node, ref)
229 if (ref->referring->symver)
230 return true;
232 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
233 return false;
234 /* When doing LTO or whole program, we can bring COMDAT functions static.
235 This improves code quality and we know we will duplicate them at most twice
236 (in the case that we are not using plugin and link with object file
237 implementing same COMDAT) */
238 if (((in_lto_p || whole_program) && !flag_incremental_link)
239 && DECL_COMDAT (node->decl)
240 && comdat_can_be_unshared_p (node))
241 return false;
243 /* When doing link time optimizations, hidden symbols become local. */
244 if ((in_lto_p && !flag_incremental_link)
245 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
246 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
247 /* Be sure that node is defined in IR file, not in other object
248 file. In that case we don't set used_from_other_object_file. */
249 && node->definition)
251 else if (!whole_program)
252 return true;
254 if (MAIN_NAME_P (DECL_NAME (node->decl)))
255 return true;
257 return false;
260 /* Return true when variable should be considered externally visible. */
262 bool
263 varpool_node::externally_visible_p (void)
265 while (transparent_alias && definition)
266 return get_alias_target ()->externally_visible_p ();
267 if (DECL_EXTERNAL (decl))
268 return true;
270 if (!TREE_PUBLIC (decl))
271 return false;
273 /* If linker counts on us, we must preserve the function. */
274 if (used_from_object_file_p ())
275 return true;
277 /* Bringing TLS variables local may cause dynamic linker failures
278 on limits of static TLS vars. */
279 if (DECL_THREAD_LOCAL_P (decl)
280 && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
281 && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
282 return true;
284 if (DECL_HARD_REGISTER (decl))
285 return true;
286 if (DECL_PRESERVE_P (decl))
287 return true;
288 if (lookup_attribute ("externally_visible",
289 DECL_ATTRIBUTES (decl)))
290 return true;
291 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
292 && lookup_attribute ("dllexport",
293 DECL_ATTRIBUTES (decl)))
294 return true;
296 /* Limitation of gas requires us to output targets of symver aliases as
297 global symbols. This is binutils PR 25295. */
298 ipa_ref *ref;
299 FOR_EACH_ALIAS (this, ref)
300 if (ref->referring->symver)
301 return true;
303 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
304 return false;
306 /* As a special case, the COMDAT virtual tables can be unshared.
307 In LTO mode turn vtables into static variables. The variable is readonly,
308 so this does not enable more optimization, but referring static var
309 is faster for dynamic linking. Also this match logic hidding vtables
310 from LTO symbol tables. */
311 if (((in_lto_p || flag_whole_program) && !flag_incremental_link)
312 && DECL_COMDAT (decl)
313 && comdat_can_be_unshared_p (this))
314 return false;
316 /* When doing link time optimizations, hidden symbols become local. */
317 if (in_lto_p && !flag_incremental_link
318 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
319 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
320 /* Be sure that node is defined in IR file, not in other object
321 file. In that case we don't set used_from_other_object_file. */
322 && definition)
324 else if (!flag_whole_program)
325 return true;
327 /* Do not attempt to privatize COMDATS by default.
328 This would break linking with C++ libraries sharing
329 inline definitions.
331 FIXME: We can do so for readonly vars with no address taken and
332 possibly also for vtables since no direct pointer comparsion is done.
333 It might be interesting to do so to reduce linking overhead. */
334 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
335 return true;
336 return false;
339 /* Return true if reference to NODE can be replaced by a local alias.
340 Local aliases save dynamic linking overhead and enable more optimizations.
343 static bool
344 can_replace_by_local_alias (symtab_node *node)
346 /* If aliases aren't supported, we can't do replacement. */
347 if (!TARGET_SUPPORTS_ALIASES)
348 return false;
350 /* Weakrefs have a reason to be non-local. Be sure we do not replace
351 them. */
352 while (node->transparent_alias && node->definition && !node->weakref)
353 node = node->get_alias_target ();
354 if (node->weakref)
355 return false;
357 return (node->get_availability () > AVAIL_INTERPOSABLE
358 && !decl_binds_to_current_def_p (node->decl)
359 && !node->can_be_discarded_p ());
362 /* Return true if we can replace reference to NODE by local alias
363 within a virtual table. Generally we can replace function pointers
364 and virtual table pointers. */
366 static bool
367 can_replace_by_local_alias_in_vtable (symtab_node *node)
369 if (is_a <varpool_node *> (node)
370 && !DECL_VIRTUAL_P (node->decl))
371 return false;
372 return can_replace_by_local_alias (node);
375 /* walk_tree callback that rewrites initializer references. */
377 static tree
378 update_vtable_references (tree *tp, int *walk_subtrees,
379 void *data ATTRIBUTE_UNUSED)
381 if (VAR_OR_FUNCTION_DECL_P (*tp))
383 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
384 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
385 *walk_subtrees = 0;
387 else if (IS_TYPE_OR_DECL_P (*tp))
388 *walk_subtrees = 0;
389 return NULL;
392 /* In LTO we can remove COMDAT groups and weak symbols.
393 Either turn them into normal symbols or external symbol depending on
394 resolution info. */
396 static void
397 update_visibility_by_resolution_info (symtab_node * node)
399 bool define;
401 if (!node->externally_visible
402 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
403 || node->resolution == LDPR_UNKNOWN)
404 return;
406 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
407 || node->resolution == LDPR_PREVAILING_DEF
408 || node->resolution == LDPR_UNDEF
409 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
411 /* The linker decisions ought to agree in the whole group. */
412 if (node->same_comdat_group)
413 for (symtab_node *next = node->same_comdat_group;
414 next != node; next = next->same_comdat_group)
416 if (!next->externally_visible || next->transparent_alias)
417 continue;
419 bool same_def
420 = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
421 || next->resolution == LDPR_PREVAILING_DEF
422 || next->resolution == LDPR_UNDEF
423 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
424 gcc_assert (in_lto_p || same_def);
425 if (!same_def)
426 return;
429 if (node->same_comdat_group)
430 for (symtab_node *next = node->same_comdat_group;
431 next != node; next = next->same_comdat_group)
433 /* During incremental linking we need to keep symbol weak for future
434 linking. We can still drop definition if we know non-LTO world
435 prevails. */
436 if (!flag_incremental_link)
438 DECL_WEAK (next->decl) = false;
439 next->set_comdat_group (NULL);
441 if (!define)
443 if (next->externally_visible)
444 DECL_EXTERNAL (next->decl) = true;
445 next->set_comdat_group (NULL);
449 /* During incremental linking we need to keep symbol weak for future
450 linking. We can still drop definition if we know non-LTO world prevails. */
451 if (!flag_incremental_link)
453 DECL_WEAK (node->decl) = false;
454 node->set_comdat_group (NULL);
455 node->dissolve_same_comdat_group_list ();
457 if (!define)
459 DECL_EXTERNAL (node->decl) = true;
460 node->set_comdat_group (NULL);
461 node->dissolve_same_comdat_group_list ();
465 /* Try to get rid of weakref. */
467 static void
468 optimize_weakref (symtab_node *node)
470 bool strip_weakref = false;
471 bool static_alias = false;
473 gcc_assert (node->weakref);
475 /* Weakrefs with no target defined cannot be optimized. */
476 if (!node->analyzed)
477 return;
478 symtab_node *target = node->get_alias_target ();
480 /* Weakrefs to weakrefs can be optimized only if target can be. */
481 if (target->weakref)
482 optimize_weakref (target);
483 if (target->weakref)
484 return;
486 /* If we have definition of weakref's target and we know it binds locally,
487 we can turn weakref to static alias. */
488 if (TARGET_SUPPORTS_ALIASES
489 && target->definition && decl_binds_to_current_def_p (target->decl))
490 strip_weakref = static_alias = true;
491 /* Otherwise we can turn weakref into transparent alias. This transformation
492 may break asm statements which directly refers to symbol name and expect
493 GNU as to translate it via .weakref directive. So do not optimize when
494 DECL_PRESERVED is set and .weakref is supported. */
495 else if ((!DECL_PRESERVE_P (target->decl)
496 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)))
497 && !DECL_WEAK (target->decl)
498 && !DECL_EXTERNAL (target->decl)
499 && ((target->definition && !target->can_be_discarded_p ())
500 || target->resolution != LDPR_UNDEF))
501 strip_weakref = true;
502 if (!strip_weakref)
503 return;
504 node->weakref = false;
505 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)) = 0;
506 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) = NULL_TREE;
507 DECL_ATTRIBUTES (node->decl) = remove_attribute ("weakref",
508 DECL_ATTRIBUTES
509 (node->decl));
511 if (dump_file)
512 fprintf (dump_file, "Optimizing weakref %s %s\n",
513 node->dump_name (),
514 static_alias ? "as static alias" : "as transparent alias");
516 if (static_alias)
518 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
519 be sure it really clears the WEAK flag. */
520 TREE_PUBLIC (node->decl) = true;
521 node->make_decl_local ();
522 node->forced_by_abi = false;
523 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
524 node->externally_visible = false;
525 gcc_assert (!DECL_WEAK (node->decl));
526 node->transparent_alias = false;
528 else
530 symtab->change_decl_assembler_name
531 (node->decl, DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
532 node->transparent_alias = true;
533 node->copy_visibility_from (target);
535 gcc_assert (node->alias);
538 /* NODE is an externally visible definition, which we've discovered is
539 not needed externally. Make it local to this compilation. */
541 static void
542 localize_node (bool whole_program, symtab_node *node)
544 gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
546 /* It is possible that one comdat group contains both hidden and non-hidden
547 symbols. In this case we can privatize all hidden symbol but we need
548 to keep non-hidden exported. */
549 if (node->same_comdat_group
550 && (node->resolution == LDPR_PREVAILING_DEF_IRONLY
551 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP))
553 symtab_node *next;
554 for (next = node->same_comdat_group;
555 next != node; next = next->same_comdat_group)
556 if (next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
557 || next->resolution == LDPR_PREVAILING_DEF)
558 break;
559 if (node != next)
561 if (!node->transparent_alias)
563 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
564 node->make_decl_local ();
565 if (!flag_incremental_link)
566 node->unique_name |= true;
567 return;
571 /* For similar reason do not privatize whole comdat when seeing comdat
572 local. Wait for non-comdat symbol to be privatized first. */
573 if (node->comdat_local_p ())
574 return;
576 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
578 for (symtab_node *next = node->same_comdat_group;
579 next != node; next = next->same_comdat_group)
581 next->set_comdat_group (NULL);
582 if (!next->alias)
583 next->set_section (NULL);
584 if (!next->transparent_alias)
585 next->make_decl_local ();
586 next->unique_name
587 |= ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
588 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
589 && TREE_PUBLIC (next->decl)
590 && !flag_incremental_link);
593 /* Now everything's localized, the grouping has no meaning, and
594 will cause crashes if we keep it around. */
595 node->dissolve_same_comdat_group_list ();
598 node->unique_name
599 |= ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
600 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
601 && TREE_PUBLIC (node->decl)
602 && !flag_incremental_link);
604 if (TREE_PUBLIC (node->decl))
605 node->set_comdat_group (NULL);
606 if (DECL_COMDAT (node->decl) && !node->alias)
607 node->set_section (NULL);
608 if (!node->transparent_alias)
610 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
611 node->make_decl_local ();
615 /* Decide on visibility of all symbols. */
617 static unsigned int
618 function_and_variable_visibility (bool whole_program)
620 struct cgraph_node *node;
621 varpool_node *vnode;
623 /* All aliases should be processed at this point. */
624 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
626 if (TARGET_SUPPORTS_ALIASES)
628 FOR_EACH_DEFINED_FUNCTION (node)
630 if (node->get_availability () != AVAIL_INTERPOSABLE
631 || DECL_EXTERNAL (node->decl)
632 || node->has_aliases_p ()
633 || lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)))
634 continue;
636 cgraph_node *alias = 0;
637 cgraph_edge *next_edge;
638 for (cgraph_edge *e = node->callees; e; e = next_edge)
640 next_edge = e->next_callee;
641 /* Recursive function calls usually can't be interposed. */
643 if (!e->recursive_p ())
644 continue;
646 if (!alias)
648 alias
649 = dyn_cast<cgraph_node *> (node->noninterposable_alias ());
650 gcc_assert (alias && alias != node);
653 e->redirect_callee (alias);
654 if (gimple_has_body_p (e->caller->decl))
656 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
657 cgraph_edge::redirect_call_stmt_to_callee (e);
658 pop_cfun ();
664 FOR_EACH_FUNCTION (node)
666 int flags = flags_from_decl_or_type (node->decl);
668 /* Optimize away PURE and CONST constructors and destructors. */
669 if (node->analyzed
670 && (DECL_STATIC_CONSTRUCTOR (node->decl)
671 || DECL_STATIC_DESTRUCTOR (node->decl))
672 && (flags & (ECF_CONST | ECF_PURE))
673 && !(flags & ECF_LOOPING_CONST_OR_PURE)
674 && opt_for_fn (node->decl, optimize))
676 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
677 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
680 /* Frontends and alias code marks nodes as needed before parsing
681 is finished. We may end up marking as node external nodes
682 where this flag is meaningless strip it. */
683 if (DECL_EXTERNAL (node->decl) || !node->definition)
685 node->force_output = 0;
686 node->forced_by_abi = 0;
689 /* C++ FE on lack of COMDAT support create local COMDAT functions
690 (that ought to be shared but cannot due to object format
691 limitations). It is necessary to keep the flag to make rest of C++ FE
692 happy. Clear the flag here to avoid confusion in middle-end. */
693 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
694 DECL_COMDAT (node->decl) = 0;
696 /* For external decls stop tracking same_comdat_group. It doesn't matter
697 what comdat group they are in when they won't be emitted in this TU.
699 An exception is LTO where we may end up with both external
700 and non-external declarations in the same comdat group in
701 the case declarations was not merged. */
702 if (node->same_comdat_group && DECL_EXTERNAL (node->decl) && !in_lto_p)
704 if (flag_checking)
706 for (symtab_node *n = node->same_comdat_group;
707 n != node;
708 n = n->same_comdat_group)
709 /* If at least one of same comdat group functions is external,
710 all of them have to be, otherwise it is a front-end bug. */
711 gcc_assert (DECL_EXTERNAL (n->decl));
713 node->dissolve_same_comdat_group_list ();
715 gcc_assert ((!DECL_WEAK (node->decl)
716 && !DECL_COMDAT (node->decl))
717 || TREE_PUBLIC (node->decl)
718 || node->weakref
719 || DECL_EXTERNAL (node->decl));
720 if (cgraph_externally_visible_p (node, whole_program))
722 gcc_assert (!node->inlined_to);
723 node->externally_visible = true;
725 else
727 node->externally_visible = false;
728 node->forced_by_abi = false;
730 if (!node->externally_visible
731 && node->definition && !node->weakref
732 && !DECL_EXTERNAL (node->decl))
733 localize_node (whole_program, node);
735 if (node->thunk
736 && TREE_PUBLIC (node->decl))
738 struct cgraph_node *decl_node = node;
740 decl_node = decl_node->callees->callee->function_symbol ();
742 /* Thunks have the same visibility as function they are attached to.
743 Make sure the C++ front end set this up properly. */
744 if (DECL_ONE_ONLY (decl_node->decl))
746 gcc_checking_assert (DECL_COMDAT (node->decl)
747 == DECL_COMDAT (decl_node->decl));
748 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
749 gcc_checking_assert (node->same_comdat_group);
751 node->forced_by_abi = decl_node->forced_by_abi;
752 if (DECL_EXTERNAL (decl_node->decl))
753 DECL_EXTERNAL (node->decl) = 1;
756 update_visibility_by_resolution_info (node);
757 if (node->weakref)
758 optimize_weakref (node);
760 FOR_EACH_DEFINED_FUNCTION (node)
762 if (!node->local)
763 node->local |= node->local_p ();
765 /* If we know that function cannot be overwritten by a
766 different semantics and moreover its section cannot be
767 discarded, replace all direct calls by calls to an
768 noninterposable alias. This make dynamic linking cheaper and
769 enable more optimization.
771 TODO: We can also update virtual tables. */
772 if (node->callers
773 && can_replace_by_local_alias (node))
775 cgraph_node *alias = dyn_cast<cgraph_node *>
776 (node->noninterposable_alias ());
778 if (alias && alias != node)
780 while (node->callers)
782 struct cgraph_edge *e = node->callers;
784 e->redirect_callee (alias);
785 if (gimple_has_body_p (e->caller->decl))
787 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
788 cgraph_edge::redirect_call_stmt_to_callee (e);
789 pop_cfun ();
795 FOR_EACH_VARIABLE (vnode)
797 /* weak flag makes no sense on local variables. */
798 gcc_assert (!DECL_WEAK (vnode->decl)
799 || vnode->weakref
800 || TREE_PUBLIC (vnode->decl)
801 || DECL_EXTERNAL (vnode->decl));
802 /* In several cases declarations cannot be common:
804 - when declaration has initializer
805 - when it is in weak
806 - when it has specific section
807 - when it resides in non-generic address space.
808 - if declaration is local, it will get into .local common section
809 so common flag is not needed. Frontends still produce these in
810 certain cases, such as for:
812 static int a __attribute__ ((common))
814 Canonicalize things here and clear the redundant flag. */
815 if (DECL_COMMON (vnode->decl)
816 && (!(TREE_PUBLIC (vnode->decl)
817 || DECL_EXTERNAL (vnode->decl))
818 || (DECL_INITIAL (vnode->decl)
819 && DECL_INITIAL (vnode->decl) != error_mark_node)
820 || DECL_WEAK (vnode->decl)
821 || DECL_SECTION_NAME (vnode->decl) != NULL
822 || ! (ADDR_SPACE_GENERIC_P
823 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
824 DECL_COMMON (vnode->decl) = 0;
825 if (vnode->weakref)
826 optimize_weakref (vnode);
828 FOR_EACH_DEFINED_VARIABLE (vnode)
830 if (!vnode->definition)
831 continue;
832 if (vnode->externally_visible_p ())
833 vnode->externally_visible = true;
834 else
836 vnode->externally_visible = false;
837 vnode->forced_by_abi = false;
839 if (lookup_attribute ("no_reorder",
840 DECL_ATTRIBUTES (vnode->decl)))
841 vnode->no_reorder = 1;
843 if (!vnode->externally_visible
844 && !vnode->transparent_alias
845 && !DECL_EXTERNAL (vnode->decl))
846 localize_node (whole_program, vnode);
848 update_visibility_by_resolution_info (vnode);
850 /* Update virtual tables to point to local aliases where possible. */
851 if (DECL_VIRTUAL_P (vnode->decl)
852 && !DECL_EXTERNAL (vnode->decl))
854 int i;
855 struct ipa_ref *ref;
856 bool found = false;
858 /* See if there is something to update. */
859 for (i = 0; vnode->iterate_reference (i, ref); i++)
860 if (ref->use == IPA_REF_ADDR
861 && can_replace_by_local_alias_in_vtable (ref->referred))
863 found = true;
864 break;
866 if (found)
868 hash_set<tree> visited_nodes;
870 vnode->get_constructor ();
871 walk_tree (&DECL_INITIAL (vnode->decl),
872 update_vtable_references, NULL, &visited_nodes);
873 vnode->remove_all_references ();
874 record_references_in_initializer (vnode->decl, false);
879 if (symtab->state >= IPA_SSA)
881 FOR_EACH_VARIABLE (vnode)
883 tree decl = vnode->decl;
885 /* Upgrade TLS access model based on optimized visibility status,
886 unless it was specified explicitly or no references remain. */
887 if (DECL_THREAD_LOCAL_P (decl)
888 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl))
889 && vnode->ref_list.referring.length ())
891 enum tls_model new_model = decl_default_tls_model (decl);
892 STATIC_ASSERT (TLS_MODEL_GLOBAL_DYNAMIC < TLS_MODEL_LOCAL_DYNAMIC);
893 STATIC_ASSERT (TLS_MODEL_INITIAL_EXEC < TLS_MODEL_LOCAL_EXEC);
894 /* We'd prefer to assert that recomputed model is not weaker than
895 what the front-end assigned, but cannot: see PR 107353. */
896 if (new_model >= decl_tls_model (decl))
897 set_decl_tls_model (decl, new_model);
902 if (dump_file)
904 fprintf (dump_file, "\nMarking local functions:");
905 FOR_EACH_DEFINED_FUNCTION (node)
906 if (node->local)
907 fprintf (dump_file, " %s", node->dump_name ());
908 fprintf (dump_file, "\n\n");
909 fprintf (dump_file, "\nMarking externally visible functions:");
910 FOR_EACH_DEFINED_FUNCTION (node)
911 if (node->externally_visible)
912 fprintf (dump_file, " %s", node->dump_name ());
913 fprintf (dump_file, "\n\n");
914 fprintf (dump_file, "\nMarking externally visible variables:");
915 FOR_EACH_DEFINED_VARIABLE (vnode)
916 if (vnode->externally_visible)
917 fprintf (dump_file, " %s", vnode->dump_name ());
918 fprintf (dump_file, "\n\n");
920 symtab->function_flags_ready = true;
921 return 0;
924 /* Local function pass handling visibilities. This happens before LTO streaming
925 so in particular -fwhole-program should be ignored at this level. */
927 namespace {
929 const pass_data pass_data_ipa_function_and_variable_visibility =
931 SIMPLE_IPA_PASS, /* type */
932 "visibility", /* name */
933 OPTGROUP_NONE, /* optinfo_flags */
934 TV_CGRAPHOPT, /* tv_id */
935 0, /* properties_required */
936 0, /* properties_provided */
937 0, /* properties_destroyed */
938 0, /* todo_flags_start */
939 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
942 /* Bring functions local at LTO time with -fwhole-program. */
944 static unsigned int
945 whole_program_function_and_variable_visibility (void)
947 function_and_variable_visibility (flag_whole_program);
948 if (optimize || in_lto_p)
949 ipa_discover_variable_flags ();
950 return 0;
953 } // anon namespace
955 namespace {
957 const pass_data pass_data_ipa_whole_program_visibility =
959 IPA_PASS, /* type */
960 "whole-program", /* name */
961 OPTGROUP_NONE, /* optinfo_flags */
962 TV_CGRAPHOPT, /* tv_id */
963 0, /* properties_required */
964 0, /* properties_provided */
965 0, /* properties_destroyed */
966 0, /* todo_flags_start */
967 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
970 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
972 public:
973 pass_ipa_whole_program_visibility (gcc::context *ctxt)
974 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
975 NULL, /* generate_summary */
976 NULL, /* write_summary */
977 NULL, /* read_summary */
978 NULL, /* write_optimization_summary */
979 NULL, /* read_optimization_summary */
980 NULL, /* stmt_fixup */
981 0, /* function_transform_todo_flags_start */
982 NULL, /* function_transform */
983 NULL) /* variable_transform */
986 /* opt_pass methods: */
988 bool gate (function *) final override
990 /* Do not re-run on ltrans stage. */
991 return !flag_ltrans;
993 unsigned int execute (function *) final override
995 return whole_program_function_and_variable_visibility ();
998 }; // class pass_ipa_whole_program_visibility
1000 } // anon namespace
1002 ipa_opt_pass_d *
1003 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1005 return new pass_ipa_whole_program_visibility (ctxt);
1008 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1010 public:
1011 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1012 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1013 ctxt)
1016 /* opt_pass methods: */
1017 unsigned int execute (function *) final override
1019 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1022 }; // class pass_ipa_function_and_variable_visibility
1024 simple_ipa_opt_pass *
1025 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1027 return new pass_ipa_function_and_variable_visibility (ctxt);