Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / lto / lto-symtab.c
blob71242c8926f1550649bb9f2b904d65386ff353be
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->forced_by_abi)
63 prevailing_node->forced_by_abi = true;
64 if (node->address_taken)
66 gcc_assert (!prevailing_node->global.inlined_to);
67 cgraph_mark_address_taken_node (prevailing_node);
70 /* Redirect all incoming edges. */
71 compatible_p
72 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
73 TREE_TYPE (TREE_TYPE (node->decl)));
74 for (e = node->callers; e; e = next)
76 next = e->next_caller;
77 cgraph_redirect_edge_callee (e, prevailing_node);
78 /* If there is a mismatch between the supposed callee return type and
79 the real one do not attempt to inline this function.
80 ??? We really need a way to match function signatures for ABI
81 compatibility and perform related promotions at inlining time. */
82 if (!compatible_p)
83 e->call_stmt_cannot_inline_p = 1;
85 /* Redirect incomming references. */
86 ipa_clone_referring (prevailing_node, &node->ref_list);
88 ipa_merge_profiles (prevailing_node, node);
89 lto_free_function_in_decl_state_for_node (node);
91 if (node->decl != prevailing_node->decl)
92 cgraph_release_function_body (node);
94 /* Time profile merging */
95 if (node->tp_first_run)
96 prevailing_node->tp_first_run = prevailing_node->tp_first_run ?
97 MIN (prevailing_node->tp_first_run, node->tp_first_run) :
98 node->tp_first_run;
100 /* Finally remove the replaced node. */
101 cgraph_remove_node (node);
104 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
105 all edges and removing the old node. */
107 static void
108 lto_varpool_replace_node (varpool_node *vnode,
109 varpool_node *prevailing_node)
111 gcc_assert (!vnode->definition || prevailing_node->definition);
112 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
114 ipa_clone_referring (prevailing_node, &vnode->ref_list);
115 if (vnode->force_output)
116 prevailing_node->force_output = true;
117 if (vnode->forced_by_abi)
118 prevailing_node->forced_by_abi = true;
120 /* Be sure we can garbage collect the initializer. */
121 if (DECL_INITIAL (vnode->decl)
122 && vnode->decl != prevailing_node->decl)
123 DECL_INITIAL (vnode->decl) = error_mark_node;
124 /* Finally remove the replaced node. */
125 varpool_remove_node (vnode);
128 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
129 Return false if the symbols are not fully compatible and a diagnostic
130 should be emitted. */
132 static bool
133 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
135 tree prevailing_decl = prevailing->decl;
136 tree decl = entry->decl;
137 tree prevailing_type, type;
139 if (prevailing_decl == decl)
140 return true;
142 /* Merge decl state in both directions, we may still end up using
143 the new decl. */
144 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
145 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
147 /* The linker may ask us to combine two incompatible symbols.
148 Detect this case and notify the caller of required diagnostics. */
150 if (TREE_CODE (decl) == FUNCTION_DECL)
152 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
153 TREE_TYPE (decl)))
154 /* If we don't have a merged type yet...sigh. The linker
155 wouldn't complain if the types were mismatched, so we
156 probably shouldn't either. Just use the type from
157 whichever decl appears to be associated with the
158 definition. If for some odd reason neither decl is, the
159 older one wins. */
160 (void) 0;
162 return true;
165 /* Now we exclusively deal with VAR_DECLs. */
167 /* Sharing a global symbol is a strong hint that two types are
168 compatible. We could use this information to complete
169 incomplete pointed-to types more aggressively here, ignoring
170 mismatches in both field and tag names. It's difficult though
171 to guarantee that this does not have side-effects on merging
172 more compatible types from other translation units though. */
174 /* We can tolerate differences in type qualification, the
175 qualification of the prevailing definition will prevail.
176 ??? In principle we might want to only warn for structurally
177 incompatible types here, but unless we have protective measures
178 for TBAA in place that would hide useful information. */
179 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
180 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
182 if (!types_compatible_p (prevailing_type, type))
184 if (COMPLETE_TYPE_P (type))
185 return false;
187 /* If type is incomplete then avoid warnings in the cases
188 that TBAA handles just fine. */
190 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
191 return false;
193 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
195 tree tem1 = TREE_TYPE (prevailing_type);
196 tree tem2 = TREE_TYPE (type);
197 while (TREE_CODE (tem1) == ARRAY_TYPE
198 && TREE_CODE (tem2) == ARRAY_TYPE)
200 tem1 = TREE_TYPE (tem1);
201 tem2 = TREE_TYPE (tem2);
204 if (TREE_CODE (tem1) != TREE_CODE (tem2))
205 return false;
207 if (!types_compatible_p (tem1, tem2))
208 return false;
211 /* Fallthru. Compatible enough. */
214 /* ??? We might want to emit a warning here if type qualification
215 differences were spotted. Do not do this unconditionally though. */
217 /* There is no point in comparing too many details of the decls here.
218 The type compatibility checks or the completing of types has properly
219 dealt with most issues. */
221 /* The following should all not invoke fatal errors as in non-LTO
222 mode the linker wouldn't complain either. Just emit warnings. */
224 /* Report a warning if user-specified alignments do not match. */
225 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
226 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
227 return false;
229 return true;
232 /* Return true if the symtab entry E can be replaced by another symtab
233 entry. */
235 static bool
236 lto_symtab_resolve_replaceable_p (symtab_node *e)
238 if (DECL_EXTERNAL (e->decl)
239 || DECL_COMDAT (e->decl)
240 || DECL_ONE_ONLY (e->decl)
241 || DECL_WEAK (e->decl))
242 return true;
244 if (TREE_CODE (e->decl) == VAR_DECL)
245 return (DECL_COMMON (e->decl)
246 || (!flag_no_common && !DECL_INITIAL (e->decl)));
248 return false;
251 /* Return true, if the symbol E should be resolved by lto-symtab.
252 Those are all external symbols and all real symbols that are not static (we
253 handle renaming of static later in partitioning). */
255 static bool
256 lto_symtab_symbol_p (symtab_node *e)
258 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
259 return false;
260 return symtab_real_symbol_p (e);
263 /* Return true if the symtab entry E can be the prevailing one. */
265 static bool
266 lto_symtab_resolve_can_prevail_p (symtab_node *e)
268 if (!lto_symtab_symbol_p (e))
269 return false;
271 /* The C++ frontend ends up neither setting TREE_STATIC nor
272 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
273 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
274 if (DECL_EXTERNAL (e->decl))
275 return false;
277 return e->definition;
280 /* Resolve the symbol with the candidates in the chain *SLOT and store
281 their resolutions. */
283 static symtab_node *
284 lto_symtab_resolve_symbols (symtab_node *first)
286 symtab_node *e;
287 symtab_node *prevailing = NULL;
289 /* Always set e->node so that edges are updated to reflect decl merging. */
290 for (e = first; e; e = e->next_sharing_asm_name)
291 if (lto_symtab_symbol_p (e)
292 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
293 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
294 || e->resolution == LDPR_PREVAILING_DEF))
296 prevailing = e;
297 break;
300 /* If the chain is already resolved there is nothing else to do. */
301 if (prevailing)
303 /* Assert it's the only one. */
304 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
305 if (lto_symtab_symbol_p (e)
306 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
307 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
308 || e->resolution == LDPR_PREVAILING_DEF))
309 fatal_error ("multiple prevailing defs for %qE",
310 DECL_NAME (prevailing->decl));
311 return prevailing;
314 /* Find the single non-replaceable prevailing symbol and
315 diagnose ODR violations. */
316 for (e = first; e; e = e->next_sharing_asm_name)
318 if (!lto_symtab_resolve_can_prevail_p (e))
319 continue;
321 /* If we have a non-replaceable definition it prevails. */
322 if (!lto_symtab_resolve_replaceable_p (e))
324 if (prevailing)
326 error_at (DECL_SOURCE_LOCATION (e->decl),
327 "%qD has already been defined", e->decl);
328 inform (DECL_SOURCE_LOCATION (prevailing->decl),
329 "previously defined here");
331 prevailing = e;
334 if (prevailing)
335 return prevailing;
337 /* Do a second round choosing one from the replaceable prevailing decls. */
338 for (e = first; e; e = e->next_sharing_asm_name)
340 if (!lto_symtab_resolve_can_prevail_p (e))
341 continue;
343 /* Choose the first function that can prevail as prevailing. */
344 if (TREE_CODE (e->decl) == FUNCTION_DECL)
346 prevailing = e;
347 break;
350 /* From variables that can prevail choose the largest one. */
351 if (!prevailing
352 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
353 DECL_SIZE (e->decl))
354 /* When variables are equivalent try to chose one that has useful
355 DECL_INITIAL. This makes sense for keyed vtables that are
356 DECL_EXTERNAL but initialized. In units that do not need them
357 we replace the initializer by error_mark_node to conserve
358 memory.
360 We know that the vtable is keyed outside the LTO unit - otherwise
361 the keyed instance would prevail. We still can preserve useful
362 info in the initializer. */
363 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
364 && (DECL_INITIAL (e->decl)
365 && DECL_INITIAL (e->decl) != error_mark_node)
366 && (!DECL_INITIAL (prevailing->decl)
367 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
368 prevailing = e;
371 return prevailing;
374 /* Merge all decls in the symbol table chain to the prevailing decl and
375 issue diagnostics about type mismatches. If DIAGNOSED_P is true
376 do not issue further diagnostics.*/
378 static void
379 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
381 symtab_node *prevailing;
382 symtab_node *e;
383 vec<tree> mismatches = vNULL;
384 unsigned i;
385 tree decl;
387 /* Nothing to do for a single entry. */
388 prevailing = first;
389 if (!prevailing->next_sharing_asm_name)
390 return;
392 /* Try to merge each entry with the prevailing one. */
393 for (e = prevailing->next_sharing_asm_name;
394 e; e = e->next_sharing_asm_name)
395 if (TREE_PUBLIC (e->decl))
397 if (!lto_symtab_merge (prevailing, e)
398 && !diagnosed_p)
399 mismatches.safe_push (e->decl);
401 if (mismatches.is_empty ())
402 return;
404 /* Diagnose all mismatched re-declarations. */
405 FOR_EACH_VEC_ELT (mismatches, i, decl)
407 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
408 TREE_TYPE (decl)))
409 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
410 "type of %qD does not match original "
411 "declaration", decl);
413 else if ((DECL_USER_ALIGN (prevailing->decl)
414 && DECL_USER_ALIGN (decl))
415 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
417 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
418 "alignment of %qD is bigger than "
419 "original declaration", decl);
422 if (diagnosed_p)
423 inform (DECL_SOURCE_LOCATION (prevailing->decl),
424 "previously declared here");
426 mismatches.release ();
429 /* Helper to process the decl chain for the symbol table entry *SLOT. */
431 static void
432 lto_symtab_merge_decls_1 (symtab_node *first)
434 symtab_node *e;
435 symtab_node *prevailing;
436 bool diagnosed_p = false;
438 if (cgraph_dump_file)
440 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
441 first->asm_name ());
442 for (e = first; e; e = e->next_sharing_asm_name)
443 if (TREE_PUBLIC (e->decl))
444 dump_symtab_node (cgraph_dump_file, e);
447 /* Compute the symbol resolutions. This is a no-op when using the
448 linker plugin and resolution was decided by the linker. */
449 prevailing = lto_symtab_resolve_symbols (first);
451 /* If there's not a prevailing symbol yet it's an external reference.
452 Happens a lot during ltrans. Choose the first symbol with a
453 cgraph or a varpool node. */
454 if (!prevailing)
456 prevailing = first;
457 /* For variables chose with a priority variant with vnode
458 attached (i.e. from unit where external declaration of
459 variable is actually used).
460 When there are multiple variants, chose one with size.
461 This is needed for C++ typeinfos, for example in
462 lto/20081204-1 there are typeifos in both units, just
463 one of them do have size. */
464 if (TREE_CODE (prevailing->decl) == VAR_DECL)
466 for (e = prevailing->next_sharing_asm_name;
467 e; e = e->next_sharing_asm_name)
468 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
469 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
470 && lto_symtab_symbol_p (e))
471 prevailing = e;
473 /* For variables prefer the non-builtin if one is available. */
474 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
476 for (e = first; e; e = e->next_sharing_asm_name)
477 if (TREE_CODE (e->decl) == FUNCTION_DECL
478 && !DECL_BUILT_IN (e->decl)
479 && lto_symtab_symbol_p (e))
481 prevailing = e;
482 break;
487 symtab_prevail_in_asm_name_hash (prevailing);
489 /* Diagnose mismatched objects. */
490 for (e = prevailing->next_sharing_asm_name;
491 e; e = e->next_sharing_asm_name)
493 if (TREE_CODE (prevailing->decl)
494 == TREE_CODE (e->decl))
495 continue;
496 if (!lto_symtab_symbol_p (e))
497 continue;
499 switch (TREE_CODE (prevailing->decl))
501 case VAR_DECL:
502 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
503 error_at (DECL_SOURCE_LOCATION (e->decl),
504 "variable %qD redeclared as function",
505 prevailing->decl);
506 break;
508 case FUNCTION_DECL:
509 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
510 error_at (DECL_SOURCE_LOCATION (e->decl),
511 "function %qD redeclared as variable",
512 prevailing->decl);
513 break;
515 default:
516 gcc_unreachable ();
519 diagnosed_p = true;
521 if (diagnosed_p)
522 inform (DECL_SOURCE_LOCATION (prevailing->decl),
523 "previously declared here");
525 /* Merge the chain to the single prevailing decl and diagnose
526 mismatches. */
527 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
529 if (cgraph_dump_file)
531 fprintf (cgraph_dump_file, "After resolution:\n");
532 for (e = prevailing; e; e = e->next_sharing_asm_name)
533 dump_symtab_node (cgraph_dump_file, e);
537 /* Resolve and merge all symbol table chains to a prevailing decl. */
539 void
540 lto_symtab_merge_decls (void)
542 symtab_node *node;
544 /* Populate assembler name hash. */
545 symtab_initialize_asm_name_hash ();
547 FOR_EACH_SYMBOL (node)
548 if (!node->previous_sharing_asm_name
549 && node->next_sharing_asm_name)
550 lto_symtab_merge_decls_1 (node);
553 /* Helper to process the decl chain for the symbol table entry *SLOT. */
555 static void
556 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
558 symtab_node *e;
559 symtab_node *next;
561 /* Replace the cgraph node of each entry with the prevailing one. */
562 for (e = prevailing->next_sharing_asm_name; e;
563 e = next)
565 next = e->next_sharing_asm_name;
567 if (!lto_symtab_symbol_p (e))
568 continue;
569 cgraph_node *ce = dyn_cast <cgraph_node> (e);
570 if (ce && !DECL_BUILT_IN (e->decl))
571 lto_cgraph_replace_node (ce, cgraph (prevailing));
572 if (varpool_node *ve = dyn_cast <varpool_node> (e))
573 lto_varpool_replace_node (ve, varpool (prevailing));
576 return;
579 /* Merge cgraph nodes according to the symbol merging done by
580 lto_symtab_merge_decls. */
582 void
583 lto_symtab_merge_symbols (void)
585 symtab_node *node;
587 if (!flag_ltrans)
589 symtab_initialize_asm_name_hash ();
591 /* Do the actual merging.
592 At this point we invalidate hash translating decls into symtab nodes
593 because after removing one of duplicate decls the hash is not correcly
594 updated to the ohter dupliate. */
595 FOR_EACH_SYMBOL (node)
596 if (lto_symtab_symbol_p (node)
597 && node->next_sharing_asm_name
598 && !node->previous_sharing_asm_name)
599 lto_symtab_merge_symbols_1 (node);
601 /* Resolve weakref aliases whose target are now in the compilation unit.
602 also re-populate the hash translating decls into symtab nodes*/
603 FOR_EACH_SYMBOL (node)
605 cgraph_node *cnode, *cnode2;
606 varpool_node *vnode;
607 symtab_node *node2;
609 if (!node->analyzed && node->alias_target)
611 symtab_node *tgt = symtab_node_for_asm (node->alias_target);
612 gcc_assert (node->weakref);
613 if (tgt)
614 symtab_resolve_alias (node, tgt);
616 node->aux = NULL;
618 if (!(cnode = dyn_cast <cgraph_node> (node))
619 || !cnode->clone_of
620 || cnode->clone_of->decl != cnode->decl)
622 /* Builtins are not merged via decl merging. It is however
623 possible that tree merging unified the declaration. We
624 do not want duplicate entries in symbol table. */
625 if (cnode && DECL_BUILT_IN (node->decl)
626 && (cnode2 = cgraph_get_node (node->decl))
627 && cnode2 != cnode)
628 lto_cgraph_replace_node (cnode2, cnode);
630 /* The user defined assembler variables are also not unified by their
631 symbol name (since it is irrelevant), but we need to unify symbol
632 nodes if tree merging occured. */
633 if ((vnode = dyn_cast <varpool_node> (node))
634 && DECL_HARD_REGISTER (vnode->decl)
635 && (node2 = symtab_get_node (vnode->decl))
636 && node2 != node)
637 lto_varpool_replace_node (dyn_cast <varpool_node> (node2),
638 vnode);
641 /* Abstract functions may have duplicated cgraph nodes attached;
642 remove them. */
643 else if (cnode && DECL_ABSTRACT (cnode->decl)
644 && (cnode2 = cgraph_get_node (node->decl))
645 && cnode2 != cnode)
646 cgraph_remove_node (cnode2);
648 symtab_insert_node_to_hashtable (node);
654 /* Given the decl DECL, return the prevailing decl with the same name. */
656 tree
657 lto_symtab_prevailing_decl (tree decl)
659 symtab_node *ret;
661 /* Builtins and local symbols are their own prevailing decl. */
662 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
663 return decl;
665 /* DECL_ABSTRACTs are their own prevailng decl. */
666 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
667 return decl;
669 /* Likewise builtins are their own prevailing decl. This preserves
670 non-builtin vs. builtin uses from compile-time. */
671 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
672 return decl;
674 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
675 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
677 /* Walk through the list of candidates and return the one we merged to. */
678 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
679 if (!ret)
680 return decl;
682 return ret->decl;