Daily bump.
[official-gcc.git] / gcc / lto / lto-symtab.c
blobec42a930b7f7afa9032904c0883287593d3175bb
1 /* LTO symbol table.
2 Copyright (C) 2009-2013 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 "gimple.h"
27 #include "ggc.h"
28 #include "hashtab.h"
29 #include "plugin-api.h"
30 #include "lto-streamer.h"
31 #include "ipa-utils.h"
33 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
34 all edges and removing the old node. */
36 static void
37 lto_cgraph_replace_node (struct cgraph_node *node,
38 struct cgraph_node *prevailing_node)
40 struct cgraph_edge *e, *next;
41 bool compatible_p;
43 if (cgraph_dump_file)
45 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
46 " for symbol %s\n",
47 cgraph_node_name (node), node->order,
48 cgraph_node_name (prevailing_node),
49 prevailing_node->order,
50 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
51 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
54 /* Merge node flags. */
55 if (node->force_output)
56 cgraph_mark_force_output_node (prevailing_node);
57 if (node->address_taken)
59 gcc_assert (!prevailing_node->global.inlined_to);
60 cgraph_mark_address_taken_node (prevailing_node);
63 /* Redirect all incoming edges. */
64 compatible_p
65 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
66 TREE_TYPE (TREE_TYPE (node->decl)));
67 for (e = node->callers; e; e = next)
69 next = e->next_caller;
70 cgraph_redirect_edge_callee (e, prevailing_node);
71 /* If there is a mismatch between the supposed callee return type and
72 the real one do not attempt to inline this function.
73 ??? We really need a way to match function signatures for ABI
74 compatibility and perform related promotions at inlining time. */
75 if (!compatible_p)
76 e->call_stmt_cannot_inline_p = 1;
78 /* Redirect incomming references. */
79 ipa_clone_referring (prevailing_node, &node->ref_list);
81 ipa_merge_profiles (prevailing_node, node);
82 lto_free_function_in_decl_state_for_node (node);
84 if (node->decl != prevailing_node->decl)
85 cgraph_release_function_body (node);
87 /* Finally remove the replaced node. */
88 cgraph_remove_node (node);
91 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
92 all edges and removing the old node. */
94 static void
95 lto_varpool_replace_node (struct varpool_node *vnode,
96 struct varpool_node *prevailing_node)
98 gcc_assert (!vnode->definition || prevailing_node->definition);
99 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
101 ipa_clone_referring (prevailing_node, &vnode->ref_list);
103 /* Be sure we can garbage collect the initializer. */
104 if (DECL_INITIAL (vnode->decl)
105 && vnode->decl != prevailing_node->decl)
106 DECL_INITIAL (vnode->decl) = error_mark_node;
107 /* Finally remove the replaced node. */
108 varpool_remove_node (vnode);
111 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
112 Return false if the symbols are not fully compatible and a diagnostic
113 should be emitted. */
115 static bool
116 lto_symtab_merge (symtab_node prevailing, symtab_node entry)
118 tree prevailing_decl = prevailing->decl;
119 tree decl = entry->decl;
120 tree prevailing_type, type;
122 if (prevailing_decl == decl)
123 return true;
125 /* Merge decl state in both directions, we may still end up using
126 the new decl. */
127 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
128 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
130 /* The linker may ask us to combine two incompatible symbols.
131 Detect this case and notify the caller of required diagnostics. */
133 if (TREE_CODE (decl) == FUNCTION_DECL)
135 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
136 TREE_TYPE (decl)))
137 /* If we don't have a merged type yet...sigh. The linker
138 wouldn't complain if the types were mismatched, so we
139 probably shouldn't either. Just use the type from
140 whichever decl appears to be associated with the
141 definition. If for some odd reason neither decl is, the
142 older one wins. */
143 (void) 0;
145 return true;
148 /* Now we exclusively deal with VAR_DECLs. */
150 /* Sharing a global symbol is a strong hint that two types are
151 compatible. We could use this information to complete
152 incomplete pointed-to types more aggressively here, ignoring
153 mismatches in both field and tag names. It's difficult though
154 to guarantee that this does not have side-effects on merging
155 more compatible types from other translation units though. */
157 /* We can tolerate differences in type qualification, the
158 qualification of the prevailing definition will prevail.
159 ??? In principle we might want to only warn for structurally
160 incompatible types here, but unless we have protective measures
161 for TBAA in place that would hide useful information. */
162 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
163 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
165 if (!types_compatible_p (prevailing_type, type))
167 if (COMPLETE_TYPE_P (type))
168 return false;
170 /* If type is incomplete then avoid warnings in the cases
171 that TBAA handles just fine. */
173 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
174 return false;
176 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
178 tree tem1 = TREE_TYPE (prevailing_type);
179 tree tem2 = TREE_TYPE (type);
180 while (TREE_CODE (tem1) == ARRAY_TYPE
181 && TREE_CODE (tem2) == ARRAY_TYPE)
183 tem1 = TREE_TYPE (tem1);
184 tem2 = TREE_TYPE (tem2);
187 if (TREE_CODE (tem1) != TREE_CODE (tem2))
188 return false;
190 if (!types_compatible_p (tem1, tem2))
191 return false;
194 /* Fallthru. Compatible enough. */
197 /* ??? We might want to emit a warning here if type qualification
198 differences were spotted. Do not do this unconditionally though. */
200 /* There is no point in comparing too many details of the decls here.
201 The type compatibility checks or the completing of types has properly
202 dealt with most issues. */
204 /* The following should all not invoke fatal errors as in non-LTO
205 mode the linker wouldn't complain either. Just emit warnings. */
207 /* Report a warning if user-specified alignments do not match. */
208 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
209 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
210 return false;
212 return true;
215 /* Return true if the symtab entry E can be replaced by another symtab
216 entry. */
218 static bool
219 lto_symtab_resolve_replaceable_p (symtab_node e)
221 if (DECL_EXTERNAL (e->decl)
222 || DECL_COMDAT (e->decl)
223 || DECL_ONE_ONLY (e->decl)
224 || DECL_WEAK (e->decl))
225 return true;
227 if (TREE_CODE (e->decl) == VAR_DECL)
228 return (DECL_COMMON (e->decl)
229 || (!flag_no_common && !DECL_INITIAL (e->decl)));
231 return false;
234 /* Return true, if the symbol E should be resolved by lto-symtab.
235 Those are all external symbols and all real symbols that are not static (we
236 handle renaming of static later in partitioning). */
238 static bool
239 lto_symtab_symbol_p (symtab_node e)
241 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
242 return false;
243 return symtab_real_symbol_p (e);
246 /* Return true if the symtab entry E can be the prevailing one. */
248 static bool
249 lto_symtab_resolve_can_prevail_p (symtab_node e)
251 if (!lto_symtab_symbol_p (e))
252 return false;
254 /* The C++ frontend ends up neither setting TREE_STATIC nor
255 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
256 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
257 if (DECL_EXTERNAL (e->decl))
258 return false;
260 return e->definition;
263 /* Resolve the symbol with the candidates in the chain *SLOT and store
264 their resolutions. */
266 static symtab_node
267 lto_symtab_resolve_symbols (symtab_node first)
269 symtab_node e;
270 symtab_node prevailing = NULL;
272 /* Always set e->node so that edges are updated to reflect decl merging. */
273 for (e = first; e; e = e->next_sharing_asm_name)
274 if (lto_symtab_symbol_p (e)
275 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
276 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
277 || e->resolution == LDPR_PREVAILING_DEF))
279 prevailing = e;
280 break;
283 /* If the chain is already resolved there is nothing else to do. */
284 if (prevailing)
286 /* Assert it's the only one. */
287 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
288 if (lto_symtab_symbol_p (e)
289 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
290 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
291 || e->resolution == LDPR_PREVAILING_DEF))
292 fatal_error ("multiple prevailing defs for %qE",
293 DECL_NAME (prevailing->decl));
294 return prevailing;
297 /* Find the single non-replaceable prevailing symbol and
298 diagnose ODR violations. */
299 for (e = first; e; e = e->next_sharing_asm_name)
301 if (!lto_symtab_resolve_can_prevail_p (e))
302 continue;
304 /* If we have a non-replaceable definition it prevails. */
305 if (!lto_symtab_resolve_replaceable_p (e))
307 if (prevailing)
309 error_at (DECL_SOURCE_LOCATION (e->decl),
310 "%qD has already been defined", e->decl);
311 inform (DECL_SOURCE_LOCATION (prevailing->decl),
312 "previously defined here");
314 prevailing = e;
317 if (prevailing)
318 return prevailing;
320 /* Do a second round choosing one from the replaceable prevailing decls. */
321 for (e = first; e; e = e->next_sharing_asm_name)
323 if (!lto_symtab_resolve_can_prevail_p (e))
324 continue;
326 /* Choose the first function that can prevail as prevailing. */
327 if (TREE_CODE (e->decl) == FUNCTION_DECL)
329 prevailing = e;
330 break;
333 /* From variables that can prevail choose the largest one. */
334 if (!prevailing
335 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
336 DECL_SIZE (e->decl))
337 /* When variables are equivalent try to chose one that has useful
338 DECL_INITIAL. This makes sense for keyed vtables that are
339 DECL_EXTERNAL but initialized. In units that do not need them
340 we replace the initializer by error_mark_node to conserve
341 memory.
343 We know that the vtable is keyed outside the LTO unit - otherwise
344 the keyed instance would prevail. We still can preserve useful
345 info in the initializer. */
346 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
347 && (DECL_INITIAL (e->decl)
348 && DECL_INITIAL (e->decl) != error_mark_node)
349 && (!DECL_INITIAL (prevailing->decl)
350 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
351 prevailing = e;
354 return prevailing;
357 /* Merge all decls in the symbol table chain to the prevailing decl and
358 issue diagnostics about type mismatches. If DIAGNOSED_P is true
359 do not issue further diagnostics.*/
361 static void
362 lto_symtab_merge_decls_2 (symtab_node first, bool diagnosed_p)
364 symtab_node prevailing;
365 symtab_node e;
366 vec<tree> mismatches = vNULL;
367 unsigned i;
368 tree decl;
370 /* Nothing to do for a single entry. */
371 prevailing = first;
372 if (!prevailing->next_sharing_asm_name)
373 return;
375 /* Try to merge each entry with the prevailing one. */
376 for (e = prevailing->next_sharing_asm_name;
377 e; e = e->next_sharing_asm_name)
378 if (TREE_PUBLIC (e->decl))
380 if (!lto_symtab_merge (prevailing, e)
381 && !diagnosed_p)
382 mismatches.safe_push (e->decl);
384 if (mismatches.is_empty ())
385 return;
387 /* Diagnose all mismatched re-declarations. */
388 FOR_EACH_VEC_ELT (mismatches, i, decl)
390 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
391 TREE_TYPE (decl)))
392 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
393 "type of %qD does not match original "
394 "declaration", decl);
396 else if ((DECL_USER_ALIGN (prevailing->decl)
397 && DECL_USER_ALIGN (decl))
398 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
400 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
401 "alignment of %qD is bigger than "
402 "original declaration", decl);
405 if (diagnosed_p)
406 inform (DECL_SOURCE_LOCATION (prevailing->decl),
407 "previously declared here");
409 mismatches.release ();
412 /* Helper to process the decl chain for the symbol table entry *SLOT. */
414 static void
415 lto_symtab_merge_decls_1 (symtab_node first)
417 symtab_node e;
418 symtab_node prevailing;
419 bool diagnosed_p = false;
421 if (cgraph_dump_file)
423 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
424 symtab_node_asm_name (first));
425 for (e = first; e; e = e->next_sharing_asm_name)
426 if (TREE_PUBLIC (e->decl))
427 dump_symtab_node (cgraph_dump_file, e);
430 /* Compute the symbol resolutions. This is a no-op when using the
431 linker plugin and resolution was decided by the linker. */
432 prevailing = lto_symtab_resolve_symbols (first);
434 /* If there's not a prevailing symbol yet it's an external reference.
435 Happens a lot during ltrans. Choose the first symbol with a
436 cgraph or a varpool node. */
437 if (!prevailing)
439 prevailing = first;
440 /* For variables chose with a priority variant with vnode
441 attached (i.e. from unit where external declaration of
442 variable is actually used).
443 When there are multiple variants, chose one with size.
444 This is needed for C++ typeinfos, for example in
445 lto/20081204-1 there are typeifos in both units, just
446 one of them do have size. */
447 if (TREE_CODE (prevailing->decl) == VAR_DECL)
449 for (e = prevailing->next_sharing_asm_name;
450 e; e = e->next_sharing_asm_name)
451 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
452 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
453 && lto_symtab_symbol_p (e))
454 prevailing = e;
456 /* For variables prefer the non-builtin if one is available. */
457 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
459 for (e = first; e; e = e->next_sharing_asm_name)
460 if (TREE_CODE (e->decl) == FUNCTION_DECL
461 && !DECL_BUILT_IN (e->decl)
462 && lto_symtab_symbol_p (e))
464 prevailing = e;
465 break;
470 symtab_prevail_in_asm_name_hash (prevailing);
472 /* Diagnose mismatched objects. */
473 for (e = prevailing->next_sharing_asm_name;
474 e; e = e->next_sharing_asm_name)
476 if (TREE_CODE (prevailing->decl)
477 == TREE_CODE (e->decl))
478 continue;
479 if (!lto_symtab_symbol_p (e))
480 continue;
482 switch (TREE_CODE (prevailing->decl))
484 case VAR_DECL:
485 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
486 error_at (DECL_SOURCE_LOCATION (e->decl),
487 "variable %qD redeclared as function",
488 prevailing->decl);
489 break;
491 case FUNCTION_DECL:
492 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
493 error_at (DECL_SOURCE_LOCATION (e->decl),
494 "function %qD redeclared as variable",
495 prevailing->decl);
496 break;
498 default:
499 gcc_unreachable ();
502 diagnosed_p = true;
504 if (diagnosed_p)
505 inform (DECL_SOURCE_LOCATION (prevailing->decl),
506 "previously declared here");
508 /* Merge the chain to the single prevailing decl and diagnose
509 mismatches. */
510 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
512 if (cgraph_dump_file)
514 fprintf (cgraph_dump_file, "After resolution:\n");
515 for (e = prevailing; e; e = e->next_sharing_asm_name)
516 dump_symtab_node (cgraph_dump_file, e);
520 /* Resolve and merge all symbol table chains to a prevailing decl. */
522 void
523 lto_symtab_merge_decls (void)
525 symtab_node node;
527 /* Populate assembler name hash. */
528 symtab_initialize_asm_name_hash ();
530 FOR_EACH_SYMBOL (node)
531 if (!node->previous_sharing_asm_name
532 && node->next_sharing_asm_name)
533 lto_symtab_merge_decls_1 (node);
536 /* Helper to process the decl chain for the symbol table entry *SLOT. */
538 static void
539 lto_symtab_merge_symbols_1 (symtab_node prevailing)
541 symtab_node e;
542 symtab_node next;
544 /* Replace the cgraph node of each entry with the prevailing one. */
545 for (e = prevailing->next_sharing_asm_name; e;
546 e = next)
548 next = e->next_sharing_asm_name;
550 if (!lto_symtab_symbol_p (e))
551 continue;
552 cgraph_node *ce = dyn_cast <cgraph_node> (e);
553 if (ce && !DECL_BUILT_IN (e->decl))
554 lto_cgraph_replace_node (ce, cgraph (prevailing));
555 if (varpool_node *ve = dyn_cast <varpool_node> (e))
556 lto_varpool_replace_node (ve, varpool (prevailing));
559 return;
562 /* Merge cgraph nodes according to the symbol merging done by
563 lto_symtab_merge_decls. */
565 void
566 lto_symtab_merge_symbols (void)
568 symtab_node node;
570 if (!flag_ltrans)
572 symtab_initialize_asm_name_hash ();
574 /* Do the actual merging.
575 At this point we invalidate hash translating decls into symtab nodes
576 because after removing one of duplicate decls the hash is not correcly
577 updated to the ohter dupliate. */
578 FOR_EACH_SYMBOL (node)
579 if (lto_symtab_symbol_p (node)
580 && node->next_sharing_asm_name
581 && !node->previous_sharing_asm_name)
582 lto_symtab_merge_symbols_1 (node);
584 /* Resolve weakref aliases whose target are now in the compilation unit.
585 also re-populate the hash translating decls into symtab nodes*/
586 FOR_EACH_SYMBOL (node)
588 cgraph_node *cnode, *cnode2;
589 varpool_node *vnode;
590 symtab_node node2;
592 if (!node->analyzed && node->alias_target)
594 symtab_node tgt = symtab_node_for_asm (node->alias_target);
595 gcc_assert (node->weakref);
596 if (tgt)
597 symtab_resolve_alias (node, tgt);
599 node->aux = NULL;
601 if (!(cnode = dyn_cast <cgraph_node> (node))
602 || !cnode->clone_of
603 || cnode->clone_of->decl != cnode->decl)
605 /* Builtins are not merged via decl merging. It is however
606 possible that tree merging unified the declaration. We
607 do not want duplicate entries in symbol table. */
608 if (cnode && DECL_BUILT_IN (node->decl)
609 && (cnode2 = cgraph_get_node (node->decl))
610 && cnode2 != cnode)
611 lto_cgraph_replace_node (cnode2, cnode);
613 /* The user defined assembler variables are also not unified by their
614 symbol name (since it is irrelevant), but we need to unify symbol
615 nodes if tree merging occured. */
616 if ((vnode = dyn_cast <varpool_node> (node))
617 && DECL_HARD_REGISTER (vnode->decl)
618 && (node2 = symtab_get_node (vnode->decl))
619 && node2 != node)
620 lto_varpool_replace_node (dyn_cast <varpool_node> (node2),
621 vnode);
624 /* Abstract functions may have duplicated cgraph nodes attached;
625 remove them. */
626 else if (cnode && DECL_ABSTRACT (cnode->decl)
627 && (cnode2 = cgraph_get_node (node->decl))
628 && cnode2 != cnode)
629 cgraph_remove_node (cnode2);
631 symtab_insert_node_to_hashtable (node);
637 /* Given the decl DECL, return the prevailing decl with the same name. */
639 tree
640 lto_symtab_prevailing_decl (tree decl)
642 symtab_node ret;
644 /* Builtins and local symbols are their own prevailing decl. */
645 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
646 return decl;
648 /* DECL_ABSTRACTs are their own prevailng decl. */
649 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
650 return decl;
652 /* Likewise builtins are their own prevailing decl. This preserves
653 non-builtin vs. builtin uses from compile-time. */
654 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
655 return decl;
657 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
658 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
660 /* Walk through the list of candidates and return the one we merged to. */
661 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
662 if (!ret)
663 return decl;
665 return ret->decl;