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