2 Copyright 2009 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"
27 #include "ggc.h" /* lambda.h needs this */
28 #include "lambda.h" /* gcd */
30 #include "plugin-api.h"
31 #include "lto-streamer.h"
33 /* Vector to keep track of external variables we've seen so far. */
34 VEC(tree,gc) *lto_global_var_decls;
36 /* Symbol table entry. */
38 struct GTY(()) lto_symtab_entry_def
40 /* The symbol table entry key, an IDENTIFIER. */
42 /* The symbol table entry, a DECL. */
44 /* The cgraph node if decl is a function decl. Filled in during the
46 struct cgraph_node *node;
47 /* The varpool node if decl is a variable decl. Filled in during the
49 struct varpool_node *vnode;
50 /* LTO file-data and symbol resolution for this decl. */
51 struct lto_file_decl_data * GTY((skip (""))) file_data;
52 enum ld_plugin_symbol_resolution resolution;
53 /* Pointer to the next entry with the same key. Before decl merging
54 this links all symbols from the different TUs. After decl merging
55 this links merged but incompatible decls, thus all prevailing ones
57 struct lto_symtab_entry_def *next;
59 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
61 /* A poor man's symbol table. This hashes identifier to prevailing DECL
64 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
65 param_is (struct lto_symtab_entry_def)))
66 htab_t lto_symtab_identifiers;
68 /* Free symtab hashtable. */
71 lto_symtab_free (void)
73 htab_delete (lto_symtab_identifiers);
74 lto_symtab_identifiers = NULL;
77 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
80 lto_symtab_entry_hash (const void *p)
82 const struct lto_symtab_entry_def *base =
83 (const struct lto_symtab_entry_def *) p;
84 return htab_hash_string (IDENTIFIER_POINTER (base->id));
87 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
88 corresponding to the same symbol. */
91 lto_symtab_entry_eq (const void *p1, const void *p2)
93 const struct lto_symtab_entry_def *base1 =
94 (const struct lto_symtab_entry_def *) p1;
95 const struct lto_symtab_entry_def *base2 =
96 (const struct lto_symtab_entry_def *) p2;
97 return (base1->id == base2->id);
100 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
101 to be marked for GC. */
104 lto_symtab_entry_marked_p (const void *p)
106 const struct lto_symtab_entry_def *base =
107 (const struct lto_symtab_entry_def *) p;
109 /* Keep this only if the common IDENTIFIER_NODE of the symtab chain
110 is marked which it will be if at least one of the DECLs in the
112 return ggc_marked_p (base->id);
115 /* Lazily initialize resolution hash tables. */
118 lto_symtab_maybe_init_hash_table (void)
120 if (lto_symtab_identifiers)
123 lto_symtab_identifiers =
124 htab_create_ggc (1021, lto_symtab_entry_hash,
125 lto_symtab_entry_eq, NULL);
128 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
129 and read from FILE_DATA. */
132 lto_symtab_register_decl (tree decl,
133 ld_plugin_symbol_resolution_t resolution,
134 struct lto_file_decl_data *file_data)
136 lto_symtab_entry_t new_entry;
139 /* Check that declarations reaching this function do not have
140 properties inconsistent with having external linkage. If any of
141 these asertions fail, then the object file reader has failed to
142 detect these cases and issue appropriate error messages. */
144 && TREE_PUBLIC (decl)
145 && (TREE_CODE (decl) == VAR_DECL
146 || TREE_CODE (decl) == FUNCTION_DECL)
147 && DECL_ASSEMBLER_NAME_SET_P (decl));
148 if (TREE_CODE (decl) == VAR_DECL
149 && DECL_INITIAL (decl))
150 gcc_assert (!DECL_EXTERNAL (decl)
151 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
152 if (TREE_CODE (decl) == FUNCTION_DECL)
153 gcc_assert (!DECL_ABSTRACT (decl));
155 new_entry = GGC_CNEW (struct lto_symtab_entry_def);
156 new_entry->id = DECL_ASSEMBLER_NAME (decl);
157 new_entry->decl = decl;
158 new_entry->resolution = resolution;
159 new_entry->file_data = file_data;
161 lto_symtab_maybe_init_hash_table ();
162 slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
163 new_entry->next = (lto_symtab_entry_t) *slot;
167 /* Get the lto_symtab_entry_def struct associated with ID
170 static lto_symtab_entry_t
171 lto_symtab_get (tree id)
173 struct lto_symtab_entry_def temp;
176 lto_symtab_maybe_init_hash_table ();
178 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
179 return slot ? (lto_symtab_entry_t) *slot : NULL;
182 /* Get the linker resolution for DECL. */
184 enum ld_plugin_symbol_resolution
185 lto_symtab_get_resolution (tree decl)
187 lto_symtab_entry_t e;
189 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
191 e = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
192 while (e && e->decl != decl)
197 return e->resolution;
201 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
202 all edges and removing the old node. */
205 lto_cgraph_replace_node (struct cgraph_node *node,
206 struct cgraph_node *prevailing_node)
208 struct cgraph_edge *e, *next;
210 /* Merge node flags. */
212 cgraph_mark_needed_node (prevailing_node);
214 cgraph_mark_reachable_node (prevailing_node);
215 if (node->address_taken)
217 gcc_assert (!prevailing_node->global.inlined_to);
218 cgraph_mark_address_taken_node (prevailing_node);
221 /* Redirect all incoming edges. */
222 for (e = node->callers; e; e = next)
224 next = e->next_caller;
225 cgraph_redirect_edge_callee (e, prevailing_node);
227 /* Redirect incomming references. */
228 ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
232 struct cgraph_node *alias;
234 for (alias = node->same_body; alias; alias = alias->next)
235 if (DECL_ASSEMBLER_NAME_SET_P (alias->decl))
237 lto_symtab_entry_t se
238 = lto_symtab_get (DECL_ASSEMBLER_NAME (alias->decl));
240 for (; se; se = se->next)
241 if (se->node == node)
249 /* Finally remove the replaced node. */
250 cgraph_remove_node (node);
253 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
254 all edges and removing the old node. */
257 lto_varpool_replace_node (struct varpool_node *vnode,
258 struct varpool_node *prevailing_node)
260 /* Merge node flags. */
263 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
264 varpool_mark_needed_node (prevailing_node);
266 /* Relink aliases. */
267 if (vnode->extra_name && !vnode->alias)
269 struct varpool_node *alias, *last;
270 for (alias = vnode->extra_name;
271 alias; alias = alias->next)
274 alias->extra_name = prevailing_node;
277 if (prevailing_node->extra_name)
279 last->next = prevailing_node->extra_name;
280 prevailing_node->extra_name->prev = last;
282 prevailing_node->extra_name = vnode->extra_name;
283 vnode->extra_name = NULL;
285 gcc_assert (!vnode->finalized || prevailing_node->finalized);
286 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
288 /* When replacing by an alias, the references goes to the original
290 if (prevailing_node->alias && prevailing_node->extra_name)
291 prevailing_node = prevailing_node->extra_name;
292 ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
294 /* Finally remove the replaced node. */
295 varpool_remove_node (vnode);
298 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
299 Return false if the symbols are not fully compatible and a diagnostic
300 should be emitted. */
303 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
305 tree prevailing_decl = prevailing->decl;
306 tree decl = entry->decl;
307 tree prevailing_type, type;
309 /* Merge decl state in both directions, we may still end up using
311 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
312 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
314 /* The linker may ask us to combine two incompatible symbols.
315 Detect this case and notify the caller of required diagnostics. */
317 if (TREE_CODE (decl) == FUNCTION_DECL)
319 if (TREE_TYPE (prevailing_decl) != TREE_TYPE (decl))
320 /* If we don't have a merged type yet...sigh. The linker
321 wouldn't complain if the types were mismatched, so we
322 probably shouldn't either. Just use the type from
323 whichever decl appears to be associated with the
324 definition. If for some odd reason neither decl is, the
331 /* Now we exclusively deal with VAR_DECLs. */
333 /* Sharing a global symbol is a strong hint that two types are
334 compatible. We could use this information to complete
335 incomplete pointed-to types more aggressively here, ignoring
336 mismatches in both field and tag names. It's difficult though
337 to guarantee that this does not have side-effects on merging
338 more compatible types from other translation units though. */
340 /* We can tolerate differences in type qualification, the
341 qualification of the prevailing definition will prevail.
342 ??? In principle we might want to only warn for structurally
343 incompatible types here, but unless we have protective measures
344 for TBAA in place that would hide useful information. */
345 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
346 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
348 /* We have to register and fetch canonical types here as the global
349 fixup process didn't yet run. */
350 prevailing_type = gimple_register_type (prevailing_type);
351 type = gimple_register_type (type);
352 if (prevailing_type != type)
354 if (COMPLETE_TYPE_P (type))
357 /* If type is incomplete then avoid warnings in the cases
358 that TBAA handles just fine. */
360 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
363 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
365 tree tem1 = TREE_TYPE (prevailing_type);
366 tree tem2 = TREE_TYPE (type);
367 while (TREE_CODE (tem1) == ARRAY_TYPE
368 && TREE_CODE (tem2) == ARRAY_TYPE)
370 tem1 = TREE_TYPE (tem1);
371 tem2 = TREE_TYPE (tem2);
374 if (TREE_CODE (tem1) != TREE_CODE (tem2))
377 if (gimple_register_type (tem1) != gimple_register_type (tem2))
381 /* Fallthru. Compatible enough. */
384 /* ??? We might want to emit a warning here if type qualification
385 differences were spotted. Do not do this unconditionally though. */
387 /* There is no point in comparing too many details of the decls here.
388 The type compatibility checks or the completing of types has properly
389 dealt with most issues. */
391 /* The following should all not invoke fatal errors as in non-LTO
392 mode the linker wouldn't complain either. Just emit warnings. */
394 /* Report a warning if user-specified alignments do not match. */
395 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
396 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
402 /* Return true if the symtab entry E can be replaced by another symtab
406 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
408 if (DECL_EXTERNAL (e->decl)
409 || DECL_COMDAT (e->decl)
410 || DECL_WEAK (e->decl))
413 if (TREE_CODE (e->decl) == VAR_DECL)
414 return (DECL_COMMON (e->decl)
415 || (!flag_no_common && !DECL_INITIAL (e->decl)));
420 /* Return true if the symtab entry E can be the prevailing one. */
423 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
425 /* The C++ frontend ends up neither setting TREE_STATIC nor
426 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
427 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
428 if (DECL_EXTERNAL (e->decl))
431 /* For functions we need a non-discarded body. */
432 if (TREE_CODE (e->decl) == FUNCTION_DECL)
433 return (e->node && e->node->analyzed);
435 /* A variable should have a size. */
436 else if (TREE_CODE (e->decl) == VAR_DECL)
440 if (e->vnode->finalized)
442 return e->vnode->alias && e->vnode->extra_name->finalized;
448 /* Resolve the symbol with the candidates in the chain *SLOT and store
449 their resolutions. */
452 lto_symtab_resolve_symbols (void **slot)
454 lto_symtab_entry_t e;
455 lto_symtab_entry_t prevailing = NULL;
457 /* Always set e->node so that edges are updated to reflect decl merging. */
458 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
460 if (TREE_CODE (e->decl) == FUNCTION_DECL)
461 e->node = cgraph_get_node (e->decl);
462 else if (TREE_CODE (e->decl) == VAR_DECL)
463 e->vnode = varpool_get_node (e->decl);
466 e = (lto_symtab_entry_t) *slot;
468 /* If the chain is already resolved there is nothing else to do. */
469 if (e->resolution != LDPR_UNKNOWN)
472 /* Find the single non-replaceable prevailing symbol and
473 diagnose ODR violations. */
474 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
476 if (!lto_symtab_resolve_can_prevail_p (e))
478 e->resolution = LDPR_RESOLVED_IR;
482 /* Set a default resolution - the final prevailing one will get
484 e->resolution = LDPR_PREEMPTED_IR;
485 if (!lto_symtab_resolve_replaceable_p (e))
489 error_at (DECL_SOURCE_LOCATION (e->decl),
490 "%qD has already been defined", e->decl);
491 inform (DECL_SOURCE_LOCATION (prevailing->decl),
492 "previously defined here");
500 /* Do a second round choosing one from the replaceable prevailing decls. */
501 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
503 if (e->resolution != LDPR_PREEMPTED_IR)
506 /* Choose the first function that can prevail as prevailing. */
507 if (TREE_CODE (e->decl) == FUNCTION_DECL)
513 /* From variables that can prevail choose the largest one. */
515 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
516 DECL_SIZE (e->decl)))
524 if (TREE_CODE (prevailing->decl) == VAR_DECL
525 && TREE_READONLY (prevailing->decl))
526 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
528 prevailing->resolution = LDPR_PREVAILING_DEF;
531 /* Merge all decls in the symbol table chain to the prevailing decl and
532 issue diagnostics about type mismatches. */
535 lto_symtab_merge_decls_2 (void **slot)
537 lto_symtab_entry_t prevailing, e;
538 VEC(tree, heap) *mismatches = NULL;
541 bool diagnosed_p = false;
543 /* Nothing to do for a single entry. */
544 prevailing = (lto_symtab_entry_t) *slot;
545 if (!prevailing->next)
548 /* Try to merge each entry with the prevailing one. */
549 for (e = prevailing->next; e; e = e->next)
551 if (!lto_symtab_merge (prevailing, e))
552 VEC_safe_push (tree, heap, mismatches, e->decl);
554 if (VEC_empty (tree, mismatches))
557 /* Diagnose all mismatched re-declarations. */
558 for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
560 if (TREE_TYPE (prevailing->decl) != TREE_TYPE (decl))
561 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
562 "type of %qD does not match original "
563 "declaration", decl);
565 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
566 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
568 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
569 "alignment of %qD is bigger than "
570 "original declaration", decl);
574 inform (DECL_SOURCE_LOCATION (prevailing->decl),
575 "previously declared here");
577 VEC_free (tree, heap, mismatches);
580 /* Helper to process the decl chain for the symbol table entry *SLOT. */
583 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
585 lto_symtab_entry_t e, prevailing;
586 bool diagnosed_p = false;
588 /* Compute the symbol resolutions. This is a no-op when using the
590 lto_symtab_resolve_symbols (slot);
592 /* Find the prevailing decl. */
593 for (prevailing = (lto_symtab_entry_t) *slot;
595 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
596 && prevailing->resolution != LDPR_PREVAILING_DEF;
597 prevailing = prevailing->next)
600 /* Assert it's the only one. */
602 for (e = prevailing->next; e; e = e->next)
603 gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
604 && e->resolution != LDPR_PREVAILING_DEF);
606 /* If there's not a prevailing symbol yet it's an external reference.
607 Happens a lot during ltrans. Choose the first symbol with a
608 cgraph or a varpool node. */
611 prevailing = (lto_symtab_entry_t) *slot;
612 /* For functions choose one with a cgraph node. */
613 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
614 while (!prevailing->node
616 prevailing = prevailing->next;
617 /* For variables chose with a priority variant with vnode
618 attached (i.e. from unit where external declaration of
619 variable is actually used).
620 When there are multiple variants, chose one with size.
621 This is needed for C++ typeinfos, for example in
622 lto/20081204-1 there are typeifos in both units, just
623 one of them do have size. */
624 if (TREE_CODE (prevailing->decl) == VAR_DECL)
626 for (e = prevailing->next; e; e = e->next)
627 if ((!prevailing->vnode && e->vnode)
628 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
629 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
630 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
635 /* Move it first in the list. */
636 if ((lto_symtab_entry_t) *slot != prevailing)
638 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
640 e->next = prevailing->next;
641 prevailing->next = (lto_symtab_entry_t) *slot;
642 *slot = (void *) prevailing;
645 /* Record the prevailing variable. */
646 if (TREE_CODE (prevailing->decl) == VAR_DECL)
647 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
649 /* Diagnose mismatched objects. */
650 for (e = prevailing->next; e; e = e->next)
652 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
655 switch (TREE_CODE (prevailing->decl))
658 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
659 error_at (DECL_SOURCE_LOCATION (e->decl),
660 "variable %qD redeclared as function", prevailing->decl);
664 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
665 error_at (DECL_SOURCE_LOCATION (e->decl),
666 "function %qD redeclared as variable", prevailing->decl);
676 inform (DECL_SOURCE_LOCATION (prevailing->decl),
677 "previously declared here");
679 /* Register and adjust types of the entries. */
680 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
681 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
683 /* Merge the chain to the single prevailing decl and diagnose
685 lto_symtab_merge_decls_2 (slot);
687 /* Drop all but the prevailing decl from the symtab. */
688 if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
689 && TREE_CODE (prevailing->decl) != VAR_DECL)
690 prevailing->next = NULL;
695 /* Resolve and merge all symbol table chains to a prevailing decl. */
698 lto_symtab_merge_decls (void)
700 lto_symtab_maybe_init_hash_table ();
701 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
704 /* Helper to process the decl chain for the symbol table entry *SLOT. */
707 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
709 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
711 if (!prevailing->next)
714 /* Replace the cgraph node of each entry with the prevailing one. */
715 for (e = prevailing->next; e; e = e->next)
719 if (e->node->decl != e->decl && e->node->same_body)
721 struct cgraph_node *alias;
723 for (alias = e->node->same_body; alias; alias = alias->next)
724 if (alias->decl == e->decl)
728 cgraph_remove_same_body_alias (alias);
732 lto_cgraph_replace_node (e->node, prevailing->node);
734 if (e->vnode != NULL)
735 lto_varpool_replace_node (e->vnode, prevailing->vnode);
738 /* Drop all but the prevailing decl from the symtab. */
739 prevailing->next = NULL;
744 /* Merge cgraph nodes according to the symbol merging done by
745 lto_symtab_merge_decls. */
748 lto_symtab_merge_cgraph_nodes (void)
750 lto_symtab_maybe_init_hash_table ();
751 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
754 /* Given the decl DECL, return the prevailing decl with the same name. */
757 lto_symtab_prevailing_decl (tree decl)
759 lto_symtab_entry_t ret;
761 /* Builtins and local symbols are their own prevailing decl. */
762 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
765 /* DECL_ABSTRACTs are their own prevailng decl. */
766 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
769 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
770 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
772 /* Walk through the list of candidates and return the one we merged to. */
773 ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
780 #include "gt-lto-symtab.h"