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 /* LTO file-data and symbol resolution for this decl. */
45 struct lto_file_decl_data * GTY((skip (""))) file_data;
46 enum ld_plugin_symbol_resolution resolution;
47 /* Pointer to the next entry with the same key. Before decl merging
48 this links all symbols from the different TUs. After decl merging
49 this links merged but incompatible decls, thus all prevailing ones
51 struct lto_symtab_entry_def *next;
53 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
55 /* A poor man's symbol table. This hashes identifier to prevailing DECL
58 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
59 param_is (struct lto_symtab_entry_def)))
60 htab_t lto_symtab_identifiers;
62 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
65 lto_symtab_entry_hash (const void *p)
67 const struct lto_symtab_entry_def *base =
68 (const struct lto_symtab_entry_def *) p;
69 return htab_hash_string (IDENTIFIER_POINTER (base->id));
72 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
73 corresponding to the same symbol. */
76 lto_symtab_entry_eq (const void *p1, const void *p2)
78 const struct lto_symtab_entry_def *base1 =
79 (const struct lto_symtab_entry_def *) p1;
80 const struct lto_symtab_entry_def *base2 =
81 (const struct lto_symtab_entry_def *) p2;
82 return (base1->id == base2->id);
85 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
86 to be marked for GC. */
89 lto_symtab_entry_marked_p (const void *p)
91 const struct lto_symtab_entry_def *base =
92 (const struct lto_symtab_entry_def *) p;
94 /* Keep this only if the decl or the chain is marked. */
95 return (ggc_marked_p (base->decl)
96 || (base->next && ggc_marked_p (base->next)));
99 /* Lazily initialize resolution hash tables. */
102 lto_symtab_maybe_init_hash_table (void)
104 if (lto_symtab_identifiers)
107 lto_symtab_identifiers =
108 htab_create_ggc (1021, lto_symtab_entry_hash,
109 lto_symtab_entry_eq, NULL);
112 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
113 and read from FILE_DATA. */
116 lto_symtab_register_decl (tree decl,
117 ld_plugin_symbol_resolution_t resolution,
118 struct lto_file_decl_data *file_data)
120 lto_symtab_entry_t new_entry;
123 /* Check that declarations reaching this function do not have
124 properties inconsistent with having external linkage. If any of
125 these asertions fail, then the object file reader has failed to
126 detect these cases and issue appropriate error messages. */
128 && TREE_PUBLIC (decl)
129 && (TREE_CODE (decl) == VAR_DECL
130 || TREE_CODE (decl) == FUNCTION_DECL)
131 && DECL_ASSEMBLER_NAME_SET_P (decl));
132 if (TREE_CODE (decl) == VAR_DECL
133 && DECL_INITIAL (decl))
134 gcc_assert (!DECL_EXTERNAL (decl)
135 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
136 if (TREE_CODE (decl) == FUNCTION_DECL)
137 gcc_assert (!DECL_ABSTRACT (decl));
139 new_entry = GGC_CNEW (struct lto_symtab_entry_def);
140 new_entry->id = DECL_ASSEMBLER_NAME (decl);
141 new_entry->decl = decl;
142 new_entry->resolution = resolution;
143 new_entry->file_data = file_data;
145 lto_symtab_maybe_init_hash_table ();
146 slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
147 new_entry->next = (lto_symtab_entry_t) *slot;
151 /* Get the lto_symtab_entry_def struct associated with ID
154 static lto_symtab_entry_t
155 lto_symtab_get (tree id)
157 struct lto_symtab_entry_def temp;
160 lto_symtab_maybe_init_hash_table ();
162 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
163 return slot ? (lto_symtab_entry_t) *slot : NULL;
166 /* Get the linker resolution for DECL. */
168 enum ld_plugin_symbol_resolution
169 lto_symtab_get_resolution (tree decl)
171 lto_symtab_entry_t e;
173 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
175 e = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
176 while (e && e->decl != decl)
181 return e->resolution;
185 static bool maybe_merge_incomplete_and_complete_type (tree, tree);
187 /* Try to merge an incomplete type INCOMPLETE with a complete type
188 COMPLETE of same kinds.
189 Return true if they were merged, false otherwise. */
192 merge_incomplete_and_complete_type (tree incomplete, tree complete)
194 /* For merging array types do some extra sanity checking. */
195 if (TREE_CODE (incomplete) == ARRAY_TYPE
196 && !maybe_merge_incomplete_and_complete_type (TREE_TYPE (incomplete),
197 TREE_TYPE (complete))
198 && !gimple_types_compatible_p (TREE_TYPE (incomplete),
199 TREE_TYPE (complete)))
202 /* ??? Ideally we would do this by means of a common canonical type, but
203 that's difficult as we do not have links from the canonical type
204 back to all its children. */
205 gimple_force_type_merge (incomplete, complete);
210 /* Try to merge a maybe complete / incomplete type pair TYPE1 and TYPE2.
211 Return true if they were merged, false otherwise. */
214 maybe_merge_incomplete_and_complete_type (tree type1, tree type2)
218 if (TREE_CODE (type1) != TREE_CODE (type2))
221 if (!COMPLETE_TYPE_P (type1) && COMPLETE_TYPE_P (type2))
222 res = merge_incomplete_and_complete_type (type1, type2);
223 else if (COMPLETE_TYPE_P (type1) && !COMPLETE_TYPE_P (type2))
224 res = merge_incomplete_and_complete_type (type2, type1);
226 /* Recurse on pointer targets. */
228 && POINTER_TYPE_P (type1)
229 && POINTER_TYPE_P (type2))
230 res = maybe_merge_incomplete_and_complete_type (TREE_TYPE (type1),
236 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
237 all edges and removing the old node. */
240 lto_cgraph_replace_node (struct cgraph_node *node,
241 struct cgraph_node *prevailing_node)
243 struct cgraph_edge *e, *next;
245 /* Merge node flags. */
247 cgraph_mark_needed_node (prevailing_node);
249 cgraph_mark_reachable_node (prevailing_node);
250 if (node->address_taken)
252 gcc_assert (!prevailing_node->global.inlined_to);
253 cgraph_mark_address_taken_node (prevailing_node);
256 /* Redirect all incoming edges. */
257 for (e = node->callers; e; e = next)
259 next = e->next_caller;
260 cgraph_redirect_edge_callee (e, prevailing_node);
263 /* There are not supposed to be any outgoing edges from a node we
264 replace. Still this can happen for multiple instances of weak
266 for (e = node->callees; e; e = next)
268 next = e->next_callee;
269 cgraph_remove_edge (e);
272 /* Finally remove the replaced node. */
273 cgraph_remove_node (node);
276 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
277 Return false if the symbols are not fully compatible and a diagnostic
278 should be emitted. */
281 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
283 tree prevailing_decl = prevailing->decl;
284 tree decl = entry->decl;
285 tree prevailing_type, type;
286 struct cgraph_node *node;
288 /* Merge decl state in both directions, we may still end up using
290 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
291 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
293 /* Replace a cgraph node of entry with the prevailing one. */
294 if (TREE_CODE (decl) == FUNCTION_DECL
295 && (node = cgraph_get_node (decl)) != NULL)
296 lto_cgraph_replace_node (node, cgraph_get_node (prevailing_decl));
298 /* The linker may ask us to combine two incompatible symbols.
299 Detect this case and notify the caller of required diagnostics. */
301 if (TREE_CODE (decl) == FUNCTION_DECL)
303 if (!gimple_types_compatible_p (TREE_TYPE (prevailing_decl),
305 /* If we don't have a merged type yet...sigh. The linker
306 wouldn't complain if the types were mismatched, so we
307 probably shouldn't either. Just use the type from
308 whichever decl appears to be associated with the
309 definition. If for some odd reason neither decl is, the
316 /* Now we exclusively deal with VAR_DECLs. */
318 /* Handle external declarations with incomplete type or pointed-to
319 incomplete types by forcefully merging the types.
320 ??? In principle all types involved in the two decls should
321 be merged forcefully, for example without considering type or
323 prevailing_type = TREE_TYPE (prevailing_decl);
324 type = TREE_TYPE (decl);
326 /* If the types are structurally equivalent we can use the knowledge
327 that both bind to the same symbol to complete incomplete types
328 of external declarations or of pointer targets.
329 ??? We should apply this recursively to aggregate members here
330 and get rid of the completion in gimple_types_compatible_p. */
331 if (DECL_EXTERNAL (prevailing_decl) || DECL_EXTERNAL (decl))
332 maybe_merge_incomplete_and_complete_type (prevailing_type, type);
333 else if (POINTER_TYPE_P (prevailing_type)
334 && POINTER_TYPE_P (type))
335 maybe_merge_incomplete_and_complete_type (TREE_TYPE (prevailing_type),
338 /* We can tolerate differences in type qualification, the
339 qualification of the prevailing definition will prevail. */
340 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
341 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
342 if (!gimple_types_compatible_p (prevailing_type, type))
345 /* ??? We might want to emit a warning here if type qualification
346 differences were spotted. Do not do this unconditionally though. */
348 /* There is no point in comparing too many details of the decls here.
349 The type compatibility checks or the completing of types has properly
350 dealt with most issues. */
352 /* The following should all not invoke fatal errors as in non-LTO
353 mode the linker wouldn't complain either. Just emit warnings. */
355 /* Report a warning if user-specified alignments do not match. */
356 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
357 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
363 /* Return true if the symtab entry E can be replaced by another symtab
367 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
369 if (DECL_EXTERNAL (e->decl)
370 || DECL_COMDAT (e->decl)
371 || DECL_WEAK (e->decl))
374 if (TREE_CODE (e->decl) == VAR_DECL)
375 return (DECL_COMMON (e->decl)
376 || (!flag_no_common && !DECL_INITIAL (e->decl)));
381 /* Return true if the symtab entry E can be the prevailing one. */
384 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
386 struct cgraph_node *node;
388 if (!TREE_STATIC (e->decl))
391 /* For functions we need a non-discarded body. */
392 if (TREE_CODE (e->decl) == FUNCTION_DECL)
393 return ((node = cgraph_get_node (e->decl))
396 /* A variable should have a size. */
397 else if (TREE_CODE (e->decl) == VAR_DECL)
398 return (DECL_SIZE (e->decl) != NULL_TREE
399 /* The C++ frontend retains TREE_STATIC on the declaration
400 of foo_ in struct Foo { static Foo *foo_; }; but it is
401 not a definition. g++.dg/lto/20090315_0.C. */
402 && !DECL_EXTERNAL (e->decl));
407 /* Resolve the symbol with the candidates in the chain *SLOT and store
408 their resolutions. */
411 lto_symtab_resolve_symbols (void **slot)
413 lto_symtab_entry_t e = (lto_symtab_entry_t) *slot;
414 lto_symtab_entry_t prevailing = NULL;
416 /* If the chain is already resolved there is nothing to do. */
417 if (e->resolution != LDPR_UNKNOWN)
420 /* Find the single non-replaceable prevailing symbol and
421 diagnose ODR violations. */
422 for (; e; e = e->next)
424 if (!lto_symtab_resolve_can_prevail_p (e))
426 e->resolution = LDPR_RESOLVED_IR;
430 /* Set a default resolution - the final prevailing one will get
432 e->resolution = LDPR_PREEMPTED_IR;
433 if (!lto_symtab_resolve_replaceable_p (e))
437 error_at (DECL_SOURCE_LOCATION (e->decl),
438 "%qD has already been defined", e->decl);
439 inform (DECL_SOURCE_LOCATION (prevailing->decl),
440 "previously defined here");
448 /* Do a second round choosing one from the replaceable prevailing decls. */
449 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
451 if (e->resolution != LDPR_PREEMPTED_IR)
454 /* Choose the first function that can prevail as prevailing. */
455 if (TREE_CODE (e->decl) == FUNCTION_DECL)
461 /* From variables that can prevail choose the largest one. */
463 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
464 DECL_SIZE (e->decl)))
472 if (TREE_CODE (prevailing->decl) == VAR_DECL
473 && TREE_READONLY (prevailing->decl))
474 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
476 prevailing->resolution = LDPR_PREVAILING_DEF;
479 /* Merge all decls in the symbol table chain to the prevailing decl and
480 issue diagnostics about type mismatches. */
483 lto_symtab_merge_decls_2 (void **slot)
485 lto_symtab_entry_t prevailing, e;
486 VEC(tree, heap) *mismatches = NULL;
489 bool diagnosed_p = false;
491 /* Nothing to do for a single entry. */
492 prevailing = (lto_symtab_entry_t) *slot;
493 if (!prevailing->next)
496 /* Try to merge each entry with the prevailing one. */
497 for (e = prevailing->next; e; e = e->next)
499 if (!lto_symtab_merge (prevailing, e))
500 VEC_safe_push (tree, heap, mismatches, e->decl);
502 if (VEC_empty (tree, mismatches))
505 /* Diagnose all mismatched re-declarations. */
506 for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
508 if (!gimple_types_compatible_p (TREE_TYPE (prevailing->decl),
510 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
511 "type of %qD does not match original "
512 "declaration", decl);
514 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
515 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
517 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
518 "alignment of %qD is bigger than "
519 "original declaration", decl);
523 inform (DECL_SOURCE_LOCATION (prevailing->decl),
524 "previously declared here");
526 VEC_free (tree, heap, mismatches);
529 /* Helper to process the decl chain for the symbol table entry *SLOT. */
532 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
534 lto_symtab_entry_t e, prevailing;
535 bool diagnosed_p = false;
537 /* Compute the symbol resolutions. This is a no-op when using the
539 lto_symtab_resolve_symbols (slot);
541 /* Find the prevailing decl. */
542 for (prevailing = (lto_symtab_entry_t) *slot;
544 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
545 && prevailing->resolution != LDPR_PREVAILING_DEF;
546 prevailing = prevailing->next)
549 /* Assert it's the only one. */
551 for (e = prevailing->next; e; e = e->next)
552 gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
553 && e->resolution != LDPR_PREVAILING_DEF);
555 /* If there's not a prevailing symbol yet it's an external reference.
556 Happens a lot during ltrans. Choose the first symbol with a
557 cgraph or a varpool node. */
560 prevailing = (lto_symtab_entry_t) *slot;
561 /* For functions choose one with a cgraph node. */
562 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
563 while (!cgraph_get_node (prevailing->decl)
565 prevailing = prevailing->next;
566 /* We do not stream varpool nodes, so the first decl has to
567 be good enough for now.
568 ??? For QOI choose a variable with readonly initializer
569 if there is one. This matches C++
570 struct Foo { static const int i = 1; }; without a real
572 if (TREE_CODE (prevailing->decl) == VAR_DECL)
573 while (!(TREE_READONLY (prevailing->decl)
574 && DECL_INITIAL (prevailing->decl))
576 prevailing = prevailing->next;
579 /* Move it first in the list. */
580 if ((lto_symtab_entry_t) *slot != prevailing)
582 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
584 e->next = prevailing->next;
585 prevailing->next = (lto_symtab_entry_t) *slot;
586 *slot = (void *) prevailing;
589 /* Record the prevailing variable. */
590 if (TREE_CODE (prevailing->decl) == VAR_DECL)
591 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
593 /* Diagnose mismatched objects. */
594 for (e = prevailing->next; e; e = e->next)
596 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
599 switch (TREE_CODE (prevailing->decl))
602 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
603 error_at (DECL_SOURCE_LOCATION (e->decl),
604 "variable %qD redeclared as function", prevailing->decl);
608 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
609 error_at (DECL_SOURCE_LOCATION (e->decl),
610 "function %qD redeclared as variable", prevailing->decl);
620 inform (DECL_SOURCE_LOCATION (prevailing->decl),
621 "previously declared here");
623 /* Register and adjust types of the entries. */
624 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
625 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
627 /* Merge the chain to the single prevailing decl and diagnose
629 lto_symtab_merge_decls_2 (slot);
631 /* Drop all but the prevailing decl from the symtab. */
632 prevailing->next = NULL;
637 /* Resolve and merge all symbol table chains to a prevailing decl. */
640 lto_symtab_merge_decls (void)
642 lto_symtab_maybe_init_hash_table ();
643 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
647 /* Given the decl DECL, return the prevailing decl with the same name. */
650 lto_symtab_prevailing_decl (tree decl)
652 lto_symtab_entry_t ret;
654 /* Builtins and local symbols are their own prevailing decl. */
655 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
658 /* DECL_ABSTRACTs are their own prevailng decl. */
659 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
662 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
663 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
665 /* Walk through the list of candidates and return the one we merged to. */
666 ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
673 /* Remove any storage used to store resolution of DECL. */
676 lto_symtab_clear_resolution (tree decl)
678 struct lto_symtab_entry_def temp;
679 lto_symtab_entry_t head;
682 if (!TREE_PUBLIC (decl))
685 /* LTO FIXME: There should be no DECL_ABSTRACT in the middle end. */
686 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
689 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
691 lto_symtab_maybe_init_hash_table ();
692 temp.id = DECL_ASSEMBLER_NAME (decl);
693 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
697 head = (lto_symtab_entry_t) *slot;
698 if (head->decl == decl)
706 htab_remove_elt (lto_symtab_identifiers, &temp);
710 lto_symtab_entry_t e;
711 while (head->next && head->next->decl != decl)
716 head->next = e->next;
722 #include "gt-lto-symtab.h"