]>
Commit | Line | Data |
---|---|---|
eefe9a99 JH |
1 | /* Basic IPA utilities for type inheritance graph construction and |
2 | devirtualization. | |
8d9254fc | 3 | Copyright (C) 2013-2020 Free Software Foundation, Inc. |
eefe9a99 JH |
4 | Contributed by Jan Hubicka |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
d6ae9a6d | 22 | /* Brief vocabulary: |
eefe9a99 JH |
23 | ODR = One Definition Rule |
24 | In short, the ODR states that: | |
25 | 1 In any translation unit, a template, type, function, or object can | |
26 | have no more than one definition. Some of these can have any number | |
27 | of declarations. A definition provides an instance. | |
28 | 2 In the entire program, an object or non-inline function cannot have | |
29 | more than one definition; if an object or function is used, it must | |
30 | have exactly one definition. You can declare an object or function | |
31 | that is never used, in which case you don't have to provide | |
32 | a definition. In no event can there be more than one definition. | |
33 | 3 Some things, like types, templates, and extern inline functions, can | |
34 | be defined in more than one translation unit. For a given entity, | |
35 | each definition must be the same. Non-extern objects and functions | |
36 | in different translation units are different entities, even if their | |
37 | names and types are the same. | |
38 | ||
39 | OTR = OBJ_TYPE_REF | |
0e1474e5 | 40 | This is the Gimple representation of type information of a polymorphic call. |
eefe9a99 JH |
41 | It contains two parameters: |
42 | otr_type is a type of class whose method is called. | |
0e1474e5 | 43 | otr_token is the index into virtual table where address is taken. |
eefe9a99 JH |
44 | |
45 | BINFO | |
46 | This is the type inheritance information attached to each tree | |
d6ae9a6d | 47 | RECORD_TYPE by the C++ frontend. It provides information about base |
eefe9a99 JH |
48 | types and virtual tables. |
49 | ||
50 | BINFO is linked to the RECORD_TYPE by TYPE_BINFO. | |
51 | BINFO also links to its type by BINFO_TYPE and to the virtual table by | |
52 | BINFO_VTABLE. | |
53 | ||
54 | Base types of a given type are enumerated by BINFO_BASE_BINFO | |
55 | vector. Members of this vectors are not BINFOs associated | |
56 | with a base type. Rather they are new copies of BINFOs | |
57 | (base BINFOs). Their virtual tables may differ from | |
0e1474e5 | 58 | virtual table of the base type. Also BINFO_OFFSET specifies |
eefe9a99 JH |
59 | offset of the base within the type. |
60 | ||
61 | In the case of single inheritance, the virtual table is shared | |
62 | and BINFO_VTABLE of base BINFO is NULL. In the case of multiple | |
63 | inheritance the individual virtual tables are pointer to by | |
609570b4 | 64 | BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of |
eefe9a99 JH |
65 | binfo associated to the base type). |
66 | ||
67 | BINFO lookup for a given base type and offset can be done by | |
68 | get_binfo_at_offset. It returns proper BINFO whose virtual table | |
69 | can be used for lookup of virtual methods associated with the | |
70 | base type. | |
71 | ||
72 | token | |
73 | This is an index of virtual method in virtual table associated | |
74 | to the type defining it. Token can be looked up from OBJ_TYPE_REF | |
0e1474e5 | 75 | or from DECL_VINDEX of a given virtual table. |
eefe9a99 JH |
76 | |
77 | polymorphic (indirect) call | |
d6ae9a6d | 78 | This is callgraph representation of virtual method call. Every |
eefe9a99 JH |
79 | polymorphic call contains otr_type and otr_token taken from |
80 | original OBJ_TYPE_REF at callgraph construction time. | |
81 | ||
82 | What we do here: | |
83 | ||
84 | build_type_inheritance_graph triggers a construction of the type inheritance | |
85 | graph. | |
86 | ||
87 | We reconstruct it based on types of methods we see in the unit. | |
88 | This means that the graph is not complete. Types with no methods are not | |
0e1474e5 | 89 | inserted into the graph. Also types without virtual methods are not |
eefe9a99 | 90 | represented at all, though it may be easy to add this. |
3fb68f2e | 91 | |
eefe9a99 JH |
92 | The inheritance graph is represented as follows: |
93 | ||
94 | Vertices are structures odr_type. Every odr_type may correspond | |
95 | to one or more tree type nodes that are equivalent by ODR rule. | |
96 | (the multiple type nodes appear only with linktime optimization) | |
97 | ||
0e1474e5 | 98 | Edges are represented by odr_type->base and odr_type->derived_types. |
eefe9a99 JH |
99 | At the moment we do not track offsets of types for multiple inheritance. |
100 | Adding this is easy. | |
101 | ||
102 | possible_polymorphic_call_targets returns, given an parameters found in | |
103 | indirect polymorphic edge all possible polymorphic call targets of the call. | |
bbc9396b JH |
104 | |
105 | pass_ipa_devirt performs simple speculative devirtualization. | |
eefe9a99 JH |
106 | */ |
107 | ||
108 | #include "config.h" | |
109 | #include "system.h" | |
110 | #include "coretypes.h" | |
c7131fb2 | 111 | #include "backend.h" |
957060b5 | 112 | #include "rtl.h" |
4d648807 | 113 | #include "tree.h" |
c7131fb2 | 114 | #include "gimple.h" |
957060b5 AM |
115 | #include "alloc-pool.h" |
116 | #include "tree-pass.h" | |
957060b5 AM |
117 | #include "cgraph.h" |
118 | #include "lto-streamer.h" | |
40e23961 | 119 | #include "fold-const.h" |
d8a2d370 DN |
120 | #include "print-tree.h" |
121 | #include "calls.h" | |
eefe9a99 | 122 | #include "ipa-utils.h" |
2fb9a547 | 123 | #include "gimple-fold.h" |
dd912cb8 | 124 | #include "symbol-summary.h" |
8bc5448f | 125 | #include "tree-vrp.h" |
c582198b | 126 | #include "ipa-prop.h" |
27d020cf | 127 | #include "ipa-fnsummary.h" |
ec77d61f | 128 | #include "demangle.h" |
2b5f0895 | 129 | #include "dbgcnt.h" |
7d0aa05b | 130 | #include "gimple-pretty-print.h" |
c59f7203 | 131 | #include "intl.h" |
314e6352 ML |
132 | #include "stringpool.h" |
133 | #include "attribs.h" | |
3fb68f2e JH |
134 | #include "data-streamer.h" |
135 | #include "lto-streamer.h" | |
136 | #include "streamer-hooks.h" | |
c59f7203 | 137 | |
2c132d34 | 138 | /* Hash based set of pairs of types. */ |
50686850 | 139 | struct type_pair |
2c132d34 JH |
140 | { |
141 | tree first; | |
142 | tree second; | |
50686850 | 143 | }; |
2c132d34 | 144 | |
b32ca1df | 145 | template <> |
6c38fbc6 NS |
146 | struct default_hash_traits <type_pair> |
147 | : typed_noop_remove <type_pair> | |
2c132d34 | 148 | { |
6c38fbc6 NS |
149 | GTY((skip)) typedef type_pair value_type; |
150 | GTY((skip)) typedef type_pair compare_type; | |
2c132d34 JH |
151 | static hashval_t |
152 | hash (type_pair p) | |
153 | { | |
154 | return TYPE_UID (p.first) ^ TYPE_UID (p.second); | |
155 | } | |
7ca50de0 | 156 | static const bool empty_zero_p = true; |
2c132d34 JH |
157 | static bool |
158 | is_empty (type_pair p) | |
159 | { | |
160 | return p.first == NULL; | |
161 | } | |
162 | static bool | |
163 | is_deleted (type_pair p ATTRIBUTE_UNUSED) | |
164 | { | |
165 | return false; | |
166 | } | |
167 | static bool | |
168 | equal (const type_pair &a, const type_pair &b) | |
169 | { | |
170 | return a.first==b.first && a.second == b.second; | |
171 | } | |
172 | static void | |
173 | mark_empty (type_pair &e) | |
174 | { | |
175 | e.first = NULL; | |
176 | } | |
177 | }; | |
178 | ||
2bc2379b JH |
179 | /* HACK alert: this is used to communicate with ipa-inline-transform that |
180 | thunk is being expanded and there is no need to clear the polymorphic | |
181 | call target cache. */ | |
182 | bool thunk_expansion; | |
183 | ||
6e2830c3 | 184 | static bool odr_types_equivalent_p (tree, tree, bool, bool *, |
b32ca1df | 185 | hash_set<type_pair> *, |
6542950e | 186 | location_t, location_t); |
420672bc JH |
187 | static void warn_odr (tree t1, tree t2, tree st1, tree st2, |
188 | bool warn, bool *warned, const char *reason); | |
ec77d61f JH |
189 | |
190 | static bool odr_violation_reported = false; | |
68377e53 | 191 | |
eefe9a99 | 192 | |
0e1474e5 | 193 | /* Pointer set of all call targets appearing in the cache. */ |
6e2830c3 | 194 | static hash_set<cgraph_node *> *cached_polymorphic_call_targets; |
0e1474e5 | 195 | |
eefe9a99 | 196 | /* The node of type inheritance graph. For each type unique in |
609570b4 | 197 | One Definition Rule (ODR) sense, we produce one node linking all |
eefe9a99 JH |
198 | main variants of types equivalent to it, bases and derived types. */ |
199 | ||
200 | struct GTY(()) odr_type_d | |
201 | { | |
eefe9a99 JH |
202 | /* leader type. */ |
203 | tree type; | |
d6ae9a6d | 204 | /* All bases; built only for main variants of types. */ |
eefe9a99 | 205 | vec<odr_type> GTY((skip)) bases; |
d6ae9a6d SL |
206 | /* All derived types with virtual methods seen in unit; |
207 | built only for main variants of types. */ | |
eefe9a99 | 208 | vec<odr_type> GTY((skip)) derived_types; |
0e1474e5 | 209 | |
61a74079 JH |
210 | /* All equivalent types, if more than one. */ |
211 | vec<tree, va_gc> *types; | |
212 | /* Set of all equivalent types, if NON-NULL. */ | |
6e2830c3 | 213 | hash_set<tree> * GTY((skip)) types_set; |
61a74079 | 214 | |
0e1474e5 JH |
215 | /* Unique ID indexing the type in odr_types array. */ |
216 | int id; | |
eefe9a99 JH |
217 | /* Is it in anonymous namespace? */ |
218 | bool anonymous_namespace; | |
2d1644bf JH |
219 | /* Do we know about all derivations of given type? */ |
220 | bool all_derivations_known; | |
549bcbd1 JH |
221 | /* Did we report ODR violation here? */ |
222 | bool odr_violated; | |
956d615d | 223 | /* Set when virtual table without RTTI prevailed table with. */ |
b1905808 | 224 | bool rtti_broken; |
a0276c00 JH |
225 | /* Set when the canonical type is determined using the type name. */ |
226 | bool tbaa_enabled; | |
eefe9a99 JH |
227 | }; |
228 | ||
2d1644bf JH |
229 | /* Return TRUE if all derived types of T are known and thus |
230 | we may consider the walk of derived type complete. | |
231 | ||
232 | This is typically true only for final anonymous namespace types and types | |
233 | defined within functions (that may be COMDAT and thus shared across units, | |
234 | but with the same set of derived types). */ | |
235 | ||
aa803cc7 JH |
236 | bool |
237 | type_all_derivations_known_p (const_tree t) | |
2d1644bf JH |
238 | { |
239 | if (TYPE_FINAL_P (t)) | |
240 | return true; | |
241 | if (flag_ltrans) | |
242 | return false; | |
5ce97055 JH |
243 | /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */ |
244 | if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL) | |
245 | return true; | |
2d1644bf JH |
246 | if (type_in_anonymous_namespace_p (t)) |
247 | return true; | |
248 | return (decl_function_context (TYPE_NAME (t)) != NULL); | |
249 | } | |
250 | ||
d6ae9a6d | 251 | /* Return TRUE if type's constructors are all visible. */ |
2d1644bf JH |
252 | |
253 | static bool | |
254 | type_all_ctors_visible_p (tree t) | |
255 | { | |
256 | return !flag_ltrans | |
3dafb85c | 257 | && symtab->state >= CONSTRUCTION |
67914693 | 258 | /* We cannot always use type_all_derivations_known_p. |
2d1644bf | 259 | For function local types we must assume case where |
609570b4 | 260 | the function is COMDAT and shared in between units. |
2d1644bf JH |
261 | |
262 | TODO: These cases are quite easy to get, but we need | |
263 | to keep track of C++ privatizing via -Wno-weak | |
264 | as well as the IPA privatizing. */ | |
265 | && type_in_anonymous_namespace_p (t); | |
266 | } | |
267 | ||
268 | /* Return TRUE if type may have instance. */ | |
269 | ||
270 | static bool | |
271 | type_possibly_instantiated_p (tree t) | |
272 | { | |
273 | tree vtable; | |
274 | varpool_node *vnode; | |
275 | ||
276 | /* TODO: Add abstract types here. */ | |
277 | if (!type_all_ctors_visible_p (t)) | |
278 | return true; | |
279 | ||
280 | vtable = BINFO_VTABLE (TYPE_BINFO (t)); | |
281 | if (TREE_CODE (vtable) == POINTER_PLUS_EXPR) | |
282 | vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0); | |
9041d2e6 | 283 | vnode = varpool_node::get (vtable); |
2d1644bf JH |
284 | return vnode && vnode->definition; |
285 | } | |
286 | ||
609570b4 JH |
287 | /* Hash used to unify ODR types based on their mangled name and for anonymous |
288 | namespace types. */ | |
eefe9a99 | 289 | |
7edd9b15 | 290 | struct odr_name_hasher : pointer_hash <odr_type_d> |
eefe9a99 | 291 | { |
67f58944 TS |
292 | typedef union tree_node *compare_type; |
293 | static inline hashval_t hash (const odr_type_d *); | |
294 | static inline bool equal (const odr_type_d *, const tree_node *); | |
295 | static inline void remove (odr_type_d *); | |
eefe9a99 JH |
296 | }; |
297 | ||
609570b4 JH |
298 | static bool |
299 | can_be_name_hashed_p (tree t) | |
300 | { | |
e7a677ca | 301 | return (!in_lto_p || odr_type_p (t)); |
609570b4 JH |
302 | } |
303 | ||
304 | /* Hash type by its ODR name. */ | |
eefe9a99 | 305 | |
549bcbd1 | 306 | static hashval_t |
609570b4 | 307 | hash_odr_name (const_tree t) |
eefe9a99 | 308 | { |
1afca3f4 | 309 | gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t); |
eefe9a99 JH |
310 | |
311 | /* If not in LTO, all main variants are unique, so we can do | |
312 | pointer hash. */ | |
313 | if (!in_lto_p) | |
314 | return htab_hash_pointer (t); | |
315 | ||
316 | /* Anonymous types are unique. */ | |
e7a677ca | 317 | if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t)) |
eefe9a99 JH |
318 | return htab_hash_pointer (t); |
319 | ||
609570b4 JH |
320 | gcc_checking_assert (TYPE_NAME (t) |
321 | && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))); | |
322 | return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t))); | |
323 | } | |
1ee85ee1 | 324 | |
609570b4 | 325 | /* Return the computed hashcode for ODR_TYPE. */ |
61a74079 | 326 | |
609570b4 | 327 | inline hashval_t |
67f58944 | 328 | odr_name_hasher::hash (const odr_type_d *odr_type) |
609570b4 JH |
329 | { |
330 | return hash_odr_name (odr_type->type); | |
331 | } | |
332 | ||
549bcbd1 JH |
333 | /* For languages with One Definition Rule, work out if |
334 | types are the same based on their name. | |
609570b4 | 335 | |
d6ae9a6d | 336 | This is non-trivial for LTO where minor differences in |
549bcbd1 JH |
337 | the type representation may have prevented type merging |
338 | to merge two copies of otherwise equivalent type. | |
339 | ||
340 | Until we start streaming mangled type names, this function works | |
609570b4 | 341 | only for polymorphic types. |
609570b4 | 342 | */ |
549bcbd1 JH |
343 | |
344 | bool | |
420672bc | 345 | types_same_for_odr (const_tree type1, const_tree type2) |
549bcbd1 JH |
346 | { |
347 | gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2)); | |
348 | ||
420672bc JH |
349 | type1 = TYPE_MAIN_VARIANT (type1); |
350 | type2 = TYPE_MAIN_VARIANT (type2); | |
549bcbd1 JH |
351 | |
352 | if (type1 == type2) | |
353 | return true; | |
354 | ||
355 | if (!in_lto_p) | |
356 | return false; | |
357 | ||
1afca3f4 | 358 | /* Anonymous namespace types are never duplicated. */ |
e7a677ca JH |
359 | if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1)) |
360 | || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2))) | |
549bcbd1 JH |
361 | return false; |
362 | ||
efeeda75 ML |
363 | /* If both type has mangled defined check if they are same. |
364 | Watch for anonymous types which are all mangled as "<anon">. */ | |
365 | if (!type_with_linkage_p (type1) || !type_with_linkage_p (type2)) | |
366 | return false; | |
367 | if (type_in_anonymous_namespace_p (type1) | |
368 | || type_in_anonymous_namespace_p (type2)) | |
369 | return false; | |
1ee85ee1 JH |
370 | return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1)) |
371 | == DECL_ASSEMBLER_NAME (TYPE_NAME (type2))); | |
549bcbd1 JH |
372 | } |
373 | ||
a1981458 JH |
374 | /* Return true if we can decide on ODR equivalency. |
375 | ||
376 | In non-LTO it is always decide, in LTO however it depends in the type has | |
420672bc | 377 | ODR info attached. */ |
a1981458 | 378 | |
aa803cc7 | 379 | bool |
420672bc | 380 | types_odr_comparable (tree t1, tree t2) |
a1981458 JH |
381 | { |
382 | return (!in_lto_p | |
420672bc JH |
383 | || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2) |
384 | || (odr_type_p (TYPE_MAIN_VARIANT (t1)) | |
686a56a8 | 385 | && odr_type_p (TYPE_MAIN_VARIANT (t2)))); |
a1981458 JH |
386 | } |
387 | ||
388 | /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not | |
389 | known, be conservative and return false. */ | |
390 | ||
aa803cc7 | 391 | bool |
a1981458 JH |
392 | types_must_be_same_for_odr (tree t1, tree t2) |
393 | { | |
394 | if (types_odr_comparable (t1, t2)) | |
395 | return types_same_for_odr (t1, t2); | |
396 | else | |
609570b4 | 397 | return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2); |
a1981458 | 398 | } |
549bcbd1 | 399 | |
259d29e3 JH |
400 | /* If T is compound type, return type it is based on. */ |
401 | ||
402 | static tree | |
403 | compound_type_base (const_tree t) | |
404 | { | |
405 | if (TREE_CODE (t) == ARRAY_TYPE | |
406 | || POINTER_TYPE_P (t) | |
407 | || TREE_CODE (t) == COMPLEX_TYPE | |
408 | || VECTOR_TYPE_P (t)) | |
409 | return TREE_TYPE (t); | |
410 | if (TREE_CODE (t) == METHOD_TYPE) | |
411 | return TYPE_METHOD_BASETYPE (t); | |
412 | if (TREE_CODE (t) == OFFSET_TYPE) | |
413 | return TYPE_OFFSET_BASETYPE (t); | |
414 | return NULL_TREE; | |
415 | } | |
416 | ||
417 | /* Return true if T is either ODR type or compound type based from it. | |
418 | If the function return true, we know that T is a type originating from C++ | |
419 | source even at link-time. */ | |
420 | ||
421 | bool | |
422 | odr_or_derived_type_p (const_tree t) | |
423 | { | |
424 | do | |
425 | { | |
420672bc | 426 | if (odr_type_p (TYPE_MAIN_VARIANT (t))) |
259d29e3 JH |
427 | return true; |
428 | /* Function type is a tricky one. Basically we can consider it | |
429 | ODR derived if return type or any of the parameters is. | |
430 | We need to check all parameters because LTO streaming merges | |
431 | common types (such as void) and they are not considered ODR then. */ | |
432 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
433 | { | |
434 | if (TYPE_METHOD_BASETYPE (t)) | |
435 | t = TYPE_METHOD_BASETYPE (t); | |
436 | else | |
437 | { | |
438 | if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t))) | |
439 | return true; | |
440 | for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t)) | |
420672bc | 441 | if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t)))) |
259d29e3 JH |
442 | return true; |
443 | return false; | |
444 | } | |
445 | } | |
446 | else | |
447 | t = compound_type_base (t); | |
448 | } | |
449 | while (t); | |
450 | return t; | |
451 | } | |
452 | ||
0e1474e5 | 453 | /* Compare types T1 and T2 and return true if they are |
eefe9a99 JH |
454 | equivalent. */ |
455 | ||
456 | inline bool | |
67f58944 | 457 | odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2) |
eefe9a99 | 458 | { |
609570b4 | 459 | tree t1 = o1->type; |
eefe9a99 | 460 | |
1afca3f4 JH |
461 | gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2); |
462 | gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1); | |
609570b4 | 463 | if (t1 == t2) |
eefe9a99 JH |
464 | return true; |
465 | if (!in_lto_p) | |
466 | return false; | |
420672bc | 467 | /* Check for anonymous namespaces. */ |
e7a677ca JH |
468 | if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1)) |
469 | || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2))) | |
609570b4 JH |
470 | return false; |
471 | gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))); | |
472 | gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))); | |
473 | return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)) | |
474 | == DECL_ASSEMBLER_NAME (TYPE_NAME (t2))); | |
475 | } | |
476 | ||
0e1474e5 | 477 | /* Free ODR type V. */ |
eefe9a99 JH |
478 | |
479 | inline void | |
67f58944 | 480 | odr_name_hasher::remove (odr_type_d *v) |
eefe9a99 JH |
481 | { |
482 | v->bases.release (); | |
483 | v->derived_types.release (); | |
61a74079 | 484 | if (v->types_set) |
6e2830c3 | 485 | delete v->types_set; |
eefe9a99 JH |
486 | ggc_free (v); |
487 | } | |
488 | ||
d6ae9a6d | 489 | /* ODR type hash used to look up ODR type based on tree type node. */ |
eefe9a99 | 490 | |
609570b4 | 491 | typedef hash_table<odr_name_hasher> odr_hash_type; |
c203e8a7 | 492 | static odr_hash_type *odr_hash; |
eefe9a99 JH |
493 | |
494 | /* ODR types are also stored into ODR_TYPE vector to allow consistent | |
495 | walking. Bases appear before derived types. Vector is garbage collected | |
496 | so we won't end up visiting empty types. */ | |
497 | ||
498 | static GTY(()) vec <odr_type, va_gc> *odr_types_ptr; | |
499 | #define odr_types (*odr_types_ptr) | |
500 | ||
3fb68f2e JH |
501 | /* All enums defined and accessible for the unit. */ |
502 | static GTY(()) vec <tree, va_gc> *odr_enums; | |
503 | ||
504 | /* Information we hold about value defined by an enum type. */ | |
505 | struct odr_enum_val | |
506 | { | |
507 | const char *name; | |
eca7a60b | 508 | wide_int val; |
3fb68f2e JH |
509 | location_t locus; |
510 | }; | |
511 | ||
512 | /* Information about enum values. */ | |
513 | struct odr_enum | |
514 | { | |
515 | location_t locus; | |
516 | auto_vec<odr_enum_val, 0> vals; | |
517 | bool warned; | |
518 | }; | |
519 | ||
520 | /* A table of all ODR enum definitions. */ | |
521 | static hash_map <nofree_string_hash, odr_enum> *odr_enum_map = NULL; | |
522 | static struct obstack odr_enum_obstack; | |
523 | ||
c7e1befa JH |
524 | /* Set TYPE_BINFO of TYPE and its variants to BINFO. */ |
525 | void | |
526 | set_type_binfo (tree type, tree binfo) | |
527 | { | |
528 | for (; type; type = TYPE_NEXT_VARIANT (type)) | |
529 | if (COMPLETE_TYPE_P (type)) | |
530 | TYPE_BINFO (type) = binfo; | |
531 | else | |
532 | gcc_assert (!TYPE_BINFO (type)); | |
533 | } | |
534 | ||
420672bc JH |
535 | /* Return true if type variants match. |
536 | This assumes that we already verified that T1 and T2 are variants of the | |
537 | same type. */ | |
538 | ||
539 | static bool | |
bbbef996 | 540 | type_variants_equivalent_p (tree t1, tree t2) |
420672bc JH |
541 | { |
542 | if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) | |
bbbef996 | 543 | return false; |
420672bc JH |
544 | |
545 | if (comp_type_attributes (t1, t2) != 1) | |
bbbef996 | 546 | return false; |
420672bc JH |
547 | |
548 | if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2) | |
549 | && TYPE_ALIGN (t1) != TYPE_ALIGN (t2)) | |
bbbef996 | 550 | return false; |
420672bc JH |
551 | |
552 | return true; | |
553 | } | |
554 | ||
59a8062a | 555 | /* Compare T1 and T2 based on name or structure. */ |
c59f7203 JH |
556 | |
557 | static bool | |
bbbef996 | 558 | odr_subtypes_equivalent_p (tree t1, tree t2, |
b32ca1df | 559 | hash_set<type_pair> *visited, |
6542950e | 560 | location_t loc1, location_t loc2) |
c59f7203 | 561 | { |
c59f7203 JH |
562 | |
563 | /* This can happen in incomplete types that should be handled earlier. */ | |
564 | gcc_assert (t1 && t2); | |
565 | ||
c59f7203 JH |
566 | if (t1 == t2) |
567 | return true; | |
c59f7203 JH |
568 | |
569 | /* Anonymous namespace types must match exactly. */ | |
420672bc JH |
570 | if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1)) |
571 | && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1))) | |
572 | || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2)) | |
573 | && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2)))) | |
c59f7203 JH |
574 | return false; |
575 | ||
9d8fc086 | 576 | /* For ODR types be sure to compare their names. |
59a8062a | 577 | To support -Wno-odr-type-merging we allow one type to be non-ODR |
9d8fc086 | 578 | and other ODR even though it is a violation. */ |
420672bc | 579 | if (types_odr_comparable (t1, t2)) |
c59f7203 | 580 | { |
7656fa3e JH |
581 | if (t1 != t2 |
582 | && odr_type_p (TYPE_MAIN_VARIANT (t1)) | |
583 | && get_odr_type (TYPE_MAIN_VARIANT (t1), true)->odr_violated) | |
584 | return false; | |
420672bc | 585 | if (!types_same_for_odr (t1, t2)) |
2c132d34 | 586 | return false; |
bbbef996 | 587 | if (!type_variants_equivalent_p (t1, t2)) |
420672bc | 588 | return false; |
3c674e3e | 589 | /* Limit recursion: If subtypes are ODR types and we know |
6867cd69 | 590 | that they are same, be happy. */ |
7656fa3e | 591 | if (odr_type_p (TYPE_MAIN_VARIANT (t1))) |
2c132d34 | 592 | return true; |
c59f7203 | 593 | } |
1ee85ee1 | 594 | |
d6ae9a6d | 595 | /* Component types, builtins and possibly violating ODR types |
2c132d34 JH |
596 | have to be compared structurally. */ |
597 | if (TREE_CODE (t1) != TREE_CODE (t2)) | |
598 | return false; | |
32496fdd JH |
599 | if (AGGREGATE_TYPE_P (t1) |
600 | && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE)) | |
2c132d34 | 601 | return false; |
2c132d34 | 602 | |
420672bc JH |
603 | type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)}; |
604 | if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2))) | |
2c132d34 | 605 | { |
420672bc JH |
606 | pair.first = TYPE_MAIN_VARIANT (t2); |
607 | pair.second = TYPE_MAIN_VARIANT (t1); | |
2c132d34 JH |
608 | } |
609 | if (visited->add (pair)) | |
610 | return true; | |
abb967da JH |
611 | if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2), |
612 | false, NULL, visited, loc1, loc2)) | |
613 | return false; | |
bbbef996 | 614 | if (!type_variants_equivalent_p (t1, t2)) |
420672bc JH |
615 | return false; |
616 | return true; | |
c59f7203 JH |
617 | } |
618 | ||
f8a1abf8 JH |
619 | /* Return true if DECL1 and DECL2 are identical methods. Consider |
620 | name equivalent to name.localalias.xyz. */ | |
621 | ||
622 | static bool | |
623 | methods_equal_p (tree decl1, tree decl2) | |
624 | { | |
625 | if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2)) | |
626 | return true; | |
627 | const char sep = symbol_table::symbol_suffix_separator (); | |
628 | ||
629 | const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1)); | |
630 | const char *ptr1 = strchr (name1, sep); | |
631 | int len1 = ptr1 ? ptr1 - name1 : strlen (name1); | |
632 | ||
633 | const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2)); | |
634 | const char *ptr2 = strchr (name2, sep); | |
635 | int len2 = ptr2 ? ptr2 - name2 : strlen (name2); | |
636 | ||
637 | if (len1 != len2) | |
638 | return false; | |
639 | return !strncmp (name1, name2, len1); | |
640 | } | |
641 | ||
56b1f114 | 642 | /* Compare two virtual tables, PREVAILING and VTABLE and output ODR |
d6ae9a6d | 643 | violation warnings. */ |
56b1f114 JH |
644 | |
645 | void | |
646 | compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable) | |
647 | { | |
648 | int n1, n2; | |
b1905808 | 649 | |
56b1f114 JH |
650 | if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl)) |
651 | { | |
652 | odr_violation_reported = true; | |
653 | if (DECL_VIRTUAL_P (prevailing->decl)) | |
654 | { | |
655 | varpool_node *tmp = prevailing; | |
656 | prevailing = vtable; | |
657 | vtable = tmp; | |
658 | } | |
097f82ec | 659 | auto_diagnostic_group d; |
88b16508 JH |
660 | if (warning_at (DECL_SOURCE_LOCATION |
661 | (TYPE_NAME (DECL_CONTEXT (vtable->decl))), | |
56b1f114 JH |
662 | OPT_Wodr, |
663 | "virtual table of type %qD violates one definition rule", | |
664 | DECL_CONTEXT (vtable->decl))) | |
665 | inform (DECL_SOURCE_LOCATION (prevailing->decl), | |
666 | "variable of same assembler name as the virtual table is " | |
667 | "defined in another translation unit"); | |
668 | return; | |
669 | } | |
670 | if (!prevailing->definition || !vtable->definition) | |
671 | return; | |
b1905808 JH |
672 | |
673 | /* If we do not stream ODR type info, do not bother to do useful compare. */ | |
674 | if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl)) | |
675 | || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl)))) | |
676 | return; | |
677 | ||
678 | odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true); | |
679 | ||
680 | if (class_type->odr_violated) | |
681 | return; | |
682 | ||
56b1f114 JH |
683 | for (n1 = 0, n2 = 0; true; n1++, n2++) |
684 | { | |
685 | struct ipa_ref *ref1, *ref2; | |
686 | bool end1, end2; | |
88b16508 | 687 | |
56b1f114 JH |
688 | end1 = !prevailing->iterate_reference (n1, ref1); |
689 | end2 = !vtable->iterate_reference (n2, ref2); | |
88b16508 JH |
690 | |
691 | /* !DECL_VIRTUAL_P means RTTI entry; | |
956d615d | 692 | We warn when RTTI is lost because non-RTTI prevails; we silently |
88b16508 JH |
693 | accept the other case. */ |
694 | while (!end2 | |
695 | && (end1 | |
f8a1abf8 JH |
696 | || (methods_equal_p (ref1->referred->decl, |
697 | ref2->referred->decl) | |
b1905808 JH |
698 | && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)) |
699 | && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL) | |
56b1f114 | 700 | { |
097f82ec | 701 | if (!class_type->rtti_broken) |
56b1f114 | 702 | { |
097f82ec DM |
703 | auto_diagnostic_group d; |
704 | if (warning_at (DECL_SOURCE_LOCATION | |
705 | (TYPE_NAME (DECL_CONTEXT (vtable->decl))), | |
706 | OPT_Wodr, | |
707 | "virtual table of type %qD contains RTTI " | |
708 | "information", | |
709 | DECL_CONTEXT (vtable->decl))) | |
710 | { | |
711 | inform (DECL_SOURCE_LOCATION | |
712 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), | |
713 | "but is prevailed by one without from other" | |
714 | " translation unit"); | |
715 | inform (DECL_SOURCE_LOCATION | |
716 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), | |
717 | "RTTI will not work on this type"); | |
718 | class_type->rtti_broken = true; | |
719 | } | |
56b1f114 JH |
720 | } |
721 | n2++; | |
722 | end2 = !vtable->iterate_reference (n2, ref2); | |
723 | } | |
88b16508 JH |
724 | while (!end1 |
725 | && (end2 | |
f8a1abf8 | 726 | || (methods_equal_p (ref2->referred->decl, ref1->referred->decl) |
b1905808 JH |
727 | && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL)) |
728 | && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL) | |
56b1f114 JH |
729 | { |
730 | n1++; | |
b1905808 | 731 | end1 = !prevailing->iterate_reference (n1, ref1); |
56b1f114 | 732 | } |
88b16508 JH |
733 | |
734 | /* Finished? */ | |
735 | if (end1 && end2) | |
736 | { | |
737 | /* Extra paranoia; compare the sizes. We do not have information | |
738 | about virtual inheritance offsets, so just be sure that these | |
609570b4 | 739 | match. |
88b16508 JH |
740 | Do this as very last check so the not very informative error |
741 | is not output too often. */ | |
742 | if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl)) | |
743 | { | |
b1905808 | 744 | class_type->odr_violated = true; |
097f82ec | 745 | auto_diagnostic_group d; |
4ee494c0 JJ |
746 | tree ctx = TYPE_NAME (DECL_CONTEXT (vtable->decl)); |
747 | if (warning_at (DECL_SOURCE_LOCATION (ctx), OPT_Wodr, | |
88b16508 | 748 | "virtual table of type %qD violates " |
4ee494c0 | 749 | "one definition rule", |
88b16508 JH |
750 | DECL_CONTEXT (vtable->decl))) |
751 | { | |
4ee494c0 JJ |
752 | ctx = TYPE_NAME (DECL_CONTEXT (prevailing->decl)); |
753 | inform (DECL_SOURCE_LOCATION (ctx), | |
754 | "the conflicting type defined in another translation" | |
755 | " unit has virtual table of different size"); | |
88b16508 JH |
756 | } |
757 | } | |
758 | return; | |
759 | } | |
760 | ||
761 | if (!end1 && !end2) | |
762 | { | |
f8a1abf8 | 763 | if (methods_equal_p (ref1->referred->decl, ref2->referred->decl)) |
88b16508 JH |
764 | continue; |
765 | ||
b1905808 JH |
766 | class_type->odr_violated = true; |
767 | ||
88b16508 JH |
768 | /* If the loops above stopped on non-virtual pointer, we have |
769 | mismatch in RTTI information mangling. */ | |
b1905808 JH |
770 | if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL |
771 | && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL) | |
88b16508 | 772 | { |
097f82ec | 773 | auto_diagnostic_group d; |
88b16508 | 774 | if (warning_at (DECL_SOURCE_LOCATION |
b1905808 JH |
775 | (TYPE_NAME (DECL_CONTEXT (vtable->decl))), |
776 | OPT_Wodr, | |
88b16508 | 777 | "virtual table of type %qD violates " |
765f8786 | 778 | "one definition rule", |
88b16508 JH |
779 | DECL_CONTEXT (vtable->decl))) |
780 | { | |
609570b4 | 781 | inform (DECL_SOURCE_LOCATION |
88b16508 JH |
782 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), |
783 | "the conflicting type defined in another translation " | |
609570b4 | 784 | "unit with different RTTI information"); |
88b16508 JH |
785 | } |
786 | return; | |
787 | } | |
788 | /* At this point both REF1 and REF2 points either to virtual table | |
789 | or virtual method. If one points to virtual table and other to | |
790 | method we can complain the same way as if one table was shorter | |
791 | than other pointing out the extra method. */ | |
88b16508 JH |
792 | if (TREE_CODE (ref1->referred->decl) |
793 | != TREE_CODE (ref2->referred->decl)) | |
794 | { | |
8813a647 | 795 | if (VAR_P (ref1->referred->decl)) |
88b16508 | 796 | end1 = true; |
8813a647 | 797 | else if (VAR_P (ref2->referred->decl)) |
88b16508 JH |
798 | end2 = true; |
799 | } | |
800 | } | |
801 | ||
b1905808 JH |
802 | class_type->odr_violated = true; |
803 | ||
956d615d | 804 | /* Complain about size mismatch. Either we have too many virtual |
88b16508 | 805 | functions or too many virtual table pointers. */ |
56b1f114 JH |
806 | if (end1 || end2) |
807 | { | |
808 | if (end1) | |
809 | { | |
810 | varpool_node *tmp = prevailing; | |
811 | prevailing = vtable; | |
812 | vtable = tmp; | |
813 | ref1 = ref2; | |
814 | } | |
097f82ec | 815 | auto_diagnostic_group d; |
56b1f114 | 816 | if (warning_at (DECL_SOURCE_LOCATION |
b1905808 JH |
817 | (TYPE_NAME (DECL_CONTEXT (vtable->decl))), |
818 | OPT_Wodr, | |
56b1f114 JH |
819 | "virtual table of type %qD violates " |
820 | "one definition rule", | |
821 | DECL_CONTEXT (vtable->decl))) | |
822 | { | |
88b16508 JH |
823 | if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL) |
824 | { | |
825 | inform (DECL_SOURCE_LOCATION | |
826 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), | |
827 | "the conflicting type defined in another translation " | |
828 | "unit"); | |
829 | inform (DECL_SOURCE_LOCATION | |
830 | (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))), | |
831 | "contains additional virtual method %qD", | |
832 | ref1->referred->decl); | |
833 | } | |
834 | else | |
835 | { | |
836 | inform (DECL_SOURCE_LOCATION | |
837 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), | |
838 | "the conflicting type defined in another translation " | |
026c3cfd | 839 | "unit has virtual table with more entries"); |
88b16508 | 840 | } |
56b1f114 JH |
841 | } |
842 | return; | |
843 | } | |
88b16508 | 844 | |
4e8e460b | 845 | /* And in the last case we have either mismatch in between two virtual |
88b16508 | 846 | methods or two virtual table pointers. */ |
097f82ec | 847 | auto_diagnostic_group d; |
88b16508 | 848 | if (warning_at (DECL_SOURCE_LOCATION |
b1905808 | 849 | (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr, |
88b16508 | 850 | "virtual table of type %qD violates " |
a9c697b8 | 851 | "one definition rule", |
88b16508 | 852 | DECL_CONTEXT (vtable->decl))) |
56b1f114 | 853 | { |
88b16508 | 854 | if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL) |
56b1f114 | 855 | { |
609570b4 | 856 | inform (DECL_SOURCE_LOCATION |
56b1f114 JH |
857 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), |
858 | "the conflicting type defined in another translation " | |
859 | "unit"); | |
88b16508 JH |
860 | gcc_assert (TREE_CODE (ref2->referred->decl) |
861 | == FUNCTION_DECL); | |
f8a1abf8 JH |
862 | inform (DECL_SOURCE_LOCATION |
863 | (ref1->referred->ultimate_alias_target ()->decl), | |
864 | "virtual method %qD", | |
865 | ref1->referred->ultimate_alias_target ()->decl); | |
866 | inform (DECL_SOURCE_LOCATION | |
867 | (ref2->referred->ultimate_alias_target ()->decl), | |
56b1f114 | 868 | "ought to match virtual method %qD but does not", |
f8a1abf8 | 869 | ref2->referred->ultimate_alias_target ()->decl); |
56b1f114 | 870 | } |
88b16508 | 871 | else |
609570b4 | 872 | inform (DECL_SOURCE_LOCATION |
88b16508 JH |
873 | (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), |
874 | "the conflicting type defined in another translation " | |
026c3cfd | 875 | "unit has virtual table with different contents"); |
88b16508 | 876 | return; |
56b1f114 JH |
877 | } |
878 | } | |
879 | } | |
880 | ||
c59f7203 JH |
881 | /* Output ODR violation warning about T1 and T2 with REASON. |
882 | Display location of ST1 and ST2 if REASON speaks about field or | |
883 | method of the type. | |
884 | If WARN is false, do nothing. Set WARNED if warning was indeed | |
885 | output. */ | |
886 | ||
420672bc | 887 | static void |
c59f7203 JH |
888 | warn_odr (tree t1, tree t2, tree st1, tree st2, |
889 | bool warn, bool *warned, const char *reason) | |
890 | { | |
ba167748 | 891 | tree decl2 = TYPE_NAME (TYPE_MAIN_VARIANT (t2)); |
88b16508 JH |
892 | if (warned) |
893 | *warned = false; | |
c59f7203 | 894 | |
ba167748 | 895 | if (!warn || !TYPE_NAME(TYPE_MAIN_VARIANT (t1))) |
c59f7203 | 896 | return; |
b1905808 | 897 | |
956d615d | 898 | /* ODR warnings are output during LTO streaming; we must apply location |
eaeec5ec | 899 | cache for potential warnings to be output correctly. */ |
36ceb0e3 JH |
900 | if (lto_location_cache::current_cache) |
901 | lto_location_cache::current_cache->apply_location_cache (); | |
eaeec5ec | 902 | |
097f82ec | 903 | auto_diagnostic_group d; |
ba167748 | 904 | if (t1 != TYPE_MAIN_VARIANT (t1) |
bbbef996 | 905 | && TYPE_NAME (t1) != TYPE_NAME (TYPE_MAIN_VARIANT (t1))) |
ba167748 JH |
906 | { |
907 | if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))), | |
908 | OPT_Wodr, "type %qT (typedef of %qT) violates the " | |
909 | "C++ One Definition Rule", | |
910 | t1, TYPE_MAIN_VARIANT (t1))) | |
911 | return; | |
912 | } | |
913 | else | |
914 | { | |
915 | if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))), | |
916 | OPT_Wodr, "type %qT violates the C++ One Definition Rule", | |
917 | t1)) | |
918 | return; | |
919 | } | |
2c132d34 | 920 | if (!st1 && !st2) |
c59f7203 | 921 | ; |
2c132d34 JH |
922 | /* For FIELD_DECL support also case where one of fields is |
923 | NULL - this is used when the structures have mismatching number of | |
924 | elements. */ | |
925 | else if (!st1 || TREE_CODE (st1) == FIELD_DECL) | |
c59f7203 JH |
926 | { |
927 | inform (DECL_SOURCE_LOCATION (decl2), | |
928 | "a different type is defined in another translation unit"); | |
2c132d34 JH |
929 | if (!st1) |
930 | { | |
931 | st1 = st2; | |
932 | st2 = NULL; | |
933 | } | |
c59f7203 JH |
934 | inform (DECL_SOURCE_LOCATION (st1), |
935 | "the first difference of corresponding definitions is field %qD", | |
936 | st1); | |
2c132d34 JH |
937 | if (st2) |
938 | decl2 = st2; | |
c59f7203 JH |
939 | } |
940 | else if (TREE_CODE (st1) == FUNCTION_DECL) | |
941 | { | |
942 | inform (DECL_SOURCE_LOCATION (decl2), | |
943 | "a different type is defined in another translation unit"); | |
944 | inform (DECL_SOURCE_LOCATION (st1), | |
945 | "the first difference of corresponding definitions is method %qD", | |
946 | st1); | |
947 | decl2 = st2; | |
948 | } | |
949 | else | |
950 | return; | |
951 | inform (DECL_SOURCE_LOCATION (decl2), reason); | |
952 | ||
953 | if (warned) | |
954 | *warned = true; | |
955 | } | |
956 | ||
956d615d | 957 | /* Return true if T1 and T2 are incompatible and we want to recursively |
6542950e JH |
958 | dive into them from warn_type_mismatch to give sensible answer. */ |
959 | ||
960 | static bool | |
961 | type_mismatch_p (tree t1, tree t2) | |
962 | { | |
963 | if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2) | |
964 | && !odr_types_equivalent_p (t1, t2)) | |
965 | return true; | |
966 | return !types_compatible_p (t1, t2); | |
967 | } | |
968 | ||
969 | ||
970 | /* Types T1 and T2 was found to be incompatible in a context they can't | |
971 | (either used to declare a symbol of same assembler name or unified by | |
972 | ODR rule). We already output warning about this, but if possible, output | |
973 | extra information on how the types mismatch. | |
974 | ||
975 | This is hard to do in general. We basically handle the common cases. | |
976 | ||
977 | If LOC1 and LOC2 are meaningful locations, use it in the case the types | |
956d615d | 978 | themselves do not have one. */ |
c59f7203 JH |
979 | |
980 | void | |
6542950e | 981 | warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2) |
c59f7203 | 982 | { |
6542950e JH |
983 | /* Location of type is known only if it has TYPE_NAME and the name is |
984 | TYPE_DECL. */ | |
985 | location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL | |
986 | ? DECL_SOURCE_LOCATION (TYPE_NAME (t1)) | |
987 | : UNKNOWN_LOCATION; | |
988 | location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL | |
989 | ? DECL_SOURCE_LOCATION (TYPE_NAME (t2)) | |
990 | : UNKNOWN_LOCATION; | |
991 | bool loc_t2_useful = false; | |
992 | ||
993 | /* With LTO it is a common case that the location of both types match. | |
994 | See if T2 has a location that is different from T1. If so, we will | |
995 | inform user about the location. | |
996 | Do not consider the location passed to us in LOC1/LOC2 as those are | |
997 | already output. */ | |
998 | if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1) | |
999 | { | |
1000 | if (loc_t1 <= BUILTINS_LOCATION) | |
1001 | loc_t2_useful = true; | |
1002 | else | |
1003 | { | |
1004 | expanded_location xloc1 = expand_location (loc_t1); | |
1005 | expanded_location xloc2 = expand_location (loc_t2); | |
1006 | ||
1007 | if (strcmp (xloc1.file, xloc2.file) | |
1008 | || xloc1.line != xloc2.line | |
1009 | || xloc1.column != xloc2.column) | |
1010 | loc_t2_useful = true; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | if (loc_t1 <= BUILTINS_LOCATION) | |
1015 | loc_t1 = loc1; | |
1016 | if (loc_t2 <= BUILTINS_LOCATION) | |
1017 | loc_t2 = loc2; | |
1018 | ||
1019 | location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1; | |
1020 | ||
1021 | /* It is a quite common bug to reference anonymous namespace type in | |
1022 | non-anonymous namespace class. */ | |
2fb2966c ML |
1023 | tree mt1 = TYPE_MAIN_VARIANT (t1); |
1024 | tree mt2 = TYPE_MAIN_VARIANT (t2); | |
1025 | if ((type_with_linkage_p (mt1) | |
1026 | && type_in_anonymous_namespace_p (mt1)) | |
1027 | || (type_with_linkage_p (mt2) | |
1028 | && type_in_anonymous_namespace_p (mt2))) | |
6542950e | 1029 | { |
2fb2966c ML |
1030 | if (!type_with_linkage_p (mt1) |
1031 | || !type_in_anonymous_namespace_p (mt1)) | |
6542950e JH |
1032 | { |
1033 | std::swap (t1, t2); | |
2fb2966c | 1034 | std::swap (mt1, mt2); |
6542950e JH |
1035 | std::swap (loc_t1, loc_t2); |
1036 | } | |
2fb2966c ML |
1037 | gcc_assert (TYPE_NAME (mt1) |
1038 | && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL); | |
1039 | tree n1 = TYPE_NAME (mt1); | |
1040 | tree n2 = TYPE_NAME (mt2) ? TYPE_NAME (mt2) : NULL; | |
56f1a16c | 1041 | |
420672bc JH |
1042 | if (TREE_CODE (n1) == TYPE_DECL) |
1043 | n1 = DECL_NAME (n1); | |
56f1a16c | 1044 | if (n2 && TREE_CODE (n2) == TYPE_DECL) |
abb967da | 1045 | n2 = DECL_NAME (n2); |
956d615d | 1046 | /* Most of the time, the type names will match, do not be unnecessarily |
6542950e | 1047 | verbose. */ |
66fafc3b | 1048 | if (n1 != n2) |
6542950e | 1049 | inform (loc_t1, |
67914693 | 1050 | "type %qT defined in anonymous namespace cannot match " |
6542950e JH |
1051 | "type %qT across the translation unit boundary", |
1052 | t1, t2); | |
1053 | else | |
1054 | inform (loc_t1, | |
67914693 | 1055 | "type %qT defined in anonymous namespace cannot match " |
6542950e JH |
1056 | "across the translation unit boundary", |
1057 | t1); | |
1058 | if (loc_t2_useful) | |
1059 | inform (loc_t2, | |
1060 | "the incompatible type defined in another translation unit"); | |
1061 | return; | |
1062 | } | |
1063 | /* If types have mangled ODR names and they are different, it is most | |
1064 | informative to output those. | |
1065 | This also covers types defined in different namespaces. */ | |
74fee042 ML |
1066 | const char *odr1 = get_odr_name_for_type (mt1); |
1067 | const char *odr2 = get_odr_name_for_type (mt2); | |
1068 | if (odr1 != NULL && odr2 != NULL && odr1 != odr2) | |
1069 | { | |
1070 | const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES; | |
1071 | char *name1 = xstrdup (cplus_demangle (odr1, opts)); | |
c175aa77 | 1072 | char *name2 = cplus_demangle (odr2, opts); |
b1905808 JH |
1073 | if (name1 && name2 && strcmp (name1, name2)) |
1074 | { | |
6542950e | 1075 | inform (loc_t1, |
2f6f187a | 1076 | "type name %qs should match type name %qs", |
b1905808 | 1077 | name1, name2); |
6542950e JH |
1078 | if (loc_t2_useful) |
1079 | inform (loc_t2, | |
1080 | "the incompatible type is defined here"); | |
b1905808 JH |
1081 | free (name1); |
1082 | return; | |
1083 | } | |
1084 | free (name1); | |
1085 | } | |
6542950e | 1086 | /* A tricky case are compound types. Often they appear the same in source |
b1905808 JH |
1087 | code and the mismatch is dragged in by type they are build from. |
1088 | Look for those differences in subtypes and try to be informative. In other | |
1089 | cases just output nothing because the source code is probably different | |
1090 | and in this case we already output a all necessary info. */ | |
c59f7203 | 1091 | if (!TYPE_NAME (t1) || !TYPE_NAME (t2)) |
b1905808 JH |
1092 | { |
1093 | if (TREE_CODE (t1) == TREE_CODE (t2)) | |
1094 | { | |
b1905808 JH |
1095 | if (TREE_CODE (t1) == ARRAY_TYPE |
1096 | && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)) | |
1097 | { | |
1098 | tree i1 = TYPE_DOMAIN (t1); | |
1099 | tree i2 = TYPE_DOMAIN (t2); | |
1100 | ||
1101 | if (i1 && i2 | |
1102 | && TYPE_MAX_VALUE (i1) | |
1103 | && TYPE_MAX_VALUE (i2) | |
1104 | && !operand_equal_p (TYPE_MAX_VALUE (i1), | |
1105 | TYPE_MAX_VALUE (i2), 0)) | |
1106 | { | |
6542950e | 1107 | inform (loc, |
b1905808 JH |
1108 | "array types have different bounds"); |
1109 | return; | |
1110 | } | |
1111 | } | |
1112 | if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE) | |
6542950e JH |
1113 | && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
1114 | warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2); | |
b1905808 JH |
1115 | else if (TREE_CODE (t1) == METHOD_TYPE |
1116 | || TREE_CODE (t1) == FUNCTION_TYPE) | |
1117 | { | |
ad5bc324 | 1118 | tree parms1 = NULL, parms2 = NULL; |
b1905808 JH |
1119 | int count = 1; |
1120 | ||
6542950e | 1121 | if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
b1905808 | 1122 | { |
6542950e JH |
1123 | inform (loc, "return value type mismatch"); |
1124 | warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, | |
1125 | loc_t2); | |
b1905808 JH |
1126 | return; |
1127 | } | |
ad5bc324 JH |
1128 | if (prototype_p (t1) && prototype_p (t2)) |
1129 | for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2); | |
1130 | parms1 && parms2; | |
1131 | parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2), | |
1132 | count++) | |
1133 | { | |
6542950e | 1134 | if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2))) |
ad5bc324 JH |
1135 | { |
1136 | if (count == 1 && TREE_CODE (t1) == METHOD_TYPE) | |
6542950e | 1137 | inform (loc, |
ad5bc324 JH |
1138 | "implicit this pointer type mismatch"); |
1139 | else | |
6542950e | 1140 | inform (loc, |
ad5bc324 JH |
1141 | "type mismatch in parameter %i", |
1142 | count - (TREE_CODE (t1) == METHOD_TYPE)); | |
1143 | warn_types_mismatch (TREE_VALUE (parms1), | |
6542950e JH |
1144 | TREE_VALUE (parms2), |
1145 | loc_t1, loc_t2); | |
ad5bc324 JH |
1146 | return; |
1147 | } | |
1148 | } | |
b1905808 JH |
1149 | if (parms1 || parms2) |
1150 | { | |
6542950e | 1151 | inform (loc, |
b1905808 JH |
1152 | "types have different parameter counts"); |
1153 | return; | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | return; | |
1158 | } | |
6542950e | 1159 | |
420672bc | 1160 | if (types_odr_comparable (t1, t2) |
bbbef996 JH |
1161 | /* We make assign integers mangled names to be able to handle |
1162 | signed/unsigned chars. Accepting them here would however lead to | |
956d615d | 1163 | confusing message like |
bbbef996 JH |
1164 | "type ‘const int’ itself violates the C++ One Definition Rule" */ |
1165 | && TREE_CODE (t1) != INTEGER_TYPE | |
420672bc | 1166 | && types_same_for_odr (t1, t2)) |
6542950e | 1167 | inform (loc_t1, |
d8c9bc36 | 1168 | "type %qT itself violates the C++ One Definition Rule", t1); |
6542950e JH |
1169 | /* Prevent pointless warnings like "struct aa" should match "struct aa". */ |
1170 | else if (TYPE_NAME (t1) == TYPE_NAME (t2) | |
1171 | && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful) | |
c59f7203 | 1172 | return; |
c59f7203 | 1173 | else |
6542950e | 1174 | inform (loc_t1, "type %qT should match type %qT", |
c59f7203 | 1175 | t1, t2); |
6542950e JH |
1176 | if (loc_t2_useful) |
1177 | inform (loc_t2, "the incompatible type is defined here"); | |
c59f7203 JH |
1178 | } |
1179 | ||
956d615d | 1180 | /* Return true if T should be ignored in TYPE_FIELDS for ODR comparison. */ |
d2a0371d JH |
1181 | |
1182 | static bool | |
1183 | skip_in_fields_list_p (tree t) | |
1184 | { | |
1185 | if (TREE_CODE (t) != FIELD_DECL) | |
1186 | return true; | |
1187 | /* C++ FE introduces zero sized fields depending on -std setting, see | |
1188 | PR89358. */ | |
1189 | if (DECL_SIZE (t) | |
1190 | && integer_zerop (DECL_SIZE (t)) | |
1191 | && DECL_ARTIFICIAL (t) | |
1192 | && DECL_IGNORED_P (t) | |
1193 | && !DECL_NAME (t)) | |
1194 | return true; | |
1195 | return false; | |
1196 | } | |
1197 | ||
c59f7203 JH |
1198 | /* Compare T1 and T2, report ODR violations if WARN is true and set |
1199 | WARNED to true if anything is reported. Return true if types match. | |
1200 | If true is returned, the types are also compatible in the sense of | |
6542950e JH |
1201 | gimple_canonical_types_compatible_p. |
1202 | If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning | |
1203 | about the type if the type itself do not have location. */ | |
c59f7203 JH |
1204 | |
1205 | static bool | |
7d8adcba | 1206 | odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned, |
b32ca1df | 1207 | hash_set<type_pair> *visited, |
6542950e | 1208 | location_t loc1, location_t loc2) |
c59f7203 JH |
1209 | { |
1210 | /* Check first for the obvious case of pointer identity. */ | |
1211 | if (t1 == t2) | |
1212 | return true; | |
c59f7203 JH |
1213 | |
1214 | /* Can't be the same type if the types don't have the same code. */ | |
1215 | if (TREE_CODE (t1) != TREE_CODE (t2)) | |
1216 | { | |
1217 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1218 | G_("a different type is defined in another translation unit")); | |
1219 | return false; | |
1220 | } | |
1221 | ||
420672bc JH |
1222 | if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1)) |
1223 | && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1))) | |
1224 | || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2)) | |
1225 | && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2)))) | |
259d29e3 | 1226 | { |
67914693 | 1227 | /* We cannot trip this when comparing ODR types, only when trying to |
259d29e3 JH |
1228 | match different ODR derivations from different declarations. |
1229 | So WARN should be always false. */ | |
1230 | gcc_assert (!warn); | |
1231 | return false; | |
1232 | } | |
1233 | ||
c59f7203 JH |
1234 | /* Non-aggregate types can be handled cheaply. */ |
1235 | if (INTEGRAL_TYPE_P (t1) | |
1236 | || SCALAR_FLOAT_TYPE_P (t1) | |
1237 | || FIXED_POINT_TYPE_P (t1) | |
1238 | || TREE_CODE (t1) == VECTOR_TYPE | |
1239 | || TREE_CODE (t1) == COMPLEX_TYPE | |
1240 | || TREE_CODE (t1) == OFFSET_TYPE | |
1241 | || POINTER_TYPE_P (t1)) | |
1242 | { | |
1243 | if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)) | |
1244 | { | |
1245 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1246 | G_("a type with different precision is defined " | |
1247 | "in another translation unit")); | |
1248 | return false; | |
1249 | } | |
1250 | if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)) | |
1251 | { | |
1252 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1253 | G_("a type with different signedness is defined " | |
1254 | "in another translation unit")); | |
1255 | return false; | |
1256 | } | |
1257 | ||
1258 | if (TREE_CODE (t1) == INTEGER_TYPE | |
1259 | && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)) | |
1260 | { | |
1261 | /* char WRT uint_8? */ | |
1262 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1263 | G_("a different type is defined in another " | |
1264 | "translation unit")); | |
1265 | return false; | |
1266 | } | |
1267 | ||
1268 | /* For canonical type comparisons we do not want to build SCCs | |
1269 | so we cannot compare pointed-to types. But we can, for now, | |
1270 | require the same pointed-to type kind and match what | |
1271 | useless_type_conversion_p would do. */ | |
1272 | if (POINTER_TYPE_P (t1)) | |
1273 | { | |
1274 | if (TYPE_ADDR_SPACE (TREE_TYPE (t1)) | |
1275 | != TYPE_ADDR_SPACE (TREE_TYPE (t2))) | |
1276 | { | |
1277 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1278 | G_("it is defined as a pointer in different address " | |
1279 | "space in another translation unit")); | |
1280 | return false; | |
1281 | } | |
1282 | ||
6542950e | 1283 | if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), |
bbbef996 | 1284 | visited, loc1, loc2)) |
c59f7203 JH |
1285 | { |
1286 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1287 | G_("it is defined as a pointer to different type " | |
1288 | "in another translation unit")); | |
1289 | if (warn && warned) | |
6542950e JH |
1290 | warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), |
1291 | loc1, loc2); | |
c59f7203 JH |
1292 | return false; |
1293 | } | |
1294 | } | |
1295 | ||
c59f7203 | 1296 | if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE) |
6542950e JH |
1297 | && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), |
1298 | visited, loc1, loc2)) | |
c59f7203 JH |
1299 | { |
1300 | /* Probably specific enough. */ | |
1301 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1302 | G_("a different type is defined " | |
1303 | "in another translation unit")); | |
1304 | if (warn && warned) | |
6542950e | 1305 | warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2); |
c59f7203 JH |
1306 | return false; |
1307 | } | |
c59f7203 | 1308 | } |
c59f7203 | 1309 | /* Do type-specific comparisons. */ |
2c132d34 | 1310 | else switch (TREE_CODE (t1)) |
c59f7203 JH |
1311 | { |
1312 | case ARRAY_TYPE: | |
1313 | { | |
1314 | /* Array types are the same if the element types are the same and | |
1315 | the number of elements are the same. */ | |
6542950e | 1316 | if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), |
bbbef996 | 1317 | visited, loc1, loc2)) |
c59f7203 JH |
1318 | { |
1319 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1320 | G_("a different type is defined in another " | |
1321 | "translation unit")); | |
1322 | if (warn && warned) | |
6542950e | 1323 | warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2); |
c59f7203 JH |
1324 | } |
1325 | gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2)); | |
1326 | gcc_assert (TYPE_NONALIASED_COMPONENT (t1) | |
1327 | == TYPE_NONALIASED_COMPONENT (t2)); | |
1328 | ||
1329 | tree i1 = TYPE_DOMAIN (t1); | |
1330 | tree i2 = TYPE_DOMAIN (t2); | |
1331 | ||
1332 | /* For an incomplete external array, the type domain can be | |
1333 | NULL_TREE. Check this condition also. */ | |
1334 | if (i1 == NULL_TREE || i2 == NULL_TREE) | |
bbbef996 | 1335 | return type_variants_equivalent_p (t1, t2); |
c59f7203 JH |
1336 | |
1337 | tree min1 = TYPE_MIN_VALUE (i1); | |
1338 | tree min2 = TYPE_MIN_VALUE (i2); | |
1339 | tree max1 = TYPE_MAX_VALUE (i1); | |
1340 | tree max2 = TYPE_MAX_VALUE (i2); | |
1341 | ||
1342 | /* In C++, minimums should be always 0. */ | |
1343 | gcc_assert (min1 == min2); | |
1344 | if (!operand_equal_p (max1, max2, 0)) | |
1345 | { | |
1346 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1347 | G_("an array of different size is defined " | |
1348 | "in another translation unit")); | |
1349 | return false; | |
1350 | } | |
c59f7203 | 1351 | } |
2c132d34 | 1352 | break; |
c59f7203 JH |
1353 | |
1354 | case METHOD_TYPE: | |
1355 | case FUNCTION_TYPE: | |
1356 | /* Function types are the same if the return type and arguments types | |
1357 | are the same. */ | |
6542950e | 1358 | if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), |
bbbef996 | 1359 | visited, loc1, loc2)) |
c59f7203 JH |
1360 | { |
1361 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1362 | G_("has different return value " | |
1363 | "in another translation unit")); | |
1364 | if (warn && warned) | |
6542950e | 1365 | warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2); |
c59f7203 JH |
1366 | return false; |
1367 | } | |
1368 | ||
ad5bc324 JH |
1369 | if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2) |
1370 | || !prototype_p (t1) || !prototype_p (t2)) | |
bbbef996 | 1371 | return type_variants_equivalent_p (t1, t2); |
c59f7203 JH |
1372 | else |
1373 | { | |
1374 | tree parms1, parms2; | |
1375 | ||
1376 | for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2); | |
1377 | parms1 && parms2; | |
1378 | parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2)) | |
1379 | { | |
1380 | if (!odr_subtypes_equivalent_p | |
bbbef996 | 1381 | (TREE_VALUE (parms1), TREE_VALUE (parms2), |
420672bc | 1382 | visited, loc1, loc2)) |
c59f7203 JH |
1383 | { |
1384 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1385 | G_("has different parameters in another " | |
1386 | "translation unit")); | |
1387 | if (warn && warned) | |
1388 | warn_types_mismatch (TREE_VALUE (parms1), | |
6542950e | 1389 | TREE_VALUE (parms2), loc1, loc2); |
c59f7203 JH |
1390 | return false; |
1391 | } | |
1392 | } | |
1393 | ||
1394 | if (parms1 || parms2) | |
1395 | { | |
1396 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1397 | G_("has different parameters " | |
1398 | "in another translation unit")); | |
1399 | return false; | |
1400 | } | |
1401 | ||
bbbef996 | 1402 | return type_variants_equivalent_p (t1, t2); |
c59f7203 JH |
1403 | } |
1404 | ||
1405 | case RECORD_TYPE: | |
1406 | case UNION_TYPE: | |
1407 | case QUAL_UNION_TYPE: | |
1408 | { | |
1409 | tree f1, f2; | |
1410 | ||
1411 | /* For aggregate types, all the fields must be the same. */ | |
1412 | if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)) | |
1413 | { | |
b1905808 JH |
1414 | if (TYPE_BINFO (t1) && TYPE_BINFO (t2) |
1415 | && polymorphic_type_binfo_p (TYPE_BINFO (t1)) | |
1416 | != polymorphic_type_binfo_p (TYPE_BINFO (t2))) | |
1417 | { | |
1418 | if (polymorphic_type_binfo_p (TYPE_BINFO (t1))) | |
1419 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1420 | G_("a type defined in another translation unit " | |
1421 | "is not polymorphic")); | |
1422 | else | |
1423 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1424 | G_("a type defined in another translation unit " | |
1425 | "is polymorphic")); | |
1426 | return false; | |
1427 | } | |
c59f7203 JH |
1428 | for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2); |
1429 | f1 || f2; | |
1430 | f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2)) | |
1431 | { | |
1432 | /* Skip non-fields. */ | |
d2a0371d | 1433 | while (f1 && skip_in_fields_list_p (f1)) |
c59f7203 | 1434 | f1 = TREE_CHAIN (f1); |
d2a0371d | 1435 | while (f2 && skip_in_fields_list_p (f2)) |
c59f7203 JH |
1436 | f2 = TREE_CHAIN (f2); |
1437 | if (!f1 || !f2) | |
1438 | break; | |
88b16508 JH |
1439 | if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2)) |
1440 | { | |
1441 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1442 | G_("a type with different virtual table pointers" | |
1443 | " is defined in another translation unit")); | |
1444 | return false; | |
1445 | } | |
c59f7203 | 1446 | if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2)) |
88b16508 JH |
1447 | { |
1448 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1449 | G_("a type with different bases is defined " | |
1450 | "in another translation unit")); | |
1451 | return false; | |
1452 | } | |
c59f7203 JH |
1453 | if (DECL_NAME (f1) != DECL_NAME (f2) |
1454 | && !DECL_ARTIFICIAL (f1)) | |
1455 | { | |
1456 | warn_odr (t1, t2, f1, f2, warn, warned, | |
1457 | G_("a field with different name is defined " | |
1458 | "in another translation unit")); | |
1459 | return false; | |
1460 | } | |
88b16508 | 1461 | if (!odr_subtypes_equivalent_p (TREE_TYPE (f1), |
bbbef996 | 1462 | TREE_TYPE (f2), |
420672bc | 1463 | visited, loc1, loc2)) |
c59f7203 | 1464 | { |
88b16508 JH |
1465 | /* Do not warn about artificial fields and just go into |
1466 | generic field mismatch warning. */ | |
c59f7203 JH |
1467 | if (DECL_ARTIFICIAL (f1)) |
1468 | break; | |
1469 | ||
1470 | warn_odr (t1, t2, f1, f2, warn, warned, | |
1471 | G_("a field of same name but different type " | |
1472 | "is defined in another translation unit")); | |
1473 | if (warn && warned) | |
6542950e | 1474 | warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2); |
c59f7203 JH |
1475 | return false; |
1476 | } | |
1477 | if (!gimple_compare_field_offset (f1, f2)) | |
1478 | { | |
88b16508 JH |
1479 | /* Do not warn about artificial fields and just go into |
1480 | generic field mismatch warning. */ | |
c59f7203 JH |
1481 | if (DECL_ARTIFICIAL (f1)) |
1482 | break; | |
88b16508 | 1483 | warn_odr (t1, t2, f1, f2, warn, warned, |
d8c9bc36 | 1484 | G_("fields have different layout " |
c59f7203 JH |
1485 | "in another translation unit")); |
1486 | return false; | |
1487 | } | |
ec214f92 ML |
1488 | if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2)) |
1489 | { | |
1490 | warn_odr (t1, t2, f1, f2, warn, warned, | |
4ee494c0 JJ |
1491 | G_("one field is a bitfield while the other " |
1492 | "is not")); | |
ec214f92 ML |
1493 | return false; |
1494 | } | |
1495 | else | |
1496 | gcc_assert (DECL_NONADDRESSABLE_P (f1) | |
1497 | == DECL_NONADDRESSABLE_P (f2)); | |
c59f7203 JH |
1498 | } |
1499 | ||
1500 | /* If one aggregate has more fields than the other, they | |
1501 | are not the same. */ | |
1502 | if (f1 || f2) | |
1503 | { | |
88b16508 JH |
1504 | if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2))) |
1505 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1506 | G_("a type with different virtual table pointers" | |
1507 | " is defined in another translation unit")); | |
b1905808 JH |
1508 | else if ((f1 && DECL_ARTIFICIAL (f1)) |
1509 | || (f2 && DECL_ARTIFICIAL (f2))) | |
88b16508 JH |
1510 | warn_odr (t1, t2, NULL, NULL, warn, warned, |
1511 | G_("a type with different bases is defined " | |
1512 | "in another translation unit")); | |
2c132d34 JH |
1513 | else |
1514 | warn_odr (t1, t2, f1, f2, warn, warned, | |
88b16508 | 1515 | G_("a type with different number of fields " |
2c132d34 JH |
1516 | "is defined in another translation unit")); |
1517 | ||
c59f7203 JH |
1518 | return false; |
1519 | } | |
c59f7203 | 1520 | } |
2c132d34 | 1521 | break; |
c59f7203 | 1522 | } |
2c132d34 | 1523 | case VOID_TYPE: |
bb83a43d | 1524 | case NULLPTR_TYPE: |
2c132d34 | 1525 | break; |
c59f7203 JH |
1526 | |
1527 | default: | |
2c132d34 | 1528 | debug_tree (t1); |
c59f7203 JH |
1529 | gcc_unreachable (); |
1530 | } | |
2c132d34 JH |
1531 | |
1532 | /* Those are better to come last as they are utterly uninformative. */ | |
1533 | if (TYPE_SIZE (t1) && TYPE_SIZE (t2) | |
1534 | && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0)) | |
1535 | { | |
1536 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1537 | G_("a type with different size " | |
1538 | "is defined in another translation unit")); | |
1539 | return false; | |
1540 | } | |
420672bc | 1541 | |
288c5324 JH |
1542 | if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2) |
1543 | && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)) | |
1544 | { | |
1545 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
5e2dae50 | 1546 | G_("one type needs to be constructed while the other does not")); |
288c5324 JH |
1547 | gcc_checking_assert (RECORD_OR_UNION_TYPE_P (t1)); |
1548 | return false; | |
1549 | } | |
1550 | /* There is no really good user facing warning for this. | |
1551 | Either the original reason for modes being different is lost during | |
1552 | streaming or we should catch earlier warnings. We however must detect | |
1553 | the mismatch to avoid type verifier from cmplaining on mismatched | |
1554 | types between type and canonical type. See PR91576. */ | |
1555 | if (TYPE_MODE (t1) != TYPE_MODE (t2) | |
1556 | && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)) | |
1557 | { | |
1558 | warn_odr (t1, t2, NULL, NULL, warn, warned, | |
1559 | G_("memory layout mismatch")); | |
1560 | return false; | |
1561 | } | |
1562 | ||
2c132d34 JH |
1563 | gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2) |
1564 | || operand_equal_p (TYPE_SIZE_UNIT (t1), | |
1565 | TYPE_SIZE_UNIT (t2), 0)); | |
bbbef996 | 1566 | return type_variants_equivalent_p (t1, t2); |
c59f7203 JH |
1567 | } |
1568 | ||
259d29e3 JH |
1569 | /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */ |
1570 | ||
1571 | bool | |
1572 | odr_types_equivalent_p (tree type1, tree type2) | |
1573 | { | |
b2b29377 MM |
1574 | gcc_checking_assert (odr_or_derived_type_p (type1) |
1575 | && odr_or_derived_type_p (type2)); | |
259d29e3 | 1576 | |
b2b29377 | 1577 | hash_set<type_pair> visited; |
259d29e3 | 1578 | return odr_types_equivalent_p (type1, type2, false, NULL, |
6542950e | 1579 | &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION); |
259d29e3 JH |
1580 | } |
1581 | ||
61a74079 JH |
1582 | /* TYPE is equivalent to VAL by ODR, but its tree representation differs |
1583 | from VAL->type. This may happen in LTO where tree merging did not merge | |
609570b4 JH |
1584 | all variants of the same type or due to ODR violation. |
1585 | ||
1586 | Analyze and report ODR violations and add type to duplicate list. | |
1587 | If TYPE is more specified than VAL->type, prevail VAL->type. Also if | |
1588 | this is first time we see definition of a class return true so the | |
1589 | base types are analyzed. */ | |
61a74079 | 1590 | |
549bcbd1 | 1591 | static bool |
61a74079 JH |
1592 | add_type_duplicate (odr_type val, tree type) |
1593 | { | |
549bcbd1 | 1594 | bool build_bases = false; |
609570b4 | 1595 | bool prevail = false; |
b1905808 | 1596 | bool odr_must_violate = false; |
609570b4 | 1597 | |
61a74079 | 1598 | if (!val->types_set) |
6e2830c3 | 1599 | val->types_set = new hash_set<tree>; |
61a74079 | 1600 | |
730c436a JH |
1601 | /* Chose polymorphic type as leader (this happens only in case of ODR |
1602 | violations. */ | |
1603 | if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type) | |
1604 | && polymorphic_type_binfo_p (TYPE_BINFO (type))) | |
1605 | && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type) | |
1606 | || !polymorphic_type_binfo_p (TYPE_BINFO (val->type)))) | |
1607 | { | |
1608 | prevail = true; | |
1609 | build_bases = true; | |
1610 | } | |
549bcbd1 | 1611 | /* Always prefer complete type to be the leader. */ |
730c436a | 1612 | else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type)) |
609570b4 JH |
1613 | { |
1614 | prevail = true; | |
012b51cf JH |
1615 | if (TREE_CODE (type) == RECORD_TYPE) |
1616 | build_bases = TYPE_BINFO (type); | |
609570b4 | 1617 | } |
88b16508 JH |
1618 | else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type)) |
1619 | ; | |
88b16508 JH |
1620 | else if (TREE_CODE (val->type) == RECORD_TYPE |
1621 | && TREE_CODE (type) == RECORD_TYPE | |
1622 | && TYPE_BINFO (type) && !TYPE_BINFO (val->type)) | |
609570b4 JH |
1623 | { |
1624 | gcc_assert (!val->bases.length ()); | |
1625 | build_bases = true; | |
1626 | prevail = true; | |
1627 | } | |
88b16508 | 1628 | |
609570b4 | 1629 | if (prevail) |
6b4db501 | 1630 | std::swap (val->type, type); |
549bcbd1 | 1631 | |
609570b4 JH |
1632 | val->types_set->add (type); |
1633 | ||
a0276c00 | 1634 | if (!odr_hash) |
2fc233b7 | 1635 | return false; |
a0276c00 | 1636 | |
686a56a8 JH |
1637 | gcc_checking_assert (can_be_name_hashed_p (type) |
1638 | && can_be_name_hashed_p (val->type)); | |
609570b4 JH |
1639 | |
1640 | bool merge = true; | |
1641 | bool base_mismatch = false; | |
1642 | unsigned int i; | |
1643 | bool warned = false; | |
b32ca1df | 1644 | hash_set<type_pair> visited; |
609570b4 JH |
1645 | |
1646 | gcc_assert (in_lto_p); | |
1647 | vec_safe_push (val->types, type); | |
1648 | ||
b1905808 | 1649 | /* If both are class types, compare the bases. */ |
609570b4 JH |
1650 | if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type) |
1651 | && TREE_CODE (val->type) == RECORD_TYPE | |
1652 | && TREE_CODE (type) == RECORD_TYPE | |
1653 | && TYPE_BINFO (val->type) && TYPE_BINFO (type)) | |
1654 | { | |
1655 | if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) | |
1656 | != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type))) | |
1657 | { | |
b1905808 | 1658 | if (!flag_ltrans && !warned && !val->odr_violated) |
609570b4 JH |
1659 | { |
1660 | tree extra_base; | |
1661 | warn_odr (type, val->type, NULL, NULL, !warned, &warned, | |
1662 | "a type with the same name but different " | |
1663 | "number of polymorphic bases is " | |
1664 | "defined in another translation unit"); | |
b1905808 JH |
1665 | if (warned) |
1666 | { | |
1667 | if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) | |
1668 | > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type))) | |
1669 | extra_base = BINFO_BASE_BINFO | |
1670 | (TYPE_BINFO (type), | |
1671 | BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type))); | |
1672 | else | |
1673 | extra_base = BINFO_BASE_BINFO | |
1674 | (TYPE_BINFO (val->type), | |
1675 | BINFO_N_BASE_BINFOS (TYPE_BINFO (type))); | |
1676 | tree extra_base_type = BINFO_TYPE (extra_base); | |
1677 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)), | |
1678 | "the extra base is defined here"); | |
1679 | } | |
609570b4 JH |
1680 | } |
1681 | base_mismatch = true; | |
1682 | } | |
1683 | else | |
1684 | for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++) | |
1685 | { | |
1686 | tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i); | |
1687 | tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i); | |
1688 | tree type1 = BINFO_TYPE (base1); | |
1689 | tree type2 = BINFO_TYPE (base2); | |
549bcbd1 | 1690 | |
609570b4 JH |
1691 | if (types_odr_comparable (type1, type2)) |
1692 | { | |
1693 | if (!types_same_for_odr (type1, type2)) | |
1694 | base_mismatch = true; | |
1695 | } | |
1696 | else | |
259d29e3 JH |
1697 | if (!odr_types_equivalent_p (type1, type2)) |
1698 | base_mismatch = true; | |
609570b4 JH |
1699 | if (base_mismatch) |
1700 | { | |
1701 | if (!warned && !val->odr_violated) | |
1702 | { | |
1703 | warn_odr (type, val->type, NULL, NULL, | |
1704 | !warned, &warned, | |
1705 | "a type with the same name but different base " | |
1706 | "type is defined in another translation unit"); | |
1707 | if (warned) | |
6542950e JH |
1708 | warn_types_mismatch (type1, type2, |
1709 | UNKNOWN_LOCATION, UNKNOWN_LOCATION); | |
609570b4 JH |
1710 | } |
1711 | break; | |
1712 | } | |
1713 | if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2)) | |
1714 | { | |
1715 | base_mismatch = true; | |
1716 | if (!warned && !val->odr_violated) | |
1717 | warn_odr (type, val->type, NULL, NULL, | |
1718 | !warned, &warned, | |
1719 | "a type with the same name but different base " | |
1720 | "layout is defined in another translation unit"); | |
1721 | break; | |
1722 | } | |
1723 | /* One of bases is not of complete type. */ | |
1724 | if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2)) | |
1725 | { | |
1726 | /* If we have a polymorphic type info specified for TYPE1 | |
1727 | but not for TYPE2 we possibly missed a base when recording | |
1728 | VAL->type earlier. | |
1729 | Be sure this does not happen. */ | |
b1905808 JH |
1730 | if (TYPE_BINFO (type1) |
1731 | && polymorphic_type_binfo_p (TYPE_BINFO (type1)) | |
1732 | && !build_bases) | |
1733 | odr_must_violate = true; | |
609570b4 JH |
1734 | break; |
1735 | } | |
1736 | /* One base is polymorphic and the other not. | |
1737 | This ought to be diagnosed earlier, but do not ICE in the | |
1738 | checking bellow. */ | |
1739 | else if (TYPE_BINFO (type1) | |
1740 | && polymorphic_type_binfo_p (TYPE_BINFO (type1)) | |
1741 | != polymorphic_type_binfo_p (TYPE_BINFO (type2))) | |
1742 | { | |
b1905808 JH |
1743 | if (!warned && !val->odr_violated) |
1744 | warn_odr (type, val->type, NULL, NULL, | |
1745 | !warned, &warned, | |
1746 | "a base of the type is polymorphic only in one " | |
1747 | "translation unit"); | |
609570b4 JH |
1748 | base_mismatch = true; |
1749 | break; | |
1750 | } | |
1751 | } | |
609570b4 | 1752 | if (base_mismatch) |
61a74079 JH |
1753 | { |
1754 | merge = false; | |
ec77d61f | 1755 | odr_violation_reported = true; |
549bcbd1 | 1756 | val->odr_violated = true; |
609570b4 | 1757 | |
3dafb85c | 1758 | if (symtab->dump_file) |
61a74079 | 1759 | { |
609570b4 | 1760 | fprintf (symtab->dump_file, "ODR base violation\n"); |
61a74079 | 1761 | |
3dafb85c ML |
1762 | print_node (symtab->dump_file, "", val->type, 0); |
1763 | putc ('\n',symtab->dump_file); | |
1764 | print_node (symtab->dump_file, "", type, 0); | |
1765 | putc ('\n',symtab->dump_file); | |
61a74079 JH |
1766 | } |
1767 | } | |
609570b4 | 1768 | } |
61a74079 | 1769 | |
824721f0 DM |
1770 | /* Next compare memory layout. |
1771 | The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming. | |
1772 | We must apply the location cache to ensure that they are valid | |
1773 | before we can pass them to odr_types_equivalent_p (PR lto/83121). */ | |
1774 | if (lto_location_cache::current_cache) | |
1775 | lto_location_cache::current_cache->apply_location_cache (); | |
1afca3f4 JH |
1776 | /* As a special case we stream mangles names of integer types so we can see |
1777 | if they are believed to be same even though they have different | |
1778 | representation. Avoid bogus warning on mismatches in these. */ | |
1779 | if (TREE_CODE (type) != INTEGER_TYPE | |
1780 | && TREE_CODE (val->type) != INTEGER_TYPE | |
1781 | && !odr_types_equivalent_p (val->type, type, | |
b1905808 | 1782 | !flag_ltrans && !val->odr_violated && !warned, |
6542950e JH |
1783 | &warned, &visited, |
1784 | DECL_SOURCE_LOCATION (TYPE_NAME (val->type)), | |
1785 | DECL_SOURCE_LOCATION (TYPE_NAME (type)))) | |
b1905808 JH |
1786 | { |
1787 | merge = false; | |
1788 | odr_violation_reported = true; | |
1789 | val->odr_violated = true; | |
b1905808 JH |
1790 | } |
1791 | gcc_assert (val->odr_violated || !odr_must_violate); | |
1792 | /* Sanity check that all bases will be build same way again. */ | |
b2b29377 MM |
1793 | if (flag_checking |
1794 | && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type) | |
b1905808 JH |
1795 | && TREE_CODE (val->type) == RECORD_TYPE |
1796 | && TREE_CODE (type) == RECORD_TYPE | |
1797 | && TYPE_BINFO (val->type) && TYPE_BINFO (type) | |
1798 | && !val->odr_violated | |
1799 | && !base_mismatch && val->bases.length ()) | |
1800 | { | |
1801 | unsigned int num_poly_bases = 0; | |
1802 | unsigned int j; | |
1803 | ||
1804 | for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++) | |
1805 | if (polymorphic_type_binfo_p (BINFO_BASE_BINFO | |
1806 | (TYPE_BINFO (type), i))) | |
1807 | num_poly_bases++; | |
1808 | gcc_assert (num_poly_bases == val->bases.length ()); | |
1809 | for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); | |
1810 | i++) | |
1811 | if (polymorphic_type_binfo_p (BINFO_BASE_BINFO | |
1812 | (TYPE_BINFO (type), i))) | |
1813 | { | |
1814 | odr_type base = get_odr_type | |
1815 | (BINFO_TYPE | |
1816 | (BINFO_BASE_BINFO (TYPE_BINFO (type), | |
1817 | i)), | |
1818 | true); | |
1819 | gcc_assert (val->bases[j] == base); | |
1820 | j++; | |
1821 | } | |
1822 | } | |
b1905808 JH |
1823 | |
1824 | ||
609570b4 JH |
1825 | /* Regularize things a little. During LTO same types may come with |
1826 | different BINFOs. Either because their virtual table was | |
1827 | not merged by tree merging and only later at decl merging or | |
1828 | because one type comes with external vtable, while other | |
1829 | with internal. We want to merge equivalent binfos to conserve | |
1830 | memory and streaming overhead. | |
1831 | ||
1832 | The external vtables are more harmful: they contain references | |
1833 | to external declarations of methods that may be defined in the | |
1834 | merged LTO unit. For this reason we absolutely need to remove | |
1835 | them and replace by internal variants. Not doing so will lead | |
1836 | to incomplete answers from possible_polymorphic_call_targets. | |
1837 | ||
1838 | FIXME: disable for now; because ODR types are now build during | |
1839 | streaming in, the variants do not need to be linked to the type, | |
1840 | yet. We need to do the merging in cleanup pass to be implemented | |
1841 | soon. */ | |
1842 | if (!flag_ltrans && merge | |
1843 | && 0 | |
1844 | && TREE_CODE (val->type) == RECORD_TYPE | |
1845 | && TREE_CODE (type) == RECORD_TYPE | |
1846 | && TYPE_BINFO (val->type) && TYPE_BINFO (type) | |
1847 | && TYPE_MAIN_VARIANT (type) == type | |
1848 | && TYPE_MAIN_VARIANT (val->type) == val->type | |
1849 | && BINFO_VTABLE (TYPE_BINFO (val->type)) | |
1850 | && BINFO_VTABLE (TYPE_BINFO (type))) | |
1851 | { | |
1852 | tree master_binfo = TYPE_BINFO (val->type); | |
1853 | tree v1 = BINFO_VTABLE (master_binfo); | |
1854 | tree v2 = BINFO_VTABLE (TYPE_BINFO (type)); | |
7d8adcba | 1855 | |
609570b4 JH |
1856 | if (TREE_CODE (v1) == POINTER_PLUS_EXPR) |
1857 | { | |
1858 | gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR | |
1859 | && operand_equal_p (TREE_OPERAND (v1, 1), | |
1860 | TREE_OPERAND (v2, 1), 0)); | |
1861 | v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0); | |
1862 | v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0); | |
61a74079 | 1863 | } |
609570b4 JH |
1864 | gcc_assert (DECL_ASSEMBLER_NAME (v1) |
1865 | == DECL_ASSEMBLER_NAME (v2)); | |
61a74079 | 1866 | |
609570b4 | 1867 | if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2)) |
61a74079 | 1868 | { |
609570b4 | 1869 | unsigned int i; |
61a74079 | 1870 | |
609570b4 JH |
1871 | set_type_binfo (val->type, TYPE_BINFO (type)); |
1872 | for (i = 0; i < val->types->length (); i++) | |
61a74079 | 1873 | { |
609570b4 JH |
1874 | if (TYPE_BINFO ((*val->types)[i]) |
1875 | == master_binfo) | |
1876 | set_type_binfo ((*val->types)[i], TYPE_BINFO (type)); | |
61a74079 | 1877 | } |
609570b4 | 1878 | BINFO_TYPE (TYPE_BINFO (type)) = val->type; |
61a74079 | 1879 | } |
609570b4 JH |
1880 | else |
1881 | set_type_binfo (type, master_binfo); | |
61a74079 | 1882 | } |
549bcbd1 | 1883 | return build_bases; |
61a74079 JH |
1884 | } |
1885 | ||
5834e96a RS |
1886 | /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. |
1887 | FOR_DUMP_P is true when being called from the dump routines. */ | |
4611c03d JH |
1888 | |
1889 | tree | |
5834e96a | 1890 | obj_type_ref_class (const_tree ref, bool for_dump_p) |
4611c03d JH |
1891 | { |
1892 | gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF); | |
1893 | ref = TREE_TYPE (ref); | |
1894 | gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE); | |
1895 | ref = TREE_TYPE (ref); | |
1896 | /* We look for type THIS points to. ObjC also builds | |
1897 | OBJ_TYPE_REF with non-method calls, Their first parameter | |
1898 | ID however also corresponds to class type. */ | |
1899 | gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE | |
1900 | || TREE_CODE (ref) == FUNCTION_TYPE); | |
1901 | ref = TREE_VALUE (TYPE_ARG_TYPES (ref)); | |
1902 | gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE); | |
1903 | tree ret = TREE_TYPE (ref); | |
d1700aa1 | 1904 | if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (ret)) |
4611c03d | 1905 | ret = TYPE_CANONICAL (ret); |
5834e96a RS |
1906 | else if (odr_type ot = get_odr_type (ret, !for_dump_p)) |
1907 | ret = ot->type; | |
4611c03d | 1908 | else |
5834e96a | 1909 | gcc_assert (for_dump_p); |
4611c03d JH |
1910 | return ret; |
1911 | } | |
1912 | ||
eefe9a99 JH |
1913 | /* Get ODR type hash entry for TYPE. If INSERT is true, create |
1914 | possibly new entry. */ | |
1915 | ||
1916 | odr_type | |
1917 | get_odr_type (tree type, bool insert) | |
1918 | { | |
609570b4 | 1919 | odr_type_d **slot = NULL; |
609570b4 | 1920 | odr_type val = NULL; |
eefe9a99 | 1921 | hashval_t hash; |
549bcbd1 JH |
1922 | bool build_bases = false; |
1923 | bool insert_to_odr_array = false; | |
1924 | int base_id = -1; | |
1925 | ||
1afca3f4 | 1926 | type = TYPE_MAIN_VARIANT (type); |
d1700aa1 | 1927 | if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (type)) |
4611c03d | 1928 | type = TYPE_CANONICAL (type); |
eefe9a99 | 1929 | |
686a56a8 | 1930 | gcc_checking_assert (can_be_name_hashed_p (type)); |
609570b4 | 1931 | |
686a56a8 JH |
1932 | hash = hash_odr_name (type); |
1933 | slot = odr_hash->find_slot_with_hash (type, hash, | |
1934 | insert ? INSERT : NO_INSERT); | |
609570b4 | 1935 | |
686a56a8 | 1936 | if (!slot) |
eefe9a99 JH |
1937 | return NULL; |
1938 | ||
1939 | /* See if we already have entry for type. */ | |
686a56a8 | 1940 | if (*slot) |
eefe9a99 | 1941 | { |
686a56a8 | 1942 | val = *slot; |
eefe9a99 | 1943 | |
7656fa3e | 1944 | if (val->type != type && insert |
609570b4 | 1945 | && (!val->types_set || !val->types_set->add (type))) |
686a56a8 | 1946 | build_bases = add_type_duplicate (val, type); |
eefe9a99 JH |
1947 | } |
1948 | else | |
1949 | { | |
766090c2 | 1950 | val = ggc_cleared_alloc<odr_type_d> (); |
eefe9a99 JH |
1951 | val->type = type; |
1952 | val->bases = vNULL; | |
1953 | val->derived_types = vNULL; | |
e7a677ca JH |
1954 | if (type_with_linkage_p (type)) |
1955 | val->anonymous_namespace = type_in_anonymous_namespace_p (type); | |
1956 | else | |
1957 | val->anonymous_namespace = 0; | |
549bcbd1 JH |
1958 | build_bases = COMPLETE_TYPE_P (val->type); |
1959 | insert_to_odr_array = true; | |
686a56a8 | 1960 | *slot = val; |
549bcbd1 JH |
1961 | } |
1962 | ||
1963 | if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type) | |
4d6eb35a | 1964 | && type_with_linkage_p (type) |
549bcbd1 JH |
1965 | && type == TYPE_MAIN_VARIANT (type)) |
1966 | { | |
1967 | tree binfo = TYPE_BINFO (type); | |
1968 | unsigned int i; | |
1969 | ||
b1905808 | 1970 | gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type); |
3fb68f2e | 1971 | |
2d1644bf | 1972 | val->all_derivations_known = type_all_derivations_known_p (type); |
eefe9a99 JH |
1973 | for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++) |
1974 | /* For now record only polymorphic types. other are | |
67914693 | 1975 | pointless for devirtualization and we cannot precisely |
eefe9a99 JH |
1976 | determine ODR equivalency of these during LTO. */ |
1977 | if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i))) | |
1978 | { | |
b1905808 JH |
1979 | tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i)); |
1980 | odr_type base = get_odr_type (base_type, true); | |
1981 | gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type); | |
eefe9a99 JH |
1982 | base->derived_types.safe_push (val); |
1983 | val->bases.safe_push (base); | |
549bcbd1 JH |
1984 | if (base->id > base_id) |
1985 | base_id = base->id; | |
eefe9a99 | 1986 | } |
549bcbd1 JH |
1987 | } |
1988 | /* Ensure that type always appears after bases. */ | |
1989 | if (insert_to_odr_array) | |
1990 | { | |
eefe9a99 | 1991 | if (odr_types_ptr) |
c3284718 | 1992 | val->id = odr_types.length (); |
eefe9a99 JH |
1993 | vec_safe_push (odr_types_ptr, val); |
1994 | } | |
549bcbd1 JH |
1995 | else if (base_id > val->id) |
1996 | { | |
1997 | odr_types[val->id] = 0; | |
1998 | /* Be sure we did not recorded any derived types; these may need | |
1999 | renumbering too. */ | |
2000 | gcc_assert (val->derived_types.length() == 0); | |
61f46d0e | 2001 | val->id = odr_types.length (); |
549bcbd1 JH |
2002 | vec_safe_push (odr_types_ptr, val); |
2003 | } | |
eefe9a99 JH |
2004 | return val; |
2005 | } | |
2006 | ||
e4b44fd7 JH |
2007 | /* Return type that in ODR type hash prevailed TYPE. Be careful and punt |
2008 | on ODR violations. */ | |
2009 | ||
2010 | tree | |
2011 | prevailing_odr_type (tree type) | |
2012 | { | |
2013 | odr_type t = get_odr_type (type, false); | |
2014 | if (!t || t->odr_violated) | |
2015 | return type; | |
2016 | return t->type; | |
2017 | } | |
2018 | ||
a0276c00 JH |
2019 | /* Set tbaa_enabled flag for TYPE. */ |
2020 | ||
2021 | void | |
2022 | enable_odr_based_tbaa (tree type) | |
2023 | { | |
2024 | odr_type t = get_odr_type (type, true); | |
2025 | t->tbaa_enabled = true; | |
2026 | } | |
2027 | ||
2028 | /* True if canonical type of TYPE is determined using ODR name. */ | |
2029 | ||
2030 | bool | |
2031 | odr_based_tbaa_p (const_tree type) | |
2032 | { | |
2033 | if (!RECORD_OR_UNION_TYPE_P (type)) | |
2034 | return false; | |
18dd2956 JH |
2035 | if (!odr_hash) |
2036 | return false; | |
a0276c00 JH |
2037 | odr_type t = get_odr_type (const_cast <tree> (type), false); |
2038 | if (!t || !t->tbaa_enabled) | |
2039 | return false; | |
2040 | return true; | |
2041 | } | |
2042 | ||
2043 | /* Set TYPE_CANONICAL of type and all its variants and duplicates | |
2044 | to CANONICAL. */ | |
2045 | ||
2046 | void | |
2047 | set_type_canonical_for_odr_type (tree type, tree canonical) | |
2048 | { | |
2049 | odr_type t = get_odr_type (type, false); | |
2050 | unsigned int i; | |
2051 | tree tt; | |
2052 | ||
2053 | for (tree t2 = t->type; t2; t2 = TYPE_NEXT_VARIANT (t2)) | |
2054 | TYPE_CANONICAL (t2) = canonical; | |
2055 | if (t->types) | |
2056 | FOR_EACH_VEC_ELT (*t->types, i, tt) | |
2057 | for (tree t2 = tt; t2; t2 = TYPE_NEXT_VARIANT (t2)) | |
2058 | TYPE_CANONICAL (t2) = canonical; | |
2059 | } | |
2060 | ||
e4b44fd7 JH |
2061 | /* Return true if we reported some ODR violation on TYPE. */ |
2062 | ||
d840d7a2 JH |
2063 | bool |
2064 | odr_type_violation_reported_p (tree type) | |
2065 | { | |
2066 | return get_odr_type (type, false)->odr_violated; | |
2067 | } | |
2068 | ||
956d615d | 2069 | /* Add TYPE of ODR type hash. */ |
1ee85ee1 JH |
2070 | |
2071 | void | |
2072 | register_odr_type (tree type) | |
2073 | { | |
2074 | if (!odr_hash) | |
686a56a8 | 2075 | odr_hash = new odr_hash_type (23); |
1afca3f4 | 2076 | if (type == TYPE_MAIN_VARIANT (type)) |
7656fa3e | 2077 | { |
956d615d | 2078 | /* To get ODR warnings right, first register all sub-types. */ |
7656fa3e JH |
2079 | if (RECORD_OR_UNION_TYPE_P (type) |
2080 | && COMPLETE_TYPE_P (type)) | |
2081 | { | |
2082 | /* Limit recursion on types which are already registered. */ | |
2083 | odr_type ot = get_odr_type (type, false); | |
2084 | if (ot | |
2085 | && (ot->type == type | |
2086 | || (ot->types_set | |
2087 | && ot->types_set->contains (type)))) | |
2088 | return; | |
2089 | for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f)) | |
2090 | if (TREE_CODE (f) == FIELD_DECL) | |
2091 | { | |
2092 | tree subtype = TREE_TYPE (f); | |
2093 | ||
2094 | while (TREE_CODE (subtype) == ARRAY_TYPE) | |
2095 | subtype = TREE_TYPE (subtype); | |
2096 | if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype))) | |
2097 | register_odr_type (TYPE_MAIN_VARIANT (subtype)); | |
2098 | } | |
2099 | if (TYPE_BINFO (type)) | |
2100 | for (unsigned int i = 0; | |
2101 | i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++) | |
2102 | register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO | |
2103 | (TYPE_BINFO (type), i))); | |
2104 | } | |
2105 | get_odr_type (type, true); | |
2106 | } | |
1ee85ee1 JH |
2107 | } |
2108 | ||
aa803cc7 JH |
2109 | /* Return true if type is known to have no derivations. */ |
2110 | ||
2111 | bool | |
4d6eb35a | 2112 | type_known_to_have_no_derivations_p (tree t) |
aa803cc7 JH |
2113 | { |
2114 | return (type_all_derivations_known_p (t) | |
2115 | && (TYPE_FINAL_P (t) | |
2116 | || (odr_hash | |
2117 | && !get_odr_type (t, true)->derived_types.length()))); | |
2118 | } | |
2119 | ||
d6ae9a6d SL |
2120 | /* Dump ODR type T and all its derived types. INDENT specifies indentation for |
2121 | recursive printing. */ | |
eefe9a99 JH |
2122 | |
2123 | static void | |
2124 | dump_odr_type (FILE *f, odr_type t, int indent=0) | |
2125 | { | |
2126 | unsigned int i; | |
2127 | fprintf (f, "%*s type %i: ", indent * 2, "", t->id); | |
2128 | print_generic_expr (f, t->type, TDF_SLIM); | |
2d1644bf JH |
2129 | fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":""); |
2130 | fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":""); | |
eefe9a99 JH |
2131 | if (TYPE_NAME (t->type)) |
2132 | { | |
88b16508 JH |
2133 | if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type))) |
2134 | fprintf (f, "%*s mangled name: %s\n", indent * 2, "", | |
2135 | IDENTIFIER_POINTER | |
2136 | (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type)))); | |
eefe9a99 | 2137 | } |
c3284718 | 2138 | if (t->bases.length ()) |
eefe9a99 JH |
2139 | { |
2140 | fprintf (f, "%*s base odr type ids: ", indent * 2, ""); | |
c3284718 | 2141 | for (i = 0; i < t->bases.length (); i++) |
eefe9a99 JH |
2142 | fprintf (f, " %i", t->bases[i]->id); |
2143 | fprintf (f, "\n"); | |
2144 | } | |
c3284718 | 2145 | if (t->derived_types.length ()) |
eefe9a99 JH |
2146 | { |
2147 | fprintf (f, "%*s derived types:\n", indent * 2, ""); | |
c3284718 | 2148 | for (i = 0; i < t->derived_types.length (); i++) |
eefe9a99 JH |
2149 | dump_odr_type (f, t->derived_types[i], indent + 1); |
2150 | } | |
2151 | fprintf (f, "\n"); | |
2152 | } | |
2153 | ||
2154 | /* Dump the type inheritance graph. */ | |
2155 | ||
2156 | static void | |
2157 | dump_type_inheritance_graph (FILE *f) | |
2158 | { | |
2159 | unsigned int i; | |
7d3a67d7 | 2160 | unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0; |
0e1474e5 JH |
2161 | if (!odr_types_ptr) |
2162 | return; | |
eefe9a99 | 2163 | fprintf (f, "\n\nType inheritance graph:\n"); |
c3284718 | 2164 | for (i = 0; i < odr_types.length (); i++) |
eefe9a99 | 2165 | { |
549bcbd1 | 2166 | if (odr_types[i] && odr_types[i]->bases.length () == 0) |
eefe9a99 JH |
2167 | dump_odr_type (f, odr_types[i]); |
2168 | } | |
c3284718 | 2169 | for (i = 0; i < odr_types.length (); i++) |
61a74079 | 2170 | { |
7d3a67d7 JH |
2171 | if (!odr_types[i]) |
2172 | continue; | |
2173 | ||
2174 | num_all_types++; | |
2175 | if (!odr_types[i]->types || !odr_types[i]->types->length ()) | |
2176 | continue; | |
2177 | ||
2178 | /* To aid ODR warnings we also mangle integer constants but do | |
956d615d | 2179 | not consider duplicates there. */ |
7d3a67d7 JH |
2180 | if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE) |
2181 | continue; | |
2182 | ||
2183 | /* It is normal to have one duplicate and one normal variant. */ | |
2184 | if (odr_types[i]->types->length () == 1 | |
2185 | && COMPLETE_TYPE_P (odr_types[i]->type) | |
2186 | && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0])) | |
2187 | continue; | |
2188 | ||
2189 | num_types ++; | |
2190 | ||
2191 | unsigned int j; | |
2192 | fprintf (f, "Duplicate tree types for odr type %i\n", i); | |
2193 | print_node (f, "", odr_types[i]->type, 0); | |
2194 | print_node (f, "", TYPE_NAME (odr_types[i]->type), 0); | |
2195 | putc ('\n',f); | |
2196 | for (j = 0; j < odr_types[i]->types->length (); j++) | |
61a74079 | 2197 | { |
7d3a67d7 JH |
2198 | tree t; |
2199 | num_duplicates ++; | |
2200 | fprintf (f, "duplicate #%i\n", j); | |
2201 | print_node (f, "", (*odr_types[i]->types)[j], 0); | |
2202 | t = (*odr_types[i]->types)[j]; | |
2203 | while (TYPE_P (t) && TYPE_CONTEXT (t)) | |
61a74079 | 2204 | { |
7d3a67d7 JH |
2205 | t = TYPE_CONTEXT (t); |
2206 | print_node (f, "", t, 0); | |
61a74079 | 2207 | } |
7d3a67d7 JH |
2208 | print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0); |
2209 | putc ('\n',f); | |
61a74079 JH |
2210 | } |
2211 | } | |
7d3a67d7 JH |
2212 | fprintf (f, "Out of %i types there are %i types with duplicates; " |
2213 | "%i duplicates overall\n", num_all_types, num_types, num_duplicates); | |
2214 | } | |
2215 | ||
7424b7c1 JH |
2216 | /* Save some WPA->ltrans streaming by freeing stuff needed only for good |
2217 | ODR warnings. | |
3fb68f2e | 2218 | We make TYPE_DECLs to not point back |
7424b7c1 JH |
2219 | to the type (which is needed to keep them in the same SCC and preserve |
2220 | location information to output warnings) and subsequently we make all | |
2221 | TYPE_DECLS of same assembler name equivalent. */ | |
7d3a67d7 JH |
2222 | |
2223 | static void | |
7424b7c1 | 2224 | free_odr_warning_data () |
7d3a67d7 | 2225 | { |
7424b7c1 JH |
2226 | static bool odr_data_freed = false; |
2227 | ||
2228 | if (odr_data_freed || !flag_wpa || !odr_types_ptr) | |
7d3a67d7 | 2229 | return; |
7424b7c1 JH |
2230 | |
2231 | odr_data_freed = true; | |
2232 | ||
2233 | for (unsigned int i = 0; i < odr_types.length (); i++) | |
502e897d | 2234 | if (odr_types[i]) |
7d3a67d7 | 2235 | { |
7424b7c1 JH |
2236 | tree t = odr_types[i]->type; |
2237 | ||
7424b7c1 JH |
2238 | TREE_TYPE (TYPE_NAME (t)) = void_type_node; |
2239 | ||
7d3a67d7 JH |
2240 | if (odr_types[i]->types) |
2241 | for (unsigned int j = 0; j < odr_types[i]->types->length (); j++) | |
7424b7c1 JH |
2242 | { |
2243 | tree td = (*odr_types[i]->types)[j]; | |
2244 | ||
7424b7c1 JH |
2245 | TYPE_NAME (td) = TYPE_NAME (t); |
2246 | } | |
7d3a67d7 | 2247 | } |
7424b7c1 | 2248 | odr_data_freed = true; |
eefe9a99 JH |
2249 | } |
2250 | ||
eefe9a99 JH |
2251 | /* Initialize IPA devirt and build inheritance tree graph. */ |
2252 | ||
2253 | void | |
2254 | build_type_inheritance_graph (void) | |
2255 | { | |
b270b096 | 2256 | struct symtab_node *n; |
eefe9a99 | 2257 | FILE *inheritance_dump_file; |
1a817418 | 2258 | dump_flags_t flags; |
eefe9a99 | 2259 | |
c203e8a7 | 2260 | if (odr_hash) |
7d3a67d7 | 2261 | { |
7424b7c1 | 2262 | free_odr_warning_data (); |
7d3a67d7 JH |
2263 | return; |
2264 | } | |
eefe9a99 JH |
2265 | timevar_push (TV_IPA_INHERITANCE); |
2266 | inheritance_dump_file = dump_begin (TDI_inheritance, &flags); | |
c203e8a7 | 2267 | odr_hash = new odr_hash_type (23); |
eefe9a99 JH |
2268 | |
2269 | /* We reconstruct the graph starting of types of all methods seen in the | |
6af801f5 | 2270 | unit. */ |
b270b096 | 2271 | FOR_EACH_SYMBOL (n) |
7de90a6c | 2272 | if (is_a <cgraph_node *> (n) |
b270b096 | 2273 | && DECL_VIRTUAL_P (n->decl) |
d52f5295 | 2274 | && n->real_symbol_p ()) |
70e7f2a2 | 2275 | get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true); |
b270b096 JH |
2276 | |
2277 | /* Look also for virtual tables of types that do not define any methods. | |
3fb68f2e | 2278 | |
b270b096 JH |
2279 | We need it in a case where class B has virtual base of class A |
2280 | re-defining its virtual method and there is class C with no virtual | |
2281 | methods with B as virtual base. | |
2282 | ||
2283 | Here we output B's virtual method in two variant - for non-virtual | |
2284 | and virtual inheritance. B's virtual table has non-virtual version, | |
2285 | while C's has virtual. | |
2286 | ||
2287 | For this reason we need to know about C in order to include both | |
2288 | variants of B. More correctly, record_target_from_binfo should | |
2289 | add both variants of the method when walking B, but we have no | |
2290 | link in between them. | |
2291 | ||
2292 | We rely on fact that either the method is exported and thus we | |
2293 | assume it is called externally or C is in anonymous namespace and | |
2294 | thus we will see the vtable. */ | |
2295 | ||
7de90a6c | 2296 | else if (is_a <varpool_node *> (n) |
b270b096 JH |
2297 | && DECL_VIRTUAL_P (n->decl) |
2298 | && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE | |
2299 | && TYPE_BINFO (DECL_CONTEXT (n->decl)) | |
2300 | && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl)))) | |
549bcbd1 | 2301 | get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true); |
eefe9a99 JH |
2302 | if (inheritance_dump_file) |
2303 | { | |
2304 | dump_type_inheritance_graph (inheritance_dump_file); | |
2305 | dump_end (TDI_inheritance, inheritance_dump_file); | |
2306 | } | |
7424b7c1 | 2307 | free_odr_warning_data (); |
eefe9a99 JH |
2308 | timevar_pop (TV_IPA_INHERITANCE); |
2309 | } | |
2310 | ||
ccb05ef2 JH |
2311 | /* Return true if N has reference from live virtual table |
2312 | (and thus can be a destination of polymorphic call). | |
2313 | Be conservatively correct when callgraph is not built or | |
2314 | if the method may be referred externally. */ | |
2315 | ||
2316 | static bool | |
2317 | referenced_from_vtable_p (struct cgraph_node *node) | |
2318 | { | |
2319 | int i; | |
2320 | struct ipa_ref *ref; | |
2321 | bool found = false; | |
2322 | ||
2323 | if (node->externally_visible | |
7d0aa05b | 2324 | || DECL_EXTERNAL (node->decl) |
ccb05ef2 JH |
2325 | || node->used_from_other_partition) |
2326 | return true; | |
2327 | ||
2328 | /* Keep this test constant time. | |
2329 | It is unlikely this can happen except for the case where speculative | |
2330 | devirtualization introduced many speculative edges to this node. | |
2331 | In this case the target is very likely alive anyway. */ | |
2332 | if (node->ref_list.referring.length () > 100) | |
2333 | return true; | |
2334 | ||
2335 | /* We need references built. */ | |
3dafb85c | 2336 | if (symtab->state <= CONSTRUCTION) |
ccb05ef2 JH |
2337 | return true; |
2338 | ||
d122681a | 2339 | for (i = 0; node->iterate_referring (i, ref); i++) |
ccb05ef2 | 2340 | if ((ref->use == IPA_REF_ALIAS |
d52f5295 | 2341 | && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring))) |
ccb05ef2 | 2342 | || (ref->use == IPA_REF_ADDR |
8813a647 | 2343 | && VAR_P (ref->referring->decl) |
ccb05ef2 JH |
2344 | && DECL_VIRTUAL_P (ref->referring->decl))) |
2345 | { | |
2346 | found = true; | |
2347 | break; | |
2348 | } | |
2349 | return found; | |
2350 | } | |
2351 | ||
ceda2c69 JH |
2352 | /* Return if TARGET is cxa_pure_virtual. */ |
2353 | ||
2354 | static bool | |
2355 | is_cxa_pure_virtual_p (tree target) | |
2356 | { | |
2357 | return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE | |
2358 | && DECL_NAME (target) | |
a01f151f | 2359 | && id_equal (DECL_NAME (target), |
ceda2c69 JH |
2360 | "__cxa_pure_virtual"); |
2361 | } | |
2362 | ||
68377e53 | 2363 | /* If TARGET has associated node, record it in the NODES array. |
ec77d61f | 2364 | CAN_REFER specify if program can refer to the target directly. |
67914693 | 2365 | if TARGET is unknown (NULL) or it cannot be inserted (for example because |
ec77d61f JH |
2366 | its body was already removed and there is no way to refer to it), clear |
2367 | COMPLETEP. */ | |
eefe9a99 JH |
2368 | |
2369 | static void | |
2370 | maybe_record_node (vec <cgraph_node *> &nodes, | |
6e2830c3 | 2371 | tree target, hash_set<tree> *inserted, |
ec77d61f | 2372 | bool can_refer, |
68377e53 | 2373 | bool *completep) |
eefe9a99 | 2374 | { |
958c1d61 JH |
2375 | struct cgraph_node *target_node, *alias_target; |
2376 | enum availability avail; | |
ceda2c69 | 2377 | bool pure_virtual = is_cxa_pure_virtual_p (target); |
88f592e3 | 2378 | |
ceda2c69 | 2379 | /* __builtin_unreachable do not need to be added into |
88f592e3 JH |
2380 | list of targets; the runtime effect of calling them is undefined. |
2381 | Only "real" virtual methods should be accounted. */ | |
ceda2c69 | 2382 | if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual) |
88f592e3 | 2383 | return; |
eefe9a99 | 2384 | |
ec77d61f JH |
2385 | if (!can_refer) |
2386 | { | |
2387 | /* The only case when method of anonymous namespace becomes unreferable | |
2388 | is when we completely optimized it out. */ | |
2389 | if (flag_ltrans | |
2390 | || !target | |
88f592e3 | 2391 | || !type_in_anonymous_namespace_p (DECL_CONTEXT (target))) |
ec77d61f JH |
2392 | *completep = false; |
2393 | return; | |
2394 | } | |
2395 | ||
88f592e3 | 2396 | if (!target) |
68377e53 JH |
2397 | return; |
2398 | ||
d52f5295 | 2399 | target_node = cgraph_node::get (target); |
68377e53 | 2400 | |
d6ae9a6d | 2401 | /* Prefer alias target over aliases, so we do not get confused by |
958c1d61 JH |
2402 | fake duplicates. */ |
2403 | if (target_node) | |
2404 | { | |
d52f5295 | 2405 | alias_target = target_node->ultimate_alias_target (&avail); |
958c1d61 JH |
2406 | if (target_node != alias_target |
2407 | && avail >= AVAIL_AVAILABLE | |
d52f5295 | 2408 | && target_node->get_availability ()) |
958c1d61 JH |
2409 | target_node = alias_target; |
2410 | } | |
2411 | ||
ccb05ef2 | 2412 | /* Method can only be called by polymorphic call if any |
d6ae9a6d | 2413 | of vtables referring to it are alive. |
ccb05ef2 JH |
2414 | |
2415 | While this holds for non-anonymous functions, too, there are | |
2416 | cases where we want to keep them in the list; for example | |
2417 | inline functions with -fno-weak are static, but we still | |
2418 | may devirtualize them when instance comes from other unit. | |
2419 | The same holds for LTO. | |
2420 | ||
2421 | Currently we ignore these functions in speculative devirtualization. | |
2422 | ??? Maybe it would make sense to be more aggressive for LTO even | |
d6ae9a6d | 2423 | elsewhere. */ |
ccb05ef2 | 2424 | if (!flag_ltrans |
ceda2c69 | 2425 | && !pure_virtual |
ccb05ef2 JH |
2426 | && type_in_anonymous_namespace_p (DECL_CONTEXT (target)) |
2427 | && (!target_node | |
2428 | || !referenced_from_vtable_p (target_node))) | |
2429 | ; | |
2430 | /* See if TARGET is useful function we can deal with. */ | |
2431 | else if (target_node != NULL | |
2432 | && (TREE_PUBLIC (target) | |
2433 | || DECL_EXTERNAL (target) | |
2434 | || target_node->definition) | |
d52f5295 | 2435 | && target_node->real_symbol_p ()) |
0e1474e5 | 2436 | { |
a62bfab5 | 2437 | gcc_assert (!target_node->inlined_to); |
d52f5295 | 2438 | gcc_assert (target_node->real_symbol_p ()); |
84278ed9 | 2439 | /* When sanitizing, do not assume that __cxa_pure_virtual is not called |
d27ecc49 | 2440 | by valid program. */ |
84278ed9 | 2441 | if (flag_sanitize & SANITIZE_UNREACHABLE) |
d27ecc49 | 2442 | ; |
ceda2c69 JH |
2443 | /* Only add pure virtual if it is the only possible target. This way |
2444 | we will preserve the diagnostics about pure virtual called in many | |
2445 | cases without disabling optimization in other. */ | |
d27ecc49 | 2446 | else if (pure_virtual) |
ceda2c69 JH |
2447 | { |
2448 | if (nodes.length ()) | |
2449 | return; | |
2450 | } | |
2451 | /* If we found a real target, take away cxa_pure_virtual. */ | |
2452 | else if (!pure_virtual && nodes.length () == 1 | |
2453 | && is_cxa_pure_virtual_p (nodes[0]->decl)) | |
2454 | nodes.pop (); | |
2455 | if (pure_virtual && nodes.length ()) | |
2456 | return; | |
6e2830c3 | 2457 | if (!inserted->add (target)) |
68377e53 | 2458 | { |
6e2830c3 | 2459 | cached_polymorphic_call_targets->add (target_node); |
68377e53 JH |
2460 | nodes.safe_push (target_node); |
2461 | } | |
0e1474e5 | 2462 | } |
8479ed2c JH |
2463 | else if (!completep) |
2464 | ; | |
2465 | /* We have definition of __cxa_pure_virtual that is not accessible (it is | |
67914693 | 2466 | optimized out or partitioned to other unit) so we cannot add it. When |
8479ed2c JH |
2467 | not sanitizing, there is nothing to do. |
2468 | Otherwise declare the list incomplete. */ | |
2469 | else if (pure_virtual) | |
2470 | { | |
2471 | if (flag_sanitize & SANITIZE_UNREACHABLE) | |
2472 | *completep = false; | |
2473 | } | |
2474 | else if (flag_ltrans | |
2475 | || !type_in_anonymous_namespace_p (DECL_CONTEXT (target))) | |
0439a947 | 2476 | *completep = false; |
eefe9a99 JH |
2477 | } |
2478 | ||
d6ae9a6d | 2479 | /* See if BINFO's type matches OUTER_TYPE. If so, look up |
68377e53 | 2480 | BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find |
2d1644bf JH |
2481 | method in vtable and insert method to NODES array |
2482 | or BASES_TO_CONSIDER if this array is non-NULL. | |
eefe9a99 | 2483 | Otherwise recurse to base BINFOs. |
d6ae9a6d | 2484 | This matches what get_binfo_at_offset does, but with offset |
eefe9a99 JH |
2485 | being unknown. |
2486 | ||
a3788dde JH |
2487 | TYPE_BINFOS is a stack of BINFOS of types with defined |
2488 | virtual table seen on way from class type to BINFO. | |
eefe9a99 JH |
2489 | |
2490 | MATCHED_VTABLES tracks virtual tables we already did lookup | |
68377e53 JH |
2491 | for virtual function in. INSERTED tracks nodes we already |
2492 | inserted. | |
3462aa02 JH |
2493 | |
2494 | ANONYMOUS is true if BINFO is part of anonymous namespace. | |
ec77d61f JH |
2495 | |
2496 | Clear COMPLETEP when we hit unreferable target. | |
eefe9a99 JH |
2497 | */ |
2498 | ||
2499 | static void | |
68377e53 | 2500 | record_target_from_binfo (vec <cgraph_node *> &nodes, |
2d1644bf | 2501 | vec <tree> *bases_to_consider, |
68377e53 JH |
2502 | tree binfo, |
2503 | tree otr_type, | |
a3788dde | 2504 | vec <tree> &type_binfos, |
68377e53 JH |
2505 | HOST_WIDE_INT otr_token, |
2506 | tree outer_type, | |
2507 | HOST_WIDE_INT offset, | |
6e2830c3 TS |
2508 | hash_set<tree> *inserted, |
2509 | hash_set<tree> *matched_vtables, | |
ec77d61f JH |
2510 | bool anonymous, |
2511 | bool *completep) | |
eefe9a99 JH |
2512 | { |
2513 | tree type = BINFO_TYPE (binfo); | |
2514 | int i; | |
2515 | tree base_binfo; | |
2516 | ||
eefe9a99 | 2517 | |
a3788dde JH |
2518 | if (BINFO_VTABLE (binfo)) |
2519 | type_binfos.safe_push (binfo); | |
68377e53 | 2520 | if (types_same_for_odr (type, outer_type)) |
eefe9a99 | 2521 | { |
a3788dde JH |
2522 | int i; |
2523 | tree type_binfo = NULL; | |
2524 | ||
d6ae9a6d | 2525 | /* Look up BINFO with virtual table. For normal types it is always last |
a3788dde JH |
2526 | binfo on stack. */ |
2527 | for (i = type_binfos.length () - 1; i >= 0; i--) | |
2528 | if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo)) | |
2529 | { | |
2530 | type_binfo = type_binfos[i]; | |
2531 | break; | |
2532 | } | |
2533 | if (BINFO_VTABLE (binfo)) | |
2534 | type_binfos.pop (); | |
2535 | /* If this is duplicated BINFO for base shared by virtual inheritance, | |
2536 | we may not have its associated vtable. This is not a problem, since | |
2537 | we will walk it on the other path. */ | |
2538 | if (!type_binfo) | |
6d6af792 | 2539 | return; |
68377e53 JH |
2540 | tree inner_binfo = get_binfo_at_offset (type_binfo, |
2541 | offset, otr_type); | |
ec77d61f JH |
2542 | if (!inner_binfo) |
2543 | { | |
2544 | gcc_assert (odr_violation_reported); | |
2545 | return; | |
2546 | } | |
3462aa02 JH |
2547 | /* For types in anonymous namespace first check if the respective vtable |
2548 | is alive. If not, we know the type can't be called. */ | |
2549 | if (!flag_ltrans && anonymous) | |
2550 | { | |
68377e53 | 2551 | tree vtable = BINFO_VTABLE (inner_binfo); |
2c8326a5 | 2552 | varpool_node *vnode; |
3462aa02 JH |
2553 | |
2554 | if (TREE_CODE (vtable) == POINTER_PLUS_EXPR) | |
2555 | vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0); | |
9041d2e6 | 2556 | vnode = varpool_node::get (vtable); |
67348ccc | 2557 | if (!vnode || !vnode->definition) |
3462aa02 JH |
2558 | return; |
2559 | } | |
68377e53 | 2560 | gcc_assert (inner_binfo); |
2d1644bf | 2561 | if (bases_to_consider |
6e2830c3 TS |
2562 | ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo)) |
2563 | : !matched_vtables->add (BINFO_VTABLE (inner_binfo))) | |
68377e53 | 2564 | { |
ec77d61f JH |
2565 | bool can_refer; |
2566 | tree target = gimple_get_virt_method_for_binfo (otr_token, | |
2567 | inner_binfo, | |
2568 | &can_refer); | |
2d1644bf JH |
2569 | if (!bases_to_consider) |
2570 | maybe_record_node (nodes, target, inserted, can_refer, completep); | |
2571 | /* Destructors are never called via construction vtables. */ | |
2572 | else if (!target || !DECL_CXX_DESTRUCTOR_P (target)) | |
2573 | bases_to_consider->safe_push (target); | |
68377e53 | 2574 | } |
eefe9a99 JH |
2575 | return; |
2576 | } | |
2577 | ||
2578 | /* Walk bases. */ | |
2579 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) | |
d6ae9a6d | 2580 | /* Walking bases that have no virtual method is pointless exercise. */ |
eefe9a99 | 2581 | if (polymorphic_type_binfo_p (base_binfo)) |
2d1644bf | 2582 | record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type, |
a3788dde | 2583 | type_binfos, |
68377e53 | 2584 | otr_token, outer_type, offset, inserted, |
ec77d61f | 2585 | matched_vtables, anonymous, completep); |
a3788dde JH |
2586 | if (BINFO_VTABLE (binfo)) |
2587 | type_binfos.pop (); | |
eefe9a99 JH |
2588 | } |
2589 | ||
d6ae9a6d | 2590 | /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN) |
eefe9a99 JH |
2591 | of TYPE, insert them to NODES, recurse into derived nodes. |
2592 | INSERTED is used to avoid duplicate insertions of methods into NODES. | |
ec77d61f | 2593 | MATCHED_VTABLES are used to avoid duplicate walking vtables. |
2d1644bf | 2594 | Clear COMPLETEP if unreferable target is found. |
3fb68f2e | 2595 | |
d6ae9a6d | 2596 | If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER |
2d1644bf JH |
2597 | all cases where BASE_SKIPPED is true (because the base is abstract |
2598 | class). */ | |
eefe9a99 JH |
2599 | |
2600 | static void | |
2601 | possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes, | |
6e2830c3 TS |
2602 | hash_set<tree> *inserted, |
2603 | hash_set<tree> *matched_vtables, | |
eefe9a99 JH |
2604 | tree otr_type, |
2605 | odr_type type, | |
68377e53 JH |
2606 | HOST_WIDE_INT otr_token, |
2607 | tree outer_type, | |
ec77d61f | 2608 | HOST_WIDE_INT offset, |
2d1644bf JH |
2609 | bool *completep, |
2610 | vec <tree> &bases_to_consider, | |
2611 | bool consider_construction) | |
eefe9a99 JH |
2612 | { |
2613 | tree binfo = TYPE_BINFO (type->type); | |
2614 | unsigned int i; | |
1be0e58d | 2615 | auto_vec <tree, 8> type_binfos; |
2d1644bf JH |
2616 | bool possibly_instantiated = type_possibly_instantiated_p (type->type); |
2617 | ||
2618 | /* We may need to consider types w/o instances because of possible derived | |
2619 | types using their methods either directly or via construction vtables. | |
2620 | We are safe to skip them when all derivations are known, since we will | |
2621 | handle them later. | |
2622 | This is done by recording them to BASES_TO_CONSIDER array. */ | |
2623 | if (possibly_instantiated || consider_construction) | |
2624 | { | |
2625 | record_target_from_binfo (nodes, | |
2626 | (!possibly_instantiated | |
2627 | && type_all_derivations_known_p (type->type)) | |
2628 | ? &bases_to_consider : NULL, | |
2629 | binfo, otr_type, type_binfos, otr_token, | |
2630 | outer_type, offset, | |
2631 | inserted, matched_vtables, | |
2632 | type->anonymous_namespace, completep); | |
2633 | } | |
c3284718 | 2634 | for (i = 0; i < type->derived_types.length (); i++) |
eefe9a99 JH |
2635 | possible_polymorphic_call_targets_1 (nodes, inserted, |
2636 | matched_vtables, | |
2637 | otr_type, | |
2638 | type->derived_types[i], | |
2d1644bf JH |
2639 | otr_token, outer_type, offset, completep, |
2640 | bases_to_consider, consider_construction); | |
eefe9a99 JH |
2641 | } |
2642 | ||
2643 | /* Cache of queries for polymorphic call targets. | |
2644 | ||
2645 | Enumerating all call targets may get expensive when there are many | |
2646 | polymorphic calls in the program, so we memoize all the previous | |
2647 | queries and avoid duplicated work. */ | |
2648 | ||
6c1dae73 | 2649 | class polymorphic_call_target_d |
eefe9a99 | 2650 | { |
6c1dae73 | 2651 | public: |
eefe9a99 | 2652 | HOST_WIDE_INT otr_token; |
68377e53 JH |
2653 | ipa_polymorphic_call_context context; |
2654 | odr_type type; | |
eefe9a99 | 2655 | vec <cgraph_node *> targets; |
91bc34a9 | 2656 | tree decl_warning; |
2f28755f | 2657 | int type_warning; |
f7293b9d | 2658 | unsigned int n_odr_types; |
2f28755f JH |
2659 | bool complete; |
2660 | bool speculative; | |
eefe9a99 JH |
2661 | }; |
2662 | ||
2663 | /* Polymorphic call target cache helpers. */ | |
2664 | ||
7edd9b15 RS |
2665 | struct polymorphic_call_target_hasher |
2666 | : pointer_hash <polymorphic_call_target_d> | |
eefe9a99 | 2667 | { |
67f58944 TS |
2668 | static inline hashval_t hash (const polymorphic_call_target_d *); |
2669 | static inline bool equal (const polymorphic_call_target_d *, | |
2670 | const polymorphic_call_target_d *); | |
2671 | static inline void remove (polymorphic_call_target_d *); | |
eefe9a99 JH |
2672 | }; |
2673 | ||
2674 | /* Return the computed hashcode for ODR_QUERY. */ | |
2675 | ||
2676 | inline hashval_t | |
67f58944 | 2677 | polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query) |
eefe9a99 | 2678 | { |
d313d45f AK |
2679 | inchash::hash hstate (odr_query->otr_token); |
2680 | ||
449e9a33 | 2681 | hstate.add_hwi (odr_query->type->id); |
d313d45f | 2682 | hstate.merge_hash (TYPE_UID (odr_query->context.outer_type)); |
449e9a33 | 2683 | hstate.add_hwi (odr_query->context.offset); |
f7293b9d | 2684 | hstate.add_hwi (odr_query->n_odr_types); |
68377e53 | 2685 | |
3339f0bc JH |
2686 | if (odr_query->context.speculative_outer_type) |
2687 | { | |
d313d45f | 2688 | hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type)); |
449e9a33 | 2689 | hstate.add_hwi (odr_query->context.speculative_offset); |
3339f0bc | 2690 | } |
2f28755f | 2691 | hstate.add_flag (odr_query->speculative); |
d313d45f AK |
2692 | hstate.add_flag (odr_query->context.maybe_in_construction); |
2693 | hstate.add_flag (odr_query->context.maybe_derived_type); | |
2694 | hstate.add_flag (odr_query->context.speculative_maybe_derived_type); | |
2695 | hstate.commit_flag (); | |
2696 | return hstate.end (); | |
eefe9a99 JH |
2697 | } |
2698 | ||
2699 | /* Compare cache entries T1 and T2. */ | |
2700 | ||
2701 | inline bool | |
67f58944 TS |
2702 | polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1, |
2703 | const polymorphic_call_target_d *t2) | |
eefe9a99 | 2704 | { |
68377e53 | 2705 | return (t1->type == t2->type && t1->otr_token == t2->otr_token |
2f28755f | 2706 | && t1->speculative == t2->speculative |
68377e53 | 2707 | && t1->context.offset == t2->context.offset |
3339f0bc | 2708 | && t1->context.speculative_offset == t2->context.speculative_offset |
68377e53 | 2709 | && t1->context.outer_type == t2->context.outer_type |
3339f0bc | 2710 | && t1->context.speculative_outer_type == t2->context.speculative_outer_type |
68377e53 JH |
2711 | && t1->context.maybe_in_construction |
2712 | == t2->context.maybe_in_construction | |
3339f0bc JH |
2713 | && t1->context.maybe_derived_type == t2->context.maybe_derived_type |
2714 | && (t1->context.speculative_maybe_derived_type | |
f7293b9d JH |
2715 | == t2->context.speculative_maybe_derived_type) |
2716 | /* Adding new type may affect outcome of target search. */ | |
2717 | && t1->n_odr_types == t2->n_odr_types); | |
eefe9a99 JH |
2718 | } |
2719 | ||
2720 | /* Remove entry in polymorphic call target cache hash. */ | |
2721 | ||
2722 | inline void | |
67f58944 | 2723 | polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v) |
eefe9a99 JH |
2724 | { |
2725 | v->targets.release (); | |
2726 | free (v); | |
2727 | } | |
2728 | ||
2729 | /* Polymorphic call target query cache. */ | |
2730 | ||
c203e8a7 | 2731 | typedef hash_table<polymorphic_call_target_hasher> |
eefe9a99 | 2732 | polymorphic_call_target_hash_type; |
c203e8a7 | 2733 | static polymorphic_call_target_hash_type *polymorphic_call_target_hash; |
eefe9a99 JH |
2734 | |
2735 | /* Destroy polymorphic call target query cache. */ | |
2736 | ||
2737 | static void | |
2738 | free_polymorphic_call_targets_hash () | |
2739 | { | |
0e1474e5 JH |
2740 | if (cached_polymorphic_call_targets) |
2741 | { | |
c203e8a7 TS |
2742 | delete polymorphic_call_target_hash; |
2743 | polymorphic_call_target_hash = NULL; | |
6e2830c3 | 2744 | delete cached_polymorphic_call_targets; |
0e1474e5 JH |
2745 | cached_polymorphic_call_targets = NULL; |
2746 | } | |
eefe9a99 JH |
2747 | } |
2748 | ||
c1b8f25d JH |
2749 | /* Force rebuilding type inheritance graph from scratch. |
2750 | This is use to make sure that we do not keep references to types | |
2751 | which was not visible to free_lang_data. */ | |
2752 | ||
2753 | void | |
2754 | rebuild_type_inheritance_graph () | |
2755 | { | |
2756 | if (!odr_hash) | |
2757 | return; | |
2758 | delete odr_hash; | |
c1b8f25d | 2759 | odr_hash = NULL; |
c1b8f25d JH |
2760 | odr_types_ptr = NULL; |
2761 | free_polymorphic_call_targets_hash (); | |
2762 | } | |
2763 | ||
eefe9a99 JH |
2764 | /* When virtual function is removed, we may need to flush the cache. */ |
2765 | ||
2766 | static void | |
2767 | devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED) | |
2768 | { | |
0e1474e5 | 2769 | if (cached_polymorphic_call_targets |
2bc2379b | 2770 | && !thunk_expansion |
6e2830c3 | 2771 | && cached_polymorphic_call_targets->contains (n)) |
eefe9a99 JH |
2772 | free_polymorphic_call_targets_hash (); |
2773 | } | |
2774 | ||
d6ae9a6d | 2775 | /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */ |
390675c8 | 2776 | |
aa803cc7 | 2777 | tree |
85942f45 JH |
2778 | subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset, |
2779 | tree vtable) | |
390675c8 JH |
2780 | { |
2781 | tree v = BINFO_VTABLE (binfo); | |
2782 | int i; | |
2783 | tree base_binfo; | |
85942f45 | 2784 | unsigned HOST_WIDE_INT this_offset; |
390675c8 | 2785 | |
85942f45 JH |
2786 | if (v) |
2787 | { | |
2788 | if (!vtable_pointer_value_to_vtable (v, &v, &this_offset)) | |
2789 | gcc_unreachable (); | |
2790 | ||
2791 | if (offset == this_offset | |
2792 | && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable)) | |
2793 | return binfo; | |
2794 | } | |
3fb68f2e | 2795 | |
390675c8 JH |
2796 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
2797 | if (polymorphic_type_binfo_p (base_binfo)) | |
2798 | { | |
2799 | base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable); | |
2800 | if (base_binfo) | |
2801 | return base_binfo; | |
2802 | } | |
2803 | return NULL; | |
2804 | } | |
2805 | ||
85942f45 JH |
2806 | /* T is known constant value of virtual table pointer. |
2807 | Store virtual table to V and its offset to OFFSET. | |
2808 | Return false if T does not look like virtual table reference. */ | |
390675c8 | 2809 | |
85942f45 | 2810 | bool |
d570d364 JH |
2811 | vtable_pointer_value_to_vtable (const_tree t, tree *v, |
2812 | unsigned HOST_WIDE_INT *offset) | |
390675c8 JH |
2813 | { |
2814 | /* We expect &MEM[(void *)&virtual_table + 16B]. | |
2815 | We obtain object's BINFO from the context of the virtual table. | |
2816 | This one contains pointer to virtual table represented via | |
d6ae9a6d | 2817 | POINTER_PLUS_EXPR. Verify that this pointer matches what |
390675c8 JH |
2818 | we propagated through. |
2819 | ||
2820 | In the case of virtual inheritance, the virtual tables may | |
2821 | be nested, i.e. the offset may be different from 16 and we may | |
2822 | need to dive into the type representation. */ | |
85942f45 | 2823 | if (TREE_CODE (t) == ADDR_EXPR |
390675c8 JH |
2824 | && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF |
2825 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR | |
2826 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST | |
2827 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0)) | |
2828 | == VAR_DECL) | |
2829 | && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND | |
2830 | (TREE_OPERAND (t, 0), 0), 0))) | |
2831 | { | |
85942f45 JH |
2832 | *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0); |
2833 | *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1)); | |
2834 | return true; | |
390675c8 | 2835 | } |
85942f45 JH |
2836 | |
2837 | /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR. | |
2838 | We need to handle it when T comes from static variable initializer or | |
2839 | BINFO. */ | |
2840 | if (TREE_CODE (t) == POINTER_PLUS_EXPR) | |
2841 | { | |
2842 | *offset = tree_to_uhwi (TREE_OPERAND (t, 1)); | |
2843 | t = TREE_OPERAND (t, 0); | |
2844 | } | |
2845 | else | |
2846 | *offset = 0; | |
2847 | ||
2848 | if (TREE_CODE (t) != ADDR_EXPR) | |
2849 | return false; | |
2850 | *v = TREE_OPERAND (t, 0); | |
2851 | return true; | |
2852 | } | |
2853 | ||
2854 | /* T is known constant value of virtual table pointer. Return BINFO of the | |
2855 | instance type. */ | |
2856 | ||
2857 | tree | |
d570d364 | 2858 | vtable_pointer_value_to_binfo (const_tree t) |
85942f45 JH |
2859 | { |
2860 | tree vtable; | |
2861 | unsigned HOST_WIDE_INT offset; | |
2862 | ||
2863 | if (!vtable_pointer_value_to_vtable (t, &vtable, &offset)) | |
2864 | return NULL_TREE; | |
2865 | ||
2866 | /* FIXME: for stores of construction vtables we return NULL, | |
2867 | because we do not have BINFO for those. Eventually we should fix | |
2868 | our representation to allow this case to be handled, too. | |
2869 | In the case we see store of BINFO we however may assume | |
d6ae9a6d | 2870 | that standard folding will be able to cope with it. */ |
85942f45 JH |
2871 | return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)), |
2872 | offset, vtable); | |
390675c8 JH |
2873 | } |
2874 | ||
68377e53 | 2875 | /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET. |
d6ae9a6d SL |
2876 | Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE |
2877 | and insert them in NODES. | |
68377e53 JH |
2878 | |
2879 | MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */ | |
2880 | ||
2881 | static void | |
2882 | record_targets_from_bases (tree otr_type, | |
2883 | HOST_WIDE_INT otr_token, | |
2884 | tree outer_type, | |
2885 | HOST_WIDE_INT offset, | |
ec77d61f | 2886 | vec <cgraph_node *> &nodes, |
6e2830c3 TS |
2887 | hash_set<tree> *inserted, |
2888 | hash_set<tree> *matched_vtables, | |
68377e53 JH |
2889 | bool *completep) |
2890 | { | |
2891 | while (true) | |
2892 | { | |
2893 | HOST_WIDE_INT pos, size; | |
2894 | tree base_binfo; | |
2895 | tree fld; | |
2896 | ||
2897 | if (types_same_for_odr (outer_type, otr_type)) | |
2898 | return; | |
2899 | ||
2900 | for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld)) | |
2901 | { | |
2902 | if (TREE_CODE (fld) != FIELD_DECL) | |
2903 | continue; | |
2904 | ||
2905 | pos = int_bit_position (fld); | |
2906 | size = tree_to_shwi (DECL_SIZE (fld)); | |
ec77d61f JH |
2907 | if (pos <= offset && (pos + size) > offset |
2908 | /* Do not get confused by zero sized bases. */ | |
2909 | && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld)))) | |
68377e53 JH |
2910 | break; |
2911 | } | |
d6ae9a6d | 2912 | /* Within a class type we should always find corresponding fields. */ |
68377e53 JH |
2913 | gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE); |
2914 | ||
d6ae9a6d | 2915 | /* Nonbase types should have been stripped by outer_class_type. */ |
68377e53 JH |
2916 | gcc_assert (DECL_ARTIFICIAL (fld)); |
2917 | ||
2918 | outer_type = TREE_TYPE (fld); | |
2919 | offset -= pos; | |
2920 | ||
2921 | base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type), | |
2922 | offset, otr_type); | |
ec77d61f JH |
2923 | if (!base_binfo) |
2924 | { | |
2925 | gcc_assert (odr_violation_reported); | |
2926 | return; | |
2927 | } | |
68377e53 | 2928 | gcc_assert (base_binfo); |
6e2830c3 | 2929 | if (!matched_vtables->add (BINFO_VTABLE (base_binfo))) |
68377e53 | 2930 | { |
ec77d61f JH |
2931 | bool can_refer; |
2932 | tree target = gimple_get_virt_method_for_binfo (otr_token, | |
2933 | base_binfo, | |
2934 | &can_refer); | |
2d1644bf JH |
2935 | if (!target || ! DECL_CXX_DESTRUCTOR_P (target)) |
2936 | maybe_record_node (nodes, target, inserted, can_refer, completep); | |
6e2830c3 | 2937 | matched_vtables->add (BINFO_VTABLE (base_binfo)); |
68377e53 JH |
2938 | } |
2939 | } | |
2940 | } | |
2941 | ||
3462aa02 JH |
2942 | /* When virtual table is removed, we may need to flush the cache. */ |
2943 | ||
2944 | static void | |
2c8326a5 | 2945 | devirt_variable_node_removal_hook (varpool_node *n, |
3462aa02 JH |
2946 | void *d ATTRIBUTE_UNUSED) |
2947 | { | |
2948 | if (cached_polymorphic_call_targets | |
67348ccc DM |
2949 | && DECL_VIRTUAL_P (n->decl) |
2950 | && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl))) | |
3462aa02 JH |
2951 | free_polymorphic_call_targets_hash (); |
2952 | } | |
2953 | ||
91bc34a9 | 2954 | /* Record about how many calls would benefit from given type to be final. */ |
7d0aa05b | 2955 | |
91bc34a9 JH |
2956 | struct odr_type_warn_count |
2957 | { | |
9716cc3e | 2958 | tree type; |
91bc34a9 | 2959 | int count; |
3995f3a2 | 2960 | profile_count dyn_count; |
91bc34a9 JH |
2961 | }; |
2962 | ||
2963 | /* Record about how many calls would benefit from given method to be final. */ | |
7d0aa05b | 2964 | |
91bc34a9 JH |
2965 | struct decl_warn_count |
2966 | { | |
2967 | tree decl; | |
2968 | int count; | |
3995f3a2 | 2969 | profile_count dyn_count; |
91bc34a9 JH |
2970 | }; |
2971 | ||
2972 | /* Information about type and decl warnings. */ | |
7d0aa05b | 2973 | |
6c1dae73 | 2974 | class final_warning_record |
91bc34a9 | 2975 | { |
6c1dae73 | 2976 | public: |
53b73588 ML |
2977 | /* If needed grow type_warnings vector and initialize new decl_warn_count |
2978 | to have dyn_count set to profile_count::zero (). */ | |
2979 | void grow_type_warnings (unsigned newlen); | |
2980 | ||
3995f3a2 | 2981 | profile_count dyn_count; |
ed37a6cf | 2982 | auto_vec<odr_type_warn_count> type_warnings; |
91bc34a9 JH |
2983 | hash_map<tree, decl_warn_count> decl_warnings; |
2984 | }; | |
53b73588 ML |
2985 | |
2986 | void | |
2987 | final_warning_record::grow_type_warnings (unsigned newlen) | |
2988 | { | |
2989 | unsigned len = type_warnings.length (); | |
2990 | if (newlen > len) | |
2991 | { | |
cb3874dc | 2992 | type_warnings.safe_grow_cleared (newlen, true); |
53b73588 ML |
2993 | for (unsigned i = len; i < newlen; i++) |
2994 | type_warnings[i].dyn_count = profile_count::zero (); | |
2995 | } | |
2996 | } | |
2997 | ||
99b1c316 | 2998 | class final_warning_record *final_warning_records; |
91bc34a9 | 2999 | |
eefe9a99 | 3000 | /* Return vector containing possible targets of polymorphic call of type |
d6ae9a6d SL |
3001 | OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET. |
3002 | If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing | |
68377e53 JH |
3003 | OTR_TYPE and include their virtual method. This is useful for types |
3004 | possibly in construction or destruction where the virtual table may | |
956d615d | 3005 | temporarily change to one of base types. INCLUDE_DERIVED_TYPES make |
68377e53 JH |
3006 | us to walk the inheritance graph for all derivations. |
3007 | ||
add5c763 | 3008 | If COMPLETEP is non-NULL, store true if the list is complete. |
eefe9a99 JH |
3009 | CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry |
3010 | in the target cache. If user needs to visit every target list | |
3011 | just once, it can memoize them. | |
3012 | ||
2f28755f JH |
3013 | If SPECULATIVE is set, the list will not contain targets that |
3014 | are not speculatively taken. | |
ec77d61f | 3015 | |
eefe9a99 JH |
3016 | Returned vector is placed into cache. It is NOT caller's responsibility |
3017 | to free it. The vector can be freed on cgraph_remove_node call if | |
3018 | the particular node is a virtual function present in the cache. */ | |
3019 | ||
3020 | vec <cgraph_node *> | |
3021 | possible_polymorphic_call_targets (tree otr_type, | |
3022 | HOST_WIDE_INT otr_token, | |
68377e53 JH |
3023 | ipa_polymorphic_call_context context, |
3024 | bool *completep, | |
ec77d61f | 3025 | void **cache_token, |
2f28755f | 3026 | bool speculative) |
eefe9a99 JH |
3027 | { |
3028 | static struct cgraph_node_hook_list *node_removal_hook_holder; | |
add5c763 | 3029 | vec <cgraph_node *> nodes = vNULL; |
1be0e58d | 3030 | auto_vec <tree, 8> bases_to_consider; |
68377e53 | 3031 | odr_type type, outer_type; |
eefe9a99 JH |
3032 | polymorphic_call_target_d key; |
3033 | polymorphic_call_target_d **slot; | |
3034 | unsigned int i; | |
3035 | tree binfo, target; | |
ec77d61f | 3036 | bool complete; |
b2d82f2d | 3037 | bool can_refer = false; |
2d1644bf | 3038 | bool skipped = false; |
eefe9a99 | 3039 | |
c7e1befa JH |
3040 | otr_type = TYPE_MAIN_VARIANT (otr_type); |
3041 | ||
d6ae9a6d | 3042 | /* If ODR is not initialized or the context is invalid, return empty |
6f8091fc | 3043 | incomplete list. */ |
b41e0d29 | 3044 | if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type)) |
3e86c6a8 JH |
3045 | { |
3046 | if (completep) | |
6f8091fc | 3047 | *completep = context.invalid; |
beb683ab MJ |
3048 | if (cache_token) |
3049 | *cache_token = NULL; | |
3e86c6a8 JH |
3050 | return nodes; |
3051 | } | |
3052 | ||
91bc34a9 | 3053 | /* Do not bother to compute speculative info when user do not asks for it. */ |
2f28755f | 3054 | if (!speculative || !context.speculative_outer_type) |
4d7cf10d | 3055 | context.clear_speculation (); |
91bc34a9 | 3056 | |
68377e53 | 3057 | type = get_odr_type (otr_type, true); |
eefe9a99 | 3058 | |
d6ae9a6d | 3059 | /* Recording type variants would waste results cache. */ |
c7e1befa JH |
3060 | gcc_assert (!context.outer_type |
3061 | || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type); | |
3062 | ||
d6ae9a6d | 3063 | /* Look up the outer class type we want to walk. |
a1981458 | 3064 | If we fail to do so, the context is invalid. */ |
a0fd3373 | 3065 | if ((context.outer_type || context.speculative_outer_type) |
4d7cf10d | 3066 | && !context.restrict_to_inner_class (otr_type)) |
3e86c6a8 JH |
3067 | { |
3068 | if (completep) | |
a1981458 | 3069 | *completep = true; |
beb683ab MJ |
3070 | if (cache_token) |
3071 | *cache_token = NULL; | |
3e86c6a8 JH |
3072 | return nodes; |
3073 | } | |
a1981458 | 3074 | gcc_assert (!context.invalid); |
eefe9a99 | 3075 | |
4d7cf10d | 3076 | /* Check that restrict_to_inner_class kept the main variant. */ |
c7e1befa JH |
3077 | gcc_assert (!context.outer_type |
3078 | || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type); | |
3079 | ||
79c7de84 | 3080 | /* We canonicalize our query, so we do not need extra hashtable entries. */ |
68377e53 JH |
3081 | |
3082 | /* Without outer type, we have no use for offset. Just do the | |
d6ae9a6d | 3083 | basic search from inner type. */ |
68377e53 | 3084 | if (!context.outer_type) |
6ff65dd7 | 3085 | context.clear_outer_type (otr_type); |
d6ae9a6d | 3086 | /* We need to update our hierarchy if the type does not exist. */ |
68377e53 | 3087 | outer_type = get_odr_type (context.outer_type, true); |
ec77d61f | 3088 | /* If the type is complete, there are no derivations. */ |
68377e53 JH |
3089 | if (TYPE_FINAL_P (outer_type->type)) |
3090 | context.maybe_derived_type = false; | |
eefe9a99 JH |
3091 | |
3092 | /* Initialize query cache. */ | |
3093 | if (!cached_polymorphic_call_targets) | |
3094 | { | |
6e2830c3 | 3095 | cached_polymorphic_call_targets = new hash_set<cgraph_node *>; |
c203e8a7 TS |
3096 | polymorphic_call_target_hash |
3097 | = new polymorphic_call_target_hash_type (23); | |
eefe9a99 | 3098 | if (!node_removal_hook_holder) |
3462aa02 JH |
3099 | { |
3100 | node_removal_hook_holder = | |
3dafb85c ML |
3101 | symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL); |
3102 | symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook, | |
3462aa02 JH |
3103 | NULL); |
3104 | } | |
eefe9a99 JH |
3105 | } |
3106 | ||
21a9ce6e JH |
3107 | if (in_lto_p) |
3108 | { | |
3109 | if (context.outer_type != otr_type) | |
3110 | context.outer_type | |
3111 | = get_odr_type (context.outer_type, true)->type; | |
3112 | if (context.speculative_outer_type) | |
3113 | context.speculative_outer_type | |
3114 | = get_odr_type (context.speculative_outer_type, true)->type; | |
3115 | } | |
3116 | ||
d6ae9a6d | 3117 | /* Look up cached answer. */ |
eefe9a99 JH |
3118 | key.type = type; |
3119 | key.otr_token = otr_token; | |
2f28755f | 3120 | key.speculative = speculative; |
68377e53 | 3121 | key.context = context; |
f7293b9d | 3122 | key.n_odr_types = odr_types.length (); |
c203e8a7 | 3123 | slot = polymorphic_call_target_hash->find_slot (&key, INSERT); |
eefe9a99 JH |
3124 | if (cache_token) |
3125 | *cache_token = (void *)*slot; | |
3126 | if (*slot) | |
68377e53 JH |
3127 | { |
3128 | if (completep) | |
ec77d61f | 3129 | *completep = (*slot)->complete; |
91bc34a9 JH |
3130 | if ((*slot)->type_warning && final_warning_records) |
3131 | { | |
3132 | final_warning_records->type_warnings[(*slot)->type_warning - 1].count++; | |
3995f3a2 JH |
3133 | if (!final_warning_records->type_warnings |
3134 | [(*slot)->type_warning - 1].dyn_count.initialized_p ()) | |
3135 | final_warning_records->type_warnings | |
3136 | [(*slot)->type_warning - 1].dyn_count = profile_count::zero (); | |
3137 | if (final_warning_records->dyn_count > 0) | |
3138 | final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count | |
3139 | = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count | |
3140 | + final_warning_records->dyn_count; | |
91bc34a9 | 3141 | } |
2f28755f | 3142 | if (!speculative && (*slot)->decl_warning && final_warning_records) |
91bc34a9 JH |
3143 | { |
3144 | struct decl_warn_count *c = | |
3145 | final_warning_records->decl_warnings.get ((*slot)->decl_warning); | |
3146 | c->count++; | |
3995f3a2 JH |
3147 | if (final_warning_records->dyn_count > 0) |
3148 | c->dyn_count += final_warning_records->dyn_count; | |
91bc34a9 | 3149 | } |
68377e53 JH |
3150 | return (*slot)->targets; |
3151 | } | |
3152 | ||
ec77d61f | 3153 | complete = true; |
eefe9a99 JH |
3154 | |
3155 | /* Do actual search. */ | |
3156 | timevar_push (TV_IPA_VIRTUAL_CALL); | |
3157 | *slot = XCNEW (polymorphic_call_target_d); | |
3158 | if (cache_token) | |
68377e53 | 3159 | *cache_token = (void *)*slot; |
eefe9a99 JH |
3160 | (*slot)->type = type; |
3161 | (*slot)->otr_token = otr_token; | |
68377e53 | 3162 | (*slot)->context = context; |
2f28755f | 3163 | (*slot)->speculative = speculative; |
eefe9a99 | 3164 | |
6e2830c3 TS |
3165 | hash_set<tree> inserted; |
3166 | hash_set<tree> matched_vtables; | |
eefe9a99 | 3167 | |
91bc34a9 | 3168 | /* First insert targets we speculatively identified as likely. */ |
a0fd3373 JH |
3169 | if (context.speculative_outer_type) |
3170 | { | |
3171 | odr_type speculative_outer_type; | |
91bc34a9 JH |
3172 | bool speculation_complete = true; |
3173 | ||
d6ae9a6d SL |
3174 | /* First insert target from type itself and check if it may have |
3175 | derived types. */ | |
a0fd3373 JH |
3176 | speculative_outer_type = get_odr_type (context.speculative_outer_type, true); |
3177 | if (TYPE_FINAL_P (speculative_outer_type->type)) | |
3178 | context.speculative_maybe_derived_type = false; | |
3179 | binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type), | |
3180 | context.speculative_offset, otr_type); | |
3181 | if (binfo) | |
3182 | target = gimple_get_virt_method_for_binfo (otr_token, binfo, | |
3183 | &can_refer); | |
3184 | else | |
3185 | target = NULL; | |
3186 | ||
91bc34a9 JH |
3187 | /* In the case we get complete method, we don't need |
3188 | to walk derivations. */ | |
3189 | if (target && DECL_FINAL_P (target)) | |
3190 | context.speculative_maybe_derived_type = false; | |
a0fd3373 | 3191 | if (type_possibly_instantiated_p (speculative_outer_type->type)) |
91bc34a9 | 3192 | maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete); |
a0fd3373 | 3193 | if (binfo) |
6e2830c3 | 3194 | matched_vtables.add (BINFO_VTABLE (binfo)); |
91bc34a9 | 3195 | |
9716cc3e | 3196 | |
a0fd3373 JH |
3197 | /* Next walk recursively all derived types. */ |
3198 | if (context.speculative_maybe_derived_type) | |
91bc34a9 JH |
3199 | for (i = 0; i < speculative_outer_type->derived_types.length(); i++) |
3200 | possible_polymorphic_call_targets_1 (nodes, &inserted, | |
3201 | &matched_vtables, | |
3202 | otr_type, | |
3203 | speculative_outer_type->derived_types[i], | |
3204 | otr_token, speculative_outer_type->type, | |
3205 | context.speculative_offset, | |
3206 | &speculation_complete, | |
3207 | bases_to_consider, | |
3208 | false); | |
a0fd3373 JH |
3209 | } |
3210 | ||
2f28755f | 3211 | if (!speculative || !nodes.length ()) |
68377e53 | 3212 | { |
2f28755f JH |
3213 | /* First see virtual method of type itself. */ |
3214 | binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type), | |
3215 | context.offset, otr_type); | |
3216 | if (binfo) | |
3217 | target = gimple_get_virt_method_for_binfo (otr_token, binfo, | |
3218 | &can_refer); | |
3219 | else | |
3220 | { | |
3221 | gcc_assert (odr_violation_reported); | |
3222 | target = NULL; | |
3223 | } | |
68377e53 | 3224 | |
2f28755f JH |
3225 | /* Destructors are never called through construction virtual tables, |
3226 | because the type is always known. */ | |
3227 | if (target && DECL_CXX_DESTRUCTOR_P (target)) | |
3228 | context.maybe_in_construction = false; | |
ec77d61f | 3229 | |
2f28755f JH |
3230 | if (target) |
3231 | { | |
3232 | /* In the case we get complete method, we don't need | |
3233 | to walk derivations. */ | |
3234 | if (DECL_FINAL_P (target)) | |
3235 | context.maybe_derived_type = false; | |
3236 | } | |
2d1644bf | 3237 | |
2f28755f JH |
3238 | /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */ |
3239 | if (type_possibly_instantiated_p (outer_type->type)) | |
3240 | maybe_record_node (nodes, target, &inserted, can_refer, &complete); | |
3241 | else | |
3242 | skipped = true; | |
79c7de84 | 3243 | |
2f28755f JH |
3244 | if (binfo) |
3245 | matched_vtables.add (BINFO_VTABLE (binfo)); | |
eefe9a99 | 3246 | |
2f28755f JH |
3247 | /* Next walk recursively all derived types. */ |
3248 | if (context.maybe_derived_type) | |
91bc34a9 | 3249 | { |
2f28755f JH |
3250 | for (i = 0; i < outer_type->derived_types.length(); i++) |
3251 | possible_polymorphic_call_targets_1 (nodes, &inserted, | |
3252 | &matched_vtables, | |
3253 | otr_type, | |
3254 | outer_type->derived_types[i], | |
3255 | otr_token, outer_type->type, | |
3256 | context.offset, &complete, | |
3257 | bases_to_consider, | |
3258 | context.maybe_in_construction); | |
3259 | ||
3260 | if (!outer_type->all_derivations_known) | |
91bc34a9 | 3261 | { |
079cd854 | 3262 | if (!speculative && final_warning_records |
448476ff | 3263 | && nodes.length () == 1 |
079cd854 | 3264 | && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE) |
91bc34a9 | 3265 | { |
2f28755f | 3266 | if (complete |
2f28755f JH |
3267 | && warn_suggest_final_types |
3268 | && !outer_type->derived_types.length ()) | |
91bc34a9 | 3269 | { |
53b73588 ML |
3270 | final_warning_records->grow_type_warnings |
3271 | (outer_type->id); | |
2f28755f | 3272 | final_warning_records->type_warnings[outer_type->id].count++; |
3995f3a2 JH |
3273 | if (!final_warning_records->type_warnings |
3274 | [outer_type->id].dyn_count.initialized_p ()) | |
3275 | final_warning_records->type_warnings | |
3276 | [outer_type->id].dyn_count = profile_count::zero (); | |
2f28755f JH |
3277 | final_warning_records->type_warnings[outer_type->id].dyn_count |
3278 | += final_warning_records->dyn_count; | |
3279 | final_warning_records->type_warnings[outer_type->id].type | |
3280 | = outer_type->type; | |
3281 | (*slot)->type_warning = outer_type->id + 1; | |
91bc34a9 | 3282 | } |
2f28755f JH |
3283 | if (complete |
3284 | && warn_suggest_final_methods | |
2f28755f JH |
3285 | && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl), |
3286 | outer_type->type)) | |
91bc34a9 | 3287 | { |
2f28755f JH |
3288 | bool existed; |
3289 | struct decl_warn_count &c = | |
3290 | final_warning_records->decl_warnings.get_or_insert | |
3291 | (nodes[0]->decl, &existed); | |
3292 | ||
3293 | if (existed) | |
3294 | { | |
3295 | c.count++; | |
3296 | c.dyn_count += final_warning_records->dyn_count; | |
3297 | } | |
3298 | else | |
3299 | { | |
3300 | c.count = 1; | |
3301 | c.dyn_count = final_warning_records->dyn_count; | |
3302 | c.decl = nodes[0]->decl; | |
3303 | } | |
3304 | (*slot)->decl_warning = nodes[0]->decl; | |
91bc34a9 | 3305 | } |
91bc34a9 | 3306 | } |
2f28755f | 3307 | complete = false; |
91bc34a9 | 3308 | } |
91bc34a9 | 3309 | } |
2d1644bf | 3310 | |
2f28755f JH |
3311 | if (!speculative) |
3312 | { | |
3313 | /* Destructors are never called through construction virtual tables, | |
d6ae9a6d SL |
3314 | because the type is always known. One of entries may be |
3315 | cxa_pure_virtual so look to at least two of them. */ | |
2f28755f JH |
3316 | if (context.maybe_in_construction) |
3317 | for (i =0 ; i < MIN (nodes.length (), 2); i++) | |
3318 | if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl)) | |
3319 | context.maybe_in_construction = false; | |
3320 | if (context.maybe_in_construction) | |
3321 | { | |
3322 | if (type != outer_type | |
3323 | && (!skipped | |
3324 | || (context.maybe_derived_type | |
3325 | && !type_all_derivations_known_p (outer_type->type)))) | |
3326 | record_targets_from_bases (otr_type, otr_token, outer_type->type, | |
3327 | context.offset, nodes, &inserted, | |
3328 | &matched_vtables, &complete); | |
3329 | if (skipped) | |
3330 | maybe_record_node (nodes, target, &inserted, can_refer, &complete); | |
3331 | for (i = 0; i < bases_to_consider.length(); i++) | |
3332 | maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete); | |
3333 | } | |
3334 | } | |
2d1644bf | 3335 | } |
ec77d61f | 3336 | |
eefe9a99 | 3337 | (*slot)->targets = nodes; |
ec77d61f | 3338 | (*slot)->complete = complete; |
f7293b9d | 3339 | (*slot)->n_odr_types = odr_types.length (); |
68377e53 | 3340 | if (completep) |
ec77d61f | 3341 | *completep = complete; |
eefe9a99 | 3342 | |
eefe9a99 JH |
3343 | timevar_pop (TV_IPA_VIRTUAL_CALL); |
3344 | return nodes; | |
3345 | } | |
3346 | ||
91bc34a9 JH |
3347 | bool |
3348 | add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value, | |
3349 | vec<const decl_warn_count*> *vec) | |
3350 | { | |
3351 | vec->safe_push (&value); | |
3352 | return true; | |
3353 | } | |
3354 | ||
2f28755f JH |
3355 | /* Dump target list TARGETS into FILE. */ |
3356 | ||
3357 | static void | |
c1dd347c | 3358 | dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose) |
2f28755f JH |
3359 | { |
3360 | unsigned int i; | |
3361 | ||
3362 | for (i = 0; i < targets.length (); i++) | |
3363 | { | |
3364 | char *name = NULL; | |
3365 | if (in_lto_p) | |
3366 | name = cplus_demangle_v3 (targets[i]->asm_name (), 0); | |
3629ff8a | 3367 | fprintf (f, " %s", name ? name : targets[i]->dump_name ()); |
2f28755f JH |
3368 | if (in_lto_p) |
3369 | free (name); | |
3370 | if (!targets[i]->definition) | |
3371 | fprintf (f, " (no definition%s)", | |
3372 | DECL_DECLARED_INLINE_P (targets[i]->decl) | |
3373 | ? " inline" : ""); | |
c1dd347c JH |
3374 | /* With many targets for every call polymorphic dumps are going to |
3375 | be quadratic in size. */ | |
3376 | if (i > 10 && !verbose) | |
3377 | { | |
3378 | fprintf (f, " ... and %i more targets\n", targets.length () - i); | |
3379 | return; | |
3380 | } | |
2f28755f JH |
3381 | } |
3382 | fprintf (f, "\n"); | |
3383 | } | |
3384 | ||
eefe9a99 JH |
3385 | /* Dump all possible targets of a polymorphic call. */ |
3386 | ||
3387 | void | |
3388 | dump_possible_polymorphic_call_targets (FILE *f, | |
68377e53 JH |
3389 | tree otr_type, |
3390 | HOST_WIDE_INT otr_token, | |
c1dd347c JH |
3391 | const ipa_polymorphic_call_context &ctx, |
3392 | bool verbose) | |
eefe9a99 JH |
3393 | { |
3394 | vec <cgraph_node *> targets; | |
3395 | bool final; | |
549bcbd1 | 3396 | odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false); |
2f28755f | 3397 | unsigned int len; |
eefe9a99 JH |
3398 | |
3399 | if (!type) | |
3400 | return; | |
3401 | targets = possible_polymorphic_call_targets (otr_type, otr_token, | |
68377e53 | 3402 | ctx, |
2f28755f | 3403 | &final, NULL, false); |
68377e53 | 3404 | fprintf (f, " Targets of polymorphic call of type %i:", type->id); |
eefe9a99 | 3405 | print_generic_expr (f, type->type, TDF_SLIM); |
ec77d61f | 3406 | fprintf (f, " token %i\n", (int)otr_token); |
a1981458 JH |
3407 | |
3408 | ctx.dump (f); | |
ec77d61f | 3409 | |
a0fd3373 | 3410 | fprintf (f, " %s%s%s%s\n ", |
ec77d61f | 3411 | final ? "This is a complete list." : |
68377e53 JH |
3412 | "This is partial list; extra targets may be defined in other units.", |
3413 | ctx.maybe_in_construction ? " (base types included)" : "", | |
a0fd3373 JH |
3414 | ctx.maybe_derived_type ? " (derived types included)" : "", |
3415 | ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : ""); | |
2f28755f | 3416 | len = targets.length (); |
c1dd347c | 3417 | dump_targets (f, targets, verbose); |
2f28755f JH |
3418 | |
3419 | targets = possible_polymorphic_call_targets (otr_type, otr_token, | |
3420 | ctx, | |
3421 | &final, NULL, true); | |
2f28755f | 3422 | if (targets.length () != len) |
ec77d61f | 3423 | { |
2f28755f | 3424 | fprintf (f, " Speculative targets:"); |
c1dd347c | 3425 | dump_targets (f, targets, verbose); |
ec77d61f | 3426 | } |
3ebd8e62 ML |
3427 | /* Ugly: during callgraph construction the target cache may get populated |
3428 | before all targets are found. While this is harmless (because all local | |
3429 | types are discovered and only in those case we devirtualize fully and we | |
3430 | don't do speculative devirtualization before IPA stage) it triggers | |
3431 | assert here when dumping at that stage also populates the case with | |
3432 | speculative targets. Quietly ignore this. */ | |
3433 | gcc_assert (symtab->state < IPA_SSA || targets.length () <= len); | |
2f28755f | 3434 | fprintf (f, "\n"); |
eefe9a99 JH |
3435 | } |
3436 | ||
0e1474e5 JH |
3437 | |
3438 | /* Return true if N can be possibly target of a polymorphic call of | |
3439 | OTR_TYPE/OTR_TOKEN. */ | |
3440 | ||
3441 | bool | |
3442 | possible_polymorphic_call_target_p (tree otr_type, | |
3443 | HOST_WIDE_INT otr_token, | |
68377e53 | 3444 | const ipa_polymorphic_call_context &ctx, |
0e1474e5 JH |
3445 | struct cgraph_node *n) |
3446 | { | |
3447 | vec <cgraph_node *> targets; | |
3448 | unsigned int i; | |
450ad0cd | 3449 | bool final; |
0e1474e5 | 3450 | |
cb1180d5 RS |
3451 | if (fndecl_built_in_p (n->decl, BUILT_IN_UNREACHABLE) |
3452 | || fndecl_built_in_p (n->decl, BUILT_IN_TRAP)) | |
68377e53 JH |
3453 | return true; |
3454 | ||
ceda2c69 JH |
3455 | if (is_cxa_pure_virtual_p (n->decl)) |
3456 | return true; | |
3457 | ||
c203e8a7 | 3458 | if (!odr_hash) |
0e1474e5 | 3459 | return true; |
68377e53 | 3460 | targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final); |
0e1474e5 | 3461 | for (i = 0; i < targets.length (); i++) |
d52f5295 | 3462 | if (n->semantically_equivalent_p (targets[i])) |
0e1474e5 | 3463 | return true; |
450ad0cd JH |
3464 | |
3465 | /* At a moment we allow middle end to dig out new external declarations | |
3466 | as a targets of polymorphic calls. */ | |
67348ccc | 3467 | if (!final && !n->definition) |
450ad0cd | 3468 | return true; |
0e1474e5 JH |
3469 | return false; |
3470 | } | |
3471 | ||
3472 | ||
6f8091fc JH |
3473 | |
3474 | /* Return true if N can be possibly target of a polymorphic call of | |
3475 | OBJ_TYPE_REF expression REF in STMT. */ | |
3476 | ||
3477 | bool | |
3478 | possible_polymorphic_call_target_p (tree ref, | |
355fe088 | 3479 | gimple *stmt, |
6f8091fc JH |
3480 | struct cgraph_node *n) |
3481 | { | |
3482 | ipa_polymorphic_call_context context (current_function_decl, ref, stmt); | |
3483 | tree call_fn = gimple_call_fn (stmt); | |
3484 | ||
3485 | return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn), | |
3486 | tree_to_uhwi | |
3487 | (OBJ_TYPE_REF_TOKEN (call_fn)), | |
3488 | context, | |
3489 | n); | |
3490 | } | |
3491 | ||
3492 | ||
0e1474e5 JH |
3493 | /* After callgraph construction new external nodes may appear. |
3494 | Add them into the graph. */ | |
3495 | ||
3496 | void | |
3497 | update_type_inheritance_graph (void) | |
3498 | { | |
3499 | struct cgraph_node *n; | |
3500 | ||
c203e8a7 | 3501 | if (!odr_hash) |
0e1474e5 JH |
3502 | return; |
3503 | free_polymorphic_call_targets_hash (); | |
3504 | timevar_push (TV_IPA_INHERITANCE); | |
68377e53 | 3505 | /* We reconstruct the graph starting from types of all methods seen in the |
6af801f5 | 3506 | unit. */ |
0e1474e5 | 3507 | FOR_EACH_FUNCTION (n) |
67348ccc DM |
3508 | if (DECL_VIRTUAL_P (n->decl) |
3509 | && !n->definition | |
d52f5295 | 3510 | && n->real_symbol_p ()) |
70e7f2a2 | 3511 | get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true); |
0e1474e5 JH |
3512 | timevar_pop (TV_IPA_INHERITANCE); |
3513 | } | |
bbc9396b JH |
3514 | |
3515 | ||
3516 | /* Return true if N looks like likely target of a polymorphic call. | |
3517 | Rule out cxa_pure_virtual, noreturns, function declared cold and | |
3518 | other obvious cases. */ | |
3519 | ||
3520 | bool | |
3521 | likely_target_p (struct cgraph_node *n) | |
3522 | { | |
3523 | int flags; | |
3524 | /* cxa_pure_virtual and similar things are not likely. */ | |
67348ccc | 3525 | if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE) |
bbc9396b | 3526 | return false; |
67348ccc | 3527 | flags = flags_from_decl_or_type (n->decl); |
bbc9396b JH |
3528 | if (flags & ECF_NORETURN) |
3529 | return false; | |
3530 | if (lookup_attribute ("cold", | |
67348ccc | 3531 | DECL_ATTRIBUTES (n->decl))) |
bbc9396b JH |
3532 | return false; |
3533 | if (n->frequency < NODE_FREQUENCY_NORMAL) | |
3534 | return false; | |
d6ae9a6d SL |
3535 | /* If there are no live virtual tables referring the target, |
3536 | the only way the target can be called is an instance coming from other | |
3537 | compilation unit; speculative devirtualization is built around an | |
ccb05ef2 JH |
3538 | assumption that won't happen. */ |
3539 | if (!referenced_from_vtable_p (n)) | |
3540 | return false; | |
bbc9396b JH |
3541 | return true; |
3542 | } | |
3543 | ||
d6ae9a6d | 3544 | /* Compare type warning records P1 and P2 and choose one with larger count; |
91bc34a9 JH |
3545 | helper for qsort. */ |
3546 | ||
b6de3028 | 3547 | static int |
91bc34a9 JH |
3548 | type_warning_cmp (const void *p1, const void *p2) |
3549 | { | |
3550 | const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1; | |
3551 | const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2; | |
3552 | ||
3553 | if (t1->dyn_count < t2->dyn_count) | |
3554 | return 1; | |
3555 | if (t1->dyn_count > t2->dyn_count) | |
3556 | return -1; | |
3557 | return t2->count - t1->count; | |
3558 | } | |
3559 | ||
d6ae9a6d | 3560 | /* Compare decl warning records P1 and P2 and choose one with larger count; |
91bc34a9 JH |
3561 | helper for qsort. */ |
3562 | ||
b6de3028 | 3563 | static int |
91bc34a9 JH |
3564 | decl_warning_cmp (const void *p1, const void *p2) |
3565 | { | |
3566 | const decl_warn_count *t1 = *(const decl_warn_count * const *)p1; | |
3567 | const decl_warn_count *t2 = *(const decl_warn_count * const *)p2; | |
3568 | ||
3569 | if (t1->dyn_count < t2->dyn_count) | |
3570 | return 1; | |
3571 | if (t1->dyn_count > t2->dyn_count) | |
3572 | return -1; | |
3573 | return t2->count - t1->count; | |
3574 | } | |
3575 | ||
5ce97055 | 3576 | |
d6ae9a6d | 3577 | /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with |
5ce97055 JH |
3578 | context CTX. */ |
3579 | ||
3580 | struct cgraph_node * | |
3581 | try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token, | |
3582 | ipa_polymorphic_call_context ctx) | |
3583 | { | |
3584 | vec <cgraph_node *>targets | |
3585 | = possible_polymorphic_call_targets | |
3586 | (otr_type, otr_token, ctx, NULL, NULL, true); | |
3587 | unsigned int i; | |
3588 | struct cgraph_node *likely_target = NULL; | |
3589 | ||
3590 | for (i = 0; i < targets.length (); i++) | |
3591 | if (likely_target_p (targets[i])) | |
3592 | { | |
3593 | if (likely_target) | |
3594 | return NULL; | |
3595 | likely_target = targets[i]; | |
3596 | } | |
3597 | if (!likely_target | |
3598 | ||!likely_target->definition | |
3599 | || DECL_EXTERNAL (likely_target->decl)) | |
3600 | return NULL; | |
3601 | ||
3602 | /* Don't use an implicitly-declared destructor (c++/58678). */ | |
3603 | struct cgraph_node *non_thunk_target | |
3604 | = likely_target->function_symbol (); | |
3605 | if (DECL_ARTIFICIAL (non_thunk_target->decl)) | |
3606 | return NULL; | |
3607 | if (likely_target->get_availability () <= AVAIL_INTERPOSABLE | |
3608 | && likely_target->can_be_discarded_p ()) | |
3609 | return NULL; | |
3610 | return likely_target; | |
3611 | } | |
3612 | ||
bbc9396b | 3613 | /* The ipa-devirt pass. |
3462aa02 | 3614 | When polymorphic call has only one likely target in the unit, |
d6ae9a6d | 3615 | turn it into a speculative call. */ |
bbc9396b JH |
3616 | |
3617 | static unsigned int | |
3618 | ipa_devirt (void) | |
3619 | { | |
3620 | struct cgraph_node *n; | |
6e2830c3 | 3621 | hash_set<void *> bad_call_targets; |
bbc9396b JH |
3622 | struct cgraph_edge *e; |
3623 | ||
3624 | int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0; | |
3625 | int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0; | |
570215f9 | 3626 | int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0; |
68c9467f | 3627 | int ndropped = 0; |
bbc9396b | 3628 | |
4cd5658b MT |
3629 | if (!odr_types_ptr) |
3630 | return 0; | |
3631 | ||
609570b4 JH |
3632 | if (dump_file) |
3633 | dump_type_inheritance_graph (dump_file); | |
3634 | ||
91bc34a9 JH |
3635 | /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings. |
3636 | This is implemented by setting up final_warning_records that are updated | |
3637 | by get_polymorphic_call_targets. | |
3638 | We need to clear cache in this case to trigger recomputation of all | |
3639 | entries. */ | |
3640 | if (warn_suggest_final_methods || warn_suggest_final_types) | |
3641 | { | |
3642 | final_warning_records = new (final_warning_record); | |
3995f3a2 | 3643 | final_warning_records->dyn_count = profile_count::zero (); |
53b73588 | 3644 | final_warning_records->grow_type_warnings (odr_types.length ()); |
91bc34a9 JH |
3645 | free_polymorphic_call_targets_hash (); |
3646 | } | |
3647 | ||
bbc9396b JH |
3648 | FOR_EACH_DEFINED_FUNCTION (n) |
3649 | { | |
3650 | bool update = false; | |
2bf86c84 JH |
3651 | if (!opt_for_fn (n->decl, flag_devirtualize)) |
3652 | continue; | |
bbc9396b | 3653 | if (dump_file && n->indirect_calls) |
464d0118 ML |
3654 | fprintf (dump_file, "\n\nProcesing function %s\n", |
3655 | n->dump_name ()); | |
bbc9396b JH |
3656 | for (e = n->indirect_calls; e; e = e->next_callee) |
3657 | if (e->indirect_info->polymorphic) | |
3658 | { | |
3659 | struct cgraph_node *likely_target = NULL; | |
3660 | void *cache_token; | |
3661 | bool final; | |
91bc34a9 JH |
3662 | |
3663 | if (final_warning_records) | |
1bad9c18 | 3664 | final_warning_records->dyn_count = e->count.ipa (); |
91bc34a9 | 3665 | |
bbc9396b JH |
3666 | vec <cgraph_node *>targets |
3667 | = possible_polymorphic_call_targets | |
2f28755f | 3668 | (e, &final, &cache_token, true); |
bbc9396b JH |
3669 | unsigned int i; |
3670 | ||
2f28755f JH |
3671 | /* Trigger warnings by calculating non-speculative targets. */ |
3672 | if (warn_suggest_final_methods || warn_suggest_final_types) | |
3673 | possible_polymorphic_call_targets (e); | |
3674 | ||
bbc9396b JH |
3675 | if (dump_file) |
3676 | dump_possible_polymorphic_call_targets | |
c1dd347c | 3677 | (dump_file, e, (dump_flags & TDF_DETAILS)); |
3462aa02 | 3678 | |
bbc9396b JH |
3679 | npolymorphic++; |
3680 | ||
68c9467f JH |
3681 | /* See if the call can be devirtualized by means of ipa-prop's |
3682 | polymorphic call context propagation. If not, we can just | |
3683 | forget about this call being polymorphic and avoid some heavy | |
3684 | lifting in remove_unreachable_nodes that will otherwise try to | |
3685 | keep all possible targets alive until inlining and in the inliner | |
3686 | itself. | |
3687 | ||
3688 | This may need to be revisited once we add further ways to use | |
956d615d | 3689 | the may edges, but it is a reasonable thing to do right now. */ |
68c9467f JH |
3690 | |
3691 | if ((e->indirect_info->param_index == -1 | |
3692 | || (!opt_for_fn (n->decl, flag_devirtualize_speculatively) | |
3693 | && e->indirect_info->vptr_changed)) | |
3694 | && !flag_ltrans_devirtualize) | |
3695 | { | |
3696 | e->indirect_info->polymorphic = false; | |
3697 | ndropped++; | |
3698 | if (dump_file) | |
3699 | fprintf (dump_file, "Dropping polymorphic call info;" | |
67914693 | 3700 | " it cannot be used by ipa-prop\n"); |
68c9467f JH |
3701 | } |
3702 | ||
2bf86c84 | 3703 | if (!opt_for_fn (n->decl, flag_devirtualize_speculatively)) |
91bc34a9 JH |
3704 | continue; |
3705 | ||
3dafb85c | 3706 | if (!e->maybe_hot_p ()) |
bbc9396b JH |
3707 | { |
3708 | if (dump_file) | |
ec77d61f | 3709 | fprintf (dump_file, "Call is cold\n\n"); |
bbc9396b JH |
3710 | ncold++; |
3711 | continue; | |
3712 | } | |
3713 | if (e->speculative) | |
3714 | { | |
3715 | if (dump_file) | |
d6ae9a6d | 3716 | fprintf (dump_file, "Call is already speculated\n\n"); |
bbc9396b JH |
3717 | nspeculated++; |
3718 | ||
3719 | /* When dumping see if we agree with speculation. */ | |
3720 | if (!dump_file) | |
3721 | continue; | |
3722 | } | |
6e2830c3 | 3723 | if (bad_call_targets.contains (cache_token)) |
bbc9396b JH |
3724 | { |
3725 | if (dump_file) | |
ec77d61f | 3726 | fprintf (dump_file, "Target list is known to be useless\n\n"); |
bbc9396b JH |
3727 | nmultiple++; |
3728 | continue; | |
3729 | } | |
c3284718 | 3730 | for (i = 0; i < targets.length (); i++) |
bbc9396b JH |
3731 | if (likely_target_p (targets[i])) |
3732 | { | |
3733 | if (likely_target) | |
3734 | { | |
2f28755f JH |
3735 | likely_target = NULL; |
3736 | if (dump_file) | |
3737 | fprintf (dump_file, "More than one likely target\n\n"); | |
3738 | nmultiple++; | |
bbc9396b JH |
3739 | break; |
3740 | } | |
3741 | likely_target = targets[i]; | |
3742 | } | |
3743 | if (!likely_target) | |
3744 | { | |
6e2830c3 | 3745 | bad_call_targets.add (cache_token); |
bbc9396b JH |
3746 | continue; |
3747 | } | |
3748 | /* This is reached only when dumping; check if we agree or disagree | |
3749 | with the speculation. */ | |
3750 | if (e->speculative) | |
3751 | { | |
845bb366 JH |
3752 | bool found = e->speculative_call_for_target (likely_target); |
3753 | if (found) | |
bbc9396b | 3754 | { |
ec77d61f | 3755 | fprintf (dump_file, "We agree with speculation\n\n"); |
bbc9396b JH |
3756 | nok++; |
3757 | } | |
3758 | else | |
3759 | { | |
ec77d61f | 3760 | fprintf (dump_file, "We disagree with speculation\n\n"); |
bbc9396b JH |
3761 | nwrong++; |
3762 | } | |
3763 | continue; | |
3764 | } | |
67348ccc | 3765 | if (!likely_target->definition) |
bbc9396b JH |
3766 | { |
3767 | if (dump_file) | |
d6ae9a6d | 3768 | fprintf (dump_file, "Target is not a definition\n\n"); |
bbc9396b JH |
3769 | nnotdefined++; |
3770 | continue; | |
3771 | } | |
3772 | /* Do not introduce new references to external symbols. While we | |
3773 | can handle these just well, it is common for programs to | |
3774 | incorrectly with headers defining methods they are linked | |
3775 | with. */ | |
67348ccc | 3776 | if (DECL_EXTERNAL (likely_target->decl)) |
bbc9396b JH |
3777 | { |
3778 | if (dump_file) | |
ec77d61f | 3779 | fprintf (dump_file, "Target is external\n\n"); |
bbc9396b JH |
3780 | nexternal++; |
3781 | continue; | |
3782 | } | |
570215f9 JM |
3783 | /* Don't use an implicitly-declared destructor (c++/58678). */ |
3784 | struct cgraph_node *non_thunk_target | |
d52f5295 | 3785 | = likely_target->function_symbol (); |
89632536 | 3786 | if (DECL_ARTIFICIAL (non_thunk_target->decl)) |
570215f9 JM |
3787 | { |
3788 | if (dump_file) | |
3789 | fprintf (dump_file, "Target is artificial\n\n"); | |
3790 | nartificial++; | |
3791 | continue; | |
3792 | } | |
d52f5295 ML |
3793 | if (likely_target->get_availability () <= AVAIL_INTERPOSABLE |
3794 | && likely_target->can_be_discarded_p ()) | |
bbc9396b JH |
3795 | { |
3796 | if (dump_file) | |
ec77d61f | 3797 | fprintf (dump_file, "Target is overwritable\n\n"); |
bbc9396b JH |
3798 | noverwritable++; |
3799 | continue; | |
3800 | } | |
2b5f0895 | 3801 | else if (dbg_cnt (devirt)) |
bbc9396b | 3802 | { |
2b5f0895 XDL |
3803 | if (dump_enabled_p ()) |
3804 | { | |
4f5b9c80 | 3805 | dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt, |
464d0118 ML |
3806 | "speculatively devirtualizing call " |
3807 | "in %s to %s\n", | |
3808 | n->dump_name (), | |
3809 | likely_target->dump_name ()); | |
2b5f0895 | 3810 | } |
d52f5295 | 3811 | if (!likely_target->can_be_discarded_p ()) |
5b79657a JH |
3812 | { |
3813 | cgraph_node *alias; | |
d52f5295 | 3814 | alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ()); |
5b79657a JH |
3815 | if (alias) |
3816 | likely_target = alias; | |
3817 | } | |
bbc9396b JH |
3818 | nconverted++; |
3819 | update = true; | |
3dafb85c | 3820 | e->make_speculative |
1bad9c18 | 3821 | (likely_target, e->count.apply_scale (8, 10)); |
bbc9396b JH |
3822 | } |
3823 | } | |
3824 | if (update) | |
0bceb671 | 3825 | ipa_update_overall_fn_summary (n); |
bbc9396b | 3826 | } |
91bc34a9 JH |
3827 | if (warn_suggest_final_methods || warn_suggest_final_types) |
3828 | { | |
3829 | if (warn_suggest_final_types) | |
3830 | { | |
3831 | final_warning_records->type_warnings.qsort (type_warning_cmp); | |
3832 | for (unsigned int i = 0; | |
3833 | i < final_warning_records->type_warnings.length (); i++) | |
3834 | if (final_warning_records->type_warnings[i].count) | |
3835 | { | |
9716cc3e | 3836 | tree type = final_warning_records->type_warnings[i].type; |
26e82579 | 3837 | int count = final_warning_records->type_warnings[i].count; |
3995f3a2 | 3838 | profile_count dyn_count |
26e82579 JH |
3839 | = final_warning_records->type_warnings[i].dyn_count; |
3840 | ||
3995f3a2 | 3841 | if (!(dyn_count > 0)) |
26e82579 JH |
3842 | warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)), |
3843 | OPT_Wsuggest_final_types, count, | |
3844 | "Declaring type %qD final " | |
3845 | "would enable devirtualization of %i call", | |
3846 | "Declaring type %qD final " | |
3847 | "would enable devirtualization of %i calls", | |
3848 | type, | |
3849 | count); | |
3850 | else | |
3851 | warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)), | |
3852 | OPT_Wsuggest_final_types, count, | |
3853 | "Declaring type %qD final " | |
3854 | "would enable devirtualization of %i call " | |
3855 | "executed %lli times", | |
3856 | "Declaring type %qD final " | |
3857 | "would enable devirtualization of %i calls " | |
3858 | "executed %lli times", | |
3859 | type, | |
3860 | count, | |
3995f3a2 | 3861 | (long long) dyn_count.to_gcov_type ()); |
91bc34a9 JH |
3862 | } |
3863 | } | |
3864 | ||
3865 | if (warn_suggest_final_methods) | |
3866 | { | |
ed37a6cf | 3867 | auto_vec<const decl_warn_count*> decl_warnings_vec; |
91bc34a9 JH |
3868 | |
3869 | final_warning_records->decl_warnings.traverse | |
3870 | <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec); | |
3871 | decl_warnings_vec.qsort (decl_warning_cmp); | |
3872 | for (unsigned int i = 0; i < decl_warnings_vec.length (); i++) | |
3873 | { | |
3874 | tree decl = decl_warnings_vec[i]->decl; | |
3875 | int count = decl_warnings_vec[i]->count; | |
3995f3a2 JH |
3876 | profile_count dyn_count |
3877 | = decl_warnings_vec[i]->dyn_count; | |
26e82579 | 3878 | |
3995f3a2 | 3879 | if (!(dyn_count > 0)) |
26e82579 JH |
3880 | if (DECL_CXX_DESTRUCTOR_P (decl)) |
3881 | warning_n (DECL_SOURCE_LOCATION (decl), | |
3882 | OPT_Wsuggest_final_methods, count, | |
3883 | "Declaring virtual destructor of %qD final " | |
3884 | "would enable devirtualization of %i call", | |
3885 | "Declaring virtual destructor of %qD final " | |
3886 | "would enable devirtualization of %i calls", | |
3887 | DECL_CONTEXT (decl), count); | |
3888 | else | |
3889 | warning_n (DECL_SOURCE_LOCATION (decl), | |
3890 | OPT_Wsuggest_final_methods, count, | |
3891 | "Declaring method %qD final " | |
3892 | "would enable devirtualization of %i call", | |
3893 | "Declaring method %qD final " | |
3894 | "would enable devirtualization of %i calls", | |
3895 | decl, count); | |
3896 | else if (DECL_CXX_DESTRUCTOR_P (decl)) | |
3897 | warning_n (DECL_SOURCE_LOCATION (decl), | |
3898 | OPT_Wsuggest_final_methods, count, | |
3899 | "Declaring virtual destructor of %qD final " | |
3900 | "would enable devirtualization of %i call " | |
3901 | "executed %lli times", | |
3902 | "Declaring virtual destructor of %qD final " | |
3903 | "would enable devirtualization of %i calls " | |
3904 | "executed %lli times", | |
3995f3a2 JH |
3905 | DECL_CONTEXT (decl), count, |
3906 | (long long)dyn_count.to_gcov_type ()); | |
26e82579 JH |
3907 | else |
3908 | warning_n (DECL_SOURCE_LOCATION (decl), | |
3909 | OPT_Wsuggest_final_methods, count, | |
3910 | "Declaring method %qD final " | |
3911 | "would enable devirtualization of %i call " | |
3912 | "executed %lli times", | |
3913 | "Declaring method %qD final " | |
3914 | "would enable devirtualization of %i calls " | |
3915 | "executed %lli times", | |
3995f3a2 JH |
3916 | decl, count, |
3917 | (long long)dyn_count.to_gcov_type ()); | |
91bc34a9 JH |
3918 | } |
3919 | } | |
ed37a6cf | 3920 | |
91bc34a9 JH |
3921 | delete (final_warning_records); |
3922 | final_warning_records = 0; | |
3923 | } | |
bbc9396b JH |
3924 | |
3925 | if (dump_file) | |
3926 | fprintf (dump_file, | |
3927 | "%i polymorphic calls, %i devirtualized," | |
3928 | " %i speculatively devirtualized, %i cold\n" | |
3929 | "%i have multiple targets, %i overwritable," | |
3930 | " %i already speculated (%i agree, %i disagree)," | |
68c9467f | 3931 | " %i external, %i not defined, %i artificial, %i infos dropped\n", |
bbc9396b JH |
3932 | npolymorphic, ndevirtualized, nconverted, ncold, |
3933 | nmultiple, noverwritable, nspeculated, nok, nwrong, | |
68c9467f JH |
3934 | nexternal, nnotdefined, nartificial, ndropped); |
3935 | return ndevirtualized || ndropped ? TODO_remove_functions : 0; | |
bbc9396b JH |
3936 | } |
3937 | ||
bbc9396b JH |
3938 | namespace { |
3939 | ||
3940 | const pass_data pass_data_ipa_devirt = | |
3941 | { | |
3942 | IPA_PASS, /* type */ | |
3943 | "devirt", /* name */ | |
3944 | OPTGROUP_NONE, /* optinfo_flags */ | |
bbc9396b JH |
3945 | TV_IPA_DEVIRT, /* tv_id */ |
3946 | 0, /* properties_required */ | |
3947 | 0, /* properties_provided */ | |
3948 | 0, /* properties_destroyed */ | |
3949 | 0, /* todo_flags_start */ | |
3950 | ( TODO_dump_symtab ), /* todo_flags_finish */ | |
3951 | }; | |
3952 | ||
3953 | class pass_ipa_devirt : public ipa_opt_pass_d | |
3954 | { | |
3955 | public: | |
c3284718 RS |
3956 | pass_ipa_devirt (gcc::context *ctxt) |
3957 | : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt, | |
3958 | NULL, /* generate_summary */ | |
3959 | NULL, /* write_summary */ | |
3960 | NULL, /* read_summary */ | |
3961 | NULL, /* write_optimization_summary */ | |
3962 | NULL, /* read_optimization_summary */ | |
3963 | NULL, /* stmt_fixup */ | |
3964 | 0, /* function_transform_todo_flags_start */ | |
3965 | NULL, /* function_transform */ | |
3966 | NULL) /* variable_transform */ | |
bbc9396b JH |
3967 | {} |
3968 | ||
3969 | /* opt_pass methods: */ | |
1a3d085c TS |
3970 | virtual bool gate (function *) |
3971 | { | |
2bf86c84 JH |
3972 | /* In LTO, always run the IPA passes and decide on function basis if the |
3973 | pass is enabled. */ | |
3974 | if (in_lto_p) | |
3975 | return true; | |
1a3d085c | 3976 | return (flag_devirtualize |
91bc34a9 JH |
3977 | && (flag_devirtualize_speculatively |
3978 | || (warn_suggest_final_methods | |
3979 | || warn_suggest_final_types)) | |
1a3d085c TS |
3980 | && optimize); |
3981 | } | |
3982 | ||
be55bfe6 | 3983 | virtual unsigned int execute (function *) { return ipa_devirt (); } |
bbc9396b JH |
3984 | |
3985 | }; // class pass_ipa_devirt | |
3986 | ||
3987 | } // anon namespace | |
3988 | ||
3989 | ipa_opt_pass_d * | |
3990 | make_pass_ipa_devirt (gcc::context *ctxt) | |
3991 | { | |
3992 | return new pass_ipa_devirt (ctxt); | |
3993 | } | |
3994 | ||
74fee042 ML |
3995 | /* Print ODR name of a TYPE if available. |
3996 | Use demangler when option DEMANGLE is used. */ | |
3997 | ||
3998 | DEBUG_FUNCTION void | |
3999 | debug_tree_odr_name (tree type, bool demangle) | |
4000 | { | |
4001 | const char *odr = get_odr_name_for_type (type); | |
4002 | if (demangle) | |
4003 | { | |
4004 | const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES; | |
4005 | odr = cplus_demangle (odr, opts); | |
4006 | } | |
4007 | ||
4008 | fprintf (stderr, "%s\n", odr); | |
4009 | } | |
4010 | ||
3fb68f2e JH |
4011 | /* Register ODR enum so we later stream record about its values. */ |
4012 | ||
4013 | void | |
4014 | register_odr_enum (tree t) | |
4015 | { | |
4016 | if (flag_lto) | |
4017 | vec_safe_push (odr_enums, t); | |
4018 | } | |
4019 | ||
4020 | /* Write ODR enums to LTO stream file. */ | |
4021 | ||
4022 | static void | |
4023 | ipa_odr_summary_write (void) | |
4024 | { | |
4025 | if (!odr_enums && !odr_enum_map) | |
4026 | return; | |
4027 | struct output_block *ob = create_output_block (LTO_section_odr_types); | |
4028 | unsigned int i; | |
4029 | tree t; | |
4030 | ||
4031 | if (odr_enums) | |
4032 | { | |
4033 | streamer_write_uhwi (ob, odr_enums->length ()); | |
4034 | ||
4035 | /* For every ODR enum stream out | |
4036 | - its ODR name | |
4037 | - number of values, | |
4038 | - value names and constant their represent | |
4039 | - bitpack of locations so we can do good diagnostics. */ | |
4040 | FOR_EACH_VEC_ELT (*odr_enums, i, t) | |
4041 | { | |
4042 | streamer_write_string (ob, ob->main_stream, | |
4043 | IDENTIFIER_POINTER | |
4044 | (DECL_ASSEMBLER_NAME (TYPE_NAME (t))), | |
4045 | true); | |
4046 | ||
4047 | int n = 0; | |
4048 | for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e)) | |
4049 | n++; | |
4050 | streamer_write_uhwi (ob, n); | |
4051 | for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e)) | |
4052 | { | |
4053 | streamer_write_string (ob, ob->main_stream, | |
4054 | IDENTIFIER_POINTER (TREE_PURPOSE (e)), | |
4055 | true); | |
eca7a60b JH |
4056 | streamer_write_wide_int (ob, |
4057 | wi::to_wide (DECL_INITIAL | |
4058 | (TREE_VALUE (e)))); | |
3fb68f2e JH |
4059 | } |
4060 | ||
4061 | bitpack_d bp = bitpack_create (ob->main_stream); | |
4062 | lto_output_location (ob, &bp, DECL_SOURCE_LOCATION (TYPE_NAME (t))); | |
4063 | for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e)) | |
4064 | lto_output_location (ob, &bp, | |
4065 | DECL_SOURCE_LOCATION (TREE_VALUE (e))); | |
4066 | streamer_write_bitpack (&bp); | |
4067 | } | |
4068 | vec_free (odr_enums); | |
4069 | odr_enums = NULL; | |
4070 | } | |
4071 | /* During LTO incremental linking we already have streamed in types. */ | |
4072 | else if (odr_enum_map) | |
4073 | { | |
4074 | gcc_checking_assert (!odr_enums); | |
4075 | streamer_write_uhwi (ob, odr_enum_map->elements ()); | |
4076 | ||
4077 | hash_map<nofree_string_hash, odr_enum>::iterator iter | |
4078 | = odr_enum_map->begin (); | |
4079 | for (; iter != odr_enum_map->end (); ++iter) | |
4080 | { | |
4081 | odr_enum &this_enum = (*iter).second; | |
4082 | streamer_write_string (ob, ob->main_stream, (*iter).first, true); | |
4083 | ||
4084 | streamer_write_uhwi (ob, this_enum.vals.length ()); | |
4085 | for (unsigned j = 0; j < this_enum.vals.length (); j++) | |
4086 | { | |
4087 | streamer_write_string (ob, ob->main_stream, | |
4088 | this_enum.vals[j].name, true); | |
eca7a60b | 4089 | streamer_write_wide_int (ob, this_enum.vals[j].val); |
3fb68f2e JH |
4090 | } |
4091 | ||
4092 | bitpack_d bp = bitpack_create (ob->main_stream); | |
4093 | lto_output_location (ob, &bp, this_enum.locus); | |
4094 | for (unsigned j = 0; j < this_enum.vals.length (); j++) | |
4095 | lto_output_location (ob, &bp, this_enum.vals[j].locus); | |
4096 | streamer_write_bitpack (&bp); | |
4097 | } | |
4098 | ||
4099 | delete odr_enum_map; | |
4100 | obstack_free (&odr_enum_obstack, NULL); | |
4101 | odr_enum_map = NULL; | |
4102 | } | |
4103 | ||
4104 | produce_asm (ob, NULL); | |
4105 | destroy_output_block (ob); | |
4106 | } | |
4107 | ||
4108 | /* Write ODR enums from LTO stream file and warn on mismatches. */ | |
4109 | ||
4110 | static void | |
4111 | ipa_odr_read_section (struct lto_file_decl_data *file_data, const char *data, | |
4112 | size_t len) | |
4113 | { | |
4114 | const struct lto_function_header *header | |
4115 | = (const struct lto_function_header *) data; | |
4116 | const int cfg_offset = sizeof (struct lto_function_header); | |
4117 | const int main_offset = cfg_offset + header->cfg_size; | |
4118 | const int string_offset = main_offset + header->main_size; | |
4119 | class data_in *data_in; | |
4120 | ||
4121 | lto_input_block ib ((const char *) data + main_offset, header->main_size, | |
4122 | file_data->mode_table); | |
4123 | ||
4124 | data_in | |
4125 | = lto_data_in_create (file_data, (const char *) data + string_offset, | |
4126 | header->string_size, vNULL); | |
4127 | unsigned int n = streamer_read_uhwi (&ib); | |
4128 | ||
4129 | if (!odr_enum_map) | |
4130 | { | |
4131 | gcc_obstack_init (&odr_enum_obstack); | |
4132 | odr_enum_map = new (hash_map <nofree_string_hash, odr_enum>); | |
4133 | } | |
4134 | ||
4135 | for (unsigned i = 0; i < n; i++) | |
4136 | { | |
4137 | const char *rname = streamer_read_string (data_in, &ib); | |
4138 | unsigned int nvals = streamer_read_uhwi (&ib); | |
4139 | char *name; | |
4140 | ||
4141 | obstack_grow (&odr_enum_obstack, rname, strlen (rname) + 1); | |
4142 | name = XOBFINISH (&odr_enum_obstack, char *); | |
4143 | ||
4144 | bool existed_p; | |
4145 | class odr_enum &this_enum | |
4146 | = odr_enum_map->get_or_insert (xstrdup (name), &existed_p); | |
4147 | ||
eca7a60b | 4148 | /* If this is first time we see the enum, remember its definition. */ |
3fb68f2e JH |
4149 | if (!existed_p) |
4150 | { | |
cb3874dc | 4151 | this_enum.vals.safe_grow_cleared (nvals, true); |
3fb68f2e | 4152 | this_enum.warned = false; |
eca7a60b JH |
4153 | if (dump_file) |
4154 | fprintf (dump_file, "enum %s\n{\n", name); | |
3fb68f2e JH |
4155 | for (unsigned j = 0; j < nvals; j++) |
4156 | { | |
4157 | const char *val_name = streamer_read_string (data_in, &ib); | |
4158 | obstack_grow (&odr_enum_obstack, val_name, strlen (val_name) + 1); | |
4159 | this_enum.vals[j].name = XOBFINISH (&odr_enum_obstack, char *); | |
eca7a60b JH |
4160 | this_enum.vals[j].val = streamer_read_wide_int (&ib); |
4161 | if (dump_file) | |
4162 | fprintf (dump_file, " %s = " HOST_WIDE_INT_PRINT_DEC ",\n", | |
4163 | val_name, wi::fits_shwi_p (this_enum.vals[j].val) | |
4164 | ? this_enum.vals[j].val.to_shwi () : -1); | |
3fb68f2e JH |
4165 | } |
4166 | bitpack_d bp = streamer_read_bitpack (&ib); | |
4167 | stream_input_location (&this_enum.locus, &bp, data_in); | |
4168 | for (unsigned j = 0; j < nvals; j++) | |
4169 | stream_input_location (&this_enum.vals[j].locus, &bp, data_in); | |
4170 | data_in->location_cache.apply_location_cache (); | |
eca7a60b JH |
4171 | if (dump_file) |
4172 | fprintf (dump_file, "}\n"); | |
3fb68f2e | 4173 | } |
eca7a60b JH |
4174 | /* If we already have definition, compare it with new one and output |
4175 | warnings if they differs. */ | |
3fb68f2e JH |
4176 | else |
4177 | { | |
4178 | int do_warning = -1; | |
4179 | char *warn_name = NULL; | |
eca7a60b | 4180 | wide_int warn_value = wi::zero (1); |
3fb68f2e | 4181 | |
eca7a60b JH |
4182 | if (dump_file) |
4183 | fprintf (dump_file, "Comparing enum %s\n", name); | |
4184 | ||
4185 | /* Look for differences which we will warn about later once locations | |
4186 | are streamed. */ | |
3fb68f2e JH |
4187 | for (unsigned j = 0; j < nvals; j++) |
4188 | { | |
4189 | const char *id = streamer_read_string (data_in, &ib); | |
eca7a60b | 4190 | wide_int val = streamer_read_wide_int (&ib); |
3fb68f2e | 4191 | |
eca7a60b | 4192 | if (do_warning != -1 || j >= this_enum.vals.length ()) |
3fb68f2e JH |
4193 | continue; |
4194 | if (strcmp (id, this_enum.vals[j].name) | |
4195 | || val != this_enum.vals[j].val) | |
4196 | { | |
4197 | warn_name = xstrdup (id); | |
4198 | warn_value = val; | |
4199 | do_warning = j; | |
eca7a60b JH |
4200 | if (dump_file) |
4201 | fprintf (dump_file, " Different on entry %i\n", j); | |
3fb68f2e JH |
4202 | } |
4203 | } | |
3fb68f2e | 4204 | |
eca7a60b JH |
4205 | /* Stream in locations, but do not apply them unless we are going |
4206 | to warn. */ | |
4207 | bitpack_d bp = streamer_read_bitpack (&ib); | |
3fb68f2e | 4208 | location_t locus; |
eca7a60b | 4209 | |
3fb68f2e JH |
4210 | stream_input_location (&locus, &bp, data_in); |
4211 | ||
eca7a60b | 4212 | /* Did we find a difference? */ |
3fb68f2e JH |
4213 | if (do_warning != -1 || nvals != this_enum.vals.length ()) |
4214 | { | |
4215 | data_in->location_cache.apply_location_cache (); | |
4216 | ||
4217 | const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES; | |
4218 | char *dmgname = cplus_demangle (name, opts); | |
4219 | if (this_enum.warned | |
4220 | || !warning_at (this_enum.locus, | |
4221 | OPT_Wodr, "type %qs violates the " | |
4222 | "C++ One Definition Rule", | |
4223 | dmgname)) | |
4224 | do_warning = -1; | |
4225 | else | |
4226 | { | |
4227 | this_enum.warned = true; | |
4228 | if (do_warning == -1) | |
4229 | inform (locus, | |
4230 | "an enum with different number of values is defined" | |
4231 | " in another translation unit"); | |
4232 | else if (warn_name) | |
4233 | inform (locus, | |
4234 | "an enum with different value name" | |
4235 | " is defined in another translation unit"); | |
4236 | else | |
4237 | inform (locus, | |
4238 | "an enum with different values" | |
4239 | " is defined in another translation unit"); | |
4240 | } | |
4241 | } | |
4242 | else | |
4243 | data_in->location_cache.revert_location_cache (); | |
eca7a60b JH |
4244 | |
4245 | /* Finally look up for location of the actual value that diverged. */ | |
3fb68f2e JH |
4246 | for (unsigned j = 0; j < nvals; j++) |
4247 | { | |
4248 | location_t id_locus; | |
4249 | ||
4250 | data_in->location_cache.revert_location_cache (); | |
4251 | stream_input_location (&id_locus, &bp, data_in); | |
eca7a60b | 4252 | |
3fb68f2e JH |
4253 | if ((int) j == do_warning) |
4254 | { | |
4255 | data_in->location_cache.apply_location_cache (); | |
eca7a60b | 4256 | |
3fb68f2e JH |
4257 | if (strcmp (warn_name, this_enum.vals[j].name)) |
4258 | inform (this_enum.vals[j].locus, | |
4259 | "name %qs differs from name %qs defined" | |
4260 | " in another translation unit", | |
4261 | this_enum.vals[j].name, warn_name); | |
eca7a60b JH |
4262 | /* FIXME: In case there is easy way to print wide_ints, |
4263 | perhaps we could do it here instead of overlfow checpl. */ | |
4264 | else if (wi::fits_shwi_p (this_enum.vals[j].val) | |
4265 | && wi::fits_shwi_p (warn_value)) | |
3fb68f2e JH |
4266 | inform (this_enum.vals[j].locus, |
4267 | "name %qs is defined to " HOST_WIDE_INT_PRINT_DEC | |
4268 | " while another translation unit defines " | |
4269 | "it as " HOST_WIDE_INT_PRINT_DEC, | |
eca7a60b JH |
4270 | warn_name, this_enum.vals[j].val.to_shwi (), |
4271 | warn_value.to_shwi ()); | |
4272 | else | |
4273 | inform (this_enum.vals[j].locus, | |
4274 | "name %qs is defined to different value " | |
4275 | "in another translation unit", | |
4276 | warn_name); | |
4277 | ||
3fb68f2e JH |
4278 | inform (id_locus, |
4279 | "mismatching definition"); | |
4280 | } | |
4281 | else | |
4282 | data_in->location_cache.revert_location_cache (); | |
4283 | } | |
4284 | if (warn_name) | |
4285 | free (warn_name); | |
4286 | obstack_free (&odr_enum_obstack, name); | |
4287 | } | |
4288 | } | |
4289 | lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data, | |
4290 | len); | |
4291 | lto_data_in_delete (data_in); | |
4292 | } | |
4293 | ||
4294 | /* Read all ODR type sections. */ | |
4295 | ||
4296 | static void | |
4297 | ipa_odr_summary_read (void) | |
4298 | { | |
4299 | struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); | |
4300 | struct lto_file_decl_data *file_data; | |
4301 | unsigned int j = 0; | |
4302 | ||
4303 | while ((file_data = file_data_vec[j++])) | |
4304 | { | |
4305 | size_t len; | |
4306 | const char *data | |
4307 | = lto_get_summary_section_data (file_data, LTO_section_odr_types, | |
4308 | &len); | |
4309 | if (data) | |
4310 | ipa_odr_read_section (file_data, data, len); | |
4311 | } | |
4312 | /* Enum info is used only to produce warnings. Only case we will need it | |
4313 | again is streaming for incremental LTO. */ | |
4314 | if (flag_incremental_link != INCREMENTAL_LINK_LTO) | |
4315 | { | |
4316 | delete odr_enum_map; | |
4317 | obstack_free (&odr_enum_obstack, NULL); | |
4318 | odr_enum_map = NULL; | |
4319 | } | |
4320 | } | |
4321 | ||
4322 | namespace { | |
4323 | ||
4324 | const pass_data pass_data_ipa_odr = | |
4325 | { | |
4326 | IPA_PASS, /* type */ | |
4327 | "odr", /* name */ | |
4328 | OPTGROUP_NONE, /* optinfo_flags */ | |
4329 | TV_IPA_ODR, /* tv_id */ | |
4330 | 0, /* properties_required */ | |
4331 | 0, /* properties_provided */ | |
4332 | 0, /* properties_destroyed */ | |
4333 | 0, /* todo_flags_start */ | |
4334 | 0, /* todo_flags_finish */ | |
4335 | }; | |
4336 | ||
4337 | class pass_ipa_odr : public ipa_opt_pass_d | |
4338 | { | |
4339 | public: | |
4340 | pass_ipa_odr (gcc::context *ctxt) | |
4341 | : ipa_opt_pass_d (pass_data_ipa_odr, ctxt, | |
4342 | NULL, /* generate_summary */ | |
4343 | ipa_odr_summary_write, /* write_summary */ | |
4344 | ipa_odr_summary_read, /* read_summary */ | |
4345 | NULL, /* write_optimization_summary */ | |
4346 | NULL, /* read_optimization_summary */ | |
4347 | NULL, /* stmt_fixup */ | |
4348 | 0, /* function_transform_todo_flags_start */ | |
4349 | NULL, /* function_transform */ | |
4350 | NULL) /* variable_transform */ | |
4351 | {} | |
4352 | ||
4353 | /* opt_pass methods: */ | |
4354 | virtual bool gate (function *) | |
4355 | { | |
4356 | return (in_lto_p || flag_lto); | |
4357 | } | |
4358 | ||
4359 | virtual unsigned int execute (function *) | |
4360 | { | |
4361 | return 0; | |
4362 | } | |
4363 | ||
4364 | }; // class pass_ipa_odr | |
4365 | ||
4366 | } // anon namespace | |
4367 | ||
4368 | ipa_opt_pass_d * | |
4369 | make_pass_ipa_odr (gcc::context *ctxt) | |
4370 | { | |
4371 | return new pass_ipa_odr (ctxt); | |
4372 | } | |
4373 | ||
4374 | ||
eefe9a99 | 4375 | #include "gt-ipa-devirt.h" |