2013-07-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / lto-symtab.c
blobf9bf37c7005bead7f3be0f121c7d1ffd58c59756
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"
32 /* Vector to keep track of external variables we've seen so far. */
33 vec<tree, va_gc> *lto_global_var_decls;
35 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
36 all edges and removing the old node. */
38 static void
39 lto_cgraph_replace_node (struct cgraph_node *node,
40 struct cgraph_node *prevailing_node)
42 struct cgraph_edge *e, *next;
43 bool compatible_p;
45 if (cgraph_dump_file)
47 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
48 " for symbol %s\n",
49 cgraph_node_name (node), node->symbol.order,
50 cgraph_node_name (prevailing_node),
51 prevailing_node->symbol.order,
52 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
53 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl)))));
56 /* Merge node flags. */
57 if (node->symbol.force_output)
58 cgraph_mark_force_output_node (prevailing_node);
59 if (node->symbol.address_taken)
61 gcc_assert (!prevailing_node->global.inlined_to);
62 cgraph_mark_address_taken_node (prevailing_node);
65 /* Redirect all incoming edges. */
66 compatible_p
67 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->symbol.decl)),
68 TREE_TYPE (TREE_TYPE (node->symbol.decl)));
69 for (e = node->callers; e; e = next)
71 next = e->next_caller;
72 cgraph_redirect_edge_callee (e, prevailing_node);
73 /* If there is a mismatch between the supposed callee return type and
74 the real one do not attempt to inline this function.
75 ??? We really need a way to match function signatures for ABI
76 compatibility and perform related promotions at inlining time. */
77 if (!compatible_p)
78 e->call_stmt_cannot_inline_p = 1;
80 /* Redirect incomming references. */
81 ipa_clone_referring ((symtab_node)prevailing_node, &node->symbol.ref_list);
83 if (node->symbol.decl != prevailing_node->symbol.decl)
84 cgraph_release_function_body (node);
86 /* Finally remove the replaced node. */
87 cgraph_remove_node (node);
90 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
91 all edges and removing the old node. */
93 static void
94 lto_varpool_replace_node (struct varpool_node *vnode,
95 struct varpool_node *prevailing_node)
97 gcc_assert (!vnode->symbol.definition || prevailing_node->symbol.definition);
98 gcc_assert (!vnode->symbol.analyzed || prevailing_node->symbol.analyzed);
100 ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list);
102 /* Be sure we can garbage collect the initializer. */
103 if (DECL_INITIAL (vnode->symbol.decl)
104 && vnode->symbol.decl != prevailing_node->symbol.decl)
105 DECL_INITIAL (vnode->symbol.decl) = error_mark_node;
106 /* Finally remove the replaced node. */
107 varpool_remove_node (vnode);
110 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
111 Return false if the symbols are not fully compatible and a diagnostic
112 should be emitted. */
114 static bool
115 lto_symtab_merge (symtab_node prevailing, symtab_node entry)
117 tree prevailing_decl = prevailing->symbol.decl;
118 tree decl = entry->symbol.decl;
119 tree prevailing_type, type;
121 if (prevailing_decl == decl)
122 return true;
124 /* Merge decl state in both directions, we may still end up using
125 the new decl. */
126 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
127 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
129 /* The linker may ask us to combine two incompatible symbols.
130 Detect this case and notify the caller of required diagnostics. */
132 if (TREE_CODE (decl) == FUNCTION_DECL)
134 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
135 TREE_TYPE (decl)))
136 /* If we don't have a merged type yet...sigh. The linker
137 wouldn't complain if the types were mismatched, so we
138 probably shouldn't either. Just use the type from
139 whichever decl appears to be associated with the
140 definition. If for some odd reason neither decl is, the
141 older one wins. */
142 (void) 0;
144 return true;
147 /* Now we exclusively deal with VAR_DECLs. */
149 /* Sharing a global symbol is a strong hint that two types are
150 compatible. We could use this information to complete
151 incomplete pointed-to types more aggressively here, ignoring
152 mismatches in both field and tag names. It's difficult though
153 to guarantee that this does not have side-effects on merging
154 more compatible types from other translation units though. */
156 /* We can tolerate differences in type qualification, the
157 qualification of the prevailing definition will prevail.
158 ??? In principle we might want to only warn for structurally
159 incompatible types here, but unless we have protective measures
160 for TBAA in place that would hide useful information. */
161 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
162 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
164 if (!types_compatible_p (prevailing_type, type))
166 if (COMPLETE_TYPE_P (type))
167 return false;
169 /* If type is incomplete then avoid warnings in the cases
170 that TBAA handles just fine. */
172 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
173 return false;
175 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
177 tree tem1 = TREE_TYPE (prevailing_type);
178 tree tem2 = TREE_TYPE (type);
179 while (TREE_CODE (tem1) == ARRAY_TYPE
180 && TREE_CODE (tem2) == ARRAY_TYPE)
182 tem1 = TREE_TYPE (tem1);
183 tem2 = TREE_TYPE (tem2);
186 if (TREE_CODE (tem1) != TREE_CODE (tem2))
187 return false;
189 if (!types_compatible_p (tem1, tem2))
190 return false;
193 /* Fallthru. Compatible enough. */
196 /* ??? We might want to emit a warning here if type qualification
197 differences were spotted. Do not do this unconditionally though. */
199 /* There is no point in comparing too many details of the decls here.
200 The type compatibility checks or the completing of types has properly
201 dealt with most issues. */
203 /* The following should all not invoke fatal errors as in non-LTO
204 mode the linker wouldn't complain either. Just emit warnings. */
206 /* Report a warning if user-specified alignments do not match. */
207 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
208 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
209 return false;
211 return true;
214 /* Return true if the symtab entry E can be replaced by another symtab
215 entry. */
217 static bool
218 lto_symtab_resolve_replaceable_p (symtab_node e)
220 if (DECL_EXTERNAL (e->symbol.decl)
221 || DECL_COMDAT (e->symbol.decl)
222 || DECL_ONE_ONLY (e->symbol.decl)
223 || DECL_WEAK (e->symbol.decl))
224 return true;
226 if (TREE_CODE (e->symbol.decl) == VAR_DECL)
227 return (DECL_COMMON (e->symbol.decl)
228 || (!flag_no_common && !DECL_INITIAL (e->symbol.decl)));
230 return false;
233 /* Return true, if the symbol E should be resolved by lto-symtab.
234 Those are all external symbols and all real symbols that are not static (we
235 handle renaming of static later in partitioning). */
237 static bool
238 lto_symtab_symbol_p (symtab_node e)
240 if (!TREE_PUBLIC (e->symbol.decl) && !DECL_EXTERNAL (e->symbol.decl))
241 return false;
242 return symtab_real_symbol_p (e);
245 /* Return true if the symtab entry E can be the prevailing one. */
247 static bool
248 lto_symtab_resolve_can_prevail_p (symtab_node e)
250 if (!lto_symtab_symbol_p (e))
251 return false;
253 /* The C++ frontend ends up neither setting TREE_STATIC nor
254 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
255 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
256 if (DECL_EXTERNAL (e->symbol.decl))
257 return false;
259 return e->symbol.definition;
262 /* Resolve the symbol with the candidates in the chain *SLOT and store
263 their resolutions. */
265 static symtab_node
266 lto_symtab_resolve_symbols (symtab_node first)
268 symtab_node e;
269 symtab_node prevailing = NULL;
271 /* Always set e->node so that edges are updated to reflect decl merging. */
272 for (e = first; e; e = e->symbol.next_sharing_asm_name)
273 if (lto_symtab_symbol_p (e)
274 && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
275 || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
276 || e->symbol.resolution == LDPR_PREVAILING_DEF))
278 prevailing = e;
279 break;
282 /* If the chain is already resolved there is nothing else to do. */
283 if (prevailing)
285 /* Assert it's the only one. */
286 for (e = prevailing->symbol.next_sharing_asm_name; e; e = e->symbol.next_sharing_asm_name)
287 if (lto_symtab_symbol_p (e)
288 && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
289 || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
290 || e->symbol.resolution == LDPR_PREVAILING_DEF))
291 fatal_error ("multiple prevailing defs for %qE",
292 DECL_NAME (prevailing->symbol.decl));
293 return prevailing;
296 /* Find the single non-replaceable prevailing symbol and
297 diagnose ODR violations. */
298 for (e = first; e; e = e->symbol.next_sharing_asm_name)
300 if (!lto_symtab_resolve_can_prevail_p (e))
301 continue;
303 /* If we have a non-replaceable definition it prevails. */
304 if (!lto_symtab_resolve_replaceable_p (e))
306 if (prevailing)
308 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
309 "%qD has already been defined", e->symbol.decl);
310 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
311 "previously defined here");
313 prevailing = e;
316 if (prevailing)
317 return prevailing;
319 /* Do a second round choosing one from the replaceable prevailing decls. */
320 for (e = first; e; e = e->symbol.next_sharing_asm_name)
322 if (!lto_symtab_resolve_can_prevail_p (e))
323 continue;
325 /* Choose the first function that can prevail as prevailing. */
326 if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL)
328 prevailing = e;
329 break;
332 /* From variables that can prevail choose the largest one. */
333 if (!prevailing
334 || tree_int_cst_lt (DECL_SIZE (prevailing->symbol.decl),
335 DECL_SIZE (e->symbol.decl))
336 /* When variables are equivalent try to chose one that has useful
337 DECL_INITIAL. This makes sense for keyed vtables that are
338 DECL_EXTERNAL but initialized. In units that do not need them
339 we replace the initializer by error_mark_node to conserve
340 memory.
342 We know that the vtable is keyed outside the LTO unit - otherwise
343 the keyed instance would prevail. We still can preserve useful
344 info in the initializer. */
345 || (DECL_SIZE (prevailing->symbol.decl) == DECL_SIZE (e->symbol.decl)
346 && (DECL_INITIAL (e->symbol.decl)
347 && DECL_INITIAL (e->symbol.decl) != error_mark_node)
348 && (!DECL_INITIAL (prevailing->symbol.decl)
349 || DECL_INITIAL (prevailing->symbol.decl) == error_mark_node)))
350 prevailing = e;
353 return prevailing;
356 /* Merge all decls in the symbol table chain to the prevailing decl and
357 issue diagnostics about type mismatches. If DIAGNOSED_P is true
358 do not issue further diagnostics.*/
360 static void
361 lto_symtab_merge_decls_2 (symtab_node first, bool diagnosed_p)
363 symtab_node prevailing, e;
364 vec<tree> mismatches = vNULL;
365 unsigned i;
366 tree decl;
368 /* Nothing to do for a single entry. */
369 prevailing = first;
370 if (!prevailing->symbol.next_sharing_asm_name)
371 return;
373 /* Try to merge each entry with the prevailing one. */
374 for (e = prevailing->symbol.next_sharing_asm_name;
375 e; e = e->symbol.next_sharing_asm_name)
376 if (TREE_PUBLIC (e->symbol.decl))
378 if (!lto_symtab_merge (prevailing, e)
379 && !diagnosed_p)
380 mismatches.safe_push (e->symbol.decl);
382 if (mismatches.is_empty ())
383 return;
385 /* Diagnose all mismatched re-declarations. */
386 FOR_EACH_VEC_ELT (mismatches, i, decl)
388 if (!types_compatible_p (TREE_TYPE (prevailing->symbol.decl),
389 TREE_TYPE (decl)))
390 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
391 "type of %qD does not match original "
392 "declaration", decl);
394 else if ((DECL_USER_ALIGN (prevailing->symbol.decl)
395 && DECL_USER_ALIGN (decl))
396 && DECL_ALIGN (prevailing->symbol.decl) < DECL_ALIGN (decl))
398 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
399 "alignment of %qD is bigger than "
400 "original declaration", decl);
403 if (diagnosed_p)
404 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
405 "previously declared here");
407 mismatches.release ();
410 /* Helper to process the decl chain for the symbol table entry *SLOT. */
412 static void
413 lto_symtab_merge_decls_1 (symtab_node first)
415 symtab_node e, prevailing;
416 bool diagnosed_p = false;
418 if (cgraph_dump_file)
420 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
421 symtab_node_asm_name (first));
422 for (e = first; e; e = e->symbol.next_sharing_asm_name)
423 if (TREE_PUBLIC (e->symbol.decl))
424 dump_symtab_node (cgraph_dump_file, e);
427 /* Compute the symbol resolutions. This is a no-op when using the
428 linker plugin and resolution was decided by the linker. */
429 prevailing = lto_symtab_resolve_symbols (first);
431 /* If there's not a prevailing symbol yet it's an external reference.
432 Happens a lot during ltrans. Choose the first symbol with a
433 cgraph or a varpool node. */
434 if (!prevailing)
436 prevailing = first;
437 /* For variables chose with a priority variant with vnode
438 attached (i.e. from unit where external declaration of
439 variable is actually used).
440 When there are multiple variants, chose one with size.
441 This is needed for C++ typeinfos, for example in
442 lto/20081204-1 there are typeifos in both units, just
443 one of them do have size. */
444 if (TREE_CODE (prevailing->symbol.decl) == VAR_DECL)
446 for (e = prevailing->symbol.next_sharing_asm_name;
447 e; e = e->symbol.next_sharing_asm_name)
448 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->symbol.decl))
449 && COMPLETE_TYPE_P (TREE_TYPE (e->symbol.decl))
450 && lto_symtab_symbol_p (e))
451 prevailing = e;
453 /* For variables prefer the non-builtin if one is available. */
454 else if (TREE_CODE (prevailing->symbol.decl) == FUNCTION_DECL)
456 for (e = first; e; e = e->symbol.next_sharing_asm_name)
457 if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL
458 && !DECL_BUILT_IN (e->symbol.decl)
459 && lto_symtab_symbol_p (e))
461 prevailing = e;
462 break;
467 symtab_prevail_in_asm_name_hash (prevailing);
469 /* Diagnose mismatched objects. */
470 for (e = prevailing->symbol.next_sharing_asm_name;
471 e; e = e->symbol.next_sharing_asm_name)
473 if (TREE_CODE (prevailing->symbol.decl)
474 == TREE_CODE (e->symbol.decl))
475 continue;
476 if (!lto_symtab_symbol_p (e))
477 continue;
479 switch (TREE_CODE (prevailing->symbol.decl))
481 case VAR_DECL:
482 gcc_assert (TREE_CODE (e->symbol.decl) == FUNCTION_DECL);
483 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
484 "variable %qD redeclared as function",
485 prevailing->symbol.decl);
486 break;
488 case FUNCTION_DECL:
489 gcc_assert (TREE_CODE (e->symbol.decl) == VAR_DECL);
490 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
491 "function %qD redeclared as variable",
492 prevailing->symbol.decl);
493 break;
495 default:
496 gcc_unreachable ();
499 diagnosed_p = true;
501 if (diagnosed_p)
502 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
503 "previously declared here");
505 /* Merge the chain to the single prevailing decl and diagnose
506 mismatches. */
507 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
509 if (cgraph_dump_file)
511 fprintf (cgraph_dump_file, "After resolution:\n");
512 for (e = prevailing; e; e = e->symbol.next_sharing_asm_name)
513 dump_symtab_node (cgraph_dump_file, e);
517 /* Resolve and merge all symbol table chains to a prevailing decl. */
519 void
520 lto_symtab_merge_decls (void)
522 symtab_node node;
524 /* Populate assembler name hash. */
525 symtab_initialize_asm_name_hash ();
527 FOR_EACH_SYMBOL (node)
528 if (!node->symbol.previous_sharing_asm_name
529 && node->symbol.next_sharing_asm_name)
530 lto_symtab_merge_decls_1 (node);
533 /* Helper to process the decl chain for the symbol table entry *SLOT. */
535 static void
536 lto_symtab_merge_symbols_1 (symtab_node prevailing)
538 symtab_node e, next;
540 /* Replace the cgraph node of each entry with the prevailing one. */
541 for (e = prevailing->symbol.next_sharing_asm_name; e;
542 e = next)
544 next = e->symbol.next_sharing_asm_name;
546 if (!lto_symtab_symbol_p (e))
547 continue;
548 cgraph_node *ce = dyn_cast <cgraph_node> (e);
549 if (ce && !DECL_BUILT_IN (e->symbol.decl))
550 lto_cgraph_replace_node (ce, cgraph (prevailing));
551 if (varpool_node *ve = dyn_cast <varpool_node> (e))
552 lto_varpool_replace_node (ve, varpool (prevailing));
555 return;
558 /* Merge cgraph nodes according to the symbol merging done by
559 lto_symtab_merge_decls. */
561 void
562 lto_symtab_merge_symbols (void)
564 symtab_node node;
566 if (!flag_ltrans)
568 symtab_initialize_asm_name_hash ();
570 /* Do the actual merging.
571 At this point we invalidate hash translating decls into symtab nodes
572 because after removing one of duplicate decls the hash is not correcly
573 updated to the ohter dupliate. */
574 FOR_EACH_SYMBOL (node)
575 if (lto_symtab_symbol_p (node)
576 && node->symbol.next_sharing_asm_name
577 && !node->symbol.previous_sharing_asm_name)
578 lto_symtab_merge_symbols_1 (node);
580 /* Resolve weakref aliases whose target are now in the compilation unit.
581 also re-populate the hash translating decls into symtab nodes*/
582 FOR_EACH_SYMBOL (node)
584 cgraph_node *cnode, *cnode2;
585 if (!node->symbol.analyzed && node->symbol.alias_target)
587 symtab_node tgt = symtab_node_for_asm (node->symbol.alias_target);
588 gcc_assert (node->symbol.weakref);
589 if (tgt)
590 symtab_resolve_alias (node, tgt);
592 node->symbol.aux = NULL;
594 if (!(cnode = dyn_cast <cgraph_node> (node))
595 || !cnode->clone_of
596 || cnode->clone_of->symbol.decl != cnode->symbol.decl)
598 if (cnode && DECL_BUILT_IN (node->symbol.decl)
599 && (cnode2 = cgraph_get_node (node->symbol.decl))
600 && cnode2 != cnode)
601 lto_cgraph_replace_node (cnode2, cnode);
602 symtab_insert_node_to_hashtable ((symtab_node)node);
608 /* Given the decl DECL, return the prevailing decl with the same name. */
610 tree
611 lto_symtab_prevailing_decl (tree decl)
613 symtab_node ret;
615 /* Builtins and local symbols are their own prevailing decl. */
616 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
617 return decl;
619 /* DECL_ABSTRACTs are their own prevailng decl. */
620 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
621 return decl;
623 /* Likewise builtins are their own prevailing decl. This preserves
624 non-builtin vs. builtin uses from compile-time. */
625 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
626 return decl;
628 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
629 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
631 /* Walk through the list of candidates and return the one we merged to. */
632 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
633 if (!ret)
634 return decl;
636 return ret->symbol.decl;