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