OSDN Git Service

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