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"
32 /* Vector to keep track of external variables we've seen so far. */
33 vec
<tree
, va_gc
> *lto_global_var_decls
;
35 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
36 all edges and removing the old node. */
39 lto_cgraph_replace_node (struct cgraph_node
*node
,
40 struct cgraph_node
*prevailing_node
)
42 struct cgraph_edge
*e
, *next
;
47 fprintf (cgraph_dump_file
, "Replacing cgraph node %s/%i by %s/%i"
49 cgraph_node_name (node
), node
->uid
,
50 cgraph_node_name (prevailing_node
),
52 IDENTIFIER_POINTER ((*targetm
.asm_out
.mangle_assembler_name
)
53 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->symbol
.decl
)))));
56 /* Merge node flags. */
57 if (node
->symbol
.force_output
)
58 cgraph_mark_force_output_node (prevailing_node
);
59 if (node
->symbol
.address_taken
)
61 gcc_assert (!prevailing_node
->global
.inlined_to
);
62 cgraph_mark_address_taken_node (prevailing_node
);
65 /* Redirect all incoming edges. */
67 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node
->symbol
.decl
)),
68 TREE_TYPE (TREE_TYPE (node
->symbol
.decl
)));
69 for (e
= node
->callers
; e
; e
= next
)
71 next
= e
->next_caller
;
72 cgraph_redirect_edge_callee (e
, prevailing_node
);
73 /* If there is a mismatch between the supposed callee return type and
74 the real one do not attempt to inline this function.
75 ??? We really need a way to match function signatures for ABI
76 compatibility and perform related promotions at inlining time. */
78 e
->call_stmt_cannot_inline_p
= 1;
80 /* Redirect incomming references. */
81 ipa_clone_referring ((symtab_node
)prevailing_node
, &node
->symbol
.ref_list
);
83 /* Finally remove the replaced node. */
84 cgraph_remove_node (node
);
87 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
88 all edges and removing the old node. */
91 lto_varpool_replace_node (struct varpool_node
*vnode
,
92 struct varpool_node
*prevailing_node
)
94 gcc_assert (!vnode
->finalized
|| prevailing_node
->finalized
);
95 gcc_assert (!vnode
->analyzed
|| prevailing_node
->analyzed
);
97 ipa_clone_referring ((symtab_node
)prevailing_node
, &vnode
->symbol
.ref_list
);
99 /* Be sure we can garbage collect the initializer. */
100 if (DECL_INITIAL (vnode
->symbol
.decl
))
101 DECL_INITIAL (vnode
->symbol
.decl
) = error_mark_node
;
102 /* Finally remove the replaced node. */
103 varpool_remove_node (vnode
);
106 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
107 Return false if the symbols are not fully compatible and a diagnostic
108 should be emitted. */
111 lto_symtab_merge (symtab_node prevailing
, symtab_node entry
)
113 tree prevailing_decl
= prevailing
->symbol
.decl
;
114 tree decl
= entry
->symbol
.decl
;
115 tree prevailing_type
, type
;
117 if (prevailing_decl
== decl
)
120 /* Merge decl state in both directions, we may still end up using
122 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
123 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
125 /* The linker may ask us to combine two incompatible symbols.
126 Detect this case and notify the caller of required diagnostics. */
128 if (TREE_CODE (decl
) == FUNCTION_DECL
)
130 if (!types_compatible_p (TREE_TYPE (prevailing_decl
),
132 /* If we don't have a merged type yet...sigh. The linker
133 wouldn't complain if the types were mismatched, so we
134 probably shouldn't either. Just use the type from
135 whichever decl appears to be associated with the
136 definition. If for some odd reason neither decl is, the
143 /* Now we exclusively deal with VAR_DECLs. */
145 /* Sharing a global symbol is a strong hint that two types are
146 compatible. We could use this information to complete
147 incomplete pointed-to types more aggressively here, ignoring
148 mismatches in both field and tag names. It's difficult though
149 to guarantee that this does not have side-effects on merging
150 more compatible types from other translation units though. */
152 /* We can tolerate differences in type qualification, the
153 qualification of the prevailing definition will prevail.
154 ??? In principle we might want to only warn for structurally
155 incompatible types here, but unless we have protective measures
156 for TBAA in place that would hide useful information. */
157 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
158 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
160 if (!types_compatible_p (prevailing_type
, type
))
162 if (COMPLETE_TYPE_P (type
))
165 /* If type is incomplete then avoid warnings in the cases
166 that TBAA handles just fine. */
168 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
171 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
173 tree tem1
= TREE_TYPE (prevailing_type
);
174 tree tem2
= TREE_TYPE (type
);
175 while (TREE_CODE (tem1
) == ARRAY_TYPE
176 && TREE_CODE (tem2
) == ARRAY_TYPE
)
178 tem1
= TREE_TYPE (tem1
);
179 tem2
= TREE_TYPE (tem2
);
182 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
185 if (!types_compatible_p (tem1
, tem2
))
189 /* Fallthru. Compatible enough. */
192 /* ??? We might want to emit a warning here if type qualification
193 differences were spotted. Do not do this unconditionally though. */
195 /* There is no point in comparing too many details of the decls here.
196 The type compatibility checks or the completing of types has properly
197 dealt with most issues. */
199 /* The following should all not invoke fatal errors as in non-LTO
200 mode the linker wouldn't complain either. Just emit warnings. */
202 /* Report a warning if user-specified alignments do not match. */
203 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
204 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
210 /* Return true if the symtab entry E can be replaced by another symtab
214 lto_symtab_resolve_replaceable_p (symtab_node e
)
216 if (DECL_EXTERNAL (e
->symbol
.decl
)
217 || DECL_COMDAT (e
->symbol
.decl
)
218 || DECL_ONE_ONLY (e
->symbol
.decl
)
219 || DECL_WEAK (e
->symbol
.decl
))
222 if (TREE_CODE (e
->symbol
.decl
) == VAR_DECL
)
223 return (DECL_COMMON (e
->symbol
.decl
)
224 || (!flag_no_common
&& !DECL_INITIAL (e
->symbol
.decl
)));
229 /* Return true, if the symbol E should be resolved by lto-symtab.
230 Those are all real symbols that are not static (we handle renaming
231 of static later in partitioning). */
234 lto_symtab_symbol_p (symtab_node e
)
236 if (!TREE_PUBLIC (e
->symbol
.decl
))
238 return symtab_real_symbol_p (e
);
241 /* Return true if the symtab entry E can be the prevailing one. */
244 lto_symtab_resolve_can_prevail_p (symtab_node e
)
246 if (!lto_symtab_symbol_p (e
))
249 /* The C++ frontend ends up neither setting TREE_STATIC nor
250 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
251 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
252 if (DECL_EXTERNAL (e
->symbol
.decl
))
255 /* For functions we need a non-discarded body. */
256 if (TREE_CODE (e
->symbol
.decl
) == FUNCTION_DECL
)
257 return (cgraph (e
)->analyzed
);
259 else if (TREE_CODE (e
->symbol
.decl
) == VAR_DECL
)
260 return varpool (e
)->finalized
;
265 /* Resolve the symbol with the candidates in the chain *SLOT and store
266 their resolutions. */
269 lto_symtab_resolve_symbols (symtab_node first
)
272 symtab_node prevailing
= NULL
;
274 /* Always set e->node so that edges are updated to reflect decl merging. */
275 for (e
= first
; e
; e
= e
->symbol
.next_sharing_asm_name
)
276 if (lto_symtab_symbol_p (e
)
277 && (e
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
278 || e
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
279 || e
->symbol
.resolution
== LDPR_PREVAILING_DEF
))
285 /* If the chain is already resolved there is nothing else to do. */
288 /* Assert it's the only one. */
289 for (e
= prevailing
->symbol
.next_sharing_asm_name
; e
; e
= e
->symbol
.next_sharing_asm_name
)
290 if (lto_symtab_symbol_p (e
)
291 && (e
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
292 || e
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
293 || e
->symbol
.resolution
== LDPR_PREVAILING_DEF
))
294 fatal_error ("multiple prevailing defs for %qE",
295 DECL_NAME (prevailing
->symbol
.decl
));
299 /* Find the single non-replaceable prevailing symbol and
300 diagnose ODR violations. */
301 for (e
= first
; e
; e
= e
->symbol
.next_sharing_asm_name
)
303 if (!lto_symtab_resolve_can_prevail_p (e
))
306 /* If we have a non-replaceable definition it prevails. */
307 if (!lto_symtab_resolve_replaceable_p (e
))
311 error_at (DECL_SOURCE_LOCATION (e
->symbol
.decl
),
312 "%qD has already been defined", e
->symbol
.decl
);
313 inform (DECL_SOURCE_LOCATION (prevailing
->symbol
.decl
),
314 "previously defined here");
322 /* Do a second round choosing one from the replaceable prevailing decls. */
323 for (e
= first
; e
; e
= e
->symbol
.next_sharing_asm_name
)
325 if (!lto_symtab_resolve_can_prevail_p (e
))
328 /* Choose the first function that can prevail as prevailing. */
329 if (TREE_CODE (e
->symbol
.decl
) == FUNCTION_DECL
)
335 /* From variables that can prevail choose the largest one. */
337 || tree_int_cst_lt (DECL_SIZE (prevailing
->symbol
.decl
),
338 DECL_SIZE (e
->symbol
.decl
))
339 /* When variables are equivalent try to chose one that has useful
340 DECL_INITIAL. This makes sense for keyed vtables that are
341 DECL_EXTERNAL but initialized. In units that do not need them
342 we replace the initializer by error_mark_node to conserve
345 We know that the vtable is keyed outside the LTO unit - otherwise
346 the keyed instance would prevail. We still can preserve useful
347 info in the initializer. */
348 || (DECL_SIZE (prevailing
->symbol
.decl
) == DECL_SIZE (e
->symbol
.decl
)
349 && (DECL_INITIAL (e
->symbol
.decl
)
350 && DECL_INITIAL (e
->symbol
.decl
) != error_mark_node
)
351 && (!DECL_INITIAL (prevailing
->symbol
.decl
)
352 || DECL_INITIAL (prevailing
->symbol
.decl
) == error_mark_node
)))
359 /* Merge all decls in the symbol table chain to the prevailing decl and
360 issue diagnostics about type mismatches. If DIAGNOSED_P is true
361 do not issue further diagnostics.*/
364 lto_symtab_merge_decls_2 (symtab_node first
, bool diagnosed_p
)
366 symtab_node prevailing
, e
;
367 vec
<tree
> mismatches
= vNULL
;
371 /* Nothing to do for a single entry. */
373 if (!prevailing
->symbol
.next_sharing_asm_name
)
376 /* Try to merge each entry with the prevailing one. */
377 for (e
= prevailing
->symbol
.next_sharing_asm_name
;
378 e
; e
= e
->symbol
.next_sharing_asm_name
)
379 if (TREE_PUBLIC (e
->symbol
.decl
))
381 if (!lto_symtab_merge (prevailing
, e
)
383 mismatches
.safe_push (e
->symbol
.decl
);
385 if (mismatches
.is_empty ())
388 /* Diagnose all mismatched re-declarations. */
389 FOR_EACH_VEC_ELT (mismatches
, i
, decl
)
391 if (!types_compatible_p (TREE_TYPE (prevailing
->symbol
.decl
),
393 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
394 "type of %qD does not match original "
395 "declaration", decl
);
397 else if ((DECL_USER_ALIGN (prevailing
->symbol
.decl
)
398 && DECL_USER_ALIGN (decl
))
399 && DECL_ALIGN (prevailing
->symbol
.decl
) < DECL_ALIGN (decl
))
401 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
402 "alignment of %qD is bigger than "
403 "original declaration", decl
);
407 inform (DECL_SOURCE_LOCATION (prevailing
->symbol
.decl
),
408 "previously declared here");
410 mismatches
.release ();
413 /* Helper to process the decl chain for the symbol table entry *SLOT. */
416 lto_symtab_merge_decls_1 (symtab_node first
)
418 symtab_node e
, prevailing
;
419 bool diagnosed_p
= false;
421 if (cgraph_dump_file
)
423 fprintf (cgraph_dump_file
, "Merging nodes for %s. Candidates:\n",
424 symtab_node_asm_name (first
));
425 for (e
= first
; e
; e
= e
->symbol
.next_sharing_asm_name
)
426 if (TREE_PUBLIC (e
->symbol
.decl
))
427 dump_symtab_node (cgraph_dump_file
, e
);
430 /* Compute the symbol resolutions. This is a no-op when using the
431 linker plugin and resolution was decided by the linker. */
432 prevailing
= lto_symtab_resolve_symbols (first
);
434 /* If there's not a prevailing symbol yet it's an external reference.
435 Happens a lot during ltrans. Choose the first symbol with a
436 cgraph or a varpool node. */
440 /* For variables chose with a priority variant with vnode
441 attached (i.e. from unit where external declaration of
442 variable is actually used).
443 When there are multiple variants, chose one with size.
444 This is needed for C++ typeinfos, for example in
445 lto/20081204-1 there are typeifos in both units, just
446 one of them do have size. */
447 if (TREE_CODE (prevailing
->symbol
.decl
) == VAR_DECL
)
449 for (e
= prevailing
->symbol
.next_sharing_asm_name
;
450 e
; e
= e
->symbol
.next_sharing_asm_name
)
451 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing
->symbol
.decl
))
452 && COMPLETE_TYPE_P (TREE_TYPE (e
->symbol
.decl
))
453 && lto_symtab_symbol_p (e
))
456 /* For variables prefer the non-builtin if one is available. */
457 else if (TREE_CODE (prevailing
->symbol
.decl
) == FUNCTION_DECL
)
459 for (e
= first
; e
; e
= e
->symbol
.next_sharing_asm_name
)
460 if (TREE_CODE (e
->symbol
.decl
) == FUNCTION_DECL
461 && !DECL_BUILT_IN (e
->symbol
.decl
)
462 && lto_symtab_symbol_p (e
))
470 symtab_prevail_in_asm_name_hash (prevailing
);
472 /* Diagnose mismatched objects. */
473 for (e
= prevailing
->symbol
.next_sharing_asm_name
;
474 e
; e
= e
->symbol
.next_sharing_asm_name
)
476 if (TREE_CODE (prevailing
->symbol
.decl
)
477 == TREE_CODE (e
->symbol
.decl
))
479 if (!lto_symtab_symbol_p (e
))
482 switch (TREE_CODE (prevailing
->symbol
.decl
))
485 gcc_assert (TREE_CODE (e
->symbol
.decl
) == FUNCTION_DECL
);
486 error_at (DECL_SOURCE_LOCATION (e
->symbol
.decl
),
487 "variable %qD redeclared as function",
488 prevailing
->symbol
.decl
);
492 gcc_assert (TREE_CODE (e
->symbol
.decl
) == VAR_DECL
);
493 error_at (DECL_SOURCE_LOCATION (e
->symbol
.decl
),
494 "function %qD redeclared as variable",
495 prevailing
->symbol
.decl
);
505 inform (DECL_SOURCE_LOCATION (prevailing
->symbol
.decl
),
506 "previously declared here");
508 /* Merge the chain to the single prevailing decl and diagnose
510 lto_symtab_merge_decls_2 (prevailing
, diagnosed_p
);
512 if (cgraph_dump_file
)
514 fprintf (cgraph_dump_file
, "After resolution:\n");
515 for (e
= prevailing
; e
; e
= e
->symbol
.next_sharing_asm_name
)
516 dump_symtab_node (cgraph_dump_file
, e
);
520 /* Resolve and merge all symbol table chains to a prevailing decl. */
523 lto_symtab_merge_decls (void)
527 /* Populate assembler name hash. */
528 symtab_initialize_asm_name_hash ();
530 FOR_EACH_SYMBOL (node
)
531 if (TREE_PUBLIC (node
->symbol
.decl
)
532 && node
->symbol
.next_sharing_asm_name
533 && !node
->symbol
.previous_sharing_asm_name
)
534 lto_symtab_merge_decls_1 (node
);
537 /* Helper to process the decl chain for the symbol table entry *SLOT. */
540 lto_symtab_merge_cgraph_nodes_1 (symtab_node prevailing
)
544 /* Replace the cgraph node of each entry with the prevailing one. */
545 for (e
= prevailing
->symbol
.next_sharing_asm_name
; e
;
548 next
= e
->symbol
.next_sharing_asm_name
;
550 if (!lto_symtab_symbol_p (e
))
552 cgraph_node
*ce
= dyn_cast
<cgraph_node
> (e
);
553 if (ce
&& !DECL_BUILT_IN (e
->symbol
.decl
))
554 lto_cgraph_replace_node (ce
, cgraph (prevailing
));
555 if (varpool_node
*ve
= dyn_cast
<varpool_node
> (e
))
556 lto_varpool_replace_node (ve
, varpool (prevailing
));
562 /* Merge cgraph nodes according to the symbol merging done by
563 lto_symtab_merge_decls. */
566 lto_symtab_merge_cgraph_nodes (void)
568 struct cgraph_node
*cnode
;
569 struct varpool_node
*vnode
;
572 /* Populate assembler name hash. */
573 symtab_initialize_asm_name_hash ();
576 FOR_EACH_SYMBOL (node
)
577 if (TREE_PUBLIC (node
->symbol
.decl
)
578 && node
->symbol
.next_sharing_asm_name
579 && !node
->symbol
.previous_sharing_asm_name
)
580 lto_symtab_merge_cgraph_nodes_1 (node
);
582 FOR_EACH_FUNCTION (cnode
)
584 if ((cnode
->thunk
.thunk_p
|| cnode
->alias
)
585 && cnode
->thunk
.alias
)
586 cnode
->thunk
.alias
= lto_symtab_prevailing_decl (cnode
->thunk
.alias
);
587 cnode
->symbol
.aux
= NULL
;
589 FOR_EACH_VARIABLE (vnode
)
592 vnode
->alias_of
= lto_symtab_prevailing_decl (vnode
->alias_of
);
593 vnode
->symbol
.aux
= NULL
;
597 /* Given the decl DECL, return the prevailing decl with the same name. */
600 lto_symtab_prevailing_decl (tree decl
)
604 /* Builtins and local symbols are their own prevailing decl. */
605 if (!TREE_PUBLIC (decl
) || is_builtin_fn (decl
))
608 /* DECL_ABSTRACTs are their own prevailng decl. */
609 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT (decl
))
612 /* Likewise builtins are their own prevailing decl. This preserves
613 non-builtin vs. builtin uses from compile-time. */
614 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
))
617 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
618 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
620 /* Walk through the list of candidates and return the one we merged to. */
621 ret
= symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl
));
625 return ret
->symbol
.decl
;