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