OSDN Git Service

* lto.c (get_filename_for_set): Look for cgraph node and if none found, use
[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   /* There are not supposed to be any outgoing edges from a node we
219      replace.  Still this can happen for multiple instances of weak
220      functions.  */
221   for (e = node->callees; e; e = next)
222     {
223       next = e->next_callee;
224       cgraph_remove_edge (e);
225     }
226
227   if (node->same_body)
228     {
229       struct cgraph_node *alias;
230
231       for (alias = node->same_body; alias; alias = alias->next)
232         if (DECL_ASSEMBLER_NAME_SET_P (alias->decl))
233           {
234             lto_symtab_entry_t se
235               = lto_symtab_get (DECL_ASSEMBLER_NAME (alias->decl));
236
237             for (; se; se = se->next)
238               if (se->node == node)
239                 {
240                   se->node = NULL;
241                   break;
242                 }
243           }
244     }
245
246   /* Finally remove the replaced node.  */
247   cgraph_remove_node (node);
248 }
249
250 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
251    all edges and removing the old node.  */
252
253 static void
254 lto_varpool_replace_node (struct varpool_node *vnode,
255                           struct varpool_node *prevailing_node)
256 {
257   /* Merge node flags.  */
258   if (vnode->needed)
259     {
260       gcc_assert (prevailing_node->analyzed);
261       varpool_mark_needed_node (prevailing_node);
262     }
263   gcc_assert (!vnode->finalized || prevailing_node->finalized);
264   gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
265
266   /* Finally remove the replaced node.  */
267   varpool_remove_node (vnode);
268 }
269
270 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
271    Return false if the symbols are not fully compatible and a diagnostic
272    should be emitted.  */
273
274 static bool
275 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
276 {
277   tree prevailing_decl = prevailing->decl;
278   tree decl = entry->decl;
279   tree prevailing_type, type;
280
281   /* Merge decl state in both directions, we may still end up using
282      the new decl.  */
283   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
284   TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
285
286   /* The linker may ask us to combine two incompatible symbols.
287      Detect this case and notify the caller of required diagnostics.  */
288
289   if (TREE_CODE (decl) == FUNCTION_DECL)
290     {
291       if (TREE_TYPE (prevailing_decl) != TREE_TYPE (decl))
292         /* If we don't have a merged type yet...sigh.  The linker
293            wouldn't complain if the types were mismatched, so we
294            probably shouldn't either.  Just use the type from
295            whichever decl appears to be associated with the
296            definition.  If for some odd reason neither decl is, the
297            older one wins.  */
298         (void) 0;
299
300       return true;
301     }
302
303   /* Now we exclusively deal with VAR_DECLs.  */
304
305   /* Sharing a global symbol is a strong hint that two types are
306      compatible.  We could use this information to complete
307      incomplete pointed-to types more aggressively here, ignoring
308      mismatches in both field and tag names.  It's difficult though
309      to guarantee that this does not have side-effects on merging
310      more compatible types from other translation units though.  */
311
312   /* We can tolerate differences in type qualification, the
313      qualification of the prevailing definition will prevail.
314      ???  In principle we might want to only warn for structurally
315      incompatible types here, but unless we have protective measures
316      for TBAA in place that would hide useful information.  */
317   prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
318   type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
319
320   /* We have to register and fetch canonical types here as the global
321      fixup process didn't yet run.  */
322   prevailing_type = gimple_register_type (prevailing_type);
323   type = gimple_register_type (type);
324   if (prevailing_type != type)
325     {
326       if (COMPLETE_TYPE_P (type))
327         return false;
328
329       /* If type is incomplete then avoid warnings in the cases
330          that TBAA handles just fine.  */
331
332       if (TREE_CODE (prevailing_type) != TREE_CODE (type))
333         return false;
334
335       if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
336         {
337           tree tem1 = TREE_TYPE (prevailing_type);
338           tree tem2 = TREE_TYPE (type);
339           while (TREE_CODE (tem1) == ARRAY_TYPE
340                  && TREE_CODE (tem2) == ARRAY_TYPE)
341             {
342               tem1 = TREE_TYPE (tem1);
343               tem2 = TREE_TYPE (tem2);
344             }
345
346           if (TREE_CODE (tem1) != TREE_CODE (tem2))
347             return false;
348
349           if (gimple_register_type (tem1) != gimple_register_type (tem2))
350             return false;
351         }
352
353       /* Fallthru.  Compatible enough.  */
354     }
355
356   /* ???  We might want to emit a warning here if type qualification
357      differences were spotted.  Do not do this unconditionally though.  */
358
359   /* There is no point in comparing too many details of the decls here.
360      The type compatibility checks or the completing of types has properly
361      dealt with most issues.  */
362
363   /* The following should all not invoke fatal errors as in non-LTO
364      mode the linker wouldn't complain either.  Just emit warnings.  */
365
366   /* Report a warning if user-specified alignments do not match.  */
367   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
368       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
369     return false;
370
371   return true;
372 }
373
374 /* Return true if the symtab entry E can be replaced by another symtab
375    entry.  */
376
377 static bool
378 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
379 {
380   if (DECL_EXTERNAL (e->decl)
381       || DECL_COMDAT (e->decl)
382       || DECL_WEAK (e->decl))
383     return true;
384
385   if (TREE_CODE (e->decl) == VAR_DECL)
386     return (DECL_COMMON (e->decl)
387             || (!flag_no_common && !DECL_INITIAL (e->decl)));
388
389   return false;
390 }
391
392 /* Return true if the symtab entry E can be the prevailing one.  */
393
394 static bool
395 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
396 {
397   /* The C++ frontend ends up neither setting TREE_STATIC nor
398      DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
399      So do not reject !TREE_STATIC here but only DECL_EXTERNAL.  */
400   if (DECL_EXTERNAL (e->decl))
401     return false;
402
403   /* For functions we need a non-discarded body.  */
404   if (TREE_CODE (e->decl) == FUNCTION_DECL)
405     return (e->node && e->node->analyzed);
406
407   /* A variable should have a size.  */
408   else if (TREE_CODE (e->decl) == VAR_DECL)
409     return (DECL_SIZE (e->decl) != NULL_TREE
410             /* The C++ frontend retains TREE_STATIC on the declaration
411                of foo_ in struct Foo { static Foo *foo_; }; but it is
412                not a definition.  g++.dg/lto/20090315_0.C.  */
413             && !DECL_EXTERNAL (e->decl));
414
415   gcc_unreachable ();
416 }
417
418 /* Resolve the symbol with the candidates in the chain *SLOT and store
419    their resolutions.  */
420
421 static void
422 lto_symtab_resolve_symbols (void **slot)
423 {
424   lto_symtab_entry_t e;
425   lto_symtab_entry_t prevailing = NULL;
426
427   /* Always set e->node so that edges are updated to reflect decl merging. */
428   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
429     {
430       if (TREE_CODE (e->decl) == FUNCTION_DECL)
431         e->node = cgraph_get_node (e->decl);
432       else if (TREE_CODE (e->decl) == VAR_DECL)
433         e->vnode = varpool_get_node (e->decl);
434     }
435
436   e = (lto_symtab_entry_t) *slot;
437
438   /* If the chain is already resolved there is nothing else to do.  */
439   if (e->resolution != LDPR_UNKNOWN)
440     return;
441
442   /* Find the single non-replaceable prevailing symbol and
443      diagnose ODR violations.  */
444   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
445     {
446       if (!lto_symtab_resolve_can_prevail_p (e))
447         {
448           e->resolution = LDPR_RESOLVED_IR;
449           continue;
450         }
451
452       /* Set a default resolution - the final prevailing one will get
453          adjusted later.  */
454       e->resolution = LDPR_PREEMPTED_IR;
455       if (!lto_symtab_resolve_replaceable_p (e))
456         {
457           if (prevailing)
458             {
459               error_at (DECL_SOURCE_LOCATION (e->decl),
460                         "%qD has already been defined", e->decl);
461               inform (DECL_SOURCE_LOCATION (prevailing->decl),
462                       "previously defined here");
463             }
464           prevailing = e;
465         }
466     }
467   if (prevailing)
468     goto found;
469
470   /* Do a second round choosing one from the replaceable prevailing decls.  */
471   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
472     {
473       if (e->resolution != LDPR_PREEMPTED_IR)
474         continue;
475
476       /* Choose the first function that can prevail as prevailing.  */
477       if (TREE_CODE (e->decl) == FUNCTION_DECL)
478         {
479           prevailing = e;
480           break;
481         }
482
483       /* From variables that can prevail choose the largest one.  */
484       if (!prevailing
485           || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
486                               DECL_SIZE (e->decl)))
487         prevailing = e;
488     }
489
490   if (!prevailing)
491     return;
492
493 found:
494   if (TREE_CODE (prevailing->decl) == VAR_DECL
495       && TREE_READONLY (prevailing->decl))
496     prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
497   else
498     prevailing->resolution = LDPR_PREVAILING_DEF;
499 }
500
501 /* Merge all decls in the symbol table chain to the prevailing decl and
502    issue diagnostics about type mismatches.  */
503
504 static void
505 lto_symtab_merge_decls_2 (void **slot)
506 {
507   lto_symtab_entry_t prevailing, e;
508   VEC(tree, heap) *mismatches = NULL;
509   unsigned i;
510   tree decl;
511   bool diagnosed_p = false;
512
513   /* Nothing to do for a single entry.  */
514   prevailing = (lto_symtab_entry_t) *slot;
515   if (!prevailing->next)
516     return;
517
518   /* Try to merge each entry with the prevailing one.  */
519   for (e = prevailing->next; e; e = e->next)
520     {
521       if (!lto_symtab_merge (prevailing, e))
522         VEC_safe_push (tree, heap, mismatches, e->decl);
523     }
524   if (VEC_empty (tree, mismatches))
525     return;
526
527   /* Diagnose all mismatched re-declarations.  */
528   for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
529     {
530       if (TREE_TYPE (prevailing->decl) != TREE_TYPE (decl))
531         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
532                                    "type of %qD does not match original "
533                                    "declaration", decl);
534
535       else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
536                && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
537         {
538           diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
539                                      "alignment of %qD is bigger than "
540                                      "original declaration", decl);
541         }
542     }
543   if (diagnosed_p)
544     inform (DECL_SOURCE_LOCATION (prevailing->decl),
545             "previously declared here");
546
547   VEC_free (tree, heap, mismatches);
548 }
549
550 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
551
552 static int
553 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
554 {
555   lto_symtab_entry_t e, prevailing;
556   bool diagnosed_p = false;
557
558   /* Compute the symbol resolutions.  This is a no-op when using the
559      linker plugin.  */
560   lto_symtab_resolve_symbols (slot);
561
562   /* Find the prevailing decl.  */
563   for (prevailing = (lto_symtab_entry_t) *slot;
564        prevailing
565        && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
566        && prevailing->resolution != LDPR_PREVAILING_DEF;
567        prevailing = prevailing->next)
568     ;
569
570   /* Assert it's the only one.  */
571   if (prevailing)
572     for (e = prevailing->next; e; e = e->next)
573       gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
574                   && e->resolution != LDPR_PREVAILING_DEF);
575
576   /* If there's not a prevailing symbol yet it's an external reference.
577      Happens a lot during ltrans.  Choose the first symbol with a
578      cgraph or a varpool node.  */
579   if (!prevailing)
580     {
581       prevailing = (lto_symtab_entry_t) *slot;
582       /* For functions choose one with a cgraph node.  */
583       if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
584         while (!prevailing->node
585                && prevailing->next)
586           prevailing = prevailing->next;
587       if (TREE_CODE (prevailing->decl) == VAR_DECL)
588         while (!prevailing->vnode
589                && prevailing->next)
590           prevailing = prevailing->next;
591       /* We do not stream varpool nodes, so the first decl has to
592          be good enough for now.
593          ???  For QOI choose a variable with readonly initializer
594          if there is one.  This matches C++
595          struct Foo { static const int i = 1; }; without a real
596          definition.  */
597       if (TREE_CODE (prevailing->decl) == VAR_DECL)
598         while (!(TREE_READONLY (prevailing->decl)
599                  && DECL_INITIAL (prevailing->decl))
600                && prevailing->next)
601           prevailing = prevailing->next;
602     }
603
604   /* Move it first in the list.  */
605   if ((lto_symtab_entry_t) *slot != prevailing)
606     {
607       for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
608         ;
609       e->next = prevailing->next;
610       prevailing->next = (lto_symtab_entry_t) *slot;
611       *slot = (void *) prevailing;
612     }
613
614   /* Record the prevailing variable.  */
615   if (TREE_CODE (prevailing->decl) == VAR_DECL)
616     VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
617
618   /* Diagnose mismatched objects.  */
619   for (e = prevailing->next; e; e = e->next)
620     {
621       if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
622         continue;
623
624       switch (TREE_CODE (prevailing->decl))
625         {
626         case VAR_DECL:
627           gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
628           error_at (DECL_SOURCE_LOCATION (e->decl),
629                     "variable %qD redeclared as function", prevailing->decl);
630           break;
631
632         case FUNCTION_DECL:
633           gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
634           error_at (DECL_SOURCE_LOCATION (e->decl),
635                     "function %qD redeclared as variable", prevailing->decl);
636           break;
637
638         default:
639           gcc_unreachable ();
640         }
641
642       diagnosed_p = true;
643     }
644   if (diagnosed_p)
645       inform (DECL_SOURCE_LOCATION (prevailing->decl),
646               "previously declared here");
647
648   /* Register and adjust types of the entries.  */
649   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
650     TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
651
652   /* Merge the chain to the single prevailing decl and diagnose
653      mismatches.  */
654   lto_symtab_merge_decls_2 (slot);
655
656   /* Drop all but the prevailing decl from the symtab.  */
657   if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
658       && TREE_CODE (prevailing->decl) != VAR_DECL)
659     prevailing->next = NULL;
660
661   return 1;
662 }
663
664 /* Resolve and merge all symbol table chains to a prevailing decl.  */
665
666 void
667 lto_symtab_merge_decls (void)
668 {
669   lto_symtab_maybe_init_hash_table ();
670   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
671 }
672
673 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
674
675 static int
676 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
677 {
678   lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
679
680   if (!prevailing->next)
681     return 1;
682
683   /* Replace the cgraph node of each entry with the prevailing one.  */
684   for (e = prevailing->next; e; e = e->next)
685     {
686       if (e->node != NULL)
687         {
688           if (e->node->decl != e->decl && e->node->same_body)
689             {
690               struct cgraph_node *alias;
691
692               for (alias = e->node->same_body; alias; alias = alias->next)
693                 if (alias->decl == e->decl)
694                   break;
695               if (alias)
696                 {
697                   cgraph_remove_same_body_alias (alias);
698                   continue;
699                 }
700             }
701           lto_cgraph_replace_node (e->node, prevailing->node);
702         }
703       if (e->vnode != NULL)
704         lto_varpool_replace_node (e->vnode, prevailing->vnode);
705     }
706
707   /* Drop all but the prevailing decl from the symtab.  */
708   prevailing->next = NULL;
709
710   return 1;
711 }
712
713 /* Merge cgraph nodes according to the symbol merging done by
714    lto_symtab_merge_decls.  */
715
716 void
717 lto_symtab_merge_cgraph_nodes (void)
718 {
719   lto_symtab_maybe_init_hash_table ();
720   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
721 }
722
723 /* Given the decl DECL, return the prevailing decl with the same name. */
724
725 tree
726 lto_symtab_prevailing_decl (tree decl)
727 {
728   lto_symtab_entry_t ret;
729
730   /* Builtins and local symbols are their own prevailing decl.  */
731   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
732     return decl;
733
734   /* DECL_ABSTRACTs are their own prevailng decl.  */
735   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
736     return decl;
737
738   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
739   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
740
741   /* Walk through the list of candidates and return the one we merged to.  */
742   ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
743   if (!ret)
744     return NULL_TREE;
745
746   return ret->decl;
747 }
748
749 #include "gt-lto-symtab.h"