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