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