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