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