]>
Commit | Line | Data |
---|---|---|
2aae7680 | 1 | /* Symbol table. |
5624e564 | 2 | Copyright (C) 2012-2015 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" |
40e23961 MC |
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" | |
2aae7680 | 35 | #include "tree.h" |
40e23961 | 36 | #include "fold-const.h" |
d8a2d370 DN |
37 | #include "print-tree.h" |
38 | #include "varasm.h" | |
83685514 | 39 | #include "hashtab.h" |
83685514 AM |
40 | #include "hard-reg-set.h" |
41 | #include "input.h" | |
d8a2d370 DN |
42 | #include "function.h" |
43 | #include "emit-rtl.h" | |
60393bbc | 44 | #include "predict.h" |
2fb9a547 AM |
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" | |
8e9055ae | 50 | #include "gimple.h" |
2aae7680 | 51 | #include "tree-inline.h" |
8f940ee6 | 52 | #include "langhooks.h" |
c582198b AM |
53 | #include "hash-map.h" |
54 | #include "plugin-api.h" | |
55 | #include "ipa-ref.h" | |
2aae7680 | 56 | #include "cgraph.h" |
1ab24192 | 57 | #include "diagnostic.h" |
474ffc72 | 58 | #include "timevar.h" |
65d630d4 | 59 | #include "lto-streamer.h" |
862d0b35 | 60 | #include "output.h" |
d122681a | 61 | #include "ipa-utils.h" |
72732f3e | 62 | #include "calls.h" |
d122681a | 63 | |
d5e254e1 | 64 | static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"}; |
65d630d4 JH |
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 | }; | |
1ab24192 | 79 | |
862d0b35 DN |
80 | /* Hash asmnames ignoring the user specified marks. */ |
81 | ||
3dafb85c ML |
82 | hashval_t |
83 | symbol_table::decl_assembler_name_hash (const_tree asmname) | |
862d0b35 DN |
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 | ||
862d0b35 DN |
102 | /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */ |
103 | ||
3dafb85c ML |
104 | bool |
105 | symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname) | |
862d0b35 DN |
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 | ||
1ab24192 JH |
158 | /* Returns nonzero if P1 and P2 are equal. */ |
159 | ||
1ab24192 JH |
160 | /* Insert NODE to assembler name hash. */ |
161 | ||
3dafb85c ML |
162 | void |
163 | symbol_table::insert_to_assembler_name_hash (symtab_node *node, | |
164 | bool with_clones) | |
1ab24192 | 165 | { |
7de90a6c | 166 | if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl)) |
b5493fb2 | 167 | return; |
67348ccc DM |
168 | gcc_checking_assert (!node->previous_sharing_asm_name |
169 | && !node->next_sharing_asm_name); | |
1ab24192 JH |
170 | if (assembler_name_hash) |
171 | { | |
2a22f99c | 172 | symtab_node **aslot; |
3dafb85c | 173 | cgraph_node *cnode; |
67348ccc | 174 | tree decl = node->decl; |
c3167b00 | 175 | |
67348ccc | 176 | tree name = DECL_ASSEMBLER_NAME (node->decl); |
1ab24192 | 177 | |
59b5b466 JH |
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 | ||
2a22f99c TS |
183 | hashval_t hash = decl_assembler_name_hash (name); |
184 | aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT); | |
1ab24192 | 185 | gcc_assert (*aslot != node); |
5e20cdc9 | 186 | node->next_sharing_asm_name = (symtab_node *)*aslot; |
1ab24192 | 187 | if (*aslot != NULL) |
2a22f99c | 188 | (*aslot)->previous_sharing_asm_name = node; |
1ab24192 | 189 | *aslot = node; |
c3167b00 JH |
190 | |
191 | /* Update also possible inline clones sharing a decl. */ | |
7de90a6c | 192 | cnode = dyn_cast <cgraph_node *> (node); |
c3167b00 JH |
193 | if (cnode && cnode->clones && with_clones) |
194 | for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) | |
67348ccc DM |
195 | if (cnode->decl == decl) |
196 | insert_to_assembler_name_hash (cnode, true); | |
1ab24192 JH |
197 | } |
198 | ||
199 | } | |
200 | ||
201 | /* Remove NODE from assembler name hash. */ | |
202 | ||
3dafb85c ML |
203 | void |
204 | symbol_table::unlink_from_assembler_name_hash (symtab_node *node, | |
205 | bool with_clones) | |
1ab24192 JH |
206 | { |
207 | if (assembler_name_hash) | |
208 | { | |
3dafb85c | 209 | cgraph_node *cnode; |
67348ccc | 210 | tree decl = node->decl; |
c3167b00 | 211 | |
67348ccc DM |
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) | |
1ab24192 | 216 | { |
67348ccc DM |
217 | node->previous_sharing_asm_name->next_sharing_asm_name |
218 | = node->next_sharing_asm_name; | |
1ab24192 JH |
219 | } |
220 | else | |
221 | { | |
67348ccc | 222 | tree name = DECL_ASSEMBLER_NAME (node->decl); |
2a22f99c | 223 | symtab_node **slot; |
59b5b466 JH |
224 | |
225 | if (!name) | |
226 | return; | |
227 | ||
2a22f99c TS |
228 | hashval_t hash = decl_assembler_name_hash (name); |
229 | slot = assembler_name_hash->find_slot_with_hash (name, hash, | |
230 | NO_INSERT); | |
1ab24192 | 231 | gcc_assert (*slot == node); |
67348ccc | 232 | if (!node->next_sharing_asm_name) |
2a22f99c | 233 | assembler_name_hash->clear_slot (slot); |
1ab24192 | 234 | else |
67348ccc | 235 | *slot = node->next_sharing_asm_name; |
1ab24192 | 236 | } |
67348ccc DM |
237 | node->next_sharing_asm_name = NULL; |
238 | node->previous_sharing_asm_name = NULL; | |
c3167b00 JH |
239 | |
240 | /* Update also possible inline clones sharing a decl. */ | |
7de90a6c | 241 | cnode = dyn_cast <cgraph_node *> (node); |
c3167b00 JH |
242 | if (cnode && cnode->clones && with_clones) |
243 | for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone) | |
67348ccc DM |
244 | if (cnode->decl == decl) |
245 | unlink_from_assembler_name_hash (cnode, true); | |
1ab24192 JH |
246 | } |
247 | } | |
248 | ||
b5493fb2 JH |
249 | /* Arrange node to be first in its entry of assembler_name_hash. */ |
250 | ||
251 | void | |
3dafb85c | 252 | symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node) |
b5493fb2 | 253 | { |
c3167b00 JH |
254 | unlink_from_assembler_name_hash (node, false); |
255 | insert_to_assembler_name_hash (node, false); | |
b5493fb2 JH |
256 | } |
257 | ||
b5493fb2 | 258 | /* Initalize asm name hash unless. */ |
1ab24192 | 259 | |
b5493fb2 | 260 | void |
3dafb85c | 261 | symbol_table::symtab_initialize_asm_name_hash (void) |
1ab24192 | 262 | { |
5e20cdc9 | 263 | symtab_node *node; |
1ab24192 JH |
264 | if (!assembler_name_hash) |
265 | { | |
2a22f99c | 266 | assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10); |
1ab24192 | 267 | FOR_EACH_SYMBOL (node) |
c3167b00 | 268 | insert_to_assembler_name_hash (node, false); |
1ab24192 | 269 | } |
b5493fb2 | 270 | } |
1ab24192 | 271 | |
1ab24192 JH |
272 | /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */ |
273 | ||
274 | void | |
3dafb85c | 275 | symbol_table::change_decl_assembler_name (tree decl, tree name) |
1ab24192 | 276 | { |
5e20cdc9 | 277 | symtab_node *node = NULL; |
1ab24192 JH |
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) | |
d52f5295 | 284 | node = symtab_node::get (decl); |
1ab24192 JH |
285 | if (!DECL_ASSEMBLER_NAME_SET_P (decl)) |
286 | { | |
287 | SET_DECL_ASSEMBLER_NAME (decl, name); | |
288 | if (node) | |
c3167b00 | 289 | insert_to_assembler_name_hash (node, true); |
1ab24192 JH |
290 | } |
291 | else | |
292 | { | |
293 | if (name == DECL_ASSEMBLER_NAME (decl)) | |
294 | return; | |
295 | ||
8a41354f JH |
296 | tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl)) |
297 | ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl)) | |
298 | : NULL); | |
1ab24192 | 299 | if (node) |
c3167b00 | 300 | unlink_from_assembler_name_hash (node, true); |
1ab24192 JH |
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); | |
8a41354f JH |
306 | if (alias) |
307 | { | |
308 | IDENTIFIER_TRANSPARENT_ALIAS (name) = 1; | |
1fed15fc | 309 | TREE_CHAIN (name) = alias; |
8a41354f | 310 | } |
1ab24192 | 311 | if (node) |
c3167b00 | 312 | insert_to_assembler_name_hash (node, true); |
1ab24192 JH |
313 | } |
314 | } | |
315 | ||
d52f5295 ML |
316 | /* Hash sections by their names. */ |
317 | ||
2a22f99c TS |
318 | hashval_t |
319 | section_name_hasher::hash (section_hash_entry *n) | |
d52f5295 | 320 | { |
d52f5295 ML |
321 | return htab_hash_string (n->name); |
322 | } | |
323 | ||
324 | /* Return true if section P1 name equals to P2. */ | |
325 | ||
2a22f99c TS |
326 | bool |
327 | section_name_hasher::equal (section_hash_entry *n1, const char *name) | |
d52f5295 | 328 | { |
d52f5295 ML |
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 | { | |
3dafb85c | 338 | symtab->register_symbol (this); |
d52f5295 ML |
339 | |
340 | if (!decl->decl_with_vis.symtab_node) | |
341 | decl->decl_with_vis.symtab_node = this; | |
342 | ||
343 | ref_list.clear (); | |
344 | ||
d52f5295 ML |
345 | /* Be sure to do this last; C++ FE might create new nodes via |
346 | DECL_ASSEMBLER_NAME langhook! */ | |
3dafb85c | 347 | symtab->insert_to_assembler_name_hash (this, false); |
d52f5295 ML |
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 | ||
3dafb85c | 385 | symtab->unregister (this); |
d52f5295 ML |
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)) | |
3dafb85c | 398 | symtab->unlink_from_assembler_name_hash (this, false); |
d52f5295 | 399 | if (in_init_priority_hash) |
b086d530 | 400 | symtab->init_priority_hash->remove (this); |
d52f5295 ML |
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 | ||
65d630d4 JH |
415 | /* Add NEW_ to the same comdat group that OLD is in. */ |
416 | ||
417 | void | |
d52f5295 | 418 | symtab_node::add_to_same_comdat_group (symtab_node *old_node) |
65d630d4 | 419 | { |
aede2c10 | 420 | gcc_assert (old_node->get_comdat_group ()); |
d52f5295 ML |
421 | gcc_assert (!same_comdat_group); |
422 | gcc_assert (this != old_node); | |
65d630d4 | 423 | |
d52f5295 ML |
424 | set_comdat_group (old_node->get_comdat_group ()); |
425 | same_comdat_group = old_node; | |
67348ccc | 426 | if (!old_node->same_comdat_group) |
d52f5295 | 427 | old_node->same_comdat_group = this; |
65d630d4 JH |
428 | else |
429 | { | |
5e20cdc9 | 430 | symtab_node *n; |
67348ccc DM |
431 | for (n = old_node->same_comdat_group; |
432 | n->same_comdat_group != old_node; | |
433 | n = n->same_comdat_group) | |
65d630d4 | 434 | ; |
d52f5295 | 435 | n->same_comdat_group = this; |
65d630d4 JH |
436 | } |
437 | } | |
438 | ||
439 | /* Dissolve the same_comdat_group list in which NODE resides. */ | |
440 | ||
441 | void | |
d52f5295 | 442 | symtab_node::dissolve_same_comdat_group_list (void) |
65d630d4 | 443 | { |
d52f5295 | 444 | symtab_node *n = this; |
5e20cdc9 | 445 | symtab_node *next; |
65d630d4 | 446 | |
d52f5295 | 447 | if (!same_comdat_group) |
65d630d4 JH |
448 | return; |
449 | do | |
450 | { | |
67348ccc DM |
451 | next = n->same_comdat_group; |
452 | n->same_comdat_group = NULL; | |
aede2c10 | 453 | /* Clear comdat_group for comdat locals, since |
1f26ac87 JM |
454 | make_decl_local doesn't. */ |
455 | if (!TREE_PUBLIC (n->decl)) | |
aede2c10 | 456 | n->set_comdat_group (NULL); |
65d630d4 JH |
457 | n = next; |
458 | } | |
d52f5295 | 459 | while (n != this); |
65d630d4 JH |
460 | } |
461 | ||
8f940ee6 JH |
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 * | |
fec39fa6 | 467 | symtab_node::asm_name () const |
8f940ee6 | 468 | { |
fec39fa6 TS |
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)); | |
8f940ee6 JH |
472 | } |
473 | ||
474 | /* Return printable identifier name. */ | |
475 | ||
476 | const char * | |
fec39fa6 | 477 | symtab_node::name () const |
8f940ee6 | 478 | { |
fec39fa6 | 479 | return lang_hooks.decl_printable_name (decl, 2); |
8f940ee6 JH |
480 | } |
481 | ||
d122681a ML |
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 | ||
3dafb85c ML |
486 | ipa_ref * |
487 | symtab_node::create_reference (symtab_node *referred_node, | |
488 | enum ipa_ref_use use_type) | |
d122681a | 489 | { |
3dafb85c | 490 | return create_reference (referred_node, use_type, NULL); |
d122681a ML |
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 | ||
3dafb85c ML |
498 | ipa_ref * |
499 | symtab_node::create_reference (symtab_node *referred_node, | |
500 | enum ipa_ref_use use_type, gimple stmt) | |
d122681a | 501 | { |
3dafb85c ML |
502 | ipa_ref *ref = NULL, *ref2 = NULL; |
503 | ipa_ref_list *list, *list2; | |
d122681a ML |
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; | |
e55637b7 ML |
515 | |
516 | /* IPA_REF_ALIAS is always inserted at the beginning of the list. */ | |
517 | if(use_type == IPA_REF_ALIAS) | |
31de7606 JH |
518 | { |
519 | list2->referring.safe_insert (0, ref); | |
520 | ref->referred_index = 0; | |
e55637b7 | 521 | |
31de7606 JH |
522 | for (unsigned int i = 1; i < list2->referring.length (); i++) |
523 | list2->referring[i]->referred_index = i; | |
524 | } | |
e55637b7 | 525 | else |
31de7606 JH |
526 | { |
527 | list2->referring.safe_push (ref); | |
528 | ref->referred_index = list2->referring.length () - 1; | |
529 | } | |
e55637b7 | 530 | |
d122681a ML |
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 | ||
3dafb85c ML |
553 | ipa_ref * |
554 | symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type, | |
555 | gimple stmt) | |
d122681a ML |
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 | { | |
d52f5295 | 564 | symtab_node *referred = symtab_node::get (val); |
d122681a | 565 | gcc_checking_assert (referred); |
3dafb85c | 566 | return create_reference (referred, use_type, stmt); |
d122681a ML |
567 | } |
568 | return NULL; | |
569 | } | |
570 | ||
571 | /* Clone all references from symtab NODE to this symtab_node. */ | |
572 | ||
573 | void | |
3dafb85c | 574 | symtab_node::clone_references (symtab_node *node) |
d122681a | 575 | { |
3dafb85c | 576 | ipa_ref *ref = NULL, *ref2 = NULL; |
d122681a ML |
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 | ||
3dafb85c | 583 | ref2 = create_reference (ref->referred, ref->use, ref->stmt); |
d122681a ML |
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 | |
3dafb85c | 592 | symtab_node::clone_referring (symtab_node *node) |
d122681a | 593 | { |
3dafb85c | 594 | ipa_ref *ref = NULL, *ref2 = NULL; |
d122681a ML |
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 | ||
3dafb85c | 601 | ref2 = ref->referring->create_reference (this, ref->use, ref->stmt); |
d122681a ML |
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 | ||
3dafb85c ML |
609 | ipa_ref * |
610 | symtab_node::clone_reference (ipa_ref *ref, gimple stmt) | |
d122681a ML |
611 | { |
612 | bool speculative = ref->speculative; | |
613 | unsigned int stmt_uid = ref->lto_stmt_uid; | |
3dafb85c | 614 | ipa_ref *ref2; |
d122681a | 615 | |
3dafb85c | 616 | ref2 = create_reference (ref->referred, ref->use, stmt); |
d122681a ML |
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 | ||
3dafb85c | 625 | ipa_ref * |
d122681a ML |
626 | symtab_node::find_reference (symtab_node *referred_node, |
627 | gimple stmt, unsigned int lto_stmt_uid) | |
628 | { | |
3dafb85c | 629 | ipa_ref *r = NULL; |
d122681a ML |
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 | { | |
3dafb85c | 647 | ipa_ref *r = NULL; |
d122681a ML |
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 | { | |
3dafb85c | 665 | ipa_ref *r = NULL; |
d122681a ML |
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 | { | |
3dafb85c | 701 | ipa_ref *ref = NULL; |
d122681a ML |
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 | { | |
3dafb85c | 720 | ipa_ref *ref = NULL; |
d122681a ML |
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 | ||
8f940ee6 JH |
734 | static const char * const symtab_type_names[] = {"symbol", "function", "variable"}; |
735 | ||
d52f5295 | 736 | /* Dump base fields of symtab nodes to F. Not to be used directly. */ |
8f940ee6 JH |
737 | |
738 | void | |
d52f5295 | 739 | symtab_node::dump_base (FILE *f) |
8f940ee6 JH |
740 | { |
741 | static const char * const visibility_types[] = { | |
742 | "default", "protected", "hidden", "internal" | |
743 | }; | |
744 | ||
d52f5295 ML |
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]); | |
e70670cf | 748 | |
d52f5295 | 749 | if (definition) |
e70670cf | 750 | fprintf (f, " definition"); |
d52f5295 | 751 | if (analyzed) |
e70670cf | 752 | fprintf (f, " analyzed"); |
d52f5295 | 753 | if (alias) |
e70670cf | 754 | fprintf (f, " alias"); |
d52f5295 | 755 | if (weakref) |
08346abd | 756 | fprintf (f, " weakref"); |
d52f5295 | 757 | if (cpp_implicit_alias) |
40a7fe1e | 758 | fprintf (f, " cpp_implicit_alias"); |
d52f5295 | 759 | if (alias_target) |
40a7fe1e | 760 | fprintf (f, " target:%s", |
d52f5295 | 761 | DECL_P (alias_target) |
40a7fe1e | 762 | ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME |
d52f5295 ML |
763 | (alias_target)) |
764 | : IDENTIFIER_POINTER (alias_target)); | |
765 | if (body_removed) | |
3d8d0043 | 766 | fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes"); |
e70670cf | 767 | fprintf (f, "\n Visibility:"); |
d52f5295 | 768 | if (in_other_partition) |
8f940ee6 | 769 | fprintf (f, " in_other_partition"); |
d52f5295 | 770 | if (used_from_other_partition) |
8f940ee6 | 771 | fprintf (f, " used_from_other_partition"); |
d52f5295 | 772 | if (force_output) |
ead84f73 | 773 | fprintf (f, " force_output"); |
d52f5295 | 774 | if (forced_by_abi) |
edb983b2 | 775 | fprintf (f, " forced_by_abi"); |
d52f5295 | 776 | if (externally_visible) |
e5b962d0 | 777 | fprintf (f, " externally_visible"); |
7861b648 AK |
778 | if (no_reorder) |
779 | fprintf (f, " no_reorder"); | |
d52f5295 | 780 | if (resolution != LDPR_UNKNOWN) |
8f940ee6 | 781 | fprintf (f, " %s", |
d52f5295 ML |
782 | ld_plugin_symbol_resolution_names[(int)resolution]); |
783 | if (TREE_ASM_WRITTEN (decl)) | |
8f940ee6 | 784 | fprintf (f, " asm_written"); |
d52f5295 | 785 | if (DECL_EXTERNAL (decl)) |
8f940ee6 | 786 | fprintf (f, " external"); |
d52f5295 | 787 | if (TREE_PUBLIC (decl)) |
8f940ee6 | 788 | fprintf (f, " public"); |
d52f5295 | 789 | if (DECL_COMMON (decl)) |
8f940ee6 | 790 | fprintf (f, " common"); |
d52f5295 | 791 | if (DECL_WEAK (decl)) |
8f940ee6 | 792 | fprintf (f, " weak"); |
d52f5295 | 793 | if (DECL_DLLIMPORT_P (decl)) |
8f940ee6 | 794 | fprintf (f, " dll_import"); |
d52f5295 | 795 | if (DECL_COMDAT (decl)) |
8f940ee6 | 796 | fprintf (f, " comdat"); |
d52f5295 | 797 | if (get_comdat_group ()) |
8f940ee6 | 798 | fprintf (f, " comdat_group:%s", |
d52f5295 ML |
799 | IDENTIFIER_POINTER (get_comdat_group_id ())); |
800 | if (DECL_ONE_ONLY (decl)) | |
8f940ee6 | 801 | fprintf (f, " one_only"); |
d52f5295 | 802 | if (get_section ()) |
24d047a3 | 803 | fprintf (f, " section:%s", |
d52f5295 ML |
804 | get_section ()); |
805 | if (implicit_section) | |
e257a17c | 806 | fprintf (f," (implicit_section)"); |
d52f5295 | 807 | if (DECL_VISIBILITY_SPECIFIED (decl)) |
8f940ee6 | 808 | fprintf (f, " visibility_specified"); |
d52f5295 | 809 | if (DECL_VISIBILITY (decl)) |
8f940ee6 | 810 | fprintf (f, " visibility:%s", |
d52f5295 ML |
811 | visibility_types [DECL_VISIBILITY (decl)]); |
812 | if (DECL_VIRTUAL_P (decl)) | |
8f940ee6 | 813 | fprintf (f, " virtual"); |
d52f5295 | 814 | if (DECL_ARTIFICIAL (decl)) |
8f940ee6 | 815 | fprintf (f, " artificial"); |
d52f5295 | 816 | if (TREE_CODE (decl) == FUNCTION_DECL) |
838ff415 | 817 | { |
d52f5295 | 818 | if (DECL_STATIC_CONSTRUCTOR (decl)) |
838ff415 | 819 | fprintf (f, " constructor"); |
d52f5295 | 820 | if (DECL_STATIC_DESTRUCTOR (decl)) |
838ff415 JH |
821 | fprintf (f, " destructor"); |
822 | } | |
8f940ee6 JH |
823 | fprintf (f, "\n"); |
824 | ||
d52f5295 | 825 | if (same_comdat_group) |
8f940ee6 | 826 | fprintf (f, " Same comdat group as: %s/%i\n", |
d52f5295 ML |
827 | same_comdat_group->asm_name (), |
828 | same_comdat_group->order); | |
829 | if (next_sharing_asm_name) | |
8f940ee6 | 830 | fprintf (f, " next sharing asm name: %i\n", |
d52f5295 ML |
831 | next_sharing_asm_name->order); |
832 | if (previous_sharing_asm_name) | |
8f940ee6 | 833 | fprintf (f, " previous sharing asm name: %i\n", |
d52f5295 | 834 | previous_sharing_asm_name->order); |
8f940ee6 | 835 | |
d52f5295 | 836 | if (address_taken) |
fe0bd630 | 837 | fprintf (f, " Address is taken.\n"); |
d52f5295 | 838 | if (aux) |
66058468 JH |
839 | { |
840 | fprintf (f, " Aux:"); | |
d52f5295 | 841 | dump_addr (f, " @", (void *)aux); |
66058468 | 842 | } |
8f940ee6 JH |
843 | |
844 | fprintf (f, " References: "); | |
d52f5295 | 845 | dump_references (f); |
5932a4d4 | 846 | fprintf (f, " Referring: "); |
d52f5295 ML |
847 | dump_referring (f); |
848 | if (lto_file_data) | |
b5493fb2 | 849 | fprintf (f, " Read from file: %s\n", |
d52f5295 | 850 | lto_file_data->file_name); |
8f940ee6 JH |
851 | } |
852 | ||
d52f5295 | 853 | /* Dump symtab node to F. */ |
8f940ee6 JH |
854 | |
855 | void | |
d52f5295 | 856 | symtab_node::dump (FILE *f) |
8f940ee6 | 857 | { |
d52f5295 ML |
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); | |
8f940ee6 JH |
862 | } |
863 | ||
d52f5295 | 864 | /* Dump symbol table to F. */ |
8f940ee6 JH |
865 | |
866 | void | |
d52f5295 | 867 | symtab_node::dump_table (FILE *f) |
8f940ee6 | 868 | { |
5e20cdc9 | 869 | symtab_node *node; |
8f940ee6 JH |
870 | fprintf (f, "Symbol table:\n\n"); |
871 | FOR_EACH_SYMBOL (node) | |
d52f5295 | 872 | node->dump (f); |
8f940ee6 JH |
873 | } |
874 | ||
3dafb85c ML |
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; | |
3dafb85c ML |
883 | |
884 | symtab->symtab_initialize_asm_name_hash (); | |
2a22f99c TS |
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); | |
3dafb85c ML |
889 | |
890 | if (slot) | |
891 | { | |
2a22f99c | 892 | node = *slot; |
3dafb85c ML |
893 | return node; |
894 | } | |
895 | return NULL; | |
896 | } | |
897 | ||
8f940ee6 JH |
898 | /* Dump symtab node NODE to stderr. */ |
899 | ||
900 | DEBUG_FUNCTION void | |
d52f5295 | 901 | symtab_node::debug (void) |
8f940ee6 | 902 | { |
d52f5295 | 903 | dump (stderr); |
8f940ee6 JH |
904 | } |
905 | ||
474ffc72 JH |
906 | /* Verify common part of symtab nodes. */ |
907 | ||
908 | DEBUG_FUNCTION bool | |
d52f5295 | 909 | symtab_node::verify_base (void) |
474ffc72 JH |
910 | { |
911 | bool error_found = false; | |
5e20cdc9 | 912 | symtab_node *hashed_node; |
474ffc72 | 913 | |
d52f5295 | 914 | if (is_a <cgraph_node *> (this)) |
474ffc72 | 915 | { |
d52f5295 | 916 | if (TREE_CODE (decl) != FUNCTION_DECL) |
474ffc72 JH |
917 | { |
918 | error ("function symbol is not function"); | |
919 | error_found = true; | |
920 | } | |
921 | } | |
d52f5295 | 922 | else if (is_a <varpool_node *> (this)) |
474ffc72 | 923 | { |
d52f5295 | 924 | if (TREE_CODE (decl) != VAR_DECL) |
474ffc72 JH |
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 | ||
3dafb85c | 936 | if (symtab->state != LTO_STREAMING) |
474ffc72 | 937 | { |
d52f5295 | 938 | hashed_node = symtab_node::get (decl); |
ca0f62a8 JH |
939 | if (!hashed_node) |
940 | { | |
aede2c10 | 941 | error ("node not found node->decl->decl_with_vis.symtab_node"); |
ca0f62a8 JH |
942 | error_found = true; |
943 | } | |
d52f5295 ML |
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)) | |
e5b962d0 | 948 | { |
aede2c10 | 949 | error ("node differs from node->decl->decl_with_vis.symtab_node"); |
e5b962d0 JH |
950 | error_found = true; |
951 | } | |
474ffc72 | 952 | } |
3dafb85c | 953 | if (symtab->assembler_name_hash) |
474ffc72 | 954 | { |
3dafb85c | 955 | hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl)); |
67348ccc | 956 | if (hashed_node && hashed_node->previous_sharing_asm_name) |
474ffc72 JH |
957 | { |
958 | error ("assembler name hash list corrupted"); | |
959 | error_found = true; | |
960 | } | |
961 | while (hashed_node) | |
962 | { | |
d52f5295 | 963 | if (hashed_node == this) |
474ffc72 | 964 | break; |
67348ccc | 965 | hashed_node = hashed_node->next_sharing_asm_name; |
474ffc72 | 966 | } |
b5493fb2 | 967 | if (!hashed_node |
d52f5295 ML |
968 | && !(is_a <varpool_node *> (this) |
969 | || DECL_HARD_REGISTER (decl))) | |
474ffc72 JH |
970 | { |
971 | error ("node not found in symtab assembler name hash"); | |
972 | error_found = true; | |
973 | } | |
974 | } | |
d52f5295 ML |
975 | if (previous_sharing_asm_name |
976 | && previous_sharing_asm_name->next_sharing_asm_name != this) | |
474ffc72 JH |
977 | { |
978 | error ("double linked list of assembler names corrupted"); | |
87be7f0c JH |
979 | error_found = true; |
980 | } | |
981 | if (body_removed && definition) | |
982 | { | |
983 | error ("node has body_removed but is definition"); | |
e70670cf JH |
984 | error_found = true; |
985 | } | |
d52f5295 | 986 | if (analyzed && !definition) |
e70670cf JH |
987 | { |
988 | error ("node is analyzed byt it is not a definition"); | |
989 | error_found = true; | |
474ffc72 | 990 | } |
d52f5295 | 991 | if (cpp_implicit_alias && !alias) |
40a7fe1e JH |
992 | { |
993 | error ("node is alias but not implicit alias"); | |
994 | error_found = true; | |
995 | } | |
d52f5295 | 996 | if (alias && !definition && !weakref) |
40a7fe1e JH |
997 | { |
998 | error ("node is alias but not definition"); | |
999 | error_found = true; | |
1000 | } | |
d52f5295 | 1001 | if (weakref && !alias) |
08346abd JH |
1002 | { |
1003 | error ("node is weakref but not an alias"); | |
1004 | error_found = true; | |
1005 | } | |
d52f5295 | 1006 | if (same_comdat_group) |
474ffc72 | 1007 | { |
d52f5295 | 1008 | symtab_node *n = same_comdat_group; |
474ffc72 | 1009 | |
aede2c10 | 1010 | if (!n->get_comdat_group ()) |
474ffc72 | 1011 | { |
aede2c10 | 1012 | error ("node is in same_comdat_group list but has no comdat_group"); |
474ffc72 JH |
1013 | error_found = true; |
1014 | } | |
d52f5295 | 1015 | if (n->get_comdat_group () != get_comdat_group ()) |
7b3376a0 JH |
1016 | { |
1017 | error ("same_comdat_group list across different groups"); | |
1018 | error_found = true; | |
1019 | } | |
d52f5295 | 1020 | if (n->type != type) |
474ffc72 JH |
1021 | { |
1022 | error ("mixing different types of symbol in same comdat groups is not supported"); | |
1023 | error_found = true; | |
1024 | } | |
d52f5295 | 1025 | if (n == this) |
474ffc72 JH |
1026 | { |
1027 | error ("node is alone in a comdat group"); | |
1028 | error_found = true; | |
1029 | } | |
1030 | do | |
1031 | { | |
67348ccc | 1032 | if (!n->same_comdat_group) |
474ffc72 JH |
1033 | { |
1034 | error ("same_comdat_group is not a circular list"); | |
1035 | error_found = true; | |
1036 | break; | |
1037 | } | |
67348ccc | 1038 | n = n->same_comdat_group; |
474ffc72 | 1039 | } |
d52f5295 ML |
1040 | while (n != this); |
1041 | if (comdat_local_p ()) | |
1f26ac87 | 1042 | { |
3dafb85c | 1043 | ipa_ref *ref = NULL; |
e257a17c | 1044 | |
d52f5295 | 1045 | for (int i = 0; iterate_referring (i, ref); ++i) |
1f26ac87 | 1046 | { |
d52f5295 | 1047 | if (!in_same_comdat_group_p (ref->referring)) |
1f26ac87 JM |
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 | } | |
474ffc72 | 1056 | } |
d52f5295 | 1057 | if (implicit_section && !get_section ()) |
e257a17c JH |
1058 | { |
1059 | error ("implicit_section flag is set but section isn't"); | |
1060 | error_found = true; | |
1061 | } | |
d52f5295 | 1062 | if (get_section () && get_comdat_group () |
b0122457 TS |
1063 | && !implicit_section |
1064 | && !lookup_attribute ("section", DECL_ATTRIBUTES (decl))) | |
e257a17c JH |
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. */ | |
d52f5295 ML |
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 ()))) | |
e257a17c JH |
1077 | { |
1078 | error ("Alias and target's section differs"); | |
d52f5295 | 1079 | get_alias_target ()->dump (stderr); |
e257a17c JH |
1080 | error_found = true; |
1081 | } | |
d52f5295 ML |
1082 | if (alias && definition |
1083 | && get_comdat_group () != get_alias_target ()->get_comdat_group ()) | |
e257a17c JH |
1084 | { |
1085 | error ("Alias and target's comdat groups differs"); | |
d52f5295 | 1086 | get_alias_target ()->dump (stderr); |
e257a17c JH |
1087 | error_found = true; |
1088 | } | |
1089 | ||
474ffc72 JH |
1090 | return error_found; |
1091 | } | |
1092 | ||
1093 | /* Verify consistency of NODE. */ | |
1094 | ||
1095 | DEBUG_FUNCTION void | |
d52f5295 | 1096 | symtab_node::verify (void) |
474ffc72 JH |
1097 | { |
1098 | if (seen_error ()) | |
1099 | return; | |
1100 | ||
1101 | timevar_push (TV_CGRAPH_VERIFY); | |
d52f5295 ML |
1102 | if (cgraph_node *node = dyn_cast <cgraph_node *> (this)) |
1103 | node->verify_node (); | |
474ffc72 | 1104 | else |
d52f5295 | 1105 | if (verify_base ()) |
474ffc72 | 1106 | { |
d52f5295 ML |
1107 | debug (); |
1108 | internal_error ("symtab_node::verify failed"); | |
474ffc72 JH |
1109 | } |
1110 | timevar_pop (TV_CGRAPH_VERIFY); | |
1111 | } | |
1112 | ||
1113 | /* Verify symbol table for internal consistency. */ | |
1114 | ||
1115 | DEBUG_FUNCTION void | |
d52f5295 | 1116 | symtab_node::verify_symtab_nodes (void) |
474ffc72 | 1117 | { |
5e20cdc9 | 1118 | symtab_node *node; |
1eb68d2d | 1119 | hash_map<tree, symtab_node *> comdat_head_map (251); |
e257a17c | 1120 | |
474ffc72 | 1121 | FOR_EACH_SYMBOL (node) |
e257a17c | 1122 | { |
d52f5295 | 1123 | node->verify (); |
e257a17c JH |
1124 | if (node->get_comdat_group ()) |
1125 | { | |
1126 | symtab_node **entry, *s; | |
1127 | bool existed; | |
1128 | ||
1eb68d2d TS |
1129 | entry = &comdat_head_map.get_or_insert (node->get_comdat_group (), |
1130 | &existed); | |
e257a17c JH |
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."); | |
d52f5295 ML |
1138 | (*entry)->debug (); |
1139 | node->debug (); | |
1140 | internal_error ("symtab_node::verify failed"); | |
e257a17c JH |
1141 | } |
1142 | } | |
1143 | } | |
474ffc72 JH |
1144 | } |
1145 | ||
65d630d4 JH |
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. */ | |
40a7fe1e | 1148 | |
65d630d4 | 1149 | void |
d52f5295 | 1150 | symtab_node::make_decl_local (void) |
65d630d4 JH |
1151 | { |
1152 | rtx rtl, symbol; | |
1153 | ||
aede2c10 | 1154 | /* Avoid clearing comdat_groups on comdat-local decls. */ |
1f26ac87 JM |
1155 | if (TREE_PUBLIC (decl) == 0) |
1156 | return; | |
1157 | ||
65d630d4 | 1158 | if (TREE_CODE (decl) == VAR_DECL) |
0a7246ee JH |
1159 | { |
1160 | DECL_COMMON (decl) = 0; | |
1161 | /* ADDRESSABLE flag is not defined for public symbols. */ | |
1162 | TREE_ADDRESSABLE (decl) = 1; | |
1163 | } | |
65d630d4 JH |
1164 | else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); |
1165 | ||
24d047a3 | 1166 | DECL_COMDAT (decl) = 0; |
65d630d4 JH |
1167 | DECL_WEAK (decl) = 0; |
1168 | DECL_EXTERNAL (decl) = 0; | |
b5493fb2 JH |
1169 | DECL_VISIBILITY_SPECIFIED (decl) = 0; |
1170 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; | |
65d630d4 | 1171 | TREE_PUBLIC (decl) = 0; |
feeca9cc | 1172 | DECL_DLLIMPORT_P (decl) = 0; |
65d630d4 JH |
1173 | if (!DECL_RTL_SET_P (decl)) |
1174 | return; | |
1175 | ||
1176 | /* Update rtl flags. */ | |
1177 | make_decl_rtl (decl); | |
1178 | ||
1179 | rtl = DECL_RTL (decl); | |
1180 | if (!MEM_P (rtl)) | |
1181 | return; | |
1182 | ||
1183 | symbol = XEXP (rtl, 0); | |
1184 | if (GET_CODE (symbol) != SYMBOL_REF) | |
1185 | return; | |
1186 | ||
1187 | SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl); | |
1188 | } | |
e70670cf | 1189 | |
d52f5295 | 1190 | /* Walk the alias chain to return the symbol NODE is alias of. |
e70670cf | 1191 | If NODE is not an alias, return NODE. |
31de7606 | 1192 | Assumes NODE is known to be alias. */ |
e70670cf | 1193 | |
5e20cdc9 | 1194 | symtab_node * |
31de7606 | 1195 | symtab_node::ultimate_alias_target_1 (enum availability *availability) |
e70670cf | 1196 | { |
aaae719d JH |
1197 | bool weakref_p = false; |
1198 | ||
aaae719d JH |
1199 | /* To determine visibility of the target, we follow ELF semantic of aliases. |
1200 | Here alias is an alternative assembler name of a given definition. Its | |
1aa95df7 | 1201 | availability prevails the availability of its target (i.e. static alias of |
aaae719d JH |
1202 | weak definition is available. |
1203 | ||
1204 | Weakref is a different animal (and not part of ELF per se). It is just | |
1205 | alternative name of a given symbol used within one complation unit | |
1206 | and is translated prior hitting the object file. It inherits the | |
1207 | visibility of its target (i.e. weakref of non-overwritable definition | |
1208 | is non-overwritable, while weakref of weak definition is weak). | |
1209 | ||
1210 | If we ever get into supporting targets with different semantics, a target | |
1211 | hook will be needed here. */ | |
1212 | ||
e70670cf | 1213 | if (availability) |
aaae719d | 1214 | { |
d52f5295 | 1215 | weakref_p = weakref; |
aaae719d | 1216 | if (!weakref_p) |
d52f5295 | 1217 | *availability = get_availability (); |
aaae719d JH |
1218 | else |
1219 | *availability = AVAIL_LOCAL; | |
1220 | } | |
d52f5295 ML |
1221 | |
1222 | symtab_node *node = this; | |
e70670cf JH |
1223 | while (node) |
1224 | { | |
67348ccc | 1225 | if (node->alias && node->analyzed) |
d52f5295 | 1226 | node = node->get_alias_target (); |
e70670cf | 1227 | else |
aaae719d JH |
1228 | { |
1229 | if (!availability) | |
1230 | ; | |
67348ccc | 1231 | else if (node->analyzed) |
aaae719d JH |
1232 | { |
1233 | if (weakref_p) | |
1234 | { | |
d52f5295 | 1235 | enum availability a = node->get_availability (); |
aaae719d JH |
1236 | if (a < *availability) |
1237 | *availability = a; | |
1238 | } | |
1239 | } | |
1240 | else | |
1241 | *availability = AVAIL_NOT_AVAILABLE; | |
1242 | return node; | |
1243 | } | |
1244 | if (node && availability && weakref_p) | |
e70670cf | 1245 | { |
d52f5295 | 1246 | enum availability a = node->get_availability (); |
e70670cf JH |
1247 | if (a < *availability) |
1248 | *availability = a; | |
67348ccc | 1249 | weakref_p = node->weakref; |
e70670cf JH |
1250 | } |
1251 | } | |
1252 | if (availability) | |
1253 | *availability = AVAIL_NOT_AVAILABLE; | |
1254 | return NULL; | |
1255 | } | |
40a7fe1e JH |
1256 | |
1257 | /* C++ FE sometimes change linkage flags after producing same body aliases. | |
1258 | ||
1259 | FIXME: C++ produce implicit aliases for virtual functions and vtables that | |
1260 | are obviously equivalent. The way it is doing so is however somewhat | |
1261 | kludgy and interferes with the visibility code. As a result we need to | |
1262 | copy the visibility from the target to get things right. */ | |
1263 | ||
1264 | void | |
d52f5295 | 1265 | symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target) |
40a7fe1e | 1266 | { |
d52f5295 | 1267 | if (is_a <cgraph_node *> (this)) |
40a7fe1e | 1268 | { |
d52f5295 | 1269 | DECL_DECLARED_INLINE_P (decl) |
67348ccc | 1270 | = DECL_DECLARED_INLINE_P (target->decl); |
d52f5295 | 1271 | DECL_DISREGARD_INLINE_LIMITS (decl) |
67348ccc | 1272 | = DECL_DISREGARD_INLINE_LIMITS (target->decl); |
40a7fe1e JH |
1273 | } |
1274 | /* FIXME: It is not really clear why those flags should not be copied for | |
1275 | functions, too. */ | |
1276 | else | |
1277 | { | |
d52f5295 ML |
1278 | DECL_WEAK (decl) = DECL_WEAK (target->decl); |
1279 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); | |
1280 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl); | |
40a7fe1e | 1281 | } |
d52f5295 ML |
1282 | DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl); |
1283 | if (TREE_PUBLIC (decl)) | |
40a7fe1e | 1284 | { |
aede2c10 JH |
1285 | tree group; |
1286 | ||
d52f5295 ML |
1287 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl); |
1288 | DECL_COMDAT (decl) = DECL_COMDAT (target->decl); | |
aede2c10 | 1289 | group = target->get_comdat_group (); |
d52f5295 ML |
1290 | set_comdat_group (group); |
1291 | if (group && !same_comdat_group) | |
1292 | add_to_same_comdat_group (target); | |
40a7fe1e | 1293 | } |
d52f5295 | 1294 | externally_visible = target->externally_visible; |
f961457f JH |
1295 | } |
1296 | ||
1297 | /* Set section, do not recurse into aliases. | |
1298 | When one wants to change section of symbol and its aliases, | |
d52f5295 | 1299 | use set_section. */ |
f961457f JH |
1300 | |
1301 | void | |
1302 | symtab_node::set_section_for_node (const char *section) | |
1303 | { | |
1304 | const char *current = get_section (); | |
2a22f99c | 1305 | section_hash_entry **slot; |
f961457f JH |
1306 | |
1307 | if (current == section | |
1308 | || (current && section | |
1309 | && !strcmp (current, section))) | |
1310 | return; | |
1311 | ||
1312 | if (current) | |
1313 | { | |
1314 | x_section->ref_count--; | |
1315 | if (!x_section->ref_count) | |
1316 | { | |
2a22f99c TS |
1317 | hashval_t hash = htab_hash_string (x_section->name); |
1318 | slot = symtab->section_hash->find_slot_with_hash (x_section->name, | |
1319 | hash, INSERT); | |
f961457f | 1320 | ggc_free (x_section); |
2a22f99c | 1321 | symtab->section_hash->clear_slot (slot); |
f961457f JH |
1322 | } |
1323 | x_section = NULL; | |
1324 | } | |
1325 | if (!section) | |
1326 | { | |
1327 | implicit_section = false; | |
1328 | return; | |
1329 | } | |
3dafb85c | 1330 | if (!symtab->section_hash) |
2a22f99c TS |
1331 | symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10); |
1332 | slot = symtab->section_hash->find_slot_with_hash (section, | |
1333 | htab_hash_string (section), | |
1334 | INSERT); | |
f961457f JH |
1335 | if (*slot) |
1336 | x_section = (section_hash_entry *)*slot; | |
1337 | else | |
1338 | { | |
1339 | int len = strlen (section); | |
1340 | *slot = x_section = ggc_cleared_alloc<section_hash_entry> (); | |
1341 | x_section->name = ggc_vec_alloc<char> (len + 1); | |
1342 | memcpy (x_section->name, section, len + 1); | |
1343 | } | |
1344 | x_section->ref_count++; | |
1345 | } | |
1346 | ||
e257a17c JH |
1347 | /* Worker for set_section. */ |
1348 | ||
d52f5295 ML |
1349 | bool |
1350 | symtab_node::set_section (symtab_node *n, void *s) | |
e257a17c | 1351 | { |
f961457f | 1352 | n->set_section_for_node ((char *)s); |
e257a17c JH |
1353 | return false; |
1354 | } | |
1355 | ||
1356 | /* Set section of symbol and its aliases. */ | |
1357 | ||
1358 | void | |
f961457f | 1359 | symtab_node::set_section (const char *section) |
e257a17c JH |
1360 | { |
1361 | gcc_assert (!this->alias); | |
d52f5295 ML |
1362 | call_for_symbol_and_aliases |
1363 | (symtab_node::set_section, const_cast<char *>(section), true); | |
e257a17c JH |
1364 | } |
1365 | ||
569b1784 JH |
1366 | /* Return the initialization priority. */ |
1367 | ||
1368 | priority_type | |
1369 | symtab_node::get_init_priority () | |
1370 | { | |
569b1784 JH |
1371 | if (!this->in_init_priority_hash) |
1372 | return DEFAULT_INIT_PRIORITY; | |
b086d530 TS |
1373 | |
1374 | symbol_priority_map *h = symtab->init_priority_hash->get (this); | |
569b1784 JH |
1375 | return h ? h->init : DEFAULT_INIT_PRIORITY; |
1376 | } | |
1377 | ||
1378 | /* Return the finalization priority. */ | |
1379 | ||
1380 | priority_type | |
1381 | cgraph_node::get_fini_priority () | |
1382 | { | |
569b1784 JH |
1383 | if (!this->in_init_priority_hash) |
1384 | return DEFAULT_INIT_PRIORITY; | |
b086d530 | 1385 | symbol_priority_map *h = symtab->init_priority_hash->get (this); |
569b1784 JH |
1386 | return h ? h->fini : DEFAULT_INIT_PRIORITY; |
1387 | } | |
1388 | ||
569b1784 JH |
1389 | /* Return the initialization and finalization priority information for |
1390 | DECL. If there is no previous priority information, a freshly | |
1391 | allocated structure is returned. */ | |
1392 | ||
3dafb85c | 1393 | symbol_priority_map * |
d52f5295 | 1394 | symtab_node::priority_info (void) |
569b1784 | 1395 | { |
3dafb85c | 1396 | if (!symtab->init_priority_hash) |
b086d530 | 1397 | symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13); |
569b1784 | 1398 | |
b086d530 TS |
1399 | bool existed; |
1400 | symbol_priority_map *h | |
1401 | = &symtab->init_priority_hash->get_or_insert (this, &existed); | |
1402 | if (!existed) | |
569b1784 | 1403 | { |
569b1784 JH |
1404 | h->init = DEFAULT_INIT_PRIORITY; |
1405 | h->fini = DEFAULT_INIT_PRIORITY; | |
d52f5295 | 1406 | in_init_priority_hash = true; |
569b1784 JH |
1407 | } |
1408 | ||
1409 | return h; | |
1410 | } | |
1411 | ||
1412 | /* Set initialization priority to PRIORITY. */ | |
1413 | ||
1414 | void | |
1415 | symtab_node::set_init_priority (priority_type priority) | |
1416 | { | |
3dafb85c | 1417 | symbol_priority_map *h; |
569b1784 JH |
1418 | |
1419 | if (is_a <cgraph_node *> (this)) | |
1420 | gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl)); | |
1421 | ||
1422 | if (priority == DEFAULT_INIT_PRIORITY) | |
1423 | { | |
1424 | gcc_assert (get_init_priority() == priority); | |
1425 | return; | |
1426 | } | |
d52f5295 | 1427 | h = priority_info (); |
569b1784 JH |
1428 | h->init = priority; |
1429 | } | |
1430 | ||
1431 | /* Set fialization priority to PRIORITY. */ | |
1432 | ||
1433 | void | |
1434 | cgraph_node::set_fini_priority (priority_type priority) | |
1435 | { | |
3dafb85c | 1436 | symbol_priority_map *h; |
569b1784 JH |
1437 | |
1438 | gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl)); | |
1439 | ||
1440 | if (priority == DEFAULT_INIT_PRIORITY) | |
1441 | { | |
1442 | gcc_assert (get_fini_priority() == priority); | |
1443 | return; | |
1444 | } | |
d52f5295 | 1445 | h = priority_info (); |
569b1784 JH |
1446 | h->fini = priority; |
1447 | } | |
1448 | ||
e257a17c JH |
1449 | /* Worker for symtab_resolve_alias. */ |
1450 | ||
d52f5295 ML |
1451 | bool |
1452 | symtab_node::set_implicit_section (symtab_node *n, | |
1453 | void *data ATTRIBUTE_UNUSED) | |
e257a17c JH |
1454 | { |
1455 | n->implicit_section = true; | |
1456 | return false; | |
1457 | } | |
1458 | ||
d52f5295 | 1459 | /* Add reference recording that symtab node is alias of TARGET. |
40a7fe1e JH |
1460 | The function can fail in the case of aliasing cycles; in this case |
1461 | it returns false. */ | |
1462 | ||
1463 | bool | |
d52f5295 | 1464 | symtab_node::resolve_alias (symtab_node *target) |
40a7fe1e | 1465 | { |
5e20cdc9 | 1466 | symtab_node *n; |
40a7fe1e | 1467 | |
d52f5295 | 1468 | gcc_assert (!analyzed && !vec_safe_length (ref_list.references)); |
40a7fe1e JH |
1469 | |
1470 | /* Never let cycles to creep into the symbol table alias references; | |
1471 | those will make alias walkers to be infinite. */ | |
67348ccc | 1472 | for (n = target; n && n->alias; |
d52f5295 ML |
1473 | n = n->analyzed ? n->get_alias_target () : NULL) |
1474 | if (n == this) | |
40a7fe1e | 1475 | { |
d52f5295 ML |
1476 | if (is_a <cgraph_node *> (this)) |
1477 | error ("function %q+D part of alias cycle", decl); | |
1478 | else if (is_a <varpool_node *> (this)) | |
1479 | error ("variable %q+D part of alias cycle", decl); | |
40a7fe1e JH |
1480 | else |
1481 | gcc_unreachable (); | |
d52f5295 | 1482 | alias = false; |
40a7fe1e JH |
1483 | return false; |
1484 | } | |
1485 | ||
1486 | /* "analyze" the node - i.e. mark the reference. */ | |
d52f5295 ML |
1487 | definition = true; |
1488 | alias = true; | |
1489 | analyzed = true; | |
3dafb85c | 1490 | create_reference (target, IPA_REF_ALIAS, NULL); |
40a7fe1e | 1491 | |
e257a17c | 1492 | /* Add alias into the comdat group of its target unless it is already there. */ |
d52f5295 ML |
1493 | if (same_comdat_group) |
1494 | remove_from_same_comdat_group (); | |
1495 | set_comdat_group (NULL); | |
e257a17c | 1496 | if (target->get_comdat_group ()) |
d52f5295 | 1497 | add_to_same_comdat_group (target); |
e257a17c | 1498 | |
d52f5295 ML |
1499 | if ((get_section () != target->get_section () |
1500 | || target->get_comdat_group ()) && get_section () && !implicit_section) | |
e257a17c | 1501 | { |
d52f5295 | 1502 | error ("section of alias %q+D must match section of its target", decl); |
e257a17c | 1503 | } |
d52f5295 ML |
1504 | call_for_symbol_and_aliases (symtab_node::set_section, |
1505 | const_cast<char *>(target->get_section ()), true); | |
e257a17c | 1506 | if (target->implicit_section) |
d52f5295 | 1507 | call_for_symbol_and_aliases (set_implicit_section, NULL, true); |
e257a17c | 1508 | |
d67ff7b7 | 1509 | /* Alias targets become redundant after alias is resolved into an reference. |
40a7fe1e JH |
1510 | We do not want to keep it around or we would have to mind updating them |
1511 | when renaming symbols. */ | |
d52f5295 | 1512 | alias_target = NULL; |
40a7fe1e | 1513 | |
3dafb85c | 1514 | if (cpp_implicit_alias && symtab->state >= CONSTRUCTION) |
d52f5295 | 1515 | fixup_same_cpp_alias_visibility (target); |
40a7fe1e JH |
1516 | |
1517 | /* If alias has address taken, so does the target. */ | |
d52f5295 ML |
1518 | if (address_taken) |
1519 | target->ultimate_alias_target ()->address_taken = true; | |
0a7246ee JH |
1520 | |
1521 | /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */ | |
1522 | ipa_ref *ref; | |
1523 | for (unsigned i = 0; iterate_direct_aliases (i, ref);) | |
1524 | { | |
1525 | struct symtab_node *alias_alias = ref->referring; | |
1526 | if (!alias_alias->weakref) | |
1527 | { | |
1528 | alias_alias->remove_all_references (); | |
1529 | alias_alias->create_reference (target, IPA_REF_ALIAS, NULL); | |
1530 | } | |
1531 | else i++; | |
1532 | } | |
40a7fe1e JH |
1533 | return true; |
1534 | } | |
af15184a | 1535 | |
d52f5295 | 1536 | /* Worker searching noninterposable alias. */ |
af15184a | 1537 | |
d52f5295 ML |
1538 | bool |
1539 | symtab_node::noninterposable_alias (symtab_node *node, void *data) | |
af15184a | 1540 | { |
67348ccc | 1541 | if (decl_binds_to_current_def_p (node->decl)) |
af15184a | 1542 | { |
d52f5295 | 1543 | symtab_node *fn = node->ultimate_alias_target (); |
72732f3e JH |
1544 | |
1545 | /* Ensure that the alias is well formed this may not be the case | |
1546 | of user defined aliases and currently it is not always the case | |
1547 | of C++ same body aliases (that is a bug). */ | |
1548 | if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl) | |
1549 | || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl) | |
1550 | || (TREE_CODE (node->decl) == FUNCTION_DECL | |
1551 | && flags_from_decl_or_type (node->decl) | |
1552 | != flags_from_decl_or_type (fn->decl)) | |
1553 | || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl)) | |
1554 | return false; | |
5e20cdc9 | 1555 | *(symtab_node **)data = node; |
af15184a JH |
1556 | return true; |
1557 | } | |
1558 | return false; | |
1559 | } | |
1560 | ||
d52f5295 ML |
1561 | /* If node can not be overwriten by static or dynamic linker to point to |
1562 | different definition, return NODE. Otherwise look for alias with such | |
1563 | property and if none exists, introduce new one. */ | |
af15184a | 1564 | |
5e20cdc9 | 1565 | symtab_node * |
d52f5295 | 1566 | symtab_node::noninterposable_alias (void) |
af15184a JH |
1567 | { |
1568 | tree new_decl; | |
5e20cdc9 | 1569 | symtab_node *new_node = NULL; |
8a41354f JH |
1570 | |
1571 | /* First try to look up existing alias or base object | |
1572 | (if that is already non-overwritable). */ | |
d52f5295 | 1573 | symtab_node *node = ultimate_alias_target (); |
67348ccc | 1574 | gcc_assert (!node->alias && !node->weakref); |
d52f5295 ML |
1575 | node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias, |
1576 | (void *)&new_node, true); | |
af15184a JH |
1577 | if (new_node) |
1578 | return new_node; | |
cdb87c08 JH |
1579 | #ifndef ASM_OUTPUT_DEF |
1580 | /* If aliases aren't supported by the assembler, fail. */ | |
1581 | return NULL; | |
1582 | #endif | |
af15184a | 1583 | |
8a41354f | 1584 | /* Otherwise create a new one. */ |
67348ccc | 1585 | new_decl = copy_node (node->decl); |
feeca9cc | 1586 | DECL_DLLIMPORT_P (new_decl) = 0; |
67348ccc | 1587 | DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias"); |
af15184a JH |
1588 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1589 | DECL_STRUCT_FUNCTION (new_decl) = NULL; | |
1590 | DECL_INITIAL (new_decl) = NULL; | |
1591 | SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl)); | |
1592 | SET_DECL_RTL (new_decl, NULL); | |
1593 | ||
1594 | /* Update the properties. */ | |
1595 | DECL_EXTERNAL (new_decl) = 0; | |
af15184a JH |
1596 | TREE_PUBLIC (new_decl) = 0; |
1597 | DECL_COMDAT (new_decl) = 0; | |
1598 | DECL_WEAK (new_decl) = 0; | |
80bc9b6e JH |
1599 | |
1600 | /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */ | |
1601 | DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl); | |
af15184a JH |
1602 | if (TREE_CODE (new_decl) == FUNCTION_DECL) |
1603 | { | |
1604 | DECL_STATIC_CONSTRUCTOR (new_decl) = 0; | |
1605 | DECL_STATIC_DESTRUCTOR (new_decl) = 0; | |
d52f5295 | 1606 | new_node = cgraph_node::create_alias (new_decl, node->decl); |
af15184a JH |
1607 | } |
1608 | else | |
97ae6b64 JH |
1609 | { |
1610 | TREE_READONLY (new_decl) = TREE_READONLY (node->decl); | |
80bc9b6e | 1611 | DECL_INITIAL (new_decl) = error_mark_node; |
9041d2e6 | 1612 | new_node = varpool_node::create_alias (new_decl, node->decl); |
97ae6b64 | 1613 | } |
d52f5295 | 1614 | new_node->resolve_alias (node); |
97ae6b64 JH |
1615 | gcc_assert (decl_binds_to_current_def_p (new_decl) |
1616 | && targetm.binds_local_p (new_decl)); | |
af15184a JH |
1617 | return new_node; |
1618 | } | |
fc11f321 | 1619 | |
d52f5295 ML |
1620 | /* Return true if symtab node and TARGET represents |
1621 | semantically equivalent symbols. */ | |
fc11f321 JH |
1622 | |
1623 | bool | |
d52f5295 | 1624 | symtab_node::semantically_equivalent_p (symtab_node *target) |
fc11f321 JH |
1625 | { |
1626 | enum availability avail; | |
5e20cdc9 DM |
1627 | symtab_node *ba; |
1628 | symtab_node *bb; | |
fc11f321 JH |
1629 | |
1630 | /* Equivalent functions are equivalent. */ | |
d52f5295 | 1631 | if (decl == target->decl) |
fc11f321 JH |
1632 | return true; |
1633 | ||
1634 | /* If symbol is not overwritable by different implementation, | |
1635 | walk to the base object it defines. */ | |
d52f5295 | 1636 | ba = ultimate_alias_target (&avail); |
fc11f321 JH |
1637 | if (avail >= AVAIL_AVAILABLE) |
1638 | { | |
d52f5295 | 1639 | if (target == ba) |
fc11f321 JH |
1640 | return true; |
1641 | } | |
1642 | else | |
d52f5295 ML |
1643 | ba = this; |
1644 | bb = target->ultimate_alias_target (&avail); | |
fc11f321 JH |
1645 | if (avail >= AVAIL_AVAILABLE) |
1646 | { | |
d52f5295 | 1647 | if (this == bb) |
fc11f321 JH |
1648 | return true; |
1649 | } | |
1650 | else | |
d52f5295 | 1651 | bb = target; |
fc11f321 JH |
1652 | return bb == ba; |
1653 | } | |
ddb3e20a | 1654 | |
d52f5295 | 1655 | /* Classify symbol symtab node for partitioning. */ |
ddb3e20a JH |
1656 | |
1657 | enum symbol_partitioning_class | |
d52f5295 | 1658 | symtab_node::get_partitioning_class (void) |
ddb3e20a JH |
1659 | { |
1660 | /* Inline clones are always duplicated. | |
1661 | This include external delcarations. */ | |
d52f5295 | 1662 | cgraph_node *cnode = dyn_cast <cgraph_node *> (this); |
ddb3e20a | 1663 | |
00de328a | 1664 | if (DECL_ABSTRACT_P (decl)) |
ddb3e20a JH |
1665 | return SYMBOL_EXTERNAL; |
1666 | ||
1667 | if (cnode && cnode->global.inlined_to) | |
1668 | return SYMBOL_DUPLICATE; | |
1669 | ||
1670 | /* Weakref aliases are always duplicated. */ | |
d52f5295 | 1671 | if (weakref) |
ddb3e20a JH |
1672 | return SYMBOL_DUPLICATE; |
1673 | ||
1674 | /* External declarations are external. */ | |
d52f5295 | 1675 | if (DECL_EXTERNAL (decl)) |
ddb3e20a JH |
1676 | return SYMBOL_EXTERNAL; |
1677 | ||
d52f5295 | 1678 | if (varpool_node *vnode = dyn_cast <varpool_node *> (this)) |
ddb3e20a | 1679 | { |
d3f2e41e JH |
1680 | if (alias && definition && !ultimate_alias_target ()->definition) |
1681 | return SYMBOL_EXTERNAL; | |
ddb3e20a JH |
1682 | /* Constant pool references use local symbol names that can not |
1683 | be promoted global. We should never put into a constant pool | |
1684 | objects that can not be duplicated across partitions. */ | |
d52f5295 | 1685 | if (DECL_IN_CONSTANT_POOL (decl)) |
ddb3e20a JH |
1686 | return SYMBOL_DUPLICATE; |
1687 | gcc_checking_assert (vnode->definition); | |
1688 | } | |
1689 | /* Functions that are cloned may stay in callgraph even if they are unused. | |
1690 | Handle them as external; compute_ltrans_boundary take care to make | |
1691 | proper things to happen (i.e. to make them appear in the boundary but | |
1692 | with body streamed, so clone can me materialized). */ | |
d3f2e41e | 1693 | else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition) |
ddb3e20a JH |
1694 | return SYMBOL_EXTERNAL; |
1695 | ||
1696 | /* Linker discardable symbols are duplicated to every use unless they are | |
cf288ed3 | 1697 | keyed. */ |
d52f5295 ML |
1698 | if (DECL_ONE_ONLY (decl) |
1699 | && !force_output | |
1700 | && !forced_by_abi | |
1701 | && !used_from_object_file_p ()) | |
ddb3e20a JH |
1702 | return SYMBOL_DUPLICATE; |
1703 | ||
1704 | return SYMBOL_PARTITION; | |
1705 | } | |
89330618 JH |
1706 | |
1707 | /* Return true when symbol is known to be non-zero. */ | |
1708 | ||
1709 | bool | |
1710 | symtab_node::nonzero_address () | |
1711 | { | |
1712 | /* Weakrefs may be NULL when their target is not defined. */ | |
f7217cde | 1713 | if (alias && weakref) |
89330618 | 1714 | { |
f7217cde | 1715 | if (analyzed) |
89330618 | 1716 | { |
d52f5295 | 1717 | symtab_node *target = ultimate_alias_target (); |
89330618 JH |
1718 | |
1719 | if (target->alias && target->weakref) | |
1720 | return false; | |
1721 | /* We can not recurse to target::nonzero. It is possible that the | |
1722 | target is used only via the alias. | |
1723 | We may walk references and look for strong use, but we do not know | |
1724 | if this strong use will survive to final binary, so be | |
1725 | conservative here. | |
1726 | ??? Maybe we could do the lookup during late optimization that | |
1727 | could be useful to eliminate the NULL pointer checks in LTO | |
1728 | programs. */ | |
1729 | if (target->definition && !DECL_EXTERNAL (target->decl)) | |
f7217cde | 1730 | return true; |
89330618 JH |
1731 | if (target->resolution != LDPR_UNKNOWN |
1732 | && target->resolution != LDPR_UNDEF | |
1733 | && flag_delete_null_pointer_checks) | |
1734 | return true; | |
1735 | return false; | |
1736 | } | |
1737 | else | |
1738 | return false; | |
1739 | } | |
1740 | ||
1741 | /* With !flag_delete_null_pointer_checks we assume that symbols may | |
1742 | bind to NULL. This is on by default on embedded targets only. | |
1743 | ||
1744 | Otherwise all non-WEAK symbols must be defined and thus non-NULL or | |
1745 | linking fails. Important case of WEAK we want to do well are comdats. | |
1746 | Those are handled by later check for definition. | |
1747 | ||
1748 | When parsing, beware the cases when WEAK attribute is added later. */ | |
f7217cde JH |
1749 | if (!DECL_WEAK (decl) |
1750 | && flag_delete_null_pointer_checks) | |
1751 | { | |
1752 | refuse_visibility_changes = true; | |
1753 | return true; | |
1754 | } | |
89330618 JH |
1755 | |
1756 | /* If target is defined and not extern, we know it will be output and thus | |
1757 | it will bind to non-NULL. | |
1758 | Play safe for flag_delete_null_pointer_checks where weak definition maye | |
1759 | be re-defined by NULL. */ | |
f7217cde JH |
1760 | if (definition && !DECL_EXTERNAL (decl) |
1761 | && (flag_delete_null_pointer_checks || !DECL_WEAK (decl))) | |
1762 | { | |
1763 | if (!DECL_WEAK (decl)) | |
1764 | refuse_visibility_changes = true; | |
1765 | return true; | |
1766 | } | |
89330618 JH |
1767 | |
1768 | /* As the last resort, check the resolution info. */ | |
f7217cde JH |
1769 | if (resolution != LDPR_UNKNOWN |
1770 | && resolution != LDPR_UNDEF | |
89330618 JH |
1771 | && flag_delete_null_pointer_checks) |
1772 | return true; | |
1773 | return false; | |
1774 | } | |
3cb251b7 JH |
1775 | |
1776 | /* Return 0 if symbol is known to have different address than S2, | |
1777 | Return 1 if symbol is known to have same address as S2, | |
1778 | return 2 otherwise. */ | |
1779 | int | |
1780 | symtab_node::equal_address_to (symtab_node *s2) | |
1781 | { | |
1782 | enum availability avail1, avail2; | |
1783 | ||
1784 | /* A Shortcut: equivalent symbols are always equivalent. */ | |
1785 | if (this == s2) | |
1786 | return 1; | |
1787 | ||
1788 | /* For non-interposable aliases, lookup and compare their actual definitions. | |
1789 | Also check if the symbol needs to bind to given definition. */ | |
1790 | symtab_node *rs1 = ultimate_alias_target (&avail1); | |
1791 | symtab_node *rs2 = s2->ultimate_alias_target (&avail2); | |
1792 | bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl); | |
1793 | bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl); | |
1794 | bool really_binds_local1 = binds_local1; | |
1795 | bool really_binds_local2 = binds_local2; | |
1796 | ||
1797 | /* Addresses of vtables and virtual functions can not be used by user | |
1798 | code and are used only within speculation. In this case we may make | |
1799 | symbol equivalent to its alias even if interposition may break this | |
1800 | rule. Doing so will allow us to turn speculative inlining into | |
1801 | non-speculative more agressively. */ | |
1802 | if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE) | |
1803 | binds_local1 = true; | |
1804 | if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE) | |
1805 | binds_local2 = true; | |
1806 | ||
1807 | /* If both definitions are available we know that even if they are bound | |
1808 | to other unit they must be defined same way and therefore we can use | |
1809 | equivalence test. */ | |
1810 | if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE) | |
1811 | binds_local1 = binds_local2 = true; | |
1812 | ||
1813 | if ((binds_local1 ? rs1 : this) | |
1814 | == (binds_local2 ? rs2 : s2)) | |
1815 | { | |
1816 | /* We made use of the fact that alias is not weak. */ | |
1817 | if (binds_local1 && rs1 != this) | |
1818 | refuse_visibility_changes = true; | |
1819 | if (binds_local2 && rs2 != s2) | |
1820 | s2->refuse_visibility_changes = true; | |
1821 | return 1; | |
1822 | } | |
1823 | ||
1824 | /* If both symbols may resolve to NULL, we can not really prove them different. */ | |
1825 | if (!nonzero_address () && !s2->nonzero_address ()) | |
1826 | return 2; | |
1827 | ||
1828 | /* Except for NULL, functions and variables never overlap. */ | |
1829 | if (TREE_CODE (decl) != TREE_CODE (s2->decl)) | |
1830 | return 0; | |
1831 | ||
1832 | /* If one of the symbols is unresolved alias, punt. */ | |
1833 | if (rs1->alias || rs2->alias) | |
1834 | return 2; | |
1835 | ||
1836 | /* If we have a non-interposale definition of at least one of the symbols | |
1837 | and the other symbol is different, we know other unit can not interpose | |
1838 | it to the first symbol; all aliases of the definition needs to be | |
1839 | present in the current unit. */ | |
1840 | if (((really_binds_local1 || really_binds_local2) | |
1841 | /* If we have both definitions and they are different, we know they | |
1842 | will be different even in units they binds to. */ | |
1843 | || (binds_local1 && binds_local2)) | |
1844 | && rs1 != rs2) | |
1845 | { | |
1846 | /* We make use of the fact that one symbol is not alias of the other | |
1847 | and that the definition is non-interposable. */ | |
1848 | refuse_visibility_changes = true; | |
1849 | s2->refuse_visibility_changes = true; | |
1850 | rs1->refuse_visibility_changes = true; | |
1851 | rs2->refuse_visibility_changes = true; | |
1852 | return 0; | |
1853 | } | |
1854 | ||
1855 | /* TODO: Alias oracle basically assume that addresses of global variables | |
1856 | are different unless they are declared as alias of one to another. | |
1857 | We probably should be consistent and use this fact here, too, and update | |
1858 | alias oracle to use this predicate. */ | |
1859 | ||
1860 | return 2; | |
1861 | } | |
31de7606 JH |
1862 | |
1863 | /* Worker for call_for_symbol_and_aliases. */ | |
1864 | ||
1865 | bool | |
1866 | symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *, | |
1867 | void *), | |
1868 | void *data, | |
1869 | bool include_overwritable) | |
1870 | { | |
1871 | ipa_ref *ref; | |
1872 | FOR_EACH_ALIAS (this, ref) | |
1873 | { | |
1874 | symtab_node *alias = ref->referring; | |
1875 | if (include_overwritable | |
1876 | || alias->get_availability () > AVAIL_INTERPOSABLE) | |
1877 | if (alias->call_for_symbol_and_aliases (callback, data, | |
1878 | include_overwritable)) | |
1879 | return true; | |
1880 | } | |
1881 | return false; | |
1882 | } | |
0a7246ee JH |
1883 | |
1884 | /* Return ture if address of N is possibly compared. */ | |
1885 | ||
1886 | static bool | |
1887 | address_matters_1 (symtab_node *n, void *) | |
1888 | { | |
1889 | struct ipa_ref *ref; | |
1890 | ||
1891 | if (!n->address_can_be_compared_p ()) | |
1892 | return false; | |
1893 | if (n->externally_visible || n->force_output) | |
1894 | return true; | |
1895 | ||
1896 | for (unsigned int i = 0; n->iterate_referring (i, ref); i++) | |
1897 | if (ref->address_matters_p ()) | |
1898 | return true; | |
1899 | return false; | |
1900 | } | |
1901 | ||
1902 | /* Return true if symbol's address may possibly be compared to other | |
1903 | symbol's address. */ | |
1904 | ||
1905 | bool | |
1906 | symtab_node::address_matters_p () | |
1907 | { | |
1908 | gcc_assert (!alias); | |
1909 | return call_for_symbol_and_aliases (address_matters_1, NULL, true); | |
1910 | } | |
428f0c67 JH |
1911 | |
1912 | /* Return ture if symbol's alignment may be increased. */ | |
1913 | ||
1914 | bool | |
1915 | symtab_node::can_increase_alignment_p (void) | |
1916 | { | |
1917 | symtab_node *target = ultimate_alias_target (); | |
1918 | ||
1919 | /* For now support only variables. */ | |
1920 | if (TREE_CODE (decl) != VAR_DECL) | |
1921 | return false; | |
1922 | ||
1923 | /* With -fno-toplevel-reorder we may have already output the constant. */ | |
1924 | if (TREE_ASM_WRITTEN (target->decl)) | |
1925 | return false; | |
1926 | ||
caf2df93 JH |
1927 | /* If target is already placed in an anchor, we can not touch its |
1928 | alignment. */ | |
1929 | if (DECL_RTL_SET_P (target->decl) | |
1930 | && MEM_P (DECL_RTL (target->decl)) | |
1931 | && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0))) | |
1932 | return false; | |
1933 | ||
428f0c67 JH |
1934 | /* Constant pool entries may be shared. */ |
1935 | if (DECL_IN_CONSTANT_POOL (target->decl)) | |
1936 | return false; | |
1937 | ||
1938 | /* We cannot change alignment of symbols that may bind to symbols | |
1939 | in other translation unit that may contain a definition with lower | |
1940 | alignment. */ | |
1941 | if (!decl_binds_to_current_def_p (decl)) | |
1942 | return false; | |
1943 | ||
1944 | /* When compiling partition, be sure the symbol is not output by other | |
1945 | partition. */ | |
1946 | if (flag_ltrans | |
1947 | && (target->in_other_partition | |
1948 | || target->get_partitioning_class () == SYMBOL_DUPLICATE)) | |
1949 | return false; | |
1950 | ||
1951 | /* Do not override the alignment as specified by the ABI when the used | |
1952 | attribute is set. */ | |
1953 | if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl)) | |
1954 | return false; | |
1955 | ||
1956 | /* Do not override explicit alignment set by the user when an explicit | |
1957 | section name is also used. This is a common idiom used by many | |
1958 | software projects. */ | |
1959 | if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section) | |
1960 | return false; | |
1961 | ||
1962 | return true; | |
1963 | } | |
1964 | ||
1965 | /* Worker for symtab_node::increase_alignment. */ | |
1966 | ||
1967 | static bool | |
1968 | increase_alignment_1 (symtab_node *n, void *v) | |
1969 | { | |
1970 | unsigned int align = (size_t)v; | |
1971 | if (DECL_ALIGN (n->decl) < align | |
1972 | && n->can_increase_alignment_p ()) | |
1973 | { | |
1974 | DECL_ALIGN (n->decl) = align; | |
1975 | DECL_USER_ALIGN (n->decl) = 1; | |
1976 | } | |
1977 | return false; | |
1978 | } | |
1979 | ||
1980 | /* Increase alignment of THIS to ALIGN. */ | |
1981 | ||
1982 | void | |
1983 | symtab_node::increase_alignment (unsigned int align) | |
1984 | { | |
1985 | gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT); | |
1986 | ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1, | |
1987 | (void *)(size_t) align, | |
1988 | true); | |
1989 | gcc_assert (DECL_ALIGN (decl) >= align); | |
1990 | } | |
1991 | ||
1992 | /* Helper for symtab_node::definition_alignment. */ | |
1993 | ||
1994 | static bool | |
1995 | get_alignment_1 (symtab_node *n, void *v) | |
1996 | { | |
1997 | *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl)); | |
1998 | return false; | |
1999 | } | |
2000 | ||
2001 | /* Return desired alignment of the definition. This is NOT alignment useful | |
2002 | to access THIS, because THIS may be interposable and DECL_ALIGN should | |
2003 | be used instead. It however must be guaranteed when output definition | |
2004 | of THIS. */ | |
2005 | ||
2006 | unsigned int | |
2007 | symtab_node::definition_alignment () | |
2008 | { | |
2009 | unsigned int align = 0; | |
2010 | gcc_assert (!alias); | |
2011 | call_for_symbol_and_aliases (get_alignment_1, &align, true); | |
2012 | return align; | |
2013 | } |