]>
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. */ | |
f1f41a6c | 33 | vec<tree, va_gc> *lto_global_var_decls; |
7bfefa9d | 34 | |
bc0ed27c | 35 | /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging |
36 | all edges and removing the old node. */ | |
7bfefa9d | 37 | |
bc0ed27c | 38 | static void |
39 | lto_cgraph_replace_node (struct cgraph_node *node, | |
40 | struct cgraph_node *prevailing_node) | |
7bfefa9d | 41 | { |
bc0ed27c | 42 | struct cgraph_edge *e, *next; |
fa7a183a | 43 | bool compatible_p; |
0f4e132d | 44 | |
45 | if (cgraph_dump_file) | |
46 | { | |
47 | fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i" | |
48 | " for symbol %s\n", | |
883554eb | 49 | cgraph_node_name (node), node->uid, |
50 | cgraph_node_name (prevailing_node), | |
0f4e132d | 51 | prevailing_node->uid, |
d86d364d | 52 | IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name) |
7d0d0ce1 | 53 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl))))); |
0f4e132d | 54 | } |
55 | ||
bc0ed27c | 56 | /* Merge node flags. */ |
8efa224a | 57 | if (node->symbol.force_output) |
58 | cgraph_mark_force_output_node (prevailing_node); | |
7d0d0ce1 | 59 | if (node->symbol.address_taken) |
7bfefa9d | 60 | { |
bc0ed27c | 61 | gcc_assert (!prevailing_node->global.inlined_to); |
62 | cgraph_mark_address_taken_node (prevailing_node); | |
63 | } | |
7bfefa9d | 64 | |
bc0ed27c | 65 | /* Redirect all incoming edges. */ |
fa7a183a | 66 | compatible_p |
7d0d0ce1 | 67 | = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->symbol.decl)), |
68 | TREE_TYPE (TREE_TYPE (node->symbol.decl))); | |
bc0ed27c | 69 | for (e = node->callers; e; e = next) |
70 | { | |
71 | next = e->next_caller; | |
72 | cgraph_redirect_edge_callee (e, prevailing_node); | |
fa7a183a | 73 | /* If there is a mismatch between the supposed callee return type and |
74 | the real one do not attempt to inline this function. | |
75 | ??? We really need a way to match function signatures for ABI | |
76 | compatibility and perform related promotions at inlining time. */ | |
77 | if (!compatible_p) | |
78 | e->call_stmt_cannot_inline_p = 1; | |
bc0ed27c | 79 | } |
8d810329 | 80 | /* Redirect incomming references. */ |
04ec15fa | 81 | ipa_clone_referring ((symtab_node)prevailing_node, &node->symbol.ref_list); |
7bfefa9d | 82 | |
bc0ed27c | 83 | /* Finally remove the replaced node. */ |
c70f46b0 | 84 | cgraph_remove_node (node); |
bc0ed27c | 85 | } |
86 | ||
0cddb138 | 87 | /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging |
88 | all edges and removing the old node. */ | |
89 | ||
90 | static void | |
91 | lto_varpool_replace_node (struct varpool_node *vnode, | |
92 | struct varpool_node *prevailing_node) | |
93 | { | |
0cddb138 | 94 | gcc_assert (!vnode->finalized || prevailing_node->finalized); |
95 | gcc_assert (!vnode->analyzed || prevailing_node->analyzed); | |
96 | ||
04ec15fa | 97 | ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list); |
8d810329 | 98 | |
6f932b06 | 99 | /* Be sure we can garbage collect the initializer. */ |
7d0d0ce1 | 100 | if (DECL_INITIAL (vnode->symbol.decl)) |
101 | DECL_INITIAL (vnode->symbol.decl) = error_mark_node; | |
0cddb138 | 102 | /* Finally remove the replaced node. */ |
103 | varpool_remove_node (vnode); | |
104 | } | |
105 | ||
bc0ed27c | 106 | /* Merge two variable or function symbol table entries PREVAILING and ENTRY. |
107 | Return false if the symbols are not fully compatible and a diagnostic | |
108 | should be emitted. */ | |
109 | ||
110 | static bool | |
c9aa6453 | 111 | lto_symtab_merge (symtab_node prevailing, symtab_node entry) |
bc0ed27c | 112 | { |
c9aa6453 | 113 | tree prevailing_decl = prevailing->symbol.decl; |
114 | tree decl = entry->symbol.decl; | |
bc0ed27c | 115 | tree prevailing_type, type; |
bc0ed27c | 116 | |
c9aa6453 | 117 | if (prevailing_decl == decl) |
118 | return true; | |
119 | ||
bc0ed27c | 120 | /* Merge decl state in both directions, we may still end up using |
121 | the new decl. */ | |
122 | TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl); | |
123 | TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl); | |
124 | ||
bc0ed27c | 125 | /* The linker may ask us to combine two incompatible symbols. |
126 | Detect this case and notify the caller of required diagnostics. */ | |
127 | ||
128 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
fdd4f660 | 129 | { |
79e191db | 130 | if (!types_compatible_p (TREE_TYPE (prevailing_decl), |
131 | TREE_TYPE (decl))) | |
fdd4f660 | 132 | /* If we don't have a merged type yet...sigh. The linker |
133 | wouldn't complain if the types were mismatched, so we | |
134 | probably shouldn't either. Just use the type from | |
135 | whichever decl appears to be associated with the | |
136 | definition. If for some odd reason neither decl is, the | |
137 | older one wins. */ | |
138 | (void) 0; | |
139 | ||
140 | return true; | |
141 | } | |
142 | ||
143 | /* Now we exclusively deal with VAR_DECLs. */ | |
144 | ||
66bd377c | 145 | /* Sharing a global symbol is a strong hint that two types are |
146 | compatible. We could use this information to complete | |
147 | incomplete pointed-to types more aggressively here, ignoring | |
148 | mismatches in both field and tag names. It's difficult though | |
149 | to guarantee that this does not have side-effects on merging | |
150 | more compatible types from other translation units though. */ | |
7bfefa9d | 151 | |
fdd4f660 | 152 | /* We can tolerate differences in type qualification, the |
66bd377c | 153 | qualification of the prevailing definition will prevail. |
154 | ??? In principle we might want to only warn for structurally | |
155 | incompatible types here, but unless we have protective measures | |
156 | for TBAA in place that would hide useful information. */ | |
bc0ed27c | 157 | prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl)); |
158 | type = TYPE_MAIN_VARIANT (TREE_TYPE (decl)); | |
66bd377c | 159 | |
79e191db | 160 | if (!types_compatible_p (prevailing_type, type)) |
66bd377c | 161 | { |
162 | if (COMPLETE_TYPE_P (type)) | |
163 | return false; | |
164 | ||
165 | /* If type is incomplete then avoid warnings in the cases | |
166 | that TBAA handles just fine. */ | |
167 | ||
168 | if (TREE_CODE (prevailing_type) != TREE_CODE (type)) | |
169 | return false; | |
170 | ||
171 | if (TREE_CODE (prevailing_type) == ARRAY_TYPE) | |
172 | { | |
173 | tree tem1 = TREE_TYPE (prevailing_type); | |
174 | tree tem2 = TREE_TYPE (type); | |
175 | while (TREE_CODE (tem1) == ARRAY_TYPE | |
176 | && TREE_CODE (tem2) == ARRAY_TYPE) | |
177 | { | |
178 | tem1 = TREE_TYPE (tem1); | |
179 | tem2 = TREE_TYPE (tem2); | |
180 | } | |
181 | ||
182 | if (TREE_CODE (tem1) != TREE_CODE (tem2)) | |
183 | return false; | |
184 | ||
79e191db | 185 | if (!types_compatible_p (tem1, tem2)) |
66bd377c | 186 | return false; |
187 | } | |
188 | ||
189 | /* Fallthru. Compatible enough. */ | |
190 | } | |
7bfefa9d | 191 | |
fdd4f660 | 192 | /* ??? We might want to emit a warning here if type qualification |
193 | differences were spotted. Do not do this unconditionally though. */ | |
7bfefa9d | 194 | |
fdd4f660 | 195 | /* There is no point in comparing too many details of the decls here. |
196 | The type compatibility checks or the completing of types has properly | |
197 | dealt with most issues. */ | |
7bfefa9d | 198 | |
fdd4f660 | 199 | /* The following should all not invoke fatal errors as in non-LTO |
200 | mode the linker wouldn't complain either. Just emit warnings. */ | |
7bfefa9d | 201 | |
fdd4f660 | 202 | /* Report a warning if user-specified alignments do not match. */ |
bc0ed27c | 203 | if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl)) |
204 | && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl)) | |
205 | return false; | |
7bfefa9d | 206 | |
7bfefa9d | 207 | return true; |
208 | } | |
209 | ||
bc0ed27c | 210 | /* Return true if the symtab entry E can be replaced by another symtab |
211 | entry. */ | |
7bfefa9d | 212 | |
bc0ed27c | 213 | static bool |
c9aa6453 | 214 | lto_symtab_resolve_replaceable_p (symtab_node e) |
7bfefa9d | 215 | { |
c9aa6453 | 216 | if (DECL_EXTERNAL (e->symbol.decl) |
217 | || DECL_COMDAT (e->symbol.decl) | |
218 | || DECL_ONE_ONLY (e->symbol.decl) | |
219 | || DECL_WEAK (e->symbol.decl)) | |
bc0ed27c | 220 | return true; |
7bfefa9d | 221 | |
c9aa6453 | 222 | if (TREE_CODE (e->symbol.decl) == VAR_DECL) |
223 | return (DECL_COMMON (e->symbol.decl) | |
224 | || (!flag_no_common && !DECL_INITIAL (e->symbol.decl))); | |
7bfefa9d | 225 | |
bc0ed27c | 226 | return false; |
fd193bcd | 227 | } |
7bfefa9d | 228 | |
bc0ed27c | 229 | /* Return true if the symtab entry E can be the prevailing one. */ |
7bfefa9d | 230 | |
bc0ed27c | 231 | static bool |
c9aa6453 | 232 | lto_symtab_resolve_can_prevail_p (symtab_node e) |
fd193bcd | 233 | { |
c9aa6453 | 234 | if (!symtab_real_symbol_p (e)) |
235 | return false; | |
236 | ||
d91c3d40 | 237 | /* The C++ frontend ends up neither setting TREE_STATIC nor |
238 | DECL_EXTERNAL on virtual methods but only TREE_PUBLIC. | |
239 | So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */ | |
c9aa6453 | 240 | if (DECL_EXTERNAL (e->symbol.decl)) |
bc0ed27c | 241 | return false; |
7bfefa9d | 242 | |
bc0ed27c | 243 | /* For functions we need a non-discarded body. */ |
c9aa6453 | 244 | if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL) |
245 | return (cgraph (e)->analyzed); | |
7bfefa9d | 246 | |
c9aa6453 | 247 | else if (TREE_CODE (e->symbol.decl) == VAR_DECL) |
248 | return varpool (e)->finalized; | |
7bfefa9d | 249 | |
bc0ed27c | 250 | gcc_unreachable (); |
7bfefa9d | 251 | } |
252 | ||
fd193bcd | 253 | /* Resolve the symbol with the candidates in the chain *SLOT and store |
254 | their resolutions. */ | |
7bfefa9d | 255 | |
67575a90 | 256 | static symtab_node |
c9aa6453 | 257 | lto_symtab_resolve_symbols (symtab_node first) |
fd193bcd | 258 | { |
c9aa6453 | 259 | symtab_node e; |
260 | symtab_node prevailing = NULL; | |
7bfefa9d | 261 | |
67b2bb71 | 262 | /* Always set e->node so that edges are updated to reflect decl merging. */ |
c9aa6453 | 263 | for (e = first; e; e = e->symbol.next_sharing_asm_name) |
264 | if (symtab_real_symbol_p (e) | |
265 | && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY | |
266 | || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP | |
267 | || e->symbol.resolution == LDPR_PREVAILING_DEF)) | |
67575a90 | 268 | { |
269 | prevailing = e; | |
270 | break; | |
271 | } | |
67b2bb71 | 272 | |
67b2bb71 | 273 | /* If the chain is already resolved there is nothing else to do. */ |
da722561 | 274 | if (prevailing) |
67575a90 | 275 | { |
276 | /* Assert it's the only one. */ | |
277 | for (e = prevailing->symbol.next_sharing_asm_name; e; e = e->symbol.next_sharing_asm_name) | |
278 | if (symtab_real_symbol_p (e) | |
279 | && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY | |
280 | || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP | |
281 | || e->symbol.resolution == LDPR_PREVAILING_DEF)) | |
282 | fatal_error ("multiple prevailing defs for %qE", | |
283 | DECL_NAME (prevailing->symbol.decl)); | |
284 | return prevailing; | |
285 | } | |
fd193bcd | 286 | |
bc0ed27c | 287 | /* Find the single non-replaceable prevailing symbol and |
288 | diagnose ODR violations. */ | |
c9aa6453 | 289 | for (e = first; e; e = e->symbol.next_sharing_asm_name) |
fd193bcd | 290 | { |
bc0ed27c | 291 | if (!lto_symtab_resolve_can_prevail_p (e)) |
67575a90 | 292 | continue; |
bc0ed27c | 293 | |
67575a90 | 294 | /* If we have a non-replaceable definition it prevails. */ |
bc0ed27c | 295 | if (!lto_symtab_resolve_replaceable_p (e)) |
296 | { | |
297 | if (prevailing) | |
298 | { | |
c9aa6453 | 299 | error_at (DECL_SOURCE_LOCATION (e->symbol.decl), |
300 | "%qD has already been defined", e->symbol.decl); | |
301 | inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl), | |
bc0ed27c | 302 | "previously defined here"); |
303 | } | |
304 | prevailing = e; | |
305 | } | |
306 | } | |
307 | if (prevailing) | |
67575a90 | 308 | return prevailing; |
bc0ed27c | 309 | |
310 | /* Do a second round choosing one from the replaceable prevailing decls. */ | |
c9aa6453 | 311 | for (e = first; e; e = e->symbol.next_sharing_asm_name) |
bc0ed27c | 312 | { |
67575a90 | 313 | if (!lto_symtab_resolve_can_prevail_p (e) |
c9aa6453 | 314 | || !symtab_real_symbol_p (e)) |
bc0ed27c | 315 | continue; |
316 | ||
317 | /* Choose the first function that can prevail as prevailing. */ | |
c9aa6453 | 318 | if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL) |
fd193bcd | 319 | { |
bc0ed27c | 320 | prevailing = e; |
321 | break; | |
fd193bcd | 322 | } |
bc0ed27c | 323 | |
324 | /* From variables that can prevail choose the largest one. */ | |
325 | if (!prevailing | |
c9aa6453 | 326 | || tree_int_cst_lt (DECL_SIZE (prevailing->symbol.decl), |
327 | DECL_SIZE (e->symbol.decl)) | |
aa419a52 | 328 | /* When variables are equivalent try to chose one that has useful |
329 | DECL_INITIAL. This makes sense for keyed vtables that are | |
330 | DECL_EXTERNAL but initialized. In units that do not need them | |
331 | we replace the initializer by error_mark_node to conserve | |
332 | memory. | |
333 | ||
334 | We know that the vtable is keyed outside the LTO unit - otherwise | |
335 | the keyed instance would prevail. We still can preserve useful | |
336 | info in the initializer. */ | |
c9aa6453 | 337 | || (DECL_SIZE (prevailing->symbol.decl) == DECL_SIZE (e->symbol.decl) |
338 | && (DECL_INITIAL (e->symbol.decl) | |
339 | && DECL_INITIAL (e->symbol.decl) != error_mark_node) | |
340 | && (!DECL_INITIAL (prevailing->symbol.decl) | |
341 | || DECL_INITIAL (prevailing->symbol.decl) == error_mark_node))) | |
bc0ed27c | 342 | prevailing = e; |
fd193bcd | 343 | } |
bc0ed27c | 344 | |
67575a90 | 345 | return prevailing; |
fd193bcd | 346 | } |
347 | ||
bc0ed27c | 348 | /* Merge all decls in the symbol table chain to the prevailing decl and |
1b9e1cc0 | 349 | issue diagnostics about type mismatches. If DIAGNOSED_P is true |
350 | do not issue further diagnostics.*/ | |
fd193bcd | 351 | |
352 | static void | |
c9aa6453 | 353 | lto_symtab_merge_decls_2 (symtab_node first, bool diagnosed_p) |
fd193bcd | 354 | { |
c9aa6453 | 355 | symtab_node prevailing, e; |
f1f41a6c | 356 | vec<tree> mismatches = vec<tree>(); |
bc0ed27c | 357 | unsigned i; |
358 | tree decl; | |
fd193bcd | 359 | |
360 | /* Nothing to do for a single entry. */ | |
c9aa6453 | 361 | prevailing = first; |
362 | if (!prevailing->symbol.next_sharing_asm_name) | |
fd193bcd | 363 | return; |
364 | ||
bc0ed27c | 365 | /* Try to merge each entry with the prevailing one. */ |
c9aa6453 | 366 | for (e = prevailing->symbol.next_sharing_asm_name; |
367 | e; e = e->symbol.next_sharing_asm_name) | |
bc0ed27c | 368 | { |
1b9e1cc0 | 369 | if (!lto_symtab_merge (prevailing, e) |
370 | && !diagnosed_p) | |
f1f41a6c | 371 | mismatches.safe_push (e->symbol.decl); |
bc0ed27c | 372 | } |
f1f41a6c | 373 | if (mismatches.is_empty ()) |
bc0ed27c | 374 | return; |
fd193bcd | 375 | |
bc0ed27c | 376 | /* Diagnose all mismatched re-declarations. */ |
f1f41a6c | 377 | FOR_EACH_VEC_ELT (mismatches, i, decl) |
fd193bcd | 378 | { |
c9aa6453 | 379 | if (!types_compatible_p (TREE_TYPE (prevailing->symbol.decl), |
380 | TREE_TYPE (decl))) | |
bc0ed27c | 381 | diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0, |
382 | "type of %qD does not match original " | |
383 | "declaration", decl); | |
384 | ||
c9aa6453 | 385 | else if ((DECL_USER_ALIGN (prevailing->symbol.decl) |
386 | && DECL_USER_ALIGN (decl)) | |
387 | && DECL_ALIGN (prevailing->symbol.decl) < DECL_ALIGN (decl)) | |
fd193bcd | 388 | { |
bc0ed27c | 389 | diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0, |
390 | "alignment of %qD is bigger than " | |
391 | "original declaration", decl); | |
fd193bcd | 392 | } |
fd193bcd | 393 | } |
bc0ed27c | 394 | if (diagnosed_p) |
c9aa6453 | 395 | inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl), |
bc0ed27c | 396 | "previously declared here"); |
fd193bcd | 397 | |
f1f41a6c | 398 | mismatches.release (); |
fd193bcd | 399 | } |
400 | ||
401 | /* Helper to process the decl chain for the symbol table entry *SLOT. */ | |
402 | ||
c9aa6453 | 403 | static void |
404 | lto_symtab_merge_decls_1 (symtab_node first) | |
7bfefa9d | 405 | { |
c9aa6453 | 406 | symtab_node e, prevailing; |
bc0ed27c | 407 | bool diagnosed_p = false; |
fd193bcd | 408 | |
c9aa6453 | 409 | if (cgraph_dump_file) |
410 | { | |
411 | fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n", | |
412 | symtab_node_asm_name (first)); | |
413 | for (e = first; e; e = e->symbol.next_sharing_asm_name) | |
414 | dump_symtab_node (cgraph_dump_file, e); | |
415 | } | |
416 | ||
bc0ed27c | 417 | /* Compute the symbol resolutions. This is a no-op when using the |
c9aa6453 | 418 | linker plugin and resolution was decided by the linker. */ |
67575a90 | 419 | prevailing = lto_symtab_resolve_symbols (first); |
bc0ed27c | 420 | |
421 | /* If there's not a prevailing symbol yet it's an external reference. | |
422 | Happens a lot during ltrans. Choose the first symbol with a | |
423 | cgraph or a varpool node. */ | |
424 | if (!prevailing) | |
425 | { | |
c9aa6453 | 426 | prevailing = first; |
9269f553 | 427 | /* For variables chose with a priority variant with vnode |
428 | attached (i.e. from unit where external declaration of | |
429 | variable is actually used). | |
430 | When there are multiple variants, chose one with size. | |
431 | This is needed for C++ typeinfos, for example in | |
432 | lto/20081204-1 there are typeifos in both units, just | |
433 | one of them do have size. */ | |
c9aa6453 | 434 | if (TREE_CODE (prevailing->symbol.decl) == VAR_DECL) |
9269f553 | 435 | { |
c9aa6453 | 436 | for (e = prevailing->symbol.next_sharing_asm_name; |
437 | e; e = e->symbol.next_sharing_asm_name) | |
438 | if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->symbol.decl)) | |
439 | && COMPLETE_TYPE_P (TREE_TYPE (e->symbol.decl))) | |
9269f553 | 440 | prevailing = e; |
441 | } | |
bc0ed27c | 442 | } |
fd193bcd | 443 | |
c9aa6453 | 444 | symtab_prevail_in_asm_name_hash (prevailing); |
fd193bcd | 445 | |
bc0ed27c | 446 | /* Record the prevailing variable. */ |
c9aa6453 | 447 | if (TREE_CODE (prevailing->symbol.decl) == VAR_DECL) |
f1f41a6c | 448 | vec_safe_push (lto_global_var_decls, prevailing->symbol.decl); |
fd193bcd | 449 | |
bc0ed27c | 450 | /* Diagnose mismatched objects. */ |
c9aa6453 | 451 | for (e = prevailing->symbol.next_sharing_asm_name; |
452 | e; e = e->symbol.next_sharing_asm_name) | |
bc0ed27c | 453 | { |
c9aa6453 | 454 | if (TREE_CODE (prevailing->symbol.decl) |
455 | == TREE_CODE (e->symbol.decl)) | |
bc0ed27c | 456 | continue; |
fd193bcd | 457 | |
c9aa6453 | 458 | switch (TREE_CODE (prevailing->symbol.decl)) |
bc0ed27c | 459 | { |
460 | case VAR_DECL: | |
c9aa6453 | 461 | gcc_assert (TREE_CODE (e->symbol.decl) == FUNCTION_DECL); |
462 | error_at (DECL_SOURCE_LOCATION (e->symbol.decl), | |
463 | "variable %qD redeclared as function", | |
464 | prevailing->symbol.decl); | |
bc0ed27c | 465 | break; |
466 | ||
467 | case FUNCTION_DECL: | |
c9aa6453 | 468 | gcc_assert (TREE_CODE (e->symbol.decl) == VAR_DECL); |
469 | error_at (DECL_SOURCE_LOCATION (e->symbol.decl), | |
470 | "function %qD redeclared as variable", | |
471 | prevailing->symbol.decl); | |
bc0ed27c | 472 | break; |
fd193bcd | 473 | |
bc0ed27c | 474 | default: |
475 | gcc_unreachable (); | |
476 | } | |
477 | ||
478 | diagnosed_p = true; | |
479 | } | |
480 | if (diagnosed_p) | |
c9aa6453 | 481 | inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl), |
bc0ed27c | 482 | "previously declared here"); |
483 | ||
bc0ed27c | 484 | /* Merge the chain to the single prevailing decl and diagnose |
485 | mismatches. */ | |
a1003dd5 | 486 | lto_symtab_merge_decls_2 (prevailing, diagnosed_p); |
c9aa6453 | 487 | |
488 | if (cgraph_dump_file) | |
489 | { | |
490 | fprintf (cgraph_dump_file, "After resolution:\n"); | |
a1003dd5 | 491 | for (e = prevailing; e; e = e->symbol.next_sharing_asm_name) |
c9aa6453 | 492 | dump_symtab_node (cgraph_dump_file, e); |
493 | } | |
7bfefa9d | 494 | } |
495 | ||
fd193bcd | 496 | /* Resolve and merge all symbol table chains to a prevailing decl. */ |
7bfefa9d | 497 | |
498 | void | |
fd193bcd | 499 | lto_symtab_merge_decls (void) |
7bfefa9d | 500 | { |
c9aa6453 | 501 | symtab_node node; |
502 | ||
503 | /* In ltrans mode we read merged cgraph, we do not really need to care | |
504 | about resolving symbols again, we only need to replace duplicated declarations | |
505 | read from the callgraph and from function sections. */ | |
506 | if (flag_ltrans) | |
507 | return; | |
508 | ||
509 | /* Populate assembler name hash. */ | |
510 | symtab_initialize_asm_name_hash (); | |
511 | ||
512 | FOR_EACH_SYMBOL (node) | |
513 | if (TREE_PUBLIC (node->symbol.decl) | |
514 | && node->symbol.next_sharing_asm_name | |
515 | && !node->symbol.previous_sharing_asm_name) | |
516 | lto_symtab_merge_decls_1 (node); | |
7bfefa9d | 517 | } |
518 | ||
21ce3cc7 | 519 | /* Helper to process the decl chain for the symbol table entry *SLOT. */ |
520 | ||
c9aa6453 | 521 | static void |
522 | lto_symtab_merge_cgraph_nodes_1 (symtab_node prevailing) | |
21ce3cc7 | 523 | { |
c9aa6453 | 524 | symtab_node e, next; |
21ce3cc7 | 525 | |
21ce3cc7 | 526 | /* Replace the cgraph node of each entry with the prevailing one. */ |
c9aa6453 | 527 | for (e = prevailing->symbol.next_sharing_asm_name; e; |
528 | e = next) | |
21ce3cc7 | 529 | { |
c9aa6453 | 530 | next = e->symbol.next_sharing_asm_name; |
21ce3cc7 | 531 | |
c9aa6453 | 532 | if (!symtab_real_symbol_p (e)) |
533 | continue; | |
2dc9831f | 534 | cgraph_node *ce = dyn_cast <cgraph_node> (e); |
535 | if (ce && !DECL_BUILT_IN (e->symbol.decl)) | |
536 | lto_cgraph_replace_node (ce, cgraph (prevailing)); | |
537 | if (varpool_node *ve = dyn_cast <varpool_node> (e)) | |
538 | lto_varpool_replace_node (ve, varpool (prevailing)); | |
c9aa6453 | 539 | } |
21ce3cc7 | 540 | |
c9aa6453 | 541 | return; |
21ce3cc7 | 542 | } |
543 | ||
544 | /* Merge cgraph nodes according to the symbol merging done by | |
545 | lto_symtab_merge_decls. */ | |
546 | ||
547 | void | |
548 | lto_symtab_merge_cgraph_nodes (void) | |
549 | { | |
c9aa6453 | 550 | struct cgraph_node *cnode; |
e0eaac80 | 551 | struct varpool_node *vnode; |
c9aa6453 | 552 | symtab_node node; |
553 | ||
554 | /* Populate assembler name hash. */ | |
555 | symtab_initialize_asm_name_hash (); | |
aa29908f | 556 | |
c9aa6453 | 557 | if (!flag_ltrans) |
558 | FOR_EACH_SYMBOL (node) | |
559 | if (TREE_PUBLIC (node->symbol.decl) | |
560 | && node->symbol.next_sharing_asm_name | |
561 | && !node->symbol.previous_sharing_asm_name) | |
562 | lto_symtab_merge_cgraph_nodes_1 (node); | |
563 | ||
564 | FOR_EACH_FUNCTION (cnode) | |
565 | { | |
566 | if ((cnode->thunk.thunk_p || cnode->alias) | |
567 | && cnode->thunk.alias) | |
568 | cnode->thunk.alias = lto_symtab_prevailing_decl (cnode->thunk.alias); | |
569 | cnode->symbol.aux = NULL; | |
570 | } | |
7c455d87 | 571 | FOR_EACH_VARIABLE (vnode) |
c9aa6453 | 572 | { |
573 | if (vnode->alias_of) | |
574 | vnode->alias_of = lto_symtab_prevailing_decl (vnode->alias_of); | |
575 | vnode->symbol.aux = NULL; | |
576 | } | |
21ce3cc7 | 577 | } |
fd193bcd | 578 | |
7bfefa9d | 579 | /* Given the decl DECL, return the prevailing decl with the same name. */ |
580 | ||
581 | tree | |
582 | lto_symtab_prevailing_decl (tree decl) | |
583 | { | |
c9aa6453 | 584 | symtab_node ret; |
7bfefa9d | 585 | |
586 | /* Builtins and local symbols are their own prevailing decl. */ | |
587 | if (!TREE_PUBLIC (decl) || is_builtin_fn (decl)) | |
588 | return decl; | |
589 | ||
590 | /* DECL_ABSTRACTs are their own prevailng decl. */ | |
591 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl)) | |
592 | return decl; | |
593 | ||
594 | /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */ | |
595 | gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); | |
596 | ||
597 | /* Walk through the list of candidates and return the one we merged to. */ | |
c9aa6453 | 598 | ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl)); |
fd193bcd | 599 | if (!ret) |
c9aa6453 | 600 | return decl; |
fd193bcd | 601 | |
c9aa6453 | 602 | return ret->symbol.decl; |
7bfefa9d | 603 | } |