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