2 Copyright (C) 2009-2013 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"
29 #include "plugin-api.h"
30 #include "lto-streamer.h"
31 #include "ipa-utils.h"
33 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
34 all edges and removing the old node. */
37 lto_cgraph_replace_node (struct cgraph_node
*node
,
38 struct cgraph_node
*prevailing_node
)
40 struct cgraph_edge
*e
, *next
;
45 fprintf (cgraph_dump_file
, "Replacing cgraph node %s/%i by %s/%i"
47 cgraph_node_name (node
), node
->order
,
48 cgraph_node_name (prevailing_node
),
49 prevailing_node
->order
,
50 IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
)
51 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
)))));
54 /* Merge node flags. */
55 if (node
->force_output
)
56 cgraph_mark_force_output_node (prevailing_node
);
57 if (node
->address_taken
)
59 gcc_assert (!prevailing_node
->global
.inlined_to
);
60 cgraph_mark_address_taken_node (prevailing_node
);
63 /* Redirect all incoming edges. */
65 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node
->decl
)),
66 TREE_TYPE (TREE_TYPE (node
->decl
)));
67 for (e
= node
->callers
; e
; e
= next
)
69 next
= e
->next_caller
;
70 cgraph_redirect_edge_callee (e
, prevailing_node
);
71 /* If there is a mismatch between the supposed callee return type and
72 the real one do not attempt to inline this function.
73 ??? We really need a way to match function signatures for ABI
74 compatibility and perform related promotions at inlining time. */
76 e
->call_stmt_cannot_inline_p
= 1;
78 /* Redirect incomming references. */
79 ipa_clone_referring (prevailing_node
, &node
->ref_list
);
81 ipa_merge_profiles (prevailing_node
, node
);
82 lto_free_function_in_decl_state_for_node (node
);
84 if (node
->decl
!= prevailing_node
->decl
)
85 cgraph_release_function_body (node
);
87 /* Finally remove the replaced node. */
88 cgraph_remove_node (node
);
91 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
92 all edges and removing the old node. */
95 lto_varpool_replace_node (struct varpool_node
*vnode
,
96 struct varpool_node
*prevailing_node
)
98 gcc_assert (!vnode
->definition
|| prevailing_node
->definition
);
99 gcc_assert (!vnode
->analyzed
|| prevailing_node
->analyzed
);
101 ipa_clone_referring (prevailing_node
, &vnode
->ref_list
);
103 /* Be sure we can garbage collect the initializer. */
104 if (DECL_INITIAL (vnode
->decl
)
105 && vnode
->decl
!= prevailing_node
->decl
)
106 DECL_INITIAL (vnode
->decl
) = error_mark_node
;
107 /* Finally remove the replaced node. */
108 varpool_remove_node (vnode
);
111 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
112 Return false if the symbols are not fully compatible and a diagnostic
113 should be emitted. */
116 lto_symtab_merge (symtab_node prevailing
, symtab_node entry
)
118 tree prevailing_decl
= prevailing
->decl
;
119 tree decl
= entry
->decl
;
120 tree prevailing_type
, type
;
122 if (prevailing_decl
== decl
)
125 /* Merge decl state in both directions, we may still end up using
127 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
128 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
130 /* The linker may ask us to combine two incompatible symbols.
131 Detect this case and notify the caller of required diagnostics. */
133 if (TREE_CODE (decl
) == FUNCTION_DECL
)
135 if (!types_compatible_p (TREE_TYPE (prevailing_decl
),
137 /* If we don't have a merged type yet...sigh. The linker
138 wouldn't complain if the types were mismatched, so we
139 probably shouldn't either. Just use the type from
140 whichever decl appears to be associated with the
141 definition. If for some odd reason neither decl is, the
148 /* Now we exclusively deal with VAR_DECLs. */
150 /* Sharing a global symbol is a strong hint that two types are
151 compatible. We could use this information to complete
152 incomplete pointed-to types more aggressively here, ignoring
153 mismatches in both field and tag names. It's difficult though
154 to guarantee that this does not have side-effects on merging
155 more compatible types from other translation units though. */
157 /* We can tolerate differences in type qualification, the
158 qualification of the prevailing definition will prevail.
159 ??? In principle we might want to only warn for structurally
160 incompatible types here, but unless we have protective measures
161 for TBAA in place that would hide useful information. */
162 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
163 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
165 if (!types_compatible_p (prevailing_type
, type
))
167 if (COMPLETE_TYPE_P (type
))
170 /* If type is incomplete then avoid warnings in the cases
171 that TBAA handles just fine. */
173 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
176 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
178 tree tem1
= TREE_TYPE (prevailing_type
);
179 tree tem2
= TREE_TYPE (type
);
180 while (TREE_CODE (tem1
) == ARRAY_TYPE
181 && TREE_CODE (tem2
) == ARRAY_TYPE
)
183 tem1
= TREE_TYPE (tem1
);
184 tem2
= TREE_TYPE (tem2
);
187 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
190 if (!types_compatible_p (tem1
, tem2
))
194 /* Fallthru. Compatible enough. */
197 /* ??? We might want to emit a warning here if type qualification
198 differences were spotted. Do not do this unconditionally though. */
200 /* There is no point in comparing too many details of the decls here.
201 The type compatibility checks or the completing of types has properly
202 dealt with most issues. */
204 /* The following should all not invoke fatal errors as in non-LTO
205 mode the linker wouldn't complain either. Just emit warnings. */
207 /* Report a warning if user-specified alignments do not match. */
208 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
209 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
215 /* Return true if the symtab entry E can be replaced by another symtab
219 lto_symtab_resolve_replaceable_p (symtab_node e
)
221 if (DECL_EXTERNAL (e
->decl
)
222 || DECL_COMDAT (e
->decl
)
223 || DECL_ONE_ONLY (e
->decl
)
224 || DECL_WEAK (e
->decl
))
227 if (TREE_CODE (e
->decl
) == VAR_DECL
)
228 return (DECL_COMMON (e
->decl
)
229 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
234 /* Return true, if the symbol E should be resolved by lto-symtab.
235 Those are all external symbols and all real symbols that are not static (we
236 handle renaming of static later in partitioning). */
239 lto_symtab_symbol_p (symtab_node e
)
241 if (!TREE_PUBLIC (e
->decl
) && !DECL_EXTERNAL (e
->decl
))
243 return symtab_real_symbol_p (e
);
246 /* Return true if the symtab entry E can be the prevailing one. */
249 lto_symtab_resolve_can_prevail_p (symtab_node e
)
251 if (!lto_symtab_symbol_p (e
))
254 /* The C++ frontend ends up neither setting TREE_STATIC nor
255 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
256 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
257 if (DECL_EXTERNAL (e
->decl
))
260 return e
->definition
;
263 /* Resolve the symbol with the candidates in the chain *SLOT and store
264 their resolutions. */
267 lto_symtab_resolve_symbols (symtab_node first
)
270 symtab_node prevailing
= NULL
;
272 /* Always set e->node so that edges are updated to reflect decl merging. */
273 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
274 if (lto_symtab_symbol_p (e
)
275 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
276 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
277 || e
->resolution
== LDPR_PREVAILING_DEF
))
283 /* If the chain is already resolved there is nothing else to do. */
286 /* Assert it's the only one. */
287 for (e
= prevailing
->next_sharing_asm_name
; e
; e
= e
->next_sharing_asm_name
)
288 if (lto_symtab_symbol_p (e
)
289 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
290 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
291 || e
->resolution
== LDPR_PREVAILING_DEF
))
292 fatal_error ("multiple prevailing defs for %qE",
293 DECL_NAME (prevailing
->decl
));
297 /* Find the single non-replaceable prevailing symbol and
298 diagnose ODR violations. */
299 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
301 if (!lto_symtab_resolve_can_prevail_p (e
))
304 /* If we have a non-replaceable definition it prevails. */
305 if (!lto_symtab_resolve_replaceable_p (e
))
309 error_at (DECL_SOURCE_LOCATION (e
->decl
),
310 "%qD has already been defined", e
->decl
);
311 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
312 "previously defined here");
320 /* Do a second round choosing one from the replaceable prevailing decls. */
321 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
323 if (!lto_symtab_resolve_can_prevail_p (e
))
326 /* Choose the first function that can prevail as prevailing. */
327 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
333 /* From variables that can prevail choose the largest one. */
335 || tree_int_cst_lt (DECL_SIZE (prevailing
->decl
),
337 /* When variables are equivalent try to chose one that has useful
338 DECL_INITIAL. This makes sense for keyed vtables that are
339 DECL_EXTERNAL but initialized. In units that do not need them
340 we replace the initializer by error_mark_node to conserve
343 We know that the vtable is keyed outside the LTO unit - otherwise
344 the keyed instance would prevail. We still can preserve useful
345 info in the initializer. */
346 || (DECL_SIZE (prevailing
->decl
) == DECL_SIZE (e
->decl
)
347 && (DECL_INITIAL (e
->decl
)
348 && DECL_INITIAL (e
->decl
) != error_mark_node
)
349 && (!DECL_INITIAL (prevailing
->decl
)
350 || DECL_INITIAL (prevailing
->decl
) == error_mark_node
)))
357 /* Merge all decls in the symbol table chain to the prevailing decl and
358 issue diagnostics about type mismatches. If DIAGNOSED_P is true
359 do not issue further diagnostics.*/
362 lto_symtab_merge_decls_2 (symtab_node first
, bool diagnosed_p
)
364 symtab_node prevailing
, e
;
365 vec
<tree
> mismatches
= vNULL
;
369 /* Nothing to do for a single entry. */
371 if (!prevailing
->next_sharing_asm_name
)
374 /* Try to merge each entry with the prevailing one. */
375 for (e
= prevailing
->next_sharing_asm_name
;
376 e
; e
= e
->next_sharing_asm_name
)
377 if (TREE_PUBLIC (e
->decl
))
379 if (!lto_symtab_merge (prevailing
, e
)
381 mismatches
.safe_push (e
->decl
);
383 if (mismatches
.is_empty ())
386 /* Diagnose all mismatched re-declarations. */
387 FOR_EACH_VEC_ELT (mismatches
, i
, decl
)
389 if (!types_compatible_p (TREE_TYPE (prevailing
->decl
),
391 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
392 "type of %qD does not match original "
393 "declaration", decl
);
395 else if ((DECL_USER_ALIGN (prevailing
->decl
)
396 && DECL_USER_ALIGN (decl
))
397 && DECL_ALIGN (prevailing
->decl
) < DECL_ALIGN (decl
))
399 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
400 "alignment of %qD is bigger than "
401 "original declaration", decl
);
405 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
406 "previously declared here");
408 mismatches
.release ();
411 /* Helper to process the decl chain for the symbol table entry *SLOT. */
414 lto_symtab_merge_decls_1 (symtab_node first
)
416 symtab_node e
, prevailing
;
417 bool diagnosed_p
= false;
419 if (cgraph_dump_file
)
421 fprintf (cgraph_dump_file
, "Merging nodes for %s. Candidates:\n",
422 symtab_node_asm_name (first
));
423 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
424 if (TREE_PUBLIC (e
->decl
))
425 dump_symtab_node (cgraph_dump_file
, e
);
428 /* Compute the symbol resolutions. This is a no-op when using the
429 linker plugin and resolution was decided by the linker. */
430 prevailing
= lto_symtab_resolve_symbols (first
);
432 /* If there's not a prevailing symbol yet it's an external reference.
433 Happens a lot during ltrans. Choose the first symbol with a
434 cgraph or a varpool node. */
438 /* For variables chose with a priority variant with vnode
439 attached (i.e. from unit where external declaration of
440 variable is actually used).
441 When there are multiple variants, chose one with size.
442 This is needed for C++ typeinfos, for example in
443 lto/20081204-1 there are typeifos in both units, just
444 one of them do have size. */
445 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
447 for (e
= prevailing
->next_sharing_asm_name
;
448 e
; e
= e
->next_sharing_asm_name
)
449 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing
->decl
))
450 && COMPLETE_TYPE_P (TREE_TYPE (e
->decl
))
451 && lto_symtab_symbol_p (e
))
454 /* For variables prefer the non-builtin if one is available. */
455 else if (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
)
457 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
458 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
459 && !DECL_BUILT_IN (e
->decl
)
460 && lto_symtab_symbol_p (e
))
468 symtab_prevail_in_asm_name_hash (prevailing
);
470 /* Diagnose mismatched objects. */
471 for (e
= prevailing
->next_sharing_asm_name
;
472 e
; e
= e
->next_sharing_asm_name
)
474 if (TREE_CODE (prevailing
->decl
)
475 == TREE_CODE (e
->decl
))
477 if (!lto_symtab_symbol_p (e
))
480 switch (TREE_CODE (prevailing
->decl
))
483 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
484 error_at (DECL_SOURCE_LOCATION (e
->decl
),
485 "variable %qD redeclared as function",
490 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
491 error_at (DECL_SOURCE_LOCATION (e
->decl
),
492 "function %qD redeclared as variable",
503 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
504 "previously declared here");
506 /* Merge the chain to the single prevailing decl and diagnose
508 lto_symtab_merge_decls_2 (prevailing
, diagnosed_p
);
510 if (cgraph_dump_file
)
512 fprintf (cgraph_dump_file
, "After resolution:\n");
513 for (e
= prevailing
; e
; e
= e
->next_sharing_asm_name
)
514 dump_symtab_node (cgraph_dump_file
, e
);
518 /* Resolve and merge all symbol table chains to a prevailing decl. */
521 lto_symtab_merge_decls (void)
525 /* Populate assembler name hash. */
526 symtab_initialize_asm_name_hash ();
528 FOR_EACH_SYMBOL (node
)
529 if (!node
->previous_sharing_asm_name
530 && node
->next_sharing_asm_name
)
531 lto_symtab_merge_decls_1 (node
);
534 /* Helper to process the decl chain for the symbol table entry *SLOT. */
537 lto_symtab_merge_symbols_1 (symtab_node prevailing
)
541 /* Replace the cgraph node of each entry with the prevailing one. */
542 for (e
= prevailing
->next_sharing_asm_name
; e
;
545 next
= e
->next_sharing_asm_name
;
547 if (!lto_symtab_symbol_p (e
))
549 cgraph_node
*ce
= dyn_cast
<cgraph_node
> (e
);
550 if (ce
&& !DECL_BUILT_IN (e
->decl
))
551 lto_cgraph_replace_node (ce
, cgraph (prevailing
));
552 if (varpool_node
*ve
= dyn_cast
<varpool_node
> (e
))
553 lto_varpool_replace_node (ve
, varpool (prevailing
));
559 /* Merge cgraph nodes according to the symbol merging done by
560 lto_symtab_merge_decls. */
563 lto_symtab_merge_symbols (void)
569 symtab_initialize_asm_name_hash ();
571 /* Do the actual merging.
572 At this point we invalidate hash translating decls into symtab nodes
573 because after removing one of duplicate decls the hash is not correcly
574 updated to the ohter dupliate. */
575 FOR_EACH_SYMBOL (node
)
576 if (lto_symtab_symbol_p (node
)
577 && node
->next_sharing_asm_name
578 && !node
->previous_sharing_asm_name
)
579 lto_symtab_merge_symbols_1 (node
);
581 /* Resolve weakref aliases whose target are now in the compilation unit.
582 also re-populate the hash translating decls into symtab nodes*/
583 FOR_EACH_SYMBOL (node
)
585 cgraph_node
*cnode
, *cnode2
;
589 if (!node
->analyzed
&& node
->alias_target
)
591 symtab_node tgt
= symtab_node_for_asm (node
->alias_target
);
592 gcc_assert (node
->weakref
);
594 symtab_resolve_alias (node
, tgt
);
598 if (!(cnode
= dyn_cast
<cgraph_node
> (node
))
600 || cnode
->clone_of
->decl
!= cnode
->decl
)
602 /* Builtins are not merged via decl merging. It is however
603 possible that tree merging unified the declaration. We
604 do not want duplicate entries in symbol table. */
605 if (cnode
&& DECL_BUILT_IN (node
->decl
)
606 && (cnode2
= cgraph_get_node (node
->decl
))
608 lto_cgraph_replace_node (cnode2
, cnode
);
610 /* The user defined assembler variables are also not unified by their
611 symbol name (since it is irrelevant), but we need to unify symbol
612 nodes if tree merging occured. */
613 if ((vnode
= dyn_cast
<varpool_node
> (node
))
614 && DECL_HARD_REGISTER (vnode
->decl
)
615 && (node2
= symtab_get_node (vnode
->decl
))
617 lto_varpool_replace_node (dyn_cast
<varpool_node
> (node2
),
621 /* Abstract functions may have duplicated cgraph nodes attached;
623 else if (cnode
&& DECL_ABSTRACT (cnode
->decl
)
624 && (cnode2
= cgraph_get_node (node
->decl
))
626 cgraph_remove_node (cnode2
);
628 symtab_insert_node_to_hashtable (node
);
634 /* Given the decl DECL, return the prevailing decl with the same name. */
637 lto_symtab_prevailing_decl (tree decl
)
641 /* Builtins and local symbols are their own prevailing decl. */
642 if ((!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
)) || is_builtin_fn (decl
))
645 /* DECL_ABSTRACTs are their own prevailng decl. */
646 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT (decl
))
649 /* Likewise builtins are their own prevailing decl. This preserves
650 non-builtin vs. builtin uses from compile-time. */
651 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
654 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
655 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
657 /* Walk through the list of candidates and return the one we merged to. */
658 ret
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl
));