Add support for ARMv8-R architecture
[official-gcc.git] / gcc / ipa-visibility.c
blobda4a22e7329cb95ac2ff4aa9a572dc48d9e7fea8
1 /* IPA visibility pass
2 Copyright (C) 2003-2017 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 local calling
94 convetions. */
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
101 && node->get_availability () >= AVAIL_AVAILABLE);
104 /* Return true when function can be marked local. */
106 bool
107 cgraph_node::local_p (void)
109 cgraph_node *n = ultimate_alias_target ();
111 if (n->thunk.thunk_p)
112 return n->callees->callee->local_p ();
113 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
114 NULL, true);
118 /* A helper for comdat_can_be_unshared_p. */
120 static bool
121 comdat_can_be_unshared_p_1 (symtab_node *node)
123 if (!node->externally_visible)
124 return true;
125 if (node->address_can_be_compared_p ())
127 struct ipa_ref *ref;
129 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
130 if (ref->address_matters_p ())
131 return false;
134 /* If the symbol is used in some weird way, better to not touch it. */
135 if (node->force_output)
136 return false;
138 /* Explicit instantiations needs to be output when possibly
139 used externally. */
140 if (node->forced_by_abi
141 && TREE_PUBLIC (node->decl)
142 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
143 && !flag_whole_program))
144 return false;
146 /* Non-readonly and volatile variables can not be duplicated. */
147 if (is_a <varpool_node *> (node)
148 && (!TREE_READONLY (node->decl)
149 || TREE_THIS_VOLATILE (node->decl)))
150 return false;
151 return true;
154 /* COMDAT functions must be shared only if they have address taken,
155 otherwise we can produce our own private implementation with
156 -fwhole-program.
157 Return true when turning COMDAT function static can not lead to wrong
158 code when the resulting object links with a library defining same COMDAT.
160 Virtual functions do have their addresses taken from the vtables,
161 but in C++ there is no way to compare their addresses for equality. */
163 static bool
164 comdat_can_be_unshared_p (symtab_node *node)
166 if (!comdat_can_be_unshared_p_1 (node))
167 return false;
168 if (node->same_comdat_group)
170 symtab_node *next;
172 /* If more than one function is in the same COMDAT group, it must
173 be shared even if just one function in the comdat group has
174 address taken. */
175 for (next = node->same_comdat_group;
176 next != node; next = next->same_comdat_group)
177 if (!comdat_can_be_unshared_p_1 (next))
178 return false;
180 return true;
183 /* Return true when function NODE should be considered externally visible. */
185 static bool
186 cgraph_externally_visible_p (struct cgraph_node *node,
187 bool whole_program)
189 while (node->transparent_alias && node->definition)
190 node = node->get_alias_target ();
191 if (!node->definition)
192 return false;
193 if (!TREE_PUBLIC (node->decl)
194 || DECL_EXTERNAL (node->decl))
195 return false;
197 /* Do not try to localize built-in functions yet. One of problems is that we
198 end up mangling their asm for WHOPR that makes it impossible to call them
199 using the implicit built-in declarations anymore. Similarly this enables
200 us to remove them as unreachable before actual calls may appear during
201 expansion or folding. */
202 if (DECL_BUILT_IN (node->decl))
203 return true;
205 /* If linker counts on us, we must preserve the function. */
206 if (node->used_from_object_file_p ())
207 return true;
208 if (DECL_PRESERVE_P (node->decl))
209 return true;
210 if (lookup_attribute ("externally_visible",
211 DECL_ATTRIBUTES (node->decl)))
212 return true;
213 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
214 && lookup_attribute ("dllexport",
215 DECL_ATTRIBUTES (node->decl)))
216 return true;
217 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
218 return false;
219 /* When doing LTO or whole program, we can bring COMDAT functoins static.
220 This improves code quality and we know we will duplicate them at most twice
221 (in the case that we are not using plugin and link with object file
222 implementing same COMDAT) */
223 if (((in_lto_p || whole_program) && !flag_incremental_link)
224 && DECL_COMDAT (node->decl)
225 && comdat_can_be_unshared_p (node))
226 return false;
228 /* When doing link time optimizations, hidden symbols become local. */
229 if ((in_lto_p && !flag_incremental_link)
230 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
231 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
232 /* Be sure that node is defined in IR file, not in other object
233 file. In that case we don't set used_from_other_object_file. */
234 && node->definition)
236 else if (!whole_program)
237 return true;
239 if (MAIN_NAME_P (DECL_NAME (node->decl)))
240 return true;
242 if (node->instrumentation_clone
243 && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
244 return true;
246 return false;
249 /* Return true when variable should be considered externally visible. */
251 bool
252 varpool_node::externally_visible_p (void)
254 while (transparent_alias && definition)
255 return get_alias_target ()->externally_visible_p ();
256 if (DECL_EXTERNAL (decl))
257 return true;
259 if (!TREE_PUBLIC (decl))
260 return false;
262 /* If linker counts on us, we must preserve the function. */
263 if (used_from_object_file_p ())
264 return true;
266 /* Bringing TLS variables local may cause dynamic linker failures
267 on limits of static TLS vars. */
268 if (DECL_THREAD_LOCAL_P (decl)
269 && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
270 && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
271 return true;
273 if (DECL_HARD_REGISTER (decl))
274 return true;
275 if (DECL_PRESERVE_P (decl))
276 return true;
277 if (lookup_attribute ("externally_visible",
278 DECL_ATTRIBUTES (decl)))
279 return true;
280 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
281 && lookup_attribute ("dllexport",
282 DECL_ATTRIBUTES (decl)))
283 return true;
285 /* See if we have linker information about symbol not being used or
286 if we need to make guess based on the declaration.
288 Even if the linker clams the symbol is unused, never bring internal
289 symbols that are declared by user as used or externally visible.
290 This is needed for i.e. references from asm statements. */
291 if (used_from_object_file_p ())
292 return true;
293 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
294 return false;
296 /* As a special case, the COMDAT virtual tables can be unshared.
297 In LTO mode turn vtables into static variables. The variable is readonly,
298 so this does not enable more optimization, but referring static var
299 is faster for dynamic linking. Also this match logic hidding vtables
300 from LTO symbol tables. */
301 if (((in_lto_p || flag_whole_program) && !flag_incremental_link)
302 && DECL_COMDAT (decl)
303 && comdat_can_be_unshared_p (this))
304 return false;
306 /* When doing link time optimizations, hidden symbols become local. */
307 if (in_lto_p && !flag_incremental_link
308 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
309 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
310 /* Be sure that node is defined in IR file, not in other object
311 file. In that case we don't set used_from_other_object_file. */
312 && definition)
314 else if (!flag_whole_program)
315 return true;
317 /* Do not attempt to privatize COMDATS by default.
318 This would break linking with C++ libraries sharing
319 inline definitions.
321 FIXME: We can do so for readonly vars with no address taken and
322 possibly also for vtables since no direct pointer comparsion is done.
323 It might be interesting to do so to reduce linking overhead. */
324 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
325 return true;
326 return false;
329 /* Return true if reference to NODE can be replaced by a local alias.
330 Local aliases save dynamic linking overhead and enable more optimizations.
333 static bool
334 can_replace_by_local_alias (symtab_node *node)
336 #ifndef ASM_OUTPUT_DEF
337 /* If aliases aren't supported, we can't do replacement. */
338 return false;
339 #endif
340 /* Weakrefs have a reason to be non-local. Be sure we do not replace
341 them. */
342 while (node->transparent_alias && node->definition && !node->weakref)
343 node = node->get_alias_target ();
344 if (node->weakref)
345 return false;
347 return (node->get_availability () > AVAIL_INTERPOSABLE
348 && !decl_binds_to_current_def_p (node->decl)
349 && !node->can_be_discarded_p ());
352 /* Return true if we can replace reference to NODE by local alias
353 within a virtual table. Generally we can replace function pointers
354 and virtual table pointers. */
356 static bool
357 can_replace_by_local_alias_in_vtable (symtab_node *node)
359 if (is_a <varpool_node *> (node)
360 && !DECL_VIRTUAL_P (node->decl))
361 return false;
362 return can_replace_by_local_alias (node);
365 /* walk_tree callback that rewrites initializer references. */
367 static tree
368 update_vtable_references (tree *tp, int *walk_subtrees,
369 void *data ATTRIBUTE_UNUSED)
371 if (VAR_OR_FUNCTION_DECL_P (*tp))
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 /* NODE is an externally visible definition, which we've discovered is
534 not needed externally. Make it local to this compilation. */
536 static void
537 localize_node (bool whole_program, symtab_node *node)
539 gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
541 /* It is possible that one comdat group contains both hidden and non-hidden
542 symbols. In this case we can privatize all hidden symbol but we need
543 to keep non-hidden exported. */
544 if (node->same_comdat_group
545 && node->resolution == LDPR_PREVAILING_DEF_IRONLY)
547 symtab_node *next;
548 for (next = node->same_comdat_group;
549 next != node; next = next->same_comdat_group)
550 if (next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
551 || next->resolution == LDPR_PREVAILING_DEF)
552 break;
553 if (node != next)
555 if (!node->transparent_alias)
557 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
558 node->make_decl_local ();
559 if (!flag_incremental_link)
560 node->unique_name |= true;
561 return;
565 /* For similar reason do not privatize whole comdat when seeing comdat
566 local. Wait for non-comdat symbol to be privatized first. */
567 if (node->comdat_local_p ())
568 return;
570 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
572 for (symtab_node *next = node->same_comdat_group;
573 next != node; next = next->same_comdat_group)
575 next->set_comdat_group (NULL);
576 if (!next->alias)
577 next->set_section (NULL);
578 if (!next->transparent_alias)
579 next->make_decl_local ();
580 next->unique_name
581 |= ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
582 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
583 && TREE_PUBLIC (next->decl)
584 && !flag_incremental_link);
587 /* Now everything's localized, the grouping has no meaning, and
588 will cause crashes if we keep it around. */
589 node->dissolve_same_comdat_group_list ();
592 node->unique_name
593 |= ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
594 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
595 && TREE_PUBLIC (node->decl)
596 && !flag_incremental_link);
598 if (TREE_PUBLIC (node->decl))
599 node->set_comdat_group (NULL);
600 if (DECL_COMDAT (node->decl) && !node->alias)
601 node->set_section (NULL);
602 if (!node->transparent_alias)
604 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
605 node->make_decl_local ();
609 /* Decide on visibility of all symbols. */
611 static unsigned int
612 function_and_variable_visibility (bool whole_program)
614 struct cgraph_node *node;
615 varpool_node *vnode;
617 /* All aliases should be procssed at this point. */
618 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
620 FOR_EACH_FUNCTION (node)
622 int flags = flags_from_decl_or_type (node->decl);
624 /* Optimize away PURE and CONST constructors and destructors. */
625 if (optimize
626 && (flags & (ECF_CONST | ECF_PURE))
627 && !(flags & ECF_LOOPING_CONST_OR_PURE))
629 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
630 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
633 /* Frontends and alias code marks nodes as needed before parsing
634 is finished. We may end up marking as node external nodes
635 where this flag is meaningless strip it. */
636 if (DECL_EXTERNAL (node->decl) || !node->definition)
638 node->force_output = 0;
639 node->forced_by_abi = 0;
642 /* C++ FE on lack of COMDAT support create local COMDAT functions
643 (that ought to be shared but can not due to object format
644 limitations). It is necessary to keep the flag to make rest of C++ FE
645 happy. Clear the flag here to avoid confusion in middle-end. */
646 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
647 DECL_COMDAT (node->decl) = 0;
649 /* For external decls stop tracking same_comdat_group. It doesn't matter
650 what comdat group they are in when they won't be emitted in this TU.
652 An exception is LTO where we may end up with both external
653 and non-external declarations in the same comdat group in
654 the case declarations was not merged. */
655 if (node->same_comdat_group && DECL_EXTERNAL (node->decl) && !in_lto_p)
657 if (flag_checking)
659 for (symtab_node *n = node->same_comdat_group;
660 n != node;
661 n = n->same_comdat_group)
662 /* If at least one of same comdat group functions is external,
663 all of them have to be, otherwise it is a front-end bug. */
664 gcc_assert (DECL_EXTERNAL (n->decl));
666 node->dissolve_same_comdat_group_list ();
668 gcc_assert ((!DECL_WEAK (node->decl)
669 && !DECL_COMDAT (node->decl))
670 || TREE_PUBLIC (node->decl)
671 || node->weakref
672 || DECL_EXTERNAL (node->decl));
673 if (cgraph_externally_visible_p (node, whole_program))
675 gcc_assert (!node->global.inlined_to);
676 node->externally_visible = true;
678 else
680 node->externally_visible = false;
681 node->forced_by_abi = false;
683 if (!node->externally_visible
684 && node->definition && !node->weakref
685 && !DECL_EXTERNAL (node->decl))
686 localize_node (whole_program, node);
688 if (node->thunk.thunk_p
689 && !node->thunk.add_pointer_bounds_args
690 && TREE_PUBLIC (node->decl))
692 struct cgraph_node *decl_node = node;
694 decl_node = decl_node->callees->callee->function_symbol ();
696 /* Thunks have the same visibility as function they are attached to.
697 Make sure the C++ front end set this up properly. */
698 if (DECL_ONE_ONLY (decl_node->decl))
700 gcc_checking_assert (DECL_COMDAT (node->decl)
701 == DECL_COMDAT (decl_node->decl));
702 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
703 gcc_checking_assert (node->same_comdat_group);
705 node->forced_by_abi = decl_node->forced_by_abi;
706 if (DECL_EXTERNAL (decl_node->decl))
707 DECL_EXTERNAL (node->decl) = 1;
710 update_visibility_by_resolution_info (node);
711 if (node->weakref)
712 optimize_weakref (node);
714 FOR_EACH_DEFINED_FUNCTION (node)
716 if (!node->local.local)
717 node->local.local |= node->local_p ();
719 /* If we know that function can not be overwritten by a
720 different semantics and moreover its section can not be
721 discarded, replace all direct calls by calls to an
722 noninterposable alias. This make dynamic linking cheaper and
723 enable more optimization.
725 TODO: We can also update virtual tables. */
726 if (node->callers
727 && can_replace_by_local_alias (node))
729 cgraph_node *alias = dyn_cast<cgraph_node *>
730 (node->noninterposable_alias ());
732 if (alias && alias != node)
734 while (node->callers)
736 struct cgraph_edge *e = node->callers;
738 e->redirect_callee (alias);
739 if (gimple_has_body_p (e->caller->decl))
741 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
742 e->redirect_call_stmt_to_callee ();
743 pop_cfun ();
749 FOR_EACH_VARIABLE (vnode)
751 /* weak flag makes no sense on local variables. */
752 gcc_assert (!DECL_WEAK (vnode->decl)
753 || vnode->weakref
754 || TREE_PUBLIC (vnode->decl)
755 || DECL_EXTERNAL (vnode->decl));
756 /* In several cases declarations can not be common:
758 - when declaration has initializer
759 - when it is in weak
760 - when it has specific section
761 - when it resides in non-generic address space.
762 - if declaration is local, it will get into .local common section
763 so common flag is not needed. Frontends still produce these in
764 certain cases, such as for:
766 static int a __attribute__ ((common))
768 Canonicalize things here and clear the redundant flag. */
769 if (DECL_COMMON (vnode->decl)
770 && (!(TREE_PUBLIC (vnode->decl)
771 || DECL_EXTERNAL (vnode->decl))
772 || (DECL_INITIAL (vnode->decl)
773 && DECL_INITIAL (vnode->decl) != error_mark_node)
774 || DECL_WEAK (vnode->decl)
775 || DECL_SECTION_NAME (vnode->decl) != NULL
776 || ! (ADDR_SPACE_GENERIC_P
777 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
778 DECL_COMMON (vnode->decl) = 0;
779 if (vnode->weakref)
780 optimize_weakref (vnode);
782 FOR_EACH_DEFINED_VARIABLE (vnode)
784 if (!vnode->definition)
785 continue;
786 if (vnode->externally_visible_p ())
787 vnode->externally_visible = true;
788 else
790 vnode->externally_visible = false;
791 vnode->forced_by_abi = false;
793 if (lookup_attribute ("no_reorder",
794 DECL_ATTRIBUTES (vnode->decl)))
795 vnode->no_reorder = 1;
797 if (!vnode->externally_visible
798 && !vnode->transparent_alias
799 && !DECL_EXTERNAL (vnode->decl))
800 localize_node (whole_program, vnode);
802 update_visibility_by_resolution_info (vnode);
804 /* Update virtual tables to point to local aliases where possible. */
805 if (DECL_VIRTUAL_P (vnode->decl)
806 && !DECL_EXTERNAL (vnode->decl))
808 int i;
809 struct ipa_ref *ref;
810 bool found = false;
812 /* See if there is something to update. */
813 for (i = 0; vnode->iterate_reference (i, ref); i++)
814 if (ref->use == IPA_REF_ADDR
815 && can_replace_by_local_alias_in_vtable (ref->referred))
817 found = true;
818 break;
820 if (found)
822 hash_set<tree> visited_nodes;
824 vnode->get_constructor ();
825 walk_tree (&DECL_INITIAL (vnode->decl),
826 update_vtable_references, NULL, &visited_nodes);
827 vnode->remove_all_references ();
828 record_references_in_initializer (vnode->decl, false);
833 if (dump_file)
835 fprintf (dump_file, "\nMarking local functions:");
836 FOR_EACH_DEFINED_FUNCTION (node)
837 if (node->local.local)
838 fprintf (dump_file, " %s", node->name ());
839 fprintf (dump_file, "\n\n");
840 fprintf (dump_file, "\nMarking externally visible functions:");
841 FOR_EACH_DEFINED_FUNCTION (node)
842 if (node->externally_visible)
843 fprintf (dump_file, " %s", node->name ());
844 fprintf (dump_file, "\n\n");
845 fprintf (dump_file, "\nMarking externally visible variables:");
846 FOR_EACH_DEFINED_VARIABLE (vnode)
847 if (vnode->externally_visible)
848 fprintf (dump_file, " %s", vnode->name ());
849 fprintf (dump_file, "\n\n");
851 symtab->function_flags_ready = true;
852 return 0;
855 /* Local function pass handling visibilities. This happens before LTO streaming
856 so in particular -fwhole-program should be ignored at this level. */
858 namespace {
860 const pass_data pass_data_ipa_function_and_variable_visibility =
862 SIMPLE_IPA_PASS, /* type */
863 "visibility", /* name */
864 OPTGROUP_NONE, /* optinfo_flags */
865 TV_CGRAPHOPT, /* tv_id */
866 0, /* properties_required */
867 0, /* properties_provided */
868 0, /* properties_destroyed */
869 0, /* todo_flags_start */
870 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
873 /* Bring functions local at LTO time with -fwhole-program. */
875 static unsigned int
876 whole_program_function_and_variable_visibility (void)
878 function_and_variable_visibility (flag_whole_program);
879 if (optimize)
880 ipa_discover_readonly_nonaddressable_vars ();
881 return 0;
884 } // anon namespace
886 namespace {
888 const pass_data pass_data_ipa_whole_program_visibility =
890 IPA_PASS, /* type */
891 "whole-program", /* name */
892 OPTGROUP_NONE, /* optinfo_flags */
893 TV_CGRAPHOPT, /* tv_id */
894 0, /* properties_required */
895 0, /* properties_provided */
896 0, /* properties_destroyed */
897 0, /* todo_flags_start */
898 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
901 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
903 public:
904 pass_ipa_whole_program_visibility (gcc::context *ctxt)
905 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
906 NULL, /* generate_summary */
907 NULL, /* write_summary */
908 NULL, /* read_summary */
909 NULL, /* write_optimization_summary */
910 NULL, /* read_optimization_summary */
911 NULL, /* stmt_fixup */
912 0, /* function_transform_todo_flags_start */
913 NULL, /* function_transform */
914 NULL) /* variable_transform */
917 /* opt_pass methods: */
919 virtual bool gate (function *)
921 /* Do not re-run on ltrans stage. */
922 return !flag_ltrans;
924 virtual unsigned int execute (function *)
926 return whole_program_function_and_variable_visibility ();
929 }; // class pass_ipa_whole_program_visibility
931 } // anon namespace
933 ipa_opt_pass_d *
934 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
936 return new pass_ipa_whole_program_visibility (ctxt);
939 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
941 public:
942 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
943 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
944 ctxt)
947 /* opt_pass methods: */
948 virtual unsigned int execute (function *)
950 return function_and_variable_visibility (flag_whole_program && !flag_lto);
953 }; // class pass_ipa_function_and_variable_visibility
955 simple_ipa_opt_pass *
956 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
958 return new pass_ipa_function_and_variable_visibility (ctxt);