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