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