OSDN Git Service

* ipa-inline.c (update_edge_key): Break out from ...
[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"
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_alloc_cleared_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         {
467           e->vnode = varpool_get_node (e->decl);
468           /* The LTO plugin for gold doesn't handle common symbols
469              properly.  Let us choose manually.  */
470           if (DECL_COMMON (e->decl))
471             e->resolution = LDPR_UNKNOWN;
472         }
473     }
474
475   e = (lto_symtab_entry_t) *slot;
476
477   /* If the chain is already resolved there is nothing else to do.  */
478   if (e->resolution != LDPR_UNKNOWN)
479     return;
480
481   /* Find the single non-replaceable prevailing symbol and
482      diagnose ODR violations.  */
483   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
484     {
485       if (!lto_symtab_resolve_can_prevail_p (e))
486         {
487           e->resolution = LDPR_RESOLVED_IR;
488           continue;
489         }
490
491       /* Set a default resolution - the final prevailing one will get
492          adjusted later.  */
493       e->resolution = LDPR_PREEMPTED_IR;
494       if (!lto_symtab_resolve_replaceable_p (e))
495         {
496           if (prevailing)
497             {
498               error_at (DECL_SOURCE_LOCATION (e->decl),
499                         "%qD has already been defined", e->decl);
500               inform (DECL_SOURCE_LOCATION (prevailing->decl),
501                       "previously defined here");
502             }
503           prevailing = e;
504         }
505     }
506   if (prevailing)
507     goto found;
508
509   /* Do a second round choosing one from the replaceable prevailing decls.  */
510   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
511     {
512       if (e->resolution != LDPR_PREEMPTED_IR)
513         continue;
514
515       /* Choose the first function that can prevail as prevailing.  */
516       if (TREE_CODE (e->decl) == FUNCTION_DECL)
517         {
518           prevailing = e;
519           break;
520         }
521
522       /* From variables that can prevail choose the largest one.  */
523       if (!prevailing
524           || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
525                               DECL_SIZE (e->decl)))
526         prevailing = e;
527     }
528
529   if (!prevailing)
530     return;
531
532 found:
533   /* If current lto files represent the whole program,
534     it is correct to use LDPR_PREVALING_DEF_IRONLY.
535     If current lto files are part of whole program, internal
536     resolver doesn't know if it is LDPR_PREVAILING_DEF
537     or LDPR_PREVAILING_DEF_IRONLY.  Use IRONLY conforms to
538     using -fwhole-program.  Otherwise, it doesn't
539     matter using either LDPR_PREVAILING_DEF or
540     LDPR_PREVAILING_DEF_IRONLY
541     
542     FIXME: above workaround due to gold plugin makes some
543     variables IRONLY, which are indeed PREVAILING_DEF in
544     resolution file.  These variables still need manual
545     externally_visible attribute.  */
546     prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
547 }
548
549 /* Merge all decls in the symbol table chain to the prevailing decl and
550    issue diagnostics about type mismatches.  */
551
552 static void
553 lto_symtab_merge_decls_2 (void **slot)
554 {
555   lto_symtab_entry_t prevailing, e;
556   VEC(tree, heap) *mismatches = NULL;
557   unsigned i;
558   tree decl;
559   bool diagnosed_p = false;
560
561   /* Nothing to do for a single entry.  */
562   prevailing = (lto_symtab_entry_t) *slot;
563   if (!prevailing->next)
564     return;
565
566   /* Try to merge each entry with the prevailing one.  */
567   for (e = prevailing->next; e; e = e->next)
568     {
569       if (!lto_symtab_merge (prevailing, e))
570         VEC_safe_push (tree, heap, mismatches, e->decl);
571     }
572   if (VEC_empty (tree, mismatches))
573     return;
574
575   /* Diagnose all mismatched re-declarations.  */
576   for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
577     {
578       if (TREE_TYPE (prevailing->decl) != TREE_TYPE (decl))
579         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
580                                    "type of %qD does not match original "
581                                    "declaration", decl);
582
583       else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
584                && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
585         {
586           diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
587                                      "alignment of %qD is bigger than "
588                                      "original declaration", decl);
589         }
590     }
591   if (diagnosed_p)
592     inform (DECL_SOURCE_LOCATION (prevailing->decl),
593             "previously declared here");
594
595   VEC_free (tree, heap, mismatches);
596 }
597
598 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
599
600 static int
601 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
602 {
603   lto_symtab_entry_t e, prevailing;
604   bool diagnosed_p = false;
605
606   /* Compute the symbol resolutions.  This is a no-op when using the
607      linker plugin.  */
608   lto_symtab_resolve_symbols (slot);
609
610   /* Find the prevailing decl.  */
611   for (prevailing = (lto_symtab_entry_t) *slot;
612        prevailing
613        && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
614        && prevailing->resolution != LDPR_PREVAILING_DEF;
615        prevailing = prevailing->next)
616     ;
617
618   /* Assert it's the only one.  */
619   if (prevailing)
620     for (e = prevailing->next; e; e = e->next)
621       gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
622                   && e->resolution != LDPR_PREVAILING_DEF);
623
624   /* If there's not a prevailing symbol yet it's an external reference.
625      Happens a lot during ltrans.  Choose the first symbol with a
626      cgraph or a varpool node.  */
627   if (!prevailing)
628     {
629       prevailing = (lto_symtab_entry_t) *slot;
630       /* For functions choose one with a cgraph node.  */
631       if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
632         while (!prevailing->node
633                && prevailing->next)
634           prevailing = prevailing->next;
635       /* For variables chose with a priority variant with vnode
636          attached (i.e. from unit where external declaration of
637          variable is actually used).
638          When there are multiple variants, chose one with size.
639          This is needed for C++ typeinfos, for example in
640          lto/20081204-1 there are typeifos in both units, just
641          one of them do have size.  */
642       if (TREE_CODE (prevailing->decl) == VAR_DECL)
643         {
644           for (e = prevailing->next; e; e = e->next)
645             if ((!prevailing->vnode && e->vnode)
646                 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
647                     && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
648                     && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
649               prevailing = e;
650         }
651     }
652
653   /* Move it first in the list.  */
654   if ((lto_symtab_entry_t) *slot != prevailing)
655     {
656       for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
657         ;
658       e->next = prevailing->next;
659       prevailing->next = (lto_symtab_entry_t) *slot;
660       *slot = (void *) prevailing;
661     }
662
663   /* Record the prevailing variable.  */
664   if (TREE_CODE (prevailing->decl) == VAR_DECL)
665     VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
666
667   /* Diagnose mismatched objects.  */
668   for (e = prevailing->next; e; e = e->next)
669     {
670       if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
671         continue;
672
673       switch (TREE_CODE (prevailing->decl))
674         {
675         case VAR_DECL:
676           gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
677           error_at (DECL_SOURCE_LOCATION (e->decl),
678                     "variable %qD redeclared as function", prevailing->decl);
679           break;
680
681         case FUNCTION_DECL:
682           gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
683           error_at (DECL_SOURCE_LOCATION (e->decl),
684                     "function %qD redeclared as variable", prevailing->decl);
685           break;
686
687         default:
688           gcc_unreachable ();
689         }
690
691       diagnosed_p = true;
692     }
693   if (diagnosed_p)
694       inform (DECL_SOURCE_LOCATION (prevailing->decl),
695               "previously declared here");
696
697   /* Register and adjust types of the entries.  */
698   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
699     TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
700
701   /* Merge the chain to the single prevailing decl and diagnose
702      mismatches.  */
703   lto_symtab_merge_decls_2 (slot);
704
705   /* Drop all but the prevailing decl from the symtab.  */
706   if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
707       && TREE_CODE (prevailing->decl) != VAR_DECL)
708     prevailing->next = NULL;
709
710   /* Set externally_visible flags for declaration of LDPR_PREVAILING_DEF */
711   if (flag_whole_program)
712     {
713       if (prevailing->resolution == LDPR_PREVAILING_DEF)
714         {
715           if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
716             prevailing->node->local.used_from_object_file = true;
717           else
718             prevailing->vnode->used_from_object_file = true;
719         }
720       else if (prevailing->resolution == LDPR_PREVAILING_DEF_IRONLY)
721         {
722           if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
723             prevailing->node->local.used_from_object_file = false;
724           else
725             prevailing->vnode->used_from_object_file = false;
726         }
727     }
728   return 1;
729 }
730
731 /* Resolve and merge all symbol table chains to a prevailing decl.  */
732
733 void
734 lto_symtab_merge_decls (void)
735 {
736   lto_symtab_maybe_init_hash_table ();
737   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
738 }
739
740 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
741
742 static int
743 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
744 {
745   lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
746
747   if (!prevailing->next)
748     return 1;
749
750   /* Replace the cgraph node of each entry with the prevailing one.  */
751   for (e = prevailing->next; e; e = e->next)
752     {
753       if (e->node != NULL)
754         {
755           if (e->node->decl != e->decl && e->node->same_body)
756             {
757               struct cgraph_node *alias;
758
759               for (alias = e->node->same_body; alias; alias = alias->next)
760                 if (alias->decl == e->decl)
761                   break;
762               if (alias)
763                 {
764                   cgraph_remove_same_body_alias (alias);
765                   continue;
766                 }
767             }
768           lto_cgraph_replace_node (e->node, prevailing->node);
769         }
770       if (e->vnode != NULL)
771         lto_varpool_replace_node (e->vnode, prevailing->vnode);
772     }
773
774   /* Drop all but the prevailing decl from the symtab.  */
775   prevailing->next = NULL;
776
777   return 1;
778 }
779
780 /* Merge cgraph nodes according to the symbol merging done by
781    lto_symtab_merge_decls.  */
782
783 void
784 lto_symtab_merge_cgraph_nodes (void)
785 {
786   lto_symtab_maybe_init_hash_table ();
787   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
788 }
789
790 /* Given the decl DECL, return the prevailing decl with the same name. */
791
792 tree
793 lto_symtab_prevailing_decl (tree decl)
794 {
795   lto_symtab_entry_t ret;
796
797   /* Builtins and local symbols are their own prevailing decl.  */
798   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
799     return decl;
800
801   /* DECL_ABSTRACTs are their own prevailng decl.  */
802   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
803     return decl;
804
805   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
806   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
807
808   /* Walk through the list of candidates and return the one we merged to.  */
809   ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
810   if (!ret)
811     return NULL_TREE;
812
813   return ret->decl;
814 }
815
816 #include "gt-lto-symtab.h"