OSDN Git Service

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