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