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