Fix change log
[official-gcc.git] / gcc / lto-symtab.c
blob1d90ab113f60992af02ad145fb14bd588046b763
1 /* LTO symbol table.
2 Copyright 2009 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "toplev.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ggc.h"
29 #include "lambda.h" /* gcd */
30 #include "hashtab.h"
31 #include "plugin-api.h"
32 #include "lto-streamer.h"
34 /* Vector to keep track of external variables we've seen so far. */
35 VEC(tree,gc) *lto_global_var_decls;
37 /* Symbol table entry. */
39 struct GTY(()) lto_symtab_entry_def
41 /* The symbol table entry key, an IDENTIFIER. */
42 tree id;
43 /* The symbol table entry, a DECL. */
44 tree decl;
45 /* The cgraph node if decl is a function decl. Filled in during the
46 merging process. */
47 struct cgraph_node *node;
48 /* The varpool node if decl is a variable decl. Filled in during the
49 merging process. */
50 struct varpool_node *vnode;
51 /* LTO file-data and symbol resolution for this decl. */
52 struct lto_file_decl_data * GTY((skip (""))) file_data;
53 enum ld_plugin_symbol_resolution resolution;
54 /* True when resolution was guessed and not read from the file. */
55 bool guessed;
56 /* Pointer to the next entry with the same key. Before decl merging
57 this links all symbols from the different TUs. After decl merging
58 this links merged but incompatible decls, thus all prevailing ones
59 remaining. */
60 struct lto_symtab_entry_def *next;
62 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
64 /* A poor man's symbol table. This hashes identifier to prevailing DECL
65 if there is one. */
67 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
68 param_is (struct lto_symtab_entry_def)))
69 htab_t lto_symtab_identifiers;
71 /* Free symtab hashtable. */
73 void
74 lto_symtab_free (void)
76 htab_delete (lto_symtab_identifiers);
77 lto_symtab_identifiers = NULL;
80 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
82 static hashval_t
83 lto_symtab_entry_hash (const void *p)
85 const struct lto_symtab_entry_def *base =
86 (const struct lto_symtab_entry_def *) p;
87 return IDENTIFIER_HASH_VALUE (base->id);
90 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
91 corresponding to the same symbol. */
93 static int
94 lto_symtab_entry_eq (const void *p1, const void *p2)
96 const struct lto_symtab_entry_def *base1 =
97 (const struct lto_symtab_entry_def *) p1;
98 const struct lto_symtab_entry_def *base2 =
99 (const struct lto_symtab_entry_def *) p2;
100 return (base1->id == base2->id);
103 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
104 to be marked for GC. */
106 static int
107 lto_symtab_entry_marked_p (const void *p)
109 const struct lto_symtab_entry_def *base =
110 (const struct lto_symtab_entry_def *) p;
112 /* Keep this only if the common IDENTIFIER_NODE of the symtab chain
113 is marked which it will be if at least one of the DECLs in the
114 chain is marked. */
115 return ggc_marked_p (base->id);
118 /* Lazily initialize resolution hash tables. */
120 static void
121 lto_symtab_maybe_init_hash_table (void)
123 if (lto_symtab_identifiers)
124 return;
126 lto_symtab_identifiers =
127 htab_create_ggc (1021, lto_symtab_entry_hash,
128 lto_symtab_entry_eq, NULL);
131 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
132 and read from FILE_DATA. */
134 void
135 lto_symtab_register_decl (tree decl,
136 ld_plugin_symbol_resolution_t resolution,
137 struct lto_file_decl_data *file_data)
139 lto_symtab_entry_t new_entry;
140 void **slot;
142 /* Check that declarations reaching this function do not have
143 properties inconsistent with having external linkage. If any of
144 these asertions fail, then the object file reader has failed to
145 detect these cases and issue appropriate error messages. */
146 gcc_assert (decl
147 && TREE_PUBLIC (decl)
148 && (TREE_CODE (decl) == VAR_DECL
149 || TREE_CODE (decl) == FUNCTION_DECL)
150 && DECL_ASSEMBLER_NAME_SET_P (decl));
151 if (TREE_CODE (decl) == VAR_DECL
152 && DECL_INITIAL (decl))
153 gcc_assert (!DECL_EXTERNAL (decl)
154 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
155 if (TREE_CODE (decl) == FUNCTION_DECL)
156 gcc_assert (!DECL_ABSTRACT (decl));
158 new_entry = ggc_alloc_cleared_lto_symtab_entry_def ();
159 new_entry->id = DECL_ASSEMBLER_NAME (decl);
160 new_entry->decl = decl;
161 new_entry->resolution = resolution;
162 new_entry->file_data = file_data;
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;
170 /* Get the lto_symtab_entry_def struct associated with ID
171 if there is one. */
173 static lto_symtab_entry_t
174 lto_symtab_get (tree id)
176 struct lto_symtab_entry_def temp;
177 void **slot;
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;
185 /* Get the linker resolution for DECL. */
187 enum ld_plugin_symbol_resolution
188 lto_symtab_get_resolution (tree decl)
190 lto_symtab_entry_t e;
192 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
194 e = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
195 while (e && e->decl != decl)
196 e = e->next;
197 if (!e)
198 return LDPR_UNKNOWN;
200 return e->resolution;
204 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
205 all edges and removing the old node. */
207 static void
208 lto_cgraph_replace_node (struct cgraph_node *node,
209 struct cgraph_node *prevailing_node)
211 struct cgraph_edge *e, *next;
212 bool no_aliases_please = false;
214 if (cgraph_dump_file)
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 (DECL_ASSEMBLER_NAME (node->decl)));
224 if (prevailing_node->same_body_alias)
226 if (prevailing_node->thunk.thunk_p)
227 no_aliases_please = true;
228 prevailing_node = prevailing_node->same_body;
231 /* Merge node flags. */
232 if (node->needed)
233 cgraph_mark_needed_node (prevailing_node);
234 if (node->reachable)
235 cgraph_mark_reachable_node (prevailing_node);
236 if (node->address_taken)
238 gcc_assert (!prevailing_node->global.inlined_to);
239 cgraph_mark_address_taken_node (prevailing_node);
242 /* Redirect all incoming edges. */
243 for (e = node->callers; e; e = next)
245 next = e->next_caller;
246 cgraph_redirect_edge_callee (e, prevailing_node);
248 /* Redirect incomming references. */
249 ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
251 /* If we have aliases, redirect them to the prevailing node. */
252 if (!node->same_body_alias && node->same_body)
254 struct cgraph_node *alias, *last;
255 /* We prevail aliases/tunks by a thunk. This is doable but
256 would need thunk combination. Hopefully no ABI changes will
257 every be crazy enough. */
258 gcc_assert (!no_aliases_please);
260 for (alias = node->same_body; alias; alias = alias->next)
262 last = alias;
263 gcc_assert (alias->same_body_alias);
264 alias->same_body = prevailing_node;
265 alias->thunk.alias = prevailing_node->decl;
267 last->next = prevailing_node->same_body;
268 /* Node with aliases is prevailed by alias.
269 We could handle this, but combining thunks together will be tricky.
270 Hopefully this does not happen. */
271 if (prevailing_node->same_body)
272 prevailing_node->same_body->previous = last;
273 prevailing_node->same_body = node->same_body;
274 node->same_body = NULL;
277 /* Finally remove the replaced node. */
278 if (node->same_body_alias)
279 cgraph_remove_same_body_alias (node);
280 else
281 cgraph_remove_node (node);
284 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
285 all edges and removing the old node. */
287 static void
288 lto_varpool_replace_node (struct varpool_node *vnode,
289 struct varpool_node *prevailing_node)
291 /* Merge node flags. */
292 if (vnode->needed)
294 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
295 varpool_mark_needed_node (prevailing_node);
297 /* Relink aliases. */
298 if (vnode->extra_name && !vnode->alias)
300 struct varpool_node *alias, *last;
301 for (alias = vnode->extra_name;
302 alias; alias = alias->next)
304 last = alias;
305 alias->extra_name = prevailing_node;
308 if (prevailing_node->extra_name)
310 last->next = prevailing_node->extra_name;
311 prevailing_node->extra_name->prev = last;
313 prevailing_node->extra_name = vnode->extra_name;
314 vnode->extra_name = NULL;
316 gcc_assert (!vnode->finalized || prevailing_node->finalized);
317 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
319 /* When replacing by an alias, the references goes to the original
320 variable. */
321 if (prevailing_node->alias && prevailing_node->extra_name)
322 prevailing_node = prevailing_node->extra_name;
323 ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
325 /* Be sure we can garbage collect the initializer. */
326 if (DECL_INITIAL (vnode->decl))
327 DECL_INITIAL (vnode->decl) = error_mark_node;
328 /* Finally remove the replaced node. */
329 varpool_remove_node (vnode);
332 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
333 Return false if the symbols are not fully compatible and a diagnostic
334 should be emitted. */
336 static bool
337 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
339 tree prevailing_decl = prevailing->decl;
340 tree decl = entry->decl;
341 tree prevailing_type, type;
343 /* Merge decl state in both directions, we may still end up using
344 the new decl. */
345 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
346 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
348 /* The linker may ask us to combine two incompatible symbols.
349 Detect this case and notify the caller of required diagnostics. */
351 if (TREE_CODE (decl) == FUNCTION_DECL)
353 if (!gimple_types_compatible_p (TREE_TYPE (prevailing_decl),
354 TREE_TYPE (decl), GTC_DIAG))
355 /* If we don't have a merged type yet...sigh. The linker
356 wouldn't complain if the types were mismatched, so we
357 probably shouldn't either. Just use the type from
358 whichever decl appears to be associated with the
359 definition. If for some odd reason neither decl is, the
360 older one wins. */
361 (void) 0;
363 return true;
366 /* Now we exclusively deal with VAR_DECLs. */
368 /* Sharing a global symbol is a strong hint that two types are
369 compatible. We could use this information to complete
370 incomplete pointed-to types more aggressively here, ignoring
371 mismatches in both field and tag names. It's difficult though
372 to guarantee that this does not have side-effects on merging
373 more compatible types from other translation units though. */
375 /* We can tolerate differences in type qualification, the
376 qualification of the prevailing definition will prevail.
377 ??? In principle we might want to only warn for structurally
378 incompatible types here, but unless we have protective measures
379 for TBAA in place that would hide useful information. */
380 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
381 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
383 /* We have to register and fetch canonical types here as the global
384 fixup process didn't yet run. */
385 prevailing_type = gimple_register_type (prevailing_type);
386 type = gimple_register_type (type);
387 if (!gimple_types_compatible_p (prevailing_type, type, GTC_DIAG))
389 if (COMPLETE_TYPE_P (type))
390 return false;
392 /* If type is incomplete then avoid warnings in the cases
393 that TBAA handles just fine. */
395 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
396 return false;
398 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
400 tree tem1 = TREE_TYPE (prevailing_type);
401 tree tem2 = TREE_TYPE (type);
402 while (TREE_CODE (tem1) == ARRAY_TYPE
403 && TREE_CODE (tem2) == ARRAY_TYPE)
405 tem1 = TREE_TYPE (tem1);
406 tem2 = TREE_TYPE (tem2);
409 if (TREE_CODE (tem1) != TREE_CODE (tem2))
410 return false;
412 if (!gimple_types_compatible_p (gimple_register_type (tem1),
413 gimple_register_type (tem2),
414 GTC_DIAG))
415 return false;
418 /* Fallthru. Compatible enough. */
421 /* ??? We might want to emit a warning here if type qualification
422 differences were spotted. Do not do this unconditionally though. */
424 /* There is no point in comparing too many details of the decls here.
425 The type compatibility checks or the completing of types has properly
426 dealt with most issues. */
428 /* The following should all not invoke fatal errors as in non-LTO
429 mode the linker wouldn't complain either. Just emit warnings. */
431 /* Report a warning if user-specified alignments do not match. */
432 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
433 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
434 return false;
436 return true;
439 /* Return true if the symtab entry E can be replaced by another symtab
440 entry. */
442 static bool
443 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
445 if (DECL_EXTERNAL (e->decl)
446 || DECL_COMDAT (e->decl)
447 || DECL_WEAK (e->decl))
448 return true;
450 if (TREE_CODE (e->decl) == VAR_DECL)
451 return (DECL_COMMON (e->decl)
452 || (!flag_no_common && !DECL_INITIAL (e->decl)));
454 return false;
457 /* Return true if the symtab entry E can be the prevailing one. */
459 static bool
460 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
462 /* The C++ frontend ends up neither setting TREE_STATIC nor
463 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
464 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
465 if (DECL_EXTERNAL (e->decl))
466 return false;
468 /* For functions we need a non-discarded body. */
469 if (TREE_CODE (e->decl) == FUNCTION_DECL)
470 return (e->node
471 && (e->node->analyzed
472 || (e->node->same_body_alias && e->node->same_body->analyzed)));
474 /* A variable should have a size. */
475 else if (TREE_CODE (e->decl) == VAR_DECL)
477 if (!e->vnode)
478 return false;
479 if (e->vnode->finalized)
480 return true;
481 return e->vnode->alias && e->vnode->extra_name->finalized;
484 gcc_unreachable ();
487 /* Resolve the symbol with the candidates in the chain *SLOT and store
488 their resolutions. */
490 static void
491 lto_symtab_resolve_symbols (void **slot)
493 lto_symtab_entry_t e;
494 lto_symtab_entry_t prevailing = NULL;
496 /* Always set e->node so that edges are updated to reflect decl merging. */
497 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
499 if (TREE_CODE (e->decl) == FUNCTION_DECL)
500 e->node = cgraph_get_node_or_alias (e->decl);
501 else if (TREE_CODE (e->decl) == VAR_DECL)
502 e->vnode = varpool_get_node (e->decl);
505 e = (lto_symtab_entry_t) *slot;
507 /* If the chain is already resolved there is nothing else to do. */
508 if (e->resolution != LDPR_UNKNOWN)
509 return;
511 /* Find the single non-replaceable prevailing symbol and
512 diagnose ODR violations. */
513 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
515 if (!lto_symtab_resolve_can_prevail_p (e))
517 e->resolution = LDPR_RESOLVED_IR;
518 e->guessed = true;
519 continue;
522 /* Set a default resolution - the final prevailing one will get
523 adjusted later. */
524 e->resolution = LDPR_PREEMPTED_IR;
525 e->guessed = true;
526 if (!lto_symtab_resolve_replaceable_p (e))
528 if (prevailing)
530 error_at (DECL_SOURCE_LOCATION (e->decl),
531 "%qD has already been defined", e->decl);
532 inform (DECL_SOURCE_LOCATION (prevailing->decl),
533 "previously defined here");
535 prevailing = e;
538 if (prevailing)
539 goto found;
541 /* Do a second round choosing one from the replaceable prevailing decls. */
542 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
544 if (e->resolution != LDPR_PREEMPTED_IR)
545 continue;
547 /* Choose the first function that can prevail as prevailing. */
548 if (TREE_CODE (e->decl) == FUNCTION_DECL)
550 prevailing = e;
551 break;
554 /* From variables that can prevail choose the largest one. */
555 if (!prevailing
556 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
557 DECL_SIZE (e->decl)))
558 prevailing = e;
561 if (!prevailing)
562 return;
564 found:
565 /* If current lto files represent the whole program,
566 it is correct to use LDPR_PREVALING_DEF_IRONLY.
567 If current lto files are part of whole program, internal
568 resolver doesn't know if it is LDPR_PREVAILING_DEF
569 or LDPR_PREVAILING_DEF_IRONLY. Use IRONLY conforms to
570 using -fwhole-program. Otherwise, it doesn't
571 matter using either LDPR_PREVAILING_DEF or
572 LDPR_PREVAILING_DEF_IRONLY
574 FIXME: above workaround due to gold plugin makes some
575 variables IRONLY, which are indeed PREVAILING_DEF in
576 resolution file. These variables still need manual
577 externally_visible attribute. */
578 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
579 prevailing->guessed = true;
582 /* Merge all decls in the symbol table chain to the prevailing decl and
583 issue diagnostics about type mismatches. */
585 static void
586 lto_symtab_merge_decls_2 (void **slot)
588 lto_symtab_entry_t prevailing, e;
589 VEC(tree, heap) *mismatches = NULL;
590 unsigned i;
591 tree decl;
592 bool diagnosed_p = false;
594 /* Nothing to do for a single entry. */
595 prevailing = (lto_symtab_entry_t) *slot;
596 if (!prevailing->next)
597 return;
599 /* Try to merge each entry with the prevailing one. */
600 for (e = prevailing->next; e; e = e->next)
602 if (!lto_symtab_merge (prevailing, e))
603 VEC_safe_push (tree, heap, mismatches, e->decl);
605 if (VEC_empty (tree, mismatches))
606 return;
608 /* Diagnose all mismatched re-declarations. */
609 FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
611 if (!gimple_types_compatible_p (TREE_TYPE (prevailing->decl),
612 TREE_TYPE (decl), GTC_DIAG))
613 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
614 "type of %qD does not match original "
615 "declaration", decl);
617 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
618 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
620 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
621 "alignment of %qD is bigger than "
622 "original declaration", decl);
625 if (diagnosed_p)
626 inform (DECL_SOURCE_LOCATION (prevailing->decl),
627 "previously declared here");
629 VEC_free (tree, heap, mismatches);
632 /* Helper to process the decl chain for the symbol table entry *SLOT. */
634 static int
635 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
637 lto_symtab_entry_t e, prevailing;
638 bool diagnosed_p = false;
640 /* Compute the symbol resolutions. This is a no-op when using the
641 linker plugin. */
642 lto_symtab_resolve_symbols (slot);
644 /* Find the prevailing decl. */
645 for (prevailing = (lto_symtab_entry_t) *slot;
646 prevailing
647 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
648 && prevailing->resolution != LDPR_PREVAILING_DEF;
649 prevailing = prevailing->next)
652 /* Assert it's the only one. */
653 if (prevailing)
654 for (e = prevailing->next; e; e = e->next)
656 if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
657 || e->resolution == LDPR_PREVAILING_DEF)
658 fatal_error ("multiple prevailing defs for %qE",
659 DECL_NAME (prevailing->decl));
662 /* If there's not a prevailing symbol yet it's an external reference.
663 Happens a lot during ltrans. Choose the first symbol with a
664 cgraph or a varpool node. */
665 if (!prevailing)
667 prevailing = (lto_symtab_entry_t) *slot;
668 /* For functions choose one with a cgraph node. */
669 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
670 while (!prevailing->node
671 && prevailing->next)
672 prevailing = prevailing->next;
673 /* For variables chose with a priority variant with vnode
674 attached (i.e. from unit where external declaration of
675 variable is actually used).
676 When there are multiple variants, chose one with size.
677 This is needed for C++ typeinfos, for example in
678 lto/20081204-1 there are typeifos in both units, just
679 one of them do have size. */
680 if (TREE_CODE (prevailing->decl) == VAR_DECL)
682 for (e = prevailing->next; e; e = e->next)
683 if ((!prevailing->vnode && e->vnode)
684 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
685 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
686 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
687 prevailing = e;
691 /* Move it first in the list. */
692 if ((lto_symtab_entry_t) *slot != prevailing)
694 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
696 e->next = prevailing->next;
697 prevailing->next = (lto_symtab_entry_t) *slot;
698 *slot = (void *) prevailing;
701 /* Record the prevailing variable. */
702 if (TREE_CODE (prevailing->decl) == VAR_DECL)
703 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
705 /* Diagnose mismatched objects. */
706 for (e = prevailing->next; e; e = e->next)
708 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
709 continue;
711 switch (TREE_CODE (prevailing->decl))
713 case VAR_DECL:
714 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
715 error_at (DECL_SOURCE_LOCATION (e->decl),
716 "variable %qD redeclared as function", prevailing->decl);
717 break;
719 case FUNCTION_DECL:
720 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
721 error_at (DECL_SOURCE_LOCATION (e->decl),
722 "function %qD redeclared as variable", prevailing->decl);
723 break;
725 default:
726 gcc_unreachable ();
729 diagnosed_p = true;
731 if (diagnosed_p)
732 inform (DECL_SOURCE_LOCATION (prevailing->decl),
733 "previously declared here");
735 /* Register and adjust types of the entries. */
736 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
737 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
739 /* Merge the chain to the single prevailing decl and diagnose
740 mismatches. */
741 lto_symtab_merge_decls_2 (slot);
743 /* Drop all but the prevailing decl from the symtab. */
744 if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
745 && TREE_CODE (prevailing->decl) != VAR_DECL)
746 prevailing->next = NULL;
748 /* Store resolution decision into the callgraph.
749 In LTRANS don't overwrite information we stored into callgraph at
750 WPA stage.
752 Do not bother to store guessed decisions. Generic code knows how
753 to handle UNKNOWN relocation well.
755 The problem with storing guessed decision is whether to use
756 PREVAILING_DEF or PREVAILING_DEF_IRONLY. First one would disable
757 some whole program optimizations, while ther second would imply
758 to many whole program assumptions. */
759 if (prevailing->node && !flag_ltrans && !prevailing->guessed)
760 prevailing->node->resolution = prevailing->resolution;
761 else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed)
762 prevailing->vnode->resolution = prevailing->resolution;
763 return 1;
766 /* Resolve and merge all symbol table chains to a prevailing decl. */
768 void
769 lto_symtab_merge_decls (void)
771 lto_symtab_maybe_init_hash_table ();
772 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
775 /* Helper to process the decl chain for the symbol table entry *SLOT. */
777 static int
778 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
780 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
782 if (!prevailing->next)
783 return 1;
785 /* Replace the cgraph node of each entry with the prevailing one. */
786 for (e = prevailing->next; e; e = e->next)
788 if (e->node != NULL)
789 lto_cgraph_replace_node (e->node, prevailing->node);
790 if (e->vnode != NULL)
791 lto_varpool_replace_node (e->vnode, prevailing->vnode);
794 /* Drop all but the prevailing decl from the symtab. */
795 prevailing->next = NULL;
797 return 1;
800 /* Merge cgraph nodes according to the symbol merging done by
801 lto_symtab_merge_decls. */
803 void
804 lto_symtab_merge_cgraph_nodes (void)
806 lto_symtab_maybe_init_hash_table ();
807 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
810 /* Given the decl DECL, return the prevailing decl with the same name. */
812 tree
813 lto_symtab_prevailing_decl (tree decl)
815 lto_symtab_entry_t ret;
817 /* Builtins and local symbols are their own prevailing decl. */
818 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
819 return decl;
821 /* DECL_ABSTRACTs are their own prevailng decl. */
822 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
823 return decl;
825 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
826 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
828 /* Walk through the list of candidates and return the one we merged to. */
829 ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
830 if (!ret)
831 return NULL_TREE;
833 return ret->decl;
836 #include "gt-lto-symtab.h"