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
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
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/>. */
23 #include "coretypes.h"
26 #include "basic-block.h"
30 #include "lto-streamer.h"
31 #include "ipa-utils.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. */
40 lto_cgraph_replace_node (struct cgraph_node
*node
,
41 struct cgraph_node
*prevailing_node
)
43 struct cgraph_edge
*e
, *next
;
46 if (symtab
->dump_file
)
48 fprintf (symtab
->dump_file
, "Replacing cgraph node %s/%i by %s/%i"
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. */
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. */
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. */
115 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
116 all edges and removing the old node. */
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
)
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
)
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
)
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. */
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. */
206 warn_type_compatibility_p (tree prevailing_type
, tree type
,
207 bool common_or_extern
)
210 bool odr_p
= odr_or_derived_type_p (prevailing_type
)
211 && odr_or_derived_type_p (type
);
213 if (prevailing_type
== type
)
216 /* C++ provide a robust way to check for type compatibility via the ODR
218 if (odr_p
&& !odr_types_equivalent_p (prevailing_type
, type
))
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
))
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
))
237 for (parm1
= TYPE_ARG_TYPES (prevailing_type
),
238 parm2
= TYPE_ARG_TYPES (type
);
240 parm1
= TREE_CHAIN (parm1
),
241 parm2
= TREE_CHAIN (parm2
))
242 lev
|= warn_type_compatibility_p (TREE_VALUE (parm1
),
243 TREE_VALUE (parm2
), false);
245 lev
|= odr_p
? 3 : 1;
247 if (comp_type_attributes (prevailing_type
, type
) == 0)
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
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
)))
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
))))
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. */
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
)
309 if (TREE_CODE (decl
) != TREE_CODE (prevailing_decl
))
312 /* Merge decl state in both directions, we may still end up using
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
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
),
330 || DECL_EXTERNAL (decl
)))
336 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl
),
338 DECL_COMMON (decl
) || DECL_EXTERNAL (decl
)))
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
))
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
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
)))))
370 /* Return true if the symtab entry E can be replaced by another symtab
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
))
382 if (TREE_CODE (e
->decl
) == VAR_DECL
)
383 return (DECL_COMMON (e
->decl
)
384 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
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). */
394 lto_symtab_symbol_p (symtab_node
*e
)
396 if (!TREE_PUBLIC (e
->decl
) && !DECL_EXTERNAL (e
->decl
))
398 return e
->real_symbol_p ();
401 /* Return true if the symtab entry E can be the prevailing one. */
404 lto_symtab_resolve_can_prevail_p (symtab_node
*e
)
406 if (!lto_symtab_symbol_p (e
))
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
))
415 return e
->definition
;
418 /* Resolve the symbol with the candidates in the chain *SLOT and store
419 their resolutions. */
422 lto_symtab_resolve_symbols (symtab_node
*first
)
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
))
438 /* If the chain is already resolved there is nothing else to do. */
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
));
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
))
459 /* If we have a non-replaceable definition it prevails. */
460 if (!lto_symtab_resolve_replaceable_p (e
))
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");
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
))
481 /* Choose the first function that can prevail as prevailing. */
482 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
488 /* From variables that can prevail choose the largest one. */
490 || tree_int_cst_lt (DECL_SIZE (prevailing
->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
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
)))
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. */
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");
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");
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");
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
)
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");
562 prev_attr
= lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing
));
563 attr
= lookup_attribute ("warning", DECL_ATTRIBUTES (decl
));
564 if ((prev_attr
== NULL
) != (attr
== NULL
)
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");
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.*/
583 lto_symtab_merge_decls_2 (symtab_node
*first
, bool diagnosed_p
)
585 symtab_node
*prevailing
;
587 vec
<tree
> mismatches
= vNULL
;
592 /* Nothing to do for a single entry. */
594 if (!prevailing
->next_sharing_asm_name
)
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
605 if (lto_symtab_prevailing_decl (e
->decl
) != e
->decl
606 || !lto_symtab_symbol_p (e
)
607 || e
->decl
== prevailing
->decl
)
610 if (!lto_symtab_merge (prevailing
, e
)
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
))
622 if (this_prevailing
== last_prevailing
)
624 this_prevailing
= NULL
;
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
)
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
)
652 if (mismatches
.is_empty ())
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
),
665 diag
= warning_at (DECL_SOURCE_LOCATION (decl
),
667 "%qD violates the C++ One Definition Rule ",
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
);
676 warn_types_mismatch (TREE_TYPE (prevailing
->decl
),
678 DECL_SOURCE_LOCATION (prevailing
->decl
),
679 DECL_SOURCE_LOCATION (decl
));
681 && !TREE_READONLY (prevailing
->decl
))
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
);
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
);
702 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
703 "%qD was previously declared here", prevailing
->decl
);
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. */
715 lto_symtab_merge_decls_1 (symtab_node
*first
)
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",
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. */
739 for (prevailing
= first
;
740 prevailing
; prevailing
= prevailing
->next_sharing_asm_name
)
741 if (lto_symtab_symbol_p (prevailing
))
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
))
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
))
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
))
784 if (!lto_symtab_symbol_p (e
))
787 switch (TREE_CODE (prevailing
->decl
))
790 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
791 error_at (DECL_SOURCE_LOCATION (e
->decl
),
792 "variable %qD redeclared as function",
797 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
798 error_at (DECL_SOURCE_LOCATION (e
->decl
),
799 "function %qD redeclared as variable",
810 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
811 "previously declared here");
813 /* Merge the chain to the single prevailing decl and diagnose
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. */
828 lto_symtab_merge_decls (void)
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. */
844 lto_symtab_merge_symbols_1 (symtab_node
*prevailing
)
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
;
855 next
= e
->next_sharing_asm_name
;
857 if (!lto_symtab_symbol_p (e
))
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. */
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. */
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
885 lto_free_function_in_decl_state_for_node (ce
);
889 symtab
->call_cgraph_removal_hooks (ce
);
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
);
911 /* Merge cgraph nodes according to the symbol merging done by
912 lto_symtab_merge_decls. */
915 lto_symtab_merge_symbols (void)
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
;
941 if (!node
->analyzed
&& node
->alias_target
)
943 symtab_node
*tgt
= symtab_node::get_for_asmname (node
->alias_target
);
944 gcc_assert (node
->weakref
);
946 node
->resolve_alias (tgt
, true);
949 if (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
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
))
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
))
968 lto_varpool_replace_node (dyn_cast
<varpool_node
*> (node2
),
972 /* Abstract functions may have duplicated cgraph nodes attached;
974 else if (cnode
&& DECL_ABSTRACT_P (cnode
->decl
)
975 && (cnode2
= cgraph_node::get (node
->decl
))
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. */
994 lto_symtab_prevailing_virtual_decl (tree decl
)
996 if (DECL_ABSTRACT_P (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
;
1008 /* Merge decl state in both directions, we may still end up using
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
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
);
1024 symtab_node::get_create (decl
);