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