In gcc/: 2011-01-12 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / lto-symtab.c
blobb331d5cee61bd44b97f2f7ab67bbd447a884bfb4
1 /* LTO symbol table.
2 Copyright 2009, 2010 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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "ggc.h"
28 #include "lambda.h" /* gcd */
29 #include "hashtab.h"
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. */
41 tree id;
42 /* The symbol table entry, a DECL. */
43 tree decl;
44 /* The cgraph node if decl is a function decl. Filled in during the
45 merging process. */
46 struct cgraph_node *node;
47 /* The varpool node if decl is a variable decl. Filled in during the
48 merging process. */
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 /* True when resolution was guessed and not read from the file. */
54 bool guessed;
55 /* Pointer to the next entry with the same key. Before decl merging
56 this links all symbols from the different TUs. After decl merging
57 this links merged but incompatible decls, thus all prevailing ones
58 remaining. */
59 struct lto_symtab_entry_def *next;
61 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
63 /* A poor man's symbol table. This hashes identifier to prevailing DECL
64 if there is one. */
66 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
67 param_is (struct lto_symtab_entry_def)))
68 htab_t lto_symtab_identifiers;
70 /* Free symtab hashtable. */
72 void
73 lto_symtab_free (void)
75 htab_delete (lto_symtab_identifiers);
76 lto_symtab_identifiers = NULL;
79 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
81 static hashval_t
82 lto_symtab_entry_hash (const void *p)
84 const struct lto_symtab_entry_def *base =
85 (const struct lto_symtab_entry_def *) p;
86 return IDENTIFIER_HASH_VALUE (base->id);
89 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
90 corresponding to the same symbol. */
92 static int
93 lto_symtab_entry_eq (const void *p1, const void *p2)
95 const struct lto_symtab_entry_def *base1 =
96 (const struct lto_symtab_entry_def *) p1;
97 const struct lto_symtab_entry_def *base2 =
98 (const struct lto_symtab_entry_def *) p2;
99 return (base1->id == base2->id);
102 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
103 to be marked for GC. */
105 static int
106 lto_symtab_entry_marked_p (const void *p)
108 const struct lto_symtab_entry_def *base =
109 (const struct lto_symtab_entry_def *) p;
111 /* Keep this only if the common IDENTIFIER_NODE of the symtab chain
112 is marked which it will be if at least one of the DECLs in the
113 chain is marked. */
114 return ggc_marked_p (base->id);
117 /* Lazily initialize resolution hash tables. */
119 static void
120 lto_symtab_maybe_init_hash_table (void)
122 if (lto_symtab_identifiers)
123 return;
125 lto_symtab_identifiers =
126 htab_create_ggc (1021, lto_symtab_entry_hash,
127 lto_symtab_entry_eq, NULL);
130 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
131 and read from FILE_DATA. */
133 void
134 lto_symtab_register_decl (tree decl,
135 ld_plugin_symbol_resolution_t resolution,
136 struct lto_file_decl_data *file_data)
138 lto_symtab_entry_t new_entry;
139 void **slot;
141 /* Check that declarations reaching this function do not have
142 properties inconsistent with having external linkage. If any of
143 these asertions fail, then the object file reader has failed to
144 detect these cases and issue appropriate error messages. */
145 gcc_assert (decl
146 && TREE_PUBLIC (decl)
147 && (TREE_CODE (decl) == VAR_DECL
148 || TREE_CODE (decl) == FUNCTION_DECL)
149 && DECL_ASSEMBLER_NAME_SET_P (decl));
150 if (TREE_CODE (decl) == VAR_DECL
151 && DECL_INITIAL (decl))
152 gcc_assert (!DECL_EXTERNAL (decl)
153 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
154 if (TREE_CODE (decl) == FUNCTION_DECL)
155 gcc_assert (!DECL_ABSTRACT (decl));
157 new_entry = ggc_alloc_cleared_lto_symtab_entry_def ();
158 new_entry->id = (*targetm.asm_out.mangle_assembler_name)
159 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
160 new_entry->decl = decl;
161 new_entry->resolution = resolution;
162 new_entry->file_data = file_data;
164 lto_symtab_maybe_init_hash_table ();
165 slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
166 new_entry->next = (lto_symtab_entry_t) *slot;
167 *slot = new_entry;
170 /* Get the lto_symtab_entry_def struct associated with ID
171 if there is one. */
173 static lto_symtab_entry_t
174 lto_symtab_get (tree id)
176 struct lto_symtab_entry_def temp;
177 void **slot;
179 lto_symtab_maybe_init_hash_table ();
180 temp.id = id;
181 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
182 return slot ? (lto_symtab_entry_t) *slot : NULL;
185 /* Get the linker resolution for DECL. */
187 enum ld_plugin_symbol_resolution
188 lto_symtab_get_resolution (tree decl)
190 lto_symtab_entry_t e;
192 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
194 e = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
195 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
196 while (e && e->decl != decl)
197 e = e->next;
198 if (!e)
199 return LDPR_UNKNOWN;
201 return e->resolution;
205 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
206 all edges and removing the old node. */
208 static void
209 lto_cgraph_replace_node (struct cgraph_node *node,
210 struct cgraph_node *prevailing_node)
212 struct cgraph_edge *e, *next;
213 bool no_aliases_please = false;
214 bool compatible_p;
216 if (cgraph_dump_file)
218 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
219 " for symbol %s\n",
220 cgraph_node_name (node), node->uid,
221 cgraph_node_name (prevailing_node),
222 prevailing_node->uid,
223 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
224 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
227 if (prevailing_node->same_body_alias)
229 if (prevailing_node->thunk.thunk_p)
230 no_aliases_please = true;
231 prevailing_node = prevailing_node->same_body;
234 /* Merge node flags. */
235 if (node->needed)
236 cgraph_mark_needed_node (prevailing_node);
237 if (node->reachable)
238 cgraph_mark_reachable_node (prevailing_node);
239 if (node->address_taken)
241 gcc_assert (!prevailing_node->global.inlined_to);
242 cgraph_mark_address_taken_node (prevailing_node);
245 /* Redirect all incoming edges. */
246 compatible_p
247 = gimple_types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
248 TREE_TYPE (TREE_TYPE (node->decl)), GTC_DIAG);
249 for (e = node->callers; e; e = next)
251 next = e->next_caller;
252 cgraph_redirect_edge_callee (e, prevailing_node);
253 /* If there is a mismatch between the supposed callee return type and
254 the real one do not attempt to inline this function.
255 ??? We really need a way to match function signatures for ABI
256 compatibility and perform related promotions at inlining time. */
257 if (!compatible_p)
258 e->call_stmt_cannot_inline_p = 1;
260 /* Redirect incomming references. */
261 ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
263 /* If we have aliases, redirect them to the prevailing node. */
264 if (!node->same_body_alias && node->same_body)
266 struct cgraph_node *alias, *last;
267 /* We prevail aliases/tunks by a thunk. This is doable but
268 would need thunk combination. Hopefully no ABI changes will
269 every be crazy enough. */
270 gcc_assert (!no_aliases_please);
272 for (alias = node->same_body; alias; alias = alias->next)
274 last = alias;
275 gcc_assert (alias->same_body_alias);
276 alias->same_body = prevailing_node;
277 alias->thunk.alias = prevailing_node->decl;
279 last->next = prevailing_node->same_body;
280 /* Node with aliases is prevailed by alias.
281 We could handle this, but combining thunks together will be tricky.
282 Hopefully this does not happen. */
283 if (prevailing_node->same_body)
284 prevailing_node->same_body->previous = last;
285 prevailing_node->same_body = node->same_body;
286 node->same_body = NULL;
289 /* Finally remove the replaced node. */
290 if (node->same_body_alias)
291 cgraph_remove_same_body_alias (node);
292 else
293 cgraph_remove_node (node);
296 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
297 all edges and removing the old node. */
299 static void
300 lto_varpool_replace_node (struct varpool_node *vnode,
301 struct varpool_node *prevailing_node)
303 /* Merge node flags. */
304 if (vnode->needed)
306 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
307 varpool_mark_needed_node (prevailing_node);
309 /* Relink aliases. */
310 if (vnode->extra_name && !vnode->alias)
312 struct varpool_node *alias, *last;
313 for (alias = vnode->extra_name;
314 alias; alias = alias->next)
316 last = alias;
317 alias->extra_name = prevailing_node;
320 if (prevailing_node->extra_name)
322 last->next = prevailing_node->extra_name;
323 prevailing_node->extra_name->prev = last;
325 prevailing_node->extra_name = vnode->extra_name;
326 vnode->extra_name = NULL;
328 gcc_assert (!vnode->finalized || prevailing_node->finalized);
329 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
331 /* When replacing by an alias, the references goes to the original
332 variable. */
333 if (prevailing_node->alias && prevailing_node->extra_name)
334 prevailing_node = prevailing_node->extra_name;
335 ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
337 /* Be sure we can garbage collect the initializer. */
338 if (DECL_INITIAL (vnode->decl))
339 DECL_INITIAL (vnode->decl) = error_mark_node;
340 /* Finally remove the replaced node. */
341 varpool_remove_node (vnode);
344 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
345 Return false if the symbols are not fully compatible and a diagnostic
346 should be emitted. */
348 static bool
349 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
351 tree prevailing_decl = prevailing->decl;
352 tree decl = entry->decl;
353 tree prevailing_type, type;
355 /* Merge decl state in both directions, we may still end up using
356 the new decl. */
357 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
358 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
360 /* The linker may ask us to combine two incompatible symbols.
361 Detect this case and notify the caller of required diagnostics. */
363 if (TREE_CODE (decl) == FUNCTION_DECL)
365 if (!gimple_types_compatible_p (TREE_TYPE (prevailing_decl),
366 TREE_TYPE (decl), GTC_DIAG))
367 /* If we don't have a merged type yet...sigh. The linker
368 wouldn't complain if the types were mismatched, so we
369 probably shouldn't either. Just use the type from
370 whichever decl appears to be associated with the
371 definition. If for some odd reason neither decl is, the
372 older one wins. */
373 (void) 0;
375 return true;
378 /* Now we exclusively deal with VAR_DECLs. */
380 /* Sharing a global symbol is a strong hint that two types are
381 compatible. We could use this information to complete
382 incomplete pointed-to types more aggressively here, ignoring
383 mismatches in both field and tag names. It's difficult though
384 to guarantee that this does not have side-effects on merging
385 more compatible types from other translation units though. */
387 /* We can tolerate differences in type qualification, the
388 qualification of the prevailing definition will prevail.
389 ??? In principle we might want to only warn for structurally
390 incompatible types here, but unless we have protective measures
391 for TBAA in place that would hide useful information. */
392 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
393 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
395 /* We have to register and fetch canonical types here as the global
396 fixup process didn't yet run. */
397 prevailing_type = gimple_register_type (prevailing_type);
398 type = gimple_register_type (type);
399 if (!gimple_types_compatible_p (prevailing_type, type, GTC_DIAG))
401 if (COMPLETE_TYPE_P (type))
402 return false;
404 /* If type is incomplete then avoid warnings in the cases
405 that TBAA handles just fine. */
407 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
408 return false;
410 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
412 tree tem1 = TREE_TYPE (prevailing_type);
413 tree tem2 = TREE_TYPE (type);
414 while (TREE_CODE (tem1) == ARRAY_TYPE
415 && TREE_CODE (tem2) == ARRAY_TYPE)
417 tem1 = TREE_TYPE (tem1);
418 tem2 = TREE_TYPE (tem2);
421 if (TREE_CODE (tem1) != TREE_CODE (tem2))
422 return false;
424 if (!gimple_types_compatible_p (gimple_register_type (tem1),
425 gimple_register_type (tem2),
426 GTC_DIAG))
427 return false;
430 /* Fallthru. Compatible enough. */
433 /* ??? We might want to emit a warning here if type qualification
434 differences were spotted. Do not do this unconditionally though. */
436 /* There is no point in comparing too many details of the decls here.
437 The type compatibility checks or the completing of types has properly
438 dealt with most issues. */
440 /* The following should all not invoke fatal errors as in non-LTO
441 mode the linker wouldn't complain either. Just emit warnings. */
443 /* Report a warning if user-specified alignments do not match. */
444 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
445 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
446 return false;
448 return true;
451 /* Return true if the symtab entry E can be replaced by another symtab
452 entry. */
454 static bool
455 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
457 if (DECL_EXTERNAL (e->decl)
458 || DECL_COMDAT (e->decl)
459 || DECL_ONE_ONLY (e->decl)
460 || DECL_WEAK (e->decl))
461 return true;
463 if (TREE_CODE (e->decl) == VAR_DECL)
464 return (DECL_COMMON (e->decl)
465 || (!flag_no_common && !DECL_INITIAL (e->decl)));
467 return false;
470 /* Return true if the symtab entry E can be the prevailing one. */
472 static bool
473 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
475 /* The C++ frontend ends up neither setting TREE_STATIC nor
476 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
477 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
478 if (DECL_EXTERNAL (e->decl))
479 return false;
481 /* For functions we need a non-discarded body. */
482 if (TREE_CODE (e->decl) == FUNCTION_DECL)
483 return (e->node
484 && (e->node->analyzed
485 || (e->node->same_body_alias && e->node->same_body->analyzed)));
487 /* A variable should have a size. */
488 else if (TREE_CODE (e->decl) == VAR_DECL)
490 if (!e->vnode)
491 return false;
492 if (e->vnode->finalized)
493 return true;
494 return e->vnode->alias && e->vnode->extra_name->finalized;
497 gcc_unreachable ();
500 /* Resolve the symbol with the candidates in the chain *SLOT and store
501 their resolutions. */
503 static void
504 lto_symtab_resolve_symbols (void **slot)
506 lto_symtab_entry_t e;
507 lto_symtab_entry_t prevailing = NULL;
509 /* Always set e->node so that edges are updated to reflect decl merging. */
510 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
512 if (TREE_CODE (e->decl) == FUNCTION_DECL)
513 e->node = cgraph_get_node_or_alias (e->decl);
514 else if (TREE_CODE (e->decl) == VAR_DECL)
515 e->vnode = varpool_get_node (e->decl);
518 e = (lto_symtab_entry_t) *slot;
520 /* If the chain is already resolved there is nothing else to do. */
521 if (e->resolution != LDPR_UNKNOWN)
522 return;
524 /* Find the single non-replaceable prevailing symbol and
525 diagnose ODR violations. */
526 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
528 if (!lto_symtab_resolve_can_prevail_p (e))
530 e->resolution = LDPR_RESOLVED_IR;
531 e->guessed = true;
532 continue;
535 /* Set a default resolution - the final prevailing one will get
536 adjusted later. */
537 e->resolution = LDPR_PREEMPTED_IR;
538 e->guessed = true;
539 if (!lto_symtab_resolve_replaceable_p (e))
541 if (prevailing)
543 error_at (DECL_SOURCE_LOCATION (e->decl),
544 "%qD has already been defined", e->decl);
545 inform (DECL_SOURCE_LOCATION (prevailing->decl),
546 "previously defined here");
548 prevailing = e;
551 if (prevailing)
552 goto found;
554 /* Do a second round choosing one from the replaceable prevailing decls. */
555 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
557 if (e->resolution != LDPR_PREEMPTED_IR)
558 continue;
560 /* Choose the first function that can prevail as prevailing. */
561 if (TREE_CODE (e->decl) == FUNCTION_DECL)
563 prevailing = e;
564 break;
567 /* From variables that can prevail choose the largest one. */
568 if (!prevailing
569 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
570 DECL_SIZE (e->decl)))
571 prevailing = e;
574 if (!prevailing)
575 return;
577 found:
578 /* If current lto files represent the whole program,
579 it is correct to use LDPR_PREVALING_DEF_IRONLY.
580 If current lto files are part of whole program, internal
581 resolver doesn't know if it is LDPR_PREVAILING_DEF
582 or LDPR_PREVAILING_DEF_IRONLY. Use IRONLY conforms to
583 using -fwhole-program. Otherwise, it doesn't
584 matter using either LDPR_PREVAILING_DEF or
585 LDPR_PREVAILING_DEF_IRONLY
587 FIXME: above workaround due to gold plugin makes some
588 variables IRONLY, which are indeed PREVAILING_DEF in
589 resolution file. These variables still need manual
590 externally_visible attribute. */
591 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
592 prevailing->guessed = true;
595 /* Merge all decls in the symbol table chain to the prevailing decl and
596 issue diagnostics about type mismatches. If DIAGNOSED_P is true
597 do not issue further diagnostics.*/
599 static void
600 lto_symtab_merge_decls_2 (void **slot, bool diagnosed_p)
602 lto_symtab_entry_t prevailing, e;
603 VEC(tree, heap) *mismatches = NULL;
604 unsigned i;
605 tree decl;
607 /* Nothing to do for a single entry. */
608 prevailing = (lto_symtab_entry_t) *slot;
609 if (!prevailing->next)
610 return;
612 /* Try to merge each entry with the prevailing one. */
613 for (e = prevailing->next; e; e = e->next)
615 if (!lto_symtab_merge (prevailing, e)
616 && !diagnosed_p)
617 VEC_safe_push (tree, heap, mismatches, e->decl);
619 if (VEC_empty (tree, mismatches))
620 return;
622 /* Diagnose all mismatched re-declarations. */
623 FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
625 if (!gimple_types_compatible_p (TREE_TYPE (prevailing->decl),
626 TREE_TYPE (decl), GTC_DIAG))
627 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
628 "type of %qD does not match original "
629 "declaration", decl);
631 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
632 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
634 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
635 "alignment of %qD is bigger than "
636 "original declaration", decl);
639 if (diagnosed_p)
640 inform (DECL_SOURCE_LOCATION (prevailing->decl),
641 "previously declared here");
643 VEC_free (tree, heap, mismatches);
646 /* Helper to process the decl chain for the symbol table entry *SLOT. */
648 static int
649 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
651 lto_symtab_entry_t e, prevailing;
652 bool diagnosed_p = false;
654 /* Compute the symbol resolutions. This is a no-op when using the
655 linker plugin. */
656 lto_symtab_resolve_symbols (slot);
658 /* Find the prevailing decl. */
659 for (prevailing = (lto_symtab_entry_t) *slot;
660 prevailing
661 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
662 && prevailing->resolution != LDPR_PREVAILING_DEF;
663 prevailing = prevailing->next)
666 /* Assert it's the only one. */
667 if (prevailing)
668 for (e = prevailing->next; e; e = e->next)
670 if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
671 || e->resolution == LDPR_PREVAILING_DEF)
672 fatal_error ("multiple prevailing defs for %qE",
673 DECL_NAME (prevailing->decl));
676 /* If there's not a prevailing symbol yet it's an external reference.
677 Happens a lot during ltrans. Choose the first symbol with a
678 cgraph or a varpool node. */
679 if (!prevailing)
681 prevailing = (lto_symtab_entry_t) *slot;
682 /* For functions choose one with a cgraph node. */
683 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
684 while (!prevailing->node
685 && prevailing->next)
686 prevailing = prevailing->next;
687 /* For variables chose with a priority variant with vnode
688 attached (i.e. from unit where external declaration of
689 variable is actually used).
690 When there are multiple variants, chose one with size.
691 This is needed for C++ typeinfos, for example in
692 lto/20081204-1 there are typeifos in both units, just
693 one of them do have size. */
694 if (TREE_CODE (prevailing->decl) == VAR_DECL)
696 for (e = prevailing->next; e; e = e->next)
697 if ((!prevailing->vnode && e->vnode)
698 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
699 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
700 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
701 prevailing = e;
705 /* Move it first in the list. */
706 if ((lto_symtab_entry_t) *slot != prevailing)
708 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
710 e->next = prevailing->next;
711 prevailing->next = (lto_symtab_entry_t) *slot;
712 *slot = (void *) prevailing;
715 /* Record the prevailing variable. */
716 if (TREE_CODE (prevailing->decl) == VAR_DECL)
717 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
719 /* Diagnose mismatched objects. */
720 for (e = prevailing->next; e; e = e->next)
722 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
723 continue;
725 switch (TREE_CODE (prevailing->decl))
727 case VAR_DECL:
728 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
729 error_at (DECL_SOURCE_LOCATION (e->decl),
730 "variable %qD redeclared as function", prevailing->decl);
731 break;
733 case FUNCTION_DECL:
734 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
735 error_at (DECL_SOURCE_LOCATION (e->decl),
736 "function %qD redeclared as variable", prevailing->decl);
737 break;
739 default:
740 gcc_unreachable ();
743 diagnosed_p = true;
745 if (diagnosed_p)
746 inform (DECL_SOURCE_LOCATION (prevailing->decl),
747 "previously declared here");
749 /* Register and adjust types of the entries. */
750 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
751 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
753 /* Merge the chain to the single prevailing decl and diagnose
754 mismatches. */
755 lto_symtab_merge_decls_2 (slot, diagnosed_p);
757 /* Store resolution decision into the callgraph.
758 In LTRANS don't overwrite information we stored into callgraph at
759 WPA stage.
761 Do not bother to store guessed decisions. Generic code knows how
762 to handle UNKNOWN relocation well.
764 The problem with storing guessed decision is whether to use
765 PREVAILING_DEF or PREVAILING_DEF_IRONLY. First one would disable
766 some whole program optimizations, while ther second would imply
767 to many whole program assumptions. */
768 if (prevailing->node && !flag_ltrans && !prevailing->guessed)
769 prevailing->node->resolution = prevailing->resolution;
770 else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed)
771 prevailing->vnode->resolution = prevailing->resolution;
772 return 1;
775 /* Resolve and merge all symbol table chains to a prevailing decl. */
777 void
778 lto_symtab_merge_decls (void)
780 lto_symtab_maybe_init_hash_table ();
781 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
784 /* Helper to process the decl chain for the symbol table entry *SLOT. */
786 static int
787 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
789 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
791 if (!prevailing->next)
792 return 1;
794 /* Replace the cgraph node of each entry with the prevailing one. */
795 for (e = prevailing->next; e; e = e->next)
797 if (e->node != NULL)
799 /* In case we prevail funcion by an alias, we can run into case
800 that the alias has no cgraph node attached, since it was
801 previously unused. Create the node. */
802 if (!prevailing->node)
804 prevailing->node = cgraph_node (prevailing->decl);
805 prevailing->node->alias = true;
807 lto_cgraph_replace_node (e->node, prevailing->node);
809 if (e->vnode != NULL)
811 if (!prevailing->vnode)
813 prevailing->vnode = varpool_node (prevailing->decl);
814 prevailing->vnode->alias = true;
816 lto_varpool_replace_node (e->vnode, prevailing->vnode);
820 /* Drop all but the prevailing decl from the symtab. */
821 prevailing->next = NULL;
823 return 1;
826 /* Merge cgraph nodes according to the symbol merging done by
827 lto_symtab_merge_decls. */
829 void
830 lto_symtab_merge_cgraph_nodes (void)
832 lto_symtab_maybe_init_hash_table ();
833 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
836 /* Given the decl DECL, return the prevailing decl with the same name. */
838 tree
839 lto_symtab_prevailing_decl (tree decl)
841 lto_symtab_entry_t ret;
843 /* Builtins and local symbols are their own prevailing decl. */
844 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
845 return decl;
847 /* DECL_ABSTRACTs are their own prevailng decl. */
848 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
849 return decl;
851 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
852 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
854 /* Walk through the list of candidates and return the one we merged to. */
855 ret = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
856 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
857 if (!ret)
858 return NULL_TREE;
860 return ret->decl;
863 #include "gt-lto-symtab.h"