]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto/lto-symtab.c
tree-core.h: Include symtab.h.
[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 "tm.h"
27 #include "function.h"
28 #include "predict.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "hard-reg-set.h"
33 #include "options.h"
34 #include "fold-const.h"
35 #include "internal-fn.h"
36 #include "cgraph.h"
37 #include "lto-streamer.h"
38 #include "ipa-utils.h"
39 #include "alloc-pool.h"
40 #include "symbol-summary.h"
41 #include "ipa-prop.h"
42 #include "ipa-inline.h"
43 #include "builtins.h"
44 #include "print-tree.h"
45
46 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
47 all edges and removing the old node. */
48
49 static void
50 lto_cgraph_replace_node (struct cgraph_node *node,
51 struct cgraph_node *prevailing_node)
52 {
53 struct cgraph_edge *e, *next;
54 bool compatible_p;
55
56 if (symtab->dump_file)
57 {
58 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
59 " for symbol %s\n",
60 node->name (), node->order,
61 prevailing_node->name (),
62 prevailing_node->order,
63 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
64 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
65 }
66
67 /* Merge node flags. */
68 if (node->force_output)
69 prevailing_node->mark_force_output ();
70 if (node->forced_by_abi)
71 prevailing_node->forced_by_abi = true;
72 if (node->address_taken)
73 {
74 gcc_assert (!prevailing_node->global.inlined_to);
75 prevailing_node->mark_address_taken ();
76 }
77 if (node->definition && prevailing_node->definition)
78 prevailing_node->merged = true;
79
80 /* Redirect all incoming edges. */
81 compatible_p
82 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
83 TREE_TYPE (TREE_TYPE (node->decl)));
84 for (e = node->callers; e; e = next)
85 {
86 next = e->next_caller;
87 e->redirect_callee (prevailing_node);
88 /* If there is a mismatch between the supposed callee return type and
89 the real one do not attempt to inline this function.
90 ??? We really need a way to match function signatures for ABI
91 compatibility and perform related promotions at inlining time. */
92 if (!compatible_p)
93 e->call_stmt_cannot_inline_p = 1;
94 }
95 /* Redirect incomming references. */
96 prevailing_node->clone_referring (node);
97
98 /* Fix instrumentation references. */
99 if (node->instrumented_version)
100 {
101 gcc_assert (node->instrumentation_clone
102 == prevailing_node->instrumentation_clone);
103 node->instrumented_version->instrumented_version = prevailing_node;
104 if (!prevailing_node->instrumented_version)
105 prevailing_node->instrumented_version = node->instrumented_version;
106 /* Need to reset node->instrumented_version to NULL,
107 otherwise node removal code would reset
108 node->instrumented_version->instrumented_version. */
109 node->instrumented_version = NULL;
110 }
111
112 ipa_merge_profiles (prevailing_node, node);
113 lto_free_function_in_decl_state_for_node (node);
114
115 if (node->decl != prevailing_node->decl)
116 node->release_body ();
117
118 /* Finally remove the replaced node. */
119 node->remove ();
120 }
121
122 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
123 all edges and removing the old node. */
124
125 static void
126 lto_varpool_replace_node (varpool_node *vnode,
127 varpool_node *prevailing_node)
128 {
129 gcc_assert (!vnode->definition || prevailing_node->definition);
130 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
131
132 prevailing_node->clone_referring (vnode);
133 if (vnode->force_output)
134 prevailing_node->force_output = true;
135 if (vnode->forced_by_abi)
136 prevailing_node->forced_by_abi = true;
137
138 /* Be sure we can garbage collect the initializer. */
139 if (DECL_INITIAL (vnode->decl)
140 && vnode->decl != prevailing_node->decl)
141 DECL_INITIAL (vnode->decl) = error_mark_node;
142
143 /* Check and report ODR violations on virtual tables. */
144 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
145 compare_virtual_tables (prevailing_node, vnode);
146
147 if (vnode->tls_model != prevailing_node->tls_model)
148 {
149 bool error = false;
150
151 /* Non-TLS and TLS never mix together. Also emulated model is not
152 compatible with anything else. */
153 if (prevailing_node->tls_model == TLS_MODEL_NONE
154 || prevailing_node->tls_model == TLS_MODEL_EMULATED
155 || vnode->tls_model == TLS_MODEL_NONE
156 || vnode->tls_model == TLS_MODEL_EMULATED)
157 error = true;
158 /* Linked is silently supporting transitions
159 GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
160 Do the same transitions and error out on others. */
161 else if ((prevailing_node->tls_model == TLS_MODEL_REAL
162 || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
163 && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
164 || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
165 prevailing_node->tls_model = vnode->tls_model;
166 else if ((vnode->tls_model == TLS_MODEL_REAL
167 || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
168 && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
169 || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
170 ;
171 else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
172 && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
173 prevailing_node->tls_model = vnode->tls_model;
174 else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
175 && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
176 ;
177 else
178 error = true;
179 if (error)
180 {
181 error_at (DECL_SOURCE_LOCATION (vnode->decl),
182 "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
183 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
184 "previously defined here as %s",
185 tls_model_names [prevailing_node->tls_model]);
186 }
187 }
188 /* Finally remove the replaced node. */
189 vnode->remove ();
190 }
191
192 /* Return non-zero if we want to output waring about T1 and T2.
193 Return value is a bitmask of reasons of violation:
194 Bit 0 indicates that types are not compatible of memory layout.
195 Bot 1 indicates that types are not compatible because of C++ ODR rule. */
196
197 static int
198 warn_type_compatibility_p (tree prevailing_type, tree type)
199 {
200 int lev = 0;
201 /* C++ provide a robust way to check for type compatibility via the ODR
202 rule. */
203 if (odr_or_derived_type_p (prevailing_type) && odr_or_derived_type_p (type)
204 && !odr_types_equivalent_p (prevailing_type, type))
205 lev = 2;
206
207 /* Function types needs special care, because types_compatible_p never
208 thinks prototype is compatible to non-prototype. */
209 if ((TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
210 && TREE_CODE (type) == TREE_CODE (prevailing_type))
211 {
212 lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
213 TREE_TYPE (type));
214 if (TREE_CODE (type) == METHOD_TYPE)
215 lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
216 TYPE_METHOD_BASETYPE (type));
217 if (prototype_p (prevailing_type) && prototype_p (type)
218 && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
219 {
220 tree parm1, parm2;
221 for (parm1 = TYPE_ARG_TYPES (prevailing_type),
222 parm2 = TYPE_ARG_TYPES (type);
223 parm1 && parm2;
224 parm1 = TREE_CHAIN (prevailing_type),
225 parm2 = TREE_CHAIN (type))
226 lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
227 TREE_VALUE (parm2));
228 if (parm1 || parm2)
229 lev = 3;
230 }
231 if (comp_type_attributes (prevailing_type, type) == 0)
232 lev = 3;
233 return lev;
234 }
235 /* Sharing a global symbol is a strong hint that two types are
236 compatible. We could use this information to complete
237 incomplete pointed-to types more aggressively here, ignoring
238 mismatches in both field and tag names. It's difficult though
239 to guarantee that this does not have side-effects on merging
240 more compatible types from other translation units though. */
241
242 /* We can tolerate differences in type qualification, the
243 qualification of the prevailing definition will prevail.
244 ??? In principle we might want to only warn for structurally
245 incompatible types here, but unless we have protective measures
246 for TBAA in place that would hide useful information. */
247 prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
248 type = TYPE_MAIN_VARIANT (type);
249
250 if (!types_compatible_p (prevailing_type, type))
251 {
252 if (TREE_CODE (prevailing_type) == FUNCTION_TYPE
253 || TREE_CODE (type) == METHOD_TYPE)
254 return 1 | lev;
255 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (prevailing_type))
256 return 1 | lev;
257
258 /* If type is incomplete then avoid warnings in the cases
259 that TBAA handles just fine. */
260
261 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
262 return 1 | lev;
263
264 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
265 {
266 tree tem1 = TREE_TYPE (prevailing_type);
267 tree tem2 = TREE_TYPE (type);
268 while (TREE_CODE (tem1) == ARRAY_TYPE
269 && TREE_CODE (tem2) == ARRAY_TYPE)
270 {
271 tem1 = TREE_TYPE (tem1);
272 tem2 = TREE_TYPE (tem2);
273 }
274
275 if (TREE_CODE (tem1) != TREE_CODE (tem2))
276 return 1 | lev;
277
278 if (!types_compatible_p (tem1, tem2))
279 return 1 | lev;
280 }
281
282 /* Fallthru. Compatible enough. */
283 }
284
285 /* ??? We might want to emit a warning here if type qualification
286 differences were spotted. Do not do this unconditionally though. */
287
288 return lev;
289 }
290
291 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
292 Return false if the symbols are not fully compatible and a diagnostic
293 should be emitted. */
294
295 static bool
296 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
297 {
298 tree prevailing_decl = prevailing->decl;
299 tree decl = entry->decl;
300
301 if (prevailing_decl == decl)
302 return true;
303
304 /* Merge decl state in both directions, we may still end up using
305 the new decl. */
306 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
307 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
308
309 /* The linker may ask us to combine two incompatible symbols.
310 Detect this case and notify the caller of required diagnostics. */
311
312 if (TREE_CODE (decl) == FUNCTION_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 /* Likewise builtins are their own prevailing decl. This preserves
801 non-builtin vs. builtin uses from compile-time. */
802 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
803 return decl;
804
805 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
806 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
807
808 /* Walk through the list of candidates and return the one we merged to. */
809 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
810 if (!ret)
811 return decl;
812
813 return ret->decl;
814 }