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