2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lto / lto-symtab.c
blobc77ae947c32617c268c5a961649b5bc651c10cfd
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 /* Merge decl state in both directions, we may still end up using
316 the new decl. */
317 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
318 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
320 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
321 TREE_TYPE (decl)))
322 return false;
324 return true;
327 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
328 TREE_TYPE (decl)))
329 return false;
331 /* There is no point in comparing too many details of the decls here.
332 The type compatibility checks or the completing of types has properly
333 dealt with most issues. */
335 /* The following should all not invoke fatal errors as in non-LTO
336 mode the linker wouldn't complain either. Just emit warnings. */
338 /* Report a warning if user-specified alignments do not match. */
339 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
340 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
341 return false;
343 return true;
346 /* Return true if the symtab entry E can be replaced by another symtab
347 entry. */
349 static bool
350 lto_symtab_resolve_replaceable_p (symtab_node *e)
352 if (DECL_EXTERNAL (e->decl)
353 || DECL_COMDAT (e->decl)
354 || DECL_ONE_ONLY (e->decl)
355 || DECL_WEAK (e->decl))
356 return true;
358 if (TREE_CODE (e->decl) == VAR_DECL)
359 return (DECL_COMMON (e->decl)
360 || (!flag_no_common && !DECL_INITIAL (e->decl)));
362 return false;
365 /* Return true, if the symbol E should be resolved by lto-symtab.
366 Those are all external symbols and all real symbols that are not static (we
367 handle renaming of static later in partitioning). */
369 static bool
370 lto_symtab_symbol_p (symtab_node *e)
372 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
373 return false;
374 return e->real_symbol_p ();
377 /* Return true if the symtab entry E can be the prevailing one. */
379 static bool
380 lto_symtab_resolve_can_prevail_p (symtab_node *e)
382 if (!lto_symtab_symbol_p (e))
383 return false;
385 /* The C++ frontend ends up neither setting TREE_STATIC nor
386 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
387 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
388 if (DECL_EXTERNAL (e->decl))
389 return false;
391 return e->definition;
394 /* Resolve the symbol with the candidates in the chain *SLOT and store
395 their resolutions. */
397 static symtab_node *
398 lto_symtab_resolve_symbols (symtab_node *first)
400 symtab_node *e;
401 symtab_node *prevailing = NULL;
403 /* Always set e->node so that edges are updated to reflect decl merging. */
404 for (e = first; e; e = e->next_sharing_asm_name)
405 if (lto_symtab_symbol_p (e)
406 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
407 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
408 || e->resolution == LDPR_PREVAILING_DEF))
410 prevailing = e;
411 break;
414 /* If the chain is already resolved there is nothing else to do. */
415 if (prevailing)
417 /* Assert it's the only one. */
418 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
419 if (lto_symtab_symbol_p (e)
420 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
421 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
422 || e->resolution == LDPR_PREVAILING_DEF))
423 fatal_error (input_location, "multiple prevailing defs for %qE",
424 DECL_NAME (prevailing->decl));
425 return prevailing;
428 /* Find the single non-replaceable prevailing symbol and
429 diagnose ODR violations. */
430 for (e = first; e; e = e->next_sharing_asm_name)
432 if (!lto_symtab_resolve_can_prevail_p (e))
433 continue;
435 /* If we have a non-replaceable definition it prevails. */
436 if (!lto_symtab_resolve_replaceable_p (e))
438 if (prevailing)
440 error_at (DECL_SOURCE_LOCATION (e->decl),
441 "%qD has already been defined", e->decl);
442 inform (DECL_SOURCE_LOCATION (prevailing->decl),
443 "previously defined here");
445 prevailing = e;
448 if (prevailing)
449 return prevailing;
451 /* Do a second round choosing one from the replaceable prevailing decls. */
452 for (e = first; e; e = e->next_sharing_asm_name)
454 if (!lto_symtab_resolve_can_prevail_p (e))
455 continue;
457 /* Choose the first function that can prevail as prevailing. */
458 if (TREE_CODE (e->decl) == FUNCTION_DECL)
460 prevailing = e;
461 break;
464 /* From variables that can prevail choose the largest one. */
465 if (!prevailing
466 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
467 DECL_SIZE (e->decl))
468 /* When variables are equivalent try to chose one that has useful
469 DECL_INITIAL. This makes sense for keyed vtables that are
470 DECL_EXTERNAL but initialized. In units that do not need them
471 we replace the initializer by error_mark_node to conserve
472 memory.
474 We know that the vtable is keyed outside the LTO unit - otherwise
475 the keyed instance would prevail. We still can preserve useful
476 info in the initializer. */
477 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
478 && (DECL_INITIAL (e->decl)
479 && DECL_INITIAL (e->decl) != error_mark_node)
480 && (!DECL_INITIAL (prevailing->decl)
481 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
482 prevailing = e;
485 return prevailing;
488 /* Merge all decls in the symbol table chain to the prevailing decl and
489 issue diagnostics about type mismatches. If DIAGNOSED_P is true
490 do not issue further diagnostics.*/
492 static void
493 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
495 symtab_node *prevailing;
496 symtab_node *e;
497 vec<tree> mismatches = vNULL;
498 unsigned i;
499 tree decl;
501 /* Nothing to do for a single entry. */
502 prevailing = first;
503 if (!prevailing->next_sharing_asm_name)
504 return;
506 /* Try to merge each entry with the prevailing one. */
507 for (e = prevailing->next_sharing_asm_name;
508 e; e = e->next_sharing_asm_name)
509 if (TREE_PUBLIC (e->decl))
511 if (!lto_symtab_merge (prevailing, e)
512 && !diagnosed_p
513 && !DECL_ARTIFICIAL (e->decl))
514 mismatches.safe_push (e->decl);
516 if (mismatches.is_empty ())
517 return;
519 /* Diagnose all mismatched re-declarations. */
520 FOR_EACH_VEC_ELT (mismatches, i, decl)
522 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
523 TREE_TYPE (decl));
524 if (level)
526 bool diag = false;
527 if (level > 1)
528 diag = warning_at (DECL_SOURCE_LOCATION (decl),
529 OPT_Wodr,
530 "%qD violates the C++ One Definition Rule ",
531 decl);
532 if (!diag && (level & 1))
533 diag = warning_at (DECL_SOURCE_LOCATION (decl),
534 OPT_Wlto_type_mismatch,
535 "type of %qD does not match original "
536 "declaration", decl);
537 if (diag)
538 warn_types_mismatch (TREE_TYPE (prevailing->decl),
539 TREE_TYPE (decl),
540 DECL_SOURCE_LOCATION (prevailing->decl),
541 DECL_SOURCE_LOCATION (decl));
542 diagnosed_p |= diag;
544 else if ((DECL_USER_ALIGN (prevailing->decl)
545 && DECL_USER_ALIGN (decl))
546 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
548 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
549 OPT_Wlto_type_mismatch,
550 "alignment of %qD is bigger than "
551 "original declaration", decl);
554 if (diagnosed_p)
555 inform (DECL_SOURCE_LOCATION (prevailing->decl),
556 "%qD was previously declared here", prevailing->decl);
558 mismatches.release ();
561 /* Helper to process the decl chain for the symbol table entry *SLOT. */
563 static void
564 lto_symtab_merge_decls_1 (symtab_node *first)
566 symtab_node *e;
567 symtab_node *prevailing;
568 bool diagnosed_p = false;
570 if (symtab->dump_file)
572 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
573 first->asm_name ());
574 for (e = first; e; e = e->next_sharing_asm_name)
575 if (TREE_PUBLIC (e->decl))
576 e->dump (symtab->dump_file);
579 /* Compute the symbol resolutions. This is a no-op when using the
580 linker plugin and resolution was decided by the linker. */
581 prevailing = lto_symtab_resolve_symbols (first);
583 /* If there's not a prevailing symbol yet it's an external reference.
584 Happens a lot during ltrans. Choose the first symbol with a
585 cgraph or a varpool node. */
586 if (!prevailing)
588 for (prevailing = first;
589 prevailing; prevailing = prevailing->next_sharing_asm_name)
590 if (lto_symtab_symbol_p (prevailing))
591 break;
592 if (!prevailing)
593 return;
594 /* For variables chose with a priority variant with vnode
595 attached (i.e. from unit where external declaration of
596 variable is actually used).
597 When there are multiple variants, chose one with size.
598 This is needed for C++ typeinfos, for example in
599 lto/20081204-1 there are typeifos in both units, just
600 one of them do have size. */
601 if (TREE_CODE (prevailing->decl) == VAR_DECL)
603 for (e = prevailing->next_sharing_asm_name;
604 e; e = e->next_sharing_asm_name)
605 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
606 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
607 && lto_symtab_symbol_p (e))
608 prevailing = e;
610 /* For variables prefer the non-builtin if one is available. */
611 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
613 for (e = first; e; e = e->next_sharing_asm_name)
614 if (TREE_CODE (e->decl) == FUNCTION_DECL
615 && !DECL_BUILT_IN (e->decl)
616 && lto_symtab_symbol_p (e))
618 prevailing = e;
619 break;
624 symtab->symtab_prevail_in_asm_name_hash (prevailing);
626 /* Diagnose mismatched objects. */
627 for (e = prevailing->next_sharing_asm_name;
628 e; e = e->next_sharing_asm_name)
630 if (TREE_CODE (prevailing->decl)
631 == TREE_CODE (e->decl))
632 continue;
633 if (!lto_symtab_symbol_p (e))
634 continue;
636 switch (TREE_CODE (prevailing->decl))
638 case VAR_DECL:
639 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
640 error_at (DECL_SOURCE_LOCATION (e->decl),
641 "variable %qD redeclared as function",
642 prevailing->decl);
643 break;
645 case FUNCTION_DECL:
646 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
647 error_at (DECL_SOURCE_LOCATION (e->decl),
648 "function %qD redeclared as variable",
649 prevailing->decl);
650 break;
652 default:
653 gcc_unreachable ();
656 diagnosed_p = true;
658 if (diagnosed_p)
659 inform (DECL_SOURCE_LOCATION (prevailing->decl),
660 "previously declared here");
662 /* Merge the chain to the single prevailing decl and diagnose
663 mismatches. */
664 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
666 if (symtab->dump_file)
668 fprintf (symtab->dump_file, "After resolution:\n");
669 for (e = prevailing; e; e = e->next_sharing_asm_name)
670 e->dump (symtab->dump_file);
674 /* Resolve and merge all symbol table chains to a prevailing decl. */
676 void
677 lto_symtab_merge_decls (void)
679 symtab_node *node;
681 /* Populate assembler name hash. */
682 symtab->symtab_initialize_asm_name_hash ();
684 FOR_EACH_SYMBOL (node)
685 if (!node->previous_sharing_asm_name
686 && node->next_sharing_asm_name)
687 lto_symtab_merge_decls_1 (node);
690 /* Helper to process the decl chain for the symbol table entry *SLOT. */
692 static void
693 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
695 symtab_node *e;
696 symtab_node *next;
698 /* Replace the cgraph node of each entry with the prevailing one. */
699 for (e = prevailing->next_sharing_asm_name; e;
700 e = next)
702 next = e->next_sharing_asm_name;
704 if (!lto_symtab_symbol_p (e))
705 continue;
706 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
707 if (ce && !DECL_BUILT_IN (e->decl))
708 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
709 if (varpool_node *ve = dyn_cast <varpool_node *> (e))
710 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
713 return;
716 /* Merge cgraph nodes according to the symbol merging done by
717 lto_symtab_merge_decls. */
719 void
720 lto_symtab_merge_symbols (void)
722 symtab_node *node;
724 if (!flag_ltrans)
726 symtab->symtab_initialize_asm_name_hash ();
728 /* Do the actual merging.
729 At this point we invalidate hash translating decls into symtab nodes
730 because after removing one of duplicate decls the hash is not correcly
731 updated to the ohter dupliate. */
732 FOR_EACH_SYMBOL (node)
733 if (lto_symtab_symbol_p (node)
734 && node->next_sharing_asm_name
735 && !node->previous_sharing_asm_name)
736 lto_symtab_merge_symbols_1 (node);
738 /* Resolve weakref aliases whose target are now in the compilation unit.
739 also re-populate the hash translating decls into symtab nodes*/
740 FOR_EACH_SYMBOL (node)
742 cgraph_node *cnode, *cnode2;
743 varpool_node *vnode;
744 symtab_node *node2;
746 if (!node->analyzed && node->alias_target)
748 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
749 gcc_assert (node->weakref);
750 if (tgt)
751 node->resolve_alias (tgt);
753 node->aux = NULL;
755 if (!(cnode = dyn_cast <cgraph_node *> (node))
756 || !cnode->clone_of
757 || cnode->clone_of->decl != cnode->decl)
759 /* Builtins are not merged via decl merging. It is however
760 possible that tree merging unified the declaration. We
761 do not want duplicate entries in symbol table. */
762 if (cnode && DECL_BUILT_IN (node->decl)
763 && (cnode2 = cgraph_node::get (node->decl))
764 && cnode2 != cnode)
765 lto_cgraph_replace_node (cnode2, cnode);
767 /* The user defined assembler variables are also not unified by their
768 symbol name (since it is irrelevant), but we need to unify symbol
769 nodes if tree merging occured. */
770 if ((vnode = dyn_cast <varpool_node *> (node))
771 && DECL_HARD_REGISTER (vnode->decl)
772 && (node2 = symtab_node::get (vnode->decl))
773 && node2 != node)
774 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
775 vnode);
778 /* Abstract functions may have duplicated cgraph nodes attached;
779 remove them. */
780 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
781 && (cnode2 = cgraph_node::get (node->decl))
782 && cnode2 != cnode)
783 cnode2->remove ();
785 node->decl->decl_with_vis.symtab_node = node;
791 /* Given the decl DECL, return the prevailing decl with the same name. */
793 tree
794 lto_symtab_prevailing_decl (tree decl)
796 symtab_node *ret;
798 /* Builtins and local symbols are their own prevailing decl. */
799 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
800 return decl;
802 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
803 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
804 return decl;
806 /* When decl did not participate in symbol resolution leave it alone.
807 This can happen when we streamed the decl as abstract origin
808 from the block tree of inlining a partially inlined function.
809 If all, the split function and the original function end up
810 optimized away early we do not put the abstract origin into the
811 ltrans boundary and we'll end up ICEing in
812 dwarf2out.c:gen_inlined_subroutine_die because we eventually
813 replace a decl with DECL_POSSIBLY_INLINED set with one without. */
814 if (TREE_CODE (decl) == FUNCTION_DECL
815 && ! cgraph_node::get (decl))
816 return decl;
818 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
819 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
821 /* Walk through the list of candidates and return the one we merged to. */
822 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
823 if (!ret)
824 return decl;
826 /* Do not replace a non-builtin with a builtin. */
827 if (is_builtin_fn (ret->decl))
828 return decl;
830 return ret->decl;