Branch data Line data Source code
1 : : /* LTO symbol table.
2 : : Copyright (C) 2009-2025 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 "target.h"
25 : : #include "function.h"
26 : : #include "basic-block.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "cgraph.h"
30 : : #include "lto-streamer.h"
31 : : #include "ipa-utils.h"
32 : : #include "builtins.h"
33 : : #include "alias.h"
34 : : #include "lto.h"
35 : : #include "lto-symtab.h"
36 : : #include "stringpool.h"
37 : : #include "attribs.h"
38 : :
39 : : /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
40 : : all edges and removing the old node. */
41 : :
42 : : static void
43 : 1902 : lto_cgraph_replace_node (struct cgraph_node *node,
44 : : struct cgraph_node *prevailing_node)
45 : : {
46 : 1902 : struct cgraph_edge *e, *next;
47 : 1902 : bool compatible_p;
48 : :
49 : 1902 : if (dump_file)
50 : : {
51 : 0 : fprintf (dump_file, "Replacing cgraph node %s by %s"
52 : : " for symbol %s\n",
53 : : node->dump_name (),
54 : : prevailing_node->dump_name (),
55 : 0 : IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
56 : : (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
57 : : }
58 : :
59 : : /* Merge node flags. */
60 : 1902 : if (node->force_output)
61 : 0 : prevailing_node->mark_force_output ();
62 : 1902 : if (node->forced_by_abi)
63 : 0 : prevailing_node->forced_by_abi = true;
64 : 1902 : if (node->address_taken)
65 : : {
66 : 142 : gcc_assert (!prevailing_node->inlined_to);
67 : 142 : prevailing_node->mark_address_taken ();
68 : : }
69 : 76 : if (node->definition && prevailing_node->definition
70 : 1978 : && DECL_COMDAT (node->decl) && DECL_COMDAT (prevailing_node->decl))
71 : 71 : prevailing_node->merged_comdat = true;
72 : 1831 : else if ((node->definition || node->body_removed)
73 : 5 : && DECL_DECLARED_INLINE_P (node->decl)
74 : 3 : && DECL_EXTERNAL (node->decl)
75 : 1834 : && prevailing_node->definition)
76 : 3 : prevailing_node->merged_extern_inline = true;
77 : 1902 : prevailing_node->merged_comdat |= node->merged_comdat;
78 : 1902 : prevailing_node->merged_extern_inline |= node->merged_extern_inline;
79 : :
80 : : /* Redirect all incoming edges. */
81 : 1902 : compatible_p
82 : 1902 : = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
83 : 1902 : TREE_TYPE (TREE_TYPE (node->decl)));
84 : 7148 : for (e = node->callers; e; e = next)
85 : : {
86 : 5246 : next = e->next_caller;
87 : 5246 : e->redirect_callee (prevailing_node);
88 : : /* If there is a mismatch between the supposed callee return type and
89 : : the real one do not attempt to inline this function.
90 : : ??? We really need a way to match function signatures for ABI
91 : : compatibility and perform related promotions at inlining time. */
92 : 5246 : if (!compatible_p)
93 : : {
94 : 1 : e->inline_failed = CIF_LTO_MISMATCHED_DECLARATIONS;
95 : 1 : e->call_stmt_cannot_inline_p = 1;
96 : : }
97 : : }
98 : : /* Redirect incomming references. */
99 : 1902 : prevailing_node->clone_referring (node);
100 : 1902 : lto_free_function_in_decl_state_for_node (node);
101 : :
102 : 1902 : if (node->decl != prevailing_node->decl)
103 : 1103 : node->release_body ();
104 : :
105 : : /* Finally remove the replaced node. */
106 : 1902 : node->remove ();
107 : 1902 : }
108 : :
109 : : /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
110 : : all edges and removing the old node. */
111 : :
112 : : static void
113 : 725 : lto_varpool_replace_node (varpool_node *vnode,
114 : : varpool_node *prevailing_node)
115 : : {
116 : 725 : gcc_assert (!vnode->definition || prevailing_node->definition);
117 : 725 : gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
118 : :
119 : 725 : prevailing_node->clone_referring (vnode);
120 : 725 : if (vnode->force_output)
121 : 11 : prevailing_node->force_output = true;
122 : 725 : if (vnode->forced_by_abi)
123 : 3 : prevailing_node->forced_by_abi = true;
124 : :
125 : : /* Be sure we can garbage collect the initializer. */
126 : 725 : if (DECL_INITIAL (vnode->decl)
127 : 725 : && vnode->decl != prevailing_node->decl)
128 : 29 : DECL_INITIAL (vnode->decl) = error_mark_node;
129 : :
130 : : /* Check and report ODR violations on virtual tables. */
131 : 725 : if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
132 : 63 : compare_virtual_tables (prevailing_node, vnode);
133 : :
134 : 725 : if (vnode->tls_model != prevailing_node->tls_model)
135 : : {
136 : 0 : bool error = false;
137 : :
138 : : /* Non-TLS and TLS never mix together. Also emulated model is not
139 : : compatible with anything else. */
140 : 0 : if (prevailing_node->tls_model == TLS_MODEL_NONE
141 : 0 : || prevailing_node->tls_model == TLS_MODEL_EMULATED
142 : 0 : || vnode->tls_model == TLS_MODEL_NONE
143 : 0 : || vnode->tls_model == TLS_MODEL_EMULATED)
144 : : error = true;
145 : : /* Linked is silently supporting transitions
146 : : GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
147 : : Do the same transitions and error out on others. */
148 : 0 : else if ((prevailing_node->tls_model == TLS_MODEL_REAL
149 : 0 : || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
150 : 0 : && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
151 : 0 : || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
152 : 0 : prevailing_node->tls_model = vnode->tls_model;
153 : 0 : else if ((vnode->tls_model == TLS_MODEL_REAL
154 : 0 : || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
155 : 0 : && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
156 : 0 : || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
157 : : ;
158 : 0 : else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
159 : 0 : && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
160 : 0 : prevailing_node->tls_model = vnode->tls_model;
161 : 0 : else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
162 : 0 : && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
163 : : ;
164 : : else
165 : : error = true;
166 : 0 : if (error)
167 : : {
168 : 0 : error_at (DECL_SOURCE_LOCATION (vnode->decl),
169 : 0 : "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
170 : 0 : inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
171 : : "previously defined here as %s",
172 : 0 : tls_model_names [prevailing_node->tls_model]);
173 : : }
174 : : }
175 : : /* Finally remove the replaced node. */
176 : 725 : vnode->remove ();
177 : 725 : }
178 : :
179 : : /* Return non-zero if we want to output waring about T1 and T2.
180 : : Return value is a bitmask of reasons of violation:
181 : : Bit 0 indicates that types are not compatible.
182 : : Bit 1 indicates that types are not compatible because of C++ ODR rule.
183 : : If COMMON_OR_EXTERN is true, do not warn on size mismatches of arrays.
184 : : Bit 2 indicates that types are not ODR compatible
185 : :
186 : : The interoperability rules are language specific. At present we do only
187 : : full checking for C++ ODR rule and for other languages we do basic check
188 : : that data structures are of same size and TBAA compatible. Our TBAA
189 : : implementation should be coarse enough so all valid type transitions
190 : : across different languages are allowed.
191 : :
192 : : In partiucular we thus allow almost arbitrary type changes with
193 : : -fno-strict-aliasing which may be tough of as a feature rather than bug
194 : : as it allows to implement dodgy tricks in the language runtimes.
195 : :
196 : : Naturally this code can be strenghtened significantly if we could track
197 : : down the language of origin. */
198 : :
199 : : static int
200 : 2470 : warn_type_compatibility_p (tree prevailing_type, tree type,
201 : : bool common_or_extern)
202 : : {
203 : 2470 : int lev = 0;
204 : 2470 : bool odr_p = odr_or_derived_type_p (prevailing_type)
205 : 2470 : && odr_or_derived_type_p (type);
206 : :
207 : 2470 : if (prevailing_type == type)
208 : : return 0;
209 : :
210 : : /* C++ provide a robust way to check for type compatibility via the ODR
211 : : rule. */
212 : 848 : if (odr_p && !odr_types_equivalent_p (prevailing_type, type))
213 : : lev |= 2;
214 : :
215 : : /* Function types needs special care, because types_compatible_p never
216 : : thinks prototype is compatible to non-prototype. */
217 : 848 : if (FUNC_OR_METHOD_TYPE_P (type))
218 : : {
219 : 263 : if (TREE_CODE (type) != TREE_CODE (prevailing_type))
220 : 0 : lev |= 1;
221 : 263 : lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
222 : 263 : TREE_TYPE (type), false);
223 : 263 : if (TREE_CODE (type) == METHOD_TYPE
224 : 78 : && TREE_CODE (prevailing_type) == METHOD_TYPE)
225 : 78 : lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
226 : 78 : TYPE_METHOD_BASETYPE (type), false);
227 : 525 : if (prototype_p (prevailing_type) && prototype_p (type)
228 : 518 : && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
229 : : {
230 : 78 : tree parm1, parm2;
231 : 78 : for (parm1 = TYPE_ARG_TYPES (prevailing_type),
232 : 78 : parm2 = TYPE_ARG_TYPES (type);
233 : 542 : parm1 && parm2;
234 : 928 : parm1 = TREE_CHAIN (parm1),
235 : 464 : parm2 = TREE_CHAIN (parm2))
236 : 928 : lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
237 : 464 : TREE_VALUE (parm2), false);
238 : 78 : if (parm1 || parm2)
239 : 24 : lev |= odr_p ? 3 : 1;
240 : : }
241 : 263 : if (comp_type_attributes (prevailing_type, type) == 0)
242 : 0 : lev |= 1;
243 : 263 : return lev;
244 : : }
245 : :
246 : : /* Get complete type. */
247 : 585 : prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
248 : 585 : type = TYPE_MAIN_VARIANT (type);
249 : :
250 : : /* We cannot use types_compatible_p because we permit some changes
251 : : across types. For example unsigned size_t and "signed size_t" may be
252 : : compatible when merging C and Fortran types. */
253 : 585 : if (COMPLETE_TYPE_P (prevailing_type)
254 : 566 : && COMPLETE_TYPE_P (type)
255 : : /* While global declarations are never variadic, we can recurse here
256 : : for function parameter types. */
257 : 500 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
258 : 500 : && TREE_CODE (TYPE_SIZE (prevailing_type)) == INTEGER_CST
259 : 1085 : && !tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (prevailing_type)))
260 : : {
261 : : /* As a special case do not warn about merging
262 : : int a[];
263 : : and
264 : : int a[]={1,2,3};
265 : : here the first declaration is COMMON or EXTERN
266 : : and sizeof(a) == sizeof (int). */
267 : 53 : if (!common_or_extern
268 : 34 : || TREE_CODE (type) != ARRAY_TYPE
269 : 59 : || TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type)))
270 : 47 : lev |= 1;
271 : : }
272 : :
273 : : /* Verify TBAA compatibility. Take care of alias set 0 and the fact that
274 : : we make ptr_type_node to TBAA compatible with every other type. */
275 : 585 : if (type_with_alias_set_p (type) && type_with_alias_set_p (prevailing_type))
276 : : {
277 : 560 : alias_set_type set1 = get_alias_set (type);
278 : 560 : alias_set_type set2 = get_alias_set (prevailing_type);
279 : :
280 : 560 : if (set1 && set2 && set1 != set2)
281 : : {
282 : : tree t1 = type, t2 = prevailing_type;
283 : :
284 : : /* Alias sets of arrays with aliased components are the same as alias
285 : : sets of the inner types. */
286 : 225 : while (TREE_CODE (t1) == ARRAY_TYPE
287 : 6 : && !TYPE_NONALIASED_COMPONENT (t1)
288 : 6 : && TREE_CODE (t2) == ARRAY_TYPE
289 : 231 : && !TYPE_NONALIASED_COMPONENT (t2))
290 : : {
291 : 6 : t1 = TREE_TYPE (t1);
292 : 6 : t2 = TREE_TYPE (t2);
293 : : }
294 : 179 : if ((!POINTER_TYPE_P (t1) || !POINTER_TYPE_P (t2))
295 : 396 : || (set1 != TYPE_ALIAS_SET (ptr_type_node)
296 : 171 : && set2 != TYPE_ALIAS_SET (ptr_type_node)))
297 : 198 : lev |= 5;
298 : : }
299 : : }
300 : :
301 : : return lev;
302 : : }
303 : :
304 : : /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
305 : : Return false if the symbols are not fully compatible and a diagnostic
306 : : should be emitted. */
307 : :
308 : : static bool
309 : 1626 : lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
310 : : {
311 : 1626 : tree prevailing_decl = prevailing->decl;
312 : 1626 : tree decl = entry->decl;
313 : :
314 : 1626 : if (prevailing_decl == decl)
315 : : return true;
316 : :
317 : 1626 : if (TREE_CODE (decl) != TREE_CODE (prevailing_decl))
318 : : return false;
319 : :
320 : : /* Merge decl state in both directions, we may still end up using
321 : : the new decl. */
322 : 1626 : TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
323 : 1626 : TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
324 : :
325 : : /* The linker may ask us to combine two incompatible symbols.
326 : : Detect this case and notify the caller of required diagnostics. */
327 : :
328 : 1626 : if (TREE_CODE (decl) == FUNCTION_DECL)
329 : : {
330 : : /* Merge decl state in both directions, we may still end up using
331 : : the new decl. */
332 : 1092 : DECL_POSSIBLY_INLINED (prevailing_decl) |= DECL_POSSIBLY_INLINED (decl);
333 : 1092 : DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (prevailing_decl);
334 : :
335 : 1092 : if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
336 : 1092 : TREE_TYPE (decl),
337 : 1092 : DECL_COMMON (decl)
338 : 1092 : || DECL_EXTERNAL (decl)))
339 : : return false;
340 : :
341 : : return true;
342 : : }
343 : :
344 : 534 : if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
345 : 534 : TREE_TYPE (decl),
346 : 534 : DECL_COMMON (decl) || DECL_EXTERNAL (decl)))
347 : : return false;
348 : :
349 : : /* There is no point in comparing too many details of the decls here.
350 : : The type compatibility checks or the completing of types has properly
351 : : dealt with most issues. */
352 : :
353 : : /* The following should all not invoke fatal errors as in non-LTO
354 : : mode the linker wouldn't complain either. Just emit warnings. */
355 : :
356 : : /* Report a warning if user-specified alignments do not match. */
357 : 580 : if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
358 : 536 : && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
359 : : return false;
360 : :
361 : 952 : if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
362 : 952 : && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
363 : : {
364 : 20 : if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
365 : : return false;
366 : :
367 : 20 : tree type = TREE_TYPE (decl);
368 : :
369 : : /* For record type, check for array at the end of the structure. */
370 : 20 : if (TREE_CODE (type) == RECORD_TYPE)
371 : : {
372 : 14 : tree field = TYPE_FIELDS (type);
373 : 30 : while (DECL_CHAIN (field) != NULL_TREE)
374 : 16 : field = DECL_CHAIN (field);
375 : :
376 : 14 : return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE;
377 : : }
378 : : /* As a special case do not warn about merging
379 : : int a[];
380 : : and
381 : : int a[]={1,2,3};
382 : : here the first declaration is COMMON
383 : : and sizeof(a) == sizeof (int). */
384 : 6 : else if (TREE_CODE (type) != ARRAY_TYPE
385 : 6 : || (TYPE_SIZE (type) != TYPE_SIZE (TREE_TYPE (type))))
386 : : return false;
387 : : }
388 : :
389 : : return true;
390 : : }
391 : :
392 : : /* Return true if the symtab entry E can be replaced by another symtab
393 : : entry. */
394 : :
395 : : static bool
396 : 666 : lto_symtab_resolve_replaceable_p (symtab_node *e)
397 : : {
398 : 666 : if (DECL_EXTERNAL (e->decl)
399 : 666 : || DECL_COMDAT (e->decl)
400 : 594 : || DECL_ONE_ONLY (e->decl)
401 : 1260 : || DECL_WEAK (e->decl))
402 : 75 : return true;
403 : :
404 : 591 : if (VAR_P (e->decl))
405 : 183 : return (DECL_COMMON (e->decl)
406 : 183 : || (!flag_no_common && !DECL_INITIAL (e->decl)));
407 : :
408 : : return false;
409 : : }
410 : :
411 : : /* Return true, if the symbol E should be resolved by lto-symtab.
412 : : Those are all external symbols and all real symbols that are not static (we
413 : : handle renaming of static later in partitioning). */
414 : :
415 : : static bool
416 : 166916 : lto_symtab_symbol_p (symtab_node *e)
417 : : {
418 : 166916 : if (!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
419 : : return false;
420 : 135201 : return e->real_symbol_p ();
421 : : }
422 : :
423 : : /* Return true if the symtab entry E can be the prevailing one. */
424 : :
425 : : static bool
426 : 4421 : lto_symtab_resolve_can_prevail_p (symtab_node *e)
427 : : {
428 : 4421 : if (!lto_symtab_symbol_p (e))
429 : : return false;
430 : :
431 : : /* The C++ frontend ends up neither setting TREE_STATIC nor
432 : : DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
433 : : So do not reject !TREE_STATIC here but only DECL_EXTERNAL. */
434 : 4047 : if (DECL_EXTERNAL (e->decl))
435 : : return false;
436 : :
437 : 735 : return e->definition;
438 : : }
439 : :
440 : : /* Resolve the symbol with the candidates in the chain *SLOT and store
441 : : their resolutions. */
442 : :
443 : : static symtab_node *
444 : 2113 : lto_symtab_resolve_symbols (symtab_node *first)
445 : : {
446 : 2113 : symtab_node *e;
447 : 2113 : symtab_node *prevailing = NULL;
448 : :
449 : : /* Always set e->node so that edges are updated to reflect decl merging. */
450 : 5497 : for (e = first; e; e = e->next_sharing_asm_name)
451 : 4393 : if (lto_symtab_symbol_p (e)
452 : 4393 : && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
453 : : || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
454 : : || e->resolution == LDPR_PREVAILING_DEF))
455 : : {
456 : : prevailing = e;
457 : : break;
458 : : }
459 : :
460 : : /* If the chain is already resolved there is nothing else to do. */
461 : 2113 : if (prevailing)
462 : : {
463 : : /* Assert it's the only one.
464 : : GCC should silence multiple PREVAILING_DEF_IRONLY defs error
465 : : on COMMON symbols since it isn't error.
466 : : See: https://sourceware.org/bugzilla/show_bug.cgi?id=23079. */
467 : 1472 : for (e = prevailing->next_sharing_asm_name; e; e = e->next_sharing_asm_name)
468 : 463 : if (lto_symtab_symbol_p (e)
469 : 455 : && !DECL_COMMON (prevailing->decl)
470 : 445 : && !DECL_COMMON (e->decl)
471 : 900 : && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
472 : : || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
473 : : || e->resolution == LDPR_PREVAILING_DEF))
474 : 0 : fatal_error (input_location, "multiple prevailing defs for %qE",
475 : 0 : DECL_NAME (prevailing->decl));
476 : : return prevailing;
477 : : }
478 : :
479 : : /* Find the single non-replaceable prevailing symbol and
480 : : diagnose ODR violations. */
481 : 3911 : for (e = first; e; e = e->next_sharing_asm_name)
482 : : {
483 : 2807 : if (!lto_symtab_resolve_can_prevail_p (e))
484 : 2141 : continue;
485 : :
486 : : /* If we have a non-replaceable definition it prevails. */
487 : 666 : if (!lto_symtab_resolve_replaceable_p (e))
488 : : {
489 : 578 : if (prevailing)
490 : : {
491 : 0 : error_at (DECL_SOURCE_LOCATION (e->decl),
492 : : "%qD has already been defined", e->decl);
493 : 0 : inform (DECL_SOURCE_LOCATION (prevailing->decl),
494 : : "previously defined here");
495 : : }
496 : : prevailing = e;
497 : : }
498 : : }
499 : 1104 : if (prevailing)
500 : : return prevailing;
501 : :
502 : : /* Do a second round choosing one from the replaceable prevailing decls. */
503 : 2120 : for (e = first; e; e = e->next_sharing_asm_name)
504 : : {
505 : 1614 : if (!lto_symtab_resolve_can_prevail_p (e))
506 : 1545 : continue;
507 : :
508 : : /* Choose the first function that can prevail as prevailing. */
509 : 69 : if (TREE_CODE (e->decl) == FUNCTION_DECL)
510 : : {
511 : : prevailing = e;
512 : : break;
513 : : }
514 : :
515 : : /* From variables that can prevail choose the largest one. */
516 : 49 : if (!prevailing
517 : 18 : || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
518 : 18 : DECL_SIZE (e->decl))
519 : : /* When variables are equivalent try to chose one that has useful
520 : : DECL_INITIAL. This makes sense for keyed vtables that are
521 : : DECL_EXTERNAL but initialized. In units that do not need them
522 : : we replace the initializer by error_mark_node to conserve
523 : : memory.
524 : :
525 : : We know that the vtable is keyed outside the LTO unit - otherwise
526 : : the keyed instance would prevail. We still can preserve useful
527 : : info in the initializer. */
528 : 65 : || (DECL_SIZE (prevailing->decl) == DECL_SIZE (e->decl)
529 : 12 : && (DECL_INITIAL (e->decl)
530 : 12 : && DECL_INITIAL (e->decl) != error_mark_node)
531 : 4 : && (!DECL_INITIAL (prevailing->decl)
532 : 4 : || DECL_INITIAL (prevailing->decl) == error_mark_node)))
533 : : prevailing = e;
534 : : }
535 : :
536 : : return prevailing;
537 : : }
538 : :
539 : : /* Decide if it is OK to merge DECL into PREVAILING.
540 : : Because we wrap most of uses of declarations in MEM_REF, we can tolerate
541 : : some differences but other code may inspect directly the DECL. */
542 : :
543 : : static bool
544 : 1626 : lto_symtab_merge_p (tree prevailing, tree decl)
545 : : {
546 : 1626 : if (TREE_CODE (prevailing) != TREE_CODE (decl))
547 : : {
548 : 0 : if (dump_file)
549 : 0 : fprintf (dump_file, "Not merging decls; "
550 : : "TREE_CODE mismatch\n");
551 : 0 : return false;
552 : : }
553 : 1626 : gcc_checking_assert (TREE_CHAIN (prevailing) == TREE_CHAIN (decl));
554 : :
555 : 1626 : if (TREE_CODE (prevailing) == FUNCTION_DECL)
556 : : {
557 : 1092 : if (fndecl_built_in_p (prevailing) != fndecl_built_in_p (decl))
558 : : {
559 : 2 : if (dump_file)
560 : 0 : fprintf (dump_file, "Not merging decls; "
561 : : "DECL_BUILT_IN mismatch\n");
562 : 2 : return false;
563 : : }
564 : 1090 : if (fndecl_built_in_p (prevailing)
565 : 1090 : && (DECL_BUILT_IN_CLASS (prevailing) != DECL_BUILT_IN_CLASS (decl)
566 : 239 : || (DECL_UNCHECKED_FUNCTION_CODE (prevailing)
567 : 239 : != DECL_UNCHECKED_FUNCTION_CODE (decl))))
568 : : {
569 : 0 : if (dump_file)
570 : 0 : fprintf (dump_file, "Not merging decls; "
571 : : "DECL_BUILT_IN_CLASS or CODE mismatch\n");
572 : 0 : return false;
573 : : }
574 : : }
575 : :
576 : 1624 : if (DECL_ATTRIBUTES (prevailing) != DECL_ATTRIBUTES (decl))
577 : : {
578 : 225 : tree prev_attr = lookup_attribute ("error", DECL_ATTRIBUTES (prevailing));
579 : 225 : tree attr = lookup_attribute ("error", DECL_ATTRIBUTES (decl));
580 : 225 : if ((prev_attr == NULL) != (attr == NULL)
581 : 225 : || (prev_attr && !attribute_value_equal (prev_attr, attr)))
582 : : {
583 : 0 : if (dump_file)
584 : 0 : fprintf (dump_file, "Not merging decls; "
585 : : "error attribute mismatch\n");
586 : 0 : return false;
587 : : }
588 : :
589 : 225 : prev_attr = lookup_attribute ("warning", DECL_ATTRIBUTES (prevailing));
590 : 225 : attr = lookup_attribute ("warning", DECL_ATTRIBUTES (decl));
591 : 225 : if ((prev_attr == NULL) != (attr == NULL)
592 : 225 : || (prev_attr && !attribute_value_equal (prev_attr, attr)))
593 : : {
594 : 1 : if (dump_file)
595 : 0 : fprintf (dump_file, "Not merging decls; "
596 : : "warning attribute mismatch\n");
597 : 1 : return false;
598 : : }
599 : :
600 : 224 : prev_attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (prevailing));
601 : 224 : attr = lookup_attribute ("noreturn", DECL_ATTRIBUTES (decl));
602 : 224 : if ((prev_attr == NULL) != (attr == NULL))
603 : : {
604 : 9 : if (dump_file)
605 : 0 : fprintf (dump_file, "Not merging decls; "
606 : : "noreturn attribute mismatch\n");
607 : 9 : return false;
608 : : }
609 : : }
610 : : return true;
611 : : }
612 : :
613 : : /* Merge all decls in the symbol table chain to the prevailing decl and
614 : : issue diagnostics about type mismatches. If DIAGNOSED_P is true
615 : : do not issue further diagnostics.*/
616 : :
617 : : static void
618 : 2021 : lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
619 : : {
620 : 2021 : symtab_node *prevailing;
621 : 2021 : symtab_node *e;
622 : 2021 : vec<tree> mismatches = vNULL;
623 : 2021 : unsigned i;
624 : 2021 : tree decl;
625 : 2021 : bool tbaa_p = false;
626 : :
627 : : /* Nothing to do for a single entry. */
628 : 2021 : prevailing = first;
629 : 2021 : if (!prevailing->next_sharing_asm_name)
630 : 1982 : return;
631 : :
632 : : /* Try to merge each entry with the prevailing one. */
633 : : symtab_node *last_prevailing = prevailing, *next;
634 : 4672 : for (e = prevailing->next_sharing_asm_name; e; e = next)
635 : : {
636 : 2651 : next = e->next_sharing_asm_name;
637 : :
638 : : /* Skip non-LTO symbols and symbols whose declaration we already
639 : : visited. */
640 : 2651 : if (lto_symtab_prevailing_decl (e->decl) != e->decl
641 : 2613 : || !lto_symtab_symbol_p (e)
642 : 5251 : || e->decl == prevailing->decl)
643 : 1025 : continue;
644 : :
645 : 1626 : if (!lto_symtab_merge (prevailing, e)
646 : 48 : && !diagnosed_p
647 : 1674 : && !DECL_ARTIFICIAL (e->decl))
648 : 39 : mismatches.safe_push (e->decl);
649 : :
650 : : symtab_node *this_prevailing;
651 : 0 : for (this_prevailing = prevailing; ;
652 : 0 : this_prevailing = this_prevailing->next_sharing_asm_name)
653 : : {
654 : 1626 : if (this_prevailing->decl != e->decl
655 : 1626 : && lto_symtab_merge_p (this_prevailing->decl, e->decl))
656 : : break;
657 : 12 : if (this_prevailing == last_prevailing)
658 : : {
659 : : this_prevailing = NULL;
660 : : break;
661 : : }
662 : : }
663 : :
664 : 1626 : if (this_prevailing)
665 : 1614 : lto_symtab_prevail_decl (this_prevailing->decl, e->decl);
666 : : /* Maintain LRU list: relink the new prevaililng symbol
667 : : just after previaling node in the chain and update last_prevailing.
668 : : Since the number of possible declarations of a given symbol is
669 : : small, this should be faster than building a hash. */
670 : 12 : else if (e == prevailing->next_sharing_asm_name)
671 : : last_prevailing = e;
672 : : else
673 : : {
674 : 0 : if (e->next_sharing_asm_name)
675 : 0 : e->next_sharing_asm_name->previous_sharing_asm_name
676 : 0 : = e->previous_sharing_asm_name;
677 : 0 : e->previous_sharing_asm_name->next_sharing_asm_name
678 : 0 : = e->next_sharing_asm_name;
679 : 0 : e->previous_sharing_asm_name = prevailing;
680 : 0 : e->next_sharing_asm_name = prevailing->next_sharing_asm_name;
681 : 0 : prevailing->next_sharing_asm_name->previous_sharing_asm_name = e;
682 : 0 : prevailing->next_sharing_asm_name = e;
683 : 0 : if (last_prevailing == prevailing)
684 : 0 : last_prevailing = e;
685 : : }
686 : : }
687 : 2021 : if (mismatches.is_empty ())
688 : : return;
689 : :
690 : : /* Diagnose all mismatched re-declarations. */
691 : 78 : FOR_EACH_VEC_ELT (mismatches, i, decl)
692 : : {
693 : : /* Do not diagnose two built-in declarations, there is no useful
694 : : location in that case. It also happens for AVR if two built-ins
695 : : use the same asm name because their libgcc assembler code is the
696 : : same, see PR78562. */
697 : 39 : if (DECL_IS_UNDECLARED_BUILTIN (prevailing->decl)
698 : 39 : && DECL_IS_UNDECLARED_BUILTIN (decl))
699 : 0 : continue;
700 : :
701 : 39 : int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
702 : 39 : TREE_TYPE (decl),
703 : 39 : DECL_COMDAT (decl));
704 : 39 : if (level)
705 : : {
706 : 39 : bool diag = false;
707 : 39 : if (level & 2)
708 : : {
709 : : /* Silence warning for method and variables which belong
710 : : to types which already have ODR violation reported. Complaining
711 : : once is enough. */
712 : 6 : if (TREE_CODE (decl) != FUNCTION_DECL
713 : 0 : || TREE_CODE (TREE_TYPE (decl)) != METHOD_TYPE
714 : 0 : || !TYPE_METHOD_BASETYPE (TREE_TYPE (decl))
715 : 0 : || !odr_type_p (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)))
716 : 6 : || !odr_type_violation_reported_p
717 : 0 : (TYPE_METHOD_BASETYPE (TREE_TYPE (decl))))
718 : 6 : diag = warning_at (DECL_SOURCE_LOCATION (decl),
719 : : OPT_Wodr,
720 : : "%qD violates the C++ One Definition Rule",
721 : : decl);
722 : : }
723 : 39 : if (!diag && (level & 1))
724 : 33 : diag = warning_at (DECL_SOURCE_LOCATION (decl),
725 : : OPT_Wlto_type_mismatch,
726 : : "type of %qD does not match original "
727 : : "declaration", decl);
728 : 33 : if (diag)
729 : : {
730 : 13 : warn_types_mismatch (TREE_TYPE (prevailing->decl),
731 : 13 : TREE_TYPE (decl),
732 : 13 : DECL_SOURCE_LOCATION (prevailing->decl),
733 : 13 : DECL_SOURCE_LOCATION (decl));
734 : 13 : if ((level & 4)
735 : 13 : && !TREE_READONLY (prevailing->decl))
736 : : tbaa_p = true;
737 : : }
738 : 39 : diagnosed_p |= diag;
739 : : }
740 : 0 : else if ((DECL_USER_ALIGN (prevailing->decl)
741 : 0 : && DECL_USER_ALIGN (decl))
742 : 0 : && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
743 : : {
744 : 0 : diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
745 : : OPT_Wlto_type_mismatch,
746 : : "alignment of %qD is bigger than "
747 : : "original declaration", decl);
748 : : }
749 : : else
750 : 0 : diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
751 : : OPT_Wlto_type_mismatch,
752 : : "size of %qD differ from the size of "
753 : : "original declaration", decl);
754 : : }
755 : 39 : if (diagnosed_p)
756 : 13 : inform (DECL_SOURCE_LOCATION (prevailing->decl),
757 : : "%qD was previously declared here", prevailing->decl);
758 : 39 : if (tbaa_p)
759 : 13 : inform (DECL_SOURCE_LOCATION (prevailing->decl),
760 : : "code may be misoptimized unless "
761 : : "%<-fno-strict-aliasing%> is used");
762 : :
763 : 39 : mismatches.release ();
764 : : }
765 : :
766 : : /* Helper to process the decl chain for the symbol table entry *SLOT. */
767 : :
768 : : static void
769 : 2113 : lto_symtab_merge_decls_1 (symtab_node *first)
770 : : {
771 : 2113 : symtab_node *e;
772 : 2113 : symtab_node *prevailing;
773 : 2113 : bool diagnosed_p = false;
774 : :
775 : 2113 : if (dump_file)
776 : : {
777 : 0 : fprintf (dump_file, "Merging nodes for %s. Candidates:\n",
778 : : first->asm_name ());
779 : 0 : for (e = first; e; e = e->next_sharing_asm_name)
780 : 0 : if (TREE_PUBLIC (e->decl))
781 : 0 : e->dump (dump_file);
782 : : }
783 : :
784 : : /* Compute the symbol resolutions. This is a no-op when using the
785 : : linker plugin and resolution was decided by the linker. */
786 : 2113 : prevailing = lto_symtab_resolve_symbols (first);
787 : :
788 : : /* If there's not a prevailing symbol yet it's an external reference.
789 : : Happens a lot during ltrans. Choose the first symbol with a
790 : : cgraph or a varpool node. */
791 : 2113 : if (!prevailing)
792 : : {
793 : 185 : for (prevailing = first;
794 : 660 : prevailing; prevailing = prevailing->next_sharing_asm_name)
795 : 568 : if (lto_symtab_symbol_p (prevailing))
796 : : break;
797 : 475 : if (!prevailing)
798 : : return;
799 : : /* For variables chose with a priority variant with vnode
800 : : attached (i.e. from unit where external declaration of
801 : : variable is actually used).
802 : : When there are multiple variants, chose one with size.
803 : : This is needed for C++ typeinfos, for example in
804 : : lto/20081204-1 there are typeifos in both units, just
805 : : one of them do have size. */
806 : 383 : if (VAR_P (prevailing->decl))
807 : : {
808 : 77 : for (e = prevailing->next_sharing_asm_name;
809 : 208 : e; e = e->next_sharing_asm_name)
810 : 131 : if (!COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
811 : 23 : && COMPLETE_TYPE_P (TREE_TYPE (e->decl))
812 : 131 : && lto_symtab_symbol_p (e))
813 : : prevailing = e;
814 : : }
815 : : /* For functions prefer the non-builtin if one is available. */
816 : 306 : else if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
817 : : {
818 : 678 : for (e = first; e; e = e->next_sharing_asm_name)
819 : 494 : if (TREE_CODE (e->decl) == FUNCTION_DECL
820 : 494 : && !fndecl_built_in_p (e->decl)
821 : 616 : && lto_symtab_symbol_p (e))
822 : : {
823 : : prevailing = e;
824 : : break;
825 : : }
826 : : }
827 : : }
828 : :
829 : 2021 : symtab->symtab_prevail_in_asm_name_hash (prevailing);
830 : :
831 : : /* Diagnose mismatched objects. */
832 : 2021 : for (e = prevailing->next_sharing_asm_name;
833 : 4672 : e; e = e->next_sharing_asm_name)
834 : : {
835 : 5302 : if (TREE_CODE (prevailing->decl)
836 : 2651 : == TREE_CODE (e->decl))
837 : 2651 : continue;
838 : 0 : if (!lto_symtab_symbol_p (e))
839 : 0 : continue;
840 : :
841 : 0 : switch (TREE_CODE (prevailing->decl))
842 : : {
843 : 0 : case VAR_DECL:
844 : 0 : gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
845 : 0 : error_at (DECL_SOURCE_LOCATION (e->decl),
846 : : "variable %qD redeclared as function",
847 : : prevailing->decl);
848 : 0 : break;
849 : :
850 : 0 : case FUNCTION_DECL:
851 : 0 : gcc_assert (VAR_P (e->decl));
852 : 0 : error_at (DECL_SOURCE_LOCATION (e->decl),
853 : : "function %qD redeclared as variable",
854 : : prevailing->decl);
855 : 0 : break;
856 : :
857 : 0 : default:
858 : 0 : gcc_unreachable ();
859 : : }
860 : :
861 : : diagnosed_p = true;
862 : : }
863 : 2021 : if (diagnosed_p)
864 : 0 : inform (DECL_SOURCE_LOCATION (prevailing->decl),
865 : : "previously declared here");
866 : :
867 : : /* Merge the chain to the single prevailing decl and diagnose
868 : : mismatches. */
869 : 2021 : lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
870 : :
871 : 2021 : if (dump_file)
872 : : {
873 : 0 : fprintf (dump_file, "After resolution:\n");
874 : 0 : for (e = prevailing; e; e = e->next_sharing_asm_name)
875 : 0 : e->dump (dump_file);
876 : : }
877 : : }
878 : :
879 : : /* Resolve and merge all symbol table chains to a prevailing decl. */
880 : :
881 : : void
882 : 12965 : lto_symtab_merge_decls (void)
883 : : {
884 : 12965 : symtab_node *node;
885 : :
886 : 12965 : gcc_assert (!dump_file);
887 : 12965 : dump_file = dump_begin (decl_merge_dump_id, NULL);
888 : :
889 : : /* Populate assembler name hash. */
890 : 12965 : symtab->symtab_initialize_asm_name_hash ();
891 : :
892 : 167683 : FOR_EACH_SYMBOL (node)
893 : 154718 : if (!node->previous_sharing_asm_name
894 : 151975 : && node->next_sharing_asm_name)
895 : 2113 : lto_symtab_merge_decls_1 (node);
896 : :
897 : 12965 : if (dump_file)
898 : 0 : dump_end (decl_merge_dump_id, dump_file);
899 : 12965 : dump_file = NULL;
900 : 12965 : }
901 : :
902 : : /* Helper to process the decl chain for the symbol table entry *SLOT. */
903 : :
904 : : static void
905 : 2021 : lto_symtab_merge_symbols_1 (symtab_node *prevailing)
906 : : {
907 : 2021 : symtab_node *e;
908 : 2021 : symtab_node *next;
909 : :
910 : 2021 : prevailing->decl->decl_with_vis.symtab_node = prevailing;
911 : :
912 : : /* Replace the cgraph node of each entry with the prevailing one. */
913 : 2021 : for (e = prevailing->next_sharing_asm_name; e;
914 : : e = next)
915 : : {
916 : 2651 : next = e->next_sharing_asm_name;
917 : 2651 : cgraph_node *ce = dyn_cast <cgraph_node *> (e);
918 : :
919 : 13 : if ((!TREE_PUBLIC (e->decl) && !DECL_EXTERNAL (e->decl))
920 : 2651 : || (ce != NULL && ce->inlined_to))
921 : 13 : continue;
922 : 2638 : symtab_node *to = symtab_node::get (lto_symtab_prevailing_decl (e->decl));
923 : :
924 : : /* No matter how we are going to deal with resolution, we will ultimately
925 : : use prevailing definition. */
926 : 2638 : if (ce)
927 : 1914 : ipa_merge_profiles (dyn_cast<cgraph_node *> (prevailing),
928 : : dyn_cast<cgraph_node *> (e));
929 : :
930 : : /* If we decided to replace the node by TO, do it. */
931 : 2638 : if (e != to)
932 : : {
933 : 2626 : if (ce)
934 : 3804 : lto_cgraph_replace_node (ce, dyn_cast<cgraph_node *> (to));
935 : 5396 : else if (varpool_node *ve = dyn_cast <varpool_node *> (e))
936 : 1448 : lto_varpool_replace_node (ve, dyn_cast<varpool_node *> (to));
937 : : }
938 : : /* Watch out for duplicated symbols for a given declaration. */
939 : 12 : else if (!e->transparent_alias
940 : 12 : || !e->definition || e->get_alias_target () != to)
941 : : {
942 : : /* We got a new declaration we do not want to merge. In this case
943 : : get rid of the existing definition and create a transparent
944 : : alias. */
945 : 12 : if (ce)
946 : : {
947 : 12 : lto_free_function_in_decl_state_for_node (ce);
948 : 12 : if (!ce->weakref)
949 : 12 : ce->release_body ();
950 : 12 : ce->reset ();
951 : 12 : symtab->call_cgraph_removal_hooks (ce);
952 : : }
953 : : else
954 : : {
955 : 0 : DECL_INITIAL (e->decl) = error_mark_node;
956 : 0 : if (e->lto_file_data)
957 : : {
958 : 0 : lto_free_function_in_decl_state_for_node (e);
959 : 0 : e->lto_file_data = NULL;
960 : : }
961 : 0 : symtab->call_varpool_removal_hooks (dyn_cast<varpool_node *> (e));
962 : : }
963 : 12 : e->remove_all_references ();
964 : 12 : e->analyzed = e->body_removed = false;
965 : 12 : e->resolve_alias (prevailing, true);
966 : 12 : gcc_assert (e != prevailing);
967 : : }
968 : : }
969 : :
970 : 2021 : return;
971 : : }
972 : :
973 : : /* Merge cgraph nodes according to the symbol merging done by
974 : : lto_symtab_merge_decls. */
975 : :
976 : : void
977 : 12965 : lto_symtab_merge_symbols (void)
978 : : {
979 : 12965 : symtab_node *node;
980 : :
981 : 12965 : if (!flag_ltrans)
982 : : {
983 : 12965 : symtab->symtab_initialize_asm_name_hash ();
984 : :
985 : : /* Do the actual merging.
986 : : At this point we invalidate hash translating decls into symtab nodes
987 : : because after removing one of duplicate decls the hash is not correcly
988 : : updated to the other duplicate. */
989 : 167301 : FOR_EACH_SYMBOL (node)
990 : 154336 : if (lto_symtab_symbol_p (node)
991 : 123390 : && node->next_sharing_asm_name
992 : 156951 : && !node->previous_sharing_asm_name)
993 : 2021 : lto_symtab_merge_symbols_1 (node);
994 : :
995 : : /* Resolve weakref aliases whose target are now in the compilation unit.
996 : : also re-populate the hash translating decls into symtab nodes*/
997 : 165659 : FOR_EACH_SYMBOL (node)
998 : : {
999 : 152694 : cgraph_node *cnode, *cnode2;
1000 : 152694 : varpool_node *vnode;
1001 : 152694 : symtab_node *node2;
1002 : :
1003 : 152694 : if (!node->analyzed && node->alias_target)
1004 : : {
1005 : 20 : symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
1006 : 20 : gcc_assert (node->weakref);
1007 : 20 : if (tgt)
1008 : 12 : node->resolve_alias (tgt, true);
1009 : : }
1010 : : /* If the symbol was preempted outside IR, see if we want to get rid
1011 : : of the definition. */
1012 : 152694 : if (node->analyzed
1013 : 129605 : && !DECL_EXTERNAL (node->decl)
1014 : 282173 : && (node->resolution == LDPR_PREEMPTED_REG
1015 : 129471 : || node->resolution == LDPR_RESOLVED_IR
1016 : 129471 : || node->resolution == LDPR_RESOLVED_EXEC
1017 : 129470 : || node->resolution == LDPR_RESOLVED_DYN))
1018 : : {
1019 : : /* If alias to local symbol was preempted by external definition,
1020 : : we know it is not pointing to the local symbol. Remove it. */
1021 : 9 : if (node->alias
1022 : : && !node->weakref
1023 : 9 : && !node->transparent_alias
1024 : 9 : && node->get_alias_target ()->binds_to_current_def_p ())
1025 : : {
1026 : 4 : node->alias = false;
1027 : 4 : node->remove_all_references ();
1028 : 4 : node->definition = false;
1029 : 4 : node->analyzed = false;
1030 : 4 : node->cpp_implicit_alias = false;
1031 : : }
1032 : 5 : else if (!node->alias
1033 : 5 : && node->definition
1034 : 5 : && node->get_availability () <= AVAIL_INTERPOSABLE)
1035 : : {
1036 : 2 : if ((cnode = dyn_cast <cgraph_node *> (node)) != NULL)
1037 : 1 : cnode->reset ();
1038 : : else
1039 : : {
1040 : 1 : node->analyzed = node->definition = false;
1041 : 1 : node->remove_all_references ();
1042 : : }
1043 : : }
1044 : 9 : DECL_EXTERNAL (node->decl) = 1;
1045 : : }
1046 : :
1047 : 152694 : if (!(cnode = dyn_cast <cgraph_node *> (node))
1048 : 108195 : || !cnode->clone_of
1049 : 0 : || cnode->clone_of->decl != cnode->decl)
1050 : : {
1051 : : /* Builtins are not merged via decl merging. It is however
1052 : : possible that tree merging unified the declaration. We
1053 : : do not want duplicate entries in symbol table. */
1054 : 108195 : if (cnode && fndecl_built_in_p (node->decl)
1055 : 17468 : && (cnode2 = cgraph_node::get (node->decl))
1056 : 170162 : && cnode2 != cnode)
1057 : 0 : lto_cgraph_replace_node (cnode2, cnode);
1058 : :
1059 : : /* The user defined assembler variables are also not unified by their
1060 : : symbol name (since it is irrelevant), but we need to unify symbol
1061 : : nodes if tree merging occurred. */
1062 : 152694 : if ((vnode = dyn_cast <varpool_node *> (node))
1063 : 44499 : && DECL_HARD_REGISTER (vnode->decl)
1064 : 6 : && (node2 = symtab_node::get (vnode->decl))
1065 : 6 : && node2 != node)
1066 : 1 : lto_varpool_replace_node (dyn_cast <varpool_node *> (node2),
1067 : : vnode);
1068 : :
1069 : :
1070 : : /* Abstract functions may have duplicated cgraph nodes attached;
1071 : : remove them. */
1072 : 108195 : else if (cnode && DECL_ABSTRACT_P (cnode->decl)
1073 : 0 : && (cnode2 = cgraph_node::get (node->decl))
1074 : 152693 : && cnode2 != cnode)
1075 : 0 : cnode2->remove ();
1076 : :
1077 : 152694 : node->decl->decl_with_vis.symtab_node = node;
1078 : : }
1079 : : }
1080 : : }
1081 : 12965 : }
1082 : :
1083 : : /* Virtual tables may matter for code generation even if they are not
1084 : : directly referenced by the code because they may be used for
1085 : : devirtualization.
1086 : : For this reason it is important to merge even virtual tables that have no
1087 : : associated symbol table entries. Without doing so we lose optimization
1088 : : oppurtunities by losing track of the vtable constructor.
1089 : : FIXME: we probably ought to introduce explicit symbol table entries for
1090 : : those before streaming. */
1091 : :
1092 : : tree
1093 : 999 : lto_symtab_prevailing_virtual_decl (tree decl)
1094 : : {
1095 : 999 : if (DECL_ABSTRACT_P (decl))
1096 : : return decl;
1097 : :
1098 : 656 : if (type_in_anonymous_namespace_p (DECL_CONTEXT (decl)))
1099 : : /* There can't be any other declarations. */
1100 : : return decl;
1101 : :
1102 : 655 : gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
1103 : :
1104 : 655 : symtab_node *n = symtab_node::get_for_asmname
1105 : 655 : (DECL_ASSEMBLER_NAME (decl));
1106 : 1310 : while (n && ((!DECL_EXTERNAL (n->decl) && !TREE_PUBLIC (n->decl))
1107 : 52 : || !DECL_VIRTUAL_P (n->decl)))
1108 : 0 : n = n->next_sharing_asm_name;
1109 : 655 : if (n)
1110 : : {
1111 : : /* Merge decl state in both directions, we may still end up using
1112 : : the other decl. */
1113 : 52 : TREE_ADDRESSABLE (n->decl) |= TREE_ADDRESSABLE (decl);
1114 : 52 : TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (n->decl);
1115 : :
1116 : 52 : if (TREE_CODE (decl) == FUNCTION_DECL)
1117 : : {
1118 : : /* Merge decl state in both directions, we may still end up using
1119 : : the other decl. */
1120 : 26 : DECL_POSSIBLY_INLINED (n->decl) |= DECL_POSSIBLY_INLINED (decl);
1121 : 26 : DECL_POSSIBLY_INLINED (decl) |= DECL_POSSIBLY_INLINED (n->decl);
1122 : : }
1123 : 52 : lto_symtab_prevail_decl (n->decl, decl);
1124 : 52 : decl = n->decl;
1125 : : }
1126 : : else
1127 : 603 : symtab_node::get_create (decl);
1128 : :
1129 : : return decl;
1130 : : }
|