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