2015-08-04 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / gcc / lto / lto-symtab.c
blob979579f4f161840508d168efb252bb146d2b58ff
1 /* LTO symbol table.
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "alias.h"
26 #include "tm.h"
27 #include "function.h"
28 #include "predict.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "hard-reg-set.h"
33 #include "options.h"
34 #include "fold-const.h"
35 #include "internal-fn.h"
36 #include "cgraph.h"
37 #include "target.h"
38 #include "alloc-pool.h"
39 #include "lto-streamer.h"
40 #include "ipa-utils.h"
41 #include "symbol-summary.h"
42 #include "ipa-prop.h"
43 #include "ipa-inline.h"
44 #include "builtins.h"
45 #include "print-tree.h"
47 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
48 all edges and removing the old node. */
50 static void
51 lto_cgraph_replace_node (struct cgraph_node *node,
52 struct cgraph_node *prevailing_node)
54 struct cgraph_edge *e, *next;
55 bool compatible_p;
57 if (symtab->dump_file)
59 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
60 " for symbol %s\n",
61 node->name (), node->order,
62 prevailing_node->name (),
63 prevailing_node->order,
64 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
65 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
68 /* Merge node flags. */
69 if (node->force_output)
70 prevailing_node->mark_force_output ();
71 if (node->forced_by_abi)
72 prevailing_node->forced_by_abi = true;
73 if (node->address_taken)
75 gcc_assert (!prevailing_node->global.inlined_to);
76 prevailing_node->mark_address_taken ();
78 if (node->definition && prevailing_node->definition)
79 prevailing_node->merged = true;
81 /* Redirect all incoming edges. */
82 compatible_p
83 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
84 TREE_TYPE (TREE_TYPE (node->decl)));
85 for (e = node->callers; e; e = next)
87 next = e->next_caller;
88 e->redirect_callee (prevailing_node);
89 /* If there is a mismatch between the supposed callee return type and
90 the real one do not attempt to inline this function.
91 ??? We really need a way to match function signatures for ABI
92 compatibility and perform related promotions at inlining time. */
93 if (!compatible_p)
94 e->call_stmt_cannot_inline_p = 1;
96 /* Redirect incomming references. */
97 prevailing_node->clone_referring (node);
99 /* Fix instrumentation references. */
100 if (node->instrumented_version)
102 gcc_assert (node->instrumentation_clone
103 == prevailing_node->instrumentation_clone);
104 node->instrumented_version->instrumented_version = prevailing_node;
105 if (!prevailing_node->instrumented_version)
106 prevailing_node->instrumented_version = node->instrumented_version;
107 /* Need to reset node->instrumented_version to NULL,
108 otherwise node removal code would reset
109 node->instrumented_version->instrumented_version. */
110 node->instrumented_version = NULL;
113 ipa_merge_profiles (prevailing_node, node);
114 lto_free_function_in_decl_state_for_node (node);
116 if (node->decl != prevailing_node->decl)
117 node->release_body ();
119 /* Finally remove the replaced node. */
120 node->remove ();
123 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
124 all edges and removing the old node. */
126 static void
127 lto_varpool_replace_node (varpool_node *vnode,
128 varpool_node *prevailing_node)
130 gcc_assert (!vnode->definition || prevailing_node->definition);
131 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
133 prevailing_node->clone_referring (vnode);
134 if (vnode->force_output)
135 prevailing_node->force_output = true;
136 if (vnode->forced_by_abi)
137 prevailing_node->forced_by_abi = true;
139 /* Be sure we can garbage collect the initializer. */
140 if (DECL_INITIAL (vnode->decl)
141 && vnode->decl != prevailing_node->decl)
142 DECL_INITIAL (vnode->decl) = error_mark_node;
144 /* Check and report ODR violations on virtual tables. */
145 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
146 compare_virtual_tables (prevailing_node, vnode);
148 if (vnode->tls_model != prevailing_node->tls_model)
150 bool error = false;
152 /* Non-TLS and TLS never mix together. Also emulated model is not
153 compatible with anything else. */
154 if (prevailing_node->tls_model == TLS_MODEL_NONE
155 || prevailing_node->tls_model == TLS_MODEL_EMULATED
156 || vnode->tls_model == TLS_MODEL_NONE
157 || vnode->tls_model == TLS_MODEL_EMULATED)
158 error = true;
159 /* Linked is silently supporting transitions
160 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
161 Do the same transitions and error out on others. */
162 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
163 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
164 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
165 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
166 prevailing_node->tls_model = vnode->tls_model;
167 else if ((vnode->tls_model == TLS_MODEL_REAL
168 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
169 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
170 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
172 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
173 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
174 prevailing_node->tls_model = vnode->tls_model;
175 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
176 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
178 else
179 error = true;
180 if (error)
182 error_at (DECL_SOURCE_LOCATION (vnode->decl),
183 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
184 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
185 "previously defined here as %s",
186 tls_model_names [prevailing_node->tls_model]);
189 /* Finally remove the replaced node. */
190 vnode->remove ();
193 /* Return non-zero if we want to output waring about T1 and T2.
194 Return value is a bitmask of reasons of violation:
195 Bit 0 indicates that types are not compatible of memory layout.
196 Bot 1 indicates that types are not compatible because of C++ ODR rule. */
198 static int
199 warn_type_compatibility_p (tree prevailing_type, tree type)
201 int lev = 0;
202 /* C++ provide a robust way to check for type compatibility via the ODR
203 rule. */
204 if (odr_or_derived_type_p (prevailing_type) && odr_or_derived_type_p (type)
205 && !odr_types_equivalent_p (prevailing_type, type))
206 lev = 2;
208 /* Function types needs special care, because types_compatible_p never
209 thinks prototype is compatible to non-prototype. */
210 if ((TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
211 && TREE_CODE (type) == TREE_CODE (prevailing_type))
213 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
214 TREE_TYPE (type));
215 if (TREE_CODE (type) == METHOD_TYPE)
216 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
217 TYPE_METHOD_BASETYPE (type));
218 if (prototype_p (prevailing_type) && prototype_p (type)
219 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
221 tree parm1, parm2;
222 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
223 parm2 = TYPE_ARG_TYPES (type);
224 parm1 && parm2;
225 parm1 = TREE_CHAIN (prevailing_type),
226 parm2 = TREE_CHAIN (type))
227 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
228 TREE_VALUE (parm2));
229 if (parm1 || parm2)
230 lev = 3;
232 if (comp_type_attributes (prevailing_type, type) == 0)
233 lev = 3;
234 return lev;
236 /* Sharing a global symbol is a strong hint that two types are
237 compatible. We could use this information to complete
238 incomplete pointed-to types more aggressively here, ignoring
239 mismatches in both field and tag names. It's difficult though
240 to guarantee that this does not have side-effects on merging
241 more compatible types from other translation units though. */
243 /* We can tolerate differences in type qualification, the
244 qualification of the prevailing definition will prevail.
245 ??? In principle we might want to only warn for structurally
246 incompatible types here, but unless we have protective measures
247 for TBAA in place that would hide useful information. */
248 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
249 type = TYPE_MAIN_VARIANT (type);
251 if (!types_compatible_p (prevailing_type, type))
253 if (TREE_CODE (prevailing_type) == FUNCTION_TYPE
254 || TREE_CODE (type) == METHOD_TYPE)
255 return 1 | lev;
256 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (prevailing_type))
257 return 1 | lev;
259 /* If type is incomplete then avoid warnings in the cases
260 that TBAA handles just fine. */
262 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
263 return 1 | lev;
265 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
267 tree tem1 = TREE_TYPE (prevailing_type);
268 tree tem2 = TREE_TYPE (type);
269 while (TREE_CODE (tem1) == ARRAY_TYPE
270 && TREE_CODE (tem2) == ARRAY_TYPE)
272 tem1 = TREE_TYPE (tem1);
273 tem2 = TREE_TYPE (tem2);
276 if (TREE_CODE (tem1) != TREE_CODE (tem2))
277 return 1 | lev;
279 if (!types_compatible_p (tem1, tem2))
280 return 1 | lev;
283 /* Fallthru. Compatible enough. */
286 /* ??? We might want to emit a warning here if type qualification
287 differences were spotted. Do not do this unconditionally though. */
289 return lev;
292 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
293 Return false if the symbols are not fully compatible and a diagnostic
294 should be emitted. */
296 static bool
297 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
299 tree prevailing_decl = prevailing->decl;
300 tree decl = entry->decl;
302 if (prevailing_decl == decl)
303 return true;
305 /* Merge decl state in both directions, we may still end up using
306 the new decl. */
307 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
308 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
310 /* The linker may ask us to combine two incompatible symbols.
311 Detect this case and notify the caller of required diagnostics. */
313 if (TREE_CODE (decl) == FUNCTION_DECL)
315 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
316 TREE_TYPE (decl)))
317 return false;
319 return true;
322 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
323 TREE_TYPE (decl)))
324 return false;
326 /* There is no point in comparing too many details of the decls here.
327 The type compatibility checks or the completing of types has properly
328 dealt with most issues. */
330 /* The following should all not invoke fatal errors as in non-LTO
331 mode the linker wouldn't complain either. Just emit warnings. */
333 /* Report a warning if user-specified alignments do not match. */
334 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
335 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
336 return false;
338 return true;
341 /* Return true if the symtab entry E can be replaced by another symtab
342 entry. */
344 static bool
345 lto_symtab_resolve_replaceable_p (symtab_node *e)
347 if (DECL_EXTERNAL (e->decl)
348 || DECL_COMDAT (e->decl)
349 || DECL_ONE_ONLY (e->decl)
350 || DECL_WEAK (e->decl))
351 return true;
353 if (TREE_CODE (e->decl) == VAR_DECL)
354 return (DECL_COMMON (e->decl)
355 || (!flag_no_common && !DECL_INITIAL (e->decl)));
357 return false;
360 /* Return true, if the symbol E should be resolved by lto-symtab.
361 Those are all external symbols and all real symbols that are not static (we
362 handle renaming of static later in partitioning). */
364 static bool
365 lto_symtab_symbol_p (symtab_node *e)
367 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
368 return false;
369 return e->real_symbol_p ();
372 /* Return true if the symtab entry E can be the prevailing one. */
374 static bool
375 lto_symtab_resolve_can_prevail_p (symtab_node *e)
377 if (!lto_symtab_symbol_p (e))
378 return false;
380 /* The C++ frontend ends up neither setting TREE_STATIC nor
381 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
382 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
383 if (DECL_EXTERNAL (e->decl))
384 return false;
386 return e->definition;
389 /* Resolve the symbol with the candidates in the chain *SLOT and store
390 their resolutions. */
392 static symtab_node *
393 lto_symtab_resolve_symbols (symtab_node *first)
395 symtab_node *e;
396 symtab_node *prevailing = NULL;
398 /* Always set e->node so that edges are updated to reflect decl merging. */
399 for (e = first; e; e = e->next_sharing_asm_name)
400 if (lto_symtab_symbol_p (e)
401 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
402 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
403 || e->resolution == LDPR_PREVAILING_DEF))
405 prevailing = e;
406 break;
409 /* If the chain is already resolved there is nothing else to do. */
410 if (prevailing)
412 /* Assert it's the only one. */
413 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
414 if (lto_symtab_symbol_p (e)
415 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
416 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
417 || e->resolution == LDPR_PREVAILING_DEF))
418 fatal_error (input_location, "multiple prevailing defs for %qE",
419 DECL_NAME (prevailing->decl));
420 return prevailing;
423 /* Find the single non-replaceable prevailing symbol and
424 diagnose ODR violations. */
425 for (e = first; e; e = e->next_sharing_asm_name)
427 if (!lto_symtab_resolve_can_prevail_p (e))
428 continue;
430 /* If we have a non-replaceable definition it prevails. */
431 if (!lto_symtab_resolve_replaceable_p (e))
433 if (prevailing)
435 error_at (DECL_SOURCE_LOCATION (e->decl),
436 "%qD has already been defined", e->decl);
437 inform (DECL_SOURCE_LOCATION (prevailing->decl),
438 "previously defined here");
440 prevailing = e;
443 if (prevailing)
444 return prevailing;
446 /* Do a second round choosing one from the replaceable prevailing decls. */
447 for (e = first; e; e = e->next_sharing_asm_name)
449 if (!lto_symtab_resolve_can_prevail_p (e))
450 continue;
452 /* Choose the first function that can prevail as prevailing. */
453 if (TREE_CODE (e->decl) == FUNCTION_DECL)
455 prevailing = e;
456 break;
459 /* From variables that can prevail choose the largest one. */
460 if (!prevailing
461 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
462 DECL_SIZE (e->decl))
463 /* When variables are equivalent try to chose one that has useful
464 DECL_INITIAL. This makes sense for keyed vtables that are
465 DECL_EXTERNAL but initialized. In units that do not need them
466 we replace the initializer by error_mark_node to conserve
467 memory.
469 We know that the vtable is keyed outside the LTO unit - otherwise
470 the keyed instance would prevail. We still can preserve useful
471 info in the initializer. */
472 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
473 && (DECL_INITIAL (e->decl)
474 && DECL_INITIAL (e->decl) != error_mark_node)
475 && (!DECL_INITIAL (prevailing->decl)
476 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
477 prevailing = e;
480 return prevailing;
483 /* Merge all decls in the symbol table chain to the prevailing decl and
484 issue diagnostics about type mismatches. If DIAGNOSED_P is true
485 do not issue further diagnostics.*/
487 static void
488 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
490 symtab_node *prevailing;
491 symtab_node *e;
492 vec<tree> mismatches = vNULL;
493 unsigned i;
494 tree decl;
496 /* Nothing to do for a single entry. */
497 prevailing = first;
498 if (!prevailing->next_sharing_asm_name)
499 return;
501 /* Try to merge each entry with the prevailing one. */
502 for (e = prevailing->next_sharing_asm_name;
503 e; e = e->next_sharing_asm_name)
504 if (TREE_PUBLIC (e->decl))
506 if (!lto_symtab_merge (prevailing, e)
507 && !diagnosed_p
508 && !DECL_ARTIFICIAL (e->decl))
509 mismatches.safe_push (e->decl);
511 if (mismatches.is_empty ())
512 return;
514 /* Diagnose all mismatched re-declarations. */
515 FOR_EACH_VEC_ELT (mismatches, i, decl)
517 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
518 TREE_TYPE (decl));
519 if (level)
521 bool diag = false;
522 if (level > 1)
523 diag = warning_at (DECL_SOURCE_LOCATION (decl),
524 OPT_Wodr,
525 "%qD violates the C++ One Definition Rule ",
526 decl);
527 if (!diag && (level & 1))
528 diag = warning_at (DECL_SOURCE_LOCATION (decl),
529 OPT_Wlto_type_mismatch,
530 "type of %qD does not match original "
531 "declaration", decl);
532 if (diag)
533 warn_types_mismatch (TREE_TYPE (prevailing->decl),
534 TREE_TYPE (decl),
535 DECL_SOURCE_LOCATION (prevailing->decl),
536 DECL_SOURCE_LOCATION (decl));
537 diagnosed_p |= diag;
539 else if ((DECL_USER_ALIGN (prevailing->decl)
540 && DECL_USER_ALIGN (decl))
541 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
543 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
544 OPT_Wlto_type_mismatch,
545 "alignment of %qD is bigger than "
546 "original declaration", decl);
549 if (diagnosed_p)
550 inform (DECL_SOURCE_LOCATION (prevailing->decl),
551 "%qD was previously declared here", prevailing->decl);
553 mismatches.release ();
556 /* Helper to process the decl chain for the symbol table entry *SLOT. */
558 static void
559 lto_symtab_merge_decls_1 (symtab_node *first)
561 symtab_node *e;
562 symtab_node *prevailing;
563 bool diagnosed_p = false;
565 if (symtab->dump_file)
567 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
568 first->asm_name ());
569 for (e = first; e; e = e->next_sharing_asm_name)
570 if (TREE_PUBLIC (e->decl))
571 e->dump (symtab->dump_file);
574 /* Compute the symbol resolutions. This is a no-op when using the
575 linker plugin and resolution was decided by the linker. */
576 prevailing = lto_symtab_resolve_symbols (first);
578 /* If there's not a prevailing symbol yet it's an external reference.
579 Happens a lot during ltrans. Choose the first symbol with a
580 cgraph or a varpool node. */
581 if (!prevailing)
583 for (prevailing = first;
584 prevailing; prevailing = prevailing->next_sharing_asm_name)
585 if (lto_symtab_symbol_p (prevailing))
586 break;
587 if (!prevailing)
588 return;
589 /* For variables chose with a priority variant with vnode
590 attached (i.e. from unit where external declaration of
591 variable is actually used).
592 When there are multiple variants, chose one with size.
593 This is needed for C++ typeinfos, for example in
594 lto/20081204-1 there are typeifos in both units, just
595 one of them do have size. */
596 if (TREE_CODE (prevailing->decl) == VAR_DECL)
598 for (e = prevailing->next_sharing_asm_name;
599 e; e = e->next_sharing_asm_name)
600 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
601 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
602 && lto_symtab_symbol_p (e))
603 prevailing = e;
605 /* For variables prefer the non-builtin if one is available. */
606 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
608 for (e = first; e; e = e->next_sharing_asm_name)
609 if (TREE_CODE (e->decl) == FUNCTION_DECL
610 && !DECL_BUILT_IN (e->decl)
611 && lto_symtab_symbol_p (e))
613 prevailing = e;
614 break;
619 symtab->symtab_prevail_in_asm_name_hash (prevailing);
621 /* Diagnose mismatched objects. */
622 for (e = prevailing->next_sharing_asm_name;
623 e; e = e->next_sharing_asm_name)
625 if (TREE_CODE (prevailing->decl)
626 == TREE_CODE (e->decl))
627 continue;
628 if (!lto_symtab_symbol_p (e))
629 continue;
631 switch (TREE_CODE (prevailing->decl))
633 case VAR_DECL:
634 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
635 error_at (DECL_SOURCE_LOCATION (e->decl),
636 "variable %qD redeclared as function",
637 prevailing->decl);
638 break;
640 case FUNCTION_DECL:
641 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
642 error_at (DECL_SOURCE_LOCATION (e->decl),
643 "function %qD redeclared as variable",
644 prevailing->decl);
645 break;
647 default:
648 gcc_unreachable ();
651 diagnosed_p = true;
653 if (diagnosed_p)
654 inform (DECL_SOURCE_LOCATION (prevailing->decl),
655 "previously declared here");
657 /* Merge the chain to the single prevailing decl and diagnose
658 mismatches. */
659 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
661 if (symtab->dump_file)
663 fprintf (symtab->dump_file, "After resolution:\n");
664 for (e = prevailing; e; e = e->next_sharing_asm_name)
665 e->dump (symtab->dump_file);
669 /* Resolve and merge all symbol table chains to a prevailing decl. */
671 void
672 lto_symtab_merge_decls (void)
674 symtab_node *node;
676 /* Populate assembler name hash. */
677 symtab->symtab_initialize_asm_name_hash ();
679 FOR_EACH_SYMBOL (node)
680 if (!node->previous_sharing_asm_name
681 && node->next_sharing_asm_name)
682 lto_symtab_merge_decls_1 (node);
685 /* Helper to process the decl chain for the symbol table entry *SLOT. */
687 static void
688 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
690 symtab_node *e;
691 symtab_node *next;
693 /* Replace the cgraph node of each entry with the prevailing one. */
694 for (e = prevailing->next_sharing_asm_name; e;
695 e = next)
697 next = e->next_sharing_asm_name;
699 if (!lto_symtab_symbol_p (e))
700 continue;
701 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
702 if (ce && !DECL_BUILT_IN (e->decl))
703 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
704 if (varpool_node *ve = dyn_cast <varpool_node *> (e))
705 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
708 return;
711 /* Merge cgraph nodes according to the symbol merging done by
712 lto_symtab_merge_decls. */
714 void
715 lto_symtab_merge_symbols (void)
717 symtab_node *node;
719 if (!flag_ltrans)
721 symtab->symtab_initialize_asm_name_hash ();
723 /* Do the actual merging.
724 At this point we invalidate hash translating decls into symtab nodes
725 because after removing one of duplicate decls the hash is not correcly
726 updated to the ohter dupliate. */
727 FOR_EACH_SYMBOL (node)
728 if (lto_symtab_symbol_p (node)
729 && node->next_sharing_asm_name
730 && !node->previous_sharing_asm_name)
731 lto_symtab_merge_symbols_1 (node);
733 /* Resolve weakref aliases whose target are now in the compilation unit.
734 also re-populate the hash translating decls into symtab nodes*/
735 FOR_EACH_SYMBOL (node)
737 cgraph_node *cnode, *cnode2;
738 varpool_node *vnode;
739 symtab_node *node2;
741 if (!node->analyzed && node->alias_target)
743 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
744 gcc_assert (node->weakref);
745 if (tgt)
746 node->resolve_alias (tgt);
748 node->aux = NULL;
750 if (!(cnode = dyn_cast <cgraph_node *> (node))
751 || !cnode->clone_of
752 || cnode->clone_of->decl != cnode->decl)
754 /* Builtins are not merged via decl merging. It is however
755 possible that tree merging unified the declaration. We
756 do not want duplicate entries in symbol table. */
757 if (cnode && DECL_BUILT_IN (node->decl)
758 && (cnode2 = cgraph_node::get (node->decl))
759 && cnode2 != cnode)
760 lto_cgraph_replace_node (cnode2, cnode);
762 /* The user defined assembler variables are also not unified by their
763 symbol name (since it is irrelevant), but we need to unify symbol
764 nodes if tree merging occured. */
765 if ((vnode = dyn_cast <varpool_node *> (node))
766 && DECL_HARD_REGISTER (vnode->decl)
767 && (node2 = symtab_node::get (vnode->decl))
768 && node2 != node)
769 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
770 vnode);
773 /* Abstract functions may have duplicated cgraph nodes attached;
774 remove them. */
775 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
776 && (cnode2 = cgraph_node::get (node->decl))
777 && cnode2 != cnode)
778 cnode2->remove ();
780 node->decl->decl_with_vis.symtab_node = node;
786 /* Given the decl DECL, return the prevailing decl with the same name. */
788 tree
789 lto_symtab_prevailing_decl (tree decl)
791 symtab_node *ret;
793 /* Builtins and local symbols are their own prevailing decl. */
794 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
795 return decl;
797 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
798 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
799 return decl;
801 /* Likewise builtins are their own prevailing decl. This preserves
802 non-builtin vs. builtin uses from compile-time. */
803 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
804 return decl;
806 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
807 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
809 /* Walk through the list of candidates and return the one we merged to. */
810 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
811 if (!ret)
812 return decl;
814 return ret->decl;