Remove the lambda framework and make -ftree-loop-linear an alias of -floop-interchange.
[official-gcc/graphite-test-results.git] / gcc / lto-symtab.c
blob757327686534adc76e4d499a3f9831a31bda9a77
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 no_aliases_please = false;
213 bool compatible_p;
215 if (cgraph_dump_file)
217 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
218 " for symbol %s\n",
219 cgraph_node_name (node), node->uid,
220 cgraph_node_name (prevailing_node),
221 prevailing_node->uid,
222 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
223 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
226 if (prevailing_node->same_body_alias)
228 if (prevailing_node->thunk.thunk_p)
229 no_aliases_please = true;
230 prevailing_node = prevailing_node->same_body;
233 /* Merge node flags. */
234 if (node->needed)
235 cgraph_mark_needed_node (prevailing_node);
236 if (node->reachable)
237 cgraph_mark_reachable_node (prevailing_node);
238 if (node->address_taken)
240 gcc_assert (!prevailing_node->global.inlined_to);
241 cgraph_mark_address_taken_node (prevailing_node);
244 /* Redirect all incoming edges. */
245 compatible_p
246 = gimple_types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
247 TREE_TYPE (TREE_TYPE (node->decl)), GTC_DIAG);
248 for (e = node->callers; e; e = next)
250 next = e->next_caller;
251 cgraph_redirect_edge_callee (e, prevailing_node);
252 /* If there is a mismatch between the supposed callee return type and
253 the real one do not attempt to inline this function.
254 ??? We really need a way to match function signatures for ABI
255 compatibility and perform related promotions at inlining time. */
256 if (!compatible_p)
257 e->call_stmt_cannot_inline_p = 1;
259 /* Redirect incomming references. */
260 ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
262 /* If we have aliases, redirect them to the prevailing node. */
263 if (!node->same_body_alias && node->same_body)
265 struct cgraph_node *alias, *last;
266 /* We prevail aliases/tunks by a thunk. This is doable but
267 would need thunk combination. Hopefully no ABI changes will
268 every be crazy enough. */
269 gcc_assert (!no_aliases_please);
271 for (alias = node->same_body; alias; alias = alias->next)
273 last = alias;
274 gcc_assert (alias->same_body_alias);
275 alias->same_body = prevailing_node;
276 alias->thunk.alias = prevailing_node->decl;
278 last->next = prevailing_node->same_body;
279 /* Node with aliases is prevailed by alias.
280 We could handle this, but combining thunks together will be tricky.
281 Hopefully this does not happen. */
282 if (prevailing_node->same_body)
283 prevailing_node->same_body->previous = last;
284 prevailing_node->same_body = node->same_body;
285 node->same_body = NULL;
288 /* Finally remove the replaced node. */
289 if (node->same_body_alias)
290 cgraph_remove_same_body_alias (node);
291 else
292 cgraph_remove_node (node);
295 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
296 all edges and removing the old node. */
298 static void
299 lto_varpool_replace_node (struct varpool_node *vnode,
300 struct varpool_node *prevailing_node)
302 /* Merge node flags. */
303 if (vnode->needed)
305 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
306 varpool_mark_needed_node (prevailing_node);
308 /* Relink aliases. */
309 if (vnode->extra_name && !vnode->alias)
311 struct varpool_node *alias, *last;
312 for (alias = vnode->extra_name;
313 alias; alias = alias->next)
315 last = alias;
316 alias->extra_name = prevailing_node;
319 if (prevailing_node->extra_name)
321 last->next = prevailing_node->extra_name;
322 prevailing_node->extra_name->prev = last;
324 prevailing_node->extra_name = vnode->extra_name;
325 vnode->extra_name = NULL;
327 gcc_assert (!vnode->finalized || prevailing_node->finalized);
328 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
330 /* When replacing by an alias, the references goes to the original
331 variable. */
332 if (prevailing_node->alias && prevailing_node->extra_name)
333 prevailing_node = prevailing_node->extra_name;
334 ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
336 /* Be sure we can garbage collect the initializer. */
337 if (DECL_INITIAL (vnode->decl))
338 DECL_INITIAL (vnode->decl) = error_mark_node;
339 /* Finally remove the replaced node. */
340 varpool_remove_node (vnode);
343 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
344 Return false if the symbols are not fully compatible and a diagnostic
345 should be emitted. */
347 static bool
348 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
350 tree prevailing_decl = prevailing->decl;
351 tree decl = entry->decl;
352 tree prevailing_type, type;
354 /* Merge decl state in both directions, we may still end up using
355 the new decl. */
356 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
357 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
359 /* The linker may ask us to combine two incompatible symbols.
360 Detect this case and notify the caller of required diagnostics. */
362 if (TREE_CODE (decl) == FUNCTION_DECL)
364 if (!gimple_types_compatible_p (TREE_TYPE (prevailing_decl),
365 TREE_TYPE (decl), GTC_DIAG))
366 /* If we don't have a merged type yet...sigh. The linker
367 wouldn't complain if the types were mismatched, so we
368 probably shouldn't either. Just use the type from
369 whichever decl appears to be associated with the
370 definition. If for some odd reason neither decl is, the
371 older one wins. */
372 (void) 0;
374 return true;
377 /* Now we exclusively deal with VAR_DECLs. */
379 /* Sharing a global symbol is a strong hint that two types are
380 compatible. We could use this information to complete
381 incomplete pointed-to types more aggressively here, ignoring
382 mismatches in both field and tag names. It's difficult though
383 to guarantee that this does not have side-effects on merging
384 more compatible types from other translation units though. */
386 /* We can tolerate differences in type qualification, the
387 qualification of the prevailing definition will prevail.
388 ??? In principle we might want to only warn for structurally
389 incompatible types here, but unless we have protective measures
390 for TBAA in place that would hide useful information. */
391 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
392 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
394 /* We have to register and fetch canonical types here as the global
395 fixup process didn't yet run. */
396 prevailing_type = gimple_register_type (prevailing_type);
397 type = gimple_register_type (type);
398 if (!gimple_types_compatible_p (prevailing_type, type, GTC_DIAG))
400 if (COMPLETE_TYPE_P (type))
401 return false;
403 /* If type is incomplete then avoid warnings in the cases
404 that TBAA handles just fine. */
406 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
407 return false;
409 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
411 tree tem1 = TREE_TYPE (prevailing_type);
412 tree tem2 = TREE_TYPE (type);
413 while (TREE_CODE (tem1) == ARRAY_TYPE
414 && TREE_CODE (tem2) == ARRAY_TYPE)
416 tem1 = TREE_TYPE (tem1);
417 tem2 = TREE_TYPE (tem2);
420 if (TREE_CODE (tem1) != TREE_CODE (tem2))
421 return false;
423 if (!gimple_types_compatible_p (gimple_register_type (tem1),
424 gimple_register_type (tem2),
425 GTC_DIAG))
426 return false;
429 /* Fallthru. Compatible enough. */
432 /* ??? We might want to emit a warning here if type qualification
433 differences were spotted. Do not do this unconditionally though. */
435 /* There is no point in comparing too many details of the decls here.
436 The type compatibility checks or the completing of types has properly
437 dealt with most issues. */
439 /* The following should all not invoke fatal errors as in non-LTO
440 mode the linker wouldn't complain either. Just emit warnings. */
442 /* Report a warning if user-specified alignments do not match. */
443 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
444 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
445 return false;
447 return true;
450 /* Return true if the symtab entry E can be replaced by another symtab
451 entry. */
453 static bool
454 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
456 if (DECL_EXTERNAL (e->decl)
457 || DECL_COMDAT (e->decl)
458 || DECL_ONE_ONLY (e->decl)
459 || DECL_WEAK (e->decl))
460 return true;
462 if (TREE_CODE (e->decl) == VAR_DECL)
463 return (DECL_COMMON (e->decl)
464 || (!flag_no_common && !DECL_INITIAL (e->decl)));
466 return false;
469 /* Return true if the symtab entry E can be the prevailing one. */
471 static bool
472 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
474 /* The C++ frontend ends up neither setting TREE_STATIC nor
475 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
476 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
477 if (DECL_EXTERNAL (e->decl))
478 return false;
480 /* For functions we need a non-discarded body. */
481 if (TREE_CODE (e->decl) == FUNCTION_DECL)
482 return (e->node
483 && (e->node->analyzed
484 || (e->node->same_body_alias && e->node->same_body->analyzed)));
486 /* A variable should have a size. */
487 else if (TREE_CODE (e->decl) == VAR_DECL)
489 if (!e->vnode)
490 return false;
491 if (e->vnode->finalized)
492 return true;
493 return e->vnode->alias && e->vnode->extra_name->finalized;
496 gcc_unreachable ();
499 /* Resolve the symbol with the candidates in the chain *SLOT and store
500 their resolutions. */
502 static void
503 lto_symtab_resolve_symbols (void **slot)
505 lto_symtab_entry_t e;
506 lto_symtab_entry_t prevailing = NULL;
508 /* Always set e->node so that edges are updated to reflect decl merging. */
509 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
511 if (TREE_CODE (e->decl) == FUNCTION_DECL)
512 e->node = cgraph_get_node_or_alias (e->decl);
513 else if (TREE_CODE (e->decl) == VAR_DECL)
514 e->vnode = varpool_get_node (e->decl);
517 e = (lto_symtab_entry_t) *slot;
519 /* If the chain is already resolved there is nothing else to do. */
520 if (e->resolution != LDPR_UNKNOWN)
521 return;
523 /* Find the single non-replaceable prevailing symbol and
524 diagnose ODR violations. */
525 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
527 if (!lto_symtab_resolve_can_prevail_p (e))
529 e->resolution = LDPR_RESOLVED_IR;
530 e->guessed = true;
531 continue;
534 /* Set a default resolution - the final prevailing one will get
535 adjusted later. */
536 e->resolution = LDPR_PREEMPTED_IR;
537 e->guessed = true;
538 if (!lto_symtab_resolve_replaceable_p (e))
540 if (prevailing)
542 error_at (DECL_SOURCE_LOCATION (e->decl),
543 "%qD has already been defined", e->decl);
544 inform (DECL_SOURCE_LOCATION (prevailing->decl),
545 "previously defined here");
547 prevailing = e;
550 if (prevailing)
551 goto found;
553 /* Do a second round choosing one from the replaceable prevailing decls. */
554 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
556 if (e->resolution != LDPR_PREEMPTED_IR)
557 continue;
559 /* Choose the first function that can prevail as prevailing. */
560 if (TREE_CODE (e->decl) == FUNCTION_DECL)
562 prevailing = e;
563 break;
566 /* From variables that can prevail choose the largest one. */
567 if (!prevailing
568 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
569 DECL_SIZE (e->decl)))
570 prevailing = e;
573 if (!prevailing)
574 return;
576 found:
577 /* If current lto files represent the whole program,
578 it is correct to use LDPR_PREVALING_DEF_IRONLY.
579 If current lto files are part of whole program, internal
580 resolver doesn't know if it is LDPR_PREVAILING_DEF
581 or LDPR_PREVAILING_DEF_IRONLY. Use IRONLY conforms to
582 using -fwhole-program. Otherwise, it doesn't
583 matter using either LDPR_PREVAILING_DEF or
584 LDPR_PREVAILING_DEF_IRONLY
586 FIXME: above workaround due to gold plugin makes some
587 variables IRONLY, which are indeed PREVAILING_DEF in
588 resolution file. These variables still need manual
589 externally_visible attribute. */
590 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
591 prevailing->guessed = true;
594 /* Merge all decls in the symbol table chain to the prevailing decl and
595 issue diagnostics about type mismatches. If DIAGNOSED_P is true
596 do not issue further diagnostics.*/
598 static void
599 lto_symtab_merge_decls_2 (void **slot, bool diagnosed_p)
601 lto_symtab_entry_t prevailing, e;
602 VEC(tree, heap) *mismatches = NULL;
603 unsigned i;
604 tree decl;
606 /* Nothing to do for a single entry. */
607 prevailing = (lto_symtab_entry_t) *slot;
608 if (!prevailing->next)
609 return;
611 /* Try to merge each entry with the prevailing one. */
612 for (e = prevailing->next; e; e = e->next)
614 if (!lto_symtab_merge (prevailing, e)
615 && !diagnosed_p)
616 VEC_safe_push (tree, heap, mismatches, e->decl);
618 if (VEC_empty (tree, mismatches))
619 return;
621 /* Diagnose all mismatched re-declarations. */
622 FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
624 if (!gimple_types_compatible_p (TREE_TYPE (prevailing->decl),
625 TREE_TYPE (decl), GTC_DIAG))
626 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
627 "type of %qD does not match original "
628 "declaration", decl);
630 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
631 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
633 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
634 "alignment of %qD is bigger than "
635 "original declaration", decl);
638 if (diagnosed_p)
639 inform (DECL_SOURCE_LOCATION (prevailing->decl),
640 "previously declared here");
642 VEC_free (tree, heap, mismatches);
645 /* Helper to process the decl chain for the symbol table entry *SLOT. */
647 static int
648 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
650 lto_symtab_entry_t e, prevailing;
651 bool diagnosed_p = false;
653 /* Compute the symbol resolutions. This is a no-op when using the
654 linker plugin. */
655 lto_symtab_resolve_symbols (slot);
657 /* Find the prevailing decl. */
658 for (prevailing = (lto_symtab_entry_t) *slot;
659 prevailing
660 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
661 && prevailing->resolution != LDPR_PREVAILING_DEF;
662 prevailing = prevailing->next)
665 /* Assert it's the only one. */
666 if (prevailing)
667 for (e = prevailing->next; e; e = e->next)
669 if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
670 || e->resolution == LDPR_PREVAILING_DEF)
671 fatal_error ("multiple prevailing defs for %qE",
672 DECL_NAME (prevailing->decl));
675 /* If there's not a prevailing symbol yet it's an external reference.
676 Happens a lot during ltrans. Choose the first symbol with a
677 cgraph or a varpool node. */
678 if (!prevailing)
680 prevailing = (lto_symtab_entry_t) *slot;
681 /* For functions choose one with a cgraph node. */
682 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
683 while (!prevailing->node
684 && prevailing->next)
685 prevailing = prevailing->next;
686 /* For variables chose with a priority variant with vnode
687 attached (i.e. from unit where external declaration of
688 variable is actually used).
689 When there are multiple variants, chose one with size.
690 This is needed for C++ typeinfos, for example in
691 lto/20081204-1 there are typeifos in both units, just
692 one of them do have size. */
693 if (TREE_CODE (prevailing->decl) == VAR_DECL)
695 for (e = prevailing->next; e; e = e->next)
696 if ((!prevailing->vnode && e->vnode)
697 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
698 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
699 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
700 prevailing = e;
704 /* Move it first in the list. */
705 if ((lto_symtab_entry_t) *slot != prevailing)
707 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
709 e->next = prevailing->next;
710 prevailing->next = (lto_symtab_entry_t) *slot;
711 *slot = (void *) prevailing;
714 /* Record the prevailing variable. */
715 if (TREE_CODE (prevailing->decl) == VAR_DECL)
716 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
718 /* Diagnose mismatched objects. */
719 for (e = prevailing->next; e; e = e->next)
721 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
722 continue;
724 switch (TREE_CODE (prevailing->decl))
726 case VAR_DECL:
727 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
728 error_at (DECL_SOURCE_LOCATION (e->decl),
729 "variable %qD redeclared as function", prevailing->decl);
730 break;
732 case FUNCTION_DECL:
733 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
734 error_at (DECL_SOURCE_LOCATION (e->decl),
735 "function %qD redeclared as variable", prevailing->decl);
736 break;
738 default:
739 gcc_unreachable ();
742 diagnosed_p = true;
744 if (diagnosed_p)
745 inform (DECL_SOURCE_LOCATION (prevailing->decl),
746 "previously declared here");
748 /* Register and adjust types of the entries. */
749 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
750 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
752 /* Merge the chain to the single prevailing decl and diagnose
753 mismatches. */
754 lto_symtab_merge_decls_2 (slot, diagnosed_p);
756 /* Store resolution decision into the callgraph.
757 In LTRANS don't overwrite information we stored into callgraph at
758 WPA stage.
760 Do not bother to store guessed decisions. Generic code knows how
761 to handle UNKNOWN relocation well.
763 The problem with storing guessed decision is whether to use
764 PREVAILING_DEF or PREVAILING_DEF_IRONLY. First one would disable
765 some whole program optimizations, while ther second would imply
766 to many whole program assumptions. */
767 if (prevailing->node && !flag_ltrans && !prevailing->guessed)
768 prevailing->node->resolution = prevailing->resolution;
769 else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed)
770 prevailing->vnode->resolution = prevailing->resolution;
771 return 1;
774 /* Resolve and merge all symbol table chains to a prevailing decl. */
776 void
777 lto_symtab_merge_decls (void)
779 lto_symtab_maybe_init_hash_table ();
780 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
783 /* Helper to process the decl chain for the symbol table entry *SLOT. */
785 static int
786 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
788 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
790 if (!prevailing->next)
791 return 1;
793 /* Replace the cgraph node of each entry with the prevailing one. */
794 for (e = prevailing->next; e; e = e->next)
796 if (e->node != NULL)
798 /* In case we prevail funcion by an alias, we can run into case
799 that the alias has no cgraph node attached, since it was
800 previously unused. Create the node. */
801 if (!prevailing->node)
803 prevailing->node = cgraph_node (prevailing->decl);
804 prevailing->node->alias = true;
806 lto_cgraph_replace_node (e->node, prevailing->node);
808 if (e->vnode != NULL)
810 if (!prevailing->vnode)
812 prevailing->vnode = varpool_node (prevailing->decl);
813 prevailing->vnode->alias = true;
815 lto_varpool_replace_node (e->vnode, prevailing->vnode);
819 /* Drop all but the prevailing decl from the symtab. */
820 prevailing->next = NULL;
822 return 1;
825 /* Merge cgraph nodes according to the symbol merging done by
826 lto_symtab_merge_decls. */
828 void
829 lto_symtab_merge_cgraph_nodes (void)
831 lto_symtab_maybe_init_hash_table ();
832 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
835 /* Given the decl DECL, return the prevailing decl with the same name. */
837 tree
838 lto_symtab_prevailing_decl (tree decl)
840 lto_symtab_entry_t ret;
842 /* Builtins and local symbols are their own prevailing decl. */
843 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
844 return decl;
846 /* DECL_ABSTRACTs are their own prevailng decl. */
847 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
848 return decl;
850 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
851 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
853 /* Walk through the list of candidates and return the one we merged to. */
854 ret = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
855 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
856 if (!ret)
857 return NULL_TREE;
859 return ret->decl;
862 #include "gt-lto-symtab.h"