OSDN Git Service

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