re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / ipa-visibility.c
1 /* IPA visibility pass
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
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
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.
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"
79 #include "alias.h"
80 #include "symtab.h"
81 #include "tree.h"
82 #include "hard-reg-set.h"
83 #include "function.h"
84 #include "cgraph.h"
85 #include "tree-pass.h"
86 #include "calls.h"
87 #include "gimple-expr.h"
88 #include "varasm.h"
89
90 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
91
92 static bool
93 non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
94 {
95 return !(node->only_called_directly_or_aliased_p ()
96 /* i386 would need update to output thunk with locak calling
97 ocnvetions. */
98 && !node->thunk.thunk_p
99 && node->definition
100 && !DECL_EXTERNAL (node->decl)
101 && !node->externally_visible
102 && !node->used_from_other_partition
103 && !node->in_other_partition);
104 }
105
106 /* Return true when function can be marked local. */
107
108 bool
109 cgraph_node::local_p (void)
110 {
111 cgraph_node *n = ultimate_alias_target ();
112
113 if (n->thunk.thunk_p)
114 return n->callees->callee->local_p ();
115 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
116 NULL, true);
117
118 }
119
120 /* A helper for comdat_can_be_unshared_p. */
121
122 static bool
123 comdat_can_be_unshared_p_1 (symtab_node *node)
124 {
125 if (!node->externally_visible)
126 return true;
127 if (node->address_can_be_compared_p ())
128 {
129 struct ipa_ref *ref;
130
131 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
132 if (ref->address_matters_p ())
133 return false;
134 }
135
136 /* If the symbol is used in some weird way, better to not touch it. */
137 if (node->force_output)
138 return false;
139
140 /* Explicit instantiations needs to be output when possibly
141 used externally. */
142 if (node->forced_by_abi
143 && TREE_PUBLIC (node->decl)
144 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
145 && !flag_whole_program))
146 return false;
147
148 /* Non-readonly and volatile variables can not be duplicated. */
149 if (is_a <varpool_node *> (node)
150 && (!TREE_READONLY (node->decl)
151 || TREE_THIS_VOLATILE (node->decl)))
152 return false;
153 return true;
154 }
155
156 /* COMDAT functions must be shared only if they have address taken,
157 otherwise we can produce our own private implementation with
158 -fwhole-program.
159 Return true when turning COMDAT functoin static can not lead to wrong
160 code when the resulting object links with a library defining same COMDAT.
161
162 Virtual functions do have their addresses taken from the vtables,
163 but in C++ there is no way to compare their addresses for equality. */
164
165 static bool
166 comdat_can_be_unshared_p (symtab_node *node)
167 {
168 if (!comdat_can_be_unshared_p_1 (node))
169 return false;
170 if (node->same_comdat_group)
171 {
172 symtab_node *next;
173
174 /* If more than one function is in the same COMDAT group, it must
175 be shared even if just one function in the comdat group has
176 address taken. */
177 for (next = node->same_comdat_group;
178 next != node; next = next->same_comdat_group)
179 if (!comdat_can_be_unshared_p_1 (next))
180 return false;
181 }
182 return true;
183 }
184
185 /* Return true when function NODE should be considered externally visible. */
186
187 static bool
188 cgraph_externally_visible_p (struct cgraph_node *node,
189 bool whole_program)
190 {
191 if (!node->definition)
192 return false;
193 if (!TREE_PUBLIC (node->decl)
194 || DECL_EXTERNAL (node->decl))
195 return false;
196
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;
204
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)
224 && DECL_COMDAT (node->decl)
225 && comdat_can_be_unshared_p (node))
226 return false;
227
228 /* When doing link time optimizations, hidden symbols become local. */
229 if (in_lto_p
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)
235 ;
236 else if (!whole_program)
237 return true;
238
239 if (MAIN_NAME_P (DECL_NAME (node->decl)))
240 return true;
241
242 if (node->instrumentation_clone
243 && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
244 return true;
245
246 return false;
247 }
248
249 /* Return true when variable should be considered externally visible. */
250
251 bool
252 varpool_node::externally_visible_p (void)
253 {
254 if (DECL_EXTERNAL (decl))
255 return true;
256
257 if (!TREE_PUBLIC (decl))
258 return false;
259
260 /* If linker counts on us, we must preserve the function. */
261 if (used_from_object_file_p ())
262 return true;
263
264 /* Bringing TLS variables local may cause dynamic linker failures
265 on limits of static TLS vars. */
266 if (DECL_THREAD_LOCAL_P (decl)
267 && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
268 && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
269 return true;
270
271 if (DECL_HARD_REGISTER (decl))
272 return true;
273 if (DECL_PRESERVE_P (decl))
274 return true;
275 if (lookup_attribute ("externally_visible",
276 DECL_ATTRIBUTES (decl)))
277 return true;
278 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
279 && lookup_attribute ("dllexport",
280 DECL_ATTRIBUTES (decl)))
281 return true;
282
283 /* See if we have linker information about symbol not being used or
284 if we need to make guess based on the declaration.
285
286 Even if the linker clams the symbol is unused, never bring internal
287 symbols that are declared by user as used or externally visible.
288 This is needed for i.e. references from asm statements. */
289 if (used_from_object_file_p ())
290 return true;
291 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
292 return false;
293
294 /* As a special case, the COMDAT virtual tables can be unshared.
295 In LTO mode turn vtables into static variables. The variable is readonly,
296 so this does not enable more optimization, but referring static var
297 is faster for dynamic linking. Also this match logic hidding vtables
298 from LTO symbol tables. */
299 if ((in_lto_p || flag_whole_program)
300 && DECL_COMDAT (decl)
301 && comdat_can_be_unshared_p (this))
302 return false;
303
304 /* When doing link time optimizations, hidden symbols become local. */
305 if (in_lto_p
306 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
307 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
308 /* Be sure that node is defined in IR file, not in other object
309 file. In that case we don't set used_from_other_object_file. */
310 && definition)
311 ;
312 else if (!flag_whole_program)
313 return true;
314
315 /* Do not attempt to privatize COMDATS by default.
316 This would break linking with C++ libraries sharing
317 inline definitions.
318
319 FIXME: We can do so for readonly vars with no address taken and
320 possibly also for vtables since no direct pointer comparsion is done.
321 It might be interesting to do so to reduce linking overhead. */
322 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
323 return true;
324 return false;
325 }
326
327 /* Return true if reference to NODE can be replaced by a local alias.
328 Local aliases save dynamic linking overhead and enable more optimizations.
329 */
330
331 bool
332 can_replace_by_local_alias (symtab_node *node)
333 {
334 return (node->get_availability () > AVAIL_INTERPOSABLE
335 && !decl_binds_to_current_def_p (node->decl)
336 && !node->can_be_discarded_p ());
337 }
338
339 /* Return true if we can replace refernece to NODE by local alias
340 within a virtual table. Generally we can replace function pointers
341 and virtual table pointers. */
342
343 bool
344 can_replace_by_local_alias_in_vtable (symtab_node *node)
345 {
346 if (is_a <varpool_node *> (node)
347 && !DECL_VIRTUAL_P (node->decl))
348 return false;
349 return can_replace_by_local_alias (node);
350 }
351
352 /* walk_tree callback that rewrites initializer references. */
353
354 static tree
355 update_vtable_references (tree *tp, int *walk_subtrees,
356 void *data ATTRIBUTE_UNUSED)
357 {
358 if (TREE_CODE (*tp) == VAR_DECL
359 || TREE_CODE (*tp) == FUNCTION_DECL)
360 {
361 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
362 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
363 *walk_subtrees = 0;
364 }
365 else if (IS_TYPE_OR_DECL_P (*tp))
366 *walk_subtrees = 0;
367 return NULL;
368 }
369
370 /* In LTO we can remove COMDAT groups and weak symbols.
371 Either turn them into normal symbols or external symbol depending on
372 resolution info. */
373
374 static void
375 update_visibility_by_resolution_info (symtab_node * node)
376 {
377 bool define;
378
379 if (!node->externally_visible
380 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
381 || node->resolution == LDPR_UNKNOWN)
382 return;
383
384 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
385 || node->resolution == LDPR_PREVAILING_DEF
386 || node->resolution == LDPR_UNDEF
387 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
388
389 /* The linker decisions ought to agree in the whole group. */
390 if (node->same_comdat_group)
391 for (symtab_node *next = node->same_comdat_group;
392 next != node; next = next->same_comdat_group)
393 {
394 if (!next->externally_visible)
395 continue;
396
397 bool same_def
398 = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
399 || next->resolution == LDPR_PREVAILING_DEF
400 || next->resolution == LDPR_UNDEF
401 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
402 gcc_assert (in_lto_p || same_def);
403 if (!same_def)
404 return;
405 }
406
407 if (node->same_comdat_group)
408 for (symtab_node *next = node->same_comdat_group;
409 next != node; next = next->same_comdat_group)
410 {
411 next->set_comdat_group (NULL);
412 DECL_WEAK (next->decl) = false;
413 if (next->externally_visible
414 && !define)
415 DECL_EXTERNAL (next->decl) = true;
416 }
417 node->set_comdat_group (NULL);
418 DECL_WEAK (node->decl) = false;
419 if (!define)
420 DECL_EXTERNAL (node->decl) = true;
421 node->dissolve_same_comdat_group_list ();
422 }
423
424 /* Decide on visibility of all symbols. */
425
426 static unsigned int
427 function_and_variable_visibility (bool whole_program)
428 {
429 struct cgraph_node *node;
430 varpool_node *vnode;
431
432 /* All aliases should be procssed at this point. */
433 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
434
435 FOR_EACH_FUNCTION (node)
436 {
437 int flags = flags_from_decl_or_type (node->decl);
438
439 /* Optimize away PURE and CONST constructors and destructors. */
440 if (optimize
441 && (flags & (ECF_CONST | ECF_PURE))
442 && !(flags & ECF_LOOPING_CONST_OR_PURE))
443 {
444 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
445 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
446 }
447
448 /* Frontends and alias code marks nodes as needed before parsing is finished.
449 We may end up marking as node external nodes where this flag is meaningless
450 strip it. */
451 if (DECL_EXTERNAL (node->decl) || !node->definition)
452 {
453 node->force_output = 0;
454 node->forced_by_abi = 0;
455 }
456
457 /* C++ FE on lack of COMDAT support create local COMDAT functions
458 (that ought to be shared but can not due to object format
459 limitations). It is necessary to keep the flag to make rest of C++ FE
460 happy. Clear the flag here to avoid confusion in middle-end. */
461 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
462 DECL_COMDAT (node->decl) = 0;
463
464 /* For external decls stop tracking same_comdat_group. It doesn't matter
465 what comdat group they are in when they won't be emitted in this TU. */
466 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
467 {
468 #ifdef ENABLE_CHECKING
469 symtab_node *n;
470
471 for (n = node->same_comdat_group;
472 n != node;
473 n = n->same_comdat_group)
474 /* If at least one of same comdat group functions is external,
475 all of them have to be, otherwise it is a front-end bug. */
476 gcc_assert (DECL_EXTERNAL (n->decl));
477 #endif
478 node->dissolve_same_comdat_group_list ();
479 }
480 gcc_assert ((!DECL_WEAK (node->decl)
481 && !DECL_COMDAT (node->decl))
482 || TREE_PUBLIC (node->decl)
483 || node->weakref
484 || DECL_EXTERNAL (node->decl));
485 if (cgraph_externally_visible_p (node, whole_program))
486 {
487 gcc_assert (!node->global.inlined_to);
488 node->externally_visible = true;
489 }
490 else
491 {
492 node->externally_visible = false;
493 node->forced_by_abi = false;
494 }
495 if (!node->externally_visible
496 && node->definition && !node->weakref
497 && !DECL_EXTERNAL (node->decl))
498 {
499 gcc_assert (whole_program || in_lto_p
500 || !TREE_PUBLIC (node->decl));
501 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
502 || node->unique_name
503 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
504 && TREE_PUBLIC (node->decl));
505 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
506 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
507 {
508 symtab_node *next = node;
509
510 /* Set all members of comdat group local. */
511 if (node->same_comdat_group)
512 for (next = node->same_comdat_group;
513 next != node;
514 next = next->same_comdat_group)
515 {
516 next->set_comdat_group (NULL);
517 if (!next->alias)
518 next->set_section (NULL);
519 next->make_decl_local ();
520 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
521 || next->unique_name
522 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
523 && TREE_PUBLIC (next->decl));
524 }
525 /* cgraph_externally_visible_p has already checked all other nodes
526 in the group and they will all be made local. We need to
527 dissolve the group at once so that the predicate does not
528 segfault though. */
529 node->dissolve_same_comdat_group_list ();
530 }
531 if (TREE_PUBLIC (node->decl))
532 node->set_comdat_group (NULL);
533 if (DECL_COMDAT (node->decl) && !node->alias)
534 node->set_section (NULL);
535 node->make_decl_local ();
536 }
537
538 if (node->thunk.thunk_p
539 && !node->thunk.add_pointer_bounds_args
540 && TREE_PUBLIC (node->decl))
541 {
542 struct cgraph_node *decl_node = node;
543
544 decl_node = decl_node->callees->callee->function_symbol ();
545
546 /* Thunks have the same visibility as function they are attached to.
547 Make sure the C++ front end set this up properly. */
548 if (DECL_ONE_ONLY (decl_node->decl))
549 {
550 gcc_checking_assert (DECL_COMDAT (node->decl)
551 == DECL_COMDAT (decl_node->decl));
552 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
553 gcc_checking_assert (node->same_comdat_group);
554 }
555 node->forced_by_abi = decl_node->forced_by_abi;
556 if (DECL_EXTERNAL (decl_node->decl))
557 DECL_EXTERNAL (node->decl) = 1;
558 }
559
560 update_visibility_by_resolution_info (node);
561 }
562 FOR_EACH_DEFINED_FUNCTION (node)
563 {
564 if (!node->local.local)
565 node->local.local |= node->local_p ();
566
567 /* If we know that function can not be overwritten by a different semantics
568 and moreover its section can not be discarded, replace all direct calls
569 by calls to an noninterposable alias. This make dynamic linking
570 cheaper and enable more optimization.
571
572 TODO: We can also update virtual tables. */
573 if (node->callers
574 && can_replace_by_local_alias (node))
575 {
576 cgraph_node *alias = dyn_cast<cgraph_node *>
577 (node->noninterposable_alias ());
578
579 if (alias && alias != node)
580 {
581 while (node->callers)
582 {
583 struct cgraph_edge *e = node->callers;
584
585 e->redirect_callee (alias);
586 if (gimple_has_body_p (e->caller->decl))
587 {
588 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
589 e->redirect_call_stmt_to_callee ();
590 pop_cfun ();
591 }
592 }
593 }
594 }
595 }
596 FOR_EACH_VARIABLE (vnode)
597 {
598 /* weak flag makes no sense on local variables. */
599 gcc_assert (!DECL_WEAK (vnode->decl)
600 || vnode->weakref
601 || TREE_PUBLIC (vnode->decl)
602 || DECL_EXTERNAL (vnode->decl));
603 /* In several cases declarations can not be common:
604
605 - when declaration has initializer
606 - when it is in weak
607 - when it has specific section
608 - when it resides in non-generic address space.
609 - if declaration is local, it will get into .local common section
610 so common flag is not needed. Frontends still produce these in
611 certain cases, such as for:
612
613 static int a __attribute__ ((common))
614
615 Canonicalize things here and clear the redundant flag. */
616 if (DECL_COMMON (vnode->decl)
617 && (!(TREE_PUBLIC (vnode->decl)
618 || DECL_EXTERNAL (vnode->decl))
619 || (DECL_INITIAL (vnode->decl)
620 && DECL_INITIAL (vnode->decl) != error_mark_node)
621 || DECL_WEAK (vnode->decl)
622 || DECL_SECTION_NAME (vnode->decl) != NULL
623 || ! (ADDR_SPACE_GENERIC_P
624 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
625 DECL_COMMON (vnode->decl) = 0;
626 }
627 FOR_EACH_DEFINED_VARIABLE (vnode)
628 {
629 if (!vnode->definition)
630 continue;
631 if (vnode->externally_visible_p ())
632 vnode->externally_visible = true;
633 else
634 {
635 vnode->externally_visible = false;
636 vnode->forced_by_abi = false;
637 }
638 if (lookup_attribute ("no_reorder",
639 DECL_ATTRIBUTES (vnode->decl)))
640 vnode->no_reorder = 1;
641 if (!vnode->externally_visible
642 && !vnode->weakref)
643 {
644 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
645 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
646 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
647 && TREE_PUBLIC (vnode->decl));
648 if (vnode->same_comdat_group && TREE_PUBLIC (vnode->decl))
649 {
650 symtab_node *next = vnode;
651
652 /* Set all members of comdat group local. */
653 if (vnode->same_comdat_group)
654 for (next = vnode->same_comdat_group;
655 next != vnode;
656 next = next->same_comdat_group)
657 {
658 next->set_comdat_group (NULL);
659 if (!next->alias)
660 next->set_section (NULL);
661 next->make_decl_local ();
662 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
663 || next->unique_name
664 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
665 && TREE_PUBLIC (next->decl));
666 }
667 vnode->dissolve_same_comdat_group_list ();
668 }
669 if (TREE_PUBLIC (vnode->decl))
670 vnode->set_comdat_group (NULL);
671 if (DECL_COMDAT (vnode->decl) && !vnode->alias)
672 vnode->set_section (NULL);
673 vnode->make_decl_local ();
674 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
675 }
676 update_visibility_by_resolution_info (vnode);
677
678 /* Update virtual tables to point to local aliases where possible. */
679 if (DECL_VIRTUAL_P (vnode->decl)
680 && !DECL_EXTERNAL (vnode->decl))
681 {
682 int i;
683 struct ipa_ref *ref;
684 bool found = false;
685
686 /* See if there is something to update. */
687 for (i = 0; vnode->iterate_referring (i, ref); i++)
688 if (ref->use == IPA_REF_ADDR
689 && can_replace_by_local_alias_in_vtable (ref->referred))
690 {
691 found = true;
692 break;
693 }
694 if (found)
695 {
696 hash_set<tree> visited_nodes;
697
698 vnode->get_constructor ();
699 walk_tree (&DECL_INITIAL (vnode->decl),
700 update_vtable_references, NULL, &visited_nodes);
701 vnode->remove_all_references ();
702 record_references_in_initializer (vnode->decl, false);
703 }
704 }
705 }
706
707 if (dump_file)
708 {
709 fprintf (dump_file, "\nMarking local functions:");
710 FOR_EACH_DEFINED_FUNCTION (node)
711 if (node->local.local)
712 fprintf (dump_file, " %s", node->name ());
713 fprintf (dump_file, "\n\n");
714 fprintf (dump_file, "\nMarking externally visible functions:");
715 FOR_EACH_DEFINED_FUNCTION (node)
716 if (node->externally_visible)
717 fprintf (dump_file, " %s", node->name ());
718 fprintf (dump_file, "\n\n");
719 fprintf (dump_file, "\nMarking externally visible variables:");
720 FOR_EACH_DEFINED_VARIABLE (vnode)
721 if (vnode->externally_visible)
722 fprintf (dump_file, " %s", vnode->name ());
723 fprintf (dump_file, "\n\n");
724 }
725 symtab->function_flags_ready = true;
726 return 0;
727 }
728
729 /* Local function pass handling visibilities. This happens before LTO streaming
730 so in particular -fwhole-program should be ignored at this level. */
731
732 namespace {
733
734 const pass_data pass_data_ipa_function_and_variable_visibility =
735 {
736 SIMPLE_IPA_PASS, /* type */
737 "visibility", /* name */
738 OPTGROUP_NONE, /* optinfo_flags */
739 TV_CGRAPHOPT, /* tv_id */
740 0, /* properties_required */
741 0, /* properties_provided */
742 0, /* properties_destroyed */
743 0, /* todo_flags_start */
744 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
745 };
746
747 /* Bring functions local at LTO time with -fwhole-program. */
748
749 static unsigned int
750 whole_program_function_and_variable_visibility (void)
751 {
752 function_and_variable_visibility (flag_whole_program);
753 if (optimize)
754 ipa_discover_readonly_nonaddressable_vars ();
755 return 0;
756 }
757
758 } // anon namespace
759
760 namespace {
761
762 const pass_data pass_data_ipa_whole_program_visibility =
763 {
764 IPA_PASS, /* type */
765 "whole-program", /* name */
766 OPTGROUP_NONE, /* optinfo_flags */
767 TV_CGRAPHOPT, /* tv_id */
768 0, /* properties_required */
769 0, /* properties_provided */
770 0, /* properties_destroyed */
771 0, /* todo_flags_start */
772 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
773 };
774
775 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
776 {
777 public:
778 pass_ipa_whole_program_visibility (gcc::context *ctxt)
779 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
780 NULL, /* generate_summary */
781 NULL, /* write_summary */
782 NULL, /* read_summary */
783 NULL, /* write_optimization_summary */
784 NULL, /* read_optimization_summary */
785 NULL, /* stmt_fixup */
786 0, /* function_transform_todo_flags_start */
787 NULL, /* function_transform */
788 NULL) /* variable_transform */
789 {}
790
791 /* opt_pass methods: */
792
793 virtual bool gate (function *)
794 {
795 /* Do not re-run on ltrans stage. */
796 return !flag_ltrans;
797 }
798 virtual unsigned int execute (function *)
799 {
800 return whole_program_function_and_variable_visibility ();
801 }
802
803 }; // class pass_ipa_whole_program_visibility
804
805 } // anon namespace
806
807 ipa_opt_pass_d *
808 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
809 {
810 return new pass_ipa_whole_program_visibility (ctxt);
811 }
812
813 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
814 {
815 public:
816 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
817 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
818 ctxt)
819 {}
820
821 /* opt_pass methods: */
822 virtual unsigned int execute (function *)
823 {
824 return function_and_variable_visibility (flag_whole_program && !flag_lto);
825 }
826
827 }; // class pass_ipa_function_and_variable_visibility
828
829 simple_ipa_opt_pass *
830 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
831 {
832 return new pass_ipa_function_and_variable_visibility (ctxt);
833 }