OSDN Git Service

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