]>
Commit | Line | Data |
---|---|---|
2aae7680 | 1 | /* Symbol table. |
8d9254fc | 2 | Copyright (C) 2012-2020 Free Software Foundation, Inc. |
2aae7680 JH |
3 | Contributed by Jan Hubicka |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
c7131fb2 | 24 | #include "backend.h" |
957060b5 AM |
25 | #include "target.h" |
26 | #include "rtl.h" | |
c7131fb2 AM |
27 | #include "tree.h" |
28 | #include "gimple.h" | |
957060b5 | 29 | #include "timevar.h" |
957060b5 AM |
30 | #include "cgraph.h" |
31 | #include "lto-streamer.h" | |
d8a2d370 DN |
32 | #include "print-tree.h" |
33 | #include "varasm.h" | |
8f940ee6 | 34 | #include "langhooks.h" |
862d0b35 | 35 | #include "output.h" |
d122681a | 36 | #include "ipa-utils.h" |
72732f3e | 37 | #include "calls.h" |
314e6352 ML |
38 | #include "stringpool.h" |
39 | #include "attribs.h" | |
39aa9b23 | 40 | #include "builtins.h" |
d122681a | 41 | |
31db0fe0 | 42 | static const char *ipa_ref_use_name[] = {"read","write","addr","alias"}; |
65d630d4 JH |
43 | |
44 | const char * const ld_plugin_symbol_resolution_names[]= | |
45 | { | |
46 | "", | |
47 | "undef", | |
48 | "prevailing_def", | |
49 | "prevailing_def_ironly", | |
50 | "preempted_reg", | |
51 | "preempted_ir", | |
52 | "resolved_ir", | |
53 | "resolved_exec", | |
54 | "resolved_dyn", | |
55 | "prevailing_def_ironly_exp" | |
56 | }; | |
1ab24192 | 57 | |
71e54687 JH |
58 | /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at ALIAS |
59 | until we find an identifier that is not itself a transparent alias. */ | |
60 | ||
61 | static inline tree | |
62 | ultimate_transparent_alias_target (tree alias) | |
63 | { | |
64 | tree target = alias; | |
65 | ||
66 | while (IDENTIFIER_TRANSPARENT_ALIAS (target)) | |
67 | { | |
68 | gcc_checking_assert (TREE_CHAIN (target)); | |
69 | target = TREE_CHAIN (target); | |
70 | } | |
71 | gcc_checking_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target) | |
72 | && ! TREE_CHAIN (target)); | |
73 | ||
74 | return target; | |
75 | } | |
76 | ||
77 | ||
862d0b35 DN |
78 | /* Hash asmnames ignoring the user specified marks. */ |
79 | ||
3dafb85c ML |
80 | hashval_t |
81 | symbol_table::decl_assembler_name_hash (const_tree asmname) | |
862d0b35 DN |
82 | { |
83 | if (IDENTIFIER_POINTER (asmname)[0] == '*') | |
84 | { | |
85 | const char *decl_str = IDENTIFIER_POINTER (asmname) + 1; | |
86 | size_t ulp_len = strlen (user_label_prefix); | |
87 | ||
88 | if (ulp_len == 0) | |
89 | ; | |
90 | else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0) | |
91 | decl_str += ulp_len; | |
92 | ||
93 | return htab_hash_string (decl_str); | |
94 | } | |
95 | ||
96 | return htab_hash_string (IDENTIFIER_POINTER (asmname)); | |
97 | } | |
98 | ||
71e54687 JH |
99 | /* Return true if assembler names NAME1 and NAME2 leads to the same symbol |
100 | name. */ | |
101 | ||
102 | bool | |
103 | symbol_table::assembler_names_equal_p (const char *name1, const char *name2) | |
104 | { | |
105 | if (name1 != name2) | |
106 | { | |
107 | if (name1[0] == '*') | |
108 | { | |
109 | size_t ulp_len = strlen (user_label_prefix); | |
110 | ||
111 | name1 ++; | |
112 | ||
113 | if (ulp_len == 0) | |
114 | ; | |
115 | else if (strncmp (name1, user_label_prefix, ulp_len) == 0) | |
116 | name1 += ulp_len; | |
117 | else | |
118 | return false; | |
119 | } | |
120 | if (name2[0] == '*') | |
121 | { | |
122 | size_t ulp_len = strlen (user_label_prefix); | |
123 | ||
124 | name2 ++; | |
125 | ||
126 | if (ulp_len == 0) | |
127 | ; | |
128 | else if (strncmp (name2, user_label_prefix, ulp_len) == 0) | |
129 | name2 += ulp_len; | |
130 | else | |
131 | return false; | |
132 | } | |
133 | return !strcmp (name1, name2); | |
134 | } | |
135 | return true; | |
136 | } | |
862d0b35 | 137 | |
862d0b35 DN |
138 | /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */ |
139 | ||
3dafb85c ML |
140 | bool |
141 | symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname) | |
862d0b35 DN |
142 | { |
143 | tree decl_asmname = DECL_ASSEMBLER_NAME (decl); | |
144 | const char *decl_str; | |
145 | const char *asmname_str; | |
862d0b35 DN |
146 | |
147 | if (decl_asmname == asmname) | |
148 | return true; | |
149 | ||
150 | decl_str = IDENTIFIER_POINTER (decl_asmname); | |
151 | asmname_str = IDENTIFIER_POINTER (asmname); | |
71e54687 | 152 | return assembler_names_equal_p (decl_str, asmname_str); |
862d0b35 DN |
153 | } |
154 | ||
155 | ||
1ab24192 JH |
156 | /* Returns nonzero if P1 and P2 are equal. */ |
157 | ||
1ab24192 JH |
158 | /* Insert NODE to assembler name hash. */ |
159 | ||
3dafb85c ML |
160 | void |
161 | symbol_table::insert_to_assembler_name_hash (symtab_node *node, | |
162 | bool with_clones) | |
1ab24192 | 163 | { |
7de90a6c | 164 | if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl)) |
b5493fb2 | 165 | return; |
67348ccc DM |
166 | gcc_checking_assert (!node->previous_sharing_asm_name |
167 | && !node->next_sharing_asm_name); | |
1ab24192 JH |
168 | if (assembler_name_hash) |
169 | { | |
2a22f99c | 170 | symtab_node **aslot; |
3dafb85c | 171 | cgraph_node *cnode; |
67348ccc | 172 | tree decl = node->decl; |
c3167b00 | 173 | |
67348ccc | 174 | tree name = DECL_ASSEMBLER_NAME (node->decl); |
1ab24192 | 175 | |
59b5b466 JH |
176 | /* C++ FE can produce decls without associated assembler name and insert |
177 | them to symtab to hold section or TLS information. */ | |
178 | if (!name) | |
179 | return; | |
180 | ||
2a22f99c TS |
181 | hashval_t hash = decl_assembler_name_hash (name); |
182 | aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT); | |
1ab24192 | 183 | gcc_assert (*aslot != node); |
5e20cdc9 | 184 | node->next_sharing_asm_name = (symtab_node *)*aslot; |
1ab24192 | 185 | if (*aslot != NULL) |
2a22f99c | 186 | (*aslot)->previous_sharing_asm_name = node; |
1ab24192 | 187 | *aslot = node; |
c3167b00 JH |
188 | |
189 | /* Update also possible inline clones sharing a decl. */ | |
7de90a6c | 190 | cnode = dyn_cast <cgraph_node *> (node); |
c3167b00 JH |
191 | if (cnode && cnode->clones && with_clones) |
192 | for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) | |
67348ccc DM |
193 | if (cnode->decl == decl) |
194 | insert_to_assembler_name_hash (cnode, true); | |
1ab24192 JH |
195 | } |
196 | ||
197 | } | |
198 | ||
199 | /* Remove NODE from assembler name hash. */ | |
200 | ||
3dafb85c ML |
201 | void |
202 | symbol_table::unlink_from_assembler_name_hash (symtab_node *node, | |
203 | bool with_clones) | |
1ab24192 JH |
204 | { |
205 | if (assembler_name_hash) | |
206 | { | |
3dafb85c | 207 | cgraph_node *cnode; |
67348ccc | 208 | tree decl = node->decl; |
c3167b00 | 209 | |
67348ccc DM |
210 | if (node->next_sharing_asm_name) |
211 | node->next_sharing_asm_name->previous_sharing_asm_name | |
212 | = node->previous_sharing_asm_name; | |
213 | if (node->previous_sharing_asm_name) | |
1ab24192 | 214 | { |
67348ccc DM |
215 | node->previous_sharing_asm_name->next_sharing_asm_name |
216 | = node->next_sharing_asm_name; | |
1ab24192 JH |
217 | } |
218 | else | |
219 | { | |
67348ccc | 220 | tree name = DECL_ASSEMBLER_NAME (node->decl); |
2a22f99c | 221 | symtab_node **slot; |
59b5b466 JH |
222 | |
223 | if (!name) | |
224 | return; | |
225 | ||
2a22f99c TS |
226 | hashval_t hash = decl_assembler_name_hash (name); |
227 | slot = assembler_name_hash->find_slot_with_hash (name, hash, | |
228 | NO_INSERT); | |
1ab24192 | 229 | gcc_assert (*slot == node); |
67348ccc | 230 | if (!node->next_sharing_asm_name) |
2a22f99c | 231 | assembler_name_hash->clear_slot (slot); |
1ab24192 | 232 | else |
67348ccc | 233 | *slot = node->next_sharing_asm_name; |
1ab24192 | 234 | } |
67348ccc DM |
235 | node->next_sharing_asm_name = NULL; |
236 | node->previous_sharing_asm_name = NULL; | |
c3167b00 JH |
237 | |
238 | /* Update also possible inline clones sharing a decl. */ | |
7de90a6c | 239 | cnode = dyn_cast <cgraph_node *> (node); |
c3167b00 JH |
240 | if (cnode && cnode->clones && with_clones) |
241 | for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) | |
67348ccc DM |
242 | if (cnode->decl == decl) |
243 | unlink_from_assembler_name_hash (cnode, true); | |
1ab24192 JH |
244 | } |
245 | } | |
246 | ||
b5493fb2 JH |
247 | /* Arrange node to be first in its entry of assembler_name_hash. */ |
248 | ||
249 | void | |
3dafb85c | 250 | symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node) |
b5493fb2 | 251 | { |
c3167b00 JH |
252 | unlink_from_assembler_name_hash (node, false); |
253 | insert_to_assembler_name_hash (node, false); | |
b5493fb2 JH |
254 | } |
255 | ||
dfea3d6f | 256 | /* Initialize asm name hash unless. */ |
1ab24192 | 257 | |
b5493fb2 | 258 | void |
3dafb85c | 259 | symbol_table::symtab_initialize_asm_name_hash (void) |
1ab24192 | 260 | { |
5e20cdc9 | 261 | symtab_node *node; |
1ab24192 JH |
262 | if (!assembler_name_hash) |
263 | { | |
2a22f99c | 264 | assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10); |
1ab24192 | 265 | FOR_EACH_SYMBOL (node) |
c3167b00 | 266 | insert_to_assembler_name_hash (node, false); |
1ab24192 | 267 | } |
b5493fb2 | 268 | } |
1ab24192 | 269 | |
1ab24192 JH |
270 | /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */ |
271 | ||
272 | void | |
3dafb85c | 273 | symbol_table::change_decl_assembler_name (tree decl, tree name) |
1ab24192 | 274 | { |
5e20cdc9 | 275 | symtab_node *node = NULL; |
1ab24192 JH |
276 | |
277 | /* We can have user ASM names on things, like global register variables, that | |
278 | are not in the symbol table. */ | |
8813a647 | 279 | if ((VAR_P (decl) && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) |
1ab24192 | 280 | || TREE_CODE (decl) == FUNCTION_DECL) |
d52f5295 | 281 | node = symtab_node::get (decl); |
1ab24192 JH |
282 | if (!DECL_ASSEMBLER_NAME_SET_P (decl)) |
283 | { | |
284 | SET_DECL_ASSEMBLER_NAME (decl, name); | |
285 | if (node) | |
c3167b00 | 286 | insert_to_assembler_name_hash (node, true); |
1ab24192 JH |
287 | } |
288 | else | |
289 | { | |
290 | if (name == DECL_ASSEMBLER_NAME (decl)) | |
291 | return; | |
292 | ||
8a41354f JH |
293 | tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) |
294 | ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) | |
295 | : NULL); | |
1ab24192 | 296 | if (node) |
c3167b00 | 297 | unlink_from_assembler_name_hash (node, true); |
71e54687 JH |
298 | |
299 | const char *old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); | |
1ab24192 JH |
300 | if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) |
301 | && DECL_RTL_SET_P (decl)) | |
0f2c4a8f | 302 | warning (0, "%qD renamed after being referenced in assembly", decl); |
1ab24192 JH |
303 | |
304 | SET_DECL_ASSEMBLER_NAME (decl, name); | |
8a41354f JH |
305 | if (alias) |
306 | { | |
307 | IDENTIFIER_TRANSPARENT_ALIAS (name) = 1; | |
1fed15fc | 308 | TREE_CHAIN (name) = alias; |
8a41354f | 309 | } |
71e54687 JH |
310 | /* If we change assembler name, also all transparent aliases must |
311 | be updated. There are three kinds - those having same assembler name, | |
312 | those being renamed in varasm.c and weakref being renamed by the | |
313 | assembler. */ | |
1ab24192 | 314 | if (node) |
71e54687 JH |
315 | { |
316 | insert_to_assembler_name_hash (node, true); | |
317 | ipa_ref *ref; | |
318 | for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++) | |
319 | { | |
320 | struct symtab_node *alias = ref->referring; | |
321 | if (alias->transparent_alias && !alias->weakref | |
322 | && symbol_table::assembler_names_equal_p | |
323 | (old_name, IDENTIFIER_POINTER ( | |
324 | DECL_ASSEMBLER_NAME (alias->decl)))) | |
325 | change_decl_assembler_name (alias->decl, name); | |
326 | else if (alias->transparent_alias | |
327 | && IDENTIFIER_TRANSPARENT_ALIAS (alias->decl)) | |
328 | { | |
329 | gcc_assert (TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) | |
330 | && IDENTIFIER_TRANSPARENT_ALIAS | |
331 | (DECL_ASSEMBLER_NAME (alias->decl))); | |
332 | ||
333 | TREE_CHAIN (DECL_ASSEMBLER_NAME (alias->decl)) = | |
334 | ultimate_transparent_alias_target | |
335 | (DECL_ASSEMBLER_NAME (node->decl)); | |
336 | } | |
337 | #ifdef ASM_OUTPUT_WEAKREF | |
338 | else gcc_assert (!alias->transparent_alias || alias->weakref); | |
339 | #else | |
340 | else gcc_assert (!alias->transparent_alias); | |
341 | #endif | |
342 | } | |
343 | gcc_assert (!node->transparent_alias || !node->definition | |
344 | || node->weakref | |
345 | || TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) | |
346 | || symbol_table::assembler_names_equal_p | |
347 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), | |
348 | IDENTIFIER_POINTER | |
349 | (DECL_ASSEMBLER_NAME | |
350 | (node->get_alias_target ()->decl)))); | |
351 | } | |
1ab24192 JH |
352 | } |
353 | } | |
354 | ||
d52f5295 ML |
355 | /* Hash sections by their names. */ |
356 | ||
2a22f99c TS |
357 | hashval_t |
358 | section_name_hasher::hash (section_hash_entry *n) | |
d52f5295 | 359 | { |
d52f5295 ML |
360 | return htab_hash_string (n->name); |
361 | } | |
362 | ||
363 | /* Return true if section P1 name equals to P2. */ | |
364 | ||
2a22f99c TS |
365 | bool |
366 | section_name_hasher::equal (section_hash_entry *n1, const char *name) | |
d52f5295 | 367 | { |
d52f5295 ML |
368 | return n1->name == name || !strcmp (n1->name, name); |
369 | } | |
370 | ||
371 | /* Add node into symbol table. This function is not used directly, but via | |
372 | cgraph/varpool node creation routines. */ | |
373 | ||
374 | void | |
375 | symtab_node::register_symbol (void) | |
376 | { | |
3dafb85c | 377 | symtab->register_symbol (this); |
d52f5295 ML |
378 | |
379 | if (!decl->decl_with_vis.symtab_node) | |
380 | decl->decl_with_vis.symtab_node = this; | |
381 | ||
382 | ref_list.clear (); | |
383 | ||
d52f5295 ML |
384 | /* Be sure to do this last; C++ FE might create new nodes via |
385 | DECL_ASSEMBLER_NAME langhook! */ | |
3dafb85c | 386 | symtab->insert_to_assembler_name_hash (this, false); |
d52f5295 ML |
387 | } |
388 | ||
389 | /* Remove NODE from same comdat group. */ | |
390 | ||
391 | void | |
392 | symtab_node::remove_from_same_comdat_group (void) | |
393 | { | |
394 | if (same_comdat_group) | |
395 | { | |
396 | symtab_node *prev; | |
397 | for (prev = same_comdat_group; | |
398 | prev->same_comdat_group != this; | |
399 | prev = prev->same_comdat_group) | |
400 | ; | |
401 | if (same_comdat_group == prev) | |
402 | prev->same_comdat_group = NULL; | |
403 | else | |
404 | prev->same_comdat_group = same_comdat_group; | |
405 | same_comdat_group = NULL; | |
406 | set_comdat_group (NULL); | |
407 | } | |
408 | } | |
409 | ||
410 | /* Remove node from symbol table. This function is not used directly, but via | |
411 | cgraph/varpool node removal routines. */ | |
412 | ||
413 | void | |
414 | symtab_node::unregister (void) | |
415 | { | |
416 | remove_all_references (); | |
417 | remove_all_referring (); | |
418 | ||
419 | /* Remove reference to section. */ | |
420 | set_section_for_node (NULL); | |
421 | ||
422 | remove_from_same_comdat_group (); | |
423 | ||
3dafb85c | 424 | symtab->unregister (this); |
d52f5295 ML |
425 | |
426 | /* During LTO symtab merging we temporarily corrupt decl to symtab node | |
427 | hash. */ | |
428 | gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p); | |
429 | if (decl->decl_with_vis.symtab_node == this) | |
430 | { | |
431 | symtab_node *replacement_node = NULL; | |
432 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) | |
433 | replacement_node = cnode->find_replacement (); | |
434 | decl->decl_with_vis.symtab_node = replacement_node; | |
435 | } | |
436 | if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl)) | |
3dafb85c | 437 | symtab->unlink_from_assembler_name_hash (this, false); |
d52f5295 | 438 | if (in_init_priority_hash) |
b086d530 | 439 | symtab->init_priority_hash->remove (this); |
d52f5295 ML |
440 | } |
441 | ||
442 | ||
443 | /* Remove symbol from symbol table. */ | |
444 | ||
445 | void | |
446 | symtab_node::remove (void) | |
447 | { | |
448 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) | |
449 | cnode->remove (); | |
450 | else if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) | |
451 | vnode->remove (); | |
452 | } | |
453 | ||
65d630d4 JH |
454 | /* Add NEW_ to the same comdat group that OLD is in. */ |
455 | ||
456 | void | |
d52f5295 | 457 | symtab_node::add_to_same_comdat_group (symtab_node *old_node) |
65d630d4 | 458 | { |
aede2c10 | 459 | gcc_assert (old_node->get_comdat_group ()); |
d52f5295 ML |
460 | gcc_assert (!same_comdat_group); |
461 | gcc_assert (this != old_node); | |
65d630d4 | 462 | |
d52f5295 ML |
463 | set_comdat_group (old_node->get_comdat_group ()); |
464 | same_comdat_group = old_node; | |
67348ccc | 465 | if (!old_node->same_comdat_group) |
d52f5295 | 466 | old_node->same_comdat_group = this; |
65d630d4 JH |
467 | else |
468 | { | |
5e20cdc9 | 469 | symtab_node *n; |
67348ccc DM |
470 | for (n = old_node->same_comdat_group; |
471 | n->same_comdat_group != old_node; | |
472 | n = n->same_comdat_group) | |
65d630d4 | 473 | ; |
d52f5295 | 474 | n->same_comdat_group = this; |
65d630d4 | 475 | } |
72b3bc89 JH |
476 | |
477 | cgraph_node *n; | |
478 | if (comdat_local_p () | |
479 | && (n = dyn_cast <cgraph_node *> (this)) != NULL) | |
480 | { | |
481 | for (cgraph_edge *e = n->callers; e; e = e->next_caller) | |
482 | if (e->caller->inlined_to) | |
483 | e->caller->inlined_to->calls_comdat_local = true; | |
484 | else | |
485 | e->caller->calls_comdat_local = true; | |
486 | } | |
65d630d4 JH |
487 | } |
488 | ||
489 | /* Dissolve the same_comdat_group list in which NODE resides. */ | |
490 | ||
491 | void | |
d52f5295 | 492 | symtab_node::dissolve_same_comdat_group_list (void) |
65d630d4 | 493 | { |
d52f5295 | 494 | symtab_node *n = this; |
5e20cdc9 | 495 | symtab_node *next; |
65d630d4 | 496 | |
d52f5295 | 497 | if (!same_comdat_group) |
65d630d4 JH |
498 | return; |
499 | do | |
500 | { | |
67348ccc DM |
501 | next = n->same_comdat_group; |
502 | n->same_comdat_group = NULL; | |
21cd8589 JH |
503 | if (dyn_cast <cgraph_node *> (n)) |
504 | dyn_cast <cgraph_node *> (n)->calls_comdat_local = false; | |
aede2c10 | 505 | /* Clear comdat_group for comdat locals, since |
1f26ac87 JM |
506 | make_decl_local doesn't. */ |
507 | if (!TREE_PUBLIC (n->decl)) | |
aede2c10 | 508 | n->set_comdat_group (NULL); |
65d630d4 JH |
509 | n = next; |
510 | } | |
d52f5295 | 511 | while (n != this); |
65d630d4 JH |
512 | } |
513 | ||
8f940ee6 JH |
514 | /* Return printable assembler name of NODE. |
515 | This function is used only for debugging. When assembler name | |
516 | is unknown go with identifier name. */ | |
517 | ||
518 | const char * | |
fec39fa6 | 519 | symtab_node::asm_name () const |
8f940ee6 | 520 | { |
fec39fa6 | 521 | if (!DECL_ASSEMBLER_NAME_SET_P (decl)) |
ae4bbcb2 | 522 | return name (); |
fec39fa6 | 523 | return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); |
8f940ee6 JH |
524 | } |
525 | ||
526 | /* Return printable identifier name. */ | |
527 | ||
528 | const char * | |
fec39fa6 | 529 | symtab_node::name () const |
8f940ee6 | 530 | { |
ae4bbcb2 JH |
531 | if (!DECL_NAME (decl)) |
532 | { | |
533 | if (DECL_ASSEMBLER_NAME_SET_P (decl)) | |
534 | return asm_name (); | |
535 | else | |
536 | return "<unnamed>"; | |
537 | } | |
fec39fa6 | 538 | return lang_hooks.decl_printable_name (decl, 2); |
8f940ee6 JH |
539 | } |
540 | ||
464d0118 ML |
541 | const char * |
542 | symtab_node::get_dump_name (bool asm_name_p) const | |
543 | { | |
544 | #define EXTRA 16 | |
545 | const char *fname = asm_name_p ? asm_name () : name (); | |
546 | unsigned l = strlen (fname); | |
547 | ||
548 | char *s = (char *)ggc_internal_cleared_alloc (l + EXTRA); | |
549 | snprintf (s, l + EXTRA, "%s/%d", fname, order); | |
550 | ||
551 | return s; | |
552 | } | |
553 | ||
554 | const char * | |
555 | symtab_node::dump_name () const | |
556 | { | |
557 | return get_dump_name (false); | |
558 | } | |
559 | ||
560 | const char * | |
561 | symtab_node::dump_asm_name () const | |
562 | { | |
563 | return get_dump_name (true); | |
564 | } | |
565 | ||
d122681a | 566 | /* Return ipa reference from this symtab_node to |
dfea3d6f | 567 | REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type |
d122681a ML |
568 | of the use. */ |
569 | ||
3dafb85c ML |
570 | ipa_ref * |
571 | symtab_node::create_reference (symtab_node *referred_node, | |
572 | enum ipa_ref_use use_type) | |
d122681a | 573 | { |
3dafb85c | 574 | return create_reference (referred_node, use_type, NULL); |
d122681a ML |
575 | } |
576 | ||
577 | ||
578 | /* Return ipa reference from this symtab_node to | |
dfea3d6f | 579 | REFERRED_NODE or REFERRED_VARPOOL_NODE. USE_TYPE specify type |
d122681a ML |
580 | of the use and STMT the statement (if it exists). */ |
581 | ||
3dafb85c ML |
582 | ipa_ref * |
583 | symtab_node::create_reference (symtab_node *referred_node, | |
355fe088 | 584 | enum ipa_ref_use use_type, gimple *stmt) |
d122681a | 585 | { |
3dafb85c ML |
586 | ipa_ref *ref = NULL, *ref2 = NULL; |
587 | ipa_ref_list *list, *list2; | |
d122681a ML |
588 | ipa_ref_t *old_references; |
589 | ||
590 | gcc_checking_assert (!stmt || is_a <cgraph_node *> (this)); | |
591 | gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt); | |
592 | ||
593 | list = &ref_list; | |
594 | old_references = vec_safe_address (list->references); | |
595 | vec_safe_grow (list->references, vec_safe_length (list->references) + 1); | |
596 | ref = &list->references->last (); | |
597 | ||
598 | list2 = &referred_node->ref_list; | |
e55637b7 ML |
599 | |
600 | /* IPA_REF_ALIAS is always inserted at the beginning of the list. */ | |
601 | if(use_type == IPA_REF_ALIAS) | |
31de7606 JH |
602 | { |
603 | list2->referring.safe_insert (0, ref); | |
604 | ref->referred_index = 0; | |
e55637b7 | 605 | |
31de7606 JH |
606 | for (unsigned int i = 1; i < list2->referring.length (); i++) |
607 | list2->referring[i]->referred_index = i; | |
608 | } | |
e55637b7 | 609 | else |
31de7606 JH |
610 | { |
611 | list2->referring.safe_push (ref); | |
612 | ref->referred_index = list2->referring.length () - 1; | |
613 | } | |
e55637b7 | 614 | |
d122681a ML |
615 | ref->referring = this; |
616 | ref->referred = referred_node; | |
617 | ref->stmt = stmt; | |
618 | ref->lto_stmt_uid = 0; | |
f1ba88b1 | 619 | ref->speculative_id = 0; |
d122681a ML |
620 | ref->use = use_type; |
621 | ref->speculative = 0; | |
622 | ||
623 | /* If vector was moved in memory, update pointers. */ | |
624 | if (old_references != list->references->address ()) | |
625 | { | |
626 | int i; | |
627 | for (i = 0; iterate_reference(i, ref2); i++) | |
628 | ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2; | |
629 | } | |
630 | return ref; | |
631 | } | |
632 | ||
3dafb85c | 633 | ipa_ref * |
2d8d3ae2 | 634 | symtab_node::maybe_create_reference (tree val, gimple *stmt) |
d122681a ML |
635 | { |
636 | STRIP_NOPS (val); | |
2d8d3ae2 ML |
637 | ipa_ref_use use_type; |
638 | ||
639 | switch (TREE_CODE (val)) | |
640 | { | |
641 | case VAR_DECL: | |
642 | use_type = IPA_REF_LOAD; | |
643 | break; | |
644 | case ADDR_EXPR: | |
645 | use_type = IPA_REF_ADDR; | |
646 | break; | |
647 | default: | |
648 | gcc_assert (!handled_component_p (val)); | |
649 | return NULL; | |
650 | } | |
651 | ||
d122681a | 652 | val = get_base_var (val); |
8813a647 | 653 | if (val && VAR_OR_FUNCTION_DECL_P (val)) |
d122681a | 654 | { |
d52f5295 | 655 | symtab_node *referred = symtab_node::get (val); |
d122681a | 656 | gcc_checking_assert (referred); |
3dafb85c | 657 | return create_reference (referred, use_type, stmt); |
d122681a ML |
658 | } |
659 | return NULL; | |
660 | } | |
661 | ||
662 | /* Clone all references from symtab NODE to this symtab_node. */ | |
663 | ||
664 | void | |
3dafb85c | 665 | symtab_node::clone_references (symtab_node *node) |
d122681a | 666 | { |
3dafb85c | 667 | ipa_ref *ref = NULL, *ref2 = NULL; |
d122681a ML |
668 | int i; |
669 | for (i = 0; node->iterate_reference (i, ref); i++) | |
670 | { | |
671 | bool speculative = ref->speculative; | |
672 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
2fa4b1ff | 673 | unsigned int spec_id = ref->speculative_id; |
d122681a | 674 | |
3dafb85c | 675 | ref2 = create_reference (ref->referred, ref->use, ref->stmt); |
d122681a ML |
676 | ref2->speculative = speculative; |
677 | ref2->lto_stmt_uid = stmt_uid; | |
2fa4b1ff | 678 | ref2->speculative_id = spec_id; |
d122681a ML |
679 | } |
680 | } | |
681 | ||
682 | /* Clone all referring from symtab NODE to this symtab_node. */ | |
683 | ||
684 | void | |
3dafb85c | 685 | symtab_node::clone_referring (symtab_node *node) |
d122681a | 686 | { |
3dafb85c | 687 | ipa_ref *ref = NULL, *ref2 = NULL; |
d122681a ML |
688 | int i; |
689 | for (i = 0; node->iterate_referring(i, ref); i++) | |
690 | { | |
691 | bool speculative = ref->speculative; | |
692 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
2fa4b1ff | 693 | unsigned int spec_id = ref->speculative_id; |
d122681a | 694 | |
3dafb85c | 695 | ref2 = ref->referring->create_reference (this, ref->use, ref->stmt); |
d122681a ML |
696 | ref2->speculative = speculative; |
697 | ref2->lto_stmt_uid = stmt_uid; | |
2fa4b1ff | 698 | ref2->speculative_id = spec_id; |
d122681a ML |
699 | } |
700 | } | |
701 | ||
702 | /* Clone reference REF to this symtab_node and set its stmt to STMT. */ | |
703 | ||
3dafb85c | 704 | ipa_ref * |
355fe088 | 705 | symtab_node::clone_reference (ipa_ref *ref, gimple *stmt) |
d122681a ML |
706 | { |
707 | bool speculative = ref->speculative; | |
708 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
2fa4b1ff | 709 | unsigned int spec_id = ref->speculative_id; |
3dafb85c | 710 | ipa_ref *ref2; |
d122681a | 711 | |
3dafb85c | 712 | ref2 = create_reference (ref->referred, ref->use, stmt); |
d122681a ML |
713 | ref2->speculative = speculative; |
714 | ref2->lto_stmt_uid = stmt_uid; | |
2fa4b1ff | 715 | ref2->speculative_id = spec_id; |
d122681a ML |
716 | return ref2; |
717 | } | |
718 | ||
719 | /* Find the structure describing a reference to REFERRED_NODE | |
720 | and associated with statement STMT. */ | |
721 | ||
3dafb85c | 722 | ipa_ref * |
d122681a | 723 | symtab_node::find_reference (symtab_node *referred_node, |
355fe088 | 724 | gimple *stmt, unsigned int lto_stmt_uid) |
d122681a | 725 | { |
3dafb85c | 726 | ipa_ref *r = NULL; |
d122681a ML |
727 | int i; |
728 | ||
729 | for (i = 0; iterate_reference (i, r); i++) | |
730 | if (r->referred == referred_node | |
731 | && !r->speculative | |
732 | && ((stmt && r->stmt == stmt) | |
733 | || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid) | |
734 | || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid))) | |
735 | return r; | |
736 | return NULL; | |
737 | } | |
738 | ||
739 | /* Remove all references that are associated with statement STMT. */ | |
740 | ||
741 | void | |
355fe088 | 742 | symtab_node::remove_stmt_references (gimple *stmt) |
d122681a | 743 | { |
3dafb85c | 744 | ipa_ref *r = NULL; |
d122681a ML |
745 | int i = 0; |
746 | ||
747 | while (iterate_reference (i, r)) | |
748 | if (r->stmt == stmt) | |
749 | r->remove_reference (); | |
750 | else | |
751 | i++; | |
752 | } | |
753 | ||
754 | /* Remove all stmt references in non-speculative references. | |
dfea3d6f | 755 | Those are not maintained during inlining & cloning. |
d122681a ML |
756 | The exception are speculative references that are updated along |
757 | with callgraph edges associated with them. */ | |
758 | ||
759 | void | |
760 | symtab_node::clear_stmts_in_references (void) | |
761 | { | |
3dafb85c | 762 | ipa_ref *r = NULL; |
d122681a ML |
763 | int i; |
764 | ||
765 | for (i = 0; iterate_reference (i, r); i++) | |
766 | if (!r->speculative) | |
767 | { | |
768 | r->stmt = NULL; | |
769 | r->lto_stmt_uid = 0; | |
f1ba88b1 | 770 | r->speculative_id = 0; |
d122681a ML |
771 | } |
772 | } | |
773 | ||
774 | /* Remove all references in ref list. */ | |
775 | ||
776 | void | |
777 | symtab_node::remove_all_references (void) | |
778 | { | |
779 | while (vec_safe_length (ref_list.references)) | |
780 | ref_list.references->last ().remove_reference (); | |
781 | vec_free (ref_list.references); | |
782 | } | |
783 | ||
784 | /* Remove all referring items in ref list. */ | |
785 | ||
786 | void | |
787 | symtab_node::remove_all_referring (void) | |
788 | { | |
789 | while (ref_list.referring.length ()) | |
790 | ref_list.referring.last ()->remove_reference (); | |
791 | ref_list.referring.release (); | |
792 | } | |
793 | ||
794 | /* Dump references in ref list to FILE. */ | |
795 | ||
796 | void | |
797 | symtab_node::dump_references (FILE *file) | |
798 | { | |
3dafb85c | 799 | ipa_ref *ref = NULL; |
d122681a ML |
800 | int i; |
801 | for (i = 0; iterate_reference (i, ref); i++) | |
802 | { | |
9b4d38df ML |
803 | fprintf (file, "%s (%s) ", ref->referred->dump_asm_name (), |
804 | ipa_ref_use_name[ref->use]); | |
d122681a | 805 | if (ref->speculative) |
9b4d38df | 806 | fprintf (file, "(speculative) "); |
d122681a ML |
807 | } |
808 | fprintf (file, "\n"); | |
809 | } | |
810 | ||
811 | /* Dump referring in list to FILE. */ | |
812 | ||
813 | void | |
814 | symtab_node::dump_referring (FILE *file) | |
815 | { | |
3dafb85c | 816 | ipa_ref *ref = NULL; |
d122681a ML |
817 | int i; |
818 | for (i = 0; iterate_referring(i, ref); i++) | |
819 | { | |
9b4d38df ML |
820 | fprintf (file, "%s (%s) ", ref->referring->dump_asm_name (), |
821 | ipa_ref_use_name[ref->use]); | |
d122681a | 822 | if (ref->speculative) |
9b4d38df | 823 | fprintf (file, "(speculative) "); |
d122681a ML |
824 | } |
825 | fprintf (file, "\n"); | |
826 | } | |
827 | ||
8f940ee6 JH |
828 | static const char * const symtab_type_names[] = {"symbol", "function", "variable"}; |
829 | ||
66d62d9f HK |
830 | /* Dump the visibility of the symbol. */ |
831 | ||
832 | const char * | |
833 | symtab_node::get_visibility_string () const | |
834 | { | |
835 | static const char * const visibility_types[] | |
836 | = { "default", "protected", "hidden", "internal" }; | |
837 | return visibility_types[DECL_VISIBILITY (decl)]; | |
838 | } | |
839 | ||
840 | /* Dump the type_name of the symbol. */ | |
841 | const char * | |
842 | symtab_node::get_symtab_type_string () const | |
843 | { | |
844 | return symtab_type_names[type]; | |
845 | } | |
846 | ||
d52f5295 | 847 | /* Dump base fields of symtab nodes to F. Not to be used directly. */ |
8f940ee6 JH |
848 | |
849 | void | |
d52f5295 | 850 | symtab_node::dump_base (FILE *f) |
8f940ee6 JH |
851 | { |
852 | static const char * const visibility_types[] = { | |
853 | "default", "protected", "hidden", "internal" | |
854 | }; | |
855 | ||
464d0118 | 856 | fprintf (f, "%s (%s)", dump_asm_name (), name ()); |
d52f5295 ML |
857 | dump_addr (f, " @", (void *)this); |
858 | fprintf (f, "\n Type: %s", symtab_type_names[type]); | |
e70670cf | 859 | |
d52f5295 | 860 | if (definition) |
e70670cf | 861 | fprintf (f, " definition"); |
d52f5295 | 862 | if (analyzed) |
e70670cf | 863 | fprintf (f, " analyzed"); |
d52f5295 | 864 | if (alias) |
e70670cf | 865 | fprintf (f, " alias"); |
71e54687 JH |
866 | if (transparent_alias) |
867 | fprintf (f, " transparent_alias"); | |
d52f5295 | 868 | if (weakref) |
08346abd | 869 | fprintf (f, " weakref"); |
d7ddfbcb JH |
870 | if (symver) |
871 | fprintf (f, " symver"); | |
d52f5295 | 872 | if (cpp_implicit_alias) |
40a7fe1e | 873 | fprintf (f, " cpp_implicit_alias"); |
d52f5295 | 874 | if (alias_target) |
40a7fe1e | 875 | fprintf (f, " target:%s", |
d52f5295 | 876 | DECL_P (alias_target) |
40a7fe1e | 877 | ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME |
d52f5295 ML |
878 | (alias_target)) |
879 | : IDENTIFIER_POINTER (alias_target)); | |
880 | if (body_removed) | |
3d8d0043 | 881 | fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes"); |
e70670cf | 882 | fprintf (f, "\n Visibility:"); |
d52f5295 | 883 | if (in_other_partition) |
8f940ee6 | 884 | fprintf (f, " in_other_partition"); |
d52f5295 | 885 | if (used_from_other_partition) |
8f940ee6 | 886 | fprintf (f, " used_from_other_partition"); |
d52f5295 | 887 | if (force_output) |
ead84f73 | 888 | fprintf (f, " force_output"); |
d52f5295 | 889 | if (forced_by_abi) |
edb983b2 | 890 | fprintf (f, " forced_by_abi"); |
d52f5295 | 891 | if (externally_visible) |
e5b962d0 | 892 | fprintf (f, " externally_visible"); |
7861b648 AK |
893 | if (no_reorder) |
894 | fprintf (f, " no_reorder"); | |
d52f5295 | 895 | if (resolution != LDPR_UNKNOWN) |
8f940ee6 | 896 | fprintf (f, " %s", |
d52f5295 ML |
897 | ld_plugin_symbol_resolution_names[(int)resolution]); |
898 | if (TREE_ASM_WRITTEN (decl)) | |
8f940ee6 | 899 | fprintf (f, " asm_written"); |
d52f5295 | 900 | if (DECL_EXTERNAL (decl)) |
8f940ee6 | 901 | fprintf (f, " external"); |
d52f5295 | 902 | if (TREE_PUBLIC (decl)) |
8f940ee6 | 903 | fprintf (f, " public"); |
d52f5295 | 904 | if (DECL_COMMON (decl)) |
8f940ee6 | 905 | fprintf (f, " common"); |
d52f5295 | 906 | if (DECL_WEAK (decl)) |
8f940ee6 | 907 | fprintf (f, " weak"); |
d52f5295 | 908 | if (DECL_DLLIMPORT_P (decl)) |
8f940ee6 | 909 | fprintf (f, " dll_import"); |
d52f5295 | 910 | if (DECL_COMDAT (decl)) |
8f940ee6 | 911 | fprintf (f, " comdat"); |
d52f5295 | 912 | if (get_comdat_group ()) |
8f940ee6 | 913 | fprintf (f, " comdat_group:%s", |
d52f5295 ML |
914 | IDENTIFIER_POINTER (get_comdat_group_id ())); |
915 | if (DECL_ONE_ONLY (decl)) | |
8f940ee6 | 916 | fprintf (f, " one_only"); |
d52f5295 | 917 | if (get_section ()) |
24d047a3 | 918 | fprintf (f, " section:%s", |
d52f5295 ML |
919 | get_section ()); |
920 | if (implicit_section) | |
e257a17c | 921 | fprintf (f," (implicit_section)"); |
d52f5295 | 922 | if (DECL_VISIBILITY_SPECIFIED (decl)) |
8f940ee6 | 923 | fprintf (f, " visibility_specified"); |
d52f5295 | 924 | if (DECL_VISIBILITY (decl)) |
8f940ee6 | 925 | fprintf (f, " visibility:%s", |
d52f5295 ML |
926 | visibility_types [DECL_VISIBILITY (decl)]); |
927 | if (DECL_VIRTUAL_P (decl)) | |
8f940ee6 | 928 | fprintf (f, " virtual"); |
d52f5295 | 929 | if (DECL_ARTIFICIAL (decl)) |
8f940ee6 | 930 | fprintf (f, " artificial"); |
d52f5295 | 931 | if (TREE_CODE (decl) == FUNCTION_DECL) |
838ff415 | 932 | { |
d52f5295 | 933 | if (DECL_STATIC_CONSTRUCTOR (decl)) |
838ff415 | 934 | fprintf (f, " constructor"); |
d52f5295 | 935 | if (DECL_STATIC_DESTRUCTOR (decl)) |
838ff415 JH |
936 | fprintf (f, " destructor"); |
937 | } | |
2dfd63de MJ |
938 | if (ifunc_resolver) |
939 | fprintf (f, " ifunc_resolver"); | |
8f940ee6 | 940 | fprintf (f, "\n"); |
2dfd63de | 941 | |
d52f5295 | 942 | if (same_comdat_group) |
464d0118 ML |
943 | fprintf (f, " Same comdat group as: %s\n", |
944 | same_comdat_group->dump_asm_name ()); | |
d52f5295 | 945 | if (next_sharing_asm_name) |
8f940ee6 | 946 | fprintf (f, " next sharing asm name: %i\n", |
d52f5295 ML |
947 | next_sharing_asm_name->order); |
948 | if (previous_sharing_asm_name) | |
8f940ee6 | 949 | fprintf (f, " previous sharing asm name: %i\n", |
d52f5295 | 950 | previous_sharing_asm_name->order); |
8f940ee6 | 951 | |
d52f5295 | 952 | if (address_taken) |
fe0bd630 | 953 | fprintf (f, " Address is taken.\n"); |
d52f5295 | 954 | if (aux) |
66058468 JH |
955 | { |
956 | fprintf (f, " Aux:"); | |
d52f5295 | 957 | dump_addr (f, " @", (void *)aux); |
9e57787b | 958 | fprintf (f, "\n"); |
66058468 | 959 | } |
8f940ee6 JH |
960 | |
961 | fprintf (f, " References: "); | |
d52f5295 | 962 | dump_references (f); |
5932a4d4 | 963 | fprintf (f, " Referring: "); |
d52f5295 ML |
964 | dump_referring (f); |
965 | if (lto_file_data) | |
b5493fb2 | 966 | fprintf (f, " Read from file: %s\n", |
d52f5295 | 967 | lto_file_data->file_name); |
8f940ee6 JH |
968 | } |
969 | ||
d52f5295 | 970 | /* Dump symtab node to F. */ |
8f940ee6 JH |
971 | |
972 | void | |
d52f5295 | 973 | symtab_node::dump (FILE *f) |
8f940ee6 | 974 | { |
d52f5295 ML |
975 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) |
976 | cnode->dump (f); | |
977 | else if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) | |
978 | vnode->dump (f); | |
8f940ee6 JH |
979 | } |
980 | ||
34e64622 GB |
981 | void |
982 | symtab_node::dump_graphviz (FILE *f) | |
983 | { | |
984 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) | |
985 | cnode->dump_graphviz (f); | |
986 | } | |
987 | ||
8f940ee6 | 988 | void |
6c52831d | 989 | symbol_table::dump (FILE *f) |
8f940ee6 | 990 | { |
5e20cdc9 | 991 | symtab_node *node; |
8f940ee6 JH |
992 | fprintf (f, "Symbol table:\n\n"); |
993 | FOR_EACH_SYMBOL (node) | |
d52f5295 | 994 | node->dump (f); |
8f940ee6 JH |
995 | } |
996 | ||
34e64622 GB |
997 | void |
998 | symbol_table::dump_graphviz (FILE *f) | |
999 | { | |
1000 | symtab_node *node; | |
1001 | fprintf (f, "digraph symtab {\n"); | |
1002 | FOR_EACH_SYMBOL (node) | |
1003 | node->dump_graphviz (f); | |
1004 | fprintf (f, "}\n"); | |
1005 | } | |
1006 | ||
e94497fb RB |
1007 | DEBUG_FUNCTION void |
1008 | symbol_table::debug (void) | |
1009 | { | |
1010 | dump (stderr); | |
1011 | } | |
1012 | ||
3dafb85c ML |
1013 | /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME. |
1014 | Return NULL if there's no such node. */ | |
1015 | ||
1016 | symtab_node * | |
1017 | symtab_node::get_for_asmname (const_tree asmname) | |
1018 | { | |
1019 | symtab_node *node; | |
3dafb85c ML |
1020 | |
1021 | symtab->symtab_initialize_asm_name_hash (); | |
2a22f99c TS |
1022 | hashval_t hash = symtab->decl_assembler_name_hash (asmname); |
1023 | symtab_node **slot | |
1024 | = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash, | |
1025 | NO_INSERT); | |
3dafb85c ML |
1026 | |
1027 | if (slot) | |
1028 | { | |
2a22f99c | 1029 | node = *slot; |
3dafb85c ML |
1030 | return node; |
1031 | } | |
1032 | return NULL; | |
1033 | } | |
1034 | ||
8f940ee6 JH |
1035 | /* Dump symtab node NODE to stderr. */ |
1036 | ||
1037 | DEBUG_FUNCTION void | |
d52f5295 | 1038 | symtab_node::debug (void) |
8f940ee6 | 1039 | { |
d52f5295 | 1040 | dump (stderr); |
8f940ee6 JH |
1041 | } |
1042 | ||
474ffc72 JH |
1043 | /* Verify common part of symtab nodes. */ |
1044 | ||
0ecf545c MS |
1045 | #if __GNUC__ >= 10 |
1046 | /* Disable warnings about missing quoting in GCC diagnostics for | |
1047 | the verification errors. Their format strings don't follow GCC | |
1048 | diagnostic conventions and the calls are ultimately followed by | |
1049 | one to internal_error. */ | |
1050 | # pragma GCC diagnostic push | |
1051 | # pragma GCC diagnostic ignored "-Wformat-diag" | |
1052 | #endif | |
1053 | ||
474ffc72 | 1054 | DEBUG_FUNCTION bool |
d52f5295 | 1055 | symtab_node::verify_base (void) |
474ffc72 JH |
1056 | { |
1057 | bool error_found = false; | |
5e20cdc9 | 1058 | symtab_node *hashed_node; |
474ffc72 | 1059 | |
d52f5295 | 1060 | if (is_a <cgraph_node *> (this)) |
474ffc72 | 1061 | { |
d52f5295 | 1062 | if (TREE_CODE (decl) != FUNCTION_DECL) |
474ffc72 JH |
1063 | { |
1064 | error ("function symbol is not function"); | |
1065 | error_found = true; | |
1066 | } | |
aab778d3 L |
1067 | else if ((lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)) |
1068 | != NULL) | |
1069 | != dyn_cast <cgraph_node *> (this)->ifunc_resolver) | |
1070 | { | |
904f3daa | 1071 | error ("inconsistent %<ifunc%> attribute"); |
aab778d3 L |
1072 | error_found = true; |
1073 | } | |
474ffc72 | 1074 | } |
d52f5295 | 1075 | else if (is_a <varpool_node *> (this)) |
474ffc72 | 1076 | { |
8813a647 | 1077 | if (!VAR_P (decl)) |
474ffc72 JH |
1078 | { |
1079 | error ("variable symbol is not variable"); | |
1080 | error_found = true; | |
1081 | } | |
1082 | } | |
1083 | else | |
1084 | { | |
1085 | error ("node has unknown type"); | |
1086 | error_found = true; | |
1087 | } | |
1088 | ||
3dafb85c | 1089 | if (symtab->state != LTO_STREAMING) |
474ffc72 | 1090 | { |
d52f5295 | 1091 | hashed_node = symtab_node::get (decl); |
ca0f62a8 JH |
1092 | if (!hashed_node) |
1093 | { | |
aede2c10 | 1094 | error ("node not found node->decl->decl_with_vis.symtab_node"); |
ca0f62a8 JH |
1095 | error_found = true; |
1096 | } | |
d52f5295 ML |
1097 | if (hashed_node != this |
1098 | && (!is_a <cgraph_node *> (this) | |
1099 | || !dyn_cast <cgraph_node *> (this)->clone_of | |
1100 | || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl)) | |
e5b962d0 | 1101 | { |
aede2c10 | 1102 | error ("node differs from node->decl->decl_with_vis.symtab_node"); |
e5b962d0 JH |
1103 | error_found = true; |
1104 | } | |
474ffc72 | 1105 | } |
3dafb85c | 1106 | if (symtab->assembler_name_hash) |
474ffc72 | 1107 | { |
3dafb85c | 1108 | hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl)); |
606711a1 | 1109 | if (hashed_node) |
474ffc72 | 1110 | { |
606711a1 ML |
1111 | if (hashed_node->previous_sharing_asm_name) |
1112 | { | |
1113 | error ("assembler name hash list corrupted"); | |
1114 | error_found = true; | |
1115 | } | |
1116 | else if (previous_sharing_asm_name == NULL) | |
1117 | { | |
1118 | if (hashed_node != this) | |
1119 | { | |
1120 | error ("assembler name hash list corrupted"); | |
1121 | error_found = true; | |
1122 | } | |
1123 | } | |
1124 | else if (!(is_a <varpool_node *> (this) && DECL_HARD_REGISTER (decl))) | |
1125 | { | |
1126 | if (!asmname_hasher::equal (previous_sharing_asm_name, | |
1127 | DECL_ASSEMBLER_NAME (decl))) | |
1128 | { | |
1129 | error ("node not found in symtab assembler name hash"); | |
1130 | error_found = true; | |
1131 | } | |
1132 | } | |
474ffc72 JH |
1133 | } |
1134 | } | |
d52f5295 ML |
1135 | if (previous_sharing_asm_name |
1136 | && previous_sharing_asm_name->next_sharing_asm_name != this) | |
474ffc72 JH |
1137 | { |
1138 | error ("double linked list of assembler names corrupted"); | |
87be7f0c JH |
1139 | error_found = true; |
1140 | } | |
1141 | if (body_removed && definition) | |
1142 | { | |
1143 | error ("node has body_removed but is definition"); | |
e70670cf JH |
1144 | error_found = true; |
1145 | } | |
d52f5295 | 1146 | if (analyzed && !definition) |
e70670cf | 1147 | { |
bd2c6270 | 1148 | error ("node is analyzed but it is not a definition"); |
e70670cf | 1149 | error_found = true; |
474ffc72 | 1150 | } |
d52f5295 | 1151 | if (cpp_implicit_alias && !alias) |
40a7fe1e JH |
1152 | { |
1153 | error ("node is alias but not implicit alias"); | |
1154 | error_found = true; | |
1155 | } | |
d52f5295 | 1156 | if (alias && !definition && !weakref) |
40a7fe1e JH |
1157 | { |
1158 | error ("node is alias but not definition"); | |
1159 | error_found = true; | |
1160 | } | |
71e54687 JH |
1161 | if (weakref && !transparent_alias) |
1162 | { | |
1163 | error ("node is weakref but not an transparent_alias"); | |
1164 | error_found = true; | |
1165 | } | |
1166 | if (transparent_alias && !alias) | |
08346abd | 1167 | { |
71e54687 | 1168 | error ("node is transparent_alias but not an alias"); |
08346abd JH |
1169 | error_found = true; |
1170 | } | |
d7ddfbcb JH |
1171 | if (symver && !alias) |
1172 | { | |
1173 | error ("node is symver but not alias"); | |
1174 | error_found = true; | |
1175 | } | |
40ebe1fc JH |
1176 | /* Limitation of gas requires us to output targets of symver aliases as |
1177 | global symbols. This is binutils PR 25295. */ | |
1178 | if (symver | |
1179 | && (!TREE_PUBLIC (get_alias_target ()->decl) | |
1180 | || DECL_VISIBILITY (get_alias_target ()->decl) != VISIBILITY_DEFAULT)) | |
1181 | { | |
1182 | error ("symver target is not exported with default visibility"); | |
1183 | error_found = true; | |
1184 | } | |
1185 | if (symver | |
1186 | && (!TREE_PUBLIC (decl) | |
1187 | || DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT)) | |
1188 | { | |
1189 | error ("symver is not exported with default visibility"); | |
1190 | error_found = true; | |
1191 | } | |
d52f5295 | 1192 | if (same_comdat_group) |
474ffc72 | 1193 | { |
d52f5295 | 1194 | symtab_node *n = same_comdat_group; |
474ffc72 | 1195 | |
aede2c10 | 1196 | if (!n->get_comdat_group ()) |
474ffc72 | 1197 | { |
aede2c10 | 1198 | error ("node is in same_comdat_group list but has no comdat_group"); |
474ffc72 JH |
1199 | error_found = true; |
1200 | } | |
d52f5295 | 1201 | if (n->get_comdat_group () != get_comdat_group ()) |
7b3376a0 JH |
1202 | { |
1203 | error ("same_comdat_group list across different groups"); | |
1204 | error_found = true; | |
1205 | } | |
d52f5295 | 1206 | if (n->type != type) |
474ffc72 JH |
1207 | { |
1208 | error ("mixing different types of symbol in same comdat groups is not supported"); | |
1209 | error_found = true; | |
1210 | } | |
d52f5295 | 1211 | if (n == this) |
474ffc72 JH |
1212 | { |
1213 | error ("node is alone in a comdat group"); | |
1214 | error_found = true; | |
1215 | } | |
1216 | do | |
1217 | { | |
67348ccc | 1218 | if (!n->same_comdat_group) |
474ffc72 JH |
1219 | { |
1220 | error ("same_comdat_group is not a circular list"); | |
1221 | error_found = true; | |
1222 | break; | |
1223 | } | |
67348ccc | 1224 | n = n->same_comdat_group; |
474ffc72 | 1225 | } |
d52f5295 ML |
1226 | while (n != this); |
1227 | if (comdat_local_p ()) | |
1f26ac87 | 1228 | { |
3dafb85c | 1229 | ipa_ref *ref = NULL; |
e257a17c | 1230 | |
d52f5295 | 1231 | for (int i = 0; iterate_referring (i, ref); ++i) |
1f26ac87 | 1232 | { |
d52f5295 | 1233 | if (!in_same_comdat_group_p (ref->referring)) |
1f26ac87 JM |
1234 | { |
1235 | error ("comdat-local symbol referred to by %s outside its " | |
1236 | "comdat", | |
1237 | identifier_to_locale (ref->referring->name())); | |
1238 | error_found = true; | |
1239 | } | |
1240 | } | |
1241 | } | |
474ffc72 | 1242 | } |
d52f5295 | 1243 | if (implicit_section && !get_section ()) |
e257a17c | 1244 | { |
93964ebd | 1245 | error ("implicit_section flag is set but section isn%'t"); |
e257a17c JH |
1246 | error_found = true; |
1247 | } | |
d52f5295 | 1248 | if (get_section () && get_comdat_group () |
b0122457 TS |
1249 | && !implicit_section |
1250 | && !lookup_attribute ("section", DECL_ATTRIBUTES (decl))) | |
e257a17c JH |
1251 | { |
1252 | error ("Both section and comdat group is set"); | |
1253 | error_found = true; | |
1254 | } | |
1255 | /* TODO: Add string table for sections, so we do not keep holding duplicated | |
1256 | strings. */ | |
d52f5295 ML |
1257 | if (alias && definition |
1258 | && get_section () != get_alias_target ()->get_section () | |
1259 | && (!get_section() | |
1260 | || !get_alias_target ()->get_section () | |
1261 | || strcmp (get_section(), | |
1262 | get_alias_target ()->get_section ()))) | |
e257a17c | 1263 | { |
93964ebd | 1264 | error ("Alias and target%'s section differs"); |
d52f5295 | 1265 | get_alias_target ()->dump (stderr); |
e257a17c JH |
1266 | error_found = true; |
1267 | } | |
d52f5295 ML |
1268 | if (alias && definition |
1269 | && get_comdat_group () != get_alias_target ()->get_comdat_group ()) | |
e257a17c | 1270 | { |
93964ebd | 1271 | error ("Alias and target%'s comdat groups differs"); |
d52f5295 | 1272 | get_alias_target ()->dump (stderr); |
e257a17c JH |
1273 | error_found = true; |
1274 | } | |
71e54687 JH |
1275 | if (transparent_alias && definition && !weakref) |
1276 | { | |
1277 | symtab_node *to = get_alias_target (); | |
1278 | const char *name1 | |
1279 | = IDENTIFIER_POINTER ( | |
1280 | ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (decl))); | |
1281 | const char *name2 | |
1282 | = IDENTIFIER_POINTER ( | |
1283 | ultimate_transparent_alias_target (DECL_ASSEMBLER_NAME (to->decl))); | |
1284 | if (!symbol_table::assembler_names_equal_p (name1, name2)) | |
1285 | { | |
93964ebd | 1286 | error ("Transparent alias and target%'s assembler names differs"); |
71e54687 JH |
1287 | get_alias_target ()->dump (stderr); |
1288 | error_found = true; | |
1289 | } | |
1290 | } | |
1291 | if (transparent_alias && definition | |
1292 | && get_alias_target()->transparent_alias && get_alias_target()->analyzed) | |
1293 | { | |
1294 | error ("Chained transparent aliases"); | |
1295 | get_alias_target ()->dump (stderr); | |
1296 | error_found = true; | |
1297 | } | |
e257a17c | 1298 | |
474ffc72 JH |
1299 | return error_found; |
1300 | } | |
1301 | ||
1302 | /* Verify consistency of NODE. */ | |
1303 | ||
1304 | DEBUG_FUNCTION void | |
d52f5295 | 1305 | symtab_node::verify (void) |
474ffc72 JH |
1306 | { |
1307 | if (seen_error ()) | |
1308 | return; | |
1309 | ||
1310 | timevar_push (TV_CGRAPH_VERIFY); | |
d52f5295 ML |
1311 | if (cgraph_node *node = dyn_cast <cgraph_node *> (this)) |
1312 | node->verify_node (); | |
474ffc72 | 1313 | else |
d52f5295 | 1314 | if (verify_base ()) |
474ffc72 | 1315 | { |
d52f5295 ML |
1316 | debug (); |
1317 | internal_error ("symtab_node::verify failed"); | |
474ffc72 JH |
1318 | } |
1319 | timevar_pop (TV_CGRAPH_VERIFY); | |
1320 | } | |
1321 | ||
1322 | /* Verify symbol table for internal consistency. */ | |
1323 | ||
1324 | DEBUG_FUNCTION void | |
d52f5295 | 1325 | symtab_node::verify_symtab_nodes (void) |
474ffc72 | 1326 | { |
5e20cdc9 | 1327 | symtab_node *node; |
1eb68d2d | 1328 | hash_map<tree, symtab_node *> comdat_head_map (251); |
e257a17c | 1329 | |
474ffc72 | 1330 | FOR_EACH_SYMBOL (node) |
e257a17c | 1331 | { |
d52f5295 | 1332 | node->verify (); |
e257a17c JH |
1333 | if (node->get_comdat_group ()) |
1334 | { | |
1335 | symtab_node **entry, *s; | |
1336 | bool existed; | |
1337 | ||
1eb68d2d TS |
1338 | entry = &comdat_head_map.get_or_insert (node->get_comdat_group (), |
1339 | &existed); | |
e257a17c JH |
1340 | if (!existed) |
1341 | *entry = node; | |
6e85fbaa JH |
1342 | else if (!DECL_EXTERNAL (node->decl)) |
1343 | { | |
b7e85ee1 IE |
1344 | for (s = (*entry)->same_comdat_group; |
1345 | s != NULL && s != node && s != *entry; | |
6e85fbaa JH |
1346 | s = s->same_comdat_group) |
1347 | ; | |
e257a17c JH |
1348 | if (!s || s == *entry) |
1349 | { | |
6e85fbaa JH |
1350 | error ("Two symbols with same comdat_group are not linked by " |
1351 | "the same_comdat_group list."); | |
d52f5295 ML |
1352 | (*entry)->debug (); |
1353 | node->debug (); | |
1354 | internal_error ("symtab_node::verify failed"); | |
e257a17c | 1355 | } |
6e85fbaa | 1356 | } |
e257a17c JH |
1357 | } |
1358 | } | |
474ffc72 JH |
1359 | } |
1360 | ||
0ecf545c MS |
1361 | #if __GNUC__ >= 10 |
1362 | # pragma GCC diagnostic pop | |
1363 | #endif | |
1364 | ||
65d630d4 JH |
1365 | /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early, |
1366 | but other code such as notice_global_symbol generates rtl. */ | |
40a7fe1e | 1367 | |
65d630d4 | 1368 | void |
d52f5295 | 1369 | symtab_node::make_decl_local (void) |
65d630d4 JH |
1370 | { |
1371 | rtx rtl, symbol; | |
1372 | ||
71e54687 JH |
1373 | if (weakref) |
1374 | { | |
1375 | weakref = false; | |
1376 | IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) = 0; | |
1377 | TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) = NULL_TREE; | |
1378 | symtab->change_decl_assembler_name | |
1379 | (decl, DECL_ASSEMBLER_NAME (get_alias_target ()->decl)); | |
1380 | DECL_ATTRIBUTES (decl) = remove_attribute ("weakref", | |
1381 | DECL_ATTRIBUTES (decl)); | |
1382 | } | |
aede2c10 | 1383 | /* Avoid clearing comdat_groups on comdat-local decls. */ |
71e54687 | 1384 | else if (TREE_PUBLIC (decl) == 0) |
1f26ac87 JM |
1385 | return; |
1386 | ||
71e54687 JH |
1387 | /* Localizing a symbol also make all its transparent aliases local. */ |
1388 | ipa_ref *ref; | |
1389 | for (unsigned i = 0; iterate_direct_aliases (i, ref); i++) | |
1390 | { | |
1391 | struct symtab_node *alias = ref->referring; | |
1392 | if (alias->transparent_alias) | |
1393 | alias->make_decl_local (); | |
1394 | } | |
1395 | ||
8813a647 | 1396 | if (VAR_P (decl)) |
0a7246ee JH |
1397 | { |
1398 | DECL_COMMON (decl) = 0; | |
1399 | /* ADDRESSABLE flag is not defined for public symbols. */ | |
1400 | TREE_ADDRESSABLE (decl) = 1; | |
71e54687 | 1401 | TREE_STATIC (decl) = 1; |
0a7246ee | 1402 | } |
24d3f325 NS |
1403 | else |
1404 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); | |
65d630d4 | 1405 | |
24d047a3 | 1406 | DECL_COMDAT (decl) = 0; |
65d630d4 JH |
1407 | DECL_WEAK (decl) = 0; |
1408 | DECL_EXTERNAL (decl) = 0; | |
b5493fb2 JH |
1409 | DECL_VISIBILITY_SPECIFIED (decl) = 0; |
1410 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; | |
65d630d4 | 1411 | TREE_PUBLIC (decl) = 0; |
feeca9cc | 1412 | DECL_DLLIMPORT_P (decl) = 0; |
65d630d4 JH |
1413 | if (!DECL_RTL_SET_P (decl)) |
1414 | return; | |
1415 | ||
1416 | /* Update rtl flags. */ | |
1417 | make_decl_rtl (decl); | |
1418 | ||
1419 | rtl = DECL_RTL (decl); | |
1420 | if (!MEM_P (rtl)) | |
1421 | return; | |
1422 | ||
1423 | symbol = XEXP (rtl, 0); | |
1424 | if (GET_CODE (symbol) != SYMBOL_REF) | |
1425 | return; | |
1426 | ||
1427 | SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); | |
1428 | } | |
e70670cf | 1429 | |
ed2a53e7 JH |
1430 | /* Copy visibility from N. |
1431 | This is useful when THIS becomes a transparent alias of N. */ | |
1432 | ||
1433 | void | |
1434 | symtab_node::copy_visibility_from (symtab_node *n) | |
1435 | { | |
1436 | gcc_checking_assert (n->weakref == weakref); | |
1437 | ||
1438 | ipa_ref *ref; | |
1439 | for (unsigned i = 0; iterate_direct_aliases (i, ref); i++) | |
1440 | { | |
1441 | struct symtab_node *alias = ref->referring; | |
1442 | if (alias->transparent_alias) | |
1443 | alias->copy_visibility_from (n); | |
1444 | } | |
1445 | ||
8813a647 | 1446 | if (VAR_P (decl)) |
ed2a53e7 JH |
1447 | { |
1448 | DECL_COMMON (decl) = DECL_COMMON (n->decl); | |
1449 | /* ADDRESSABLE flag is not defined for public symbols. */ | |
1450 | if (TREE_PUBLIC (decl) && !TREE_PUBLIC (n->decl)) | |
1451 | TREE_ADDRESSABLE (decl) = 1; | |
1452 | TREE_STATIC (decl) = TREE_STATIC (n->decl); | |
1453 | } | |
1454 | else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); | |
1455 | ||
1456 | DECL_COMDAT (decl) = DECL_COMDAT (n->decl); | |
1457 | DECL_WEAK (decl) = DECL_WEAK (n->decl); | |
1458 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (n->decl); | |
1459 | DECL_VISIBILITY_SPECIFIED (decl) = DECL_VISIBILITY_SPECIFIED (n->decl); | |
1460 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (n->decl); | |
1461 | TREE_PUBLIC (decl) = TREE_PUBLIC (n->decl); | |
1462 | DECL_DLLIMPORT_P (decl) = DECL_DLLIMPORT_P (n->decl); | |
1463 | resolution = n->resolution; | |
1464 | set_comdat_group (n->get_comdat_group ()); | |
1465 | call_for_symbol_and_aliases (symtab_node::set_section, | |
1466 | const_cast<char *>(n->get_section ()), true); | |
1467 | externally_visible = n->externally_visible; | |
1468 | if (!DECL_RTL_SET_P (decl)) | |
1469 | return; | |
1470 | ||
1471 | /* Update rtl flags. */ | |
1472 | make_decl_rtl (decl); | |
1473 | ||
1474 | rtx rtl = DECL_RTL (decl); | |
1475 | if (!MEM_P (rtl)) | |
1476 | return; | |
1477 | ||
1478 | rtx symbol = XEXP (rtl, 0); | |
1479 | if (GET_CODE (symbol) != SYMBOL_REF) | |
1480 | return; | |
1481 | ||
1482 | SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); | |
1483 | } | |
1484 | ||
d52f5295 | 1485 | /* Walk the alias chain to return the symbol NODE is alias of. |
e70670cf | 1486 | If NODE is not an alias, return NODE. |
31de7606 | 1487 | Assumes NODE is known to be alias. */ |
e70670cf | 1488 | |
5e20cdc9 | 1489 | symtab_node * |
f13fe18b JH |
1490 | symtab_node::ultimate_alias_target_1 (enum availability *availability, |
1491 | symtab_node *ref) | |
e70670cf | 1492 | { |
71e54687 | 1493 | bool transparent_p = false; |
aaae719d | 1494 | |
aaae719d JH |
1495 | /* To determine visibility of the target, we follow ELF semantic of aliases. |
1496 | Here alias is an alternative assembler name of a given definition. Its | |
1aa95df7 | 1497 | availability prevails the availability of its target (i.e. static alias of |
aaae719d JH |
1498 | weak definition is available. |
1499 | ||
dfea3d6f | 1500 | Transparent alias is just alternative name of a given symbol used within |
71e54687 JH |
1501 | one compilation unit and is translated prior hitting the object file. It |
1502 | inherits the visibility of its target. | |
1503 | Weakref is a different animal (and noweak definition is weak). | |
aaae719d JH |
1504 | |
1505 | If we ever get into supporting targets with different semantics, a target | |
1506 | hook will be needed here. */ | |
1507 | ||
e70670cf | 1508 | if (availability) |
aaae719d | 1509 | { |
71e54687 JH |
1510 | transparent_p = transparent_alias; |
1511 | if (!transparent_p) | |
f13fe18b | 1512 | *availability = get_availability (ref); |
aaae719d | 1513 | else |
71e54687 | 1514 | *availability = AVAIL_NOT_AVAILABLE; |
aaae719d | 1515 | } |
d52f5295 ML |
1516 | |
1517 | symtab_node *node = this; | |
e70670cf JH |
1518 | while (node) |
1519 | { | |
67348ccc | 1520 | if (node->alias && node->analyzed) |
d52f5295 | 1521 | node = node->get_alias_target (); |
e70670cf | 1522 | else |
aaae719d | 1523 | { |
71e54687 | 1524 | if (!availability || (!transparent_p && node->analyzed)) |
aaae719d | 1525 | ; |
71e54687 | 1526 | else if (node->analyzed && !node->transparent_alias) |
f13fe18b | 1527 | *availability = node->get_availability (ref); |
aaae719d JH |
1528 | else |
1529 | *availability = AVAIL_NOT_AVAILABLE; | |
1530 | return node; | |
1531 | } | |
71e54687 JH |
1532 | if (node && availability && transparent_p |
1533 | && node->transparent_alias) | |
e70670cf | 1534 | { |
f13fe18b | 1535 | *availability = node->get_availability (ref); |
71e54687 | 1536 | transparent_p = false; |
e70670cf JH |
1537 | } |
1538 | } | |
1539 | if (availability) | |
1540 | *availability = AVAIL_NOT_AVAILABLE; | |
1541 | return NULL; | |
1542 | } | |
40a7fe1e JH |
1543 | |
1544 | /* C++ FE sometimes change linkage flags after producing same body aliases. | |
1545 | ||
1546 | FIXME: C++ produce implicit aliases for virtual functions and vtables that | |
1547 | are obviously equivalent. The way it is doing so is however somewhat | |
1548 | kludgy and interferes with the visibility code. As a result we need to | |
1549 | copy the visibility from the target to get things right. */ | |
1550 | ||
1551 | void | |
d52f5295 | 1552 | symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target) |
40a7fe1e | 1553 | { |
d52f5295 | 1554 | if (is_a <cgraph_node *> (this)) |
40a7fe1e | 1555 | { |
d52f5295 | 1556 | DECL_DECLARED_INLINE_P (decl) |
67348ccc | 1557 | = DECL_DECLARED_INLINE_P (target->decl); |
d52f5295 | 1558 | DECL_DISREGARD_INLINE_LIMITS (decl) |
67348ccc | 1559 | = DECL_DISREGARD_INLINE_LIMITS (target->decl); |
40a7fe1e JH |
1560 | } |
1561 | /* FIXME: It is not really clear why those flags should not be copied for | |
1562 | functions, too. */ | |
1563 | else | |
1564 | { | |
d52f5295 ML |
1565 | DECL_WEAK (decl) = DECL_WEAK (target->decl); |
1566 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); | |
1567 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl); | |
40a7fe1e | 1568 | } |
d52f5295 | 1569 | if (TREE_PUBLIC (decl)) |
40a7fe1e | 1570 | { |
aede2c10 JH |
1571 | tree group; |
1572 | ||
d52f5295 ML |
1573 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); |
1574 | DECL_COMDAT (decl) = DECL_COMDAT (target->decl); | |
aede2c10 | 1575 | group = target->get_comdat_group (); |
d52f5295 ML |
1576 | set_comdat_group (group); |
1577 | if (group && !same_comdat_group) | |
1578 | add_to_same_comdat_group (target); | |
40a7fe1e | 1579 | } |
d52f5295 | 1580 | externally_visible = target->externally_visible; |
f961457f JH |
1581 | } |
1582 | ||
1583 | /* Set section, do not recurse into aliases. | |
24d3f325 | 1584 | When one wants to change section of a symbol and its aliases, |
d52f5295 | 1585 | use set_section. */ |
f961457f JH |
1586 | |
1587 | void | |
1588 | symtab_node::set_section_for_node (const char *section) | |
1589 | { | |
1590 | const char *current = get_section (); | |
2a22f99c | 1591 | section_hash_entry **slot; |
f961457f JH |
1592 | |
1593 | if (current == section | |
1594 | || (current && section | |
1595 | && !strcmp (current, section))) | |
1596 | return; | |
1597 | ||
1598 | if (current) | |
1599 | { | |
1600 | x_section->ref_count--; | |
1601 | if (!x_section->ref_count) | |
1602 | { | |
2a22f99c TS |
1603 | hashval_t hash = htab_hash_string (x_section->name); |
1604 | slot = symtab->section_hash->find_slot_with_hash (x_section->name, | |
1605 | hash, INSERT); | |
f961457f | 1606 | ggc_free (x_section); |
2a22f99c | 1607 | symtab->section_hash->clear_slot (slot); |
f961457f JH |
1608 | } |
1609 | x_section = NULL; | |
1610 | } | |
1611 | if (!section) | |
1612 | { | |
1613 | implicit_section = false; | |
1614 | return; | |
1615 | } | |
3dafb85c | 1616 | if (!symtab->section_hash) |
2a22f99c TS |
1617 | symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10); |
1618 | slot = symtab->section_hash->find_slot_with_hash (section, | |
1619 | htab_hash_string (section), | |
1620 | INSERT); | |
f961457f JH |
1621 | if (*slot) |
1622 | x_section = (section_hash_entry *)*slot; | |
1623 | else | |
1624 | { | |
1625 | int len = strlen (section); | |
1626 | *slot = x_section = ggc_cleared_alloc<section_hash_entry> (); | |
1627 | x_section->name = ggc_vec_alloc<char> (len + 1); | |
1628 | memcpy (x_section->name, section, len + 1); | |
1629 | } | |
1630 | x_section->ref_count++; | |
1631 | } | |
1632 | ||
e257a17c JH |
1633 | /* Worker for set_section. */ |
1634 | ||
d52f5295 ML |
1635 | bool |
1636 | symtab_node::set_section (symtab_node *n, void *s) | |
e257a17c | 1637 | { |
f961457f | 1638 | n->set_section_for_node ((char *)s); |
e257a17c JH |
1639 | return false; |
1640 | } | |
1641 | ||
1642 | /* Set section of symbol and its aliases. */ | |
1643 | ||
1644 | void | |
f961457f | 1645 | symtab_node::set_section (const char *section) |
e257a17c | 1646 | { |
f3139680 | 1647 | gcc_assert (!this->alias || !this->analyzed); |
d52f5295 ML |
1648 | call_for_symbol_and_aliases |
1649 | (symtab_node::set_section, const_cast<char *>(section), true); | |
e257a17c JH |
1650 | } |
1651 | ||
569b1784 JH |
1652 | /* Return the initialization priority. */ |
1653 | ||
1654 | priority_type | |
1655 | symtab_node::get_init_priority () | |
1656 | { | |
569b1784 JH |
1657 | if (!this->in_init_priority_hash) |
1658 | return DEFAULT_INIT_PRIORITY; | |
b086d530 TS |
1659 | |
1660 | symbol_priority_map *h = symtab->init_priority_hash->get (this); | |
569b1784 JH |
1661 | return h ? h->init : DEFAULT_INIT_PRIORITY; |
1662 | } | |
1663 | ||
1664 | /* Return the finalization priority. */ | |
1665 | ||
1666 | priority_type | |
1667 | cgraph_node::get_fini_priority () | |
1668 | { | |
569b1784 JH |
1669 | if (!this->in_init_priority_hash) |
1670 | return DEFAULT_INIT_PRIORITY; | |
b086d530 | 1671 | symbol_priority_map *h = symtab->init_priority_hash->get (this); |
569b1784 JH |
1672 | return h ? h->fini : DEFAULT_INIT_PRIORITY; |
1673 | } | |
1674 | ||
569b1784 JH |
1675 | /* Return the initialization and finalization priority information for |
1676 | DECL. If there is no previous priority information, a freshly | |
1677 | allocated structure is returned. */ | |
1678 | ||
3dafb85c | 1679 | symbol_priority_map * |
d52f5295 | 1680 | symtab_node::priority_info (void) |
569b1784 | 1681 | { |
3dafb85c | 1682 | if (!symtab->init_priority_hash) |
b086d530 | 1683 | symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13); |
569b1784 | 1684 | |
b086d530 TS |
1685 | bool existed; |
1686 | symbol_priority_map *h | |
1687 | = &symtab->init_priority_hash->get_or_insert (this, &existed); | |
1688 | if (!existed) | |
569b1784 | 1689 | { |
569b1784 JH |
1690 | h->init = DEFAULT_INIT_PRIORITY; |
1691 | h->fini = DEFAULT_INIT_PRIORITY; | |
d52f5295 | 1692 | in_init_priority_hash = true; |
569b1784 JH |
1693 | } |
1694 | ||
1695 | return h; | |
1696 | } | |
1697 | ||
1698 | /* Set initialization priority to PRIORITY. */ | |
1699 | ||
1700 | void | |
1701 | symtab_node::set_init_priority (priority_type priority) | |
1702 | { | |
3dafb85c | 1703 | symbol_priority_map *h; |
569b1784 JH |
1704 | |
1705 | if (is_a <cgraph_node *> (this)) | |
1706 | gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl)); | |
1707 | ||
1708 | if (priority == DEFAULT_INIT_PRIORITY) | |
1709 | { | |
1710 | gcc_assert (get_init_priority() == priority); | |
1711 | return; | |
1712 | } | |
d52f5295 | 1713 | h = priority_info (); |
569b1784 JH |
1714 | h->init = priority; |
1715 | } | |
1716 | ||
dfea3d6f | 1717 | /* Set finalization priority to PRIORITY. */ |
569b1784 JH |
1718 | |
1719 | void | |
1720 | cgraph_node::set_fini_priority (priority_type priority) | |
1721 | { | |
3dafb85c | 1722 | symbol_priority_map *h; |
569b1784 JH |
1723 | |
1724 | gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl)); | |
1725 | ||
1726 | if (priority == DEFAULT_INIT_PRIORITY) | |
1727 | { | |
1728 | gcc_assert (get_fini_priority() == priority); | |
1729 | return; | |
1730 | } | |
d52f5295 | 1731 | h = priority_info (); |
569b1784 JH |
1732 | h->fini = priority; |
1733 | } | |
1734 | ||
e257a17c JH |
1735 | /* Worker for symtab_resolve_alias. */ |
1736 | ||
d52f5295 ML |
1737 | bool |
1738 | symtab_node::set_implicit_section (symtab_node *n, | |
1739 | void *data ATTRIBUTE_UNUSED) | |
e257a17c JH |
1740 | { |
1741 | n->implicit_section = true; | |
1742 | return false; | |
1743 | } | |
1744 | ||
d52f5295 | 1745 | /* Add reference recording that symtab node is alias of TARGET. |
40a7fe1e JH |
1746 | The function can fail in the case of aliasing cycles; in this case |
1747 | it returns false. */ | |
1748 | ||
1749 | bool | |
71e54687 | 1750 | symtab_node::resolve_alias (symtab_node *target, bool transparent) |
40a7fe1e | 1751 | { |
5e20cdc9 | 1752 | symtab_node *n; |
40a7fe1e | 1753 | |
d52f5295 | 1754 | gcc_assert (!analyzed && !vec_safe_length (ref_list.references)); |
40a7fe1e JH |
1755 | |
1756 | /* Never let cycles to creep into the symbol table alias references; | |
1757 | those will make alias walkers to be infinite. */ | |
67348ccc | 1758 | for (n = target; n && n->alias; |
d52f5295 ML |
1759 | n = n->analyzed ? n->get_alias_target () : NULL) |
1760 | if (n == this) | |
40a7fe1e | 1761 | { |
d52f5295 ML |
1762 | if (is_a <cgraph_node *> (this)) |
1763 | error ("function %q+D part of alias cycle", decl); | |
1764 | else if (is_a <varpool_node *> (this)) | |
1765 | error ("variable %q+D part of alias cycle", decl); | |
40a7fe1e JH |
1766 | else |
1767 | gcc_unreachable (); | |
d52f5295 | 1768 | alias = false; |
40a7fe1e JH |
1769 | return false; |
1770 | } | |
1771 | ||
1772 | /* "analyze" the node - i.e. mark the reference. */ | |
d52f5295 ML |
1773 | definition = true; |
1774 | alias = true; | |
1775 | analyzed = true; | |
71e54687 JH |
1776 | transparent |= transparent_alias; |
1777 | transparent_alias = transparent; | |
1778 | if (transparent) | |
1779 | while (target->transparent_alias && target->analyzed) | |
1780 | target = target->get_alias_target (); | |
3dafb85c | 1781 | create_reference (target, IPA_REF_ALIAS, NULL); |
40a7fe1e | 1782 | |
e257a17c | 1783 | /* Add alias into the comdat group of its target unless it is already there. */ |
d52f5295 ML |
1784 | if (same_comdat_group) |
1785 | remove_from_same_comdat_group (); | |
1786 | set_comdat_group (NULL); | |
e257a17c | 1787 | if (target->get_comdat_group ()) |
d52f5295 | 1788 | add_to_same_comdat_group (target); |
e257a17c | 1789 | |
d52f5295 ML |
1790 | if ((get_section () != target->get_section () |
1791 | || target->get_comdat_group ()) && get_section () && !implicit_section) | |
e257a17c | 1792 | { |
d52f5295 | 1793 | error ("section of alias %q+D must match section of its target", decl); |
e257a17c | 1794 | } |
d52f5295 ML |
1795 | call_for_symbol_and_aliases (symtab_node::set_section, |
1796 | const_cast<char *>(target->get_section ()), true); | |
e257a17c | 1797 | if (target->implicit_section) |
d52f5295 | 1798 | call_for_symbol_and_aliases (set_implicit_section, NULL, true); |
e257a17c | 1799 | |
d67ff7b7 | 1800 | /* Alias targets become redundant after alias is resolved into an reference. |
40a7fe1e JH |
1801 | We do not want to keep it around or we would have to mind updating them |
1802 | when renaming symbols. */ | |
d52f5295 | 1803 | alias_target = NULL; |
40a7fe1e | 1804 | |
71e54687 | 1805 | if (!transparent && cpp_implicit_alias && symtab->state >= CONSTRUCTION) |
d52f5295 | 1806 | fixup_same_cpp_alias_visibility (target); |
40a7fe1e JH |
1807 | |
1808 | /* If alias has address taken, so does the target. */ | |
d52f5295 ML |
1809 | if (address_taken) |
1810 | target->ultimate_alias_target ()->address_taken = true; | |
0a7246ee | 1811 | |
71e54687 JH |
1812 | /* All non-transparent aliases of THIS are now in fact aliases of TARGET. |
1813 | If alias is transparent, also all transparent aliases of THIS are now | |
1814 | aliases of TARGET. | |
1815 | Also merge same comdat group lists. */ | |
0a7246ee JH |
1816 | ipa_ref *ref; |
1817 | for (unsigned i = 0; iterate_direct_aliases (i, ref);) | |
1818 | { | |
1819 | struct symtab_node *alias_alias = ref->referring; | |
71e54687 JH |
1820 | if (alias_alias->get_comdat_group ()) |
1821 | { | |
1822 | alias_alias->remove_from_same_comdat_group (); | |
1823 | alias_alias->set_comdat_group (NULL); | |
1824 | if (target->get_comdat_group ()) | |
1825 | alias_alias->add_to_same_comdat_group (target); | |
1826 | } | |
d7ddfbcb JH |
1827 | if ((!alias_alias->transparent_alias |
1828 | && !alias_alias->symver) | |
1829 | || transparent) | |
0a7246ee JH |
1830 | { |
1831 | alias_alias->remove_all_references (); | |
1832 | alias_alias->create_reference (target, IPA_REF_ALIAS, NULL); | |
1833 | } | |
1834 | else i++; | |
1835 | } | |
40a7fe1e JH |
1836 | return true; |
1837 | } | |
af15184a | 1838 | |
d52f5295 | 1839 | /* Worker searching noninterposable alias. */ |
af15184a | 1840 | |
d52f5295 ML |
1841 | bool |
1842 | symtab_node::noninterposable_alias (symtab_node *node, void *data) | |
af15184a | 1843 | { |
54f97f90 | 1844 | if (!node->transparent_alias && decl_binds_to_current_def_p (node->decl)) |
af15184a | 1845 | { |
d52f5295 | 1846 | symtab_node *fn = node->ultimate_alias_target (); |
72732f3e JH |
1847 | |
1848 | /* Ensure that the alias is well formed this may not be the case | |
1849 | of user defined aliases and currently it is not always the case | |
1850 | of C++ same body aliases (that is a bug). */ | |
1851 | if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl) | |
1852 | || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl) | |
1853 | || (TREE_CODE (node->decl) == FUNCTION_DECL | |
1854 | && flags_from_decl_or_type (node->decl) | |
1855 | != flags_from_decl_or_type (fn->decl)) | |
1856 | || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl)) | |
1857 | return false; | |
5e20cdc9 | 1858 | *(symtab_node **)data = node; |
af15184a JH |
1859 | return true; |
1860 | } | |
1861 | return false; | |
1862 | } | |
1863 | ||
67914693 | 1864 | /* If node cannot be overwriten by static or dynamic linker to point to |
d52f5295 ML |
1865 | different definition, return NODE. Otherwise look for alias with such |
1866 | property and if none exists, introduce new one. */ | |
af15184a | 1867 | |
5e20cdc9 | 1868 | symtab_node * |
d52f5295 | 1869 | symtab_node::noninterposable_alias (void) |
af15184a JH |
1870 | { |
1871 | tree new_decl; | |
5e20cdc9 | 1872 | symtab_node *new_node = NULL; |
8a41354f JH |
1873 | |
1874 | /* First try to look up existing alias or base object | |
1875 | (if that is already non-overwritable). */ | |
d52f5295 | 1876 | symtab_node *node = ultimate_alias_target (); |
67348ccc | 1877 | gcc_assert (!node->alias && !node->weakref); |
d52f5295 | 1878 | node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias, |
5fb07870 | 1879 | (void *)&new_node, true); |
af15184a JH |
1880 | if (new_node) |
1881 | return new_node; | |
a8b522b4 | 1882 | |
cdb87c08 | 1883 | /* If aliases aren't supported by the assembler, fail. */ |
a8b522b4 ML |
1884 | if (!TARGET_SUPPORTS_ALIASES) |
1885 | return NULL; | |
af15184a | 1886 | |
8a41354f | 1887 | /* Otherwise create a new one. */ |
67348ccc | 1888 | new_decl = copy_node (node->decl); |
feeca9cc | 1889 | DECL_DLLIMPORT_P (new_decl) = 0; |
5fb07870 JJ |
1890 | tree name = clone_function_name (node->decl, "localalias"); |
1891 | if (!flag_wpa) | |
1892 | { | |
1893 | unsigned long num = 0; | |
1894 | /* In the rare case we already have a localalias, but the above | |
1895 | node->call_for_symbol_and_aliases call didn't find any suitable, | |
1896 | iterate until we find one not used yet. */ | |
1897 | while (symtab_node::get_for_asmname (name)) | |
1898 | name = clone_function_name (node->decl, "localalias", num++); | |
1899 | } | |
1900 | DECL_NAME (new_decl) = name; | |
af15184a JH |
1901 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1902 | DECL_STRUCT_FUNCTION (new_decl) = NULL; | |
1903 | DECL_INITIAL (new_decl) = NULL; | |
1904 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
1905 | SET_DECL_RTL (new_decl, NULL); | |
1906 | ||
1907 | /* Update the properties. */ | |
1908 | DECL_EXTERNAL (new_decl) = 0; | |
af15184a JH |
1909 | TREE_PUBLIC (new_decl) = 0; |
1910 | DECL_COMDAT (new_decl) = 0; | |
1911 | DECL_WEAK (new_decl) = 0; | |
80bc9b6e JH |
1912 | |
1913 | /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */ | |
1914 | DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl); | |
af15184a JH |
1915 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1916 | { | |
1917 | DECL_STATIC_CONSTRUCTOR (new_decl) = 0; | |
1918 | DECL_STATIC_DESTRUCTOR (new_decl) = 0; | |
d52f5295 | 1919 | new_node = cgraph_node::create_alias (new_decl, node->decl); |
b74d8dc4 JH |
1920 | |
1921 | cgraph_node *new_cnode = dyn_cast <cgraph_node *> (new_node), | |
1922 | *cnode = dyn_cast <cgraph_node *> (node); | |
1923 | ||
1924 | new_cnode->unit_id = cnode->unit_id; | |
1925 | new_cnode->merged_comdat = cnode->merged_comdat; | |
1926 | new_cnode->merged_extern_inline = cnode->merged_extern_inline; | |
af15184a JH |
1927 | } |
1928 | else | |
97ae6b64 JH |
1929 | { |
1930 | TREE_READONLY (new_decl) = TREE_READONLY (node->decl); | |
80bc9b6e | 1931 | DECL_INITIAL (new_decl) = error_mark_node; |
9041d2e6 | 1932 | new_node = varpool_node::create_alias (new_decl, node->decl); |
97ae6b64 | 1933 | } |
d52f5295 | 1934 | new_node->resolve_alias (node); |
97ae6b64 JH |
1935 | gcc_assert (decl_binds_to_current_def_p (new_decl) |
1936 | && targetm.binds_local_p (new_decl)); | |
af15184a JH |
1937 | return new_node; |
1938 | } | |
fc11f321 | 1939 | |
d52f5295 ML |
1940 | /* Return true if symtab node and TARGET represents |
1941 | semantically equivalent symbols. */ | |
fc11f321 JH |
1942 | |
1943 | bool | |
d52f5295 | 1944 | symtab_node::semantically_equivalent_p (symtab_node *target) |
fc11f321 JH |
1945 | { |
1946 | enum availability avail; | |
5e20cdc9 DM |
1947 | symtab_node *ba; |
1948 | symtab_node *bb; | |
fc11f321 JH |
1949 | |
1950 | /* Equivalent functions are equivalent. */ | |
d52f5295 | 1951 | if (decl == target->decl) |
fc11f321 JH |
1952 | return true; |
1953 | ||
1954 | /* If symbol is not overwritable by different implementation, | |
1955 | walk to the base object it defines. */ | |
d52f5295 | 1956 | ba = ultimate_alias_target (&avail); |
fc11f321 JH |
1957 | if (avail >= AVAIL_AVAILABLE) |
1958 | { | |
d52f5295 | 1959 | if (target == ba) |
fc11f321 JH |
1960 | return true; |
1961 | } | |
1962 | else | |
d52f5295 ML |
1963 | ba = this; |
1964 | bb = target->ultimate_alias_target (&avail); | |
fc11f321 JH |
1965 | if (avail >= AVAIL_AVAILABLE) |
1966 | { | |
d52f5295 | 1967 | if (this == bb) |
fc11f321 JH |
1968 | return true; |
1969 | } | |
1970 | else | |
d52f5295 | 1971 | bb = target; |
fc11f321 JH |
1972 | return bb == ba; |
1973 | } | |
ddb3e20a | 1974 | |
d52f5295 | 1975 | /* Classify symbol symtab node for partitioning. */ |
ddb3e20a JH |
1976 | |
1977 | enum symbol_partitioning_class | |
d52f5295 | 1978 | symtab_node::get_partitioning_class (void) |
ddb3e20a JH |
1979 | { |
1980 | /* Inline clones are always duplicated. | |
dfea3d6f | 1981 | This include external declarations. */ |
d52f5295 | 1982 | cgraph_node *cnode = dyn_cast <cgraph_node *> (this); |
ddb3e20a | 1983 | |
00de328a | 1984 | if (DECL_ABSTRACT_P (decl)) |
ddb3e20a JH |
1985 | return SYMBOL_EXTERNAL; |
1986 | ||
a62bfab5 | 1987 | if (cnode && cnode->inlined_to) |
ddb3e20a JH |
1988 | return SYMBOL_DUPLICATE; |
1989 | ||
71e54687 JH |
1990 | /* Transparent aliases are always duplicated. */ |
1991 | if (transparent_alias) | |
1992 | return definition ? SYMBOL_DUPLICATE : SYMBOL_EXTERNAL; | |
ddb3e20a JH |
1993 | |
1994 | /* External declarations are external. */ | |
d52f5295 | 1995 | if (DECL_EXTERNAL (decl)) |
ddb3e20a JH |
1996 | return SYMBOL_EXTERNAL; |
1997 | ||
634c5bca JH |
1998 | /* Even static aliases of external functions as external. Those can happen |
1999 | when COMDAT got resolved to non-IL implementation. */ | |
2000 | if (alias && DECL_EXTERNAL (ultimate_alias_target ()->decl)) | |
2001 | return SYMBOL_EXTERNAL; | |
2002 | ||
d52f5295 | 2003 | if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) |
ddb3e20a | 2004 | { |
d3f2e41e JH |
2005 | if (alias && definition && !ultimate_alias_target ()->definition) |
2006 | return SYMBOL_EXTERNAL; | |
67914693 | 2007 | /* Constant pool references use local symbol names that cannot |
ddb3e20a | 2008 | be promoted global. We should never put into a constant pool |
67914693 | 2009 | objects that cannot be duplicated across partitions. */ |
d52f5295 | 2010 | if (DECL_IN_CONSTANT_POOL (decl)) |
ddb3e20a | 2011 | return SYMBOL_DUPLICATE; |
c97de764 JH |
2012 | if (DECL_HARD_REGISTER (decl)) |
2013 | return SYMBOL_DUPLICATE; | |
ddb3e20a JH |
2014 | gcc_checking_assert (vnode->definition); |
2015 | } | |
2016 | /* Functions that are cloned may stay in callgraph even if they are unused. | |
2017 | Handle them as external; compute_ltrans_boundary take care to make | |
2018 | proper things to happen (i.e. to make them appear in the boundary but | |
2019 | with body streamed, so clone can me materialized). */ | |
d3f2e41e | 2020 | else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition) |
ddb3e20a JH |
2021 | return SYMBOL_EXTERNAL; |
2022 | ||
2023 | /* Linker discardable symbols are duplicated to every use unless they are | |
cf288ed3 | 2024 | keyed. */ |
d52f5295 ML |
2025 | if (DECL_ONE_ONLY (decl) |
2026 | && !force_output | |
2027 | && !forced_by_abi | |
2028 | && !used_from_object_file_p ()) | |
ddb3e20a JH |
2029 | return SYMBOL_DUPLICATE; |
2030 | ||
2031 | return SYMBOL_PARTITION; | |
2032 | } | |
89330618 JH |
2033 | |
2034 | /* Return true when symbol is known to be non-zero. */ | |
2035 | ||
2036 | bool | |
2037 | symtab_node::nonzero_address () | |
2038 | { | |
2039 | /* Weakrefs may be NULL when their target is not defined. */ | |
f7217cde | 2040 | if (alias && weakref) |
89330618 | 2041 | { |
f7217cde | 2042 | if (analyzed) |
89330618 | 2043 | { |
d52f5295 | 2044 | symtab_node *target = ultimate_alias_target (); |
89330618 JH |
2045 | |
2046 | if (target->alias && target->weakref) | |
2047 | return false; | |
67914693 | 2048 | /* We cannot recurse to target::nonzero. It is possible that the |
89330618 JH |
2049 | target is used only via the alias. |
2050 | We may walk references and look for strong use, but we do not know | |
2051 | if this strong use will survive to final binary, so be | |
2052 | conservative here. | |
2053 | ??? Maybe we could do the lookup during late optimization that | |
2054 | could be useful to eliminate the NULL pointer checks in LTO | |
2055 | programs. */ | |
2056 | if (target->definition && !DECL_EXTERNAL (target->decl)) | |
f7217cde | 2057 | return true; |
89330618 JH |
2058 | if (target->resolution != LDPR_UNKNOWN |
2059 | && target->resolution != LDPR_UNDEF | |
f1703a2e | 2060 | && !target->can_be_discarded_p () |
89330618 JH |
2061 | && flag_delete_null_pointer_checks) |
2062 | return true; | |
2063 | return false; | |
2064 | } | |
2065 | else | |
2066 | return false; | |
2067 | } | |
2068 | ||
2069 | /* With !flag_delete_null_pointer_checks we assume that symbols may | |
2070 | bind to NULL. This is on by default on embedded targets only. | |
2071 | ||
2072 | Otherwise all non-WEAK symbols must be defined and thus non-NULL or | |
3774c0b9 JM |
2073 | linking fails. Important case of WEAK we want to do well are comdats, |
2074 | which also must be defined somewhere. | |
89330618 JH |
2075 | |
2076 | When parsing, beware the cases when WEAK attribute is added later. */ | |
3774c0b9 | 2077 | if ((!DECL_WEAK (decl) || DECL_COMDAT (decl)) |
f7217cde JH |
2078 | && flag_delete_null_pointer_checks) |
2079 | { | |
2080 | refuse_visibility_changes = true; | |
2081 | return true; | |
2082 | } | |
89330618 | 2083 | |
3774c0b9 | 2084 | /* If target is defined and not extern, we know it will be |
f22ed1ed JM |
2085 | output and thus it will bind to non-NULL. |
2086 | Play safe for flag_delete_null_pointer_checks where weak definition may | |
89330618 | 2087 | be re-defined by NULL. */ |
3774c0b9 | 2088 | if (definition && !DECL_EXTERNAL (decl) |
f7217cde JH |
2089 | && (flag_delete_null_pointer_checks || !DECL_WEAK (decl))) |
2090 | { | |
2091 | if (!DECL_WEAK (decl)) | |
2092 | refuse_visibility_changes = true; | |
2093 | return true; | |
2094 | } | |
89330618 JH |
2095 | |
2096 | /* As the last resort, check the resolution info. */ | |
f7217cde JH |
2097 | if (resolution != LDPR_UNKNOWN |
2098 | && resolution != LDPR_UNDEF | |
f1703a2e | 2099 | && !can_be_discarded_p () |
89330618 JH |
2100 | && flag_delete_null_pointer_checks) |
2101 | return true; | |
2102 | return false; | |
2103 | } | |
3cb251b7 JH |
2104 | |
2105 | /* Return 0 if symbol is known to have different address than S2, | |
2106 | Return 1 if symbol is known to have same address as S2, | |
5c07bed6 | 2107 | return -1 otherwise. |
b2539e1e JH |
2108 | |
2109 | If MEMORY_ACCESSED is true, assume that both memory pointer to THIS | |
2110 | and S2 is going to be accessed. This eliminates the situations when | |
dfea3d6f | 2111 | either THIS or S2 is NULL and is useful for comparing bases when deciding |
b2539e1e | 2112 | about memory aliasing. */ |
3cb251b7 | 2113 | int |
b2539e1e | 2114 | symtab_node::equal_address_to (symtab_node *s2, bool memory_accessed) |
3cb251b7 JH |
2115 | { |
2116 | enum availability avail1, avail2; | |
2117 | ||
2118 | /* A Shortcut: equivalent symbols are always equivalent. */ | |
2119 | if (this == s2) | |
2120 | return 1; | |
2121 | ||
b2539e1e JH |
2122 | /* Unwind transparent aliases first; those are always equal to their |
2123 | target. */ | |
2124 | if (this->transparent_alias && this->analyzed) | |
2125 | return this->get_alias_target ()->equal_address_to (s2); | |
2126 | while (s2->transparent_alias && s2->analyzed) | |
2127 | s2 = s2->get_alias_target(); | |
2128 | ||
2129 | if (this == s2) | |
2130 | return 1; | |
2131 | ||
3cb251b7 JH |
2132 | /* For non-interposable aliases, lookup and compare their actual definitions. |
2133 | Also check if the symbol needs to bind to given definition. */ | |
2134 | symtab_node *rs1 = ultimate_alias_target (&avail1); | |
2135 | symtab_node *rs2 = s2->ultimate_alias_target (&avail2); | |
2136 | bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl); | |
2137 | bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl); | |
2138 | bool really_binds_local1 = binds_local1; | |
2139 | bool really_binds_local2 = binds_local2; | |
2140 | ||
67914693 | 2141 | /* Addresses of vtables and virtual functions cannot be used by user |
3cb251b7 JH |
2142 | code and are used only within speculation. In this case we may make |
2143 | symbol equivalent to its alias even if interposition may break this | |
2144 | rule. Doing so will allow us to turn speculative inlining into | |
dfea3d6f | 2145 | non-speculative more aggressively. */ |
3cb251b7 JH |
2146 | if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE) |
2147 | binds_local1 = true; | |
2148 | if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE) | |
2149 | binds_local2 = true; | |
2150 | ||
2151 | /* If both definitions are available we know that even if they are bound | |
2152 | to other unit they must be defined same way and therefore we can use | |
2153 | equivalence test. */ | |
2154 | if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE) | |
2155 | binds_local1 = binds_local2 = true; | |
2156 | ||
ea83dcf6 | 2157 | if (binds_local1 && binds_local2 && rs1 == rs2) |
3cb251b7 JH |
2158 | { |
2159 | /* We made use of the fact that alias is not weak. */ | |
ea83dcf6 | 2160 | if (rs1 != this) |
3cb251b7 | 2161 | refuse_visibility_changes = true; |
ea83dcf6 | 2162 | if (rs2 != s2) |
3cb251b7 JH |
2163 | s2->refuse_visibility_changes = true; |
2164 | return 1; | |
2165 | } | |
2166 | ||
67914693 | 2167 | /* If both symbols may resolve to NULL, we cannot really prove them |
b2539e1e JH |
2168 | different. */ |
2169 | if (!memory_accessed && !nonzero_address () && !s2->nonzero_address ()) | |
5c07bed6 | 2170 | return -1; |
3cb251b7 JH |
2171 | |
2172 | /* Except for NULL, functions and variables never overlap. */ | |
2173 | if (TREE_CODE (decl) != TREE_CODE (s2->decl)) | |
2174 | return 0; | |
2175 | ||
2176 | /* If one of the symbols is unresolved alias, punt. */ | |
2177 | if (rs1->alias || rs2->alias) | |
5c07bed6 | 2178 | return -1; |
3cb251b7 JH |
2179 | |
2180 | /* If we have a non-interposale definition of at least one of the symbols | |
67914693 | 2181 | and the other symbol is different, we know other unit cannot interpose |
3cb251b7 JH |
2182 | it to the first symbol; all aliases of the definition needs to be |
2183 | present in the current unit. */ | |
2184 | if (((really_binds_local1 || really_binds_local2) | |
2185 | /* If we have both definitions and they are different, we know they | |
2186 | will be different even in units they binds to. */ | |
2187 | || (binds_local1 && binds_local2)) | |
2188 | && rs1 != rs2) | |
2189 | { | |
2190 | /* We make use of the fact that one symbol is not alias of the other | |
2191 | and that the definition is non-interposable. */ | |
2192 | refuse_visibility_changes = true; | |
2193 | s2->refuse_visibility_changes = true; | |
2194 | rs1->refuse_visibility_changes = true; | |
2195 | rs2->refuse_visibility_changes = true; | |
2196 | return 0; | |
2197 | } | |
2198 | ||
2199 | /* TODO: Alias oracle basically assume that addresses of global variables | |
b2539e1e | 2200 | are different unless they are declared as alias of one to another while |
dfea3d6f | 2201 | the code folding comparisons doesn't. |
b2539e1e JH |
2202 | We probably should be consistent and use this fact here, too, but for |
2203 | the moment return false only when we are called from the alias oracle. */ | |
3cb251b7 | 2204 | |
5c07bed6 | 2205 | return memory_accessed && rs1 != rs2 ? 0 : -1; |
3cb251b7 | 2206 | } |
31de7606 JH |
2207 | |
2208 | /* Worker for call_for_symbol_and_aliases. */ | |
2209 | ||
2210 | bool | |
2211 | symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *, | |
2212 | void *), | |
2213 | void *data, | |
2214 | bool include_overwritable) | |
2215 | { | |
2216 | ipa_ref *ref; | |
2217 | FOR_EACH_ALIAS (this, ref) | |
2218 | { | |
2219 | symtab_node *alias = ref->referring; | |
2220 | if (include_overwritable | |
2221 | || alias->get_availability () > AVAIL_INTERPOSABLE) | |
2222 | if (alias->call_for_symbol_and_aliases (callback, data, | |
2223 | include_overwritable)) | |
2224 | return true; | |
2225 | } | |
2226 | return false; | |
2227 | } | |
0a7246ee | 2228 | |
2005b056 | 2229 | /* Return true if address of N is possibly compared. */ |
0a7246ee JH |
2230 | |
2231 | static bool | |
2232 | address_matters_1 (symtab_node *n, void *) | |
2233 | { | |
2234 | struct ipa_ref *ref; | |
2235 | ||
2236 | if (!n->address_can_be_compared_p ()) | |
2237 | return false; | |
2238 | if (n->externally_visible || n->force_output) | |
2239 | return true; | |
2240 | ||
2241 | for (unsigned int i = 0; n->iterate_referring (i, ref); i++) | |
2242 | if (ref->address_matters_p ()) | |
2243 | return true; | |
2244 | return false; | |
2245 | } | |
2246 | ||
2247 | /* Return true if symbol's address may possibly be compared to other | |
2248 | symbol's address. */ | |
2249 | ||
2250 | bool | |
2251 | symtab_node::address_matters_p () | |
2252 | { | |
2253 | gcc_assert (!alias); | |
2254 | return call_for_symbol_and_aliases (address_matters_1, NULL, true); | |
2255 | } | |
428f0c67 | 2256 | |
2005b056 | 2257 | /* Return true if symbol's alignment may be increased. */ |
428f0c67 JH |
2258 | |
2259 | bool | |
2260 | symtab_node::can_increase_alignment_p (void) | |
2261 | { | |
2262 | symtab_node *target = ultimate_alias_target (); | |
2263 | ||
2264 | /* For now support only variables. */ | |
8813a647 | 2265 | if (!VAR_P (decl)) |
428f0c67 JH |
2266 | return false; |
2267 | ||
2268 | /* With -fno-toplevel-reorder we may have already output the constant. */ | |
2269 | if (TREE_ASM_WRITTEN (target->decl)) | |
2270 | return false; | |
2271 | ||
67914693 | 2272 | /* If target is already placed in an anchor, we cannot touch its |
caf2df93 JH |
2273 | alignment. */ |
2274 | if (DECL_RTL_SET_P (target->decl) | |
2275 | && MEM_P (DECL_RTL (target->decl)) | |
2276 | && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0))) | |
2277 | return false; | |
2278 | ||
428f0c67 JH |
2279 | /* Constant pool entries may be shared. */ |
2280 | if (DECL_IN_CONSTANT_POOL (target->decl)) | |
2281 | return false; | |
2282 | ||
2283 | /* We cannot change alignment of symbols that may bind to symbols | |
2284 | in other translation unit that may contain a definition with lower | |
2285 | alignment. */ | |
2286 | if (!decl_binds_to_current_def_p (decl)) | |
2287 | return false; | |
2288 | ||
2289 | /* When compiling partition, be sure the symbol is not output by other | |
2290 | partition. */ | |
2291 | if (flag_ltrans | |
2292 | && (target->in_other_partition | |
2293 | || target->get_partitioning_class () == SYMBOL_DUPLICATE)) | |
2294 | return false; | |
2295 | ||
2296 | /* Do not override the alignment as specified by the ABI when the used | |
2297 | attribute is set. */ | |
2298 | if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl)) | |
2299 | return false; | |
2300 | ||
2301 | /* Do not override explicit alignment set by the user when an explicit | |
2302 | section name is also used. This is a common idiom used by many | |
2303 | software projects. */ | |
2304 | if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section) | |
2305 | return false; | |
2306 | ||
2307 | return true; | |
2308 | } | |
2309 | ||
2310 | /* Worker for symtab_node::increase_alignment. */ | |
2311 | ||
2312 | static bool | |
2313 | increase_alignment_1 (symtab_node *n, void *v) | |
2314 | { | |
2315 | unsigned int align = (size_t)v; | |
2316 | if (DECL_ALIGN (n->decl) < align | |
2317 | && n->can_increase_alignment_p ()) | |
2318 | { | |
fe37c7af | 2319 | SET_DECL_ALIGN (n->decl, align); |
428f0c67 JH |
2320 | DECL_USER_ALIGN (n->decl) = 1; |
2321 | } | |
2322 | return false; | |
2323 | } | |
2324 | ||
2325 | /* Increase alignment of THIS to ALIGN. */ | |
2326 | ||
2327 | void | |
2328 | symtab_node::increase_alignment (unsigned int align) | |
2329 | { | |
97580980 | 2330 | gcc_assert (can_increase_alignment_p () && align <= MAX_OFILE_ALIGNMENT); |
428f0c67 JH |
2331 | ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1, |
2332 | (void *)(size_t) align, | |
2333 | true); | |
2334 | gcc_assert (DECL_ALIGN (decl) >= align); | |
2335 | } | |
2336 | ||
2337 | /* Helper for symtab_node::definition_alignment. */ | |
2338 | ||
2339 | static bool | |
2340 | get_alignment_1 (symtab_node *n, void *v) | |
2341 | { | |
2342 | *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl)); | |
2343 | return false; | |
2344 | } | |
2345 | ||
2346 | /* Return desired alignment of the definition. This is NOT alignment useful | |
2347 | to access THIS, because THIS may be interposable and DECL_ALIGN should | |
2348 | be used instead. It however must be guaranteed when output definition | |
2349 | of THIS. */ | |
2350 | ||
2351 | unsigned int | |
2352 | symtab_node::definition_alignment () | |
2353 | { | |
2354 | unsigned int align = 0; | |
2355 | gcc_assert (!alias); | |
2356 | call_for_symbol_and_aliases (get_alignment_1, &align, true); | |
2357 | return align; | |
2358 | } | |
f8a1abf8 JH |
2359 | |
2360 | /* Return symbol used to separate symbol name from suffix. */ | |
2361 | ||
2362 | char | |
2363 | symbol_table::symbol_suffix_separator () | |
2364 | { | |
2365 | #ifndef NO_DOT_IN_LABEL | |
2366 | return '.'; | |
2367 | #elif !defined NO_DOLLAR_IN_LABEL | |
2368 | return '$'; | |
2369 | #else | |
2370 | return '_'; | |
2371 | #endif | |
2372 | } | |
f13fe18b JH |
2373 | |
2374 | /* Return true when references to this symbol from REF must bind to current | |
2375 | definition in final executable. */ | |
2376 | ||
2377 | bool | |
2378 | symtab_node::binds_to_current_def_p (symtab_node *ref) | |
2379 | { | |
2380 | if (!definition) | |
2381 | return false; | |
e2940b98 JH |
2382 | if (transparent_alias) |
2383 | return definition | |
2384 | && get_alias_target()->binds_to_current_def_p (ref); | |
aab778d3 L |
2385 | cgraph_node *cnode = dyn_cast <cgraph_node *> (this); |
2386 | if (cnode && cnode->ifunc_resolver) | |
31a31c9d | 2387 | return false; |
f13fe18b JH |
2388 | if (decl_binds_to_current_def_p (decl)) |
2389 | return true; | |
2390 | ||
2391 | /* Inline clones always binds locally. */ | |
a62bfab5 | 2392 | if (cnode && cnode->inlined_to) |
f13fe18b JH |
2393 | return true; |
2394 | ||
2395 | if (DECL_EXTERNAL (decl)) | |
2396 | return false; | |
2397 | ||
f13fe18b JH |
2398 | gcc_assert (externally_visible); |
2399 | ||
2400 | if (ref) | |
2401 | { | |
2402 | cgraph_node *cref = dyn_cast <cgraph_node *> (ref); | |
2403 | if (cref) | |
a62bfab5 | 2404 | ref = cref->inlined_to; |
f13fe18b JH |
2405 | } |
2406 | ||
2407 | /* If this is a reference from symbol itself and there are no aliases, we | |
65c74eb2 | 2408 | may be sure that the symbol was not interposed by something else because |
f13fe18b JH |
2409 | the symbol itself would be unreachable otherwise. This is important |
2410 | to optimize recursive functions well. | |
2411 | ||
2412 | This assumption may be broken by inlining: if symbol is interposable | |
2413 | but the body is available (i.e. declared inline), inliner may make | |
2414 | the body reachable even with interposition. */ | |
2415 | if (this == ref && !has_aliases_p () | |
2416 | && (!cnode | |
2417 | || symtab->state >= IPA_SSA_AFTER_INLINING | |
2418 | || get_availability () >= AVAIL_INTERPOSABLE)) | |
2419 | return true; | |
2420 | ||
2421 | ||
2422 | /* References within one comdat group are always bound in a group. */ | |
2423 | if (ref | |
2424 | && symtab->state >= IPA_SSA_AFTER_INLINING | |
2425 | && get_comdat_group () | |
2426 | && get_comdat_group () == ref->get_comdat_group ()) | |
2427 | return true; | |
2428 | ||
2429 | return false; | |
2430 | } | |
39aa9b23 JH |
2431 | |
2432 | /* Return true if symbol should be output to the symbol table. */ | |
2433 | ||
2434 | bool | |
2435 | symtab_node::output_to_lto_symbol_table_p (void) | |
2436 | { | |
2437 | /* Only externally visible symbols matter. */ | |
2438 | if (!TREE_PUBLIC (decl)) | |
2439 | return false; | |
2440 | if (!real_symbol_p ()) | |
2441 | return false; | |
2442 | /* FIXME: variables probably should not be considered as real symbols at | |
2443 | first place. */ | |
2444 | if (VAR_P (decl) && DECL_HARD_REGISTER (decl)) | |
2445 | return false; | |
5747e0c0 XHL |
2446 | if (TREE_CODE (decl) == FUNCTION_DECL && !definition |
2447 | && fndecl_built_in_p (decl)) | |
2448 | { | |
2449 | /* Builtins like those for most math functions have actual implementations | |
2450 | in libraries so make sure to output references into the symbol table to | |
2451 | make those libraries referenced. Note this is incomplete handling for | |
2452 | now and only covers math functions. */ | |
2453 | if (builtin_with_linkage_p (decl)) | |
2454 | return true; | |
2455 | else | |
2456 | return false; | |
2457 | } | |
39aa9b23 JH |
2458 | |
2459 | /* We have real symbol that should be in symbol table. However try to trim | |
dfea3d6f JJ |
2460 | down the references to libraries bit more because linker will otherwise |
2461 | bring unnecessary object files into the final link. | |
39aa9b23 JH |
2462 | FIXME: The following checks can easily be confused i.e. by self recursive |
2463 | function or self-referring variable. */ | |
2464 | ||
2465 | /* We keep external functions in symtab for sake of inlining | |
2466 | and devirtualization. We do not want to see them in symbol table as | |
2467 | references unless they are really used. */ | |
2468 | cgraph_node *cnode = dyn_cast <cgraph_node *> (this); | |
2469 | if (cnode && (!definition || DECL_EXTERNAL (decl)) | |
2470 | && cnode->callers) | |
2471 | return true; | |
2472 | ||
2473 | /* Ignore all references from external vars initializers - they are not really | |
2474 | part of the compilation unit until they are used by folding. Some symbols, | |
67914693 | 2475 | like references to external construction vtables cannot be referred to at |
39aa9b23 JH |
2476 | all. We decide this at can_refer_decl_in_current_unit_p. */ |
2477 | if (!definition || DECL_EXTERNAL (decl)) | |
2478 | { | |
2479 | int i; | |
2480 | struct ipa_ref *ref; | |
2481 | for (i = 0; iterate_referring (i, ref); i++) | |
2482 | { | |
2483 | if (ref->use == IPA_REF_ALIAS) | |
2484 | continue; | |
2485 | if (is_a <cgraph_node *> (ref->referring)) | |
2486 | return true; | |
2487 | if (!DECL_EXTERNAL (ref->referring->decl)) | |
2488 | return true; | |
2489 | } | |
2490 | return false; | |
2491 | } | |
2492 | return true; | |
2493 | } |