arm.md (split for eq(reg, 0)): Add variants for ARMv5 and Thumb2.
[official-gcc.git] / gcc / lto-symtab.c
blob1b447cfb9aeb2a326bc09737d0b6c78e713aee26
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 /* Finally remove the replaced node. */
84 cgraph_remove_node (node);
87 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
88 all edges and removing the old node. */
90 static void
91 lto_varpool_replace_node (struct varpool_node *vnode,
92 struct varpool_node *prevailing_node)
94 gcc_assert (!vnode->symbol.definition || prevailing_node->symbol.definition);
95 gcc_assert (!vnode->symbol.analyzed || prevailing_node->symbol.analyzed);
97 ipa_clone_referring ((symtab_node)prevailing_node, &vnode->symbol.ref_list);
99 /* Be sure we can garbage collect the initializer. */
100 if (DECL_INITIAL (vnode->symbol.decl)
101 && vnode->symbol.decl != prevailing_node->symbol.decl)
102 DECL_INITIAL (vnode->symbol.decl) = error_mark_node;
103 /* Finally remove the replaced node. */
104 varpool_remove_node (vnode);
107 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
108 Return false if the symbols are not fully compatible and a diagnostic
109 should be emitted. */
111 static bool
112 lto_symtab_merge (symtab_node prevailing, symtab_node entry)
114 tree prevailing_decl = prevailing->symbol.decl;
115 tree decl = entry->symbol.decl;
116 tree prevailing_type, type;
118 if (prevailing_decl == decl)
119 return true;
121 /* Merge decl state in both directions, we may still end up using
122 the new decl. */
123 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
124 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
126 /* The linker may ask us to combine two incompatible symbols.
127 Detect this case and notify the caller of required diagnostics. */
129 if (TREE_CODE (decl) == FUNCTION_DECL)
131 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
132 TREE_TYPE (decl)))
133 /* If we don't have a merged type yet...sigh. The linker
134 wouldn't complain if the types were mismatched, so we
135 probably shouldn't either. Just use the type from
136 whichever decl appears to be associated with the
137 definition. If for some odd reason neither decl is, the
138 older one wins. */
139 (void) 0;
141 return true;
144 /* Now we exclusively deal with VAR_DECLs. */
146 /* Sharing a global symbol is a strong hint that two types are
147 compatible. We could use this information to complete
148 incomplete pointed-to types more aggressively here, ignoring
149 mismatches in both field and tag names. It's difficult though
150 to guarantee that this does not have side-effects on merging
151 more compatible types from other translation units though. */
153 /* We can tolerate differences in type qualification, the
154 qualification of the prevailing definition will prevail.
155 ??? In principle we might want to only warn for structurally
156 incompatible types here, but unless we have protective measures
157 for TBAA in place that would hide useful information. */
158 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
159 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
161 if (!types_compatible_p (prevailing_type, type))
163 if (COMPLETE_TYPE_P (type))
164 return false;
166 /* If type is incomplete then avoid warnings in the cases
167 that TBAA handles just fine. */
169 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
170 return false;
172 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
174 tree tem1 = TREE_TYPE (prevailing_type);
175 tree tem2 = TREE_TYPE (type);
176 while (TREE_CODE (tem1) == ARRAY_TYPE
177 && TREE_CODE (tem2) == ARRAY_TYPE)
179 tem1 = TREE_TYPE (tem1);
180 tem2 = TREE_TYPE (tem2);
183 if (TREE_CODE (tem1) != TREE_CODE (tem2))
184 return false;
186 if (!types_compatible_p (tem1, tem2))
187 return false;
190 /* Fallthru. Compatible enough. */
193 /* ??? We might want to emit a warning here if type qualification
194 differences were spotted. Do not do this unconditionally though. */
196 /* There is no point in comparing too many details of the decls here.
197 The type compatibility checks or the completing of types has properly
198 dealt with most issues. */
200 /* The following should all not invoke fatal errors as in non-LTO
201 mode the linker wouldn't complain either. Just emit warnings. */
203 /* Report a warning if user-specified alignments do not match. */
204 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
205 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
206 return false;
208 return true;
211 /* Return true if the symtab entry E can be replaced by another symtab
212 entry. */
214 static bool
215 lto_symtab_resolve_replaceable_p (symtab_node e)
217 if (DECL_EXTERNAL (e->symbol.decl)
218 || DECL_COMDAT (e->symbol.decl)
219 || DECL_ONE_ONLY (e->symbol.decl)
220 || DECL_WEAK (e->symbol.decl))
221 return true;
223 if (TREE_CODE (e->symbol.decl) == VAR_DECL)
224 return (DECL_COMMON (e->symbol.decl)
225 || (!flag_no_common && !DECL_INITIAL (e->symbol.decl)));
227 return false;
230 /* Return true, if the symbol E should be resolved by lto-symtab.
231 Those are all external symbols and all real symbols that are not static (we
232 handle renaming of static later in partitioning). */
234 static bool
235 lto_symtab_symbol_p (symtab_node e)
237 if (!TREE_PUBLIC (e->symbol.decl) && !DECL_EXTERNAL (e->symbol.decl))
238 return false;
239 return symtab_real_symbol_p (e);
242 /* Return true if the symtab entry E can be the prevailing one. */
244 static bool
245 lto_symtab_resolve_can_prevail_p (symtab_node e)
247 if (!lto_symtab_symbol_p (e))
248 return false;
250 /* The C++ frontend ends up neither setting TREE_STATIC nor
251 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
252 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
253 if (DECL_EXTERNAL (e->symbol.decl))
254 return false;
256 return e->symbol.definition;
259 /* Resolve the symbol with the candidates in the chain *SLOT and store
260 their resolutions. */
262 static symtab_node
263 lto_symtab_resolve_symbols (symtab_node first)
265 symtab_node e;
266 symtab_node prevailing = NULL;
268 /* Always set e->node so that edges are updated to reflect decl merging. */
269 for (e = first; e; e = e->symbol.next_sharing_asm_name)
270 if (lto_symtab_symbol_p (e)
271 && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
272 || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
273 || e->symbol.resolution == LDPR_PREVAILING_DEF))
275 prevailing = e;
276 break;
279 /* If the chain is already resolved there is nothing else to do. */
280 if (prevailing)
282 /* Assert it's the only one. */
283 for (e = prevailing->symbol.next_sharing_asm_name; e; e = e->symbol.next_sharing_asm_name)
284 if (lto_symtab_symbol_p (e)
285 && (e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
286 || e->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
287 || e->symbol.resolution == LDPR_PREVAILING_DEF))
288 fatal_error ("multiple prevailing defs for %qE",
289 DECL_NAME (prevailing->symbol.decl));
290 return prevailing;
293 /* Find the single non-replaceable prevailing symbol and
294 diagnose ODR violations. */
295 for (e = first; e; e = e->symbol.next_sharing_asm_name)
297 if (!lto_symtab_resolve_can_prevail_p (e))
298 continue;
300 /* If we have a non-replaceable definition it prevails. */
301 if (!lto_symtab_resolve_replaceable_p (e))
303 if (prevailing)
305 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
306 "%qD has already been defined", e->symbol.decl);
307 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
308 "previously defined here");
310 prevailing = e;
313 if (prevailing)
314 return prevailing;
316 /* Do a second round choosing one from the replaceable prevailing decls. */
317 for (e = first; e; e = e->symbol.next_sharing_asm_name)
319 if (!lto_symtab_resolve_can_prevail_p (e))
320 continue;
322 /* Choose the first function that can prevail as prevailing. */
323 if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL)
325 prevailing = e;
326 break;
329 /* From variables that can prevail choose the largest one. */
330 if (!prevailing
331 || tree_int_cst_lt (DECL_SIZE (prevailing->symbol.decl),
332 DECL_SIZE (e->symbol.decl))
333 /* When variables are equivalent try to chose one that has useful
334 DECL_INITIAL. This makes sense for keyed vtables that are
335 DECL_EXTERNAL but initialized. In units that do not need them
336 we replace the initializer by error_mark_node to conserve
337 memory.
339 We know that the vtable is keyed outside the LTO unit - otherwise
340 the keyed instance would prevail. We still can preserve useful
341 info in the initializer. */
342 || (DECL_SIZE (prevailing->symbol.decl) == DECL_SIZE (e->symbol.decl)
343 && (DECL_INITIAL (e->symbol.decl)
344 && DECL_INITIAL (e->symbol.decl) != error_mark_node)
345 && (!DECL_INITIAL (prevailing->symbol.decl)
346 || DECL_INITIAL (prevailing->symbol.decl) == error_mark_node)))
347 prevailing = e;
350 return prevailing;
353 /* Merge all decls in the symbol table chain to the prevailing decl and
354 issue diagnostics about type mismatches. If DIAGNOSED_P is true
355 do not issue further diagnostics.*/
357 static void
358 lto_symtab_merge_decls_2 (symtab_node first, bool diagnosed_p)
360 symtab_node prevailing, e;
361 vec<tree> mismatches = vNULL;
362 unsigned i;
363 tree decl;
365 /* Nothing to do for a single entry. */
366 prevailing = first;
367 if (!prevailing->symbol.next_sharing_asm_name)
368 return;
370 /* Try to merge each entry with the prevailing one. */
371 for (e = prevailing->symbol.next_sharing_asm_name;
372 e; e = e->symbol.next_sharing_asm_name)
373 if (TREE_PUBLIC (e->symbol.decl))
375 if (!lto_symtab_merge (prevailing, e)
376 && !diagnosed_p)
377 mismatches.safe_push (e->symbol.decl);
379 if (mismatches.is_empty ())
380 return;
382 /* Diagnose all mismatched re-declarations. */
383 FOR_EACH_VEC_ELT (mismatches, i, decl)
385 if (!types_compatible_p (TREE_TYPE (prevailing->symbol.decl),
386 TREE_TYPE (decl)))
387 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
388 "type of %qD does not match original "
389 "declaration", decl);
391 else if ((DECL_USER_ALIGN (prevailing->symbol.decl)
392 && DECL_USER_ALIGN (decl))
393 && DECL_ALIGN (prevailing->symbol.decl) < DECL_ALIGN (decl))
395 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
396 "alignment of %qD is bigger than "
397 "original declaration", decl);
400 if (diagnosed_p)
401 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
402 "previously declared here");
404 mismatches.release ();
407 /* Helper to process the decl chain for the symbol table entry *SLOT. */
409 static void
410 lto_symtab_merge_decls_1 (symtab_node first)
412 symtab_node e, prevailing;
413 bool diagnosed_p = false;
415 if (cgraph_dump_file)
417 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
418 symtab_node_asm_name (first));
419 for (e = first; e; e = e->symbol.next_sharing_asm_name)
420 if (TREE_PUBLIC (e->symbol.decl))
421 dump_symtab_node (cgraph_dump_file, e);
424 /* Compute the symbol resolutions. This is a no-op when using the
425 linker plugin and resolution was decided by the linker. */
426 prevailing = lto_symtab_resolve_symbols (first);
428 /* If there's not a prevailing symbol yet it's an external reference.
429 Happens a lot during ltrans. Choose the first symbol with a
430 cgraph or a varpool node. */
431 if (!prevailing)
433 prevailing = first;
434 /* For variables chose with a priority variant with vnode
435 attached (i.e. from unit where external declaration of
436 variable is actually used).
437 When there are multiple variants, chose one with size.
438 This is needed for C++ typeinfos, for example in
439 lto/20081204-1 there are typeifos in both units, just
440 one of them do have size. */
441 if (TREE_CODE (prevailing->symbol.decl) == VAR_DECL)
443 for (e = prevailing->symbol.next_sharing_asm_name;
444 e; e = e->symbol.next_sharing_asm_name)
445 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->symbol.decl))
446 && COMPLETE_TYPE_P (TREE_TYPE (e->symbol.decl))
447 && lto_symtab_symbol_p (e))
448 prevailing = e;
450 /* For variables prefer the non-builtin if one is available. */
451 else if (TREE_CODE (prevailing->symbol.decl) == FUNCTION_DECL)
453 for (e = first; e; e = e->symbol.next_sharing_asm_name)
454 if (TREE_CODE (e->symbol.decl) == FUNCTION_DECL
455 && !DECL_BUILT_IN (e->symbol.decl)
456 && lto_symtab_symbol_p (e))
458 prevailing = e;
459 break;
464 symtab_prevail_in_asm_name_hash (prevailing);
466 /* Diagnose mismatched objects. */
467 for (e = prevailing->symbol.next_sharing_asm_name;
468 e; e = e->symbol.next_sharing_asm_name)
470 if (TREE_CODE (prevailing->symbol.decl)
471 == TREE_CODE (e->symbol.decl))
472 continue;
473 if (!lto_symtab_symbol_p (e))
474 continue;
476 switch (TREE_CODE (prevailing->symbol.decl))
478 case VAR_DECL:
479 gcc_assert (TREE_CODE (e->symbol.decl) == FUNCTION_DECL);
480 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
481 "variable %qD redeclared as function",
482 prevailing->symbol.decl);
483 break;
485 case FUNCTION_DECL:
486 gcc_assert (TREE_CODE (e->symbol.decl) == VAR_DECL);
487 error_at (DECL_SOURCE_LOCATION (e->symbol.decl),
488 "function %qD redeclared as variable",
489 prevailing->symbol.decl);
490 break;
492 default:
493 gcc_unreachable ();
496 diagnosed_p = true;
498 if (diagnosed_p)
499 inform (DECL_SOURCE_LOCATION (prevailing->symbol.decl),
500 "previously declared here");
502 /* Merge the chain to the single prevailing decl and diagnose
503 mismatches. */
504 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
506 if (cgraph_dump_file)
508 fprintf (cgraph_dump_file, "After resolution:\n");
509 for (e = prevailing; e; e = e->symbol.next_sharing_asm_name)
510 dump_symtab_node (cgraph_dump_file, e);
514 /* Resolve and merge all symbol table chains to a prevailing decl. */
516 void
517 lto_symtab_merge_decls (void)
519 symtab_node node;
521 /* Populate assembler name hash. */
522 symtab_initialize_asm_name_hash ();
524 FOR_EACH_SYMBOL (node)
525 if (!node->symbol.previous_sharing_asm_name
526 && node->symbol.next_sharing_asm_name)
527 lto_symtab_merge_decls_1 (node);
530 /* Helper to process the decl chain for the symbol table entry *SLOT. */
532 static void
533 lto_symtab_merge_symbols_1 (symtab_node prevailing)
535 symtab_node e, next;
537 /* Replace the cgraph node of each entry with the prevailing one. */
538 for (e = prevailing->symbol.next_sharing_asm_name; e;
539 e = next)
541 next = e->symbol.next_sharing_asm_name;
543 if (!lto_symtab_symbol_p (e))
544 continue;
545 cgraph_node *ce = dyn_cast <cgraph_node> (e);
546 if (ce && !DECL_BUILT_IN (e->symbol.decl))
547 lto_cgraph_replace_node (ce, cgraph (prevailing));
548 if (varpool_node *ve = dyn_cast <varpool_node> (e))
549 lto_varpool_replace_node (ve, varpool (prevailing));
552 return;
555 /* Merge cgraph nodes according to the symbol merging done by
556 lto_symtab_merge_decls. */
558 void
559 lto_symtab_merge_symbols (void)
561 symtab_node node;
563 if (!flag_ltrans)
565 symtab_initialize_asm_name_hash ();
567 /* Do the actual merging.
568 At this point we invalidate hash translating decls into symtab nodes
569 because after removing one of duplicate decls the hash is not correcly
570 updated to the ohter dupliate. */
571 FOR_EACH_SYMBOL (node)
572 if (lto_symtab_symbol_p (node)
573 && node->symbol.next_sharing_asm_name
574 && !node->symbol.previous_sharing_asm_name)
575 lto_symtab_merge_symbols_1 (node);
577 /* Resolve weakref aliases whose target are now in the compilation unit.
578 also re-populate the hash translating decls into symtab nodes*/
579 FOR_EACH_SYMBOL (node)
581 cgraph_node *cnode, *cnode2;
582 if (!node->symbol.analyzed && node->symbol.alias_target)
584 symtab_node tgt = symtab_node_for_asm (node->symbol.alias_target);
585 gcc_assert (node->symbol.weakref);
586 if (tgt)
587 symtab_resolve_alias (node, tgt);
589 node->symbol.aux = NULL;
591 if (!(cnode = dyn_cast <cgraph_node> (node))
592 || !cnode->clone_of
593 || cnode->clone_of->symbol.decl != cnode->symbol.decl)
595 if (cnode && DECL_BUILT_IN (node->symbol.decl)
596 && (cnode2 = cgraph_get_node (node->symbol.decl))
597 && cnode2 != cnode)
598 lto_cgraph_replace_node (cnode2, cnode);
599 symtab_insert_node_to_hashtable ((symtab_node)node);
605 /* Given the decl DECL, return the prevailing decl with the same name. */
607 tree
608 lto_symtab_prevailing_decl (tree decl)
610 symtab_node ret;
612 /* Builtins and local symbols are their own prevailing decl. */
613 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
614 return decl;
616 /* DECL_ABSTRACTs are their own prevailng decl. */
617 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
618 return decl;
620 /* Likewise builtins are their own prevailing decl. This preserves
621 non-builtin vs. builtin uses from compile-time. */
622 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
623 return decl;
625 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
626 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
628 /* Walk through the list of candidates and return the one we merged to. */
629 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
630 if (!ret)
631 return decl;
633 return ret->symbol.decl;