annotate gcc/ipa-visibility.c @ 16:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
kono
parents:
diff changeset
1 /* IPA visibility pass
kono
parents:
diff changeset
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 /* This file implements two related passes:
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 - pass_data_ipa_function_and_variable_visibility run just after
kono
parents:
diff changeset
23 symbol table, references and callgraph are built
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 - pass_data_ipa_function_and_variable_visibility run as first
kono
parents:
diff changeset
26 proper IPA pass (that is after early optimization, or, (with LTO)
kono
parents:
diff changeset
27 as a first pass done at link-time.
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 Purpose of both passes is to set correctly visibility properties
kono
parents:
diff changeset
30 of all symbols. This includes:
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 - Symbol privatization:
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 Some symbols that are declared public by frontend may be
kono
parents:
diff changeset
35 turned local (either by -fwhole-program flag, by linker plugin feedback
kono
parents:
diff changeset
36 or by other reasons)
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 - Discovery of local functions:
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 A local function is one whose calls can occur only in the current
kono
parents:
diff changeset
41 compilation unit and all its calls are explicit, so we can change
kono
parents:
diff changeset
42 its calling convention. We simply mark all static functions whose
kono
parents:
diff changeset
43 address is not taken as local.
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 externally_visible flag is set for symbols that can not be privatized.
kono
parents:
diff changeset
46 For privatized symbols we clear TREE_PUBLIC flag and dismantle comdat
kono
parents:
diff changeset
47 group.
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 - Dismantling of comdat groups:
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 Comdat group represent a section that may be replaced by linker by
kono
parents:
diff changeset
52 a different copy of the same section from other unit.
kono
parents:
diff changeset
53 If we have resolution information (from linker plugin) and we know that
kono
parents:
diff changeset
54 a given comdat gorup is prevailing, we can dismantle it and turn symbols
kono
parents:
diff changeset
55 into normal symbols. If the resolution information says that the
kono
parents:
diff changeset
56 section was previaled by copy from non-LTO code, we can also dismantle
kono
parents:
diff changeset
57 it and turn all symbols into external.
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 - Local aliases:
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 Some symbols can be interposed by dynamic linker. Refering to these
kono
parents:
diff changeset
62 symbols is expensive, since it needs to be overwritable by the dynamic
kono
parents:
diff changeset
63 linker. In some cases we know that the interposition does not change
kono
parents:
diff changeset
64 semantic and we can always refer to a local copy (as in the case of
kono
parents:
diff changeset
65 inline function). In this case we produce a local alias and redirect
kono
parents:
diff changeset
66 calls to it.
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 TODO: This should be done for references, too.
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 - Removal of static ocnstructors and destructors that have no side effects.
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 - Regularization of several oddities introduced by frontends that may
kono
parents:
diff changeset
73 be impractical later in the optimization queue. */
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 #include "config.h"
kono
parents:
diff changeset
76 #include "system.h"
kono
parents:
diff changeset
77 #include "coretypes.h"
kono
parents:
diff changeset
78 #include "tm.h"
kono
parents:
diff changeset
79 #include "function.h"
kono
parents:
diff changeset
80 #include "tree.h"
kono
parents:
diff changeset
81 #include "gimple-expr.h"
kono
parents:
diff changeset
82 #include "tree-pass.h"
kono
parents:
diff changeset
83 #include "cgraph.h"
kono
parents:
diff changeset
84 #include "calls.h"
kono
parents:
diff changeset
85 #include "varasm.h"
kono
parents:
diff changeset
86 #include "ipa-utils.h"
kono
parents:
diff changeset
87 #include "stringpool.h"
kono
parents:
diff changeset
88 #include "attribs.h"
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 static bool
kono
parents:
diff changeset
93 non_local_p (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
94 {
kono
parents:
diff changeset
95 return !(node->only_called_directly_or_aliased_p ()
kono
parents:
diff changeset
96 /* i386 would need update to output thunk with local calling
kono
parents:
diff changeset
97 conventions. */
kono
parents:
diff changeset
98 && !node->thunk.thunk_p
kono
parents:
diff changeset
99 && node->definition
kono
parents:
diff changeset
100 && !DECL_EXTERNAL (node->decl)
kono
parents:
diff changeset
101 && !lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl))
kono
parents:
diff changeset
102 && !node->externally_visible
kono
parents:
diff changeset
103 && !node->used_from_other_partition
kono
parents:
diff changeset
104 && !node->in_other_partition
kono
parents:
diff changeset
105 && node->get_availability () >= AVAIL_AVAILABLE);
kono
parents:
diff changeset
106 }
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 /* Return true when function can be marked local. */
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 bool
kono
parents:
diff changeset
111 cgraph_node::local_p (void)
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 cgraph_node *n = ultimate_alias_target ();
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 if (n->thunk.thunk_p)
kono
parents:
diff changeset
116 return n->callees->callee->local_p ();
kono
parents:
diff changeset
117 return !n->call_for_symbol_thunks_and_aliases (non_local_p,
kono
parents:
diff changeset
118 NULL, true);
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 }
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /* A helper for comdat_can_be_unshared_p. */
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 static bool
kono
parents:
diff changeset
125 comdat_can_be_unshared_p_1 (symtab_node *node)
kono
parents:
diff changeset
126 {
kono
parents:
diff changeset
127 if (!node->externally_visible)
kono
parents:
diff changeset
128 return true;
kono
parents:
diff changeset
129 if (node->address_can_be_compared_p ())
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 struct ipa_ref *ref;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 for (unsigned int i = 0; node->iterate_referring (i, ref); i++)
kono
parents:
diff changeset
134 if (ref->address_matters_p ())
kono
parents:
diff changeset
135 return false;
kono
parents:
diff changeset
136 }
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 /* If the symbol is used in some weird way, better to not touch it. */
kono
parents:
diff changeset
139 if (node->force_output)
kono
parents:
diff changeset
140 return false;
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 /* Explicit instantiations needs to be output when possibly
kono
parents:
diff changeset
143 used externally. */
kono
parents:
diff changeset
144 if (node->forced_by_abi
kono
parents:
diff changeset
145 && TREE_PUBLIC (node->decl)
kono
parents:
diff changeset
146 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
kono
parents:
diff changeset
147 && !flag_whole_program))
kono
parents:
diff changeset
148 return false;
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 /* Non-readonly and volatile variables can not be duplicated. */
kono
parents:
diff changeset
151 if (is_a <varpool_node *> (node)
kono
parents:
diff changeset
152 && (!TREE_READONLY (node->decl)
kono
parents:
diff changeset
153 || TREE_THIS_VOLATILE (node->decl)))
kono
parents:
diff changeset
154 return false;
kono
parents:
diff changeset
155 return true;
kono
parents:
diff changeset
156 }
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 /* COMDAT functions must be shared only if they have address taken,
kono
parents:
diff changeset
159 otherwise we can produce our own private implementation with
kono
parents:
diff changeset
160 -fwhole-program.
kono
parents:
diff changeset
161 Return true when turning COMDAT function static can not lead to wrong
kono
parents:
diff changeset
162 code when the resulting object links with a library defining same COMDAT.
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 Virtual functions do have their addresses taken from the vtables,
kono
parents:
diff changeset
165 but in C++ there is no way to compare their addresses for equality. */
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 static bool
kono
parents:
diff changeset
168 comdat_can_be_unshared_p (symtab_node *node)
kono
parents:
diff changeset
169 {
kono
parents:
diff changeset
170 if (!comdat_can_be_unshared_p_1 (node))
kono
parents:
diff changeset
171 return false;
kono
parents:
diff changeset
172 if (node->same_comdat_group)
kono
parents:
diff changeset
173 {
kono
parents:
diff changeset
174 symtab_node *next;
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 /* If more than one function is in the same COMDAT group, it must
kono
parents:
diff changeset
177 be shared even if just one function in the comdat group has
kono
parents:
diff changeset
178 address taken. */
kono
parents:
diff changeset
179 for (next = node->same_comdat_group;
kono
parents:
diff changeset
180 next != node; next = next->same_comdat_group)
kono
parents:
diff changeset
181 if (!comdat_can_be_unshared_p_1 (next))
kono
parents:
diff changeset
182 return false;
kono
parents:
diff changeset
183 }
kono
parents:
diff changeset
184 return true;
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Return true when function NODE should be considered externally visible. */
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 static bool
kono
parents:
diff changeset
190 cgraph_externally_visible_p (struct cgraph_node *node,
kono
parents:
diff changeset
191 bool whole_program)
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 while (node->transparent_alias && node->definition)
kono
parents:
diff changeset
194 node = node->get_alias_target ();
kono
parents:
diff changeset
195 if (!node->definition)
kono
parents:
diff changeset
196 return false;
kono
parents:
diff changeset
197 if (!TREE_PUBLIC (node->decl)
kono
parents:
diff changeset
198 || DECL_EXTERNAL (node->decl))
kono
parents:
diff changeset
199 return false;
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 /* Do not try to localize built-in functions yet. One of problems is that we
kono
parents:
diff changeset
202 end up mangling their asm for WHOPR that makes it impossible to call them
kono
parents:
diff changeset
203 using the implicit built-in declarations anymore. Similarly this enables
kono
parents:
diff changeset
204 us to remove them as unreachable before actual calls may appear during
kono
parents:
diff changeset
205 expansion or folding. */
kono
parents:
diff changeset
206 if (DECL_BUILT_IN (node->decl))
kono
parents:
diff changeset
207 return true;
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 /* If linker counts on us, we must preserve the function. */
kono
parents:
diff changeset
210 if (node->used_from_object_file_p ())
kono
parents:
diff changeset
211 return true;
kono
parents:
diff changeset
212 if (DECL_PRESERVE_P (node->decl))
kono
parents:
diff changeset
213 return true;
kono
parents:
diff changeset
214 if (lookup_attribute ("externally_visible",
kono
parents:
diff changeset
215 DECL_ATTRIBUTES (node->decl)))
kono
parents:
diff changeset
216 return true;
kono
parents:
diff changeset
217 if (lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)))
kono
parents:
diff changeset
218 return true;
kono
parents:
diff changeset
219 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
kono
parents:
diff changeset
220 && lookup_attribute ("dllexport",
kono
parents:
diff changeset
221 DECL_ATTRIBUTES (node->decl)))
kono
parents:
diff changeset
222 return true;
kono
parents:
diff changeset
223 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
kono
parents:
diff changeset
224 return false;
kono
parents:
diff changeset
225 /* When doing LTO or whole program, we can bring COMDAT functoins static.
kono
parents:
diff changeset
226 This improves code quality and we know we will duplicate them at most twice
kono
parents:
diff changeset
227 (in the case that we are not using plugin and link with object file
kono
parents:
diff changeset
228 implementing same COMDAT) */
kono
parents:
diff changeset
229 if (((in_lto_p || whole_program) && !flag_incremental_link)
kono
parents:
diff changeset
230 && DECL_COMDAT (node->decl)
kono
parents:
diff changeset
231 && comdat_can_be_unshared_p (node))
kono
parents:
diff changeset
232 return false;
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 /* When doing link time optimizations, hidden symbols become local. */
kono
parents:
diff changeset
235 if ((in_lto_p && !flag_incremental_link)
kono
parents:
diff changeset
236 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
kono
parents:
diff changeset
237 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
kono
parents:
diff changeset
238 /* Be sure that node is defined in IR file, not in other object
kono
parents:
diff changeset
239 file. In that case we don't set used_from_other_object_file. */
kono
parents:
diff changeset
240 && node->definition)
kono
parents:
diff changeset
241 ;
kono
parents:
diff changeset
242 else if (!whole_program)
kono
parents:
diff changeset
243 return true;
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 if (MAIN_NAME_P (DECL_NAME (node->decl)))
kono
parents:
diff changeset
246 return true;
kono
parents:
diff changeset
247
kono
parents:
diff changeset
248 if (node->instrumentation_clone
kono
parents:
diff changeset
249 && MAIN_NAME_P (DECL_NAME (node->orig_decl)))
kono
parents:
diff changeset
250 return true;
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 return false;
kono
parents:
diff changeset
253 }
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 /* Return true when variable should be considered externally visible. */
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 bool
kono
parents:
diff changeset
258 varpool_node::externally_visible_p (void)
kono
parents:
diff changeset
259 {
kono
parents:
diff changeset
260 while (transparent_alias && definition)
kono
parents:
diff changeset
261 return get_alias_target ()->externally_visible_p ();
kono
parents:
diff changeset
262 if (DECL_EXTERNAL (decl))
kono
parents:
diff changeset
263 return true;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 if (!TREE_PUBLIC (decl))
kono
parents:
diff changeset
266 return false;
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 /* If linker counts on us, we must preserve the function. */
kono
parents:
diff changeset
269 if (used_from_object_file_p ())
kono
parents:
diff changeset
270 return true;
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 /* Bringing TLS variables local may cause dynamic linker failures
kono
parents:
diff changeset
273 on limits of static TLS vars. */
kono
parents:
diff changeset
274 if (DECL_THREAD_LOCAL_P (decl)
kono
parents:
diff changeset
275 && (DECL_TLS_MODEL (decl) != TLS_MODEL_EMULATED
kono
parents:
diff changeset
276 && DECL_TLS_MODEL (decl) != TLS_MODEL_INITIAL_EXEC))
kono
parents:
diff changeset
277 return true;
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 if (DECL_HARD_REGISTER (decl))
kono
parents:
diff changeset
280 return true;
kono
parents:
diff changeset
281 if (DECL_PRESERVE_P (decl))
kono
parents:
diff changeset
282 return true;
kono
parents:
diff changeset
283 if (lookup_attribute ("externally_visible",
kono
parents:
diff changeset
284 DECL_ATTRIBUTES (decl)))
kono
parents:
diff changeset
285 return true;
kono
parents:
diff changeset
286 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
kono
parents:
diff changeset
287 && lookup_attribute ("dllexport",
kono
parents:
diff changeset
288 DECL_ATTRIBUTES (decl)))
kono
parents:
diff changeset
289 return true;
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 /* See if we have linker information about symbol not being used or
kono
parents:
diff changeset
292 if we need to make guess based on the declaration.
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 Even if the linker clams the symbol is unused, never bring internal
kono
parents:
diff changeset
295 symbols that are declared by user as used or externally visible.
kono
parents:
diff changeset
296 This is needed for i.e. references from asm statements. */
kono
parents:
diff changeset
297 if (used_from_object_file_p ())
kono
parents:
diff changeset
298 return true;
kono
parents:
diff changeset
299 if (resolution == LDPR_PREVAILING_DEF_IRONLY)
kono
parents:
diff changeset
300 return false;
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 /* As a special case, the COMDAT virtual tables can be unshared.
kono
parents:
diff changeset
303 In LTO mode turn vtables into static variables. The variable is readonly,
kono
parents:
diff changeset
304 so this does not enable more optimization, but referring static var
kono
parents:
diff changeset
305 is faster for dynamic linking. Also this match logic hidding vtables
kono
parents:
diff changeset
306 from LTO symbol tables. */
kono
parents:
diff changeset
307 if (((in_lto_p || flag_whole_program) && !flag_incremental_link)
kono
parents:
diff changeset
308 && DECL_COMDAT (decl)
kono
parents:
diff changeset
309 && comdat_can_be_unshared_p (this))
kono
parents:
diff changeset
310 return false;
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 /* When doing link time optimizations, hidden symbols become local. */
kono
parents:
diff changeset
313 if (in_lto_p && !flag_incremental_link
kono
parents:
diff changeset
314 && (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN
kono
parents:
diff changeset
315 || DECL_VISIBILITY (decl) == VISIBILITY_INTERNAL)
kono
parents:
diff changeset
316 /* Be sure that node is defined in IR file, not in other object
kono
parents:
diff changeset
317 file. In that case we don't set used_from_other_object_file. */
kono
parents:
diff changeset
318 && definition)
kono
parents:
diff changeset
319 ;
kono
parents:
diff changeset
320 else if (!flag_whole_program)
kono
parents:
diff changeset
321 return true;
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 /* Do not attempt to privatize COMDATS by default.
kono
parents:
diff changeset
324 This would break linking with C++ libraries sharing
kono
parents:
diff changeset
325 inline definitions.
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 FIXME: We can do so for readonly vars with no address taken and
kono
parents:
diff changeset
328 possibly also for vtables since no direct pointer comparsion is done.
kono
parents:
diff changeset
329 It might be interesting to do so to reduce linking overhead. */
kono
parents:
diff changeset
330 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
kono
parents:
diff changeset
331 return true;
kono
parents:
diff changeset
332 return false;
kono
parents:
diff changeset
333 }
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 /* Return true if reference to NODE can be replaced by a local alias.
kono
parents:
diff changeset
336 Local aliases save dynamic linking overhead and enable more optimizations.
kono
parents:
diff changeset
337 */
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 static bool
kono
parents:
diff changeset
340 can_replace_by_local_alias (symtab_node *node)
kono
parents:
diff changeset
341 {
kono
parents:
diff changeset
342 /* If aliases aren't supported, we can't do replacement. */
kono
parents:
diff changeset
343 if (!TARGET_SUPPORTS_ALIASES)
kono
parents:
diff changeset
344 return false;
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 /* Weakrefs have a reason to be non-local. Be sure we do not replace
kono
parents:
diff changeset
347 them. */
kono
parents:
diff changeset
348 while (node->transparent_alias && node->definition && !node->weakref)
kono
parents:
diff changeset
349 node = node->get_alias_target ();
kono
parents:
diff changeset
350 if (node->weakref)
kono
parents:
diff changeset
351 return false;
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 return (node->get_availability () > AVAIL_INTERPOSABLE
kono
parents:
diff changeset
354 && !decl_binds_to_current_def_p (node->decl)
kono
parents:
diff changeset
355 && !node->can_be_discarded_p ());
kono
parents:
diff changeset
356 }
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 /* Return true if we can replace reference to NODE by local alias
kono
parents:
diff changeset
359 within a virtual table. Generally we can replace function pointers
kono
parents:
diff changeset
360 and virtual table pointers. */
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 static bool
kono
parents:
diff changeset
363 can_replace_by_local_alias_in_vtable (symtab_node *node)
kono
parents:
diff changeset
364 {
kono
parents:
diff changeset
365 if (is_a <varpool_node *> (node)
kono
parents:
diff changeset
366 && !DECL_VIRTUAL_P (node->decl))
kono
parents:
diff changeset
367 return false;
kono
parents:
diff changeset
368 return can_replace_by_local_alias (node);
kono
parents:
diff changeset
369 }
kono
parents:
diff changeset
370
kono
parents:
diff changeset
371 /* walk_tree callback that rewrites initializer references. */
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 static tree
kono
parents:
diff changeset
374 update_vtable_references (tree *tp, int *walk_subtrees,
kono
parents:
diff changeset
375 void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
376 {
kono
parents:
diff changeset
377 if (VAR_OR_FUNCTION_DECL_P (*tp))
kono
parents:
diff changeset
378 {
kono
parents:
diff changeset
379 if (can_replace_by_local_alias_in_vtable (symtab_node::get (*tp)))
kono
parents:
diff changeset
380 *tp = symtab_node::get (*tp)->noninterposable_alias ()->decl;
kono
parents:
diff changeset
381 *walk_subtrees = 0;
kono
parents:
diff changeset
382 }
kono
parents:
diff changeset
383 else if (IS_TYPE_OR_DECL_P (*tp))
kono
parents:
diff changeset
384 *walk_subtrees = 0;
kono
parents:
diff changeset
385 return NULL;
kono
parents:
diff changeset
386 }
kono
parents:
diff changeset
387
kono
parents:
diff changeset
388 /* In LTO we can remove COMDAT groups and weak symbols.
kono
parents:
diff changeset
389 Either turn them into normal symbols or external symbol depending on
kono
parents:
diff changeset
390 resolution info. */
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 static void
kono
parents:
diff changeset
393 update_visibility_by_resolution_info (symtab_node * node)
kono
parents:
diff changeset
394 {
kono
parents:
diff changeset
395 bool define;
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 if (!node->externally_visible
kono
parents:
diff changeset
398 || (!DECL_WEAK (node->decl) && !DECL_ONE_ONLY (node->decl))
kono
parents:
diff changeset
399 || node->resolution == LDPR_UNKNOWN)
kono
parents:
diff changeset
400 return;
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 define = (node->resolution == LDPR_PREVAILING_DEF_IRONLY
kono
parents:
diff changeset
403 || node->resolution == LDPR_PREVAILING_DEF
kono
parents:
diff changeset
404 || node->resolution == LDPR_UNDEF
kono
parents:
diff changeset
405 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
kono
parents:
diff changeset
406
kono
parents:
diff changeset
407 /* The linker decisions ought to agree in the whole group. */
kono
parents:
diff changeset
408 if (node->same_comdat_group)
kono
parents:
diff changeset
409 for (symtab_node *next = node->same_comdat_group;
kono
parents:
diff changeset
410 next != node; next = next->same_comdat_group)
kono
parents:
diff changeset
411 {
kono
parents:
diff changeset
412 if (!next->externally_visible || next->transparent_alias)
kono
parents:
diff changeset
413 continue;
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 bool same_def
kono
parents:
diff changeset
416 = define == (next->resolution == LDPR_PREVAILING_DEF_IRONLY
kono
parents:
diff changeset
417 || next->resolution == LDPR_PREVAILING_DEF
kono
parents:
diff changeset
418 || next->resolution == LDPR_UNDEF
kono
parents:
diff changeset
419 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP);
kono
parents:
diff changeset
420 gcc_assert (in_lto_p || same_def);
kono
parents:
diff changeset
421 if (!same_def)
kono
parents:
diff changeset
422 return;
kono
parents:
diff changeset
423 }
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 if (node->same_comdat_group)
kono
parents:
diff changeset
426 for (symtab_node *next = node->same_comdat_group;
kono
parents:
diff changeset
427 next != node; next = next->same_comdat_group)
kono
parents:
diff changeset
428 {
kono
parents:
diff changeset
429 /* During incremental linking we need to keep symbol weak for future
kono
parents:
diff changeset
430 linking. We can still drop definition if we know non-LTO world
kono
parents:
diff changeset
431 prevails. */
kono
parents:
diff changeset
432 if (!flag_incremental_link)
kono
parents:
diff changeset
433 {
kono
parents:
diff changeset
434 DECL_WEAK (next->decl) = false;
kono
parents:
diff changeset
435 next->set_comdat_group (NULL);
kono
parents:
diff changeset
436 }
kono
parents:
diff changeset
437 if (!define)
kono
parents:
diff changeset
438 {
kono
parents:
diff changeset
439 if (next->externally_visible)
kono
parents:
diff changeset
440 DECL_EXTERNAL (next->decl) = true;
kono
parents:
diff changeset
441 next->set_comdat_group (NULL);
kono
parents:
diff changeset
442 }
kono
parents:
diff changeset
443 }
kono
parents:
diff changeset
444
kono
parents:
diff changeset
445 /* During incremental linking we need to keep symbol weak for future
kono
parents:
diff changeset
446 linking. We can still drop definition if we know non-LTO world prevails. */
kono
parents:
diff changeset
447 if (!flag_incremental_link)
kono
parents:
diff changeset
448 {
kono
parents:
diff changeset
449 DECL_WEAK (node->decl) = false;
kono
parents:
diff changeset
450 node->set_comdat_group (NULL);
kono
parents:
diff changeset
451 node->dissolve_same_comdat_group_list ();
kono
parents:
diff changeset
452 }
kono
parents:
diff changeset
453 if (!define)
kono
parents:
diff changeset
454 {
kono
parents:
diff changeset
455 DECL_EXTERNAL (node->decl) = true;
kono
parents:
diff changeset
456 node->set_comdat_group (NULL);
kono
parents:
diff changeset
457 node->dissolve_same_comdat_group_list ();
kono
parents:
diff changeset
458 }
kono
parents:
diff changeset
459 }
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 /* Try to get rid of weakref. */
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 static void
kono
parents:
diff changeset
464 optimize_weakref (symtab_node *node)
kono
parents:
diff changeset
465 {
kono
parents:
diff changeset
466 bool strip_weakref = false;
kono
parents:
diff changeset
467 bool static_alias = false;
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 gcc_assert (node->weakref);
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 /* Weakrefs with no target defined can not be optimized. */
kono
parents:
diff changeset
472 if (!node->analyzed)
kono
parents:
diff changeset
473 return;
kono
parents:
diff changeset
474 symtab_node *target = node->get_alias_target ();
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 /* Weakrefs to weakrefs can be optimized only if target can be. */
kono
parents:
diff changeset
477 if (target->weakref)
kono
parents:
diff changeset
478 optimize_weakref (target);
kono
parents:
diff changeset
479 if (target->weakref)
kono
parents:
diff changeset
480 return;
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 /* If we have definition of weakref's target and we know it binds locally,
kono
parents:
diff changeset
483 we can turn weakref to static alias. */
kono
parents:
diff changeset
484 if (TARGET_SUPPORTS_ALIASES
kono
parents:
diff changeset
485 && target->definition && decl_binds_to_current_def_p (target->decl))
kono
parents:
diff changeset
486 strip_weakref = static_alias = true;
kono
parents:
diff changeset
487 /* Otherwise we can turn weakref into transparent alias. This transformation
kono
parents:
diff changeset
488 may break asm statements which directly refers to symbol name and expect
kono
parents:
diff changeset
489 GNU as to translate it via .weakref directive. So do not optimize when
kono
parents:
diff changeset
490 DECL_PRESERVED is set and .weakref is supported. */
kono
parents:
diff changeset
491 else if ((!DECL_PRESERVE_P (target->decl)
kono
parents:
diff changeset
492 || IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)))
kono
parents:
diff changeset
493 && !DECL_WEAK (target->decl)
kono
parents:
diff changeset
494 && !DECL_EXTERNAL (target->decl)
kono
parents:
diff changeset
495 && ((target->definition && !target->can_be_discarded_p ())
kono
parents:
diff changeset
496 || target->resolution != LDPR_UNDEF))
kono
parents:
diff changeset
497 strip_weakref = true;
kono
parents:
diff changeset
498 if (!strip_weakref)
kono
parents:
diff changeset
499 return;
kono
parents:
diff changeset
500 node->weakref = false;
kono
parents:
diff changeset
501 IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (node->decl)) = 0;
kono
parents:
diff changeset
502 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl)) = NULL_TREE;
kono
parents:
diff changeset
503 DECL_ATTRIBUTES (node->decl) = remove_attribute ("weakref",
kono
parents:
diff changeset
504 DECL_ATTRIBUTES
kono
parents:
diff changeset
505 (node->decl));
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 if (dump_file)
kono
parents:
diff changeset
508 fprintf (dump_file, "Optimizing weakref %s %s\n",
kono
parents:
diff changeset
509 node->name(),
kono
parents:
diff changeset
510 static_alias ? "as static alias" : "as transparent alias");
kono
parents:
diff changeset
511
kono
parents:
diff changeset
512 if (static_alias)
kono
parents:
diff changeset
513 {
kono
parents:
diff changeset
514 /* make_decl_local will shortcircuit if it doesn't see TREE_PUBLIC.
kono
parents:
diff changeset
515 be sure it really clears the WEAK flag. */
kono
parents:
diff changeset
516 TREE_PUBLIC (node->decl) = true;
kono
parents:
diff changeset
517 node->make_decl_local ();
kono
parents:
diff changeset
518 node->forced_by_abi = false;
kono
parents:
diff changeset
519 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
kono
parents:
diff changeset
520 node->externally_visible = false;
kono
parents:
diff changeset
521 gcc_assert (!DECL_WEAK (node->decl));
kono
parents:
diff changeset
522 node->transparent_alias = false;
kono
parents:
diff changeset
523 }
kono
parents:
diff changeset
524 else
kono
parents:
diff changeset
525 {
kono
parents:
diff changeset
526 symtab->change_decl_assembler_name
kono
parents:
diff changeset
527 (node->decl, DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
kono
parents:
diff changeset
528 node->transparent_alias = true;
kono
parents:
diff changeset
529 node->copy_visibility_from (target);
kono
parents:
diff changeset
530 }
kono
parents:
diff changeset
531 gcc_assert (node->alias);
kono
parents:
diff changeset
532 }
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 /* NODE is an externally visible definition, which we've discovered is
kono
parents:
diff changeset
535 not needed externally. Make it local to this compilation. */
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 static void
kono
parents:
diff changeset
538 localize_node (bool whole_program, symtab_node *node)
kono
parents:
diff changeset
539 {
kono
parents:
diff changeset
540 gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 /* It is possible that one comdat group contains both hidden and non-hidden
kono
parents:
diff changeset
543 symbols. In this case we can privatize all hidden symbol but we need
kono
parents:
diff changeset
544 to keep non-hidden exported. */
kono
parents:
diff changeset
545 if (node->same_comdat_group
kono
parents:
diff changeset
546 && node->resolution == LDPR_PREVAILING_DEF_IRONLY)
kono
parents:
diff changeset
547 {
kono
parents:
diff changeset
548 symtab_node *next;
kono
parents:
diff changeset
549 for (next = node->same_comdat_group;
kono
parents:
diff changeset
550 next != node; next = next->same_comdat_group)
kono
parents:
diff changeset
551 if (next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
kono
parents:
diff changeset
552 || next->resolution == LDPR_PREVAILING_DEF)
kono
parents:
diff changeset
553 break;
kono
parents:
diff changeset
554 if (node != next)
kono
parents:
diff changeset
555 {
kono
parents:
diff changeset
556 if (!node->transparent_alias)
kono
parents:
diff changeset
557 {
kono
parents:
diff changeset
558 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
kono
parents:
diff changeset
559 node->make_decl_local ();
kono
parents:
diff changeset
560 if (!flag_incremental_link)
kono
parents:
diff changeset
561 node->unique_name |= true;
kono
parents:
diff changeset
562 return;
kono
parents:
diff changeset
563 }
kono
parents:
diff changeset
564 }
kono
parents:
diff changeset
565 }
kono
parents:
diff changeset
566 /* For similar reason do not privatize whole comdat when seeing comdat
kono
parents:
diff changeset
567 local. Wait for non-comdat symbol to be privatized first. */
kono
parents:
diff changeset
568 if (node->comdat_local_p ())
kono
parents:
diff changeset
569 return;
kono
parents:
diff changeset
570
kono
parents:
diff changeset
571 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
kono
parents:
diff changeset
572 {
kono
parents:
diff changeset
573 for (symtab_node *next = node->same_comdat_group;
kono
parents:
diff changeset
574 next != node; next = next->same_comdat_group)
kono
parents:
diff changeset
575 {
kono
parents:
diff changeset
576 next->set_comdat_group (NULL);
kono
parents:
diff changeset
577 if (!next->alias)
kono
parents:
diff changeset
578 next->set_section (NULL);
kono
parents:
diff changeset
579 if (!next->transparent_alias)
kono
parents:
diff changeset
580 next->make_decl_local ();
kono
parents:
diff changeset
581 next->unique_name
kono
parents:
diff changeset
582 |= ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
kono
parents:
diff changeset
583 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
kono
parents:
diff changeset
584 && TREE_PUBLIC (next->decl)
kono
parents:
diff changeset
585 && !flag_incremental_link);
kono
parents:
diff changeset
586 }
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 /* Now everything's localized, the grouping has no meaning, and
kono
parents:
diff changeset
589 will cause crashes if we keep it around. */
kono
parents:
diff changeset
590 node->dissolve_same_comdat_group_list ();
kono
parents:
diff changeset
591 }
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 node->unique_name
kono
parents:
diff changeset
594 |= ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
kono
parents:
diff changeset
595 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
kono
parents:
diff changeset
596 && TREE_PUBLIC (node->decl)
kono
parents:
diff changeset
597 && !flag_incremental_link);
kono
parents:
diff changeset
598
kono
parents:
diff changeset
599 if (TREE_PUBLIC (node->decl))
kono
parents:
diff changeset
600 node->set_comdat_group (NULL);
kono
parents:
diff changeset
601 if (DECL_COMDAT (node->decl) && !node->alias)
kono
parents:
diff changeset
602 node->set_section (NULL);
kono
parents:
diff changeset
603 if (!node->transparent_alias)
kono
parents:
diff changeset
604 {
kono
parents:
diff changeset
605 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
kono
parents:
diff changeset
606 node->make_decl_local ();
kono
parents:
diff changeset
607 }
kono
parents:
diff changeset
608 }
kono
parents:
diff changeset
609
kono
parents:
diff changeset
610 /* Decide on visibility of all symbols. */
kono
parents:
diff changeset
611
kono
parents:
diff changeset
612 static unsigned int
kono
parents:
diff changeset
613 function_and_variable_visibility (bool whole_program)
kono
parents:
diff changeset
614 {
kono
parents:
diff changeset
615 struct cgraph_node *node;
kono
parents:
diff changeset
616 varpool_node *vnode;
kono
parents:
diff changeset
617
kono
parents:
diff changeset
618 /* All aliases should be processed at this point. */
kono
parents:
diff changeset
619 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 #ifdef ASM_OUTPUT_DEF
kono
parents:
diff changeset
622 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
623 {
kono
parents:
diff changeset
624 if (node->get_availability () != AVAIL_INTERPOSABLE
kono
parents:
diff changeset
625 || DECL_EXTERNAL (node->decl)
kono
parents:
diff changeset
626 || node->has_aliases_p ())
kono
parents:
diff changeset
627 continue;
kono
parents:
diff changeset
628
kono
parents:
diff changeset
629 cgraph_node *alias = 0;
kono
parents:
diff changeset
630 for (cgraph_edge *e = node->callees; e; e = e->next_callee)
kono
parents:
diff changeset
631 {
kono
parents:
diff changeset
632 /* Recursive function calls usually can't be interposed. */
kono
parents:
diff changeset
633
kono
parents:
diff changeset
634 if (!e->recursive_p ())
kono
parents:
diff changeset
635 continue;
kono
parents:
diff changeset
636
kono
parents:
diff changeset
637 if (!alias)
kono
parents:
diff changeset
638 {
kono
parents:
diff changeset
639 alias = dyn_cast<cgraph_node *> (node->noninterposable_alias ());
kono
parents:
diff changeset
640 gcc_assert (alias && alias != node);
kono
parents:
diff changeset
641 }
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 e->redirect_callee (alias);
kono
parents:
diff changeset
644 if (gimple_has_body_p (e->caller->decl))
kono
parents:
diff changeset
645 {
kono
parents:
diff changeset
646 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
kono
parents:
diff changeset
647 e->redirect_call_stmt_to_callee ();
kono
parents:
diff changeset
648 pop_cfun ();
kono
parents:
diff changeset
649 }
kono
parents:
diff changeset
650 }
kono
parents:
diff changeset
651 }
kono
parents:
diff changeset
652 #endif
kono
parents:
diff changeset
653
kono
parents:
diff changeset
654 FOR_EACH_FUNCTION (node)
kono
parents:
diff changeset
655 {
kono
parents:
diff changeset
656 int flags = flags_from_decl_or_type (node->decl);
kono
parents:
diff changeset
657
kono
parents:
diff changeset
658 /* Optimize away PURE and CONST constructors and destructors. */
kono
parents:
diff changeset
659 if (node->analyzed
kono
parents:
diff changeset
660 && (DECL_STATIC_CONSTRUCTOR (node->decl)
kono
parents:
diff changeset
661 || DECL_STATIC_DESTRUCTOR (node->decl))
kono
parents:
diff changeset
662 && (flags & (ECF_CONST | ECF_PURE))
kono
parents:
diff changeset
663 && !(flags & ECF_LOOPING_CONST_OR_PURE)
kono
parents:
diff changeset
664 && opt_for_fn (node->decl, optimize))
kono
parents:
diff changeset
665 {
kono
parents:
diff changeset
666 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
kono
parents:
diff changeset
667 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
kono
parents:
diff changeset
668 }
kono
parents:
diff changeset
669
kono
parents:
diff changeset
670 /* Frontends and alias code marks nodes as needed before parsing
kono
parents:
diff changeset
671 is finished. We may end up marking as node external nodes
kono
parents:
diff changeset
672 where this flag is meaningless strip it. */
kono
parents:
diff changeset
673 if (DECL_EXTERNAL (node->decl) || !node->definition)
kono
parents:
diff changeset
674 {
kono
parents:
diff changeset
675 node->force_output = 0;
kono
parents:
diff changeset
676 node->forced_by_abi = 0;
kono
parents:
diff changeset
677 }
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 /* C++ FE on lack of COMDAT support create local COMDAT functions
kono
parents:
diff changeset
680 (that ought to be shared but can not due to object format
kono
parents:
diff changeset
681 limitations). It is necessary to keep the flag to make rest of C++ FE
kono
parents:
diff changeset
682 happy. Clear the flag here to avoid confusion in middle-end. */
kono
parents:
diff changeset
683 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
kono
parents:
diff changeset
684 DECL_COMDAT (node->decl) = 0;
kono
parents:
diff changeset
685
kono
parents:
diff changeset
686 /* For external decls stop tracking same_comdat_group. It doesn't matter
kono
parents:
diff changeset
687 what comdat group they are in when they won't be emitted in this TU.
kono
parents:
diff changeset
688
kono
parents:
diff changeset
689 An exception is LTO where we may end up with both external
kono
parents:
diff changeset
690 and non-external declarations in the same comdat group in
kono
parents:
diff changeset
691 the case declarations was not merged. */
kono
parents:
diff changeset
692 if (node->same_comdat_group && DECL_EXTERNAL (node->decl) && !in_lto_p)
kono
parents:
diff changeset
693 {
kono
parents:
diff changeset
694 if (flag_checking)
kono
parents:
diff changeset
695 {
kono
parents:
diff changeset
696 for (symtab_node *n = node->same_comdat_group;
kono
parents:
diff changeset
697 n != node;
kono
parents:
diff changeset
698 n = n->same_comdat_group)
kono
parents:
diff changeset
699 /* If at least one of same comdat group functions is external,
kono
parents:
diff changeset
700 all of them have to be, otherwise it is a front-end bug. */
kono
parents:
diff changeset
701 gcc_assert (DECL_EXTERNAL (n->decl));
kono
parents:
diff changeset
702 }
kono
parents:
diff changeset
703 node->dissolve_same_comdat_group_list ();
kono
parents:
diff changeset
704 }
kono
parents:
diff changeset
705 gcc_assert ((!DECL_WEAK (node->decl)
kono
parents:
diff changeset
706 && !DECL_COMDAT (node->decl))
kono
parents:
diff changeset
707 || TREE_PUBLIC (node->decl)
kono
parents:
diff changeset
708 || node->weakref
kono
parents:
diff changeset
709 || DECL_EXTERNAL (node->decl));
kono
parents:
diff changeset
710 if (cgraph_externally_visible_p (node, whole_program))
kono
parents:
diff changeset
711 {
kono
parents:
diff changeset
712 gcc_assert (!node->global.inlined_to);
kono
parents:
diff changeset
713 node->externally_visible = true;
kono
parents:
diff changeset
714 }
kono
parents:
diff changeset
715 else
kono
parents:
diff changeset
716 {
kono
parents:
diff changeset
717 node->externally_visible = false;
kono
parents:
diff changeset
718 node->forced_by_abi = false;
kono
parents:
diff changeset
719 }
kono
parents:
diff changeset
720 if (!node->externally_visible
kono
parents:
diff changeset
721 && node->definition && !node->weakref
kono
parents:
diff changeset
722 && !DECL_EXTERNAL (node->decl))
kono
parents:
diff changeset
723 localize_node (whole_program, node);
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 if (node->thunk.thunk_p
kono
parents:
diff changeset
726 && !node->thunk.add_pointer_bounds_args
kono
parents:
diff changeset
727 && TREE_PUBLIC (node->decl))
kono
parents:
diff changeset
728 {
kono
parents:
diff changeset
729 struct cgraph_node *decl_node = node;
kono
parents:
diff changeset
730
kono
parents:
diff changeset
731 decl_node = decl_node->callees->callee->function_symbol ();
kono
parents:
diff changeset
732
kono
parents:
diff changeset
733 /* Thunks have the same visibility as function they are attached to.
kono
parents:
diff changeset
734 Make sure the C++ front end set this up properly. */
kono
parents:
diff changeset
735 if (DECL_ONE_ONLY (decl_node->decl))
kono
parents:
diff changeset
736 {
kono
parents:
diff changeset
737 gcc_checking_assert (DECL_COMDAT (node->decl)
kono
parents:
diff changeset
738 == DECL_COMDAT (decl_node->decl));
kono
parents:
diff changeset
739 gcc_checking_assert (node->in_same_comdat_group_p (decl_node));
kono
parents:
diff changeset
740 gcc_checking_assert (node->same_comdat_group);
kono
parents:
diff changeset
741 }
kono
parents:
diff changeset
742 node->forced_by_abi = decl_node->forced_by_abi;
kono
parents:
diff changeset
743 if (DECL_EXTERNAL (decl_node->decl))
kono
parents:
diff changeset
744 DECL_EXTERNAL (node->decl) = 1;
kono
parents:
diff changeset
745 }
kono
parents:
diff changeset
746
kono
parents:
diff changeset
747 update_visibility_by_resolution_info (node);
kono
parents:
diff changeset
748 if (node->weakref)
kono
parents:
diff changeset
749 optimize_weakref (node);
kono
parents:
diff changeset
750 }
kono
parents:
diff changeset
751 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
752 {
kono
parents:
diff changeset
753 if (!node->local.local)
kono
parents:
diff changeset
754 node->local.local |= node->local_p ();
kono
parents:
diff changeset
755
kono
parents:
diff changeset
756 /* If we know that function can not be overwritten by a
kono
parents:
diff changeset
757 different semantics and moreover its section can not be
kono
parents:
diff changeset
758 discarded, replace all direct calls by calls to an
kono
parents:
diff changeset
759 noninterposable alias. This make dynamic linking cheaper and
kono
parents:
diff changeset
760 enable more optimization.
kono
parents:
diff changeset
761
kono
parents:
diff changeset
762 TODO: We can also update virtual tables. */
kono
parents:
diff changeset
763 if (node->callers
kono
parents:
diff changeset
764 && can_replace_by_local_alias (node))
kono
parents:
diff changeset
765 {
kono
parents:
diff changeset
766 cgraph_node *alias = dyn_cast<cgraph_node *>
kono
parents:
diff changeset
767 (node->noninterposable_alias ());
kono
parents:
diff changeset
768
kono
parents:
diff changeset
769 if (alias && alias != node)
kono
parents:
diff changeset
770 {
kono
parents:
diff changeset
771 while (node->callers)
kono
parents:
diff changeset
772 {
kono
parents:
diff changeset
773 struct cgraph_edge *e = node->callers;
kono
parents:
diff changeset
774
kono
parents:
diff changeset
775 e->redirect_callee (alias);
kono
parents:
diff changeset
776 if (gimple_has_body_p (e->caller->decl))
kono
parents:
diff changeset
777 {
kono
parents:
diff changeset
778 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
kono
parents:
diff changeset
779 e->redirect_call_stmt_to_callee ();
kono
parents:
diff changeset
780 pop_cfun ();
kono
parents:
diff changeset
781 }
kono
parents:
diff changeset
782 }
kono
parents:
diff changeset
783 }
kono
parents:
diff changeset
784 }
kono
parents:
diff changeset
785 }
kono
parents:
diff changeset
786 FOR_EACH_VARIABLE (vnode)
kono
parents:
diff changeset
787 {
kono
parents:
diff changeset
788 /* weak flag makes no sense on local variables. */
kono
parents:
diff changeset
789 gcc_assert (!DECL_WEAK (vnode->decl)
kono
parents:
diff changeset
790 || vnode->weakref
kono
parents:
diff changeset
791 || TREE_PUBLIC (vnode->decl)
kono
parents:
diff changeset
792 || DECL_EXTERNAL (vnode->decl));
kono
parents:
diff changeset
793 /* In several cases declarations can not be common:
kono
parents:
diff changeset
794
kono
parents:
diff changeset
795 - when declaration has initializer
kono
parents:
diff changeset
796 - when it is in weak
kono
parents:
diff changeset
797 - when it has specific section
kono
parents:
diff changeset
798 - when it resides in non-generic address space.
kono
parents:
diff changeset
799 - if declaration is local, it will get into .local common section
kono
parents:
diff changeset
800 so common flag is not needed. Frontends still produce these in
kono
parents:
diff changeset
801 certain cases, such as for:
kono
parents:
diff changeset
802
kono
parents:
diff changeset
803 static int a __attribute__ ((common))
kono
parents:
diff changeset
804
kono
parents:
diff changeset
805 Canonicalize things here and clear the redundant flag. */
kono
parents:
diff changeset
806 if (DECL_COMMON (vnode->decl)
kono
parents:
diff changeset
807 && (!(TREE_PUBLIC (vnode->decl)
kono
parents:
diff changeset
808 || DECL_EXTERNAL (vnode->decl))
kono
parents:
diff changeset
809 || (DECL_INITIAL (vnode->decl)
kono
parents:
diff changeset
810 && DECL_INITIAL (vnode->decl) != error_mark_node)
kono
parents:
diff changeset
811 || DECL_WEAK (vnode->decl)
kono
parents:
diff changeset
812 || DECL_SECTION_NAME (vnode->decl) != NULL
kono
parents:
diff changeset
813 || ! (ADDR_SPACE_GENERIC_P
kono
parents:
diff changeset
814 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
kono
parents:
diff changeset
815 DECL_COMMON (vnode->decl) = 0;
kono
parents:
diff changeset
816 if (vnode->weakref)
kono
parents:
diff changeset
817 optimize_weakref (vnode);
kono
parents:
diff changeset
818 }
kono
parents:
diff changeset
819 FOR_EACH_DEFINED_VARIABLE (vnode)
kono
parents:
diff changeset
820 {
kono
parents:
diff changeset
821 if (!vnode->definition)
kono
parents:
diff changeset
822 continue;
kono
parents:
diff changeset
823 if (vnode->externally_visible_p ())
kono
parents:
diff changeset
824 vnode->externally_visible = true;
kono
parents:
diff changeset
825 else
kono
parents:
diff changeset
826 {
kono
parents:
diff changeset
827 vnode->externally_visible = false;
kono
parents:
diff changeset
828 vnode->forced_by_abi = false;
kono
parents:
diff changeset
829 }
kono
parents:
diff changeset
830 if (lookup_attribute ("no_reorder",
kono
parents:
diff changeset
831 DECL_ATTRIBUTES (vnode->decl)))
kono
parents:
diff changeset
832 vnode->no_reorder = 1;
kono
parents:
diff changeset
833
kono
parents:
diff changeset
834 if (!vnode->externally_visible
kono
parents:
diff changeset
835 && !vnode->transparent_alias
kono
parents:
diff changeset
836 && !DECL_EXTERNAL (vnode->decl))
kono
parents:
diff changeset
837 localize_node (whole_program, vnode);
kono
parents:
diff changeset
838
kono
parents:
diff changeset
839 update_visibility_by_resolution_info (vnode);
kono
parents:
diff changeset
840
kono
parents:
diff changeset
841 /* Update virtual tables to point to local aliases where possible. */
kono
parents:
diff changeset
842 if (DECL_VIRTUAL_P (vnode->decl)
kono
parents:
diff changeset
843 && !DECL_EXTERNAL (vnode->decl))
kono
parents:
diff changeset
844 {
kono
parents:
diff changeset
845 int i;
kono
parents:
diff changeset
846 struct ipa_ref *ref;
kono
parents:
diff changeset
847 bool found = false;
kono
parents:
diff changeset
848
kono
parents:
diff changeset
849 /* See if there is something to update. */
kono
parents:
diff changeset
850 for (i = 0; vnode->iterate_reference (i, ref); i++)
kono
parents:
diff changeset
851 if (ref->use == IPA_REF_ADDR
kono
parents:
diff changeset
852 && can_replace_by_local_alias_in_vtable (ref->referred))
kono
parents:
diff changeset
853 {
kono
parents:
diff changeset
854 found = true;
kono
parents:
diff changeset
855 break;
kono
parents:
diff changeset
856 }
kono
parents:
diff changeset
857 if (found)
kono
parents:
diff changeset
858 {
kono
parents:
diff changeset
859 hash_set<tree> visited_nodes;
kono
parents:
diff changeset
860
kono
parents:
diff changeset
861 vnode->get_constructor ();
kono
parents:
diff changeset
862 walk_tree (&DECL_INITIAL (vnode->decl),
kono
parents:
diff changeset
863 update_vtable_references, NULL, &visited_nodes);
kono
parents:
diff changeset
864 vnode->remove_all_references ();
kono
parents:
diff changeset
865 record_references_in_initializer (vnode->decl, false);
kono
parents:
diff changeset
866 }
kono
parents:
diff changeset
867 }
kono
parents:
diff changeset
868 }
kono
parents:
diff changeset
869
kono
parents:
diff changeset
870 if (dump_file)
kono
parents:
diff changeset
871 {
kono
parents:
diff changeset
872 fprintf (dump_file, "\nMarking local functions:");
kono
parents:
diff changeset
873 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
874 if (node->local.local)
kono
parents:
diff changeset
875 fprintf (dump_file, " %s", node->name ());
kono
parents:
diff changeset
876 fprintf (dump_file, "\n\n");
kono
parents:
diff changeset
877 fprintf (dump_file, "\nMarking externally visible functions:");
kono
parents:
diff changeset
878 FOR_EACH_DEFINED_FUNCTION (node)
kono
parents:
diff changeset
879 if (node->externally_visible)
kono
parents:
diff changeset
880 fprintf (dump_file, " %s", node->name ());
kono
parents:
diff changeset
881 fprintf (dump_file, "\n\n");
kono
parents:
diff changeset
882 fprintf (dump_file, "\nMarking externally visible variables:");
kono
parents:
diff changeset
883 FOR_EACH_DEFINED_VARIABLE (vnode)
kono
parents:
diff changeset
884 if (vnode->externally_visible)
kono
parents:
diff changeset
885 fprintf (dump_file, " %s", vnode->name ());
kono
parents:
diff changeset
886 fprintf (dump_file, "\n\n");
kono
parents:
diff changeset
887 }
kono
parents:
diff changeset
888 symtab->function_flags_ready = true;
kono
parents:
diff changeset
889 return 0;
kono
parents:
diff changeset
890 }
kono
parents:
diff changeset
891
kono
parents:
diff changeset
892 /* Local function pass handling visibilities. This happens before LTO streaming
kono
parents:
diff changeset
893 so in particular -fwhole-program should be ignored at this level. */
kono
parents:
diff changeset
894
kono
parents:
diff changeset
895 namespace {
kono
parents:
diff changeset
896
kono
parents:
diff changeset
897 const pass_data pass_data_ipa_function_and_variable_visibility =
kono
parents:
diff changeset
898 {
kono
parents:
diff changeset
899 SIMPLE_IPA_PASS, /* type */
kono
parents:
diff changeset
900 "visibility", /* name */
kono
parents:
diff changeset
901 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
902 TV_CGRAPHOPT, /* tv_id */
kono
parents:
diff changeset
903 0, /* properties_required */
kono
parents:
diff changeset
904 0, /* properties_provided */
kono
parents:
diff changeset
905 0, /* properties_destroyed */
kono
parents:
diff changeset
906 0, /* todo_flags_start */
kono
parents:
diff changeset
907 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
kono
parents:
diff changeset
908 };
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 /* Bring functions local at LTO time with -fwhole-program. */
kono
parents:
diff changeset
911
kono
parents:
diff changeset
912 static unsigned int
kono
parents:
diff changeset
913 whole_program_function_and_variable_visibility (void)
kono
parents:
diff changeset
914 {
kono
parents:
diff changeset
915 function_and_variable_visibility (flag_whole_program);
kono
parents:
diff changeset
916 if (optimize || in_lto_p)
kono
parents:
diff changeset
917 ipa_discover_readonly_nonaddressable_vars ();
kono
parents:
diff changeset
918 return 0;
kono
parents:
diff changeset
919 }
kono
parents:
diff changeset
920
kono
parents:
diff changeset
921 } // anon namespace
kono
parents:
diff changeset
922
kono
parents:
diff changeset
923 namespace {
kono
parents:
diff changeset
924
kono
parents:
diff changeset
925 const pass_data pass_data_ipa_whole_program_visibility =
kono
parents:
diff changeset
926 {
kono
parents:
diff changeset
927 IPA_PASS, /* type */
kono
parents:
diff changeset
928 "whole-program", /* name */
kono
parents:
diff changeset
929 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
930 TV_CGRAPHOPT, /* tv_id */
kono
parents:
diff changeset
931 0, /* properties_required */
kono
parents:
diff changeset
932 0, /* properties_provided */
kono
parents:
diff changeset
933 0, /* properties_destroyed */
kono
parents:
diff changeset
934 0, /* todo_flags_start */
kono
parents:
diff changeset
935 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
kono
parents:
diff changeset
936 };
kono
parents:
diff changeset
937
kono
parents:
diff changeset
938 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
kono
parents:
diff changeset
939 {
kono
parents:
diff changeset
940 public:
kono
parents:
diff changeset
941 pass_ipa_whole_program_visibility (gcc::context *ctxt)
kono
parents:
diff changeset
942 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
kono
parents:
diff changeset
943 NULL, /* generate_summary */
kono
parents:
diff changeset
944 NULL, /* write_summary */
kono
parents:
diff changeset
945 NULL, /* read_summary */
kono
parents:
diff changeset
946 NULL, /* write_optimization_summary */
kono
parents:
diff changeset
947 NULL, /* read_optimization_summary */
kono
parents:
diff changeset
948 NULL, /* stmt_fixup */
kono
parents:
diff changeset
949 0, /* function_transform_todo_flags_start */
kono
parents:
diff changeset
950 NULL, /* function_transform */
kono
parents:
diff changeset
951 NULL) /* variable_transform */
kono
parents:
diff changeset
952 {}
kono
parents:
diff changeset
953
kono
parents:
diff changeset
954 /* opt_pass methods: */
kono
parents:
diff changeset
955
kono
parents:
diff changeset
956 virtual bool gate (function *)
kono
parents:
diff changeset
957 {
kono
parents:
diff changeset
958 /* Do not re-run on ltrans stage. */
kono
parents:
diff changeset
959 return !flag_ltrans;
kono
parents:
diff changeset
960 }
kono
parents:
diff changeset
961 virtual unsigned int execute (function *)
kono
parents:
diff changeset
962 {
kono
parents:
diff changeset
963 return whole_program_function_and_variable_visibility ();
kono
parents:
diff changeset
964 }
kono
parents:
diff changeset
965
kono
parents:
diff changeset
966 }; // class pass_ipa_whole_program_visibility
kono
parents:
diff changeset
967
kono
parents:
diff changeset
968 } // anon namespace
kono
parents:
diff changeset
969
kono
parents:
diff changeset
970 ipa_opt_pass_d *
kono
parents:
diff changeset
971 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
kono
parents:
diff changeset
972 {
kono
parents:
diff changeset
973 return new pass_ipa_whole_program_visibility (ctxt);
kono
parents:
diff changeset
974 }
kono
parents:
diff changeset
975
kono
parents:
diff changeset
976 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
kono
parents:
diff changeset
977 {
kono
parents:
diff changeset
978 public:
kono
parents:
diff changeset
979 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
kono
parents:
diff changeset
980 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
kono
parents:
diff changeset
981 ctxt)
kono
parents:
diff changeset
982 {}
kono
parents:
diff changeset
983
kono
parents:
diff changeset
984 /* opt_pass methods: */
kono
parents:
diff changeset
985 virtual unsigned int execute (function *)
kono
parents:
diff changeset
986 {
kono
parents:
diff changeset
987 return function_and_variable_visibility (flag_whole_program && !flag_lto);
kono
parents:
diff changeset
988 }
kono
parents:
diff changeset
989
kono
parents:
diff changeset
990 }; // class pass_ipa_function_and_variable_visibility
kono
parents:
diff changeset
991
kono
parents:
diff changeset
992 simple_ipa_opt_pass *
kono
parents:
diff changeset
993 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
kono
parents:
diff changeset
994 {
kono
parents:
diff changeset
995 return new pass_ipa_function_and_variable_visibility (ctxt);
kono
parents:
diff changeset
996 }