2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / lto / lto-symtab.c
blobce9e1465e3c186f9d55d815f9d0e8acbc5e53f7b
1 /* LTO symbol table.
2 Copyright (C) 2009-2016 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 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
659 TREE_TYPE (decl),
660 DECL_COMDAT (decl));
661 if (level)
663 bool diag = false;
664 if (level & 2)
665 diag = warning_at (DECL_SOURCE_LOCATION (decl),
666 OPT_Wodr,
667 "%qD violates the C++ One Definition Rule ",
668 decl);
669 if (!diag && (level & 1))
670 diag = warning_at (DECL_SOURCE_LOCATION (decl),
671 OPT_Wlto_type_mismatch,
672 "type of %qD does not match original "
673 "declaration", decl);
674 if (diag)
676 warn_types_mismatch (TREE_TYPE (prevailing->decl),
677 TREE_TYPE (decl),
678 DECL_SOURCE_LOCATION (prevailing->decl),
679 DECL_SOURCE_LOCATION (decl));
680 if ((level & 4)
681 && !TREE_READONLY (prevailing->decl))
682 tbaa_p = true;
684 diagnosed_p |= diag;
686 else if ((DECL_USER_ALIGN (prevailing->decl)
687 && DECL_USER_ALIGN (decl))
688 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
690 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
691 OPT_Wlto_type_mismatch,
692 "alignment of %qD is bigger than "
693 "original declaration", decl);
695 else
696 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
697 OPT_Wlto_type_mismatch,
698 "size of %qD differ from the size of "
699 "original declaration", decl);
701 if (diagnosed_p)
702 inform (DECL_SOURCE_LOCATION (prevailing->decl),
703 "%qD was previously declared here", prevailing->decl);
704 if (tbaa_p)
705 inform (DECL_SOURCE_LOCATION (prevailing->decl),
706 "code may be misoptimized unless "
707 "-fno-strict-aliasing is used");
709 mismatches.release ();
712 /* Helper to process the decl chain for the symbol table entry *SLOT. */
714 static void
715 lto_symtab_merge_decls_1 (symtab_node *first)
717 symtab_node *e;
718 symtab_node *prevailing;
719 bool diagnosed_p = false;
721 if (symtab->dump_file)
723 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
724 first->asm_name ());
725 for (e = first; e; e = e->next_sharing_asm_name)
726 if (TREE_PUBLIC (e->decl))
727 e->dump (symtab->dump_file);
730 /* Compute the symbol resolutions. This is a no-op when using the
731 linker plugin and resolution was decided by the linker. */
732 prevailing = lto_symtab_resolve_symbols (first);
734 /* If there's not a prevailing symbol yet it's an external reference.
735 Happens a lot during ltrans. Choose the first symbol with a
736 cgraph or a varpool node. */
737 if (!prevailing)
739 for (prevailing = first;
740 prevailing; prevailing = prevailing->next_sharing_asm_name)
741 if (lto_symtab_symbol_p (prevailing))
742 break;
743 if (!prevailing)
744 return;
745 /* For variables chose with a priority variant with vnode
746 attached (i.e. from unit where external declaration of
747 variable is actually used).
748 When there are multiple variants, chose one with size.
749 This is needed for C++ typeinfos, for example in
750 lto/20081204-1 there are typeifos in both units, just
751 one of them do have size. */
752 if (TREE_CODE (prevailing->decl) == VAR_DECL)
754 for (e = prevailing->next_sharing_asm_name;
755 e; e = e->next_sharing_asm_name)
756 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
757 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
758 && lto_symtab_symbol_p (e))
759 prevailing = e;
761 /* For functions prefer the non-builtin if one is available. */
762 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
764 for (e = first; e; e = e->next_sharing_asm_name)
765 if (TREE_CODE (e->decl) == FUNCTION_DECL
766 && !DECL_BUILT_IN (e->decl)
767 && lto_symtab_symbol_p (e))
769 prevailing = e;
770 break;
775 symtab->symtab_prevail_in_asm_name_hash (prevailing);
777 /* Diagnose mismatched objects. */
778 for (e = prevailing->next_sharing_asm_name;
779 e; e = e->next_sharing_asm_name)
781 if (TREE_CODE (prevailing->decl)
782 == TREE_CODE (e->decl))
783 continue;
784 if (!lto_symtab_symbol_p (e))
785 continue;
787 switch (TREE_CODE (prevailing->decl))
789 case VAR_DECL:
790 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
791 error_at (DECL_SOURCE_LOCATION (e->decl),
792 "variable %qD redeclared as function",
793 prevailing->decl);
794 break;
796 case FUNCTION_DECL:
797 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
798 error_at (DECL_SOURCE_LOCATION (e->decl),
799 "function %qD redeclared as variable",
800 prevailing->decl);
801 break;
803 default:
804 gcc_unreachable ();
807 diagnosed_p = true;
809 if (diagnosed_p)
810 inform (DECL_SOURCE_LOCATION (prevailing->decl),
811 "previously declared here");
813 /* Merge the chain to the single prevailing decl and diagnose
814 mismatches. */
815 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
817 if (symtab->dump_file)
819 fprintf (symtab->dump_file, "After resolution:\n");
820 for (e = prevailing; e; e = e->next_sharing_asm_name)
821 e->dump (symtab->dump_file);
825 /* Resolve and merge all symbol table chains to a prevailing decl. */
827 void
828 lto_symtab_merge_decls (void)
830 symtab_node *node;
832 /* Populate assembler name hash. */
833 symtab->symtab_initialize_asm_name_hash ();
835 FOR_EACH_SYMBOL (node)
836 if (!node->previous_sharing_asm_name
837 && node->next_sharing_asm_name)
838 lto_symtab_merge_decls_1 (node);
841 /* Helper to process the decl chain for the symbol table entry *SLOT. */
843 static void
844 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
846 symtab_node *e;
847 symtab_node *next;
849 prevailing->decl->decl_with_vis.symtab_node = prevailing;
851 /* Replace the cgraph node of each entry with the prevailing one. */
852 for (e = prevailing->next_sharing_asm_name; e;
853 e = next)
855 next = e->next_sharing_asm_name;
857 if (!lto_symtab_symbol_p (e))
858 continue;
859 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
860 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
862 /* No matter how we are going to deal with resolution, we will ultimately
863 use prevailing definition. */
864 if (ce)
865 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
866 dyn_cast<cgraph_node *> (e));
868 /* If we decided to replace the node by TO, do it. */
869 if (e != to)
871 if (ce)
872 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
873 else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
874 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
876 /* Watch out for duplicated symbols for a given declaration. */
877 else if (!e->transparent_alias
878 || !e->definition || e->get_alias_target () != to)
880 /* We got a new declaration we do not want to merge. In this case
881 get rid of the existing definition and create a transparent
882 alias. */
883 if (ce)
885 lto_free_function_in_decl_state_for_node (ce);
886 if (!ce->weakref)
887 ce->release_body ();
888 ce->reset ();
889 symtab->call_cgraph_removal_hooks (ce);
891 else
893 DECL_INITIAL (e->decl) = error_mark_node;
894 if (e->lto_file_data)
896 lto_free_function_in_decl_state_for_node (e);
897 e->lto_file_data = NULL;
899 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
901 e->remove_all_references ();
902 e->analyzed = e->body_removed = false;
903 e->resolve_alias (prevailing, true);
904 gcc_assert (e != prevailing);
908 return;
911 /* Merge cgraph nodes according to the symbol merging done by
912 lto_symtab_merge_decls. */
914 void
915 lto_symtab_merge_symbols (void)
917 symtab_node *node;
919 if (!flag_ltrans)
921 symtab->symtab_initialize_asm_name_hash ();
923 /* Do the actual merging.
924 At this point we invalidate hash translating decls into symtab nodes
925 because after removing one of duplicate decls the hash is not correcly
926 updated to the ohter dupliate. */
927 FOR_EACH_SYMBOL (node)
928 if (lto_symtab_symbol_p (node)
929 && node->next_sharing_asm_name
930 && !node->previous_sharing_asm_name)
931 lto_symtab_merge_symbols_1 (node);
933 /* Resolve weakref aliases whose target are now in the compilation unit.
934 also re-populate the hash translating decls into symtab nodes*/
935 FOR_EACH_SYMBOL (node)
937 cgraph_node *cnode, *cnode2;
938 varpool_node *vnode;
939 symtab_node *node2;
941 if (!node->analyzed && node->alias_target)
943 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
944 gcc_assert (node->weakref);
945 if (tgt)
946 node->resolve_alias (tgt, true);
949 if (!(cnode = dyn_cast <cgraph_node *> (node))
950 || !cnode->clone_of
951 || cnode->clone_of->decl != cnode->decl)
953 /* Builtins are not merged via decl merging. It is however
954 possible that tree merging unified the declaration. We
955 do not want duplicate entries in symbol table. */
956 if (cnode && DECL_BUILT_IN (node->decl)
957 && (cnode2 = cgraph_node::get (node->decl))
958 && cnode2 != cnode)
959 lto_cgraph_replace_node (cnode2, cnode);
961 /* The user defined assembler variables are also not unified by their
962 symbol name (since it is irrelevant), but we need to unify symbol
963 nodes if tree merging occured. */
964 if ((vnode = dyn_cast <varpool_node *> (node))
965 && DECL_HARD_REGISTER (vnode->decl)
966 && (node2 = symtab_node::get (vnode->decl))
967 && node2 != node)
968 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
969 vnode);
972 /* Abstract functions may have duplicated cgraph nodes attached;
973 remove them. */
974 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
975 && (cnode2 = cgraph_node::get (node->decl))
976 && cnode2 != cnode)
977 cnode2->remove ();
979 node->decl->decl_with_vis.symtab_node = node;
985 /* Virtual tables may matter for code generation even if they are not
986 directly refernced by the code because they may be used for devirtualizaiton.
987 For this reason it is important to merge even virtual tables that have no
988 associated symbol table entries. Without doing so we lose optimization
989 oppurtunities by losing track of the vtable constructor.
990 FIXME: we probably ought to introduce explicit symbol table entries for
991 those before streaming. */
993 tree
994 lto_symtab_prevailing_virtual_decl (tree decl)
996 if (DECL_ABSTRACT_P (decl))
997 return decl;
998 gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
999 && DECL_ASSEMBLER_NAME_SET_P (decl));
1001 symtab_node *n = symtab_node::get_for_asmname
1002 (DECL_ASSEMBLER_NAME (decl));
1003 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1004 || !DECL_VIRTUAL_P (n->decl)))
1005 n = n->next_sharing_asm_name;
1006 if (n)
1008 /* Merge decl state in both directions, we may still end up using
1009 the other decl. */
1010 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1011 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1013 if (TREE_CODE (decl) == FUNCTION_DECL)
1015 /* Merge decl state in both directions, we may still end up using
1016 the other decl. */
1017 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1018 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1020 lto_symtab_prevail_decl (n->decl, decl);
1021 decl = n->decl;
1023 else
1024 symtab_node::get_create (decl);
1026 return decl;