1 /* Basic IPA utilities for type inheritance graph construction and
3 Copyright (C) 2013-2020 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 "tree-vrp.h"
126 #include "ipa-prop.h"
127 #include "ipa-fnsummary.h"
128 #include "demangle.h"
130 #include "gimple-pretty-print.h"
132 #include "stringpool.h"
134 #include "data-streamer.h"
135 #include "lto-streamer.h"
136 #include "streamer-hooks.h"
138 /* Hash based set of pairs of types. */
146 struct default_hash_traits
<type_pair
>
147 : typed_noop_remove
<type_pair
>
149 GTY((skip
)) typedef type_pair value_type
;
150 GTY((skip
)) typedef type_pair compare_type
;
154 return TYPE_UID (p
.first
) ^ TYPE_UID (p
.second
);
156 static const bool empty_zero_p
= true;
158 is_empty (type_pair p
)
160 return p
.first
== NULL
;
163 is_deleted (type_pair p ATTRIBUTE_UNUSED
)
168 equal (const type_pair
&a
, const type_pair
&b
)
170 return a
.first
==b
.first
&& a
.second
== b
.second
;
173 mark_empty (type_pair
&e
)
179 /* HACK alert: this is used to communicate with ipa-inline-transform that
180 thunk is being expanded and there is no need to clear the polymorphic
181 call target cache. */
182 bool thunk_expansion
;
184 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
185 hash_set
<type_pair
> *,
186 location_t
, location_t
);
187 static void warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
188 bool warn
, bool *warned
, const char *reason
);
190 static bool odr_violation_reported
= false;
193 /* Pointer set of all call targets appearing in the cache. */
194 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
196 /* The node of type inheritance graph. For each type unique in
197 One Definition Rule (ODR) sense, we produce one node linking all
198 main variants of types equivalent to it, bases and derived types. */
200 struct GTY(()) odr_type_d
204 /* All bases; built only for main variants of types. */
205 vec
<odr_type
> GTY((skip
)) bases
;
206 /* All derived types with virtual methods seen in unit;
207 built only for main variants of types. */
208 vec
<odr_type
> GTY((skip
)) derived_types
;
210 /* All equivalent types, if more than one. */
211 vec
<tree
, va_gc
> *types
;
212 /* Set of all equivalent types, if NON-NULL. */
213 hash_set
<tree
> * GTY((skip
)) types_set
;
215 /* Unique ID indexing the type in odr_types array. */
217 /* Is it in anonymous namespace? */
218 bool anonymous_namespace
;
219 /* Do we know about all derivations of given type? */
220 bool all_derivations_known
;
221 /* Did we report ODR violation here? */
223 /* Set when virtual table without RTTI prevailed table with. */
225 /* Set when the canonical type is determined using the type name. */
229 /* Return TRUE if all derived types of T are known and thus
230 we may consider the walk of derived type complete.
232 This is typically true only for final anonymous namespace types and types
233 defined within functions (that may be COMDAT and thus shared across units,
234 but with the same set of derived types). */
237 type_all_derivations_known_p (const_tree t
)
239 if (TYPE_FINAL_P (t
))
243 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
244 if (!TYPE_NAME (t
) || TREE_CODE (TYPE_NAME (t
)) != TYPE_DECL
)
246 if (type_in_anonymous_namespace_p (t
))
248 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
251 /* Return TRUE if type's constructors are all visible. */
254 type_all_ctors_visible_p (tree t
)
257 && symtab
->state
>= CONSTRUCTION
258 /* We cannot always use type_all_derivations_known_p.
259 For function local types we must assume case where
260 the function is COMDAT and shared in between units.
262 TODO: These cases are quite easy to get, but we need
263 to keep track of C++ privatizing via -Wno-weak
264 as well as the IPA privatizing. */
265 && type_in_anonymous_namespace_p (t
);
268 /* Return TRUE if type may have instance. */
271 type_possibly_instantiated_p (tree t
)
276 /* TODO: Add abstract types here. */
277 if (!type_all_ctors_visible_p (t
))
280 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
281 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
282 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
283 vnode
= varpool_node::get (vtable
);
284 return vnode
&& vnode
->definition
;
287 /* Hash used to unify ODR types based on their mangled name and for anonymous
290 struct odr_name_hasher
: pointer_hash
<odr_type_d
>
292 typedef union tree_node
*compare_type
;
293 static inline hashval_t
hash (const odr_type_d
*);
294 static inline bool equal (const odr_type_d
*, const tree_node
*);
295 static inline void remove (odr_type_d
*);
299 can_be_name_hashed_p (tree t
)
301 return (!in_lto_p
|| odr_type_p (t
));
304 /* Hash type by its ODR name. */
307 hash_odr_name (const_tree t
)
309 gcc_checking_assert (TYPE_MAIN_VARIANT (t
) == t
);
311 /* If not in LTO, all main variants are unique, so we can do
314 return htab_hash_pointer (t
);
316 /* Anonymous types are unique. */
317 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
318 return htab_hash_pointer (t
);
320 gcc_checking_assert (TYPE_NAME (t
)
321 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
)));
322 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t
)));
325 /* Return the computed hashcode for ODR_TYPE. */
328 odr_name_hasher::hash (const odr_type_d
*odr_type
)
330 return hash_odr_name (odr_type
->type
);
333 /* For languages with One Definition Rule, work out if
334 types are the same based on their name.
336 This is non-trivial for LTO where minor differences in
337 the type representation may have prevented type merging
338 to merge two copies of otherwise equivalent type.
340 Until we start streaming mangled type names, this function works
341 only for polymorphic types.
345 types_same_for_odr (const_tree type1
, const_tree type2
)
347 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
349 type1
= TYPE_MAIN_VARIANT (type1
);
350 type2
= TYPE_MAIN_VARIANT (type2
);
358 /* Anonymous namespace types are never duplicated. */
359 if ((type_with_linkage_p (type1
) && type_in_anonymous_namespace_p (type1
))
360 || (type_with_linkage_p (type2
) && type_in_anonymous_namespace_p (type2
)))
363 /* If both type has mangled defined check if they are same.
364 Watch for anonymous types which are all mangled as "<anon">. */
365 if (!type_with_linkage_p (type1
) || !type_with_linkage_p (type2
))
367 if (type_in_anonymous_namespace_p (type1
)
368 || type_in_anonymous_namespace_p (type2
))
370 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1
))
371 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2
)));
374 /* Return true if we can decide on ODR equivalency.
376 In non-LTO it is always decide, in LTO however it depends in the type has
377 ODR info attached. */
380 types_odr_comparable (tree t1
, tree t2
)
383 || TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
)
384 || (odr_type_p (TYPE_MAIN_VARIANT (t1
))
385 && odr_type_p (TYPE_MAIN_VARIANT (t2
))));
388 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
389 known, be conservative and return false. */
392 types_must_be_same_for_odr (tree t1
, tree t2
)
394 if (types_odr_comparable (t1
, t2
))
395 return types_same_for_odr (t1
, t2
);
397 return TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
);
400 /* If T is compound type, return type it is based on. */
403 compound_type_base (const_tree t
)
405 if (TREE_CODE (t
) == ARRAY_TYPE
406 || POINTER_TYPE_P (t
)
407 || TREE_CODE (t
) == COMPLEX_TYPE
408 || VECTOR_TYPE_P (t
))
409 return TREE_TYPE (t
);
410 if (TREE_CODE (t
) == METHOD_TYPE
)
411 return TYPE_METHOD_BASETYPE (t
);
412 if (TREE_CODE (t
) == OFFSET_TYPE
)
413 return TYPE_OFFSET_BASETYPE (t
);
417 /* Return true if T is either ODR type or compound type based from it.
418 If the function return true, we know that T is a type originating from C++
419 source even at link-time. */
422 odr_or_derived_type_p (const_tree t
)
426 if (odr_type_p (TYPE_MAIN_VARIANT (t
)))
428 /* Function type is a tricky one. Basically we can consider it
429 ODR derived if return type or any of the parameters is.
430 We need to check all parameters because LTO streaming merges
431 common types (such as void) and they are not considered ODR then. */
432 if (TREE_CODE (t
) == FUNCTION_TYPE
)
434 if (TYPE_METHOD_BASETYPE (t
))
435 t
= TYPE_METHOD_BASETYPE (t
);
438 if (TREE_TYPE (t
) && odr_or_derived_type_p (TREE_TYPE (t
)))
440 for (t
= TYPE_ARG_TYPES (t
); t
; t
= TREE_CHAIN (t
))
441 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t
))))
447 t
= compound_type_base (t
);
453 /* Compare types T1 and T2 and return true if they are
457 odr_name_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
461 gcc_checking_assert (TYPE_MAIN_VARIANT (t2
) == t2
);
462 gcc_checking_assert (TYPE_MAIN_VARIANT (t1
) == t1
);
467 /* Check for anonymous namespaces. */
468 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
469 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
471 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
)));
472 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
473 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
474 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
477 /* Free ODR type V. */
480 odr_name_hasher::remove (odr_type_d
*v
)
483 v
->derived_types
.release ();
489 /* ODR type hash used to look up ODR type based on tree type node. */
491 typedef hash_table
<odr_name_hasher
> odr_hash_type
;
492 static odr_hash_type
*odr_hash
;
494 /* ODR types are also stored into ODR_TYPE vector to allow consistent
495 walking. Bases appear before derived types. Vector is garbage collected
496 so we won't end up visiting empty types. */
498 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
499 #define odr_types (*odr_types_ptr)
501 /* All enums defined and accessible for the unit. */
502 static GTY(()) vec
<tree
, va_gc
> *odr_enums
;
504 /* Information we hold about value defined by an enum type. */
512 /* Information about enum values. */
516 auto_vec
<odr_enum_val
, 0> vals
;
520 /* A table of all ODR enum definitions. */
521 static hash_map
<nofree_string_hash
, odr_enum
> *odr_enum_map
= NULL
;
522 static struct obstack odr_enum_obstack
;
524 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
526 set_type_binfo (tree type
, tree binfo
)
528 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
529 if (COMPLETE_TYPE_P (type
))
530 TYPE_BINFO (type
) = binfo
;
532 gcc_assert (!TYPE_BINFO (type
));
535 /* Return true if type variants match.
536 This assumes that we already verified that T1 and T2 are variants of the
540 type_variants_equivalent_p (tree t1
, tree t2
)
542 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
545 if (comp_type_attributes (t1
, t2
) != 1)
548 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
)
549 && TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
))
555 /* Compare T1 and T2 based on name or structure. */
558 odr_subtypes_equivalent_p (tree t1
, tree t2
,
559 hash_set
<type_pair
> *visited
,
560 location_t loc1
, location_t loc2
)
563 /* This can happen in incomplete types that should be handled earlier. */
564 gcc_assert (t1
&& t2
);
569 /* Anonymous namespace types must match exactly. */
570 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
571 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
572 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
573 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
576 /* For ODR types be sure to compare their names.
577 To support -Wno-odr-type-merging we allow one type to be non-ODR
578 and other ODR even though it is a violation. */
579 if (types_odr_comparable (t1
, t2
))
582 && odr_type_p (TYPE_MAIN_VARIANT (t1
))
583 && get_odr_type (TYPE_MAIN_VARIANT (t1
), true)->odr_violated
)
585 if (!types_same_for_odr (t1
, t2
))
587 if (!type_variants_equivalent_p (t1
, t2
))
589 /* Limit recursion: If subtypes are ODR types and we know
590 that they are same, be happy. */
591 if (odr_type_p (TYPE_MAIN_VARIANT (t1
)))
595 /* Component types, builtins and possibly violating ODR types
596 have to be compared structurally. */
597 if (TREE_CODE (t1
) != TREE_CODE (t2
))
599 if (AGGREGATE_TYPE_P (t1
)
600 && (TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
603 type_pair pair
={TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
)};
604 if (TYPE_UID (TYPE_MAIN_VARIANT (t1
)) > TYPE_UID (TYPE_MAIN_VARIANT (t2
)))
606 pair
.first
= TYPE_MAIN_VARIANT (t2
);
607 pair
.second
= TYPE_MAIN_VARIANT (t1
);
609 if (visited
->add (pair
))
611 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
),
612 false, NULL
, visited
, loc1
, loc2
))
614 if (!type_variants_equivalent_p (t1
, t2
))
619 /* Return true if DECL1 and DECL2 are identical methods. Consider
620 name equivalent to name.localalias.xyz. */
623 methods_equal_p (tree decl1
, tree decl2
)
625 if (DECL_ASSEMBLER_NAME (decl1
) == DECL_ASSEMBLER_NAME (decl2
))
627 const char sep
= symbol_table::symbol_suffix_separator ();
629 const char *name1
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1
));
630 const char *ptr1
= strchr (name1
, sep
);
631 int len1
= ptr1
? ptr1
- name1
: strlen (name1
);
633 const char *name2
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2
));
634 const char *ptr2
= strchr (name2
, sep
);
635 int len2
= ptr2
? ptr2
- name2
: strlen (name2
);
639 return !strncmp (name1
, name2
, len1
);
642 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
643 violation warnings. */
646 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
650 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
652 odr_violation_reported
= true;
653 if (DECL_VIRTUAL_P (prevailing
->decl
))
655 varpool_node
*tmp
= prevailing
;
659 auto_diagnostic_group d
;
660 if (warning_at (DECL_SOURCE_LOCATION
661 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
663 "virtual table of type %qD violates one definition rule",
664 DECL_CONTEXT (vtable
->decl
)))
665 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
666 "variable of same assembler name as the virtual table is "
667 "defined in another translation unit");
670 if (!prevailing
->definition
|| !vtable
->definition
)
673 /* If we do not stream ODR type info, do not bother to do useful compare. */
674 if (!TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))
675 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))))
678 odr_type class_type
= get_odr_type (DECL_CONTEXT (vtable
->decl
), true);
680 if (class_type
->odr_violated
)
683 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
685 struct ipa_ref
*ref1
, *ref2
;
688 end1
= !prevailing
->iterate_reference (n1
, ref1
);
689 end2
= !vtable
->iterate_reference (n2
, ref2
);
691 /* !DECL_VIRTUAL_P means RTTI entry;
692 We warn when RTTI is lost because non-RTTI prevails; we silently
693 accept the other case. */
696 || (methods_equal_p (ref1
->referred
->decl
,
697 ref2
->referred
->decl
)
698 && TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
))
699 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
701 if (!class_type
->rtti_broken
)
703 auto_diagnostic_group d
;
704 if (warning_at (DECL_SOURCE_LOCATION
705 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
707 "virtual table of type %qD contains RTTI "
709 DECL_CONTEXT (vtable
->decl
)))
711 inform (DECL_SOURCE_LOCATION
712 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
713 "but is prevailed by one without from other"
714 " translation unit");
715 inform (DECL_SOURCE_LOCATION
716 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
717 "RTTI will not work on this type");
718 class_type
->rtti_broken
= true;
722 end2
= !vtable
->iterate_reference (n2
, ref2
);
726 || (methods_equal_p (ref2
->referred
->decl
, ref1
->referred
->decl
)
727 && TREE_CODE (ref2
->referred
->decl
) == FUNCTION_DECL
))
728 && TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
)
731 end1
= !prevailing
->iterate_reference (n1
, ref1
);
737 /* Extra paranoia; compare the sizes. We do not have information
738 about virtual inheritance offsets, so just be sure that these
740 Do this as very last check so the not very informative error
741 is not output too often. */
742 if (DECL_SIZE (prevailing
->decl
) != DECL_SIZE (vtable
->decl
))
744 class_type
->odr_violated
= true;
745 auto_diagnostic_group d
;
746 tree ctx
= TYPE_NAME (DECL_CONTEXT (vtable
->decl
));
747 if (warning_at (DECL_SOURCE_LOCATION (ctx
), OPT_Wodr
,
748 "virtual table of type %qD violates "
749 "one definition rule",
750 DECL_CONTEXT (vtable
->decl
)))
752 ctx
= TYPE_NAME (DECL_CONTEXT (prevailing
->decl
));
753 inform (DECL_SOURCE_LOCATION (ctx
),
754 "the conflicting type defined in another translation"
755 " unit has virtual table of different size");
763 if (methods_equal_p (ref1
->referred
->decl
, ref2
->referred
->decl
))
766 class_type
->odr_violated
= true;
768 /* If the loops above stopped on non-virtual pointer, we have
769 mismatch in RTTI information mangling. */
770 if (TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
771 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
773 auto_diagnostic_group d
;
774 if (warning_at (DECL_SOURCE_LOCATION
775 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
777 "virtual table of type %qD violates "
778 "one definition rule",
779 DECL_CONTEXT (vtable
->decl
)))
781 inform (DECL_SOURCE_LOCATION
782 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
783 "the conflicting type defined in another translation "
784 "unit with different RTTI information");
788 /* At this point both REF1 and REF2 points either to virtual table
789 or virtual method. If one points to virtual table and other to
790 method we can complain the same way as if one table was shorter
791 than other pointing out the extra method. */
792 if (TREE_CODE (ref1
->referred
->decl
)
793 != TREE_CODE (ref2
->referred
->decl
))
795 if (VAR_P (ref1
->referred
->decl
))
797 else if (VAR_P (ref2
->referred
->decl
))
802 class_type
->odr_violated
= true;
804 /* Complain about size mismatch. Either we have too many virtual
805 functions or too many virtual table pointers. */
810 varpool_node
*tmp
= prevailing
;
815 auto_diagnostic_group d
;
816 if (warning_at (DECL_SOURCE_LOCATION
817 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
819 "virtual table of type %qD violates "
820 "one definition rule",
821 DECL_CONTEXT (vtable
->decl
)))
823 if (TREE_CODE (ref1
->referring
->decl
) == FUNCTION_DECL
)
825 inform (DECL_SOURCE_LOCATION
826 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
827 "the conflicting type defined in another translation "
829 inform (DECL_SOURCE_LOCATION
830 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
831 "contains additional virtual method %qD",
832 ref1
->referred
->decl
);
836 inform (DECL_SOURCE_LOCATION
837 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
838 "the conflicting type defined in another translation "
839 "unit has virtual table with more entries");
845 /* And in the last case we have either mismatch in between two virtual
846 methods or two virtual table pointers. */
847 auto_diagnostic_group d
;
848 if (warning_at (DECL_SOURCE_LOCATION
849 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), OPT_Wodr
,
850 "virtual table of type %qD violates "
851 "one definition rule",
852 DECL_CONTEXT (vtable
->decl
)))
854 if (TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
)
856 inform (DECL_SOURCE_LOCATION
857 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
858 "the conflicting type defined in another translation "
860 gcc_assert (TREE_CODE (ref2
->referred
->decl
)
862 inform (DECL_SOURCE_LOCATION
863 (ref1
->referred
->ultimate_alias_target ()->decl
),
864 "virtual method %qD",
865 ref1
->referred
->ultimate_alias_target ()->decl
);
866 inform (DECL_SOURCE_LOCATION
867 (ref2
->referred
->ultimate_alias_target ()->decl
),
868 "ought to match virtual method %qD but does not",
869 ref2
->referred
->ultimate_alias_target ()->decl
);
872 inform (DECL_SOURCE_LOCATION
873 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
874 "the conflicting type defined in another translation "
875 "unit has virtual table with different contents");
881 /* Output ODR violation warning about T1 and T2 with REASON.
882 Display location of ST1 and ST2 if REASON speaks about field or
884 If WARN is false, do nothing. Set WARNED if warning was indeed
888 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
889 bool warn
, bool *warned
, const char *reason
)
891 tree decl2
= TYPE_NAME (TYPE_MAIN_VARIANT (t2
));
895 if (!warn
|| !TYPE_NAME(TYPE_MAIN_VARIANT (t1
)))
898 /* ODR warnings are output during LTO streaming; we must apply location
899 cache for potential warnings to be output correctly. */
900 if (lto_location_cache::current_cache
)
901 lto_location_cache::current_cache
->apply_location_cache ();
903 auto_diagnostic_group d
;
904 if (t1
!= TYPE_MAIN_VARIANT (t1
)
905 && TYPE_NAME (t1
) != TYPE_NAME (TYPE_MAIN_VARIANT (t1
)))
907 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1
))),
908 OPT_Wodr
, "type %qT (typedef of %qT) violates the "
909 "C++ One Definition Rule",
910 t1
, TYPE_MAIN_VARIANT (t1
)))
915 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1
))),
916 OPT_Wodr
, "type %qT violates the C++ One Definition Rule",
922 /* For FIELD_DECL support also case where one of fields is
923 NULL - this is used when the structures have mismatching number of
925 else if (!st1
|| TREE_CODE (st1
) == FIELD_DECL
)
927 inform (DECL_SOURCE_LOCATION (decl2
),
928 "a different type is defined in another translation unit");
934 inform (DECL_SOURCE_LOCATION (st1
),
935 "the first difference of corresponding definitions is field %qD",
940 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
942 inform (DECL_SOURCE_LOCATION (decl2
),
943 "a different type is defined in another translation unit");
944 inform (DECL_SOURCE_LOCATION (st1
),
945 "the first difference of corresponding definitions is method %qD",
951 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
957 /* Return true if T1 and T2 are incompatible and we want to recursively
958 dive into them from warn_type_mismatch to give sensible answer. */
961 type_mismatch_p (tree t1
, tree t2
)
963 if (odr_or_derived_type_p (t1
) && odr_or_derived_type_p (t2
)
964 && !odr_types_equivalent_p (t1
, t2
))
966 return !types_compatible_p (t1
, t2
);
970 /* Types T1 and T2 was found to be incompatible in a context they can't
971 (either used to declare a symbol of same assembler name or unified by
972 ODR rule). We already output warning about this, but if possible, output
973 extra information on how the types mismatch.
975 This is hard to do in general. We basically handle the common cases.
977 If LOC1 and LOC2 are meaningful locations, use it in the case the types
978 themselves do not have one. */
981 warn_types_mismatch (tree t1
, tree t2
, location_t loc1
, location_t loc2
)
983 /* Location of type is known only if it has TYPE_NAME and the name is
985 location_t loc_t1
= TYPE_NAME (t1
) && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
986 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1
))
988 location_t loc_t2
= TYPE_NAME (t2
) && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
989 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2
))
991 bool loc_t2_useful
= false;
993 /* With LTO it is a common case that the location of both types match.
994 See if T2 has a location that is different from T1. If so, we will
995 inform user about the location.
996 Do not consider the location passed to us in LOC1/LOC2 as those are
998 if (loc_t2
> BUILTINS_LOCATION
&& loc_t2
!= loc_t1
)
1000 if (loc_t1
<= BUILTINS_LOCATION
)
1001 loc_t2_useful
= true;
1004 expanded_location xloc1
= expand_location (loc_t1
);
1005 expanded_location xloc2
= expand_location (loc_t2
);
1007 if (strcmp (xloc1
.file
, xloc2
.file
)
1008 || xloc1
.line
!= xloc2
.line
1009 || xloc1
.column
!= xloc2
.column
)
1010 loc_t2_useful
= true;
1014 if (loc_t1
<= BUILTINS_LOCATION
)
1016 if (loc_t2
<= BUILTINS_LOCATION
)
1019 location_t loc
= loc_t1
<= BUILTINS_LOCATION
? loc_t2
: loc_t1
;
1021 /* It is a quite common bug to reference anonymous namespace type in
1022 non-anonymous namespace class. */
1023 tree mt1
= TYPE_MAIN_VARIANT (t1
);
1024 tree mt2
= TYPE_MAIN_VARIANT (t2
);
1025 if ((type_with_linkage_p (mt1
)
1026 && type_in_anonymous_namespace_p (mt1
))
1027 || (type_with_linkage_p (mt2
)
1028 && type_in_anonymous_namespace_p (mt2
)))
1030 if (!type_with_linkage_p (mt1
)
1031 || !type_in_anonymous_namespace_p (mt1
))
1034 std::swap (mt1
, mt2
);
1035 std::swap (loc_t1
, loc_t2
);
1037 gcc_assert (TYPE_NAME (mt1
)
1038 && TREE_CODE (TYPE_NAME (mt1
)) == TYPE_DECL
);
1039 tree n1
= TYPE_NAME (mt1
);
1040 tree n2
= TYPE_NAME (mt2
) ? TYPE_NAME (mt2
) : NULL
;
1042 if (TREE_CODE (n1
) == TYPE_DECL
)
1043 n1
= DECL_NAME (n1
);
1044 if (n2
&& TREE_CODE (n2
) == TYPE_DECL
)
1045 n2
= DECL_NAME (n2
);
1046 /* Most of the time, the type names will match, do not be unnecessarily
1050 "type %qT defined in anonymous namespace cannot match "
1051 "type %qT across the translation unit boundary",
1055 "type %qT defined in anonymous namespace cannot match "
1056 "across the translation unit boundary",
1060 "the incompatible type defined in another translation unit");
1063 /* If types have mangled ODR names and they are different, it is most
1064 informative to output those.
1065 This also covers types defined in different namespaces. */
1066 const char *odr1
= get_odr_name_for_type (mt1
);
1067 const char *odr2
= get_odr_name_for_type (mt2
);
1068 if (odr1
!= NULL
&& odr2
!= NULL
&& odr1
!= odr2
)
1070 const int opts
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
1071 char *name1
= xstrdup (cplus_demangle (odr1
, opts
));
1072 char *name2
= cplus_demangle (odr2
, opts
);
1073 if (name1
&& name2
&& strcmp (name1
, name2
))
1076 "type name %qs should match type name %qs",
1080 "the incompatible type is defined here");
1086 /* A tricky case are compound types. Often they appear the same in source
1087 code and the mismatch is dragged in by type they are build from.
1088 Look for those differences in subtypes and try to be informative. In other
1089 cases just output nothing because the source code is probably different
1090 and in this case we already output a all necessary info. */
1091 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
1093 if (TREE_CODE (t1
) == TREE_CODE (t2
))
1095 if (TREE_CODE (t1
) == ARRAY_TYPE
1096 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1098 tree i1
= TYPE_DOMAIN (t1
);
1099 tree i2
= TYPE_DOMAIN (t2
);
1102 && TYPE_MAX_VALUE (i1
)
1103 && TYPE_MAX_VALUE (i2
)
1104 && !operand_equal_p (TYPE_MAX_VALUE (i1
),
1105 TYPE_MAX_VALUE (i2
), 0))
1108 "array types have different bounds");
1112 if ((POINTER_TYPE_P (t1
) || TREE_CODE (t1
) == ARRAY_TYPE
)
1113 && type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1114 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
, loc_t2
);
1115 else if (TREE_CODE (t1
) == METHOD_TYPE
1116 || TREE_CODE (t1
) == FUNCTION_TYPE
)
1118 tree parms1
= NULL
, parms2
= NULL
;
1121 if (type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1123 inform (loc
, "return value type mismatch");
1124 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
,
1128 if (prototype_p (t1
) && prototype_p (t2
))
1129 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1131 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
),
1134 if (type_mismatch_p (TREE_VALUE (parms1
), TREE_VALUE (parms2
)))
1136 if (count
== 1 && TREE_CODE (t1
) == METHOD_TYPE
)
1138 "implicit this pointer type mismatch");
1141 "type mismatch in parameter %i",
1142 count
- (TREE_CODE (t1
) == METHOD_TYPE
));
1143 warn_types_mismatch (TREE_VALUE (parms1
),
1144 TREE_VALUE (parms2
),
1149 if (parms1
|| parms2
)
1152 "types have different parameter counts");
1160 if (types_odr_comparable (t1
, t2
)
1161 /* We make assign integers mangled names to be able to handle
1162 signed/unsigned chars. Accepting them here would however lead to
1163 confusing message like
1164 "type ‘const int’ itself violates the C++ One Definition Rule" */
1165 && TREE_CODE (t1
) != INTEGER_TYPE
1166 && types_same_for_odr (t1
, t2
))
1168 "type %qT itself violates the C++ One Definition Rule", t1
);
1169 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1170 else if (TYPE_NAME (t1
) == TYPE_NAME (t2
)
1171 && TREE_CODE (t1
) == TREE_CODE (t2
) && !loc_t2_useful
)
1174 inform (loc_t1
, "type %qT should match type %qT",
1177 inform (loc_t2
, "the incompatible type is defined here");
1180 /* Return true if T should be ignored in TYPE_FIELDS for ODR comparison. */
1183 skip_in_fields_list_p (tree t
)
1185 if (TREE_CODE (t
) != FIELD_DECL
)
1187 /* C++ FE introduces zero sized fields depending on -std setting, see
1190 && integer_zerop (DECL_SIZE (t
))
1191 && DECL_ARTIFICIAL (t
)
1192 && DECL_IGNORED_P (t
)
1198 /* Compare T1 and T2, report ODR violations if WARN is true and set
1199 WARNED to true if anything is reported. Return true if types match.
1200 If true is returned, the types are also compatible in the sense of
1201 gimple_canonical_types_compatible_p.
1202 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1203 about the type if the type itself do not have location. */
1206 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
,
1207 hash_set
<type_pair
> *visited
,
1208 location_t loc1
, location_t loc2
)
1210 /* Check first for the obvious case of pointer identity. */
1214 /* Can't be the same type if the types don't have the same code. */
1215 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1217 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1218 G_("a different type is defined in another translation unit"));
1222 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
1223 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
1224 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
1225 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
1227 /* We cannot trip this when comparing ODR types, only when trying to
1228 match different ODR derivations from different declarations.
1229 So WARN should be always false. */
1234 /* Non-aggregate types can be handled cheaply. */
1235 if (INTEGRAL_TYPE_P (t1
)
1236 || SCALAR_FLOAT_TYPE_P (t1
)
1237 || FIXED_POINT_TYPE_P (t1
)
1238 || TREE_CODE (t1
) == VECTOR_TYPE
1239 || TREE_CODE (t1
) == COMPLEX_TYPE
1240 || TREE_CODE (t1
) == OFFSET_TYPE
1241 || POINTER_TYPE_P (t1
))
1243 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
1245 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1246 G_("a type with different precision is defined "
1247 "in another translation unit"));
1250 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
1252 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1253 G_("a type with different signedness is defined "
1254 "in another translation unit"));
1258 if (TREE_CODE (t1
) == INTEGER_TYPE
1259 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
1261 /* char WRT uint_8? */
1262 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1263 G_("a different type is defined in another "
1264 "translation unit"));
1268 /* For canonical type comparisons we do not want to build SCCs
1269 so we cannot compare pointed-to types. But we can, for now,
1270 require the same pointed-to type kind and match what
1271 useless_type_conversion_p would do. */
1272 if (POINTER_TYPE_P (t1
))
1274 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
1275 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
1277 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1278 G_("it is defined as a pointer in different address "
1279 "space in another translation unit"));
1283 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1284 visited
, loc1
, loc2
))
1286 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1287 G_("it is defined as a pointer to different type "
1288 "in another translation unit"));
1290 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
),
1296 if ((TREE_CODE (t1
) == VECTOR_TYPE
|| TREE_CODE (t1
) == COMPLEX_TYPE
)
1297 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1298 visited
, loc1
, loc2
))
1300 /* Probably specific enough. */
1301 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1302 G_("a different type is defined "
1303 "in another translation unit"));
1305 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1309 /* Do type-specific comparisons. */
1310 else switch (TREE_CODE (t1
))
1314 /* Array types are the same if the element types are the same and
1315 the number of elements are the same. */
1316 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1317 visited
, loc1
, loc2
))
1319 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1320 G_("a different type is defined in another "
1321 "translation unit"));
1323 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1325 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
1326 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
1327 == TYPE_NONALIASED_COMPONENT (t2
));
1329 tree i1
= TYPE_DOMAIN (t1
);
1330 tree i2
= TYPE_DOMAIN (t2
);
1332 /* For an incomplete external array, the type domain can be
1333 NULL_TREE. Check this condition also. */
1334 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
1335 return type_variants_equivalent_p (t1
, t2
);
1337 tree min1
= TYPE_MIN_VALUE (i1
);
1338 tree min2
= TYPE_MIN_VALUE (i2
);
1339 tree max1
= TYPE_MAX_VALUE (i1
);
1340 tree max2
= TYPE_MAX_VALUE (i2
);
1342 /* In C++, minimums should be always 0. */
1343 gcc_assert (min1
== min2
);
1344 if (!operand_equal_p (max1
, max2
, 0))
1346 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1347 G_("an array of different size is defined "
1348 "in another translation unit"));
1356 /* Function types are the same if the return type and arguments types
1358 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1359 visited
, loc1
, loc2
))
1361 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1362 G_("has different return value "
1363 "in another translation unit"));
1365 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1369 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
)
1370 || !prototype_p (t1
) || !prototype_p (t2
))
1371 return type_variants_equivalent_p (t1
, t2
);
1374 tree parms1
, parms2
;
1376 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1378 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
1380 if (!odr_subtypes_equivalent_p
1381 (TREE_VALUE (parms1
), TREE_VALUE (parms2
),
1382 visited
, loc1
, loc2
))
1384 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1385 G_("has different parameters in another "
1386 "translation unit"));
1388 warn_types_mismatch (TREE_VALUE (parms1
),
1389 TREE_VALUE (parms2
), loc1
, loc2
);
1394 if (parms1
|| parms2
)
1396 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1397 G_("has different parameters "
1398 "in another translation unit"));
1402 return type_variants_equivalent_p (t1
, t2
);
1407 case QUAL_UNION_TYPE
:
1411 /* For aggregate types, all the fields must be the same. */
1412 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1414 if (TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
1415 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
1416 != polymorphic_type_binfo_p (TYPE_BINFO (t2
)))
1418 if (polymorphic_type_binfo_p (TYPE_BINFO (t1
)))
1419 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1420 G_("a type defined in another translation unit "
1421 "is not polymorphic"));
1423 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1424 G_("a type defined in another translation unit "
1428 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1430 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1432 /* Skip non-fields. */
1433 while (f1
&& skip_in_fields_list_p (f1
))
1434 f1
= TREE_CHAIN (f1
);
1435 while (f2
&& skip_in_fields_list_p (f2
))
1436 f2
= TREE_CHAIN (f2
);
1439 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1441 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1442 G_("a type with different virtual table pointers"
1443 " is defined in another translation unit"));
1446 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
1448 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1449 G_("a type with different bases is defined "
1450 "in another translation unit"));
1453 if (DECL_NAME (f1
) != DECL_NAME (f2
)
1454 && !DECL_ARTIFICIAL (f1
))
1456 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1457 G_("a field with different name is defined "
1458 "in another translation unit"));
1461 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1463 visited
, loc1
, loc2
))
1465 /* Do not warn about artificial fields and just go into
1466 generic field mismatch warning. */
1467 if (DECL_ARTIFICIAL (f1
))
1470 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1471 G_("a field of same name but different type "
1472 "is defined in another translation unit"));
1474 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
), loc1
, loc2
);
1477 if (!gimple_compare_field_offset (f1
, f2
))
1479 /* Do not warn about artificial fields and just go into
1480 generic field mismatch warning. */
1481 if (DECL_ARTIFICIAL (f1
))
1483 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1484 G_("fields have different layout "
1485 "in another translation unit"));
1488 if (DECL_BIT_FIELD (f1
) != DECL_BIT_FIELD (f2
))
1490 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1491 G_("one field is a bitfield while the other "
1496 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
1497 == DECL_NONADDRESSABLE_P (f2
));
1500 /* If one aggregate has more fields than the other, they
1501 are not the same. */
1504 if ((f1
&& DECL_VIRTUAL_P (f1
)) || (f2
&& DECL_VIRTUAL_P (f2
)))
1505 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1506 G_("a type with different virtual table pointers"
1507 " is defined in another translation unit"));
1508 else if ((f1
&& DECL_ARTIFICIAL (f1
))
1509 || (f2
&& DECL_ARTIFICIAL (f2
)))
1510 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1511 G_("a type with different bases is defined "
1512 "in another translation unit"));
1514 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1515 G_("a type with different number of fields "
1516 "is defined in another translation unit"));
1532 /* Those are better to come last as they are utterly uninformative. */
1533 if (TYPE_SIZE (t1
) && TYPE_SIZE (t2
)
1534 && !operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0))
1536 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1537 G_("a type with different size "
1538 "is defined in another translation unit"));
1542 if (TREE_ADDRESSABLE (t1
) != TREE_ADDRESSABLE (t2
)
1543 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1545 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1546 G_("one type needs to be constructed while the other does not"));
1547 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (t1
));
1550 /* There is no really good user facing warning for this.
1551 Either the original reason for modes being different is lost during
1552 streaming or we should catch earlier warnings. We however must detect
1553 the mismatch to avoid type verifier from cmplaining on mismatched
1554 types between type and canonical type. See PR91576. */
1555 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1556 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1558 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1559 G_("memory layout mismatch"));
1563 gcc_assert (!TYPE_SIZE_UNIT (t1
) || !TYPE_SIZE_UNIT (t2
)
1564 || operand_equal_p (TYPE_SIZE_UNIT (t1
),
1565 TYPE_SIZE_UNIT (t2
), 0));
1566 return type_variants_equivalent_p (t1
, t2
);
1569 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1572 odr_types_equivalent_p (tree type1
, tree type2
)
1574 gcc_checking_assert (odr_or_derived_type_p (type1
)
1575 && odr_or_derived_type_p (type2
));
1577 hash_set
<type_pair
> visited
;
1578 return odr_types_equivalent_p (type1
, type2
, false, NULL
,
1579 &visited
, UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1582 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1583 from VAL->type. This may happen in LTO where tree merging did not merge
1584 all variants of the same type or due to ODR violation.
1586 Analyze and report ODR violations and add type to duplicate list.
1587 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1588 this is first time we see definition of a class return true so the
1589 base types are analyzed. */
1592 add_type_duplicate (odr_type val
, tree type
)
1594 bool build_bases
= false;
1595 bool prevail
= false;
1596 bool odr_must_violate
= false;
1598 if (!val
->types_set
)
1599 val
->types_set
= new hash_set
<tree
>;
1601 /* Chose polymorphic type as leader (this happens only in case of ODR
1603 if ((TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1604 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
1605 && (TREE_CODE (val
->type
) != RECORD_TYPE
|| !TYPE_BINFO (val
->type
)
1606 || !polymorphic_type_binfo_p (TYPE_BINFO (val
->type
))))
1611 /* Always prefer complete type to be the leader. */
1612 else if (!COMPLETE_TYPE_P (val
->type
) && COMPLETE_TYPE_P (type
))
1615 if (TREE_CODE (type
) == RECORD_TYPE
)
1616 build_bases
= TYPE_BINFO (type
);
1618 else if (COMPLETE_TYPE_P (val
->type
) && !COMPLETE_TYPE_P (type
))
1620 else if (TREE_CODE (val
->type
) == RECORD_TYPE
1621 && TREE_CODE (type
) == RECORD_TYPE
1622 && TYPE_BINFO (type
) && !TYPE_BINFO (val
->type
))
1624 gcc_assert (!val
->bases
.length ());
1630 std::swap (val
->type
, type
);
1632 val
->types_set
->add (type
);
1637 gcc_checking_assert (can_be_name_hashed_p (type
)
1638 && can_be_name_hashed_p (val
->type
));
1641 bool base_mismatch
= false;
1643 bool warned
= false;
1644 hash_set
<type_pair
> visited
;
1646 gcc_assert (in_lto_p
);
1647 vec_safe_push (val
->types
, type
);
1649 /* If both are class types, compare the bases. */
1650 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1651 && TREE_CODE (val
->type
) == RECORD_TYPE
1652 && TREE_CODE (type
) == RECORD_TYPE
1653 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1655 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1656 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1658 if (!flag_ltrans
&& !warned
&& !val
->odr_violated
)
1661 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1662 "a type with the same name but different "
1663 "number of polymorphic bases is "
1664 "defined in another translation unit");
1667 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1668 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1669 extra_base
= BINFO_BASE_BINFO
1671 BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)));
1673 extra_base
= BINFO_BASE_BINFO
1674 (TYPE_BINFO (val
->type
),
1675 BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1676 tree extra_base_type
= BINFO_TYPE (extra_base
);
1677 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type
)),
1678 "the extra base is defined here");
1681 base_mismatch
= true;
1684 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1686 tree base1
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
1687 tree base2
= BINFO_BASE_BINFO (TYPE_BINFO (val
->type
), i
);
1688 tree type1
= BINFO_TYPE (base1
);
1689 tree type2
= BINFO_TYPE (base2
);
1691 if (types_odr_comparable (type1
, type2
))
1693 if (!types_same_for_odr (type1
, type2
))
1694 base_mismatch
= true;
1697 if (!odr_types_equivalent_p (type1
, type2
))
1698 base_mismatch
= true;
1701 if (!warned
&& !val
->odr_violated
)
1703 warn_odr (type
, val
->type
, NULL
, NULL
,
1705 "a type with the same name but different base "
1706 "type is defined in another translation unit");
1708 warn_types_mismatch (type1
, type2
,
1709 UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1713 if (BINFO_OFFSET (base1
) != BINFO_OFFSET (base2
))
1715 base_mismatch
= true;
1716 if (!warned
&& !val
->odr_violated
)
1717 warn_odr (type
, val
->type
, NULL
, NULL
,
1719 "a type with the same name but different base "
1720 "layout is defined in another translation unit");
1723 /* One of bases is not of complete type. */
1724 if (!TYPE_BINFO (type1
) != !TYPE_BINFO (type2
))
1726 /* If we have a polymorphic type info specified for TYPE1
1727 but not for TYPE2 we possibly missed a base when recording
1729 Be sure this does not happen. */
1730 if (TYPE_BINFO (type1
)
1731 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1733 odr_must_violate
= true;
1736 /* One base is polymorphic and the other not.
1737 This ought to be diagnosed earlier, but do not ICE in the
1739 else if (TYPE_BINFO (type1
)
1740 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1741 != polymorphic_type_binfo_p (TYPE_BINFO (type2
)))
1743 if (!warned
&& !val
->odr_violated
)
1744 warn_odr (type
, val
->type
, NULL
, NULL
,
1746 "a base of the type is polymorphic only in one "
1747 "translation unit");
1748 base_mismatch
= true;
1755 odr_violation_reported
= true;
1756 val
->odr_violated
= true;
1758 if (symtab
->dump_file
)
1760 fprintf (symtab
->dump_file
, "ODR base violation\n");
1762 print_node (symtab
->dump_file
, "", val
->type
, 0);
1763 putc ('\n',symtab
->dump_file
);
1764 print_node (symtab
->dump_file
, "", type
, 0);
1765 putc ('\n',symtab
->dump_file
);
1770 /* Next compare memory layout.
1771 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1772 We must apply the location cache to ensure that they are valid
1773 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1774 if (lto_location_cache::current_cache
)
1775 lto_location_cache::current_cache
->apply_location_cache ();
1776 /* As a special case we stream mangles names of integer types so we can see
1777 if they are believed to be same even though they have different
1778 representation. Avoid bogus warning on mismatches in these. */
1779 if (TREE_CODE (type
) != INTEGER_TYPE
1780 && TREE_CODE (val
->type
) != INTEGER_TYPE
1781 && !odr_types_equivalent_p (val
->type
, type
,
1782 !flag_ltrans
&& !val
->odr_violated
&& !warned
,
1784 DECL_SOURCE_LOCATION (TYPE_NAME (val
->type
)),
1785 DECL_SOURCE_LOCATION (TYPE_NAME (type
))))
1788 odr_violation_reported
= true;
1789 val
->odr_violated
= true;
1791 gcc_assert (val
->odr_violated
|| !odr_must_violate
);
1792 /* Sanity check that all bases will be build same way again. */
1794 && COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1795 && TREE_CODE (val
->type
) == RECORD_TYPE
1796 && TREE_CODE (type
) == RECORD_TYPE
1797 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1798 && !val
->odr_violated
1799 && !base_mismatch
&& val
->bases
.length ())
1801 unsigned int num_poly_bases
= 0;
1804 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1805 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1806 (TYPE_BINFO (type
), i
)))
1808 gcc_assert (num_poly_bases
== val
->bases
.length ());
1809 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
));
1811 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1812 (TYPE_BINFO (type
), i
)))
1814 odr_type base
= get_odr_type
1816 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1819 gcc_assert (val
->bases
[j
] == base
);
1825 /* Regularize things a little. During LTO same types may come with
1826 different BINFOs. Either because their virtual table was
1827 not merged by tree merging and only later at decl merging or
1828 because one type comes with external vtable, while other
1829 with internal. We want to merge equivalent binfos to conserve
1830 memory and streaming overhead.
1832 The external vtables are more harmful: they contain references
1833 to external declarations of methods that may be defined in the
1834 merged LTO unit. For this reason we absolutely need to remove
1835 them and replace by internal variants. Not doing so will lead
1836 to incomplete answers from possible_polymorphic_call_targets.
1838 FIXME: disable for now; because ODR types are now build during
1839 streaming in, the variants do not need to be linked to the type,
1840 yet. We need to do the merging in cleanup pass to be implemented
1842 if (!flag_ltrans
&& merge
1844 && TREE_CODE (val
->type
) == RECORD_TYPE
1845 && TREE_CODE (type
) == RECORD_TYPE
1846 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1847 && TYPE_MAIN_VARIANT (type
) == type
1848 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1849 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1850 && BINFO_VTABLE (TYPE_BINFO (type
)))
1852 tree master_binfo
= TYPE_BINFO (val
->type
);
1853 tree v1
= BINFO_VTABLE (master_binfo
);
1854 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1856 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1858 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1859 && operand_equal_p (TREE_OPERAND (v1
, 1),
1860 TREE_OPERAND (v2
, 1), 0));
1861 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1862 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1864 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1865 == DECL_ASSEMBLER_NAME (v2
));
1867 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1871 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1872 for (i
= 0; i
< val
->types
->length (); i
++)
1874 if (TYPE_BINFO ((*val
->types
)[i
])
1876 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1878 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1881 set_type_binfo (type
, master_binfo
);
1886 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
1889 obj_type_ref_class (const_tree ref
)
1891 gcc_checking_assert (TREE_CODE (ref
) == OBJ_TYPE_REF
);
1892 ref
= TREE_TYPE (ref
);
1893 gcc_checking_assert (TREE_CODE (ref
) == POINTER_TYPE
);
1894 ref
= TREE_TYPE (ref
);
1895 /* We look for type THIS points to. ObjC also builds
1896 OBJ_TYPE_REF with non-method calls, Their first parameter
1897 ID however also corresponds to class type. */
1898 gcc_checking_assert (TREE_CODE (ref
) == METHOD_TYPE
1899 || TREE_CODE (ref
) == FUNCTION_TYPE
);
1900 ref
= TREE_VALUE (TYPE_ARG_TYPES (ref
));
1901 gcc_checking_assert (TREE_CODE (ref
) == POINTER_TYPE
);
1902 tree ret
= TREE_TYPE (ref
);
1903 if (!in_lto_p
&& !TYPE_STRUCTURAL_EQUALITY_P (ret
))
1904 ret
= TYPE_CANONICAL (ret
);
1906 ret
= get_odr_type (ret
)->type
;
1910 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1911 possibly new entry. */
1914 get_odr_type (tree type
, bool insert
)
1916 odr_type_d
**slot
= NULL
;
1917 odr_type val
= NULL
;
1919 bool build_bases
= false;
1920 bool insert_to_odr_array
= false;
1923 type
= TYPE_MAIN_VARIANT (type
);
1924 if (!in_lto_p
&& !TYPE_STRUCTURAL_EQUALITY_P (type
))
1925 type
= TYPE_CANONICAL (type
);
1927 gcc_checking_assert (can_be_name_hashed_p (type
));
1929 hash
= hash_odr_name (type
);
1930 slot
= odr_hash
->find_slot_with_hash (type
, hash
,
1931 insert
? INSERT
: NO_INSERT
);
1936 /* See if we already have entry for type. */
1941 if (val
->type
!= type
&& insert
1942 && (!val
->types_set
|| !val
->types_set
->add (type
)))
1943 build_bases
= add_type_duplicate (val
, type
);
1947 val
= ggc_cleared_alloc
<odr_type_d
> ();
1950 val
->derived_types
= vNULL
;
1951 if (type_with_linkage_p (type
))
1952 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
1954 val
->anonymous_namespace
= 0;
1955 build_bases
= COMPLETE_TYPE_P (val
->type
);
1956 insert_to_odr_array
= true;
1960 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1961 && type_with_linkage_p (type
)
1962 && type
== TYPE_MAIN_VARIANT (type
))
1964 tree binfo
= TYPE_BINFO (type
);
1967 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) == type
);
1969 val
->all_derivations_known
= type_all_derivations_known_p (type
);
1970 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
1971 /* For now record only polymorphic types. other are
1972 pointless for devirtualization and we cannot precisely
1973 determine ODR equivalency of these during LTO. */
1974 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
1976 tree base_type
= BINFO_TYPE (BINFO_BASE_BINFO (binfo
, i
));
1977 odr_type base
= get_odr_type (base_type
, true);
1978 gcc_assert (TYPE_MAIN_VARIANT (base_type
) == base_type
);
1979 base
->derived_types
.safe_push (val
);
1980 val
->bases
.safe_push (base
);
1981 if (base
->id
> base_id
)
1985 /* Ensure that type always appears after bases. */
1986 if (insert_to_odr_array
)
1989 val
->id
= odr_types
.length ();
1990 vec_safe_push (odr_types_ptr
, val
);
1992 else if (base_id
> val
->id
)
1994 odr_types
[val
->id
] = 0;
1995 /* Be sure we did not recorded any derived types; these may need
1997 gcc_assert (val
->derived_types
.length() == 0);
1998 val
->id
= odr_types
.length ();
1999 vec_safe_push (odr_types_ptr
, val
);
2004 /* Return type that in ODR type hash prevailed TYPE. Be careful and punt
2005 on ODR violations. */
2008 prevailing_odr_type (tree type
)
2010 odr_type t
= get_odr_type (type
, false);
2011 if (!t
|| t
->odr_violated
)
2016 /* Set tbaa_enabled flag for TYPE. */
2019 enable_odr_based_tbaa (tree type
)
2021 odr_type t
= get_odr_type (type
, true);
2022 t
->tbaa_enabled
= true;
2025 /* True if canonical type of TYPE is determined using ODR name. */
2028 odr_based_tbaa_p (const_tree type
)
2030 if (!RECORD_OR_UNION_TYPE_P (type
))
2032 odr_type t
= get_odr_type (const_cast <tree
> (type
), false);
2033 if (!t
|| !t
->tbaa_enabled
)
2038 /* Set TYPE_CANONICAL of type and all its variants and duplicates
2042 set_type_canonical_for_odr_type (tree type
, tree canonical
)
2044 odr_type t
= get_odr_type (type
, false);
2048 for (tree t2
= t
->type
; t2
; t2
= TYPE_NEXT_VARIANT (t2
))
2049 TYPE_CANONICAL (t2
) = canonical
;
2051 FOR_EACH_VEC_ELT (*t
->types
, i
, tt
)
2052 for (tree t2
= tt
; t2
; t2
= TYPE_NEXT_VARIANT (t2
))
2053 TYPE_CANONICAL (t2
) = canonical
;
2056 /* Return true if we reported some ODR violation on TYPE. */
2059 odr_type_violation_reported_p (tree type
)
2061 return get_odr_type (type
, false)->odr_violated
;
2064 /* Add TYPE of ODR type hash. */
2067 register_odr_type (tree type
)
2070 odr_hash
= new odr_hash_type (23);
2071 if (type
== TYPE_MAIN_VARIANT (type
))
2073 /* To get ODR warnings right, first register all sub-types. */
2074 if (RECORD_OR_UNION_TYPE_P (type
)
2075 && COMPLETE_TYPE_P (type
))
2077 /* Limit recursion on types which are already registered. */
2078 odr_type ot
= get_odr_type (type
, false);
2080 && (ot
->type
== type
2082 && ot
->types_set
->contains (type
))))
2084 for (tree f
= TYPE_FIELDS (type
); f
; f
= TREE_CHAIN (f
))
2085 if (TREE_CODE (f
) == FIELD_DECL
)
2087 tree subtype
= TREE_TYPE (f
);
2089 while (TREE_CODE (subtype
) == ARRAY_TYPE
)
2090 subtype
= TREE_TYPE (subtype
);
2091 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype
)))
2092 register_odr_type (TYPE_MAIN_VARIANT (subtype
));
2094 if (TYPE_BINFO (type
))
2095 for (unsigned int i
= 0;
2096 i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
2097 register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
2098 (TYPE_BINFO (type
), i
)));
2100 get_odr_type (type
, true);
2104 /* Return true if type is known to have no derivations. */
2107 type_known_to_have_no_derivations_p (tree t
)
2109 return (type_all_derivations_known_p (t
)
2110 && (TYPE_FINAL_P (t
)
2112 && !get_odr_type (t
, true)->derived_types
.length())));
2115 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2116 recursive printing. */
2119 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
2122 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
2123 print_generic_expr (f
, t
->type
, TDF_SLIM
);
2124 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
2125 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
2126 if (TYPE_NAME (t
->type
))
2128 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
->type
)))
2129 fprintf (f
, "%*s mangled name: %s\n", indent
* 2, "",
2131 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
->type
))));
2133 if (t
->bases
.length ())
2135 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
2136 for (i
= 0; i
< t
->bases
.length (); i
++)
2137 fprintf (f
, " %i", t
->bases
[i
]->id
);
2140 if (t
->derived_types
.length ())
2142 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
2143 for (i
= 0; i
< t
->derived_types
.length (); i
++)
2144 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
2149 /* Dump the type inheritance graph. */
2152 dump_type_inheritance_graph (FILE *f
)
2155 unsigned int num_all_types
= 0, num_types
= 0, num_duplicates
= 0;
2158 fprintf (f
, "\n\nType inheritance graph:\n");
2159 for (i
= 0; i
< odr_types
.length (); i
++)
2161 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
2162 dump_odr_type (f
, odr_types
[i
]);
2164 for (i
= 0; i
< odr_types
.length (); i
++)
2170 if (!odr_types
[i
]->types
|| !odr_types
[i
]->types
->length ())
2173 /* To aid ODR warnings we also mangle integer constants but do
2174 not consider duplicates there. */
2175 if (TREE_CODE (odr_types
[i
]->type
) == INTEGER_TYPE
)
2178 /* It is normal to have one duplicate and one normal variant. */
2179 if (odr_types
[i
]->types
->length () == 1
2180 && COMPLETE_TYPE_P (odr_types
[i
]->type
)
2181 && !COMPLETE_TYPE_P ((*odr_types
[i
]->types
)[0]))
2187 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
2188 print_node (f
, "", odr_types
[i
]->type
, 0);
2189 print_node (f
, "", TYPE_NAME (odr_types
[i
]->type
), 0);
2191 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2195 fprintf (f
, "duplicate #%i\n", j
);
2196 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
2197 t
= (*odr_types
[i
]->types
)[j
];
2198 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
2200 t
= TYPE_CONTEXT (t
);
2201 print_node (f
, "", t
, 0);
2203 print_node (f
, "", TYPE_NAME ((*odr_types
[i
]->types
)[j
]), 0);
2207 fprintf (f
, "Out of %i types there are %i types with duplicates; "
2208 "%i duplicates overall\n", num_all_types
, num_types
, num_duplicates
);
2211 /* Save some WPA->ltrans streaming by freeing stuff needed only for good
2213 We make TYPE_DECLs to not point back
2214 to the type (which is needed to keep them in the same SCC and preserve
2215 location information to output warnings) and subsequently we make all
2216 TYPE_DECLS of same assembler name equivalent. */
2219 free_odr_warning_data ()
2221 static bool odr_data_freed
= false;
2223 if (odr_data_freed
|| !flag_wpa
|| !odr_types_ptr
)
2226 odr_data_freed
= true;
2228 for (unsigned int i
= 0; i
< odr_types
.length (); i
++)
2231 tree t
= odr_types
[i
]->type
;
2233 TREE_TYPE (TYPE_NAME (t
)) = void_type_node
;
2235 if (odr_types
[i
]->types
)
2236 for (unsigned int j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2238 tree td
= (*odr_types
[i
]->types
)[j
];
2240 TYPE_NAME (td
) = TYPE_NAME (t
);
2243 odr_data_freed
= true;
2246 /* Initialize IPA devirt and build inheritance tree graph. */
2249 build_type_inheritance_graph (void)
2251 struct symtab_node
*n
;
2252 FILE *inheritance_dump_file
;
2257 free_odr_warning_data ();
2260 timevar_push (TV_IPA_INHERITANCE
);
2261 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
2262 odr_hash
= new odr_hash_type (23);
2264 /* We reconstruct the graph starting of types of all methods seen in the
2267 if (is_a
<cgraph_node
*> (n
)
2268 && DECL_VIRTUAL_P (n
->decl
)
2269 && n
->real_symbol_p ())
2270 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
2272 /* Look also for virtual tables of types that do not define any methods.
2274 We need it in a case where class B has virtual base of class A
2275 re-defining its virtual method and there is class C with no virtual
2276 methods with B as virtual base.
2278 Here we output B's virtual method in two variant - for non-virtual
2279 and virtual inheritance. B's virtual table has non-virtual version,
2280 while C's has virtual.
2282 For this reason we need to know about C in order to include both
2283 variants of B. More correctly, record_target_from_binfo should
2284 add both variants of the method when walking B, but we have no
2285 link in between them.
2287 We rely on fact that either the method is exported and thus we
2288 assume it is called externally or C is in anonymous namespace and
2289 thus we will see the vtable. */
2291 else if (is_a
<varpool_node
*> (n
)
2292 && DECL_VIRTUAL_P (n
->decl
)
2293 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
2294 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
2295 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
2296 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
2297 if (inheritance_dump_file
)
2299 dump_type_inheritance_graph (inheritance_dump_file
);
2300 dump_end (TDI_inheritance
, inheritance_dump_file
);
2302 free_odr_warning_data ();
2303 timevar_pop (TV_IPA_INHERITANCE
);
2306 /* Return true if N has reference from live virtual table
2307 (and thus can be a destination of polymorphic call).
2308 Be conservatively correct when callgraph is not built or
2309 if the method may be referred externally. */
2312 referenced_from_vtable_p (struct cgraph_node
*node
)
2315 struct ipa_ref
*ref
;
2318 if (node
->externally_visible
2319 || DECL_EXTERNAL (node
->decl
)
2320 || node
->used_from_other_partition
)
2323 /* Keep this test constant time.
2324 It is unlikely this can happen except for the case where speculative
2325 devirtualization introduced many speculative edges to this node.
2326 In this case the target is very likely alive anyway. */
2327 if (node
->ref_list
.referring
.length () > 100)
2330 /* We need references built. */
2331 if (symtab
->state
<= CONSTRUCTION
)
2334 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
2335 if ((ref
->use
== IPA_REF_ALIAS
2336 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
2337 || (ref
->use
== IPA_REF_ADDR
2338 && VAR_P (ref
->referring
->decl
)
2339 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
2347 /* Return if TARGET is cxa_pure_virtual. */
2350 is_cxa_pure_virtual_p (tree target
)
2352 return target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
2353 && DECL_NAME (target
)
2354 && id_equal (DECL_NAME (target
),
2355 "__cxa_pure_virtual");
2358 /* If TARGET has associated node, record it in the NODES array.
2359 CAN_REFER specify if program can refer to the target directly.
2360 if TARGET is unknown (NULL) or it cannot be inserted (for example because
2361 its body was already removed and there is no way to refer to it), clear
2365 maybe_record_node (vec
<cgraph_node
*> &nodes
,
2366 tree target
, hash_set
<tree
> *inserted
,
2370 struct cgraph_node
*target_node
, *alias_target
;
2371 enum availability avail
;
2372 bool pure_virtual
= is_cxa_pure_virtual_p (target
);
2374 /* __builtin_unreachable do not need to be added into
2375 list of targets; the runtime effect of calling them is undefined.
2376 Only "real" virtual methods should be accounted. */
2377 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
&& !pure_virtual
)
2382 /* The only case when method of anonymous namespace becomes unreferable
2383 is when we completely optimized it out. */
2386 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2394 target_node
= cgraph_node::get (target
);
2396 /* Prefer alias target over aliases, so we do not get confused by
2400 alias_target
= target_node
->ultimate_alias_target (&avail
);
2401 if (target_node
!= alias_target
2402 && avail
>= AVAIL_AVAILABLE
2403 && target_node
->get_availability ())
2404 target_node
= alias_target
;
2407 /* Method can only be called by polymorphic call if any
2408 of vtables referring to it are alive.
2410 While this holds for non-anonymous functions, too, there are
2411 cases where we want to keep them in the list; for example
2412 inline functions with -fno-weak are static, but we still
2413 may devirtualize them when instance comes from other unit.
2414 The same holds for LTO.
2416 Currently we ignore these functions in speculative devirtualization.
2417 ??? Maybe it would make sense to be more aggressive for LTO even
2421 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
2423 || !referenced_from_vtable_p (target_node
)))
2425 /* See if TARGET is useful function we can deal with. */
2426 else if (target_node
!= NULL
2427 && (TREE_PUBLIC (target
)
2428 || DECL_EXTERNAL (target
)
2429 || target_node
->definition
)
2430 && target_node
->real_symbol_p ())
2432 gcc_assert (!target_node
->inlined_to
);
2433 gcc_assert (target_node
->real_symbol_p ());
2434 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2435 by valid program. */
2436 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
2438 /* Only add pure virtual if it is the only possible target. This way
2439 we will preserve the diagnostics about pure virtual called in many
2440 cases without disabling optimization in other. */
2441 else if (pure_virtual
)
2443 if (nodes
.length ())
2446 /* If we found a real target, take away cxa_pure_virtual. */
2447 else if (!pure_virtual
&& nodes
.length () == 1
2448 && is_cxa_pure_virtual_p (nodes
[0]->decl
))
2450 if (pure_virtual
&& nodes
.length ())
2452 if (!inserted
->add (target
))
2454 cached_polymorphic_call_targets
->add (target_node
);
2455 nodes
.safe_push (target_node
);
2458 else if (!completep
)
2460 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2461 optimized out or partitioned to other unit) so we cannot add it. When
2462 not sanitizing, there is nothing to do.
2463 Otherwise declare the list incomplete. */
2464 else if (pure_virtual
)
2466 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
2469 else if (flag_ltrans
2470 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2474 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2475 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2476 method in vtable and insert method to NODES array
2477 or BASES_TO_CONSIDER if this array is non-NULL.
2478 Otherwise recurse to base BINFOs.
2479 This matches what get_binfo_at_offset does, but with offset
2482 TYPE_BINFOS is a stack of BINFOS of types with defined
2483 virtual table seen on way from class type to BINFO.
2485 MATCHED_VTABLES tracks virtual tables we already did lookup
2486 for virtual function in. INSERTED tracks nodes we already
2489 ANONYMOUS is true if BINFO is part of anonymous namespace.
2491 Clear COMPLETEP when we hit unreferable target.
2495 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
2496 vec
<tree
> *bases_to_consider
,
2499 vec
<tree
> &type_binfos
,
2500 HOST_WIDE_INT otr_token
,
2502 HOST_WIDE_INT offset
,
2503 hash_set
<tree
> *inserted
,
2504 hash_set
<tree
> *matched_vtables
,
2508 tree type
= BINFO_TYPE (binfo
);
2513 if (BINFO_VTABLE (binfo
))
2514 type_binfos
.safe_push (binfo
);
2515 if (types_same_for_odr (type
, outer_type
))
2518 tree type_binfo
= NULL
;
2520 /* Look up BINFO with virtual table. For normal types it is always last
2522 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
2523 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
2525 type_binfo
= type_binfos
[i
];
2528 if (BINFO_VTABLE (binfo
))
2530 /* If this is duplicated BINFO for base shared by virtual inheritance,
2531 we may not have its associated vtable. This is not a problem, since
2532 we will walk it on the other path. */
2535 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
2539 gcc_assert (odr_violation_reported
);
2542 /* For types in anonymous namespace first check if the respective vtable
2543 is alive. If not, we know the type can't be called. */
2544 if (!flag_ltrans
&& anonymous
)
2546 tree vtable
= BINFO_VTABLE (inner_binfo
);
2547 varpool_node
*vnode
;
2549 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
2550 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
2551 vnode
= varpool_node::get (vtable
);
2552 if (!vnode
|| !vnode
->definition
)
2555 gcc_assert (inner_binfo
);
2556 if (bases_to_consider
2557 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
2558 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
2561 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2564 if (!bases_to_consider
)
2565 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2566 /* Destructors are never called via construction vtables. */
2567 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
2568 bases_to_consider
->safe_push (target
);
2574 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2575 /* Walking bases that have no virtual method is pointless exercise. */
2576 if (polymorphic_type_binfo_p (base_binfo
))
2577 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
2579 otr_token
, outer_type
, offset
, inserted
,
2580 matched_vtables
, anonymous
, completep
);
2581 if (BINFO_VTABLE (binfo
))
2585 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2586 of TYPE, insert them to NODES, recurse into derived nodes.
2587 INSERTED is used to avoid duplicate insertions of methods into NODES.
2588 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2589 Clear COMPLETEP if unreferable target is found.
2591 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2592 all cases where BASE_SKIPPED is true (because the base is abstract
2596 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
2597 hash_set
<tree
> *inserted
,
2598 hash_set
<tree
> *matched_vtables
,
2601 HOST_WIDE_INT otr_token
,
2603 HOST_WIDE_INT offset
,
2605 vec
<tree
> &bases_to_consider
,
2606 bool consider_construction
)
2608 tree binfo
= TYPE_BINFO (type
->type
);
2610 auto_vec
<tree
, 8> type_binfos
;
2611 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
2613 /* We may need to consider types w/o instances because of possible derived
2614 types using their methods either directly or via construction vtables.
2615 We are safe to skip them when all derivations are known, since we will
2617 This is done by recording them to BASES_TO_CONSIDER array. */
2618 if (possibly_instantiated
|| consider_construction
)
2620 record_target_from_binfo (nodes
,
2621 (!possibly_instantiated
2622 && type_all_derivations_known_p (type
->type
))
2623 ? &bases_to_consider
: NULL
,
2624 binfo
, otr_type
, type_binfos
, otr_token
,
2626 inserted
, matched_vtables
,
2627 type
->anonymous_namespace
, completep
);
2629 for (i
= 0; i
< type
->derived_types
.length (); i
++)
2630 possible_polymorphic_call_targets_1 (nodes
, inserted
,
2633 type
->derived_types
[i
],
2634 otr_token
, outer_type
, offset
, completep
,
2635 bases_to_consider
, consider_construction
);
2638 /* Cache of queries for polymorphic call targets.
2640 Enumerating all call targets may get expensive when there are many
2641 polymorphic calls in the program, so we memoize all the previous
2642 queries and avoid duplicated work. */
2644 class polymorphic_call_target_d
2647 HOST_WIDE_INT otr_token
;
2648 ipa_polymorphic_call_context context
;
2650 vec
<cgraph_node
*> targets
;
2653 unsigned int n_odr_types
;
2658 /* Polymorphic call target cache helpers. */
2660 struct polymorphic_call_target_hasher
2661 : pointer_hash
<polymorphic_call_target_d
>
2663 static inline hashval_t
hash (const polymorphic_call_target_d
*);
2664 static inline bool equal (const polymorphic_call_target_d
*,
2665 const polymorphic_call_target_d
*);
2666 static inline void remove (polymorphic_call_target_d
*);
2669 /* Return the computed hashcode for ODR_QUERY. */
2672 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d
*odr_query
)
2674 inchash::hash
hstate (odr_query
->otr_token
);
2676 hstate
.add_hwi (odr_query
->type
->id
);
2677 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
2678 hstate
.add_hwi (odr_query
->context
.offset
);
2679 hstate
.add_hwi (odr_query
->n_odr_types
);
2681 if (odr_query
->context
.speculative_outer_type
)
2683 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
2684 hstate
.add_hwi (odr_query
->context
.speculative_offset
);
2686 hstate
.add_flag (odr_query
->speculative
);
2687 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
2688 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
2689 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
2690 hstate
.commit_flag ();
2691 return hstate
.end ();
2694 /* Compare cache entries T1 and T2. */
2697 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d
*t1
,
2698 const polymorphic_call_target_d
*t2
)
2700 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
2701 && t1
->speculative
== t2
->speculative
2702 && t1
->context
.offset
== t2
->context
.offset
2703 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
2704 && t1
->context
.outer_type
== t2
->context
.outer_type
2705 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
2706 && t1
->context
.maybe_in_construction
2707 == t2
->context
.maybe_in_construction
2708 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
2709 && (t1
->context
.speculative_maybe_derived_type
2710 == t2
->context
.speculative_maybe_derived_type
)
2711 /* Adding new type may affect outcome of target search. */
2712 && t1
->n_odr_types
== t2
->n_odr_types
);
2715 /* Remove entry in polymorphic call target cache hash. */
2718 polymorphic_call_target_hasher::remove (polymorphic_call_target_d
*v
)
2720 v
->targets
.release ();
2724 /* Polymorphic call target query cache. */
2726 typedef hash_table
<polymorphic_call_target_hasher
>
2727 polymorphic_call_target_hash_type
;
2728 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
2730 /* Destroy polymorphic call target query cache. */
2733 free_polymorphic_call_targets_hash ()
2735 if (cached_polymorphic_call_targets
)
2737 delete polymorphic_call_target_hash
;
2738 polymorphic_call_target_hash
= NULL
;
2739 delete cached_polymorphic_call_targets
;
2740 cached_polymorphic_call_targets
= NULL
;
2744 /* Force rebuilding type inheritance graph from scratch.
2745 This is use to make sure that we do not keep references to types
2746 which was not visible to free_lang_data. */
2749 rebuild_type_inheritance_graph ()
2755 odr_types_ptr
= NULL
;
2756 free_polymorphic_call_targets_hash ();
2759 /* When virtual function is removed, we may need to flush the cache. */
2762 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
2764 if (cached_polymorphic_call_targets
2766 && cached_polymorphic_call_targets
->contains (n
))
2767 free_polymorphic_call_targets_hash ();
2770 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2773 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2776 tree v
= BINFO_VTABLE (binfo
);
2779 unsigned HOST_WIDE_INT this_offset
;
2783 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2786 if (offset
== this_offset
2787 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2791 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2792 if (polymorphic_type_binfo_p (base_binfo
))
2794 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2801 /* T is known constant value of virtual table pointer.
2802 Store virtual table to V and its offset to OFFSET.
2803 Return false if T does not look like virtual table reference. */
2806 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2807 unsigned HOST_WIDE_INT
*offset
)
2809 /* We expect &MEM[(void *)&virtual_table + 16B].
2810 We obtain object's BINFO from the context of the virtual table.
2811 This one contains pointer to virtual table represented via
2812 POINTER_PLUS_EXPR. Verify that this pointer matches what
2813 we propagated through.
2815 In the case of virtual inheritance, the virtual tables may
2816 be nested, i.e. the offset may be different from 16 and we may
2817 need to dive into the type representation. */
2818 if (TREE_CODE (t
) == ADDR_EXPR
2819 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2820 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2821 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2822 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2824 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2825 (TREE_OPERAND (t
, 0), 0), 0)))
2827 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2828 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2832 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2833 We need to handle it when T comes from static variable initializer or
2835 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2837 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2838 t
= TREE_OPERAND (t
, 0);
2843 if (TREE_CODE (t
) != ADDR_EXPR
)
2845 *v
= TREE_OPERAND (t
, 0);
2849 /* T is known constant value of virtual table pointer. Return BINFO of the
2853 vtable_pointer_value_to_binfo (const_tree t
)
2856 unsigned HOST_WIDE_INT offset
;
2858 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2861 /* FIXME: for stores of construction vtables we return NULL,
2862 because we do not have BINFO for those. Eventually we should fix
2863 our representation to allow this case to be handled, too.
2864 In the case we see store of BINFO we however may assume
2865 that standard folding will be able to cope with it. */
2866 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2870 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2871 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2872 and insert them in NODES.
2874 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2877 record_targets_from_bases (tree otr_type
,
2878 HOST_WIDE_INT otr_token
,
2880 HOST_WIDE_INT offset
,
2881 vec
<cgraph_node
*> &nodes
,
2882 hash_set
<tree
> *inserted
,
2883 hash_set
<tree
> *matched_vtables
,
2888 HOST_WIDE_INT pos
, size
;
2892 if (types_same_for_odr (outer_type
, otr_type
))
2895 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
2897 if (TREE_CODE (fld
) != FIELD_DECL
)
2900 pos
= int_bit_position (fld
);
2901 size
= tree_to_shwi (DECL_SIZE (fld
));
2902 if (pos
<= offset
&& (pos
+ size
) > offset
2903 /* Do not get confused by zero sized bases. */
2904 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
2907 /* Within a class type we should always find corresponding fields. */
2908 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
2910 /* Nonbase types should have been stripped by outer_class_type. */
2911 gcc_assert (DECL_ARTIFICIAL (fld
));
2913 outer_type
= TREE_TYPE (fld
);
2916 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
2920 gcc_assert (odr_violation_reported
);
2923 gcc_assert (base_binfo
);
2924 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
2927 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2930 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
2931 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2932 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
2937 /* When virtual table is removed, we may need to flush the cache. */
2940 devirt_variable_node_removal_hook (varpool_node
*n
,
2941 void *d ATTRIBUTE_UNUSED
)
2943 if (cached_polymorphic_call_targets
2944 && DECL_VIRTUAL_P (n
->decl
)
2945 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
2946 free_polymorphic_call_targets_hash ();
2949 /* Record about how many calls would benefit from given type to be final. */
2951 struct odr_type_warn_count
2955 profile_count dyn_count
;
2958 /* Record about how many calls would benefit from given method to be final. */
2960 struct decl_warn_count
2964 profile_count dyn_count
;
2967 /* Information about type and decl warnings. */
2969 class final_warning_record
2972 /* If needed grow type_warnings vector and initialize new decl_warn_count
2973 to have dyn_count set to profile_count::zero (). */
2974 void grow_type_warnings (unsigned newlen
);
2976 profile_count dyn_count
;
2977 auto_vec
<odr_type_warn_count
> type_warnings
;
2978 hash_map
<tree
, decl_warn_count
> decl_warnings
;
2982 final_warning_record::grow_type_warnings (unsigned newlen
)
2984 unsigned len
= type_warnings
.length ();
2987 type_warnings
.safe_grow_cleared (newlen
);
2988 for (unsigned i
= len
; i
< newlen
; i
++)
2989 type_warnings
[i
].dyn_count
= profile_count::zero ();
2993 class final_warning_record
*final_warning_records
;
2995 /* Return vector containing possible targets of polymorphic call of type
2996 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2997 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2998 OTR_TYPE and include their virtual method. This is useful for types
2999 possibly in construction or destruction where the virtual table may
3000 temporarily change to one of base types. INCLUDE_DERIVED_TYPES make
3001 us to walk the inheritance graph for all derivations.
3003 If COMPLETEP is non-NULL, store true if the list is complete.
3004 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3005 in the target cache. If user needs to visit every target list
3006 just once, it can memoize them.
3008 If SPECULATIVE is set, the list will not contain targets that
3009 are not speculatively taken.
3011 Returned vector is placed into cache. It is NOT caller's responsibility
3012 to free it. The vector can be freed on cgraph_remove_node call if
3013 the particular node is a virtual function present in the cache. */
3016 possible_polymorphic_call_targets (tree otr_type
,
3017 HOST_WIDE_INT otr_token
,
3018 ipa_polymorphic_call_context context
,
3023 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
3024 vec
<cgraph_node
*> nodes
= vNULL
;
3025 auto_vec
<tree
, 8> bases_to_consider
;
3026 odr_type type
, outer_type
;
3027 polymorphic_call_target_d key
;
3028 polymorphic_call_target_d
**slot
;
3032 bool can_refer
= false;
3033 bool skipped
= false;
3035 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
3037 /* If ODR is not initialized or the context is invalid, return empty
3039 if (!odr_hash
|| context
.invalid
|| !TYPE_BINFO (otr_type
))
3042 *completep
= context
.invalid
;
3044 *cache_token
= NULL
;
3048 /* Do not bother to compute speculative info when user do not asks for it. */
3049 if (!speculative
|| !context
.speculative_outer_type
)
3050 context
.clear_speculation ();
3052 type
= get_odr_type (otr_type
, true);
3054 /* Recording type variants would waste results cache. */
3055 gcc_assert (!context
.outer_type
3056 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3058 /* Look up the outer class type we want to walk.
3059 If we fail to do so, the context is invalid. */
3060 if ((context
.outer_type
|| context
.speculative_outer_type
)
3061 && !context
.restrict_to_inner_class (otr_type
))
3066 *cache_token
= NULL
;
3069 gcc_assert (!context
.invalid
);
3071 /* Check that restrict_to_inner_class kept the main variant. */
3072 gcc_assert (!context
.outer_type
3073 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3075 /* We canonicalize our query, so we do not need extra hashtable entries. */
3077 /* Without outer type, we have no use for offset. Just do the
3078 basic search from inner type. */
3079 if (!context
.outer_type
)
3080 context
.clear_outer_type (otr_type
);
3081 /* We need to update our hierarchy if the type does not exist. */
3082 outer_type
= get_odr_type (context
.outer_type
, true);
3083 /* If the type is complete, there are no derivations. */
3084 if (TYPE_FINAL_P (outer_type
->type
))
3085 context
.maybe_derived_type
= false;
3087 /* Initialize query cache. */
3088 if (!cached_polymorphic_call_targets
)
3090 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
3091 polymorphic_call_target_hash
3092 = new polymorphic_call_target_hash_type (23);
3093 if (!node_removal_hook_holder
)
3095 node_removal_hook_holder
=
3096 symtab
->add_cgraph_removal_hook (&devirt_node_removal_hook
, NULL
);
3097 symtab
->add_varpool_removal_hook (&devirt_variable_node_removal_hook
,
3104 if (context
.outer_type
!= otr_type
)
3106 = get_odr_type (context
.outer_type
, true)->type
;
3107 if (context
.speculative_outer_type
)
3108 context
.speculative_outer_type
3109 = get_odr_type (context
.speculative_outer_type
, true)->type
;
3112 /* Look up cached answer. */
3114 key
.otr_token
= otr_token
;
3115 key
.speculative
= speculative
;
3116 key
.context
= context
;
3117 key
.n_odr_types
= odr_types
.length ();
3118 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
3120 *cache_token
= (void *)*slot
;
3124 *completep
= (*slot
)->complete
;
3125 if ((*slot
)->type_warning
&& final_warning_records
)
3127 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
3128 if (!final_warning_records
->type_warnings
3129 [(*slot
)->type_warning
- 1].dyn_count
.initialized_p ())
3130 final_warning_records
->type_warnings
3131 [(*slot
)->type_warning
- 1].dyn_count
= profile_count::zero ();
3132 if (final_warning_records
->dyn_count
> 0)
3133 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3134 = final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3135 + final_warning_records
->dyn_count
;
3137 if (!speculative
&& (*slot
)->decl_warning
&& final_warning_records
)
3139 struct decl_warn_count
*c
=
3140 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
3142 if (final_warning_records
->dyn_count
> 0)
3143 c
->dyn_count
+= final_warning_records
->dyn_count
;
3145 return (*slot
)->targets
;
3150 /* Do actual search. */
3151 timevar_push (TV_IPA_VIRTUAL_CALL
);
3152 *slot
= XCNEW (polymorphic_call_target_d
);
3154 *cache_token
= (void *)*slot
;
3155 (*slot
)->type
= type
;
3156 (*slot
)->otr_token
= otr_token
;
3157 (*slot
)->context
= context
;
3158 (*slot
)->speculative
= speculative
;
3160 hash_set
<tree
> inserted
;
3161 hash_set
<tree
> matched_vtables
;
3163 /* First insert targets we speculatively identified as likely. */
3164 if (context
.speculative_outer_type
)
3166 odr_type speculative_outer_type
;
3167 bool speculation_complete
= true;
3169 /* First insert target from type itself and check if it may have
3171 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
3172 if (TYPE_FINAL_P (speculative_outer_type
->type
))
3173 context
.speculative_maybe_derived_type
= false;
3174 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
3175 context
.speculative_offset
, otr_type
);
3177 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3182 /* In the case we get complete method, we don't need
3183 to walk derivations. */
3184 if (target
&& DECL_FINAL_P (target
))
3185 context
.speculative_maybe_derived_type
= false;
3186 if (type_possibly_instantiated_p (speculative_outer_type
->type
))
3187 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &speculation_complete
);
3189 matched_vtables
.add (BINFO_VTABLE (binfo
));
3192 /* Next walk recursively all derived types. */
3193 if (context
.speculative_maybe_derived_type
)
3194 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
3195 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3198 speculative_outer_type
->derived_types
[i
],
3199 otr_token
, speculative_outer_type
->type
,
3200 context
.speculative_offset
,
3201 &speculation_complete
,
3206 if (!speculative
|| !nodes
.length ())
3208 /* First see virtual method of type itself. */
3209 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
3210 context
.offset
, otr_type
);
3212 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3216 gcc_assert (odr_violation_reported
);
3220 /* Destructors are never called through construction virtual tables,
3221 because the type is always known. */
3222 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
3223 context
.maybe_in_construction
= false;
3227 /* In the case we get complete method, we don't need
3228 to walk derivations. */
3229 if (DECL_FINAL_P (target
))
3230 context
.maybe_derived_type
= false;
3233 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3234 if (type_possibly_instantiated_p (outer_type
->type
))
3235 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3240 matched_vtables
.add (BINFO_VTABLE (binfo
));
3242 /* Next walk recursively all derived types. */
3243 if (context
.maybe_derived_type
)
3245 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
3246 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3249 outer_type
->derived_types
[i
],
3250 otr_token
, outer_type
->type
,
3251 context
.offset
, &complete
,
3253 context
.maybe_in_construction
);
3255 if (!outer_type
->all_derivations_known
)
3257 if (!speculative
&& final_warning_records
3258 && nodes
.length () == 1
3259 && TREE_CODE (TREE_TYPE (nodes
[0]->decl
)) == METHOD_TYPE
)
3262 && warn_suggest_final_types
3263 && !outer_type
->derived_types
.length ())
3265 final_warning_records
->grow_type_warnings
3267 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
3268 if (!final_warning_records
->type_warnings
3269 [outer_type
->id
].dyn_count
.initialized_p ())
3270 final_warning_records
->type_warnings
3271 [outer_type
->id
].dyn_count
= profile_count::zero ();
3272 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
3273 += final_warning_records
->dyn_count
;
3274 final_warning_records
->type_warnings
[outer_type
->id
].type
3276 (*slot
)->type_warning
= outer_type
->id
+ 1;
3279 && warn_suggest_final_methods
3280 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
3284 struct decl_warn_count
&c
=
3285 final_warning_records
->decl_warnings
.get_or_insert
3286 (nodes
[0]->decl
, &existed
);
3291 c
.dyn_count
+= final_warning_records
->dyn_count
;
3296 c
.dyn_count
= final_warning_records
->dyn_count
;
3297 c
.decl
= nodes
[0]->decl
;
3299 (*slot
)->decl_warning
= nodes
[0]->decl
;
3308 /* Destructors are never called through construction virtual tables,
3309 because the type is always known. One of entries may be
3310 cxa_pure_virtual so look to at least two of them. */
3311 if (context
.maybe_in_construction
)
3312 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
3313 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
3314 context
.maybe_in_construction
= false;
3315 if (context
.maybe_in_construction
)
3317 if (type
!= outer_type
3319 || (context
.maybe_derived_type
3320 && !type_all_derivations_known_p (outer_type
->type
))))
3321 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
3322 context
.offset
, nodes
, &inserted
,
3323 &matched_vtables
, &complete
);
3325 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3326 for (i
= 0; i
< bases_to_consider
.length(); i
++)
3327 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
3332 (*slot
)->targets
= nodes
;
3333 (*slot
)->complete
= complete
;
3334 (*slot
)->n_odr_types
= odr_types
.length ();
3336 *completep
= complete
;
3338 timevar_pop (TV_IPA_VIRTUAL_CALL
);
3343 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
3344 vec
<const decl_warn_count
*> *vec
)
3346 vec
->safe_push (&value
);
3350 /* Dump target list TARGETS into FILE. */
3353 dump_targets (FILE *f
, vec
<cgraph_node
*> targets
, bool verbose
)
3357 for (i
= 0; i
< targets
.length (); i
++)
3361 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
3362 fprintf (f
, " %s", name
? name
: targets
[i
]->dump_name ());
3365 if (!targets
[i
]->definition
)
3366 fprintf (f
, " (no definition%s)",
3367 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
3369 /* With many targets for every call polymorphic dumps are going to
3370 be quadratic in size. */
3371 if (i
> 10 && !verbose
)
3373 fprintf (f
, " ... and %i more targets\n", targets
.length () - i
);
3380 /* Dump all possible targets of a polymorphic call. */
3383 dump_possible_polymorphic_call_targets (FILE *f
,
3385 HOST_WIDE_INT otr_token
,
3386 const ipa_polymorphic_call_context
&ctx
,
3389 vec
<cgraph_node
*> targets
;
3391 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
3396 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3398 &final
, NULL
, false);
3399 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
3400 print_generic_expr (f
, type
->type
, TDF_SLIM
);
3401 fprintf (f
, " token %i\n", (int)otr_token
);
3405 fprintf (f
, " %s%s%s%s\n ",
3406 final
? "This is a complete list." :
3407 "This is partial list; extra targets may be defined in other units.",
3408 ctx
.maybe_in_construction
? " (base types included)" : "",
3409 ctx
.maybe_derived_type
? " (derived types included)" : "",
3410 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
3411 len
= targets
.length ();
3412 dump_targets (f
, targets
, verbose
);
3414 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3416 &final
, NULL
, true);
3417 if (targets
.length () != len
)
3419 fprintf (f
, " Speculative targets:");
3420 dump_targets (f
, targets
, verbose
);
3422 /* Ugly: during callgraph construction the target cache may get populated
3423 before all targets are found. While this is harmless (because all local
3424 types are discovered and only in those case we devirtualize fully and we
3425 don't do speculative devirtualization before IPA stage) it triggers
3426 assert here when dumping at that stage also populates the case with
3427 speculative targets. Quietly ignore this. */
3428 gcc_assert (symtab
->state
< IPA_SSA
|| targets
.length () <= len
);
3433 /* Return true if N can be possibly target of a polymorphic call of
3434 OTR_TYPE/OTR_TOKEN. */
3437 possible_polymorphic_call_target_p (tree otr_type
,
3438 HOST_WIDE_INT otr_token
,
3439 const ipa_polymorphic_call_context
&ctx
,
3440 struct cgraph_node
*n
)
3442 vec
<cgraph_node
*> targets
;
3446 if (fndecl_built_in_p (n
->decl
, BUILT_IN_UNREACHABLE
)
3447 || fndecl_built_in_p (n
->decl
, BUILT_IN_TRAP
))
3450 if (is_cxa_pure_virtual_p (n
->decl
))
3455 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
3456 for (i
= 0; i
< targets
.length (); i
++)
3457 if (n
->semantically_equivalent_p (targets
[i
]))
3460 /* At a moment we allow middle end to dig out new external declarations
3461 as a targets of polymorphic calls. */
3462 if (!final
&& !n
->definition
)
3469 /* Return true if N can be possibly target of a polymorphic call of
3470 OBJ_TYPE_REF expression REF in STMT. */
3473 possible_polymorphic_call_target_p (tree ref
,
3475 struct cgraph_node
*n
)
3477 ipa_polymorphic_call_context
context (current_function_decl
, ref
, stmt
);
3478 tree call_fn
= gimple_call_fn (stmt
);
3480 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn
),
3482 (OBJ_TYPE_REF_TOKEN (call_fn
)),
3488 /* After callgraph construction new external nodes may appear.
3489 Add them into the graph. */
3492 update_type_inheritance_graph (void)
3494 struct cgraph_node
*n
;
3498 free_polymorphic_call_targets_hash ();
3499 timevar_push (TV_IPA_INHERITANCE
);
3500 /* We reconstruct the graph starting from types of all methods seen in the
3502 FOR_EACH_FUNCTION (n
)
3503 if (DECL_VIRTUAL_P (n
->decl
)
3505 && n
->real_symbol_p ())
3506 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
3507 timevar_pop (TV_IPA_INHERITANCE
);
3511 /* Return true if N looks like likely target of a polymorphic call.
3512 Rule out cxa_pure_virtual, noreturns, function declared cold and
3513 other obvious cases. */
3516 likely_target_p (struct cgraph_node
*n
)
3519 /* cxa_pure_virtual and similar things are not likely. */
3520 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
3522 flags
= flags_from_decl_or_type (n
->decl
);
3523 if (flags
& ECF_NORETURN
)
3525 if (lookup_attribute ("cold",
3526 DECL_ATTRIBUTES (n
->decl
)))
3528 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
3530 /* If there are no live virtual tables referring the target,
3531 the only way the target can be called is an instance coming from other
3532 compilation unit; speculative devirtualization is built around an
3533 assumption that won't happen. */
3534 if (!referenced_from_vtable_p (n
))
3539 /* Compare type warning records P1 and P2 and choose one with larger count;
3540 helper for qsort. */
3543 type_warning_cmp (const void *p1
, const void *p2
)
3545 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
3546 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
3548 if (t1
->dyn_count
< t2
->dyn_count
)
3550 if (t1
->dyn_count
> t2
->dyn_count
)
3552 return t2
->count
- t1
->count
;
3555 /* Compare decl warning records P1 and P2 and choose one with larger count;
3556 helper for qsort. */
3559 decl_warning_cmp (const void *p1
, const void *p2
)
3561 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
3562 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
3564 if (t1
->dyn_count
< t2
->dyn_count
)
3566 if (t1
->dyn_count
> t2
->dyn_count
)
3568 return t2
->count
- t1
->count
;
3572 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3575 struct cgraph_node
*
3576 try_speculative_devirtualization (tree otr_type
, HOST_WIDE_INT otr_token
,
3577 ipa_polymorphic_call_context ctx
)
3579 vec
<cgraph_node
*>targets
3580 = possible_polymorphic_call_targets
3581 (otr_type
, otr_token
, ctx
, NULL
, NULL
, true);
3583 struct cgraph_node
*likely_target
= NULL
;
3585 for (i
= 0; i
< targets
.length (); i
++)
3586 if (likely_target_p (targets
[i
]))
3590 likely_target
= targets
[i
];
3593 ||!likely_target
->definition
3594 || DECL_EXTERNAL (likely_target
->decl
))
3597 /* Don't use an implicitly-declared destructor (c++/58678). */
3598 struct cgraph_node
*non_thunk_target
3599 = likely_target
->function_symbol ();
3600 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3602 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3603 && likely_target
->can_be_discarded_p ())
3605 return likely_target
;
3608 /* The ipa-devirt pass.
3609 When polymorphic call has only one likely target in the unit,
3610 turn it into a speculative call. */
3615 struct cgraph_node
*n
;
3616 hash_set
<void *> bad_call_targets
;
3617 struct cgraph_edge
*e
;
3619 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
3620 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
3621 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
3628 dump_type_inheritance_graph (dump_file
);
3630 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3631 This is implemented by setting up final_warning_records that are updated
3632 by get_polymorphic_call_targets.
3633 We need to clear cache in this case to trigger recomputation of all
3635 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3637 final_warning_records
= new (final_warning_record
);
3638 final_warning_records
->dyn_count
= profile_count::zero ();
3639 final_warning_records
->grow_type_warnings (odr_types
.length ());
3640 free_polymorphic_call_targets_hash ();
3643 FOR_EACH_DEFINED_FUNCTION (n
)
3645 bool update
= false;
3646 if (!opt_for_fn (n
->decl
, flag_devirtualize
))
3648 if (dump_file
&& n
->indirect_calls
)
3649 fprintf (dump_file
, "\n\nProcesing function %s\n",
3651 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
3652 if (e
->indirect_info
->polymorphic
)
3654 struct cgraph_node
*likely_target
= NULL
;
3658 if (final_warning_records
)
3659 final_warning_records
->dyn_count
= e
->count
.ipa ();
3661 vec
<cgraph_node
*>targets
3662 = possible_polymorphic_call_targets
3663 (e
, &final
, &cache_token
, true);
3666 /* Trigger warnings by calculating non-speculative targets. */
3667 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3668 possible_polymorphic_call_targets (e
);
3671 dump_possible_polymorphic_call_targets
3672 (dump_file
, e
, (dump_flags
& TDF_DETAILS
));
3676 /* See if the call can be devirtualized by means of ipa-prop's
3677 polymorphic call context propagation. If not, we can just
3678 forget about this call being polymorphic and avoid some heavy
3679 lifting in remove_unreachable_nodes that will otherwise try to
3680 keep all possible targets alive until inlining and in the inliner
3683 This may need to be revisited once we add further ways to use
3684 the may edges, but it is a reasonable thing to do right now. */
3686 if ((e
->indirect_info
->param_index
== -1
3687 || (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
)
3688 && e
->indirect_info
->vptr_changed
))
3689 && !flag_ltrans_devirtualize
)
3691 e
->indirect_info
->polymorphic
= false;
3694 fprintf (dump_file
, "Dropping polymorphic call info;"
3695 " it cannot be used by ipa-prop\n");
3698 if (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
))
3701 if (!e
->maybe_hot_p ())
3704 fprintf (dump_file
, "Call is cold\n\n");
3711 fprintf (dump_file
, "Call is already speculated\n\n");
3714 /* When dumping see if we agree with speculation. */
3718 if (bad_call_targets
.contains (cache_token
))
3721 fprintf (dump_file
, "Target list is known to be useless\n\n");
3725 for (i
= 0; i
< targets
.length (); i
++)
3726 if (likely_target_p (targets
[i
]))
3730 likely_target
= NULL
;
3732 fprintf (dump_file
, "More than one likely target\n\n");
3736 likely_target
= targets
[i
];
3740 bad_call_targets
.add (cache_token
);
3743 /* This is reached only when dumping; check if we agree or disagree
3744 with the speculation. */
3747 bool found
= e
->speculative_call_for_target (likely_target
);
3750 fprintf (dump_file
, "We agree with speculation\n\n");
3755 fprintf (dump_file
, "We disagree with speculation\n\n");
3760 if (!likely_target
->definition
)
3763 fprintf (dump_file
, "Target is not a definition\n\n");
3767 /* Do not introduce new references to external symbols. While we
3768 can handle these just well, it is common for programs to
3769 incorrectly with headers defining methods they are linked
3771 if (DECL_EXTERNAL (likely_target
->decl
))
3774 fprintf (dump_file
, "Target is external\n\n");
3778 /* Don't use an implicitly-declared destructor (c++/58678). */
3779 struct cgraph_node
*non_thunk_target
3780 = likely_target
->function_symbol ();
3781 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3784 fprintf (dump_file
, "Target is artificial\n\n");
3788 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3789 && likely_target
->can_be_discarded_p ())
3792 fprintf (dump_file
, "Target is overwritable\n\n");
3796 else if (dbg_cnt (devirt
))
3798 if (dump_enabled_p ())
3800 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, e
->call_stmt
,
3801 "speculatively devirtualizing call "
3804 likely_target
->dump_name ());
3806 if (!likely_target
->can_be_discarded_p ())
3809 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
3811 likely_target
= alias
;
3816 (likely_target
, e
->count
.apply_scale (8, 10));
3820 ipa_update_overall_fn_summary (n
);
3822 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3824 if (warn_suggest_final_types
)
3826 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
3827 for (unsigned int i
= 0;
3828 i
< final_warning_records
->type_warnings
.length (); i
++)
3829 if (final_warning_records
->type_warnings
[i
].count
)
3831 tree type
= final_warning_records
->type_warnings
[i
].type
;
3832 int count
= final_warning_records
->type_warnings
[i
].count
;
3833 profile_count dyn_count
3834 = final_warning_records
->type_warnings
[i
].dyn_count
;
3836 if (!(dyn_count
> 0))
3837 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3838 OPT_Wsuggest_final_types
, count
,
3839 "Declaring type %qD final "
3840 "would enable devirtualization of %i call",
3841 "Declaring type %qD final "
3842 "would enable devirtualization of %i calls",
3846 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3847 OPT_Wsuggest_final_types
, count
,
3848 "Declaring type %qD final "
3849 "would enable devirtualization of %i call "
3850 "executed %lli times",
3851 "Declaring type %qD final "
3852 "would enable devirtualization of %i calls "
3853 "executed %lli times",
3856 (long long) dyn_count
.to_gcov_type ());
3860 if (warn_suggest_final_methods
)
3862 auto_vec
<const decl_warn_count
*> decl_warnings_vec
;
3864 final_warning_records
->decl_warnings
.traverse
3865 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3866 decl_warnings_vec
.qsort (decl_warning_cmp
);
3867 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3869 tree decl
= decl_warnings_vec
[i
]->decl
;
3870 int count
= decl_warnings_vec
[i
]->count
;
3871 profile_count dyn_count
3872 = decl_warnings_vec
[i
]->dyn_count
;
3874 if (!(dyn_count
> 0))
3875 if (DECL_CXX_DESTRUCTOR_P (decl
))
3876 warning_n (DECL_SOURCE_LOCATION (decl
),
3877 OPT_Wsuggest_final_methods
, count
,
3878 "Declaring virtual destructor of %qD final "
3879 "would enable devirtualization of %i call",
3880 "Declaring virtual destructor of %qD final "
3881 "would enable devirtualization of %i calls",
3882 DECL_CONTEXT (decl
), count
);
3884 warning_n (DECL_SOURCE_LOCATION (decl
),
3885 OPT_Wsuggest_final_methods
, count
,
3886 "Declaring method %qD final "
3887 "would enable devirtualization of %i call",
3888 "Declaring method %qD final "
3889 "would enable devirtualization of %i calls",
3891 else if (DECL_CXX_DESTRUCTOR_P (decl
))
3892 warning_n (DECL_SOURCE_LOCATION (decl
),
3893 OPT_Wsuggest_final_methods
, count
,
3894 "Declaring virtual destructor of %qD final "
3895 "would enable devirtualization of %i call "
3896 "executed %lli times",
3897 "Declaring virtual destructor of %qD final "
3898 "would enable devirtualization of %i calls "
3899 "executed %lli times",
3900 DECL_CONTEXT (decl
), count
,
3901 (long long)dyn_count
.to_gcov_type ());
3903 warning_n (DECL_SOURCE_LOCATION (decl
),
3904 OPT_Wsuggest_final_methods
, count
,
3905 "Declaring method %qD final "
3906 "would enable devirtualization of %i call "
3907 "executed %lli times",
3908 "Declaring method %qD final "
3909 "would enable devirtualization of %i calls "
3910 "executed %lli times",
3912 (long long)dyn_count
.to_gcov_type ());
3916 delete (final_warning_records
);
3917 final_warning_records
= 0;
3922 "%i polymorphic calls, %i devirtualized,"
3923 " %i speculatively devirtualized, %i cold\n"
3924 "%i have multiple targets, %i overwritable,"
3925 " %i already speculated (%i agree, %i disagree),"
3926 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3927 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3928 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3929 nexternal
, nnotdefined
, nartificial
, ndropped
);
3930 return ndevirtualized
|| ndropped
? TODO_remove_functions
: 0;
3935 const pass_data pass_data_ipa_devirt
=
3937 IPA_PASS
, /* type */
3938 "devirt", /* name */
3939 OPTGROUP_NONE
, /* optinfo_flags */
3940 TV_IPA_DEVIRT
, /* tv_id */
3941 0, /* properties_required */
3942 0, /* properties_provided */
3943 0, /* properties_destroyed */
3944 0, /* todo_flags_start */
3945 ( TODO_dump_symtab
), /* todo_flags_finish */
3948 class pass_ipa_devirt
: public ipa_opt_pass_d
3951 pass_ipa_devirt (gcc::context
*ctxt
)
3952 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3953 NULL
, /* generate_summary */
3954 NULL
, /* write_summary */
3955 NULL
, /* read_summary */
3956 NULL
, /* write_optimization_summary */
3957 NULL
, /* read_optimization_summary */
3958 NULL
, /* stmt_fixup */
3959 0, /* function_transform_todo_flags_start */
3960 NULL
, /* function_transform */
3961 NULL
) /* variable_transform */
3964 /* opt_pass methods: */
3965 virtual bool gate (function
*)
3967 /* In LTO, always run the IPA passes and decide on function basis if the
3971 return (flag_devirtualize
3972 && (flag_devirtualize_speculatively
3973 || (warn_suggest_final_methods
3974 || warn_suggest_final_types
))
3978 virtual unsigned int execute (function
*) { return ipa_devirt (); }
3980 }; // class pass_ipa_devirt
3985 make_pass_ipa_devirt (gcc::context
*ctxt
)
3987 return new pass_ipa_devirt (ctxt
);
3990 /* Print ODR name of a TYPE if available.
3991 Use demangler when option DEMANGLE is used. */
3994 debug_tree_odr_name (tree type
, bool demangle
)
3996 const char *odr
= get_odr_name_for_type (type
);
3999 const int opts
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4000 odr
= cplus_demangle (odr
, opts
);
4003 fprintf (stderr
, "%s\n", odr
);
4006 /* Register ODR enum so we later stream record about its values. */
4009 register_odr_enum (tree t
)
4012 vec_safe_push (odr_enums
, t
);
4015 /* Write ODR enums to LTO stream file. */
4018 ipa_odr_summary_write (void)
4020 if (!odr_enums
&& !odr_enum_map
)
4022 struct output_block
*ob
= create_output_block (LTO_section_odr_types
);
4028 streamer_write_uhwi (ob
, odr_enums
->length ());
4030 /* For every ODR enum stream out
4033 - value names and constant their represent
4034 - bitpack of locations so we can do good diagnostics. */
4035 FOR_EACH_VEC_ELT (*odr_enums
, i
, t
)
4037 streamer_write_string (ob
, ob
->main_stream
,
4039 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
))),
4043 for (tree e
= TYPE_VALUES (t
); e
; e
= TREE_CHAIN (e
))
4045 streamer_write_uhwi (ob
, n
);
4046 for (tree e
= TYPE_VALUES (t
); e
; e
= TREE_CHAIN (e
))
4048 streamer_write_string (ob
, ob
->main_stream
,
4049 IDENTIFIER_POINTER (TREE_PURPOSE (e
)),
4051 streamer_write_wide_int (ob
,
4052 wi::to_wide (DECL_INITIAL
4056 bitpack_d bp
= bitpack_create (ob
->main_stream
);
4057 lto_output_location (ob
, &bp
, DECL_SOURCE_LOCATION (TYPE_NAME (t
)));
4058 for (tree e
= TYPE_VALUES (t
); e
; e
= TREE_CHAIN (e
))
4059 lto_output_location (ob
, &bp
,
4060 DECL_SOURCE_LOCATION (TREE_VALUE (e
)));
4061 streamer_write_bitpack (&bp
);
4063 vec_free (odr_enums
);
4066 /* During LTO incremental linking we already have streamed in types. */
4067 else if (odr_enum_map
)
4069 gcc_checking_assert (!odr_enums
);
4070 streamer_write_uhwi (ob
, odr_enum_map
->elements ());
4072 hash_map
<nofree_string_hash
, odr_enum
>::iterator iter
4073 = odr_enum_map
->begin ();
4074 for (; iter
!= odr_enum_map
->end (); ++iter
)
4076 odr_enum
&this_enum
= (*iter
).second
;
4077 streamer_write_string (ob
, ob
->main_stream
, (*iter
).first
, true);
4079 streamer_write_uhwi (ob
, this_enum
.vals
.length ());
4080 for (unsigned j
= 0; j
< this_enum
.vals
.length (); j
++)
4082 streamer_write_string (ob
, ob
->main_stream
,
4083 this_enum
.vals
[j
].name
, true);
4084 streamer_write_wide_int (ob
, this_enum
.vals
[j
].val
);
4087 bitpack_d bp
= bitpack_create (ob
->main_stream
);
4088 lto_output_location (ob
, &bp
, this_enum
.locus
);
4089 for (unsigned j
= 0; j
< this_enum
.vals
.length (); j
++)
4090 lto_output_location (ob
, &bp
, this_enum
.vals
[j
].locus
);
4091 streamer_write_bitpack (&bp
);
4094 delete odr_enum_map
;
4095 obstack_free (&odr_enum_obstack
, NULL
);
4096 odr_enum_map
= NULL
;
4099 produce_asm (ob
, NULL
);
4100 destroy_output_block (ob
);
4103 /* Write ODR enums from LTO stream file and warn on mismatches. */
4106 ipa_odr_read_section (struct lto_file_decl_data
*file_data
, const char *data
,
4109 const struct lto_function_header
*header
4110 = (const struct lto_function_header
*) data
;
4111 const int cfg_offset
= sizeof (struct lto_function_header
);
4112 const int main_offset
= cfg_offset
+ header
->cfg_size
;
4113 const int string_offset
= main_offset
+ header
->main_size
;
4114 class data_in
*data_in
;
4116 lto_input_block
ib ((const char *) data
+ main_offset
, header
->main_size
,
4117 file_data
->mode_table
);
4120 = lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
4121 header
->string_size
, vNULL
);
4122 unsigned int n
= streamer_read_uhwi (&ib
);
4126 gcc_obstack_init (&odr_enum_obstack
);
4127 odr_enum_map
= new (hash_map
<nofree_string_hash
, odr_enum
>);
4130 for (unsigned i
= 0; i
< n
; i
++)
4132 const char *rname
= streamer_read_string (data_in
, &ib
);
4133 unsigned int nvals
= streamer_read_uhwi (&ib
);
4136 obstack_grow (&odr_enum_obstack
, rname
, strlen (rname
) + 1);
4137 name
= XOBFINISH (&odr_enum_obstack
, char *);
4140 class odr_enum
&this_enum
4141 = odr_enum_map
->get_or_insert (xstrdup (name
), &existed_p
);
4143 /* If this is first time we see the enum, remember its definition. */
4146 this_enum
.vals
.safe_grow_cleared (nvals
);
4147 this_enum
.warned
= false;
4149 fprintf (dump_file
, "enum %s\n{\n", name
);
4150 for (unsigned j
= 0; j
< nvals
; j
++)
4152 const char *val_name
= streamer_read_string (data_in
, &ib
);
4153 obstack_grow (&odr_enum_obstack
, val_name
, strlen (val_name
) + 1);
4154 this_enum
.vals
[j
].name
= XOBFINISH (&odr_enum_obstack
, char *);
4155 this_enum
.vals
[j
].val
= streamer_read_wide_int (&ib
);
4157 fprintf (dump_file
, " %s = " HOST_WIDE_INT_PRINT_DEC
",\n",
4158 val_name
, wi::fits_shwi_p (this_enum
.vals
[j
].val
)
4159 ? this_enum
.vals
[j
].val
.to_shwi () : -1);
4161 bitpack_d bp
= streamer_read_bitpack (&ib
);
4162 stream_input_location (&this_enum
.locus
, &bp
, data_in
);
4163 for (unsigned j
= 0; j
< nvals
; j
++)
4164 stream_input_location (&this_enum
.vals
[j
].locus
, &bp
, data_in
);
4165 data_in
->location_cache
.apply_location_cache ();
4167 fprintf (dump_file
, "}\n");
4169 /* If we already have definition, compare it with new one and output
4170 warnings if they differs. */
4173 int do_warning
= -1;
4174 char *warn_name
= NULL
;
4175 wide_int warn_value
= wi::zero (1);
4178 fprintf (dump_file
, "Comparing enum %s\n", name
);
4180 /* Look for differences which we will warn about later once locations
4182 for (unsigned j
= 0; j
< nvals
; j
++)
4184 const char *id
= streamer_read_string (data_in
, &ib
);
4185 wide_int val
= streamer_read_wide_int (&ib
);
4187 if (do_warning
!= -1 || j
>= this_enum
.vals
.length ())
4189 if (strcmp (id
, this_enum
.vals
[j
].name
)
4190 || val
!= this_enum
.vals
[j
].val
)
4192 warn_name
= xstrdup (id
);
4196 fprintf (dump_file
, " Different on entry %i\n", j
);
4200 /* Stream in locations, but do not apply them unless we are going
4202 bitpack_d bp
= streamer_read_bitpack (&ib
);
4205 stream_input_location (&locus
, &bp
, data_in
);
4207 /* Did we find a difference? */
4208 if (do_warning
!= -1 || nvals
!= this_enum
.vals
.length ())
4210 data_in
->location_cache
.apply_location_cache ();
4212 const int opts
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4213 char *dmgname
= cplus_demangle (name
, opts
);
4214 if (this_enum
.warned
4215 || !warning_at (this_enum
.locus
,
4216 OPT_Wodr
, "type %qs violates the "
4217 "C++ One Definition Rule",
4222 this_enum
.warned
= true;
4223 if (do_warning
== -1)
4225 "an enum with different number of values is defined"
4226 " in another translation unit");
4229 "an enum with different value name"
4230 " is defined in another translation unit");
4233 "an enum with different values"
4234 " is defined in another translation unit");
4238 data_in
->location_cache
.revert_location_cache ();
4240 /* Finally look up for location of the actual value that diverged. */
4241 for (unsigned j
= 0; j
< nvals
; j
++)
4243 location_t id_locus
;
4245 data_in
->location_cache
.revert_location_cache ();
4246 stream_input_location (&id_locus
, &bp
, data_in
);
4248 if ((int) j
== do_warning
)
4250 data_in
->location_cache
.apply_location_cache ();
4252 if (strcmp (warn_name
, this_enum
.vals
[j
].name
))
4253 inform (this_enum
.vals
[j
].locus
,
4254 "name %qs differs from name %qs defined"
4255 " in another translation unit",
4256 this_enum
.vals
[j
].name
, warn_name
);
4257 /* FIXME: In case there is easy way to print wide_ints,
4258 perhaps we could do it here instead of overlfow checpl. */
4259 else if (wi::fits_shwi_p (this_enum
.vals
[j
].val
)
4260 && wi::fits_shwi_p (warn_value
))
4261 inform (this_enum
.vals
[j
].locus
,
4262 "name %qs is defined to " HOST_WIDE_INT_PRINT_DEC
4263 " while another translation unit defines "
4264 "it as " HOST_WIDE_INT_PRINT_DEC
,
4265 warn_name
, this_enum
.vals
[j
].val
.to_shwi (),
4266 warn_value
.to_shwi ());
4268 inform (this_enum
.vals
[j
].locus
,
4269 "name %qs is defined to different value "
4270 "in another translation unit",
4274 "mismatching definition");
4277 data_in
->location_cache
.revert_location_cache ();
4281 obstack_free (&odr_enum_obstack
, name
);
4284 lto_free_section_data (file_data
, LTO_section_ipa_fn_summary
, NULL
, data
,
4286 lto_data_in_delete (data_in
);
4289 /* Read all ODR type sections. */
4292 ipa_odr_summary_read (void)
4294 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
4295 struct lto_file_decl_data
*file_data
;
4298 while ((file_data
= file_data_vec
[j
++]))
4302 = lto_get_summary_section_data (file_data
, LTO_section_odr_types
,
4305 ipa_odr_read_section (file_data
, data
, len
);
4307 /* Enum info is used only to produce warnings. Only case we will need it
4308 again is streaming for incremental LTO. */
4309 if (flag_incremental_link
!= INCREMENTAL_LINK_LTO
)
4311 delete odr_enum_map
;
4312 obstack_free (&odr_enum_obstack
, NULL
);
4313 odr_enum_map
= NULL
;
4319 const pass_data pass_data_ipa_odr
=
4321 IPA_PASS
, /* type */
4323 OPTGROUP_NONE
, /* optinfo_flags */
4324 TV_IPA_ODR
, /* tv_id */
4325 0, /* properties_required */
4326 0, /* properties_provided */
4327 0, /* properties_destroyed */
4328 0, /* todo_flags_start */
4329 0, /* todo_flags_finish */
4332 class pass_ipa_odr
: public ipa_opt_pass_d
4335 pass_ipa_odr (gcc::context
*ctxt
)
4336 : ipa_opt_pass_d (pass_data_ipa_odr
, ctxt
,
4337 NULL
, /* generate_summary */
4338 ipa_odr_summary_write
, /* write_summary */
4339 ipa_odr_summary_read
, /* read_summary */
4340 NULL
, /* write_optimization_summary */
4341 NULL
, /* read_optimization_summary */
4342 NULL
, /* stmt_fixup */
4343 0, /* function_transform_todo_flags_start */
4344 NULL
, /* function_transform */
4345 NULL
) /* variable_transform */
4348 /* opt_pass methods: */
4349 virtual bool gate (function
*)
4351 return (in_lto_p
|| flag_lto
);
4354 virtual unsigned int execute (function
*)
4359 }; // class pass_ipa_odr
4364 make_pass_ipa_odr (gcc::context
*ctxt
)
4366 return new pass_ipa_odr (ctxt
);
4370 #include "gt-ipa-devirt.h"