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