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
);
226 struct cgraph_node
*alias
;
228 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
229 if (DECL_ASSEMBLER_NAME_SET_P (alias
->decl
))
231 lto_symtab_entry_t se
232 = lto_symtab_get (DECL_ASSEMBLER_NAME (alias
->decl
));
234 for (; se
; se
= se
->next
)
235 if (se
->node
== node
)
243 /* Finally remove the replaced node. */
244 cgraph_remove_node (node
);
247 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
248 Return false if the symbols are not fully compatible and a diagnostic
249 should be emitted. */
252 lto_symtab_merge (lto_symtab_entry_t prevailing
, lto_symtab_entry_t entry
)
254 tree prevailing_decl
= prevailing
->decl
;
255 tree decl
= entry
->decl
;
256 tree prevailing_type
, type
;
258 /* Merge decl state in both directions, we may still end up using
260 TREE_ADDRESSABLE (prevailing_decl
) |= TREE_ADDRESSABLE (decl
);
261 TREE_ADDRESSABLE (decl
) |= TREE_ADDRESSABLE (prevailing_decl
);
263 /* The linker may ask us to combine two incompatible symbols.
264 Detect this case and notify the caller of required diagnostics. */
266 if (TREE_CODE (decl
) == FUNCTION_DECL
)
268 if (TREE_TYPE (prevailing_decl
) != TREE_TYPE (decl
))
269 /* If we don't have a merged type yet...sigh. The linker
270 wouldn't complain if the types were mismatched, so we
271 probably shouldn't either. Just use the type from
272 whichever decl appears to be associated with the
273 definition. If for some odd reason neither decl is, the
280 /* Now we exclusively deal with VAR_DECLs. */
282 /* Sharing a global symbol is a strong hint that two types are
283 compatible. We could use this information to complete
284 incomplete pointed-to types more aggressively here, ignoring
285 mismatches in both field and tag names. It's difficult though
286 to guarantee that this does not have side-effects on merging
287 more compatible types from other translation units though. */
289 /* We can tolerate differences in type qualification, the
290 qualification of the prevailing definition will prevail.
291 ??? In principle we might want to only warn for structurally
292 incompatible types here, but unless we have protective measures
293 for TBAA in place that would hide useful information. */
294 prevailing_type
= TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl
));
295 type
= TYPE_MAIN_VARIANT (TREE_TYPE (decl
));
297 /* We have to register and fetch canonical types here as the global
298 fixup process didn't yet run. */
299 prevailing_type
= gimple_register_type (prevailing_type
);
300 type
= gimple_register_type (type
);
301 if (prevailing_type
!= type
)
303 if (COMPLETE_TYPE_P (type
))
306 /* If type is incomplete then avoid warnings in the cases
307 that TBAA handles just fine. */
309 if (TREE_CODE (prevailing_type
) != TREE_CODE (type
))
312 if (TREE_CODE (prevailing_type
) == ARRAY_TYPE
)
314 tree tem1
= TREE_TYPE (prevailing_type
);
315 tree tem2
= TREE_TYPE (type
);
316 while (TREE_CODE (tem1
) == ARRAY_TYPE
317 && TREE_CODE (tem2
) == ARRAY_TYPE
)
319 tem1
= TREE_TYPE (tem1
);
320 tem2
= TREE_TYPE (tem2
);
323 if (TREE_CODE (tem1
) != TREE_CODE (tem2
))
326 if (gimple_register_type (tem1
) != gimple_register_type (tem2
))
330 /* Fallthru. Compatible enough. */
333 /* ??? We might want to emit a warning here if type qualification
334 differences were spotted. Do not do this unconditionally though. */
336 /* There is no point in comparing too many details of the decls here.
337 The type compatibility checks or the completing of types has properly
338 dealt with most issues. */
340 /* The following should all not invoke fatal errors as in non-LTO
341 mode the linker wouldn't complain either. Just emit warnings. */
343 /* Report a warning if user-specified alignments do not match. */
344 if ((DECL_USER_ALIGN (prevailing_decl
) && DECL_USER_ALIGN (decl
))
345 && DECL_ALIGN (prevailing_decl
) < DECL_ALIGN (decl
))
351 /* Return true if the symtab entry E can be replaced by another symtab
355 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e
)
357 if (DECL_EXTERNAL (e
->decl
)
358 || DECL_COMDAT (e
->decl
)
359 || DECL_WEAK (e
->decl
))
362 if (TREE_CODE (e
->decl
) == VAR_DECL
)
363 return (DECL_COMMON (e
->decl
)
364 || (!flag_no_common
&& !DECL_INITIAL (e
->decl
)));
369 /* Return true if the symtab entry E can be the prevailing one. */
372 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e
)
374 /* The C++ frontend ends up neither setting TREE_STATIC nor
375 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
376 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
377 if (DECL_EXTERNAL (e
->decl
))
380 /* For functions we need a non-discarded body. */
381 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
382 return (e
->node
&& e
->node
->analyzed
);
384 /* A variable should have a size. */
385 else if (TREE_CODE (e
->decl
) == VAR_DECL
)
386 return (DECL_SIZE (e
->decl
) != NULL_TREE
387 /* The C++ frontend retains TREE_STATIC on the declaration
388 of foo_ in struct Foo { static Foo *foo_; }; but it is
389 not a definition. g++.dg/lto/20090315_0.C. */
390 && !DECL_EXTERNAL (e
->decl
));
395 /* Resolve the symbol with the candidates in the chain *SLOT and store
396 their resolutions. */
399 lto_symtab_resolve_symbols (void **slot
)
401 lto_symtab_entry_t e
;
402 lto_symtab_entry_t prevailing
= NULL
;
404 /* Always set e->node so that edges are updated to reflect decl merging. */
405 for (e
= (lto_symtab_entry_t
) *slot
; e
; e
= e
->next
)
407 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
408 e
->node
= cgraph_get_node (e
->decl
);
411 e
= (lto_symtab_entry_t
) *slot
;
413 /* If the chain is already resolved there is nothing else to do. */
414 if (e
->resolution
!= LDPR_UNKNOWN
)
417 /* Find the single non-replaceable prevailing symbol and
418 diagnose ODR violations. */
419 for (e
= (lto_symtab_entry_t
) *slot
; e
; e
= e
->next
)
421 if (!lto_symtab_resolve_can_prevail_p (e
))
423 e
->resolution
= LDPR_RESOLVED_IR
;
427 /* Set a default resolution - the final prevailing one will get
429 e
->resolution
= LDPR_PREEMPTED_IR
;
430 if (!lto_symtab_resolve_replaceable_p (e
))
434 error_at (DECL_SOURCE_LOCATION (e
->decl
),
435 "%qD has already been defined", e
->decl
);
436 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
437 "previously defined here");
445 /* Do a second round choosing one from the replaceable prevailing decls. */
446 for (e
= (lto_symtab_entry_t
) *slot
; e
; e
= e
->next
)
448 if (e
->resolution
!= LDPR_PREEMPTED_IR
)
451 /* Choose the first function that can prevail as prevailing. */
452 if (TREE_CODE (e
->decl
) == FUNCTION_DECL
)
458 /* From variables that can prevail choose the largest one. */
460 || tree_int_cst_lt (DECL_SIZE (prevailing
->decl
),
461 DECL_SIZE (e
->decl
)))
469 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
470 && TREE_READONLY (prevailing
->decl
))
471 prevailing
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
473 prevailing
->resolution
= LDPR_PREVAILING_DEF
;
476 /* Merge all decls in the symbol table chain to the prevailing decl and
477 issue diagnostics about type mismatches. */
480 lto_symtab_merge_decls_2 (void **slot
)
482 lto_symtab_entry_t prevailing
, e
;
483 VEC(tree
, heap
) *mismatches
= NULL
;
486 bool diagnosed_p
= false;
488 /* Nothing to do for a single entry. */
489 prevailing
= (lto_symtab_entry_t
) *slot
;
490 if (!prevailing
->next
)
493 /* Try to merge each entry with the prevailing one. */
494 for (e
= prevailing
->next
; e
; e
= e
->next
)
496 if (!lto_symtab_merge (prevailing
, e
))
497 VEC_safe_push (tree
, heap
, mismatches
, e
->decl
);
499 if (VEC_empty (tree
, mismatches
))
502 /* Diagnose all mismatched re-declarations. */
503 for (i
= 0; VEC_iterate (tree
, mismatches
, i
, decl
); ++i
)
505 if (TREE_TYPE (prevailing
->decl
) != TREE_TYPE (decl
))
506 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
507 "type of %qD does not match original "
508 "declaration", decl
);
510 else if ((DECL_USER_ALIGN (prevailing
->decl
) && DECL_USER_ALIGN (decl
))
511 && DECL_ALIGN (prevailing
->decl
) < DECL_ALIGN (decl
))
513 diagnosed_p
|= warning_at (DECL_SOURCE_LOCATION (decl
), 0,
514 "alignment of %qD is bigger than "
515 "original declaration", decl
);
519 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
520 "previously declared here");
522 VEC_free (tree
, heap
, mismatches
);
525 /* Helper to process the decl chain for the symbol table entry *SLOT. */
528 lto_symtab_merge_decls_1 (void **slot
, void *data ATTRIBUTE_UNUSED
)
530 lto_symtab_entry_t e
, prevailing
;
531 bool diagnosed_p
= false;
533 /* Compute the symbol resolutions. This is a no-op when using the
535 lto_symtab_resolve_symbols (slot
);
537 /* Find the prevailing decl. */
538 for (prevailing
= (lto_symtab_entry_t
) *slot
;
540 && prevailing
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
541 && prevailing
->resolution
!= LDPR_PREVAILING_DEF
;
542 prevailing
= prevailing
->next
)
545 /* Assert it's the only one. */
547 for (e
= prevailing
->next
; e
; e
= e
->next
)
548 gcc_assert (e
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
549 && e
->resolution
!= LDPR_PREVAILING_DEF
);
551 /* If there's not a prevailing symbol yet it's an external reference.
552 Happens a lot during ltrans. Choose the first symbol with a
553 cgraph or a varpool node. */
556 prevailing
= (lto_symtab_entry_t
) *slot
;
557 /* For functions choose one with a cgraph node. */
558 if (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
)
559 while (!prevailing
->node
561 prevailing
= prevailing
->next
;
562 /* We do not stream varpool nodes, so the first decl has to
563 be good enough for now.
564 ??? For QOI choose a variable with readonly initializer
565 if there is one. This matches C++
566 struct Foo { static const int i = 1; }; without a real
568 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
569 while (!(TREE_READONLY (prevailing
->decl
)
570 && DECL_INITIAL (prevailing
->decl
))
572 prevailing
= prevailing
->next
;
575 /* Move it first in the list. */
576 if ((lto_symtab_entry_t
) *slot
!= prevailing
)
578 for (e
= (lto_symtab_entry_t
) *slot
; e
->next
!= prevailing
; e
= e
->next
)
580 e
->next
= prevailing
->next
;
581 prevailing
->next
= (lto_symtab_entry_t
) *slot
;
582 *slot
= (void *) prevailing
;
585 /* Record the prevailing variable. */
586 if (TREE_CODE (prevailing
->decl
) == VAR_DECL
)
587 VEC_safe_push (tree
, gc
, lto_global_var_decls
, prevailing
->decl
);
589 /* Diagnose mismatched objects. */
590 for (e
= prevailing
->next
; e
; e
= e
->next
)
592 if (TREE_CODE (prevailing
->decl
) == TREE_CODE (e
->decl
))
595 switch (TREE_CODE (prevailing
->decl
))
598 gcc_assert (TREE_CODE (e
->decl
) == FUNCTION_DECL
);
599 error_at (DECL_SOURCE_LOCATION (e
->decl
),
600 "variable %qD redeclared as function", prevailing
->decl
);
604 gcc_assert (TREE_CODE (e
->decl
) == VAR_DECL
);
605 error_at (DECL_SOURCE_LOCATION (e
->decl
),
606 "function %qD redeclared as variable", prevailing
->decl
);
616 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
617 "previously declared here");
619 /* Register and adjust types of the entries. */
620 for (e
= (lto_symtab_entry_t
) *slot
; e
; e
= e
->next
)
621 TREE_TYPE (e
->decl
) = gimple_register_type (TREE_TYPE (e
->decl
));
623 /* Merge the chain to the single prevailing decl and diagnose
625 lto_symtab_merge_decls_2 (slot
);
627 /* Drop all but the prevailing decl from the symtab. */
628 if (TREE_CODE (prevailing
->decl
) != FUNCTION_DECL
)
629 prevailing
->next
= NULL
;
634 /* Resolve and merge all symbol table chains to a prevailing decl. */
637 lto_symtab_merge_decls (void)
639 lto_symtab_maybe_init_hash_table ();
640 htab_traverse (lto_symtab_identifiers
, lto_symtab_merge_decls_1
, NULL
);
643 /* Helper to process the decl chain for the symbol table entry *SLOT. */
646 lto_symtab_merge_cgraph_nodes_1 (void **slot
, void *data ATTRIBUTE_UNUSED
)
648 lto_symtab_entry_t e
, prevailing
= (lto_symtab_entry_t
) *slot
;
650 if (!prevailing
->next
)
653 gcc_assert (TREE_CODE (prevailing
->decl
) == FUNCTION_DECL
);
655 /* Replace the cgraph node of each entry with the prevailing one. */
656 for (e
= prevailing
->next
; e
; e
= e
->next
)
660 if (e
->node
->decl
!= e
->decl
&& e
->node
->same_body
)
662 struct cgraph_node
*alias
;
664 for (alias
= e
->node
->same_body
; alias
; alias
= alias
->next
)
665 if (alias
->decl
== e
->decl
)
669 cgraph_remove_same_body_alias (alias
);
673 lto_cgraph_replace_node (e
->node
, prevailing
->node
);
677 /* Drop all but the prevailing decl from the symtab. */
678 prevailing
->next
= NULL
;
683 /* Merge cgraph nodes according to the symbol merging done by
684 lto_symtab_merge_decls. */
687 lto_symtab_merge_cgraph_nodes (void)
689 lto_symtab_maybe_init_hash_table ();
690 htab_traverse (lto_symtab_identifiers
, lto_symtab_merge_cgraph_nodes_1
, NULL
);
693 /* Given the decl DECL, return the prevailing decl with the same name. */
696 lto_symtab_prevailing_decl (tree decl
)
698 lto_symtab_entry_t ret
;
700 /* Builtins and local symbols are their own prevailing decl. */
701 if (!TREE_PUBLIC (decl
) || is_builtin_fn (decl
))
704 /* DECL_ABSTRACTs are their own prevailng decl. */
705 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_ABSTRACT (decl
))
708 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
709 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl
));
711 /* Walk through the list of candidates and return the one we merged to. */
712 ret
= lto_symtab_get (DECL_ASSEMBLER_NAME (decl
));
719 #include "gt-lto-symtab.h"