PR target/81369
[official-gcc.git] / gcc / lto / lto-symtab.c
blob019677eaf95e906d5dfc15f609f70fadfa1b3856
1 /* LTO symbol table.
2 Copyright (C) 2009-2017 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 "target.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cgraph.h"
30 #include "lto-streamer.h"
31 #include "ipa-utils.h"
32 #include "builtins.h"
33 #include "alias.h"
34 #include "lto-symtab.h"
36 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
37 all edges and removing the old node. */
39 static void
40 lto_cgraph_replace_node (struct cgraph_node *node,
41 struct cgraph_node *prevailing_node)
43 struct cgraph_edge *e, *next;
44 bool compatible_p;
46 if (symtab->dump_file)
48 fprintf (symtab->dump_file, "Replacing cgraph node %s by %s"
49 " for symbol %s\n",
50 node->dump_name (),
51 prevailing_node->dump_name (),
52 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
53 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
56 /* Merge node flags. */
57 if (node->force_output)
58 prevailing_node->mark_force_output ();
59 if (node->forced_by_abi)
60 prevailing_node->forced_by_abi = true;
61 if (node->address_taken)
63 gcc_assert (!prevailing_node->global.inlined_to);
64 prevailing_node->mark_address_taken ();
66 if (node->definition && prevailing_node->definition
67 && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
68 prevailing_node->merged_comdat = true;
70 /* Redirect all incoming edges. */
71 compatible_p
72 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
73 TREE_TYPE (TREE_TYPE (node->decl)));
74 for (e = node->callers; e; e = next)
76 next = e->next_caller;
77 e->redirect_callee (prevailing_node);
78 /* If there is a mismatch between the supposed callee return type and
79 the real one do not attempt to inline this function.
80 ??? We really need a way to match function signatures for ABI
81 compatibility and perform related promotions at inlining time. */
82 if (!compatible_p)
84 e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
85 e->call_stmt_cannot_inline_p = 1;
88 /* Redirect incomming references. */
89 prevailing_node->clone_referring (node);
91 /* Fix instrumentation references. */
92 if (node->instrumented_version)
94 gcc_assert (node->instrumentation_clone
95 == prevailing_node->instrumentation_clone);
96 node->instrumented_version->instrumented_version = prevailing_node;
97 if (!prevailing_node->instrumented_version)
98 prevailing_node->instrumented_version = node->instrumented_version;
99 /* Need to reset node->instrumented_version to NULL,
100 otherwise node removal code would reset
101 node->instrumented_version->instrumented_version. */
102 node->instrumented_version = NULL;
105 lto_free_function_in_decl_state_for_node (node);
107 if (node->decl != prevailing_node->decl)
108 node->release_body ();
110 /* Finally remove the replaced node. */
111 node->remove ();
114 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
115 all edges and removing the old node. */
117 static void
118 lto_varpool_replace_node (varpool_node *vnode,
119 varpool_node *prevailing_node)
121 gcc_assert (!vnode->definition || prevailing_node->definition);
122 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
124 prevailing_node->clone_referring (vnode);
125 if (vnode->force_output)
126 prevailing_node->force_output = true;
127 if (vnode->forced_by_abi)
128 prevailing_node->forced_by_abi = true;
130 /* Be sure we can garbage collect the initializer. */
131 if (DECL_INITIAL (vnode->decl)
132 && vnode->decl != prevailing_node->decl)
133 DECL_INITIAL (vnode->decl) = error_mark_node;
135 /* Check and report ODR violations on virtual tables. */
136 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
137 compare_virtual_tables (prevailing_node, vnode);
139 if (vnode->tls_model != prevailing_node->tls_model)
141 bool error = false;
143 /* Non-TLS and TLS never mix together. Also emulated model is not
144 compatible with anything else. */
145 if (prevailing_node->tls_model == TLS_MODEL_NONE
146 || prevailing_node->tls_model == TLS_MODEL_EMULATED
147 || vnode->tls_model == TLS_MODEL_NONE
148 || vnode->tls_model == TLS_MODEL_EMULATED)
149 error = true;
150 /* Linked is silently supporting transitions
151 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
152 Do the same transitions and error out on others. */
153 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
154 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
155 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
156 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
157 prevailing_node->tls_model = vnode->tls_model;
158 else if ((vnode->tls_model == TLS_MODEL_REAL
159 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
160 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
161 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
163 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
164 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
165 prevailing_node->tls_model = vnode->tls_model;
166 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
167 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
169 else
170 error = true;
171 if (error)
173 error_at (DECL_SOURCE_LOCATION (vnode->decl),
174 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
175 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
176 "previously defined here as %s",
177 tls_model_names [prevailing_node->tls_model]);
180 /* Finally remove the replaced node. */
181 vnode->remove ();
184 /* Return non-zero if we want to output waring about T1 and T2.
185 Return value is a bitmask of reasons of violation:
186 Bit 0 indicates that types are not compatible.
187 Bit 1 indicates that types are not compatible because of C++ ODR rule.
188 If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
189 Bit 2 indicates that types are not ODR compatible
191 The interoperability rules are language specific. At present we do only
192 full checking for C++ ODR rule and for other languages we do basic check
193 that data structures are of same size and TBAA compatible. Our TBAA
194 implementation should be coarse enough so all valid type transitions
195 across different languages are allowed.
197 In partiucular we thus allow almost arbitrary type changes with
198 -fno-strict-aliasing which may be tough of as a feature rather than bug
199 as it allows to implement dodgy tricks in the language runtimes.
201 Naturally this code can be strenghtened significantly if we could track
202 down the language of origin. */
204 static int
205 warn_type_compatibility_p (tree prevailing_type, tree type,
206 bool common_or_extern)
208 int lev = 0;
209 bool odr_p = odr_or_derived_type_p (prevailing_type)
210 && odr_or_derived_type_p (type);
212 if (prevailing_type == type)
213 return 0;
215 /* C++ provide a robust way to check for type compatibility via the ODR
216 rule. */
217 if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
218 lev |= 2;
220 /* Function types needs special care, because types_compatible_p never
221 thinks prototype is compatible to non-prototype. */
222 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
224 if (TREE_CODE (type) != TREE_CODE (prevailing_type))
225 lev |= 1;
226 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
227 TREE_TYPE (type), false);
228 if (TREE_CODE (type) == METHOD_TYPE
229 && TREE_CODE (prevailing_type) == METHOD_TYPE)
230 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
231 TYPE_METHOD_BASETYPE (type), false);
232 if (prototype_p (prevailing_type) && prototype_p (type)
233 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
235 tree parm1, parm2;
236 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
237 parm2 = TYPE_ARG_TYPES (type);
238 parm1 && parm2;
239 parm1 = TREE_CHAIN (parm1),
240 parm2 = TREE_CHAIN (parm2))
241 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
242 TREE_VALUE (parm2), false);
243 if (parm1 || parm2)
244 lev |= odr_p ? 3 : 1;
246 if (comp_type_attributes (prevailing_type, type) == 0)
247 lev |= 1;
248 return lev;
251 /* Get complete type. */
252 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
253 type = TYPE_MAIN_VARIANT (type);
255 /* We can not use types_compatible_p because we permit some changes
256 across types. For example unsigned size_t and "signed size_t" may be
257 compatible when merging C and Fortran types. */
258 if (COMPLETE_TYPE_P (prevailing_type)
259 && COMPLETE_TYPE_P (type)
260 /* While global declarations are never variadic, we can recurse here
261 for function parameter types. */
262 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
263 && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
264 && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
266 /* As a special case do not warn about merging
267 int a[];
269 int a[]={1,2,3};
270 here the first declaration is COMMON or EXTERN
271 and sizeof(a) == sizeof (int). */
272 if (!common_or_extern
273 || TREE_CODE (type) != ARRAY_TYPE
274 || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
275 lev |= 1;
278 /* Verify TBAA compatibility. Take care of alias set 0 and the fact that
279 we make ptr_type_node to TBAA compatible with every other type. */
280 if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
282 alias_set_type set1 = get_alias_set (type);
283 alias_set_type set2 = get_alias_set (prevailing_type);
285 if (set1 && set2 && set1 != set2
286 && (!POINTER_TYPE_P (type) || !POINTER_TYPE_P (prevailing_type)
287 || (set1 != TYPE_ALIAS_SET (ptr_type_node)
288 && set2 != TYPE_ALIAS_SET (ptr_type_node))))
289 lev |= 5;
292 return lev;
295 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
296 Return false if the symbols are not fully compatible and a diagnostic
297 should be emitted. */
299 static bool
300 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
302 tree prevailing_decl = prevailing->decl;
303 tree decl = entry->decl;
305 if (prevailing_decl == decl)
306 return true;
308 if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
309 return false;
311 /* Merge decl state in both directions, we may still end up using
312 the new decl. */
313 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
314 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
316 /* The linker may ask us to combine two incompatible symbols.
317 Detect this case and notify the caller of required diagnostics. */
319 if (TREE_CODE (decl) == FUNCTION_DECL)
321 /* Merge decl state in both directions, we may still end up using
322 the new decl. */
323 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
324 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
326 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
327 TREE_TYPE (decl),
328 DECL_COMMON (decl)
329 || DECL_EXTERNAL (decl)))
330 return false;
332 return true;
335 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
336 TREE_TYPE (decl),
337 DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
338 return false;
340 /* There is no point in comparing too many details of the decls here.
341 The type compatibility checks or the completing of types has properly
342 dealt with most issues. */
344 /* The following should all not invoke fatal errors as in non-LTO
345 mode the linker wouldn't complain either. Just emit warnings. */
347 /* Report a warning if user-specified alignments do not match. */
348 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
349 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
350 return false;
352 if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
353 && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl))
354 /* As a special case do not warn about merging
355 int a[];
357 int a[]={1,2,3};
358 here the first declaration is COMMON
359 and sizeof(a) == sizeof (int). */
360 && ((!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
361 || TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE
362 || TYPE_SIZE (TREE_TYPE (decl))
363 != TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))))
364 return false;
366 return true;
369 /* Return true if the symtab entry E can be replaced by another symtab
370 entry. */
372 static bool
373 lto_symtab_resolve_replaceable_p (symtab_node *e)
375 if (DECL_EXTERNAL (e->decl)
376 || DECL_COMDAT (e->decl)
377 || DECL_ONE_ONLY (e->decl)
378 || DECL_WEAK (e->decl))
379 return true;
381 if (TREE_CODE (e->decl) == VAR_DECL)
382 return (DECL_COMMON (e->decl)
383 || (!flag_no_common && !DECL_INITIAL (e->decl)));
385 return false;
388 /* Return true, if the symbol E should be resolved by lto-symtab.
389 Those are all external symbols and all real symbols that are not static (we
390 handle renaming of static later in partitioning). */
392 static bool
393 lto_symtab_symbol_p (symtab_node *e)
395 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
396 return false;
397 return e->real_symbol_p ();
400 /* Return true if the symtab entry E can be the prevailing one. */
402 static bool
403 lto_symtab_resolve_can_prevail_p (symtab_node *e)
405 if (!lto_symtab_symbol_p (e))
406 return false;
408 /* The C++ frontend ends up neither setting TREE_STATIC nor
409 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
410 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
411 if (DECL_EXTERNAL (e->decl))
412 return false;
414 return e->definition;
417 /* Resolve the symbol with the candidates in the chain *SLOT and store
418 their resolutions. */
420 static symtab_node *
421 lto_symtab_resolve_symbols (symtab_node *first)
423 symtab_node *e;
424 symtab_node *prevailing = NULL;
426 /* Always set e->node so that edges are updated to reflect decl merging. */
427 for (e = first; e; e = e->next_sharing_asm_name)
428 if (lto_symtab_symbol_p (e)
429 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
430 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
431 || e->resolution == LDPR_PREVAILING_DEF))
433 prevailing = e;
434 break;
437 /* If the chain is already resolved there is nothing else to do. */
438 if (prevailing)
440 /* Assert it's the only one. */
441 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
442 if (lto_symtab_symbol_p (e)
443 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
444 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
445 || e->resolution == LDPR_PREVAILING_DEF))
446 fatal_error (input_location, "multiple prevailing defs for %qE",
447 DECL_NAME (prevailing->decl));
448 return prevailing;
451 /* Find the single non-replaceable prevailing symbol and
452 diagnose ODR violations. */
453 for (e = first; e; e = e->next_sharing_asm_name)
455 if (!lto_symtab_resolve_can_prevail_p (e))
456 continue;
458 /* If we have a non-replaceable definition it prevails. */
459 if (!lto_symtab_resolve_replaceable_p (e))
461 if (prevailing)
463 error_at (DECL_SOURCE_LOCATION (e->decl),
464 "%qD has already been defined", e->decl);
465 inform (DECL_SOURCE_LOCATION (prevailing->decl),
466 "previously defined here");
468 prevailing = e;
471 if (prevailing)
472 return prevailing;
474 /* Do a second round choosing one from the replaceable prevailing decls. */
475 for (e = first; e; e = e->next_sharing_asm_name)
477 if (!lto_symtab_resolve_can_prevail_p (e))
478 continue;
480 /* Choose the first function that can prevail as prevailing. */
481 if (TREE_CODE (e->decl) == FUNCTION_DECL)
483 prevailing = e;
484 break;
487 /* From variables that can prevail choose the largest one. */
488 if (!prevailing
489 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
490 DECL_SIZE (e->decl))
491 /* When variables are equivalent try to chose one that has useful
492 DECL_INITIAL. This makes sense for keyed vtables that are
493 DECL_EXTERNAL but initialized. In units that do not need them
494 we replace the initializer by error_mark_node to conserve
495 memory.
497 We know that the vtable is keyed outside the LTO unit - otherwise
498 the keyed instance would prevail. We still can preserve useful
499 info in the initializer. */
500 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
501 && (DECL_INITIAL (e->decl)
502 && DECL_INITIAL (e->decl) != error_mark_node)
503 && (!DECL_INITIAL (prevailing->decl)
504 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
505 prevailing = e;
508 return prevailing;
511 /* Decide if it is OK to merge DECL into PREVAILING.
512 Because we wrap most of uses of declarations in MEM_REF, we can tolerate
513 some differences but other code may inspect directly the DECL. */
515 static bool
516 lto_symtab_merge_p (tree prevailing, tree decl)
518 if (TREE_CODE (prevailing) != TREE_CODE (decl))
520 if (symtab->dump_file)
521 fprintf (symtab->dump_file, "Not merging decls; "
522 "TREE_CODE mismatch\n");
523 return false;
525 gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
527 if (TREE_CODE (prevailing) == FUNCTION_DECL)
529 if (DECL_BUILT_IN (prevailing) != DECL_BUILT_IN (decl))
531 if (symtab->dump_file)
532 fprintf (symtab->dump_file, "Not merging decls; "
533 "DECL_BUILT_IN mismatch\n");
534 return false;
536 if (DECL_BUILT_IN (prevailing)
537 && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
538 || DECL_FUNCTION_CODE (prevailing) != DECL_FUNCTION_CODE (decl)))
540 if (symtab->dump_file)
541 fprintf (symtab->dump_file, "Not merging decls; "
542 "DECL_BUILT_IN_CLASS or CODE mismatch\n");
543 return false;
546 if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
548 tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
549 tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
550 if ((prev_attr == NULL) != (attr == NULL)
551 || (prev_attr
552 && TREE_VALUE (TREE_VALUE (prev_attr))
553 != TREE_VALUE (TREE_VALUE (attr))))
555 if (symtab->dump_file)
556 fprintf (symtab->dump_file, "Not merging decls; "
557 "error attribute mismatch\n");
558 return false;
561 prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
562 attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
563 if ((prev_attr == NULL) != (attr == NULL)
564 || (prev_attr
565 && TREE_VALUE (TREE_VALUE (prev_attr))
566 != TREE_VALUE (TREE_VALUE (attr))))
568 if (symtab->dump_file)
569 fprintf (symtab->dump_file, "Not merging decls; "
570 "warning attribute mismatch\n");
571 return false;
574 return true;
577 /* Merge all decls in the symbol table chain to the prevailing decl and
578 issue diagnostics about type mismatches. If DIAGNOSED_P is true
579 do not issue further diagnostics.*/
581 static void
582 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
584 symtab_node *prevailing;
585 symtab_node *e;
586 vec<tree> mismatches = vNULL;
587 unsigned i;
588 tree decl;
589 bool tbaa_p = false;
591 /* Nothing to do for a single entry. */
592 prevailing = first;
593 if (!prevailing->next_sharing_asm_name)
594 return;
596 /* Try to merge each entry with the prevailing one. */
597 symtab_node *last_prevailing = prevailing, *next;
598 for (e = prevailing->next_sharing_asm_name; e; e = next)
600 next = e->next_sharing_asm_name;
602 /* Skip non-LTO symbols and symbols whose declaration we already
603 visited. */
604 if (lto_symtab_prevailing_decl (e->decl) != e->decl
605 || !lto_symtab_symbol_p (e)
606 || e->decl == prevailing->decl)
607 continue;
609 if (!lto_symtab_merge (prevailing, e)
610 && !diagnosed_p
611 && !DECL_ARTIFICIAL (e->decl))
612 mismatches.safe_push (e->decl);
614 symtab_node *this_prevailing;
615 for (this_prevailing = prevailing; ;
616 this_prevailing = this_prevailing->next_sharing_asm_name)
618 if (this_prevailing->decl != e->decl
619 && lto_symtab_merge_p (this_prevailing->decl, e->decl))
620 break;
621 if (this_prevailing == last_prevailing)
623 this_prevailing = NULL;
624 break;
628 if (this_prevailing)
629 lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
630 /* Maintain LRU list: relink the new prevaililng symbol
631 just after previaling node in the chain and update last_prevailing.
632 Since the number of possible declarations of a given symbol is
633 small, this should be faster than building a hash. */
634 else if (e == prevailing->next_sharing_asm_name)
635 last_prevailing = e;
636 else
638 if (e->next_sharing_asm_name)
639 e->next_sharing_asm_name->previous_sharing_asm_name
640 = e->previous_sharing_asm_name;
641 e->previous_sharing_asm_name->next_sharing_asm_name
642 = e->next_sharing_asm_name;
643 e->previous_sharing_asm_name = prevailing;
644 e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
645 prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
646 prevailing->next_sharing_asm_name = e;
647 if (last_prevailing == prevailing)
648 last_prevailing = e;
651 if (mismatches.is_empty ())
652 return;
654 /* Diagnose all mismatched re-declarations. */
655 FOR_EACH_VEC_ELT (mismatches, i, decl)
657 /* Do not diagnose two built-in declarations, there is no useful
658 location in that case. It also happens for AVR if two built-ins
659 use the same asm name because their libgcc assembler code is the
660 same, see PR78562. */
661 if (DECL_IS_BUILTIN (prevailing->decl)
662 && DECL_IS_BUILTIN (decl))
663 continue;
665 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
666 TREE_TYPE (decl),
667 DECL_COMDAT (decl));
668 if (level)
670 bool diag = false;
671 if (level & 2)
672 diag = warning_at (DECL_SOURCE_LOCATION (decl),
673 OPT_Wodr,
674 "%qD violates the C++ One Definition Rule ",
675 decl);
676 if (!diag && (level & 1))
677 diag = warning_at (DECL_SOURCE_LOCATION (decl),
678 OPT_Wlto_type_mismatch,
679 "type of %qD does not match original "
680 "declaration", decl);
681 if (diag)
683 warn_types_mismatch (TREE_TYPE (prevailing->decl),
684 TREE_TYPE (decl),
685 DECL_SOURCE_LOCATION (prevailing->decl),
686 DECL_SOURCE_LOCATION (decl));
687 if ((level & 4)
688 && !TREE_READONLY (prevailing->decl))
689 tbaa_p = true;
691 diagnosed_p |= diag;
693 else if ((DECL_USER_ALIGN (prevailing->decl)
694 && DECL_USER_ALIGN (decl))
695 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
697 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
698 OPT_Wlto_type_mismatch,
699 "alignment of %qD is bigger than "
700 "original declaration", decl);
702 else
703 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
704 OPT_Wlto_type_mismatch,
705 "size of %qD differ from the size of "
706 "original declaration", decl);
708 if (diagnosed_p)
709 inform (DECL_SOURCE_LOCATION (prevailing->decl),
710 "%qD was previously declared here", prevailing->decl);
711 if (tbaa_p)
712 inform (DECL_SOURCE_LOCATION (prevailing->decl),
713 "code may be misoptimized unless "
714 "-fno-strict-aliasing is used");
716 mismatches.release ();
719 /* Helper to process the decl chain for the symbol table entry *SLOT. */
721 static void
722 lto_symtab_merge_decls_1 (symtab_node *first)
724 symtab_node *e;
725 symtab_node *prevailing;
726 bool diagnosed_p = false;
728 if (symtab->dump_file)
730 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
731 first->asm_name ());
732 for (e = first; e; e = e->next_sharing_asm_name)
733 if (TREE_PUBLIC (e->decl))
734 e->dump (symtab->dump_file);
737 /* Compute the symbol resolutions. This is a no-op when using the
738 linker plugin and resolution was decided by the linker. */
739 prevailing = lto_symtab_resolve_symbols (first);
741 /* If there's not a prevailing symbol yet it's an external reference.
742 Happens a lot during ltrans. Choose the first symbol with a
743 cgraph or a varpool node. */
744 if (!prevailing)
746 for (prevailing = first;
747 prevailing; prevailing = prevailing->next_sharing_asm_name)
748 if (lto_symtab_symbol_p (prevailing))
749 break;
750 if (!prevailing)
751 return;
752 /* For variables chose with a priority variant with vnode
753 attached (i.e. from unit where external declaration of
754 variable is actually used).
755 When there are multiple variants, chose one with size.
756 This is needed for C++ typeinfos, for example in
757 lto/20081204-1 there are typeifos in both units, just
758 one of them do have size. */
759 if (TREE_CODE (prevailing->decl) == VAR_DECL)
761 for (e = prevailing->next_sharing_asm_name;
762 e; e = e->next_sharing_asm_name)
763 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
764 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
765 && lto_symtab_symbol_p (e))
766 prevailing = e;
768 /* For functions prefer the non-builtin if one is available. */
769 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
771 for (e = first; e; e = e->next_sharing_asm_name)
772 if (TREE_CODE (e->decl) == FUNCTION_DECL
773 && !DECL_BUILT_IN (e->decl)
774 && lto_symtab_symbol_p (e))
776 prevailing = e;
777 break;
782 symtab->symtab_prevail_in_asm_name_hash (prevailing);
784 /* Diagnose mismatched objects. */
785 for (e = prevailing->next_sharing_asm_name;
786 e; e = e->next_sharing_asm_name)
788 if (TREE_CODE (prevailing->decl)
789 == TREE_CODE (e->decl))
790 continue;
791 if (!lto_symtab_symbol_p (e))
792 continue;
794 switch (TREE_CODE (prevailing->decl))
796 case VAR_DECL:
797 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
798 error_at (DECL_SOURCE_LOCATION (e->decl),
799 "variable %qD redeclared as function",
800 prevailing->decl);
801 break;
803 case FUNCTION_DECL:
804 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
805 error_at (DECL_SOURCE_LOCATION (e->decl),
806 "function %qD redeclared as variable",
807 prevailing->decl);
808 break;
810 default:
811 gcc_unreachable ();
814 diagnosed_p = true;
816 if (diagnosed_p)
817 inform (DECL_SOURCE_LOCATION (prevailing->decl),
818 "previously declared here");
820 /* Merge the chain to the single prevailing decl and diagnose
821 mismatches. */
822 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
824 if (symtab->dump_file)
826 fprintf (symtab->dump_file, "After resolution:\n");
827 for (e = prevailing; e; e = e->next_sharing_asm_name)
828 e->dump (symtab->dump_file);
832 /* Resolve and merge all symbol table chains to a prevailing decl. */
834 void
835 lto_symtab_merge_decls (void)
837 symtab_node *node;
839 /* Populate assembler name hash. */
840 symtab->symtab_initialize_asm_name_hash ();
842 FOR_EACH_SYMBOL (node)
843 if (!node->previous_sharing_asm_name
844 && node->next_sharing_asm_name)
845 lto_symtab_merge_decls_1 (node);
848 /* Helper to process the decl chain for the symbol table entry *SLOT. */
850 static void
851 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
853 symtab_node *e;
854 symtab_node *next;
856 prevailing->decl->decl_with_vis.symtab_node = prevailing;
858 /* Replace the cgraph node of each entry with the prevailing one. */
859 for (e = prevailing->next_sharing_asm_name; e;
860 e = next)
862 next = e->next_sharing_asm_name;
864 if (!lto_symtab_symbol_p (e))
865 continue;
866 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
867 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
869 /* No matter how we are going to deal with resolution, we will ultimately
870 use prevailing definition. */
871 if (ce)
872 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
873 dyn_cast<cgraph_node *> (e));
875 /* If we decided to replace the node by TO, do it. */
876 if (e != to)
878 if (ce)
879 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
880 else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
881 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
883 /* Watch out for duplicated symbols for a given declaration. */
884 else if (!e->transparent_alias
885 || !e->definition || e->get_alias_target () != to)
887 /* We got a new declaration we do not want to merge. In this case
888 get rid of the existing definition and create a transparent
889 alias. */
890 if (ce)
892 lto_free_function_in_decl_state_for_node (ce);
893 if (!ce->weakref)
894 ce->release_body ();
895 ce->reset ();
896 symtab->call_cgraph_removal_hooks (ce);
898 else
900 DECL_INITIAL (e->decl) = error_mark_node;
901 if (e->lto_file_data)
903 lto_free_function_in_decl_state_for_node (e);
904 e->lto_file_data = NULL;
906 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
908 e->remove_all_references ();
909 e->analyzed = e->body_removed = false;
910 e->resolve_alias (prevailing, true);
911 gcc_assert (e != prevailing);
915 return;
918 /* Merge cgraph nodes according to the symbol merging done by
919 lto_symtab_merge_decls. */
921 void
922 lto_symtab_merge_symbols (void)
924 symtab_node *node;
926 if (!flag_ltrans)
928 symtab->symtab_initialize_asm_name_hash ();
930 /* Do the actual merging.
931 At this point we invalidate hash translating decls into symtab nodes
932 because after removing one of duplicate decls the hash is not correcly
933 updated to the ohter dupliate. */
934 FOR_EACH_SYMBOL (node)
935 if (lto_symtab_symbol_p (node)
936 && node->next_sharing_asm_name
937 && !node->previous_sharing_asm_name)
938 lto_symtab_merge_symbols_1 (node);
940 /* Resolve weakref aliases whose target are now in the compilation unit.
941 also re-populate the hash translating decls into symtab nodes*/
942 FOR_EACH_SYMBOL (node)
944 cgraph_node *cnode, *cnode2;
945 varpool_node *vnode;
946 symtab_node *node2;
948 if (!node->analyzed && node->alias_target)
950 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
951 gcc_assert (node->weakref);
952 if (tgt)
953 node->resolve_alias (tgt, true);
955 /* If the symbol was preempted outside IR, see if we want to get rid
956 of the definition. */
957 if (node->analyzed
958 && !DECL_EXTERNAL (node->decl)
959 && (node->resolution == LDPR_PREEMPTED_REG
960 || node->resolution == LDPR_RESOLVED_IR
961 || node->resolution == LDPR_RESOLVED_EXEC
962 || node->resolution == LDPR_RESOLVED_DYN))
964 DECL_EXTERNAL (node->decl) = 1;
965 /* If alias to local symbol was preempted by external definition,
966 we know it is not pointing to the local symbol. Remove it. */
967 if (node->alias
968 && !node->weakref
969 && !node->transparent_alias
970 && node->get_alias_target ()->binds_to_current_def_p ())
972 node->alias = false;
973 node->remove_all_references ();
974 node->definition = false;
975 node->analyzed = false;
976 node->cpp_implicit_alias = false;
978 else if (!node->alias
979 && node->definition
980 && node->get_availability () <= AVAIL_INTERPOSABLE)
982 if ((cnode = dyn_cast <cgraph_node *> (node)) != NULL)
983 cnode->reset ();
984 else
986 node->analyzed = node->definition = false;
987 node->remove_all_references ();
992 if (!(cnode = dyn_cast <cgraph_node *> (node))
993 || !cnode->clone_of
994 || cnode->clone_of->decl != cnode->decl)
996 /* Builtins are not merged via decl merging. It is however
997 possible that tree merging unified the declaration. We
998 do not want duplicate entries in symbol table. */
999 if (cnode && DECL_BUILT_IN (node->decl)
1000 && (cnode2 = cgraph_node::get (node->decl))
1001 && cnode2 != cnode)
1002 lto_cgraph_replace_node (cnode2, cnode);
1004 /* The user defined assembler variables are also not unified by their
1005 symbol name (since it is irrelevant), but we need to unify symbol
1006 nodes if tree merging occurred. */
1007 if ((vnode = dyn_cast <varpool_node *> (node))
1008 && DECL_HARD_REGISTER (vnode->decl)
1009 && (node2 = symtab_node::get (vnode->decl))
1010 && node2 != node)
1011 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
1012 vnode);
1015 /* Abstract functions may have duplicated cgraph nodes attached;
1016 remove them. */
1017 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
1018 && (cnode2 = cgraph_node::get (node->decl))
1019 && cnode2 != cnode)
1020 cnode2->remove ();
1022 node->decl->decl_with_vis.symtab_node = node;
1028 /* Virtual tables may matter for code generation even if they are not
1029 directly refernced by the code because they may be used for devirtualizaiton.
1030 For this reason it is important to merge even virtual tables that have no
1031 associated symbol table entries. Without doing so we lose optimization
1032 oppurtunities by losing track of the vtable constructor.
1033 FIXME: we probably ought to introduce explicit symbol table entries for
1034 those before streaming. */
1036 tree
1037 lto_symtab_prevailing_virtual_decl (tree decl)
1039 if (DECL_ABSTRACT_P (decl))
1040 return decl;
1041 gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
1042 && DECL_ASSEMBLER_NAME_SET_P (decl));
1044 symtab_node *n = symtab_node::get_for_asmname
1045 (DECL_ASSEMBLER_NAME (decl));
1046 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1047 || !DECL_VIRTUAL_P (n->decl)))
1048 n = n->next_sharing_asm_name;
1049 if (n)
1051 /* Merge decl state in both directions, we may still end up using
1052 the other decl. */
1053 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1054 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1056 if (TREE_CODE (decl) == FUNCTION_DECL)
1058 /* Merge decl state in both directions, we may still end up using
1059 the other decl. */
1060 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1061 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1063 lto_symtab_prevail_decl (n->decl, decl);
1064 decl = n->decl;
1066 else
1067 symtab_node::get_create (decl);
1069 return decl;