OSDN Git Service

2009-10-10 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[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   /* LTO file-data and symbol resolution for this decl.  */
45   struct lto_file_decl_data * GTY((skip (""))) file_data;
46   enum ld_plugin_symbol_resolution resolution;
47   /* Pointer to the next entry with the same key.  Before decl merging
48      this links all symbols from the different TUs.  After decl merging
49      this links merged but incompatible decls, thus all prevailing ones
50      remaining.  */
51   struct lto_symtab_entry_def *next;
52 };
53 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
54
55 /* A poor man's symbol table. This hashes identifier to prevailing DECL
56    if there is one. */
57
58 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
59              param_is (struct lto_symtab_entry_def)))
60   htab_t lto_symtab_identifiers;
61
62 /* Return the hash value of an lto_symtab_entry_t object pointed to by P.  */
63
64 static hashval_t
65 lto_symtab_entry_hash (const void *p)
66 {
67   const struct lto_symtab_entry_def *base =
68     (const struct lto_symtab_entry_def *) p;
69   return htab_hash_string (IDENTIFIER_POINTER (base->id));
70 }
71
72 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
73    corresponding to the same symbol.  */
74
75 static int
76 lto_symtab_entry_eq (const void *p1, const void *p2)
77 {
78   const struct lto_symtab_entry_def *base1 =
79      (const struct lto_symtab_entry_def *) p1;
80   const struct lto_symtab_entry_def *base2 =
81      (const struct lto_symtab_entry_def *) p2;
82   return (base1->id == base2->id);
83 }
84
85 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
86    to be marked for GC.  */ 
87
88 static int
89 lto_symtab_entry_marked_p (const void *p)
90 {
91   const struct lto_symtab_entry_def *base =
92      (const struct lto_symtab_entry_def *) p;
93
94   /* Keep this only if the decl or the chain is marked.  */
95   return (ggc_marked_p (base->decl)
96           || (base->next && ggc_marked_p (base->next)));
97 }
98
99 /* Lazily initialize resolution hash tables.  */
100
101 static void
102 lto_symtab_maybe_init_hash_table (void)
103 {
104   if (lto_symtab_identifiers)
105     return;
106
107   lto_symtab_identifiers =
108     htab_create_ggc (1021, lto_symtab_entry_hash,
109                      lto_symtab_entry_eq, NULL);
110 }
111
112 static bool maybe_merge_incomplete_and_complete_type (tree, tree);
113
114 /* Try to merge an incomplete type INCOMPLETE with a complete type
115    COMPLETE of same kinds.
116    Return true if they were merged, false otherwise.  */
117
118 static bool
119 merge_incomplete_and_complete_type (tree incomplete, tree complete)
120 {
121   /* For merging array types do some extra sanity checking.  */
122   if (TREE_CODE (incomplete) == ARRAY_TYPE
123       && !maybe_merge_incomplete_and_complete_type (TREE_TYPE (incomplete),
124                                                     TREE_TYPE (complete))
125       && !gimple_types_compatible_p (TREE_TYPE (incomplete),
126                                      TREE_TYPE (complete)))
127     return false;
128
129   /* ??? Ideally we would do this by means of a common canonical type, but
130      that's difficult as we do not have links from the canonical type
131      back to all its children.  */
132   gimple_force_type_merge (incomplete, complete);
133
134   return true;
135 }
136
137 /* Try to merge a maybe complete / incomplete type pair TYPE1 and TYPE2.
138    Return true if they were merged, false otherwise.  */
139
140 static bool
141 maybe_merge_incomplete_and_complete_type (tree type1, tree type2)
142 {
143   bool res = false;
144
145   if (TREE_CODE (type1) != TREE_CODE (type2))
146     return false;
147
148   if (!COMPLETE_TYPE_P (type1) && COMPLETE_TYPE_P (type2))
149     res = merge_incomplete_and_complete_type (type1, type2);
150   else if (COMPLETE_TYPE_P (type1) && !COMPLETE_TYPE_P (type2))
151     res = merge_incomplete_and_complete_type (type2, type1);
152
153   /* Recurse on pointer targets.  */
154   if (!res
155       && POINTER_TYPE_P (type1)
156       && POINTER_TYPE_P (type2))
157     res = maybe_merge_incomplete_and_complete_type (TREE_TYPE (type1),
158                                                     TREE_TYPE (type2));
159
160   return res;
161 }
162
163 /* Check if OLD_DECL and NEW_DECL are compatible. */
164
165 static bool
166 lto_symtab_compatible (tree old_decl, tree new_decl)
167 {
168   tree old_type, new_type;
169
170   if (TREE_CODE (old_decl) != TREE_CODE (new_decl))
171     {
172       switch (TREE_CODE (new_decl))
173         {
174         case VAR_DECL:
175           gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL);
176           error_at (DECL_SOURCE_LOCATION (new_decl),
177                     "function %qD redeclared as variable", new_decl);
178           inform (DECL_SOURCE_LOCATION (old_decl),
179                   "previously declared here");
180           return false;
181
182         case FUNCTION_DECL:
183           gcc_assert (TREE_CODE (old_decl) == VAR_DECL);
184           error_at (DECL_SOURCE_LOCATION (new_decl),
185                     "variable %qD redeclared as function", new_decl);
186           inform (DECL_SOURCE_LOCATION (old_decl),
187                   "previously declared here");
188           return false;
189
190         default:
191           gcc_unreachable ();
192         }
193     }
194
195   if (TREE_CODE (new_decl) == FUNCTION_DECL)
196     {
197       if (!gimple_types_compatible_p (TREE_TYPE (old_decl),
198                                       TREE_TYPE (new_decl)))
199         /* If we don't have a merged type yet...sigh.  The linker
200            wouldn't complain if the types were mismatched, so we
201            probably shouldn't either.  Just use the type from
202            whichever decl appears to be associated with the
203            definition.  If for some odd reason neither decl is, the
204            older one wins.  */
205         (void) 0;
206
207       return true;
208     }
209
210   /* Now we exclusively deal with VAR_DECLs.  */
211
212   /* Handle external declarations with incomplete type or pointed-to
213      incomplete types by forcefully merging the types.
214      ???  In principle all types involved in the two decls should
215      be merged forcefully, for example without considering type or
216      field names.  */
217   old_type = TREE_TYPE (old_decl);
218   new_type = TREE_TYPE (new_decl);
219
220   if (DECL_EXTERNAL (old_decl) || DECL_EXTERNAL (new_decl))
221     maybe_merge_incomplete_and_complete_type (old_type, new_type);
222   else if (POINTER_TYPE_P (old_type)
223            && POINTER_TYPE_P (new_type))
224     maybe_merge_incomplete_and_complete_type (TREE_TYPE (old_type),
225                                               TREE_TYPE (new_type));
226
227   /* For array types we have to accept external declarations with
228      different sizes than the actual definition (164.gzip).
229      ???  We could emit a warning here.  */
230   if (TREE_CODE (old_type) == TREE_CODE (new_type)
231       && TREE_CODE (old_type) == ARRAY_TYPE
232       && COMPLETE_TYPE_P (old_type)
233       && COMPLETE_TYPE_P (new_type)
234       && tree_int_cst_compare (TYPE_SIZE (old_type),
235                                TYPE_SIZE (new_type)) != 0
236       && gimple_types_compatible_p (TREE_TYPE (old_type),
237                                     TREE_TYPE (new_type)))
238     {
239       /* If only one is external use the type of the non-external decl.
240          Else use the larger one and also adjust the decl size.
241          ???  Directional merging would allow us to simply pick the
242          larger one instead of rewriting it.  */
243       if (DECL_EXTERNAL (old_decl) ^ DECL_EXTERNAL (new_decl))
244         {
245           if (DECL_EXTERNAL (old_decl))
246             TREE_TYPE (old_decl) = new_type;
247           else if (DECL_EXTERNAL (new_decl))
248             TREE_TYPE (new_decl) = old_type;
249         }
250       else
251         {
252           if (tree_int_cst_compare (TYPE_SIZE (old_type),
253                                     TYPE_SIZE (new_type)) < 0)
254             {
255               TREE_TYPE (old_decl) = new_type;
256               DECL_SIZE (old_decl) = DECL_SIZE (new_decl);
257               DECL_SIZE_UNIT (old_decl) = DECL_SIZE_UNIT (new_decl);
258             }
259           else
260             {
261               TREE_TYPE (new_decl) = old_type;
262               DECL_SIZE (new_decl) = DECL_SIZE (old_decl);
263               DECL_SIZE_UNIT (new_decl) = DECL_SIZE_UNIT (old_decl);
264             }
265         }
266     }
267
268   /* We can tolerate differences in type qualification, the
269      qualification of the prevailing definition will prevail.  */
270   old_type = TYPE_MAIN_VARIANT (TREE_TYPE (old_decl));
271   new_type = TYPE_MAIN_VARIANT (TREE_TYPE (new_decl));
272   if (!gimple_types_compatible_p (old_type, new_type))
273     {
274       if (warning_at (DECL_SOURCE_LOCATION (new_decl), 0,
275                       "type of %qD does not match original declaration",
276                       new_decl))
277         inform (DECL_SOURCE_LOCATION (old_decl),
278                 "previously declared here");
279       return false;
280     }
281
282   /* ???  We might want to emit a warning here if type qualification
283      differences were spotted.  Do not do this unconditionally though.  */
284
285   /* There is no point in comparing too many details of the decls here.
286      The type compatibility checks or the completing of types has properly
287      dealt with most issues.  */
288
289   /* The following should all not invoke fatal errors as in non-LTO
290      mode the linker wouldn't complain either.  Just emit warnings.  */
291
292   /* Report a warning if user-specified alignments do not match.  */
293   if ((DECL_USER_ALIGN (old_decl) && DECL_USER_ALIGN (new_decl))
294       && DECL_ALIGN (old_decl) != DECL_ALIGN (new_decl))
295     {
296       warning_at (DECL_SOURCE_LOCATION (new_decl), 0,
297                   "alignment of %qD does not match original declaration",
298                   new_decl);
299       inform (DECL_SOURCE_LOCATION (old_decl), "previously declared here");
300       return false;
301     }
302
303   return true;
304 }
305
306 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
307    and read from FILE_DATA. */
308
309 void
310 lto_symtab_register_decl (tree decl,
311                           ld_plugin_symbol_resolution_t resolution,
312                           struct lto_file_decl_data *file_data)
313 {
314   lto_symtab_entry_t new_entry;
315   void **slot;
316
317   /* Check that declarations reaching this function do not have
318      properties inconsistent with having external linkage.  If any of
319      these asertions fail, then the object file reader has failed to
320      detect these cases and issue appropriate error messages.  */
321   gcc_assert (decl
322               && TREE_PUBLIC (decl)
323               && (TREE_CODE (decl) == VAR_DECL
324                   || TREE_CODE (decl) == FUNCTION_DECL)
325               && DECL_ASSEMBLER_NAME_SET_P (decl));
326   if (TREE_CODE (decl) == VAR_DECL
327       && DECL_INITIAL (decl))
328     gcc_assert (!DECL_EXTERNAL (decl)
329                 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
330   if (TREE_CODE (decl) == FUNCTION_DECL)
331     gcc_assert (!DECL_ABSTRACT (decl));
332
333   new_entry = GGC_CNEW (struct lto_symtab_entry_def);
334   new_entry->id = DECL_ASSEMBLER_NAME (decl);
335   new_entry->decl = decl;
336   new_entry->resolution = resolution;
337   new_entry->file_data = file_data;
338   
339   lto_symtab_maybe_init_hash_table ();
340   slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
341   new_entry->next = (lto_symtab_entry_t) *slot;
342   *slot = new_entry;
343 }
344
345 /* Get the lto_symtab_entry_def struct associated with ID
346    if there is one.  */
347
348 static lto_symtab_entry_t
349 lto_symtab_get (tree id)
350 {
351   struct lto_symtab_entry_def temp;
352   void **slot;
353
354   lto_symtab_maybe_init_hash_table ();
355   temp.id = id;
356   slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
357   return slot ? (lto_symtab_entry_t) *slot : NULL;
358 }
359
360 /* Get the linker resolution for DECL.  */
361
362 enum ld_plugin_symbol_resolution
363 lto_symtab_get_resolution (tree decl)
364 {
365   lto_symtab_entry_t e;
366
367   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
368
369   e = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
370   while (e && e->decl != decl)
371     e = e->next;
372   if (!e)
373     return LDPR_UNKNOWN;
374
375   return e->resolution;
376 }
377
378 /* Replace the cgraph node OLD_NODE with NEW_NODE in the cgraph, merging
379    all edges and removing the old node.  */
380
381 static void
382 lto_cgraph_replace_node (struct cgraph_node *old_node,
383                          struct cgraph_node *new_node)
384 {
385   struct cgraph_edge *e, *next;
386
387   /* Merge node flags.  */
388   if (old_node->needed)
389     cgraph_mark_needed_node (new_node);
390   if (old_node->reachable)
391     cgraph_mark_reachable_node (new_node);
392   if (old_node->address_taken)
393     {
394       gcc_assert (!new_node->global.inlined_to);
395       cgraph_mark_address_taken_node (new_node);
396     }
397
398   /* Redirect all incoming edges.  */
399   for (e = old_node->callers; e; e = next)
400     {
401       next = e->next_caller;
402       cgraph_redirect_edge_callee (e, new_node);
403     }
404
405   /* There are not supposed to be any outgoing edges from a node we
406      replace.  Still this can happen for multiple instances of weak
407      functions.
408      ???  For now do what the old code did.  Do not create edges for them.  */
409   for (e = old_node->callees; e; e = next)
410     {
411       next = e->next_callee;
412       cgraph_remove_edge (e);
413     }
414
415   /* Finally remove the replaced node.  */
416   cgraph_remove_node (old_node);
417 }
418
419 /* Merge two variable or function symbol table entries ENTRY1 and ENTRY2.
420    Return the prevailing one or NULL if a merge is not possible.  */
421
422 static lto_symtab_entry_t
423 lto_symtab_merge (lto_symtab_entry_t entry1, lto_symtab_entry_t entry2)
424 {
425   tree old_decl = entry1->decl;
426   tree new_decl = entry2->decl;
427   ld_plugin_symbol_resolution_t old_resolution = entry1->resolution;
428   ld_plugin_symbol_resolution_t new_resolution = entry2->resolution;
429   struct cgraph_node *old_node = NULL;
430   struct cgraph_node *new_node = NULL;
431
432   /* Give ODR violation errors.  */
433   if (new_resolution == LDPR_PREVAILING_DEF
434       || new_resolution == LDPR_PREVAILING_DEF_IRONLY)
435     {
436       if ((old_resolution == LDPR_PREVAILING_DEF
437            || old_resolution == LDPR_PREVAILING_DEF_IRONLY)
438           && (old_resolution != new_resolution || flag_no_common))
439         {
440           error_at (DECL_SOURCE_LOCATION (new_decl),
441                     "%qD has already been defined", new_decl);
442           inform (DECL_SOURCE_LOCATION (old_decl),
443                   "previously defined here");
444           return NULL;
445         }
446     }
447
448   /* The linker may ask us to combine two incompatible symbols.  */
449   if (!lto_symtab_compatible (old_decl, new_decl))
450     return NULL;
451
452   if (TREE_CODE (old_decl) == FUNCTION_DECL)
453     old_node = cgraph_get_node (old_decl);
454   if (TREE_CODE (new_decl) == FUNCTION_DECL)
455     new_node = cgraph_get_node (new_decl);
456
457   /* Merge decl state in both directions, we may still end up using
458      the new decl.  */
459   TREE_ADDRESSABLE (old_decl) |= TREE_ADDRESSABLE (new_decl);
460   TREE_ADDRESSABLE (new_decl) |= TREE_ADDRESSABLE (old_decl);
461
462   gcc_assert (new_resolution != LDPR_UNKNOWN
463               && new_resolution != LDPR_UNDEF
464               && old_resolution != LDPR_UNKNOWN
465               && old_resolution != LDPR_UNDEF);
466
467   if (new_resolution == LDPR_PREVAILING_DEF
468       || new_resolution == LDPR_PREVAILING_DEF_IRONLY
469       || (!old_node && new_node))
470     {
471       gcc_assert ((!old_node && new_node)
472                   || old_resolution == LDPR_PREEMPTED_IR
473                   || old_resolution ==  LDPR_RESOLVED_IR
474                   || (old_resolution == new_resolution && !flag_no_common));
475       if (old_node)
476         lto_cgraph_replace_node (old_node, new_node);
477       /* Choose new_decl, entry2.  */
478       return entry2;
479     }
480
481   if (new_resolution == LDPR_PREEMPTED_REG
482       || new_resolution == LDPR_RESOLVED_EXEC
483       || new_resolution == LDPR_RESOLVED_DYN)
484     gcc_assert (old_resolution == LDPR_PREEMPTED_REG
485                 || old_resolution == LDPR_RESOLVED_EXEC
486                 || old_resolution == LDPR_RESOLVED_DYN);
487
488   if (new_resolution == LDPR_PREEMPTED_IR
489       || new_resolution == LDPR_RESOLVED_IR)
490     gcc_assert (old_resolution == LDPR_PREVAILING_DEF
491                 || old_resolution == LDPR_PREVAILING_DEF_IRONLY
492                 || old_resolution == LDPR_PREEMPTED_IR
493                 || old_resolution == LDPR_RESOLVED_IR);
494
495   if (new_node)
496     lto_cgraph_replace_node (new_node, old_node);
497
498   /* Choose old_decl, entry1.  */
499   return entry1;
500 }
501
502 /* Resolve the symbol with the candidates in the chain *SLOT and store
503    their resolutions.  */
504
505 static void
506 lto_symtab_resolve_symbols (void **slot)
507 {
508   lto_symtab_entry_t e = (lto_symtab_entry_t) *slot;
509
510   /* If the chain is already resolved there is nothing to do.  */
511   if (e->resolution != LDPR_UNKNOWN)
512     return;
513
514   /* This is a poor mans resolver.  */
515   for (; e; e = e->next)
516     {
517       gcc_assert (e->resolution == LDPR_UNKNOWN);
518       if (DECL_EXTERNAL (e->decl)
519           || (TREE_CODE (e->decl) == FUNCTION_DECL
520               && !cgraph_get_node (e->decl)))
521         e->resolution = LDPR_RESOLVED_IR;
522       else
523         {
524           if (TREE_READONLY (e->decl))
525             e->resolution = LDPR_PREVAILING_DEF_IRONLY;
526           else
527             e->resolution = LDPR_PREVAILING_DEF;
528         }
529     }
530 }
531
532 /* Merge one symbol table chain to a (set of) prevailing decls.  */
533
534 static void
535 lto_symtab_merge_decls_2 (void **slot)
536 {
537   lto_symtab_entry_t e2, e1;
538
539   /* Nothing to do for a single entry.  */
540   e1 = (lto_symtab_entry_t) *slot;
541   if (!e1->next)
542     return;
543
544   /* Try to merge each entry with each other entry.  In case of a
545      single prevailing decl this is linear.  */
546 restart:
547   for (; e1; e1 = e1->next)
548     for (e2 = e1->next; e2; e2 = e2->next)
549       {
550         lto_symtab_entry_t prevailing = lto_symtab_merge (e1, e2);
551         if (prevailing == e1)
552           {
553             lto_symtab_entry_t tmp = prevailing;
554             while (tmp->next != e2)
555               tmp = tmp->next;
556             tmp->next = e2->next;
557             e2->next = NULL;
558             e2 = tmp;
559           }
560         else if (prevailing == e2)
561           {
562             lto_symtab_entry_t tmp = (lto_symtab_entry_t) *slot;
563             if (tmp == e1)
564               {
565                 *slot = e1->next;
566                 tmp = e1->next;
567               }
568             else
569               {
570                 while (tmp->next != e1)
571                   tmp = tmp->next;
572                 tmp->next = e1->next;
573               }
574             e1->next = NULL;
575             e1 = tmp;
576             goto restart;
577           }
578       }
579 }
580
581 /* Fixup the chain of prevailing variable decls *SLOT that are commonized
582    during link-time.  */
583
584 static void
585 lto_symtab_fixup_var_decls (void **slot)
586 {
587   lto_symtab_entry_t e = (lto_symtab_entry_t) *slot;
588   tree size = bitsize_zero_node;
589
590   /* Find the largest prevailing decl and move it to the front of the chain.
591      This is the decl we will output as representative for the common
592      section.  */
593   size = bitsize_zero_node;
594   if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
595       || e->resolution == LDPR_PREVAILING_DEF)
596     size = DECL_SIZE (e->decl);
597   for (; e->next;)
598     {
599       lto_symtab_entry_t next = e->next;
600       if ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
601            || next->resolution == LDPR_PREVAILING_DEF)
602           && tree_int_cst_lt (size, DECL_SIZE (next->decl)))
603         {
604           size = DECL_SIZE (next->decl);
605           e->next = next->next;
606           next->next = (lto_symtab_entry_t) *slot;
607           *slot = next;
608         }
609       else
610         e = next;
611     }
612
613   /* Mark everything apart from the first var as written out.  */
614   e = (lto_symtab_entry_t) *slot;
615   for (e = e->next; e; e = e->next)
616     TREE_ASM_WRITTEN (e->decl) = true;
617 }
618
619 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
620
621 static int
622 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
623 {
624   lto_symtab_entry_t e;
625
626   /* Compute the symbol resolutions.  */
627   lto_symtab_resolve_symbols (slot);
628
629   /* Register and adjust types of the entries.  */
630   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
631     TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
632
633   /* Merge the chain to a (hopefully) single prevailing decl.  */
634   lto_symtab_merge_decls_2 (slot);
635
636   /* ???  Ideally we should delay all diagnostics until this point to
637      avoid duplicates.  */
638
639   /* All done for FUNCTION_DECLs.  */
640   e = (lto_symtab_entry_t) *slot;
641   if (TREE_CODE (e->decl) == FUNCTION_DECL)
642     return 1;
643
644   /* Fixup variables in case there are multiple prevailing ones.  */
645   if (e->next)
646     lto_symtab_fixup_var_decls (slot);
647
648   /* Insert all variable decls into the global variable decl vector.  */
649   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
650     VEC_safe_push (tree, gc, lto_global_var_decls, e->decl);
651
652   return 1;
653 }
654
655 /* Resolve and merge all symbol table chains to a prevailing decl.  */
656
657 void
658 lto_symtab_merge_decls (void)
659 {
660   lto_symtab_maybe_init_hash_table ();
661   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
662 }
663
664
665 /* Given the decl DECL, return the prevailing decl with the same name. */
666
667 tree
668 lto_symtab_prevailing_decl (tree decl)
669 {
670   lto_symtab_entry_t ret;
671
672   /* Builtins and local symbols are their own prevailing decl.  */
673   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
674     return decl;
675
676   /* DECL_ABSTRACTs are their own prevailng decl.  */
677   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
678     return decl;
679
680   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
681   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
682
683   /* Walk through the list of candidates and return the one we merged to.  */
684   ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
685   if (!ret)
686     return NULL_TREE;
687
688   /* If there is only one candidate return it.  */
689   if (ret->next == NULL)
690     return ret->decl;
691
692   /* If there are multiple decls to choose from find the one we merged
693      with and return that.  */
694   while (ret)
695     {
696       if (gimple_types_compatible_p (TREE_TYPE (decl), TREE_TYPE (ret->decl)))
697         return ret->decl;
698
699       ret = ret->next;
700     }
701
702   gcc_unreachable ();
703 }
704
705 /* Remove any storage used to store resolution of DECL.  */
706
707 void
708 lto_symtab_clear_resolution (tree decl)
709 {
710   struct lto_symtab_entry_def temp;
711   lto_symtab_entry_t head;
712   void **slot;
713
714   if (!TREE_PUBLIC (decl))
715     return;
716
717   /* LTO FIXME: There should be no DECL_ABSTRACT in the middle end. */
718   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
719     return;
720
721   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
722
723   lto_symtab_maybe_init_hash_table ();
724   temp.id = DECL_ASSEMBLER_NAME (decl);
725   slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
726   if (!*slot)
727     return;
728
729   head = (lto_symtab_entry_t) *slot;
730   if (head->decl == decl)
731     {
732       if (head->next)
733         {
734           *slot = head->next;
735           head->next = NULL;
736         }
737       else
738         htab_remove_elt (lto_symtab_identifiers, &temp);
739     }
740   else
741     {
742       lto_symtab_entry_t e;
743       while (head->next && head->next->decl != decl)
744         head = head->next;
745       if (head->next)
746         {
747           e = head->next;
748           head->next = e->next;
749           e->next = NULL;
750         }
751     }
752 }
753
754 #include "gt-lto-symtab.h"