]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/lto/lto-symtab.c
Autogenerated fixes of "->symbol." to "->"
[thirdparty/gcc.git] / gcc / lto / lto-symtab.c
1 /* LTO symbol table.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
31 #include "ipa-utils.h"
32
33 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
34 all edges and removing the old node. */
35
36 static void
37 lto_cgraph_replace_node (struct cgraph_node *node,
38 struct cgraph_node *prevailing_node)
39 {
40 struct cgraph_edge *e, *next;
41 bool compatible_p;
42
43 if (cgraph_dump_file)
44 {
45 fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
46 " for symbol %s\n",
47 cgraph_node_name (node), node->order,
48 cgraph_node_name (prevailing_node),
49 prevailing_node->order,
50 IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
51 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
52 }
53
54 /* Merge node flags. */
55 if (node->force_output)
56 cgraph_mark_force_output_node (prevailing_node);
57 if (node->address_taken)
58 {
59 gcc_assert (!prevailing_node->global.inlined_to);
60 cgraph_mark_address_taken_node (prevailing_node);
61 }
62
63 /* Redirect all incoming edges. */
64 compatible_p
65 = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
66 TREE_TYPE (TREE_TYPE (node->decl)));
67 for (e = node->callers; e; e = next)
68 {
69 next = e->next_caller;
70 cgraph_redirect_edge_callee (e, prevailing_node);
71 /* If there is a mismatch between the supposed callee return type and
72 the real one do not attempt to inline this function.
73 ??? We really need a way to match function signatures for ABI
74 compatibility and perform related promotions at inlining time. */
75 if (!compatible_p)
76 e->call_stmt_cannot_inline_p = 1;
77 }
78 /* Redirect incomming references. */
79 ipa_clone_referring (prevailing_node, &node->ref_list);
80
81 ipa_merge_profiles (prevailing_node, node);
82 lto_free_function_in_decl_state_for_node (node);
83
84 if (node->decl != prevailing_node->decl)
85 cgraph_release_function_body (node);
86
87 /* Finally remove the replaced node. */
88 cgraph_remove_node (node);
89 }
90
91 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
92 all edges and removing the old node. */
93
94 static void
95 lto_varpool_replace_node (struct varpool_node *vnode,
96 struct varpool_node *prevailing_node)
97 {
98 gcc_assert (!vnode->definition || prevailing_node->definition);
99 gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
100
101 ipa_clone_referring (prevailing_node, &vnode->ref_list);
102
103 /* Be sure we can garbage collect the initializer. */
104 if (DECL_INITIAL (vnode->decl)
105 && vnode->decl != prevailing_node->decl)
106 DECL_INITIAL (vnode->decl) = error_mark_node;
107 /* Finally remove the replaced node. */
108 varpool_remove_node (vnode);
109 }
110
111 /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
112 Return false if the symbols are not fully compatible and a diagnostic
113 should be emitted. */
114
115 static bool
116 lto_symtab_merge (symtab_node prevailing, symtab_node entry)
117 {
118 tree prevailing_decl = prevailing->decl;
119 tree decl = entry->decl;
120 tree prevailing_type, type;
121
122 if (prevailing_decl == decl)
123 return true;
124
125 /* Merge decl state in both directions, we may still end up using
126 the new decl. */
127 TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
128 TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
129
130 /* The linker may ask us to combine two incompatible symbols.
131 Detect this case and notify the caller of required diagnostics. */
132
133 if (TREE_CODE (decl) == FUNCTION_DECL)
134 {
135 if (!types_compatible_p (TREE_TYPE (prevailing_decl),
136 TREE_TYPE (decl)))
137 /* If we don't have a merged type yet...sigh. The linker
138 wouldn't complain if the types were mismatched, so we
139 probably shouldn't either. Just use the type from
140 whichever decl appears to be associated with the
141 definition. If for some odd reason neither decl is, the
142 older one wins. */
143 (void) 0;
144
145 return true;
146 }
147
148 /* Now we exclusively deal with VAR_DECLs. */
149
150 /* Sharing a global symbol is a strong hint that two types are
151 compatible. We could use this information to complete
152 incomplete pointed-to types more aggressively here, ignoring
153 mismatches in both field and tag names. It's difficult though
154 to guarantee that this does not have side-effects on merging
155 more compatible types from other translation units though. */
156
157 /* We can tolerate differences in type qualification, the
158 qualification of the prevailing definition will prevail.
159 ??? In principle we might want to only warn for structurally
160 incompatible types here, but unless we have protective measures
161 for TBAA in place that would hide useful information. */
162 prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
163 type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
164
165 if (!types_compatible_p (prevailing_type, type))
166 {
167 if (COMPLETE_TYPE_P (type))
168 return false;
169
170 /* If type is incomplete then avoid warnings in the cases
171 that TBAA handles just fine. */
172
173 if (TREE_CODE (prevailing_type) != TREE_CODE (type))
174 return false;
175
176 if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
177 {
178 tree tem1 = TREE_TYPE (prevailing_type);
179 tree tem2 = TREE_TYPE (type);
180 while (TREE_CODE (tem1) == ARRAY_TYPE
181 && TREE_CODE (tem2) == ARRAY_TYPE)
182 {
183 tem1 = TREE_TYPE (tem1);
184 tem2 = TREE_TYPE (tem2);
185 }
186
187 if (TREE_CODE (tem1) != TREE_CODE (tem2))
188 return false;
189
190 if (!types_compatible_p (tem1, tem2))
191 return false;
192 }
193
194 /* Fallthru. Compatible enough. */
195 }
196
197 /* ??? We might want to emit a warning here if type qualification
198 differences were spotted. Do not do this unconditionally though. */
199
200 /* There is no point in comparing too many details of the decls here.
201 The type compatibility checks or the completing of types has properly
202 dealt with most issues. */
203
204 /* The following should all not invoke fatal errors as in non-LTO
205 mode the linker wouldn't complain either. Just emit warnings. */
206
207 /* Report a warning if user-specified alignments do not match. */
208 if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
209 && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
210 return false;
211
212 return true;
213 }
214
215 /* Return true if the symtab entry E can be replaced by another symtab
216 entry. */
217
218 static bool
219 lto_symtab_resolve_replaceable_p (symtab_node e)
220 {
221 if (DECL_EXTERNAL (e->decl)
222 || DECL_COMDAT (e->decl)
223 || DECL_ONE_ONLY (e->decl)
224 || DECL_WEAK (e->decl))
225 return true;
226
227 if (TREE_CODE (e->decl) == VAR_DECL)
228 return (DECL_COMMON (e->decl)
229 || (!flag_no_common && !DECL_INITIAL (e->decl)));
230
231 return false;
232 }
233
234 /* Return true, if the symbol E should be resolved by lto-symtab.
235 Those are all external symbols and all real symbols that are not static (we
236 handle renaming of static later in partitioning). */
237
238 static bool
239 lto_symtab_symbol_p (symtab_node e)
240 {
241 if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
242 return false;
243 return symtab_real_symbol_p (e);
244 }
245
246 /* Return true if the symtab entry E can be the prevailing one. */
247
248 static bool
249 lto_symtab_resolve_can_prevail_p (symtab_node e)
250 {
251 if (!lto_symtab_symbol_p (e))
252 return false;
253
254 /* The C++ frontend ends up neither setting TREE_STATIC nor
255 DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
256 So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
257 if (DECL_EXTERNAL (e->decl))
258 return false;
259
260 return e->definition;
261 }
262
263 /* Resolve the symbol with the candidates in the chain *SLOT and store
264 their resolutions. */
265
266 static symtab_node
267 lto_symtab_resolve_symbols (symtab_node first)
268 {
269 symtab_node e;
270 symtab_node prevailing = NULL;
271
272 /* Always set e->node so that edges are updated to reflect decl merging. */
273 for (e = first; e; e = e->next_sharing_asm_name)
274 if (lto_symtab_symbol_p (e)
275 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
276 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
277 || e->resolution == LDPR_PREVAILING_DEF))
278 {
279 prevailing = e;
280 break;
281 }
282
283 /* If the chain is already resolved there is nothing else to do. */
284 if (prevailing)
285 {
286 /* Assert it's the only one. */
287 for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
288 if (lto_symtab_symbol_p (e)
289 && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
290 || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
291 || e->resolution == LDPR_PREVAILING_DEF))
292 fatal_error ("multiple prevailing defs for %qE",
293 DECL_NAME (prevailing->decl));
294 return prevailing;
295 }
296
297 /* Find the single non-replaceable prevailing symbol and
298 diagnose ODR violations. */
299 for (e = first; e; e = e->next_sharing_asm_name)
300 {
301 if (!lto_symtab_resolve_can_prevail_p (e))
302 continue;
303
304 /* If we have a non-replaceable definition it prevails. */
305 if (!lto_symtab_resolve_replaceable_p (e))
306 {
307 if (prevailing)
308 {
309 error_at (DECL_SOURCE_LOCATION (e->decl),
310 "%qD has already been defined", e->decl);
311 inform (DECL_SOURCE_LOCATION (prevailing->decl),
312 "previously defined here");
313 }
314 prevailing = e;
315 }
316 }
317 if (prevailing)
318 return prevailing;
319
320 /* Do a second round choosing one from the replaceable prevailing decls. */
321 for (e = first; e; e = e->next_sharing_asm_name)
322 {
323 if (!lto_symtab_resolve_can_prevail_p (e))
324 continue;
325
326 /* Choose the first function that can prevail as prevailing. */
327 if (TREE_CODE (e->decl) == FUNCTION_DECL)
328 {
329 prevailing = e;
330 break;
331 }
332
333 /* From variables that can prevail choose the largest one. */
334 if (!prevailing
335 || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
336 DECL_SIZE (e->decl))
337 /* When variables are equivalent try to chose one that has useful
338 DECL_INITIAL. This makes sense for keyed vtables that are
339 DECL_EXTERNAL but initialized. In units that do not need them
340 we replace the initializer by error_mark_node to conserve
341 memory.
342
343 We know that the vtable is keyed outside the LTO unit - otherwise
344 the keyed instance would prevail. We still can preserve useful
345 info in the initializer. */
346 || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
347 && (DECL_INITIAL (e->decl)
348 && DECL_INITIAL (e->decl) != error_mark_node)
349 && (!DECL_INITIAL (prevailing->decl)
350 || DECL_INITIAL (prevailing->decl) == error_mark_node)))
351 prevailing = e;
352 }
353
354 return prevailing;
355 }
356
357 /* Merge all decls in the symbol table chain to the prevailing decl and
358 issue diagnostics about type mismatches. If DIAGNOSED_P is true
359 do not issue further diagnostics.*/
360
361 static void
362 lto_symtab_merge_decls_2 (symtab_node first, bool diagnosed_p)
363 {
364 symtab_node prevailing, e;
365 vec<tree> mismatches = vNULL;
366 unsigned i;
367 tree decl;
368
369 /* Nothing to do for a single entry. */
370 prevailing = first;
371 if (!prevailing->next_sharing_asm_name)
372 return;
373
374 /* Try to merge each entry with the prevailing one. */
375 for (e = prevailing->next_sharing_asm_name;
376 e; e = e->next_sharing_asm_name)
377 if (TREE_PUBLIC (e->decl))
378 {
379 if (!lto_symtab_merge (prevailing, e)
380 && !diagnosed_p)
381 mismatches.safe_push (e->decl);
382 }
383 if (mismatches.is_empty ())
384 return;
385
386 /* Diagnose all mismatched re-declarations. */
387 FOR_EACH_VEC_ELT (mismatches, i, decl)
388 {
389 if (!types_compatible_p (TREE_TYPE (prevailing->decl),
390 TREE_TYPE (decl)))
391 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
392 "type of %qD does not match original "
393 "declaration", decl);
394
395 else if ((DECL_USER_ALIGN (prevailing->decl)
396 && DECL_USER_ALIGN (decl))
397 && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
398 {
399 diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
400 "alignment of %qD is bigger than "
401 "original declaration", decl);
402 }
403 }
404 if (diagnosed_p)
405 inform (DECL_SOURCE_LOCATION (prevailing->decl),
406 "previously declared here");
407
408 mismatches.release ();
409 }
410
411 /* Helper to process the decl chain for the symbol table entry *SLOT. */
412
413 static void
414 lto_symtab_merge_decls_1 (symtab_node first)
415 {
416 symtab_node e, prevailing;
417 bool diagnosed_p = false;
418
419 if (cgraph_dump_file)
420 {
421 fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
422 symtab_node_asm_name (first));
423 for (e = first; e; e = e->next_sharing_asm_name)
424 if (TREE_PUBLIC (e->decl))
425 dump_symtab_node (cgraph_dump_file, e);
426 }
427
428 /* Compute the symbol resolutions. This is a no-op when using the
429 linker plugin and resolution was decided by the linker. */
430 prevailing = lto_symtab_resolve_symbols (first);
431
432 /* If there's not a prevailing symbol yet it's an external reference.
433 Happens a lot during ltrans. Choose the first symbol with a
434 cgraph or a varpool node. */
435 if (!prevailing)
436 {
437 prevailing = first;
438 /* For variables chose with a priority variant with vnode
439 attached (i.e. from unit where external declaration of
440 variable is actually used).
441 When there are multiple variants, chose one with size.
442 This is needed for C++ typeinfos, for example in
443 lto/20081204-1 there are typeifos in both units, just
444 one of them do have size. */
445 if (TREE_CODE (prevailing->decl) == VAR_DECL)
446 {
447 for (e = prevailing->next_sharing_asm_name;
448 e; e = e->next_sharing_asm_name)
449 if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
450 && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
451 && lto_symtab_symbol_p (e))
452 prevailing = e;
453 }
454 /* For variables prefer the non-builtin if one is available. */
455 else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
456 {
457 for (e = first; e; e = e->next_sharing_asm_name)
458 if (TREE_CODE (e->decl) == FUNCTION_DECL
459 && !DECL_BUILT_IN (e->decl)
460 && lto_symtab_symbol_p (e))
461 {
462 prevailing = e;
463 break;
464 }
465 }
466 }
467
468 symtab_prevail_in_asm_name_hash (prevailing);
469
470 /* Diagnose mismatched objects. */
471 for (e = prevailing->next_sharing_asm_name;
472 e; e = e->next_sharing_asm_name)
473 {
474 if (TREE_CODE (prevailing->decl)
475 == TREE_CODE (e->decl))
476 continue;
477 if (!lto_symtab_symbol_p (e))
478 continue;
479
480 switch (TREE_CODE (prevailing->decl))
481 {
482 case VAR_DECL:
483 gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
484 error_at (DECL_SOURCE_LOCATION (e->decl),
485 "variable %qD redeclared as function",
486 prevailing->decl);
487 break;
488
489 case FUNCTION_DECL:
490 gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
491 error_at (DECL_SOURCE_LOCATION (e->decl),
492 "function %qD redeclared as variable",
493 prevailing->decl);
494 break;
495
496 default:
497 gcc_unreachable ();
498 }
499
500 diagnosed_p = true;
501 }
502 if (diagnosed_p)
503 inform (DECL_SOURCE_LOCATION (prevailing->decl),
504 "previously declared here");
505
506 /* Merge the chain to the single prevailing decl and diagnose
507 mismatches. */
508 lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
509
510 if (cgraph_dump_file)
511 {
512 fprintf (cgraph_dump_file, "After resolution:\n");
513 for (e = prevailing; e; e = e->next_sharing_asm_name)
514 dump_symtab_node (cgraph_dump_file, e);
515 }
516 }
517
518 /* Resolve and merge all symbol table chains to a prevailing decl. */
519
520 void
521 lto_symtab_merge_decls (void)
522 {
523 symtab_node node;
524
525 /* Populate assembler name hash. */
526 symtab_initialize_asm_name_hash ();
527
528 FOR_EACH_SYMBOL (node)
529 if (!node->previous_sharing_asm_name
530 && node->next_sharing_asm_name)
531 lto_symtab_merge_decls_1 (node);
532 }
533
534 /* Helper to process the decl chain for the symbol table entry *SLOT. */
535
536 static void
537 lto_symtab_merge_symbols_1 (symtab_node prevailing)
538 {
539 symtab_node e, next;
540
541 /* Replace the cgraph node of each entry with the prevailing one. */
542 for (e = prevailing->next_sharing_asm_name; e;
543 e = next)
544 {
545 next = e->next_sharing_asm_name;
546
547 if (!lto_symtab_symbol_p (e))
548 continue;
549 cgraph_node *ce = dyn_cast <cgraph_node> (e);
550 if (ce && !DECL_BUILT_IN (e->decl))
551 lto_cgraph_replace_node (ce, cgraph (prevailing));
552 if (varpool_node *ve = dyn_cast <varpool_node> (e))
553 lto_varpool_replace_node (ve, varpool (prevailing));
554 }
555
556 return;
557 }
558
559 /* Merge cgraph nodes according to the symbol merging done by
560 lto_symtab_merge_decls. */
561
562 void
563 lto_symtab_merge_symbols (void)
564 {
565 symtab_node node;
566
567 if (!flag_ltrans)
568 {
569 symtab_initialize_asm_name_hash ();
570
571 /* Do the actual merging.
572 At this point we invalidate hash translating decls into symtab nodes
573 because after removing one of duplicate decls the hash is not correcly
574 updated to the ohter dupliate. */
575 FOR_EACH_SYMBOL (node)
576 if (lto_symtab_symbol_p (node)
577 && node->next_sharing_asm_name
578 && !node->previous_sharing_asm_name)
579 lto_symtab_merge_symbols_1 (node);
580
581 /* Resolve weakref aliases whose target are now in the compilation unit.
582 also re-populate the hash translating decls into symtab nodes*/
583 FOR_EACH_SYMBOL (node)
584 {
585 cgraph_node *cnode, *cnode2;
586 varpool_node *vnode;
587 symtab_node node2;
588
589 if (!node->analyzed && node->alias_target)
590 {
591 symtab_node tgt = symtab_node_for_asm (node->alias_target);
592 gcc_assert (node->weakref);
593 if (tgt)
594 symtab_resolve_alias (node, tgt);
595 }
596 node->aux = NULL;
597
598 if (!(cnode = dyn_cast <cgraph_node> (node))
599 || !cnode->clone_of
600 || cnode->clone_of->decl != cnode->decl)
601 {
602 /* Builtins are not merged via decl merging. It is however
603 possible that tree merging unified the declaration. We
604 do not want duplicate entries in symbol table. */
605 if (cnode && DECL_BUILT_IN (node->decl)
606 && (cnode2 = cgraph_get_node (node->decl))
607 && cnode2 != cnode)
608 lto_cgraph_replace_node (cnode2, cnode);
609
610 /* The user defined assembler variables are also not unified by their
611 symbol name (since it is irrelevant), but we need to unify symbol
612 nodes if tree merging occured. */
613 if ((vnode = dyn_cast <varpool_node> (node))
614 && DECL_HARD_REGISTER (vnode->decl)
615 && (node2 = symtab_get_node (vnode->decl))
616 && node2 != node)
617 lto_varpool_replace_node (dyn_cast <varpool_node> (node2),
618 vnode);
619
620
621 /* Abstract functions may have duplicated cgraph nodes attached;
622 remove them. */
623 else if (cnode && DECL_ABSTRACT (cnode->decl)
624 && (cnode2 = cgraph_get_node (node->decl))
625 && cnode2 != cnode)
626 cgraph_remove_node (cnode2);
627
628 symtab_insert_node_to_hashtable (node);
629 }
630 }
631 }
632 }
633
634 /* Given the decl DECL, return the prevailing decl with the same name. */
635
636 tree
637 lto_symtab_prevailing_decl (tree decl)
638 {
639 symtab_node ret;
640
641 /* Builtins and local symbols are their own prevailing decl. */
642 if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
643 return decl;
644
645 /* DECL_ABSTRACTs are their own prevailng decl. */
646 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
647 return decl;
648
649 /* Likewise builtins are their own prevailing decl. This preserves
650 non-builtin vs. builtin uses from compile-time. */
651 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
652 return decl;
653
654 /* Ensure DECL_ASSEMBLER_NAME will not set assembler name. */
655 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
656
657 /* Walk through the list of candidates and return the one we merged to. */
658 ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
659 if (!ret)
660 return decl;
661
662 return ret->decl;
663 }