]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lto/lto-symtab.c
Always pass explicit location to fatal_error.
[thirdparty/gcc.git] / gcc / lto / lto-symtab.c
CommitLineData
7bfefa9d 1/* LTO symbol table.
d353bf18 2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
7bfefa9d 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"
0b205f4c 24#include "diagnostic-core.h"
94ea8568 25#include "hash-set.h"
26#include "machmode.h"
b20a8bb4 27#include "vec.h"
28#include "double-int.h"
29#include "input.h"
30#include "alias.h"
31#include "symtab.h"
32#include "options.h"
33#include "wide-int.h"
34#include "inchash.h"
35#include "tree.h"
36#include "fold-const.h"
37#include "predict.h"
94ea8568 38#include "tm.h"
39#include "hard-reg-set.h"
40#include "input.h"
41#include "function.h"
bc61cadb 42#include "basic-block.h"
43#include "tree-ssa-alias.h"
44#include "internal-fn.h"
45#include "gimple-expr.h"
46#include "is-a.h"
7bfefa9d 47#include "gimple.h"
7bfefa9d 48#include "plugin-api.h"
1140c305 49#include "hash-map.h"
50#include "ipa-ref.h"
51#include "cgraph.h"
7bfefa9d 52#include "lto-streamer.h"
06d12890 53#include "ipa-utils.h"
1140c305 54#include "alloc-pool.h"
2cc80ac3 55#include "symbol-summary.h"
1140c305 56#include "ipa-prop.h"
38fe12e3 57#include "ipa-inline.h"
f7715905 58#include "builtins.h"
7bfefa9d 59
bc0ed27c 60/* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
61 all edges and removing the old node. */
7bfefa9d 62
bc0ed27c 63static void
64lto_cgraph_replace_node (struct cgraph_node *node,
65 struct cgraph_node *prevailing_node)
7bfefa9d 66{
bc0ed27c 67 struct cgraph_edge *e, *next;
fa7a183a 68 bool compatible_p;
0f4e132d 69
35ee1c66 70 if (symtab->dump_file)
0f4e132d 71 {
35ee1c66 72 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
0f4e132d 73 " for symbol %s\n",
f1c8b4d7 74 node->name (), node->order,
75 prevailing_node->name (),
02774f2d 76 prevailing_node->order,
d86d364d 77 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
02774f2d 78 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
0f4e132d 79 }
80
bc0ed27c 81 /* Merge node flags. */
02774f2d 82 if (node->force_output)
415d1b9a 83 prevailing_node->mark_force_output ();
b89ce5e3 84 if (node->forced_by_abi)
85 prevailing_node->forced_by_abi = true;
02774f2d 86 if (node->address_taken)
7bfefa9d 87 {
bc0ed27c 88 gcc_assert (!prevailing_node->global.inlined_to);
415d1b9a 89 prevailing_node->mark_address_taken ();
bc0ed27c 90 }
7bfefa9d 91
bc0ed27c 92 /* Redirect all incoming edges. */
fa7a183a 93 compatible_p
02774f2d 94 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
95 TREE_TYPE (TREE_TYPE (node->decl)));
bc0ed27c 96 for (e = node->callers; e; e = next)
97 {
98 next = e->next_caller;
35ee1c66 99 e->redirect_callee (prevailing_node);
fa7a183a 100 /* If there is a mismatch between the supposed callee return type and
101 the real one do not attempt to inline this function.
102 ??? We really need a way to match function signatures for ABI
103 compatibility and perform related promotions at inlining time. */
104 if (!compatible_p)
105 e->call_stmt_cannot_inline_p = 1;
bc0ed27c 106 }
8d810329 107 /* Redirect incomming references. */
51ce5652 108 prevailing_node->clone_referring (node);
7bfefa9d 109
15d46991 110 /* Fix instrumentation references. */
111 if (node->instrumented_version)
112 {
113 gcc_assert (node->instrumentation_clone
114 == prevailing_node->instrumentation_clone);
115 node->instrumented_version->instrumented_version = prevailing_node;
116 if (!prevailing_node->instrumented_version)
117 prevailing_node->instrumented_version = node->instrumented_version;
118 /* Need to reset node->instrumented_version to NULL,
119 otherwise node removal code would reset
120 node->instrumented_version->instrumented_version. */
121 node->instrumented_version = NULL;
122 }
123
06d12890 124 ipa_merge_profiles (prevailing_node, node);
02774f2d 125 lto_free_function_in_decl_state_for_node (node);
5f74a074 126
02774f2d 127 if (node->decl != prevailing_node->decl)
415d1b9a 128 node->release_body ();
b9b49047 129
bc0ed27c 130 /* Finally remove the replaced node. */
415d1b9a 131 node->remove ();
bc0ed27c 132}
133
0cddb138 134/* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
135 all edges and removing the old node. */
136
137static void
098f44bc 138lto_varpool_replace_node (varpool_node *vnode,
139 varpool_node *prevailing_node)
0cddb138 140{
02774f2d 141 gcc_assert (!vnode->definition || prevailing_node->definition);
142 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
0cddb138 143
51ce5652 144 prevailing_node->clone_referring (vnode);
b89ce5e3 145 if (vnode->force_output)
146 prevailing_node->force_output = true;
147 if (vnode->forced_by_abi)
148 prevailing_node->forced_by_abi = true;
8d810329 149
6f932b06 150 /* Be sure we can garbage collect the initializer. */
02774f2d 151 if (DECL_INITIAL (vnode->decl)
152 && vnode->decl != prevailing_node->decl)
153 DECL_INITIAL (vnode->decl) = error_mark_node;
b7393cee 154
1df805e3 155 /* Check and report ODR violations on virtual tables. */
156 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
157 compare_virtual_tables (prevailing_node, vnode);
158
b7393cee 159 if (vnode->tls_model != prevailing_node->tls_model)
160 {
bbcd979a 161 bool error = false;
162
163 /* Non-TLS and TLS never mix together. Also emulated model is not
164 compatible with anything else. */
165 if (prevailing_node->tls_model == TLS_MODEL_NONE
166 || prevailing_node->tls_model == TLS_MODEL_EMULATED
167 || vnode->tls_model == TLS_MODEL_NONE
168 || vnode->tls_model == TLS_MODEL_EMULATED)
169 error = true;
170 /* Linked is silently supporting transitions
171 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
172 Do the same transitions and error out on others. */
173 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
174 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
175 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
176 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
177 prevailing_node->tls_model = vnode->tls_model;
178 else if ((vnode->tls_model == TLS_MODEL_REAL
179 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
180 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
181 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
182 ;
183 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
184 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
185 prevailing_node->tls_model = vnode->tls_model;
186 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
187 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
188 ;
189 else
190 error = true;
191 if (error)
192 {
193 error_at (DECL_SOURCE_LOCATION (vnode->decl),
194 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
195 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
196 "previously defined here as %s",
197 tls_model_names [prevailing_node->tls_model]);
198 }
b7393cee 199 }
0cddb138 200 /* Finally remove the replaced node. */
415d1b9a 201 vnode->remove ();
0cddb138 202}
203
bc0ed27c 204/* Merge two variable or function symbol table entries PREVAILING and ENTRY.
205 Return false if the symbols are not fully compatible and a diagnostic
206 should be emitted. */
207
208static bool
452659af 209lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
bc0ed27c 210{
02774f2d 211 tree prevailing_decl = prevailing->decl;
212 tree decl = entry->decl;
bc0ed27c 213 tree prevailing_type, type;
bc0ed27c 214
c9aa6453 215 if (prevailing_decl == decl)
216 return true;
217
bc0ed27c 218 /* Merge decl state in both directions, we may still end up using
219 the new decl. */
220 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
221 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
222
bc0ed27c 223 /* The linker may ask us to combine two incompatible symbols.
224 Detect this case and notify the caller of required diagnostics. */
225
226 if (TREE_CODE (decl) == FUNCTION_DECL)
fdd4f660 227 {
79e191db 228 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
229 TREE_TYPE (decl)))
fdd4f660 230 /* If we don't have a merged type yet...sigh. The linker
231 wouldn't complain if the types were mismatched, so we
232 probably shouldn't either. Just use the type from
233 whichever decl appears to be associated with the
234 definition. If for some odd reason neither decl is, the
235 older one wins. */
236 (void) 0;
237
238 return true;
239 }
240
241 /* Now we exclusively deal with VAR_DECLs. */
242
66bd377c 243 /* Sharing a global symbol is a strong hint that two types are
244 compatible. We could use this information to complete
245 incomplete pointed-to types more aggressively here, ignoring
246 mismatches in both field and tag names. It's difficult though
247 to guarantee that this does not have side-effects on merging
248 more compatible types from other translation units though. */
7bfefa9d 249
fdd4f660 250 /* We can tolerate differences in type qualification, the
66bd377c 251 qualification of the prevailing definition will prevail.
252 ??? In principle we might want to only warn for structurally
253 incompatible types here, but unless we have protective measures
254 for TBAA in place that would hide useful information. */
bc0ed27c 255 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
256 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
66bd377c 257
79e191db 258 if (!types_compatible_p (prevailing_type, type))
66bd377c 259 {
260 if (COMPLETE_TYPE_P (type))
261 return false;
262
263 /* If type is incomplete then avoid warnings in the cases
264 that TBAA handles just fine. */
265
266 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
267 return false;
268
269 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
270 {
271 tree tem1 = TREE_TYPE (prevailing_type);
272 tree tem2 = TREE_TYPE (type);
273 while (TREE_CODE (tem1) == ARRAY_TYPE
274 && TREE_CODE (tem2) == ARRAY_TYPE)
275 {
276 tem1 = TREE_TYPE (tem1);
277 tem2 = TREE_TYPE (tem2);
278 }
279
280 if (TREE_CODE (tem1) != TREE_CODE (tem2))
281 return false;
282
79e191db 283 if (!types_compatible_p (tem1, tem2))
66bd377c 284 return false;
285 }
286
287 /* Fallthru. Compatible enough. */
288 }
7bfefa9d 289
fdd4f660 290 /* ??? We might want to emit a warning here if type qualification
291 differences were spotted. Do not do this unconditionally though. */
7bfefa9d 292
fdd4f660 293 /* There is no point in comparing too many details of the decls here.
294 The type compatibility checks or the completing of types has properly
295 dealt with most issues. */
7bfefa9d 296
fdd4f660 297 /* The following should all not invoke fatal errors as in non-LTO
298 mode the linker wouldn't complain either. Just emit warnings. */
7bfefa9d 299
fdd4f660 300 /* Report a warning if user-specified alignments do not match. */
bc0ed27c 301 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
302 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
303 return false;
7bfefa9d 304
7bfefa9d 305 return true;
306}
307
bc0ed27c 308/* Return true if the symtab entry E can be replaced by another symtab
309 entry. */
7bfefa9d 310
bc0ed27c 311static bool
452659af 312lto_symtab_resolve_replaceable_p (symtab_node *e)
7bfefa9d 313{
02774f2d 314 if (DECL_EXTERNAL (e->decl)
315 || DECL_COMDAT (e->decl)
316 || DECL_ONE_ONLY (e->decl)
317 || DECL_WEAK (e->decl))
bc0ed27c 318 return true;
7bfefa9d 319
02774f2d 320 if (TREE_CODE (e->decl) == VAR_DECL)
321 return (DECL_COMMON (e->decl)
322 || (!flag_no_common && !DECL_INITIAL (e->decl)));
7bfefa9d 323
bc0ed27c 324 return false;
fd193bcd 325}
7bfefa9d 326
48bbff00 327/* Return true, if the symbol E should be resolved by lto-symtab.
33864132 328 Those are all external symbols and all real symbols that are not static (we
329 handle renaming of static later in partitioning). */
48bbff00 330
331static bool
452659af 332lto_symtab_symbol_p (symtab_node *e)
48bbff00 333{
02774f2d 334 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
33864132 335 return false;
415d1b9a 336 return e->real_symbol_p ();
48bbff00 337}
338
bc0ed27c 339/* Return true if the symtab entry E can be the prevailing one. */
7bfefa9d 340
bc0ed27c 341static bool
452659af 342lto_symtab_resolve_can_prevail_p (symtab_node *e)
fd193bcd 343{
48bbff00 344 if (!lto_symtab_symbol_p (e))
c9aa6453 345 return false;
346
d91c3d40 347 /* The C++ frontend ends up neither setting TREE_STATIC nor
348 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
349 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
02774f2d 350 if (DECL_EXTERNAL (e->decl))
bc0ed27c 351 return false;
7bfefa9d 352
02774f2d 353 return e->definition;
7bfefa9d 354}
355
fd193bcd 356/* Resolve the symbol with the candidates in the chain *SLOT and store
357 their resolutions. */
7bfefa9d 358
452659af 359static symtab_node *
360lto_symtab_resolve_symbols (symtab_node *first)
fd193bcd 361{
452659af 362 symtab_node *e;
363 symtab_node *prevailing = NULL;
7bfefa9d 364
67b2bb71 365 /* Always set e->node so that edges are updated to reflect decl merging. */
02774f2d 366 for (e = first; e; e = e->next_sharing_asm_name)
48bbff00 367 if (lto_symtab_symbol_p (e)
02774f2d 368 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
369 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
370 || e->resolution == LDPR_PREVAILING_DEF))
67575a90 371 {
372 prevailing = e;
373 break;
374 }
67b2bb71 375
67b2bb71 376 /* If the chain is already resolved there is nothing else to do. */
da722561 377 if (prevailing)
67575a90 378 {
379 /* Assert it's the only one. */
02774f2d 380 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
48bbff00 381 if (lto_symtab_symbol_p (e)
02774f2d 382 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
383 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
384 || e->resolution == LDPR_PREVAILING_DEF))
c05be867 385 fatal_error (input_location, "multiple prevailing defs for %qE",
02774f2d 386 DECL_NAME (prevailing->decl));
67575a90 387 return prevailing;
388 }
fd193bcd 389
bc0ed27c 390 /* Find the single non-replaceable prevailing symbol and
391 diagnose ODR violations. */
02774f2d 392 for (e = first; e; e = e->next_sharing_asm_name)
fd193bcd 393 {
bc0ed27c 394 if (!lto_symtab_resolve_can_prevail_p (e))
67575a90 395 continue;
bc0ed27c 396
67575a90 397 /* If we have a non-replaceable definition it prevails. */
bc0ed27c 398 if (!lto_symtab_resolve_replaceable_p (e))
399 {
400 if (prevailing)
401 {
02774f2d 402 error_at (DECL_SOURCE_LOCATION (e->decl),
403 "%qD has already been defined", e->decl);
404 inform (DECL_SOURCE_LOCATION (prevailing->decl),
bc0ed27c 405 "previously defined here");
406 }
407 prevailing = e;
408 }
409 }
410 if (prevailing)
67575a90 411 return prevailing;
bc0ed27c 412
413 /* Do a second round choosing one from the replaceable prevailing decls. */
02774f2d 414 for (e = first; e; e = e->next_sharing_asm_name)
bc0ed27c 415 {
48bbff00 416 if (!lto_symtab_resolve_can_prevail_p (e))
bc0ed27c 417 continue;
418
419 /* Choose the first function that can prevail as prevailing. */
02774f2d 420 if (TREE_CODE (e->decl) == FUNCTION_DECL)
fd193bcd 421 {
bc0ed27c 422 prevailing = e;
423 break;
fd193bcd 424 }
bc0ed27c 425
426 /* From variables that can prevail choose the largest one. */
427 if (!prevailing
02774f2d 428 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
429 DECL_SIZE (e->decl))
aa419a52 430 /* When variables are equivalent try to chose one that has useful
431 DECL_INITIAL. This makes sense for keyed vtables that are
432 DECL_EXTERNAL but initialized. In units that do not need them
433 we replace the initializer by error_mark_node to conserve
434 memory.
435
436 We know that the vtable is keyed outside the LTO unit - otherwise
437 the keyed instance would prevail. We still can preserve useful
438 info in the initializer. */
02774f2d 439 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
440 && (DECL_INITIAL (e->decl)
441 && DECL_INITIAL (e->decl) != error_mark_node)
442 && (!DECL_INITIAL (prevailing->decl)
443 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
bc0ed27c 444 prevailing = e;
fd193bcd 445 }
bc0ed27c 446
67575a90 447 return prevailing;
fd193bcd 448}
449
bc0ed27c 450/* Merge all decls in the symbol table chain to the prevailing decl and
1b9e1cc0 451 issue diagnostics about type mismatches. If DIAGNOSED_P is true
452 do not issue further diagnostics.*/
fd193bcd 453
454static void
452659af 455lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
fd193bcd 456{
452659af 457 symtab_node *prevailing;
458 symtab_node *e;
1e094109 459 vec<tree> mismatches = vNULL;
bc0ed27c 460 unsigned i;
461 tree decl;
fd193bcd 462
463 /* Nothing to do for a single entry. */
c9aa6453 464 prevailing = first;
02774f2d 465 if (!prevailing->next_sharing_asm_name)
fd193bcd 466 return;
467
bc0ed27c 468 /* Try to merge each entry with the prevailing one. */
02774f2d 469 for (e = prevailing->next_sharing_asm_name;
470 e; e = e->next_sharing_asm_name)
471 if (TREE_PUBLIC (e->decl))
48bbff00 472 {
473 if (!lto_symtab_merge (prevailing, e)
474 && !diagnosed_p)
02774f2d 475 mismatches.safe_push (e->decl);
48bbff00 476 }
f1f41a6c 477 if (mismatches.is_empty ())
bc0ed27c 478 return;
fd193bcd 479
bc0ed27c 480 /* Diagnose all mismatched re-declarations. */
f1f41a6c 481 FOR_EACH_VEC_ELT (mismatches, i, decl)
fd193bcd 482 {
02774f2d 483 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
c9aa6453 484 TREE_TYPE (decl)))
bc0ed27c 485 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
486 "type of %qD does not match original "
487 "declaration", decl);
488
02774f2d 489 else if ((DECL_USER_ALIGN (prevailing->decl)
c9aa6453 490 && DECL_USER_ALIGN (decl))
02774f2d 491 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
fd193bcd 492 {
bc0ed27c 493 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
494 "alignment of %qD is bigger than "
495 "original declaration", decl);
fd193bcd 496 }
fd193bcd 497 }
bc0ed27c 498 if (diagnosed_p)
02774f2d 499 inform (DECL_SOURCE_LOCATION (prevailing->decl),
bc0ed27c 500 "previously declared here");
fd193bcd 501
f1f41a6c 502 mismatches.release ();
fd193bcd 503}
504
505/* Helper to process the decl chain for the symbol table entry *SLOT. */
506
c9aa6453 507static void
452659af 508lto_symtab_merge_decls_1 (symtab_node *first)
7bfefa9d 509{
452659af 510 symtab_node *e;
511 symtab_node *prevailing;
bc0ed27c 512 bool diagnosed_p = false;
fd193bcd 513
35ee1c66 514 if (symtab->dump_file)
c9aa6453 515 {
35ee1c66 516 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
f1c8b4d7 517 first->asm_name ());
02774f2d 518 for (e = first; e; e = e->next_sharing_asm_name)
519 if (TREE_PUBLIC (e->decl))
35ee1c66 520 e->dump (symtab->dump_file);
c9aa6453 521 }
522
bc0ed27c 523 /* Compute the symbol resolutions. This is a no-op when using the
c9aa6453 524 linker plugin and resolution was decided by the linker. */
67575a90 525 prevailing = lto_symtab_resolve_symbols (first);
bc0ed27c 526
527 /* If there's not a prevailing symbol yet it's an external reference.
528 Happens a lot during ltrans. Choose the first symbol with a
529 cgraph or a varpool node. */
530 if (!prevailing)
531 {
24888760 532 for (prevailing = first;
533 prevailing; prevailing = prevailing->next_sharing_asm_name)
534 if (lto_symtab_symbol_p (prevailing))
535 break;
536 if (!prevailing)
537 return;
9269f553 538 /* For variables chose with a priority variant with vnode
539 attached (i.e. from unit where external declaration of
540 variable is actually used).
541 When there are multiple variants, chose one with size.
542 This is needed for C++ typeinfos, for example in
543 lto/20081204-1 there are typeifos in both units, just
544 one of them do have size. */
02774f2d 545 if (TREE_CODE (prevailing->decl) == VAR_DECL)
9269f553 546 {
02774f2d 547 for (e = prevailing->next_sharing_asm_name;
548 e; e = e->next_sharing_asm_name)
549 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
550 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
48bbff00 551 && lto_symtab_symbol_p (e))
9269f553 552 prevailing = e;
553 }
871609a8 554 /* For variables prefer the non-builtin if one is available. */
02774f2d 555 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
5b777cea 556 {
02774f2d 557 for (e = first; e; e = e->next_sharing_asm_name)
558 if (TREE_CODE (e->decl) == FUNCTION_DECL
559 && !DECL_BUILT_IN (e->decl)
48bbff00 560 && lto_symtab_symbol_p (e))
5b777cea 561 {
562 prevailing = e;
563 break;
564 }
565 }
bc0ed27c 566 }
fd193bcd 567
35ee1c66 568 symtab->symtab_prevail_in_asm_name_hash (prevailing);
fd193bcd 569
bc0ed27c 570 /* Diagnose mismatched objects. */
02774f2d 571 for (e = prevailing->next_sharing_asm_name;
572 e; e = e->next_sharing_asm_name)
bc0ed27c 573 {
02774f2d 574 if (TREE_CODE (prevailing->decl)
575 == TREE_CODE (e->decl))
bc0ed27c 576 continue;
48bbff00 577 if (!lto_symtab_symbol_p (e))
578 continue;
fd193bcd 579
02774f2d 580 switch (TREE_CODE (prevailing->decl))
bc0ed27c 581 {
582 case VAR_DECL:
02774f2d 583 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
584 error_at (DECL_SOURCE_LOCATION (e->decl),
c9aa6453 585 "variable %qD redeclared as function",
02774f2d 586 prevailing->decl);
bc0ed27c 587 break;
588
589 case FUNCTION_DECL:
02774f2d 590 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
591 error_at (DECL_SOURCE_LOCATION (e->decl),
c9aa6453 592 "function %qD redeclared as variable",
02774f2d 593 prevailing->decl);
bc0ed27c 594 break;
fd193bcd 595
bc0ed27c 596 default:
597 gcc_unreachable ();
598 }
599
600 diagnosed_p = true;
601 }
602 if (diagnosed_p)
02774f2d 603 inform (DECL_SOURCE_LOCATION (prevailing->decl),
bc0ed27c 604 "previously declared here");
605
bc0ed27c 606 /* Merge the chain to the single prevailing decl and diagnose
607 mismatches. */
a1003dd5 608 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
c9aa6453 609
35ee1c66 610 if (symtab->dump_file)
c9aa6453 611 {
35ee1c66 612 fprintf (symtab->dump_file, "After resolution:\n");
02774f2d 613 for (e = prevailing; e; e = e->next_sharing_asm_name)
35ee1c66 614 e->dump (symtab->dump_file);
c9aa6453 615 }
7bfefa9d 616}
617
fd193bcd 618/* Resolve and merge all symbol table chains to a prevailing decl. */
7bfefa9d 619
620void
fd193bcd 621lto_symtab_merge_decls (void)
7bfefa9d 622{
452659af 623 symtab_node *node;
c9aa6453 624
c9aa6453 625 /* Populate assembler name hash. */
35ee1c66 626 symtab->symtab_initialize_asm_name_hash ();
c9aa6453 627
628 FOR_EACH_SYMBOL (node)
02774f2d 629 if (!node->previous_sharing_asm_name
630 && node->next_sharing_asm_name)
97e13fca 631 lto_symtab_merge_decls_1 (node);
7bfefa9d 632}
633
21ce3cc7 634/* Helper to process the decl chain for the symbol table entry *SLOT. */
635
c9aa6453 636static void
452659af 637lto_symtab_merge_symbols_1 (symtab_node *prevailing)
21ce3cc7 638{
452659af 639 symtab_node *e;
640 symtab_node *next;
21ce3cc7 641
21ce3cc7 642 /* Replace the cgraph node of each entry with the prevailing one. */
02774f2d 643 for (e = prevailing->next_sharing_asm_name; e;
c9aa6453 644 e = next)
21ce3cc7 645 {
02774f2d 646 next = e->next_sharing_asm_name;
21ce3cc7 647
48bbff00 648 if (!lto_symtab_symbol_p (e))
c9aa6453 649 continue;
13cbeaac 650 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
02774f2d 651 if (ce && !DECL_BUILT_IN (e->decl))
415d1b9a 652 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
13cbeaac 653 if (varpool_node *ve = dyn_cast <varpool_node *> (e))
415d1b9a 654 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
c9aa6453 655 }
21ce3cc7 656
c9aa6453 657 return;
21ce3cc7 658}
659
660/* Merge cgraph nodes according to the symbol merging done by
661 lto_symtab_merge_decls. */
662
663void
48669653 664lto_symtab_merge_symbols (void)
21ce3cc7 665{
452659af 666 symtab_node *node;
c9aa6453 667
c9aa6453 668 if (!flag_ltrans)
c9aa6453 669 {
35ee1c66 670 symtab->symtab_initialize_asm_name_hash ();
48669653 671
9e9c3e92 672 /* Do the actual merging.
673 At this point we invalidate hash translating decls into symtab nodes
674 because after removing one of duplicate decls the hash is not correcly
675 updated to the ohter dupliate. */
48669653 676 FOR_EACH_SYMBOL (node)
677 if (lto_symtab_symbol_p (node)
02774f2d 678 && node->next_sharing_asm_name
679 && !node->previous_sharing_asm_name)
48669653 680 lto_symtab_merge_symbols_1 (node);
681
9e9c3e92 682 /* Resolve weakref aliases whose target are now in the compilation unit.
683 also re-populate the hash translating decls into symtab nodes*/
48669653 684 FOR_EACH_SYMBOL (node)
fc8456b4 685 {
da4b8721 686 cgraph_node *cnode, *cnode2;
46000538 687 varpool_node *vnode;
452659af 688 symtab_node *node2;
46000538 689
02774f2d 690 if (!node->analyzed && node->alias_target)
fc8456b4 691 {
35ee1c66 692 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
02774f2d 693 gcc_assert (node->weakref);
48669653 694 if (tgt)
415d1b9a 695 node->resolve_alias (tgt);
fc8456b4 696 }
02774f2d 697 node->aux = NULL;
46000538 698
13cbeaac 699 if (!(cnode = dyn_cast <cgraph_node *> (node))
9e9c3e92 700 || !cnode->clone_of
02774f2d 701 || cnode->clone_of->decl != cnode->decl)
da4b8721 702 {
46000538 703 /* Builtins are not merged via decl merging. It is however
704 possible that tree merging unified the declaration. We
705 do not want duplicate entries in symbol table. */
02774f2d 706 if (cnode && DECL_BUILT_IN (node->decl)
415d1b9a 707 && (cnode2 = cgraph_node::get (node->decl))
da4b8721 708 && cnode2 != cnode)
709 lto_cgraph_replace_node (cnode2, cnode);
abb1a237 710
46000538 711 /* The user defined assembler variables are also not unified by their
712 symbol name (since it is irrelevant), but we need to unify symbol
713 nodes if tree merging occured. */
13cbeaac 714 if ((vnode = dyn_cast <varpool_node *> (node))
02774f2d 715 && DECL_HARD_REGISTER (vnode->decl)
415d1b9a 716 && (node2 = symtab_node::get (vnode->decl))
46000538 717 && node2 != node)
13cbeaac 718 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
46000538 719 vnode);
720
721
abb1a237 722 /* Abstract functions may have duplicated cgraph nodes attached;
723 remove them. */
16d41ae2 724 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
415d1b9a 725 && (cnode2 = cgraph_node::get (node->decl))
abb1a237 726 && cnode2 != cnode)
415d1b9a 727 cnode2->remove ();
46000538 728
8c016392 729 node->decl->decl_with_vis.symtab_node = node;
da4b8721 730 }
fc8456b4 731 }
c9aa6453 732 }
21ce3cc7 733}
fd193bcd 734
7bfefa9d 735/* Given the decl DECL, return the prevailing decl with the same name. */
736
737tree
738lto_symtab_prevailing_decl (tree decl)
739{
452659af 740 symtab_node *ret;
7bfefa9d 741
742 /* Builtins and local symbols are their own prevailing decl. */
33864132 743 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
7bfefa9d 744 return decl;
745
16d41ae2 746 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
747 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
7bfefa9d 748 return decl;
749
871609a8 750 /* Likewise builtins are their own prevailing decl. This preserves
751 non-builtin vs. builtin uses from compile-time. */
752 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
753 return decl;
754
7bfefa9d 755 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
756 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
757
758 /* Walk through the list of candidates and return the one we merged to. */
35ee1c66 759 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
fd193bcd 760 if (!ret)
c9aa6453 761 return decl;
fd193bcd 762
02774f2d 763 return ret->decl;
7bfefa9d 764}