]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-visibility.cc
fortran: Add -finline-intrinsics flag for MINLOC/MAXLOC [PR90608]
[thirdparty/gcc.git] / gcc / ipa-visibility.cc
CommitLineData
7f7beb3f 1/* IPA visibility pass
a945c346 2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
7f7beb3f
JH
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
67914693 45 externally_visible flag is set for symbols that cannot be privatized.
7f7beb3f
JH
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"
c582198b 79#include "function.h"
957060b5
AM
80#include "tree.h"
81#include "gimple-expr.h"
7f7beb3f 82#include "tree-pass.h"
957060b5 83#include "cgraph.h"
7f7beb3f 84#include "calls.h"
94c2e46c 85#include "varasm.h"
325fe816 86#include "ipa-utils.h"
314e6352
ML
87#include "stringpool.h"
88#include "attribs.h"
7f7beb3f 89
67914693 90/* Return true when NODE cannot be local. Worker for cgraph_local_node_p. */
7f7beb3f 91
4bd019b8
JH
92static bool
93non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
7f7beb3f 94{
953aad61 95 return !(node->only_called_directly_or_aliased_p ()
fb81a61b 96 /* i386 would need update to output thunk with local calling
036ea399 97 conventions. */
67f3791f 98 && !node->thunk
953aad61
JH
99 && node->definition
100 && !DECL_EXTERNAL (node->decl)
036ea399 101 && !lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl))
953aad61
JH
102 && !node->externally_visible
103 && !node->used_from_other_partition
17a7218b 104 && !node->in_other_partition
e98ad6a0
MJ
105 && node->get_availability () >= AVAIL_AVAILABLE
106 && !DECL_STATIC_CONSTRUCTOR (node->decl)
107 && !DECL_STATIC_DESTRUCTOR (node->decl));
7f7beb3f
JH
108}
109
110/* Return true when function can be marked local. */
111
112bool
d52f5295 113cgraph_node::local_p (void)
7f7beb3f 114{
d52f5295 115 cgraph_node *n = ultimate_alias_target ();
7f7beb3f 116
67f3791f 117 if (n->thunk)
953aad61 118 return n->callees->callee->local_p ();
4bd019b8 119 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
953aad61 120 NULL, true);
7f7beb3f
JH
121}
122
7f7beb3f
JH
123/* A helper for comdat_can_be_unshared_p. */
124
125static bool
126comdat_can_be_unshared_p_1 (symtab_node *node)
127{
128 if (!node->externally_visible)
129 return true;
0a7246ee
JH
130 if (node->address_can_be_compared_p ())
131 {
132 struct ipa_ref *ref;
133
134 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
135 if (ref->address_matters_p ())
136 return false;
137 }
7f7beb3f
JH
138
139 /* If the symbol is used in some weird way, better to not touch it. */
140 if (node->force_output)
141 return false;
142
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;
150
67914693 151 /* Non-readonly and volatile variables cannot be duplicated. */
7f7beb3f
JH
152 if (is_a <varpool_node *> (node)
153 && (!TREE_READONLY (node->decl)
154 || TREE_THIS_VOLATILE (node->decl)))
155 return false;
156 return true;
157}
158
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.
67914693 162 Return true when turning COMDAT function static cannot lead to wrong
7f7beb3f
JH
163 code when the resulting object links with a library defining same COMDAT.
164
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. */
167
168static bool
169comdat_can_be_unshared_p (symtab_node *node)
170{
171 if (!comdat_can_be_unshared_p_1 (node))
172 return false;
173 if (node->same_comdat_group)
174 {
175 symtab_node *next;
176
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;
184 }
185 return true;
186}
187
188/* Return true when function NODE should be considered externally visible. */
189
190static bool
191cgraph_externally_visible_p (struct cgraph_node *node,
192 bool whole_program)
193{
71e54687
JH
194 while (node->transparent_alias && node->definition)
195 node = node->get_alias_target ();
7f7beb3f
JH
196 if (!node->definition)
197 return false;
198 if (!TREE_PUBLIC (node->decl)
199 || DECL_EXTERNAL (node->decl))
200 return false;
201
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. */
3d78e008 207 if (fndecl_built_in_p (node->decl))
7f7beb3f
JH
208 return true;
209
210 /* If linker counts on us, we must preserve the function. */
d52f5295 211 if (node->used_from_object_file_p ())
7f7beb3f
JH
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;
036ea399
JJ
218 if (lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)))
219 return true;
7f7beb3f
JH
220 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
221 && lookup_attribute ("dllexport",
222 DECL_ATTRIBUTES (node->decl)))
223 return true;
40ebe1fc
JH
224
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;
231
7f7beb3f
JH
232 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
233 return false;
8def1d52 234 /* When doing LTO or whole program, we can bring COMDAT functions static.
7f7beb3f
JH
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) */
1ff9ed6f 238 if (((in_lto_p || whole_program) && !flag_incremental_link)
7f7beb3f
JH
239 && DECL_COMDAT (node->decl)
240 && comdat_can_be_unshared_p (node))
241 return false;
242
243 /* When doing link time optimizations, hidden symbols become local. */
1ff9ed6f 244 if ((in_lto_p && !flag_incremental_link)
7f7beb3f
JH
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)
250 ;
251 else if (!whole_program)
252 return true;
253
254 if (MAIN_NAME_P (DECL_NAME (node->decl)))
255 return true;
256
257 return false;
258}
259
9041d2e6 260/* Return true when variable should be considered externally visible. */
7f7beb3f
JH
261
262bool
9041d2e6 263varpool_node::externally_visible_p (void)
7f7beb3f 264{
71e54687
JH
265 while (transparent_alias && definition)
266 return get_alias_target ()->externally_visible_p ();
9041d2e6 267 if (DECL_EXTERNAL (decl))
7f7beb3f
JH
268 return true;
269
9041d2e6 270 if (!TREE_PUBLIC (decl))
7f7beb3f
JH
271 return false;
272
273 /* If linker counts on us, we must preserve the function. */
9041d2e6 274 if (used_from_object_file_p ())
7f7beb3f
JH
275 return true;
276
8e1ba78f
JH
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;
283
9041d2e6 284 if (DECL_HARD_REGISTER (decl))
7f7beb3f 285 return true;
9041d2e6 286 if (DECL_PRESERVE_P (decl))
7f7beb3f
JH
287 return true;
288 if (lookup_attribute ("externally_visible",
9041d2e6 289 DECL_ATTRIBUTES (decl)))
7f7beb3f
JH
290 return true;
291 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
292 && lookup_attribute ("dllexport",
9041d2e6 293 DECL_ATTRIBUTES (decl)))
7f7beb3f
JH
294 return true;
295
40ebe1fc
JH
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;
7f7beb3f 302
9041d2e6 303 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
7f7beb3f
JH
304 return false;
305
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. */
1ff9ed6f 311 if (((in_lto_p || flag_whole_program) && !flag_incremental_link)
9041d2e6
ML
312 && DECL_COMDAT (decl)
313 && comdat_can_be_unshared_p (this))
7f7beb3f
JH
314 return false;
315
316 /* When doing link time optimizations, hidden symbols become local. */
1ff9ed6f 317 if (in_lto_p && !flag_incremental_link
9041d2e6
ML
318 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
319 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
7f7beb3f
JH
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. */
9041d2e6 322 && definition)
7f7beb3f
JH
323 ;
324 else if (!flag_whole_program)
325 return true;
326
327 /* Do not attempt to privatize COMDATS by default.
328 This would break linking with C++ libraries sharing
329 inline definitions.
330
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. */
9041d2e6 334 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
7f7beb3f
JH
335 return true;
336 return false;
337}
338
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.
341 */
342
f95ff97f 343static bool
7f7beb3f
JH
344can_replace_by_local_alias (symtab_node *node)
345{
f95ff97f 346 /* If aliases aren't supported, we can't do replacement. */
a8b522b4
ML
347 if (!TARGET_SUPPORTS_ALIASES)
348 return false;
349
ced7e958
JH
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;
356
d52f5295 357 return (node->get_availability () > AVAIL_INTERPOSABLE
94c2e46c 358 && !decl_binds_to_current_def_p (node->decl)
d52f5295 359 && !node->can_be_discarded_p ());
7f7beb3f
JH
360}
361
f95ff97f 362/* Return true if we can replace reference to NODE by local alias
07990a5e
JH
363 within a virtual table. Generally we can replace function pointers
364 and virtual table pointers. */
365
f95ff97f 366static bool
07990a5e
JH
367can_replace_by_local_alias_in_vtable (symtab_node *node)
368{
369 if (is_a <varpool_node *> (node)
370 && !DECL_VIRTUAL_P (node->decl))
371 return false;
372 return can_replace_by_local_alias (node);
373}
374
375/* walk_tree callback that rewrites initializer references. */
376
377static tree
0a7246ee
JH
378update_vtable_references (tree *tp, int *walk_subtrees,
379 void *data ATTRIBUTE_UNUSED)
07990a5e 380{
8813a647 381 if (VAR_OR_FUNCTION_DECL_P (*tp))
07990a5e 382 {
d52f5295
ML
383 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
384 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
07990a5e
JH
385 *walk_subtrees = 0;
386 }
387 else if (IS_TYPE_OR_DECL_P (*tp))
388 *walk_subtrees = 0;
389 return NULL;
390}
391
7f7beb3f
JH
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. */
395
396static void
397update_visibility_by_resolution_info (symtab_node * node)
398{
399 bool define;
400
401 if (!node->externally_visible
402 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
da66d596 403 || node->resolution == LDPR_UNKNOWN)
7f7beb3f
JH
404 return;
405
406 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
407 || node->resolution == LDPR_PREVAILING_DEF
2e4c1e6c 408 || node->resolution == LDPR_UNDEF
7f7beb3f
JH
409 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
410
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)
a9e083cc 415 {
ced7e958 416 if (!next->externally_visible || next->transparent_alias)
a9e083cc
TS
417 continue;
418
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;
427 }
7f7beb3f
JH
428
429 if (node->same_comdat_group)
430 for (symtab_node *next = node->same_comdat_group;
431 next != node; next = next->same_comdat_group)
432 {
1ff9ed6f
JH
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)
437 {
438 DECL_WEAK (next->decl) = false;
439 next->set_comdat_group (NULL);
440 }
f1703a2e 441 if (!define)
1ff9ed6f 442 {
f1703a2e
JH
443 if (next->externally_visible)
444 DECL_EXTERNAL (next->decl) = true;
1ff9ed6f
JH
445 next->set_comdat_group (NULL);
446 }
7f7beb3f 447 }
1ff9ed6f
JH
448
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)
452 {
453 DECL_WEAK (node->decl) = false;
454 node->set_comdat_group (NULL);
455 node->dissolve_same_comdat_group_list ();
456 }
7f7beb3f 457 if (!define)
1ff9ed6f
JH
458 {
459 DECL_EXTERNAL (node->decl) = true;
460 node->set_comdat_group (NULL);
461 node->dissolve_same_comdat_group_list ();
462 }
7f7beb3f
JH
463}
464
ed2a53e7
JH
465/* Try to get rid of weakref. */
466
467static void
468optimize_weakref (symtab_node *node)
469{
ed2a53e7
JH
470 bool strip_weakref = false;
471 bool static_alias = false;
472
473 gcc_assert (node->weakref);
474
67914693 475 /* Weakrefs with no target defined cannot be optimized. */
ed2a53e7
JH
476 if (!node->analyzed)
477 return;
478 symtab_node *target = node->get_alias_target ();
479
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;
485
486 /* If we have definition of weakref's target and we know it binds locally,
487 we can turn weakref to static alias. */
a8b522b4
ML
488 if (TARGET_SUPPORTS_ALIASES
489 && target->definition && decl_binds_to_current_def_p (target->decl))
ed2a53e7
JH
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));
510
511 if (dump_file)
512 fprintf (dump_file, "Optimizing weakref %s %s\n",
3629ff8a 513 node->dump_name (),
ed2a53e7
JH
514 static_alias ? "as static alias" : "as transparent alias");
515
516 if (static_alias)
517 {
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;
527 }
528 else
529 {
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);
534 }
535 gcc_assert (node->alias);
536}
537
408de159
NS
538/* NODE is an externally visible definition, which we've discovered is
539 not needed externally. Make it local to this compilation. */
540
541static void
542localize_node (bool whole_program, symtab_node *node)
543{
544 gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
545
fb81a61b
JH
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
5873f613
JH
550 && (node->resolution == LDPR_PREVAILING_DEF_IRONLY
551 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP))
fb81a61b
JH
552 {
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)
560 {
561 if (!node->transparent_alias)
562 {
563 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
564 node->make_decl_local ();
565 if (!flag_incremental_link)
566 node->unique_name |= true;
567 return;
568 }
569 }
570 }
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;
575
408de159
NS
576 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
577 {
578 for (symtab_node *next = node->same_comdat_group;
579 next != node; next = next->same_comdat_group)
580 {
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);
591 }
592
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 ();
596 }
597
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);
603
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)
609 {
610 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
611 node->make_decl_local ();
612 }
613}
614
7f7beb3f
JH
615/* Decide on visibility of all symbols. */
616
617static unsigned int
618function_and_variable_visibility (bool whole_program)
619{
620 struct cgraph_node *node;
621 varpool_node *vnode;
622
4287b4e8 623 /* All aliases should be processed at this point. */
7f7beb3f
JH
624 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
625
3dfe7e2d 626 if (TARGET_SUPPORTS_ALIASES)
325fe816 627 {
3dfe7e2d 628 FOR_EACH_DEFINED_FUNCTION (node)
325fe816 629 {
3dfe7e2d
TS
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)))
325fe816
YG
634 continue;
635
3dfe7e2d
TS
636 cgraph_node *alias = 0;
637 cgraph_edge *next_edge;
638 for (cgraph_edge *e = node->callees; e; e = next_edge)
4287b4e8 639 {
3dfe7e2d
TS
640 next_edge = e->next_callee;
641 /* Recursive function calls usually can't be interposed. */
642
643 if (!e->recursive_p ())
644 continue;
645
646 if (!alias)
647 {
648 alias
649 = dyn_cast<cgraph_node *> (node->noninterposable_alias ());
650 gcc_assert (alias && alias != node);
651 }
652
653 e->redirect_callee (alias);
654 if (gimple_has_body_p (e->caller->decl))
655 {
656 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
657 cgraph_edge::redirect_call_stmt_to_callee (e);
658 pop_cfun ();
659 }
325fe816
YG
660 }
661 }
662 }
4287b4e8 663
7f7beb3f
JH
664 FOR_EACH_FUNCTION (node)
665 {
666 int flags = flags_from_decl_or_type (node->decl);
667
668 /* Optimize away PURE and CONST constructors and destructors. */
29f1e2b1
JH
669 if (node->analyzed
670 && (DECL_STATIC_CONSTRUCTOR (node->decl)
e1813e4b 671 || DECL_STATIC_DESTRUCTOR (node->decl))
7f7beb3f 672 && (flags & (ECF_CONST | ECF_PURE))
29f1e2b1
JH
673 && !(flags & ECF_LOOPING_CONST_OR_PURE)
674 && opt_for_fn (node->decl, optimize))
7f7beb3f
JH
675 {
676 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
677 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
678 }
679
24d3f325
NS
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. */
7f7beb3f
JH
683 if (DECL_EXTERNAL (node->decl) || !node->definition)
684 {
685 node->force_output = 0;
686 node->forced_by_abi = 0;
687 }
688
689 /* C++ FE on lack of COMDAT support create local COMDAT functions
67914693 690 (that ought to be shared but cannot due to object format
7f7beb3f
JH
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;
695
696 /* For external decls stop tracking same_comdat_group. It doesn't matter
71713fa6
JH
697 what comdat group they are in when they won't be emitted in this TU.
698
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)
7f7beb3f 703 {
b2b29377
MM
704 if (flag_checking)
705 {
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));
712 }
d52f5295 713 node->dissolve_same_comdat_group_list ();
7f7beb3f
JH
714 }
715 gcc_assert ((!DECL_WEAK (node->decl)
da66d596 716 && !DECL_COMDAT (node->decl))
7f7beb3f
JH
717 || TREE_PUBLIC (node->decl)
718 || node->weakref
719 || DECL_EXTERNAL (node->decl));
720 if (cgraph_externally_visible_p (node, whole_program))
721 {
a62bfab5 722 gcc_assert (!node->inlined_to);
7f7beb3f
JH
723 node->externally_visible = true;
724 }
725 else
726 {
727 node->externally_visible = false;
728 node->forced_by_abi = false;
729 }
730 if (!node->externally_visible
731 && node->definition && !node->weakref
732 && !DECL_EXTERNAL (node->decl))
408de159 733 localize_node (whole_program, node);
7f7beb3f 734
67f3791f 735 if (node->thunk
7f7beb3f
JH
736 && TREE_PUBLIC (node->decl))
737 {
738 struct cgraph_node *decl_node = node;
739
d52f5295 740 decl_node = decl_node->callees->callee->function_symbol ();
7f7beb3f
JH
741
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))
745 {
746 gcc_checking_assert (DECL_COMDAT (node->decl)
747 == DECL_COMDAT (decl_node->decl));
d52f5295 748 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
7f7beb3f
JH
749 gcc_checking_assert (node->same_comdat_group);
750 }
751 node->forced_by_abi = decl_node->forced_by_abi;
752 if (DECL_EXTERNAL (decl_node->decl))
753 DECL_EXTERNAL (node->decl) = 1;
754 }
755
756 update_visibility_by_resolution_info (node);
ed2a53e7
JH
757 if (node->weakref)
758 optimize_weakref (node);
7f7beb3f
JH
759 }
760 FOR_EACH_DEFINED_FUNCTION (node)
761 {
87f94429
ML
762 if (!node->local)
763 node->local |= node->local_p ();
7f7beb3f 764
67914693
SL
765 /* If we know that function cannot be overwritten by a
766 different semantics and moreover its section cannot be
f95ff97f
NS
767 discarded, replace all direct calls by calls to an
768 noninterposable alias. This make dynamic linking cheaper and
769 enable more optimization.
7f7beb3f
JH
770
771 TODO: We can also update virtual tables. */
b4897212 772 if (node->callers
b4897212 773 && can_replace_by_local_alias (node))
7f7beb3f 774 {
d52f5295
ML
775 cgraph_node *alias = dyn_cast<cgraph_node *>
776 (node->noninterposable_alias ());
7f7beb3f
JH
777
778 if (alias && alias != node)
779 {
780 while (node->callers)
781 {
782 struct cgraph_edge *e = node->callers;
783
3dafb85c 784 e->redirect_callee (alias);
7f7beb3f
JH
785 if (gimple_has_body_p (e->caller->decl))
786 {
787 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
27c5a177 788 cgraph_edge::redirect_call_stmt_to_callee (e);
7f7beb3f
JH
789 pop_cfun ();
790 }
791 }
792 }
793 }
794 }
795 FOR_EACH_VARIABLE (vnode)
796 {
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));
67914693 802 /* In several cases declarations cannot be common:
7f7beb3f
JH
803
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:
811
812 static int a __attribute__ ((common))
813
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;
ed2a53e7
JH
825 if (vnode->weakref)
826 optimize_weakref (vnode);
7f7beb3f
JH
827 }
828 FOR_EACH_DEFINED_VARIABLE (vnode)
829 {
830 if (!vnode->definition)
831 continue;
9041d2e6 832 if (vnode->externally_visible_p ())
7f7beb3f
JH
833 vnode->externally_visible = true;
834 else
835 {
836 vnode->externally_visible = false;
837 vnode->forced_by_abi = false;
838 }
7861b648
AK
839 if (lookup_attribute ("no_reorder",
840 DECL_ATTRIBUTES (vnode->decl)))
841 vnode->no_reorder = 1;
408de159 842
7f7beb3f 843 if (!vnode->externally_visible
fb81a61b
JH
844 && !vnode->transparent_alias
845 && !DECL_EXTERNAL (vnode->decl))
408de159 846 localize_node (whole_program, vnode);
7f7beb3f 847
7f7beb3f 848 update_visibility_by_resolution_info (vnode);
07990a5e 849
862b3da6 850 /* Update virtual tables to point to local aliases where possible. */
07990a5e 851 if (DECL_VIRTUAL_P (vnode->decl)
ef68f4ab 852 && !DECL_EXTERNAL (vnode->decl))
07990a5e
JH
853 {
854 int i;
855 struct ipa_ref *ref;
856 bool found = false;
857
858 /* See if there is something to update. */
ff70d83c 859 for (i = 0; vnode->iterate_reference (i, ref); i++)
07990a5e
JH
860 if (ref->use == IPA_REF_ADDR
861 && can_replace_by_local_alias_in_vtable (ref->referred))
862 {
863 found = true;
864 break;
865 }
866 if (found)
867 {
6e2830c3 868 hash_set<tree> visited_nodes;
0b83e688 869
9041d2e6 870 vnode->get_constructor ();
07990a5e 871 walk_tree (&DECL_INITIAL (vnode->decl),
6e2830c3 872 update_vtable_references, NULL, &visited_nodes);
d122681a 873 vnode->remove_all_references ();
07990a5e
JH
874 record_references_in_initializer (vnode->decl, false);
875 }
876 }
7f7beb3f
JH
877 }
878
1d561e18
AK
879 if (symtab->state >= IPA_SSA)
880 {
881 FOR_EACH_VARIABLE (vnode)
882 {
883 tree decl = vnode->decl;
884
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 ())
890 {
891 enum tls_model new_model = decl_default_tls_model (decl);
82e629c2
AM
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);
1d561e18
AK
898 }
899 }
900 }
901
7f7beb3f
JH
902 if (dump_file)
903 {
904 fprintf (dump_file, "\nMarking local functions:");
905 FOR_EACH_DEFINED_FUNCTION (node)
87f94429 906 if (node->local)
3629ff8a 907 fprintf (dump_file, " %s", node->dump_name ());
7f7beb3f
JH
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)
3629ff8a 912 fprintf (dump_file, " %s", node->dump_name ());
7f7beb3f
JH
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)
3629ff8a 917 fprintf (dump_file, " %s", vnode->dump_name ());
7f7beb3f
JH
918 fprintf (dump_file, "\n\n");
919 }
3dafb85c 920 symtab->function_flags_ready = true;
7f7beb3f
JH
921 return 0;
922}
923
924/* Local function pass handling visibilities. This happens before LTO streaming
925 so in particular -fwhole-program should be ignored at this level. */
926
17795822
TS
927namespace {
928
929const pass_data pass_data_ipa_function_and_variable_visibility =
7f7beb3f
JH
930{
931 SIMPLE_IPA_PASS, /* type */
932 "visibility", /* name */
933 OPTGROUP_NONE, /* optinfo_flags */
7f7beb3f
JH
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 */
940};
941
942/* Bring functions local at LTO time with -fwhole-program. */
943
944static unsigned int
945whole_program_function_and_variable_visibility (void)
946{
947 function_and_variable_visibility (flag_whole_program);
29f1e2b1 948 if (optimize || in_lto_p)
2e14744f 949 ipa_discover_variable_flags ();
7f7beb3f
JH
950 return 0;
951}
952
17795822
TS
953} // anon namespace
954
955namespace {
956
957const pass_data pass_data_ipa_whole_program_visibility =
7f7beb3f
JH
958{
959 IPA_PASS, /* type */
960 "whole-program", /* name */
961 OPTGROUP_NONE, /* optinfo_flags */
7f7beb3f
JH
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 */
968};
969
17795822 970class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
7f7beb3f
JH
971{
972public:
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 */
984 {}
985
986 /* opt_pass methods: */
987
725793af 988 bool gate (function *) final override
7f7beb3f
JH
989 {
990 /* Do not re-run on ltrans stage. */
991 return !flag_ltrans;
992 }
725793af 993 unsigned int execute (function *) final override
7f7beb3f
JH
994 {
995 return whole_program_function_and_variable_visibility ();
996 }
997
998}; // class pass_ipa_whole_program_visibility
999
17795822
TS
1000} // anon namespace
1001
7f7beb3f
JH
1002ipa_opt_pass_d *
1003make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1004{
1005 return new pass_ipa_whole_program_visibility (ctxt);
1006}
1007
1008class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1009{
1010public:
1011 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1012 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1013 ctxt)
1014 {}
1015
1016 /* opt_pass methods: */
725793af 1017 unsigned int execute (function *) final override
7f7beb3f
JH
1018 {
1019 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1020 }
1021
1022}; // class pass_ipa_function_and_variable_visibility
1023
1024simple_ipa_opt_pass *
1025make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1026{
1027 return new pass_ipa_function_and_variable_visibility (ctxt);
1028}