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"
28 #include "double-int.h"
36 #include "fold-const.h"
39 #include "hard-reg-set.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
48 #include "plugin-api.h"
52 #include "lto-streamer.h"
53 #include "ipa-utils.h"
54 #include "alloc-pool.h"
55 #include "symbol-summary.h"
57 #include "ipa-inline.h"
60 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
61 all edges and removing the old node. */
64 lto_cgraph_replace_node (struct cgraph_node *node,
65 struct cgraph_node *prevailing_node)
67 struct cgraph_edge *e, *next;
70 if (symtab->dump_file)
72 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
74 node->name (), node->order,
75 prevailing_node->name (),
76 prevailing_node->order,
77 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
78 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
81 /* Merge node flags. */
82 if (node->force_output)
83 prevailing_node->mark_force_output ();
84 if (node->forced_by_abi)
85 prevailing_node->forced_by_abi = true;
86 if (node->address_taken)
88 gcc_assert (!prevailing_node->global.inlined_to);
89 prevailing_node->mark_address_taken ();
91 if (node->definition && prevailing_node->definition)
92 prevailing_node->merged = true;
94 /* Redirect all incoming edges. */
96 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
97 TREE_TYPE (TREE_TYPE (node->decl)));
98 for (e = node->callers; e; e = next)
100 next = e->next_caller;
101 e->redirect_callee (prevailing_node);
102 /* If there is a mismatch between the supposed callee return type and
103 the real one do not attempt to inline this function.
104 ??? We really need a way to match function signatures for ABI
105 compatibility and perform related promotions at inlining time. */
107 e->call_stmt_cannot_inline_p = 1;
109 /* Redirect incomming references. */
110 prevailing_node->clone_referring (node);
112 /* Fix instrumentation references. */
113 if (node->instrumented_version)
115 gcc_assert (node->instrumentation_clone
116 == prevailing_node->instrumentation_clone);
117 node->instrumented_version->instrumented_version = prevailing_node;
118 if (!prevailing_node->instrumented_version)
119 prevailing_node->instrumented_version = node->instrumented_version;
120 /* Need to reset node->instrumented_version to NULL,
121 otherwise node removal code would reset
122 node->instrumented_version->instrumented_version. */
123 node->instrumented_version = NULL;
126 ipa_merge_profiles (prevailing_node, node);
127 lto_free_function_in_decl_state_for_node (node);
129 if (node->decl != prevailing_node->decl)
130 node->release_body ();
132 /* Finally remove the replaced node. */
136 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
137 all edges and removing the old node. */
140 lto_varpool_replace_node (varpool_node *vnode,
141 varpool_node *prevailing_node)
143 gcc_assert (!vnode->definition || prevailing_node->definition);
144 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
146 prevailing_node->clone_referring (vnode);
147 if (vnode->force_output)
148 prevailing_node->force_output = true;
149 if (vnode->forced_by_abi)
150 prevailing_node->forced_by_abi = true;
152 /* Be sure we can garbage collect the initializer. */
153 if (DECL_INITIAL (vnode->decl)
154 && vnode->decl != prevailing_node->decl)
155 DECL_INITIAL (vnode->decl) = error_mark_node;
157 /* Check and report ODR violations on virtual tables. */
158 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
159 compare_virtual_tables (prevailing_node, vnode);
161 if (vnode->tls_model != prevailing_node->tls_model)
165 /* Non-TLS and TLS never mix together. Also emulated model is not
166 compatible with anything else. */
167 if (prevailing_node->tls_model == TLS_MODEL_NONE
168 || prevailing_node->tls_model == TLS_MODEL_EMULATED
169 || vnode->tls_model == TLS_MODEL_NONE
170 || vnode->tls_model == TLS_MODEL_EMULATED)
172 /* Linked is silently supporting transitions
173 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
174 Do the same transitions and error out on others. */
175 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
176 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
177 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
178 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
179 prevailing_node->tls_model = vnode->tls_model;
180 else if ((vnode->tls_model == TLS_MODEL_REAL
181 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
182 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
183 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
185 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
186 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
187 prevailing_node->tls_model = vnode->tls_model;
188 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
189 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
195 error_at (DECL_SOURCE_LOCATION (vnode->decl),
196 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
197 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
198 "previously defined here as %s",
199 tls_model_names [prevailing_node->tls_model]);
202 /* Finally remove the replaced node. */
206 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
207 Return false if the symbols are not fully compatible and a diagnostic
208 should be emitted. */
211 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
213 tree prevailing_decl = prevailing->decl;
214 tree decl = entry->decl;
215 tree prevailing_type, type;
217 if (prevailing_decl == decl)
220 /* Merge decl state in both directions, we may still end up using
222 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
223 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
225 /* The linker may ask us to combine two incompatible symbols.
226 Detect this case and notify the caller of required diagnostics. */
228 if (TREE_CODE (decl) == FUNCTION_DECL)
230 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
232 /* If we don't have a merged type yet...sigh. The linker
233 wouldn't complain if the types were mismatched, so we
234 probably shouldn't either. Just use the type from
235 whichever decl appears to be associated with the
236 definition. If for some odd reason neither decl is, the
243 /* Now we exclusively deal with VAR_DECLs. */
245 /* Sharing a global symbol is a strong hint that two types are
246 compatible. We could use this information to complete
247 incomplete pointed-to types more aggressively here, ignoring
248 mismatches in both field and tag names. It's difficult though
249 to guarantee that this does not have side-effects on merging
250 more compatible types from other translation units though. */
252 /* We can tolerate differences in type qualification, the
253 qualification of the prevailing definition will prevail.
254 ??? In principle we might want to only warn for structurally
255 incompatible types here, but unless we have protective measures
256 for TBAA in place that would hide useful information. */
257 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
258 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
260 if (!types_compatible_p (prevailing_type, type))
262 if (COMPLETE_TYPE_P (type))
265 /* If type is incomplete then avoid warnings in the cases
266 that TBAA handles just fine. */
268 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
271 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
273 tree tem1 = TREE_TYPE (prevailing_type);
274 tree tem2 = TREE_TYPE (type);
275 while (TREE_CODE (tem1) == ARRAY_TYPE
276 && TREE_CODE (tem2) == ARRAY_TYPE)
278 tem1 = TREE_TYPE (tem1);
279 tem2 = TREE_TYPE (tem2);
282 if (TREE_CODE (tem1) != TREE_CODE (tem2))
285 if (!types_compatible_p (tem1, tem2))
289 /* Fallthru. Compatible enough. */
292 /* ??? We might want to emit a warning here if type qualification
293 differences were spotted. Do not do this unconditionally though. */
295 /* There is no point in comparing too many details of the decls here.
296 The type compatibility checks or the completing of types has properly
297 dealt with most issues. */
299 /* The following should all not invoke fatal errors as in non-LTO
300 mode the linker wouldn't complain either. Just emit warnings. */
302 /* Report a warning if user-specified alignments do not match. */
303 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
304 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
310 /* Return true if the symtab entry E can be replaced by another symtab
314 lto_symtab_resolve_replaceable_p (symtab_node *e)
316 if (DECL_EXTERNAL (e->decl)
317 || DECL_COMDAT (e->decl)
318 || DECL_ONE_ONLY (e->decl)
319 || DECL_WEAK (e->decl))
322 if (TREE_CODE (e->decl) == VAR_DECL)
323 return (DECL_COMMON (e->decl)
324 || (!flag_no_common && !DECL_INITIAL (e->decl)));
329 /* Return true, if the symbol E should be resolved by lto-symtab.
330 Those are all external symbols and all real symbols that are not static (we
331 handle renaming of static later in partitioning). */
334 lto_symtab_symbol_p (symtab_node *e)
336 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
338 return e->real_symbol_p ();
341 /* Return true if the symtab entry E can be the prevailing one. */
344 lto_symtab_resolve_can_prevail_p (symtab_node *e)
346 if (!lto_symtab_symbol_p (e))
349 /* The C++ frontend ends up neither setting TREE_STATIC nor
350 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
351 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
352 if (DECL_EXTERNAL (e->decl))
355 return e->definition;
358 /* Resolve the symbol with the candidates in the chain *SLOT and store
359 their resolutions. */
362 lto_symtab_resolve_symbols (symtab_node *first)
365 symtab_node *prevailing = NULL;
367 /* Always set e->node so that edges are updated to reflect decl merging. */
368 for (e = first; e; e = e->next_sharing_asm_name)
369 if (lto_symtab_symbol_p (e)
370 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
371 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
372 || e->resolution == LDPR_PREVAILING_DEF))
378 /* If the chain is already resolved there is nothing else to do. */
381 /* Assert it's the only one. */
382 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
383 if (lto_symtab_symbol_p (e)
384 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
385 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
386 || e->resolution == LDPR_PREVAILING_DEF))
387 fatal_error (input_location, "multiple prevailing defs for %qE",
388 DECL_NAME (prevailing->decl));
392 /* Find the single non-replaceable prevailing symbol and
393 diagnose ODR violations. */
394 for (e = first; e; e = e->next_sharing_asm_name)
396 if (!lto_symtab_resolve_can_prevail_p (e))
399 /* If we have a non-replaceable definition it prevails. */
400 if (!lto_symtab_resolve_replaceable_p (e))
404 error_at (DECL_SOURCE_LOCATION (e->decl),
405 "%qD has already been defined", e->decl);
406 inform (DECL_SOURCE_LOCATION (prevailing->decl),
407 "previously defined here");
415 /* Do a second round choosing one from the replaceable prevailing decls. */
416 for (e = first; e; e = e->next_sharing_asm_name)
418 if (!lto_symtab_resolve_can_prevail_p (e))
421 /* Choose the first function that can prevail as prevailing. */
422 if (TREE_CODE (e->decl) == FUNCTION_DECL)
428 /* From variables that can prevail choose the largest one. */
430 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
432 /* When variables are equivalent try to chose one that has useful
433 DECL_INITIAL. This makes sense for keyed vtables that are
434 DECL_EXTERNAL but initialized. In units that do not need them
435 we replace the initializer by error_mark_node to conserve
438 We know that the vtable is keyed outside the LTO unit - otherwise
439 the keyed instance would prevail. We still can preserve useful
440 info in the initializer. */
441 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
442 && (DECL_INITIAL (e->decl)
443 && DECL_INITIAL (e->decl) != error_mark_node)
444 && (!DECL_INITIAL (prevailing->decl)
445 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
452 /* Merge all decls in the symbol table chain to the prevailing decl and
453 issue diagnostics about type mismatches. If DIAGNOSED_P is true
454 do not issue further diagnostics.*/
457 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
459 symtab_node *prevailing;
461 vec<tree> mismatches = vNULL;
465 /* Nothing to do for a single entry. */
467 if (!prevailing->next_sharing_asm_name)
470 /* Try to merge each entry with the prevailing one. */
471 for (e = prevailing->next_sharing_asm_name;
472 e; e = e->next_sharing_asm_name)
473 if (TREE_PUBLIC (e->decl))
475 if (!lto_symtab_merge (prevailing, e)
477 mismatches.safe_push (e->decl);
479 if (mismatches.is_empty ())
482 /* Diagnose all mismatched re-declarations. */
483 FOR_EACH_VEC_ELT (mismatches, i, decl)
485 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
487 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
488 "type of %qD does not match original "
489 "declaration", decl);
491 else if ((DECL_USER_ALIGN (prevailing->decl)
492 && DECL_USER_ALIGN (decl))
493 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
495 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
496 "alignment of %qD is bigger than "
497 "original declaration", decl);
501 inform (DECL_SOURCE_LOCATION (prevailing->decl),
502 "previously declared here");
504 mismatches.release ();
507 /* Helper to process the decl chain for the symbol table entry *SLOT. */
510 lto_symtab_merge_decls_1 (symtab_node *first)
513 symtab_node *prevailing;
514 bool diagnosed_p = false;
516 if (symtab->dump_file)
518 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
520 for (e = first; e; e = e->next_sharing_asm_name)
521 if (TREE_PUBLIC (e->decl))
522 e->dump (symtab->dump_file);
525 /* Compute the symbol resolutions. This is a no-op when using the
526 linker plugin and resolution was decided by the linker. */
527 prevailing = lto_symtab_resolve_symbols (first);
529 /* If there's not a prevailing symbol yet it's an external reference.
530 Happens a lot during ltrans. Choose the first symbol with a
531 cgraph or a varpool node. */
534 for (prevailing = first;
535 prevailing; prevailing = prevailing->next_sharing_asm_name)
536 if (lto_symtab_symbol_p (prevailing))
540 /* For variables chose with a priority variant with vnode
541 attached (i.e. from unit where external declaration of
542 variable is actually used).
543 When there are multiple variants, chose one with size.
544 This is needed for C++ typeinfos, for example in
545 lto/20081204-1 there are typeifos in both units, just
546 one of them do have size. */
547 if (TREE_CODE (prevailing->decl) == VAR_DECL)
549 for (e = prevailing->next_sharing_asm_name;
550 e; e = e->next_sharing_asm_name)
551 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
552 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
553 && lto_symtab_symbol_p (e))
556 /* For variables prefer the non-builtin if one is available. */
557 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
559 for (e = first; e; e = e->next_sharing_asm_name)
560 if (TREE_CODE (e->decl) == FUNCTION_DECL
561 && !DECL_BUILT_IN (e->decl)
562 && lto_symtab_symbol_p (e))
570 symtab->symtab_prevail_in_asm_name_hash (prevailing);
572 /* Diagnose mismatched objects. */
573 for (e = prevailing->next_sharing_asm_name;
574 e; e = e->next_sharing_asm_name)
576 if (TREE_CODE (prevailing->decl)
577 == TREE_CODE (e->decl))
579 if (!lto_symtab_symbol_p (e))
582 switch (TREE_CODE (prevailing->decl))
585 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
586 error_at (DECL_SOURCE_LOCATION (e->decl),
587 "variable %qD redeclared as function",
592 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
593 error_at (DECL_SOURCE_LOCATION (e->decl),
594 "function %qD redeclared as variable",
605 inform (DECL_SOURCE_LOCATION (prevailing->decl),
606 "previously declared here");
608 /* Merge the chain to the single prevailing decl and diagnose
610 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
612 if (symtab->dump_file)
614 fprintf (symtab->dump_file, "After resolution:\n");
615 for (e = prevailing; e; e = e->next_sharing_asm_name)
616 e->dump (symtab->dump_file);
620 /* Resolve and merge all symbol table chains to a prevailing decl. */
623 lto_symtab_merge_decls (void)
627 /* Populate assembler name hash. */
628 symtab->symtab_initialize_asm_name_hash ();
630 FOR_EACH_SYMBOL (node)
631 if (!node->previous_sharing_asm_name
632 && node->next_sharing_asm_name)
633 lto_symtab_merge_decls_1 (node);
636 /* Helper to process the decl chain for the symbol table entry *SLOT. */
639 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
644 /* Replace the cgraph node of each entry with the prevailing one. */
645 for (e = prevailing->next_sharing_asm_name; e;
648 next = e->next_sharing_asm_name;
650 if (!lto_symtab_symbol_p (e))
652 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
653 if (ce && !DECL_BUILT_IN (e->decl))
654 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
655 if (varpool_node *ve = dyn_cast <varpool_node *> (e))
656 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
662 /* Merge cgraph nodes according to the symbol merging done by
663 lto_symtab_merge_decls. */
666 lto_symtab_merge_symbols (void)
672 symtab->symtab_initialize_asm_name_hash ();
674 /* Do the actual merging.
675 At this point we invalidate hash translating decls into symtab nodes
676 because after removing one of duplicate decls the hash is not correcly
677 updated to the ohter dupliate. */
678 FOR_EACH_SYMBOL (node)
679 if (lto_symtab_symbol_p (node)
680 && node->next_sharing_asm_name
681 && !node->previous_sharing_asm_name)
682 lto_symtab_merge_symbols_1 (node);
684 /* Resolve weakref aliases whose target are now in the compilation unit.
685 also re-populate the hash translating decls into symtab nodes*/
686 FOR_EACH_SYMBOL (node)
688 cgraph_node *cnode, *cnode2;
692 if (!node->analyzed && node->alias_target)
694 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
695 gcc_assert (node->weakref);
697 node->resolve_alias (tgt);
701 if (!(cnode = dyn_cast <cgraph_node *> (node))
703 || cnode->clone_of->decl != cnode->decl)
705 /* Builtins are not merged via decl merging. It is however
706 possible that tree merging unified the declaration. We
707 do not want duplicate entries in symbol table. */
708 if (cnode && DECL_BUILT_IN (node->decl)
709 && (cnode2 = cgraph_node::get (node->decl))
711 lto_cgraph_replace_node (cnode2, cnode);
713 /* The user defined assembler variables are also not unified by their
714 symbol name (since it is irrelevant), but we need to unify symbol
715 nodes if tree merging occured. */
716 if ((vnode = dyn_cast <varpool_node *> (node))
717 && DECL_HARD_REGISTER (vnode->decl)
718 && (node2 = symtab_node::get (vnode->decl))
720 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
724 /* Abstract functions may have duplicated cgraph nodes attached;
726 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
727 && (cnode2 = cgraph_node::get (node->decl))
731 node->decl->decl_with_vis.symtab_node = node;
737 /* Given the decl DECL, return the prevailing decl with the same name. */
740 lto_symtab_prevailing_decl (tree decl)
744 /* Builtins and local symbols are their own prevailing decl. */
745 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
748 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
749 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
752 /* Likewise builtins are their own prevailing decl. This preserves
753 non-builtin vs. builtin uses from compile-time. */
754 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
757 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
758 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
760 /* Walk through the list of candidates and return the one we merged to. */
761 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));