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