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