OSDN Git Service

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