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 /* LTO file-data and symbol resolution for this decl. */
48 struct lto_file_decl_data
* GTY((skip (""))) file_data
;
49 enum ld_plugin_symbol_resolution resolution
;
50 /* Pointer to the next entry with the same key. Before decl merging
51 this links all symbols from the different TUs. After decl merging
52 this links merged but incompatible decls, thus all prevailing ones
54 struct lto_symtab_entry_def
*next
;
56 typedef struct lto_symtab_entry_def
*lto_symtab_entry_t
;
58 /* A poor man's symbol table. This hashes identifier to prevailing DECL
61 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
62 param_is (struct lto_symtab_entry_def
)))
63 htab_t lto_symtab_identifiers
;
65 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
68 lto_symtab_entry_hash (const void *p
)
70 const struct lto_symtab_entry_def
*base
=
71 (const struct lto_symtab_entry_def
*) p
;
72 return htab_hash_string (IDENTIFIER_POINTER (base
->id
));
75 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
76 corresponding to the same symbol. */
79 lto_symtab_entry_eq (const void *p1
, const void *p2
)
81 const struct lto_symtab_entry_def
*base1
=
82 (const struct lto_symtab_entry_def
*) p1
;
83 const struct lto_symtab_entry_def
*base2
=
84 (const struct lto_symtab_entry_def
*) p2
;
85 return (base1
->id
== base2
->id
);
88 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
89 to be marked for GC. */
92 lto_symtab_entry_marked_p (const void *p
)
94 const struct lto_symtab_entry_def
*base
=
95 (const struct lto_symtab_entry_def
*) p
;
97 /* Keep this only if the decl or the chain is marked. */
98 return (ggc_marked_p (base
->decl
)
99 || (base
->next
&& ggc_marked_p (base
->next
)));
102 /* Lazily initialize resolution hash tables. */
105 lto_symtab_maybe_init_hash_table (void)
107 if (lto_symtab_identifiers
)
110 lto_symtab_identifiers
=
111 htab_create_ggc (1021, lto_symtab_entry_hash
,
112 lto_symtab_entry_eq
, NULL
);
115 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
116 and read from FILE_DATA. */
119 lto_symtab_register_decl (tree decl
,
120 ld_plugin_symbol_resolution_t resolution
,
121 struct lto_file_decl_data
*file_data
)
123 lto_symtab_entry_t new_entry
;
126 /* Check that declarations reaching this function do not have
127 properties inconsistent with having external linkage. If any of
128 these asertions fail, then the object file reader has failed to
129 detect these cases and issue appropriate error messages. */
131 && TREE_PUBLIC (decl
)
132 && (TREE_CODE (decl
) == VAR_DECL
133 || TREE_CODE (decl
) == FUNCTION_DECL
)
134 && DECL_ASSEMBLER_NAME_SET_P (decl
));
135 if (TREE_CODE (decl
) == VAR_DECL
136 && DECL_INITIAL (decl
))
137 gcc_assert (!DECL_EXTERNAL (decl
)
138 || (TREE_STATIC (decl
) && TREE_READONLY (decl
)));
139 if (TREE_CODE (decl
) == FUNCTION_DECL
)
140 gcc_assert (!DECL_ABSTRACT (decl
));
142 new_entry
= GGC_CNEW (struct lto_symtab_entry_def
);
143 new_entry
->id
= DECL_ASSEMBLER_NAME (decl
);
144 new_entry
->decl
= decl
;
145 new_entry
->resolution
= resolution
;
146 new_entry
->file_data
= file_data
;
148 lto_symtab_maybe_init_hash_table ();
149 slot
= htab_find_slot (lto_symtab_identifiers
, new_entry
, INSERT
);
150 new_entry
->next
= (lto_symtab_entry_t
) *slot
;
154 /* Get the lto_symtab_entry_def struct associated with ID
157 static lto_symtab_entry_t
158 lto_symtab_get (tree id
)
160 struct lto_symtab_entry_def temp
;
163 lto_symtab_maybe_init_hash_table ();
165 slot
= htab_find_slot (lto_symtab_identifiers
, &temp
, NO_INSERT
);
166 return slot
? (lto_symtab_entry_t
) *slot
: NULL
;
169 /* Get the linker resolution for DECL. */
171 enum ld_plugin_symbol_resolution
172 lto_symtab_get_resolution (tree decl
)
174 lto_symtab_entry_t e
;
176 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
178 e
= lto_symtab_get (DECL_ASSEMBLER_NAME (decl
));
179 while (e
&& e
->decl
!= decl
)
184 return e
->resolution
;
188 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
189 all edges and removing the old node. */
192 lto_cgraph_replace_node (struct cgraph_node
*node
,
193 struct cgraph_node
*prevailing_node
)
195 struct cgraph_edge
*e
, *next
;
197 /* Merge node flags. */
199 cgraph_mark_needed_node (prevailing_node
);
201 cgraph_mark_reachable_node (prevailing_node
);
202 if (node
->address_taken
)
204 gcc_assert (!prevailing_node
->global
.inlined_to
);
205 cgraph_mark_address_taken_node (prevailing_node
);
208 /* Redirect all incoming edges. */
209 for (e
= node
->callers
; e
; e
= next
)
211 next
= e
->next_caller
;
212 cgraph_redirect_edge_callee (e
, prevailing_node
);
215 /* There are not supposed to be any outgoing edges from a node we
216 replace. Still this can happen for multiple instances of weak
218 for (e
= node
->callees
; e
; e
= next
)
220 next
= e
->next_callee
;
221 cgraph_remove_edge (e
);
224 /* Finally remove the replaced node. */
225 cgraph_remove_node (node
);
228 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
229 Return false if the symbols are not fully compatible and a diagnostic
230 should be emitted. */
233 lto_symtab_merge (lto_symtab_entry_t prevailing
, lto_symtab_entry_t entry
)
235 tree prevailing_decl
= prevailing
->decl
;
236 tree decl
= entry
->decl
;
237 tree prevailing_type
, type
;
239 /* Merge decl state in both directions, we may still end up using
241 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
242 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
244 /* The linker may ask us to combine two incompatible symbols.
245 Detect this case and notify the caller of required diagnostics. */
247 if (TREE_CODE (decl
) == FUNCTION_DECL
)
249 if (TREE_TYPE (prevailing_decl
) != TREE_TYPE (decl
))
250 /* If we don't have a merged type yet...sigh. The linker
251 wouldn't complain if the types were mismatched, so we
252 probably shouldn't either. Just use the type from
253 whichever decl appears to be associated with the
254 definition. If for some odd reason neither decl is, the
261 /* Now we exclusively deal with VAR_DECLs. */
263 /* Sharing a global symbol is a strong hint that two types are
264 compatible. We could use this information to complete
265 incomplete pointed-to types more aggressively here, ignoring
266 mismatches in both field and tag names. It's difficult though
267 to guarantee that this does not have side-effects on merging
268 more compatible types from other translation units though. */
270 /* We can tolerate differences in type qualification, the
271 qualification of the prevailing definition will prevail.
272 ??? In principle we might want to only warn for structurally
273 incompatible types here, but unless we have protective measures
274 for TBAA in place that would hide useful information. */
275 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
276 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
278 /* We have to register and fetch canonical types here as the global
279 fixup process didn't yet run. */
280 prevailing_type
= gimple_register_type (prevailing_type
);
281 type
= gimple_register_type (type
);
282 if (prevailing_type
!= type
)
284 if (COMPLETE_TYPE_P (type
))
287 /* If type is incomplete then avoid warnings in the cases
288 that TBAA handles just fine. */
290 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
293 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
295 tree tem1
= TREE_TYPE (prevailing_type
);
296 tree tem2
= TREE_TYPE (type
);
297 while (TREE_CODE (tem1
) == ARRAY_TYPE
298 && TREE_CODE (tem2
) == ARRAY_TYPE
)
300 tem1
= TREE_TYPE (tem1
);
301 tem2
= TREE_TYPE (tem2
);
304 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
307 if (gimple_register_type (tem1
) != gimple_register_type (tem2
))
311 /* Fallthru. Compatible enough. */
314 /* ??? We might want to emit a warning here if type qualification
315 differences were spotted. Do not do this unconditionally though. */
317 /* There is no point in comparing too many details of the decls here.
318 The type compatibility checks or the completing of types has properly
319 dealt with most issues. */
321 /* The following should all not invoke fatal errors as in non-LTO
322 mode the linker wouldn't complain either. Just emit warnings. */
324 /* Report a warning if user-specified alignments do not match. */
325 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
326 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
332 /* Return true if the symtab entry E can be replaced by another symtab
336 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e
)
338 if (DECL_EXTERNAL (e
->decl
)
339 || DECL_COMDAT (e
->decl
)
340 || DECL_WEAK (e
->decl
))
343 if (TREE_CODE (e
->decl
) == VAR_DECL
)
344 return (DECL_COMMON (e
->decl
)
345 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
350 /* Return true if the symtab entry E can be the prevailing one. */
353 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e
)
355 if (!TREE_STATIC (e
->decl
))
358 /* For functions we need a non-discarded body. */
359 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
360 return (e
->node
&& e
->node
->analyzed
);
362 /* A variable should have a size. */
363 else if (TREE_CODE (e
->decl
) == VAR_DECL
)
364 return (DECL_SIZE (e
->decl
) != NULL_TREE
365 /* The C++ frontend retains TREE_STATIC on the declaration
366 of foo_ in struct Foo { static Foo *foo_; }; but it is
367 not a definition. g++.dg/lto/20090315_0.C. */
368 && !DECL_EXTERNAL (e
->decl
));
373 /* Resolve the symbol with the candidates in the chain *SLOT and store
374 their resolutions. */
377 lto_symtab_resolve_symbols (void **slot
)
379 lto_symtab_entry_t e
= (lto_symtab_entry_t
) *slot
;
380 lto_symtab_entry_t prevailing
= NULL
;
382 /* If the chain is already resolved there is nothing to do. */
383 if (e
->resolution
!= LDPR_UNKNOWN
)
386 /* Find the single non-replaceable prevailing symbol and
387 diagnose ODR violations. */
388 for (; e
; e
= e
->next
)
390 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
391 e
->node
= cgraph_get_node (e
->decl
);
393 if (!lto_symtab_resolve_can_prevail_p (e
))
395 e
->resolution
= LDPR_RESOLVED_IR
;
399 /* Set a default resolution - the final prevailing one will get
401 e
->resolution
= LDPR_PREEMPTED_IR
;
402 if (!lto_symtab_resolve_replaceable_p (e
))
406 error_at (DECL_SOURCE_LOCATION (e
->decl
),
407 "%qD has already been defined", e
->decl
);
408 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
409 "previously defined here");
417 /* Do a second round choosing one from the replaceable prevailing decls. */
418 for (e
= (lto_symtab_entry_t
) *slot
; e
; e
= e
->next
)
420 if (e
->resolution
!= LDPR_PREEMPTED_IR
)
423 /* Choose the first function that can prevail as prevailing. */
424 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
430 /* From variables that can prevail choose the largest one. */
432 || tree_int_cst_lt (DECL_SIZE (prevailing
->decl
),
433 DECL_SIZE (e
->decl
)))
441 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
442 && TREE_READONLY (prevailing
->decl
))
443 prevailing
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
445 prevailing
->resolution
= LDPR_PREVAILING_DEF
;
448 /* Merge all decls in the symbol table chain to the prevailing decl and
449 issue diagnostics about type mismatches. */
452 lto_symtab_merge_decls_2 (void **slot
)
454 lto_symtab_entry_t prevailing
, e
;
455 VEC(tree
, heap
) *mismatches
= NULL
;
458 bool diagnosed_p
= false;
460 /* Nothing to do for a single entry. */
461 prevailing
= (lto_symtab_entry_t
) *slot
;
462 if (!prevailing
->next
)
465 /* Try to merge each entry with the prevailing one. */
466 for (e
= prevailing
->next
; e
; e
= e
->next
)
468 if (!lto_symtab_merge (prevailing
, e
))
469 VEC_safe_push (tree
, heap
, mismatches
, e
->decl
);
471 if (VEC_empty (tree
, mismatches
))
474 /* Diagnose all mismatched re-declarations. */
475 for (i
= 0; VEC_iterate (tree
, mismatches
, i
, decl
); ++i
)
477 if (TREE_TYPE (prevailing
->decl
) != TREE_TYPE (decl
))
478 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
479 "type of %qD does not match original "
480 "declaration", decl
);
482 else if ((DECL_USER_ALIGN (prevailing
->decl
) && DECL_USER_ALIGN (decl
))
483 && DECL_ALIGN (prevailing
->decl
) < DECL_ALIGN (decl
))
485 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
486 "alignment of %qD is bigger than "
487 "original declaration", decl
);
491 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
492 "previously declared here");
494 VEC_free (tree
, heap
, mismatches
);
497 /* Helper to process the decl chain for the symbol table entry *SLOT. */
500 lto_symtab_merge_decls_1 (void **slot
, void *data ATTRIBUTE_UNUSED
)
502 lto_symtab_entry_t e
, prevailing
;
503 bool diagnosed_p
= false;
505 /* Compute the symbol resolutions. This is a no-op when using the
507 lto_symtab_resolve_symbols (slot
);
509 /* Find the prevailing decl. */
510 for (prevailing
= (lto_symtab_entry_t
) *slot
;
512 && prevailing
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
513 && prevailing
->resolution
!= LDPR_PREVAILING_DEF
;
514 prevailing
= prevailing
->next
)
517 /* Assert it's the only one. */
519 for (e
= prevailing
->next
; e
; e
= e
->next
)
520 gcc_assert (e
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
521 && e
->resolution
!= LDPR_PREVAILING_DEF
);
523 /* If there's not a prevailing symbol yet it's an external reference.
524 Happens a lot during ltrans. Choose the first symbol with a
525 cgraph or a varpool node. */
528 prevailing
= (lto_symtab_entry_t
) *slot
;
529 /* For functions choose one with a cgraph node. */
530 if (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
)
531 while (!prevailing
->node
533 prevailing
= prevailing
->next
;
534 /* We do not stream varpool nodes, so the first decl has to
535 be good enough for now.
536 ??? For QOI choose a variable with readonly initializer
537 if there is one. This matches C++
538 struct Foo { static const int i = 1; }; without a real
540 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
541 while (!(TREE_READONLY (prevailing
->decl
)
542 && DECL_INITIAL (prevailing
->decl
))
544 prevailing
= prevailing
->next
;
547 /* Move it first in the list. */
548 if ((lto_symtab_entry_t
) *slot
!= prevailing
)
550 for (e
= (lto_symtab_entry_t
) *slot
; e
->next
!= prevailing
; e
= e
->next
)
552 e
->next
= prevailing
->next
;
553 prevailing
->next
= (lto_symtab_entry_t
) *slot
;
554 *slot
= (void *) prevailing
;
557 /* Record the prevailing variable. */
558 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
559 VEC_safe_push (tree
, gc
, lto_global_var_decls
, prevailing
->decl
);
561 /* Diagnose mismatched objects. */
562 for (e
= prevailing
->next
; e
; e
= e
->next
)
564 if (TREE_CODE (prevailing
->decl
) == TREE_CODE (e
->decl
))
567 switch (TREE_CODE (prevailing
->decl
))
570 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
571 error_at (DECL_SOURCE_LOCATION (e
->decl
),
572 "variable %qD redeclared as function", prevailing
->decl
);
576 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
577 error_at (DECL_SOURCE_LOCATION (e
->decl
),
578 "function %qD redeclared as variable", prevailing
->decl
);
588 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
589 "previously declared here");
591 /* Register and adjust types of the entries. */
592 for (e
= (lto_symtab_entry_t
) *slot
; e
; e
= e
->next
)
593 TREE_TYPE (e
->decl
) = gimple_register_type (TREE_TYPE (e
->decl
));
595 /* Merge the chain to the single prevailing decl and diagnose
597 lto_symtab_merge_decls_2 (slot
);
599 /* Drop all but the prevailing decl from the symtab. */
600 if (TREE_CODE (prevailing
->decl
) != FUNCTION_DECL
)
601 prevailing
->next
= NULL
;
606 /* Resolve and merge all symbol table chains to a prevailing decl. */
609 lto_symtab_merge_decls (void)
611 lto_symtab_maybe_init_hash_table ();
612 htab_traverse (lto_symtab_identifiers
, lto_symtab_merge_decls_1
, NULL
);
615 /* Helper to process the decl chain for the symbol table entry *SLOT. */
618 lto_symtab_merge_cgraph_nodes_1 (void **slot
, void *data ATTRIBUTE_UNUSED
)
620 lto_symtab_entry_t e
, prevailing
= (lto_symtab_entry_t
) *slot
;
622 if (!prevailing
->next
)
625 gcc_assert (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
);
627 /* Replace the cgraph node of each entry with the prevailing one. */
628 for (e
= prevailing
->next
; e
; e
= e
->next
)
631 lto_cgraph_replace_node (e
->node
, prevailing
->node
);
634 /* Drop all but the prevailing decl from the symtab. */
635 prevailing
->next
= NULL
;
640 /* Merge cgraph nodes according to the symbol merging done by
641 lto_symtab_merge_decls. */
644 lto_symtab_merge_cgraph_nodes (void)
646 lto_symtab_maybe_init_hash_table ();
647 htab_traverse (lto_symtab_identifiers
, lto_symtab_merge_cgraph_nodes_1
, NULL
);
650 /* Given the decl DECL, return the prevailing decl with the same name. */
653 lto_symtab_prevailing_decl (tree decl
)
655 lto_symtab_entry_t ret
;
657 /* Builtins and local symbols are their own prevailing decl. */
658 if (!TREE_PUBLIC (decl
) || is_builtin_fn (decl
))
661 /* DECL_ABSTRACTs are their own prevailng decl. */
662 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT (decl
))
665 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
666 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
668 /* Walk through the list of candidates and return the one we merged to. */
669 ret
= lto_symtab_get (DECL_ASSEMBLER_NAME (decl
));
676 #include "gt-lto-symtab.h"