]>
Commit | Line | Data |
---|---|---|
2aae7680 | 1 | /* Symbol table. |
23a5b65a | 2 | Copyright (C) 2012-2014 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" | |
24 | #include "tm.h" | |
d8a2d370 | 25 | #include "rtl.h" |
2aae7680 | 26 | #include "tree.h" |
d8a2d370 DN |
27 | #include "print-tree.h" |
28 | #include "varasm.h" | |
29 | #include "function.h" | |
30 | #include "emit-rtl.h" | |
2fb9a547 AM |
31 | #include "basic-block.h" |
32 | #include "tree-ssa-alias.h" | |
33 | #include "internal-fn.h" | |
34 | #include "gimple-expr.h" | |
35 | #include "is-a.h" | |
8e9055ae | 36 | #include "gimple.h" |
2aae7680 | 37 | #include "tree-inline.h" |
8f940ee6 | 38 | #include "langhooks.h" |
2aae7680 JH |
39 | #include "hashtab.h" |
40 | #include "cgraph.h" | |
1ab24192 | 41 | #include "diagnostic.h" |
474ffc72 | 42 | #include "timevar.h" |
65d630d4 | 43 | #include "lto-streamer.h" |
862d0b35 | 44 | #include "output.h" |
d122681a | 45 | #include "ipa-utils.h" |
72732f3e | 46 | #include "calls.h" |
d122681a ML |
47 | |
48 | static const char *ipa_ref_use_name[] = {"read","write","addr","alias"}; | |
65d630d4 JH |
49 | |
50 | const char * const ld_plugin_symbol_resolution_names[]= | |
51 | { | |
52 | "", | |
53 | "undef", | |
54 | "prevailing_def", | |
55 | "prevailing_def_ironly", | |
56 | "preempted_reg", | |
57 | "preempted_ir", | |
58 | "resolved_ir", | |
59 | "resolved_exec", | |
60 | "resolved_dyn", | |
61 | "prevailing_def_ironly_exp" | |
62 | }; | |
1ab24192 | 63 | |
862d0b35 DN |
64 | /* Hash asmnames ignoring the user specified marks. */ |
65 | ||
3dafb85c ML |
66 | hashval_t |
67 | symbol_table::decl_assembler_name_hash (const_tree asmname) | |
862d0b35 DN |
68 | { |
69 | if (IDENTIFIER_POINTER (asmname)[0] == '*') | |
70 | { | |
71 | const char *decl_str = IDENTIFIER_POINTER (asmname) + 1; | |
72 | size_t ulp_len = strlen (user_label_prefix); | |
73 | ||
74 | if (ulp_len == 0) | |
75 | ; | |
76 | else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0) | |
77 | decl_str += ulp_len; | |
78 | ||
79 | return htab_hash_string (decl_str); | |
80 | } | |
81 | ||
82 | return htab_hash_string (IDENTIFIER_POINTER (asmname)); | |
83 | } | |
84 | ||
85 | ||
1ab24192 JH |
86 | /* Returns a hash code for P. */ |
87 | ||
3dafb85c ML |
88 | hashval_t |
89 | symbol_table::hash_node_by_assembler_name (const void *p) | |
1ab24192 | 90 | { |
5e20cdc9 | 91 | const symtab_node *n = (const symtab_node *) p; |
67348ccc | 92 | return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl)); |
1ab24192 JH |
93 | } |
94 | ||
862d0b35 DN |
95 | /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */ |
96 | ||
3dafb85c ML |
97 | bool |
98 | symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname) | |
862d0b35 DN |
99 | { |
100 | tree decl_asmname = DECL_ASSEMBLER_NAME (decl); | |
101 | const char *decl_str; | |
102 | const char *asmname_str; | |
103 | bool test = false; | |
104 | ||
105 | if (decl_asmname == asmname) | |
106 | return true; | |
107 | ||
108 | decl_str = IDENTIFIER_POINTER (decl_asmname); | |
109 | asmname_str = IDENTIFIER_POINTER (asmname); | |
110 | ||
111 | ||
112 | /* If the target assembler name was set by the user, things are trickier. | |
113 | We have a leading '*' to begin with. After that, it's arguable what | |
114 | is the correct thing to do with -fleading-underscore. Arguably, we've | |
115 | historically been doing the wrong thing in assemble_alias by always | |
116 | printing the leading underscore. Since we're not changing that, make | |
117 | sure user_label_prefix follows the '*' before matching. */ | |
118 | if (decl_str[0] == '*') | |
119 | { | |
120 | size_t ulp_len = strlen (user_label_prefix); | |
121 | ||
122 | decl_str ++; | |
123 | ||
124 | if (ulp_len == 0) | |
125 | test = true; | |
126 | else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0) | |
127 | decl_str += ulp_len, test=true; | |
128 | else | |
129 | decl_str --; | |
130 | } | |
131 | if (asmname_str[0] == '*') | |
132 | { | |
133 | size_t ulp_len = strlen (user_label_prefix); | |
134 | ||
135 | asmname_str ++; | |
136 | ||
137 | if (ulp_len == 0) | |
138 | test = true; | |
139 | else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0) | |
140 | asmname_str += ulp_len, test=true; | |
141 | else | |
142 | asmname_str --; | |
143 | } | |
144 | ||
145 | if (!test) | |
146 | return false; | |
147 | return strcmp (decl_str, asmname_str) == 0; | |
148 | } | |
149 | ||
150 | ||
1ab24192 JH |
151 | /* Returns nonzero if P1 and P2 are equal. */ |
152 | ||
3dafb85c ML |
153 | int |
154 | symbol_table::eq_assembler_name (const void *p1, const void *p2) | |
1ab24192 | 155 | { |
5e20cdc9 | 156 | const symtab_node *n1 = (const symtab_node *) p1; |
1ab24192 | 157 | const_tree name = (const_tree)p2; |
67348ccc | 158 | return (decl_assembler_name_equal (n1->decl, name)); |
1ab24192 JH |
159 | } |
160 | ||
161 | /* Insert NODE to assembler name hash. */ | |
162 | ||
3dafb85c ML |
163 | void |
164 | symbol_table::insert_to_assembler_name_hash (symtab_node *node, | |
165 | bool with_clones) | |
1ab24192 | 166 | { |
7de90a6c | 167 | if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl)) |
b5493fb2 | 168 | return; |
67348ccc DM |
169 | gcc_checking_assert (!node->previous_sharing_asm_name |
170 | && !node->next_sharing_asm_name); | |
1ab24192 JH |
171 | if (assembler_name_hash) |
172 | { | |
173 | void **aslot; | |
3dafb85c | 174 | cgraph_node *cnode; |
67348ccc | 175 | tree decl = node->decl; |
c3167b00 | 176 | |
67348ccc | 177 | tree name = DECL_ASSEMBLER_NAME (node->decl); |
1ab24192 JH |
178 | |
179 | aslot = htab_find_slot_with_hash (assembler_name_hash, name, | |
180 | decl_assembler_name_hash (name), | |
181 | INSERT); | |
182 | gcc_assert (*aslot != node); | |
5e20cdc9 | 183 | node->next_sharing_asm_name = (symtab_node *)*aslot; |
1ab24192 | 184 | if (*aslot != NULL) |
5e20cdc9 | 185 | ((symtab_node *)*aslot)->previous_sharing_asm_name = node; |
1ab24192 | 186 | *aslot = node; |
c3167b00 JH |
187 | |
188 | /* Update also possible inline clones sharing a decl. */ | |
7de90a6c | 189 | cnode = dyn_cast <cgraph_node *> (node); |
c3167b00 JH |
190 | if (cnode && cnode->clones && with_clones) |
191 | for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) | |
67348ccc DM |
192 | if (cnode->decl == decl) |
193 | insert_to_assembler_name_hash (cnode, true); | |
1ab24192 JH |
194 | } |
195 | ||
196 | } | |
197 | ||
198 | /* Remove NODE from assembler name hash. */ | |
199 | ||
3dafb85c ML |
200 | void |
201 | symbol_table::unlink_from_assembler_name_hash (symtab_node *node, | |
202 | bool with_clones) | |
1ab24192 JH |
203 | { |
204 | if (assembler_name_hash) | |
205 | { | |
3dafb85c | 206 | cgraph_node *cnode; |
67348ccc | 207 | tree decl = node->decl; |
c3167b00 | 208 | |
67348ccc DM |
209 | if (node->next_sharing_asm_name) |
210 | node->next_sharing_asm_name->previous_sharing_asm_name | |
211 | = node->previous_sharing_asm_name; | |
212 | if (node->previous_sharing_asm_name) | |
1ab24192 | 213 | { |
67348ccc DM |
214 | node->previous_sharing_asm_name->next_sharing_asm_name |
215 | = node->next_sharing_asm_name; | |
1ab24192 JH |
216 | } |
217 | else | |
218 | { | |
67348ccc | 219 | tree name = DECL_ASSEMBLER_NAME (node->decl); |
1ab24192 JH |
220 | void **slot; |
221 | slot = htab_find_slot_with_hash (assembler_name_hash, name, | |
222 | decl_assembler_name_hash (name), | |
223 | NO_INSERT); | |
224 | gcc_assert (*slot == node); | |
67348ccc | 225 | if (!node->next_sharing_asm_name) |
1ab24192 JH |
226 | htab_clear_slot (assembler_name_hash, slot); |
227 | else | |
67348ccc | 228 | *slot = node->next_sharing_asm_name; |
1ab24192 | 229 | } |
67348ccc DM |
230 | node->next_sharing_asm_name = NULL; |
231 | node->previous_sharing_asm_name = NULL; | |
c3167b00 JH |
232 | |
233 | /* Update also possible inline clones sharing a decl. */ | |
7de90a6c | 234 | cnode = dyn_cast <cgraph_node *> (node); |
c3167b00 JH |
235 | if (cnode && cnode->clones && with_clones) |
236 | for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) | |
67348ccc DM |
237 | if (cnode->decl == decl) |
238 | unlink_from_assembler_name_hash (cnode, true); | |
1ab24192 JH |
239 | } |
240 | } | |
241 | ||
b5493fb2 JH |
242 | /* Arrange node to be first in its entry of assembler_name_hash. */ |
243 | ||
244 | void | |
3dafb85c | 245 | symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node) |
b5493fb2 | 246 | { |
c3167b00 JH |
247 | unlink_from_assembler_name_hash (node, false); |
248 | insert_to_assembler_name_hash (node, false); | |
b5493fb2 JH |
249 | } |
250 | ||
b5493fb2 | 251 | /* Initalize asm name hash unless. */ |
1ab24192 | 252 | |
b5493fb2 | 253 | void |
3dafb85c | 254 | symbol_table::symtab_initialize_asm_name_hash (void) |
1ab24192 | 255 | { |
5e20cdc9 | 256 | symtab_node *node; |
1ab24192 JH |
257 | if (!assembler_name_hash) |
258 | { | |
259 | assembler_name_hash = | |
260 | htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name, | |
261 | NULL); | |
262 | FOR_EACH_SYMBOL (node) | |
c3167b00 | 263 | insert_to_assembler_name_hash (node, false); |
1ab24192 | 264 | } |
b5493fb2 | 265 | } |
1ab24192 | 266 | |
1ab24192 JH |
267 | /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */ |
268 | ||
269 | void | |
3dafb85c | 270 | symbol_table::change_decl_assembler_name (tree decl, tree name) |
1ab24192 | 271 | { |
5e20cdc9 | 272 | symtab_node *node = NULL; |
1ab24192 JH |
273 | |
274 | /* We can have user ASM names on things, like global register variables, that | |
275 | are not in the symbol table. */ | |
276 | if ((TREE_CODE (decl) == VAR_DECL | |
277 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) | |
278 | || TREE_CODE (decl) == FUNCTION_DECL) | |
d52f5295 | 279 | node = symtab_node::get (decl); |
1ab24192 JH |
280 | if (!DECL_ASSEMBLER_NAME_SET_P (decl)) |
281 | { | |
282 | SET_DECL_ASSEMBLER_NAME (decl, name); | |
283 | if (node) | |
c3167b00 | 284 | insert_to_assembler_name_hash (node, true); |
1ab24192 JH |
285 | } |
286 | else | |
287 | { | |
288 | if (name == DECL_ASSEMBLER_NAME (decl)) | |
289 | return; | |
290 | ||
8a41354f JH |
291 | tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) |
292 | ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) | |
293 | : NULL); | |
1ab24192 | 294 | if (node) |
c3167b00 | 295 | unlink_from_assembler_name_hash (node, true); |
1ab24192 JH |
296 | if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) |
297 | && DECL_RTL_SET_P (decl)) | |
298 | warning (0, "%D renamed after being referenced in assembly", decl); | |
299 | ||
300 | SET_DECL_ASSEMBLER_NAME (decl, name); | |
8a41354f JH |
301 | if (alias) |
302 | { | |
303 | IDENTIFIER_TRANSPARENT_ALIAS (name) = 1; | |
1fed15fc | 304 | TREE_CHAIN (name) = alias; |
8a41354f | 305 | } |
1ab24192 | 306 | if (node) |
c3167b00 | 307 | insert_to_assembler_name_hash (node, true); |
1ab24192 JH |
308 | } |
309 | } | |
310 | ||
d52f5295 ML |
311 | /* Return true when RESOLUTION indicate that linker will use |
312 | the symbol from non-LTO object files. */ | |
313 | ||
314 | bool | |
315 | resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution) | |
316 | { | |
317 | return (resolution == LDPR_PREVAILING_DEF | |
318 | || resolution == LDPR_PREEMPTED_REG | |
319 | || resolution == LDPR_RESOLVED_EXEC | |
320 | || resolution == LDPR_RESOLVED_DYN); | |
321 | } | |
322 | ||
323 | /* Hash sections by their names. */ | |
324 | ||
325 | static hashval_t | |
326 | hash_section_hash_entry (const void *p) | |
327 | { | |
328 | const section_hash_entry *n = (const section_hash_entry *) p; | |
329 | return htab_hash_string (n->name); | |
330 | } | |
331 | ||
332 | /* Return true if section P1 name equals to P2. */ | |
333 | ||
334 | static int | |
335 | eq_sections (const void *p1, const void *p2) | |
336 | { | |
337 | const section_hash_entry *n1 = (const section_hash_entry *) p1; | |
338 | const char *name = (const char *)p2; | |
339 | return n1->name == name || !strcmp (n1->name, name); | |
340 | } | |
341 | ||
342 | /* Add node into symbol table. This function is not used directly, but via | |
343 | cgraph/varpool node creation routines. */ | |
344 | ||
345 | void | |
346 | symtab_node::register_symbol (void) | |
347 | { | |
3dafb85c | 348 | symtab->register_symbol (this); |
d52f5295 ML |
349 | |
350 | if (!decl->decl_with_vis.symtab_node) | |
351 | decl->decl_with_vis.symtab_node = this; | |
352 | ||
353 | ref_list.clear (); | |
354 | ||
d52f5295 ML |
355 | /* Be sure to do this last; C++ FE might create new nodes via |
356 | DECL_ASSEMBLER_NAME langhook! */ | |
3dafb85c | 357 | symtab->insert_to_assembler_name_hash (this, false); |
d52f5295 ML |
358 | } |
359 | ||
360 | /* Remove NODE from same comdat group. */ | |
361 | ||
362 | void | |
363 | symtab_node::remove_from_same_comdat_group (void) | |
364 | { | |
365 | if (same_comdat_group) | |
366 | { | |
367 | symtab_node *prev; | |
368 | for (prev = same_comdat_group; | |
369 | prev->same_comdat_group != this; | |
370 | prev = prev->same_comdat_group) | |
371 | ; | |
372 | if (same_comdat_group == prev) | |
373 | prev->same_comdat_group = NULL; | |
374 | else | |
375 | prev->same_comdat_group = same_comdat_group; | |
376 | same_comdat_group = NULL; | |
377 | set_comdat_group (NULL); | |
378 | } | |
379 | } | |
380 | ||
381 | /* Remove node from symbol table. This function is not used directly, but via | |
382 | cgraph/varpool node removal routines. */ | |
383 | ||
384 | void | |
385 | symtab_node::unregister (void) | |
386 | { | |
387 | remove_all_references (); | |
388 | remove_all_referring (); | |
389 | ||
390 | /* Remove reference to section. */ | |
391 | set_section_for_node (NULL); | |
392 | ||
393 | remove_from_same_comdat_group (); | |
394 | ||
3dafb85c | 395 | symtab->unregister (this); |
d52f5295 ML |
396 | |
397 | /* During LTO symtab merging we temporarily corrupt decl to symtab node | |
398 | hash. */ | |
399 | gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p); | |
400 | if (decl->decl_with_vis.symtab_node == this) | |
401 | { | |
402 | symtab_node *replacement_node = NULL; | |
403 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) | |
404 | replacement_node = cnode->find_replacement (); | |
405 | decl->decl_with_vis.symtab_node = replacement_node; | |
406 | } | |
407 | if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl)) | |
3dafb85c | 408 | symtab->unlink_from_assembler_name_hash (this, false); |
d52f5295 | 409 | if (in_init_priority_hash) |
b086d530 | 410 | symtab->init_priority_hash->remove (this); |
d52f5295 ML |
411 | } |
412 | ||
413 | ||
414 | /* Remove symbol from symbol table. */ | |
415 | ||
416 | void | |
417 | symtab_node::remove (void) | |
418 | { | |
419 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) | |
420 | cnode->remove (); | |
421 | else if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) | |
422 | vnode->remove (); | |
423 | } | |
424 | ||
65d630d4 JH |
425 | /* Add NEW_ to the same comdat group that OLD is in. */ |
426 | ||
427 | void | |
d52f5295 | 428 | symtab_node::add_to_same_comdat_group (symtab_node *old_node) |
65d630d4 | 429 | { |
aede2c10 | 430 | gcc_assert (old_node->get_comdat_group ()); |
d52f5295 ML |
431 | gcc_assert (!same_comdat_group); |
432 | gcc_assert (this != old_node); | |
65d630d4 | 433 | |
d52f5295 ML |
434 | set_comdat_group (old_node->get_comdat_group ()); |
435 | same_comdat_group = old_node; | |
67348ccc | 436 | if (!old_node->same_comdat_group) |
d52f5295 | 437 | old_node->same_comdat_group = this; |
65d630d4 JH |
438 | else |
439 | { | |
5e20cdc9 | 440 | symtab_node *n; |
67348ccc DM |
441 | for (n = old_node->same_comdat_group; |
442 | n->same_comdat_group != old_node; | |
443 | n = n->same_comdat_group) | |
65d630d4 | 444 | ; |
d52f5295 | 445 | n->same_comdat_group = this; |
65d630d4 JH |
446 | } |
447 | } | |
448 | ||
449 | /* Dissolve the same_comdat_group list in which NODE resides. */ | |
450 | ||
451 | void | |
d52f5295 | 452 | symtab_node::dissolve_same_comdat_group_list (void) |
65d630d4 | 453 | { |
d52f5295 | 454 | symtab_node *n = this; |
5e20cdc9 | 455 | symtab_node *next; |
65d630d4 | 456 | |
d52f5295 | 457 | if (!same_comdat_group) |
65d630d4 JH |
458 | return; |
459 | do | |
460 | { | |
67348ccc DM |
461 | next = n->same_comdat_group; |
462 | n->same_comdat_group = NULL; | |
aede2c10 | 463 | /* Clear comdat_group for comdat locals, since |
1f26ac87 JM |
464 | make_decl_local doesn't. */ |
465 | if (!TREE_PUBLIC (n->decl)) | |
aede2c10 | 466 | n->set_comdat_group (NULL); |
65d630d4 JH |
467 | n = next; |
468 | } | |
d52f5295 | 469 | while (n != this); |
65d630d4 JH |
470 | } |
471 | ||
8f940ee6 JH |
472 | /* Return printable assembler name of NODE. |
473 | This function is used only for debugging. When assembler name | |
474 | is unknown go with identifier name. */ | |
475 | ||
476 | const char * | |
fec39fa6 | 477 | symtab_node::asm_name () const |
8f940ee6 | 478 | { |
fec39fa6 TS |
479 | if (!DECL_ASSEMBLER_NAME_SET_P (decl)) |
480 | return lang_hooks.decl_printable_name (decl, 2); | |
481 | return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); | |
8f940ee6 JH |
482 | } |
483 | ||
484 | /* Return printable identifier name. */ | |
485 | ||
486 | const char * | |
fec39fa6 | 487 | symtab_node::name () const |
8f940ee6 | 488 | { |
fec39fa6 | 489 | return lang_hooks.decl_printable_name (decl, 2); |
8f940ee6 JH |
490 | } |
491 | ||
d122681a ML |
492 | /* Return ipa reference from this symtab_node to |
493 | REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type | |
494 | of the use. */ | |
495 | ||
3dafb85c ML |
496 | ipa_ref * |
497 | symtab_node::create_reference (symtab_node *referred_node, | |
498 | enum ipa_ref_use use_type) | |
d122681a | 499 | { |
3dafb85c | 500 | return create_reference (referred_node, use_type, NULL); |
d122681a ML |
501 | } |
502 | ||
503 | ||
504 | /* Return ipa reference from this symtab_node to | |
505 | REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type | |
506 | of the use and STMT the statement (if it exists). */ | |
507 | ||
3dafb85c ML |
508 | ipa_ref * |
509 | symtab_node::create_reference (symtab_node *referred_node, | |
510 | enum ipa_ref_use use_type, gimple stmt) | |
d122681a | 511 | { |
3dafb85c ML |
512 | ipa_ref *ref = NULL, *ref2 = NULL; |
513 | ipa_ref_list *list, *list2; | |
d122681a ML |
514 | ipa_ref_t *old_references; |
515 | ||
516 | gcc_checking_assert (!stmt || is_a <cgraph_node *> (this)); | |
517 | gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt); | |
518 | ||
519 | list = &ref_list; | |
520 | old_references = vec_safe_address (list->references); | |
521 | vec_safe_grow (list->references, vec_safe_length (list->references) + 1); | |
522 | ref = &list->references->last (); | |
523 | ||
524 | list2 = &referred_node->ref_list; | |
e55637b7 ML |
525 | |
526 | /* IPA_REF_ALIAS is always inserted at the beginning of the list. */ | |
527 | if(use_type == IPA_REF_ALIAS) | |
528 | { | |
529 | list2->referring.safe_insert (0, ref); | |
530 | ref->referred_index = 0; | |
531 | ||
532 | for (unsigned int i = 1; i < list2->referring.length (); i++) | |
533 | list2->referring[i]->referred_index = i; | |
534 | } | |
535 | else | |
536 | { | |
537 | list2->referring.safe_push (ref); | |
538 | ref->referred_index = list2->referring.length () - 1; | |
539 | } | |
540 | ||
d122681a ML |
541 | ref->referring = this; |
542 | ref->referred = referred_node; | |
543 | ref->stmt = stmt; | |
544 | ref->lto_stmt_uid = 0; | |
545 | ref->use = use_type; | |
546 | ref->speculative = 0; | |
547 | ||
548 | /* If vector was moved in memory, update pointers. */ | |
549 | if (old_references != list->references->address ()) | |
550 | { | |
551 | int i; | |
552 | for (i = 0; iterate_reference(i, ref2); i++) | |
553 | ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2; | |
554 | } | |
555 | return ref; | |
556 | } | |
557 | ||
558 | /* If VAL is a reference to a function or a variable, add a reference from | |
559 | this symtab_node to the corresponding symbol table node. USE_TYPE specify | |
560 | type of the use and STMT the statement (if it exists). Return the new | |
561 | reference or NULL if none was created. */ | |
562 | ||
3dafb85c ML |
563 | ipa_ref * |
564 | symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type, | |
565 | gimple stmt) | |
d122681a ML |
566 | { |
567 | STRIP_NOPS (val); | |
568 | if (TREE_CODE (val) != ADDR_EXPR) | |
569 | return NULL; | |
570 | val = get_base_var (val); | |
571 | if (val && (TREE_CODE (val) == FUNCTION_DECL | |
572 | || TREE_CODE (val) == VAR_DECL)) | |
573 | { | |
d52f5295 | 574 | symtab_node *referred = symtab_node::get (val); |
d122681a | 575 | gcc_checking_assert (referred); |
3dafb85c | 576 | return create_reference (referred, use_type, stmt); |
d122681a ML |
577 | } |
578 | return NULL; | |
579 | } | |
580 | ||
581 | /* Clone all references from symtab NODE to this symtab_node. */ | |
582 | ||
583 | void | |
3dafb85c | 584 | symtab_node::clone_references (symtab_node *node) |
d122681a | 585 | { |
3dafb85c | 586 | ipa_ref *ref = NULL, *ref2 = NULL; |
d122681a ML |
587 | int i; |
588 | for (i = 0; node->iterate_reference (i, ref); i++) | |
589 | { | |
590 | bool speculative = ref->speculative; | |
591 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
592 | ||
3dafb85c | 593 | ref2 = create_reference (ref->referred, ref->use, ref->stmt); |
d122681a ML |
594 | ref2->speculative = speculative; |
595 | ref2->lto_stmt_uid = stmt_uid; | |
596 | } | |
597 | } | |
598 | ||
599 | /* Clone all referring from symtab NODE to this symtab_node. */ | |
600 | ||
601 | void | |
3dafb85c | 602 | symtab_node::clone_referring (symtab_node *node) |
d122681a | 603 | { |
3dafb85c | 604 | ipa_ref *ref = NULL, *ref2 = NULL; |
d122681a ML |
605 | int i; |
606 | for (i = 0; node->iterate_referring(i, ref); i++) | |
607 | { | |
608 | bool speculative = ref->speculative; | |
609 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
610 | ||
3dafb85c | 611 | ref2 = ref->referring->create_reference (this, ref->use, ref->stmt); |
d122681a ML |
612 | ref2->speculative = speculative; |
613 | ref2->lto_stmt_uid = stmt_uid; | |
614 | } | |
615 | } | |
616 | ||
617 | /* Clone reference REF to this symtab_node and set its stmt to STMT. */ | |
618 | ||
3dafb85c ML |
619 | ipa_ref * |
620 | symtab_node::clone_reference (ipa_ref *ref, gimple stmt) | |
d122681a ML |
621 | { |
622 | bool speculative = ref->speculative; | |
623 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
3dafb85c | 624 | ipa_ref *ref2; |
d122681a | 625 | |
3dafb85c | 626 | ref2 = create_reference (ref->referred, ref->use, stmt); |
d122681a ML |
627 | ref2->speculative = speculative; |
628 | ref2->lto_stmt_uid = stmt_uid; | |
629 | return ref2; | |
630 | } | |
631 | ||
632 | /* Find the structure describing a reference to REFERRED_NODE | |
633 | and associated with statement STMT. */ | |
634 | ||
3dafb85c | 635 | ipa_ref * |
d122681a ML |
636 | symtab_node::find_reference (symtab_node *referred_node, |
637 | gimple stmt, unsigned int lto_stmt_uid) | |
638 | { | |
3dafb85c | 639 | ipa_ref *r = NULL; |
d122681a ML |
640 | int i; |
641 | ||
642 | for (i = 0; iterate_reference (i, r); i++) | |
643 | if (r->referred == referred_node | |
644 | && !r->speculative | |
645 | && ((stmt && r->stmt == stmt) | |
646 | || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid) | |
647 | || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid))) | |
648 | return r; | |
649 | return NULL; | |
650 | } | |
651 | ||
652 | /* Remove all references that are associated with statement STMT. */ | |
653 | ||
654 | void | |
655 | symtab_node::remove_stmt_references (gimple stmt) | |
656 | { | |
3dafb85c | 657 | ipa_ref *r = NULL; |
d122681a ML |
658 | int i = 0; |
659 | ||
660 | while (iterate_reference (i, r)) | |
661 | if (r->stmt == stmt) | |
662 | r->remove_reference (); | |
663 | else | |
664 | i++; | |
665 | } | |
666 | ||
667 | /* Remove all stmt references in non-speculative references. | |
668 | Those are not maintained during inlining & clonning. | |
669 | The exception are speculative references that are updated along | |
670 | with callgraph edges associated with them. */ | |
671 | ||
672 | void | |
673 | symtab_node::clear_stmts_in_references (void) | |
674 | { | |
3dafb85c | 675 | ipa_ref *r = NULL; |
d122681a ML |
676 | int i; |
677 | ||
678 | for (i = 0; iterate_reference (i, r); i++) | |
679 | if (!r->speculative) | |
680 | { | |
681 | r->stmt = NULL; | |
682 | r->lto_stmt_uid = 0; | |
683 | } | |
684 | } | |
685 | ||
686 | /* Remove all references in ref list. */ | |
687 | ||
688 | void | |
689 | symtab_node::remove_all_references (void) | |
690 | { | |
691 | while (vec_safe_length (ref_list.references)) | |
692 | ref_list.references->last ().remove_reference (); | |
693 | vec_free (ref_list.references); | |
694 | } | |
695 | ||
696 | /* Remove all referring items in ref list. */ | |
697 | ||
698 | void | |
699 | symtab_node::remove_all_referring (void) | |
700 | { | |
701 | while (ref_list.referring.length ()) | |
702 | ref_list.referring.last ()->remove_reference (); | |
703 | ref_list.referring.release (); | |
704 | } | |
705 | ||
706 | /* Dump references in ref list to FILE. */ | |
707 | ||
708 | void | |
709 | symtab_node::dump_references (FILE *file) | |
710 | { | |
3dafb85c | 711 | ipa_ref *ref = NULL; |
d122681a ML |
712 | int i; |
713 | for (i = 0; iterate_reference (i, ref); i++) | |
714 | { | |
715 | fprintf (file, "%s/%i (%s)", | |
716 | ref->referred->asm_name (), | |
717 | ref->referred->order, | |
718 | ipa_ref_use_name [ref->use]); | |
719 | if (ref->speculative) | |
720 | fprintf (file, " (speculative)"); | |
721 | } | |
722 | fprintf (file, "\n"); | |
723 | } | |
724 | ||
725 | /* Dump referring in list to FILE. */ | |
726 | ||
727 | void | |
728 | symtab_node::dump_referring (FILE *file) | |
729 | { | |
3dafb85c | 730 | ipa_ref *ref = NULL; |
d122681a ML |
731 | int i; |
732 | for (i = 0; iterate_referring(i, ref); i++) | |
733 | { | |
734 | fprintf (file, "%s/%i (%s)", | |
735 | ref->referring->asm_name (), | |
736 | ref->referring->order, | |
737 | ipa_ref_use_name [ref->use]); | |
738 | if (ref->speculative) | |
739 | fprintf (file, " (speculative)"); | |
740 | } | |
741 | fprintf (file, "\n"); | |
742 | } | |
743 | ||
744 | /* Return true if list contains an alias. */ | |
745 | bool | |
746 | symtab_node::has_aliases_p (void) | |
747 | { | |
3dafb85c | 748 | ipa_ref *ref = NULL; |
d122681a ML |
749 | int i; |
750 | ||
751 | for (i = 0; iterate_referring (i, ref); i++) | |
752 | if (ref->use == IPA_REF_ALIAS) | |
753 | return true; | |
754 | return false; | |
755 | } | |
756 | ||
757 | /* Iterates I-th reference in the list, REF is also set. */ | |
758 | ||
3dafb85c ML |
759 | ipa_ref * |
760 | symtab_node::iterate_reference (unsigned i, ipa_ref *&ref) | |
d122681a ML |
761 | { |
762 | vec_safe_iterate (ref_list.references, i, &ref); | |
763 | ||
764 | return ref; | |
765 | } | |
766 | ||
767 | /* Iterates I-th referring item in the list, REF is also set. */ | |
768 | ||
3dafb85c ML |
769 | ipa_ref * |
770 | symtab_node::iterate_referring (unsigned i, ipa_ref *&ref) | |
d122681a ML |
771 | { |
772 | ref_list.referring.iterate (i, &ref); | |
773 | ||
774 | return ref; | |
775 | } | |
776 | ||
e55637b7 ML |
777 | /* Iterates I-th referring alias item in the list, REF is also set. */ |
778 | ||
3dafb85c ML |
779 | ipa_ref * |
780 | symtab_node::iterate_direct_aliases (unsigned i, ipa_ref *&ref) | |
e55637b7 ML |
781 | { |
782 | ref_list.referring.iterate (i, &ref); | |
783 | ||
784 | if (ref && ref->use != IPA_REF_ALIAS) | |
785 | return NULL; | |
786 | ||
787 | return ref; | |
788 | } | |
789 | ||
8f940ee6 JH |
790 | static const char * const symtab_type_names[] = {"symbol", "function", "variable"}; |
791 | ||
d52f5295 | 792 | /* Dump base fields of symtab nodes to F. Not to be used directly. */ |
8f940ee6 JH |
793 | |
794 | void | |
d52f5295 | 795 | symtab_node::dump_base (FILE *f) |
8f940ee6 JH |
796 | { |
797 | static const char * const visibility_types[] = { | |
798 | "default", "protected", "hidden", "internal" | |
799 | }; | |
800 | ||
d52f5295 ML |
801 | fprintf (f, "%s/%i (%s)", asm_name (), order, name ()); |
802 | dump_addr (f, " @", (void *)this); | |
803 | fprintf (f, "\n Type: %s", symtab_type_names[type]); | |
e70670cf | 804 | |
d52f5295 | 805 | if (definition) |
e70670cf | 806 | fprintf (f, " definition"); |
d52f5295 | 807 | if (analyzed) |
e70670cf | 808 | fprintf (f, " analyzed"); |
d52f5295 | 809 | if (alias) |
e70670cf | 810 | fprintf (f, " alias"); |
d52f5295 | 811 | if (weakref) |
08346abd | 812 | fprintf (f, " weakref"); |
d52f5295 | 813 | if (cpp_implicit_alias) |
40a7fe1e | 814 | fprintf (f, " cpp_implicit_alias"); |
d52f5295 | 815 | if (alias_target) |
40a7fe1e | 816 | fprintf (f, " target:%s", |
d52f5295 | 817 | DECL_P (alias_target) |
40a7fe1e | 818 | ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME |
d52f5295 ML |
819 | (alias_target)) |
820 | : IDENTIFIER_POINTER (alias_target)); | |
821 | if (body_removed) | |
3d8d0043 | 822 | fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes"); |
e70670cf | 823 | fprintf (f, "\n Visibility:"); |
d52f5295 | 824 | if (in_other_partition) |
8f940ee6 | 825 | fprintf (f, " in_other_partition"); |
d52f5295 | 826 | if (used_from_other_partition) |
8f940ee6 | 827 | fprintf (f, " used_from_other_partition"); |
d52f5295 | 828 | if (force_output) |
ead84f73 | 829 | fprintf (f, " force_output"); |
d52f5295 | 830 | if (forced_by_abi) |
edb983b2 | 831 | fprintf (f, " forced_by_abi"); |
d52f5295 | 832 | if (externally_visible) |
e5b962d0 | 833 | fprintf (f, " externally_visible"); |
d52f5295 | 834 | if (resolution != LDPR_UNKNOWN) |
8f940ee6 | 835 | fprintf (f, " %s", |
d52f5295 ML |
836 | ld_plugin_symbol_resolution_names[(int)resolution]); |
837 | if (TREE_ASM_WRITTEN (decl)) | |
8f940ee6 | 838 | fprintf (f, " asm_written"); |
d52f5295 | 839 | if (DECL_EXTERNAL (decl)) |
8f940ee6 | 840 | fprintf (f, " external"); |
d52f5295 | 841 | if (TREE_PUBLIC (decl)) |
8f940ee6 | 842 | fprintf (f, " public"); |
d52f5295 | 843 | if (DECL_COMMON (decl)) |
8f940ee6 | 844 | fprintf (f, " common"); |
d52f5295 | 845 | if (DECL_WEAK (decl)) |
8f940ee6 | 846 | fprintf (f, " weak"); |
d52f5295 | 847 | if (DECL_DLLIMPORT_P (decl)) |
8f940ee6 | 848 | fprintf (f, " dll_import"); |
d52f5295 | 849 | if (DECL_COMDAT (decl)) |
8f940ee6 | 850 | fprintf (f, " comdat"); |
d52f5295 | 851 | if (get_comdat_group ()) |
8f940ee6 | 852 | fprintf (f, " comdat_group:%s", |
d52f5295 ML |
853 | IDENTIFIER_POINTER (get_comdat_group_id ())); |
854 | if (DECL_ONE_ONLY (decl)) | |
8f940ee6 | 855 | fprintf (f, " one_only"); |
d52f5295 | 856 | if (get_section ()) |
24d047a3 | 857 | fprintf (f, " section:%s", |
d52f5295 ML |
858 | get_section ()); |
859 | if (implicit_section) | |
e257a17c | 860 | fprintf (f," (implicit_section)"); |
d52f5295 | 861 | if (DECL_VISIBILITY_SPECIFIED (decl)) |
8f940ee6 | 862 | fprintf (f, " visibility_specified"); |
d52f5295 | 863 | if (DECL_VISIBILITY (decl)) |
8f940ee6 | 864 | fprintf (f, " visibility:%s", |
d52f5295 ML |
865 | visibility_types [DECL_VISIBILITY (decl)]); |
866 | if (DECL_VIRTUAL_P (decl)) | |
8f940ee6 | 867 | fprintf (f, " virtual"); |
d52f5295 | 868 | if (DECL_ARTIFICIAL (decl)) |
8f940ee6 | 869 | fprintf (f, " artificial"); |
d52f5295 | 870 | if (TREE_CODE (decl) == FUNCTION_DECL) |
838ff415 | 871 | { |
d52f5295 | 872 | if (DECL_STATIC_CONSTRUCTOR (decl)) |
838ff415 | 873 | fprintf (f, " constructor"); |
d52f5295 | 874 | if (DECL_STATIC_DESTRUCTOR (decl)) |
838ff415 JH |
875 | fprintf (f, " destructor"); |
876 | } | |
8f940ee6 JH |
877 | fprintf (f, "\n"); |
878 | ||
d52f5295 | 879 | if (same_comdat_group) |
8f940ee6 | 880 | fprintf (f, " Same comdat group as: %s/%i\n", |
d52f5295 ML |
881 | same_comdat_group->asm_name (), |
882 | same_comdat_group->order); | |
883 | if (next_sharing_asm_name) | |
8f940ee6 | 884 | fprintf (f, " next sharing asm name: %i\n", |
d52f5295 ML |
885 | next_sharing_asm_name->order); |
886 | if (previous_sharing_asm_name) | |
8f940ee6 | 887 | fprintf (f, " previous sharing asm name: %i\n", |
d52f5295 | 888 | previous_sharing_asm_name->order); |
8f940ee6 | 889 | |
d52f5295 | 890 | if (address_taken) |
fe0bd630 | 891 | fprintf (f, " Address is taken.\n"); |
d52f5295 | 892 | if (aux) |
66058468 JH |
893 | { |
894 | fprintf (f, " Aux:"); | |
d52f5295 | 895 | dump_addr (f, " @", (void *)aux); |
66058468 | 896 | } |
8f940ee6 JH |
897 | |
898 | fprintf (f, " References: "); | |
d52f5295 | 899 | dump_references (f); |
5932a4d4 | 900 | fprintf (f, " Referring: "); |
d52f5295 ML |
901 | dump_referring (f); |
902 | if (lto_file_data) | |
b5493fb2 | 903 | fprintf (f, " Read from file: %s\n", |
d52f5295 | 904 | lto_file_data->file_name); |
8f940ee6 JH |
905 | } |
906 | ||
d52f5295 | 907 | /* Dump symtab node to F. */ |
8f940ee6 JH |
908 | |
909 | void | |
d52f5295 | 910 | symtab_node::dump (FILE *f) |
8f940ee6 | 911 | { |
d52f5295 ML |
912 | if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this)) |
913 | cnode->dump (f); | |
914 | else if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) | |
915 | vnode->dump (f); | |
8f940ee6 JH |
916 | } |
917 | ||
d52f5295 | 918 | /* Dump symbol table to F. */ |
8f940ee6 JH |
919 | |
920 | void | |
d52f5295 | 921 | symtab_node::dump_table (FILE *f) |
8f940ee6 | 922 | { |
5e20cdc9 | 923 | symtab_node *node; |
8f940ee6 JH |
924 | fprintf (f, "Symbol table:\n\n"); |
925 | FOR_EACH_SYMBOL (node) | |
d52f5295 | 926 | node->dump (f); |
8f940ee6 JH |
927 | } |
928 | ||
3dafb85c ML |
929 | |
930 | /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME. | |
931 | Return NULL if there's no such node. */ | |
932 | ||
933 | symtab_node * | |
934 | symtab_node::get_for_asmname (const_tree asmname) | |
935 | { | |
936 | symtab_node *node; | |
937 | void **slot; | |
938 | ||
939 | symtab->symtab_initialize_asm_name_hash (); | |
940 | slot = htab_find_slot_with_hash (symtab->assembler_name_hash, asmname, | |
941 | symtab->decl_assembler_name_hash (asmname), | |
942 | NO_INSERT); | |
943 | ||
944 | if (slot) | |
945 | { | |
946 | node = (symtab_node *) *slot; | |
947 | return node; | |
948 | } | |
949 | return NULL; | |
950 | } | |
951 | ||
8f940ee6 JH |
952 | /* Dump symtab node NODE to stderr. */ |
953 | ||
954 | DEBUG_FUNCTION void | |
d52f5295 | 955 | symtab_node::debug (void) |
8f940ee6 | 956 | { |
d52f5295 | 957 | dump (stderr); |
8f940ee6 JH |
958 | } |
959 | ||
474ffc72 JH |
960 | /* Verify common part of symtab nodes. */ |
961 | ||
962 | DEBUG_FUNCTION bool | |
d52f5295 | 963 | symtab_node::verify_base (void) |
474ffc72 JH |
964 | { |
965 | bool error_found = false; | |
5e20cdc9 | 966 | symtab_node *hashed_node; |
474ffc72 | 967 | |
d52f5295 | 968 | if (is_a <cgraph_node *> (this)) |
474ffc72 | 969 | { |
d52f5295 | 970 | if (TREE_CODE (decl) != FUNCTION_DECL) |
474ffc72 JH |
971 | { |
972 | error ("function symbol is not function"); | |
973 | error_found = true; | |
974 | } | |
975 | } | |
d52f5295 | 976 | else if (is_a <varpool_node *> (this)) |
474ffc72 | 977 | { |
d52f5295 | 978 | if (TREE_CODE (decl) != VAR_DECL) |
474ffc72 JH |
979 | { |
980 | error ("variable symbol is not variable"); | |
981 | error_found = true; | |
982 | } | |
983 | } | |
984 | else | |
985 | { | |
986 | error ("node has unknown type"); | |
987 | error_found = true; | |
988 | } | |
989 | ||
3dafb85c | 990 | if (symtab->state != LTO_STREAMING) |
474ffc72 | 991 | { |
d52f5295 | 992 | hashed_node = symtab_node::get (decl); |
ca0f62a8 JH |
993 | if (!hashed_node) |
994 | { | |
aede2c10 | 995 | error ("node not found node->decl->decl_with_vis.symtab_node"); |
ca0f62a8 JH |
996 | error_found = true; |
997 | } | |
d52f5295 ML |
998 | if (hashed_node != this |
999 | && (!is_a <cgraph_node *> (this) | |
1000 | || !dyn_cast <cgraph_node *> (this)->clone_of | |
1001 | || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl)) | |
e5b962d0 | 1002 | { |
aede2c10 | 1003 | error ("node differs from node->decl->decl_with_vis.symtab_node"); |
e5b962d0 JH |
1004 | error_found = true; |
1005 | } | |
474ffc72 | 1006 | } |
3dafb85c | 1007 | if (symtab->assembler_name_hash) |
474ffc72 | 1008 | { |
3dafb85c | 1009 | hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl)); |
67348ccc | 1010 | if (hashed_node && hashed_node->previous_sharing_asm_name) |
474ffc72 JH |
1011 | { |
1012 | error ("assembler name hash list corrupted"); | |
1013 | error_found = true; | |
1014 | } | |
1015 | while (hashed_node) | |
1016 | { | |
d52f5295 | 1017 | if (hashed_node == this) |
474ffc72 | 1018 | break; |
67348ccc | 1019 | hashed_node = hashed_node->next_sharing_asm_name; |
474ffc72 | 1020 | } |
b5493fb2 | 1021 | if (!hashed_node |
d52f5295 ML |
1022 | && !(is_a <varpool_node *> (this) |
1023 | || DECL_HARD_REGISTER (decl))) | |
474ffc72 JH |
1024 | { |
1025 | error ("node not found in symtab assembler name hash"); | |
1026 | error_found = true; | |
1027 | } | |
1028 | } | |
d52f5295 ML |
1029 | if (previous_sharing_asm_name |
1030 | && previous_sharing_asm_name->next_sharing_asm_name != this) | |
474ffc72 JH |
1031 | { |
1032 | error ("double linked list of assembler names corrupted"); | |
e70670cf JH |
1033 | error_found = true; |
1034 | } | |
d52f5295 | 1035 | if (analyzed && !definition) |
e70670cf JH |
1036 | { |
1037 | error ("node is analyzed byt it is not a definition"); | |
1038 | error_found = true; | |
474ffc72 | 1039 | } |
d52f5295 | 1040 | if (cpp_implicit_alias && !alias) |
40a7fe1e JH |
1041 | { |
1042 | error ("node is alias but not implicit alias"); | |
1043 | error_found = true; | |
1044 | } | |
d52f5295 | 1045 | if (alias && !definition && !weakref) |
40a7fe1e JH |
1046 | { |
1047 | error ("node is alias but not definition"); | |
1048 | error_found = true; | |
1049 | } | |
d52f5295 | 1050 | if (weakref && !alias) |
08346abd JH |
1051 | { |
1052 | error ("node is weakref but not an alias"); | |
1053 | error_found = true; | |
1054 | } | |
d52f5295 | 1055 | if (same_comdat_group) |
474ffc72 | 1056 | { |
d52f5295 | 1057 | symtab_node *n = same_comdat_group; |
474ffc72 | 1058 | |
aede2c10 | 1059 | if (!n->get_comdat_group ()) |
474ffc72 | 1060 | { |
aede2c10 | 1061 | error ("node is in same_comdat_group list but has no comdat_group"); |
474ffc72 JH |
1062 | error_found = true; |
1063 | } | |
d52f5295 | 1064 | if (n->get_comdat_group () != get_comdat_group ()) |
7b3376a0 JH |
1065 | { |
1066 | error ("same_comdat_group list across different groups"); | |
1067 | error_found = true; | |
1068 | } | |
1069 | if (!n->definition) | |
1070 | { | |
1071 | error ("Node has same_comdat_group but it is not a definition"); | |
1072 | error_found = true; | |
1073 | } | |
d52f5295 | 1074 | if (n->type != type) |
474ffc72 JH |
1075 | { |
1076 | error ("mixing different types of symbol in same comdat groups is not supported"); | |
1077 | error_found = true; | |
1078 | } | |
d52f5295 | 1079 | if (n == this) |
474ffc72 JH |
1080 | { |
1081 | error ("node is alone in a comdat group"); | |
1082 | error_found = true; | |
1083 | } | |
1084 | do | |
1085 | { | |
67348ccc | 1086 | if (!n->same_comdat_group) |
474ffc72 JH |
1087 | { |
1088 | error ("same_comdat_group is not a circular list"); | |
1089 | error_found = true; | |
1090 | break; | |
1091 | } | |
67348ccc | 1092 | n = n->same_comdat_group; |
474ffc72 | 1093 | } |
d52f5295 ML |
1094 | while (n != this); |
1095 | if (comdat_local_p ()) | |
1f26ac87 | 1096 | { |
3dafb85c | 1097 | ipa_ref *ref = NULL; |
e257a17c | 1098 | |
d52f5295 | 1099 | for (int i = 0; iterate_referring (i, ref); ++i) |
1f26ac87 | 1100 | { |
d52f5295 | 1101 | if (!in_same_comdat_group_p (ref->referring)) |
1f26ac87 JM |
1102 | { |
1103 | error ("comdat-local symbol referred to by %s outside its " | |
1104 | "comdat", | |
1105 | identifier_to_locale (ref->referring->name())); | |
1106 | error_found = true; | |
1107 | } | |
1108 | } | |
1109 | } | |
474ffc72 | 1110 | } |
d52f5295 | 1111 | if (implicit_section && !get_section ()) |
e257a17c JH |
1112 | { |
1113 | error ("implicit_section flag is set but section isn't"); | |
1114 | error_found = true; | |
1115 | } | |
d52f5295 ML |
1116 | if (get_section () && get_comdat_group () |
1117 | && !implicit_section) | |
e257a17c JH |
1118 | { |
1119 | error ("Both section and comdat group is set"); | |
1120 | error_found = true; | |
1121 | } | |
1122 | /* TODO: Add string table for sections, so we do not keep holding duplicated | |
1123 | strings. */ | |
d52f5295 ML |
1124 | if (alias && definition |
1125 | && get_section () != get_alias_target ()->get_section () | |
1126 | && (!get_section() | |
1127 | || !get_alias_target ()->get_section () | |
1128 | || strcmp (get_section(), | |
1129 | get_alias_target ()->get_section ()))) | |
e257a17c JH |
1130 | { |
1131 | error ("Alias and target's section differs"); | |
d52f5295 | 1132 | get_alias_target ()->dump (stderr); |
e257a17c JH |
1133 | error_found = true; |
1134 | } | |
d52f5295 ML |
1135 | if (alias && definition |
1136 | && get_comdat_group () != get_alias_target ()->get_comdat_group ()) | |
e257a17c JH |
1137 | { |
1138 | error ("Alias and target's comdat groups differs"); | |
d52f5295 | 1139 | get_alias_target ()->dump (stderr); |
e257a17c JH |
1140 | error_found = true; |
1141 | } | |
1142 | ||
474ffc72 JH |
1143 | return error_found; |
1144 | } | |
1145 | ||
1146 | /* Verify consistency of NODE. */ | |
1147 | ||
1148 | DEBUG_FUNCTION void | |
d52f5295 | 1149 | symtab_node::verify (void) |
474ffc72 JH |
1150 | { |
1151 | if (seen_error ()) | |
1152 | return; | |
1153 | ||
1154 | timevar_push (TV_CGRAPH_VERIFY); | |
d52f5295 ML |
1155 | if (cgraph_node *node = dyn_cast <cgraph_node *> (this)) |
1156 | node->verify_node (); | |
474ffc72 | 1157 | else |
d52f5295 | 1158 | if (verify_base ()) |
474ffc72 | 1159 | { |
d52f5295 ML |
1160 | debug (); |
1161 | internal_error ("symtab_node::verify failed"); | |
474ffc72 JH |
1162 | } |
1163 | timevar_pop (TV_CGRAPH_VERIFY); | |
1164 | } | |
1165 | ||
1166 | /* Verify symbol table for internal consistency. */ | |
1167 | ||
1168 | DEBUG_FUNCTION void | |
d52f5295 | 1169 | symtab_node::verify_symtab_nodes (void) |
474ffc72 | 1170 | { |
5e20cdc9 | 1171 | symtab_node *node; |
1eb68d2d | 1172 | hash_map<tree, symtab_node *> comdat_head_map (251); |
e257a17c | 1173 | |
474ffc72 | 1174 | FOR_EACH_SYMBOL (node) |
e257a17c | 1175 | { |
d52f5295 | 1176 | node->verify (); |
e257a17c JH |
1177 | if (node->get_comdat_group ()) |
1178 | { | |
1179 | symtab_node **entry, *s; | |
1180 | bool existed; | |
1181 | ||
1eb68d2d TS |
1182 | entry = &comdat_head_map.get_or_insert (node->get_comdat_group (), |
1183 | &existed); | |
e257a17c JH |
1184 | if (!existed) |
1185 | *entry = node; | |
1186 | else | |
1187 | for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group) | |
1188 | if (!s || s == *entry) | |
1189 | { | |
1190 | error ("Two symbols with same comdat_group are not linked by the same_comdat_group list."); | |
d52f5295 ML |
1191 | (*entry)->debug (); |
1192 | node->debug (); | |
1193 | internal_error ("symtab_node::verify failed"); | |
e257a17c JH |
1194 | } |
1195 | } | |
1196 | } | |
474ffc72 JH |
1197 | } |
1198 | ||
d52f5295 ML |
1199 | /* Return true when NODE is known to be used from other (non-LTO) |
1200 | object file. Known only when doing LTO via linker plugin. */ | |
65d630d4 JH |
1201 | |
1202 | bool | |
d52f5295 | 1203 | symtab_node::used_from_object_file_p_worker (symtab_node *node) |
65d630d4 | 1204 | { |
67348ccc | 1205 | if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl)) |
65d630d4 | 1206 | return false; |
67348ccc | 1207 | if (resolution_used_from_other_file_p (node->resolution)) |
65d630d4 JH |
1208 | return true; |
1209 | return false; | |
1210 | } | |
1211 | ||
d52f5295 ML |
1212 | |
1213 | /* Return true when symtab_node is known to be used from other (non-LTO) | |
1214 | object file. Known only when doing LTO via linker plugin. */ | |
1215 | ||
1216 | bool | |
1217 | symtab_node::used_from_object_file_p (void) | |
1218 | { | |
1219 | return symtab_node::used_from_object_file_p_worker (this); | |
1220 | } | |
1221 | ||
65d630d4 JH |
1222 | /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early, |
1223 | but other code such as notice_global_symbol generates rtl. */ | |
40a7fe1e | 1224 | |
65d630d4 | 1225 | void |
d52f5295 | 1226 | symtab_node::make_decl_local (void) |
65d630d4 JH |
1227 | { |
1228 | rtx rtl, symbol; | |
1229 | ||
aede2c10 | 1230 | /* Avoid clearing comdat_groups on comdat-local decls. */ |
1f26ac87 JM |
1231 | if (TREE_PUBLIC (decl) == 0) |
1232 | return; | |
1233 | ||
65d630d4 JH |
1234 | if (TREE_CODE (decl) == VAR_DECL) |
1235 | DECL_COMMON (decl) = 0; | |
1236 | else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); | |
1237 | ||
24d047a3 | 1238 | DECL_COMDAT (decl) = 0; |
65d630d4 JH |
1239 | DECL_WEAK (decl) = 0; |
1240 | DECL_EXTERNAL (decl) = 0; | |
b5493fb2 JH |
1241 | DECL_VISIBILITY_SPECIFIED (decl) = 0; |
1242 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; | |
65d630d4 JH |
1243 | TREE_PUBLIC (decl) = 0; |
1244 | if (!DECL_RTL_SET_P (decl)) | |
1245 | return; | |
1246 | ||
1247 | /* Update rtl flags. */ | |
1248 | make_decl_rtl (decl); | |
1249 | ||
1250 | rtl = DECL_RTL (decl); | |
1251 | if (!MEM_P (rtl)) | |
1252 | return; | |
1253 | ||
1254 | symbol = XEXP (rtl, 0); | |
1255 | if (GET_CODE (symbol) != SYMBOL_REF) | |
1256 | return; | |
1257 | ||
1258 | SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); | |
1259 | } | |
e70670cf | 1260 | |
d52f5295 | 1261 | /* Walk the alias chain to return the symbol NODE is alias of. |
e70670cf JH |
1262 | If NODE is not an alias, return NODE. |
1263 | When AVAILABILITY is non-NULL, get minimal availability in the chain. */ | |
1264 | ||
5e20cdc9 | 1265 | symtab_node * |
d52f5295 | 1266 | symtab_node::ultimate_alias_target (enum availability *availability) |
e70670cf | 1267 | { |
aaae719d JH |
1268 | bool weakref_p = false; |
1269 | ||
d52f5295 | 1270 | if (!alias) |
aaae719d JH |
1271 | { |
1272 | if (availability) | |
d52f5295 ML |
1273 | *availability = get_availability (); |
1274 | return this; | |
aaae719d JH |
1275 | } |
1276 | ||
1277 | /* To determine visibility of the target, we follow ELF semantic of aliases. | |
1278 | Here alias is an alternative assembler name of a given definition. Its | |
1aa95df7 | 1279 | availability prevails the availability of its target (i.e. static alias of |
aaae719d JH |
1280 | weak definition is available. |
1281 | ||
1282 | Weakref is a different animal (and not part of ELF per se). It is just | |
1283 | alternative name of a given symbol used within one complation unit | |
1284 | and is translated prior hitting the object file. It inherits the | |
1285 | visibility of its target (i.e. weakref of non-overwritable definition | |
1286 | is non-overwritable, while weakref of weak definition is weak). | |
1287 | ||
1288 | If we ever get into supporting targets with different semantics, a target | |
1289 | hook will be needed here. */ | |
1290 | ||
e70670cf | 1291 | if (availability) |
aaae719d | 1292 | { |
d52f5295 | 1293 | weakref_p = weakref; |
aaae719d | 1294 | if (!weakref_p) |
d52f5295 | 1295 | *availability = get_availability (); |
aaae719d JH |
1296 | else |
1297 | *availability = AVAIL_LOCAL; | |
1298 | } | |
d52f5295 ML |
1299 | |
1300 | symtab_node *node = this; | |
e70670cf JH |
1301 | while (node) |
1302 | { | |
67348ccc | 1303 | if (node->alias && node->analyzed) |
d52f5295 | 1304 | node = node->get_alias_target (); |
e70670cf | 1305 | else |
aaae719d JH |
1306 | { |
1307 | if (!availability) | |
1308 | ; | |
67348ccc | 1309 | else if (node->analyzed) |
aaae719d JH |
1310 | { |
1311 | if (weakref_p) | |
1312 | { | |
d52f5295 | 1313 | enum availability a = node->get_availability (); |
aaae719d JH |
1314 | if (a < *availability) |
1315 | *availability = a; | |
1316 | } | |
1317 | } | |
1318 | else | |
1319 | *availability = AVAIL_NOT_AVAILABLE; | |
1320 | return node; | |
1321 | } | |
1322 | if (node && availability && weakref_p) | |
e70670cf | 1323 | { |
d52f5295 | 1324 | enum availability a = node->get_availability (); |
e70670cf JH |
1325 | if (a < *availability) |
1326 | *availability = a; | |
67348ccc | 1327 | weakref_p = node->weakref; |
e70670cf JH |
1328 | } |
1329 | } | |
1330 | if (availability) | |
1331 | *availability = AVAIL_NOT_AVAILABLE; | |
1332 | return NULL; | |
1333 | } | |
40a7fe1e JH |
1334 | |
1335 | /* C++ FE sometimes change linkage flags after producing same body aliases. | |
1336 | ||
1337 | FIXME: C++ produce implicit aliases for virtual functions and vtables that | |
1338 | are obviously equivalent. The way it is doing so is however somewhat | |
1339 | kludgy and interferes with the visibility code. As a result we need to | |
1340 | copy the visibility from the target to get things right. */ | |
1341 | ||
1342 | void | |
d52f5295 | 1343 | symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target) |
40a7fe1e | 1344 | { |
d52f5295 | 1345 | if (is_a <cgraph_node *> (this)) |
40a7fe1e | 1346 | { |
d52f5295 | 1347 | DECL_DECLARED_INLINE_P (decl) |
67348ccc | 1348 | = DECL_DECLARED_INLINE_P (target->decl); |
d52f5295 | 1349 | DECL_DISREGARD_INLINE_LIMITS (decl) |
67348ccc | 1350 | = DECL_DISREGARD_INLINE_LIMITS (target->decl); |
40a7fe1e JH |
1351 | } |
1352 | /* FIXME: It is not really clear why those flags should not be copied for | |
1353 | functions, too. */ | |
1354 | else | |
1355 | { | |
d52f5295 ML |
1356 | DECL_WEAK (decl) = DECL_WEAK (target->decl); |
1357 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); | |
1358 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl); | |
40a7fe1e | 1359 | } |
d52f5295 ML |
1360 | DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl); |
1361 | if (TREE_PUBLIC (decl)) | |
40a7fe1e | 1362 | { |
aede2c10 JH |
1363 | tree group; |
1364 | ||
d52f5295 ML |
1365 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); |
1366 | DECL_COMDAT (decl) = DECL_COMDAT (target->decl); | |
aede2c10 | 1367 | group = target->get_comdat_group (); |
d52f5295 ML |
1368 | set_comdat_group (group); |
1369 | if (group && !same_comdat_group) | |
1370 | add_to_same_comdat_group (target); | |
40a7fe1e | 1371 | } |
d52f5295 | 1372 | externally_visible = target->externally_visible; |
f961457f JH |
1373 | } |
1374 | ||
1375 | /* Set section, do not recurse into aliases. | |
1376 | When one wants to change section of symbol and its aliases, | |
d52f5295 | 1377 | use set_section. */ |
f961457f JH |
1378 | |
1379 | void | |
1380 | symtab_node::set_section_for_node (const char *section) | |
1381 | { | |
1382 | const char *current = get_section (); | |
1383 | void **slot; | |
1384 | ||
1385 | if (current == section | |
1386 | || (current && section | |
1387 | && !strcmp (current, section))) | |
1388 | return; | |
1389 | ||
1390 | if (current) | |
1391 | { | |
1392 | x_section->ref_count--; | |
1393 | if (!x_section->ref_count) | |
1394 | { | |
3dafb85c | 1395 | slot = htab_find_slot_with_hash (symtab->section_hash, x_section->name, |
f961457f JH |
1396 | htab_hash_string (x_section->name), |
1397 | INSERT); | |
1398 | ggc_free (x_section); | |
3dafb85c | 1399 | htab_clear_slot (symtab->section_hash, slot); |
f961457f JH |
1400 | } |
1401 | x_section = NULL; | |
1402 | } | |
1403 | if (!section) | |
1404 | { | |
1405 | implicit_section = false; | |
1406 | return; | |
1407 | } | |
3dafb85c ML |
1408 | if (!symtab->section_hash) |
1409 | symtab->section_hash = htab_create_ggc (10, hash_section_hash_entry, | |
f961457f | 1410 | eq_sections, NULL); |
3dafb85c | 1411 | slot = htab_find_slot_with_hash (symtab->section_hash, section, |
f961457f JH |
1412 | htab_hash_string (section), |
1413 | INSERT); | |
1414 | if (*slot) | |
1415 | x_section = (section_hash_entry *)*slot; | |
1416 | else | |
1417 | { | |
1418 | int len = strlen (section); | |
1419 | *slot = x_section = ggc_cleared_alloc<section_hash_entry> (); | |
1420 | x_section->name = ggc_vec_alloc<char> (len + 1); | |
1421 | memcpy (x_section->name, section, len + 1); | |
1422 | } | |
1423 | x_section->ref_count++; | |
1424 | } | |
1425 | ||
e257a17c JH |
1426 | /* Worker for set_section. */ |
1427 | ||
d52f5295 ML |
1428 | bool |
1429 | symtab_node::set_section (symtab_node *n, void *s) | |
e257a17c | 1430 | { |
f961457f | 1431 | n->set_section_for_node ((char *)s); |
e257a17c JH |
1432 | return false; |
1433 | } | |
1434 | ||
1435 | /* Set section of symbol and its aliases. */ | |
1436 | ||
1437 | void | |
f961457f | 1438 | symtab_node::set_section (const char *section) |
e257a17c JH |
1439 | { |
1440 | gcc_assert (!this->alias); | |
d52f5295 ML |
1441 | call_for_symbol_and_aliases |
1442 | (symtab_node::set_section, const_cast<char *>(section), true); | |
e257a17c JH |
1443 | } |
1444 | ||
569b1784 JH |
1445 | /* Return the initialization priority. */ |
1446 | ||
1447 | priority_type | |
1448 | symtab_node::get_init_priority () | |
1449 | { | |
569b1784 JH |
1450 | if (!this->in_init_priority_hash) |
1451 | return DEFAULT_INIT_PRIORITY; | |
b086d530 TS |
1452 | |
1453 | symbol_priority_map *h = symtab->init_priority_hash->get (this); | |
569b1784 JH |
1454 | return h ? h->init : DEFAULT_INIT_PRIORITY; |
1455 | } | |
1456 | ||
d52f5295 ML |
1457 | /* Return availability of NODE. */ |
1458 | enum availability symtab_node::get_availability (void) | |
1459 | { | |
1460 | if (is_a <cgraph_node *> (this)) | |
1461 | return dyn_cast <cgraph_node *> (this)->get_availability (); | |
1462 | else | |
9041d2e6 | 1463 | return dyn_cast <varpool_node *> (this)->get_availability ();; |
d52f5295 ML |
1464 | } |
1465 | ||
1466 | ||
569b1784 JH |
1467 | /* Return the finalization priority. */ |
1468 | ||
1469 | priority_type | |
1470 | cgraph_node::get_fini_priority () | |
1471 | { | |
569b1784 JH |
1472 | if (!this->in_init_priority_hash) |
1473 | return DEFAULT_INIT_PRIORITY; | |
b086d530 | 1474 | symbol_priority_map *h = symtab->init_priority_hash->get (this); |
569b1784 JH |
1475 | return h ? h->fini : DEFAULT_INIT_PRIORITY; |
1476 | } | |
1477 | ||
569b1784 JH |
1478 | /* Return the initialization and finalization priority information for |
1479 | DECL. If there is no previous priority information, a freshly | |
1480 | allocated structure is returned. */ | |
1481 | ||
3dafb85c | 1482 | symbol_priority_map * |
d52f5295 | 1483 | symtab_node::priority_info (void) |
569b1784 | 1484 | { |
3dafb85c | 1485 | if (!symtab->init_priority_hash) |
b086d530 | 1486 | symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13); |
569b1784 | 1487 | |
b086d530 TS |
1488 | bool existed; |
1489 | symbol_priority_map *h | |
1490 | = &symtab->init_priority_hash->get_or_insert (this, &existed); | |
1491 | if (!existed) | |
569b1784 | 1492 | { |
569b1784 JH |
1493 | h->init = DEFAULT_INIT_PRIORITY; |
1494 | h->fini = DEFAULT_INIT_PRIORITY; | |
d52f5295 | 1495 | in_init_priority_hash = true; |
569b1784 JH |
1496 | } |
1497 | ||
1498 | return h; | |
1499 | } | |
1500 | ||
1501 | /* Set initialization priority to PRIORITY. */ | |
1502 | ||
1503 | void | |
1504 | symtab_node::set_init_priority (priority_type priority) | |
1505 | { | |
3dafb85c | 1506 | symbol_priority_map *h; |
569b1784 JH |
1507 | |
1508 | if (is_a <cgraph_node *> (this)) | |
1509 | gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl)); | |
1510 | ||
1511 | if (priority == DEFAULT_INIT_PRIORITY) | |
1512 | { | |
1513 | gcc_assert (get_init_priority() == priority); | |
1514 | return; | |
1515 | } | |
d52f5295 | 1516 | h = priority_info (); |
569b1784 JH |
1517 | h->init = priority; |
1518 | } | |
1519 | ||
1520 | /* Set fialization priority to PRIORITY. */ | |
1521 | ||
1522 | void | |
1523 | cgraph_node::set_fini_priority (priority_type priority) | |
1524 | { | |
3dafb85c | 1525 | symbol_priority_map *h; |
569b1784 JH |
1526 | |
1527 | gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl)); | |
1528 | ||
1529 | if (priority == DEFAULT_INIT_PRIORITY) | |
1530 | { | |
1531 | gcc_assert (get_fini_priority() == priority); | |
1532 | return; | |
1533 | } | |
d52f5295 | 1534 | h = priority_info (); |
569b1784 JH |
1535 | h->fini = priority; |
1536 | } | |
1537 | ||
e257a17c JH |
1538 | /* Worker for symtab_resolve_alias. */ |
1539 | ||
d52f5295 ML |
1540 | bool |
1541 | symtab_node::set_implicit_section (symtab_node *n, | |
1542 | void *data ATTRIBUTE_UNUSED) | |
e257a17c JH |
1543 | { |
1544 | n->implicit_section = true; | |
1545 | return false; | |
1546 | } | |
1547 | ||
d52f5295 | 1548 | /* Add reference recording that symtab node is alias of TARGET. |
40a7fe1e JH |
1549 | The function can fail in the case of aliasing cycles; in this case |
1550 | it returns false. */ | |
1551 | ||
1552 | bool | |
d52f5295 | 1553 | symtab_node::resolve_alias (symtab_node *target) |
40a7fe1e | 1554 | { |
5e20cdc9 | 1555 | symtab_node *n; |
40a7fe1e | 1556 | |
d52f5295 | 1557 | gcc_assert (!analyzed && !vec_safe_length (ref_list.references)); |
40a7fe1e JH |
1558 | |
1559 | /* Never let cycles to creep into the symbol table alias references; | |
1560 | those will make alias walkers to be infinite. */ | |
67348ccc | 1561 | for (n = target; n && n->alias; |
d52f5295 ML |
1562 | n = n->analyzed ? n->get_alias_target () : NULL) |
1563 | if (n == this) | |
40a7fe1e | 1564 | { |
d52f5295 ML |
1565 | if (is_a <cgraph_node *> (this)) |
1566 | error ("function %q+D part of alias cycle", decl); | |
1567 | else if (is_a <varpool_node *> (this)) | |
1568 | error ("variable %q+D part of alias cycle", decl); | |
40a7fe1e JH |
1569 | else |
1570 | gcc_unreachable (); | |
d52f5295 | 1571 | alias = false; |
40a7fe1e JH |
1572 | return false; |
1573 | } | |
1574 | ||
1575 | /* "analyze" the node - i.e. mark the reference. */ | |
d52f5295 ML |
1576 | definition = true; |
1577 | alias = true; | |
1578 | analyzed = true; | |
3dafb85c | 1579 | create_reference (target, IPA_REF_ALIAS, NULL); |
40a7fe1e | 1580 | |
e257a17c | 1581 | /* Add alias into the comdat group of its target unless it is already there. */ |
d52f5295 ML |
1582 | if (same_comdat_group) |
1583 | remove_from_same_comdat_group (); | |
1584 | set_comdat_group (NULL); | |
e257a17c | 1585 | if (target->get_comdat_group ()) |
d52f5295 | 1586 | add_to_same_comdat_group (target); |
e257a17c | 1587 | |
d52f5295 ML |
1588 | if ((get_section () != target->get_section () |
1589 | || target->get_comdat_group ()) && get_section () && !implicit_section) | |
e257a17c | 1590 | { |
d52f5295 | 1591 | error ("section of alias %q+D must match section of its target", decl); |
e257a17c | 1592 | } |
d52f5295 ML |
1593 | call_for_symbol_and_aliases (symtab_node::set_section, |
1594 | const_cast<char *>(target->get_section ()), true); | |
e257a17c | 1595 | if (target->implicit_section) |
d52f5295 | 1596 | call_for_symbol_and_aliases (set_implicit_section, NULL, true); |
e257a17c | 1597 | |
d67ff7b7 | 1598 | /* Alias targets become redundant after alias is resolved into an reference. |
40a7fe1e JH |
1599 | We do not want to keep it around or we would have to mind updating them |
1600 | when renaming symbols. */ | |
d52f5295 | 1601 | alias_target = NULL; |
40a7fe1e | 1602 | |
3dafb85c | 1603 | if (cpp_implicit_alias && symtab->state >= CONSTRUCTION) |
d52f5295 | 1604 | fixup_same_cpp_alias_visibility (target); |
40a7fe1e JH |
1605 | |
1606 | /* If alias has address taken, so does the target. */ | |
d52f5295 ML |
1607 | if (address_taken) |
1608 | target->ultimate_alias_target ()->address_taken = true; | |
40a7fe1e JH |
1609 | return true; |
1610 | } | |
af15184a | 1611 | |
d52f5295 | 1612 | /* Call calback on symtab node and aliases associated to this node. |
af15184a JH |
1613 | When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are |
1614 | skipped. */ | |
1615 | ||
1616 | bool | |
d52f5295 ML |
1617 | symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *, |
1618 | void *), | |
1619 | void *data, bool include_overwritable) | |
af15184a JH |
1620 | { |
1621 | int i; | |
3dafb85c | 1622 | ipa_ref *ref; |
af15184a | 1623 | |
d52f5295 | 1624 | if (callback (this, data)) |
af15184a | 1625 | return true; |
d52f5295 | 1626 | for (i = 0; iterate_referring (i, ref); i++) |
af15184a JH |
1627 | if (ref->use == IPA_REF_ALIAS) |
1628 | { | |
5e20cdc9 | 1629 | symtab_node *alias = ref->referring; |
af15184a | 1630 | if (include_overwritable |
d52f5295 ML |
1631 | || alias->get_availability () > AVAIL_INTERPOSABLE) |
1632 | if (alias->call_for_symbol_and_aliases (callback, data, | |
1633 | include_overwritable)) | |
af15184a JH |
1634 | return true; |
1635 | } | |
1636 | return false; | |
1637 | } | |
1638 | ||
d52f5295 | 1639 | /* Worker searching noninterposable alias. */ |
af15184a | 1640 | |
d52f5295 ML |
1641 | bool |
1642 | symtab_node::noninterposable_alias (symtab_node *node, void *data) | |
af15184a | 1643 | { |
67348ccc | 1644 | if (decl_binds_to_current_def_p (node->decl)) |
af15184a | 1645 | { |
d52f5295 | 1646 | symtab_node *fn = node->ultimate_alias_target (); |
72732f3e JH |
1647 | |
1648 | /* Ensure that the alias is well formed this may not be the case | |
1649 | of user defined aliases and currently it is not always the case | |
1650 | of C++ same body aliases (that is a bug). */ | |
1651 | if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl) | |
1652 | || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl) | |
1653 | || (TREE_CODE (node->decl) == FUNCTION_DECL | |
1654 | && flags_from_decl_or_type (node->decl) | |
1655 | != flags_from_decl_or_type (fn->decl)) | |
1656 | || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl)) | |
1657 | return false; | |
1658 | ||
5e20cdc9 | 1659 | *(symtab_node **)data = node; |
af15184a JH |
1660 | return true; |
1661 | } | |
1662 | return false; | |
1663 | } | |
1664 | ||
d52f5295 ML |
1665 | /* If node can not be overwriten by static or dynamic linker to point to |
1666 | different definition, return NODE. Otherwise look for alias with such | |
1667 | property and if none exists, introduce new one. */ | |
af15184a | 1668 | |
5e20cdc9 | 1669 | symtab_node * |
d52f5295 | 1670 | symtab_node::noninterposable_alias (void) |
af15184a JH |
1671 | { |
1672 | tree new_decl; | |
5e20cdc9 | 1673 | symtab_node *new_node = NULL; |
8a41354f JH |
1674 | |
1675 | /* First try to look up existing alias or base object | |
1676 | (if that is already non-overwritable). */ | |
d52f5295 | 1677 | symtab_node *node = ultimate_alias_target (); |
67348ccc | 1678 | gcc_assert (!node->alias && !node->weakref); |
d52f5295 ML |
1679 | node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias, |
1680 | (void *)&new_node, true); | |
af15184a JH |
1681 | if (new_node) |
1682 | return new_node; | |
cdb87c08 JH |
1683 | #ifndef ASM_OUTPUT_DEF |
1684 | /* If aliases aren't supported by the assembler, fail. */ | |
1685 | return NULL; | |
1686 | #endif | |
af15184a | 1687 | |
8a41354f | 1688 | /* Otherwise create a new one. */ |
67348ccc DM |
1689 | new_decl = copy_node (node->decl); |
1690 | DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias"); | |
af15184a JH |
1691 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1692 | DECL_STRUCT_FUNCTION (new_decl) = NULL; | |
1693 | DECL_INITIAL (new_decl) = NULL; | |
1694 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
1695 | SET_DECL_RTL (new_decl, NULL); | |
1696 | ||
1697 | /* Update the properties. */ | |
1698 | DECL_EXTERNAL (new_decl) = 0; | |
af15184a JH |
1699 | TREE_PUBLIC (new_decl) = 0; |
1700 | DECL_COMDAT (new_decl) = 0; | |
1701 | DECL_WEAK (new_decl) = 0; | |
80bc9b6e JH |
1702 | |
1703 | /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */ | |
1704 | DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl); | |
af15184a JH |
1705 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1706 | { | |
1707 | DECL_STATIC_CONSTRUCTOR (new_decl) = 0; | |
1708 | DECL_STATIC_DESTRUCTOR (new_decl) = 0; | |
d52f5295 | 1709 | new_node = cgraph_node::create_alias (new_decl, node->decl); |
af15184a JH |
1710 | } |
1711 | else | |
97ae6b64 JH |
1712 | { |
1713 | TREE_READONLY (new_decl) = TREE_READONLY (node->decl); | |
80bc9b6e | 1714 | DECL_INITIAL (new_decl) = error_mark_node; |
9041d2e6 | 1715 | new_node = varpool_node::create_alias (new_decl, node->decl); |
97ae6b64 | 1716 | } |
d52f5295 | 1717 | new_node->resolve_alias (node); |
97ae6b64 JH |
1718 | gcc_assert (decl_binds_to_current_def_p (new_decl) |
1719 | && targetm.binds_local_p (new_decl)); | |
af15184a JH |
1720 | return new_node; |
1721 | } | |
fc11f321 | 1722 | |
d52f5295 ML |
1723 | /* Return true if symtab node and TARGET represents |
1724 | semantically equivalent symbols. */ | |
fc11f321 JH |
1725 | |
1726 | bool | |
d52f5295 | 1727 | symtab_node::semantically_equivalent_p (symtab_node *target) |
fc11f321 JH |
1728 | { |
1729 | enum availability avail; | |
5e20cdc9 DM |
1730 | symtab_node *ba; |
1731 | symtab_node *bb; | |
fc11f321 JH |
1732 | |
1733 | /* Equivalent functions are equivalent. */ | |
d52f5295 | 1734 | if (decl == target->decl) |
fc11f321 JH |
1735 | return true; |
1736 | ||
1737 | /* If symbol is not overwritable by different implementation, | |
1738 | walk to the base object it defines. */ | |
d52f5295 | 1739 | ba = ultimate_alias_target (&avail); |
fc11f321 JH |
1740 | if (avail >= AVAIL_AVAILABLE) |
1741 | { | |
d52f5295 | 1742 | if (target == ba) |
fc11f321 JH |
1743 | return true; |
1744 | } | |
1745 | else | |
d52f5295 ML |
1746 | ba = this; |
1747 | bb = target->ultimate_alias_target (&avail); | |
fc11f321 JH |
1748 | if (avail >= AVAIL_AVAILABLE) |
1749 | { | |
d52f5295 | 1750 | if (this == bb) |
fc11f321 JH |
1751 | return true; |
1752 | } | |
1753 | else | |
d52f5295 | 1754 | bb = target; |
fc11f321 JH |
1755 | return bb == ba; |
1756 | } | |
ddb3e20a | 1757 | |
d52f5295 | 1758 | /* Classify symbol symtab node for partitioning. */ |
ddb3e20a JH |
1759 | |
1760 | enum symbol_partitioning_class | |
d52f5295 | 1761 | symtab_node::get_partitioning_class (void) |
ddb3e20a JH |
1762 | { |
1763 | /* Inline clones are always duplicated. | |
1764 | This include external delcarations. */ | |
d52f5295 | 1765 | cgraph_node *cnode = dyn_cast <cgraph_node *> (this); |
ddb3e20a | 1766 | |
d52f5295 | 1767 | if (DECL_ABSTRACT (decl)) |
ddb3e20a JH |
1768 | return SYMBOL_EXTERNAL; |
1769 | ||
1770 | if (cnode && cnode->global.inlined_to) | |
1771 | return SYMBOL_DUPLICATE; | |
1772 | ||
1773 | /* Weakref aliases are always duplicated. */ | |
d52f5295 | 1774 | if (weakref) |
ddb3e20a JH |
1775 | return SYMBOL_DUPLICATE; |
1776 | ||
1777 | /* External declarations are external. */ | |
d52f5295 | 1778 | if (DECL_EXTERNAL (decl)) |
ddb3e20a JH |
1779 | return SYMBOL_EXTERNAL; |
1780 | ||
d52f5295 | 1781 | if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) |
ddb3e20a JH |
1782 | { |
1783 | /* Constant pool references use local symbol names that can not | |
1784 | be promoted global. We should never put into a constant pool | |
1785 | objects that can not be duplicated across partitions. */ | |
d52f5295 | 1786 | if (DECL_IN_CONSTANT_POOL (decl)) |
ddb3e20a JH |
1787 | return SYMBOL_DUPLICATE; |
1788 | gcc_checking_assert (vnode->definition); | |
1789 | } | |
1790 | /* Functions that are cloned may stay in callgraph even if they are unused. | |
1791 | Handle them as external; compute_ltrans_boundary take care to make | |
1792 | proper things to happen (i.e. to make them appear in the boundary but | |
1793 | with body streamed, so clone can me materialized). */ | |
d52f5295 | 1794 | else if (!dyn_cast <cgraph_node *> (this)->definition) |
ddb3e20a JH |
1795 | return SYMBOL_EXTERNAL; |
1796 | ||
1797 | /* Linker discardable symbols are duplicated to every use unless they are | |
cf288ed3 | 1798 | keyed. */ |
d52f5295 ML |
1799 | if (DECL_ONE_ONLY (decl) |
1800 | && !force_output | |
1801 | && !forced_by_abi | |
1802 | && !used_from_object_file_p ()) | |
ddb3e20a JH |
1803 | return SYMBOL_DUPLICATE; |
1804 | ||
1805 | return SYMBOL_PARTITION; | |
1806 | } | |
89330618 JH |
1807 | |
1808 | /* Return true when symbol is known to be non-zero. */ | |
1809 | ||
1810 | bool | |
1811 | symtab_node::nonzero_address () | |
1812 | { | |
1813 | /* Weakrefs may be NULL when their target is not defined. */ | |
f7217cde | 1814 | if (alias && weakref) |
89330618 | 1815 | { |
f7217cde | 1816 | if (analyzed) |
89330618 | 1817 | { |
d52f5295 | 1818 | symtab_node *target = ultimate_alias_target (); |
89330618 JH |
1819 | |
1820 | if (target->alias && target->weakref) | |
1821 | return false; | |
1822 | /* We can not recurse to target::nonzero. It is possible that the | |
1823 | target is used only via the alias. | |
1824 | We may walk references and look for strong use, but we do not know | |
1825 | if this strong use will survive to final binary, so be | |
1826 | conservative here. | |
1827 | ??? Maybe we could do the lookup during late optimization that | |
1828 | could be useful to eliminate the NULL pointer checks in LTO | |
1829 | programs. */ | |
1830 | if (target->definition && !DECL_EXTERNAL (target->decl)) | |
f7217cde | 1831 | return true; |
89330618 JH |
1832 | if (target->resolution != LDPR_UNKNOWN |
1833 | && target->resolution != LDPR_UNDEF | |
1834 | && flag_delete_null_pointer_checks) | |
1835 | return true; | |
1836 | return false; | |
1837 | } | |
1838 | else | |
1839 | return false; | |
1840 | } | |
1841 | ||
1842 | /* With !flag_delete_null_pointer_checks we assume that symbols may | |
1843 | bind to NULL. This is on by default on embedded targets only. | |
1844 | ||
1845 | Otherwise all non-WEAK symbols must be defined and thus non-NULL or | |
1846 | linking fails. Important case of WEAK we want to do well are comdats. | |
1847 | Those are handled by later check for definition. | |
1848 | ||
1849 | When parsing, beware the cases when WEAK attribute is added later. */ | |
f7217cde JH |
1850 | if (!DECL_WEAK (decl) |
1851 | && flag_delete_null_pointer_checks) | |
1852 | { | |
1853 | refuse_visibility_changes = true; | |
1854 | return true; | |
1855 | } | |
89330618 JH |
1856 | |
1857 | /* If target is defined and not extern, we know it will be output and thus | |
1858 | it will bind to non-NULL. | |
1859 | Play safe for flag_delete_null_pointer_checks where weak definition maye | |
1860 | be re-defined by NULL. */ | |
f7217cde JH |
1861 | if (definition && !DECL_EXTERNAL (decl) |
1862 | && (flag_delete_null_pointer_checks || !DECL_WEAK (decl))) | |
1863 | { | |
1864 | if (!DECL_WEAK (decl)) | |
1865 | refuse_visibility_changes = true; | |
1866 | return true; | |
1867 | } | |
89330618 JH |
1868 | |
1869 | /* As the last resort, check the resolution info. */ | |
f7217cde JH |
1870 | if (resolution != LDPR_UNKNOWN |
1871 | && resolution != LDPR_UNDEF | |
89330618 JH |
1872 | && flag_delete_null_pointer_checks) |
1873 | return true; | |
1874 | return false; | |
1875 | } |