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