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