2012-07-26 Kazu Hirata <kazu@codesourcery.com>
[official-gcc.git] / gcc / lto-symtab.c
blobc5efb7f0156ed430d9fbfa1089a96b59290e9832
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 "hashtab.h"
29 #include "plugin-api.h"
30 #include "lto-streamer.h"
32 /* Vector to keep track of external variables we've seen so far. */
33 VEC(tree,gc) *lto_global_var_decls;
35 /* Symbol table entry. */
37 struct GTY(()) lto_symtab_entry_def
39 /* The symbol table entry key, an IDENTIFIER. */
40 tree id;
41 /* The symbol table entry, a DECL. */
42 tree decl;
43 /* The cgraph node if decl is a function decl. Filled in during the
44 merging process. */
45 struct cgraph_node *node;
46 /* The varpool node if decl is a variable decl. Filled in during the
47 merging process. */
48 struct varpool_node *vnode;
49 /* LTO file-data and symbol resolution for this decl. */
50 struct lto_file_decl_data * GTY((skip (""))) file_data;
51 enum ld_plugin_symbol_resolution resolution;
52 /* True when resolution was guessed and not read from the file. */
53 bool guessed;
54 /* Pointer to the next entry with the same key. Before decl merging
55 this links all symbols from the different TUs. After decl merging
56 this links merged but incompatible decls, thus all prevailing ones
57 remaining. */
58 struct lto_symtab_entry_def *next;
60 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
62 /* A poor man's symbol table. This hashes identifier to prevailing DECL
63 if there is one. */
65 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
66 param_is (struct lto_symtab_entry_def)))
67 htab_t lto_symtab_identifiers;
69 /* Free symtab hashtable. */
71 void
72 lto_symtab_free (void)
74 htab_delete (lto_symtab_identifiers);
75 lto_symtab_identifiers = NULL;
78 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
80 static hashval_t
81 lto_symtab_entry_hash (const void *p)
83 const struct lto_symtab_entry_def *base =
84 (const struct lto_symtab_entry_def *) p;
85 return IDENTIFIER_HASH_VALUE (base->id);
88 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
89 corresponding to the same symbol. */
91 static int
92 lto_symtab_entry_eq (const void *p1, const void *p2)
94 const struct lto_symtab_entry_def *base1 =
95 (const struct lto_symtab_entry_def *) p1;
96 const struct lto_symtab_entry_def *base2 =
97 (const struct lto_symtab_entry_def *) p2;
98 return (base1->id == base2->id);
101 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
102 to be marked for GC. */
104 static int
105 lto_symtab_entry_marked_p (const void *p)
107 const struct lto_symtab_entry_def *base =
108 (const struct lto_symtab_entry_def *) p;
110 /* Keep this only if the common IDENTIFIER_NODE of the symtab chain
111 is marked which it will be if at least one of the DECLs in the
112 chain is marked. */
113 return ggc_marked_p (base->id);
116 /* Lazily initialize resolution hash tables. */
118 static void
119 lto_symtab_maybe_init_hash_table (void)
121 if (lto_symtab_identifiers)
122 return;
124 lto_symtab_identifiers =
125 htab_create_ggc (1021, lto_symtab_entry_hash,
126 lto_symtab_entry_eq, NULL);
129 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
130 and read from FILE_DATA. */
132 void
133 lto_symtab_register_decl (tree decl,
134 ld_plugin_symbol_resolution_t resolution,
135 struct lto_file_decl_data *file_data)
137 lto_symtab_entry_t new_entry;
138 void **slot;
140 /* Check that declarations reaching this function do not have
141 properties inconsistent with having external linkage. If any of
142 these asertions fail, then the object file reader has failed to
143 detect these cases and issue appropriate error messages. */
144 gcc_assert (decl
145 && TREE_PUBLIC (decl)
146 && (TREE_CODE (decl) == VAR_DECL
147 || TREE_CODE (decl) == FUNCTION_DECL)
148 && DECL_ASSEMBLER_NAME_SET_P (decl));
149 if (TREE_CODE (decl) == VAR_DECL
150 && DECL_INITIAL (decl))
151 gcc_assert (!DECL_EXTERNAL (decl)
152 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
153 if (TREE_CODE (decl) == FUNCTION_DECL)
154 gcc_assert (!DECL_ABSTRACT (decl));
156 new_entry = ggc_alloc_cleared_lto_symtab_entry_def ();
157 new_entry->id = (*targetm.asm_out.mangle_assembler_name)
158 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
159 new_entry->decl = decl;
160 new_entry->resolution = resolution;
161 new_entry->file_data = file_data;
163 lto_symtab_maybe_init_hash_table ();
164 slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
165 new_entry->next = (lto_symtab_entry_t) *slot;
166 *slot = new_entry;
169 /* Get the lto_symtab_entry_def struct associated with ID
170 if there is one. */
172 static lto_symtab_entry_t
173 lto_symtab_get (tree id)
175 struct lto_symtab_entry_def temp;
176 void **slot;
178 lto_symtab_maybe_init_hash_table ();
179 temp.id = id;
180 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
181 return slot ? (lto_symtab_entry_t) *slot : NULL;
184 /* Get the linker resolution for DECL. */
186 enum ld_plugin_symbol_resolution
187 lto_symtab_get_resolution (tree decl)
189 lto_symtab_entry_t e;
191 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
193 e = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
194 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
195 while (e && e->decl != decl)
196 e = e->next;
197 if (!e)
198 return LDPR_UNKNOWN;
200 return e->resolution;
204 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
205 all edges and removing the old node. */
207 static void
208 lto_cgraph_replace_node (struct cgraph_node *node,
209 struct cgraph_node *prevailing_node)
211 struct cgraph_edge *e, *next;
212 bool compatible_p;
214 if (cgraph_dump_file)
216 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
217 " for symbol %s\n",
218 xstrdup (cgraph_node_name (node)), node->uid,
219 xstrdup (cgraph_node_name (prevailing_node)),
220 prevailing_node->uid,
221 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
222 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl)))));
225 /* Merge node flags. */
226 if (node->symbol.force_output)
227 cgraph_mark_force_output_node (prevailing_node);
228 if (node->symbol.address_taken)
230 gcc_assert (!prevailing_node->global.inlined_to);
231 cgraph_mark_address_taken_node (prevailing_node);
234 /* Redirect all incoming edges. */
235 compatible_p
236 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->symbol.decl)),
237 TREE_TYPE (TREE_TYPE (node->symbol.decl)));
238 for (e = node->callers; e; e = next)
240 next = e->next_caller;
241 cgraph_redirect_edge_callee (e, prevailing_node);
242 /* If there is a mismatch between the supposed callee return type and
243 the real one do not attempt to inline this function.
244 ??? We really need a way to match function signatures for ABI
245 compatibility and perform related promotions at inlining time. */
246 if (!compatible_p)
247 e->call_stmt_cannot_inline_p = 1;
249 /* Redirect incomming references. */
250 ipa_clone_referring ((symtab_node)prevailing_node, &node->symbol.ref_list);
252 /* Finally remove the replaced node. */
253 cgraph_remove_node (node);
256 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
257 all edges and removing the old node. */
259 static void
260 lto_varpool_replace_node (struct varpool_node *vnode,
261 struct varpool_node *prevailing_node)
263 gcc_assert (!vnode->finalized || prevailing_node->finalized);
264 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
266 ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list);
268 /* Be sure we can garbage collect the initializer. */
269 if (DECL_INITIAL (vnode->symbol.decl))
270 DECL_INITIAL (vnode->symbol.decl) = error_mark_node;
271 /* Finally remove the replaced node. */
272 varpool_remove_node (vnode);
275 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
276 Return false if the symbols are not fully compatible and a diagnostic
277 should be emitted. */
279 static bool
280 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
282 tree prevailing_decl = prevailing->decl;
283 tree decl = entry->decl;
284 tree prevailing_type, type;
286 /* Merge decl state in both directions, we may still end up using
287 the new decl. */
288 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
289 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
291 /* The linker may ask us to combine two incompatible symbols.
292 Detect this case and notify the caller of required diagnostics. */
294 if (TREE_CODE (decl) == FUNCTION_DECL)
296 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
297 TREE_TYPE (decl)))
298 /* If we don't have a merged type yet...sigh. The linker
299 wouldn't complain if the types were mismatched, so we
300 probably shouldn't either. Just use the type from
301 whichever decl appears to be associated with the
302 definition. If for some odd reason neither decl is, the
303 older one wins. */
304 (void) 0;
306 return true;
309 /* Now we exclusively deal with VAR_DECLs. */
311 /* Sharing a global symbol is a strong hint that two types are
312 compatible. We could use this information to complete
313 incomplete pointed-to types more aggressively here, ignoring
314 mismatches in both field and tag names. It's difficult though
315 to guarantee that this does not have side-effects on merging
316 more compatible types from other translation units though. */
318 /* We can tolerate differences in type qualification, the
319 qualification of the prevailing definition will prevail.
320 ??? In principle we might want to only warn for structurally
321 incompatible types here, but unless we have protective measures
322 for TBAA in place that would hide useful information. */
323 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
324 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
326 if (!types_compatible_p (prevailing_type, type))
328 if (COMPLETE_TYPE_P (type))
329 return false;
331 /* If type is incomplete then avoid warnings in the cases
332 that TBAA handles just fine. */
334 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
335 return false;
337 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
339 tree tem1 = TREE_TYPE (prevailing_type);
340 tree tem2 = TREE_TYPE (type);
341 while (TREE_CODE (tem1) == ARRAY_TYPE
342 && TREE_CODE (tem2) == ARRAY_TYPE)
344 tem1 = TREE_TYPE (tem1);
345 tem2 = TREE_TYPE (tem2);
348 if (TREE_CODE (tem1) != TREE_CODE (tem2))
349 return false;
351 if (!types_compatible_p (tem1, tem2))
352 return false;
355 /* Fallthru. Compatible enough. */
358 /* ??? We might want to emit a warning here if type qualification
359 differences were spotted. Do not do this unconditionally though. */
361 /* There is no point in comparing too many details of the decls here.
362 The type compatibility checks or the completing of types has properly
363 dealt with most issues. */
365 /* The following should all not invoke fatal errors as in non-LTO
366 mode the linker wouldn't complain either. Just emit warnings. */
368 /* Report a warning if user-specified alignments do not match. */
369 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
370 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
371 return false;
373 return true;
376 /* Return true if the symtab entry E can be replaced by another symtab
377 entry. */
379 static bool
380 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
382 if (DECL_EXTERNAL (e->decl)
383 || DECL_COMDAT (e->decl)
384 || DECL_ONE_ONLY (e->decl)
385 || DECL_WEAK (e->decl))
386 return true;
388 if (TREE_CODE (e->decl) == VAR_DECL)
389 return (DECL_COMMON (e->decl)
390 || (!flag_no_common && !DECL_INITIAL (e->decl)));
392 return false;
395 /* Return true if the symtab entry E can be the prevailing one. */
397 static bool
398 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
400 /* The C++ frontend ends up neither setting TREE_STATIC nor
401 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
402 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
403 if (DECL_EXTERNAL (e->decl))
404 return false;
406 /* For functions we need a non-discarded body. */
407 if (TREE_CODE (e->decl) == FUNCTION_DECL)
408 return (e->node && e->node->analyzed);
410 else if (TREE_CODE (e->decl) == VAR_DECL)
412 if (!e->vnode)
413 return false;
414 return e->vnode->finalized;
417 gcc_unreachable ();
420 /* Resolve the symbol with the candidates in the chain *SLOT and store
421 their resolutions. */
423 static void
424 lto_symtab_resolve_symbols (void **slot)
426 lto_symtab_entry_t e;
427 lto_symtab_entry_t prevailing = NULL;
429 /* Always set e->node so that edges are updated to reflect decl merging. */
430 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
432 if (TREE_CODE (e->decl) == FUNCTION_DECL)
433 e->node = cgraph_get_node (e->decl);
434 else if (TREE_CODE (e->decl) == VAR_DECL)
435 e->vnode = varpool_get_node (e->decl);
436 if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
437 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
438 || e->resolution == LDPR_PREVAILING_DEF)
439 prevailing = e;
442 /* If the chain is already resolved there is nothing else to do. */
443 if (prevailing)
444 return;
446 /* Find the single non-replaceable prevailing symbol and
447 diagnose ODR violations. */
448 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
450 if (!lto_symtab_resolve_can_prevail_p (e))
452 e->resolution = LDPR_RESOLVED_IR;
453 e->guessed = true;
454 continue;
457 /* Set a default resolution - the final prevailing one will get
458 adjusted later. */
459 e->resolution = LDPR_PREEMPTED_IR;
460 e->guessed = true;
461 if (!lto_symtab_resolve_replaceable_p (e))
463 if (prevailing)
465 error_at (DECL_SOURCE_LOCATION (e->decl),
466 "%qD has already been defined", e->decl);
467 inform (DECL_SOURCE_LOCATION (prevailing->decl),
468 "previously defined here");
470 prevailing = e;
473 if (prevailing)
474 goto found;
476 /* Do a second round choosing one from the replaceable prevailing decls. */
477 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
479 if (e->resolution != LDPR_PREEMPTED_IR)
480 continue;
482 /* Choose the first function that can prevail as prevailing. */
483 if (TREE_CODE (e->decl) == FUNCTION_DECL)
485 prevailing = e;
486 break;
489 /* From variables that can prevail choose the largest one. */
490 if (!prevailing
491 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
492 DECL_SIZE (e->decl))
493 /* When variables are equivalent try to chose one that has useful
494 DECL_INITIAL. This makes sense for keyed vtables that are
495 DECL_EXTERNAL but initialized. In units that do not need them
496 we replace the initializer by error_mark_node to conserve
497 memory.
499 We know that the vtable is keyed outside the LTO unit - otherwise
500 the keyed instance would prevail. We still can preserve useful
501 info in the initializer. */
502 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
503 && (DECL_INITIAL (e->decl)
504 && DECL_INITIAL (e->decl) != error_mark_node)
505 && (!DECL_INITIAL (prevailing->decl)
506 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
507 prevailing = e;
510 if (!prevailing)
511 return;
513 found:
514 /* If current lto files represent the whole program,
515 it is correct to use LDPR_PREVALING_DEF_IRONLY.
516 If current lto files are part of whole program, internal
517 resolver doesn't know if it is LDPR_PREVAILING_DEF
518 or LDPR_PREVAILING_DEF_IRONLY. Use IRONLY conforms to
519 using -fwhole-program. Otherwise, it doesn't
520 matter using either LDPR_PREVAILING_DEF or
521 LDPR_PREVAILING_DEF_IRONLY
523 FIXME: above workaround due to gold plugin makes some
524 variables IRONLY, which are indeed PREVAILING_DEF in
525 resolution file. These variables still need manual
526 externally_visible attribute. */
527 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
528 prevailing->guessed = true;
531 /* Merge all decls in the symbol table chain to the prevailing decl and
532 issue diagnostics about type mismatches. If DIAGNOSED_P is true
533 do not issue further diagnostics.*/
535 static void
536 lto_symtab_merge_decls_2 (void **slot, bool diagnosed_p)
538 lto_symtab_entry_t prevailing, e;
539 VEC(tree, heap) *mismatches = NULL;
540 unsigned i;
541 tree decl;
543 /* Nothing to do for a single entry. */
544 prevailing = (lto_symtab_entry_t) *slot;
545 if (!prevailing->next)
546 return;
548 /* Try to merge each entry with the prevailing one. */
549 for (e = prevailing->next; e; e = e->next)
551 if (!lto_symtab_merge (prevailing, e)
552 && !diagnosed_p)
553 VEC_safe_push (tree, heap, mismatches, e->decl);
555 if (VEC_empty (tree, mismatches))
556 return;
558 /* Diagnose all mismatched re-declarations. */
559 FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
561 if (!types_compatible_p (TREE_TYPE (prevailing->decl), TREE_TYPE (decl)))
562 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
563 "type of %qD does not match original "
564 "declaration", decl);
566 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
567 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
569 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
570 "alignment of %qD is bigger than "
571 "original declaration", decl);
574 if (diagnosed_p)
575 inform (DECL_SOURCE_LOCATION (prevailing->decl),
576 "previously declared here");
578 VEC_free (tree, heap, mismatches);
581 /* Helper to process the decl chain for the symbol table entry *SLOT. */
583 static int
584 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
586 lto_symtab_entry_t e, prevailing;
587 bool diagnosed_p = false;
589 /* Compute the symbol resolutions. This is a no-op when using the
590 linker plugin. */
591 lto_symtab_resolve_symbols (slot);
593 /* Find the prevailing decl. */
594 for (prevailing = (lto_symtab_entry_t) *slot;
595 prevailing
596 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
597 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP
598 && prevailing->resolution != LDPR_PREVAILING_DEF;
599 prevailing = prevailing->next)
602 /* Assert it's the only one. */
603 if (prevailing)
604 for (e = prevailing->next; e; e = e->next)
606 if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
607 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
608 || e->resolution == LDPR_PREVAILING_DEF)
609 fatal_error ("multiple prevailing defs for %qE",
610 DECL_NAME (prevailing->decl));
613 /* If there's not a prevailing symbol yet it's an external reference.
614 Happens a lot during ltrans. Choose the first symbol with a
615 cgraph or a varpool node. */
616 if (!prevailing)
618 prevailing = (lto_symtab_entry_t) *slot;
619 /* For functions choose one with a cgraph node. */
620 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
621 while (!prevailing->node
622 && prevailing->next)
623 prevailing = prevailing->next;
624 /* For variables chose with a priority variant with vnode
625 attached (i.e. from unit where external declaration of
626 variable is actually used).
627 When there are multiple variants, chose one with size.
628 This is needed for C++ typeinfos, for example in
629 lto/20081204-1 there are typeifos in both units, just
630 one of them do have size. */
631 if (TREE_CODE (prevailing->decl) == VAR_DECL)
633 for (e = prevailing->next; e; e = e->next)
634 if ((!prevailing->vnode && e->vnode)
635 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
636 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
637 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
638 prevailing = e;
642 /* Move it first in the list. */
643 if ((lto_symtab_entry_t) *slot != prevailing)
645 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
647 e->next = prevailing->next;
648 prevailing->next = (lto_symtab_entry_t) *slot;
649 *slot = (void *) prevailing;
652 /* Record the prevailing variable. */
653 if (TREE_CODE (prevailing->decl) == VAR_DECL)
654 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
656 /* Diagnose mismatched objects. */
657 for (e = prevailing->next; e; e = e->next)
659 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
660 continue;
662 switch (TREE_CODE (prevailing->decl))
664 case VAR_DECL:
665 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
666 error_at (DECL_SOURCE_LOCATION (e->decl),
667 "variable %qD redeclared as function", prevailing->decl);
668 break;
670 case FUNCTION_DECL:
671 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
672 error_at (DECL_SOURCE_LOCATION (e->decl),
673 "function %qD redeclared as variable", prevailing->decl);
674 break;
676 default:
677 gcc_unreachable ();
680 diagnosed_p = true;
682 if (diagnosed_p)
683 inform (DECL_SOURCE_LOCATION (prevailing->decl),
684 "previously declared here");
686 /* Merge the chain to the single prevailing decl and diagnose
687 mismatches. */
688 lto_symtab_merge_decls_2 (slot, diagnosed_p);
690 /* Store resolution decision into the callgraph.
691 In LTRANS don't overwrite information we stored into callgraph at
692 WPA stage.
694 Do not bother to store guessed decisions. Generic code knows how
695 to handle UNKNOWN relocation well.
697 The problem with storing guessed decision is whether to use
698 PREVAILING_DEF, PREVAILING_DEF_IRONLY, PREVAILING_DEF_IRONLY_EXP.
699 First one would disable some whole program optimizations, while
700 ther second would imply to many whole program assumptions. */
701 if (prevailing->node && !flag_ltrans && !prevailing->guessed)
702 prevailing->node->symbol.resolution = prevailing->resolution;
703 else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed)
704 prevailing->vnode->symbol.resolution = prevailing->resolution;
705 return 1;
708 /* Resolve and merge all symbol table chains to a prevailing decl. */
710 void
711 lto_symtab_merge_decls (void)
713 lto_symtab_maybe_init_hash_table ();
714 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
717 /* Helper to process the decl chain for the symbol table entry *SLOT. */
719 static int
720 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
722 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
724 if (!prevailing->next)
725 return 1;
727 /* Replace the cgraph node of each entry with the prevailing one. */
728 for (e = prevailing->next; e; e = e->next)
730 if (e->node != NULL)
732 /* In case we prevail funcion by an alias, we can run into case
733 that the alias has no cgraph node attached, since it was
734 previously unused. Create the node. */
735 if (!prevailing->node)
737 prevailing->node = cgraph_create_node (prevailing->decl);
738 prevailing->node->alias = true;
740 lto_cgraph_replace_node (e->node, prevailing->node);
742 if (e->vnode != NULL)
744 if (!prevailing->vnode)
746 prevailing->vnode = varpool_node (prevailing->decl);
747 prevailing->vnode->alias = true;
749 lto_varpool_replace_node (e->vnode, prevailing->vnode);
753 /* Drop all but the prevailing decl from the symtab. */
754 prevailing->next = NULL;
756 return 1;
759 /* Merge cgraph nodes according to the symbol merging done by
760 lto_symtab_merge_decls. */
762 void
763 lto_symtab_merge_cgraph_nodes (void)
765 struct cgraph_node *node;
766 struct varpool_node *vnode;
767 lto_symtab_maybe_init_hash_table ();
768 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
770 FOR_EACH_FUNCTION (node)
771 if ((node->thunk.thunk_p || node->alias)
772 && node->thunk.alias)
773 node->thunk.alias = lto_symtab_prevailing_decl (node->thunk.alias);
774 FOR_EACH_VARIABLE (vnode)
775 if (vnode->alias_of)
776 vnode->alias_of = lto_symtab_prevailing_decl (vnode->alias_of);
779 /* Given the decl DECL, return the prevailing decl with the same name. */
781 tree
782 lto_symtab_prevailing_decl (tree decl)
784 lto_symtab_entry_t ret;
786 /* Builtins and local symbols are their own prevailing decl. */
787 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
788 return decl;
790 /* DECL_ABSTRACTs are their own prevailng decl. */
791 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
792 return decl;
794 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
795 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
797 /* Walk through the list of candidates and return the one we merged to. */
798 ret = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
799 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
800 if (!ret)
801 return NULL_TREE;
803 return ret->decl;
806 #include "gt-lto-symtab.h"