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