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