Update gcc-50 to SVN version 220677
[dragonfly.git] / contrib / gcc-5.0 / gcc / lto / lto-symtab.c
1 /* LTO symbol table.
2    Copyright (C) 2009-2015 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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "predict.h"
38 #include "tm.h"
39 #include "hard-reg-set.h"
40 #include "input.h"
41 #include "function.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
46 #include "is-a.h"
47 #include "gimple.h"
48 #include "plugin-api.h"
49 #include "hash-map.h"
50 #include "ipa-ref.h"
51 #include "cgraph.h"
52 #include "lto-streamer.h"
53 #include "ipa-utils.h"
54 #include "alloc-pool.h"
55 #include "symbol-summary.h"
56 #include "ipa-prop.h"
57 #include "ipa-inline.h"
58 #include "builtins.h"
59
60 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
61    all edges and removing the old node.  */
62
63 static void
64 lto_cgraph_replace_node (struct cgraph_node *node,
65                          struct cgraph_node *prevailing_node)
66 {
67   struct cgraph_edge *e, *next;
68   bool compatible_p;
69
70   if (symtab->dump_file)
71     {
72       fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
73                " for symbol %s\n",
74                node->name (), node->order,
75                prevailing_node->name (),
76                prevailing_node->order,
77                IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
78                  (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
79     }
80
81   /* Merge node flags.  */
82   if (node->force_output)
83     prevailing_node->mark_force_output ();
84   if (node->forced_by_abi)
85     prevailing_node->forced_by_abi = true;
86   if (node->address_taken)
87     {
88       gcc_assert (!prevailing_node->global.inlined_to);
89       prevailing_node->mark_address_taken ();
90     }
91   if (node->definition && prevailing_node->definition)
92     prevailing_node->merged = true;
93
94   /* Redirect all incoming edges.  */
95   compatible_p
96     = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
97                           TREE_TYPE (TREE_TYPE (node->decl)));
98   for (e = node->callers; e; e = next)
99     {
100       next = e->next_caller;
101       e->redirect_callee (prevailing_node);
102       /* If there is a mismatch between the supposed callee return type and
103          the real one do not attempt to inline this function.
104          ???  We really need a way to match function signatures for ABI
105          compatibility and perform related promotions at inlining time.  */
106       if (!compatible_p)
107         e->call_stmt_cannot_inline_p = 1;
108     }
109   /* Redirect incomming references.  */
110   prevailing_node->clone_referring (node);
111
112   /* Fix instrumentation references.  */
113   if (node->instrumented_version)
114     {
115       gcc_assert (node->instrumentation_clone
116                   == prevailing_node->instrumentation_clone);
117       node->instrumented_version->instrumented_version = prevailing_node;
118       if (!prevailing_node->instrumented_version)
119         prevailing_node->instrumented_version = node->instrumented_version;
120       /* Need to reset node->instrumented_version to NULL,
121          otherwise node removal code would reset
122          node->instrumented_version->instrumented_version.  */
123       node->instrumented_version = NULL;
124     }
125
126   ipa_merge_profiles (prevailing_node, node);
127   lto_free_function_in_decl_state_for_node (node);
128
129   if (node->decl != prevailing_node->decl)
130     node->release_body ();
131
132   /* Finally remove the replaced node.  */
133   node->remove ();
134 }
135
136 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
137    all edges and removing the old node.  */
138
139 static void
140 lto_varpool_replace_node (varpool_node *vnode,
141                           varpool_node *prevailing_node)
142 {
143   gcc_assert (!vnode->definition || prevailing_node->definition);
144   gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
145
146   prevailing_node->clone_referring (vnode);
147   if (vnode->force_output)
148     prevailing_node->force_output = true;
149   if (vnode->forced_by_abi)
150     prevailing_node->forced_by_abi = true;
151
152   /* Be sure we can garbage collect the initializer.  */
153   if (DECL_INITIAL (vnode->decl)
154       && vnode->decl != prevailing_node->decl)
155     DECL_INITIAL (vnode->decl) = error_mark_node;
156
157   /* Check and report ODR violations on virtual tables.  */
158   if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
159     compare_virtual_tables (prevailing_node, vnode);
160
161   if (vnode->tls_model != prevailing_node->tls_model)
162     {
163       bool error = false;
164
165       /* Non-TLS and TLS never mix together.  Also emulated model is not
166          compatible with anything else.  */
167       if (prevailing_node->tls_model == TLS_MODEL_NONE
168           || prevailing_node->tls_model == TLS_MODEL_EMULATED
169           || vnode->tls_model == TLS_MODEL_NONE
170           || vnode->tls_model == TLS_MODEL_EMULATED)
171         error = true;
172       /* Linked is silently supporting transitions
173          GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
174          Do the same transitions and error out on others.  */
175       else if ((prevailing_node->tls_model == TLS_MODEL_REAL
176                 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
177                && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
178                    || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
179         prevailing_node->tls_model = vnode->tls_model;
180       else if ((vnode->tls_model == TLS_MODEL_REAL
181                 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
182                && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
183                    || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
184         ;
185       else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
186                && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
187         prevailing_node->tls_model = vnode->tls_model;
188       else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
189                && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
190         ;
191       else
192         error = true;
193       if (error)
194         {
195           error_at (DECL_SOURCE_LOCATION (vnode->decl),
196                     "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
197           inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
198                   "previously defined here as %s",
199                   tls_model_names [prevailing_node->tls_model]);
200         }
201     }
202   /* Finally remove the replaced node.  */
203   vnode->remove ();
204 }
205
206 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
207    Return false if the symbols are not fully compatible and a diagnostic
208    should be emitted.  */
209
210 static bool
211 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
212 {
213   tree prevailing_decl = prevailing->decl;
214   tree decl = entry->decl;
215   tree prevailing_type, type;
216
217   if (prevailing_decl == decl)
218     return true;
219
220   /* Merge decl state in both directions, we may still end up using
221      the new decl.  */
222   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
223   TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
224
225   /* The linker may ask us to combine two incompatible symbols.
226      Detect this case and notify the caller of required diagnostics.  */
227
228   if (TREE_CODE (decl) == FUNCTION_DECL)
229     {
230       if (!types_compatible_p (TREE_TYPE (prevailing_decl),
231                                TREE_TYPE (decl)))
232         /* If we don't have a merged type yet...sigh.  The linker
233            wouldn't complain if the types were mismatched, so we
234            probably shouldn't either.  Just use the type from
235            whichever decl appears to be associated with the
236            definition.  If for some odd reason neither decl is, the
237            older one wins.  */
238         (void) 0;
239
240       return true;
241     }
242
243   /* Now we exclusively deal with VAR_DECLs.  */
244
245   /* Sharing a global symbol is a strong hint that two types are
246      compatible.  We could use this information to complete
247      incomplete pointed-to types more aggressively here, ignoring
248      mismatches in both field and tag names.  It's difficult though
249      to guarantee that this does not have side-effects on merging
250      more compatible types from other translation units though.  */
251
252   /* We can tolerate differences in type qualification, the
253      qualification of the prevailing definition will prevail.
254      ???  In principle we might want to only warn for structurally
255      incompatible types here, but unless we have protective measures
256      for TBAA in place that would hide useful information.  */
257   prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
258   type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
259
260   if (!types_compatible_p (prevailing_type, type))
261     {
262       if (COMPLETE_TYPE_P (type))
263         return false;
264
265       /* If type is incomplete then avoid warnings in the cases
266          that TBAA handles just fine.  */
267
268       if (TREE_CODE (prevailing_type) != TREE_CODE (type))
269         return false;
270
271       if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
272         {
273           tree tem1 = TREE_TYPE (prevailing_type);
274           tree tem2 = TREE_TYPE (type);
275           while (TREE_CODE (tem1) == ARRAY_TYPE
276                  && TREE_CODE (tem2) == ARRAY_TYPE)
277             {
278               tem1 = TREE_TYPE (tem1);
279               tem2 = TREE_TYPE (tem2);
280             }
281
282           if (TREE_CODE (tem1) != TREE_CODE (tem2))
283             return false;
284
285           if (!types_compatible_p (tem1, tem2))
286             return false;
287         }
288
289       /* Fallthru.  Compatible enough.  */
290     }
291
292   /* ???  We might want to emit a warning here if type qualification
293      differences were spotted.  Do not do this unconditionally though.  */
294
295   /* There is no point in comparing too many details of the decls here.
296      The type compatibility checks or the completing of types has properly
297      dealt with most issues.  */
298
299   /* The following should all not invoke fatal errors as in non-LTO
300      mode the linker wouldn't complain either.  Just emit warnings.  */
301
302   /* Report a warning if user-specified alignments do not match.  */
303   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
304       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
305     return false;
306
307   return true;
308 }
309
310 /* Return true if the symtab entry E can be replaced by another symtab
311    entry.  */
312
313 static bool
314 lto_symtab_resolve_replaceable_p (symtab_node *e)
315 {
316   if (DECL_EXTERNAL (e->decl)
317       || DECL_COMDAT (e->decl)
318       || DECL_ONE_ONLY (e->decl)
319       || DECL_WEAK (e->decl))
320     return true;
321
322   if (TREE_CODE (e->decl) == VAR_DECL)
323     return (DECL_COMMON (e->decl)
324             || (!flag_no_common && !DECL_INITIAL (e->decl)));
325
326   return false;
327 }
328
329 /* Return true, if the symbol E should be resolved by lto-symtab.
330    Those are all external symbols and all real symbols that are not static (we
331    handle renaming of static later in partitioning).  */
332
333 static bool
334 lto_symtab_symbol_p (symtab_node *e)
335 {
336   if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
337     return false;
338   return e->real_symbol_p ();
339 }
340
341 /* Return true if the symtab entry E can be the prevailing one.  */
342
343 static bool
344 lto_symtab_resolve_can_prevail_p (symtab_node *e)
345 {
346   if (!lto_symtab_symbol_p (e))
347     return false;
348
349   /* The C++ frontend ends up neither setting TREE_STATIC nor
350      DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
351      So do not reject !TREE_STATIC here but only DECL_EXTERNAL.  */
352   if (DECL_EXTERNAL (e->decl))
353     return false;
354
355   return e->definition;
356 }
357
358 /* Resolve the symbol with the candidates in the chain *SLOT and store
359    their resolutions.  */
360
361 static symtab_node *
362 lto_symtab_resolve_symbols (symtab_node *first)
363 {
364   symtab_node *e;
365   symtab_node *prevailing = NULL;
366
367   /* Always set e->node so that edges are updated to reflect decl merging. */
368   for (e = first; e; e = e->next_sharing_asm_name)
369     if (lto_symtab_symbol_p (e)
370         && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
371             || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
372             || e->resolution == LDPR_PREVAILING_DEF))
373       {
374         prevailing = e;
375         break;
376       }
377
378   /* If the chain is already resolved there is nothing else to do.  */
379   if (prevailing)
380     {
381       /* Assert it's the only one.  */
382       for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
383         if (lto_symtab_symbol_p (e)
384             && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
385                 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
386                 || e->resolution == LDPR_PREVAILING_DEF))
387           fatal_error (input_location, "multiple prevailing defs for %qE",
388                        DECL_NAME (prevailing->decl));
389       return prevailing;
390     }
391
392   /* Find the single non-replaceable prevailing symbol and
393      diagnose ODR violations.  */
394   for (e = first; e; e = e->next_sharing_asm_name)
395     {
396       if (!lto_symtab_resolve_can_prevail_p (e))
397         continue;
398
399       /* If we have a non-replaceable definition it prevails.  */
400       if (!lto_symtab_resolve_replaceable_p (e))
401         {
402           if (prevailing)
403             {
404               error_at (DECL_SOURCE_LOCATION (e->decl),
405                         "%qD has already been defined", e->decl);
406               inform (DECL_SOURCE_LOCATION (prevailing->decl),
407                       "previously defined here");
408             }
409           prevailing = e;
410         }
411     }
412   if (prevailing)
413     return prevailing;
414
415   /* Do a second round choosing one from the replaceable prevailing decls.  */
416   for (e = first; e; e = e->next_sharing_asm_name)
417     {
418       if (!lto_symtab_resolve_can_prevail_p (e))
419         continue;
420
421       /* Choose the first function that can prevail as prevailing.  */
422       if (TREE_CODE (e->decl) == FUNCTION_DECL)
423         {
424           prevailing = e;
425           break;
426         }
427
428       /* From variables that can prevail choose the largest one.  */
429       if (!prevailing
430           || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
431                               DECL_SIZE (e->decl))
432           /* When variables are equivalent try to chose one that has useful
433              DECL_INITIAL.  This makes sense for keyed vtables that are
434              DECL_EXTERNAL but initialized.  In units that do not need them
435              we replace the initializer by error_mark_node to conserve
436              memory.
437
438              We know that the vtable is keyed outside the LTO unit - otherwise
439              the keyed instance would prevail.  We still can preserve useful
440              info in the initializer.  */
441           || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
442               && (DECL_INITIAL (e->decl)
443                   && DECL_INITIAL (e->decl) != error_mark_node)
444               && (!DECL_INITIAL (prevailing->decl)
445                   || DECL_INITIAL (prevailing->decl) == error_mark_node)))
446         prevailing = e;
447     }
448
449   return prevailing;
450 }
451
452 /* Merge all decls in the symbol table chain to the prevailing decl and
453    issue diagnostics about type mismatches.  If DIAGNOSED_P is true
454    do not issue further diagnostics.*/
455
456 static void
457 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
458 {
459   symtab_node *prevailing;
460   symtab_node *e;
461   vec<tree> mismatches = vNULL;
462   unsigned i;
463   tree decl;
464
465   /* Nothing to do for a single entry.  */
466   prevailing = first;
467   if (!prevailing->next_sharing_asm_name)
468     return;
469
470   /* Try to merge each entry with the prevailing one.  */
471   for (e = prevailing->next_sharing_asm_name;
472        e; e = e->next_sharing_asm_name)
473     if (TREE_PUBLIC (e->decl))
474       {
475         if (!lto_symtab_merge (prevailing, e)
476             && !diagnosed_p)
477           mismatches.safe_push (e->decl);
478       }
479   if (mismatches.is_empty ())
480     return;
481
482   /* Diagnose all mismatched re-declarations.  */
483   FOR_EACH_VEC_ELT (mismatches, i, decl)
484     {
485       if (!types_compatible_p (TREE_TYPE (prevailing->decl),
486                                TREE_TYPE (decl)))
487         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
488                                    "type of %qD does not match original "
489                                    "declaration", decl);
490
491       else if ((DECL_USER_ALIGN (prevailing->decl)
492                 && DECL_USER_ALIGN (decl))
493                && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
494         {
495           diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
496                                      "alignment of %qD is bigger than "
497                                      "original declaration", decl);
498         }
499     }
500   if (diagnosed_p)
501     inform (DECL_SOURCE_LOCATION (prevailing->decl),
502             "previously declared here");
503
504   mismatches.release ();
505 }
506
507 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
508
509 static void
510 lto_symtab_merge_decls_1 (symtab_node *first)
511 {
512   symtab_node *e;
513   symtab_node *prevailing;
514   bool diagnosed_p = false;
515
516   if (symtab->dump_file)
517     {
518       fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
519                first->asm_name ());
520       for (e = first; e; e = e->next_sharing_asm_name)
521         if (TREE_PUBLIC (e->decl))
522           e->dump (symtab->dump_file);
523     }
524
525   /* Compute the symbol resolutions.  This is a no-op when using the
526      linker plugin and resolution was decided by the linker.  */
527   prevailing = lto_symtab_resolve_symbols (first);
528
529   /* If there's not a prevailing symbol yet it's an external reference.
530      Happens a lot during ltrans.  Choose the first symbol with a
531      cgraph or a varpool node.  */
532   if (!prevailing)
533     {
534       for (prevailing = first;
535            prevailing; prevailing = prevailing->next_sharing_asm_name)
536         if (lto_symtab_symbol_p (prevailing))
537           break;
538       if (!prevailing)
539         return;
540       /* For variables chose with a priority variant with vnode
541          attached (i.e. from unit where external declaration of
542          variable is actually used).
543          When there are multiple variants, chose one with size.
544          This is needed for C++ typeinfos, for example in
545          lto/20081204-1 there are typeifos in both units, just
546          one of them do have size.  */
547       if (TREE_CODE (prevailing->decl) == VAR_DECL)
548         {
549           for (e = prevailing->next_sharing_asm_name;
550                e; e = e->next_sharing_asm_name)
551             if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
552                 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
553                 && lto_symtab_symbol_p (e))
554               prevailing = e;
555         }
556       /* For variables prefer the non-builtin if one is available.  */
557       else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
558         {
559           for (e = first; e; e = e->next_sharing_asm_name)
560             if (TREE_CODE (e->decl) == FUNCTION_DECL
561                 && !DECL_BUILT_IN (e->decl)
562                 && lto_symtab_symbol_p (e))
563               {
564                 prevailing = e;
565                 break;
566               }
567         }
568     }
569
570   symtab->symtab_prevail_in_asm_name_hash (prevailing);
571
572   /* Diagnose mismatched objects.  */
573   for (e = prevailing->next_sharing_asm_name;
574        e; e = e->next_sharing_asm_name)
575     {
576       if (TREE_CODE (prevailing->decl)
577           == TREE_CODE (e->decl))
578         continue;
579       if (!lto_symtab_symbol_p (e))
580         continue;
581
582       switch (TREE_CODE (prevailing->decl))
583         {
584         case VAR_DECL:
585           gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
586           error_at (DECL_SOURCE_LOCATION (e->decl),
587                     "variable %qD redeclared as function",
588                     prevailing->decl);
589           break;
590
591         case FUNCTION_DECL:
592           gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
593           error_at (DECL_SOURCE_LOCATION (e->decl),
594                     "function %qD redeclared as variable",
595                     prevailing->decl);
596           break;
597
598         default:
599           gcc_unreachable ();
600         }
601
602       diagnosed_p = true;
603     }
604   if (diagnosed_p)
605       inform (DECL_SOURCE_LOCATION (prevailing->decl),
606               "previously declared here");
607
608   /* Merge the chain to the single prevailing decl and diagnose
609      mismatches.  */
610   lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
611
612   if (symtab->dump_file)
613     {
614       fprintf (symtab->dump_file, "After resolution:\n");
615       for (e = prevailing; e; e = e->next_sharing_asm_name)
616         e->dump (symtab->dump_file);
617     }
618 }
619
620 /* Resolve and merge all symbol table chains to a prevailing decl.  */
621
622 void
623 lto_symtab_merge_decls (void)
624 {
625   symtab_node *node;
626
627   /* Populate assembler name hash.   */
628   symtab->symtab_initialize_asm_name_hash ();
629
630   FOR_EACH_SYMBOL (node)
631     if (!node->previous_sharing_asm_name
632         && node->next_sharing_asm_name)
633       lto_symtab_merge_decls_1 (node);
634 }
635
636 /* Helper to process the decl chain for the symbol table entry *SLOT.  */
637
638 static void
639 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
640 {
641   symtab_node *e;
642   symtab_node *next;
643
644   /* Replace the cgraph node of each entry with the prevailing one.  */
645   for (e = prevailing->next_sharing_asm_name; e;
646        e = next)
647     {
648       next = e->next_sharing_asm_name;
649
650       if (!lto_symtab_symbol_p (e))
651         continue;
652       cgraph_node *ce = dyn_cast <cgraph_node *> (e);
653       if (ce && !DECL_BUILT_IN (e->decl))
654         lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
655       if (varpool_node *ve = dyn_cast <varpool_node *> (e))
656         lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
657     }
658
659   return;
660 }
661
662 /* Merge cgraph nodes according to the symbol merging done by
663    lto_symtab_merge_decls.  */
664
665 void
666 lto_symtab_merge_symbols (void)
667 {
668   symtab_node *node;
669
670   if (!flag_ltrans)
671     {
672       symtab->symtab_initialize_asm_name_hash ();
673
674       /* Do the actual merging.  
675          At this point we invalidate hash translating decls into symtab nodes
676          because after removing one of duplicate decls the hash is not correcly
677          updated to the ohter dupliate.  */
678       FOR_EACH_SYMBOL (node)
679         if (lto_symtab_symbol_p (node)
680             && node->next_sharing_asm_name
681             && !node->previous_sharing_asm_name)
682           lto_symtab_merge_symbols_1 (node);
683
684       /* Resolve weakref aliases whose target are now in the compilation unit.  
685          also re-populate the hash translating decls into symtab nodes*/
686       FOR_EACH_SYMBOL (node)
687         {
688           cgraph_node *cnode, *cnode2;
689           varpool_node *vnode;
690           symtab_node *node2;
691
692           if (!node->analyzed && node->alias_target)
693             {
694               symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
695               gcc_assert (node->weakref);
696               if (tgt)
697                 node->resolve_alias (tgt);
698             }
699           node->aux = NULL;
700
701           if (!(cnode = dyn_cast <cgraph_node *> (node))
702               || !cnode->clone_of
703               || cnode->clone_of->decl != cnode->decl)
704             {
705               /* Builtins are not merged via decl merging.  It is however
706                  possible that tree merging unified the declaration.  We
707                  do not want duplicate entries in symbol table.  */
708               if (cnode && DECL_BUILT_IN (node->decl)
709                   && (cnode2 = cgraph_node::get (node->decl))
710                   && cnode2 != cnode)
711                 lto_cgraph_replace_node (cnode2, cnode);
712
713               /* The user defined assembler variables are also not unified by their
714                  symbol name (since it is irrelevant), but we need to unify symbol
715                  nodes if tree merging occured.  */
716               if ((vnode = dyn_cast <varpool_node *> (node))
717                   && DECL_HARD_REGISTER (vnode->decl)
718                   && (node2 = symtab_node::get (vnode->decl))
719                   && node2 != node)
720                 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
721                                           vnode);
722           
723
724               /* Abstract functions may have duplicated cgraph nodes attached;
725                  remove them.  */
726               else if (cnode && DECL_ABSTRACT_P (cnode->decl)
727                        && (cnode2 = cgraph_node::get (node->decl))
728                        && cnode2 != cnode)
729                 cnode2->remove ();
730
731               node->decl->decl_with_vis.symtab_node = node;
732             }
733         }
734     }
735 }
736
737 /* Given the decl DECL, return the prevailing decl with the same name. */
738
739 tree
740 lto_symtab_prevailing_decl (tree decl)
741 {
742   symtab_node *ret;
743
744   /* Builtins and local symbols are their own prevailing decl.  */
745   if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
746     return decl;
747
748   /* DECL_ABSTRACT_Ps are their own prevailing decl.  */
749   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
750     return decl;
751
752   /* Likewise builtins are their own prevailing decl.  This preserves
753      non-builtin vs. builtin uses from compile-time.  */
754   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
755     return decl;
756
757   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
758   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
759
760   /* Walk through the list of candidates and return the one we merged to.  */
761   ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
762   if (!ret)
763     return decl;
764
765   return ret->decl;
766 }