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