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