]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lto/lto-symtab.c
re PR translation/79019 (translatable string typo in cif-code.def:141)
[thirdparty/gcc.git] / gcc / lto / lto-symtab.c
CommitLineData
d7f09764 1/* LTO symbol table.
cbe34bb5 2 Copyright (C) 2009-2017 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"
2adfab87 24#include "target.h"
60393bbc 25#include "function.h"
2fb9a547 26#include "basic-block.h"
c7131fb2 27#include "tree.h"
d7f09764 28#include "gimple.h"
2adfab87 29#include "cgraph.h"
d7f09764 30#include "lto-streamer.h"
5e581212 31#include "ipa-utils.h"
9b2b7279 32#include "builtins.h"
22bea0be 33#include "alias.h"
6b9ac179 34#include "lto-symtab.h"
d7f09764 35
200c8750
RG
36/* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
37 all edges and removing the old node. */
d7f09764 38
200c8750
RG
39static void
40lto_cgraph_replace_node (struct cgraph_node *node,
41 struct cgraph_node *prevailing_node)
d7f09764 42{
200c8750 43 struct cgraph_edge *e, *next;
ecf9b8ae 44 bool compatible_p;
e10aaec0 45
3dafb85c 46 if (symtab->dump_file)
e10aaec0 47 {
3dafb85c 48 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
e10aaec0 49 " for symbol %s\n",
fec39fa6
TS
50 node->name (), node->order,
51 prevailing_node->name (),
67348ccc 52 prevailing_node->order,
77754180 53 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
67348ccc 54 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
e10aaec0
JH
55 }
56
200c8750 57 /* Merge node flags. */
67348ccc 58 if (node->force_output)
d52f5295 59 prevailing_node->mark_force_output ();
5f876ae7
JH
60 if (node->forced_by_abi)
61 prevailing_node->forced_by_abi = true;
67348ccc 62 if (node->address_taken)
d7f09764 63 {
200c8750 64 gcc_assert (!prevailing_node->global.inlined_to);
d52f5295 65 prevailing_node->mark_address_taken ();
200c8750 66 }
88636b62
JH
67 if (node->definition && prevailing_node->definition
68 && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
69 prevailing_node->merged_comdat = true;
d7f09764 70
200c8750 71 /* Redirect all incoming edges. */
ecf9b8ae 72 compatible_p
67348ccc
DM
73 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
74 TREE_TYPE (TREE_TYPE (node->decl)));
200c8750
RG
75 for (e = node->callers; e; e = next)
76 {
77 next = e->next_caller;
3dafb85c 78 e->redirect_callee (prevailing_node);
ecf9b8ae
RG
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)
1a0bf5e1
JH
84 {
85 e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
86 e->call_stmt_cannot_inline_p = 1;
87 }
200c8750 88 }
369451ec 89 /* Redirect incomming references. */
d122681a 90 prevailing_node->clone_referring (node);
d7f09764 91
6a73516d
IE
92 /* Fix instrumentation references. */
93 if (node->instrumented_version)
94 {
95 gcc_assert (node->instrumentation_clone
96 == prevailing_node->instrumentation_clone);
97 node->instrumented_version->instrumented_version = prevailing_node;
98 if (!prevailing_node->instrumented_version)
99 prevailing_node->instrumented_version = node->instrumented_version;
100 /* Need to reset node->instrumented_version to NULL,
101 otherwise node removal code would reset
102 node->instrumented_version->instrumented_version. */
103 node->instrumented_version = NULL;
104 }
105
67348ccc 106 lto_free_function_in_decl_state_for_node (node);
256eefa9 107
67348ccc 108 if (node->decl != prevailing_node->decl)
d52f5295 109 node->release_body ();
4f63dfc6 110
200c8750 111 /* Finally remove the replaced node. */
d52f5295 112 node->remove ();
200c8750
RG
113}
114
2942c502
JH
115/* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
116 all edges and removing the old node. */
117
118static void
2c8326a5
OE
119lto_varpool_replace_node (varpool_node *vnode,
120 varpool_node *prevailing_node)
2942c502 121{
67348ccc
DM
122 gcc_assert (!vnode->definition || prevailing_node->definition);
123 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
2942c502 124
d122681a 125 prevailing_node->clone_referring (vnode);
5f876ae7
JH
126 if (vnode->force_output)
127 prevailing_node->force_output = true;
128 if (vnode->forced_by_abi)
129 prevailing_node->forced_by_abi = true;
369451ec 130
b34fd25c 131 /* Be sure we can garbage collect the initializer. */
67348ccc
DM
132 if (DECL_INITIAL (vnode->decl)
133 && vnode->decl != prevailing_node->decl)
134 DECL_INITIAL (vnode->decl) = error_mark_node;
714c800f 135
56b1f114
JH
136 /* Check and report ODR violations on virtual tables. */
137 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
138 compare_virtual_tables (prevailing_node, vnode);
139
714c800f
JH
140 if (vnode->tls_model != prevailing_node->tls_model)
141 {
040968a8
JH
142 bool error = false;
143
144 /* Non-TLS and TLS never mix together. Also emulated model is not
145 compatible with anything else. */
146 if (prevailing_node->tls_model == TLS_MODEL_NONE
147 || prevailing_node->tls_model == TLS_MODEL_EMULATED
148 || vnode->tls_model == TLS_MODEL_NONE
149 || vnode->tls_model == TLS_MODEL_EMULATED)
150 error = true;
151 /* Linked is silently supporting transitions
152 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
153 Do the same transitions and error out on others. */
154 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
155 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
156 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
157 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
158 prevailing_node->tls_model = vnode->tls_model;
159 else if ((vnode->tls_model == TLS_MODEL_REAL
160 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
161 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
162 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
163 ;
164 else if (prevailing_node->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_INITIAL_EXEC
168 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
169 ;
170 else
171 error = true;
172 if (error)
173 {
174 error_at (DECL_SOURCE_LOCATION (vnode->decl),
175 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
176 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
177 "previously defined here as %s",
178 tls_model_names [prevailing_node->tls_model]);
179 }
714c800f 180 }
2942c502 181 /* Finally remove the replaced node. */
d52f5295 182 vnode->remove ();
2942c502
JH
183}
184
259d29e3
JH
185/* Return non-zero if we want to output waring about T1 and T2.
186 Return value is a bitmask of reasons of violation:
22bea0be
JH
187 Bit 0 indicates that types are not compatible.
188 Bit 1 indicates that types are not compatible because of C++ ODR rule.
189 If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
190 Bit 2 indicates that types are not ODR compatible
191
192 The interoperability rules are language specific. At present we do only
193 full checking for C++ ODR rule and for other languages we do basic check
194 that data structures are of same size and TBAA compatible. Our TBAA
195 implementation should be coarse enough so all valid type transitions
196 across different languages are allowed.
197
198 In partiucular we thus allow almost arbitrary type changes with
199 -fno-strict-aliasing which may be tough of as a feature rather than bug
200 as it allows to implement dodgy tricks in the language runtimes.
201
202 Naturally this code can be strenghtened significantly if we could track
203 down the language of origin. */
200c8750 204
259d29e3 205static int
22bea0be
JH
206warn_type_compatibility_p (tree prevailing_type, tree type,
207 bool common_or_extern)
200c8750 208{
259d29e3 209 int lev = 0;
22bea0be
JH
210 bool odr_p = odr_or_derived_type_p (prevailing_type)
211 && odr_or_derived_type_p (type);
feb391fc 212
feb391fc
JH
213 if (prevailing_type == type)
214 return 0;
215
259d29e3
JH
216 /* C++ provide a robust way to check for type compatibility via the ODR
217 rule. */
feb391fc 218 if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
22bea0be 219 lev |= 2;
259d29e3
JH
220
221 /* Function types needs special care, because types_compatible_p never
222 thinks prototype is compatible to non-prototype. */
22bea0be 223 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
fd7588bc 224 {
22bea0be
JH
225 if (TREE_CODE (type) != TREE_CODE (prevailing_type))
226 lev |= 1;
259d29e3 227 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
22bea0be
JH
228 TREE_TYPE (type), false);
229 if (TREE_CODE (type) == METHOD_TYPE
230 && TREE_CODE (prevailing_type) == METHOD_TYPE)
259d29e3 231 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
22bea0be 232 TYPE_METHOD_BASETYPE (type), false);
259d29e3
JH
233 if (prototype_p (prevailing_type) && prototype_p (type)
234 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
235 {
236 tree parm1, parm2;
237 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
238 parm2 = TYPE_ARG_TYPES (type);
239 parm1 && parm2;
feb391fc
JH
240 parm1 = TREE_CHAIN (parm1),
241 parm2 = TREE_CHAIN (parm2))
259d29e3 242 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
22bea0be 243 TREE_VALUE (parm2), false);
259d29e3 244 if (parm1 || parm2)
22bea0be 245 lev |= odr_p ? 3 : 1;
259d29e3
JH
246 }
247 if (comp_type_attributes (prevailing_type, type) == 0)
22bea0be 248 lev |= 1;
259d29e3 249 return lev;
fd7588bc 250 }
22bea0be
JH
251
252 /* Get complete type. */
259d29e3
JH
253 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
254 type = TYPE_MAIN_VARIANT (type);
e575382e 255
22bea0be
JH
256 /* We can not use types_compatible_p because we permit some changes
257 across types. For example unsigned size_t and "signed size_t" may be
258 compatible when merging C and Fortran types. */
259 if (COMPLETE_TYPE_P (prevailing_type)
260 && COMPLETE_TYPE_P (type)
261 /* While global declarations are never variadic, we can recurse here
262 for function parameter types. */
263 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
264 && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
265 && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
e575382e 266 {
22bea0be
JH
267 /* As a special case do not warn about merging
268 int a[];
269 and
270 int a[]={1,2,3};
271 here the first declaration is COMMON or EXTERN
272 and sizeof(a) == sizeof (int). */
273 if (!common_or_extern
274 || TREE_CODE (type) != ARRAY_TYPE
275 || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
276 lev |= 1;
277 }
e575382e 278
22bea0be
JH
279 /* Verify TBAA compatibility. Take care of alias set 0 and the fact that
280 we make ptr_type_node to TBAA compatible with every other type. */
281 if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
282 {
283 alias_set_type set1 = get_alias_set (type);
284 alias_set_type set2 = get_alias_set (prevailing_type);
285
286 if (set1 && set2 && set1 != set2
287 && (!POINTER_TYPE_P (type) || !POINTER_TYPE_P (prevailing_type)
288 || (set1 != TYPE_ALIAS_SET (ptr_type_node)
289 && set2 != TYPE_ALIAS_SET (ptr_type_node))))
290 lev |= 5;
e575382e 291 }
d7f09764 292
259d29e3
JH
293 return lev;
294}
295
296/* Merge two variable or function symbol table entries PREVAILING and ENTRY.
297 Return false if the symbols are not fully compatible and a diagnostic
298 should be emitted. */
299
300static bool
301lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
302{
303 tree prevailing_decl = prevailing->decl;
304 tree decl = entry->decl;
305
306 if (prevailing_decl == decl)
307 return true;
308
13092f61
RB
309 if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
310 return false;
311
259d29e3
JH
312 /* Merge decl state in both directions, we may still end up using
313 the new decl. */
314 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
315 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
316
317 /* The linker may ask us to combine two incompatible symbols.
318 Detect this case and notify the caller of required diagnostics. */
319
320 if (TREE_CODE (decl) == FUNCTION_DECL)
321 {
f196d062
RB
322 /* Merge decl state in both directions, we may still end up using
323 the new decl. */
324 DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
325 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
326
259d29e3 327 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
22bea0be
JH
328 TREE_TYPE (decl),
329 DECL_COMMON (decl)
330 || DECL_EXTERNAL (decl)))
259d29e3
JH
331 return false;
332
333 return true;
334 }
335
336 if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
22bea0be
JH
337 TREE_TYPE (decl),
338 DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
259d29e3
JH
339 return false;
340
fd7588bc
RG
341 /* There is no point in comparing too many details of the decls here.
342 The type compatibility checks or the completing of types has properly
343 dealt with most issues. */
d7f09764 344
fd7588bc
RG
345 /* The following should all not invoke fatal errors as in non-LTO
346 mode the linker wouldn't complain either. Just emit warnings. */
d7f09764 347
fd7588bc 348 /* Report a warning if user-specified alignments do not match. */
200c8750
RG
349 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
350 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
351 return false;
d7f09764 352
22bea0be
JH
353 if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
354 && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl))
355 /* As a special case do not warn about merging
356 int a[];
357 and
358 int a[]={1,2,3};
359 here the first declaration is COMMON
360 and sizeof(a) == sizeof (int). */
361 && ((!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
362 || TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE
363 || TYPE_SIZE (TREE_TYPE (decl))
364 != TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))))
365 return false;
366
d7f09764
DN
367 return true;
368}
369
200c8750
RG
370/* Return true if the symtab entry E can be replaced by another symtab
371 entry. */
d7f09764 372
200c8750 373static bool
5e20cdc9 374lto_symtab_resolve_replaceable_p (symtab_node *e)
d7f09764 375{
67348ccc
DM
376 if (DECL_EXTERNAL (e->decl)
377 || DECL_COMDAT (e->decl)
378 || DECL_ONE_ONLY (e->decl)
379 || DECL_WEAK (e->decl))
200c8750 380 return true;
d7f09764 381
67348ccc
DM
382 if (TREE_CODE (e->decl) == VAR_DECL)
383 return (DECL_COMMON (e->decl)
384 || (!flag_no_common && !DECL_INITIAL (e->decl)));
d7f09764 385
200c8750 386 return false;
1a735925 387}
d7f09764 388
e86074fd 389/* Return true, if the symbol E should be resolved by lto-symtab.
2d6e4603
JH
390 Those are all external symbols and all real symbols that are not static (we
391 handle renaming of static later in partitioning). */
e86074fd
JH
392
393static bool
5e20cdc9 394lto_symtab_symbol_p (symtab_node *e)
e86074fd 395{
67348ccc 396 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
2d6e4603 397 return false;
d52f5295 398 return e->real_symbol_p ();
e86074fd
JH
399}
400
200c8750 401/* Return true if the symtab entry E can be the prevailing one. */
d7f09764 402
200c8750 403static bool
5e20cdc9 404lto_symtab_resolve_can_prevail_p (symtab_node *e)
1a735925 405{
e86074fd 406 if (!lto_symtab_symbol_p (e))
b5493fb2
JH
407 return false;
408
5d96330a
RG
409 /* The C++ frontend ends up neither setting TREE_STATIC nor
410 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
411 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
67348ccc 412 if (DECL_EXTERNAL (e->decl))
200c8750 413 return false;
d7f09764 414
67348ccc 415 return e->definition;
d7f09764
DN
416}
417
1a735925
RG
418/* Resolve the symbol with the candidates in the chain *SLOT and store
419 their resolutions. */
d7f09764 420
5e20cdc9
DM
421static symtab_node *
422lto_symtab_resolve_symbols (symtab_node *first)
1a735925 423{
5e20cdc9
DM
424 symtab_node *e;
425 symtab_node *prevailing = NULL;
d7f09764 426
a5ac2cdf 427 /* Always set e->node so that edges are updated to reflect decl merging. */
67348ccc 428 for (e = first; e; e = e->next_sharing_asm_name)
e86074fd 429 if (lto_symtab_symbol_p (e)
67348ccc
DM
430 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
431 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
432 || e->resolution == LDPR_PREVAILING_DEF))
e649d346
RG
433 {
434 prevailing = e;
435 break;
436 }
a5ac2cdf 437
a5ac2cdf 438 /* If the chain is already resolved there is nothing else to do. */
ed0d2da0 439 if (prevailing)
e649d346
RG
440 {
441 /* Assert it's the only one. */
67348ccc 442 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
e86074fd 443 if (lto_symtab_symbol_p (e)
67348ccc
DM
444 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
445 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
446 || e->resolution == LDPR_PREVAILING_DEF))
40fecdd6 447 fatal_error (input_location, "multiple prevailing defs for %qE",
67348ccc 448 DECL_NAME (prevailing->decl));
e649d346
RG
449 return prevailing;
450 }
1a735925 451
200c8750
RG
452 /* Find the single non-replaceable prevailing symbol and
453 diagnose ODR violations. */
67348ccc 454 for (e = first; e; e = e->next_sharing_asm_name)
1a735925 455 {
200c8750 456 if (!lto_symtab_resolve_can_prevail_p (e))
e649d346 457 continue;
200c8750 458
e649d346 459 /* If we have a non-replaceable definition it prevails. */
200c8750
RG
460 if (!lto_symtab_resolve_replaceable_p (e))
461 {
462 if (prevailing)
463 {
67348ccc
DM
464 error_at (DECL_SOURCE_LOCATION (e->decl),
465 "%qD has already been defined", e->decl);
466 inform (DECL_SOURCE_LOCATION (prevailing->decl),
200c8750
RG
467 "previously defined here");
468 }
469 prevailing = e;
470 }
471 }
472 if (prevailing)
e649d346 473 return prevailing;
200c8750
RG
474
475 /* Do a second round choosing one from the replaceable prevailing decls. */
67348ccc 476 for (e = first; e; e = e->next_sharing_asm_name)
200c8750 477 {
e86074fd 478 if (!lto_symtab_resolve_can_prevail_p (e))
200c8750
RG
479 continue;
480
481 /* Choose the first function that can prevail as prevailing. */
67348ccc 482 if (TREE_CODE (e->decl) == FUNCTION_DECL)
1a735925 483 {
200c8750
RG
484 prevailing = e;
485 break;
1a735925 486 }
200c8750
RG
487
488 /* From variables that can prevail choose the largest one. */
489 if (!prevailing
67348ccc
DM
490 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
491 DECL_SIZE (e->decl))
6649df51
JH
492 /* When variables are equivalent try to chose one that has useful
493 DECL_INITIAL. This makes sense for keyed vtables that are
494 DECL_EXTERNAL but initialized. In units that do not need them
495 we replace the initializer by error_mark_node to conserve
496 memory.
497
498 We know that the vtable is keyed outside the LTO unit - otherwise
499 the keyed instance would prevail. We still can preserve useful
500 info in the initializer. */
67348ccc
DM
501 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
502 && (DECL_INITIAL (e->decl)
503 && DECL_INITIAL (e->decl) != error_mark_node)
504 && (!DECL_INITIAL (prevailing->decl)
505 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
200c8750 506 prevailing = e;
1a735925 507 }
200c8750 508
e649d346 509 return prevailing;
1a735925
RG
510}
511
6b9ac179
JH
512/* Decide if it is OK to merge DECL into PREVAILING.
513 Because we wrap most of uses of declarations in MEM_REF, we can tolerate
514 some differences but other code may inspect directly the DECL. */
515
516static bool
517lto_symtab_merge_p (tree prevailing, tree decl)
518{
519 if (TREE_CODE (prevailing) != TREE_CODE (decl))
958a627f
JH
520 {
521 if (symtab->dump_file)
522 fprintf (symtab->dump_file, "Not merging decls; "
523 "TREE_CODE mismatch\n");
524 return false;
525 }
3fddb2ef
JH
526 gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
527
6b9ac179
JH
528 if (TREE_CODE (prevailing) == FUNCTION_DECL)
529 {
530 if (DECL_BUILT_IN (prevailing) != DECL_BUILT_IN (decl))
958a627f
JH
531 {
532 if (symtab->dump_file)
533 fprintf (symtab->dump_file, "Not merging decls; "
534 "DECL_BUILT_IN mismatch\n");
535 return false;
536 }
6b9ac179
JH
537 if (DECL_BUILT_IN (prevailing)
538 && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
539 || DECL_FUNCTION_CODE (prevailing) != DECL_FUNCTION_CODE (decl)))
958a627f
JH
540 {
541 if (symtab->dump_file)
542 fprintf (symtab->dump_file, "Not merging decls; "
543 "DECL_BUILT_IN_CLASS or CODE mismatch\n");
544 return false;
545 }
546 }
547 if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
548 {
549 tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
550 tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
551 if ((prev_attr == NULL) != (attr == NULL)
552 || (prev_attr
553 && TREE_VALUE (TREE_VALUE (prev_attr))
554 != TREE_VALUE (TREE_VALUE (attr))))
555 {
556 if (symtab->dump_file)
557 fprintf (symtab->dump_file, "Not merging decls; "
558 "error attribute mismatch\n");
559 return false;
560 }
561
562 prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
563 attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
564 if ((prev_attr == NULL) != (attr == NULL)
565 || (prev_attr
566 && TREE_VALUE (TREE_VALUE (prev_attr))
567 != TREE_VALUE (TREE_VALUE (attr))))
568 {
569 if (symtab->dump_file)
570 fprintf (symtab->dump_file, "Not merging decls; "
571 "warning attribute mismatch\n");
572 return false;
573 }
6b9ac179 574 }
6b9ac179
JH
575 return true;
576}
577
200c8750 578/* Merge all decls in the symbol table chain to the prevailing decl and
97ce54b9
RG
579 issue diagnostics about type mismatches. If DIAGNOSED_P is true
580 do not issue further diagnostics.*/
1a735925
RG
581
582static void
5e20cdc9 583lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
1a735925 584{
5e20cdc9
DM
585 symtab_node *prevailing;
586 symtab_node *e;
6e1aa848 587 vec<tree> mismatches = vNULL;
200c8750
RG
588 unsigned i;
589 tree decl;
22bea0be 590 bool tbaa_p = false;
1a735925
RG
591
592 /* Nothing to do for a single entry. */
b5493fb2 593 prevailing = first;
67348ccc 594 if (!prevailing->next_sharing_asm_name)
1a735925
RG
595 return;
596
200c8750 597 /* Try to merge each entry with the prevailing one. */
6b9ac179
JH
598 symtab_node *last_prevailing = prevailing, *next;
599 for (e = prevailing->next_sharing_asm_name; e; e = next)
600 {
601 next = e->next_sharing_asm_name;
602
603 /* Skip non-LTO symbols and symbols whose declaration we already
604 visited. */
605 if (lto_symtab_prevailing_decl (e->decl) != e->decl
606 || !lto_symtab_symbol_p (e)
607 || e->decl == prevailing->decl)
608 continue;
609
610 if (!lto_symtab_merge (prevailing, e)
611 && !diagnosed_p
612 && !DECL_ARTIFICIAL (e->decl))
613 mismatches.safe_push (e->decl);
614
615 symtab_node *this_prevailing;
616 for (this_prevailing = prevailing; ;
617 this_prevailing = this_prevailing->next_sharing_asm_name)
618 {
378004b9
JH
619 if (this_prevailing->decl != e->decl
620 && lto_symtab_merge_p (this_prevailing->decl, e->decl))
6b9ac179
JH
621 break;
622 if (this_prevailing == last_prevailing)
623 {
624 this_prevailing = NULL;
625 break;
626 }
627 }
628
629 if (this_prevailing)
630 lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
631 /* Maintain LRU list: relink the new prevaililng symbol
632 just after previaling node in the chain and update last_prevailing.
633 Since the number of possible declarations of a given symbol is
634 small, this should be faster than building a hash. */
635 else if (e == prevailing->next_sharing_asm_name)
636 last_prevailing = e;
637 else
638 {
639 if (e->next_sharing_asm_name)
640 e->next_sharing_asm_name->previous_sharing_asm_name
641 = e->previous_sharing_asm_name;
642 e->previous_sharing_asm_name->next_sharing_asm_name
643 = e->next_sharing_asm_name;
644 e->previous_sharing_asm_name = prevailing;
645 e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
646 prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
647 prevailing->next_sharing_asm_name = e;
648 if (last_prevailing == prevailing)
649 last_prevailing = e;
650 }
651 }
9771b263 652 if (mismatches.is_empty ())
200c8750 653 return;
1a735925 654
200c8750 655 /* Diagnose all mismatched re-declarations. */
9771b263 656 FOR_EACH_VEC_ELT (mismatches, i, decl)
1a735925 657 {
6c8bf066
GJL
658 /* Do not diagnose two built-in declarations, there is no useful
659 location in that case. It also happens for AVR if two built-ins
660 use the same asm name because their libgcc assembler code is the
661 same, see PR78562. */
662 if (DECL_IS_BUILTIN (prevailing->decl)
663 && DECL_IS_BUILTIN (decl))
664 continue;
665
259d29e3 666 int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
22bea0be
JH
667 TREE_TYPE (decl),
668 DECL_COMDAT (decl));
259d29e3
JH
669 if (level)
670 {
671 bool diag = false;
22bea0be 672 if (level & 2)
259d29e3
JH
673 diag = warning_at (DECL_SOURCE_LOCATION (decl),
674 OPT_Wodr,
675 "%qD violates the C++ One Definition Rule ",
676 decl);
677 if (!diag && (level & 1))
678 diag = warning_at (DECL_SOURCE_LOCATION (decl),
679 OPT_Wlto_type_mismatch,
680 "type of %qD does not match original "
681 "declaration", decl);
682 if (diag)
22bea0be
JH
683 {
684 warn_types_mismatch (TREE_TYPE (prevailing->decl),
685 TREE_TYPE (decl),
686 DECL_SOURCE_LOCATION (prevailing->decl),
687 DECL_SOURCE_LOCATION (decl));
688 if ((level & 4)
689 && !TREE_READONLY (prevailing->decl))
690 tbaa_p = true;
691 }
259d29e3
JH
692 diagnosed_p |= diag;
693 }
67348ccc 694 else if ((DECL_USER_ALIGN (prevailing->decl)
b5493fb2 695 && DECL_USER_ALIGN (decl))
67348ccc 696 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
1a735925 697 {
259d29e3
JH
698 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
699 OPT_Wlto_type_mismatch,
200c8750
RG
700 "alignment of %qD is bigger than "
701 "original declaration", decl);
1a735925 702 }
22bea0be
JH
703 else
704 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
705 OPT_Wlto_type_mismatch,
706 "size of %qD differ from the size of "
707 "original declaration", decl);
1a735925 708 }
200c8750 709 if (diagnosed_p)
67348ccc 710 inform (DECL_SOURCE_LOCATION (prevailing->decl),
259d29e3 711 "%qD was previously declared here", prevailing->decl);
22bea0be
JH
712 if (tbaa_p)
713 inform (DECL_SOURCE_LOCATION (prevailing->decl),
714 "code may be misoptimized unless "
715 "-fno-strict-aliasing is used");
1a735925 716
9771b263 717 mismatches.release ();
1a735925
RG
718}
719
720/* Helper to process the decl chain for the symbol table entry *SLOT. */
721
b5493fb2 722static void
5e20cdc9 723lto_symtab_merge_decls_1 (symtab_node *first)
d7f09764 724{
5e20cdc9
DM
725 symtab_node *e;
726 symtab_node *prevailing;
200c8750 727 bool diagnosed_p = false;
1a735925 728
3dafb85c 729 if (symtab->dump_file)
b5493fb2 730 {
3dafb85c 731 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
fec39fa6 732 first->asm_name ());
67348ccc
DM
733 for (e = first; e; e = e->next_sharing_asm_name)
734 if (TREE_PUBLIC (e->decl))
3dafb85c 735 e->dump (symtab->dump_file);
b5493fb2
JH
736 }
737
200c8750 738 /* Compute the symbol resolutions. This is a no-op when using the
b5493fb2 739 linker plugin and resolution was decided by the linker. */
e649d346 740 prevailing = lto_symtab_resolve_symbols (first);
200c8750
RG
741
742 /* If there's not a prevailing symbol yet it's an external reference.
743 Happens a lot during ltrans. Choose the first symbol with a
744 cgraph or a varpool node. */
745 if (!prevailing)
746 {
b9559136
JH
747 for (prevailing = first;
748 prevailing; prevailing = prevailing->next_sharing_asm_name)
749 if (lto_symtab_symbol_p (prevailing))
750 break;
751 if (!prevailing)
752 return;
9e0546ef
JH
753 /* For variables chose with a priority variant with vnode
754 attached (i.e. from unit where external declaration of
755 variable is actually used).
756 When there are multiple variants, chose one with size.
757 This is needed for C++ typeinfos, for example in
758 lto/20081204-1 there are typeifos in both units, just
759 one of them do have size. */
67348ccc 760 if (TREE_CODE (prevailing->decl) == VAR_DECL)
9e0546ef 761 {
67348ccc
DM
762 for (e = prevailing->next_sharing_asm_name;
763 e; e = e->next_sharing_asm_name)
764 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
765 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
e86074fd 766 && lto_symtab_symbol_p (e))
9e0546ef
JH
767 prevailing = e;
768 }
378004b9 769 /* For functions prefer the non-builtin if one is available. */
67348ccc 770 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
f09b77ca 771 {
67348ccc
DM
772 for (e = first; e; e = e->next_sharing_asm_name)
773 if (TREE_CODE (e->decl) == FUNCTION_DECL
774 && !DECL_BUILT_IN (e->decl)
e86074fd 775 && lto_symtab_symbol_p (e))
f09b77ca
RB
776 {
777 prevailing = e;
778 break;
779 }
780 }
200c8750 781 }
1a735925 782
3dafb85c 783 symtab->symtab_prevail_in_asm_name_hash (prevailing);
1a735925 784
200c8750 785 /* Diagnose mismatched objects. */
67348ccc
DM
786 for (e = prevailing->next_sharing_asm_name;
787 e; e = e->next_sharing_asm_name)
200c8750 788 {
67348ccc
DM
789 if (TREE_CODE (prevailing->decl)
790 == TREE_CODE (e->decl))
200c8750 791 continue;
e86074fd
JH
792 if (!lto_symtab_symbol_p (e))
793 continue;
1a735925 794
67348ccc 795 switch (TREE_CODE (prevailing->decl))
200c8750
RG
796 {
797 case VAR_DECL:
67348ccc
DM
798 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
799 error_at (DECL_SOURCE_LOCATION (e->decl),
b5493fb2 800 "variable %qD redeclared as function",
67348ccc 801 prevailing->decl);
200c8750
RG
802 break;
803
804 case FUNCTION_DECL:
67348ccc
DM
805 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
806 error_at (DECL_SOURCE_LOCATION (e->decl),
b5493fb2 807 "function %qD redeclared as variable",
67348ccc 808 prevailing->decl);
200c8750 809 break;
1a735925 810
200c8750
RG
811 default:
812 gcc_unreachable ();
813 }
814
815 diagnosed_p = true;
816 }
817 if (diagnosed_p)
67348ccc 818 inform (DECL_SOURCE_LOCATION (prevailing->decl),
200c8750
RG
819 "previously declared here");
820
200c8750
RG
821 /* Merge the chain to the single prevailing decl and diagnose
822 mismatches. */
0237949a 823 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
b5493fb2 824
3dafb85c 825 if (symtab->dump_file)
b5493fb2 826 {
3dafb85c 827 fprintf (symtab->dump_file, "After resolution:\n");
67348ccc 828 for (e = prevailing; e; e = e->next_sharing_asm_name)
3dafb85c 829 e->dump (symtab->dump_file);
b5493fb2 830 }
d7f09764
DN
831}
832
1a735925 833/* Resolve and merge all symbol table chains to a prevailing decl. */
d7f09764
DN
834
835void
1a735925 836lto_symtab_merge_decls (void)
d7f09764 837{
5e20cdc9 838 symtab_node *node;
b5493fb2 839
b5493fb2 840 /* Populate assembler name hash. */
3dafb85c 841 symtab->symtab_initialize_asm_name_hash ();
b5493fb2
JH
842
843 FOR_EACH_SYMBOL (node)
67348ccc
DM
844 if (!node->previous_sharing_asm_name
845 && node->next_sharing_asm_name)
8951f345 846 lto_symtab_merge_decls_1 (node);
d7f09764
DN
847}
848
2c928155
RG
849/* Helper to process the decl chain for the symbol table entry *SLOT. */
850
b5493fb2 851static void
5e20cdc9 852lto_symtab_merge_symbols_1 (symtab_node *prevailing)
2c928155 853{
5e20cdc9
DM
854 symtab_node *e;
855 symtab_node *next;
2c928155 856
6b9ac179
JH
857 prevailing->decl->decl_with_vis.symtab_node = prevailing;
858
2c928155 859 /* Replace the cgraph node of each entry with the prevailing one. */
67348ccc 860 for (e = prevailing->next_sharing_asm_name; e;
b5493fb2 861 e = next)
2c928155 862 {
67348ccc 863 next = e->next_sharing_asm_name;
2c928155 864
e86074fd 865 if (!lto_symtab_symbol_p (e))
b5493fb2 866 continue;
7de90a6c 867 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
6b9ac179
JH
868 symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
869
870 /* No matter how we are going to deal with resolution, we will ultimately
871 use prevailing definition. */
872 if (ce)
873 ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
874 dyn_cast<cgraph_node *> (e));
875
876 /* If we decided to replace the node by TO, do it. */
877 if (e != to)
878 {
879 if (ce)
880 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
881 else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
882 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
883 }
884 /* Watch out for duplicated symbols for a given declaration. */
885 else if (!e->transparent_alias
886 || !e->definition || e->get_alias_target () != to)
887 {
888 /* We got a new declaration we do not want to merge. In this case
889 get rid of the existing definition and create a transparent
890 alias. */
891 if (ce)
892 {
893 lto_free_function_in_decl_state_for_node (ce);
894 if (!ce->weakref)
895 ce->release_body ();
896 ce->reset ();
897 symtab->call_cgraph_removal_hooks (ce);
898 }
899 else
900 {
901 DECL_INITIAL (e->decl) = error_mark_node;
3fddb2ef
JH
902 if (e->lto_file_data)
903 {
904 lto_free_function_in_decl_state_for_node (e);
905 e->lto_file_data = NULL;
906 }
6b9ac179
JH
907 symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
908 }
909 e->remove_all_references ();
910 e->analyzed = e->body_removed = false;
911 e->resolve_alias (prevailing, true);
912 gcc_assert (e != prevailing);
913 }
b5493fb2 914 }
2c928155 915
b5493fb2 916 return;
2c928155
RG
917}
918
919/* Merge cgraph nodes according to the symbol merging done by
920 lto_symtab_merge_decls. */
921
922void
40a7fe1e 923lto_symtab_merge_symbols (void)
2c928155 924{
5e20cdc9 925 symtab_node *node;
b5493fb2 926
b5493fb2 927 if (!flag_ltrans)
b5493fb2 928 {
3dafb85c 929 symtab->symtab_initialize_asm_name_hash ();
40a7fe1e 930
bbf9ad07
JH
931 /* Do the actual merging.
932 At this point we invalidate hash translating decls into symtab nodes
933 because after removing one of duplicate decls the hash is not correcly
934 updated to the ohter dupliate. */
40a7fe1e
JH
935 FOR_EACH_SYMBOL (node)
936 if (lto_symtab_symbol_p (node)
67348ccc
DM
937 && node->next_sharing_asm_name
938 && !node->previous_sharing_asm_name)
40a7fe1e
JH
939 lto_symtab_merge_symbols_1 (node);
940
bbf9ad07
JH
941 /* Resolve weakref aliases whose target are now in the compilation unit.
942 also re-populate the hash translating decls into symtab nodes*/
40a7fe1e 943 FOR_EACH_SYMBOL (node)
e01c7cca 944 {
ca0f62a8 945 cgraph_node *cnode, *cnode2;
8e4c9a10 946 varpool_node *vnode;
5e20cdc9 947 symtab_node *node2;
8e4c9a10 948
67348ccc 949 if (!node->analyzed && node->alias_target)
e01c7cca 950 {
3dafb85c 951 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
67348ccc 952 gcc_assert (node->weakref);
40a7fe1e 953 if (tgt)
6b9ac179 954 node->resolve_alias (tgt, true);
e01c7cca 955 }
8e4c9a10 956
7de90a6c 957 if (!(cnode = dyn_cast <cgraph_node *> (node))
bbf9ad07 958 || !cnode->clone_of
67348ccc 959 || cnode->clone_of->decl != cnode->decl)
ca0f62a8 960 {
8e4c9a10
JH
961 /* Builtins are not merged via decl merging. It is however
962 possible that tree merging unified the declaration. We
963 do not want duplicate entries in symbol table. */
67348ccc 964 if (cnode && DECL_BUILT_IN (node->decl)
d52f5295 965 && (cnode2 = cgraph_node::get (node->decl))
ca0f62a8
JH
966 && cnode2 != cnode)
967 lto_cgraph_replace_node (cnode2, cnode);
c0c123ef 968
8e4c9a10
JH
969 /* The user defined assembler variables are also not unified by their
970 symbol name (since it is irrelevant), but we need to unify symbol
bd2c6270 971 nodes if tree merging occurred. */
7de90a6c 972 if ((vnode = dyn_cast <varpool_node *> (node))
67348ccc 973 && DECL_HARD_REGISTER (vnode->decl)
d52f5295 974 && (node2 = symtab_node::get (vnode->decl))
8e4c9a10 975 && node2 != node)
7de90a6c 976 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
8e4c9a10
JH
977 vnode);
978
979
c0c123ef
JH
980 /* Abstract functions may have duplicated cgraph nodes attached;
981 remove them. */
00de328a 982 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
d52f5295 983 && (cnode2 = cgraph_node::get (node->decl))
c0c123ef 984 && cnode2 != cnode)
d52f5295 985 cnode2->remove ();
8e4c9a10 986
aede2c10 987 node->decl->decl_with_vis.symtab_node = node;
ca0f62a8 988 }
e01c7cca 989 }
b5493fb2 990 }
2c928155 991}
3fddb2ef
JH
992
993/* Virtual tables may matter for code generation even if they are not
994 directly refernced by the code because they may be used for devirtualizaiton.
995 For this reason it is important to merge even virtual tables that have no
996 associated symbol table entries. Without doing so we lose optimization
997 oppurtunities by losing track of the vtable constructor.
998 FIXME: we probably ought to introduce explicit symbol table entries for
999 those before streaming. */
1000
1001tree
1002lto_symtab_prevailing_virtual_decl (tree decl)
1003{
e49d321f
JH
1004 if (DECL_ABSTRACT_P (decl))
1005 return decl;
3fddb2ef
JH
1006 gcc_checking_assert (!type_in_anonymous_namespace_p (DECL_CONTEXT (decl))
1007 && DECL_ASSEMBLER_NAME_SET_P (decl));
1008
1009 symtab_node *n = symtab_node::get_for_asmname
1010 (DECL_ASSEMBLER_NAME (decl));
1011 while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1012 || !DECL_VIRTUAL_P (n->decl)))
1013 n = n->next_sharing_asm_name;
1014 if (n)
1015 {
3c4e91c9
RB
1016 /* Merge decl state in both directions, we may still end up using
1017 the other decl. */
1018 TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1019 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1020
1021 if (TREE_CODE (decl) == FUNCTION_DECL)
1022 {
1023 /* Merge decl state in both directions, we may still end up using
1024 the other decl. */
1025 DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1026 DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1027 }
3fddb2ef
JH
1028 lto_symtab_prevail_decl (n->decl, decl);
1029 decl = n->decl;
1030 }
1031 else
1032 symtab_node::get_create (decl);
1033
1034 return decl;
1035}