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