]>
Commit | Line | Data |
---|---|---|
7bfefa9d | 1 | /* LTO symbol table. |
92468061 | 2 | Copyright 2009, 2010 Free Software Foundation, Inc. |
7bfefa9d | 3 | Contributed by CodeSourcery, Inc. |
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" | |
0b205f4c | 24 | #include "diagnostic-core.h" |
7bfefa9d | 25 | #include "tree.h" |
26 | #include "gimple.h" | |
ba72912a | 27 | #include "ggc.h" |
7bfefa9d | 28 | #include "hashtab.h" |
29 | #include "plugin-api.h" | |
30 | #include "lto-streamer.h" | |
31 | ||
32 | /* Vector to keep track of external variables we've seen so far. */ | |
33 | VEC(tree,gc) *lto_global_var_decls; | |
34 | ||
fd193bcd | 35 | /* Symbol table entry. */ |
7bfefa9d | 36 | |
fd193bcd | 37 | struct GTY(()) lto_symtab_entry_def |
7bfefa9d | 38 | { |
fd193bcd | 39 | /* The symbol table entry key, an IDENTIFIER. */ |
40 | tree id; | |
41 | /* The symbol table entry, a DECL. */ | |
7bfefa9d | 42 | tree decl; |
21ce3cc7 | 43 | /* The cgraph node if decl is a function decl. Filled in during the |
44 | merging process. */ | |
45 | struct cgraph_node *node; | |
0cddb138 | 46 | /* The varpool node if decl is a variable decl. Filled in during the |
47 | merging process. */ | |
48 | struct varpool_node *vnode; | |
fd193bcd | 49 | /* LTO file-data and symbol resolution for this decl. */ |
7bfefa9d | 50 | struct lto_file_decl_data * GTY((skip (""))) file_data; |
fd193bcd | 51 | enum ld_plugin_symbol_resolution resolution; |
9ced88d0 | 52 | /* True when resolution was guessed and not read from the file. */ |
53 | bool guessed; | |
fd193bcd | 54 | /* Pointer to the next entry with the same key. Before decl merging |
55 | this links all symbols from the different TUs. After decl merging | |
56 | this links merged but incompatible decls, thus all prevailing ones | |
57 | remaining. */ | |
58 | struct lto_symtab_entry_def *next; | |
7bfefa9d | 59 | }; |
fd193bcd | 60 | typedef struct lto_symtab_entry_def *lto_symtab_entry_t; |
7bfefa9d | 61 | |
62 | /* A poor man's symbol table. This hashes identifier to prevailing DECL | |
63 | if there is one. */ | |
64 | ||
fd193bcd | 65 | static GTY ((if_marked ("lto_symtab_entry_marked_p"), |
66 | param_is (struct lto_symtab_entry_def))) | |
7bfefa9d | 67 | htab_t lto_symtab_identifiers; |
68 | ||
25429dc2 | 69 | /* Free symtab hashtable. */ |
70 | ||
71 | void | |
72 | lto_symtab_free (void) | |
73 | { | |
74 | htab_delete (lto_symtab_identifiers); | |
75 | lto_symtab_identifiers = NULL; | |
76 | } | |
77 | ||
fd193bcd | 78 | /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */ |
7bfefa9d | 79 | |
80 | static hashval_t | |
fd193bcd | 81 | lto_symtab_entry_hash (const void *p) |
7bfefa9d | 82 | { |
fd193bcd | 83 | const struct lto_symtab_entry_def *base = |
84 | (const struct lto_symtab_entry_def *) p; | |
4bdd2942 | 85 | return IDENTIFIER_HASH_VALUE (base->id); |
7bfefa9d | 86 | } |
87 | ||
fd193bcd | 88 | /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs |
89 | corresponding to the same symbol. */ | |
7bfefa9d | 90 | |
91 | static int | |
fd193bcd | 92 | lto_symtab_entry_eq (const void *p1, const void *p2) |
7bfefa9d | 93 | { |
fd193bcd | 94 | const struct lto_symtab_entry_def *base1 = |
95 | (const struct lto_symtab_entry_def *) p1; | |
96 | const struct lto_symtab_entry_def *base2 = | |
97 | (const struct lto_symtab_entry_def *) p2; | |
98 | return (base1->id == base2->id); | |
7bfefa9d | 99 | } |
100 | ||
fd193bcd | 101 | /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs |
48e1416a | 102 | to be marked for GC. */ |
7bfefa9d | 103 | |
104 | static int | |
fd193bcd | 105 | lto_symtab_entry_marked_p (const void *p) |
7bfefa9d | 106 | { |
fd193bcd | 107 | const struct lto_symtab_entry_def *base = |
108 | (const struct lto_symtab_entry_def *) p; | |
7bfefa9d | 109 | |
0fa4671b | 110 | /* Keep this only if the common IDENTIFIER_NODE of the symtab chain |
111 | is marked which it will be if at least one of the DECLs in the | |
112 | chain is marked. */ | |
113 | return ggc_marked_p (base->id); | |
7bfefa9d | 114 | } |
115 | ||
7bfefa9d | 116 | /* Lazily initialize resolution hash tables. */ |
117 | ||
118 | static void | |
fd193bcd | 119 | lto_symtab_maybe_init_hash_table (void) |
7bfefa9d | 120 | { |
fd193bcd | 121 | if (lto_symtab_identifiers) |
122 | return; | |
123 | ||
124 | lto_symtab_identifiers = | |
125 | htab_create_ggc (1021, lto_symtab_entry_hash, | |
126 | lto_symtab_entry_eq, NULL); | |
7bfefa9d | 127 | } |
128 | ||
bc0ed27c | 129 | /* Registers DECL with the LTO symbol table as having resolution RESOLUTION |
130 | and read from FILE_DATA. */ | |
131 | ||
132 | void | |
133 | lto_symtab_register_decl (tree decl, | |
134 | ld_plugin_symbol_resolution_t resolution, | |
135 | struct lto_file_decl_data *file_data) | |
136 | { | |
137 | lto_symtab_entry_t new_entry; | |
138 | void **slot; | |
139 | ||
140 | /* Check that declarations reaching this function do not have | |
141 | properties inconsistent with having external linkage. If any of | |
142 | these asertions fail, then the object file reader has failed to | |
143 | detect these cases and issue appropriate error messages. */ | |
144 | gcc_assert (decl | |
145 | && TREE_PUBLIC (decl) | |
146 | && (TREE_CODE (decl) == VAR_DECL | |
147 | || TREE_CODE (decl) == FUNCTION_DECL) | |
148 | && DECL_ASSEMBLER_NAME_SET_P (decl)); | |
149 | if (TREE_CODE (decl) == VAR_DECL | |
150 | && DECL_INITIAL (decl)) | |
151 | gcc_assert (!DECL_EXTERNAL (decl) | |
152 | || (TREE_STATIC (decl) && TREE_READONLY (decl))); | |
153 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
154 | gcc_assert (!DECL_ABSTRACT (decl)); | |
155 | ||
ba72912a | 156 | new_entry = ggc_alloc_cleared_lto_symtab_entry_def (); |
d86d364d | 157 | new_entry->id = (*targetm.asm_out.mangle_assembler_name) |
158 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))); | |
bc0ed27c | 159 | new_entry->decl = decl; |
160 | new_entry->resolution = resolution; | |
161 | new_entry->file_data = file_data; | |
162 | ||
163 | lto_symtab_maybe_init_hash_table (); | |
164 | slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT); | |
165 | new_entry->next = (lto_symtab_entry_t) *slot; | |
166 | *slot = new_entry; | |
167 | } | |
168 | ||
169 | /* Get the lto_symtab_entry_def struct associated with ID | |
170 | if there is one. */ | |
171 | ||
172 | static lto_symtab_entry_t | |
173 | lto_symtab_get (tree id) | |
174 | { | |
175 | struct lto_symtab_entry_def temp; | |
176 | void **slot; | |
177 | ||
178 | lto_symtab_maybe_init_hash_table (); | |
179 | temp.id = id; | |
180 | slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT); | |
181 | return slot ? (lto_symtab_entry_t) *slot : NULL; | |
182 | } | |
183 | ||
184 | /* Get the linker resolution for DECL. */ | |
185 | ||
186 | enum ld_plugin_symbol_resolution | |
187 | lto_symtab_get_resolution (tree decl) | |
188 | { | |
189 | lto_symtab_entry_t e; | |
190 | ||
191 | gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); | |
192 | ||
d86d364d | 193 | e = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name) |
194 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)))); | |
bc0ed27c | 195 | while (e && e->decl != decl) |
196 | e = e->next; | |
197 | if (!e) | |
198 | return LDPR_UNKNOWN; | |
199 | ||
200 | return e->resolution; | |
201 | } | |
202 | ||
203 | ||
bc0ed27c | 204 | /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging |
205 | all edges and removing the old node. */ | |
7bfefa9d | 206 | |
bc0ed27c | 207 | static void |
208 | lto_cgraph_replace_node (struct cgraph_node *node, | |
209 | struct cgraph_node *prevailing_node) | |
7bfefa9d | 210 | { |
bc0ed27c | 211 | struct cgraph_edge *e, *next; |
fa7a183a | 212 | bool compatible_p; |
0f4e132d | 213 | |
214 | if (cgraph_dump_file) | |
215 | { | |
216 | fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i" | |
217 | " for symbol %s\n", | |
218 | cgraph_node_name (node), node->uid, | |
219 | cgraph_node_name (prevailing_node), | |
220 | prevailing_node->uid, | |
d86d364d | 221 | IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) |
7d0d0ce1 | 222 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl))))); |
0f4e132d | 223 | } |
224 | ||
bc0ed27c | 225 | /* Merge node flags. */ |
226 | if (node->needed) | |
227 | cgraph_mark_needed_node (prevailing_node); | |
228 | if (node->reachable) | |
229 | cgraph_mark_reachable_node (prevailing_node); | |
7d0d0ce1 | 230 | if (node->symbol.address_taken) |
7bfefa9d | 231 | { |
bc0ed27c | 232 | gcc_assert (!prevailing_node->global.inlined_to); |
233 | cgraph_mark_address_taken_node (prevailing_node); | |
234 | } | |
7bfefa9d | 235 | |
bc0ed27c | 236 | /* Redirect all incoming edges. */ |
fa7a183a | 237 | compatible_p |
7d0d0ce1 | 238 | = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->symbol.decl)), |
239 | TREE_TYPE (TREE_TYPE (node->symbol.decl))); | |
bc0ed27c | 240 | for (e = node->callers; e; e = next) |
241 | { | |
242 | next = e->next_caller; | |
243 | cgraph_redirect_edge_callee (e, prevailing_node); | |
fa7a183a | 244 | /* If there is a mismatch between the supposed callee return type and |
245 | the real one do not attempt to inline this function. | |
246 | ??? We really need a way to match function signatures for ABI | |
247 | compatibility and perform related promotions at inlining time. */ | |
248 | if (!compatible_p) | |
249 | e->call_stmt_cannot_inline_p = 1; | |
bc0ed27c | 250 | } |
8d810329 | 251 | /* Redirect incomming references. */ |
04ec15fa | 252 | ipa_clone_referring ((symtab_node)prevailing_node, &node->symbol.ref_list); |
7bfefa9d | 253 | |
bc0ed27c | 254 | /* Finally remove the replaced node. */ |
c70f46b0 | 255 | cgraph_remove_node (node); |
bc0ed27c | 256 | } |
257 | ||
0cddb138 | 258 | /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging |
259 | all edges and removing the old node. */ | |
260 | ||
261 | static void | |
262 | lto_varpool_replace_node (struct varpool_node *vnode, | |
263 | struct varpool_node *prevailing_node) | |
264 | { | |
265 | /* Merge node flags. */ | |
266 | if (vnode->needed) | |
267 | { | |
b55bfc2f | 268 | gcc_assert (!vnode->analyzed || prevailing_node->analyzed); |
0cddb138 | 269 | varpool_mark_needed_node (prevailing_node); |
270 | } | |
271 | gcc_assert (!vnode->finalized || prevailing_node->finalized); | |
272 | gcc_assert (!vnode->analyzed || prevailing_node->analyzed); | |
273 | ||
04ec15fa | 274 | ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list); |
8d810329 | 275 | |
6f932b06 | 276 | /* Be sure we can garbage collect the initializer. */ |
7d0d0ce1 | 277 | if (DECL_INITIAL (vnode->symbol.decl)) |
278 | DECL_INITIAL (vnode->symbol.decl) = error_mark_node; | |
0cddb138 | 279 | /* Finally remove the replaced node. */ |
280 | varpool_remove_node (vnode); | |
281 | } | |
282 | ||
bc0ed27c | 283 | /* Merge two variable or function symbol table entries PREVAILING and ENTRY. |
284 | Return false if the symbols are not fully compatible and a diagnostic | |
285 | should be emitted. */ | |
286 | ||
287 | static bool | |
288 | lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry) | |
289 | { | |
290 | tree prevailing_decl = prevailing->decl; | |
291 | tree decl = entry->decl; | |
292 | tree prevailing_type, type; | |
bc0ed27c | 293 | |
294 | /* Merge decl state in both directions, we may still end up using | |
295 | the new decl. */ | |
296 | TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl); | |
297 | TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl); | |
298 | ||
bc0ed27c | 299 | /* The linker may ask us to combine two incompatible symbols. |
300 | Detect this case and notify the caller of required diagnostics. */ | |
301 | ||
302 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
fdd4f660 | 303 | { |
79e191db | 304 | if (!types_compatible_p (TREE_TYPE (prevailing_decl), |
305 | TREE_TYPE (decl))) | |
fdd4f660 | 306 | /* If we don't have a merged type yet...sigh. The linker |
307 | wouldn't complain if the types were mismatched, so we | |
308 | probably shouldn't either. Just use the type from | |
309 | whichever decl appears to be associated with the | |
310 | definition. If for some odd reason neither decl is, the | |
311 | older one wins. */ | |
312 | (void) 0; | |
313 | ||
314 | return true; | |
315 | } | |
316 | ||
317 | /* Now we exclusively deal with VAR_DECLs. */ | |
318 | ||
66bd377c | 319 | /* Sharing a global symbol is a strong hint that two types are |
320 | compatible. We could use this information to complete | |
321 | incomplete pointed-to types more aggressively here, ignoring | |
322 | mismatches in both field and tag names. It's difficult though | |
323 | to guarantee that this does not have side-effects on merging | |
324 | more compatible types from other translation units though. */ | |
7bfefa9d | 325 | |
fdd4f660 | 326 | /* We can tolerate differences in type qualification, the |
66bd377c | 327 | qualification of the prevailing definition will prevail. |
328 | ??? In principle we might want to only warn for structurally | |
329 | incompatible types here, but unless we have protective measures | |
330 | for TBAA in place that would hide useful information. */ | |
bc0ed27c | 331 | prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl)); |
332 | type = TYPE_MAIN_VARIANT (TREE_TYPE (decl)); | |
66bd377c | 333 | |
79e191db | 334 | if (!types_compatible_p (prevailing_type, type)) |
66bd377c | 335 | { |
336 | if (COMPLETE_TYPE_P (type)) | |
337 | return false; | |
338 | ||
339 | /* If type is incomplete then avoid warnings in the cases | |
340 | that TBAA handles just fine. */ | |
341 | ||
342 | if (TREE_CODE (prevailing_type) != TREE_CODE (type)) | |
343 | return false; | |
344 | ||
345 | if (TREE_CODE (prevailing_type) == ARRAY_TYPE) | |
346 | { | |
347 | tree tem1 = TREE_TYPE (prevailing_type); | |
348 | tree tem2 = TREE_TYPE (type); | |
349 | while (TREE_CODE (tem1) == ARRAY_TYPE | |
350 | && TREE_CODE (tem2) == ARRAY_TYPE) | |
351 | { | |
352 | tem1 = TREE_TYPE (tem1); | |
353 | tem2 = TREE_TYPE (tem2); | |
354 | } | |
355 | ||
356 | if (TREE_CODE (tem1) != TREE_CODE (tem2)) | |
357 | return false; | |
358 | ||
79e191db | 359 | if (!types_compatible_p (tem1, tem2)) |
66bd377c | 360 | return false; |
361 | } | |
362 | ||
363 | /* Fallthru. Compatible enough. */ | |
364 | } | |
7bfefa9d | 365 | |
fdd4f660 | 366 | /* ??? We might want to emit a warning here if type qualification |
367 | differences were spotted. Do not do this unconditionally though. */ | |
7bfefa9d | 368 | |
fdd4f660 | 369 | /* There is no point in comparing too many details of the decls here. |
370 | The type compatibility checks or the completing of types has properly | |
371 | dealt with most issues. */ | |
7bfefa9d | 372 | |
fdd4f660 | 373 | /* The following should all not invoke fatal errors as in non-LTO |
374 | mode the linker wouldn't complain either. Just emit warnings. */ | |
7bfefa9d | 375 | |
fdd4f660 | 376 | /* Report a warning if user-specified alignments do not match. */ |
bc0ed27c | 377 | if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl)) |
378 | && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl)) | |
379 | return false; | |
7bfefa9d | 380 | |
7bfefa9d | 381 | return true; |
382 | } | |
383 | ||
bc0ed27c | 384 | /* Return true if the symtab entry E can be replaced by another symtab |
385 | entry. */ | |
7bfefa9d | 386 | |
bc0ed27c | 387 | static bool |
388 | lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e) | |
7bfefa9d | 389 | { |
bc0ed27c | 390 | if (DECL_EXTERNAL (e->decl) |
391 | || DECL_COMDAT (e->decl) | |
aedd1301 | 392 | || DECL_ONE_ONLY (e->decl) |
bc0ed27c | 393 | || DECL_WEAK (e->decl)) |
394 | return true; | |
7bfefa9d | 395 | |
bc0ed27c | 396 | if (TREE_CODE (e->decl) == VAR_DECL) |
397 | return (DECL_COMMON (e->decl) | |
398 | || (!flag_no_common && !DECL_INITIAL (e->decl))); | |
7bfefa9d | 399 | |
bc0ed27c | 400 | return false; |
fd193bcd | 401 | } |
7bfefa9d | 402 | |
bc0ed27c | 403 | /* Return true if the symtab entry E can be the prevailing one. */ |
7bfefa9d | 404 | |
bc0ed27c | 405 | static bool |
406 | lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e) | |
fd193bcd | 407 | { |
d91c3d40 | 408 | /* The C++ frontend ends up neither setting TREE_STATIC nor |
409 | DECL_EXTERNAL on virtual methods but only TREE_PUBLIC. | |
410 | So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */ | |
411 | if (DECL_EXTERNAL (e->decl)) | |
bc0ed27c | 412 | return false; |
7bfefa9d | 413 | |
bc0ed27c | 414 | /* For functions we need a non-discarded body. */ |
415 | if (TREE_CODE (e->decl) == FUNCTION_DECL) | |
c70f46b0 | 416 | return (e->node && e->node->analyzed); |
7bfefa9d | 417 | |
bc0ed27c | 418 | else if (TREE_CODE (e->decl) == VAR_DECL) |
9269f553 | 419 | { |
420 | if (!e->vnode) | |
421 | return false; | |
e0eaac80 | 422 | return e->vnode->finalized; |
9269f553 | 423 | } |
7bfefa9d | 424 | |
bc0ed27c | 425 | gcc_unreachable (); |
7bfefa9d | 426 | } |
427 | ||
fd193bcd | 428 | /* Resolve the symbol with the candidates in the chain *SLOT and store |
429 | their resolutions. */ | |
7bfefa9d | 430 | |
fd193bcd | 431 | static void |
432 | lto_symtab_resolve_symbols (void **slot) | |
433 | { | |
67b2bb71 | 434 | lto_symtab_entry_t e; |
bc0ed27c | 435 | lto_symtab_entry_t prevailing = NULL; |
7bfefa9d | 436 | |
67b2bb71 | 437 | /* Always set e->node so that edges are updated to reflect decl merging. */ |
438 | for (e = (lto_symtab_entry_t) *slot; e; e = e->next) | |
439 | { | |
440 | if (TREE_CODE (e->decl) == FUNCTION_DECL) | |
7f70d5bc | 441 | e->node = cgraph_get_node (e->decl); |
0cddb138 | 442 | else if (TREE_CODE (e->decl) == VAR_DECL) |
4fc1631a | 443 | e->vnode = varpool_get_node (e->decl); |
da722561 | 444 | if (e->resolution == LDPR_PREVAILING_DEF_IRONLY |
445 | || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP | |
446 | || e->resolution == LDPR_PREVAILING_DEF) | |
447 | prevailing = e; | |
67b2bb71 | 448 | } |
449 | ||
67b2bb71 | 450 | /* If the chain is already resolved there is nothing else to do. */ |
da722561 | 451 | if (prevailing) |
fd193bcd | 452 | return; |
453 | ||
bc0ed27c | 454 | /* Find the single non-replaceable prevailing symbol and |
455 | diagnose ODR violations. */ | |
67b2bb71 | 456 | for (e = (lto_symtab_entry_t) *slot; e; e = e->next) |
fd193bcd | 457 | { |
bc0ed27c | 458 | if (!lto_symtab_resolve_can_prevail_p (e)) |
459 | { | |
460 | e->resolution = LDPR_RESOLVED_IR; | |
9ced88d0 | 461 | e->guessed = true; |
bc0ed27c | 462 | continue; |
463 | } | |
464 | ||
465 | /* Set a default resolution - the final prevailing one will get | |
466 | adjusted later. */ | |
467 | e->resolution = LDPR_PREEMPTED_IR; | |
9ced88d0 | 468 | e->guessed = true; |
bc0ed27c | 469 | if (!lto_symtab_resolve_replaceable_p (e)) |
470 | { | |
471 | if (prevailing) | |
472 | { | |
473 | error_at (DECL_SOURCE_LOCATION (e->decl), | |
474 | "%qD has already been defined", e->decl); | |
475 | inform (DECL_SOURCE_LOCATION (prevailing->decl), | |
476 | "previously defined here"); | |
477 | } | |
478 | prevailing = e; | |
479 | } | |
480 | } | |
481 | if (prevailing) | |
482 | goto found; | |
483 | ||
484 | /* Do a second round choosing one from the replaceable prevailing decls. */ | |
485 | for (e = (lto_symtab_entry_t) *slot; e; e = e->next) | |
486 | { | |
487 | if (e->resolution != LDPR_PREEMPTED_IR) | |
488 | continue; | |
489 | ||
490 | /* Choose the first function that can prevail as prevailing. */ | |
491 | if (TREE_CODE (e->decl) == FUNCTION_DECL) | |
fd193bcd | 492 | { |
bc0ed27c | 493 | prevailing = e; |
494 | break; | |
fd193bcd | 495 | } |
bc0ed27c | 496 | |
497 | /* From variables that can prevail choose the largest one. */ | |
498 | if (!prevailing | |
499 | || tree_int_cst_lt (DECL_SIZE (prevailing->decl), | |
500 | DECL_SIZE (e->decl))) | |
501 | prevailing = e; | |
fd193bcd | 502 | } |
bc0ed27c | 503 | |
504 | if (!prevailing) | |
505 | return; | |
506 | ||
507 | found: | |
61a3c321 | 508 | /* If current lto files represent the whole program, |
509 | it is correct to use LDPR_PREVALING_DEF_IRONLY. | |
510 | If current lto files are part of whole program, internal | |
511 | resolver doesn't know if it is LDPR_PREVAILING_DEF | |
512 | or LDPR_PREVAILING_DEF_IRONLY. Use IRONLY conforms to | |
513 | using -fwhole-program. Otherwise, it doesn't | |
514 | matter using either LDPR_PREVAILING_DEF or | |
515 | LDPR_PREVAILING_DEF_IRONLY | |
516 | ||
517 | FIXME: above workaround due to gold plugin makes some | |
518 | variables IRONLY, which are indeed PREVAILING_DEF in | |
519 | resolution file. These variables still need manual | |
520 | externally_visible attribute. */ | |
bc0ed27c | 521 | prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY; |
9ced88d0 | 522 | prevailing->guessed = true; |
fd193bcd | 523 | } |
524 | ||
bc0ed27c | 525 | /* Merge all decls in the symbol table chain to the prevailing decl and |
1b9e1cc0 | 526 | issue diagnostics about type mismatches. If DIAGNOSED_P is true |
527 | do not issue further diagnostics.*/ | |
fd193bcd | 528 | |
529 | static void | |
1b9e1cc0 | 530 | lto_symtab_merge_decls_2 (void **slot, bool diagnosed_p) |
fd193bcd | 531 | { |
bc0ed27c | 532 | lto_symtab_entry_t prevailing, e; |
533 | VEC(tree, heap) *mismatches = NULL; | |
534 | unsigned i; | |
535 | tree decl; | |
fd193bcd | 536 | |
537 | /* Nothing to do for a single entry. */ | |
bc0ed27c | 538 | prevailing = (lto_symtab_entry_t) *slot; |
539 | if (!prevailing->next) | |
fd193bcd | 540 | return; |
541 | ||
bc0ed27c | 542 | /* Try to merge each entry with the prevailing one. */ |
543 | for (e = prevailing->next; e; e = e->next) | |
544 | { | |
1b9e1cc0 | 545 | if (!lto_symtab_merge (prevailing, e) |
546 | && !diagnosed_p) | |
bc0ed27c | 547 | VEC_safe_push (tree, heap, mismatches, e->decl); |
548 | } | |
549 | if (VEC_empty (tree, mismatches)) | |
550 | return; | |
fd193bcd | 551 | |
bc0ed27c | 552 | /* Diagnose all mismatched re-declarations. */ |
48148244 | 553 | FOR_EACH_VEC_ELT (tree, mismatches, i, decl) |
fd193bcd | 554 | { |
79e191db | 555 | if (!types_compatible_p (TREE_TYPE (prevailing->decl), TREE_TYPE (decl))) |
bc0ed27c | 556 | diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0, |
557 | "type of %qD does not match original " | |
558 | "declaration", decl); | |
559 | ||
560 | else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl)) | |
561 | && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl)) | |
fd193bcd | 562 | { |
bc0ed27c | 563 | diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0, |
564 | "alignment of %qD is bigger than " | |
565 | "original declaration", decl); | |
fd193bcd | 566 | } |
fd193bcd | 567 | } |
bc0ed27c | 568 | if (diagnosed_p) |
569 | inform (DECL_SOURCE_LOCATION (prevailing->decl), | |
570 | "previously declared here"); | |
fd193bcd | 571 | |
bc0ed27c | 572 | VEC_free (tree, heap, mismatches); |
fd193bcd | 573 | } |
574 | ||
575 | /* Helper to process the decl chain for the symbol table entry *SLOT. */ | |
576 | ||
577 | static int | |
578 | lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED) | |
7bfefa9d | 579 | { |
bc0ed27c | 580 | lto_symtab_entry_t e, prevailing; |
581 | bool diagnosed_p = false; | |
fd193bcd | 582 | |
bc0ed27c | 583 | /* Compute the symbol resolutions. This is a no-op when using the |
584 | linker plugin. */ | |
fd193bcd | 585 | lto_symtab_resolve_symbols (slot); |
586 | ||
bc0ed27c | 587 | /* Find the prevailing decl. */ |
588 | for (prevailing = (lto_symtab_entry_t) *slot; | |
589 | prevailing | |
590 | && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY | |
da722561 | 591 | && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP |
bc0ed27c | 592 | && prevailing->resolution != LDPR_PREVAILING_DEF; |
593 | prevailing = prevailing->next) | |
594 | ; | |
595 | ||
596 | /* Assert it's the only one. */ | |
597 | if (prevailing) | |
598 | for (e = prevailing->next; e; e = e->next) | |
12283fd1 | 599 | { |
600 | if (e->resolution == LDPR_PREVAILING_DEF_IRONLY | |
da722561 | 601 | || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP |
12283fd1 | 602 | || e->resolution == LDPR_PREVAILING_DEF) |
603 | fatal_error ("multiple prevailing defs for %qE", | |
604 | DECL_NAME (prevailing->decl)); | |
605 | } | |
bc0ed27c | 606 | |
607 | /* If there's not a prevailing symbol yet it's an external reference. | |
608 | Happens a lot during ltrans. Choose the first symbol with a | |
609 | cgraph or a varpool node. */ | |
610 | if (!prevailing) | |
611 | { | |
612 | prevailing = (lto_symtab_entry_t) *slot; | |
613 | /* For functions choose one with a cgraph node. */ | |
614 | if (TREE_CODE (prevailing->decl) == FUNCTION_DECL) | |
21ce3cc7 | 615 | while (!prevailing->node |
bc0ed27c | 616 | && prevailing->next) |
617 | prevailing = prevailing->next; | |
9269f553 | 618 | /* For variables chose with a priority variant with vnode |
619 | attached (i.e. from unit where external declaration of | |
620 | variable is actually used). | |
621 | When there are multiple variants, chose one with size. | |
622 | This is needed for C++ typeinfos, for example in | |
623 | lto/20081204-1 there are typeifos in both units, just | |
624 | one of them do have size. */ | |
0cddb138 | 625 | if (TREE_CODE (prevailing->decl) == VAR_DECL) |
9269f553 | 626 | { |
627 | for (e = prevailing->next; e; e = e->next) | |
628 | if ((!prevailing->vnode && e->vnode) | |
629 | || ((prevailing->vnode != NULL) == (e->vnode != NULL) | |
630 | && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl)) | |
631 | && COMPLETE_TYPE_P (TREE_TYPE (e->decl)))) | |
632 | prevailing = e; | |
633 | } | |
bc0ed27c | 634 | } |
fd193bcd | 635 | |
bc0ed27c | 636 | /* Move it first in the list. */ |
637 | if ((lto_symtab_entry_t) *slot != prevailing) | |
638 | { | |
639 | for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next) | |
640 | ; | |
641 | e->next = prevailing->next; | |
642 | prevailing->next = (lto_symtab_entry_t) *slot; | |
643 | *slot = (void *) prevailing; | |
644 | } | |
fd193bcd | 645 | |
bc0ed27c | 646 | /* Record the prevailing variable. */ |
647 | if (TREE_CODE (prevailing->decl) == VAR_DECL) | |
648 | VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl); | |
fd193bcd | 649 | |
bc0ed27c | 650 | /* Diagnose mismatched objects. */ |
651 | for (e = prevailing->next; e; e = e->next) | |
652 | { | |
653 | if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl)) | |
654 | continue; | |
fd193bcd | 655 | |
bc0ed27c | 656 | switch (TREE_CODE (prevailing->decl)) |
657 | { | |
658 | case VAR_DECL: | |
659 | gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL); | |
660 | error_at (DECL_SOURCE_LOCATION (e->decl), | |
661 | "variable %qD redeclared as function", prevailing->decl); | |
662 | break; | |
663 | ||
664 | case FUNCTION_DECL: | |
665 | gcc_assert (TREE_CODE (e->decl) == VAR_DECL); | |
666 | error_at (DECL_SOURCE_LOCATION (e->decl), | |
667 | "function %qD redeclared as variable", prevailing->decl); | |
668 | break; | |
fd193bcd | 669 | |
bc0ed27c | 670 | default: |
671 | gcc_unreachable (); | |
672 | } | |
673 | ||
674 | diagnosed_p = true; | |
675 | } | |
676 | if (diagnosed_p) | |
677 | inform (DECL_SOURCE_LOCATION (prevailing->decl), | |
678 | "previously declared here"); | |
679 | ||
bc0ed27c | 680 | /* Merge the chain to the single prevailing decl and diagnose |
681 | mismatches. */ | |
1b9e1cc0 | 682 | lto_symtab_merge_decls_2 (slot, diagnosed_p); |
fd193bcd | 683 | |
9ced88d0 | 684 | /* Store resolution decision into the callgraph. |
685 | In LTRANS don't overwrite information we stored into callgraph at | |
686 | WPA stage. | |
687 | ||
688 | Do not bother to store guessed decisions. Generic code knows how | |
689 | to handle UNKNOWN relocation well. | |
690 | ||
691 | The problem with storing guessed decision is whether to use | |
da722561 | 692 | PREVAILING_DEF, PREVAILING_DEF_IRONLY, PREVAILING_DEF_IRONLY_EXP. |
693 | First one would disable some whole program optimizations, while | |
694 | ther second would imply to many whole program assumptions. */ | |
9ced88d0 | 695 | if (prevailing->node && !flag_ltrans && !prevailing->guessed) |
7d0d0ce1 | 696 | prevailing->node->symbol.resolution = prevailing->resolution; |
9ced88d0 | 697 | else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed) |
7d0d0ce1 | 698 | prevailing->vnode->symbol.resolution = prevailing->resolution; |
fd193bcd | 699 | return 1; |
7bfefa9d | 700 | } |
701 | ||
fd193bcd | 702 | /* Resolve and merge all symbol table chains to a prevailing decl. */ |
7bfefa9d | 703 | |
704 | void | |
fd193bcd | 705 | lto_symtab_merge_decls (void) |
7bfefa9d | 706 | { |
fd193bcd | 707 | lto_symtab_maybe_init_hash_table (); |
708 | htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL); | |
7bfefa9d | 709 | } |
710 | ||
21ce3cc7 | 711 | /* Helper to process the decl chain for the symbol table entry *SLOT. */ |
712 | ||
713 | static int | |
714 | lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED) | |
715 | { | |
716 | lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot; | |
717 | ||
718 | if (!prevailing->next) | |
719 | return 1; | |
720 | ||
21ce3cc7 | 721 | /* Replace the cgraph node of each entry with the prevailing one. */ |
722 | for (e = prevailing->next; e; e = e->next) | |
723 | { | |
724 | if (e->node != NULL) | |
c55bd8e4 | 725 | { |
726 | /* In case we prevail funcion by an alias, we can run into case | |
727 | that the alias has no cgraph node attached, since it was | |
728 | previously unused. Create the node. */ | |
729 | if (!prevailing->node) | |
730 | { | |
5a90471f | 731 | prevailing->node = cgraph_create_node (prevailing->decl); |
c55bd8e4 | 732 | prevailing->node->alias = true; |
733 | } | |
734 | lto_cgraph_replace_node (e->node, prevailing->node); | |
735 | } | |
0cddb138 | 736 | if (e->vnode != NULL) |
c55bd8e4 | 737 | { |
738 | if (!prevailing->vnode) | |
739 | { | |
740 | prevailing->vnode = varpool_node (prevailing->decl); | |
741 | prevailing->vnode->alias = true; | |
742 | } | |
743 | lto_varpool_replace_node (e->vnode, prevailing->vnode); | |
744 | } | |
21ce3cc7 | 745 | } |
746 | ||
747 | /* Drop all but the prevailing decl from the symtab. */ | |
748 | prevailing->next = NULL; | |
749 | ||
750 | return 1; | |
751 | } | |
752 | ||
753 | /* Merge cgraph nodes according to the symbol merging done by | |
754 | lto_symtab_merge_decls. */ | |
755 | ||
756 | void | |
757 | lto_symtab_merge_cgraph_nodes (void) | |
758 | { | |
c70f46b0 | 759 | struct cgraph_node *node; |
e0eaac80 | 760 | struct varpool_node *vnode; |
21ce3cc7 | 761 | lto_symtab_maybe_init_hash_table (); |
762 | htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL); | |
aa29908f | 763 | |
7c455d87 | 764 | FOR_EACH_FUNCTION (node) |
c70f46b0 | 765 | if ((node->thunk.thunk_p || node->alias) |
766 | && node->thunk.alias) | |
767 | node->thunk.alias = lto_symtab_prevailing_decl (node->thunk.alias); | |
7c455d87 | 768 | FOR_EACH_VARIABLE (vnode) |
e0eaac80 | 769 | if (vnode->alias_of) |
770 | vnode->alias_of = lto_symtab_prevailing_decl (vnode->alias_of); | |
21ce3cc7 | 771 | } |
fd193bcd | 772 | |
7bfefa9d | 773 | /* Given the decl DECL, return the prevailing decl with the same name. */ |
774 | ||
775 | tree | |
776 | lto_symtab_prevailing_decl (tree decl) | |
777 | { | |
fd193bcd | 778 | lto_symtab_entry_t ret; |
7bfefa9d | 779 | |
780 | /* Builtins and local symbols are their own prevailing decl. */ | |
781 | if (!TREE_PUBLIC (decl) || is_builtin_fn (decl)) | |
782 | return decl; | |
783 | ||
784 | /* DECL_ABSTRACTs are their own prevailng decl. */ | |
785 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl)) | |
786 | return decl; | |
787 | ||
788 | /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */ | |
789 | gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); | |
790 | ||
791 | /* Walk through the list of candidates and return the one we merged to. */ | |
d86d364d | 792 | ret = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name) |
793 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)))); | |
fd193bcd | 794 | if (!ret) |
795 | return NULL_TREE; | |
796 | ||
bc0ed27c | 797 | return ret->decl; |
7bfefa9d | 798 | } |
799 | ||
7bfefa9d | 800 | #include "gt-lto-symtab.h" |