OSDN Git Service

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