* config/rs6000/rs6000.c (rs6000_option_override_internal): Do not
[official-gcc.git] / gcc / lto-symtab.c
blobcd8ca67b0c0a549c614f545c5b11a3a1a946d05d
1 /* LTO symbol table.
2 Copyright 2009, 2010 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,gc) *lto_global_var_decls;
35 /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
36 and read from FILE_DATA. */
38 void
39 lto_symtab_register_decl (tree decl,
40 ld_plugin_symbol_resolution_t resolution,
41 struct lto_file_decl_data *file_data)
43 symtab_node node;
45 /* Check that declarations reaching this function do not have
46 properties inconsistent with having external linkage. If any of
47 these asertions fail, then the object file reader has failed to
48 detect these cases and issue appropriate error messages. */
49 gcc_assert (decl
50 && TREE_PUBLIC (decl)
51 && (TREE_CODE (decl) == VAR_DECL
52 || TREE_CODE (decl) == FUNCTION_DECL)
53 && DECL_ASSEMBLER_NAME_SET_P (decl));
54 if (TREE_CODE (decl) == VAR_DECL
55 && DECL_INITIAL (decl))
56 gcc_assert (!DECL_EXTERNAL (decl)
57 || (TREE_STATIC (decl) && TREE_READONLY (decl)));
58 if (TREE_CODE (decl) == FUNCTION_DECL)
59 gcc_assert (!DECL_ABSTRACT (decl));
61 node = symtab_get_node (decl);
62 if (node)
64 node->symbol.resolution = resolution;
65 gcc_assert (node->symbol.lto_file_data == file_data);
69 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
70 all edges and removing the old node. */
72 static void
73 lto_cgraph_replace_node (struct cgraph_node *node,
74 struct cgraph_node *prevailing_node)
76 struct cgraph_edge *e, *next;
77 bool compatible_p;
79 if (cgraph_dump_file)
81 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
82 " for symbol %s\n",
83 xstrdup (cgraph_node_name (node)), node->uid,
84 xstrdup (cgraph_node_name (prevailing_node)),
85 prevailing_node->uid,
86 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
87 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->symbol.decl)))));
90 /* Merge node flags. */
91 if (node->symbol.force_output)
92 cgraph_mark_force_output_node (prevailing_node);
93 if (node->symbol.address_taken)
95 gcc_assert (!prevailing_node->global.inlined_to);
96 cgraph_mark_address_taken_node (prevailing_node);
99 /* Redirect all incoming edges. */
100 compatible_p
101 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->symbol.decl)),
102 TREE_TYPE (TREE_TYPE (node->symbol.decl)));
103 for (e = node->callers; e; e = next)
105 next = e->next_caller;
106 cgraph_redirect_edge_callee (e, prevailing_node);
107 /* If there is a mismatch between the supposed callee return type and
108 the real one do not attempt to inline this function.
109 ??? We really need a way to match function signatures for ABI
110 compatibility and perform related promotions at inlining time. */
111 if (!compatible_p)
112 e->call_stmt_cannot_inline_p = 1;
114 /* Redirect incomming references. */
115 ipa_clone_referring ((symtab_node)prevailing_node, &node->symbol.ref_list);
117 /* Finally remove the replaced node. */
118 cgraph_remove_node (node);
121 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
122 all edges and removing the old node. */
124 static void
125 lto_varpool_replace_node (struct varpool_node *vnode,
126 struct varpool_node *prevailing_node)
128 gcc_assert (!vnode->finalized || prevailing_node->finalized);
129 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
131 ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list);
133 /* Be sure we can garbage collect the initializer. */
134 if (DECL_INITIAL (vnode->symbol.decl))
135 DECL_INITIAL (vnode->symbol.decl) = error_mark_node;
136 /* Finally remove the replaced node. */
137 varpool_remove_node (vnode);
140 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
141 Return false if the symbols are not fully compatible and a diagnostic
142 should be emitted. */
144 static bool
145 lto_symtab_merge (symtab_node prevailing, symtab_node entry)
147 tree prevailing_decl = prevailing->symbol.decl;
148 tree decl = entry->symbol.decl;
149 tree prevailing_type, type;
151 if (prevailing_decl == decl)
152 return true;
154 /* Merge decl state in both directions, we may still end up using
155 the new decl. */
156 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
157 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
159 /* The linker may ask us to combine two incompatible symbols.
160 Detect this case and notify the caller of required diagnostics. */
162 if (TREE_CODE (decl) == FUNCTION_DECL)
164 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
165 TREE_TYPE (decl)))
166 /* If we don't have a merged type yet...sigh. The linker
167 wouldn't complain if the types were mismatched, so we
168 probably shouldn't either. Just use the type from
169 whichever decl appears to be associated with the
170 definition. If for some odd reason neither decl is, the
171 older one wins. */
172 (void) 0;
174 return true;
177 /* Now we exclusively deal with VAR_DECLs. */
179 /* Sharing a global symbol is a strong hint that two types are
180 compatible. We could use this information to complete
181 incomplete pointed-to types more aggressively here, ignoring
182 mismatches in both field and tag names. It's difficult though
183 to guarantee that this does not have side-effects on merging
184 more compatible types from other translation units though. */
186 /* We can tolerate differences in type qualification, the
187 qualification of the prevailing definition will prevail.
188 ??? In principle we might want to only warn for structurally
189 incompatible types here, but unless we have protective measures
190 for TBAA in place that would hide useful information. */
191 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
192 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
194 if (!types_compatible_p (prevailing_type, type))
196 if (COMPLETE_TYPE_P (type))
197 return false;
199 /* If type is incomplete then avoid warnings in the cases
200 that TBAA handles just fine. */
202 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
203 return false;
205 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
207 tree tem1 = TREE_TYPE (prevailing_type);
208 tree tem2 = TREE_TYPE (type);
209 while (TREE_CODE (tem1) == ARRAY_TYPE
210 && TREE_CODE (tem2) == ARRAY_TYPE)
212 tem1 = TREE_TYPE (tem1);
213 tem2 = TREE_TYPE (tem2);
216 if (TREE_CODE (tem1) != TREE_CODE (tem2))
217 return false;
219 if (!types_compatible_p (tem1, tem2))
220 return false;
223 /* Fallthru. Compatible enough. */
226 /* ??? We might want to emit a warning here if type qualification
227 differences were spotted. Do not do this unconditionally though. */
229 /* There is no point in comparing too many details of the decls here.
230 The type compatibility checks or the completing of types has properly
231 dealt with most issues. */
233 /* The following should all not invoke fatal errors as in non-LTO
234 mode the linker wouldn't complain either. Just emit warnings. */
236 /* Report a warning if user-specified alignments do not match. */
237 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
238 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
239 return false;
241 return true;
244 /* Return true if the symtab entry E can be replaced by another symtab
245 entry. */
247 static bool
248 lto_symtab_resolve_replaceable_p (symtab_node e)
250 if (DECL_EXTERNAL (e->symbol.decl)
251 || DECL_COMDAT (e->symbol.decl)
252 || DECL_ONE_ONLY (e->symbol.decl)
253 || DECL_WEAK (e->symbol.decl))
254 return true;
256 if (TREE_CODE (e->symbol.decl) == VAR_DECL)
257 return (DECL_COMMON (e->symbol.decl)
258 || (!flag_no_common && !DECL_INITIAL (e->symbol.decl)));
260 return false;
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 (!symtab_real_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->symbol.decl))
275 return false;
277 /* For functions we need a non-discarded body. */
278 if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL)
279 return (cgraph (e)->analyzed);
281 else if (TREE_CODE (e->symbol.decl) == VAR_DECL)
282 return varpool (e)->finalized;
284 gcc_unreachable ();
287 /* Resolve the symbol with the candidates in the chain *SLOT and store
288 their resolutions. */
290 static symtab_node
291 lto_symtab_resolve_symbols (symtab_node first)
293 symtab_node e;
294 symtab_node prevailing = NULL;
296 /* Always set e->node so that edges are updated to reflect decl merging. */
297 for (e = first; e; e = e->symbol.next_sharing_asm_name)
298 if (symtab_real_symbol_p (e)
299 && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
300 || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
301 || e->symbol.resolution == LDPR_PREVAILING_DEF))
303 prevailing = e;
304 break;
307 /* If the chain is already resolved there is nothing else to do. */
308 if (prevailing)
310 /* Assert it's the only one. */
311 for (e = prevailing->symbol.next_sharing_asm_name; e; e = e->symbol.next_sharing_asm_name)
312 if (symtab_real_symbol_p (e)
313 && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
314 || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
315 || e->symbol.resolution == LDPR_PREVAILING_DEF))
316 fatal_error ("multiple prevailing defs for %qE",
317 DECL_NAME (prevailing->symbol.decl));
318 return prevailing;
321 /* Find the single non-replaceable prevailing symbol and
322 diagnose ODR violations. */
323 for (e = first; e; e = e->symbol.next_sharing_asm_name)
325 if (!lto_symtab_resolve_can_prevail_p (e))
326 continue;
328 /* If we have a non-replaceable definition it prevails. */
329 if (!lto_symtab_resolve_replaceable_p (e))
331 if (prevailing)
333 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
334 "%qD has already been defined", e->symbol.decl);
335 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
336 "previously defined here");
338 prevailing = e;
341 if (prevailing)
342 return prevailing;
344 /* Do a second round choosing one from the replaceable prevailing decls. */
345 for (e = first; e; e = e->symbol.next_sharing_asm_name)
347 if (!lto_symtab_resolve_can_prevail_p (e)
348 || !symtab_real_symbol_p (e))
349 continue;
351 /* Choose the first function that can prevail as prevailing. */
352 if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL)
354 prevailing = e;
355 break;
358 /* From variables that can prevail choose the largest one. */
359 if (!prevailing
360 || tree_int_cst_lt (DECL_SIZE (prevailing->symbol.decl),
361 DECL_SIZE (e->symbol.decl))
362 /* When variables are equivalent try to chose one that has useful
363 DECL_INITIAL. This makes sense for keyed vtables that are
364 DECL_EXTERNAL but initialized. In units that do not need them
365 we replace the initializer by error_mark_node to conserve
366 memory.
368 We know that the vtable is keyed outside the LTO unit - otherwise
369 the keyed instance would prevail. We still can preserve useful
370 info in the initializer. */
371 || (DECL_SIZE (prevailing->symbol.decl) == DECL_SIZE (e->symbol.decl)
372 && (DECL_INITIAL (e->symbol.decl)
373 && DECL_INITIAL (e->symbol.decl) != error_mark_node)
374 && (!DECL_INITIAL (prevailing->symbol.decl)
375 || DECL_INITIAL (prevailing->symbol.decl) == error_mark_node)))
376 prevailing = e;
379 return prevailing;
382 /* Merge all decls in the symbol table chain to the prevailing decl and
383 issue diagnostics about type mismatches. If DIAGNOSED_P is true
384 do not issue further diagnostics.*/
386 static void
387 lto_symtab_merge_decls_2 (symtab_node first, bool diagnosed_p)
389 symtab_node prevailing, e;
390 VEC(tree, heap) *mismatches = NULL;
391 unsigned i;
392 tree decl;
394 /* Nothing to do for a single entry. */
395 prevailing = first;
396 if (!prevailing->symbol.next_sharing_asm_name)
397 return;
399 /* Try to merge each entry with the prevailing one. */
400 for (e = prevailing->symbol.next_sharing_asm_name;
401 e; e = e->symbol.next_sharing_asm_name)
403 if (!lto_symtab_merge (prevailing, e)
404 && !diagnosed_p)
405 VEC_safe_push (tree, heap, mismatches, e->symbol.decl);
407 if (VEC_empty (tree, mismatches))
408 return;
410 /* Diagnose all mismatched re-declarations. */
411 FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
413 if (!types_compatible_p (TREE_TYPE (prevailing->symbol.decl),
414 TREE_TYPE (decl)))
415 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
416 "type of %qD does not match original "
417 "declaration", decl);
419 else if ((DECL_USER_ALIGN (prevailing->symbol.decl)
420 && DECL_USER_ALIGN (decl))
421 && DECL_ALIGN (prevailing->symbol.decl) < DECL_ALIGN (decl))
423 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
424 "alignment of %qD is bigger than "
425 "original declaration", decl);
428 if (diagnosed_p)
429 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
430 "previously declared here");
432 VEC_free (tree, heap, mismatches);
435 /* Helper to process the decl chain for the symbol table entry *SLOT. */
437 static void
438 lto_symtab_merge_decls_1 (symtab_node first)
440 symtab_node e, prevailing;
441 bool diagnosed_p = false;
443 if (cgraph_dump_file)
445 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
446 symtab_node_asm_name (first));
447 for (e = first; e; e = e->symbol.next_sharing_asm_name)
448 dump_symtab_node (cgraph_dump_file, e);
451 /* Compute the symbol resolutions. This is a no-op when using the
452 linker plugin and resolution was decided by the linker. */
453 prevailing = lto_symtab_resolve_symbols (first);
455 /* If there's not a prevailing symbol yet it's an external reference.
456 Happens a lot during ltrans. Choose the first symbol with a
457 cgraph or a varpool node. */
458 if (!prevailing)
460 prevailing = first;
461 /* For variables chose with a priority variant with vnode
462 attached (i.e. from unit where external declaration of
463 variable is actually used).
464 When there are multiple variants, chose one with size.
465 This is needed for C++ typeinfos, for example in
466 lto/20081204-1 there are typeifos in both units, just
467 one of them do have size. */
468 if (TREE_CODE (prevailing->symbol.decl) == VAR_DECL)
470 for (e = prevailing->symbol.next_sharing_asm_name;
471 e; e = e->symbol.next_sharing_asm_name)
472 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->symbol.decl))
473 && COMPLETE_TYPE_P (TREE_TYPE (e->symbol.decl)))
474 prevailing = e;
478 symtab_prevail_in_asm_name_hash (prevailing);
480 /* Record the prevailing variable. */
481 if (TREE_CODE (prevailing->symbol.decl) == VAR_DECL)
482 VEC_safe_push (tree, gc, lto_global_var_decls,
483 prevailing->symbol.decl);
485 /* Diagnose mismatched objects. */
486 for (e = prevailing->symbol.next_sharing_asm_name;
487 e; e = e->symbol.next_sharing_asm_name)
489 if (TREE_CODE (prevailing->symbol.decl)
490 == TREE_CODE (e->symbol.decl))
491 continue;
493 switch (TREE_CODE (prevailing->symbol.decl))
495 case VAR_DECL:
496 gcc_assert (TREE_CODE (e->symbol.decl) == FUNCTION_DECL);
497 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
498 "variable %qD redeclared as function",
499 prevailing->symbol.decl);
500 break;
502 case FUNCTION_DECL:
503 gcc_assert (TREE_CODE (e->symbol.decl) == VAR_DECL);
504 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
505 "function %qD redeclared as variable",
506 prevailing->symbol.decl);
507 break;
509 default:
510 gcc_unreachable ();
513 diagnosed_p = true;
515 if (diagnosed_p)
516 inform (DECL_SOURCE_LOCATION (prevailing->symbol.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->symbol.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 /* In ltrans mode we read merged cgraph, we do not really need to care
539 about resolving symbols again, we only need to replace duplicated declarations
540 read from the callgraph and from function sections. */
541 if (flag_ltrans)
542 return;
544 /* Populate assembler name hash. */
545 symtab_initialize_asm_name_hash ();
547 FOR_EACH_SYMBOL (node)
548 if (TREE_PUBLIC (node->symbol.decl)
549 && node->symbol.next_sharing_asm_name
550 && !node->symbol.previous_sharing_asm_name)
551 lto_symtab_merge_decls_1 (node);
554 /* Helper to process the decl chain for the symbol table entry *SLOT. */
556 static void
557 lto_symtab_merge_cgraph_nodes_1 (symtab_node prevailing)
559 symtab_node e, next;
561 /* Replace the cgraph node of each entry with the prevailing one. */
562 for (e = prevailing->symbol.next_sharing_asm_name; e;
563 e = next)
565 next = e->symbol.next_sharing_asm_name;
567 if (!symtab_real_symbol_p (e))
568 continue;
569 if (symtab_function_p (e)
570 && !DECL_BUILT_IN (e->symbol.decl))
571 lto_cgraph_replace_node (cgraph (e), cgraph (prevailing));
572 if (symtab_variable_p (e))
573 lto_varpool_replace_node (varpool (e), 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_cgraph_nodes (void)
585 struct cgraph_node *cnode;
586 struct varpool_node *vnode;
587 symtab_node node;
589 /* Populate assembler name hash. */
590 symtab_initialize_asm_name_hash ();
592 if (!flag_ltrans)
593 FOR_EACH_SYMBOL (node)
594 if (TREE_PUBLIC (node->symbol.decl)
595 && node->symbol.next_sharing_asm_name
596 && !node->symbol.previous_sharing_asm_name)
597 lto_symtab_merge_cgraph_nodes_1 (node);
599 FOR_EACH_FUNCTION (cnode)
601 if ((cnode->thunk.thunk_p || cnode->alias)
602 && cnode->thunk.alias)
603 cnode->thunk.alias = lto_symtab_prevailing_decl (cnode->thunk.alias);
604 cnode->symbol.aux = NULL;
606 FOR_EACH_VARIABLE (vnode)
608 if (vnode->alias_of)
609 vnode->alias_of = lto_symtab_prevailing_decl (vnode->alias_of);
610 vnode->symbol.aux = NULL;
614 /* Given the decl DECL, return the prevailing decl with the same name. */
616 tree
617 lto_symtab_prevailing_decl (tree decl)
619 symtab_node ret;
621 /* Builtins and local symbols are their own prevailing decl. */
622 if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
623 return decl;
625 /* DECL_ABSTRACTs are their own prevailng decl. */
626 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
627 return decl;
629 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
630 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
632 /* Walk through the list of candidates and return the one we merged to. */
633 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
634 if (!ret)
635 return decl;
637 return ret->symbol.decl;