PR c/79855: add full stop to store merging param descriptions
[official-gcc.git] / gcc / lto / lto-symtab.c
blobf61b1f801337f86142fef7d3ac4f91fd119dbfe3
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/%i by %s/%i"
49 " for symbol %s\n",
50 node->name (), node->order,
51 prevailing_node->name (),
52 prevailing_node->order,
53 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
54 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
57 /* Merge node flags. */
58 if (node->force_output)
59 prevailing_node->mark_force_output ();
60 if (node->forced_by_abi)
61 prevailing_node->forced_by_abi = true;
62 if (node->address_taken)
64 gcc_assert (!prevailing_node->global.inlined_to);
65 prevailing_node->mark_address_taken ();
67 if (node->definition && prevailing_node->definition
68 && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
69 prevailing_node->merged_comdat = true;
71 /* Redirect all incoming edges. */
72 compatible_p
73 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
74 TREE_TYPE (TREE_TYPE (node->decl)));
75 for (e = node->callers; e; e = next)
77 next = e->next_caller;
78 e->redirect_callee (prevailing_node);
79 /* If there is a mismatch between the supposed callee return type and
80 the real one do not attempt to inline this function.
81 ??? We really need a way to match function signatures for ABI
82 compatibility and perform related promotions at inlining time. */
83 if (!compatible_p)
85 e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
86 e->call_stmt_cannot_inline_p = 1;
89 /* Redirect incomming references. */
90 prevailing_node->clone_referring (node);
92 /* Fix instrumentation references. */
93 if (node->instrumented_version)
95 gcc_assert (node->instrumentation_clone
96 == prevailing_node->instrumentation_clone);
97 node->instrumented_version->instrumented_version = prevailing_node;
98 if (!prevailing_node->instrumented_version)
99 prevailing_node->instrumented_version = node->instrumented_version;
100 /* Need to reset node->instrumented_version to NULL,
101 otherwise node removal code would reset
102 node->instrumented_version->instrumented_version. */
103 node->instrumented_version = NULL;
106 lto_free_function_in_decl_state_for_node (node);
108 if (node->decl != prevailing_node->decl)
109 node->release_body ();
111 /* Finally remove the replaced node. */
112 node->remove ();
115 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
116 all edges and removing the old node. */
118 static void
119 lto_varpool_replace_node (varpool_node *vnode,
120 varpool_node *prevailing_node)
122 gcc_assert (!vnode->definition || prevailing_node->definition);
123 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
125 prevailing_node->clone_referring (vnode);
126 if (vnode->force_output)
127 prevailing_node->force_output = true;
128 if (vnode->forced_by_abi)
129 prevailing_node->forced_by_abi = true;
131 /* Be sure we can garbage collect the initializer. */
132 if (DECL_INITIAL (vnode->decl)
133 && vnode->decl != prevailing_node->decl)
134 DECL_INITIAL (vnode->decl) = error_mark_node;
136 /* Check and report ODR violations on virtual tables. */
137 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
138 compare_virtual_tables (prevailing_node, vnode);
140 if (vnode->tls_model != prevailing_node->tls_model)
142 bool error = false;
144 /* Non-TLS and TLS never mix together. Also emulated model is not
145 compatible with anything else. */
146 if (prevailing_node->tls_model == TLS_MODEL_NONE
147 || prevailing_node->tls_model == TLS_MODEL_EMULATED
148 || vnode->tls_model == TLS_MODEL_NONE
149 || vnode->tls_model == TLS_MODEL_EMULATED)
150 error = true;
151 /* Linked is silently supporting transitions
152 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
153 Do the same transitions and error out on others. */
154 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
155 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
156 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
157 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
158 prevailing_node->tls_model = vnode->tls_model;
159 else if ((vnode->tls_model == TLS_MODEL_REAL
160 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
161 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
162 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
164 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
165 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
166 prevailing_node->tls_model = vnode->tls_model;
167 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
168 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
170 else
171 error = true;
172 if (error)
174 error_at (DECL_SOURCE_LOCATION (vnode->decl),
175 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
176 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
177 "previously defined here as %s",
178 tls_model_names [prevailing_node->tls_model]);
181 /* Finally remove the replaced node. */
182 vnode->remove ();
185 /* Return non-zero if we want to output waring about T1 and T2.
186 Return value is a bitmask of reasons of violation:
187 Bit 0 indicates that types are not compatible.
188 Bit 1 indicates that types are not compatible because of C++ ODR rule.
189 If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
190 Bit 2 indicates that types are not ODR compatible
192 The interoperability rules are language specific. At present we do only
193 full checking for C++ ODR rule and for other languages we do basic check
194 that data structures are of same size and TBAA compatible. Our TBAA
195 implementation should be coarse enough so all valid type transitions
196 across different languages are allowed.
198 In partiucular we thus allow almost arbitrary type changes with
199 -fno-strict-aliasing which may be tough of as a feature rather than bug
200 as it allows to implement dodgy tricks in the language runtimes.
202 Naturally this code can be strenghtened significantly if we could track
203 down the language of origin. */
205 static int
206 warn_type_compatibility_p (tree prevailing_type, tree type,
207 bool common_or_extern)
209 int lev = 0;
210 bool odr_p = odr_or_derived_type_p (prevailing_type)
211 && odr_or_derived_type_p (type);
213 if (prevailing_type == type)
214 return 0;
216 /* C++ provide a robust way to check for type compatibility via the ODR
217 rule. */
218 if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
219 lev |= 2;
221 /* Function types needs special care, because types_compatible_p never
222 thinks prototype is compatible to non-prototype. */
223 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
225 if (TREE_CODE (type) != TREE_CODE (prevailing_type))
226 lev |= 1;
227 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
228 TREE_TYPE (type), false);
229 if (TREE_CODE (type) == METHOD_TYPE
230 && TREE_CODE (prevailing_type) == METHOD_TYPE)
231 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
232 TYPE_METHOD_BASETYPE (type), false);
233 if (prototype_p (prevailing_type) && prototype_p (type)
234 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
236 tree parm1, parm2;
237 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
238 parm2 = TYPE_ARG_TYPES (type);
239 parm1 && parm2;
240 parm1 = TREE_CHAIN (parm1),
241 parm2 = TREE_CHAIN (parm2))
242 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
243 TREE_VALUE (parm2), false);
244 if (parm1 || parm2)
245 lev |= odr_p ? 3 : 1;
247 if (comp_type_attributes (prevailing_type, type) == 0)
248 lev |= 1;
249 return lev;
252 /* Get complete type. */
253 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
254 type = TYPE_MAIN_VARIANT (type);
256 /* We can not use types_compatible_p because we permit some changes
257 across types. For example unsigned size_t and "signed size_t" may be
258 compatible when merging C and Fortran types. */
259 if (COMPLETE_TYPE_P (prevailing_type)
260 && COMPLETE_TYPE_P (type)
261 /* While global declarations are never variadic, we can recurse here
262 for function parameter types. */
263 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
264 && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
265 && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
267 /* As a special case do not warn about merging
268 int a[];
270 int a[]={1,2,3};
271 here the first declaration is COMMON or EXTERN
272 and sizeof(a) == sizeof (int). */
273 if (!common_or_extern
274 || TREE_CODE (type) != ARRAY_TYPE
275 || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
276 lev |= 1;
279 /* Verify TBAA compatibility. Take care of alias set 0 and the fact that
280 we make ptr_type_node to TBAA compatible with every other type. */
281 if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
283 alias_set_type set1 = get_alias_set (type);
284 alias_set_type set2 = get_alias_set (prevailing_type);
286 if (set1 && set2 && set1 != set2
287 && (!POINTER_TYPE_P (type) || !POINTER_TYPE_P (prevailing_type)
288 || (set1 != TYPE_ALIAS_SET (ptr_type_node)
289 && set2 != TYPE_ALIAS_SET (ptr_type_node))))
290 lev |= 5;
293 return lev;
296 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
297 Return false if the symbols are not fully compatible and a diagnostic
298 should be emitted. */
300 static bool
301 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
303 tree prevailing_decl = prevailing->decl;
304 tree decl = entry->decl;
306 if (prevailing_decl == decl)
307 return true;
309 if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
310 return false;
312 /* Merge decl state in both directions, we may still end up using
313 the new decl. */
314 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
315 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
317 /* The linker may ask us to combine two incompatible symbols.
318 Detect this case and notify the caller of required diagnostics. */
320 if (TREE_CODE (decl) == FUNCTION_DECL)
322 /* Merge decl state in both directions, we may still end up using
323 the new decl. */
324 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
325 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
327 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
328 TREE_TYPE (decl),
329 DECL_COMMON (decl)
330 || DECL_EXTERNAL (decl)))
331 return false;
333 return true;
336 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
337 TREE_TYPE (decl),
338 DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
339 return false;
341 /* There is no point in comparing too many details of the decls here.
342 The type compatibility checks or the completing of types has properly
343 dealt with most issues. */
345 /* The following should all not invoke fatal errors as in non-LTO
346 mode the linker wouldn't complain either. Just emit warnings. */
348 /* Report a warning if user-specified alignments do not match. */
349 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
350 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
351 return false;
353 if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
354 && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl))
355 /* As a special case do not warn about merging
356 int a[];
358 int a[]={1,2,3};
359 here the first declaration is COMMON
360 and sizeof(a) == sizeof (int). */
361 && ((!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
362 || TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE
363 || TYPE_SIZE (TREE_TYPE (decl))
364 != TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))))
365 return false;
367 return true;
370 /* Return true if the symtab entry E can be replaced by another symtab
371 entry. */
373 static bool
374 lto_symtab_resolve_replaceable_p (symtab_node *e)
376 if (DECL_EXTERNAL (e->decl)
377 || DECL_COMDAT (e->decl)
378 || DECL_ONE_ONLY (e->decl)
379 || DECL_WEAK (e->decl))
380 return true;
382 if (TREE_CODE (e->decl) == VAR_DECL)
383 return (DECL_COMMON (e->decl)
384 || (!flag_no_common && !DECL_INITIAL (e->decl)));
386 return false;
389 /* Return true, if the symbol E should be resolved by lto-symtab.
390 Those are all external symbols and all real symbols that are not static (we
391 handle renaming of static later in partitioning). */
393 static bool
394 lto_symtab_symbol_p (symtab_node *e)
396 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
397 return false;
398 return e->real_symbol_p ();
401 /* Return true if the symtab entry E can be the prevailing one. */
403 static bool
404 lto_symtab_resolve_can_prevail_p (symtab_node *e)
406 if (!lto_symtab_symbol_p (e))
407 return false;
409 /* The C++ frontend ends up neither setting TREE_STATIC nor
410 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
411 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
412 if (DECL_EXTERNAL (e->decl))
413 return false;
415 return e->definition;
418 /* Resolve the symbol with the candidates in the chain *SLOT and store
419 their resolutions. */
421 static symtab_node *
422 lto_symtab_resolve_symbols (symtab_node *first)
424 symtab_node *e;
425 symtab_node *prevailing = NULL;
427 /* Always set e->node so that edges are updated to reflect decl merging. */
428 for (e = first; e; e = e->next_sharing_asm_name)
429 if (lto_symtab_symbol_p (e)
430 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
431 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
432 || e->resolution == LDPR_PREVAILING_DEF))
434 prevailing = e;
435 break;
438 /* If the chain is already resolved there is nothing else to do. */
439 if (prevailing)
441 /* Assert it's the only one. */
442 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
443 if (lto_symtab_symbol_p (e)
444 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
445 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
446 || e->resolution == LDPR_PREVAILING_DEF))
447 fatal_error (input_location, "multiple prevailing defs for %qE",
448 DECL_NAME (prevailing->decl));
449 return prevailing;
452 /* Find the single non-replaceable prevailing symbol and
453 diagnose ODR violations. */
454 for (e = first; e; e = e->next_sharing_asm_name)
456 if (!lto_symtab_resolve_can_prevail_p (e))
457 continue;
459 /* If we have a non-replaceable definition it prevails. */
460 if (!lto_symtab_resolve_replaceable_p (e))
462 if (prevailing)
464 error_at (DECL_SOURCE_LOCATION (e->decl),
465 "%qD has already been defined", e->decl);
466 inform (DECL_SOURCE_LOCATION (prevailing->decl),
467 "previously defined here");
469 prevailing = e;
472 if (prevailing)
473 return prevailing;
475 /* Do a second round choosing one from the replaceable prevailing decls. */
476 for (e = first; e; e = e->next_sharing_asm_name)
478 if (!lto_symtab_resolve_can_prevail_p (e))
479 continue;
481 /* Choose the first function that can prevail as prevailing. */
482 if (TREE_CODE (e->decl) == FUNCTION_DECL)
484 prevailing = e;
485 break;
488 /* From variables that can prevail choose the largest one. */
489 if (!prevailing
490 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
491 DECL_SIZE (e->decl))
492 /* When variables are equivalent try to chose one that has useful
493 DECL_INITIAL. This makes sense for keyed vtables that are
494 DECL_EXTERNAL but initialized. In units that do not need them
495 we replace the initializer by error_mark_node to conserve
496 memory.
498 We know that the vtable is keyed outside the LTO unit - otherwise
499 the keyed instance would prevail. We still can preserve useful
500 info in the initializer. */
501 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
502 && (DECL_INITIAL (e->decl)
503 && DECL_INITIAL (e->decl) != error_mark_node)
504 && (!DECL_INITIAL (prevailing->decl)
505 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
506 prevailing = e;
509 return prevailing;
512 /* Decide if it is OK to merge DECL into PREVAILING.
513 Because we wrap most of uses of declarations in MEM_REF, we can tolerate
514 some differences but other code may inspect directly the DECL. */
516 static bool
517 lto_symtab_merge_p (tree prevailing, tree decl)
519 if (TREE_CODE (prevailing) != TREE_CODE (decl))
521 if (symtab->dump_file)
522 fprintf (symtab->dump_file, "Not merging decls; "
523 "TREE_CODE mismatch\n");
524 return false;
526 gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
528 if (TREE_CODE (prevailing) == FUNCTION_DECL)
530 if (DECL_BUILT_IN (prevailing) != DECL_BUILT_IN (decl))
532 if (symtab->dump_file)
533 fprintf (symtab->dump_file, "Not merging decls; "
534 "DECL_BUILT_IN mismatch\n");
535 return false;
537 if (DECL_BUILT_IN (prevailing)
538 && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
539 || DECL_FUNCTION_CODE (prevailing) != DECL_FUNCTION_CODE (decl)))
541 if (symtab->dump_file)
542 fprintf (symtab->dump_file, "Not merging decls; "
543 "DECL_BUILT_IN_CLASS or CODE mismatch\n");
544 return false;
547 if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
549 tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
550 tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
551 if ((prev_attr == NULL) != (attr == NULL)
552 || (prev_attr
553 && TREE_VALUE (TREE_VALUE (prev_attr))
554 != TREE_VALUE (TREE_VALUE (attr))))
556 if (symtab->dump_file)
557 fprintf (symtab->dump_file, "Not merging decls; "
558 "error attribute mismatch\n");
559 return false;
562 prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
563 attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
564 if ((prev_attr == NULL) != (attr == NULL)
565 || (prev_attr
566 && TREE_VALUE (TREE_VALUE (prev_attr))
567 != TREE_VALUE (TREE_VALUE (attr))))
569 if (symtab->dump_file)
570 fprintf (symtab->dump_file, "Not merging decls; "
571 "warning attribute mismatch\n");
572 return false;
575 return true;
578 /* Merge all decls in the symbol table chain to the prevailing decl and
579 issue diagnostics about type mismatches. If DIAGNOSED_P is true
580 do not issue further diagnostics.*/
582 static void
583 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
585 symtab_node *prevailing;
586 symtab_node *e;
587 vec<tree> mismatches = vNULL;
588 unsigned i;
589 tree decl;
590 bool tbaa_p = false;
592 /* Nothing to do for a single entry. */
593 prevailing = first;
594 if (!prevailing->next_sharing_asm_name)
595 return;
597 /* Try to merge each entry with the prevailing one. */
598 symtab_node *last_prevailing = prevailing, *next;
599 for (e = prevailing->next_sharing_asm_name; e; e = next)
601 next = e->next_sharing_asm_name;
603 /* Skip non-LTO symbols and symbols whose declaration we already
604 visited. */
605 if (lto_symtab_prevailing_decl (e->decl) != e->decl
606 || !lto_symtab_symbol_p (e)
607 || e->decl == prevailing->decl)
608 continue;
610 if (!lto_symtab_merge (prevailing, e)
611 && !diagnosed_p
612 && !DECL_ARTIFICIAL (e->decl))
613 mismatches.safe_push (e->decl);
615 symtab_node *this_prevailing;
616 for (this_prevailing = prevailing; ;
617 this_prevailing = this_prevailing->next_sharing_asm_name)
619 if (this_prevailing->decl != e->decl
620 && lto_symtab_merge_p (this_prevailing->decl, e->decl))
621 break;
622 if (this_prevailing == last_prevailing)
624 this_prevailing = NULL;
625 break;
629 if (this_prevailing)
630 lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
631 /* Maintain LRU list: relink the new prevaililng symbol
632 just after previaling node in the chain and update last_prevailing.
633 Since the number of possible declarations of a given symbol is
634 small, this should be faster than building a hash. */
635 else if (e == prevailing->next_sharing_asm_name)
636 last_prevailing = e;
637 else
639 if (e->next_sharing_asm_name)
640 e->next_sharing_asm_name->previous_sharing_asm_name
641 = e->previous_sharing_asm_name;
642 e->previous_sharing_asm_name->next_sharing_asm_name
643 = e->next_sharing_asm_name;
644 e->previous_sharing_asm_name = prevailing;
645 e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
646 prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
647 prevailing->next_sharing_asm_name = e;
648 if (last_prevailing == prevailing)
649 last_prevailing = e;
652 if (mismatches.is_empty ())
653 return;
655 /* Diagnose all mismatched re-declarations. */
656 FOR_EACH_VEC_ELT (mismatches, i, decl)
658 /* Do not diagnose two built-in declarations, there is no useful
659 location in that case. It also happens for AVR if two built-ins
660 use the same asm name because their libgcc assembler code is the
661 same, see PR78562. */
662 if (DECL_IS_BUILTIN (prevailing->decl)
663 && DECL_IS_BUILTIN (decl))
664 continue;
666 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
667 TREE_TYPE (decl),
668 DECL_COMDAT (decl));
669 if (level)
671 bool diag = false;
672 if (level & 2)
673 diag = warning_at (DECL_SOURCE_LOCATION (decl),
674 OPT_Wodr,
675 "%qD violates the C++ One Definition Rule ",
676 decl);
677 if (!diag && (level & 1))
678 diag = warning_at (DECL_SOURCE_LOCATION (decl),
679 OPT_Wlto_type_mismatch,
680 "type of %qD does not match original "
681 "declaration", decl);
682 if (diag)
684 warn_types_mismatch (TREE_TYPE (prevailing->decl),
685 TREE_TYPE (decl),
686 DECL_SOURCE_LOCATION (prevailing->decl),
687 DECL_SOURCE_LOCATION (decl));
688 if ((level & 4)
689 && !TREE_READONLY (prevailing->decl))
690 tbaa_p = true;
692 diagnosed_p |= diag;
694 else if ((DECL_USER_ALIGN (prevailing->decl)
695 && DECL_USER_ALIGN (decl))
696 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
698 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
699 OPT_Wlto_type_mismatch,
700 "alignment of %qD is bigger than "
701 "original declaration", decl);
703 else
704 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
705 OPT_Wlto_type_mismatch,
706 "size of %qD differ from the size of "
707 "original declaration", decl);
709 if (diagnosed_p)
710 inform (DECL_SOURCE_LOCATION (prevailing->decl),
711 "%qD was previously declared here", prevailing->decl);
712 if (tbaa_p)
713 inform (DECL_SOURCE_LOCATION (prevailing->decl),
714 "code may be misoptimized unless "
715 "-fno-strict-aliasing is used");
717 mismatches.release ();
720 /* Helper to process the decl chain for the symbol table entry *SLOT. */
722 static void
723 lto_symtab_merge_decls_1 (symtab_node *first)
725 symtab_node *e;
726 symtab_node *prevailing;
727 bool diagnosed_p = false;
729 if (symtab->dump_file)
731 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
732 first->asm_name ());
733 for (e = first; e; e = e->next_sharing_asm_name)
734 if (TREE_PUBLIC (e->decl))
735 e->dump (symtab->dump_file);
738 /* Compute the symbol resolutions. This is a no-op when using the
739 linker plugin and resolution was decided by the linker. */
740 prevailing = lto_symtab_resolve_symbols (first);
742 /* If there's not a prevailing symbol yet it's an external reference.
743 Happens a lot during ltrans. Choose the first symbol with a
744 cgraph or a varpool node. */
745 if (!prevailing)
747 for (prevailing = first;
748 prevailing; prevailing = prevailing->next_sharing_asm_name)
749 if (lto_symtab_symbol_p (prevailing))
750 break;
751 if (!prevailing)
752 return;
753 /* For variables chose with a priority variant with vnode
754 attached (i.e. from unit where external declaration of
755 variable is actually used).
756 When there are multiple variants, chose one with size.
757 This is needed for C++ typeinfos, for example in
758 lto/20081204-1 there are typeifos in both units, just
759 one of them do have size. */
760 if (TREE_CODE (prevailing->decl) == VAR_DECL)
762 for (e = prevailing->next_sharing_asm_name;
763 e; e = e->next_sharing_asm_name)
764 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
765 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
766 && lto_symtab_symbol_p (e))
767 prevailing = e;
769 /* For functions prefer the non-builtin if one is available. */
770 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
772 for (e = first; e; e = e->next_sharing_asm_name)
773 if (TREE_CODE (e->decl) == FUNCTION_DECL
774 && !DECL_BUILT_IN (e->decl)
775 && lto_symtab_symbol_p (e))
777 prevailing = e;
778 break;
783 symtab->symtab_prevail_in_asm_name_hash (prevailing);
785 /* Diagnose mismatched objects. */
786 for (e = prevailing->next_sharing_asm_name;
787 e; e = e->next_sharing_asm_name)
789 if (TREE_CODE (prevailing->decl)
790 == TREE_CODE (e->decl))
791 continue;
792 if (!lto_symtab_symbol_p (e))
793 continue;
795 switch (TREE_CODE (prevailing->decl))
797 case VAR_DECL:
798 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
799 error_at (DECL_SOURCE_LOCATION (e->decl),
800 "variable %qD redeclared as function",
801 prevailing->decl);
802 break;
804 case FUNCTION_DECL:
805 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
806 error_at (DECL_SOURCE_LOCATION (e->decl),
807 "function %qD redeclared as variable",
808 prevailing->decl);
809 break;
811 default:
812 gcc_unreachable ();
815 diagnosed_p = true;
817 if (diagnosed_p)
818 inform (DECL_SOURCE_LOCATION (prevailing->decl),
819 "previously declared here");
821 /* Merge the chain to the single prevailing decl and diagnose
822 mismatches. */
823 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
825 if (symtab->dump_file)
827 fprintf (symtab->dump_file, "After resolution:\n");
828 for (e = prevailing; e; e = e->next_sharing_asm_name)
829 e->dump (symtab->dump_file);
833 /* Resolve and merge all symbol table chains to a prevailing decl. */
835 void
836 lto_symtab_merge_decls (void)
838 symtab_node *node;
840 /* Populate assembler name hash. */
841 symtab->symtab_initialize_asm_name_hash ();
843 FOR_EACH_SYMBOL (node)
844 if (!node->previous_sharing_asm_name
845 && node->next_sharing_asm_name)
846 lto_symtab_merge_decls_1 (node);
849 /* Helper to process the decl chain for the symbol table entry *SLOT. */
851 static void
852 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
854 symtab_node *e;
855 symtab_node *next;
857 prevailing->decl->decl_with_vis.symtab_node = prevailing;
859 /* Replace the cgraph node of each entry with the prevailing one. */
860 for (e = prevailing->next_sharing_asm_name; e;
861 e = next)
863 next = e->next_sharing_asm_name;
865 if (!lto_symtab_symbol_p (e))
866 continue;
867 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
868 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
870 /* No matter how we are going to deal with resolution, we will ultimately
871 use prevailing definition. */
872 if (ce)
873 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
874 dyn_cast<cgraph_node *> (e));
876 /* If we decided to replace the node by TO, do it. */
877 if (e != to)
879 if (ce)
880 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
881 else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
882 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
884 /* Watch out for duplicated symbols for a given declaration. */
885 else if (!e->transparent_alias
886 || !e->definition || e->get_alias_target () != to)
888 /* We got a new declaration we do not want to merge. In this case
889 get rid of the existing definition and create a transparent
890 alias. */
891 if (ce)
893 lto_free_function_in_decl_state_for_node (ce);
894 if (!ce->weakref)
895 ce->release_body ();
896 ce->reset ();
897 symtab->call_cgraph_removal_hooks (ce);
899 else
901 DECL_INITIAL (e->decl) = error_mark_node;
902 if (e->lto_file_data)
904 lto_free_function_in_decl_state_for_node (e);
905 e->lto_file_data = NULL;
907 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
909 e->remove_all_references ();
910 e->analyzed = e->body_removed = false;
911 e->resolve_alias (prevailing, true);
912 gcc_assert (e != prevailing);
916 return;
919 /* Merge cgraph nodes according to the symbol merging done by
920 lto_symtab_merge_decls. */
922 void
923 lto_symtab_merge_symbols (void)
925 symtab_node *node;
927 if (!flag_ltrans)
929 symtab->symtab_initialize_asm_name_hash ();
931 /* Do the actual merging.
932 At this point we invalidate hash translating decls into symtab nodes
933 because after removing one of duplicate decls the hash is not correcly
934 updated to the ohter dupliate. */
935 FOR_EACH_SYMBOL (node)
936 if (lto_symtab_symbol_p (node)
937 && node->next_sharing_asm_name
938 && !node->previous_sharing_asm_name)
939 lto_symtab_merge_symbols_1 (node);
941 /* Resolve weakref aliases whose target are now in the compilation unit.
942 also re-populate the hash translating decls into symtab nodes*/
943 FOR_EACH_SYMBOL (node)
945 cgraph_node *cnode, *cnode2;
946 varpool_node *vnode;
947 symtab_node *node2;
949 if (!node->analyzed && node->alias_target)
951 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
952 gcc_assert (node->weakref);
953 if (tgt)
954 node->resolve_alias (tgt, true);
957 if (!(cnode = dyn_cast <cgraph_node *> (node))
958 || !cnode->clone_of
959 || cnode->clone_of->decl != cnode->decl)
961 /* Builtins are not merged via decl merging. It is however
962 possible that tree merging unified the declaration. We
963 do not want duplicate entries in symbol table. */
964 if (cnode && DECL_BUILT_IN (node->decl)
965 && (cnode2 = cgraph_node::get (node->decl))
966 && cnode2 != cnode)
967 lto_cgraph_replace_node (cnode2, cnode);
969 /* The user defined assembler variables are also not unified by their
970 symbol name (since it is irrelevant), but we need to unify symbol
971 nodes if tree merging occurred. */
972 if ((vnode = dyn_cast <varpool_node *> (node))
973 && DECL_HARD_REGISTER (vnode->decl)
974 && (node2 = symtab_node::get (vnode->decl))
975 && node2 != node)
976 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
977 vnode);
980 /* Abstract functions may have duplicated cgraph nodes attached;
981 remove them. */
982 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
983 && (cnode2 = cgraph_node::get (node->decl))
984 && cnode2 != cnode)
985 cnode2->remove ();
987 node->decl->decl_with_vis.symtab_node = node;
993 /* Virtual tables may matter for code generation even if they are not
994 directly refernced by the code because they may be used for devirtualizaiton.
995 For this reason it is important to merge even virtual tables that have no
996 associated symbol table entries. Without doing so we lose optimization
997 oppurtunities by losing track of the vtable constructor.
998 FIXME: we probably ought to introduce explicit symbol table entries for
999 those before streaming. */
1001 tree
1002 lto_symtab_prevailing_virtual_decl (tree decl)
1004 if (DECL_ABSTRACT_P (decl))
1005 return decl;
1006 gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
1007 && DECL_ASSEMBLER_NAME_SET_P (decl));
1009 symtab_node *n = symtab_node::get_for_asmname
1010 (DECL_ASSEMBLER_NAME (decl));
1011 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1012 || !DECL_VIRTUAL_P (n->decl)))
1013 n = n->next_sharing_asm_name;
1014 if (n)
1016 /* Merge decl state in both directions, we may still end up using
1017 the other decl. */
1018 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1019 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1021 if (TREE_CODE (decl) == FUNCTION_DECL)
1023 /* Merge decl state in both directions, we may still end up using
1024 the other decl. */
1025 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1026 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1028 lto_symtab_prevail_decl (n->decl, decl);
1029 decl = n->decl;
1031 else
1032 symtab_node::get_create (decl);
1034 return decl;