2 Copyright (C) 2009-2014 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"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
33 #include "plugin-api.h"
34 #include "lto-streamer.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
39 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
40 all edges and removing the old node. */
43 lto_cgraph_replace_node (struct cgraph_node
*node
,
44 struct cgraph_node
*prevailing_node
)
46 struct cgraph_edge
*e
, *next
;
51 fprintf (cgraph_dump_file
, "Replacing cgraph node %s/%i by %s/%i"
53 node
->name (), node
->order
,
54 prevailing_node
->name (),
55 prevailing_node
->order
,
56 IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
)
57 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
)))));
60 /* Merge node flags. */
61 if (node
->force_output
)
62 prevailing_node
->mark_force_output ();
63 if (node
->forced_by_abi
)
64 prevailing_node
->forced_by_abi
= true;
65 if (node
->address_taken
)
67 gcc_assert (!prevailing_node
->global
.inlined_to
);
68 prevailing_node
->mark_address_taken ();
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 cgraph_redirect_edge_callee (e
, 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. */
84 e
->call_stmt_cannot_inline_p
= 1;
86 /* Redirect incomming references. */
87 prevailing_node
->clone_referring (node
);
89 ipa_merge_profiles (prevailing_node
, node
);
90 lto_free_function_in_decl_state_for_node (node
);
92 if (node
->decl
!= prevailing_node
->decl
)
93 node
->release_body ();
95 /* Finally remove the replaced node. */
99 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
100 all edges and removing the old node. */
103 lto_varpool_replace_node (varpool_node
*vnode
,
104 varpool_node
*prevailing_node
)
106 gcc_assert (!vnode
->definition
|| prevailing_node
->definition
);
107 gcc_assert (!vnode
->analyzed
|| prevailing_node
->analyzed
);
109 prevailing_node
->clone_referring (vnode
);
110 if (vnode
->force_output
)
111 prevailing_node
->force_output
= true;
112 if (vnode
->forced_by_abi
)
113 prevailing_node
->forced_by_abi
= true;
115 /* Be sure we can garbage collect the initializer. */
116 if (DECL_INITIAL (vnode
->decl
)
117 && vnode
->decl
!= prevailing_node
->decl
)
118 DECL_INITIAL (vnode
->decl
) = error_mark_node
;
120 /* Check and report ODR violations on virtual tables. */
121 if (DECL_VIRTUAL_P (vnode
->decl
) || DECL_VIRTUAL_P (prevailing_node
->decl
))
122 compare_virtual_tables (prevailing_node
, vnode
);
124 if (vnode
->tls_model
!= prevailing_node
->tls_model
)
126 error_at (DECL_SOURCE_LOCATION (vnode
->decl
),
127 "%qD is defined as %s", vnode
->decl
, tls_model_names
[vnode
->tls_model
]);
128 inform (DECL_SOURCE_LOCATION (prevailing_node
->decl
),
129 "previously defined here as %s",
130 tls_model_names
[prevailing_node
->tls_model
]);
132 /* Finally remove the replaced node. */
136 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
137 Return false if the symbols are not fully compatible and a diagnostic
138 should be emitted. */
141 lto_symtab_merge (symtab_node
*prevailing
, symtab_node
*entry
)
143 tree prevailing_decl
= prevailing
->decl
;
144 tree decl
= entry
->decl
;
145 tree prevailing_type
, type
;
147 if (prevailing_decl
== decl
)
150 /* Merge decl state in both directions, we may still end up using
152 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
153 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
155 /* The linker may ask us to combine two incompatible symbols.
156 Detect this case and notify the caller of required diagnostics. */
158 if (TREE_CODE (decl
) == FUNCTION_DECL
)
160 if (!types_compatible_p (TREE_TYPE (prevailing_decl
),
162 /* If we don't have a merged type yet...sigh. The linker
163 wouldn't complain if the types were mismatched, so we
164 probably shouldn't either. Just use the type from
165 whichever decl appears to be associated with the
166 definition. If for some odd reason neither decl is, the
173 /* Now we exclusively deal with VAR_DECLs. */
175 /* Sharing a global symbol is a strong hint that two types are
176 compatible. We could use this information to complete
177 incomplete pointed-to types more aggressively here, ignoring
178 mismatches in both field and tag names. It's difficult though
179 to guarantee that this does not have side-effects on merging
180 more compatible types from other translation units though. */
182 /* We can tolerate differences in type qualification, the
183 qualification of the prevailing definition will prevail.
184 ??? In principle we might want to only warn for structurally
185 incompatible types here, but unless we have protective measures
186 for TBAA in place that would hide useful information. */
187 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
188 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
190 if (!types_compatible_p (prevailing_type
, type
))
192 if (COMPLETE_TYPE_P (type
))
195 /* If type is incomplete then avoid warnings in the cases
196 that TBAA handles just fine. */
198 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
201 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
203 tree tem1
= TREE_TYPE (prevailing_type
);
204 tree tem2
= TREE_TYPE (type
);
205 while (TREE_CODE (tem1
) == ARRAY_TYPE
206 && TREE_CODE (tem2
) == ARRAY_TYPE
)
208 tem1
= TREE_TYPE (tem1
);
209 tem2
= TREE_TYPE (tem2
);
212 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
215 if (!types_compatible_p (tem1
, tem2
))
219 /* Fallthru. Compatible enough. */
222 /* ??? We might want to emit a warning here if type qualification
223 differences were spotted. Do not do this unconditionally though. */
225 /* There is no point in comparing too many details of the decls here.
226 The type compatibility checks or the completing of types has properly
227 dealt with most issues. */
229 /* The following should all not invoke fatal errors as in non-LTO
230 mode the linker wouldn't complain either. Just emit warnings. */
232 /* Report a warning if user-specified alignments do not match. */
233 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
234 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
240 /* Return true if the symtab entry E can be replaced by another symtab
244 lto_symtab_resolve_replaceable_p (symtab_node
*e
)
246 if (DECL_EXTERNAL (e
->decl
)
247 || DECL_COMDAT (e
->decl
)
248 || DECL_ONE_ONLY (e
->decl
)
249 || DECL_WEAK (e
->decl
))
252 if (TREE_CODE (e
->decl
) == VAR_DECL
)
253 return (DECL_COMMON (e
->decl
)
254 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
259 /* Return true, if the symbol E should be resolved by lto-symtab.
260 Those are all external symbols and all real symbols that are not static (we
261 handle renaming of static later in partitioning). */
264 lto_symtab_symbol_p (symtab_node
*e
)
266 if (!TREE_PUBLIC (e
->decl
) && !DECL_EXTERNAL (e
->decl
))
268 return e
->real_symbol_p ();
271 /* Return true if the symtab entry E can be the prevailing one. */
274 lto_symtab_resolve_can_prevail_p (symtab_node
*e
)
276 if (!lto_symtab_symbol_p (e
))
279 /* The C++ frontend ends up neither setting TREE_STATIC nor
280 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
281 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
282 if (DECL_EXTERNAL (e
->decl
))
285 return e
->definition
;
288 /* Resolve the symbol with the candidates in the chain *SLOT and store
289 their resolutions. */
292 lto_symtab_resolve_symbols (symtab_node
*first
)
295 symtab_node
*prevailing
= NULL
;
297 /* Always set e->node so that edges are updated to reflect decl merging. */
298 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
299 if (lto_symtab_symbol_p (e
)
300 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
301 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
302 || e
->resolution
== LDPR_PREVAILING_DEF
))
308 /* If the chain is already resolved there is nothing else to do. */
311 /* Assert it's the only one. */
312 for (e
= prevailing
->next_sharing_asm_name
; e
; e
= e
->next_sharing_asm_name
)
313 if (lto_symtab_symbol_p (e
)
314 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
315 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
316 || e
->resolution
== LDPR_PREVAILING_DEF
))
317 fatal_error ("multiple prevailing defs for %qE",
318 DECL_NAME (prevailing
->decl
));
322 /* Find the single non-replaceable prevailing symbol and
323 diagnose ODR violations. */
324 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
326 if (!lto_symtab_resolve_can_prevail_p (e
))
329 /* If we have a non-replaceable definition it prevails. */
330 if (!lto_symtab_resolve_replaceable_p (e
))
334 error_at (DECL_SOURCE_LOCATION (e
->decl
),
335 "%qD has already been defined", e
->decl
);
336 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
337 "previously defined here");
345 /* Do a second round choosing one from the replaceable prevailing decls. */
346 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
348 if (!lto_symtab_resolve_can_prevail_p (e
))
351 /* Choose the first function that can prevail as prevailing. */
352 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
358 /* From variables that can prevail choose the largest one. */
360 || tree_int_cst_lt (DECL_SIZE (prevailing
->decl
),
362 /* When variables are equivalent try to chose one that has useful
363 DECL_INITIAL. This makes sense for keyed vtables that are
364 DECL_EXTERNAL but initialized. In units that do not need them
365 we replace the initializer by error_mark_node to conserve
368 We know that the vtable is keyed outside the LTO unit - otherwise
369 the keyed instance would prevail. We still can preserve useful
370 info in the initializer. */
371 || (DECL_SIZE (prevailing
->decl
) == DECL_SIZE (e
->decl
)
372 && (DECL_INITIAL (e
->decl
)
373 && DECL_INITIAL (e
->decl
) != error_mark_node
)
374 && (!DECL_INITIAL (prevailing
->decl
)
375 || DECL_INITIAL (prevailing
->decl
) == error_mark_node
)))
382 /* Merge all decls in the symbol table chain to the prevailing decl and
383 issue diagnostics about type mismatches. If DIAGNOSED_P is true
384 do not issue further diagnostics.*/
387 lto_symtab_merge_decls_2 (symtab_node
*first
, bool diagnosed_p
)
389 symtab_node
*prevailing
;
391 vec
<tree
> mismatches
= vNULL
;
395 /* Nothing to do for a single entry. */
397 if (!prevailing
->next_sharing_asm_name
)
400 /* Try to merge each entry with the prevailing one. */
401 for (e
= prevailing
->next_sharing_asm_name
;
402 e
; e
= e
->next_sharing_asm_name
)
403 if (TREE_PUBLIC (e
->decl
))
405 if (!lto_symtab_merge (prevailing
, e
)
407 mismatches
.safe_push (e
->decl
);
409 if (mismatches
.is_empty ())
412 /* Diagnose all mismatched re-declarations. */
413 FOR_EACH_VEC_ELT (mismatches
, i
, decl
)
415 if (!types_compatible_p (TREE_TYPE (prevailing
->decl
),
417 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
418 "type of %qD does not match original "
419 "declaration", decl
);
421 else if ((DECL_USER_ALIGN (prevailing
->decl
)
422 && DECL_USER_ALIGN (decl
))
423 && DECL_ALIGN (prevailing
->decl
) < DECL_ALIGN (decl
))
425 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
426 "alignment of %qD is bigger than "
427 "original declaration", decl
);
431 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
432 "previously declared here");
434 mismatches
.release ();
437 /* Helper to process the decl chain for the symbol table entry *SLOT. */
440 lto_symtab_merge_decls_1 (symtab_node
*first
)
443 symtab_node
*prevailing
;
444 bool diagnosed_p
= false;
446 if (cgraph_dump_file
)
448 fprintf (cgraph_dump_file
, "Merging nodes for %s. Candidates:\n",
450 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
451 if (TREE_PUBLIC (e
->decl
))
452 e
->dump (cgraph_dump_file
);
455 /* Compute the symbol resolutions. This is a no-op when using the
456 linker plugin and resolution was decided by the linker. */
457 prevailing
= lto_symtab_resolve_symbols (first
);
459 /* If there's not a prevailing symbol yet it's an external reference.
460 Happens a lot during ltrans. Choose the first symbol with a
461 cgraph or a varpool node. */
464 for (prevailing
= first
;
465 prevailing
; prevailing
= prevailing
->next_sharing_asm_name
)
466 if (lto_symtab_symbol_p (prevailing
))
470 /* For variables chose with a priority variant with vnode
471 attached (i.e. from unit where external declaration of
472 variable is actually used).
473 When there are multiple variants, chose one with size.
474 This is needed for C++ typeinfos, for example in
475 lto/20081204-1 there are typeifos in both units, just
476 one of them do have size. */
477 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
479 for (e
= prevailing
->next_sharing_asm_name
;
480 e
; e
= e
->next_sharing_asm_name
)
481 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing
->decl
))
482 && COMPLETE_TYPE_P (TREE_TYPE (e
->decl
))
483 && lto_symtab_symbol_p (e
))
486 /* For variables prefer the non-builtin if one is available. */
487 else if (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
)
489 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
490 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
491 && !DECL_BUILT_IN (e
->decl
)
492 && lto_symtab_symbol_p (e
))
500 symtab_prevail_in_asm_name_hash (prevailing
);
502 /* Diagnose mismatched objects. */
503 for (e
= prevailing
->next_sharing_asm_name
;
504 e
; e
= e
->next_sharing_asm_name
)
506 if (TREE_CODE (prevailing
->decl
)
507 == TREE_CODE (e
->decl
))
509 if (!lto_symtab_symbol_p (e
))
512 switch (TREE_CODE (prevailing
->decl
))
515 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
516 error_at (DECL_SOURCE_LOCATION (e
->decl
),
517 "variable %qD redeclared as function",
522 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
523 error_at (DECL_SOURCE_LOCATION (e
->decl
),
524 "function %qD redeclared as variable",
535 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
536 "previously declared here");
538 /* Merge the chain to the single prevailing decl and diagnose
540 lto_symtab_merge_decls_2 (prevailing
, diagnosed_p
);
542 if (cgraph_dump_file
)
544 fprintf (cgraph_dump_file
, "After resolution:\n");
545 for (e
= prevailing
; e
; e
= e
->next_sharing_asm_name
)
546 e
->dump (cgraph_dump_file
);
550 /* Resolve and merge all symbol table chains to a prevailing decl. */
553 lto_symtab_merge_decls (void)
557 /* Populate assembler name hash. */
558 symtab_initialize_asm_name_hash ();
560 FOR_EACH_SYMBOL (node
)
561 if (!node
->previous_sharing_asm_name
562 && node
->next_sharing_asm_name
)
563 lto_symtab_merge_decls_1 (node
);
566 /* Helper to process the decl chain for the symbol table entry *SLOT. */
569 lto_symtab_merge_symbols_1 (symtab_node
*prevailing
)
574 /* Replace the cgraph node of each entry with the prevailing one. */
575 for (e
= prevailing
->next_sharing_asm_name
; e
;
578 next
= e
->next_sharing_asm_name
;
580 if (!lto_symtab_symbol_p (e
))
582 cgraph_node
*ce
= dyn_cast
<cgraph_node
*> (e
);
583 if (ce
&& !DECL_BUILT_IN (e
->decl
))
584 lto_cgraph_replace_node (ce
, dyn_cast
<cgraph_node
*> (prevailing
));
585 if (varpool_node
*ve
= dyn_cast
<varpool_node
*> (e
))
586 lto_varpool_replace_node (ve
, dyn_cast
<varpool_node
*> (prevailing
));
592 /* Merge cgraph nodes according to the symbol merging done by
593 lto_symtab_merge_decls. */
596 lto_symtab_merge_symbols (void)
602 symtab_initialize_asm_name_hash ();
604 /* Do the actual merging.
605 At this point we invalidate hash translating decls into symtab nodes
606 because after removing one of duplicate decls the hash is not correcly
607 updated to the ohter dupliate. */
608 FOR_EACH_SYMBOL (node
)
609 if (lto_symtab_symbol_p (node
)
610 && node
->next_sharing_asm_name
611 && !node
->previous_sharing_asm_name
)
612 lto_symtab_merge_symbols_1 (node
);
614 /* Resolve weakref aliases whose target are now in the compilation unit.
615 also re-populate the hash translating decls into symtab nodes*/
616 FOR_EACH_SYMBOL (node
)
618 cgraph_node
*cnode
, *cnode2
;
622 if (!node
->analyzed
&& node
->alias_target
)
624 symtab_node
*tgt
= symtab_node_for_asm (node
->alias_target
);
625 gcc_assert (node
->weakref
);
627 node
->resolve_alias (tgt
);
631 if (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
633 || cnode
->clone_of
->decl
!= cnode
->decl
)
635 /* Builtins are not merged via decl merging. It is however
636 possible that tree merging unified the declaration. We
637 do not want duplicate entries in symbol table. */
638 if (cnode
&& DECL_BUILT_IN (node
->decl
)
639 && (cnode2
= cgraph_node::get (node
->decl
))
641 lto_cgraph_replace_node (cnode2
, cnode
);
643 /* The user defined assembler variables are also not unified by their
644 symbol name (since it is irrelevant), but we need to unify symbol
645 nodes if tree merging occured. */
646 if ((vnode
= dyn_cast
<varpool_node
*> (node
))
647 && DECL_HARD_REGISTER (vnode
->decl
)
648 && (node2
= symtab_node::get (vnode
->decl
))
650 lto_varpool_replace_node (dyn_cast
<varpool_node
*> (node2
),
654 /* Abstract functions may have duplicated cgraph nodes attached;
656 else if (cnode
&& DECL_ABSTRACT (cnode
->decl
)
657 && (cnode2
= cgraph_node::get (node
->decl
))
661 node
->decl
->decl_with_vis
.symtab_node
= node
;
667 /* Given the decl DECL, return the prevailing decl with the same name. */
670 lto_symtab_prevailing_decl (tree decl
)
674 /* Builtins and local symbols are their own prevailing decl. */
675 if ((!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
)) || is_builtin_fn (decl
))
678 /* DECL_ABSTRACTs are their own prevailng decl. */
679 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT (decl
))
682 /* Likewise builtins are their own prevailing decl. This preserves
683 non-builtin vs. builtin uses from compile-time. */
684 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
687 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
688 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
690 /* Walk through the list of candidates and return the one we merged to. */
691 ret
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl
));