]> gcc.gnu.org Git - gcc.git/blame - gcc/lto-symtab.c
lto-symtab.c (lto_cgraph_replace_node): Do not call mark_reahcable_node.
[gcc.git] / gcc / lto-symtab.c
CommitLineData
d7f09764 1/* LTO symbol table.
7c475d11 2 Copyright 2009, 2010 Free Software Foundation, Inc.
d7f09764
DN
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 "tree.h"
26#include "gimple.h"
a9429e29 27#include "ggc.h"
d7f09764
DN
28#include "hashtab.h"
29#include "plugin-api.h"
30#include "lto-streamer.h"
31
32/* Vector to keep track of external variables we've seen so far. */
33VEC(tree,gc) *lto_global_var_decls;
34
1a735925 35/* Symbol table entry. */
d7f09764 36
1a735925 37struct GTY(()) lto_symtab_entry_def
d7f09764 38{
1a735925
RG
39 /* The symbol table entry key, an IDENTIFIER. */
40 tree id;
41 /* The symbol table entry, a DECL. */
d7f09764 42 tree decl;
2c928155
RG
43 /* The cgraph node if decl is a function decl. Filled in during the
44 merging process. */
45 struct cgraph_node *node;
2942c502
JH
46 /* The varpool node if decl is a variable decl. Filled in during the
47 merging process. */
48 struct varpool_node *vnode;
1a735925 49 /* LTO file-data and symbol resolution for this decl. */
d7f09764 50 struct lto_file_decl_data * GTY((skip (""))) file_data;
1a735925 51 enum ld_plugin_symbol_resolution resolution;
051f8cc6
JH
52 /* True when resolution was guessed and not read from the file. */
53 bool guessed;
1a735925
RG
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 ();
77754180
DK
157 new_entry->id = (*targetm.asm_out.mangle_assembler_name)
158 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
200c8750
RG
159 new_entry->decl = decl;
160 new_entry->resolution = resolution;
161 new_entry->file_data = file_data;
162
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;
167}
168
169/* Get the lto_symtab_entry_def struct associated with ID
170 if there is one. */
171
172static lto_symtab_entry_t
173lto_symtab_get (tree id)
174{
175 struct lto_symtab_entry_def temp;
176 void **slot;
177
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;
182}
183
184/* Get the linker resolution for DECL. */
185
186enum ld_plugin_symbol_resolution
187lto_symtab_get_resolution (tree decl)
188{
189 lto_symtab_entry_t e;
190
191 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
192
77754180
DK
193 e = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
194 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
200c8750
RG
195 while (e && e->decl != decl)
196 e = e->next;
197 if (!e)
198 return LDPR_UNKNOWN;
199
200 return e->resolution;
201}
202
203
200c8750
RG
204/* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
205 all edges and removing the old node. */
d7f09764 206
200c8750
RG
207static void
208lto_cgraph_replace_node (struct cgraph_node *node,
209 struct cgraph_node *prevailing_node)
d7f09764 210{
200c8750 211 struct cgraph_edge *e, *next;
ecf9b8ae 212 bool compatible_p;
e10aaec0
JH
213
214 if (cgraph_dump_file)
215 {
216 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
217 " for symbol %s\n",
218 cgraph_node_name (node), node->uid,
219 cgraph_node_name (prevailing_node),
220 prevailing_node->uid,
77754180 221 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
960bfb69 222 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl)))));
e10aaec0
JH
223 }
224
200c8750 225 /* Merge node flags. */
ead84f73
JH
226 if (node->symbol.force_output)
227 cgraph_mark_force_output_node (prevailing_node);
960bfb69 228 if (node->symbol.address_taken)
d7f09764 229 {
200c8750
RG
230 gcc_assert (!prevailing_node->global.inlined_to);
231 cgraph_mark_address_taken_node (prevailing_node);
232 }
d7f09764 233
200c8750 234 /* Redirect all incoming edges. */
ecf9b8ae 235 compatible_p
960bfb69
JH
236 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->symbol.decl)),
237 TREE_TYPE (TREE_TYPE (node->symbol.decl)));
200c8750
RG
238 for (e = node->callers; e; e = next)
239 {
240 next = e->next_caller;
241 cgraph_redirect_edge_callee (e, prevailing_node);
ecf9b8ae
RG
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;
200c8750 248 }
369451ec 249 /* Redirect incomming references. */
5932a4d4 250 ipa_clone_referring ((symtab_node)prevailing_node, &node->symbol.ref_list);
d7f09764 251
200c8750 252 /* Finally remove the replaced node. */
39e2db00 253 cgraph_remove_node (node);
200c8750
RG
254}
255
2942c502
JH
256/* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
257 all edges and removing the old node. */
258
259static void
260lto_varpool_replace_node (struct varpool_node *vnode,
261 struct varpool_node *prevailing_node)
262{
2942c502
JH
263 gcc_assert (!vnode->finalized || prevailing_node->finalized);
264 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
265
5932a4d4 266 ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list);
369451ec 267
b34fd25c 268 /* Be sure we can garbage collect the initializer. */
960bfb69
JH
269 if (DECL_INITIAL (vnode->symbol.decl))
270 DECL_INITIAL (vnode->symbol.decl) = error_mark_node;
2942c502
JH
271 /* Finally remove the replaced node. */
272 varpool_remove_node (vnode);
273}
274
200c8750
RG
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. */
278
279static bool
280lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
281{
282 tree prevailing_decl = prevailing->decl;
283 tree decl = entry->decl;
284 tree prevailing_type, type;
200c8750
RG
285
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);
290
200c8750
RG
291 /* The linker may ask us to combine two incompatible symbols.
292 Detect this case and notify the caller of required diagnostics. */
293
294 if (TREE_CODE (decl) == FUNCTION_DECL)
fd7588bc 295 {
0ca8de87
RG
296 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
297 TREE_TYPE (decl)))
fd7588bc
RG
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;
305
306 return true;
307 }
308
309 /* Now we exclusively deal with VAR_DECLs. */
310
e575382e
RG
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. */
d7f09764 317
fd7588bc 318 /* We can tolerate differences in type qualification, the
e575382e
RG
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. */
200c8750
RG
323 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
324 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
e575382e 325
0ca8de87 326 if (!types_compatible_p (prevailing_type, type))
e575382e
RG
327 {
328 if (COMPLETE_TYPE_P (type))
329 return false;
330
331 /* If type is incomplete then avoid warnings in the cases
332 that TBAA handles just fine. */
333
334 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
335 return false;
336
337 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
338 {
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)
343 {
344 tem1 = TREE_TYPE (tem1);
345 tem2 = TREE_TYPE (tem2);
346 }
347
348 if (TREE_CODE (tem1) != TREE_CODE (tem2))
349 return false;
350
0ca8de87 351 if (!types_compatible_p (tem1, tem2))
e575382e
RG
352 return false;
353 }
354
355 /* Fallthru. Compatible enough. */
356 }
d7f09764 357
fd7588bc
RG
358 /* ??? We might want to emit a warning here if type qualification
359 differences were spotted. Do not do this unconditionally though. */
d7f09764 360
fd7588bc
RG
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. */
d7f09764 364
fd7588bc
RG
365 /* The following should all not invoke fatal errors as in non-LTO
366 mode the linker wouldn't complain either. Just emit warnings. */
d7f09764 367
fd7588bc 368 /* Report a warning if user-specified alignments do not match. */
200c8750
RG
369 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
370 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
371 return false;
d7f09764 372
d7f09764
DN
373 return true;
374}
375
200c8750
RG
376/* Return true if the symtab entry E can be replaced by another symtab
377 entry. */
d7f09764 378
200c8750
RG
379static bool
380lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
d7f09764 381{
200c8750
RG
382 if (DECL_EXTERNAL (e->decl)
383 || DECL_COMDAT (e->decl)
8bbaa770 384 || DECL_ONE_ONLY (e->decl)
200c8750
RG
385 || DECL_WEAK (e->decl))
386 return true;
d7f09764 387
200c8750
RG
388 if (TREE_CODE (e->decl) == VAR_DECL)
389 return (DECL_COMMON (e->decl)
390 || (!flag_no_common && !DECL_INITIAL (e->decl)));
d7f09764 391
200c8750 392 return false;
1a735925 393}
d7f09764 394
200c8750 395/* Return true if the symtab entry E can be the prevailing one. */
d7f09764 396
200c8750
RG
397static bool
398lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
1a735925 399{
5d96330a
RG
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))
200c8750 404 return false;
d7f09764 405
200c8750
RG
406 /* For functions we need a non-discarded body. */
407 if (TREE_CODE (e->decl) == FUNCTION_DECL)
39e2db00 408 return (e->node && e->node->analyzed);
d7f09764 409
200c8750 410 else if (TREE_CODE (e->decl) == VAR_DECL)
9e0546ef
JH
411 {
412 if (!e->vnode)
413 return false;
cd35bcf7 414 return e->vnode->finalized;
9e0546ef 415 }
d7f09764 416
200c8750 417 gcc_unreachable ();
d7f09764
DN
418}
419
1a735925
RG
420/* Resolve the symbol with the candidates in the chain *SLOT and store
421 their resolutions. */
d7f09764 422
1a735925
RG
423static void
424lto_symtab_resolve_symbols (void **slot)
425{
a5ac2cdf 426 lto_symtab_entry_t e;
200c8750 427 lto_symtab_entry_t prevailing = NULL;
d7f09764 428
a5ac2cdf
RAE
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)
431 {
432 if (TREE_CODE (e->decl) == FUNCTION_DECL)
ab079773 433 e->node = cgraph_get_node (e->decl);
2942c502 434 else if (TREE_CODE (e->decl) == VAR_DECL)
ef8fc6c2 435 e->vnode = varpool_get_node (e->decl);
ed0d2da0
JH
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;
a5ac2cdf
RAE
440 }
441
a5ac2cdf 442 /* If the chain is already resolved there is nothing else to do. */
ed0d2da0 443 if (prevailing)
1a735925
RG
444 return;
445
200c8750
RG
446 /* Find the single non-replaceable prevailing symbol and
447 diagnose ODR violations. */
a5ac2cdf 448 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
1a735925 449 {
200c8750
RG
450 if (!lto_symtab_resolve_can_prevail_p (e))
451 {
452 e->resolution = LDPR_RESOLVED_IR;
051f8cc6 453 e->guessed = true;
200c8750
RG
454 continue;
455 }
456
457 /* Set a default resolution - the final prevailing one will get
458 adjusted later. */
459 e->resolution = LDPR_PREEMPTED_IR;
051f8cc6 460 e->guessed = true;
200c8750
RG
461 if (!lto_symtab_resolve_replaceable_p (e))
462 {
463 if (prevailing)
464 {
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");
469 }
470 prevailing = e;
471 }
472 }
473 if (prevailing)
474 goto found;
475
476 /* Do a second round choosing one from the replaceable prevailing decls. */
477 for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
478 {
479 if (e->resolution != LDPR_PREEMPTED_IR)
480 continue;
481
482 /* Choose the first function that can prevail as prevailing. */
483 if (TREE_CODE (e->decl) == FUNCTION_DECL)
1a735925 484 {
200c8750
RG
485 prevailing = e;
486 break;
1a735925 487 }
200c8750
RG
488
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 prevailing = e;
1a735925 494 }
200c8750
RG
495
496 if (!prevailing)
497 return;
498
499found:
6d41cd02
BM
500 /* If current lto files represent the whole program,
501 it is correct to use LDPR_PREVALING_DEF_IRONLY.
502 If current lto files are part of whole program, internal
503 resolver doesn't know if it is LDPR_PREVAILING_DEF
504 or LDPR_PREVAILING_DEF_IRONLY. Use IRONLY conforms to
505 using -fwhole-program. Otherwise, it doesn't
506 matter using either LDPR_PREVAILING_DEF or
507 LDPR_PREVAILING_DEF_IRONLY
508
509 FIXME: above workaround due to gold plugin makes some
510 variables IRONLY, which are indeed PREVAILING_DEF in
511 resolution file. These variables still need manual
512 externally_visible attribute. */
200c8750 513 prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
051f8cc6 514 prevailing->guessed = true;
1a735925
RG
515}
516
200c8750 517/* Merge all decls in the symbol table chain to the prevailing decl and
97ce54b9
RG
518 issue diagnostics about type mismatches. If DIAGNOSED_P is true
519 do not issue further diagnostics.*/
1a735925
RG
520
521static void
97ce54b9 522lto_symtab_merge_decls_2 (void **slot, bool diagnosed_p)
1a735925 523{
200c8750
RG
524 lto_symtab_entry_t prevailing, e;
525 VEC(tree, heap) *mismatches = NULL;
526 unsigned i;
527 tree decl;
1a735925
RG
528
529 /* Nothing to do for a single entry. */
200c8750
RG
530 prevailing = (lto_symtab_entry_t) *slot;
531 if (!prevailing->next)
1a735925
RG
532 return;
533
200c8750
RG
534 /* Try to merge each entry with the prevailing one. */
535 for (e = prevailing->next; e; e = e->next)
536 {
97ce54b9
RG
537 if (!lto_symtab_merge (prevailing, e)
538 && !diagnosed_p)
200c8750
RG
539 VEC_safe_push (tree, heap, mismatches, e->decl);
540 }
541 if (VEC_empty (tree, mismatches))
542 return;
1a735925 543
200c8750 544 /* Diagnose all mismatched re-declarations. */
ac47786e 545 FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
1a735925 546 {
0ca8de87 547 if (!types_compatible_p (TREE_TYPE (prevailing->decl), TREE_TYPE (decl)))
200c8750
RG
548 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
549 "type of %qD does not match original "
550 "declaration", decl);
551
552 else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
553 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
1a735925 554 {
200c8750
RG
555 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
556 "alignment of %qD is bigger than "
557 "original declaration", decl);
1a735925 558 }
1a735925 559 }
200c8750
RG
560 if (diagnosed_p)
561 inform (DECL_SOURCE_LOCATION (prevailing->decl),
562 "previously declared here");
1a735925 563
200c8750 564 VEC_free (tree, heap, mismatches);
1a735925
RG
565}
566
567/* Helper to process the decl chain for the symbol table entry *SLOT. */
568
569static int
570lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
d7f09764 571{
200c8750
RG
572 lto_symtab_entry_t e, prevailing;
573 bool diagnosed_p = false;
1a735925 574
200c8750
RG
575 /* Compute the symbol resolutions. This is a no-op when using the
576 linker plugin. */
1a735925
RG
577 lto_symtab_resolve_symbols (slot);
578
200c8750
RG
579 /* Find the prevailing decl. */
580 for (prevailing = (lto_symtab_entry_t) *slot;
581 prevailing
582 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
ed0d2da0 583 && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP
200c8750
RG
584 && prevailing->resolution != LDPR_PREVAILING_DEF;
585 prevailing = prevailing->next)
586 ;
587
588 /* Assert it's the only one. */
589 if (prevailing)
590 for (e = prevailing->next; e; e = e->next)
e56d9a76
AK
591 {
592 if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
ed0d2da0 593 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
e56d9a76
AK
594 || e->resolution == LDPR_PREVAILING_DEF)
595 fatal_error ("multiple prevailing defs for %qE",
596 DECL_NAME (prevailing->decl));
597 }
200c8750
RG
598
599 /* If there's not a prevailing symbol yet it's an external reference.
600 Happens a lot during ltrans. Choose the first symbol with a
601 cgraph or a varpool node. */
602 if (!prevailing)
603 {
604 prevailing = (lto_symtab_entry_t) *slot;
605 /* For functions choose one with a cgraph node. */
606 if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
2c928155 607 while (!prevailing->node
200c8750
RG
608 && prevailing->next)
609 prevailing = prevailing->next;
9e0546ef
JH
610 /* For variables chose with a priority variant with vnode
611 attached (i.e. from unit where external declaration of
612 variable is actually used).
613 When there are multiple variants, chose one with size.
614 This is needed for C++ typeinfos, for example in
615 lto/20081204-1 there are typeifos in both units, just
616 one of them do have size. */
2942c502 617 if (TREE_CODE (prevailing->decl) == VAR_DECL)
9e0546ef
JH
618 {
619 for (e = prevailing->next; e; e = e->next)
620 if ((!prevailing->vnode && e->vnode)
621 || ((prevailing->vnode != NULL) == (e->vnode != NULL)
622 && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
623 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
624 prevailing = e;
625 }
200c8750 626 }
1a735925 627
200c8750
RG
628 /* Move it first in the list. */
629 if ((lto_symtab_entry_t) *slot != prevailing)
630 {
631 for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
632 ;
633 e->next = prevailing->next;
634 prevailing->next = (lto_symtab_entry_t) *slot;
635 *slot = (void *) prevailing;
636 }
1a735925 637
200c8750
RG
638 /* Record the prevailing variable. */
639 if (TREE_CODE (prevailing->decl) == VAR_DECL)
640 VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
1a735925 641
200c8750
RG
642 /* Diagnose mismatched objects. */
643 for (e = prevailing->next; e; e = e->next)
644 {
645 if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
646 continue;
1a735925 647
200c8750
RG
648 switch (TREE_CODE (prevailing->decl))
649 {
650 case VAR_DECL:
651 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
652 error_at (DECL_SOURCE_LOCATION (e->decl),
653 "variable %qD redeclared as function", prevailing->decl);
654 break;
655
656 case FUNCTION_DECL:
657 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
658 error_at (DECL_SOURCE_LOCATION (e->decl),
659 "function %qD redeclared as variable", prevailing->decl);
660 break;
1a735925 661
200c8750
RG
662 default:
663 gcc_unreachable ();
664 }
665
666 diagnosed_p = true;
667 }
668 if (diagnosed_p)
669 inform (DECL_SOURCE_LOCATION (prevailing->decl),
670 "previously declared here");
671
200c8750
RG
672 /* Merge the chain to the single prevailing decl and diagnose
673 mismatches. */
97ce54b9 674 lto_symtab_merge_decls_2 (slot, diagnosed_p);
1a735925 675
051f8cc6
JH
676 /* Store resolution decision into the callgraph.
677 In LTRANS don't overwrite information we stored into callgraph at
678 WPA stage.
679
680 Do not bother to store guessed decisions. Generic code knows how
681 to handle UNKNOWN relocation well.
682
683 The problem with storing guessed decision is whether to use
ed0d2da0
JH
684 PREVAILING_DEF, PREVAILING_DEF_IRONLY, PREVAILING_DEF_IRONLY_EXP.
685 First one would disable some whole program optimizations, while
686 ther second would imply to many whole program assumptions. */
051f8cc6 687 if (prevailing->node && !flag_ltrans && !prevailing->guessed)
960bfb69 688 prevailing->node->symbol.resolution = prevailing->resolution;
051f8cc6 689 else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed)
960bfb69 690 prevailing->vnode->symbol.resolution = prevailing->resolution;
1a735925 691 return 1;
d7f09764
DN
692}
693
1a735925 694/* Resolve and merge all symbol table chains to a prevailing decl. */
d7f09764
DN
695
696void
1a735925 697lto_symtab_merge_decls (void)
d7f09764 698{
1a735925
RG
699 lto_symtab_maybe_init_hash_table ();
700 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
d7f09764
DN
701}
702
2c928155
RG
703/* Helper to process the decl chain for the symbol table entry *SLOT. */
704
705static int
706lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
707{
708 lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
709
710 if (!prevailing->next)
711 return 1;
712
2c928155
RG
713 /* Replace the cgraph node of each entry with the prevailing one. */
714 for (e = prevailing->next; e; e = e->next)
715 {
716 if (e->node != NULL)
5ba58d4f
JH
717 {
718 /* In case we prevail funcion by an alias, we can run into case
719 that the alias has no cgraph node attached, since it was
720 previously unused. Create the node. */
721 if (!prevailing->node)
722 {
a358e188 723 prevailing->node = cgraph_create_node (prevailing->decl);
5ba58d4f
JH
724 prevailing->node->alias = true;
725 }
726 lto_cgraph_replace_node (e->node, prevailing->node);
727 }
2942c502 728 if (e->vnode != NULL)
5ba58d4f
JH
729 {
730 if (!prevailing->vnode)
731 {
732 prevailing->vnode = varpool_node (prevailing->decl);
733 prevailing->vnode->alias = true;
734 }
735 lto_varpool_replace_node (e->vnode, prevailing->vnode);
736 }
2c928155
RG
737 }
738
739 /* Drop all but the prevailing decl from the symtab. */
740 prevailing->next = NULL;
741
742 return 1;
743}
744
745/* Merge cgraph nodes according to the symbol merging done by
746 lto_symtab_merge_decls. */
747
748void
749lto_symtab_merge_cgraph_nodes (void)
750{
39e2db00 751 struct cgraph_node *node;
cd35bcf7 752 struct varpool_node *vnode;
2c928155
RG
753 lto_symtab_maybe_init_hash_table ();
754 htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
87e7b310 755
65c70e6b 756 FOR_EACH_FUNCTION (node)
39e2db00
JH
757 if ((node->thunk.thunk_p || node->alias)
758 && node->thunk.alias)
759 node->thunk.alias = lto_symtab_prevailing_decl (node->thunk.alias);
65c70e6b 760 FOR_EACH_VARIABLE (vnode)
cd35bcf7
JH
761 if (vnode->alias_of)
762 vnode->alias_of = lto_symtab_prevailing_decl (vnode->alias_of);
2c928155 763}
1a735925 764
d7f09764
DN
765/* Given the decl DECL, return the prevailing decl with the same name. */
766
767tree
768lto_symtab_prevailing_decl (tree decl)
769{
1a735925 770 lto_symtab_entry_t ret;
d7f09764
DN
771
772 /* Builtins and local symbols are their own prevailing decl. */
773 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
774 return decl;
775
776 /* DECL_ABSTRACTs are their own prevailng decl. */
777 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
778 return decl;
779
780 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
781 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
782
783 /* Walk through the list of candidates and return the one we merged to. */
77754180
DK
784 ret = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
785 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1a735925
RG
786 if (!ret)
787 return NULL_TREE;
788
200c8750 789 return ret->decl;
d7f09764
DN
790}
791
d7f09764 792#include "gt-lto-symtab.h"
This page took 1.205864 seconds and 5 git commands to generate.