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"
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"
38 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
39 all edges and removing the old node. */
42 lto_cgraph_replace_node (struct cgraph_node
*node
,
43 struct cgraph_node
*prevailing_node
)
45 struct cgraph_edge
*e
, *next
;
50 fprintf (cgraph_dump_file
, "Replacing cgraph node %s/%i by %s/%i"
52 node
->name (), node
->order
,
53 prevailing_node
->name (),
54 prevailing_node
->order
,
55 IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
)
56 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
)))));
59 /* Merge node flags. */
60 if (node
->force_output
)
61 cgraph_mark_force_output_node (prevailing_node
);
62 if (node
->address_taken
)
64 gcc_assert (!prevailing_node
->global
.inlined_to
);
65 cgraph_mark_address_taken_node (prevailing_node
);
68 /* Redirect all incoming edges. */
70 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node
->decl
)),
71 TREE_TYPE (TREE_TYPE (node
->decl
)));
72 for (e
= node
->callers
; e
; e
= next
)
74 next
= e
->next_caller
;
75 cgraph_redirect_edge_callee (e
, prevailing_node
);
76 /* If there is a mismatch between the supposed callee return type and
77 the real one do not attempt to inline this function.
78 ??? We really need a way to match function signatures for ABI
79 compatibility and perform related promotions at inlining time. */
81 e
->call_stmt_cannot_inline_p
= 1;
83 /* Redirect incomming references. */
84 ipa_clone_referring (prevailing_node
, &node
->ref_list
);
86 ipa_merge_profiles (prevailing_node
, node
);
87 lto_free_function_in_decl_state_for_node (node
);
89 if (node
->decl
!= prevailing_node
->decl
)
90 cgraph_release_function_body (node
);
92 /* Time profile merging */
93 if (node
->tp_first_run
)
94 prevailing_node
->tp_first_run
= prevailing_node
->tp_first_run
?
95 MIN (prevailing_node
->tp_first_run
, node
->tp_first_run
) :
98 /* Finally remove the replaced node. */
99 cgraph_remove_node (node
);
102 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
103 all edges and removing the old node. */
106 lto_varpool_replace_node (varpool_node
*vnode
,
107 varpool_node
*prevailing_node
)
109 gcc_assert (!vnode
->definition
|| prevailing_node
->definition
);
110 gcc_assert (!vnode
->analyzed
|| prevailing_node
->analyzed
);
112 ipa_clone_referring (prevailing_node
, &vnode
->ref_list
);
114 /* Be sure we can garbage collect the initializer. */
115 if (DECL_INITIAL (vnode
->decl
)
116 && vnode
->decl
!= prevailing_node
->decl
)
117 DECL_INITIAL (vnode
->decl
) = error_mark_node
;
118 /* Finally remove the replaced node. */
119 varpool_remove_node (vnode
);
122 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
123 Return false if the symbols are not fully compatible and a diagnostic
124 should be emitted. */
127 lto_symtab_merge (symtab_node
*prevailing
, symtab_node
*entry
)
129 tree prevailing_decl
= prevailing
->decl
;
130 tree decl
= entry
->decl
;
131 tree prevailing_type
, type
;
133 if (prevailing_decl
== decl
)
136 /* Merge decl state in both directions, we may still end up using
138 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
139 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
141 /* The linker may ask us to combine two incompatible symbols.
142 Detect this case and notify the caller of required diagnostics. */
144 if (TREE_CODE (decl
) == FUNCTION_DECL
)
146 if (!types_compatible_p (TREE_TYPE (prevailing_decl
),
148 /* If we don't have a merged type yet...sigh. The linker
149 wouldn't complain if the types were mismatched, so we
150 probably shouldn't either. Just use the type from
151 whichever decl appears to be associated with the
152 definition. If for some odd reason neither decl is, the
159 /* Now we exclusively deal with VAR_DECLs. */
161 /* Sharing a global symbol is a strong hint that two types are
162 compatible. We could use this information to complete
163 incomplete pointed-to types more aggressively here, ignoring
164 mismatches in both field and tag names. It's difficult though
165 to guarantee that this does not have side-effects on merging
166 more compatible types from other translation units though. */
168 /* We can tolerate differences in type qualification, the
169 qualification of the prevailing definition will prevail.
170 ??? In principle we might want to only warn for structurally
171 incompatible types here, but unless we have protective measures
172 for TBAA in place that would hide useful information. */
173 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
174 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
176 if (!types_compatible_p (prevailing_type
, type
))
178 if (COMPLETE_TYPE_P (type
))
181 /* If type is incomplete then avoid warnings in the cases
182 that TBAA handles just fine. */
184 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
187 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
189 tree tem1
= TREE_TYPE (prevailing_type
);
190 tree tem2
= TREE_TYPE (type
);
191 while (TREE_CODE (tem1
) == ARRAY_TYPE
192 && TREE_CODE (tem2
) == ARRAY_TYPE
)
194 tem1
= TREE_TYPE (tem1
);
195 tem2
= TREE_TYPE (tem2
);
198 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
201 if (!types_compatible_p (tem1
, tem2
))
205 /* Fallthru. Compatible enough. */
208 /* ??? We might want to emit a warning here if type qualification
209 differences were spotted. Do not do this unconditionally though. */
211 /* There is no point in comparing too many details of the decls here.
212 The type compatibility checks or the completing of types has properly
213 dealt with most issues. */
215 /* The following should all not invoke fatal errors as in non-LTO
216 mode the linker wouldn't complain either. Just emit warnings. */
218 /* Report a warning if user-specified alignments do not match. */
219 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
220 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
226 /* Return true if the symtab entry E can be replaced by another symtab
230 lto_symtab_resolve_replaceable_p (symtab_node
*e
)
232 if (DECL_EXTERNAL (e
->decl
)
233 || DECL_COMDAT (e
->decl
)
234 || DECL_ONE_ONLY (e
->decl
)
235 || DECL_WEAK (e
->decl
))
238 if (TREE_CODE (e
->decl
) == VAR_DECL
)
239 return (DECL_COMMON (e
->decl
)
240 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
245 /* Return true, if the symbol E should be resolved by lto-symtab.
246 Those are all external symbols and all real symbols that are not static (we
247 handle renaming of static later in partitioning). */
250 lto_symtab_symbol_p (symtab_node
*e
)
252 if (!TREE_PUBLIC (e
->decl
) && !DECL_EXTERNAL (e
->decl
))
254 return symtab_real_symbol_p (e
);
257 /* Return true if the symtab entry E can be the prevailing one. */
260 lto_symtab_resolve_can_prevail_p (symtab_node
*e
)
262 if (!lto_symtab_symbol_p (e
))
265 /* The C++ frontend ends up neither setting TREE_STATIC nor
266 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
267 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
268 if (DECL_EXTERNAL (e
->decl
))
271 return e
->definition
;
274 /* Resolve the symbol with the candidates in the chain *SLOT and store
275 their resolutions. */
278 lto_symtab_resolve_symbols (symtab_node
*first
)
281 symtab_node
*prevailing
= NULL
;
283 /* Always set e->node so that edges are updated to reflect decl merging. */
284 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
285 if (lto_symtab_symbol_p (e
)
286 && (e
->resolution
== LDPR_PREVAILING_DEF_IRONLY
287 || e
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
288 || e
->resolution
== LDPR_PREVAILING_DEF
))
294 /* If the chain is already resolved there is nothing else to do. */
297 /* Assert it's the only one. */
298 for (e
= prevailing
->next_sharing_asm_name
; 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
))
303 fatal_error ("multiple prevailing defs for %qE",
304 DECL_NAME (prevailing
->decl
));
308 /* Find the single non-replaceable prevailing symbol and
309 diagnose ODR violations. */
310 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
312 if (!lto_symtab_resolve_can_prevail_p (e
))
315 /* If we have a non-replaceable definition it prevails. */
316 if (!lto_symtab_resolve_replaceable_p (e
))
320 error_at (DECL_SOURCE_LOCATION (e
->decl
),
321 "%qD has already been defined", e
->decl
);
322 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
323 "previously defined here");
331 /* Do a second round choosing one from the replaceable prevailing decls. */
332 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
334 if (!lto_symtab_resolve_can_prevail_p (e
))
337 /* Choose the first function that can prevail as prevailing. */
338 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
344 /* From variables that can prevail choose the largest one. */
346 || tree_int_cst_lt (DECL_SIZE (prevailing
->decl
),
348 /* When variables are equivalent try to chose one that has useful
349 DECL_INITIAL. This makes sense for keyed vtables that are
350 DECL_EXTERNAL but initialized. In units that do not need them
351 we replace the initializer by error_mark_node to conserve
354 We know that the vtable is keyed outside the LTO unit - otherwise
355 the keyed instance would prevail. We still can preserve useful
356 info in the initializer. */
357 || (DECL_SIZE (prevailing
->decl
) == DECL_SIZE (e
->decl
)
358 && (DECL_INITIAL (e
->decl
)
359 && DECL_INITIAL (e
->decl
) != error_mark_node
)
360 && (!DECL_INITIAL (prevailing
->decl
)
361 || DECL_INITIAL (prevailing
->decl
) == error_mark_node
)))
368 /* Merge all decls in the symbol table chain to the prevailing decl and
369 issue diagnostics about type mismatches. If DIAGNOSED_P is true
370 do not issue further diagnostics.*/
373 lto_symtab_merge_decls_2 (symtab_node
*first
, bool diagnosed_p
)
375 symtab_node
*prevailing
;
377 vec
<tree
> mismatches
= vNULL
;
381 /* Nothing to do for a single entry. */
383 if (!prevailing
->next_sharing_asm_name
)
386 /* Try to merge each entry with the prevailing one. */
387 for (e
= prevailing
->next_sharing_asm_name
;
388 e
; e
= e
->next_sharing_asm_name
)
389 if (TREE_PUBLIC (e
->decl
))
391 if (!lto_symtab_merge (prevailing
, e
)
393 mismatches
.safe_push (e
->decl
);
395 if (mismatches
.is_empty ())
398 /* Diagnose all mismatched re-declarations. */
399 FOR_EACH_VEC_ELT (mismatches
, i
, decl
)
401 if (!types_compatible_p (TREE_TYPE (prevailing
->decl
),
403 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
404 "type of %qD does not match original "
405 "declaration", decl
);
407 else if ((DECL_USER_ALIGN (prevailing
->decl
)
408 && DECL_USER_ALIGN (decl
))
409 && DECL_ALIGN (prevailing
->decl
) < DECL_ALIGN (decl
))
411 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
412 "alignment of %qD is bigger than "
413 "original declaration", decl
);
417 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
418 "previously declared here");
420 mismatches
.release ();
423 /* Helper to process the decl chain for the symbol table entry *SLOT. */
426 lto_symtab_merge_decls_1 (symtab_node
*first
)
429 symtab_node
*prevailing
;
430 bool diagnosed_p
= false;
432 if (cgraph_dump_file
)
434 fprintf (cgraph_dump_file
, "Merging nodes for %s. Candidates:\n",
436 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
437 if (TREE_PUBLIC (e
->decl
))
438 dump_symtab_node (cgraph_dump_file
, e
);
441 /* Compute the symbol resolutions. This is a no-op when using the
442 linker plugin and resolution was decided by the linker. */
443 prevailing
= lto_symtab_resolve_symbols (first
);
445 /* If there's not a prevailing symbol yet it's an external reference.
446 Happens a lot during ltrans. Choose the first symbol with a
447 cgraph or a varpool node. */
451 /* For variables chose with a priority variant with vnode
452 attached (i.e. from unit where external declaration of
453 variable is actually used).
454 When there are multiple variants, chose one with size.
455 This is needed for C++ typeinfos, for example in
456 lto/20081204-1 there are typeifos in both units, just
457 one of them do have size. */
458 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
460 for (e
= prevailing
->next_sharing_asm_name
;
461 e
; e
= e
->next_sharing_asm_name
)
462 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing
->decl
))
463 && COMPLETE_TYPE_P (TREE_TYPE (e
->decl
))
464 && lto_symtab_symbol_p (e
))
467 /* For variables prefer the non-builtin if one is available. */
468 else if (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
)
470 for (e
= first
; e
; e
= e
->next_sharing_asm_name
)
471 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
472 && !DECL_BUILT_IN (e
->decl
)
473 && lto_symtab_symbol_p (e
))
481 symtab_prevail_in_asm_name_hash (prevailing
);
483 /* Diagnose mismatched objects. */
484 for (e
= prevailing
->next_sharing_asm_name
;
485 e
; e
= e
->next_sharing_asm_name
)
487 if (TREE_CODE (prevailing
->decl
)
488 == TREE_CODE (e
->decl
))
490 if (!lto_symtab_symbol_p (e
))
493 switch (TREE_CODE (prevailing
->decl
))
496 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
497 error_at (DECL_SOURCE_LOCATION (e
->decl
),
498 "variable %qD redeclared as function",
503 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
504 error_at (DECL_SOURCE_LOCATION (e
->decl
),
505 "function %qD redeclared as variable",
516 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
517 "previously declared here");
519 /* Merge the chain to the single prevailing decl and diagnose
521 lto_symtab_merge_decls_2 (prevailing
, diagnosed_p
);
523 if (cgraph_dump_file
)
525 fprintf (cgraph_dump_file
, "After resolution:\n");
526 for (e
= prevailing
; e
; e
= e
->next_sharing_asm_name
)
527 dump_symtab_node (cgraph_dump_file
, e
);
531 /* Resolve and merge all symbol table chains to a prevailing decl. */
534 lto_symtab_merge_decls (void)
538 /* Populate assembler name hash. */
539 symtab_initialize_asm_name_hash ();
541 FOR_EACH_SYMBOL (node
)
542 if (!node
->previous_sharing_asm_name
543 && node
->next_sharing_asm_name
)
544 lto_symtab_merge_decls_1 (node
);
547 /* Helper to process the decl chain for the symbol table entry *SLOT. */
550 lto_symtab_merge_symbols_1 (symtab_node
*prevailing
)
555 /* Replace the cgraph node of each entry with the prevailing one. */
556 for (e
= prevailing
->next_sharing_asm_name
; e
;
559 next
= e
->next_sharing_asm_name
;
561 if (!lto_symtab_symbol_p (e
))
563 cgraph_node
*ce
= dyn_cast
<cgraph_node
> (e
);
564 if (ce
&& !DECL_BUILT_IN (e
->decl
))
565 lto_cgraph_replace_node (ce
, cgraph (prevailing
));
566 if (varpool_node
*ve
= dyn_cast
<varpool_node
> (e
))
567 lto_varpool_replace_node (ve
, varpool (prevailing
));
573 /* Merge cgraph nodes according to the symbol merging done by
574 lto_symtab_merge_decls. */
577 lto_symtab_merge_symbols (void)
583 symtab_initialize_asm_name_hash ();
585 /* Do the actual merging.
586 At this point we invalidate hash translating decls into symtab nodes
587 because after removing one of duplicate decls the hash is not correcly
588 updated to the ohter dupliate. */
589 FOR_EACH_SYMBOL (node
)
590 if (lto_symtab_symbol_p (node
)
591 && node
->next_sharing_asm_name
592 && !node
->previous_sharing_asm_name
)
593 lto_symtab_merge_symbols_1 (node
);
595 /* Resolve weakref aliases whose target are now in the compilation unit.
596 also re-populate the hash translating decls into symtab nodes*/
597 FOR_EACH_SYMBOL (node
)
599 cgraph_node
*cnode
, *cnode2
;
603 if (!node
->analyzed
&& node
->alias_target
)
605 symtab_node
*tgt
= symtab_node_for_asm (node
->alias_target
);
606 gcc_assert (node
->weakref
);
608 symtab_resolve_alias (node
, tgt
);
612 if (!(cnode
= dyn_cast
<cgraph_node
> (node
))
614 || cnode
->clone_of
->decl
!= cnode
->decl
)
616 /* Builtins are not merged via decl merging. It is however
617 possible that tree merging unified the declaration. We
618 do not want duplicate entries in symbol table. */
619 if (cnode
&& DECL_BUILT_IN (node
->decl
)
620 && (cnode2
= cgraph_get_node (node
->decl
))
622 lto_cgraph_replace_node (cnode2
, cnode
);
624 /* The user defined assembler variables are also not unified by their
625 symbol name (since it is irrelevant), but we need to unify symbol
626 nodes if tree merging occured. */
627 if ((vnode
= dyn_cast
<varpool_node
> (node
))
628 && DECL_HARD_REGISTER (vnode
->decl
)
629 && (node2
= symtab_get_node (vnode
->decl
))
631 lto_varpool_replace_node (dyn_cast
<varpool_node
> (node2
),
635 /* Abstract functions may have duplicated cgraph nodes attached;
637 else if (cnode
&& DECL_ABSTRACT (cnode
->decl
)
638 && (cnode2
= cgraph_get_node (node
->decl
))
640 cgraph_remove_node (cnode2
);
642 symtab_insert_node_to_hashtable (node
);
648 /* Given the decl DECL, return the prevailing decl with the same name. */
651 lto_symtab_prevailing_decl (tree decl
)
655 /* Builtins and local symbols are their own prevailing decl. */
656 if ((!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
)) || is_builtin_fn (decl
))
659 /* DECL_ABSTRACTs are their own prevailng decl. */
660 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT (decl
))
663 /* Likewise builtins are their own prevailing decl. This preserves
664 non-builtin vs. builtin uses from compile-time. */
665 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
668 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
669 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
671 /* Walk through the list of candidates and return the one we merged to. */
672 ret
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl
));