1 /* Basic IPA utilities for type inheritance graph construction and
3 Copyright (C) 2013-2015 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
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
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
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/>. */
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.
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.
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.
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
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.
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).
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
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.
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.
84 build_type_inheritance_graph triggers a construction of the type inheritance
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.
92 The inheritance graph is represented as follows:
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)
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.
102 possible_polymorphic_call_targets returns, given an parameters found in
103 indirect polymorphic edge all possible polymorphic call targets of the call.
105 pass_ipa_devirt performs simple speculative devirtualization.
110 #include "coretypes.h"
115 #include "alloc-pool.h"
116 #include "tree-pass.h"
118 #include "lto-streamer.h"
119 #include "fold-const.h"
120 #include "print-tree.h"
122 #include "ipa-utils.h"
123 #include "gimple-fold.h"
124 #include "symbol-summary.h"
125 #include "ipa-prop.h"
126 #include "ipa-inline.h"
127 #include "demangle.h"
129 #include "gimple-pretty-print.h"
132 /* Hash based set of pairs of types. */
140 struct default_hash_traits
<type_pair
> : typed_noop_remove
<type_pair
>
142 typedef type_pair value_type
;
143 typedef type_pair compare_type
;
147 return TYPE_UID (p
.first
) ^ TYPE_UID (p
.second
);
150 is_empty (type_pair p
)
152 return p
.first
== NULL
;
155 is_deleted (type_pair p ATTRIBUTE_UNUSED
)
160 equal (const type_pair
&a
, const type_pair
&b
)
162 return a
.first
==b
.first
&& a
.second
== b
.second
;
165 mark_empty (type_pair
&e
)
171 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
172 hash_set
<type_pair
> *,
173 location_t
, location_t
);
175 static bool odr_violation_reported
= false;
178 /* Pointer set of all call targets appearing in the cache. */
179 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
181 /* The node of type inheritance graph. For each type unique in
182 One Definition Rule (ODR) sense, we produce one node linking all
183 main variants of types equivalent to it, bases and derived types. */
185 struct GTY(()) odr_type_d
189 /* All bases; built only for main variants of types. */
190 vec
<odr_type
> GTY((skip
)) bases
;
191 /* All derived types with virtual methods seen in unit;
192 built only for main variants of types. */
193 vec
<odr_type
> GTY((skip
)) derived_types
;
195 /* All equivalent types, if more than one. */
196 vec
<tree
, va_gc
> *types
;
197 /* Set of all equivalent types, if NON-NULL. */
198 hash_set
<tree
> * GTY((skip
)) types_set
;
200 /* Unique ID indexing the type in odr_types array. */
202 /* Is it in anonymous namespace? */
203 bool anonymous_namespace
;
204 /* Do we know about all derivations of given type? */
205 bool all_derivations_known
;
206 /* Did we report ODR violation here? */
208 /* Set when virtual table without RTTI previaled table with. */
212 /* Return TRUE if all derived types of T are known and thus
213 we may consider the walk of derived type complete.
215 This is typically true only for final anonymous namespace types and types
216 defined within functions (that may be COMDAT and thus shared across units,
217 but with the same set of derived types). */
220 type_all_derivations_known_p (const_tree t
)
222 if (TYPE_FINAL_P (t
))
226 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
227 if (!TYPE_NAME (t
) || TREE_CODE (TYPE_NAME (t
)) != TYPE_DECL
)
229 if (type_in_anonymous_namespace_p (t
))
231 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
234 /* Return TRUE if type's constructors are all visible. */
237 type_all_ctors_visible_p (tree t
)
240 && symtab
->state
>= CONSTRUCTION
241 /* We can not always use type_all_derivations_known_p.
242 For function local types we must assume case where
243 the function is COMDAT and shared in between units.
245 TODO: These cases are quite easy to get, but we need
246 to keep track of C++ privatizing via -Wno-weak
247 as well as the IPA privatizing. */
248 && type_in_anonymous_namespace_p (t
);
251 /* Return TRUE if type may have instance. */
254 type_possibly_instantiated_p (tree t
)
259 /* TODO: Add abstract types here. */
260 if (!type_all_ctors_visible_p (t
))
263 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
264 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
265 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
266 vnode
= varpool_node::get (vtable
);
267 return vnode
&& vnode
->definition
;
270 /* Hash used to unify ODR types based on their mangled name and for anonymous
273 struct odr_name_hasher
: pointer_hash
<odr_type_d
>
275 typedef union tree_node
*compare_type
;
276 static inline hashval_t
hash (const odr_type_d
*);
277 static inline bool equal (const odr_type_d
*, const tree_node
*);
278 static inline void remove (odr_type_d
*);
281 /* Has used to unify ODR types based on their associated virtual table.
282 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
283 only polymorphic types. Types with mangled names are inserted to both. */
285 struct odr_vtable_hasher
:odr_name_hasher
287 static inline hashval_t
hash (const odr_type_d
*);
288 static inline bool equal (const odr_type_d
*, const tree_node
*);
291 /* Return type that was declared with T's name so that T is an
292 qualified variant of it. */
295 main_odr_variant (const_tree t
)
297 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
298 return TREE_TYPE (TYPE_NAME (t
));
299 /* Unnamed types and non-C++ produced types can be compared by variants. */
301 return TYPE_MAIN_VARIANT (t
);
305 can_be_name_hashed_p (tree t
)
307 return (!in_lto_p
|| odr_type_p (t
));
310 /* Hash type by its ODR name. */
313 hash_odr_name (const_tree t
)
315 gcc_checking_assert (main_odr_variant (t
) == t
);
317 /* If not in LTO, all main variants are unique, so we can do
320 return htab_hash_pointer (t
);
322 /* Anonymous types are unique. */
323 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
324 return htab_hash_pointer (t
);
326 gcc_checking_assert (TYPE_NAME (t
)
327 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
)));
328 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t
)));
331 /* Return the computed hashcode for ODR_TYPE. */
334 odr_name_hasher::hash (const odr_type_d
*odr_type
)
336 return hash_odr_name (odr_type
->type
);
340 can_be_vtable_hashed_p (tree t
)
342 /* vtable hashing can distinguish only main variants. */
343 if (TYPE_MAIN_VARIANT (t
) != t
)
345 /* Anonymous namespace types are always handled by name hash. */
346 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
348 return (TREE_CODE (t
) == RECORD_TYPE
349 && TYPE_BINFO (t
) && BINFO_VTABLE (TYPE_BINFO (t
)));
352 /* Hash type by assembler name of its vtable. */
355 hash_odr_vtable (const_tree t
)
357 tree v
= BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t
)));
358 inchash::hash hstate
;
360 gcc_checking_assert (in_lto_p
);
361 gcc_checking_assert (!type_in_anonymous_namespace_p (t
));
362 gcc_checking_assert (TREE_CODE (t
) == RECORD_TYPE
363 && TYPE_BINFO (t
) && BINFO_VTABLE (TYPE_BINFO (t
)));
364 gcc_checking_assert (main_odr_variant (t
) == t
);
366 if (TREE_CODE (v
) == POINTER_PLUS_EXPR
)
368 add_expr (TREE_OPERAND (v
, 1), hstate
);
369 v
= TREE_OPERAND (TREE_OPERAND (v
, 0), 0);
372 hstate
.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v
)));
373 return hstate
.end ();
376 /* Return the computed hashcode for ODR_TYPE. */
379 odr_vtable_hasher::hash (const odr_type_d
*odr_type
)
381 return hash_odr_vtable (odr_type
->type
);
384 /* For languages with One Definition Rule, work out if
385 types are the same based on their name.
387 This is non-trivial for LTO where minor differences in
388 the type representation may have prevented type merging
389 to merge two copies of otherwise equivalent type.
391 Until we start streaming mangled type names, this function works
392 only for polymorphic types.
394 When STRICT is true, we compare types by their names for purposes of
395 ODR violation warnings. When strict is false, we consider variants
396 equivalent, becuase it is all that matters for devirtualization machinery.
400 types_same_for_odr (const_tree type1
, const_tree type2
, bool strict
)
402 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
404 type1
= main_odr_variant (type1
);
405 type2
= main_odr_variant (type2
);
408 type1
= TYPE_MAIN_VARIANT (type1
);
409 type2
= TYPE_MAIN_VARIANT (type2
);
418 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
419 on the corresponding TYPE_STUB_DECL. */
420 if ((type_with_linkage_p (type1
) && type_in_anonymous_namespace_p (type1
))
421 || (type_with_linkage_p (type2
) && type_in_anonymous_namespace_p (type2
)))
425 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
427 Ideally we should never need types without ODR names here. It can however
430 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
431 Here testing for equivalence is safe, since their MAIN_VARIANTs are
433 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
434 establish precise ODR equivalency, but for correctness we care only
435 about equivalency on complete polymorphic types. For these we can
436 compare assembler names of their virtual tables. */
437 if ((!TYPE_NAME (type1
) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1
)))
438 || (!TYPE_NAME (type2
) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2
))))
440 /* See if types are obviously different (i.e. different codes
441 or polymorphic wrt non-polymorphic). This is not strictly correct
442 for ODR violating programs, but we can't do better without streaming
444 if (TREE_CODE (type1
) != TREE_CODE (type2
))
446 if (TREE_CODE (type1
) == RECORD_TYPE
447 && (TYPE_BINFO (type1
) == NULL_TREE
)
448 != (TYPE_BINFO (type2
) == NULL_TREE
))
450 if (TREE_CODE (type1
) == RECORD_TYPE
&& TYPE_BINFO (type1
)
451 && (BINFO_VTABLE (TYPE_BINFO (type1
)) == NULL_TREE
)
452 != (BINFO_VTABLE (TYPE_BINFO (type2
)) == NULL_TREE
))
455 /* At the moment we have no way to establish ODR equivalence at LTO
456 other than comparing virtual table pointers of polymorphic types.
457 Eventually we should start saving mangled names in TYPE_NAME.
458 Then this condition will become non-trivial. */
460 if (TREE_CODE (type1
) == RECORD_TYPE
461 && TYPE_BINFO (type1
) && TYPE_BINFO (type2
)
462 && BINFO_VTABLE (TYPE_BINFO (type1
))
463 && BINFO_VTABLE (TYPE_BINFO (type2
)))
465 tree v1
= BINFO_VTABLE (TYPE_BINFO (type1
));
466 tree v2
= BINFO_VTABLE (TYPE_BINFO (type2
));
467 gcc_assert (TREE_CODE (v1
) == POINTER_PLUS_EXPR
468 && TREE_CODE (v2
) == POINTER_PLUS_EXPR
);
469 return (operand_equal_p (TREE_OPERAND (v1
, 1),
470 TREE_OPERAND (v2
, 1), 0)
471 && DECL_ASSEMBLER_NAME
472 (TREE_OPERAND (TREE_OPERAND (v1
, 0), 0))
473 == DECL_ASSEMBLER_NAME
474 (TREE_OPERAND (TREE_OPERAND (v2
, 0), 0)));
478 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1
))
479 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2
)));
482 /* Return true if we can decide on ODR equivalency.
484 In non-LTO it is always decide, in LTO however it depends in the type has
487 When STRICT is false, compare main variants. */
490 types_odr_comparable (tree t1
, tree t2
, bool strict
)
493 || (strict
? (main_odr_variant (t1
) == main_odr_variant (t2
)
494 && main_odr_variant (t1
))
495 : TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
496 || (odr_type_p (t1
) && odr_type_p (t2
))
497 || (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
498 && TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
499 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
500 && polymorphic_type_binfo_p (TYPE_BINFO (t2
))));
503 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
504 known, be conservative and return false. */
507 types_must_be_same_for_odr (tree t1
, tree t2
)
509 if (types_odr_comparable (t1
, t2
))
510 return types_same_for_odr (t1
, t2
);
512 return TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
);
515 /* If T is compound type, return type it is based on. */
518 compound_type_base (const_tree t
)
520 if (TREE_CODE (t
) == ARRAY_TYPE
521 || POINTER_TYPE_P (t
)
522 || TREE_CODE (t
) == COMPLEX_TYPE
523 || VECTOR_TYPE_P (t
))
524 return TREE_TYPE (t
);
525 if (TREE_CODE (t
) == METHOD_TYPE
)
526 return TYPE_METHOD_BASETYPE (t
);
527 if (TREE_CODE (t
) == OFFSET_TYPE
)
528 return TYPE_OFFSET_BASETYPE (t
);
532 /* Return true if T is either ODR type or compound type based from it.
533 If the function return true, we know that T is a type originating from C++
534 source even at link-time. */
537 odr_or_derived_type_p (const_tree t
)
543 /* Function type is a tricky one. Basically we can consider it
544 ODR derived if return type or any of the parameters is.
545 We need to check all parameters because LTO streaming merges
546 common types (such as void) and they are not considered ODR then. */
547 if (TREE_CODE (t
) == FUNCTION_TYPE
)
549 if (TYPE_METHOD_BASETYPE (t
))
550 t
= TYPE_METHOD_BASETYPE (t
);
553 if (TREE_TYPE (t
) && odr_or_derived_type_p (TREE_TYPE (t
)))
555 for (t
= TYPE_ARG_TYPES (t
); t
; t
= TREE_CHAIN (t
))
556 if (odr_or_derived_type_p (TREE_VALUE (t
)))
562 t
= compound_type_base (t
);
568 /* Compare types T1 and T2 and return true if they are
572 odr_name_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
576 gcc_checking_assert (main_odr_variant (t2
) == t2
);
577 gcc_checking_assert (main_odr_variant (t1
) == t1
);
582 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
583 on the corresponding TYPE_STUB_DECL. */
584 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
585 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
587 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
)));
588 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
589 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
590 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
593 /* Compare types T1 and T2 and return true if they are
597 odr_vtable_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
601 gcc_checking_assert (main_odr_variant (t2
) == t2
);
602 gcc_checking_assert (main_odr_variant (t1
) == t1
);
603 gcc_checking_assert (in_lto_p
);
604 t1
= TYPE_MAIN_VARIANT (t1
);
605 t2
= TYPE_MAIN_VARIANT (t2
);
608 tree v1
= BINFO_VTABLE (TYPE_BINFO (t1
));
609 tree v2
= BINFO_VTABLE (TYPE_BINFO (t2
));
610 return (operand_equal_p (TREE_OPERAND (v1
, 1),
611 TREE_OPERAND (v2
, 1), 0)
612 && DECL_ASSEMBLER_NAME
613 (TREE_OPERAND (TREE_OPERAND (v1
, 0), 0))
614 == DECL_ASSEMBLER_NAME
615 (TREE_OPERAND (TREE_OPERAND (v2
, 0), 0)));
618 /* Free ODR type V. */
621 odr_name_hasher::remove (odr_type_d
*v
)
624 v
->derived_types
.release ();
630 /* ODR type hash used to look up ODR type based on tree type node. */
632 typedef hash_table
<odr_name_hasher
> odr_hash_type
;
633 static odr_hash_type
*odr_hash
;
634 typedef hash_table
<odr_vtable_hasher
> odr_vtable_hash_type
;
635 static odr_vtable_hash_type
*odr_vtable_hash
;
637 /* ODR types are also stored into ODR_TYPE vector to allow consistent
638 walking. Bases appear before derived types. Vector is garbage collected
639 so we won't end up visiting empty types. */
641 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
642 #define odr_types (*odr_types_ptr)
644 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
646 set_type_binfo (tree type
, tree binfo
)
648 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
649 if (COMPLETE_TYPE_P (type
))
650 TYPE_BINFO (type
) = binfo
;
652 gcc_assert (!TYPE_BINFO (type
));
655 /* Compare T2 and T2 based on name or structure. */
658 odr_subtypes_equivalent_p (tree t1
, tree t2
,
659 hash_set
<type_pair
> *visited
,
660 location_t loc1
, location_t loc2
)
663 /* This can happen in incomplete types that should be handled earlier. */
664 gcc_assert (t1
&& t2
);
666 t1
= main_odr_variant (t1
);
667 t2
= main_odr_variant (t2
);
671 /* Anonymous namespace types must match exactly. */
672 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
673 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
676 /* For ODR types be sure to compare their names.
677 To support -wno-odr-type-merging we allow one type to be non-ODR
678 and other ODR even though it is a violation. */
679 if (types_odr_comparable (t1
, t2
, true))
681 if (!types_same_for_odr (t1
, t2
, true))
683 /* Limit recursion: If subtypes are ODR types and we know
684 that they are same, be happy. */
685 if (!odr_type_p (t1
) || !get_odr_type (t1
, true)->odr_violated
)
689 /* Component types, builtins and possibly violating ODR types
690 have to be compared structurally. */
691 if (TREE_CODE (t1
) != TREE_CODE (t2
))
693 if (AGGREGATE_TYPE_P (t1
)
694 && (TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
697 type_pair pair
={t1
,t2
};
698 if (TYPE_UID (t1
) > TYPE_UID (t2
))
703 if (visited
->add (pair
))
705 return odr_types_equivalent_p (t1
, t2
, false, NULL
, visited
, loc1
, loc2
);
708 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
709 violation warnings. */
712 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
716 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
718 odr_violation_reported
= true;
719 if (DECL_VIRTUAL_P (prevailing
->decl
))
721 varpool_node
*tmp
= prevailing
;
725 if (warning_at (DECL_SOURCE_LOCATION
726 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
728 "virtual table of type %qD violates one definition rule",
729 DECL_CONTEXT (vtable
->decl
)))
730 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
731 "variable of same assembler name as the virtual table is "
732 "defined in another translation unit");
735 if (!prevailing
->definition
|| !vtable
->definition
)
738 /* If we do not stream ODR type info, do not bother to do useful compare. */
739 if (!TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))
740 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))))
743 odr_type class_type
= get_odr_type (DECL_CONTEXT (vtable
->decl
), true);
745 if (class_type
->odr_violated
)
748 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
750 struct ipa_ref
*ref1
, *ref2
;
753 end1
= !prevailing
->iterate_reference (n1
, ref1
);
754 end2
= !vtable
->iterate_reference (n2
, ref2
);
756 /* !DECL_VIRTUAL_P means RTTI entry;
757 We warn when RTTI is lost because non-RTTI previals; we silently
758 accept the other case. */
761 || (DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
762 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
763 && TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
))
764 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
766 if (!class_type
->rtti_broken
767 && warning_at (DECL_SOURCE_LOCATION
768 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
770 "virtual table of type %qD contains RTTI "
772 DECL_CONTEXT (vtable
->decl
)))
774 inform (DECL_SOURCE_LOCATION
775 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
776 "but is prevailed by one without from other translation "
778 inform (DECL_SOURCE_LOCATION
779 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
780 "RTTI will not work on this type");
781 class_type
->rtti_broken
= true;
784 end2
= !vtable
->iterate_reference (n2
, ref2
);
788 || (DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
789 != DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
790 && TREE_CODE (ref2
->referred
->decl
) == FUNCTION_DECL
))
791 && TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
)
794 end1
= !prevailing
->iterate_reference (n1
, ref1
);
800 /* Extra paranoia; compare the sizes. We do not have information
801 about virtual inheritance offsets, so just be sure that these
803 Do this as very last check so the not very informative error
804 is not output too often. */
805 if (DECL_SIZE (prevailing
->decl
) != DECL_SIZE (vtable
->decl
))
807 class_type
->odr_violated
= true;
808 if (warning_at (DECL_SOURCE_LOCATION
809 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
811 "virtual table of type %qD violates "
812 "one definition rule ",
813 DECL_CONTEXT (vtable
->decl
)))
815 inform (DECL_SOURCE_LOCATION
816 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
817 "the conflicting type defined in another translation "
818 "unit has virtual table of different size");
826 if (DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
827 == DECL_ASSEMBLER_NAME (ref2
->referred
->decl
))
830 class_type
->odr_violated
= true;
832 /* If the loops above stopped on non-virtual pointer, we have
833 mismatch in RTTI information mangling. */
834 if (TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
835 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
837 if (warning_at (DECL_SOURCE_LOCATION
838 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
840 "virtual table of type %qD violates "
841 "one definition rule ",
842 DECL_CONTEXT (vtable
->decl
)))
844 inform (DECL_SOURCE_LOCATION
845 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
846 "the conflicting type defined in another translation "
847 "unit with different RTTI information");
851 /* At this point both REF1 and REF2 points either to virtual table
852 or virtual method. If one points to virtual table and other to
853 method we can complain the same way as if one table was shorter
854 than other pointing out the extra method. */
855 if (TREE_CODE (ref1
->referred
->decl
)
856 != TREE_CODE (ref2
->referred
->decl
))
858 if (TREE_CODE (ref1
->referred
->decl
) == VAR_DECL
)
860 else if (TREE_CODE (ref2
->referred
->decl
) == VAR_DECL
)
865 class_type
->odr_violated
= true;
867 /* Complain about size mismatch. Either we have too many virutal
868 functions or too many virtual table pointers. */
873 varpool_node
*tmp
= prevailing
;
878 if (warning_at (DECL_SOURCE_LOCATION
879 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
881 "virtual table of type %qD violates "
882 "one definition rule",
883 DECL_CONTEXT (vtable
->decl
)))
885 if (TREE_CODE (ref1
->referring
->decl
) == FUNCTION_DECL
)
887 inform (DECL_SOURCE_LOCATION
888 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
889 "the conflicting type defined in another translation "
891 inform (DECL_SOURCE_LOCATION
892 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
893 "contains additional virtual method %qD",
894 ref1
->referred
->decl
);
898 inform (DECL_SOURCE_LOCATION
899 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
900 "the conflicting type defined in another translation "
901 "unit has virtual table with more entries");
907 /* And in the last case we have either mistmatch in between two virtual
908 methods or two virtual table pointers. */
909 if (warning_at (DECL_SOURCE_LOCATION
910 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), OPT_Wodr
,
911 "virtual table of type %qD violates "
912 "one definition rule ",
913 DECL_CONTEXT (vtable
->decl
)))
915 if (TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
)
917 inform (DECL_SOURCE_LOCATION
918 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
919 "the conflicting type defined in another translation "
921 gcc_assert (TREE_CODE (ref2
->referred
->decl
)
923 inform (DECL_SOURCE_LOCATION (ref1
->referred
->decl
),
924 "virtual method %qD", ref1
->referred
->decl
);
925 inform (DECL_SOURCE_LOCATION (ref2
->referred
->decl
),
926 "ought to match virtual method %qD but does not",
927 ref2
->referred
->decl
);
930 inform (DECL_SOURCE_LOCATION
931 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
932 "the conflicting type defined in another translation "
933 "unit has virtual table with different contents");
939 /* Output ODR violation warning about T1 and T2 with REASON.
940 Display location of ST1 and ST2 if REASON speaks about field or
942 If WARN is false, do nothing. Set WARNED if warning was indeed
946 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
947 bool warn
, bool *warned
, const char *reason
)
949 tree decl2
= TYPE_NAME (t2
);
953 if (!warn
|| !TYPE_NAME(t1
))
956 /* ODR warnings are output druing LTO streaming; we must apply location
957 cache for potential warnings to be output correctly. */
958 if (lto_location_cache::current_cache
)
959 lto_location_cache::current_cache
->apply_location_cache ();
961 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)), OPT_Wodr
,
962 "type %qT violates the C++ One Definition Rule",
967 /* For FIELD_DECL support also case where one of fields is
968 NULL - this is used when the structures have mismatching number of
970 else if (!st1
|| TREE_CODE (st1
) == FIELD_DECL
)
972 inform (DECL_SOURCE_LOCATION (decl2
),
973 "a different type is defined in another translation unit");
979 inform (DECL_SOURCE_LOCATION (st1
),
980 "the first difference of corresponding definitions is field %qD",
985 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
987 inform (DECL_SOURCE_LOCATION (decl2
),
988 "a different type is defined in another translation unit");
989 inform (DECL_SOURCE_LOCATION (st1
),
990 "the first difference of corresponding definitions is method %qD",
996 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
1002 /* Return ture if T1 and T2 are incompatible and we want to recusively
1003 dive into them from warn_type_mismatch to give sensible answer. */
1006 type_mismatch_p (tree t1
, tree t2
)
1008 if (odr_or_derived_type_p (t1
) && odr_or_derived_type_p (t2
)
1009 && !odr_types_equivalent_p (t1
, t2
))
1011 return !types_compatible_p (t1
, t2
);
1015 /* Types T1 and T2 was found to be incompatible in a context they can't
1016 (either used to declare a symbol of same assembler name or unified by
1017 ODR rule). We already output warning about this, but if possible, output
1018 extra information on how the types mismatch.
1020 This is hard to do in general. We basically handle the common cases.
1022 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1023 themselves do no thave one.*/
1026 warn_types_mismatch (tree t1
, tree t2
, location_t loc1
, location_t loc2
)
1028 /* Location of type is known only if it has TYPE_NAME and the name is
1030 location_t loc_t1
= TYPE_NAME (t1
) && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1031 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1
))
1033 location_t loc_t2
= TYPE_NAME (t2
) && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1034 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2
))
1036 bool loc_t2_useful
= false;
1038 /* With LTO it is a common case that the location of both types match.
1039 See if T2 has a location that is different from T1. If so, we will
1040 inform user about the location.
1041 Do not consider the location passed to us in LOC1/LOC2 as those are
1043 if (loc_t2
> BUILTINS_LOCATION
&& loc_t2
!= loc_t1
)
1045 if (loc_t1
<= BUILTINS_LOCATION
)
1046 loc_t2_useful
= true;
1049 expanded_location xloc1
= expand_location (loc_t1
);
1050 expanded_location xloc2
= expand_location (loc_t2
);
1052 if (strcmp (xloc1
.file
, xloc2
.file
)
1053 || xloc1
.line
!= xloc2
.line
1054 || xloc1
.column
!= xloc2
.column
)
1055 loc_t2_useful
= true;
1059 if (loc_t1
<= BUILTINS_LOCATION
)
1061 if (loc_t2
<= BUILTINS_LOCATION
)
1064 location_t loc
= loc_t1
<= BUILTINS_LOCATION
? loc_t2
: loc_t1
;
1066 /* It is a quite common bug to reference anonymous namespace type in
1067 non-anonymous namespace class. */
1068 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
1069 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
1071 if (type_with_linkage_p (t1
) && !type_in_anonymous_namespace_p (t1
))
1074 std::swap (loc_t1
, loc_t2
);
1076 gcc_assert (TYPE_NAME (t1
) && TYPE_NAME (t2
)
1077 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1078 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
);
1079 /* Most of the time, the type names will match, do not be unnecesarily
1081 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1
)))
1082 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2
))))
1084 "type %qT defined in anonymous namespace can not match "
1085 "type %qT across the translation unit boundary",
1089 "type %qT defined in anonymous namespace can not match "
1090 "across the translation unit boundary",
1094 "the incompatible type defined in another translation unit");
1097 /* If types have mangled ODR names and they are different, it is most
1098 informative to output those.
1099 This also covers types defined in different namespaces. */
1100 if (TYPE_NAME (t1
) && TYPE_NAME (t2
)
1101 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1102 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1103 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1
))
1104 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2
))
1105 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
1106 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)))
1108 char *name1
= xstrdup (cplus_demangle
1109 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))),
1110 DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
));
1111 char *name2
= cplus_demangle
1112 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
))),
1113 DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
);
1114 if (name1
&& name2
&& strcmp (name1
, name2
))
1117 "type name %<%s%> should match type name %<%s%>",
1121 "the incompatible type is defined here");
1127 /* A tricky case are compound types. Often they appear the same in source
1128 code and the mismatch is dragged in by type they are build from.
1129 Look for those differences in subtypes and try to be informative. In other
1130 cases just output nothing because the source code is probably different
1131 and in this case we already output a all necessary info. */
1132 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
1134 if (TREE_CODE (t1
) == TREE_CODE (t2
))
1136 if (TREE_CODE (t1
) == ARRAY_TYPE
1137 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1139 tree i1
= TYPE_DOMAIN (t1
);
1140 tree i2
= TYPE_DOMAIN (t2
);
1143 && TYPE_MAX_VALUE (i1
)
1144 && TYPE_MAX_VALUE (i2
)
1145 && !operand_equal_p (TYPE_MAX_VALUE (i1
),
1146 TYPE_MAX_VALUE (i2
), 0))
1149 "array types have different bounds");
1153 if ((POINTER_TYPE_P (t1
) || TREE_CODE (t1
) == ARRAY_TYPE
)
1154 && type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1155 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
, loc_t2
);
1156 else if (TREE_CODE (t1
) == METHOD_TYPE
1157 || TREE_CODE (t1
) == FUNCTION_TYPE
)
1159 tree parms1
= NULL
, parms2
= NULL
;
1162 if (type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1164 inform (loc
, "return value type mismatch");
1165 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
,
1169 if (prototype_p (t1
) && prototype_p (t2
))
1170 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1172 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
),
1175 if (type_mismatch_p (TREE_VALUE (parms1
), TREE_VALUE (parms2
)))
1177 if (count
== 1 && TREE_CODE (t1
) == METHOD_TYPE
)
1179 "implicit this pointer type mismatch");
1182 "type mismatch in parameter %i",
1183 count
- (TREE_CODE (t1
) == METHOD_TYPE
));
1184 warn_types_mismatch (TREE_VALUE (parms1
),
1185 TREE_VALUE (parms2
),
1190 if (parms1
|| parms2
)
1193 "types have different parameter counts");
1201 if (types_odr_comparable (t1
, t2
, true)
1202 && types_same_for_odr (t1
, t2
, true))
1204 "type %qT itself violate the C++ One Definition Rule", t1
);
1205 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1206 else if (TYPE_NAME (t1
) == TYPE_NAME (t2
)
1207 && TREE_CODE (t1
) == TREE_CODE (t2
) && !loc_t2_useful
)
1210 inform (loc_t1
, "type %qT should match type %qT",
1213 inform (loc_t2
, "the incompatible type is defined here");
1216 /* Compare T1 and T2, report ODR violations if WARN is true and set
1217 WARNED to true if anything is reported. Return true if types match.
1218 If true is returned, the types are also compatible in the sense of
1219 gimple_canonical_types_compatible_p.
1220 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1221 about the type if the type itself do not have location. */
1224 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
,
1225 hash_set
<type_pair
> *visited
,
1226 location_t loc1
, location_t loc2
)
1228 /* Check first for the obvious case of pointer identity. */
1231 gcc_assert (!type_with_linkage_p (t1
) || !type_in_anonymous_namespace_p (t1
));
1232 gcc_assert (!type_with_linkage_p (t2
) || !type_in_anonymous_namespace_p (t2
));
1234 /* Can't be the same type if the types don't have the same code. */
1235 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1237 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1238 G_("a different type is defined in another translation unit"));
1242 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1244 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1245 G_("a type with different qualifiers is defined in another "
1246 "translation unit"));
1250 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
1251 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
1253 /* We can not trip this when comparing ODR types, only when trying to
1254 match different ODR derivations from different declarations.
1255 So WARN should be always false. */
1260 if (comp_type_attributes (t1
, t2
) != 1)
1262 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1263 G_("a type with different attributes "
1264 "is defined in another translation unit"));
1268 if (TREE_CODE (t1
) == ENUMERAL_TYPE
1269 && TYPE_VALUES (t1
) && TYPE_VALUES (t2
))
1272 for (v1
= TYPE_VALUES (t1
), v2
= TYPE_VALUES (t2
);
1273 v1
&& v2
; v1
= TREE_CHAIN (v1
), v2
= TREE_CHAIN (v2
))
1275 if (TREE_PURPOSE (v1
) != TREE_PURPOSE (v2
))
1277 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1278 G_("an enum with different value name"
1279 " is defined in another translation unit"));
1282 if (TREE_VALUE (v1
) != TREE_VALUE (v2
)
1283 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1
)),
1284 DECL_INITIAL (TREE_VALUE (v2
)), 0))
1286 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1287 G_("an enum with different values is defined"
1288 " in another translation unit"));
1294 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1295 G_("an enum with mismatching number of values "
1296 "is defined in another translation unit"));
1301 /* Non-aggregate types can be handled cheaply. */
1302 if (INTEGRAL_TYPE_P (t1
)
1303 || SCALAR_FLOAT_TYPE_P (t1
)
1304 || FIXED_POINT_TYPE_P (t1
)
1305 || TREE_CODE (t1
) == VECTOR_TYPE
1306 || TREE_CODE (t1
) == COMPLEX_TYPE
1307 || TREE_CODE (t1
) == OFFSET_TYPE
1308 || POINTER_TYPE_P (t1
))
1310 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
1312 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1313 G_("a type with different precision is defined "
1314 "in another translation unit"));
1317 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
1319 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1320 G_("a type with different signedness is defined "
1321 "in another translation unit"));
1325 if (TREE_CODE (t1
) == INTEGER_TYPE
1326 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
1328 /* char WRT uint_8? */
1329 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1330 G_("a different type is defined in another "
1331 "translation unit"));
1335 /* For canonical type comparisons we do not want to build SCCs
1336 so we cannot compare pointed-to types. But we can, for now,
1337 require the same pointed-to type kind and match what
1338 useless_type_conversion_p would do. */
1339 if (POINTER_TYPE_P (t1
))
1341 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
1342 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
1344 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1345 G_("it is defined as a pointer in different address "
1346 "space in another translation unit"));
1350 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1351 visited
, loc1
, loc2
))
1353 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1354 G_("it is defined as a pointer to different type "
1355 "in another translation unit"));
1357 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
),
1363 if ((TREE_CODE (t1
) == VECTOR_TYPE
|| TREE_CODE (t1
) == COMPLEX_TYPE
)
1364 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1365 visited
, loc1
, loc2
))
1367 /* Probably specific enough. */
1368 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1369 G_("a different type is defined "
1370 "in another translation unit"));
1372 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1376 /* Do type-specific comparisons. */
1377 else switch (TREE_CODE (t1
))
1381 /* Array types are the same if the element types are the same and
1382 the number of elements are the same. */
1383 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1384 visited
, loc1
, loc2
))
1386 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1387 G_("a different type is defined in another "
1388 "translation unit"));
1390 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1392 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
1393 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
1394 == TYPE_NONALIASED_COMPONENT (t2
));
1396 tree i1
= TYPE_DOMAIN (t1
);
1397 tree i2
= TYPE_DOMAIN (t2
);
1399 /* For an incomplete external array, the type domain can be
1400 NULL_TREE. Check this condition also. */
1401 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
1404 tree min1
= TYPE_MIN_VALUE (i1
);
1405 tree min2
= TYPE_MIN_VALUE (i2
);
1406 tree max1
= TYPE_MAX_VALUE (i1
);
1407 tree max2
= TYPE_MAX_VALUE (i2
);
1409 /* In C++, minimums should be always 0. */
1410 gcc_assert (min1
== min2
);
1411 if (!operand_equal_p (max1
, max2
, 0))
1413 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1414 G_("an array of different size is defined "
1415 "in another translation unit"));
1423 /* Function types are the same if the return type and arguments types
1425 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1426 visited
, loc1
, loc2
))
1428 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1429 G_("has different return value "
1430 "in another translation unit"));
1432 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1436 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
)
1437 || !prototype_p (t1
) || !prototype_p (t2
))
1441 tree parms1
, parms2
;
1443 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1445 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
1447 if (!odr_subtypes_equivalent_p
1448 (TREE_VALUE (parms1
), TREE_VALUE (parms2
), visited
,
1451 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1452 G_("has different parameters in another "
1453 "translation unit"));
1455 warn_types_mismatch (TREE_VALUE (parms1
),
1456 TREE_VALUE (parms2
), loc1
, loc2
);
1461 if (parms1
|| parms2
)
1463 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1464 G_("has different parameters "
1465 "in another translation unit"));
1474 case QUAL_UNION_TYPE
:
1478 /* For aggregate types, all the fields must be the same. */
1479 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1481 if (TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
1482 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
1483 != polymorphic_type_binfo_p (TYPE_BINFO (t2
)))
1485 if (polymorphic_type_binfo_p (TYPE_BINFO (t1
)))
1486 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1487 G_("a type defined in another translation unit "
1488 "is not polymorphic"));
1490 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1491 G_("a type defined in another translation unit "
1495 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1497 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1499 /* Skip non-fields. */
1500 while (f1
&& TREE_CODE (f1
) != FIELD_DECL
)
1501 f1
= TREE_CHAIN (f1
);
1502 while (f2
&& TREE_CODE (f2
) != FIELD_DECL
)
1503 f2
= TREE_CHAIN (f2
);
1506 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1508 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1509 G_("a type with different virtual table pointers"
1510 " is defined in another translation unit"));
1513 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
1515 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1516 G_("a type with different bases is defined "
1517 "in another translation unit"));
1520 if (DECL_NAME (f1
) != DECL_NAME (f2
)
1521 && !DECL_ARTIFICIAL (f1
))
1523 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1524 G_("a field with different name is defined "
1525 "in another translation unit"));
1528 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1529 TREE_TYPE (f2
), visited
,
1532 /* Do not warn about artificial fields and just go into
1533 generic field mismatch warning. */
1534 if (DECL_ARTIFICIAL (f1
))
1537 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1538 G_("a field of same name but different type "
1539 "is defined in another translation unit"));
1541 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
), loc1
, loc2
);
1544 if (!gimple_compare_field_offset (f1
, f2
))
1546 /* Do not warn about artificial fields and just go into
1547 generic field mismatch warning. */
1548 if (DECL_ARTIFICIAL (f1
))
1550 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1551 G_("fields has different layout "
1552 "in another translation unit"));
1555 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
1556 == DECL_NONADDRESSABLE_P (f2
));
1559 /* If one aggregate has more fields than the other, they
1560 are not the same. */
1563 if ((f1
&& DECL_VIRTUAL_P (f1
)) || (f2
&& DECL_VIRTUAL_P (f2
)))
1564 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1565 G_("a type with different virtual table pointers"
1566 " is defined in another translation unit"));
1567 else if ((f1
&& DECL_ARTIFICIAL (f1
))
1568 || (f2
&& DECL_ARTIFICIAL (f2
)))
1569 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1570 G_("a type with different bases is defined "
1571 "in another translation unit"));
1573 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1574 G_("a type with different number of fields "
1575 "is defined in another translation unit"));
1579 if ((TYPE_MAIN_VARIANT (t1
) == t1
|| TYPE_MAIN_VARIANT (t2
) == t2
)
1580 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1
))
1581 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2
))
1582 && odr_type_p (TYPE_MAIN_VARIANT (t1
))
1583 && odr_type_p (TYPE_MAIN_VARIANT (t2
))
1584 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1
))
1585 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2
))))
1587 /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
1588 if it is non-NULL so this loop will never realy execute. */
1589 if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1
)) != error_mark_node
1590 && TYPE_METHODS (TYPE_MAIN_VARIANT (t2
)) != error_mark_node
)
1591 for (f1
= TYPE_METHODS (TYPE_MAIN_VARIANT (t1
)),
1592 f2
= TYPE_METHODS (TYPE_MAIN_VARIANT (t2
));
1593 f1
&& f2
; f1
= DECL_CHAIN (f1
), f2
= DECL_CHAIN (f2
))
1595 if (DECL_ASSEMBLER_NAME (f1
) != DECL_ASSEMBLER_NAME (f2
))
1597 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1598 G_("a different method of same type "
1599 "is defined in another "
1600 "translation unit"));
1603 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1605 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1606 G_("s definition that differs by virtual "
1607 "keyword in another translation unit"));
1610 if (DECL_VINDEX (f1
) != DECL_VINDEX (f2
))
1612 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1613 G_("virtual table layout differs "
1614 "in another translation unit"));
1617 if (odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1618 TREE_TYPE (f2
), visited
,
1621 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1622 G_("method with incompatible type is "
1623 "defined in another translation unit"));
1627 if ((f1
== NULL
) != (f2
== NULL
))
1629 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1630 G_("a type with different number of methods "
1631 "is defined in another translation unit"));
1647 /* Those are better to come last as they are utterly uninformative. */
1648 if (TYPE_SIZE (t1
) && TYPE_SIZE (t2
)
1649 && !operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0))
1651 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1652 G_("a type with different size "
1653 "is defined in another translation unit"));
1656 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
)
1657 && TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
))
1659 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1660 G_("a type with different alignment "
1661 "is defined in another translation unit"));
1664 gcc_assert (!TYPE_SIZE_UNIT (t1
) || !TYPE_SIZE_UNIT (t2
)
1665 || operand_equal_p (TYPE_SIZE_UNIT (t1
),
1666 TYPE_SIZE_UNIT (t2
), 0));
1670 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1673 odr_types_equivalent_p (tree type1
, tree type2
)
1675 gcc_checking_assert (odr_or_derived_type_p (type1
)
1676 && odr_or_derived_type_p (type2
));
1678 hash_set
<type_pair
> visited
;
1679 return odr_types_equivalent_p (type1
, type2
, false, NULL
,
1680 &visited
, UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1683 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1684 from VAL->type. This may happen in LTO where tree merging did not merge
1685 all variants of the same type or due to ODR violation.
1687 Analyze and report ODR violations and add type to duplicate list.
1688 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1689 this is first time we see definition of a class return true so the
1690 base types are analyzed. */
1693 add_type_duplicate (odr_type val
, tree type
)
1695 bool build_bases
= false;
1696 bool prevail
= false;
1697 bool odr_must_violate
= false;
1699 if (!val
->types_set
)
1700 val
->types_set
= new hash_set
<tree
>;
1702 /* Chose polymorphic type as leader (this happens only in case of ODR
1704 if ((TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1705 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
1706 && (TREE_CODE (val
->type
) != RECORD_TYPE
|| !TYPE_BINFO (val
->type
)
1707 || !polymorphic_type_binfo_p (TYPE_BINFO (val
->type
))))
1712 /* Always prefer complete type to be the leader. */
1713 else if (!COMPLETE_TYPE_P (val
->type
) && COMPLETE_TYPE_P (type
))
1716 build_bases
= TYPE_BINFO (type
);
1718 else if (COMPLETE_TYPE_P (val
->type
) && !COMPLETE_TYPE_P (type
))
1720 else if (TREE_CODE (val
->type
) == ENUMERAL_TYPE
1721 && TREE_CODE (type
) == ENUMERAL_TYPE
1722 && !TYPE_VALUES (val
->type
) && TYPE_VALUES (type
))
1724 else if (TREE_CODE (val
->type
) == RECORD_TYPE
1725 && TREE_CODE (type
) == RECORD_TYPE
1726 && TYPE_BINFO (type
) && !TYPE_BINFO (val
->type
))
1728 gcc_assert (!val
->bases
.length ());
1734 std::swap (val
->type
, type
);
1736 val
->types_set
->add (type
);
1738 /* If we now have a mangled name, be sure to record it to val->type
1739 so ODR hash can work. */
1741 if (can_be_name_hashed_p (type
) && !can_be_name_hashed_p (val
->type
))
1742 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val
->type
),
1743 DECL_ASSEMBLER_NAME (TYPE_NAME (type
)));
1746 bool base_mismatch
= false;
1748 bool warned
= false;
1749 hash_set
<type_pair
> visited
;
1751 gcc_assert (in_lto_p
);
1752 vec_safe_push (val
->types
, type
);
1754 /* If both are class types, compare the bases. */
1755 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1756 && TREE_CODE (val
->type
) == RECORD_TYPE
1757 && TREE_CODE (type
) == RECORD_TYPE
1758 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1760 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1761 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1763 if (!flag_ltrans
&& !warned
&& !val
->odr_violated
)
1766 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1767 "a type with the same name but different "
1768 "number of polymorphic bases is "
1769 "defined in another translation unit");
1772 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1773 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1774 extra_base
= BINFO_BASE_BINFO
1776 BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)));
1778 extra_base
= BINFO_BASE_BINFO
1779 (TYPE_BINFO (val
->type
),
1780 BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1781 tree extra_base_type
= BINFO_TYPE (extra_base
);
1782 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type
)),
1783 "the extra base is defined here");
1786 base_mismatch
= true;
1789 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1791 tree base1
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
1792 tree base2
= BINFO_BASE_BINFO (TYPE_BINFO (val
->type
), i
);
1793 tree type1
= BINFO_TYPE (base1
);
1794 tree type2
= BINFO_TYPE (base2
);
1796 if (types_odr_comparable (type1
, type2
))
1798 if (!types_same_for_odr (type1
, type2
))
1799 base_mismatch
= true;
1802 if (!odr_types_equivalent_p (type1
, type2
))
1803 base_mismatch
= true;
1806 if (!warned
&& !val
->odr_violated
)
1808 warn_odr (type
, val
->type
, NULL
, NULL
,
1810 "a type with the same name but different base "
1811 "type is defined in another translation unit");
1813 warn_types_mismatch (type1
, type2
,
1814 UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1818 if (BINFO_OFFSET (base1
) != BINFO_OFFSET (base2
))
1820 base_mismatch
= true;
1821 if (!warned
&& !val
->odr_violated
)
1822 warn_odr (type
, val
->type
, NULL
, NULL
,
1824 "a type with the same name but different base "
1825 "layout is defined in another translation unit");
1828 /* One of bases is not of complete type. */
1829 if (!TYPE_BINFO (type1
) != !TYPE_BINFO (type2
))
1831 /* If we have a polymorphic type info specified for TYPE1
1832 but not for TYPE2 we possibly missed a base when recording
1834 Be sure this does not happen. */
1835 if (TYPE_BINFO (type1
)
1836 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1838 odr_must_violate
= true;
1841 /* One base is polymorphic and the other not.
1842 This ought to be diagnosed earlier, but do not ICE in the
1844 else if (TYPE_BINFO (type1
)
1845 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1846 != polymorphic_type_binfo_p (TYPE_BINFO (type2
)))
1848 if (!warned
&& !val
->odr_violated
)
1849 warn_odr (type
, val
->type
, NULL
, NULL
,
1851 "a base of the type is polymorphic only in one "
1852 "translation unit");
1853 base_mismatch
= true;
1860 odr_violation_reported
= true;
1861 val
->odr_violated
= true;
1863 if (symtab
->dump_file
)
1865 fprintf (symtab
->dump_file
, "ODR base violation\n");
1867 print_node (symtab
->dump_file
, "", val
->type
, 0);
1868 putc ('\n',symtab
->dump_file
);
1869 print_node (symtab
->dump_file
, "", type
, 0);
1870 putc ('\n',symtab
->dump_file
);
1875 /* Next compare memory layout. */
1876 if (!odr_types_equivalent_p (val
->type
, type
,
1877 !flag_ltrans
&& !val
->odr_violated
&& !warned
,
1879 DECL_SOURCE_LOCATION (TYPE_NAME (val
->type
)),
1880 DECL_SOURCE_LOCATION (TYPE_NAME (type
))))
1883 odr_violation_reported
= true;
1884 val
->odr_violated
= true;
1886 gcc_assert (val
->odr_violated
|| !odr_must_violate
);
1887 /* Sanity check that all bases will be build same way again. */
1889 && COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1890 && TREE_CODE (val
->type
) == RECORD_TYPE
1891 && TREE_CODE (type
) == RECORD_TYPE
1892 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1893 && !val
->odr_violated
1894 && !base_mismatch
&& val
->bases
.length ())
1896 unsigned int num_poly_bases
= 0;
1899 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1900 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1901 (TYPE_BINFO (type
), i
)))
1903 gcc_assert (num_poly_bases
== val
->bases
.length ());
1904 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
));
1906 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1907 (TYPE_BINFO (type
), i
)))
1909 odr_type base
= get_odr_type
1911 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1914 gcc_assert (val
->bases
[j
] == base
);
1920 /* Regularize things a little. During LTO same types may come with
1921 different BINFOs. Either because their virtual table was
1922 not merged by tree merging and only later at decl merging or
1923 because one type comes with external vtable, while other
1924 with internal. We want to merge equivalent binfos to conserve
1925 memory and streaming overhead.
1927 The external vtables are more harmful: they contain references
1928 to external declarations of methods that may be defined in the
1929 merged LTO unit. For this reason we absolutely need to remove
1930 them and replace by internal variants. Not doing so will lead
1931 to incomplete answers from possible_polymorphic_call_targets.
1933 FIXME: disable for now; because ODR types are now build during
1934 streaming in, the variants do not need to be linked to the type,
1935 yet. We need to do the merging in cleanup pass to be implemented
1937 if (!flag_ltrans
&& merge
1939 && TREE_CODE (val
->type
) == RECORD_TYPE
1940 && TREE_CODE (type
) == RECORD_TYPE
1941 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1942 && TYPE_MAIN_VARIANT (type
) == type
1943 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1944 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1945 && BINFO_VTABLE (TYPE_BINFO (type
)))
1947 tree master_binfo
= TYPE_BINFO (val
->type
);
1948 tree v1
= BINFO_VTABLE (master_binfo
);
1949 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1951 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1953 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1954 && operand_equal_p (TREE_OPERAND (v1
, 1),
1955 TREE_OPERAND (v2
, 1), 0));
1956 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1957 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1959 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1960 == DECL_ASSEMBLER_NAME (v2
));
1962 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1966 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1967 for (i
= 0; i
< val
->types
->length (); i
++)
1969 if (TYPE_BINFO ((*val
->types
)[i
])
1971 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1973 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1976 set_type_binfo (type
, master_binfo
);
1981 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1982 possibly new entry. */
1985 get_odr_type (tree type
, bool insert
)
1987 odr_type_d
**slot
= NULL
;
1988 odr_type_d
**vtable_slot
= NULL
;
1989 odr_type val
= NULL
;
1991 bool build_bases
= false;
1992 bool insert_to_odr_array
= false;
1995 type
= main_odr_variant (type
);
1997 gcc_checking_assert (can_be_name_hashed_p (type
)
1998 || can_be_vtable_hashed_p (type
));
2000 /* Lookup entry, first try name hash, fallback to vtable hash. */
2001 if (can_be_name_hashed_p (type
))
2003 hash
= hash_odr_name (type
);
2004 slot
= odr_hash
->find_slot_with_hash (type
, hash
,
2005 insert
? INSERT
: NO_INSERT
);
2007 if ((!slot
|| !*slot
) && in_lto_p
&& can_be_vtable_hashed_p (type
))
2009 hash
= hash_odr_vtable (type
);
2010 vtable_slot
= odr_vtable_hash
->find_slot_with_hash (type
, hash
,
2011 insert
? INSERT
: NO_INSERT
);
2014 if (!slot
&& !vtable_slot
)
2017 /* See if we already have entry for type. */
2018 if ((slot
&& *slot
) || (vtable_slot
&& *vtable_slot
))
2024 && in_lto_p
&& can_be_vtable_hashed_p (type
))
2026 hash
= hash_odr_vtable (type
);
2027 vtable_slot
= odr_vtable_hash
->find_slot_with_hash (type
, hash
,
2029 gcc_assert (!vtable_slot
|| *vtable_slot
== *slot
);
2033 else if (*vtable_slot
)
2036 if (val
->type
!= type
2037 && (!val
->types_set
|| !val
->types_set
->add (type
)))
2039 gcc_assert (insert
);
2040 /* We have type duplicate, but it may introduce vtable name or
2041 mangled name; be sure to keep hashes in sync. */
2042 if (in_lto_p
&& can_be_vtable_hashed_p (type
)
2043 && (!vtable_slot
|| !*vtable_slot
))
2047 hash
= hash_odr_vtable (type
);
2048 vtable_slot
= odr_vtable_hash
->find_slot_with_hash
2049 (type
, hash
, INSERT
);
2050 gcc_checking_assert (!*vtable_slot
|| *vtable_slot
== val
);
2056 build_bases
= add_type_duplicate (val
, type
);
2061 val
= ggc_cleared_alloc
<odr_type_d
> ();
2064 val
->derived_types
= vNULL
;
2065 if (type_with_linkage_p (type
))
2066 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
2068 val
->anonymous_namespace
= 0;
2069 build_bases
= COMPLETE_TYPE_P (val
->type
);
2070 insert_to_odr_array
= true;
2077 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
2078 && type_with_linkage_p (type
)
2079 && type
== TYPE_MAIN_VARIANT (type
))
2081 tree binfo
= TYPE_BINFO (type
);
2084 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) == type
);
2086 val
->all_derivations_known
= type_all_derivations_known_p (type
);
2087 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
2088 /* For now record only polymorphic types. other are
2089 pointless for devirtualization and we can not precisely
2090 determine ODR equivalency of these during LTO. */
2091 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
2093 tree base_type
= BINFO_TYPE (BINFO_BASE_BINFO (binfo
, i
));
2094 odr_type base
= get_odr_type (base_type
, true);
2095 gcc_assert (TYPE_MAIN_VARIANT (base_type
) == base_type
);
2096 base
->derived_types
.safe_push (val
);
2097 val
->bases
.safe_push (base
);
2098 if (base
->id
> base_id
)
2102 /* Ensure that type always appears after bases. */
2103 if (insert_to_odr_array
)
2106 val
->id
= odr_types
.length ();
2107 vec_safe_push (odr_types_ptr
, val
);
2109 else if (base_id
> val
->id
)
2111 odr_types
[val
->id
] = 0;
2112 /* Be sure we did not recorded any derived types; these may need
2114 gcc_assert (val
->derived_types
.length() == 0);
2116 val
->id
= odr_types
.length ();
2117 vec_safe_push (odr_types_ptr
, val
);
2122 /* Add TYPE od ODR type hash. */
2125 register_odr_type (tree type
)
2129 odr_hash
= new odr_hash_type (23);
2131 odr_vtable_hash
= new odr_vtable_hash_type (23);
2133 /* Arrange things to be nicer and insert main variants first.
2134 ??? fundamental prerecorded types do not have mangled names; this
2135 makes it possible that non-ODR type is main_odr_variant of ODR type.
2136 Things may get smoother if LTO FE set mangled name of those types same
2137 way as C++ FE does. */
2138 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type
)))
2139 && odr_type_p (TYPE_MAIN_VARIANT (type
)))
2140 get_odr_type (TYPE_MAIN_VARIANT (type
), true);
2141 if (TYPE_MAIN_VARIANT (type
) != type
&& odr_type_p (main_odr_variant (type
)))
2142 get_odr_type (type
, true);
2145 /* Return true if type is known to have no derivations. */
2148 type_known_to_have_no_derivations_p (tree t
)
2150 return (type_all_derivations_known_p (t
)
2151 && (TYPE_FINAL_P (t
)
2153 && !get_odr_type (t
, true)->derived_types
.length())));
2156 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2157 recursive printing. */
2160 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
2163 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
2164 print_generic_expr (f
, t
->type
, TDF_SLIM
);
2165 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
2166 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
2167 if (TYPE_NAME (t
->type
))
2169 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2170 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2171 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2172 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
->type
)))
2173 fprintf (f
, "%*s mangled name: %s\n", indent
* 2, "",
2175 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
->type
))));
2177 if (t
->bases
.length ())
2179 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
2180 for (i
= 0; i
< t
->bases
.length (); i
++)
2181 fprintf (f
, " %i", t
->bases
[i
]->id
);
2184 if (t
->derived_types
.length ())
2186 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
2187 for (i
= 0; i
< t
->derived_types
.length (); i
++)
2188 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
2193 /* Dump the type inheritance graph. */
2196 dump_type_inheritance_graph (FILE *f
)
2201 fprintf (f
, "\n\nType inheritance graph:\n");
2202 for (i
= 0; i
< odr_types
.length (); i
++)
2204 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
2205 dump_odr_type (f
, odr_types
[i
]);
2207 for (i
= 0; i
< odr_types
.length (); i
++)
2209 if (odr_types
[i
] && odr_types
[i
]->types
&& odr_types
[i
]->types
->length ())
2212 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
2213 print_node (f
, "", odr_types
[i
]->type
, 0);
2214 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2217 fprintf (f
, "duplicate #%i\n", j
);
2218 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
2219 t
= (*odr_types
[i
]->types
)[j
];
2220 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
2222 t
= TYPE_CONTEXT (t
);
2223 print_node (f
, "", t
, 0);
2231 /* Initialize IPA devirt and build inheritance tree graph. */
2234 build_type_inheritance_graph (void)
2236 struct symtab_node
*n
;
2237 FILE *inheritance_dump_file
;
2242 timevar_push (TV_IPA_INHERITANCE
);
2243 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
2244 odr_hash
= new odr_hash_type (23);
2246 odr_vtable_hash
= new odr_vtable_hash_type (23);
2248 /* We reconstruct the graph starting of types of all methods seen in the
2251 if (is_a
<cgraph_node
*> (n
)
2252 && DECL_VIRTUAL_P (n
->decl
)
2253 && n
->real_symbol_p ())
2254 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
2256 /* Look also for virtual tables of types that do not define any methods.
2258 We need it in a case where class B has virtual base of class A
2259 re-defining its virtual method and there is class C with no virtual
2260 methods with B as virtual base.
2262 Here we output B's virtual method in two variant - for non-virtual
2263 and virtual inheritance. B's virtual table has non-virtual version,
2264 while C's has virtual.
2266 For this reason we need to know about C in order to include both
2267 variants of B. More correctly, record_target_from_binfo should
2268 add both variants of the method when walking B, but we have no
2269 link in between them.
2271 We rely on fact that either the method is exported and thus we
2272 assume it is called externally or C is in anonymous namespace and
2273 thus we will see the vtable. */
2275 else if (is_a
<varpool_node
*> (n
)
2276 && DECL_VIRTUAL_P (n
->decl
)
2277 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
2278 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
2279 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
2280 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
2281 if (inheritance_dump_file
)
2283 dump_type_inheritance_graph (inheritance_dump_file
);
2284 dump_end (TDI_inheritance
, inheritance_dump_file
);
2286 timevar_pop (TV_IPA_INHERITANCE
);
2289 /* Return true if N has reference from live virtual table
2290 (and thus can be a destination of polymorphic call).
2291 Be conservatively correct when callgraph is not built or
2292 if the method may be referred externally. */
2295 referenced_from_vtable_p (struct cgraph_node
*node
)
2298 struct ipa_ref
*ref
;
2301 if (node
->externally_visible
2302 || DECL_EXTERNAL (node
->decl
)
2303 || node
->used_from_other_partition
)
2306 /* Keep this test constant time.
2307 It is unlikely this can happen except for the case where speculative
2308 devirtualization introduced many speculative edges to this node.
2309 In this case the target is very likely alive anyway. */
2310 if (node
->ref_list
.referring
.length () > 100)
2313 /* We need references built. */
2314 if (symtab
->state
<= CONSTRUCTION
)
2317 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
2318 if ((ref
->use
== IPA_REF_ALIAS
2319 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
2320 || (ref
->use
== IPA_REF_ADDR
2321 && TREE_CODE (ref
->referring
->decl
) == VAR_DECL
2322 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
2330 /* If TARGET has associated node, record it in the NODES array.
2331 CAN_REFER specify if program can refer to the target directly.
2332 if TARGET is unknown (NULL) or it can not be inserted (for example because
2333 its body was already removed and there is no way to refer to it), clear
2337 maybe_record_node (vec
<cgraph_node
*> &nodes
,
2338 tree target
, hash_set
<tree
> *inserted
,
2342 struct cgraph_node
*target_node
, *alias_target
;
2343 enum availability avail
;
2345 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2346 list of targets; the runtime effect of calling them is undefined.
2347 Only "real" virtual methods should be accounted. */
2348 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
)
2353 /* The only case when method of anonymous namespace becomes unreferable
2354 is when we completely optimized it out. */
2357 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2365 target_node
= cgraph_node::get (target
);
2367 /* Prefer alias target over aliases, so we do not get confused by
2371 alias_target
= target_node
->ultimate_alias_target (&avail
);
2372 if (target_node
!= alias_target
2373 && avail
>= AVAIL_AVAILABLE
2374 && target_node
->get_availability ())
2375 target_node
= alias_target
;
2378 /* Method can only be called by polymorphic call if any
2379 of vtables referring to it are alive.
2381 While this holds for non-anonymous functions, too, there are
2382 cases where we want to keep them in the list; for example
2383 inline functions with -fno-weak are static, but we still
2384 may devirtualize them when instance comes from other unit.
2385 The same holds for LTO.
2387 Currently we ignore these functions in speculative devirtualization.
2388 ??? Maybe it would make sense to be more aggressive for LTO even
2391 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
2393 || !referenced_from_vtable_p (target_node
)))
2395 /* See if TARGET is useful function we can deal with. */
2396 else if (target_node
!= NULL
2397 && (TREE_PUBLIC (target
)
2398 || DECL_EXTERNAL (target
)
2399 || target_node
->definition
)
2400 && target_node
->real_symbol_p ())
2402 gcc_assert (!target_node
->global
.inlined_to
);
2403 gcc_assert (target_node
->real_symbol_p ());
2404 if (!inserted
->add (target
))
2406 cached_polymorphic_call_targets
->add (target_node
);
2407 nodes
.safe_push (target_node
);
2411 && (!type_in_anonymous_namespace_p
2412 (DECL_CONTEXT (target
))
2417 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2418 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2419 method in vtable and insert method to NODES array
2420 or BASES_TO_CONSIDER if this array is non-NULL.
2421 Otherwise recurse to base BINFOs.
2422 This matches what get_binfo_at_offset does, but with offset
2425 TYPE_BINFOS is a stack of BINFOS of types with defined
2426 virtual table seen on way from class type to BINFO.
2428 MATCHED_VTABLES tracks virtual tables we already did lookup
2429 for virtual function in. INSERTED tracks nodes we already
2432 ANONYMOUS is true if BINFO is part of anonymous namespace.
2434 Clear COMPLETEP when we hit unreferable target.
2438 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
2439 vec
<tree
> *bases_to_consider
,
2442 vec
<tree
> &type_binfos
,
2443 HOST_WIDE_INT otr_token
,
2445 HOST_WIDE_INT offset
,
2446 hash_set
<tree
> *inserted
,
2447 hash_set
<tree
> *matched_vtables
,
2451 tree type
= BINFO_TYPE (binfo
);
2456 if (BINFO_VTABLE (binfo
))
2457 type_binfos
.safe_push (binfo
);
2458 if (types_same_for_odr (type
, outer_type
))
2461 tree type_binfo
= NULL
;
2463 /* Look up BINFO with virtual table. For normal types it is always last
2465 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
2466 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
2468 type_binfo
= type_binfos
[i
];
2471 if (BINFO_VTABLE (binfo
))
2473 /* If this is duplicated BINFO for base shared by virtual inheritance,
2474 we may not have its associated vtable. This is not a problem, since
2475 we will walk it on the other path. */
2478 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
2482 gcc_assert (odr_violation_reported
);
2485 /* For types in anonymous namespace first check if the respective vtable
2486 is alive. If not, we know the type can't be called. */
2487 if (!flag_ltrans
&& anonymous
)
2489 tree vtable
= BINFO_VTABLE (inner_binfo
);
2490 varpool_node
*vnode
;
2492 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
2493 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
2494 vnode
= varpool_node::get (vtable
);
2495 if (!vnode
|| !vnode
->definition
)
2498 gcc_assert (inner_binfo
);
2499 if (bases_to_consider
2500 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
2501 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
2504 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2507 if (!bases_to_consider
)
2508 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2509 /* Destructors are never called via construction vtables. */
2510 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
2511 bases_to_consider
->safe_push (target
);
2517 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2518 /* Walking bases that have no virtual method is pointless exercise. */
2519 if (polymorphic_type_binfo_p (base_binfo
))
2520 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
2522 otr_token
, outer_type
, offset
, inserted
,
2523 matched_vtables
, anonymous
, completep
);
2524 if (BINFO_VTABLE (binfo
))
2528 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2529 of TYPE, insert them to NODES, recurse into derived nodes.
2530 INSERTED is used to avoid duplicate insertions of methods into NODES.
2531 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2532 Clear COMPLETEP if unreferable target is found.
2534 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2535 all cases where BASE_SKIPPED is true (because the base is abstract
2539 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
2540 hash_set
<tree
> *inserted
,
2541 hash_set
<tree
> *matched_vtables
,
2544 HOST_WIDE_INT otr_token
,
2546 HOST_WIDE_INT offset
,
2548 vec
<tree
> &bases_to_consider
,
2549 bool consider_construction
)
2551 tree binfo
= TYPE_BINFO (type
->type
);
2553 auto_vec
<tree
, 8> type_binfos
;
2554 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
2556 /* We may need to consider types w/o instances because of possible derived
2557 types using their methods either directly or via construction vtables.
2558 We are safe to skip them when all derivations are known, since we will
2560 This is done by recording them to BASES_TO_CONSIDER array. */
2561 if (possibly_instantiated
|| consider_construction
)
2563 record_target_from_binfo (nodes
,
2564 (!possibly_instantiated
2565 && type_all_derivations_known_p (type
->type
))
2566 ? &bases_to_consider
: NULL
,
2567 binfo
, otr_type
, type_binfos
, otr_token
,
2569 inserted
, matched_vtables
,
2570 type
->anonymous_namespace
, completep
);
2572 for (i
= 0; i
< type
->derived_types
.length (); i
++)
2573 possible_polymorphic_call_targets_1 (nodes
, inserted
,
2576 type
->derived_types
[i
],
2577 otr_token
, outer_type
, offset
, completep
,
2578 bases_to_consider
, consider_construction
);
2581 /* Cache of queries for polymorphic call targets.
2583 Enumerating all call targets may get expensive when there are many
2584 polymorphic calls in the program, so we memoize all the previous
2585 queries and avoid duplicated work. */
2587 struct polymorphic_call_target_d
2589 HOST_WIDE_INT otr_token
;
2590 ipa_polymorphic_call_context context
;
2592 vec
<cgraph_node
*> targets
;
2599 /* Polymorphic call target cache helpers. */
2601 struct polymorphic_call_target_hasher
2602 : pointer_hash
<polymorphic_call_target_d
>
2604 static inline hashval_t
hash (const polymorphic_call_target_d
*);
2605 static inline bool equal (const polymorphic_call_target_d
*,
2606 const polymorphic_call_target_d
*);
2607 static inline void remove (polymorphic_call_target_d
*);
2610 /* Return the computed hashcode for ODR_QUERY. */
2613 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d
*odr_query
)
2615 inchash::hash
hstate (odr_query
->otr_token
);
2617 hstate
.add_wide_int (odr_query
->type
->id
);
2618 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
2619 hstate
.add_wide_int (odr_query
->context
.offset
);
2621 if (odr_query
->context
.speculative_outer_type
)
2623 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
2624 hstate
.add_wide_int (odr_query
->context
.speculative_offset
);
2626 hstate
.add_flag (odr_query
->speculative
);
2627 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
2628 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
2629 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
2630 hstate
.commit_flag ();
2631 return hstate
.end ();
2634 /* Compare cache entries T1 and T2. */
2637 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d
*t1
,
2638 const polymorphic_call_target_d
*t2
)
2640 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
2641 && t1
->speculative
== t2
->speculative
2642 && t1
->context
.offset
== t2
->context
.offset
2643 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
2644 && t1
->context
.outer_type
== t2
->context
.outer_type
2645 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
2646 && t1
->context
.maybe_in_construction
2647 == t2
->context
.maybe_in_construction
2648 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
2649 && (t1
->context
.speculative_maybe_derived_type
2650 == t2
->context
.speculative_maybe_derived_type
));
2653 /* Remove entry in polymorphic call target cache hash. */
2656 polymorphic_call_target_hasher::remove (polymorphic_call_target_d
*v
)
2658 v
->targets
.release ();
2662 /* Polymorphic call target query cache. */
2664 typedef hash_table
<polymorphic_call_target_hasher
>
2665 polymorphic_call_target_hash_type
;
2666 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
2668 /* Destroy polymorphic call target query cache. */
2671 free_polymorphic_call_targets_hash ()
2673 if (cached_polymorphic_call_targets
)
2675 delete polymorphic_call_target_hash
;
2676 polymorphic_call_target_hash
= NULL
;
2677 delete cached_polymorphic_call_targets
;
2678 cached_polymorphic_call_targets
= NULL
;
2682 /* When virtual function is removed, we may need to flush the cache. */
2685 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
2687 if (cached_polymorphic_call_targets
2688 && cached_polymorphic_call_targets
->contains (n
))
2689 free_polymorphic_call_targets_hash ();
2692 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2695 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2698 tree v
= BINFO_VTABLE (binfo
);
2701 unsigned HOST_WIDE_INT this_offset
;
2705 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2708 if (offset
== this_offset
2709 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2713 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2714 if (polymorphic_type_binfo_p (base_binfo
))
2716 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2723 /* T is known constant value of virtual table pointer.
2724 Store virtual table to V and its offset to OFFSET.
2725 Return false if T does not look like virtual table reference. */
2728 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2729 unsigned HOST_WIDE_INT
*offset
)
2731 /* We expect &MEM[(void *)&virtual_table + 16B].
2732 We obtain object's BINFO from the context of the virtual table.
2733 This one contains pointer to virtual table represented via
2734 POINTER_PLUS_EXPR. Verify that this pointer matches what
2735 we propagated through.
2737 In the case of virtual inheritance, the virtual tables may
2738 be nested, i.e. the offset may be different from 16 and we may
2739 need to dive into the type representation. */
2740 if (TREE_CODE (t
) == ADDR_EXPR
2741 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2742 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2743 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2744 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2746 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2747 (TREE_OPERAND (t
, 0), 0), 0)))
2749 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2750 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2754 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2755 We need to handle it when T comes from static variable initializer or
2757 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2759 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2760 t
= TREE_OPERAND (t
, 0);
2765 if (TREE_CODE (t
) != ADDR_EXPR
)
2767 *v
= TREE_OPERAND (t
, 0);
2771 /* T is known constant value of virtual table pointer. Return BINFO of the
2775 vtable_pointer_value_to_binfo (const_tree t
)
2778 unsigned HOST_WIDE_INT offset
;
2780 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2783 /* FIXME: for stores of construction vtables we return NULL,
2784 because we do not have BINFO for those. Eventually we should fix
2785 our representation to allow this case to be handled, too.
2786 In the case we see store of BINFO we however may assume
2787 that standard folding will be able to cope with it. */
2788 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2792 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2793 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2794 and insert them in NODES.
2796 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2799 record_targets_from_bases (tree otr_type
,
2800 HOST_WIDE_INT otr_token
,
2802 HOST_WIDE_INT offset
,
2803 vec
<cgraph_node
*> &nodes
,
2804 hash_set
<tree
> *inserted
,
2805 hash_set
<tree
> *matched_vtables
,
2810 HOST_WIDE_INT pos
, size
;
2814 if (types_same_for_odr (outer_type
, otr_type
))
2817 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
2819 if (TREE_CODE (fld
) != FIELD_DECL
)
2822 pos
= int_bit_position (fld
);
2823 size
= tree_to_shwi (DECL_SIZE (fld
));
2824 if (pos
<= offset
&& (pos
+ size
) > offset
2825 /* Do not get confused by zero sized bases. */
2826 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
2829 /* Within a class type we should always find corresponding fields. */
2830 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
2832 /* Nonbase types should have been stripped by outer_class_type. */
2833 gcc_assert (DECL_ARTIFICIAL (fld
));
2835 outer_type
= TREE_TYPE (fld
);
2838 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
2842 gcc_assert (odr_violation_reported
);
2845 gcc_assert (base_binfo
);
2846 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
2849 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2852 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
2853 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2854 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
2859 /* When virtual table is removed, we may need to flush the cache. */
2862 devirt_variable_node_removal_hook (varpool_node
*n
,
2863 void *d ATTRIBUTE_UNUSED
)
2865 if (cached_polymorphic_call_targets
2866 && DECL_VIRTUAL_P (n
->decl
)
2867 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
2868 free_polymorphic_call_targets_hash ();
2871 /* Record about how many calls would benefit from given type to be final. */
2873 struct odr_type_warn_count
2877 gcov_type dyn_count
;
2880 /* Record about how many calls would benefit from given method to be final. */
2882 struct decl_warn_count
2886 gcov_type dyn_count
;
2889 /* Information about type and decl warnings. */
2891 struct final_warning_record
2893 gcov_type dyn_count
;
2894 auto_vec
<odr_type_warn_count
> type_warnings
;
2895 hash_map
<tree
, decl_warn_count
> decl_warnings
;
2897 struct final_warning_record
*final_warning_records
;
2899 /* Return vector containing possible targets of polymorphic call of type
2900 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2901 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2902 OTR_TYPE and include their virtual method. This is useful for types
2903 possibly in construction or destruction where the virtual table may
2904 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2905 us to walk the inheritance graph for all derivations.
2907 If COMPLETEP is non-NULL, store true if the list is complete.
2908 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2909 in the target cache. If user needs to visit every target list
2910 just once, it can memoize them.
2912 If SPECULATIVE is set, the list will not contain targets that
2913 are not speculatively taken.
2915 Returned vector is placed into cache. It is NOT caller's responsibility
2916 to free it. The vector can be freed on cgraph_remove_node call if
2917 the particular node is a virtual function present in the cache. */
2920 possible_polymorphic_call_targets (tree otr_type
,
2921 HOST_WIDE_INT otr_token
,
2922 ipa_polymorphic_call_context context
,
2927 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
2928 vec
<cgraph_node
*> nodes
= vNULL
;
2929 auto_vec
<tree
, 8> bases_to_consider
;
2930 odr_type type
, outer_type
;
2931 polymorphic_call_target_d key
;
2932 polymorphic_call_target_d
**slot
;
2936 bool can_refer
= false;
2937 bool skipped
= false;
2939 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
2941 /* If ODR is not initialized or the context is invalid, return empty
2943 if (!odr_hash
|| context
.invalid
|| !TYPE_BINFO (otr_type
))
2946 *completep
= context
.invalid
;
2948 *cache_token
= NULL
;
2952 /* Do not bother to compute speculative info when user do not asks for it. */
2953 if (!speculative
|| !context
.speculative_outer_type
)
2954 context
.clear_speculation ();
2956 type
= get_odr_type (otr_type
, true);
2958 /* Recording type variants would waste results cache. */
2959 gcc_assert (!context
.outer_type
2960 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
2962 /* Look up the outer class type we want to walk.
2963 If we fail to do so, the context is invalid. */
2964 if ((context
.outer_type
|| context
.speculative_outer_type
)
2965 && !context
.restrict_to_inner_class (otr_type
))
2970 *cache_token
= NULL
;
2973 gcc_assert (!context
.invalid
);
2975 /* Check that restrict_to_inner_class kept the main variant. */
2976 gcc_assert (!context
.outer_type
2977 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
2979 /* We canonicalize our query, so we do not need extra hashtable entries. */
2981 /* Without outer type, we have no use for offset. Just do the
2982 basic search from inner type. */
2983 if (!context
.outer_type
)
2984 context
.clear_outer_type (otr_type
);
2985 /* We need to update our hierarchy if the type does not exist. */
2986 outer_type
= get_odr_type (context
.outer_type
, true);
2987 /* If the type is complete, there are no derivations. */
2988 if (TYPE_FINAL_P (outer_type
->type
))
2989 context
.maybe_derived_type
= false;
2991 /* Initialize query cache. */
2992 if (!cached_polymorphic_call_targets
)
2994 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
2995 polymorphic_call_target_hash
2996 = new polymorphic_call_target_hash_type (23);
2997 if (!node_removal_hook_holder
)
2999 node_removal_hook_holder
=
3000 symtab
->add_cgraph_removal_hook (&devirt_node_removal_hook
, NULL
);
3001 symtab
->add_varpool_removal_hook (&devirt_variable_node_removal_hook
,
3008 if (context
.outer_type
!= otr_type
)
3010 = get_odr_type (context
.outer_type
, true)->type
;
3011 if (context
.speculative_outer_type
)
3012 context
.speculative_outer_type
3013 = get_odr_type (context
.speculative_outer_type
, true)->type
;
3016 /* Look up cached answer. */
3018 key
.otr_token
= otr_token
;
3019 key
.speculative
= speculative
;
3020 key
.context
= context
;
3021 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
3023 *cache_token
= (void *)*slot
;
3027 *completep
= (*slot
)->complete
;
3028 if ((*slot
)->type_warning
&& final_warning_records
)
3030 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
3031 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3032 += final_warning_records
->dyn_count
;
3034 if (!speculative
&& (*slot
)->decl_warning
&& final_warning_records
)
3036 struct decl_warn_count
*c
=
3037 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
3039 c
->dyn_count
+= final_warning_records
->dyn_count
;
3041 return (*slot
)->targets
;
3046 /* Do actual search. */
3047 timevar_push (TV_IPA_VIRTUAL_CALL
);
3048 *slot
= XCNEW (polymorphic_call_target_d
);
3050 *cache_token
= (void *)*slot
;
3051 (*slot
)->type
= type
;
3052 (*slot
)->otr_token
= otr_token
;
3053 (*slot
)->context
= context
;
3054 (*slot
)->speculative
= speculative
;
3056 hash_set
<tree
> inserted
;
3057 hash_set
<tree
> matched_vtables
;
3059 /* First insert targets we speculatively identified as likely. */
3060 if (context
.speculative_outer_type
)
3062 odr_type speculative_outer_type
;
3063 bool speculation_complete
= true;
3065 /* First insert target from type itself and check if it may have
3067 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
3068 if (TYPE_FINAL_P (speculative_outer_type
->type
))
3069 context
.speculative_maybe_derived_type
= false;
3070 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
3071 context
.speculative_offset
, otr_type
);
3073 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3078 /* In the case we get complete method, we don't need
3079 to walk derivations. */
3080 if (target
&& DECL_FINAL_P (target
))
3081 context
.speculative_maybe_derived_type
= false;
3082 if (type_possibly_instantiated_p (speculative_outer_type
->type
))
3083 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &speculation_complete
);
3085 matched_vtables
.add (BINFO_VTABLE (binfo
));
3088 /* Next walk recursively all derived types. */
3089 if (context
.speculative_maybe_derived_type
)
3090 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
3091 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3094 speculative_outer_type
->derived_types
[i
],
3095 otr_token
, speculative_outer_type
->type
,
3096 context
.speculative_offset
,
3097 &speculation_complete
,
3102 if (!speculative
|| !nodes
.length ())
3104 /* First see virtual method of type itself. */
3105 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
3106 context
.offset
, otr_type
);
3108 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3112 gcc_assert (odr_violation_reported
);
3116 /* Destructors are never called through construction virtual tables,
3117 because the type is always known. */
3118 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
3119 context
.maybe_in_construction
= false;
3123 /* In the case we get complete method, we don't need
3124 to walk derivations. */
3125 if (DECL_FINAL_P (target
))
3126 context
.maybe_derived_type
= false;
3129 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3130 if (type_possibly_instantiated_p (outer_type
->type
))
3131 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3136 matched_vtables
.add (BINFO_VTABLE (binfo
));
3138 /* Next walk recursively all derived types. */
3139 if (context
.maybe_derived_type
)
3141 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
3142 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3145 outer_type
->derived_types
[i
],
3146 otr_token
, outer_type
->type
,
3147 context
.offset
, &complete
,
3149 context
.maybe_in_construction
);
3151 if (!outer_type
->all_derivations_known
)
3153 if (!speculative
&& final_warning_records
)
3156 && nodes
.length () == 1
3157 && warn_suggest_final_types
3158 && !outer_type
->derived_types
.length ())
3160 if (outer_type
->id
>= (int)final_warning_records
->type_warnings
.length ())
3161 final_warning_records
->type_warnings
.safe_grow_cleared
3162 (odr_types
.length ());
3163 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
3164 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
3165 += final_warning_records
->dyn_count
;
3166 final_warning_records
->type_warnings
[outer_type
->id
].type
3168 (*slot
)->type_warning
= outer_type
->id
+ 1;
3171 && warn_suggest_final_methods
3172 && nodes
.length () == 1
3173 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
3177 struct decl_warn_count
&c
=
3178 final_warning_records
->decl_warnings
.get_or_insert
3179 (nodes
[0]->decl
, &existed
);
3184 c
.dyn_count
+= final_warning_records
->dyn_count
;
3189 c
.dyn_count
= final_warning_records
->dyn_count
;
3190 c
.decl
= nodes
[0]->decl
;
3192 (*slot
)->decl_warning
= nodes
[0]->decl
;
3201 /* Destructors are never called through construction virtual tables,
3202 because the type is always known. One of entries may be
3203 cxa_pure_virtual so look to at least two of them. */
3204 if (context
.maybe_in_construction
)
3205 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
3206 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
3207 context
.maybe_in_construction
= false;
3208 if (context
.maybe_in_construction
)
3210 if (type
!= outer_type
3212 || (context
.maybe_derived_type
3213 && !type_all_derivations_known_p (outer_type
->type
))))
3214 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
3215 context
.offset
, nodes
, &inserted
,
3216 &matched_vtables
, &complete
);
3218 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3219 for (i
= 0; i
< bases_to_consider
.length(); i
++)
3220 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
3225 (*slot
)->targets
= nodes
;
3226 (*slot
)->complete
= complete
;
3228 *completep
= complete
;
3230 timevar_pop (TV_IPA_VIRTUAL_CALL
);
3235 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
3236 vec
<const decl_warn_count
*> *vec
)
3238 vec
->safe_push (&value
);
3242 /* Dump target list TARGETS into FILE. */
3245 dump_targets (FILE *f
, vec
<cgraph_node
*> targets
)
3249 for (i
= 0; i
< targets
.length (); i
++)
3253 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
3254 fprintf (f
, " %s/%i", name
? name
: targets
[i
]->name (), targets
[i
]->order
);
3257 if (!targets
[i
]->definition
)
3258 fprintf (f
, " (no definition%s)",
3259 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
3265 /* Dump all possible targets of a polymorphic call. */
3268 dump_possible_polymorphic_call_targets (FILE *f
,
3270 HOST_WIDE_INT otr_token
,
3271 const ipa_polymorphic_call_context
&ctx
)
3273 vec
<cgraph_node
*> targets
;
3275 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
3280 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3282 &final
, NULL
, false);
3283 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
3284 print_generic_expr (f
, type
->type
, TDF_SLIM
);
3285 fprintf (f
, " token %i\n", (int)otr_token
);
3289 fprintf (f
, " %s%s%s%s\n ",
3290 final
? "This is a complete list." :
3291 "This is partial list; extra targets may be defined in other units.",
3292 ctx
.maybe_in_construction
? " (base types included)" : "",
3293 ctx
.maybe_derived_type
? " (derived types included)" : "",
3294 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
3295 len
= targets
.length ();
3296 dump_targets (f
, targets
);
3298 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3300 &final
, NULL
, true);
3301 if (targets
.length () != len
)
3303 fprintf (f
, " Speculative targets:");
3304 dump_targets (f
, targets
);
3306 gcc_assert (targets
.length () <= len
);
3311 /* Return true if N can be possibly target of a polymorphic call of
3312 OTR_TYPE/OTR_TOKEN. */
3315 possible_polymorphic_call_target_p (tree otr_type
,
3316 HOST_WIDE_INT otr_token
,
3317 const ipa_polymorphic_call_context
&ctx
,
3318 struct cgraph_node
*n
)
3320 vec
<cgraph_node
*> targets
;
3322 enum built_in_function fcode
;
3325 if (TREE_CODE (TREE_TYPE (n
->decl
)) == FUNCTION_TYPE
3326 && ((fcode
= DECL_FUNCTION_CODE (n
->decl
))
3327 == BUILT_IN_UNREACHABLE
3328 || fcode
== BUILT_IN_TRAP
))
3333 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
3334 for (i
= 0; i
< targets
.length (); i
++)
3335 if (n
->semantically_equivalent_p (targets
[i
]))
3338 /* At a moment we allow middle end to dig out new external declarations
3339 as a targets of polymorphic calls. */
3340 if (!final
&& !n
->definition
)
3347 /* Return true if N can be possibly target of a polymorphic call of
3348 OBJ_TYPE_REF expression REF in STMT. */
3351 possible_polymorphic_call_target_p (tree ref
,
3353 struct cgraph_node
*n
)
3355 ipa_polymorphic_call_context
context (current_function_decl
, ref
, stmt
);
3356 tree call_fn
= gimple_call_fn (stmt
);
3358 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn
),
3360 (OBJ_TYPE_REF_TOKEN (call_fn
)),
3366 /* After callgraph construction new external nodes may appear.
3367 Add them into the graph. */
3370 update_type_inheritance_graph (void)
3372 struct cgraph_node
*n
;
3376 free_polymorphic_call_targets_hash ();
3377 timevar_push (TV_IPA_INHERITANCE
);
3378 /* We reconstruct the graph starting from types of all methods seen in the
3380 FOR_EACH_FUNCTION (n
)
3381 if (DECL_VIRTUAL_P (n
->decl
)
3383 && n
->real_symbol_p ())
3384 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
3385 timevar_pop (TV_IPA_INHERITANCE
);
3389 /* Return true if N looks like likely target of a polymorphic call.
3390 Rule out cxa_pure_virtual, noreturns, function declared cold and
3391 other obvious cases. */
3394 likely_target_p (struct cgraph_node
*n
)
3397 /* cxa_pure_virtual and similar things are not likely. */
3398 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
3400 flags
= flags_from_decl_or_type (n
->decl
);
3401 if (flags
& ECF_NORETURN
)
3403 if (lookup_attribute ("cold",
3404 DECL_ATTRIBUTES (n
->decl
)))
3406 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
3408 /* If there are no live virtual tables referring the target,
3409 the only way the target can be called is an instance coming from other
3410 compilation unit; speculative devirtualization is built around an
3411 assumption that won't happen. */
3412 if (!referenced_from_vtable_p (n
))
3417 /* Compare type warning records P1 and P2 and choose one with larger count;
3418 helper for qsort. */
3421 type_warning_cmp (const void *p1
, const void *p2
)
3423 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
3424 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
3426 if (t1
->dyn_count
< t2
->dyn_count
)
3428 if (t1
->dyn_count
> t2
->dyn_count
)
3430 return t2
->count
- t1
->count
;
3433 /* Compare decl warning records P1 and P2 and choose one with larger count;
3434 helper for qsort. */
3437 decl_warning_cmp (const void *p1
, const void *p2
)
3439 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
3440 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
3442 if (t1
->dyn_count
< t2
->dyn_count
)
3444 if (t1
->dyn_count
> t2
->dyn_count
)
3446 return t2
->count
- t1
->count
;
3450 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3453 struct cgraph_node
*
3454 try_speculative_devirtualization (tree otr_type
, HOST_WIDE_INT otr_token
,
3455 ipa_polymorphic_call_context ctx
)
3457 vec
<cgraph_node
*>targets
3458 = possible_polymorphic_call_targets
3459 (otr_type
, otr_token
, ctx
, NULL
, NULL
, true);
3461 struct cgraph_node
*likely_target
= NULL
;
3463 for (i
= 0; i
< targets
.length (); i
++)
3464 if (likely_target_p (targets
[i
]))
3468 likely_target
= targets
[i
];
3471 ||!likely_target
->definition
3472 || DECL_EXTERNAL (likely_target
->decl
))
3475 /* Don't use an implicitly-declared destructor (c++/58678). */
3476 struct cgraph_node
*non_thunk_target
3477 = likely_target
->function_symbol ();
3478 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3480 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3481 && likely_target
->can_be_discarded_p ())
3483 return likely_target
;
3486 /* The ipa-devirt pass.
3487 When polymorphic call has only one likely target in the unit,
3488 turn it into a speculative call. */
3493 struct cgraph_node
*n
;
3494 hash_set
<void *> bad_call_targets
;
3495 struct cgraph_edge
*e
;
3497 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
3498 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
3499 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
3506 dump_type_inheritance_graph (dump_file
);
3508 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3509 This is implemented by setting up final_warning_records that are updated
3510 by get_polymorphic_call_targets.
3511 We need to clear cache in this case to trigger recomputation of all
3513 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3515 final_warning_records
= new (final_warning_record
);
3516 final_warning_records
->type_warnings
.safe_grow_cleared (odr_types
.length ());
3517 free_polymorphic_call_targets_hash ();
3520 FOR_EACH_DEFINED_FUNCTION (n
)
3522 bool update
= false;
3523 if (!opt_for_fn (n
->decl
, flag_devirtualize
))
3525 if (dump_file
&& n
->indirect_calls
)
3526 fprintf (dump_file
, "\n\nProcesing function %s/%i\n",
3527 n
->name (), n
->order
);
3528 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
3529 if (e
->indirect_info
->polymorphic
)
3531 struct cgraph_node
*likely_target
= NULL
;
3535 if (final_warning_records
)
3536 final_warning_records
->dyn_count
= e
->count
;
3538 vec
<cgraph_node
*>targets
3539 = possible_polymorphic_call_targets
3540 (e
, &final
, &cache_token
, true);
3543 /* Trigger warnings by calculating non-speculative targets. */
3544 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3545 possible_polymorphic_call_targets (e
);
3548 dump_possible_polymorphic_call_targets
3553 /* See if the call can be devirtualized by means of ipa-prop's
3554 polymorphic call context propagation. If not, we can just
3555 forget about this call being polymorphic and avoid some heavy
3556 lifting in remove_unreachable_nodes that will otherwise try to
3557 keep all possible targets alive until inlining and in the inliner
3560 This may need to be revisited once we add further ways to use
3561 the may edges, but it is a resonable thing to do right now. */
3563 if ((e
->indirect_info
->param_index
== -1
3564 || (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
)
3565 && e
->indirect_info
->vptr_changed
))
3566 && !flag_ltrans_devirtualize
)
3568 e
->indirect_info
->polymorphic
= false;
3571 fprintf (dump_file
, "Dropping polymorphic call info;"
3572 " it can not be used by ipa-prop\n");
3575 if (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
))
3578 if (!e
->maybe_hot_p ())
3581 fprintf (dump_file
, "Call is cold\n\n");
3588 fprintf (dump_file
, "Call is already speculated\n\n");
3591 /* When dumping see if we agree with speculation. */
3595 if (bad_call_targets
.contains (cache_token
))
3598 fprintf (dump_file
, "Target list is known to be useless\n\n");
3602 for (i
= 0; i
< targets
.length (); i
++)
3603 if (likely_target_p (targets
[i
]))
3607 likely_target
= NULL
;
3609 fprintf (dump_file
, "More than one likely target\n\n");
3613 likely_target
= targets
[i
];
3617 bad_call_targets
.add (cache_token
);
3620 /* This is reached only when dumping; check if we agree or disagree
3621 with the speculation. */
3624 struct cgraph_edge
*e2
;
3625 struct ipa_ref
*ref
;
3626 e
->speculative_call_info (e2
, e
, ref
);
3627 if (e2
->callee
->ultimate_alias_target ()
3628 == likely_target
->ultimate_alias_target ())
3630 fprintf (dump_file
, "We agree with speculation\n\n");
3635 fprintf (dump_file
, "We disagree with speculation\n\n");
3640 if (!likely_target
->definition
)
3643 fprintf (dump_file
, "Target is not a definition\n\n");
3647 /* Do not introduce new references to external symbols. While we
3648 can handle these just well, it is common for programs to
3649 incorrectly with headers defining methods they are linked
3651 if (DECL_EXTERNAL (likely_target
->decl
))
3654 fprintf (dump_file
, "Target is external\n\n");
3658 /* Don't use an implicitly-declared destructor (c++/58678). */
3659 struct cgraph_node
*non_thunk_target
3660 = likely_target
->function_symbol ();
3661 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3664 fprintf (dump_file
, "Target is artificial\n\n");
3668 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3669 && likely_target
->can_be_discarded_p ())
3672 fprintf (dump_file
, "Target is overwritable\n\n");
3676 else if (dbg_cnt (devirt
))
3678 if (dump_enabled_p ())
3680 location_t locus
= gimple_location_safe (e
->call_stmt
);
3681 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
3682 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3683 n
->name (), n
->order
,
3684 likely_target
->name (),
3685 likely_target
->order
);
3687 if (!likely_target
->can_be_discarded_p ())
3690 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
3692 likely_target
= alias
;
3697 (likely_target
, e
->count
* 8 / 10, e
->frequency
* 8 / 10);
3701 inline_update_overall_summary (n
);
3703 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3705 if (warn_suggest_final_types
)
3707 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
3708 for (unsigned int i
= 0;
3709 i
< final_warning_records
->type_warnings
.length (); i
++)
3710 if (final_warning_records
->type_warnings
[i
].count
)
3712 tree type
= final_warning_records
->type_warnings
[i
].type
;
3713 int count
= final_warning_records
->type_warnings
[i
].count
;
3715 = final_warning_records
->type_warnings
[i
].dyn_count
;
3718 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3719 OPT_Wsuggest_final_types
, count
,
3720 "Declaring type %qD final "
3721 "would enable devirtualization of %i call",
3722 "Declaring type %qD final "
3723 "would enable devirtualization of %i calls",
3727 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3728 OPT_Wsuggest_final_types
, count
,
3729 "Declaring type %qD final "
3730 "would enable devirtualization of %i call "
3731 "executed %lli times",
3732 "Declaring type %qD final "
3733 "would enable devirtualization of %i calls "
3734 "executed %lli times",
3741 if (warn_suggest_final_methods
)
3743 auto_vec
<const decl_warn_count
*> decl_warnings_vec
;
3745 final_warning_records
->decl_warnings
.traverse
3746 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3747 decl_warnings_vec
.qsort (decl_warning_cmp
);
3748 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3750 tree decl
= decl_warnings_vec
[i
]->decl
;
3751 int count
= decl_warnings_vec
[i
]->count
;
3752 long long dyn_count
= decl_warnings_vec
[i
]->dyn_count
;
3755 if (DECL_CXX_DESTRUCTOR_P (decl
))
3756 warning_n (DECL_SOURCE_LOCATION (decl
),
3757 OPT_Wsuggest_final_methods
, count
,
3758 "Declaring virtual destructor of %qD final "
3759 "would enable devirtualization of %i call",
3760 "Declaring virtual destructor of %qD final "
3761 "would enable devirtualization of %i calls",
3762 DECL_CONTEXT (decl
), count
);
3764 warning_n (DECL_SOURCE_LOCATION (decl
),
3765 OPT_Wsuggest_final_methods
, count
,
3766 "Declaring method %qD final "
3767 "would enable devirtualization of %i call",
3768 "Declaring method %qD final "
3769 "would enable devirtualization of %i calls",
3771 else if (DECL_CXX_DESTRUCTOR_P (decl
))
3772 warning_n (DECL_SOURCE_LOCATION (decl
),
3773 OPT_Wsuggest_final_methods
, count
,
3774 "Declaring virtual destructor of %qD final "
3775 "would enable devirtualization of %i call "
3776 "executed %lli times",
3777 "Declaring virtual destructor of %qD final "
3778 "would enable devirtualization of %i calls "
3779 "executed %lli times",
3780 DECL_CONTEXT (decl
), count
, dyn_count
);
3782 warning_n (DECL_SOURCE_LOCATION (decl
),
3783 OPT_Wsuggest_final_methods
, count
,
3784 "Declaring method %qD final "
3785 "would enable devirtualization of %i call "
3786 "executed %lli times",
3787 "Declaring method %qD final "
3788 "would enable devirtualization of %i calls "
3789 "executed %lli times",
3790 decl
, count
, dyn_count
);
3794 delete (final_warning_records
);
3795 final_warning_records
= 0;
3800 "%i polymorphic calls, %i devirtualized,"
3801 " %i speculatively devirtualized, %i cold\n"
3802 "%i have multiple targets, %i overwritable,"
3803 " %i already speculated (%i agree, %i disagree),"
3804 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3805 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3806 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3807 nexternal
, nnotdefined
, nartificial
, ndropped
);
3808 return ndevirtualized
|| ndropped
? TODO_remove_functions
: 0;
3813 const pass_data pass_data_ipa_devirt
=
3815 IPA_PASS
, /* type */
3816 "devirt", /* name */
3817 OPTGROUP_NONE
, /* optinfo_flags */
3818 TV_IPA_DEVIRT
, /* tv_id */
3819 0, /* properties_required */
3820 0, /* properties_provided */
3821 0, /* properties_destroyed */
3822 0, /* todo_flags_start */
3823 ( TODO_dump_symtab
), /* todo_flags_finish */
3826 class pass_ipa_devirt
: public ipa_opt_pass_d
3829 pass_ipa_devirt (gcc::context
*ctxt
)
3830 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3831 NULL
, /* generate_summary */
3832 NULL
, /* write_summary */
3833 NULL
, /* read_summary */
3834 NULL
, /* write_optimization_summary */
3835 NULL
, /* read_optimization_summary */
3836 NULL
, /* stmt_fixup */
3837 0, /* function_transform_todo_flags_start */
3838 NULL
, /* function_transform */
3839 NULL
) /* variable_transform */
3842 /* opt_pass methods: */
3843 virtual bool gate (function
*)
3845 /* In LTO, always run the IPA passes and decide on function basis if the
3849 return (flag_devirtualize
3850 && (flag_devirtualize_speculatively
3851 || (warn_suggest_final_methods
3852 || warn_suggest_final_types
))
3856 virtual unsigned int execute (function
*) { return ipa_devirt (); }
3858 }; // class pass_ipa_devirt
3863 make_pass_ipa_devirt (gcc::context
*ctxt
)
3865 return new pass_ipa_devirt (ctxt
);
3868 #include "gt-ipa-devirt.h"