OSDN Git Service

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