OSDN Git Service

* lto-symtab.c (lto_symtab_free): New function.
[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 htab_hash_string (IDENTIFIER_POINTER (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   /* Finally remove the replaced node.  */
295   varpool_remove_node (vnode);
296 }
297
298 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
299    Return false if the symbols are not fully compatible and a diagnostic
300    should be emitted.  */
301
302 static bool
303 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
304 {
305   tree prevailing_decl = prevailing->decl;
306   tree decl = entry->decl;
307   tree prevailing_type, type;
308
309   /* Merge decl state in both directions, we may still end up using
310      the new decl.  */
311   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
312   TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
313
314   /* The linker may ask us to combine two incompatible symbols.
315      Detect this case and notify the caller of required diagnostics.  */
316
317   if (TREE_CODE (decl) == FUNCTION_DECL)
318     {
319       if (TREE_TYPE (prevailing_decl) != TREE_TYPE (decl))
320         /* If we don't have a merged type yet...sigh.  The linker
321            wouldn't complain if the types were mismatched, so we
322            probably shouldn't either.  Just use the type from
323            whichever decl appears to be associated with the
324            definition.  If for some odd reason neither decl is, the
325            older one wins.  */
326         (void) 0;
327
328       return true;
329     }
330
331   /* Now we exclusively deal with VAR_DECLs.  */
332
333   /* Sharing a global symbol is a strong hint that two types are
334      compatible.  We could use this information to complete
335      incomplete pointed-to types more aggressively here, ignoring
336      mismatches in both field and tag names.  It's difficult though
337      to guarantee that this does not have side-effects on merging
338      more compatible types from other translation units though.  */
339
340   /* We can tolerate differences in type qualification, the
341      qualification of the prevailing definition will prevail.
342      ???  In principle we might want to only warn for structurally
343      incompatible types here, but unless we have protective measures
344      for TBAA in place that would hide useful information.  */
345   prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
346   type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
347
348   /* We have to register and fetch canonical types here as the global
349      fixup process didn't yet run.  */
350   prevailing_type = gimple_register_type (prevailing_type);
351   type = gimple_register_type (type);
352   if (prevailing_type != type)
353     {
354       if (COMPLETE_TYPE_P (type))
355         return false;
356
357       /* If type is incomplete then avoid warnings in the cases
358          that TBAA handles just fine.  */
359
360       if (TREE_CODE (prevailing_type) != TREE_CODE (type))
361         return false;
362
363       if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
364         {
365           tree tem1 = TREE_TYPE (prevailing_type);
366           tree tem2 = TREE_TYPE (type);
367           while (TREE_CODE (tem1) == ARRAY_TYPE
368                  && TREE_CODE (tem2) == ARRAY_TYPE)
369             {
370               tem1 = TREE_TYPE (tem1);
371               tem2 = TREE_TYPE (tem2);
372             }
373
374           if (TREE_CODE (tem1) != TREE_CODE (tem2))
375             return false;
376
377           if (gimple_register_type (tem1) != gimple_register_type (tem2))
378             return false;
379         }
380
381       /* Fallthru.  Compatible enough.  */
382     }
383
384   /* ???  We might want to emit a warning here if type qualification
385      differences were spotted.  Do not do this unconditionally though.  */
386
387   /* There is no point in comparing too many details of the decls here.
388      The type compatibility checks or the completing of types has properly
389      dealt with most issues.  */
390
391   /* The following should all not invoke fatal errors as in non-LTO
392      mode the linker wouldn't complain either.  Just emit warnings.  */
393
394   /* Report a warning if user-specified alignments do not match.  */
395   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
396       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
397     return false;
398
399   return true;
400 }
401
402 /* Return true if the symtab entry E can be replaced by another symtab
403    entry.  */
404
405 static bool
406 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
407 {
408   if (DECL_EXTERNAL (e->decl)
409       || DECL_COMDAT (e->decl)
410       || DECL_WEAK (e->decl))
411     return true;
412
413   if (TREE_CODE (e->decl) == VAR_DECL)
414     return (DECL_COMMON (e->decl)
415             || (!flag_no_common && !DECL_INITIAL (e->decl)));
416
417   return false;
418 }
419
420 /* Return true if the symtab entry E can be the prevailing one.  */
421
422 static bool
423 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
424 {
425   /* The C++ frontend ends up neither setting TREE_STATIC nor
426      DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
427      So do not reject !TREE_STATIC here but only DECL_EXTERNAL.  */
428   if (DECL_EXTERNAL (e->decl))
429     return false;
430
431   /* For functions we need a non-discarded body.  */
432   if (TREE_CODE (e->decl) == FUNCTION_DECL)
433     return (e->node && e->node->analyzed);
434
435   /* A variable should have a size.  */
436   else if (TREE_CODE (e->decl) == VAR_DECL)
437     {
438       if (!e->vnode)
439         return false;
440       if (e->vnode->finalized)
441         return true;
442       return e->vnode->alias && e->vnode->extra_name->finalized;
443     }
444
445   gcc_unreachable ();
446 }
447
448 /* Resolve the symbol with the candidates in the chain *SLOT and store
449    their resolutions.  */
450
451 static void
452 lto_symtab_resolve_symbols (void **slot)
453 {
454   lto_symtab_entry_t e;
455   lto_symtab_entry_t prevailing = NULL;
456
457   /* Always set e->node so that edges are updated to reflect decl merging. */
458   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
459     {
460       if (TREE_CODE (e->decl) == FUNCTION_DECL)
461         e->node = cgraph_get_node (e->decl);
462       else if (TREE_CODE (e->decl) == VAR_DECL)
463         e->vnode = varpool_get_node (e->decl);
464     }
465
466   e = (lto_symtab_entry_t) *slot;
467
468   /* If the chain is already resolved there is nothing else to do.  */
469   if (e->resolution != LDPR_UNKNOWN)
470     return;
471
472   /* Find the single non-replaceable prevailing symbol and
473      diagnose ODR violations.  */
474   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
475     {
476       if (!lto_symtab_resolve_can_prevail_p (e))
477         {
478           e->resolution = LDPR_RESOLVED_IR;
479           continue;
480         }
481
482       /* Set a default resolution - the final prevailing one will get
483          adjusted later.  */
484       e->resolution = LDPR_PREEMPTED_IR;
485       if (!lto_symtab_resolve_replaceable_p (e))
486         {
487           if (prevailing)
488             {
489               error_at (DECL_SOURCE_LOCATION (e->decl),
490                         "%qD has already been defined", e->decl);
491               inform (DECL_SOURCE_LOCATION (prevailing->decl),
492                       "previously defined here");
493             }
494           prevailing = e;
495         }
496     }
497   if (prevailing)
498     goto found;
499
500   /* Do a second round choosing one from the replaceable prevailing decls.  */
501   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
502     {
503       if (e->resolution != LDPR_PREEMPTED_IR)
504         continue;
505
506       /* Choose the first function that can prevail as prevailing.  */
507       if (TREE_CODE (e->decl) == FUNCTION_DECL)
508         {
509           prevailing = e;
510           break;
511         }
512
513       /* From variables that can prevail choose the largest one.  */
514       if (!prevailing
515           || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
516                               DECL_SIZE (e->decl)))
517         prevailing = e;
518     }
519
520   if (!prevailing)
521     return;
522
523 found:
524   if (TREE_CODE (prevailing->decl) == VAR_DECL
525       && TREE_READONLY (prevailing->decl))
526     prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
527   else
528     prevailing->resolution = LDPR_PREVAILING_DEF;
529 }
530
531 /* Merge all decls in the symbol table chain to the prevailing decl and
532    issue diagnostics about type mismatches.  */
533
534 static void
535 lto_symtab_merge_decls_2 (void **slot)
536 {
537   lto_symtab_entry_t prevailing, e;
538   VEC(tree, heap) *mismatches = NULL;
539   unsigned i;
540   tree decl;
541   bool diagnosed_p = false;
542
543   /* Nothing to do for a single entry.  */
544   prevailing = (lto_symtab_entry_t) *slot;
545   if (!prevailing->next)
546     return;
547
548   /* Try to merge each entry with the prevailing one.  */
549   for (e = prevailing->next; e; e = e->next)
550     {
551       if (!lto_symtab_merge (prevailing, e))
552         VEC_safe_push (tree, heap, mismatches, e->decl);
553     }
554   if (VEC_empty (tree, mismatches))
555     return;
556
557   /* Diagnose all mismatched re-declarations.  */
558   for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
559     {
560       if (TREE_TYPE (prevailing->decl) != TREE_TYPE (decl))
561         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
562                                    "type of %qD does not match original "
563                                    "declaration", decl);
564
565       else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
566                && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
567         {
568           diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
569                                      "alignment of %qD is bigger than "
570                                      "original declaration", decl);
571         }
572     }
573   if (diagnosed_p)
574     inform (DECL_SOURCE_LOCATION (prevailing->decl),
575             "previously declared here");
576
577   VEC_free (tree, heap, mismatches);
578 }
579
580 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
581
582 static int
583 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
584 {
585   lto_symtab_entry_t e, prevailing;
586   bool diagnosed_p = false;
587
588   /* Compute the symbol resolutions.  This is a no-op when using the
589      linker plugin.  */
590   lto_symtab_resolve_symbols (slot);
591
592   /* Find the prevailing decl.  */
593   for (prevailing = (lto_symtab_entry_t) *slot;
594        prevailing
595        && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
596        && prevailing->resolution != LDPR_PREVAILING_DEF;
597        prevailing = prevailing->next)
598     ;
599
600   /* Assert it's the only one.  */
601   if (prevailing)
602     for (e = prevailing->next; e; e = e->next)
603       gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
604                   && e->resolution != LDPR_PREVAILING_DEF);
605
606   /* If there's not a prevailing symbol yet it's an external reference.
607      Happens a lot during ltrans.  Choose the first symbol with a
608      cgraph or a varpool node.  */
609   if (!prevailing)
610     {
611       prevailing = (lto_symtab_entry_t) *slot;
612       /* For functions choose one with a cgraph node.  */
613       if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
614         while (!prevailing->node
615                && prevailing->next)
616           prevailing = prevailing->next;
617       /* For variables chose with a priority variant with vnode
618          attached (i.e. from unit where external declaration of
619          variable is actually used).
620          When there are multiple variants, chose one with size.
621          This is needed for C++ typeinfos, for example in
622          lto/20081204-1 there are typeifos in both units, just
623          one of them do have size.  */
624       if (TREE_CODE (prevailing->decl) == VAR_DECL)
625         {
626           for (e = prevailing->next; e; e = e->next)
627             if ((!prevailing->vnode && e->vnode)
628                 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
629                     && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
630                     && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
631               prevailing = e;
632         }
633     }
634
635   /* Move it first in the list.  */
636   if ((lto_symtab_entry_t) *slot != prevailing)
637     {
638       for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
639         ;
640       e->next = prevailing->next;
641       prevailing->next = (lto_symtab_entry_t) *slot;
642       *slot = (void *) prevailing;
643     }
644
645   /* Record the prevailing variable.  */
646   if (TREE_CODE (prevailing->decl) == VAR_DECL)
647     VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
648
649   /* Diagnose mismatched objects.  */
650   for (e = prevailing->next; e; e = e->next)
651     {
652       if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
653         continue;
654
655       switch (TREE_CODE (prevailing->decl))
656         {
657         case VAR_DECL:
658           gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
659           error_at (DECL_SOURCE_LOCATION (e->decl),
660                     "variable %qD redeclared as function", prevailing->decl);
661           break;
662
663         case FUNCTION_DECL:
664           gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
665           error_at (DECL_SOURCE_LOCATION (e->decl),
666                     "function %qD redeclared as variable", prevailing->decl);
667           break;
668
669         default:
670           gcc_unreachable ();
671         }
672
673       diagnosed_p = true;
674     }
675   if (diagnosed_p)
676       inform (DECL_SOURCE_LOCATION (prevailing->decl),
677               "previously declared here");
678
679   /* Register and adjust types of the entries.  */
680   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
681     TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
682
683   /* Merge the chain to the single prevailing decl and diagnose
684      mismatches.  */
685   lto_symtab_merge_decls_2 (slot);
686
687   /* Drop all but the prevailing decl from the symtab.  */
688   if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
689       && TREE_CODE (prevailing->decl) != VAR_DECL)
690     prevailing->next = NULL;
691
692   return 1;
693 }
694
695 /* Resolve and merge all symbol table chains to a prevailing decl.  */
696
697 void
698 lto_symtab_merge_decls (void)
699 {
700   lto_symtab_maybe_init_hash_table ();
701   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
702 }
703
704 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
705
706 static int
707 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
708 {
709   lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
710
711   if (!prevailing->next)
712     return 1;
713
714   /* Replace the cgraph node of each entry with the prevailing one.  */
715   for (e = prevailing->next; e; e = e->next)
716     {
717       if (e->node != NULL)
718         {
719           if (e->node->decl != e->decl && e->node->same_body)
720             {
721               struct cgraph_node *alias;
722
723               for (alias = e->node->same_body; alias; alias = alias->next)
724                 if (alias->decl == e->decl)
725                   break;
726               if (alias)
727                 {
728                   cgraph_remove_same_body_alias (alias);
729                   continue;
730                 }
731             }
732           lto_cgraph_replace_node (e->node, prevailing->node);
733         }
734       if (e->vnode != NULL)
735         lto_varpool_replace_node (e->vnode, prevailing->vnode);
736     }
737
738   /* Drop all but the prevailing decl from the symtab.  */
739   prevailing->next = NULL;
740
741   return 1;
742 }
743
744 /* Merge cgraph nodes according to the symbol merging done by
745    lto_symtab_merge_decls.  */
746
747 void
748 lto_symtab_merge_cgraph_nodes (void)
749 {
750   lto_symtab_maybe_init_hash_table ();
751   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
752 }
753
754 /* Given the decl DECL, return the prevailing decl with the same name. */
755
756 tree
757 lto_symtab_prevailing_decl (tree decl)
758 {
759   lto_symtab_entry_t ret;
760
761   /* Builtins and local symbols are their own prevailing decl.  */
762   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
763     return decl;
764
765   /* DECL_ABSTRACTs are their own prevailng decl.  */
766   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
767     return decl;
768
769   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
770   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
771
772   /* Walk through the list of candidates and return the one we merged to.  */
773   ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
774   if (!ret)
775     return NULL_TREE;
776
777   return ret->decl;
778 }
779
780 #include "gt-lto-symtab.h"