intrinsic.h (gfc_check_selected_real_kind, [...]): Update prototypes.
[gcc.git] / gcc / lto-symtab.c
1 /* LTO symbol table.
2 Copyright 2009 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "toplev.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"
32
33 /* Vector to keep track of external variables we've seen so far. */
34 VEC(tree,gc) *lto_global_var_decls;
35
36 /* Symbol table entry. */
37
38 struct GTY(()) lto_symtab_entry_def
39 {
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 /* Pointer to the next entry with the same key. Before decl merging
54 this links all symbols from the different TUs. After decl merging
55 this links merged but incompatible decls, thus all prevailing ones
56 remaining. */
57 struct lto_symtab_entry_def *next;
58 };
59 typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
60
61 /* A poor man's symbol table. This hashes identifier to prevailing DECL
62 if there is one. */
63
64 static GTY ((if_marked ("lto_symtab_entry_marked_p"),
65 param_is (struct lto_symtab_entry_def)))
66 htab_t lto_symtab_identifiers;
67
68 /* Free symtab hashtable. */
69
70 void
71 lto_symtab_free (void)
72 {
73 htab_delete (lto_symtab_identifiers);
74 lto_symtab_identifiers = NULL;
75 }
76
77 /* Return the hash value of an lto_symtab_entry_t object pointed to by P. */
78
79 static hashval_t
80 lto_symtab_entry_hash (const void *p)
81 {
82 const struct lto_symtab_entry_def *base =
83 (const struct lto_symtab_entry_def *) p;
84 return IDENTIFIER_HASH_VALUE (base->id);
85 }
86
87 /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
88 corresponding to the same symbol. */
89
90 static int
91 lto_symtab_entry_eq (const void *p1, const void *p2)
92 {
93 const struct lto_symtab_entry_def *base1 =
94 (const struct lto_symtab_entry_def *) p1;
95 const struct lto_symtab_entry_def *base2 =
96 (const struct lto_symtab_entry_def *) p2;
97 return (base1->id == base2->id);
98 }
99
100 /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
101 to be marked for GC. */
102
103 static int
104 lto_symtab_entry_marked_p (const void *p)
105 {
106 const struct lto_symtab_entry_def *base =
107 (const struct lto_symtab_entry_def *) p;
108
109 /* Keep this only if the common IDENTIFIER_NODE of the symtab chain
110 is marked which it will be if at least one of the DECLs in the
111 chain is marked. */
112 return ggc_marked_p (base->id);
113 }
114
115 /* Lazily initialize resolution hash tables. */
116
117 static void
118 lto_symtab_maybe_init_hash_table (void)
119 {
120 if (lto_symtab_identifiers)
121 return;
122
123 lto_symtab_identifiers =
124 htab_create_ggc (1021, lto_symtab_entry_hash,
125 lto_symtab_entry_eq, NULL);
126 }
127
128 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
129 and read from FILE_DATA. */
130
131 void
132 lto_symtab_register_decl (tree decl,
133 ld_plugin_symbol_resolution_t resolution,
134 struct lto_file_decl_data *file_data)
135 {
136 lto_symtab_entry_t new_entry;
137 void **slot;
138
139 /* Check that declarations reaching this function do not have
140 properties inconsistent with having external linkage. If any of
141 these asertions fail, then the object file reader has failed to
142 detect these cases and issue appropriate error messages. */
143 gcc_assert (decl
144 && TREE_PUBLIC (decl)
145 && (TREE_CODE (decl) == VAR_DECL
146 || TREE_CODE (decl) == FUNCTION_DECL)
147 && DECL_ASSEMBLER_NAME_SET_P (decl));
148 if (TREE_CODE (decl) == VAR_DECL
149 && DECL_INITIAL (decl))
150 gcc_assert (!DECL_EXTERNAL (decl)
151 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
152 if (TREE_CODE (decl) == FUNCTION_DECL)
153 gcc_assert (!DECL_ABSTRACT (decl));
154
155 new_entry = ggc_alloc_cleared_lto_symtab_entry_def ();
156 new_entry->id = DECL_ASSEMBLER_NAME (decl);
157 new_entry->decl = decl;
158 new_entry->resolution = resolution;
159 new_entry->file_data = file_data;
160
161 lto_symtab_maybe_init_hash_table ();
162 slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
163 new_entry->next = (lto_symtab_entry_t) *slot;
164 *slot = new_entry;
165 }
166
167 /* Get the lto_symtab_entry_def struct associated with ID
168 if there is one. */
169
170 static lto_symtab_entry_t
171 lto_symtab_get (tree id)
172 {
173 struct lto_symtab_entry_def temp;
174 void **slot;
175
176 lto_symtab_maybe_init_hash_table ();
177 temp.id = id;
178 slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
179 return slot ? (lto_symtab_entry_t) *slot : NULL;
180 }
181
182 /* Get the linker resolution for DECL. */
183
184 enum ld_plugin_symbol_resolution
185 lto_symtab_get_resolution (tree decl)
186 {
187 lto_symtab_entry_t e;
188
189 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
190
191 e = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
192 while (e && e->decl != decl)
193 e = e->next;
194 if (!e)
195 return LDPR_UNKNOWN;
196
197 return e->resolution;
198 }
199
200
201 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
202 all edges and removing the old node. */
203
204 static void
205 lto_cgraph_replace_node (struct cgraph_node *node,
206 struct cgraph_node *prevailing_node)
207 {
208 struct cgraph_edge *e, *next;
209
210 /* Merge node flags. */
211 if (node->needed)
212 cgraph_mark_needed_node (prevailing_node);
213 if (node->reachable)
214 cgraph_mark_reachable_node (prevailing_node);
215 if (node->address_taken)
216 {
217 gcc_assert (!prevailing_node->global.inlined_to);
218 cgraph_mark_address_taken_node (prevailing_node);
219 }
220
221 /* Redirect all incoming edges. */
222 for (e = node->callers; e; e = next)
223 {
224 next = e->next_caller;
225 cgraph_redirect_edge_callee (e, prevailing_node);
226 }
227 /* Redirect incomming references. */
228 ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
229
230 if (node->same_body)
231 {
232 struct cgraph_node *alias;
233
234 for (alias = node->same_body; alias; alias = alias->next)
235 if (DECL_ASSEMBLER_NAME_SET_P (alias->decl))
236 {
237 lto_symtab_entry_t se
238 = lto_symtab_get (DECL_ASSEMBLER_NAME (alias->decl));
239
240 for (; se; se = se->next)
241 if (se->node == node)
242 {
243 se->node = NULL;
244 break;
245 }
246 }
247 }
248
249 /* Finally remove the replaced node. */
250 cgraph_remove_node (node);
251 }
252
253 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
254 all edges and removing the old node. */
255
256 static void
257 lto_varpool_replace_node (struct varpool_node *vnode,
258 struct varpool_node *prevailing_node)
259 {
260 /* Merge node flags. */
261 if (vnode->needed)
262 {
263 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
264 varpool_mark_needed_node (prevailing_node);
265 }
266 /* Relink aliases. */
267 if (vnode->extra_name && !vnode->alias)
268 {
269 struct varpool_node *alias, *last;
270 for (alias = vnode->extra_name;
271 alias; alias = alias->next)
272 {
273 last = alias;
274 alias->extra_name = prevailing_node;
275 }
276
277 if (prevailing_node->extra_name)
278 {
279 last->next = prevailing_node->extra_name;
280 prevailing_node->extra_name->prev = last;
281 }
282 prevailing_node->extra_name = vnode->extra_name;
283 vnode->extra_name = NULL;
284 }
285 gcc_assert (!vnode->finalized || prevailing_node->finalized);
286 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
287
288 /* When replacing by an alias, the references goes to the original
289 variable. */
290 if (prevailing_node->alias && prevailing_node->extra_name)
291 prevailing_node = prevailing_node->extra_name;
292 ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
293
294 /* Be sure we can garbage collect the initializer. */
295 if (DECL_INITIAL (vnode->decl))
296 DECL_INITIAL (vnode->decl) = error_mark_node;
297 /* Finally remove the replaced node. */
298 varpool_remove_node (vnode);
299 }
300
301 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
302 Return false if the symbols are not fully compatible and a diagnostic
303 should be emitted. */
304
305 static bool
306 lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
307 {
308 tree prevailing_decl = prevailing->decl;
309 tree decl = entry->decl;
310 tree prevailing_type, type;
311
312 /* Merge decl state in both directions, we may still end up using
313 the new decl. */
314 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
315 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
316
317 /* The linker may ask us to combine two incompatible symbols.
318 Detect this case and notify the caller of required diagnostics. */
319
320 if (TREE_CODE (decl) == FUNCTION_DECL)
321 {
322 if (TREE_TYPE (prevailing_decl) != TREE_TYPE (decl))
323 /* If we don't have a merged type yet...sigh. The linker
324 wouldn't complain if the types were mismatched, so we
325 probably shouldn't either. Just use the type from
326 whichever decl appears to be associated with the
327 definition. If for some odd reason neither decl is, the
328 older one wins. */
329 (void) 0;
330
331 return true;
332 }
333
334 /* Now we exclusively deal with VAR_DECLs. */
335
336 /* Sharing a global symbol is a strong hint that two types are
337 compatible. We could use this information to complete
338 incomplete pointed-to types more aggressively here, ignoring
339 mismatches in both field and tag names. It's difficult though
340 to guarantee that this does not have side-effects on merging
341 more compatible types from other translation units though. */
342
343 /* We can tolerate differences in type qualification, the
344 qualification of the prevailing definition will prevail.
345 ??? In principle we might want to only warn for structurally
346 incompatible types here, but unless we have protective measures
347 for TBAA in place that would hide useful information. */
348 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
349 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
350
351 /* We have to register and fetch canonical types here as the global
352 fixup process didn't yet run. */
353 prevailing_type = gimple_register_type (prevailing_type);
354 type = gimple_register_type (type);
355 if (prevailing_type != type)
356 {
357 if (COMPLETE_TYPE_P (type))
358 return false;
359
360 /* If type is incomplete then avoid warnings in the cases
361 that TBAA handles just fine. */
362
363 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
364 return false;
365
366 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
367 {
368 tree tem1 = TREE_TYPE (prevailing_type);
369 tree tem2 = TREE_TYPE (type);
370 while (TREE_CODE (tem1) == ARRAY_TYPE
371 && TREE_CODE (tem2) == ARRAY_TYPE)
372 {
373 tem1 = TREE_TYPE (tem1);
374 tem2 = TREE_TYPE (tem2);
375 }
376
377 if (TREE_CODE (tem1) != TREE_CODE (tem2))
378 return false;
379
380 if (gimple_register_type (tem1) != gimple_register_type (tem2))
381 return false;
382 }
383
384 /* Fallthru. Compatible enough. */
385 }
386
387 /* ??? We might want to emit a warning here if type qualification
388 differences were spotted. Do not do this unconditionally though. */
389
390 /* There is no point in comparing too many details of the decls here.
391 The type compatibility checks or the completing of types has properly
392 dealt with most issues. */
393
394 /* The following should all not invoke fatal errors as in non-LTO
395 mode the linker wouldn't complain either. Just emit warnings. */
396
397 /* Report a warning if user-specified alignments do not match. */
398 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
399 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
400 return false;
401
402 return true;
403 }
404
405 /* Return true if the symtab entry E can be replaced by another symtab
406 entry. */
407
408 static bool
409 lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
410 {
411 if (DECL_EXTERNAL (e->decl)
412 || DECL_COMDAT (e->decl)
413 || DECL_WEAK (e->decl))
414 return true;
415
416 if (TREE_CODE (e->decl) == VAR_DECL)
417 return (DECL_COMMON (e->decl)
418 || (!flag_no_common && !DECL_INITIAL (e->decl)));
419
420 return false;
421 }
422
423 /* Return true if the symtab entry E can be the prevailing one. */
424
425 static bool
426 lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
427 {
428 /* The C++ frontend ends up neither setting TREE_STATIC nor
429 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
430 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
431 if (DECL_EXTERNAL (e->decl))
432 return false;
433
434 /* For functions we need a non-discarded body. */
435 if (TREE_CODE (e->decl) == FUNCTION_DECL)
436 return (e->node && e->node->analyzed);
437
438 /* A variable should have a size. */
439 else if (TREE_CODE (e->decl) == VAR_DECL)
440 {
441 if (!e->vnode)
442 return false;
443 if (e->vnode->finalized)
444 return true;
445 return e->vnode->alias && e->vnode->extra_name->finalized;
446 }
447
448 gcc_unreachable ();
449 }
450
451 /* Resolve the symbol with the candidates in the chain *SLOT and store
452 their resolutions. */
453
454 static void
455 lto_symtab_resolve_symbols (void **slot)
456 {
457 lto_symtab_entry_t e;
458 lto_symtab_entry_t prevailing = NULL;
459
460 /* Always set e->node so that edges are updated to reflect decl merging. */
461 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
462 {
463 if (TREE_CODE (e->decl) == FUNCTION_DECL)
464 e->node = cgraph_get_node (e->decl);
465 else if (TREE_CODE (e->decl) == VAR_DECL)
466 {
467 e->vnode = varpool_get_node (e->decl);
468 /* The LTO plugin for gold doesn't handle common symbols
469 properly. Let us choose manually. */
470 if (DECL_COMMON (e->decl))
471 e->resolution = LDPR_UNKNOWN;
472 }
473 }
474
475 e = (lto_symtab_entry_t) *slot;
476
477 /* If the chain is already resolved there is nothing else to do. */
478 if (e->resolution != LDPR_UNKNOWN)
479 return;
480
481 /* Find the single non-replaceable prevailing symbol and
482 diagnose ODR violations. */
483 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
484 {
485 if (!lto_symtab_resolve_can_prevail_p (e))
486 {
487 e->resolution = LDPR_RESOLVED_IR;
488 continue;
489 }
490
491 /* Set a default resolution - the final prevailing one will get
492 adjusted later. */
493 e->resolution = LDPR_PREEMPTED_IR;
494 if (!lto_symtab_resolve_replaceable_p (e))
495 {
496 if (prevailing)
497 {
498 error_at (DECL_SOURCE_LOCATION (e->decl),
499 "%qD has already been defined", e->decl);
500 inform (DECL_SOURCE_LOCATION (prevailing->decl),
501 "previously defined here");
502 }
503 prevailing = e;
504 }
505 }
506 if (prevailing)
507 goto found;
508
509 /* Do a second round choosing one from the replaceable prevailing decls. */
510 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
511 {
512 if (e->resolution != LDPR_PREEMPTED_IR)
513 continue;
514
515 /* Choose the first function that can prevail as prevailing. */
516 if (TREE_CODE (e->decl) == FUNCTION_DECL)
517 {
518 prevailing = e;
519 break;
520 }
521
522 /* From variables that can prevail choose the largest one. */
523 if (!prevailing
524 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
525 DECL_SIZE (e->decl)))
526 prevailing = e;
527 }
528
529 if (!prevailing)
530 return;
531
532 found:
533 if (TREE_CODE (prevailing->decl) == VAR_DECL
534 && TREE_READONLY (prevailing->decl))
535 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
536 else
537 prevailing->resolution = LDPR_PREVAILING_DEF;
538 }
539
540 /* Merge all decls in the symbol table chain to the prevailing decl and
541 issue diagnostics about type mismatches. */
542
543 static void
544 lto_symtab_merge_decls_2 (void **slot)
545 {
546 lto_symtab_entry_t prevailing, e;
547 VEC(tree, heap) *mismatches = NULL;
548 unsigned i;
549 tree decl;
550 bool diagnosed_p = false;
551
552 /* Nothing to do for a single entry. */
553 prevailing = (lto_symtab_entry_t) *slot;
554 if (!prevailing->next)
555 return;
556
557 /* Try to merge each entry with the prevailing one. */
558 for (e = prevailing->next; e; e = e->next)
559 {
560 if (!lto_symtab_merge (prevailing, e))
561 VEC_safe_push (tree, heap, mismatches, e->decl);
562 }
563 if (VEC_empty (tree, mismatches))
564 return;
565
566 /* Diagnose all mismatched re-declarations. */
567 for (i = 0; VEC_iterate (tree, mismatches, i, decl); ++i)
568 {
569 if (TREE_TYPE (prevailing->decl) != TREE_TYPE (decl))
570 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
571 "type of %qD does not match original "
572 "declaration", decl);
573
574 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
575 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
576 {
577 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
578 "alignment of %qD is bigger than "
579 "original declaration", decl);
580 }
581 }
582 if (diagnosed_p)
583 inform (DECL_SOURCE_LOCATION (prevailing->decl),
584 "previously declared here");
585
586 VEC_free (tree, heap, mismatches);
587 }
588
589 /* Helper to process the decl chain for the symbol table entry *SLOT. */
590
591 static int
592 lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
593 {
594 lto_symtab_entry_t e, prevailing;
595 bool diagnosed_p = false;
596
597 /* Compute the symbol resolutions. This is a no-op when using the
598 linker plugin. */
599 lto_symtab_resolve_symbols (slot);
600
601 /* Find the prevailing decl. */
602 for (prevailing = (lto_symtab_entry_t) *slot;
603 prevailing
604 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
605 && prevailing->resolution != LDPR_PREVAILING_DEF;
606 prevailing = prevailing->next)
607 ;
608
609 /* Assert it's the only one. */
610 if (prevailing)
611 for (e = prevailing->next; e; e = e->next)
612 gcc_assert (e->resolution != LDPR_PREVAILING_DEF_IRONLY
613 && e->resolution != LDPR_PREVAILING_DEF);
614
615 /* If there's not a prevailing symbol yet it's an external reference.
616 Happens a lot during ltrans. Choose the first symbol with a
617 cgraph or a varpool node. */
618 if (!prevailing)
619 {
620 prevailing = (lto_symtab_entry_t) *slot;
621 /* For functions choose one with a cgraph node. */
622 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
623 while (!prevailing->node
624 && prevailing->next)
625 prevailing = prevailing->next;
626 /* For variables chose with a priority variant with vnode
627 attached (i.e. from unit where external declaration of
628 variable is actually used).
629 When there are multiple variants, chose one with size.
630 This is needed for C++ typeinfos, for example in
631 lto/20081204-1 there are typeifos in both units, just
632 one of them do have size. */
633 if (TREE_CODE (prevailing->decl) == VAR_DECL)
634 {
635 for (e = prevailing->next; e; e = e->next)
636 if ((!prevailing->vnode && e->vnode)
637 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
638 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
639 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
640 prevailing = e;
641 }
642 }
643
644 /* Move it first in the list. */
645 if ((lto_symtab_entry_t) *slot != prevailing)
646 {
647 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
648 ;
649 e->next = prevailing->next;
650 prevailing->next = (lto_symtab_entry_t) *slot;
651 *slot = (void *) prevailing;
652 }
653
654 /* Record the prevailing variable. */
655 if (TREE_CODE (prevailing->decl) == VAR_DECL)
656 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
657
658 /* Diagnose mismatched objects. */
659 for (e = prevailing->next; e; e = e->next)
660 {
661 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
662 continue;
663
664 switch (TREE_CODE (prevailing->decl))
665 {
666 case VAR_DECL:
667 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
668 error_at (DECL_SOURCE_LOCATION (e->decl),
669 "variable %qD redeclared as function", prevailing->decl);
670 break;
671
672 case FUNCTION_DECL:
673 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
674 error_at (DECL_SOURCE_LOCATION (e->decl),
675 "function %qD redeclared as variable", prevailing->decl);
676 break;
677
678 default:
679 gcc_unreachable ();
680 }
681
682 diagnosed_p = true;
683 }
684 if (diagnosed_p)
685 inform (DECL_SOURCE_LOCATION (prevailing->decl),
686 "previously declared here");
687
688 /* Register and adjust types of the entries. */
689 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
690 TREE_TYPE (e->decl) = gimple_register_type (TREE_TYPE (e->decl));
691
692 /* Merge the chain to the single prevailing decl and diagnose
693 mismatches. */
694 lto_symtab_merge_decls_2 (slot);
695
696 /* Drop all but the prevailing decl from the symtab. */
697 if (TREE_CODE (prevailing->decl) != FUNCTION_DECL
698 && TREE_CODE (prevailing->decl) != VAR_DECL)
699 prevailing->next = NULL;
700
701 return 1;
702 }
703
704 /* Resolve and merge all symbol table chains to a prevailing decl. */
705
706 void
707 lto_symtab_merge_decls (void)
708 {
709 lto_symtab_maybe_init_hash_table ();
710 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
711 }
712
713 /* Helper to process the decl chain for the symbol table entry *SLOT. */
714
715 static int
716 lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
717 {
718 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
719
720 if (!prevailing->next)
721 return 1;
722
723 /* Replace the cgraph node of each entry with the prevailing one. */
724 for (e = prevailing->next; e; e = e->next)
725 {
726 if (e->node != NULL)
727 {
728 if (e->node->decl != e->decl && e->node->same_body)
729 {
730 struct cgraph_node *alias;
731
732 for (alias = e->node->same_body; alias; alias = alias->next)
733 if (alias->decl == e->decl)
734 break;
735 if (alias)
736 {
737 cgraph_remove_same_body_alias (alias);
738 continue;
739 }
740 }
741 lto_cgraph_replace_node (e->node, prevailing->node);
742 }
743 if (e->vnode != NULL)
744 lto_varpool_replace_node (e->vnode, prevailing->vnode);
745 }
746
747 /* Drop all but the prevailing decl from the symtab. */
748 prevailing->next = NULL;
749
750 return 1;
751 }
752
753 /* Merge cgraph nodes according to the symbol merging done by
754 lto_symtab_merge_decls. */
755
756 void
757 lto_symtab_merge_cgraph_nodes (void)
758 {
759 lto_symtab_maybe_init_hash_table ();
760 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
761 }
762
763 /* Given the decl DECL, return the prevailing decl with the same name. */
764
765 tree
766 lto_symtab_prevailing_decl (tree decl)
767 {
768 lto_symtab_entry_t ret;
769
770 /* Builtins and local symbols are their own prevailing decl. */
771 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
772 return decl;
773
774 /* DECL_ABSTRACTs are their own prevailng decl. */
775 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
776 return decl;
777
778 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
779 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
780
781 /* Walk through the list of candidates and return the one we merged to. */
782 ret = lto_symtab_get (DECL_ASSEMBLER_NAME (decl));
783 if (!ret)
784 return NULL_TREE;
785
786 return ret->decl;
787 }
788
789 #include "gt-lto-symtab.h"