OSDN Git Service

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