PR target/37072
[official-gcc.git] / gcc / lto / lto-symtab.c
blobdf3cc44d2a9be92c28b667acaaaa4762cceef637
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 "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "predict.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "gimple.h"
39 #include "cgraph.h"
40 #include "lto-streamer.h"
41 #include "ipa-utils.h"
42 #include "alloc-pool.h"
43 #include "symbol-summary.h"
44 #include "ipa-prop.h"
45 #include "ipa-inline.h"
46 #include "builtins.h"
47 #include "print-tree.h"
49 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
50 all edges and removing the old node. */
52 static void
53 lto_cgraph_replace_node (struct cgraph_node *node,
54 struct cgraph_node *prevailing_node)
56 struct cgraph_edge *e, *next;
57 bool compatible_p;
59 if (symtab->dump_file)
61 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
62 " for symbol %s\n",
63 node->name (), node->order,
64 prevailing_node->name (),
65 prevailing_node->order,
66 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
67 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
70 /* Merge node flags. */
71 if (node->force_output)
72 prevailing_node->mark_force_output ();
73 if (node->forced_by_abi)
74 prevailing_node->forced_by_abi = true;
75 if (node->address_taken)
77 gcc_assert (!prevailing_node->global.inlined_to);
78 prevailing_node->mark_address_taken ();
80 if (node->definition && prevailing_node->definition)
81 prevailing_node->merged = true;
83 /* Redirect all incoming edges. */
84 compatible_p
85 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
86 TREE_TYPE (TREE_TYPE (node->decl)));
87 for (e = node->callers; e; e = next)
89 next = e->next_caller;
90 e->redirect_callee (prevailing_node);
91 /* If there is a mismatch between the supposed callee return type and
92 the real one do not attempt to inline this function.
93 ??? We really need a way to match function signatures for ABI
94 compatibility and perform related promotions at inlining time. */
95 if (!compatible_p)
96 e->call_stmt_cannot_inline_p = 1;
98 /* Redirect incomming references. */
99 prevailing_node->clone_referring (node);
101 /* Fix instrumentation references. */
102 if (node->instrumented_version)
104 gcc_assert (node->instrumentation_clone
105 == prevailing_node->instrumentation_clone);
106 node->instrumented_version->instrumented_version = prevailing_node;
107 if (!prevailing_node->instrumented_version)
108 prevailing_node->instrumented_version = node->instrumented_version;
109 /* Need to reset node->instrumented_version to NULL,
110 otherwise node removal code would reset
111 node->instrumented_version->instrumented_version. */
112 node->instrumented_version = NULL;
115 ipa_merge_profiles (prevailing_node, node);
116 lto_free_function_in_decl_state_for_node (node);
118 if (node->decl != prevailing_node->decl)
119 node->release_body ();
121 /* Finally remove the replaced node. */
122 node->remove ();
125 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
126 all edges and removing the old node. */
128 static void
129 lto_varpool_replace_node (varpool_node *vnode,
130 varpool_node *prevailing_node)
132 gcc_assert (!vnode->definition || prevailing_node->definition);
133 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
135 prevailing_node->clone_referring (vnode);
136 if (vnode->force_output)
137 prevailing_node->force_output = true;
138 if (vnode->forced_by_abi)
139 prevailing_node->forced_by_abi = true;
141 /* Be sure we can garbage collect the initializer. */
142 if (DECL_INITIAL (vnode->decl)
143 && vnode->decl != prevailing_node->decl)
144 DECL_INITIAL (vnode->decl) = error_mark_node;
146 /* Check and report ODR violations on virtual tables. */
147 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
148 compare_virtual_tables (prevailing_node, vnode);
150 if (vnode->tls_model != prevailing_node->tls_model)
152 bool error = false;
154 /* Non-TLS and TLS never mix together. Also emulated model is not
155 compatible with anything else. */
156 if (prevailing_node->tls_model == TLS_MODEL_NONE
157 || prevailing_node->tls_model == TLS_MODEL_EMULATED
158 || vnode->tls_model == TLS_MODEL_NONE
159 || vnode->tls_model == TLS_MODEL_EMULATED)
160 error = true;
161 /* Linked is silently supporting transitions
162 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
163 Do the same transitions and error out on others. */
164 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
165 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
166 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
167 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
168 prevailing_node->tls_model = vnode->tls_model;
169 else if ((vnode->tls_model == TLS_MODEL_REAL
170 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
171 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
172 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
174 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
175 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
176 prevailing_node->tls_model = vnode->tls_model;
177 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
178 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
180 else
181 error = true;
182 if (error)
184 error_at (DECL_SOURCE_LOCATION (vnode->decl),
185 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
186 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
187 "previously defined here as %s",
188 tls_model_names [prevailing_node->tls_model]);
191 /* Finally remove the replaced node. */
192 vnode->remove ();
195 /* Return non-zero if we want to output waring about T1 and T2.
196 Return value is a bitmask of reasons of violation:
197 Bit 0 indicates that types are not compatible of memory layout.
198 Bot 1 indicates that types are not compatible because of C++ ODR rule. */
200 static int
201 warn_type_compatibility_p (tree prevailing_type, tree type)
203 int lev = 0;
204 /* C++ provide a robust way to check for type compatibility via the ODR
205 rule. */
206 if (odr_or_derived_type_p (prevailing_type) && odr_or_derived_type_p (type)
207 && !odr_types_equivalent_p (prevailing_type, type))
208 lev = 2;
210 /* Function types needs special care, because types_compatible_p never
211 thinks prototype is compatible to non-prototype. */
212 if ((TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
213 && TREE_CODE (type) == TREE_CODE (prevailing_type))
215 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
216 TREE_TYPE (type));
217 if (TREE_CODE (type) == METHOD_TYPE)
218 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
219 TYPE_METHOD_BASETYPE (type));
220 if (prototype_p (prevailing_type) && prototype_p (type)
221 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
223 tree parm1, parm2;
224 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
225 parm2 = TYPE_ARG_TYPES (type);
226 parm1 && parm2;
227 parm1 = TREE_CHAIN (prevailing_type),
228 parm2 = TREE_CHAIN (type))
229 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
230 TREE_VALUE (parm2));
231 if (parm1 || parm2)
232 lev = 3;
234 if (comp_type_attributes (prevailing_type, type) == 0)
235 lev = 3;
236 return lev;
238 /* Sharing a global symbol is a strong hint that two types are
239 compatible. We could use this information to complete
240 incomplete pointed-to types more aggressively here, ignoring
241 mismatches in both field and tag names. It's difficult though
242 to guarantee that this does not have side-effects on merging
243 more compatible types from other translation units though. */
245 /* We can tolerate differences in type qualification, the
246 qualification of the prevailing definition will prevail.
247 ??? In principle we might want to only warn for structurally
248 incompatible types here, but unless we have protective measures
249 for TBAA in place that would hide useful information. */
250 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
251 type = TYPE_MAIN_VARIANT (type);
253 if (!types_compatible_p (prevailing_type, type))
255 if (TREE_CODE (prevailing_type) == FUNCTION_TYPE
256 || TREE_CODE (type) == METHOD_TYPE)
257 return 1 | lev;
258 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (prevailing_type))
259 return 1 | lev;
261 /* If type is incomplete then avoid warnings in the cases
262 that TBAA handles just fine. */
264 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
265 return 1 | lev;
267 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
269 tree tem1 = TREE_TYPE (prevailing_type);
270 tree tem2 = TREE_TYPE (type);
271 while (TREE_CODE (tem1) == ARRAY_TYPE
272 && TREE_CODE (tem2) == ARRAY_TYPE)
274 tem1 = TREE_TYPE (tem1);
275 tem2 = TREE_TYPE (tem2);
278 if (TREE_CODE (tem1) != TREE_CODE (tem2))
279 return 1 | lev;
281 if (!types_compatible_p (tem1, tem2))
282 return 1 | lev;
285 /* Fallthru. Compatible enough. */
288 /* ??? We might want to emit a warning here if type qualification
289 differences were spotted. Do not do this unconditionally though. */
291 return lev;
294 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
295 Return false if the symbols are not fully compatible and a diagnostic
296 should be emitted. */
298 static bool
299 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
301 tree prevailing_decl = prevailing->decl;
302 tree decl = entry->decl;
304 if (prevailing_decl == decl)
305 return true;
307 /* Merge decl state in both directions, we may still end up using
308 the new decl. */
309 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
310 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
312 /* The linker may ask us to combine two incompatible symbols.
313 Detect this case and notify the caller of required diagnostics. */
315 if (TREE_CODE (decl) == FUNCTION_DECL)
317 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
318 TREE_TYPE (decl)))
319 return false;
321 return true;
324 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
325 TREE_TYPE (decl)))
326 return false;
328 /* There is no point in comparing too many details of the decls here.
329 The type compatibility checks or the completing of types has properly
330 dealt with most issues. */
332 /* The following should all not invoke fatal errors as in non-LTO
333 mode the linker wouldn't complain either. Just emit warnings. */
335 /* Report a warning if user-specified alignments do not match. */
336 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
337 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
338 return false;
340 return true;
343 /* Return true if the symtab entry E can be replaced by another symtab
344 entry. */
346 static bool
347 lto_symtab_resolve_replaceable_p (symtab_node *e)
349 if (DECL_EXTERNAL (e->decl)
350 || DECL_COMDAT (e->decl)
351 || DECL_ONE_ONLY (e->decl)
352 || DECL_WEAK (e->decl))
353 return true;
355 if (TREE_CODE (e->decl) == VAR_DECL)
356 return (DECL_COMMON (e->decl)
357 || (!flag_no_common && !DECL_INITIAL (e->decl)));
359 return false;
362 /* Return true, if the symbol E should be resolved by lto-symtab.
363 Those are all external symbols and all real symbols that are not static (we
364 handle renaming of static later in partitioning). */
366 static bool
367 lto_symtab_symbol_p (symtab_node *e)
369 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
370 return false;
371 return e->real_symbol_p ();
374 /* Return true if the symtab entry E can be the prevailing one. */
376 static bool
377 lto_symtab_resolve_can_prevail_p (symtab_node *e)
379 if (!lto_symtab_symbol_p (e))
380 return false;
382 /* The C++ frontend ends up neither setting TREE_STATIC nor
383 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
384 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
385 if (DECL_EXTERNAL (e->decl))
386 return false;
388 return e->definition;
391 /* Resolve the symbol with the candidates in the chain *SLOT and store
392 their resolutions. */
394 static symtab_node *
395 lto_symtab_resolve_symbols (symtab_node *first)
397 symtab_node *e;
398 symtab_node *prevailing = NULL;
400 /* Always set e->node so that edges are updated to reflect decl merging. */
401 for (e = first; e; e = e->next_sharing_asm_name)
402 if (lto_symtab_symbol_p (e)
403 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
404 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
405 || e->resolution == LDPR_PREVAILING_DEF))
407 prevailing = e;
408 break;
411 /* If the chain is already resolved there is nothing else to do. */
412 if (prevailing)
414 /* Assert it's the only one. */
415 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
416 if (lto_symtab_symbol_p (e)
417 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
418 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
419 || e->resolution == LDPR_PREVAILING_DEF))
420 fatal_error (input_location, "multiple prevailing defs for %qE",
421 DECL_NAME (prevailing->decl));
422 return prevailing;
425 /* Find the single non-replaceable prevailing symbol and
426 diagnose ODR violations. */
427 for (e = first; e; e = e->next_sharing_asm_name)
429 if (!lto_symtab_resolve_can_prevail_p (e))
430 continue;
432 /* If we have a non-replaceable definition it prevails. */
433 if (!lto_symtab_resolve_replaceable_p (e))
435 if (prevailing)
437 error_at (DECL_SOURCE_LOCATION (e->decl),
438 "%qD has already been defined", e->decl);
439 inform (DECL_SOURCE_LOCATION (prevailing->decl),
440 "previously defined here");
442 prevailing = e;
445 if (prevailing)
446 return prevailing;
448 /* Do a second round choosing one from the replaceable prevailing decls. */
449 for (e = first; e; e = e->next_sharing_asm_name)
451 if (!lto_symtab_resolve_can_prevail_p (e))
452 continue;
454 /* Choose the first function that can prevail as prevailing. */
455 if (TREE_CODE (e->decl) == FUNCTION_DECL)
457 prevailing = e;
458 break;
461 /* From variables that can prevail choose the largest one. */
462 if (!prevailing
463 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
464 DECL_SIZE (e->decl))
465 /* When variables are equivalent try to chose one that has useful
466 DECL_INITIAL. This makes sense for keyed vtables that are
467 DECL_EXTERNAL but initialized. In units that do not need them
468 we replace the initializer by error_mark_node to conserve
469 memory.
471 We know that the vtable is keyed outside the LTO unit - otherwise
472 the keyed instance would prevail. We still can preserve useful
473 info in the initializer. */
474 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
475 && (DECL_INITIAL (e->decl)
476 && DECL_INITIAL (e->decl) != error_mark_node)
477 && (!DECL_INITIAL (prevailing->decl)
478 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
479 prevailing = e;
482 return prevailing;
485 /* Merge all decls in the symbol table chain to the prevailing decl and
486 issue diagnostics about type mismatches. If DIAGNOSED_P is true
487 do not issue further diagnostics.*/
489 static void
490 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
492 symtab_node *prevailing;
493 symtab_node *e;
494 vec<tree> mismatches = vNULL;
495 unsigned i;
496 tree decl;
498 /* Nothing to do for a single entry. */
499 prevailing = first;
500 if (!prevailing->next_sharing_asm_name)
501 return;
503 /* Try to merge each entry with the prevailing one. */
504 for (e = prevailing->next_sharing_asm_name;
505 e; e = e->next_sharing_asm_name)
506 if (TREE_PUBLIC (e->decl))
508 if (!lto_symtab_merge (prevailing, e)
509 && !diagnosed_p
510 && !DECL_ARTIFICIAL (e->decl))
511 mismatches.safe_push (e->decl);
513 if (mismatches.is_empty ())
514 return;
516 /* Diagnose all mismatched re-declarations. */
517 FOR_EACH_VEC_ELT (mismatches, i, decl)
519 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
520 TREE_TYPE (decl));
521 if (level)
523 bool diag = false;
524 if (level > 1)
525 diag = warning_at (DECL_SOURCE_LOCATION (decl),
526 OPT_Wodr,
527 "%qD violates the C++ One Definition Rule ",
528 decl);
529 if (!diag && (level & 1))
530 diag = warning_at (DECL_SOURCE_LOCATION (decl),
531 OPT_Wlto_type_mismatch,
532 "type of %qD does not match original "
533 "declaration", decl);
534 if (diag)
535 warn_types_mismatch (TREE_TYPE (prevailing->decl),
536 TREE_TYPE (decl),
537 DECL_SOURCE_LOCATION (prevailing->decl),
538 DECL_SOURCE_LOCATION (decl));
539 diagnosed_p |= diag;
541 else if ((DECL_USER_ALIGN (prevailing->decl)
542 && DECL_USER_ALIGN (decl))
543 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
545 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
546 OPT_Wlto_type_mismatch,
547 "alignment of %qD is bigger than "
548 "original declaration", decl);
551 if (diagnosed_p)
552 inform (DECL_SOURCE_LOCATION (prevailing->decl),
553 "%qD was previously declared here", prevailing->decl);
555 mismatches.release ();
558 /* Helper to process the decl chain for the symbol table entry *SLOT. */
560 static void
561 lto_symtab_merge_decls_1 (symtab_node *first)
563 symtab_node *e;
564 symtab_node *prevailing;
565 bool diagnosed_p = false;
567 if (symtab->dump_file)
569 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
570 first->asm_name ());
571 for (e = first; e; e = e->next_sharing_asm_name)
572 if (TREE_PUBLIC (e->decl))
573 e->dump (symtab->dump_file);
576 /* Compute the symbol resolutions. This is a no-op when using the
577 linker plugin and resolution was decided by the linker. */
578 prevailing = lto_symtab_resolve_symbols (first);
580 /* If there's not a prevailing symbol yet it's an external reference.
581 Happens a lot during ltrans. Choose the first symbol with a
582 cgraph or a varpool node. */
583 if (!prevailing)
585 for (prevailing = first;
586 prevailing; prevailing = prevailing->next_sharing_asm_name)
587 if (lto_symtab_symbol_p (prevailing))
588 break;
589 if (!prevailing)
590 return;
591 /* For variables chose with a priority variant with vnode
592 attached (i.e. from unit where external declaration of
593 variable is actually used).
594 When there are multiple variants, chose one with size.
595 This is needed for C++ typeinfos, for example in
596 lto/20081204-1 there are typeifos in both units, just
597 one of them do have size. */
598 if (TREE_CODE (prevailing->decl) == VAR_DECL)
600 for (e = prevailing->next_sharing_asm_name;
601 e; e = e->next_sharing_asm_name)
602 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
603 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
604 && lto_symtab_symbol_p (e))
605 prevailing = e;
607 /* For variables prefer the non-builtin if one is available. */
608 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
610 for (e = first; e; e = e->next_sharing_asm_name)
611 if (TREE_CODE (e->decl) == FUNCTION_DECL
612 && !DECL_BUILT_IN (e->decl)
613 && lto_symtab_symbol_p (e))
615 prevailing = e;
616 break;
621 symtab->symtab_prevail_in_asm_name_hash (prevailing);
623 /* Diagnose mismatched objects. */
624 for (e = prevailing->next_sharing_asm_name;
625 e; e = e->next_sharing_asm_name)
627 if (TREE_CODE (prevailing->decl)
628 == TREE_CODE (e->decl))
629 continue;
630 if (!lto_symtab_symbol_p (e))
631 continue;
633 switch (TREE_CODE (prevailing->decl))
635 case VAR_DECL:
636 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
637 error_at (DECL_SOURCE_LOCATION (e->decl),
638 "variable %qD redeclared as function",
639 prevailing->decl);
640 break;
642 case FUNCTION_DECL:
643 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
644 error_at (DECL_SOURCE_LOCATION (e->decl),
645 "function %qD redeclared as variable",
646 prevailing->decl);
647 break;
649 default:
650 gcc_unreachable ();
653 diagnosed_p = true;
655 if (diagnosed_p)
656 inform (DECL_SOURCE_LOCATION (prevailing->decl),
657 "previously declared here");
659 /* Merge the chain to the single prevailing decl and diagnose
660 mismatches. */
661 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
663 if (symtab->dump_file)
665 fprintf (symtab->dump_file, "After resolution:\n");
666 for (e = prevailing; e; e = e->next_sharing_asm_name)
667 e->dump (symtab->dump_file);
671 /* Resolve and merge all symbol table chains to a prevailing decl. */
673 void
674 lto_symtab_merge_decls (void)
676 symtab_node *node;
678 /* Populate assembler name hash. */
679 symtab->symtab_initialize_asm_name_hash ();
681 FOR_EACH_SYMBOL (node)
682 if (!node->previous_sharing_asm_name
683 && node->next_sharing_asm_name)
684 lto_symtab_merge_decls_1 (node);
687 /* Helper to process the decl chain for the symbol table entry *SLOT. */
689 static void
690 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
692 symtab_node *e;
693 symtab_node *next;
695 /* Replace the cgraph node of each entry with the prevailing one. */
696 for (e = prevailing->next_sharing_asm_name; e;
697 e = next)
699 next = e->next_sharing_asm_name;
701 if (!lto_symtab_symbol_p (e))
702 continue;
703 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
704 if (ce && !DECL_BUILT_IN (e->decl))
705 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
706 if (varpool_node *ve = dyn_cast <varpool_node *> (e))
707 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
710 return;
713 /* Merge cgraph nodes according to the symbol merging done by
714 lto_symtab_merge_decls. */
716 void
717 lto_symtab_merge_symbols (void)
719 symtab_node *node;
721 if (!flag_ltrans)
723 symtab->symtab_initialize_asm_name_hash ();
725 /* Do the actual merging.
726 At this point we invalidate hash translating decls into symtab nodes
727 because after removing one of duplicate decls the hash is not correcly
728 updated to the ohter dupliate. */
729 FOR_EACH_SYMBOL (node)
730 if (lto_symtab_symbol_p (node)
731 && node->next_sharing_asm_name
732 && !node->previous_sharing_asm_name)
733 lto_symtab_merge_symbols_1 (node);
735 /* Resolve weakref aliases whose target are now in the compilation unit.
736 also re-populate the hash translating decls into symtab nodes*/
737 FOR_EACH_SYMBOL (node)
739 cgraph_node *cnode, *cnode2;
740 varpool_node *vnode;
741 symtab_node *node2;
743 if (!node->analyzed && node->alias_target)
745 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
746 gcc_assert (node->weakref);
747 if (tgt)
748 node->resolve_alias (tgt);
750 node->aux = NULL;
752 if (!(cnode = dyn_cast <cgraph_node *> (node))
753 || !cnode->clone_of
754 || cnode->clone_of->decl != cnode->decl)
756 /* Builtins are not merged via decl merging. It is however
757 possible that tree merging unified the declaration. We
758 do not want duplicate entries in symbol table. */
759 if (cnode && DECL_BUILT_IN (node->decl)
760 && (cnode2 = cgraph_node::get (node->decl))
761 && cnode2 != cnode)
762 lto_cgraph_replace_node (cnode2, cnode);
764 /* The user defined assembler variables are also not unified by their
765 symbol name (since it is irrelevant), but we need to unify symbol
766 nodes if tree merging occured. */
767 if ((vnode = dyn_cast <varpool_node *> (node))
768 && DECL_HARD_REGISTER (vnode->decl)
769 && (node2 = symtab_node::get (vnode->decl))
770 && node2 != node)
771 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
772 vnode);
775 /* Abstract functions may have duplicated cgraph nodes attached;
776 remove them. */
777 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
778 && (cnode2 = cgraph_node::get (node->decl))
779 && cnode2 != cnode)
780 cnode2->remove ();
782 node->decl->decl_with_vis.symtab_node = node;
788 /* Given the decl DECL, return the prevailing decl with the same name. */
790 tree
791 lto_symtab_prevailing_decl (tree decl)
793 symtab_node *ret;
795 /* Builtins and local symbols are their own prevailing decl. */
796 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
797 return decl;
799 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
800 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
801 return decl;
803 /* Likewise builtins are their own prevailing decl. This preserves
804 non-builtin vs. builtin uses from compile-time. */
805 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
806 return decl;
808 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
809 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
811 /* Walk through the list of candidates and return the one we merged to. */
812 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
813 if (!ret)
814 return decl;
816 return ret->decl;