* cgraph.h: Flatten. Remove all include files.
[official-gcc.git] / gcc / lto / lto-symtab.c
blob4c4e48af3341e711dc8a2a9b6f8701f30722f2a8
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 "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "plugin-api.h"
42 #include "hash-map.h"
43 #include "ipa-ref.h"
44 #include "cgraph.h"
45 #include "lto-streamer.h"
46 #include "ipa-utils.h"
47 #include "alloc-pool.h"
48 #include "ipa-prop.h"
49 #include "ipa-inline.h"
50 #include "builtins.h"
52 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
53 all edges and removing the old node. */
55 static void
56 lto_cgraph_replace_node (struct cgraph_node *node,
57 struct cgraph_node *prevailing_node)
59 struct cgraph_edge *e, *next;
60 bool compatible_p;
62 if (symtab->dump_file)
64 fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
65 " for symbol %s\n",
66 node->name (), node->order,
67 prevailing_node->name (),
68 prevailing_node->order,
69 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
70 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
73 /* Merge node flags. */
74 if (node->force_output)
75 prevailing_node->mark_force_output ();
76 if (node->forced_by_abi)
77 prevailing_node->forced_by_abi = true;
78 if (node->address_taken)
80 gcc_assert (!prevailing_node->global.inlined_to);
81 prevailing_node->mark_address_taken ();
84 /* Redirect all incoming edges. */
85 compatible_p
86 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
87 TREE_TYPE (TREE_TYPE (node->decl)));
88 for (e = node->callers; e; e = next)
90 next = e->next_caller;
91 e->redirect_callee (prevailing_node);
92 /* If there is a mismatch between the supposed callee return type and
93 the real one do not attempt to inline this function.
94 ??? We really need a way to match function signatures for ABI
95 compatibility and perform related promotions at inlining time. */
96 if (!compatible_p)
97 e->call_stmt_cannot_inline_p = 1;
99 /* Redirect incomming references. */
100 prevailing_node->clone_referring (node);
102 ipa_merge_profiles (prevailing_node, node);
103 lto_free_function_in_decl_state_for_node (node);
105 if (node->decl != prevailing_node->decl)
106 node->release_body ();
108 /* Finally remove the replaced node. */
109 node->remove ();
112 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
113 all edges and removing the old node. */
115 static void
116 lto_varpool_replace_node (varpool_node *vnode,
117 varpool_node *prevailing_node)
119 gcc_assert (!vnode->definition || prevailing_node->definition);
120 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
122 prevailing_node->clone_referring (vnode);
123 if (vnode->force_output)
124 prevailing_node->force_output = true;
125 if (vnode->forced_by_abi)
126 prevailing_node->forced_by_abi = true;
128 /* Be sure we can garbage collect the initializer. */
129 if (DECL_INITIAL (vnode->decl)
130 && vnode->decl != prevailing_node->decl)
131 DECL_INITIAL (vnode->decl) = error_mark_node;
133 /* Check and report ODR violations on virtual tables. */
134 if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
135 compare_virtual_tables (prevailing_node, vnode);
137 if (vnode->tls_model != prevailing_node->tls_model)
139 error_at (DECL_SOURCE_LOCATION (vnode->decl),
140 "%qD is defined as %s", vnode->decl, tls_model_names [vnode->tls_model]);
141 inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
142 "previously defined here as %s",
143 tls_model_names [prevailing_node->tls_model]);
145 /* Finally remove the replaced node. */
146 vnode->remove ();
149 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
150 Return false if the symbols are not fully compatible and a diagnostic
151 should be emitted. */
153 static bool
154 lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
156 tree prevailing_decl = prevailing->decl;
157 tree decl = entry->decl;
158 tree prevailing_type, type;
160 if (prevailing_decl == decl)
161 return true;
163 /* Merge decl state in both directions, we may still end up using
164 the new decl. */
165 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
166 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
168 /* The linker may ask us to combine two incompatible symbols.
169 Detect this case and notify the caller of required diagnostics. */
171 if (TREE_CODE (decl) == FUNCTION_DECL)
173 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
174 TREE_TYPE (decl)))
175 /* If we don't have a merged type yet...sigh. The linker
176 wouldn't complain if the types were mismatched, so we
177 probably shouldn't either. Just use the type from
178 whichever decl appears to be associated with the
179 definition. If for some odd reason neither decl is, the
180 older one wins. */
181 (void) 0;
183 return true;
186 /* Now we exclusively deal with VAR_DECLs. */
188 /* Sharing a global symbol is a strong hint that two types are
189 compatible. We could use this information to complete
190 incomplete pointed-to types more aggressively here, ignoring
191 mismatches in both field and tag names. It's difficult though
192 to guarantee that this does not have side-effects on merging
193 more compatible types from other translation units though. */
195 /* We can tolerate differences in type qualification, the
196 qualification of the prevailing definition will prevail.
197 ??? In principle we might want to only warn for structurally
198 incompatible types here, but unless we have protective measures
199 for TBAA in place that would hide useful information. */
200 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
201 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
203 if (!types_compatible_p (prevailing_type, type))
205 if (COMPLETE_TYPE_P (type))
206 return false;
208 /* If type is incomplete then avoid warnings in the cases
209 that TBAA handles just fine. */
211 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
212 return false;
214 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
216 tree tem1 = TREE_TYPE (prevailing_type);
217 tree tem2 = TREE_TYPE (type);
218 while (TREE_CODE (tem1) == ARRAY_TYPE
219 && TREE_CODE (tem2) == ARRAY_TYPE)
221 tem1 = TREE_TYPE (tem1);
222 tem2 = TREE_TYPE (tem2);
225 if (TREE_CODE (tem1) != TREE_CODE (tem2))
226 return false;
228 if (!types_compatible_p (tem1, tem2))
229 return false;
232 /* Fallthru. Compatible enough. */
235 /* ??? We might want to emit a warning here if type qualification
236 differences were spotted. Do not do this unconditionally though. */
238 /* There is no point in comparing too many details of the decls here.
239 The type compatibility checks or the completing of types has properly
240 dealt with most issues. */
242 /* The following should all not invoke fatal errors as in non-LTO
243 mode the linker wouldn't complain either. Just emit warnings. */
245 /* Report a warning if user-specified alignments do not match. */
246 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
247 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
248 return false;
250 return true;
253 /* Return true if the symtab entry E can be replaced by another symtab
254 entry. */
256 static bool
257 lto_symtab_resolve_replaceable_p (symtab_node *e)
259 if (DECL_EXTERNAL (e->decl)
260 || DECL_COMDAT (e->decl)
261 || DECL_ONE_ONLY (e->decl)
262 || DECL_WEAK (e->decl))
263 return true;
265 if (TREE_CODE (e->decl) == VAR_DECL)
266 return (DECL_COMMON (e->decl)
267 || (!flag_no_common && !DECL_INITIAL (e->decl)));
269 return false;
272 /* Return true, if the symbol E should be resolved by lto-symtab.
273 Those are all external symbols and all real symbols that are not static (we
274 handle renaming of static later in partitioning). */
276 static bool
277 lto_symtab_symbol_p (symtab_node *e)
279 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
280 return false;
281 return e->real_symbol_p ();
284 /* Return true if the symtab entry E can be the prevailing one. */
286 static bool
287 lto_symtab_resolve_can_prevail_p (symtab_node *e)
289 if (!lto_symtab_symbol_p (e))
290 return false;
292 /* The C++ frontend ends up neither setting TREE_STATIC nor
293 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
294 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
295 if (DECL_EXTERNAL (e->decl))
296 return false;
298 return e->definition;
301 /* Resolve the symbol with the candidates in the chain *SLOT and store
302 their resolutions. */
304 static symtab_node *
305 lto_symtab_resolve_symbols (symtab_node *first)
307 symtab_node *e;
308 symtab_node *prevailing = NULL;
310 /* Always set e->node so that edges are updated to reflect decl merging. */
311 for (e = first; e; e = e->next_sharing_asm_name)
312 if (lto_symtab_symbol_p (e)
313 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
314 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
315 || e->resolution == LDPR_PREVAILING_DEF))
317 prevailing = e;
318 break;
321 /* If the chain is already resolved there is nothing else to do. */
322 if (prevailing)
324 /* Assert it's the only one. */
325 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
326 if (lto_symtab_symbol_p (e)
327 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
328 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
329 || e->resolution == LDPR_PREVAILING_DEF))
330 fatal_error ("multiple prevailing defs for %qE",
331 DECL_NAME (prevailing->decl));
332 return prevailing;
335 /* Find the single non-replaceable prevailing symbol and
336 diagnose ODR violations. */
337 for (e = first; e; e = e->next_sharing_asm_name)
339 if (!lto_symtab_resolve_can_prevail_p (e))
340 continue;
342 /* If we have a non-replaceable definition it prevails. */
343 if (!lto_symtab_resolve_replaceable_p (e))
345 if (prevailing)
347 error_at (DECL_SOURCE_LOCATION (e->decl),
348 "%qD has already been defined", e->decl);
349 inform (DECL_SOURCE_LOCATION (prevailing->decl),
350 "previously defined here");
352 prevailing = e;
355 if (prevailing)
356 return prevailing;
358 /* Do a second round choosing one from the replaceable prevailing decls. */
359 for (e = first; e; e = e->next_sharing_asm_name)
361 if (!lto_symtab_resolve_can_prevail_p (e))
362 continue;
364 /* Choose the first function that can prevail as prevailing. */
365 if (TREE_CODE (e->decl) == FUNCTION_DECL)
367 prevailing = e;
368 break;
371 /* From variables that can prevail choose the largest one. */
372 if (!prevailing
373 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
374 DECL_SIZE (e->decl))
375 /* When variables are equivalent try to chose one that has useful
376 DECL_INITIAL. This makes sense for keyed vtables that are
377 DECL_EXTERNAL but initialized. In units that do not need them
378 we replace the initializer by error_mark_node to conserve
379 memory.
381 We know that the vtable is keyed outside the LTO unit - otherwise
382 the keyed instance would prevail. We still can preserve useful
383 info in the initializer. */
384 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
385 && (DECL_INITIAL (e->decl)
386 && DECL_INITIAL (e->decl) != error_mark_node)
387 && (!DECL_INITIAL (prevailing->decl)
388 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
389 prevailing = e;
392 return prevailing;
395 /* Merge all decls in the symbol table chain to the prevailing decl and
396 issue diagnostics about type mismatches. If DIAGNOSED_P is true
397 do not issue further diagnostics.*/
399 static void
400 lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
402 symtab_node *prevailing;
403 symtab_node *e;
404 vec<tree> mismatches = vNULL;
405 unsigned i;
406 tree decl;
408 /* Nothing to do for a single entry. */
409 prevailing = first;
410 if (!prevailing->next_sharing_asm_name)
411 return;
413 /* Try to merge each entry with the prevailing one. */
414 for (e = prevailing->next_sharing_asm_name;
415 e; e = e->next_sharing_asm_name)
416 if (TREE_PUBLIC (e->decl))
418 if (!lto_symtab_merge (prevailing, e)
419 && !diagnosed_p)
420 mismatches.safe_push (e->decl);
422 if (mismatches.is_empty ())
423 return;
425 /* Diagnose all mismatched re-declarations. */
426 FOR_EACH_VEC_ELT (mismatches, i, decl)
428 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
429 TREE_TYPE (decl)))
430 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
431 "type of %qD does not match original "
432 "declaration", decl);
434 else if ((DECL_USER_ALIGN (prevailing->decl)
435 && DECL_USER_ALIGN (decl))
436 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
438 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
439 "alignment of %qD is bigger than "
440 "original declaration", decl);
443 if (diagnosed_p)
444 inform (DECL_SOURCE_LOCATION (prevailing->decl),
445 "previously declared here");
447 mismatches.release ();
450 /* Helper to process the decl chain for the symbol table entry *SLOT. */
452 static void
453 lto_symtab_merge_decls_1 (symtab_node *first)
455 symtab_node *e;
456 symtab_node *prevailing;
457 bool diagnosed_p = false;
459 if (symtab->dump_file)
461 fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
462 first->asm_name ());
463 for (e = first; e; e = e->next_sharing_asm_name)
464 if (TREE_PUBLIC (e->decl))
465 e->dump (symtab->dump_file);
468 /* Compute the symbol resolutions. This is a no-op when using the
469 linker plugin and resolution was decided by the linker. */
470 prevailing = lto_symtab_resolve_symbols (first);
472 /* If there's not a prevailing symbol yet it's an external reference.
473 Happens a lot during ltrans. Choose the first symbol with a
474 cgraph or a varpool node. */
475 if (!prevailing)
477 for (prevailing = first;
478 prevailing; prevailing = prevailing->next_sharing_asm_name)
479 if (lto_symtab_symbol_p (prevailing))
480 break;
481 if (!prevailing)
482 return;
483 /* For variables chose with a priority variant with vnode
484 attached (i.e. from unit where external declaration of
485 variable is actually used).
486 When there are multiple variants, chose one with size.
487 This is needed for C++ typeinfos, for example in
488 lto/20081204-1 there are typeifos in both units, just
489 one of them do have size. */
490 if (TREE_CODE (prevailing->decl) == VAR_DECL)
492 for (e = prevailing->next_sharing_asm_name;
493 e; e = e->next_sharing_asm_name)
494 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
495 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
496 && lto_symtab_symbol_p (e))
497 prevailing = e;
499 /* For variables prefer the non-builtin if one is available. */
500 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
502 for (e = first; e; e = e->next_sharing_asm_name)
503 if (TREE_CODE (e->decl) == FUNCTION_DECL
504 && !DECL_BUILT_IN (e->decl)
505 && lto_symtab_symbol_p (e))
507 prevailing = e;
508 break;
513 symtab->symtab_prevail_in_asm_name_hash (prevailing);
515 /* Diagnose mismatched objects. */
516 for (e = prevailing->next_sharing_asm_name;
517 e; e = e->next_sharing_asm_name)
519 if (TREE_CODE (prevailing->decl)
520 == TREE_CODE (e->decl))
521 continue;
522 if (!lto_symtab_symbol_p (e))
523 continue;
525 switch (TREE_CODE (prevailing->decl))
527 case VAR_DECL:
528 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
529 error_at (DECL_SOURCE_LOCATION (e->decl),
530 "variable %qD redeclared as function",
531 prevailing->decl);
532 break;
534 case FUNCTION_DECL:
535 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
536 error_at (DECL_SOURCE_LOCATION (e->decl),
537 "function %qD redeclared as variable",
538 prevailing->decl);
539 break;
541 default:
542 gcc_unreachable ();
545 diagnosed_p = true;
547 if (diagnosed_p)
548 inform (DECL_SOURCE_LOCATION (prevailing->decl),
549 "previously declared here");
551 /* Merge the chain to the single prevailing decl and diagnose
552 mismatches. */
553 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
555 if (symtab->dump_file)
557 fprintf (symtab->dump_file, "After resolution:\n");
558 for (e = prevailing; e; e = e->next_sharing_asm_name)
559 e->dump (symtab->dump_file);
563 /* Resolve and merge all symbol table chains to a prevailing decl. */
565 void
566 lto_symtab_merge_decls (void)
568 symtab_node *node;
570 /* Populate assembler name hash. */
571 symtab->symtab_initialize_asm_name_hash ();
573 FOR_EACH_SYMBOL (node)
574 if (!node->previous_sharing_asm_name
575 && node->next_sharing_asm_name)
576 lto_symtab_merge_decls_1 (node);
579 /* Helper to process the decl chain for the symbol table entry *SLOT. */
581 static void
582 lto_symtab_merge_symbols_1 (symtab_node *prevailing)
584 symtab_node *e;
585 symtab_node *next;
587 /* Replace the cgraph node of each entry with the prevailing one. */
588 for (e = prevailing->next_sharing_asm_name; e;
589 e = next)
591 next = e->next_sharing_asm_name;
593 if (!lto_symtab_symbol_p (e))
594 continue;
595 cgraph_node *ce = dyn_cast <cgraph_node *> (e);
596 if (ce && !DECL_BUILT_IN (e->decl))
597 lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (prevailing));
598 if (varpool_node *ve = dyn_cast <varpool_node *> (e))
599 lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (prevailing));
602 return;
605 /* Merge cgraph nodes according to the symbol merging done by
606 lto_symtab_merge_decls. */
608 void
609 lto_symtab_merge_symbols (void)
611 symtab_node *node;
613 if (!flag_ltrans)
615 symtab->symtab_initialize_asm_name_hash ();
617 /* Do the actual merging.
618 At this point we invalidate hash translating decls into symtab nodes
619 because after removing one of duplicate decls the hash is not correcly
620 updated to the ohter dupliate. */
621 FOR_EACH_SYMBOL (node)
622 if (lto_symtab_symbol_p (node)
623 && node->next_sharing_asm_name
624 && !node->previous_sharing_asm_name)
625 lto_symtab_merge_symbols_1 (node);
627 /* Resolve weakref aliases whose target are now in the compilation unit.
628 also re-populate the hash translating decls into symtab nodes*/
629 FOR_EACH_SYMBOL (node)
631 cgraph_node *cnode, *cnode2;
632 varpool_node *vnode;
633 symtab_node *node2;
635 if (!node->analyzed && node->alias_target)
637 symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
638 gcc_assert (node->weakref);
639 if (tgt)
640 node->resolve_alias (tgt);
642 node->aux = NULL;
644 if (!(cnode = dyn_cast <cgraph_node *> (node))
645 || !cnode->clone_of
646 || cnode->clone_of->decl != cnode->decl)
648 /* Builtins are not merged via decl merging. It is however
649 possible that tree merging unified the declaration. We
650 do not want duplicate entries in symbol table. */
651 if (cnode && DECL_BUILT_IN (node->decl)
652 && (cnode2 = cgraph_node::get (node->decl))
653 && cnode2 != cnode)
654 lto_cgraph_replace_node (cnode2, cnode);
656 /* The user defined assembler variables are also not unified by their
657 symbol name (since it is irrelevant), but we need to unify symbol
658 nodes if tree merging occured. */
659 if ((vnode = dyn_cast <varpool_node *> (node))
660 && DECL_HARD_REGISTER (vnode->decl)
661 && (node2 = symtab_node::get (vnode->decl))
662 && node2 != node)
663 lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
664 vnode);
667 /* Abstract functions may have duplicated cgraph nodes attached;
668 remove them. */
669 else if (cnode && DECL_ABSTRACT_P (cnode->decl)
670 && (cnode2 = cgraph_node::get (node->decl))
671 && cnode2 != cnode)
672 cnode2->remove ();
674 node->decl->decl_with_vis.symtab_node = node;
680 /* Given the decl DECL, return the prevailing decl with the same name. */
682 tree
683 lto_symtab_prevailing_decl (tree decl)
685 symtab_node *ret;
687 /* Builtins and local symbols are their own prevailing decl. */
688 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
689 return decl;
691 /* DECL_ABSTRACT_Ps are their own prevailing decl. */
692 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
693 return decl;
695 /* Likewise builtins are their own prevailing decl. This preserves
696 non-builtin vs. builtin uses from compile-time. */
697 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
698 return decl;
700 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
701 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
703 /* Walk through the list of candidates and return the one we merged to. */
704 ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
705 if (!ret)
706 return decl;
708 return ret->decl;