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