2 Copyright (C) 2009-2015 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"
24 #include "diagnostic-core.h"
32 #include "hard-reg-set.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
41 #include "plugin-api.h"
45 #include "lto-streamer.h"
46 #include "ipa-utils.h"
47 #include "alloc-pool.h"
48 #include "symbol-summary.h"
50 #include "ipa-inline.h"
53 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
54 all edges and removing the old node. */
57 lto_cgraph_replace_node (struct cgraph_node
*node
,
58 struct cgraph_node
*prevailing_node
)
60 struct cgraph_edge
*e
, *next
;
63 if (symtab
->dump_file
)
65 fprintf (symtab
->dump_file
, "Replacing cgraph node %s/%i by %s/%i"
67 node
->name (), node
->order
,
68 prevailing_node
->name (),
69 prevailing_node
->order
,
70 IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
)
71 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
)))));
74 /* Merge node flags. */
75 if (node
->force_output
)
76 prevailing_node
->mark_force_output ();
77 if (node
->forced_by_abi
)
78 prevailing_node
->forced_by_abi
= true;
79 if (node
->address_taken
)
81 gcc_assert (!prevailing_node
->global
.inlined_to
);
82 prevailing_node
->mark_address_taken ();
85 /* Redirect all incoming edges. */
87 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node
->decl
)),
88 TREE_TYPE (TREE_TYPE (node
->decl
)));
89 for (e
= node
->callers
; e
; e
= next
)
91 next
= e
->next_caller
;
92 e
->redirect_callee (prevailing_node
);
93 /* If there is a mismatch between the supposed callee return type and
94 the real one do not attempt to inline this function.
95 ??? We really need a way to match function signatures for ABI
96 compatibility and perform related promotions at inlining time. */
98 e
->call_stmt_cannot_inline_p
= 1;
100 /* Redirect incomming references. */
101 prevailing_node
->clone_referring (node
);
103 /* Fix instrumentation references. */
104 if (node
->instrumented_version
)
106 gcc_assert (node
->instrumentation_clone
107 == prevailing_node
->instrumentation_clone
);
108 node
->instrumented_version
->instrumented_version
= prevailing_node
;
109 if (!prevailing_node
->instrumented_version
)
110 prevailing_node
->instrumented_version
= node
->instrumented_version
;
111 /* Need to reset node->instrumented_version to NULL,
112 otherwise node removal code would reset
113 node->instrumented_version->instrumented_version. */
114 node
->instrumented_version
= NULL
;
117 ipa_merge_profiles (prevailing_node
, node
);
118 lto_free_function_in_decl_state_for_node (node
);
120 if (node
->decl
!= prevailing_node
->decl
)
121 node
->release_body ();
123 /* Finally remove the replaced node. */
127 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
128 all edges and removing the old node. */
131 lto_varpool_replace_node (varpool_node
*vnode
,
132 varpool_node
*prevailing_node
)
134 gcc_assert (!vnode
->definition
|| prevailing_node
->definition
);
135 gcc_assert (!vnode
->analyzed
|| prevailing_node
->analyzed
);
137 prevailing_node
->clone_referring (vnode
);
138 if (vnode
->force_output
)
139 prevailing_node
->force_output
= true;
140 if (vnode
->forced_by_abi
)
141 prevailing_node
->forced_by_abi
= true;
143 /* Be sure we can garbage collect the initializer. */
144 if (DECL_INITIAL (vnode
->decl
)
145 && vnode
->decl
!= prevailing_node
->decl
)
146 DECL_INITIAL (vnode
->decl
) = error_mark_node
;
148 /* Check and report ODR violations on virtual tables. */
149 if (DECL_VIRTUAL_P (vnode
->decl
) || DECL_VIRTUAL_P (prevailing_node
->decl
))
150 compare_virtual_tables (prevailing_node
, vnode
);
152 if (vnode
->tls_model
!= prevailing_node
->tls_model
)
154 error_at (DECL_SOURCE_LOCATION (vnode
->decl
),
155 "%qD is defined as %s", vnode
->decl
, tls_model_names
[vnode
->tls_model
]);
156 inform (DECL_SOURCE_LOCATION (prevailing_node
->decl
),
157 "previously defined here as %s",
158 tls_model_names
[prevailing_node
->tls_model
]);
160 /* Finally remove the replaced node. */
164 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
165 Return false if the symbols are not fully compatible and a diagnostic
166 should be emitted. */
169 lto_symtab_merge (symtab_node
*prevailing
, symtab_node
*entry
)
171 tree prevailing_decl
= prevailing
->decl
;
172 tree decl
= entry
->decl
;
173 tree prevailing_type
, type
;
175 if (prevailing_decl
== decl
)
178 /* Merge decl state in both directions, we may still end up using
180 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
181 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
183 /* The linker may ask us to combine two incompatible symbols.
184 Detect this case and notify the caller of required diagnostics. */
186 if (TREE_CODE (decl
) == FUNCTION_DECL
)
188 if (!types_compatible_p (TREE_TYPE (prevailing_decl
),
190 /* If we don't have a merged type yet...sigh. The linker
191 wouldn't complain if the types were mismatched, so we
192 probably shouldn't either. Just use the type from
193 whichever decl appears to be associated with the
194 definition. If for some odd reason neither decl is, the
201 /* Now we exclusively deal with VAR_DECLs. */
203 /* Sharing a global symbol is a strong hint that two types are
204 compatible. We could use this information to complete
205 incomplete pointed-to types more aggressively here, ignoring
206 mismatches in both field and tag names. It's difficult though
207 to guarantee that this does not have side-effects on merging
208 more compatible types from other translation units though. */
210 /* We can tolerate differences in type qualification, the
211 qualification of the prevailing definition will prevail.
212 ??? In principle we might want to only warn for structurally
213 incompatible types here, but unless we have protective measures
214 for TBAA in place that would hide useful information. */
215 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
216 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
218 if (!types_compatible_p (prevailing_type
, type
))
220 if (COMPLETE_TYPE_P (type
))
223 /* If type is incomplete then avoid warnings in the cases
224 that TBAA handles just fine. */
226 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
229 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
231 tree tem1
= TREE_TYPE (prevailing_type
);
232 tree tem2
= TREE_TYPE (type
);
233 while (TREE_CODE (tem1
) == ARRAY_TYPE
234 && TREE_CODE (tem2
) == ARRAY_TYPE
)
236 tem1
= TREE_TYPE (tem1
);
237 tem2
= TREE_TYPE (tem2
);
240 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
243 if (!types_compatible_p (tem1
, tem2
))
247 /* Fallthru. Compatible enough. */
250 /* ??? We might want to emit a warning here if type qualification
251 differences were spotted. Do not do this unconditionally though. */
253 /* There is no point in comparing too many details of the decls here.
254 The type compatibility checks or the completing of types has properly
255 dealt with most issues. */
257 /* The following should all not invoke fatal errors as in non-LTO
258 mode the linker wouldn't complain either. Just emit warnings. */
260 /* Report a warning if user-specified alignments do not match. */
261 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
262 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
268 /* Return true if the symtab entry E can be replaced by another symtab
272 lto_symtab_resolve_replaceable_p (symtab_node
*e
)
274 if (DECL_EXTERNAL (e
->decl
)
275 || DECL_COMDAT (e
->decl
)
276 || DECL_ONE_ONLY (e
->decl
)
277 || DECL_WEAK (e
->decl
))
280 if (TREE_CODE (e
->decl
) == VAR_DECL
)
281 return (DECL_COMMON (e
->decl
)
282 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
287 /* Return true, if the symbol E should be resolved by lto-symtab.
288 Those are all external symbols and all real symbols that are not static (we
289 handle renaming of static later in partitioning). */
292 lto_symtab_symbol_p (symtab_node
*e
)
294 if (!TREE_PUBLIC (e
->decl
) && !DECL_EXTERNAL (e
->decl
))
296 return e
->real_symbol_p ();
299 /* Return true if the symtab entry E can be the prevailing one. */
302 lto_symtab_resolve_can_prevail_p (symtab_node
*e
)
304 if (!lto_symtab_symbol_p (e
))
307 /* The C++ frontend ends up neither setting TREE_STATIC nor
308 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
309 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
310 if (DECL_EXTERNAL (e
->decl
))
313 return e
->definition
;
316 /* Resolve the symbol with the candidates in the chain *SLOT and store
317 their resolutions. */
320 lto_symtab_resolve_symbols (symtab_node
*first
)
323 symtab_node
*prevailing
= NULL
;
325 /* Always set e->node so that edges are updated to reflect decl merging. */
326 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
327 if (lto_symtab_symbol_p (e
)
328 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
329 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
330 || e
->resolution
== LDPR_PREVAILING_DEF
))
336 /* If the chain is already resolved there is nothing else to do. */
339 /* Assert it's the only one. */
340 for (e
= prevailing
->next_sharing_asm_name
; e
; e
= e
->next_sharing_asm_name
)
341 if (lto_symtab_symbol_p (e
)
342 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
343 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
344 || e
->resolution
== LDPR_PREVAILING_DEF
))
345 fatal_error ("multiple prevailing defs for %qE",
346 DECL_NAME (prevailing
->decl
));
350 /* Find the single non-replaceable prevailing symbol and
351 diagnose ODR violations. */
352 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
354 if (!lto_symtab_resolve_can_prevail_p (e
))
357 /* If we have a non-replaceable definition it prevails. */
358 if (!lto_symtab_resolve_replaceable_p (e
))
362 error_at (DECL_SOURCE_LOCATION (e
->decl
),
363 "%qD has already been defined", e
->decl
);
364 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
365 "previously defined here");
373 /* Do a second round choosing one from the replaceable prevailing decls. */
374 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
376 if (!lto_symtab_resolve_can_prevail_p (e
))
379 /* Choose the first function that can prevail as prevailing. */
380 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
386 /* From variables that can prevail choose the largest one. */
388 || tree_int_cst_lt (DECL_SIZE (prevailing
->decl
),
390 /* When variables are equivalent try to chose one that has useful
391 DECL_INITIAL. This makes sense for keyed vtables that are
392 DECL_EXTERNAL but initialized. In units that do not need them
393 we replace the initializer by error_mark_node to conserve
396 We know that the vtable is keyed outside the LTO unit - otherwise
397 the keyed instance would prevail. We still can preserve useful
398 info in the initializer. */
399 || (DECL_SIZE (prevailing
->decl
) == DECL_SIZE (e
->decl
)
400 && (DECL_INITIAL (e
->decl
)
401 && DECL_INITIAL (e
->decl
) != error_mark_node
)
402 && (!DECL_INITIAL (prevailing
->decl
)
403 || DECL_INITIAL (prevailing
->decl
) == error_mark_node
)))
410 /* Merge all decls in the symbol table chain to the prevailing decl and
411 issue diagnostics about type mismatches. If DIAGNOSED_P is true
412 do not issue further diagnostics.*/
415 lto_symtab_merge_decls_2 (symtab_node
*first
, bool diagnosed_p
)
417 symtab_node
*prevailing
;
419 vec
<tree
> mismatches
= vNULL
;
423 /* Nothing to do for a single entry. */
425 if (!prevailing
->next_sharing_asm_name
)
428 /* Try to merge each entry with the prevailing one. */
429 for (e
= prevailing
->next_sharing_asm_name
;
430 e
; e
= e
->next_sharing_asm_name
)
431 if (TREE_PUBLIC (e
->decl
))
433 if (!lto_symtab_merge (prevailing
, e
)
435 mismatches
.safe_push (e
->decl
);
437 if (mismatches
.is_empty ())
440 /* Diagnose all mismatched re-declarations. */
441 FOR_EACH_VEC_ELT (mismatches
, i
, decl
)
443 if (!types_compatible_p (TREE_TYPE (prevailing
->decl
),
445 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
446 "type of %qD does not match original "
447 "declaration", decl
);
449 else if ((DECL_USER_ALIGN (prevailing
->decl
)
450 && DECL_USER_ALIGN (decl
))
451 && DECL_ALIGN (prevailing
->decl
) < DECL_ALIGN (decl
))
453 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
454 "alignment of %qD is bigger than "
455 "original declaration", decl
);
459 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
460 "previously declared here");
462 mismatches
.release ();
465 /* Helper to process the decl chain for the symbol table entry *SLOT. */
468 lto_symtab_merge_decls_1 (symtab_node
*first
)
471 symtab_node
*prevailing
;
472 bool diagnosed_p
= false;
474 if (symtab
->dump_file
)
476 fprintf (symtab
->dump_file
, "Merging nodes for %s. Candidates:\n",
478 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
479 if (TREE_PUBLIC (e
->decl
))
480 e
->dump (symtab
->dump_file
);
483 /* Compute the symbol resolutions. This is a no-op when using the
484 linker plugin and resolution was decided by the linker. */
485 prevailing
= lto_symtab_resolve_symbols (first
);
487 /* If there's not a prevailing symbol yet it's an external reference.
488 Happens a lot during ltrans. Choose the first symbol with a
489 cgraph or a varpool node. */
492 for (prevailing
= first
;
493 prevailing
; prevailing
= prevailing
->next_sharing_asm_name
)
494 if (lto_symtab_symbol_p (prevailing
))
498 /* For variables chose with a priority variant with vnode
499 attached (i.e. from unit where external declaration of
500 variable is actually used).
501 When there are multiple variants, chose one with size.
502 This is needed for C++ typeinfos, for example in
503 lto/20081204-1 there are typeifos in both units, just
504 one of them do have size. */
505 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
507 for (e
= prevailing
->next_sharing_asm_name
;
508 e
; e
= e
->next_sharing_asm_name
)
509 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing
->decl
))
510 && COMPLETE_TYPE_P (TREE_TYPE (e
->decl
))
511 && lto_symtab_symbol_p (e
))
514 /* For variables prefer the non-builtin if one is available. */
515 else if (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
)
517 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
518 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
519 && !DECL_BUILT_IN (e
->decl
)
520 && lto_symtab_symbol_p (e
))
528 symtab
->symtab_prevail_in_asm_name_hash (prevailing
);
530 /* Diagnose mismatched objects. */
531 for (e
= prevailing
->next_sharing_asm_name
;
532 e
; e
= e
->next_sharing_asm_name
)
534 if (TREE_CODE (prevailing
->decl
)
535 == TREE_CODE (e
->decl
))
537 if (!lto_symtab_symbol_p (e
))
540 switch (TREE_CODE (prevailing
->decl
))
543 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
544 error_at (DECL_SOURCE_LOCATION (e
->decl
),
545 "variable %qD redeclared as function",
550 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
551 error_at (DECL_SOURCE_LOCATION (e
->decl
),
552 "function %qD redeclared as variable",
563 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
564 "previously declared here");
566 /* Merge the chain to the single prevailing decl and diagnose
568 lto_symtab_merge_decls_2 (prevailing
, diagnosed_p
);
570 if (symtab
->dump_file
)
572 fprintf (symtab
->dump_file
, "After resolution:\n");
573 for (e
= prevailing
; e
; e
= e
->next_sharing_asm_name
)
574 e
->dump (symtab
->dump_file
);
578 /* Resolve and merge all symbol table chains to a prevailing decl. */
581 lto_symtab_merge_decls (void)
585 /* Populate assembler name hash. */
586 symtab
->symtab_initialize_asm_name_hash ();
588 FOR_EACH_SYMBOL (node
)
589 if (!node
->previous_sharing_asm_name
590 && node
->next_sharing_asm_name
)
591 lto_symtab_merge_decls_1 (node
);
594 /* Helper to process the decl chain for the symbol table entry *SLOT. */
597 lto_symtab_merge_symbols_1 (symtab_node
*prevailing
)
602 /* Replace the cgraph node of each entry with the prevailing one. */
603 for (e
= prevailing
->next_sharing_asm_name
; e
;
606 next
= e
->next_sharing_asm_name
;
608 if (!lto_symtab_symbol_p (e
))
610 cgraph_node
*ce
= dyn_cast
<cgraph_node
*> (e
);
611 if (ce
&& !DECL_BUILT_IN (e
->decl
))
612 lto_cgraph_replace_node (ce
, dyn_cast
<cgraph_node
*> (prevailing
));
613 if (varpool_node
*ve
= dyn_cast
<varpool_node
*> (e
))
614 lto_varpool_replace_node (ve
, dyn_cast
<varpool_node
*> (prevailing
));
620 /* Merge cgraph nodes according to the symbol merging done by
621 lto_symtab_merge_decls. */
624 lto_symtab_merge_symbols (void)
630 symtab
->symtab_initialize_asm_name_hash ();
632 /* Do the actual merging.
633 At this point we invalidate hash translating decls into symtab nodes
634 because after removing one of duplicate decls the hash is not correcly
635 updated to the ohter dupliate. */
636 FOR_EACH_SYMBOL (node
)
637 if (lto_symtab_symbol_p (node
)
638 && node
->next_sharing_asm_name
639 && !node
->previous_sharing_asm_name
)
640 lto_symtab_merge_symbols_1 (node
);
642 /* Resolve weakref aliases whose target are now in the compilation unit.
643 also re-populate the hash translating decls into symtab nodes*/
644 FOR_EACH_SYMBOL (node
)
646 cgraph_node
*cnode
, *cnode2
;
650 if (!node
->analyzed
&& node
->alias_target
)
652 symtab_node
*tgt
= symtab_node::get_for_asmname (node
->alias_target
);
653 gcc_assert (node
->weakref
);
655 node
->resolve_alias (tgt
);
659 if (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
661 || cnode
->clone_of
->decl
!= cnode
->decl
)
663 /* Builtins are not merged via decl merging. It is however
664 possible that tree merging unified the declaration. We
665 do not want duplicate entries in symbol table. */
666 if (cnode
&& DECL_BUILT_IN (node
->decl
)
667 && (cnode2
= cgraph_node::get (node
->decl
))
669 lto_cgraph_replace_node (cnode2
, cnode
);
671 /* The user defined assembler variables are also not unified by their
672 symbol name (since it is irrelevant), but we need to unify symbol
673 nodes if tree merging occured. */
674 if ((vnode
= dyn_cast
<varpool_node
*> (node
))
675 && DECL_HARD_REGISTER (vnode
->decl
)
676 && (node2
= symtab_node::get (vnode
->decl
))
678 lto_varpool_replace_node (dyn_cast
<varpool_node
*> (node2
),
682 /* Abstract functions may have duplicated cgraph nodes attached;
684 else if (cnode
&& DECL_ABSTRACT_P (cnode
->decl
)
685 && (cnode2
= cgraph_node::get (node
->decl
))
689 node
->decl
->decl_with_vis
.symtab_node
= node
;
695 /* Given the decl DECL, return the prevailing decl with the same name. */
698 lto_symtab_prevailing_decl (tree decl
)
702 /* Builtins and local symbols are their own prevailing decl. */
703 if ((!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
)) || is_builtin_fn (decl
))
706 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
707 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT_P (decl
))
710 /* Likewise builtins are their own prevailing decl. This preserves
711 non-builtin vs. builtin uses from compile-time. */
712 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
715 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
716 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
718 /* Walk through the list of candidates and return the one we merged to. */
719 ret
= symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl
));