PR rtl-optimization/88470
[official-gcc.git] / gcc / lto / lto-symtab.c
blobd018a16bd42985fa745de245352d715e0c5e8329
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.h"
35 #include "lto-symtab.h"
36 #include "stringpool.h"
37 #include "attribs.h"
39 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
40 all edges and removing the old node. */
42 static void
43 lto_cgraph_replace_node (struct cgraph_node *node,
44 struct cgraph_node *prevailing_node)
46 struct cgraph_edge *e, *next;
47 bool compatible_p;
49 if (dump_file)
51 fprintf (dump_file, "Replacing cgraph node %s by %s"
52 " for symbol %s\n",
53 node->dump_name (),
54 prevailing_node->dump_name (),
55 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
56 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
59 /* Merge node flags. */
60 if (node->force_output)
61 prevailing_node->mark_force_output ();
62 if (node->forced_by_abi)
63 prevailing_node->forced_by_abi = true;
64 if (node->address_taken)
66 gcc_assert (!prevailing_node->global.inlined_to);
67 prevailing_node->mark_address_taken ();
69 if (node->definition && prevailing_node->definition
70 && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
71 prevailing_node->merged_comdat = true;
73 /* Redirect all incoming edges. */
74 compatible_p
75 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
76 TREE_TYPE (TREE_TYPE (node->decl)));
77 for (e = node->callers; e; e = next)
79 next = e->next_caller;
80 e->redirect_callee (prevailing_node);
81 /* If there is a mismatch between the supposed callee return type and
82 the real one do not attempt to inline this function.
83 ??? We really need a way to match function signatures for ABI
84 compatibility and perform related promotions at inlining time. */
85 if (!compatible_p)
87 e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
88 e->call_stmt_cannot_inline_p = 1;
91 /* Redirect incomming references. */
92 prevailing_node->clone_referring (node);
93 lto_free_function_in_decl_state_for_node (node);
95 if (node->decl != prevailing_node->decl)
96 node->release_body ();
98 /* Finally remove the replaced node. */
99 node->remove ();
102 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
103 all edges and removing the old node. */
105 static void
106 lto_varpool_replace_node (varpool_node *vnode,
107 varpool_node *prevailing_node)
109 gcc_assert (!vnode->definition || prevailing_node->definition);
110 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
112 prevailing_node->clone_referring (vnode);
113 if (vnode->force_output)
114 prevailing_node->force_output = true;
115 if (vnode->forced_by_abi)
116 prevailing_node->forced_by_abi = true;
118 /* Be sure we can garbage collect the initializer. */
119 if (DECL_INITIAL (vnode->decl)
120 && vnode->decl != prevailing_node->decl)
121 DECL_INITIAL (vnode->decl) = error_mark_node;
123 /* Check and report ODR violations on virtual tables. */
124 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
125 compare_virtual_tables (prevailing_node, vnode);
127 if (vnode->tls_model != prevailing_node->tls_model)
129 bool error = false;
131 /* Non-TLS and TLS never mix together. Also emulated model is not
132 compatible with anything else. */
133 if (prevailing_node->tls_model == TLS_MODEL_NONE
134 || prevailing_node->tls_model == TLS_MODEL_EMULATED
135 || vnode->tls_model == TLS_MODEL_NONE
136 || vnode->tls_model == TLS_MODEL_EMULATED)
137 error = true;
138 /* Linked is silently supporting transitions
139 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
140 Do the same transitions and error out on others. */
141 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
142 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
143 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
144 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
145 prevailing_node->tls_model = vnode->tls_model;
146 else if ((vnode->tls_model == TLS_MODEL_REAL
147 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
148 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
149 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
151 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
152 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
153 prevailing_node->tls_model = vnode->tls_model;
154 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
155 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
157 else
158 error = true;
159 if (error)
161 error_at (DECL_SOURCE_LOCATION (vnode->decl),
162 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
163 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
164 "previously defined here as %s",
165 tls_model_names [prevailing_node->tls_model]);
168 /* Finally remove the replaced node. */
169 vnode->remove ();
172 /* Return non-zero if we want to output waring about T1 and T2.
173 Return value is a bitmask of reasons of violation:
174 Bit 0 indicates that types are not compatible.
175 Bit 1 indicates that types are not compatible because of C++ ODR rule.
176 If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
177 Bit 2 indicates that types are not ODR compatible
179 The interoperability rules are language specific. At present we do only
180 full checking for C++ ODR rule and for other languages we do basic check
181 that data structures are of same size and TBAA compatible. Our TBAA
182 implementation should be coarse enough so all valid type transitions
183 across different languages are allowed.
185 In partiucular we thus allow almost arbitrary type changes with
186 -fno-strict-aliasing which may be tough of as a feature rather than bug
187 as it allows to implement dodgy tricks in the language runtimes.
189 Naturally this code can be strenghtened significantly if we could track
190 down the language of origin. */
192 static int
193 warn_type_compatibility_p (tree prevailing_type, tree type,
194 bool common_or_extern)
196 int lev = 0;
197 bool odr_p = odr_or_derived_type_p (prevailing_type)
198 && odr_or_derived_type_p (type);
200 if (prevailing_type == type)
201 return 0;
203 /* C++ provide a robust way to check for type compatibility via the ODR
204 rule. */
205 if (odr_p && !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)
212 if (TREE_CODE (type) != TREE_CODE (prevailing_type))
213 lev |= 1;
214 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
215 TREE_TYPE (type), false);
216 if (TREE_CODE (type) == METHOD_TYPE
217 && TREE_CODE (prevailing_type) == METHOD_TYPE)
218 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
219 TYPE_METHOD_BASETYPE (type), false);
220 if (prototype_p (prevailing_type) && prototype_p (type)
221 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
223 tree parm1, parm2;
224 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
225 parm2 = TYPE_ARG_TYPES (type);
226 parm1 && parm2;
227 parm1 = TREE_CHAIN (parm1),
228 parm2 = TREE_CHAIN (parm2))
229 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
230 TREE_VALUE (parm2), false);
231 if (parm1 || parm2)
232 lev |= odr_p ? 3 : 1;
234 if (comp_type_attributes (prevailing_type, type) == 0)
235 lev |= 1;
236 return lev;
239 /* Get complete type. */
240 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
241 type = TYPE_MAIN_VARIANT (type);
243 /* We can not use types_compatible_p because we permit some changes
244 across types. For example unsigned size_t and "signed size_t" may be
245 compatible when merging C and Fortran types. */
246 if (COMPLETE_TYPE_P (prevailing_type)
247 && COMPLETE_TYPE_P (type)
248 /* While global declarations are never variadic, we can recurse here
249 for function parameter types. */
250 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
251 && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
252 && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
254 /* As a special case do not warn about merging
255 int a[];
257 int a[]={1,2,3};
258 here the first declaration is COMMON or EXTERN
259 and sizeof(a) == sizeof (int). */
260 if (!common_or_extern
261 || TREE_CODE (type) != ARRAY_TYPE
262 || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
263 lev |= 1;
266 /* Verify TBAA compatibility. Take care of alias set 0 and the fact that
267 we make ptr_type_node to TBAA compatible with every other type. */
268 if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
270 alias_set_type set1 = get_alias_set (type);
271 alias_set_type set2 = get_alias_set (prevailing_type);
273 if (set1 && set2 && set1 != set2)
275 tree t1 = type, t2 = prevailing_type;
277 /* Alias sets of arrays with aliased components are the same as alias
278 sets of the inner types. */
279 while (TREE_CODE (t1) == ARRAY_TYPE
280 && !TYPE_NONALIASED_COMPONENT (t1)
281 && TREE_CODE (t2) == ARRAY_TYPE
282 && !TYPE_NONALIASED_COMPONENT (t2))
284 t1 = TREE_TYPE (t1);
285 t2 = TREE_TYPE (t2);
287 if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2))
288 || (set1 != TYPE_ALIAS_SET (ptr_type_node)
289 && set2 != TYPE_ALIAS_SET (ptr_type_node)))
290 lev |= 5;
294 return lev;
297 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
298 Return false if the symbols are not fully compatible and a diagnostic
299 should be emitted. */
301 static bool
302 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
304 tree prevailing_decl = prevailing->decl;
305 tree decl = entry->decl;
307 if (prevailing_decl == decl)
308 return true;
310 if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
311 return false;
313 /* Merge decl state in both directions, we may still end up using
314 the new decl. */
315 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
316 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
318 /* The linker may ask us to combine two incompatible symbols.
319 Detect this case and notify the caller of required diagnostics. */
321 if (TREE_CODE (decl) == FUNCTION_DECL)
323 /* Merge decl state in both directions, we may still end up using
324 the new decl. */
325 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
326 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
328 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
329 TREE_TYPE (decl),
330 DECL_COMMON (decl)
331 || DECL_EXTERNAL (decl)))
332 return false;
334 return true;
337 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
338 TREE_TYPE (decl),
339 DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
340 return false;
342 /* There is no point in comparing too many details of the decls here.
343 The type compatibility checks or the completing of types has properly
344 dealt with most issues. */
346 /* The following should all not invoke fatal errors as in non-LTO
347 mode the linker wouldn't complain either. Just emit warnings. */
349 /* Report a warning if user-specified alignments do not match. */
350 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
351 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
352 return false;
354 if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
355 && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
357 if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
358 return false;
360 tree type = TREE_TYPE (decl);
362 /* For record type, check for array at the end of the structure. */
363 if (TREE_CODE (type) == RECORD_TYPE)
365 tree field = TYPE_FIELDS (type);
366 while (DECL_CHAIN (field) != NULL_TREE)
367 field = DECL_CHAIN (field);
369 return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE;
371 /* As a special case do not warn about merging
372 int a[];
374 int a[]={1,2,3};
375 here the first declaration is COMMON
376 and sizeof(a) == sizeof (int). */
377 else if (TREE_CODE (type) != ARRAY_TYPE
378 || (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type))))
379 return false;
382 return true;
385 /* Return true if the symtab entry E can be replaced by another symtab
386 entry. */
388 static bool
389 lto_symtab_resolve_replaceable_p (symtab_node *e)
391 if (DECL_EXTERNAL (e->decl)
392 || DECL_COMDAT (e->decl)
393 || DECL_ONE_ONLY (e->decl)
394 || DECL_WEAK (e->decl))
395 return true;
397 if (TREE_CODE (e->decl) == VAR_DECL)
398 return (DECL_COMMON (e->decl)
399 || (!flag_no_common && !DECL_INITIAL (e->decl)));
401 return false;
404 /* Return true, if the symbol E should be resolved by lto-symtab.
405 Those are all external symbols and all real symbols that are not static (we
406 handle renaming of static later in partitioning). */
408 static bool
409 lto_symtab_symbol_p (symtab_node *e)
411 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
412 return false;
413 return e->real_symbol_p ();
416 /* Return true if the symtab entry E can be the prevailing one. */
418 static bool
419 lto_symtab_resolve_can_prevail_p (symtab_node *e)
421 if (!lto_symtab_symbol_p (e))
422 return false;
424 /* The C++ frontend ends up neither setting TREE_STATIC nor
425 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
426 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
427 if (DECL_EXTERNAL (e->decl))
428 return false;
430 return e->definition;
433 /* Resolve the symbol with the candidates in the chain *SLOT and store
434 their resolutions. */
436 static symtab_node *
437 lto_symtab_resolve_symbols (symtab_node *first)
439 symtab_node *e;
440 symtab_node *prevailing = NULL;
442 /* Always set e->node so that edges are updated to reflect decl merging. */
443 for (e = first; e; e = e->next_sharing_asm_name)
444 if (lto_symtab_symbol_p (e)
445 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
446 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
447 || e->resolution == LDPR_PREVAILING_DEF))
449 prevailing = e;
450 break;
453 /* If the chain is already resolved there is nothing else to do. */
454 if (prevailing)
456 /* Assert it's the only one.
457 GCC should silence multiple PREVAILING_DEF_IRONLY defs error
458 on COMMON symbols since it isn't error.
459 See: https://sourceware.org/bugzilla/show_bug.cgi?id=23079. */
460 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
461 if (lto_symtab_symbol_p (e)
462 && !DECL_COMMON (prevailing->decl)
463 && !DECL_COMMON (e->decl)
464 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
465 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
466 || e->resolution == LDPR_PREVAILING_DEF))
467 fatal_error (input_location, "multiple prevailing defs for %qE",
468 DECL_NAME (prevailing->decl));
469 return prevailing;
472 /* Find the single non-replaceable prevailing symbol and
473 diagnose ODR violations. */
474 for (e = first; e; e = e->next_sharing_asm_name)
476 if (!lto_symtab_resolve_can_prevail_p (e))
477 continue;
479 /* If we have a non-replaceable definition it prevails. */
480 if (!lto_symtab_resolve_replaceable_p (e))
482 if (prevailing)
484 error_at (DECL_SOURCE_LOCATION (e->decl),
485 "%qD has already been defined", e->decl);
486 inform (DECL_SOURCE_LOCATION (prevailing->decl),
487 "previously defined here");
489 prevailing = e;
492 if (prevailing)
493 return prevailing;
495 /* Do a second round choosing one from the replaceable prevailing decls. */
496 for (e = first; e; e = e->next_sharing_asm_name)
498 if (!lto_symtab_resolve_can_prevail_p (e))
499 continue;
501 /* Choose the first function that can prevail as prevailing. */
502 if (TREE_CODE (e->decl) == FUNCTION_DECL)
504 prevailing = e;
505 break;
508 /* From variables that can prevail choose the largest one. */
509 if (!prevailing
510 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
511 DECL_SIZE (e->decl))
512 /* When variables are equivalent try to chose one that has useful
513 DECL_INITIAL. This makes sense for keyed vtables that are
514 DECL_EXTERNAL but initialized. In units that do not need them
515 we replace the initializer by error_mark_node to conserve
516 memory.
518 We know that the vtable is keyed outside the LTO unit - otherwise
519 the keyed instance would prevail. We still can preserve useful
520 info in the initializer. */
521 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
522 && (DECL_INITIAL (e->decl)
523 && DECL_INITIAL (e->decl) != error_mark_node)
524 && (!DECL_INITIAL (prevailing->decl)
525 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
526 prevailing = e;
529 return prevailing;
532 /* Decide if it is OK to merge DECL into PREVAILING.
533 Because we wrap most of uses of declarations in MEM_REF, we can tolerate
534 some differences but other code may inspect directly the DECL. */
536 static bool
537 lto_symtab_merge_p (tree prevailing, tree decl)
539 if (TREE_CODE (prevailing) != TREE_CODE (decl))
541 if (dump_file)
542 fprintf (dump_file, "Not merging decls; "
543 "TREE_CODE mismatch\n");
544 return false;
546 gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
548 if (TREE_CODE (prevailing) == FUNCTION_DECL)
550 if (fndecl_built_in_p (prevailing) != fndecl_built_in_p (decl))
552 if (dump_file)
553 fprintf (dump_file, "Not merging decls; "
554 "DECL_BUILT_IN mismatch\n");
555 return false;
557 if (fndecl_built_in_p (prevailing)
558 && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
559 || DECL_FUNCTION_CODE (prevailing) != DECL_FUNCTION_CODE (decl)))
561 if (dump_file)
562 fprintf (dump_file, "Not merging decls; "
563 "DECL_BUILT_IN_CLASS or CODE mismatch\n");
564 return false;
568 if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
570 tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
571 tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
572 if ((prev_attr == NULL) != (attr == NULL)
573 || (prev_attr && !attribute_value_equal (prev_attr, attr)))
575 if (dump_file)
576 fprintf (dump_file, "Not merging decls; "
577 "error attribute mismatch\n");
578 return false;
581 prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
582 attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
583 if ((prev_attr == NULL) != (attr == NULL)
584 || (prev_attr && !attribute_value_equal (prev_attr, attr)))
586 if (dump_file)
587 fprintf (dump_file, "Not merging decls; "
588 "warning attribute mismatch\n");
589 return false;
592 prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing));
593 attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl));
594 if ((prev_attr == NULL) != (attr == NULL))
596 if (dump_file)
597 fprintf (dump_file, "Not merging decls; "
598 "noreturn attribute mismatch\n");
599 return false;
602 return true;
605 /* Merge all decls in the symbol table chain to the prevailing decl and
606 issue diagnostics about type mismatches. If DIAGNOSED_P is true
607 do not issue further diagnostics.*/
609 static void
610 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
612 symtab_node *prevailing;
613 symtab_node *e;
614 vec<tree> mismatches = vNULL;
615 unsigned i;
616 tree decl;
617 bool tbaa_p = false;
619 /* Nothing to do for a single entry. */
620 prevailing = first;
621 if (!prevailing->next_sharing_asm_name)
622 return;
624 /* Try to merge each entry with the prevailing one. */
625 symtab_node *last_prevailing = prevailing, *next;
626 for (e = prevailing->next_sharing_asm_name; e; e = next)
628 next = e->next_sharing_asm_name;
630 /* Skip non-LTO symbols and symbols whose declaration we already
631 visited. */
632 if (lto_symtab_prevailing_decl (e->decl) != e->decl
633 || !lto_symtab_symbol_p (e)
634 || e->decl == prevailing->decl)
635 continue;
637 if (!lto_symtab_merge (prevailing, e)
638 && !diagnosed_p
639 && !DECL_ARTIFICIAL (e->decl))
640 mismatches.safe_push (e->decl);
642 symtab_node *this_prevailing;
643 for (this_prevailing = prevailing; ;
644 this_prevailing = this_prevailing->next_sharing_asm_name)
646 if (this_prevailing->decl != e->decl
647 && lto_symtab_merge_p (this_prevailing->decl, e->decl))
648 break;
649 if (this_prevailing == last_prevailing)
651 this_prevailing = NULL;
652 break;
656 if (this_prevailing)
657 lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
658 /* Maintain LRU list: relink the new prevaililng symbol
659 just after previaling node in the chain and update last_prevailing.
660 Since the number of possible declarations of a given symbol is
661 small, this should be faster than building a hash. */
662 else if (e == prevailing->next_sharing_asm_name)
663 last_prevailing = e;
664 else
666 if (e->next_sharing_asm_name)
667 e->next_sharing_asm_name->previous_sharing_asm_name
668 = e->previous_sharing_asm_name;
669 e->previous_sharing_asm_name->next_sharing_asm_name
670 = e->next_sharing_asm_name;
671 e->previous_sharing_asm_name = prevailing;
672 e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
673 prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
674 prevailing->next_sharing_asm_name = e;
675 if (last_prevailing == prevailing)
676 last_prevailing = e;
679 if (mismatches.is_empty ())
680 return;
682 /* Diagnose all mismatched re-declarations. */
683 FOR_EACH_VEC_ELT (mismatches, i, decl)
685 /* Do not diagnose two built-in declarations, there is no useful
686 location in that case. It also happens for AVR if two built-ins
687 use the same asm name because their libgcc assembler code is the
688 same, see PR78562. */
689 if (DECL_IS_BUILTIN (prevailing->decl)
690 && DECL_IS_BUILTIN (decl))
691 continue;
693 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
694 TREE_TYPE (decl),
695 DECL_COMDAT (decl));
696 if (level)
698 bool diag = false;
699 if (level & 2)
700 diag = warning_at (DECL_SOURCE_LOCATION (decl),
701 OPT_Wodr,
702 "%qD violates the C++ One Definition Rule",
703 decl);
704 if (!diag && (level & 1))
705 diag = warning_at (DECL_SOURCE_LOCATION (decl),
706 OPT_Wlto_type_mismatch,
707 "type of %qD does not match original "
708 "declaration", decl);
709 if (diag)
711 warn_types_mismatch (TREE_TYPE (prevailing->decl),
712 TREE_TYPE (decl),
713 DECL_SOURCE_LOCATION (prevailing->decl),
714 DECL_SOURCE_LOCATION (decl));
715 if ((level & 4)
716 && !TREE_READONLY (prevailing->decl))
717 tbaa_p = true;
719 diagnosed_p |= diag;
721 else if ((DECL_USER_ALIGN (prevailing->decl)
722 && DECL_USER_ALIGN (decl))
723 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
725 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
726 OPT_Wlto_type_mismatch,
727 "alignment of %qD is bigger than "
728 "original declaration", decl);
730 else
731 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
732 OPT_Wlto_type_mismatch,
733 "size of %qD differ from the size of "
734 "original declaration", decl);
736 if (diagnosed_p)
737 inform (DECL_SOURCE_LOCATION (prevailing->decl),
738 "%qD was previously declared here", prevailing->decl);
739 if (tbaa_p)
740 inform (DECL_SOURCE_LOCATION (prevailing->decl),
741 "code may be misoptimized unless "
742 "-fno-strict-aliasing is used");
744 mismatches.release ();
747 /* Helper to process the decl chain for the symbol table entry *SLOT. */
749 static void
750 lto_symtab_merge_decls_1 (symtab_node *first)
752 symtab_node *e;
753 symtab_node *prevailing;
754 bool diagnosed_p = false;
756 if (dump_file)
758 fprintf (dump_file, "Merging nodes for %s. Candidates:\n",
759 first->asm_name ());
760 for (e = first; e; e = e->next_sharing_asm_name)
761 if (TREE_PUBLIC (e->decl))
762 e->dump (dump_file);
765 /* Compute the symbol resolutions. This is a no-op when using the
766 linker plugin and resolution was decided by the linker. */
767 prevailing = lto_symtab_resolve_symbols (first);
769 /* If there's not a prevailing symbol yet it's an external reference.
770 Happens a lot during ltrans. Choose the first symbol with a
771 cgraph or a varpool node. */
772 if (!prevailing)
774 for (prevailing = first;
775 prevailing; prevailing = prevailing->next_sharing_asm_name)
776 if (lto_symtab_symbol_p (prevailing))
777 break;
778 if (!prevailing)
779 return;
780 /* For variables chose with a priority variant with vnode
781 attached (i.e. from unit where external declaration of
782 variable is actually used).
783 When there are multiple variants, chose one with size.
784 This is needed for C++ typeinfos, for example in
785 lto/20081204-1 there are typeifos in both units, just
786 one of them do have size. */
787 if (TREE_CODE (prevailing->decl) == VAR_DECL)
789 for (e = prevailing->next_sharing_asm_name;
790 e; e = e->next_sharing_asm_name)
791 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
792 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
793 && lto_symtab_symbol_p (e))
794 prevailing = e;
796 /* For functions prefer the non-builtin if one is available. */
797 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
799 for (e = first; e; e = e->next_sharing_asm_name)
800 if (TREE_CODE (e->decl) == FUNCTION_DECL
801 && !fndecl_built_in_p (e->decl)
802 && lto_symtab_symbol_p (e))
804 prevailing = e;
805 break;
810 symtab->symtab_prevail_in_asm_name_hash (prevailing);
812 /* Diagnose mismatched objects. */
813 for (e = prevailing->next_sharing_asm_name;
814 e; e = e->next_sharing_asm_name)
816 if (TREE_CODE (prevailing->decl)
817 == TREE_CODE (e->decl))
818 continue;
819 if (!lto_symtab_symbol_p (e))
820 continue;
822 switch (TREE_CODE (prevailing->decl))
824 case VAR_DECL:
825 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
826 error_at (DECL_SOURCE_LOCATION (e->decl),
827 "variable %qD redeclared as function",
828 prevailing->decl);
829 break;
831 case FUNCTION_DECL:
832 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
833 error_at (DECL_SOURCE_LOCATION (e->decl),
834 "function %qD redeclared as variable",
835 prevailing->decl);
836 break;
838 default:
839 gcc_unreachable ();
842 diagnosed_p = true;
844 if (diagnosed_p)
845 inform (DECL_SOURCE_LOCATION (prevailing->decl),
846 "previously declared here");
848 /* Merge the chain to the single prevailing decl and diagnose
849 mismatches. */
850 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
852 if (dump_file)
854 fprintf (dump_file, "After resolution:\n");
855 for (e = prevailing; e; e = e->next_sharing_asm_name)
856 e->dump (dump_file);
860 /* Resolve and merge all symbol table chains to a prevailing decl. */
862 void
863 lto_symtab_merge_decls (void)
865 symtab_node *node;
867 gcc_assert (!dump_file);
868 dump_file = dump_begin (decl_merge_dump_id, NULL);
870 /* Populate assembler name hash. */
871 symtab->symtab_initialize_asm_name_hash ();
873 FOR_EACH_SYMBOL (node)
874 if (!node->previous_sharing_asm_name
875 && node->next_sharing_asm_name)
876 lto_symtab_merge_decls_1 (node);
878 if (dump_file)
879 dump_end (decl_merge_dump_id, dump_file);
880 dump_file = NULL;
883 /* Helper to process the decl chain for the symbol table entry *SLOT. */
885 static void
886 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
888 symtab_node *e;
889 symtab_node *next;
891 prevailing->decl->decl_with_vis.symtab_node = prevailing;
893 /* Replace the cgraph node of each entry with the prevailing one. */
894 for (e = prevailing->next_sharing_asm_name; e;
895 e = next)
897 next = e->next_sharing_asm_name;
898 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
900 if ((!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
901 || (ce != NULL && ce->global.inlined_to))
902 continue;
903 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
905 /* No matter how we are going to deal with resolution, we will ultimately
906 use prevailing definition. */
907 if (ce)
908 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
909 dyn_cast<cgraph_node *> (e));
911 /* If we decided to replace the node by TO, do it. */
912 if (e != to)
914 if (ce)
915 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
916 else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
917 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
919 /* Watch out for duplicated symbols for a given declaration. */
920 else if (!e->transparent_alias
921 || !e->definition || e->get_alias_target () != to)
923 /* We got a new declaration we do not want to merge. In this case
924 get rid of the existing definition and create a transparent
925 alias. */
926 if (ce)
928 lto_free_function_in_decl_state_for_node (ce);
929 if (!ce->weakref)
930 ce->release_body ();
931 ce->reset ();
932 symtab->call_cgraph_removal_hooks (ce);
934 else
936 DECL_INITIAL (e->decl) = error_mark_node;
937 if (e->lto_file_data)
939 lto_free_function_in_decl_state_for_node (e);
940 e->lto_file_data = NULL;
942 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
944 e->remove_all_references ();
945 e->analyzed = e->body_removed = false;
946 e->resolve_alias (prevailing, true);
947 gcc_assert (e != prevailing);
951 return;
954 /* Merge cgraph nodes according to the symbol merging done by
955 lto_symtab_merge_decls. */
957 void
958 lto_symtab_merge_symbols (void)
960 symtab_node *node;
962 if (!flag_ltrans)
964 symtab->symtab_initialize_asm_name_hash ();
966 /* Do the actual merging.
967 At this point we invalidate hash translating decls into symtab nodes
968 because after removing one of duplicate decls the hash is not correcly
969 updated to the ohter dupliate. */
970 FOR_EACH_SYMBOL (node)
971 if (lto_symtab_symbol_p (node)
972 && node->next_sharing_asm_name
973 && !node->previous_sharing_asm_name)
974 lto_symtab_merge_symbols_1 (node);
976 /* Resolve weakref aliases whose target are now in the compilation unit.
977 also re-populate the hash translating decls into symtab nodes*/
978 FOR_EACH_SYMBOL (node)
980 cgraph_node *cnode, *cnode2;
981 varpool_node *vnode;
982 symtab_node *node2;
984 if (!node->analyzed && node->alias_target)
986 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
987 gcc_assert (node->weakref);
988 if (tgt)
989 node->resolve_alias (tgt, true);
991 /* If the symbol was preempted outside IR, see if we want to get rid
992 of the definition. */
993 if (node->analyzed
994 && !DECL_EXTERNAL (node->decl)
995 && (node->resolution == LDPR_PREEMPTED_REG
996 || node->resolution == LDPR_RESOLVED_IR
997 || node->resolution == LDPR_RESOLVED_EXEC
998 || node->resolution == LDPR_RESOLVED_DYN))
1000 DECL_EXTERNAL (node->decl) = 1;
1001 /* If alias to local symbol was preempted by external definition,
1002 we know it is not pointing to the local symbol. Remove it. */
1003 if (node->alias
1004 && !node->weakref
1005 && !node->transparent_alias
1006 && node->get_alias_target ()->binds_to_current_def_p ())
1008 node->alias = false;
1009 node->remove_all_references ();
1010 node->definition = false;
1011 node->analyzed = false;
1012 node->cpp_implicit_alias = false;
1014 else if (!node->alias
1015 && node->definition
1016 && node->get_availability () <= AVAIL_INTERPOSABLE)
1018 if ((cnode = dyn_cast <cgraph_node *> (node)) != NULL)
1019 cnode->reset ();
1020 else
1022 node->analyzed = node->definition = false;
1023 node->remove_all_references ();
1028 if (!(cnode = dyn_cast <cgraph_node *> (node))
1029 || !cnode->clone_of
1030 || cnode->clone_of->decl != cnode->decl)
1032 /* Builtins are not merged via decl merging. It is however
1033 possible that tree merging unified the declaration. We
1034 do not want duplicate entries in symbol table. */
1035 if (cnode && fndecl_built_in_p (node->decl)
1036 && (cnode2 = cgraph_node::get (node->decl))
1037 && cnode2 != cnode)
1038 lto_cgraph_replace_node (cnode2, cnode);
1040 /* The user defined assembler variables are also not unified by their
1041 symbol name (since it is irrelevant), but we need to unify symbol
1042 nodes if tree merging occurred. */
1043 if ((vnode = dyn_cast <varpool_node *> (node))
1044 && DECL_HARD_REGISTER (vnode->decl)
1045 && (node2 = symtab_node::get (vnode->decl))
1046 && node2 != node)
1047 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
1048 vnode);
1051 /* Abstract functions may have duplicated cgraph nodes attached;
1052 remove them. */
1053 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
1054 && (cnode2 = cgraph_node::get (node->decl))
1055 && cnode2 != cnode)
1056 cnode2->remove ();
1058 node->decl->decl_with_vis.symtab_node = node;
1064 /* Virtual tables may matter for code generation even if they are not
1065 directly refernced by the code because they may be used for devirtualizaiton.
1066 For this reason it is important to merge even virtual tables that have no
1067 associated symbol table entries. Without doing so we lose optimization
1068 oppurtunities by losing track of the vtable constructor.
1069 FIXME: we probably ought to introduce explicit symbol table entries for
1070 those before streaming. */
1072 tree
1073 lto_symtab_prevailing_virtual_decl (tree decl)
1075 if (DECL_ABSTRACT_P (decl))
1076 return decl;
1077 gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
1078 && DECL_ASSEMBLER_NAME_SET_P (decl));
1080 symtab_node *n = symtab_node::get_for_asmname
1081 (DECL_ASSEMBLER_NAME (decl));
1082 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1083 || !DECL_VIRTUAL_P (n->decl)))
1084 n = n->next_sharing_asm_name;
1085 if (n)
1087 /* Merge decl state in both directions, we may still end up using
1088 the other decl. */
1089 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1090 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1092 if (TREE_CODE (decl) == FUNCTION_DECL)
1094 /* Merge decl state in both directions, we may still end up using
1095 the other decl. */
1096 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1097 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1099 lto_symtab_prevail_decl (n->decl, decl);
1100 decl = n->decl;
1102 else
1103 symtab_node::get_create (decl);
1105 return decl;