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