Make Linaro GCC4.9-2015.06.
[official-gcc.git] / gcc-4_9-branch / gcc / lto / lto-symtab.c
blobe77448e19285e9c5e8caadf5b0c3fb729fdb0ab0
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 for (prevailing = first;
457 prevailing; prevailing = prevailing->next_sharing_asm_name)
458 if (lto_symtab_symbol_p (prevailing))
459 break;
460 if (!prevailing)
461 return;
462 /* For variables chose with a priority variant with vnode
463 attached (i.e. from unit where external declaration of
464 variable is actually used).
465 When there are multiple variants, chose one with size.
466 This is needed for C++ typeinfos, for example in
467 lto/20081204-1 there are typeifos in both units, just
468 one of them do have size. */
469 if (TREE_CODE (prevailing->decl) == VAR_DECL)
471 for (e = prevailing->next_sharing_asm_name;
472 e; e = e->next_sharing_asm_name)
473 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
474 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
475 && lto_symtab_symbol_p (e))
476 prevailing = e;
478 /* For variables prefer the non-builtin if one is available. */
479 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
481 for (e = first; e; e = e->next_sharing_asm_name)
482 if (TREE_CODE (e->decl) == FUNCTION_DECL
483 && !DECL_BUILT_IN (e->decl)
484 && lto_symtab_symbol_p (e))
486 prevailing = e;
487 break;
492 symtab_prevail_in_asm_name_hash (prevailing);
494 /* Diagnose mismatched objects. */
495 for (e = prevailing->next_sharing_asm_name;
496 e; e = e->next_sharing_asm_name)
498 if (TREE_CODE (prevailing->decl)
499 == TREE_CODE (e->decl))
500 continue;
501 if (!lto_symtab_symbol_p (e))
502 continue;
504 switch (TREE_CODE (prevailing->decl))
506 case VAR_DECL:
507 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
508 error_at (DECL_SOURCE_LOCATION (e->decl),
509 "variable %qD redeclared as function",
510 prevailing->decl);
511 break;
513 case FUNCTION_DECL:
514 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
515 error_at (DECL_SOURCE_LOCATION (e->decl),
516 "function %qD redeclared as variable",
517 prevailing->decl);
518 break;
520 default:
521 gcc_unreachable ();
524 diagnosed_p = true;
526 if (diagnosed_p)
527 inform (DECL_SOURCE_LOCATION (prevailing->decl),
528 "previously declared here");
530 /* Merge the chain to the single prevailing decl and diagnose
531 mismatches. */
532 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
534 if (cgraph_dump_file)
536 fprintf (cgraph_dump_file, "After resolution:\n");
537 for (e = prevailing; e; e = e->next_sharing_asm_name)
538 dump_symtab_node (cgraph_dump_file, e);
542 /* Resolve and merge all symbol table chains to a prevailing decl. */
544 void
545 lto_symtab_merge_decls (void)
547 symtab_node *node;
549 /* Populate assembler name hash. */
550 symtab_initialize_asm_name_hash ();
552 FOR_EACH_SYMBOL (node)
553 if (!node->previous_sharing_asm_name
554 && node->next_sharing_asm_name)
555 lto_symtab_merge_decls_1 (node);
558 /* Helper to process the decl chain for the symbol table entry *SLOT. */
560 static void
561 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
563 symtab_node *e;
564 symtab_node *next;
566 /* Replace the cgraph node of each entry with the prevailing one. */
567 for (e = prevailing->next_sharing_asm_name; e;
568 e = next)
570 next = e->next_sharing_asm_name;
572 if (!lto_symtab_symbol_p (e))
573 continue;
574 cgraph_node *ce = dyn_cast <cgraph_node> (e);
575 if (ce && !DECL_BUILT_IN (e->decl))
576 lto_cgraph_replace_node (ce, cgraph (prevailing));
577 if (varpool_node *ve = dyn_cast <varpool_node> (e))
578 lto_varpool_replace_node (ve, varpool (prevailing));
581 return;
584 /* Merge cgraph nodes according to the symbol merging done by
585 lto_symtab_merge_decls. */
587 void
588 lto_symtab_merge_symbols (void)
590 symtab_node *node;
592 if (!flag_ltrans)
594 symtab_initialize_asm_name_hash ();
596 /* Do the actual merging.
597 At this point we invalidate hash translating decls into symtab nodes
598 because after removing one of duplicate decls the hash is not correcly
599 updated to the ohter dupliate. */
600 FOR_EACH_SYMBOL (node)
601 if (lto_symtab_symbol_p (node)
602 && node->next_sharing_asm_name
603 && !node->previous_sharing_asm_name)
604 lto_symtab_merge_symbols_1 (node);
606 /* Resolve weakref aliases whose target are now in the compilation unit.
607 also re-populate the hash translating decls into symtab nodes*/
608 FOR_EACH_SYMBOL (node)
610 cgraph_node *cnode, *cnode2;
611 varpool_node *vnode;
612 symtab_node *node2;
614 if (!node->analyzed && node->alias_target)
616 symtab_node *tgt = symtab_node_for_asm (node->alias_target);
617 gcc_assert (node->weakref);
618 if (tgt)
619 symtab_resolve_alias (node, tgt);
621 node->aux = NULL;
623 if (!(cnode = dyn_cast <cgraph_node> (node))
624 || !cnode->clone_of
625 || cnode->clone_of->decl != cnode->decl)
627 /* Builtins are not merged via decl merging. It is however
628 possible that tree merging unified the declaration. We
629 do not want duplicate entries in symbol table. */
630 if (cnode && DECL_BUILT_IN (node->decl)
631 && (cnode2 = cgraph_get_node (node->decl))
632 && cnode2 != cnode)
633 lto_cgraph_replace_node (cnode2, cnode);
635 /* The user defined assembler variables are also not unified by their
636 symbol name (since it is irrelevant), but we need to unify symbol
637 nodes if tree merging occured. */
638 if ((vnode = dyn_cast <varpool_node> (node))
639 && DECL_HARD_REGISTER (vnode->decl)
640 && (node2 = symtab_get_node (vnode->decl))
641 && node2 != node)
642 lto_varpool_replace_node (dyn_cast <varpool_node> (node2),
643 vnode);
646 /* Abstract functions may have duplicated cgraph nodes attached;
647 remove them. */
648 else if (cnode && DECL_ABSTRACT (cnode->decl)
649 && (cnode2 = cgraph_get_node (node->decl))
650 && cnode2 != cnode)
651 cgraph_remove_node (cnode2);
653 symtab_insert_node_to_hashtable (node);
659 /* Given the decl DECL, return the prevailing decl with the same name. */
661 tree
662 lto_symtab_prevailing_decl (tree decl)
664 symtab_node *ret;
666 /* Builtins and local symbols are their own prevailing decl. */
667 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
668 return decl;
670 /* DECL_ABSTRACTs are their own prevailng decl. */
671 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
672 return decl;
674 /* Likewise builtins are their own prevailing decl. This preserves
675 non-builtin vs. builtin uses from compile-time. */
676 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
677 return decl;
679 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
680 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
682 /* Walk through the list of candidates and return the one we merged to. */
683 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
684 if (!ret)
685 return decl;
687 return ret->decl;