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