Clean up attribute value comparison in lto-symtab.c.
[official-gcc.git] / gcc / lto / lto-symtab.c
blob2660542300e77cc216eb7a762028dcec3c380cbc
1 /* LTO symbol table.
2 Copyright (C) 2009-2018 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"
35 #include "stringpool.h"
36 #include "attribs.h"
38 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
39 all edges and removing the old node. */
41 static void
42 lto_cgraph_replace_node (struct cgraph_node *node,
43 struct cgraph_node *prevailing_node)
45 struct cgraph_edge *e, *next;
46 bool compatible_p;
48 if (symtab->dump_file)
50 fprintf (symtab->dump_file, "Replacing cgraph node %s by %s"
51 " for symbol %s\n",
52 node->dump_name (),
53 prevailing_node->dump_name (),
54 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
55 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
58 /* Merge node flags. */
59 if (node->force_output)
60 prevailing_node->mark_force_output ();
61 if (node->forced_by_abi)
62 prevailing_node->forced_by_abi = true;
63 if (node->address_taken)
65 gcc_assert (!prevailing_node->global.inlined_to);
66 prevailing_node->mark_address_taken ();
68 if (node->definition && prevailing_node->definition
69 && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
70 prevailing_node->merged_comdat = true;
72 /* Redirect all incoming edges. */
73 compatible_p
74 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
75 TREE_TYPE (TREE_TYPE (node->decl)));
76 for (e = node->callers; e; e = next)
78 next = e->next_caller;
79 e->redirect_callee (prevailing_node);
80 /* If there is a mismatch between the supposed callee return type and
81 the real one do not attempt to inline this function.
82 ??? We really need a way to match function signatures for ABI
83 compatibility and perform related promotions at inlining time. */
84 if (!compatible_p)
86 e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
87 e->call_stmt_cannot_inline_p = 1;
90 /* Redirect incomming references. */
91 prevailing_node->clone_referring (node);
93 /* Fix instrumentation references. */
94 if (node->instrumented_version)
96 gcc_assert (node->instrumentation_clone
97 == prevailing_node->instrumentation_clone);
98 node->instrumented_version->instrumented_version = prevailing_node;
99 if (!prevailing_node->instrumented_version)
100 prevailing_node->instrumented_version = node->instrumented_version;
101 /* Need to reset node->instrumented_version to NULL,
102 otherwise node removal code would reset
103 node->instrumented_version->instrumented_version. */
104 node->instrumented_version = NULL;
107 lto_free_function_in_decl_state_for_node (node);
109 if (node->decl != prevailing_node->decl)
110 node->release_body ();
112 /* Finally remove the replaced node. */
113 node->remove ();
116 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
117 all edges and removing the old node. */
119 static void
120 lto_varpool_replace_node (varpool_node *vnode,
121 varpool_node *prevailing_node)
123 gcc_assert (!vnode->definition || prevailing_node->definition);
124 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
126 prevailing_node->clone_referring (vnode);
127 if (vnode->force_output)
128 prevailing_node->force_output = true;
129 if (vnode->forced_by_abi)
130 prevailing_node->forced_by_abi = true;
132 /* Be sure we can garbage collect the initializer. */
133 if (DECL_INITIAL (vnode->decl)
134 && vnode->decl != prevailing_node->decl)
135 DECL_INITIAL (vnode->decl) = error_mark_node;
137 /* Check and report ODR violations on virtual tables. */
138 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
139 compare_virtual_tables (prevailing_node, vnode);
141 if (vnode->tls_model != prevailing_node->tls_model)
143 bool error = false;
145 /* Non-TLS and TLS never mix together. Also emulated model is not
146 compatible with anything else. */
147 if (prevailing_node->tls_model == TLS_MODEL_NONE
148 || prevailing_node->tls_model == TLS_MODEL_EMULATED
149 || vnode->tls_model == TLS_MODEL_NONE
150 || vnode->tls_model == TLS_MODEL_EMULATED)
151 error = true;
152 /* Linked is silently supporting transitions
153 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
154 Do the same transitions and error out on others. */
155 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
156 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
157 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
158 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
159 prevailing_node->tls_model = vnode->tls_model;
160 else if ((vnode->tls_model == TLS_MODEL_REAL
161 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
162 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
163 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
165 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
166 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
167 prevailing_node->tls_model = vnode->tls_model;
168 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
169 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
171 else
172 error = true;
173 if (error)
175 error_at (DECL_SOURCE_LOCATION (vnode->decl),
176 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
177 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
178 "previously defined here as %s",
179 tls_model_names [prevailing_node->tls_model]);
182 /* Finally remove the replaced node. */
183 vnode->remove ();
186 /* Return non-zero if we want to output waring about T1 and T2.
187 Return value is a bitmask of reasons of violation:
188 Bit 0 indicates that types are not compatible.
189 Bit 1 indicates that types are not compatible because of C++ ODR rule.
190 If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
191 Bit 2 indicates that types are not ODR compatible
193 The interoperability rules are language specific. At present we do only
194 full checking for C++ ODR rule and for other languages we do basic check
195 that data structures are of same size and TBAA compatible. Our TBAA
196 implementation should be coarse enough so all valid type transitions
197 across different languages are allowed.
199 In partiucular we thus allow almost arbitrary type changes with
200 -fno-strict-aliasing which may be tough of as a feature rather than bug
201 as it allows to implement dodgy tricks in the language runtimes.
203 Naturally this code can be strenghtened significantly if we could track
204 down the language of origin. */
206 static int
207 warn_type_compatibility_p (tree prevailing_type, tree type,
208 bool common_or_extern)
210 int lev = 0;
211 bool odr_p = odr_or_derived_type_p (prevailing_type)
212 && odr_or_derived_type_p (type);
214 if (prevailing_type == type)
215 return 0;
217 /* C++ provide a robust way to check for type compatibility via the ODR
218 rule. */
219 if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
220 lev |= 2;
222 /* Function types needs special care, because types_compatible_p never
223 thinks prototype is compatible to non-prototype. */
224 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
226 if (TREE_CODE (type) != TREE_CODE (prevailing_type))
227 lev |= 1;
228 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
229 TREE_TYPE (type), false);
230 if (TREE_CODE (type) == METHOD_TYPE
231 && TREE_CODE (prevailing_type) == METHOD_TYPE)
232 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
233 TYPE_METHOD_BASETYPE (type), false);
234 if (prototype_p (prevailing_type) && prototype_p (type)
235 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
237 tree parm1, parm2;
238 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
239 parm2 = TYPE_ARG_TYPES (type);
240 parm1 && parm2;
241 parm1 = TREE_CHAIN (parm1),
242 parm2 = TREE_CHAIN (parm2))
243 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
244 TREE_VALUE (parm2), false);
245 if (parm1 || parm2)
246 lev |= odr_p ? 3 : 1;
248 if (comp_type_attributes (prevailing_type, type) == 0)
249 lev |= 1;
250 return lev;
253 /* Get complete type. */
254 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
255 type = TYPE_MAIN_VARIANT (type);
257 /* We can not use types_compatible_p because we permit some changes
258 across types. For example unsigned size_t and "signed size_t" may be
259 compatible when merging C and Fortran types. */
260 if (COMPLETE_TYPE_P (prevailing_type)
261 && COMPLETE_TYPE_P (type)
262 /* While global declarations are never variadic, we can recurse here
263 for function parameter types. */
264 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
265 && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
266 && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
268 /* As a special case do not warn about merging
269 int a[];
271 int a[]={1,2,3};
272 here the first declaration is COMMON or EXTERN
273 and sizeof(a) == sizeof (int). */
274 if (!common_or_extern
275 || TREE_CODE (type) != ARRAY_TYPE
276 || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
277 lev |= 1;
280 /* Verify TBAA compatibility. Take care of alias set 0 and the fact that
281 we make ptr_type_node to TBAA compatible with every other type. */
282 if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
284 alias_set_type set1 = get_alias_set (type);
285 alias_set_type set2 = get_alias_set (prevailing_type);
287 if (set1 && set2 && set1 != set2)
289 tree t1 = type, t2 = prevailing_type;
291 /* Alias sets of arrays with aliased components are the same as alias
292 sets of the inner types. */
293 while (TREE_CODE (t1) == ARRAY_TYPE
294 && !TYPE_NONALIASED_COMPONENT (t1)
295 && TREE_CODE (t2) == ARRAY_TYPE
296 && !TYPE_NONALIASED_COMPONENT (t2))
298 t1 = TREE_TYPE (t1);
299 t2 = TREE_TYPE (t2);
301 if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2))
302 || (set1 != TYPE_ALIAS_SET (ptr_type_node)
303 && set2 != TYPE_ALIAS_SET (ptr_type_node)))
304 lev |= 5;
308 return lev;
311 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
312 Return false if the symbols are not fully compatible and a diagnostic
313 should be emitted. */
315 static bool
316 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
318 tree prevailing_decl = prevailing->decl;
319 tree decl = entry->decl;
321 if (prevailing_decl == decl)
322 return true;
324 if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
325 return false;
327 /* Merge decl state in both directions, we may still end up using
328 the new decl. */
329 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
330 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
332 /* The linker may ask us to combine two incompatible symbols.
333 Detect this case and notify the caller of required diagnostics. */
335 if (TREE_CODE (decl) == FUNCTION_DECL)
337 /* Merge decl state in both directions, we may still end up using
338 the new decl. */
339 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
340 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
342 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
343 TREE_TYPE (decl),
344 DECL_COMMON (decl)
345 || DECL_EXTERNAL (decl)))
346 return false;
348 return true;
351 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
352 TREE_TYPE (decl),
353 DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
354 return false;
356 /* There is no point in comparing too many details of the decls here.
357 The type compatibility checks or the completing of types has properly
358 dealt with most issues. */
360 /* The following should all not invoke fatal errors as in non-LTO
361 mode the linker wouldn't complain either. Just emit warnings. */
363 /* Report a warning if user-specified alignments do not match. */
364 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
365 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
366 return false;
368 if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
369 && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
371 if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
372 return false;
374 tree type = TREE_TYPE (decl);
376 /* For record type, check for array at the end of the structure. */
377 if (TREE_CODE (type) == RECORD_TYPE)
379 tree field = TYPE_FIELDS (type);
380 while (DECL_CHAIN (field) != NULL_TREE)
381 field = DECL_CHAIN (field);
383 return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE;
385 /* As a special case do not warn about merging
386 int a[];
388 int a[]={1,2,3};
389 here the first declaration is COMMON
390 and sizeof(a) == sizeof (int). */
391 else if (TREE_CODE (type) == ARRAY_TYPE)
392 return (TYPE_SIZE (decl) == TYPE_SIZE (TREE_TYPE (type)));
395 return true;
398 /* Return true if the symtab entry E can be replaced by another symtab
399 entry. */
401 static bool
402 lto_symtab_resolve_replaceable_p (symtab_node *e)
404 if (DECL_EXTERNAL (e->decl)
405 || DECL_COMDAT (e->decl)
406 || DECL_ONE_ONLY (e->decl)
407 || DECL_WEAK (e->decl))
408 return true;
410 if (TREE_CODE (e->decl) == VAR_DECL)
411 return (DECL_COMMON (e->decl)
412 || (!flag_no_common && !DECL_INITIAL (e->decl)));
414 return false;
417 /* Return true, if the symbol E should be resolved by lto-symtab.
418 Those are all external symbols and all real symbols that are not static (we
419 handle renaming of static later in partitioning). */
421 static bool
422 lto_symtab_symbol_p (symtab_node *e)
424 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
425 return false;
426 return e->real_symbol_p ();
429 /* Return true if the symtab entry E can be the prevailing one. */
431 static bool
432 lto_symtab_resolve_can_prevail_p (symtab_node *e)
434 if (!lto_symtab_symbol_p (e))
435 return false;
437 /* The C++ frontend ends up neither setting TREE_STATIC nor
438 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
439 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
440 if (DECL_EXTERNAL (e->decl))
441 return false;
443 return e->definition;
446 /* Resolve the symbol with the candidates in the chain *SLOT and store
447 their resolutions. */
449 static symtab_node *
450 lto_symtab_resolve_symbols (symtab_node *first)
452 symtab_node *e;
453 symtab_node *prevailing = NULL;
455 /* Always set e->node so that edges are updated to reflect decl merging. */
456 for (e = first; e; e = e->next_sharing_asm_name)
457 if (lto_symtab_symbol_p (e)
458 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
459 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
460 || e->resolution == LDPR_PREVAILING_DEF))
462 prevailing = e;
463 break;
466 /* If the chain is already resolved there is nothing else to do. */
467 if (prevailing)
469 /* Assert it's the only one. */
470 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
471 if (lto_symtab_symbol_p (e)
472 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
473 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
474 || e->resolution == LDPR_PREVAILING_DEF))
475 fatal_error (input_location, "multiple prevailing defs for %qE",
476 DECL_NAME (prevailing->decl));
477 return prevailing;
480 /* Find the single non-replaceable prevailing symbol and
481 diagnose ODR violations. */
482 for (e = first; e; e = e->next_sharing_asm_name)
484 if (!lto_symtab_resolve_can_prevail_p (e))
485 continue;
487 /* If we have a non-replaceable definition it prevails. */
488 if (!lto_symtab_resolve_replaceable_p (e))
490 if (prevailing)
492 error_at (DECL_SOURCE_LOCATION (e->decl),
493 "%qD has already been defined", e->decl);
494 inform (DECL_SOURCE_LOCATION (prevailing->decl),
495 "previously defined here");
497 prevailing = e;
500 if (prevailing)
501 return prevailing;
503 /* Do a second round choosing one from the replaceable prevailing decls. */
504 for (e = first; e; e = e->next_sharing_asm_name)
506 if (!lto_symtab_resolve_can_prevail_p (e))
507 continue;
509 /* Choose the first function that can prevail as prevailing. */
510 if (TREE_CODE (e->decl) == FUNCTION_DECL)
512 prevailing = e;
513 break;
516 /* From variables that can prevail choose the largest one. */
517 if (!prevailing
518 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
519 DECL_SIZE (e->decl))
520 /* When variables are equivalent try to chose one that has useful
521 DECL_INITIAL. This makes sense for keyed vtables that are
522 DECL_EXTERNAL but initialized. In units that do not need them
523 we replace the initializer by error_mark_node to conserve
524 memory.
526 We know that the vtable is keyed outside the LTO unit - otherwise
527 the keyed instance would prevail. We still can preserve useful
528 info in the initializer. */
529 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
530 && (DECL_INITIAL (e->decl)
531 && DECL_INITIAL (e->decl) != error_mark_node)
532 && (!DECL_INITIAL (prevailing->decl)
533 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
534 prevailing = e;
537 return prevailing;
540 /* Decide if it is OK to merge DECL into PREVAILING.
541 Because we wrap most of uses of declarations in MEM_REF, we can tolerate
542 some differences but other code may inspect directly the DECL. */
544 static bool
545 lto_symtab_merge_p (tree prevailing, tree decl)
547 if (TREE_CODE (prevailing) != TREE_CODE (decl))
549 if (symtab->dump_file)
550 fprintf (symtab->dump_file, "Not merging decls; "
551 "TREE_CODE mismatch\n");
552 return false;
554 gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
556 if (TREE_CODE (prevailing) == FUNCTION_DECL)
558 if (DECL_BUILT_IN (prevailing) != DECL_BUILT_IN (decl))
560 if (symtab->dump_file)
561 fprintf (symtab->dump_file, "Not merging decls; "
562 "DECL_BUILT_IN mismatch\n");
563 return false;
565 if (DECL_BUILT_IN (prevailing)
566 && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
567 || DECL_FUNCTION_CODE (prevailing) != DECL_FUNCTION_CODE (decl)))
569 if (symtab->dump_file)
570 fprintf (symtab->dump_file, "Not merging decls; "
571 "DECL_BUILT_IN_CLASS or CODE mismatch\n");
572 return false;
576 /* FIXME: after MPX is removed, use flags_from_decl_or_type
577 function instead. PR lto/85248. */
578 if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
580 tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
581 tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
582 if ((prev_attr == NULL) != (attr == NULL)
583 || (prev_attr && !attribute_value_equal (prev_attr, attr)))
585 if (symtab->dump_file)
586 fprintf (symtab->dump_file, "Not merging decls; "
587 "error attribute mismatch\n");
588 return false;
591 prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
592 attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
593 if ((prev_attr == NULL) != (attr == NULL)
594 || (prev_attr && !attribute_value_equal (prev_attr, attr)))
596 if (symtab->dump_file)
597 fprintf (symtab->dump_file, "Not merging decls; "
598 "warning attribute mismatch\n");
599 return false;
602 prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing));
603 attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl));
604 if ((prev_attr == NULL) != (attr == NULL))
606 if (symtab->dump_file)
607 fprintf (symtab->dump_file, "Not merging decls; "
608 "noreturn attribute mismatch\n");
609 return false;
612 return true;
615 /* Merge all decls in the symbol table chain to the prevailing decl and
616 issue diagnostics about type mismatches. If DIAGNOSED_P is true
617 do not issue further diagnostics.*/
619 static void
620 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
622 symtab_node *prevailing;
623 symtab_node *e;
624 vec<tree> mismatches = vNULL;
625 unsigned i;
626 tree decl;
627 bool tbaa_p = false;
629 /* Nothing to do for a single entry. */
630 prevailing = first;
631 if (!prevailing->next_sharing_asm_name)
632 return;
634 /* Try to merge each entry with the prevailing one. */
635 symtab_node *last_prevailing = prevailing, *next;
636 for (e = prevailing->next_sharing_asm_name; e; e = next)
638 next = e->next_sharing_asm_name;
640 /* Skip non-LTO symbols and symbols whose declaration we already
641 visited. */
642 if (lto_symtab_prevailing_decl (e->decl) != e->decl
643 || !lto_symtab_symbol_p (e)
644 || e->decl == prevailing->decl)
645 continue;
647 if (!lto_symtab_merge (prevailing, e)
648 && !diagnosed_p
649 && !DECL_ARTIFICIAL (e->decl))
650 mismatches.safe_push (e->decl);
652 symtab_node *this_prevailing;
653 for (this_prevailing = prevailing; ;
654 this_prevailing = this_prevailing->next_sharing_asm_name)
656 if (this_prevailing->decl != e->decl
657 && lto_symtab_merge_p (this_prevailing->decl, e->decl))
658 break;
659 if (this_prevailing == last_prevailing)
661 this_prevailing = NULL;
662 break;
666 if (this_prevailing)
667 lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
668 /* Maintain LRU list: relink the new prevaililng symbol
669 just after previaling node in the chain and update last_prevailing.
670 Since the number of possible declarations of a given symbol is
671 small, this should be faster than building a hash. */
672 else if (e == prevailing->next_sharing_asm_name)
673 last_prevailing = e;
674 else
676 if (e->next_sharing_asm_name)
677 e->next_sharing_asm_name->previous_sharing_asm_name
678 = e->previous_sharing_asm_name;
679 e->previous_sharing_asm_name->next_sharing_asm_name
680 = e->next_sharing_asm_name;
681 e->previous_sharing_asm_name = prevailing;
682 e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
683 prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
684 prevailing->next_sharing_asm_name = e;
685 if (last_prevailing == prevailing)
686 last_prevailing = e;
689 if (mismatches.is_empty ())
690 return;
692 /* Diagnose all mismatched re-declarations. */
693 FOR_EACH_VEC_ELT (mismatches, i, decl)
695 /* Do not diagnose two built-in declarations, there is no useful
696 location in that case. It also happens for AVR if two built-ins
697 use the same asm name because their libgcc assembler code is the
698 same, see PR78562. */
699 if (DECL_IS_BUILTIN (prevailing->decl)
700 && DECL_IS_BUILTIN (decl))
701 continue;
703 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
704 TREE_TYPE (decl),
705 DECL_COMDAT (decl));
706 if (level)
708 bool diag = false;
709 if (level & 2)
710 diag = warning_at (DECL_SOURCE_LOCATION (decl),
711 OPT_Wodr,
712 "%qD violates the C++ One Definition Rule ",
713 decl);
714 if (!diag && (level & 1))
715 diag = warning_at (DECL_SOURCE_LOCATION (decl),
716 OPT_Wlto_type_mismatch,
717 "type of %qD does not match original "
718 "declaration", decl);
719 if (diag)
721 warn_types_mismatch (TREE_TYPE (prevailing->decl),
722 TREE_TYPE (decl),
723 DECL_SOURCE_LOCATION (prevailing->decl),
724 DECL_SOURCE_LOCATION (decl));
725 if ((level & 4)
726 && !TREE_READONLY (prevailing->decl))
727 tbaa_p = true;
729 diagnosed_p |= diag;
731 else if ((DECL_USER_ALIGN (prevailing->decl)
732 && DECL_USER_ALIGN (decl))
733 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
735 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
736 OPT_Wlto_type_mismatch,
737 "alignment of %qD is bigger than "
738 "original declaration", decl);
740 else
741 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
742 OPT_Wlto_type_mismatch,
743 "size of %qD differ from the size of "
744 "original declaration", decl);
746 if (diagnosed_p)
747 inform (DECL_SOURCE_LOCATION (prevailing->decl),
748 "%qD was previously declared here", prevailing->decl);
749 if (tbaa_p)
750 inform (DECL_SOURCE_LOCATION (prevailing->decl),
751 "code may be misoptimized unless "
752 "-fno-strict-aliasing is used");
754 mismatches.release ();
757 /* Helper to process the decl chain for the symbol table entry *SLOT. */
759 static void
760 lto_symtab_merge_decls_1 (symtab_node *first)
762 symtab_node *e;
763 symtab_node *prevailing;
764 bool diagnosed_p = false;
766 if (symtab->dump_file)
768 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
769 first->asm_name ());
770 for (e = first; e; e = e->next_sharing_asm_name)
771 if (TREE_PUBLIC (e->decl))
772 e->dump (symtab->dump_file);
775 /* Compute the symbol resolutions. This is a no-op when using the
776 linker plugin and resolution was decided by the linker. */
777 prevailing = lto_symtab_resolve_symbols (first);
779 /* If there's not a prevailing symbol yet it's an external reference.
780 Happens a lot during ltrans. Choose the first symbol with a
781 cgraph or a varpool node. */
782 if (!prevailing)
784 for (prevailing = first;
785 prevailing; prevailing = prevailing->next_sharing_asm_name)
786 if (lto_symtab_symbol_p (prevailing))
787 break;
788 if (!prevailing)
789 return;
790 /* For variables chose with a priority variant with vnode
791 attached (i.e. from unit where external declaration of
792 variable is actually used).
793 When there are multiple variants, chose one with size.
794 This is needed for C++ typeinfos, for example in
795 lto/20081204-1 there are typeifos in both units, just
796 one of them do have size. */
797 if (TREE_CODE (prevailing->decl) == VAR_DECL)
799 for (e = prevailing->next_sharing_asm_name;
800 e; e = e->next_sharing_asm_name)
801 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
802 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
803 && lto_symtab_symbol_p (e))
804 prevailing = e;
806 /* For functions prefer the non-builtin if one is available. */
807 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
809 for (e = first; e; e = e->next_sharing_asm_name)
810 if (TREE_CODE (e->decl) == FUNCTION_DECL
811 && !DECL_BUILT_IN (e->decl)
812 && lto_symtab_symbol_p (e))
814 prevailing = e;
815 break;
820 symtab->symtab_prevail_in_asm_name_hash (prevailing);
822 /* Diagnose mismatched objects. */
823 for (e = prevailing->next_sharing_asm_name;
824 e; e = e->next_sharing_asm_name)
826 if (TREE_CODE (prevailing->decl)
827 == TREE_CODE (e->decl))
828 continue;
829 if (!lto_symtab_symbol_p (e))
830 continue;
832 switch (TREE_CODE (prevailing->decl))
834 case VAR_DECL:
835 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
836 error_at (DECL_SOURCE_LOCATION (e->decl),
837 "variable %qD redeclared as function",
838 prevailing->decl);
839 break;
841 case FUNCTION_DECL:
842 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
843 error_at (DECL_SOURCE_LOCATION (e->decl),
844 "function %qD redeclared as variable",
845 prevailing->decl);
846 break;
848 default:
849 gcc_unreachable ();
852 diagnosed_p = true;
854 if (diagnosed_p)
855 inform (DECL_SOURCE_LOCATION (prevailing->decl),
856 "previously declared here");
858 /* Merge the chain to the single prevailing decl and diagnose
859 mismatches. */
860 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
862 if (symtab->dump_file)
864 fprintf (symtab->dump_file, "After resolution:\n");
865 for (e = prevailing; e; e = e->next_sharing_asm_name)
866 e->dump (symtab->dump_file);
870 /* Resolve and merge all symbol table chains to a prevailing decl. */
872 void
873 lto_symtab_merge_decls (void)
875 symtab_node *node;
877 /* Populate assembler name hash. */
878 symtab->symtab_initialize_asm_name_hash ();
880 FOR_EACH_SYMBOL (node)
881 if (!node->previous_sharing_asm_name
882 && node->next_sharing_asm_name)
883 lto_symtab_merge_decls_1 (node);
886 /* Helper to process the decl chain for the symbol table entry *SLOT. */
888 static void
889 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
891 symtab_node *e;
892 symtab_node *next;
894 prevailing->decl->decl_with_vis.symtab_node = prevailing;
896 /* Replace the cgraph node of each entry with the prevailing one. */
897 for (e = prevailing->next_sharing_asm_name; e;
898 e = next)
900 next = e->next_sharing_asm_name;
902 if (!lto_symtab_symbol_p (e))
903 continue;
904 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
905 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
907 /* No matter how we are going to deal with resolution, we will ultimately
908 use prevailing definition. */
909 if (ce)
910 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
911 dyn_cast<cgraph_node *> (e));
913 /* If we decided to replace the node by TO, do it. */
914 if (e != to)
916 if (ce)
917 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
918 else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
919 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
921 /* Watch out for duplicated symbols for a given declaration. */
922 else if (!e->transparent_alias
923 || !e->definition || e->get_alias_target () != to)
925 /* We got a new declaration we do not want to merge. In this case
926 get rid of the existing definition and create a transparent
927 alias. */
928 if (ce)
930 lto_free_function_in_decl_state_for_node (ce);
931 if (!ce->weakref)
932 ce->release_body ();
933 ce->reset ();
934 symtab->call_cgraph_removal_hooks (ce);
936 else
938 DECL_INITIAL (e->decl) = error_mark_node;
939 if (e->lto_file_data)
941 lto_free_function_in_decl_state_for_node (e);
942 e->lto_file_data = NULL;
944 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
946 e->remove_all_references ();
947 e->analyzed = e->body_removed = false;
948 e->resolve_alias (prevailing, true);
949 gcc_assert (e != prevailing);
953 return;
956 /* Merge cgraph nodes according to the symbol merging done by
957 lto_symtab_merge_decls. */
959 void
960 lto_symtab_merge_symbols (void)
962 symtab_node *node;
964 if (!flag_ltrans)
966 symtab->symtab_initialize_asm_name_hash ();
968 /* Do the actual merging.
969 At this point we invalidate hash translating decls into symtab nodes
970 because after removing one of duplicate decls the hash is not correcly
971 updated to the ohter dupliate. */
972 FOR_EACH_SYMBOL (node)
973 if (lto_symtab_symbol_p (node)
974 && node->next_sharing_asm_name
975 && !node->previous_sharing_asm_name)
976 lto_symtab_merge_symbols_1 (node);
978 /* Resolve weakref aliases whose target are now in the compilation unit.
979 also re-populate the hash translating decls into symtab nodes*/
980 FOR_EACH_SYMBOL (node)
982 cgraph_node *cnode, *cnode2;
983 varpool_node *vnode;
984 symtab_node *node2;
986 if (!node->analyzed && node->alias_target)
988 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
989 gcc_assert (node->weakref);
990 if (tgt)
991 node->resolve_alias (tgt, true);
993 /* If the symbol was preempted outside IR, see if we want to get rid
994 of the definition. */
995 if (node->analyzed
996 && !DECL_EXTERNAL (node->decl)
997 && (node->resolution == LDPR_PREEMPTED_REG
998 || node->resolution == LDPR_RESOLVED_IR
999 || node->resolution == LDPR_RESOLVED_EXEC
1000 || node->resolution == LDPR_RESOLVED_DYN))
1002 DECL_EXTERNAL (node->decl) = 1;
1003 /* If alias to local symbol was preempted by external definition,
1004 we know it is not pointing to the local symbol. Remove it. */
1005 if (node->alias
1006 && !node->weakref
1007 && !node->transparent_alias
1008 && node->get_alias_target ()->binds_to_current_def_p ())
1010 node->alias = false;
1011 node->remove_all_references ();
1012 node->definition = false;
1013 node->analyzed = false;
1014 node->cpp_implicit_alias = false;
1016 else if (!node->alias
1017 && node->definition
1018 && node->get_availability () <= AVAIL_INTERPOSABLE)
1020 if ((cnode = dyn_cast <cgraph_node *> (node)) != NULL)
1021 cnode->reset ();
1022 else
1024 node->analyzed = node->definition = false;
1025 node->remove_all_references ();
1030 if (!(cnode = dyn_cast <cgraph_node *> (node))
1031 || !cnode->clone_of
1032 || cnode->clone_of->decl != cnode->decl)
1034 /* Builtins are not merged via decl merging. It is however
1035 possible that tree merging unified the declaration. We
1036 do not want duplicate entries in symbol table. */
1037 if (cnode && DECL_BUILT_IN (node->decl)
1038 && (cnode2 = cgraph_node::get (node->decl))
1039 && cnode2 != cnode)
1040 lto_cgraph_replace_node (cnode2, cnode);
1042 /* The user defined assembler variables are also not unified by their
1043 symbol name (since it is irrelevant), but we need to unify symbol
1044 nodes if tree merging occurred. */
1045 if ((vnode = dyn_cast <varpool_node *> (node))
1046 && DECL_HARD_REGISTER (vnode->decl)
1047 && (node2 = symtab_node::get (vnode->decl))
1048 && node2 != node)
1049 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
1050 vnode);
1053 /* Abstract functions may have duplicated cgraph nodes attached;
1054 remove them. */
1055 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
1056 && (cnode2 = cgraph_node::get (node->decl))
1057 && cnode2 != cnode)
1058 cnode2->remove ();
1060 node->decl->decl_with_vis.symtab_node = node;
1066 /* Virtual tables may matter for code generation even if they are not
1067 directly refernced by the code because they may be used for devirtualizaiton.
1068 For this reason it is important to merge even virtual tables that have no
1069 associated symbol table entries. Without doing so we lose optimization
1070 oppurtunities by losing track of the vtable constructor.
1071 FIXME: we probably ought to introduce explicit symbol table entries for
1072 those before streaming. */
1074 tree
1075 lto_symtab_prevailing_virtual_decl (tree decl)
1077 if (DECL_ABSTRACT_P (decl))
1078 return decl;
1079 gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
1080 && DECL_ASSEMBLER_NAME_SET_P (decl));
1082 symtab_node *n = symtab_node::get_for_asmname
1083 (DECL_ASSEMBLER_NAME (decl));
1084 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1085 || !DECL_VIRTUAL_P (n->decl)))
1086 n = n->next_sharing_asm_name;
1087 if (n)
1089 /* Merge decl state in both directions, we may still end up using
1090 the other decl. */
1091 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1092 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1094 if (TREE_CODE (decl) == FUNCTION_DECL)
1096 /* Merge decl state in both directions, we may still end up using
1097 the other decl. */
1098 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1099 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1101 lto_symtab_prevail_decl (n->decl, decl);
1102 decl = n->decl;
1104 else
1105 symtab_node::get_create (decl);
1107 return decl;