PR target/58115
[official-gcc.git] / gcc / lto / lto-symtab.c
blobf5f9d1318ee072f86ea6a62bd42dbb18f56a6c3a
1 /* LTO symbol table.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic-core.h"
25 #include "tree.h"
26 #include "basic-block.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "gimple-expr.h"
30 #include "is-a.h"
31 #include "gimple.h"
32 #include "hashtab.h"
33 #include "plugin-api.h"
34 #include "lto-streamer.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
38 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
39 all edges and removing the old node. */
41 static void
42 lto_cgraph_replace_node (struct cgraph_node *node,
43 struct cgraph_node *prevailing_node)
45 struct cgraph_edge *e, *next;
46 bool compatible_p;
48 if (cgraph_dump_file)
50 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
51 " for symbol %s\n",
52 node->name (), node->order,
53 prevailing_node->name (),
54 prevailing_node->order,
55 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
56 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
59 /* Merge node flags. */
60 if (node->force_output)
61 cgraph_mark_force_output_node (prevailing_node);
62 if (node->address_taken)
64 gcc_assert (!prevailing_node->global.inlined_to);
65 cgraph_mark_address_taken_node (prevailing_node);
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)
74 next = e->next_caller;
75 cgraph_redirect_edge_callee (e, 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;
83 /* Redirect incomming references. */
84 ipa_clone_referring (prevailing_node, &node->ref_list);
86 ipa_merge_profiles (prevailing_node, node);
87 lto_free_function_in_decl_state_for_node (node);
89 if (node->decl != prevailing_node->decl)
90 cgraph_release_function_body (node);
92 /* Time profile merging */
93 if (node->tp_first_run)
94 prevailing_node->tp_first_run = prevailing_node->tp_first_run ?
95 MIN (prevailing_node->tp_first_run, node->tp_first_run) :
96 node->tp_first_run;
98 /* Finally remove the replaced node. */
99 cgraph_remove_node (node);
102 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
103 all edges and removing the old node. */
105 static void
106 lto_varpool_replace_node (varpool_node *vnode,
107 varpool_node *prevailing_node)
109 gcc_assert (!vnode->definition || prevailing_node->definition);
110 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
112 ipa_clone_referring (prevailing_node, &vnode->ref_list);
114 /* Be sure we can garbage collect the initializer. */
115 if (DECL_INITIAL (vnode->decl)
116 && vnode->decl != prevailing_node->decl)
117 DECL_INITIAL (vnode->decl) = error_mark_node;
118 /* Finally remove the replaced node. */
119 varpool_remove_node (vnode);
122 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
123 Return false if the symbols are not fully compatible and a diagnostic
124 should be emitted. */
126 static bool
127 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
129 tree prevailing_decl = prevailing->decl;
130 tree decl = entry->decl;
131 tree prevailing_type, type;
133 if (prevailing_decl == decl)
134 return true;
136 /* Merge decl state in both directions, we may still end up using
137 the new decl. */
138 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
139 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
141 /* The linker may ask us to combine two incompatible symbols.
142 Detect this case and notify the caller of required diagnostics. */
144 if (TREE_CODE (decl) == FUNCTION_DECL)
146 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
147 TREE_TYPE (decl)))
148 /* If we don't have a merged type yet...sigh. The linker
149 wouldn't complain if the types were mismatched, so we
150 probably shouldn't either. Just use the type from
151 whichever decl appears to be associated with the
152 definition. If for some odd reason neither decl is, the
153 older one wins. */
154 (void) 0;
156 return true;
159 /* Now we exclusively deal with VAR_DECLs. */
161 /* Sharing a global symbol is a strong hint that two types are
162 compatible. We could use this information to complete
163 incomplete pointed-to types more aggressively here, ignoring
164 mismatches in both field and tag names. It's difficult though
165 to guarantee that this does not have side-effects on merging
166 more compatible types from other translation units though. */
168 /* We can tolerate differences in type qualification, the
169 qualification of the prevailing definition will prevail.
170 ??? In principle we might want to only warn for structurally
171 incompatible types here, but unless we have protective measures
172 for TBAA in place that would hide useful information. */
173 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
174 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
176 if (!types_compatible_p (prevailing_type, type))
178 if (COMPLETE_TYPE_P (type))
179 return false;
181 /* If type is incomplete then avoid warnings in the cases
182 that TBAA handles just fine. */
184 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
185 return false;
187 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
189 tree tem1 = TREE_TYPE (prevailing_type);
190 tree tem2 = TREE_TYPE (type);
191 while (TREE_CODE (tem1) == ARRAY_TYPE
192 && TREE_CODE (tem2) == ARRAY_TYPE)
194 tem1 = TREE_TYPE (tem1);
195 tem2 = TREE_TYPE (tem2);
198 if (TREE_CODE (tem1) != TREE_CODE (tem2))
199 return false;
201 if (!types_compatible_p (tem1, tem2))
202 return false;
205 /* Fallthru. Compatible enough. */
208 /* ??? We might want to emit a warning here if type qualification
209 differences were spotted. Do not do this unconditionally though. */
211 /* There is no point in comparing too many details of the decls here.
212 The type compatibility checks or the completing of types has properly
213 dealt with most issues. */
215 /* The following should all not invoke fatal errors as in non-LTO
216 mode the linker wouldn't complain either. Just emit warnings. */
218 /* Report a warning if user-specified alignments do not match. */
219 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
220 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
221 return false;
223 return true;
226 /* Return true if the symtab entry E can be replaced by another symtab
227 entry. */
229 static bool
230 lto_symtab_resolve_replaceable_p (symtab_node *e)
232 if (DECL_EXTERNAL (e->decl)
233 || DECL_COMDAT (e->decl)
234 || DECL_ONE_ONLY (e->decl)
235 || DECL_WEAK (e->decl))
236 return true;
238 if (TREE_CODE (e->decl) == VAR_DECL)
239 return (DECL_COMMON (e->decl)
240 || (!flag_no_common && !DECL_INITIAL (e->decl)));
242 return false;
245 /* Return true, if the symbol E should be resolved by lto-symtab.
246 Those are all external symbols and all real symbols that are not static (we
247 handle renaming of static later in partitioning). */
249 static bool
250 lto_symtab_symbol_p (symtab_node *e)
252 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
253 return false;
254 return symtab_real_symbol_p (e);
257 /* Return true if the symtab entry E can be the prevailing one. */
259 static bool
260 lto_symtab_resolve_can_prevail_p (symtab_node *e)
262 if (!lto_symtab_symbol_p (e))
263 return false;
265 /* The C++ frontend ends up neither setting TREE_STATIC nor
266 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
267 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
268 if (DECL_EXTERNAL (e->decl))
269 return false;
271 return e->definition;
274 /* Resolve the symbol with the candidates in the chain *SLOT and store
275 their resolutions. */
277 static symtab_node *
278 lto_symtab_resolve_symbols (symtab_node *first)
280 symtab_node *e;
281 symtab_node *prevailing = NULL;
283 /* Always set e->node so that edges are updated to reflect decl merging. */
284 for (e = first; e; e = e->next_sharing_asm_name)
285 if (lto_symtab_symbol_p (e)
286 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
287 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
288 || e->resolution == LDPR_PREVAILING_DEF))
290 prevailing = e;
291 break;
294 /* If the chain is already resolved there is nothing else to do. */
295 if (prevailing)
297 /* Assert it's the only one. */
298 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
299 if (lto_symtab_symbol_p (e)
300 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
301 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
302 || e->resolution == LDPR_PREVAILING_DEF))
303 fatal_error ("multiple prevailing defs for %qE",
304 DECL_NAME (prevailing->decl));
305 return prevailing;
308 /* Find the single non-replaceable prevailing symbol and
309 diagnose ODR violations. */
310 for (e = first; e; e = e->next_sharing_asm_name)
312 if (!lto_symtab_resolve_can_prevail_p (e))
313 continue;
315 /* If we have a non-replaceable definition it prevails. */
316 if (!lto_symtab_resolve_replaceable_p (e))
318 if (prevailing)
320 error_at (DECL_SOURCE_LOCATION (e->decl),
321 "%qD has already been defined", e->decl);
322 inform (DECL_SOURCE_LOCATION (prevailing->decl),
323 "previously defined here");
325 prevailing = e;
328 if (prevailing)
329 return prevailing;
331 /* Do a second round choosing one from the replaceable prevailing decls. */
332 for (e = first; e; e = e->next_sharing_asm_name)
334 if (!lto_symtab_resolve_can_prevail_p (e))
335 continue;
337 /* Choose the first function that can prevail as prevailing. */
338 if (TREE_CODE (e->decl) == FUNCTION_DECL)
340 prevailing = e;
341 break;
344 /* From variables that can prevail choose the largest one. */
345 if (!prevailing
346 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
347 DECL_SIZE (e->decl))
348 /* When variables are equivalent try to chose one that has useful
349 DECL_INITIAL. This makes sense for keyed vtables that are
350 DECL_EXTERNAL but initialized. In units that do not need them
351 we replace the initializer by error_mark_node to conserve
352 memory.
354 We know that the vtable is keyed outside the LTO unit - otherwise
355 the keyed instance would prevail. We still can preserve useful
356 info in the initializer. */
357 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
358 && (DECL_INITIAL (e->decl)
359 && DECL_INITIAL (e->decl) != error_mark_node)
360 && (!DECL_INITIAL (prevailing->decl)
361 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
362 prevailing = e;
365 return prevailing;
368 /* Merge all decls in the symbol table chain to the prevailing decl and
369 issue diagnostics about type mismatches. If DIAGNOSED_P is true
370 do not issue further diagnostics.*/
372 static void
373 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
375 symtab_node *prevailing;
376 symtab_node *e;
377 vec<tree> mismatches = vNULL;
378 unsigned i;
379 tree decl;
381 /* Nothing to do for a single entry. */
382 prevailing = first;
383 if (!prevailing->next_sharing_asm_name)
384 return;
386 /* Try to merge each entry with the prevailing one. */
387 for (e = prevailing->next_sharing_asm_name;
388 e; e = e->next_sharing_asm_name)
389 if (TREE_PUBLIC (e->decl))
391 if (!lto_symtab_merge (prevailing, e)
392 && !diagnosed_p)
393 mismatches.safe_push (e->decl);
395 if (mismatches.is_empty ())
396 return;
398 /* Diagnose all mismatched re-declarations. */
399 FOR_EACH_VEC_ELT (mismatches, i, decl)
401 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
402 TREE_TYPE (decl)))
403 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
404 "type of %qD does not match original "
405 "declaration", decl);
407 else if ((DECL_USER_ALIGN (prevailing->decl)
408 && DECL_USER_ALIGN (decl))
409 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
411 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
412 "alignment of %qD is bigger than "
413 "original declaration", decl);
416 if (diagnosed_p)
417 inform (DECL_SOURCE_LOCATION (prevailing->decl),
418 "previously declared here");
420 mismatches.release ();
423 /* Helper to process the decl chain for the symbol table entry *SLOT. */
425 static void
426 lto_symtab_merge_decls_1 (symtab_node *first)
428 symtab_node *e;
429 symtab_node *prevailing;
430 bool diagnosed_p = false;
432 if (cgraph_dump_file)
434 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
435 first->asm_name ());
436 for (e = first; e; e = e->next_sharing_asm_name)
437 if (TREE_PUBLIC (e->decl))
438 dump_symtab_node (cgraph_dump_file, e);
441 /* Compute the symbol resolutions. This is a no-op when using the
442 linker plugin and resolution was decided by the linker. */
443 prevailing = lto_symtab_resolve_symbols (first);
445 /* If there's not a prevailing symbol yet it's an external reference.
446 Happens a lot during ltrans. Choose the first symbol with a
447 cgraph or a varpool node. */
448 if (!prevailing)
450 prevailing = first;
451 /* For variables chose with a priority variant with vnode
452 attached (i.e. from unit where external declaration of
453 variable is actually used).
454 When there are multiple variants, chose one with size.
455 This is needed for C++ typeinfos, for example in
456 lto/20081204-1 there are typeifos in both units, just
457 one of them do have size. */
458 if (TREE_CODE (prevailing->decl) == VAR_DECL)
460 for (e = prevailing->next_sharing_asm_name;
461 e; e = e->next_sharing_asm_name)
462 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
463 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
464 && lto_symtab_symbol_p (e))
465 prevailing = e;
467 /* For variables prefer the non-builtin if one is available. */
468 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
470 for (e = first; e; e = e->next_sharing_asm_name)
471 if (TREE_CODE (e->decl) == FUNCTION_DECL
472 && !DECL_BUILT_IN (e->decl)
473 && lto_symtab_symbol_p (e))
475 prevailing = e;
476 break;
481 symtab_prevail_in_asm_name_hash (prevailing);
483 /* Diagnose mismatched objects. */
484 for (e = prevailing->next_sharing_asm_name;
485 e; e = e->next_sharing_asm_name)
487 if (TREE_CODE (prevailing->decl)
488 == TREE_CODE (e->decl))
489 continue;
490 if (!lto_symtab_symbol_p (e))
491 continue;
493 switch (TREE_CODE (prevailing->decl))
495 case VAR_DECL:
496 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
497 error_at (DECL_SOURCE_LOCATION (e->decl),
498 "variable %qD redeclared as function",
499 prevailing->decl);
500 break;
502 case FUNCTION_DECL:
503 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
504 error_at (DECL_SOURCE_LOCATION (e->decl),
505 "function %qD redeclared as variable",
506 prevailing->decl);
507 break;
509 default:
510 gcc_unreachable ();
513 diagnosed_p = true;
515 if (diagnosed_p)
516 inform (DECL_SOURCE_LOCATION (prevailing->decl),
517 "previously declared here");
519 /* Merge the chain to the single prevailing decl and diagnose
520 mismatches. */
521 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
523 if (cgraph_dump_file)
525 fprintf (cgraph_dump_file, "After resolution:\n");
526 for (e = prevailing; e; e = e->next_sharing_asm_name)
527 dump_symtab_node (cgraph_dump_file, e);
531 /* Resolve and merge all symbol table chains to a prevailing decl. */
533 void
534 lto_symtab_merge_decls (void)
536 symtab_node *node;
538 /* Populate assembler name hash. */
539 symtab_initialize_asm_name_hash ();
541 FOR_EACH_SYMBOL (node)
542 if (!node->previous_sharing_asm_name
543 && node->next_sharing_asm_name)
544 lto_symtab_merge_decls_1 (node);
547 /* Helper to process the decl chain for the symbol table entry *SLOT. */
549 static void
550 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
552 symtab_node *e;
553 symtab_node *next;
555 /* Replace the cgraph node of each entry with the prevailing one. */
556 for (e = prevailing->next_sharing_asm_name; e;
557 e = next)
559 next = e->next_sharing_asm_name;
561 if (!lto_symtab_symbol_p (e))
562 continue;
563 cgraph_node *ce = dyn_cast <cgraph_node> (e);
564 if (ce && !DECL_BUILT_IN (e->decl))
565 lto_cgraph_replace_node (ce, cgraph (prevailing));
566 if (varpool_node *ve = dyn_cast <varpool_node> (e))
567 lto_varpool_replace_node (ve, varpool (prevailing));
570 return;
573 /* Merge cgraph nodes according to the symbol merging done by
574 lto_symtab_merge_decls. */
576 void
577 lto_symtab_merge_symbols (void)
579 symtab_node *node;
581 if (!flag_ltrans)
583 symtab_initialize_asm_name_hash ();
585 /* Do the actual merging.
586 At this point we invalidate hash translating decls into symtab nodes
587 because after removing one of duplicate decls the hash is not correcly
588 updated to the ohter dupliate. */
589 FOR_EACH_SYMBOL (node)
590 if (lto_symtab_symbol_p (node)
591 && node->next_sharing_asm_name
592 && !node->previous_sharing_asm_name)
593 lto_symtab_merge_symbols_1 (node);
595 /* Resolve weakref aliases whose target are now in the compilation unit.
596 also re-populate the hash translating decls into symtab nodes*/
597 FOR_EACH_SYMBOL (node)
599 cgraph_node *cnode, *cnode2;
600 varpool_node *vnode;
601 symtab_node *node2;
603 if (!node->analyzed && node->alias_target)
605 symtab_node *tgt = symtab_node_for_asm (node->alias_target);
606 gcc_assert (node->weakref);
607 if (tgt)
608 symtab_resolve_alias (node, tgt);
610 node->aux = NULL;
612 if (!(cnode = dyn_cast <cgraph_node> (node))
613 || !cnode->clone_of
614 || cnode->clone_of->decl != cnode->decl)
616 /* Builtins are not merged via decl merging. It is however
617 possible that tree merging unified the declaration. We
618 do not want duplicate entries in symbol table. */
619 if (cnode && DECL_BUILT_IN (node->decl)
620 && (cnode2 = cgraph_get_node (node->decl))
621 && cnode2 != cnode)
622 lto_cgraph_replace_node (cnode2, cnode);
624 /* The user defined assembler variables are also not unified by their
625 symbol name (since it is irrelevant), but we need to unify symbol
626 nodes if tree merging occured. */
627 if ((vnode = dyn_cast <varpool_node> (node))
628 && DECL_HARD_REGISTER (vnode->decl)
629 && (node2 = symtab_get_node (vnode->decl))
630 && node2 != node)
631 lto_varpool_replace_node (dyn_cast <varpool_node> (node2),
632 vnode);
635 /* Abstract functions may have duplicated cgraph nodes attached;
636 remove them. */
637 else if (cnode && DECL_ABSTRACT (cnode->decl)
638 && (cnode2 = cgraph_get_node (node->decl))
639 && cnode2 != cnode)
640 cgraph_remove_node (cnode2);
642 symtab_insert_node_to_hashtable (node);
648 /* Given the decl DECL, return the prevailing decl with the same name. */
650 tree
651 lto_symtab_prevailing_decl (tree decl)
653 symtab_node *ret;
655 /* Builtins and local symbols are their own prevailing decl. */
656 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
657 return decl;
659 /* DECL_ABSTRACTs are their own prevailng decl. */
660 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
661 return decl;
663 /* Likewise builtins are their own prevailing decl. This preserves
664 non-builtin vs. builtin uses from compile-time. */
665 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
666 return decl;
668 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
669 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
671 /* Walk through the list of candidates and return the one we merged to. */
672 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
673 if (!ret)
674 return decl;
676 return ret->decl;