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