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 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
71 lto_symtab_entry_hash (const void *p)
73 const struct lto_symtab_entry_def *base =
74 (const struct lto_symtab_entry_def *) p;
75 return htab_hash_string (IDENTIFIER_POINTER (base->id));
78 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
79 corresponding to the same symbol. */
82 lto_symtab_entry_eq (const void *p1, const void *p2)
84 const struct lto_symtab_entry_def *base1 =
85 (const struct lto_symtab_entry_def *) p1;
86 const struct lto_symtab_entry_def *base2 =
87 (const struct lto_symtab_entry_def *) p2;
88 return (base1->id == base2->id);
91 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
92 to be marked for GC. */
95 lto_symtab_entry_marked_p (const void *p)
97 const struct lto_symtab_entry_def *base =
98 (const struct lto_symtab_entry_def *) p;
100 /* Keep this only if the decl or the chain is marked. */
101 return (ggc_marked_p (base->decl)
102 || (base->next && ggc_marked_p (base->next)));
105 /* Lazily initialize resolution hash tables. */
108 lto_symtab_maybe_init_hash_table (void)
110 if (lto_symtab_identifiers)
113 lto_symtab_identifiers =
114 htab_create_ggc (1021, lto_symtab_entry_hash,
115 lto_symtab_entry_eq, NULL);
118 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
119 and read from FILE_DATA. */
122 lto_symtab_register_decl (tree decl,
123 ld_plugin_symbol_resolution_t resolution,
124 struct lto_file_decl_data *file_data)
126 lto_symtab_entry_t new_entry;
129 /* Check that declarations reaching this function do not have
130 properties inconsistent with having external linkage. If any of
131 these asertions fail, then the object file reader has failed to
132 detect these cases and issue appropriate error messages. */
134 && TREE_PUBLIC (decl)
135 && (TREE_CODE (decl) == VAR_DECL
136 || TREE_CODE (decl) == FUNCTION_DECL)
137 && DECL_ASSEMBLER_NAME_SET_P (decl));
138 if (TREE_CODE (decl) == VAR_DECL
139 && DECL_INITIAL (decl))
140 gcc_assert (!DECL_EXTERNAL (decl)
141 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
142 if (TREE_CODE (decl) == FUNCTION_DECL)
143 gcc_assert (!DECL_ABSTRACT (decl));
145 new_entry = GGC_CNEW (struct lto_symtab_entry_def);
146 new_entry->id = DECL_ASSEMBLER_NAME (decl);
147 new_entry->decl = decl;
148 new_entry->resolution = resolution;
149 new_entry->file_data = file_data;
151 lto_symtab_maybe_init_hash_table ();
152 slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
153 new_entry->next = (lto_symtab_entry_t) *slot;
157 /* Get the lto_symtab_entry_def struct associated with ID
160 static lto_symtab_entry_t
161 lto_symtab_get (tree id)
163 struct lto_symtab_entry_def temp;
166 lto_symtab_maybe_init_hash_table ();
168 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
169 return slot ? (lto_symtab_entry_t) *slot : NULL;
172 /* Get the linker resolution for DECL. */
174 enum ld_plugin_symbol_resolution
175 lto_symtab_get_resolution (tree decl)
177 lto_symtab_entry_t e;
179 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
181 e = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
182 while (e && e->decl != decl)
187 return e->resolution;
191 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
192 all edges and removing the old node. */
195 lto_cgraph_replace_node (struct cgraph_node *node,
196 struct cgraph_node *prevailing_node)
198 struct cgraph_edge *e, *next;
200 /* Merge node flags. */
202 cgraph_mark_needed_node (prevailing_node);
204 cgraph_mark_reachable_node (prevailing_node);
205 if (node->address_taken)
207 gcc_assert (!prevailing_node->global.inlined_to);
208 cgraph_mark_address_taken_node (prevailing_node);
211 /* Redirect all incoming edges. */
212 for (e = node->callers; e; e = next)
214 next = e->next_caller;
215 cgraph_redirect_edge_callee (e, prevailing_node);
217 /* Redirect incomming references. */
218 ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
222 struct cgraph_node *alias;
224 for (alias = node->same_body; alias; alias = alias->next)
225 if (DECL_ASSEMBLER_NAME_SET_P (alias->decl))
227 lto_symtab_entry_t se
228 = lto_symtab_get (DECL_ASSEMBLER_NAME (alias->decl));
230 for (; se; se = se->next)
231 if (se->node == node)
239 /* Finally remove the replaced node. */
240 cgraph_remove_node (node);
243 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
244 all edges and removing the old node. */
247 lto_varpool_replace_node (struct varpool_node *vnode,
248 struct varpool_node *prevailing_node)
250 /* Merge node flags. */
253 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
254 varpool_mark_needed_node (prevailing_node);
256 /* Relink aliases. */
257 if (vnode->extra_name && !vnode->alias)
259 struct varpool_node *alias, *last;
260 for (alias = vnode->extra_name;
261 alias; alias = alias->next)
264 alias->extra_name = prevailing_node;
267 if (prevailing_node->extra_name)
269 last->next = prevailing_node->extra_name;
270 prevailing_node->extra_name->prev = last;
272 prevailing_node->extra_name = vnode->extra_name;
273 vnode->extra_name = NULL;
275 gcc_assert (!vnode->finalized || prevailing_node->finalized);
276 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
278 /* When replacing by an alias, the references goes to the original
280 if (prevailing_node->alias && prevailing_node->extra_name)
281 prevailing_node = prevailing_node->extra_name;
282 ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
284 /* Finally remove the replaced node. */
285 varpool_remove_node (vnode);
288 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
289 Return false if the symbols are not fully compatible and a diagnostic
290 should be emitted. */
293 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
295 tree prevailing_decl = prevailing->decl;
296 tree decl = entry->decl;
297 tree prevailing_type, type;
299 /* Merge decl state in both directions, we may still end up using
301 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
302 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
304 /* The linker may ask us to combine two incompatible symbols.
305 Detect this case and notify the caller of required diagnostics. */
307 if (TREE_CODE (decl) == FUNCTION_DECL)
309 if (TREE_TYPE (prevailing_decl) != TREE_TYPE (decl))
310 /* If we don't have a merged type yet...sigh. The linker
311 wouldn't complain if the types were mismatched, so we
312 probably shouldn't either. Just use the type from
313 whichever decl appears to be associated with the
314 definition. If for some odd reason neither decl is, the
321 /* Now we exclusively deal with VAR_DECLs. */
323 /* Sharing a global symbol is a strong hint that two types are
324 compatible. We could use this information to complete
325 incomplete pointed-to types more aggressively here, ignoring
326 mismatches in both field and tag names. It's difficult though
327 to guarantee that this does not have side-effects on merging
328 more compatible types from other translation units though. */
330 /* We can tolerate differences in type qualification, the
331 qualification of the prevailing definition will prevail.
332 ??? In principle we might want to only warn for structurally
333 incompatible types here, but unless we have protective measures
334 for TBAA in place that would hide useful information. */
335 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
336 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
338 /* We have to register and fetch canonical types here as the global
339 fixup process didn't yet run. */
340 prevailing_type = gimple_register_type (prevailing_type);
341 type = gimple_register_type (type);
342 if (prevailing_type != type)
344 if (COMPLETE_TYPE_P (type))
347 /* If type is incomplete then avoid warnings in the cases
348 that TBAA handles just fine. */
350 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
353 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
355 tree tem1 = TREE_TYPE (prevailing_type);
356 tree tem2 = TREE_TYPE (type);
357 while (TREE_CODE (tem1) == ARRAY_TYPE
358 && TREE_CODE (tem2) == ARRAY_TYPE)
360 tem1 = TREE_TYPE (tem1);
361 tem2 = TREE_TYPE (tem2);
364 if (TREE_CODE (tem1) != TREE_CODE (tem2))
367 if (gimple_register_type (tem1) != gimple_register_type (tem2))
371 /* Fallthru. Compatible enough. */
374 /* ??? We might want to emit a warning here if type qualification
375 differences were spotted. Do not do this unconditionally though. */
377 /* There is no point in comparing too many details of the decls here.
378 The type compatibility checks or the completing of types has properly
379 dealt with most issues. */
381 /* The following should all not invoke fatal errors as in non-LTO
382 mode the linker wouldn't complain either. Just emit warnings. */
384 /* Report a warning if user-specified alignments do not match. */
385 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
386 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
392 /* Return true if the symtab entry E can be replaced by another symtab
396 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
398 if (DECL_EXTERNAL (e->decl)
399 || DECL_COMDAT (e->decl)
400 || DECL_WEAK (e->decl))
403 if (TREE_CODE (e->decl) == VAR_DECL)
404 return (DECL_COMMON (e->decl)
405 || (!flag_no_common && !DECL_INITIAL (e->decl)));
410 /* Return true if the symtab entry E can be the prevailing one. */
413 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
415 /* The C++ frontend ends up neither setting TREE_STATIC nor
416 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
417 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
418 if (DECL_EXTERNAL (e->decl))
421 /* For functions we need a non-discarded body. */
422 if (TREE_CODE (e->decl) == FUNCTION_DECL)
423 return (e->node && e->node->analyzed);
425 /* A variable should have a size. */
426 else if (TREE_CODE (e->decl) == VAR_DECL)
430 if (e->vnode->finalized)
432 return e->vnode->alias && e->vnode->extra_name->finalized;
438 /* Resolve the symbol with the candidates in the chain *SLOT and store
439 their resolutions. */
442 lto_symtab_resolve_symbols (void **slot)
444 lto_symtab_entry_t e;
445 lto_symtab_entry_t prevailing = NULL;
447 /* Always set e->node so that edges are updated to reflect decl merging. */
448 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
450 if (TREE_CODE (e->decl) == FUNCTION_DECL)
451 e->node = cgraph_get_node (e->decl);
452 else if (TREE_CODE (e->decl) == VAR_DECL)
453 e->vnode = varpool_get_node (e->decl);
456 e = (lto_symtab_entry_t) *slot;
458 /* If the chain is already resolved there is nothing else to do. */
459 if (e->resolution != LDPR_UNKNOWN)
462 /* Find the single non-replaceable prevailing symbol and
463 diagnose ODR violations. */
464 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
466 if (!lto_symtab_resolve_can_prevail_p (e))
468 e->resolution = LDPR_RESOLVED_IR;
472 /* Set a default resolution - the final prevailing one will get
474 e->resolution = LDPR_PREEMPTED_IR;
475 if (!lto_symtab_resolve_replaceable_p (e))
479 error_at (DECL_SOURCE_LOCATION (e->decl),
480 "%qD has already been defined", e->decl);
481 inform (DECL_SOURCE_LOCATION (prevailing->decl),
482 "previously defined here");
490 /* Do a second round choosing one from the replaceable prevailing decls. */
491 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
493 if (e->resolution != LDPR_PREEMPTED_IR)
496 /* Choose the first function that can prevail as prevailing. */
497 if (TREE_CODE (e->decl) == FUNCTION_DECL)
503 /* From variables that can prevail choose the largest one. */
505 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
506 DECL_SIZE (e->decl)))
514 if (TREE_CODE (prevailing->decl) == VAR_DECL
515 && TREE_READONLY (prevailing->decl))
516 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
518 prevailing->resolution = LDPR_PREVAILING_DEF;
521 /* Merge all decls in the symbol table chain to the prevailing decl and
522 issue diagnostics about type mismatches. */
525 lto_symtab_merge_decls_2 (void **slot)
527 lto_symtab_entry_t prevailing, e;
528 VEC(tree, heap) *mismatches = NULL;
531 bool diagnosed_p = false;
533 /* Nothing to do for a single entry. */
534 prevailing = (lto_symtab_entry_t) *slot;
535 if (!prevailing->next)
538 /* Try to merge each entry with the prevailing one. */
539 for (e = prevailing->next; e; e = e->next)
541 if (!lto_symtab_merge (prevailing, e))
542 VEC_safe_push (tree, heap, mismatches, e->decl);
544 if (VEC_empty (tree, mismatches))
547 /* Diagnose all mismatched re-declarations. */
548 for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
550 if (TREE_TYPE (prevailing->decl) != TREE_TYPE (decl))
551 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
552 "type of %qD does not match original "
553 "declaration", decl);
555 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
556 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
558 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
559 "alignment of %qD is bigger than "
560 "original declaration", decl);
564 inform (DECL_SOURCE_LOCATION (prevailing->decl),
565 "previously declared here");
567 VEC_free (tree, heap, mismatches);
570 /* Helper to process the decl chain for the symbol table entry *SLOT. */
573 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
575 lto_symtab_entry_t e, prevailing;
576 bool diagnosed_p = false;
578 /* Compute the symbol resolutions. This is a no-op when using the
580 lto_symtab_resolve_symbols (slot);
582 /* Find the prevailing decl. */
583 for (prevailing = (lto_symtab_entry_t) *slot;
585 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
586 && prevailing->resolution != LDPR_PREVAILING_DEF;
587 prevailing = prevailing->next)
590 /* Assert it's the only one. */
592 for (e = prevailing->next; e; e = e->next)
593 gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
594 && e->resolution != LDPR_PREVAILING_DEF);
596 /* If there's not a prevailing symbol yet it's an external reference.
597 Happens a lot during ltrans. Choose the first symbol with a
598 cgraph or a varpool node. */
601 prevailing = (lto_symtab_entry_t) *slot;
602 /* For functions choose one with a cgraph node. */
603 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
604 while (!prevailing->node
606 prevailing = prevailing->next;
607 /* For variables chose with a priority variant with vnode
608 attached (i.e. from unit where external declaration of
609 variable is actually used).
610 When there are multiple variants, chose one with size.
611 This is needed for C++ typeinfos, for example in
612 lto/20081204-1 there are typeifos in both units, just
613 one of them do have size. */
614 if (TREE_CODE (prevailing->decl) == VAR_DECL)
616 for (e = prevailing->next; e; e = e->next)
617 if ((!prevailing->vnode && e->vnode)
618 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
619 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
620 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
625 /* Move it first in the list. */
626 if ((lto_symtab_entry_t) *slot != prevailing)
628 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
630 e->next = prevailing->next;
631 prevailing->next = (lto_symtab_entry_t) *slot;
632 *slot = (void *) prevailing;
635 /* Record the prevailing variable. */
636 if (TREE_CODE (prevailing->decl) == VAR_DECL)
637 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
639 /* Diagnose mismatched objects. */
640 for (e = prevailing->next; e; e = e->next)
642 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
645 switch (TREE_CODE (prevailing->decl))
648 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
649 error_at (DECL_SOURCE_LOCATION (e->decl),
650 "variable %qD redeclared as function", prevailing->decl);
654 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
655 error_at (DECL_SOURCE_LOCATION (e->decl),
656 "function %qD redeclared as variable", prevailing->decl);
666 inform (DECL_SOURCE_LOCATION (prevailing->decl),
667 "previously declared here");
669 /* Register and adjust types of the entries. */
670 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
671 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
673 /* Merge the chain to the single prevailing decl and diagnose
675 lto_symtab_merge_decls_2 (slot);
677 /* Drop all but the prevailing decl from the symtab. */
678 if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
679 && TREE_CODE (prevailing->decl) != VAR_DECL)
680 prevailing->next = NULL;
685 /* Resolve and merge all symbol table chains to a prevailing decl. */
688 lto_symtab_merge_decls (void)
690 lto_symtab_maybe_init_hash_table ();
691 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
694 /* Helper to process the decl chain for the symbol table entry *SLOT. */
697 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
699 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
701 if (!prevailing->next)
704 /* Replace the cgraph node of each entry with the prevailing one. */
705 for (e = prevailing->next; e; e = e->next)
709 if (e->node->decl != e->decl && e->node->same_body)
711 struct cgraph_node *alias;
713 for (alias = e->node->same_body; alias; alias = alias->next)
714 if (alias->decl == e->decl)
718 cgraph_remove_same_body_alias (alias);
722 lto_cgraph_replace_node (e->node, prevailing->node);
724 if (e->vnode != NULL)
725 lto_varpool_replace_node (e->vnode, prevailing->vnode);
728 /* Drop all but the prevailing decl from the symtab. */
729 prevailing->next = NULL;
734 /* Merge cgraph nodes according to the symbol merging done by
735 lto_symtab_merge_decls. */
738 lto_symtab_merge_cgraph_nodes (void)
740 lto_symtab_maybe_init_hash_table ();
741 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
744 /* Given the decl DECL, return the prevailing decl with the same name. */
747 lto_symtab_prevailing_decl (tree decl)
749 lto_symtab_entry_t ret;
751 /* Builtins and local symbols are their own prevailing decl. */
752 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
755 /* DECL_ABSTRACTs are their own prevailng decl. */
756 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
759 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
760 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
762 /* Walk through the list of candidates and return the one we merged to. */
763 ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
770 #include "gt-lto-symtab.h"