OSDN Git Service

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