1 /* Basic IPA utilities for type inheritance graph construction and
3 Copyright (C) 2013-2024 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-iterator.h"
124 #include "gimple-fold.h"
125 #include "symbol-summary.h"
126 #include "tree-vrp.h"
129 #include "ipa-prop.h"
130 #include "ipa-fnsummary.h"
131 #include "demangle.h"
133 #include "gimple-pretty-print.h"
135 #include "stringpool.h"
137 #include "data-streamer.h"
138 #include "lto-streamer.h"
139 #include "streamer-hooks.h"
141 /* Hash based set of pairs of types. */
149 struct default_hash_traits
<type_pair
>
150 : typed_noop_remove
<type_pair
>
152 GTY((skip
)) typedef type_pair value_type
;
153 GTY((skip
)) typedef type_pair compare_type
;
157 return TYPE_UID (p
.first
) ^ TYPE_UID (p
.second
);
159 static const bool empty_zero_p
= true;
161 is_empty (type_pair p
)
163 return p
.first
== NULL
;
166 is_deleted (type_pair p ATTRIBUTE_UNUSED
)
171 equal (const type_pair
&a
, const type_pair
&b
)
173 return a
.first
==b
.first
&& a
.second
== b
.second
;
176 mark_empty (type_pair
&e
)
182 /* HACK alert: this is used to communicate with ipa-inline-transform that
183 thunk is being expanded and there is no need to clear the polymorphic
184 call target cache. */
185 bool thunk_expansion
;
187 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
188 hash_set
<type_pair
> *,
189 location_t
, location_t
);
190 static void warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
191 bool warn
, bool *warned
, const char *reason
);
193 static bool odr_violation_reported
= false;
196 /* Pointer set of all call targets appearing in the cache. */
197 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
199 /* The node of type inheritance graph. For each type unique in
200 One Definition Rule (ODR) sense, we produce one node linking all
201 main variants of types equivalent to it, bases and derived types. */
203 struct GTY(()) odr_type_d
207 /* All bases; built only for main variants of types. */
208 vec
<odr_type
> GTY((skip
)) bases
;
209 /* All derived types with virtual methods seen in unit;
210 built only for main variants of types. */
211 vec
<odr_type
> GTY((skip
)) derived_types
;
213 /* All equivalent types, if more than one. */
214 vec
<tree
, va_gc
> *types
;
215 /* Set of all equivalent types, if NON-NULL. */
216 hash_set
<tree
> * GTY((skip
)) types_set
;
218 /* Unique ID indexing the type in odr_types array. */
220 /* Is it in anonymous namespace? */
221 bool anonymous_namespace
;
222 /* Do we know about all derivations of given type? */
223 bool all_derivations_known
;
224 /* Did we report ODR violation here? */
226 /* Set when virtual table without RTTI prevailed table with. */
228 /* Set when the canonical type is determined using the type name. */
232 /* Return TRUE if all derived types of T are known and thus
233 we may consider the walk of derived type complete.
235 This is typically true only for final anonymous namespace types and types
236 defined within functions (that may be COMDAT and thus shared across units,
237 but with the same set of derived types). */
240 type_all_derivations_known_p (const_tree t
)
242 if (TYPE_FINAL_P (t
))
246 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
247 if (!TYPE_NAME (t
) || TREE_CODE (TYPE_NAME (t
)) != TYPE_DECL
)
249 if (type_in_anonymous_namespace_p (t
))
251 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
254 /* Return TRUE if type's constructors are all visible. */
257 type_all_ctors_visible_p (tree t
)
260 && symtab
->state
>= CONSTRUCTION
261 /* We cannot always use type_all_derivations_known_p.
262 For function local types we must assume case where
263 the function is COMDAT and shared in between units.
265 TODO: These cases are quite easy to get, but we need
266 to keep track of C++ privatizing via -Wno-weak
267 as well as the IPA privatizing. */
268 && type_in_anonymous_namespace_p (t
);
271 /* Return TRUE if type may have instance. */
274 type_possibly_instantiated_p (tree t
)
279 /* TODO: Add abstract types here. */
280 if (!type_all_ctors_visible_p (t
))
283 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
284 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
285 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
286 vnode
= varpool_node::get (vtable
);
287 return vnode
&& vnode
->definition
;
290 /* Return true if T or type derived from T may have instance. */
293 type_or_derived_type_possibly_instantiated_p (odr_type t
)
295 if (type_possibly_instantiated_p (t
->type
))
297 for (auto derived
: t
->derived_types
)
298 if (type_or_derived_type_possibly_instantiated_p (derived
))
303 /* Hash used to unify ODR types based on their mangled name and for anonymous
306 struct odr_name_hasher
: pointer_hash
<odr_type_d
>
308 typedef union tree_node
*compare_type
;
309 static inline hashval_t
hash (const odr_type_d
*);
310 static inline bool equal (const odr_type_d
*, const tree_node
*);
311 static inline void remove (odr_type_d
*);
315 can_be_name_hashed_p (tree t
)
317 return (!in_lto_p
|| odr_type_p (t
));
320 /* Hash type by its ODR name. */
323 hash_odr_name (const_tree t
)
325 gcc_checking_assert (TYPE_MAIN_VARIANT (t
) == t
);
327 /* If not in LTO, all main variants are unique, so we can do
330 return htab_hash_pointer (t
);
332 /* Anonymous types are unique. */
333 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
334 return htab_hash_pointer (t
);
336 gcc_checking_assert (TYPE_NAME (t
)
337 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
)));
338 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t
)));
341 /* Return the computed hashcode for ODR_TYPE. */
344 odr_name_hasher::hash (const odr_type_d
*odr_type
)
346 return hash_odr_name (odr_type
->type
);
349 /* For languages with One Definition Rule, work out if
350 types are the same based on their name.
352 This is non-trivial for LTO where minor differences in
353 the type representation may have prevented type merging
354 to merge two copies of otherwise equivalent type.
356 Until we start streaming mangled type names, this function works
357 only for polymorphic types.
361 types_same_for_odr (const_tree type1
, const_tree type2
)
363 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
365 type1
= TYPE_MAIN_VARIANT (type1
);
366 type2
= TYPE_MAIN_VARIANT (type2
);
374 /* Anonymous namespace types are never duplicated. */
375 if ((type_with_linkage_p (type1
) && type_in_anonymous_namespace_p (type1
))
376 || (type_with_linkage_p (type2
) && type_in_anonymous_namespace_p (type2
)))
379 /* If both type has mangled defined check if they are same.
380 Watch for anonymous types which are all mangled as "<anon">. */
381 if (!type_with_linkage_p (type1
) || !type_with_linkage_p (type2
))
383 if (type_in_anonymous_namespace_p (type1
)
384 || type_in_anonymous_namespace_p (type2
))
386 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1
))
387 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2
)));
390 /* Return true if we can decide on ODR equivalency.
392 In non-LTO it is always decide, in LTO however it depends in the type has
393 ODR info attached. */
396 types_odr_comparable (tree t1
, tree t2
)
399 || TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
)
400 || (odr_type_p (TYPE_MAIN_VARIANT (t1
))
401 && odr_type_p (TYPE_MAIN_VARIANT (t2
))));
404 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
405 known, be conservative and return false. */
408 types_must_be_same_for_odr (tree t1
, tree t2
)
410 if (types_odr_comparable (t1
, t2
))
411 return types_same_for_odr (t1
, t2
);
413 return TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
);
416 /* If T is compound type, return type it is based on. */
419 compound_type_base (const_tree t
)
421 if (TREE_CODE (t
) == ARRAY_TYPE
422 || POINTER_TYPE_P (t
)
423 || TREE_CODE (t
) == COMPLEX_TYPE
424 || VECTOR_TYPE_P (t
))
425 return TREE_TYPE (t
);
426 if (TREE_CODE (t
) == METHOD_TYPE
)
427 return TYPE_METHOD_BASETYPE (t
);
428 if (TREE_CODE (t
) == OFFSET_TYPE
)
429 return TYPE_OFFSET_BASETYPE (t
);
433 /* Return true if T is either ODR type or compound type based from it.
434 If the function return true, we know that T is a type originating from C++
435 source even at link-time. */
438 odr_or_derived_type_p (const_tree t
)
442 if (odr_type_p (TYPE_MAIN_VARIANT (t
)))
444 /* Function type is a tricky one. Basically we can consider it
445 ODR derived if return type or any of the parameters is.
446 We need to check all parameters because LTO streaming merges
447 common types (such as void) and they are not considered ODR then. */
448 if (TREE_CODE (t
) == FUNCTION_TYPE
)
450 if (TYPE_METHOD_BASETYPE (t
))
451 t
= TYPE_METHOD_BASETYPE (t
);
454 if (TREE_TYPE (t
) && odr_or_derived_type_p (TREE_TYPE (t
)))
456 for (t
= TYPE_ARG_TYPES (t
); t
; t
= TREE_CHAIN (t
))
457 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t
))))
463 t
= compound_type_base (t
);
469 /* Compare types T1 and T2 and return true if they are
473 odr_name_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
477 gcc_checking_assert (TYPE_MAIN_VARIANT (t2
) == t2
);
478 gcc_checking_assert (TYPE_MAIN_VARIANT (t1
) == t1
);
483 /* Check for anonymous namespaces. */
484 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
485 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
487 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
)));
488 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
489 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
490 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
493 /* Free ODR type V. */
496 odr_name_hasher::remove (odr_type_d
*v
)
499 v
->derived_types
.release ();
505 /* ODR type hash used to look up ODR type based on tree type node. */
507 typedef hash_table
<odr_name_hasher
> odr_hash_type
;
508 static odr_hash_type
*odr_hash
;
510 /* ODR types are also stored into ODR_TYPE vector to allow consistent
511 walking. Bases appear before derived types. Vector is garbage collected
512 so we won't end up visiting empty types. */
514 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
515 #define odr_types (*odr_types_ptr)
517 /* All enums defined and accessible for the unit. */
518 static GTY(()) vec
<tree
, va_gc
> *odr_enums
;
520 /* Information we hold about value defined by an enum type. */
528 /* Information about enum values. */
532 auto_vec
<odr_enum_val
, 0> vals
;
536 /* A table of all ODR enum definitions. */
537 static hash_map
<nofree_string_hash
, odr_enum
> *odr_enum_map
= NULL
;
538 static struct obstack odr_enum_obstack
;
540 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
542 set_type_binfo (tree type
, tree binfo
)
544 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
545 if (COMPLETE_TYPE_P (type
))
546 TYPE_BINFO (type
) = binfo
;
548 gcc_assert (!TYPE_BINFO (type
));
551 /* Return true if type variants match.
552 This assumes that we already verified that T1 and T2 are variants of the
556 type_variants_equivalent_p (tree t1
, tree t2
)
558 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
561 if (comp_type_attributes (t1
, t2
) != 1)
564 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
)
565 && TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
))
571 /* Compare T1 and T2 based on name or structure. */
574 odr_subtypes_equivalent_p (tree t1
, tree t2
,
575 hash_set
<type_pair
> *visited
,
576 location_t loc1
, location_t loc2
)
579 /* This can happen in incomplete types that should be handled earlier. */
580 gcc_assert (t1
&& t2
);
585 /* Anonymous namespace types must match exactly. */
586 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
587 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
588 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
589 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
592 /* For ODR types be sure to compare their names.
593 To support -Wno-odr-type-merging we allow one type to be non-ODR
594 and other ODR even though it is a violation. */
595 if (types_odr_comparable (t1
, t2
))
598 && odr_type_p (TYPE_MAIN_VARIANT (t1
))
599 && get_odr_type (TYPE_MAIN_VARIANT (t1
), true)->odr_violated
)
601 if (!types_same_for_odr (t1
, t2
))
603 if (!type_variants_equivalent_p (t1
, t2
))
605 /* Limit recursion: If subtypes are ODR types and we know
606 that they are same, be happy. */
607 if (odr_type_p (TYPE_MAIN_VARIANT (t1
)))
611 /* Component types, builtins and possibly violating ODR types
612 have to be compared structurally. */
613 if (TREE_CODE (t1
) != TREE_CODE (t2
))
615 if (AGGREGATE_TYPE_P (t1
)
616 && (TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
619 type_pair pair
={TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
)};
620 if (TYPE_UID (TYPE_MAIN_VARIANT (t1
)) > TYPE_UID (TYPE_MAIN_VARIANT (t2
)))
622 pair
.first
= TYPE_MAIN_VARIANT (t2
);
623 pair
.second
= TYPE_MAIN_VARIANT (t1
);
625 if (visited
->add (pair
))
627 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
),
628 false, NULL
, visited
, loc1
, loc2
))
630 if (!type_variants_equivalent_p (t1
, t2
))
635 /* Return true if DECL1 and DECL2 are identical methods. Consider
636 name equivalent to name.localalias.xyz. */
639 methods_equal_p (tree decl1
, tree decl2
)
641 if (DECL_ASSEMBLER_NAME (decl1
) == DECL_ASSEMBLER_NAME (decl2
))
643 const char sep
= symbol_table::symbol_suffix_separator ();
645 const char *name1
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1
));
646 const char *ptr1
= strchr (name1
, sep
);
647 int len1
= ptr1
? ptr1
- name1
: strlen (name1
);
649 const char *name2
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2
));
650 const char *ptr2
= strchr (name2
, sep
);
651 int len2
= ptr2
? ptr2
- name2
: strlen (name2
);
655 return !strncmp (name1
, name2
, len1
);
658 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
659 violation warnings. */
662 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
666 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
668 odr_violation_reported
= true;
669 if (DECL_VIRTUAL_P (prevailing
->decl
))
671 varpool_node
*tmp
= prevailing
;
675 auto_diagnostic_group d
;
676 if (warning_at (DECL_SOURCE_LOCATION
677 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
679 "virtual table of type %qD violates one definition rule",
680 DECL_CONTEXT (vtable
->decl
)))
681 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
682 "variable of same assembler name as the virtual table is "
683 "defined in another translation unit");
686 if (!prevailing
->definition
|| !vtable
->definition
)
689 /* If we do not stream ODR type info, do not bother to do useful compare. */
690 if (!TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))
691 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))))
694 odr_type class_type
= get_odr_type (DECL_CONTEXT (vtable
->decl
), true);
696 if (class_type
->odr_violated
)
699 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
701 struct ipa_ref
*ref1
, *ref2
;
704 end1
= !prevailing
->iterate_reference (n1
, ref1
);
705 end2
= !vtable
->iterate_reference (n2
, ref2
);
707 /* !DECL_VIRTUAL_P means RTTI entry;
708 We warn when RTTI is lost because non-RTTI prevails; we silently
709 accept the other case. */
712 || (methods_equal_p (ref1
->referred
->decl
,
713 ref2
->referred
->decl
)
714 && TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
))
715 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
717 if (!class_type
->rtti_broken
)
719 auto_diagnostic_group d
;
720 if (warning_at (DECL_SOURCE_LOCATION
721 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
723 "virtual table of type %qD contains RTTI "
725 DECL_CONTEXT (vtable
->decl
)))
727 inform (DECL_SOURCE_LOCATION
728 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
729 "but is prevailed by one without from other"
730 " translation unit");
731 inform (DECL_SOURCE_LOCATION
732 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
733 "RTTI will not work on this type");
734 class_type
->rtti_broken
= true;
738 end2
= !vtable
->iterate_reference (n2
, ref2
);
742 || (methods_equal_p (ref2
->referred
->decl
, ref1
->referred
->decl
)
743 && TREE_CODE (ref2
->referred
->decl
) == FUNCTION_DECL
))
744 && TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
)
747 end1
= !prevailing
->iterate_reference (n1
, ref1
);
753 /* Extra paranoia; compare the sizes. We do not have information
754 about virtual inheritance offsets, so just be sure that these
756 Do this as very last check so the not very informative error
757 is not output too often. */
758 if (DECL_SIZE (prevailing
->decl
) != DECL_SIZE (vtable
->decl
))
760 class_type
->odr_violated
= true;
761 auto_diagnostic_group d
;
762 tree ctx
= TYPE_NAME (DECL_CONTEXT (vtable
->decl
));
763 if (warning_at (DECL_SOURCE_LOCATION (ctx
), OPT_Wodr
,
764 "virtual table of type %qD violates "
765 "one definition rule",
766 DECL_CONTEXT (vtable
->decl
)))
768 ctx
= TYPE_NAME (DECL_CONTEXT (prevailing
->decl
));
769 inform (DECL_SOURCE_LOCATION (ctx
),
770 "the conflicting type defined in another translation"
771 " unit has virtual table of different size");
779 if (methods_equal_p (ref1
->referred
->decl
, ref2
->referred
->decl
))
782 class_type
->odr_violated
= true;
784 /* If the loops above stopped on non-virtual pointer, we have
785 mismatch in RTTI information mangling. */
786 if (TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
787 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
789 auto_diagnostic_group d
;
790 if (warning_at (DECL_SOURCE_LOCATION
791 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
793 "virtual table of type %qD violates "
794 "one definition rule",
795 DECL_CONTEXT (vtable
->decl
)))
797 inform (DECL_SOURCE_LOCATION
798 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
799 "the conflicting type defined in another translation "
800 "unit with different RTTI information");
804 /* At this point both REF1 and REF2 points either to virtual table
805 or virtual method. If one points to virtual table and other to
806 method we can complain the same way as if one table was shorter
807 than other pointing out the extra method. */
808 if (TREE_CODE (ref1
->referred
->decl
)
809 != TREE_CODE (ref2
->referred
->decl
))
811 if (VAR_P (ref1
->referred
->decl
))
813 else if (VAR_P (ref2
->referred
->decl
))
818 class_type
->odr_violated
= true;
820 /* Complain about size mismatch. Either we have too many virtual
821 functions or too many virtual table pointers. */
826 varpool_node
*tmp
= prevailing
;
831 auto_diagnostic_group d
;
832 if (warning_at (DECL_SOURCE_LOCATION
833 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
835 "virtual table of type %qD violates "
836 "one definition rule",
837 DECL_CONTEXT (vtable
->decl
)))
839 if (TREE_CODE (ref1
->referring
->decl
) == FUNCTION_DECL
)
841 inform (DECL_SOURCE_LOCATION
842 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
843 "the conflicting type defined in another translation "
845 inform (DECL_SOURCE_LOCATION
846 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
847 "contains additional virtual method %qD",
848 ref1
->referred
->decl
);
852 inform (DECL_SOURCE_LOCATION
853 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
854 "the conflicting type defined in another translation "
855 "unit has virtual table with more entries");
861 /* And in the last case we have either mismatch in between two virtual
862 methods or two virtual table pointers. */
863 auto_diagnostic_group d
;
864 if (warning_at (DECL_SOURCE_LOCATION
865 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), OPT_Wodr
,
866 "virtual table of type %qD violates "
867 "one definition rule",
868 DECL_CONTEXT (vtable
->decl
)))
870 if (TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
)
872 inform (DECL_SOURCE_LOCATION
873 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
874 "the conflicting type defined in another translation "
876 gcc_assert (TREE_CODE (ref2
->referred
->decl
)
878 inform (DECL_SOURCE_LOCATION
879 (ref1
->referred
->ultimate_alias_target ()->decl
),
880 "virtual method %qD",
881 ref1
->referred
->ultimate_alias_target ()->decl
);
882 inform (DECL_SOURCE_LOCATION
883 (ref2
->referred
->ultimate_alias_target ()->decl
),
884 "ought to match virtual method %qD but does not",
885 ref2
->referred
->ultimate_alias_target ()->decl
);
888 inform (DECL_SOURCE_LOCATION
889 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
890 "the conflicting type defined in another translation "
891 "unit has virtual table with different contents");
897 /* Output ODR violation warning about T1 and T2 with REASON.
898 Display location of ST1 and ST2 if REASON speaks about field or
900 If WARN is false, do nothing. Set WARNED if warning was indeed
904 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
905 bool warn
, bool *warned
, const char *reason
)
907 tree decl2
= TYPE_NAME (TYPE_MAIN_VARIANT (t2
));
911 if (!warn
|| !TYPE_NAME(TYPE_MAIN_VARIANT (t1
)))
914 /* ODR warnings are output during LTO streaming; we must apply location
915 cache for potential warnings to be output correctly. */
916 if (lto_location_cache::current_cache
)
917 lto_location_cache::current_cache
->apply_location_cache ();
919 auto_diagnostic_group d
;
920 if (t1
!= TYPE_MAIN_VARIANT (t1
)
921 && TYPE_NAME (t1
) != TYPE_NAME (TYPE_MAIN_VARIANT (t1
)))
923 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1
))),
924 OPT_Wodr
, "type %qT (typedef of %qT) violates the "
925 "C++ One Definition Rule",
926 t1
, TYPE_MAIN_VARIANT (t1
)))
931 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1
))),
932 OPT_Wodr
, "type %qT violates the C++ One Definition Rule",
938 /* For FIELD_DECL support also case where one of fields is
939 NULL - this is used when the structures have mismatching number of
941 else if (!st1
|| TREE_CODE (st1
) == FIELD_DECL
)
943 inform (DECL_SOURCE_LOCATION (decl2
),
944 "a different type is defined in another translation unit");
950 inform (DECL_SOURCE_LOCATION (st1
),
951 "the first difference of corresponding definitions is field %qD",
956 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
958 inform (DECL_SOURCE_LOCATION (decl2
),
959 "a different type is defined in another translation unit");
960 inform (DECL_SOURCE_LOCATION (st1
),
961 "the first difference of corresponding definitions is method %qD",
967 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
973 /* Return true if T1 and T2 are incompatible and we want to recursively
974 dive into them from warn_type_mismatch to give sensible answer. */
977 type_mismatch_p (tree t1
, tree t2
)
979 if (odr_or_derived_type_p (t1
) && odr_or_derived_type_p (t2
)
980 && !odr_types_equivalent_p (t1
, t2
))
982 return !types_compatible_p (t1
, t2
);
986 /* Types T1 and T2 was found to be incompatible in a context they can't
987 (either used to declare a symbol of same assembler name or unified by
988 ODR rule). We already output warning about this, but if possible, output
989 extra information on how the types mismatch.
991 This is hard to do in general. We basically handle the common cases.
993 If LOC1 and LOC2 are meaningful locations, use it in the case the types
994 themselves do not have one. */
997 warn_types_mismatch (tree t1
, tree t2
, location_t loc1
, location_t loc2
)
999 /* Location of type is known only if it has TYPE_NAME and the name is
1001 location_t loc_t1
= TYPE_NAME (t1
) && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1002 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1
))
1004 location_t loc_t2
= TYPE_NAME (t2
) && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1005 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2
))
1007 bool loc_t2_useful
= false;
1009 /* With LTO it is a common case that the location of both types match.
1010 See if T2 has a location that is different from T1. If so, we will
1011 inform user about the location.
1012 Do not consider the location passed to us in LOC1/LOC2 as those are
1014 if (loc_t2
> BUILTINS_LOCATION
&& loc_t2
!= loc_t1
)
1016 if (loc_t1
<= BUILTINS_LOCATION
)
1017 loc_t2_useful
= true;
1020 expanded_location xloc1
= expand_location (loc_t1
);
1021 expanded_location xloc2
= expand_location (loc_t2
);
1023 if (strcmp (xloc1
.file
, xloc2
.file
)
1024 || xloc1
.line
!= xloc2
.line
1025 || xloc1
.column
!= xloc2
.column
)
1026 loc_t2_useful
= true;
1030 if (loc_t1
<= BUILTINS_LOCATION
)
1032 if (loc_t2
<= BUILTINS_LOCATION
)
1035 location_t loc
= loc_t1
<= BUILTINS_LOCATION
? loc_t2
: loc_t1
;
1037 /* It is a quite common bug to reference anonymous namespace type in
1038 non-anonymous namespace class. */
1039 tree mt1
= TYPE_MAIN_VARIANT (t1
);
1040 tree mt2
= TYPE_MAIN_VARIANT (t2
);
1041 if ((type_with_linkage_p (mt1
)
1042 && type_in_anonymous_namespace_p (mt1
))
1043 || (type_with_linkage_p (mt2
)
1044 && type_in_anonymous_namespace_p (mt2
)))
1046 if (!type_with_linkage_p (mt1
)
1047 || !type_in_anonymous_namespace_p (mt1
))
1050 std::swap (mt1
, mt2
);
1051 std::swap (loc_t1
, loc_t2
);
1053 gcc_assert (TYPE_NAME (mt1
)
1054 && TREE_CODE (TYPE_NAME (mt1
)) == TYPE_DECL
);
1055 tree n1
= TYPE_NAME (mt1
);
1056 tree n2
= TYPE_NAME (mt2
) ? TYPE_NAME (mt2
) : NULL
;
1058 if (TREE_CODE (n1
) == TYPE_DECL
)
1059 n1
= DECL_NAME (n1
);
1060 if (n2
&& TREE_CODE (n2
) == TYPE_DECL
)
1061 n2
= DECL_NAME (n2
);
1062 /* Most of the time, the type names will match, do not be unnecessarily
1066 "type %qT defined in anonymous namespace cannot match "
1067 "type %qT across the translation unit boundary",
1071 "type %qT defined in anonymous namespace cannot match "
1072 "across the translation unit boundary",
1076 "the incompatible type defined in another translation unit");
1079 /* If types have mangled ODR names and they are different, it is most
1080 informative to output those.
1081 This also covers types defined in different namespaces. */
1082 const char *odr1
= get_odr_name_for_type (mt1
);
1083 const char *odr2
= get_odr_name_for_type (mt2
);
1084 if (odr1
!= NULL
&& odr2
!= NULL
&& odr1
!= odr2
)
1086 const int opts
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
1087 char *name1
= xstrdup (cplus_demangle (odr1
, opts
));
1088 char *name2
= cplus_demangle (odr2
, opts
);
1089 if (name1
&& name2
&& strcmp (name1
, name2
))
1092 "type name %qs should match type name %qs",
1096 "the incompatible type is defined here");
1102 /* A tricky case are compound types. Often they appear the same in source
1103 code and the mismatch is dragged in by type they are build from.
1104 Look for those differences in subtypes and try to be informative. In other
1105 cases just output nothing because the source code is probably different
1106 and in this case we already output a all necessary info. */
1107 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
1109 if (TREE_CODE (t1
) == TREE_CODE (t2
))
1111 if (TREE_CODE (t1
) == ARRAY_TYPE
1112 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1114 tree i1
= TYPE_DOMAIN (t1
);
1115 tree i2
= TYPE_DOMAIN (t2
);
1118 && TYPE_MAX_VALUE (i1
)
1119 && TYPE_MAX_VALUE (i2
)
1120 && !operand_equal_p (TYPE_MAX_VALUE (i1
),
1121 TYPE_MAX_VALUE (i2
), 0))
1124 "array types have different bounds");
1128 if ((POINTER_TYPE_P (t1
) || TREE_CODE (t1
) == ARRAY_TYPE
)
1129 && type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1130 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
, loc_t2
);
1131 else if (TREE_CODE (t1
) == METHOD_TYPE
1132 || TREE_CODE (t1
) == FUNCTION_TYPE
)
1134 tree parms1
= NULL
, parms2
= NULL
;
1137 if (type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1139 inform (loc
, "return value type mismatch");
1140 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
,
1144 if (prototype_p (t1
) && prototype_p (t2
))
1145 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1147 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
),
1150 if (type_mismatch_p (TREE_VALUE (parms1
), TREE_VALUE (parms2
)))
1152 if (count
== 1 && TREE_CODE (t1
) == METHOD_TYPE
)
1154 "implicit this pointer type mismatch");
1157 "type mismatch in parameter %i",
1158 count
- (TREE_CODE (t1
) == METHOD_TYPE
));
1159 warn_types_mismatch (TREE_VALUE (parms1
),
1160 TREE_VALUE (parms2
),
1165 if (parms1
|| parms2
)
1168 "types have different parameter counts");
1176 if (types_odr_comparable (t1
, t2
)
1177 /* We make assign integers mangled names to be able to handle
1178 signed/unsigned chars. Accepting them here would however lead to
1179 confusing message like
1180 "type ‘const int’ itself violates the C++ One Definition Rule" */
1181 && TREE_CODE (t1
) != INTEGER_TYPE
1182 && types_same_for_odr (t1
, t2
))
1184 "type %qT itself violates the C++ One Definition Rule", t1
);
1185 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1186 else if (TYPE_NAME (t1
) == TYPE_NAME (t2
)
1187 && TREE_CODE (t1
) == TREE_CODE (t2
) && !loc_t2_useful
)
1190 inform (loc_t1
, "type %qT should match type %qT",
1193 inform (loc_t2
, "the incompatible type is defined here");
1196 /* Return true if T should be ignored in TYPE_FIELDS for ODR comparison. */
1199 skip_in_fields_list_p (tree t
)
1201 if (TREE_CODE (t
) != FIELD_DECL
)
1203 /* C++ FE introduces zero sized fields depending on -std setting, see
1206 && integer_zerop (DECL_SIZE (t
))
1207 && DECL_ARTIFICIAL (t
)
1208 && DECL_IGNORED_P (t
)
1214 /* Compare T1 and T2, report ODR violations if WARN is true and set
1215 WARNED to true if anything is reported. Return true if types match.
1216 If true is returned, the types are also compatible in the sense of
1217 gimple_canonical_types_compatible_p.
1218 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1219 about the type if the type itself do not have location. */
1222 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
,
1223 hash_set
<type_pair
> *visited
,
1224 location_t loc1
, location_t loc2
)
1226 /* If we are asked to warn, we need warned to keep track if warning was
1228 gcc_assert (!warn
|| warned
);
1229 /* Check first for the obvious case of pointer identity. */
1233 /* Can't be the same type if the types don't have the same code. */
1234 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1236 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1237 G_("a different type is defined in another translation unit"));
1241 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
1242 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
1243 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
1244 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
1246 /* We cannot trip this when comparing ODR types, only when trying to
1247 match different ODR derivations from different declarations.
1248 So WARN should be always false. */
1253 /* Non-aggregate types can be handled cheaply. */
1254 if (INTEGRAL_TYPE_P (t1
)
1255 || SCALAR_FLOAT_TYPE_P (t1
)
1256 || FIXED_POINT_TYPE_P (t1
)
1257 || VECTOR_TYPE_P (t1
)
1258 || TREE_CODE (t1
) == COMPLEX_TYPE
1259 || TREE_CODE (t1
) == OFFSET_TYPE
1260 || POINTER_TYPE_P (t1
))
1262 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
1264 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1265 G_("a type with different precision is defined "
1266 "in another translation unit"));
1269 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
1271 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1272 G_("a type with different signedness is defined "
1273 "in another translation unit"));
1277 if (TREE_CODE (t1
) == INTEGER_TYPE
1278 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
1280 /* char WRT uint_8? */
1281 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1282 G_("a different type is defined in another "
1283 "translation unit"));
1287 /* For canonical type comparisons we do not want to build SCCs
1288 so we cannot compare pointed-to types. But we can, for now,
1289 require the same pointed-to type kind and match what
1290 useless_type_conversion_p would do. */
1291 if (POINTER_TYPE_P (t1
))
1293 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
1294 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
1296 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1297 G_("it is defined as a pointer in different address "
1298 "space in another translation unit"));
1302 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1303 visited
, loc1
, loc2
))
1305 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1306 G_("it is defined as a pointer to different type "
1307 "in another translation unit"));
1308 if (warn
&& *warned
)
1309 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
),
1315 if ((VECTOR_TYPE_P (t1
) || TREE_CODE (t1
) == COMPLEX_TYPE
)
1316 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1317 visited
, loc1
, loc2
))
1319 /* Probably specific enough. */
1320 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1321 G_("a different type is defined "
1322 "in another translation unit"));
1323 if (warn
&& *warned
)
1324 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1328 /* Do type-specific comparisons. */
1329 else switch (TREE_CODE (t1
))
1333 /* Array types are the same if the element types are the same and
1334 the number of elements are the same. */
1335 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1336 visited
, loc1
, loc2
))
1338 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1339 G_("a different type is defined in another "
1340 "translation unit"));
1341 if (warn
&& *warned
)
1342 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1344 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
1345 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
1346 == TYPE_NONALIASED_COMPONENT (t2
));
1348 tree i1
= TYPE_DOMAIN (t1
);
1349 tree i2
= TYPE_DOMAIN (t2
);
1351 /* For an incomplete external array, the type domain can be
1352 NULL_TREE. Check this condition also. */
1353 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
1354 return type_variants_equivalent_p (t1
, t2
);
1356 tree min1
= TYPE_MIN_VALUE (i1
);
1357 tree min2
= TYPE_MIN_VALUE (i2
);
1358 tree max1
= TYPE_MAX_VALUE (i1
);
1359 tree max2
= TYPE_MAX_VALUE (i2
);
1361 /* In C++, minimums should be always 0. */
1362 gcc_assert (min1
== min2
);
1363 if (!operand_equal_p (max1
, max2
, 0))
1365 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1366 G_("an array of different size is defined "
1367 "in another translation unit"));
1375 /* Function types are the same if the return type and arguments types
1377 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1378 visited
, loc1
, loc2
))
1380 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1381 G_("has different return value "
1382 "in another translation unit"));
1383 if (warn
&& *warned
)
1384 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1388 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
)
1389 || !prototype_p (t1
) || !prototype_p (t2
))
1390 return type_variants_equivalent_p (t1
, t2
);
1393 tree parms1
, parms2
;
1395 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1397 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
1399 if (!odr_subtypes_equivalent_p
1400 (TREE_VALUE (parms1
), TREE_VALUE (parms2
),
1401 visited
, loc1
, loc2
))
1403 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1404 G_("has different parameters in another "
1405 "translation unit"));
1406 if (warn
&& *warned
)
1407 warn_types_mismatch (TREE_VALUE (parms1
),
1408 TREE_VALUE (parms2
), loc1
, loc2
);
1413 if (parms1
|| parms2
)
1415 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1416 G_("has different parameters "
1417 "in another translation unit"));
1421 return type_variants_equivalent_p (t1
, t2
);
1426 case QUAL_UNION_TYPE
:
1430 /* For aggregate types, all the fields must be the same. */
1431 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1433 if (TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
1434 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
1435 != polymorphic_type_binfo_p (TYPE_BINFO (t2
)))
1437 if (polymorphic_type_binfo_p (TYPE_BINFO (t1
)))
1438 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1439 G_("a type defined in another translation unit "
1440 "is not polymorphic"));
1442 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1443 G_("a type defined in another translation unit "
1447 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1449 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1451 /* Skip non-fields. */
1452 while (f1
&& skip_in_fields_list_p (f1
))
1453 f1
= TREE_CHAIN (f1
);
1454 while (f2
&& skip_in_fields_list_p (f2
))
1455 f2
= TREE_CHAIN (f2
);
1458 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1460 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1461 G_("a type with different virtual table pointers"
1462 " is defined in another translation unit"));
1465 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
1467 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1468 G_("a type with different bases is defined "
1469 "in another translation unit"));
1472 if (DECL_NAME (f1
) != DECL_NAME (f2
)
1473 && !DECL_ARTIFICIAL (f1
))
1475 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1476 G_("a field with different name is defined "
1477 "in another translation unit"));
1480 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1482 visited
, loc1
, loc2
))
1484 /* Do not warn about artificial fields and just go into
1485 generic field mismatch warning. */
1486 if (DECL_ARTIFICIAL (f1
))
1489 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1490 G_("a field of same name but different type "
1491 "is defined in another translation unit"));
1492 if (warn
&& *warned
)
1493 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
), loc1
, loc2
);
1496 if (!gimple_compare_field_offset (f1
, f2
))
1498 /* Do not warn about artificial fields and just go into
1499 generic field mismatch warning. */
1500 if (DECL_ARTIFICIAL (f1
))
1502 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1503 G_("fields have different layout "
1504 "in another translation unit"));
1507 if (DECL_BIT_FIELD (f1
) != DECL_BIT_FIELD (f2
))
1509 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1510 G_("one field is a bitfield while the other "
1515 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
1516 == DECL_NONADDRESSABLE_P (f2
));
1519 /* If one aggregate has more fields than the other, they
1520 are not the same. */
1523 if ((f1
&& DECL_VIRTUAL_P (f1
)) || (f2
&& DECL_VIRTUAL_P (f2
)))
1524 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1525 G_("a type with different virtual table pointers"
1526 " is defined in another translation unit"));
1527 else if ((f1
&& DECL_ARTIFICIAL (f1
))
1528 || (f2
&& DECL_ARTIFICIAL (f2
)))
1529 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1530 G_("a type with different bases is defined "
1531 "in another translation unit"));
1533 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1534 G_("a type with different number of fields "
1535 "is defined in another translation unit"));
1552 /* Those are better to come last as they are utterly uninformative. */
1553 if (TYPE_SIZE (t1
) && TYPE_SIZE (t2
)
1554 && !operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0))
1556 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1557 G_("a type with different size "
1558 "is defined in another translation unit"));
1562 if (TREE_ADDRESSABLE (t1
) != TREE_ADDRESSABLE (t2
)
1563 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1565 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1566 G_("one type needs to be constructed while the other does not"));
1567 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (t1
));
1570 /* There is no really good user facing warning for this.
1571 Either the original reason for modes being different is lost during
1572 streaming or we should catch earlier warnings. We however must detect
1573 the mismatch to avoid type verifier from cmplaining on mismatched
1574 types between type and canonical type. See PR91576. */
1575 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1576 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1578 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1579 G_("memory layout mismatch"));
1583 gcc_assert (!TYPE_SIZE_UNIT (t1
) || !TYPE_SIZE_UNIT (t2
)
1584 || operand_equal_p (TYPE_SIZE_UNIT (t1
),
1585 TYPE_SIZE_UNIT (t2
), 0));
1586 return type_variants_equivalent_p (t1
, t2
);
1589 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1592 odr_types_equivalent_p (tree type1
, tree type2
)
1594 gcc_checking_assert (odr_or_derived_type_p (type1
)
1595 && odr_or_derived_type_p (type2
));
1597 hash_set
<type_pair
> visited
;
1598 return odr_types_equivalent_p (type1
, type2
, false, NULL
,
1599 &visited
, UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1602 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1603 from VAL->type. This may happen in LTO where tree merging did not merge
1604 all variants of the same type or due to ODR violation.
1606 Analyze and report ODR violations and add type to duplicate list.
1607 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1608 this is first time we see definition of a class return true so the
1609 base types are analyzed. */
1612 add_type_duplicate (odr_type val
, tree type
)
1614 bool build_bases
= false;
1615 bool prevail
= false;
1616 bool odr_must_violate
= false;
1618 if (!val
->types_set
)
1619 val
->types_set
= new hash_set
<tree
>;
1621 /* Chose polymorphic type as leader (this happens only in case of ODR
1623 if ((TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1624 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
1625 && (TREE_CODE (val
->type
) != RECORD_TYPE
|| !TYPE_BINFO (val
->type
)
1626 || !polymorphic_type_binfo_p (TYPE_BINFO (val
->type
))))
1631 /* Always prefer complete type to be the leader. */
1632 else if (!COMPLETE_TYPE_P (val
->type
) && COMPLETE_TYPE_P (type
))
1635 if (TREE_CODE (type
) == RECORD_TYPE
)
1636 build_bases
= TYPE_BINFO (type
);
1638 else if (COMPLETE_TYPE_P (val
->type
) && !COMPLETE_TYPE_P (type
))
1640 else if (TREE_CODE (val
->type
) == RECORD_TYPE
1641 && TREE_CODE (type
) == RECORD_TYPE
1642 && TYPE_BINFO (type
) && !TYPE_BINFO (val
->type
))
1644 gcc_assert (!val
->bases
.length ());
1650 std::swap (val
->type
, type
);
1652 val
->types_set
->add (type
);
1657 gcc_checking_assert (can_be_name_hashed_p (type
)
1658 && can_be_name_hashed_p (val
->type
));
1661 bool base_mismatch
= false;
1663 bool warned
= false;
1664 hash_set
<type_pair
> visited
;
1666 gcc_assert (in_lto_p
);
1667 vec_safe_push (val
->types
, type
);
1669 /* If both are class types, compare the bases. */
1670 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1671 && TREE_CODE (val
->type
) == RECORD_TYPE
1672 && TREE_CODE (type
) == RECORD_TYPE
1673 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1675 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1676 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1678 if (!flag_ltrans
&& !warned
&& !val
->odr_violated
)
1681 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1682 "a type with the same name but different "
1683 "number of polymorphic bases is "
1684 "defined in another translation unit");
1687 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1688 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1689 extra_base
= BINFO_BASE_BINFO
1691 BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)));
1693 extra_base
= BINFO_BASE_BINFO
1694 (TYPE_BINFO (val
->type
),
1695 BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1696 tree extra_base_type
= BINFO_TYPE (extra_base
);
1697 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type
)),
1698 "the extra base is defined here");
1701 base_mismatch
= true;
1704 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1706 tree base1
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
1707 tree base2
= BINFO_BASE_BINFO (TYPE_BINFO (val
->type
), i
);
1708 tree type1
= BINFO_TYPE (base1
);
1709 tree type2
= BINFO_TYPE (base2
);
1711 if (types_odr_comparable (type1
, type2
))
1713 if (!types_same_for_odr (type1
, type2
))
1714 base_mismatch
= true;
1717 if (!odr_types_equivalent_p (type1
, type2
))
1718 base_mismatch
= true;
1721 if (!warned
&& !val
->odr_violated
)
1723 warn_odr (type
, val
->type
, NULL
, NULL
,
1725 "a type with the same name but different base "
1726 "type is defined in another translation unit");
1728 warn_types_mismatch (type1
, type2
,
1729 UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1733 if (BINFO_OFFSET (base1
) != BINFO_OFFSET (base2
))
1735 base_mismatch
= true;
1736 if (!warned
&& !val
->odr_violated
)
1737 warn_odr (type
, val
->type
, NULL
, NULL
,
1739 "a type with the same name but different base "
1740 "layout is defined in another translation unit");
1743 /* One of bases is not of complete type. */
1744 if (!TYPE_BINFO (type1
) != !TYPE_BINFO (type2
))
1746 /* If we have a polymorphic type info specified for TYPE1
1747 but not for TYPE2 we possibly missed a base when recording
1749 Be sure this does not happen. */
1750 if (TYPE_BINFO (type1
)
1751 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1753 odr_must_violate
= true;
1756 /* One base is polymorphic and the other not.
1757 This ought to be diagnosed earlier, but do not ICE in the
1759 else if (TYPE_BINFO (type1
)
1760 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1761 != polymorphic_type_binfo_p (TYPE_BINFO (type2
)))
1763 if (!warned
&& !val
->odr_violated
)
1764 warn_odr (type
, val
->type
, NULL
, NULL
,
1766 "a base of the type is polymorphic only in one "
1767 "translation unit");
1768 base_mismatch
= true;
1775 odr_violation_reported
= true;
1776 val
->odr_violated
= true;
1778 if (symtab
->dump_file
)
1780 fprintf (symtab
->dump_file
, "ODR base violation\n");
1782 print_node (symtab
->dump_file
, "", val
->type
, 0);
1783 putc ('\n',symtab
->dump_file
);
1784 print_node (symtab
->dump_file
, "", type
, 0);
1785 putc ('\n',symtab
->dump_file
);
1790 /* Next compare memory layout.
1791 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1792 We must apply the location cache to ensure that they are valid
1793 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1794 if (lto_location_cache::current_cache
)
1795 lto_location_cache::current_cache
->apply_location_cache ();
1796 /* As a special case we stream mangles names of integer types so we can see
1797 if they are believed to be same even though they have different
1798 representation. Avoid bogus warning on mismatches in these. */
1799 if (TREE_CODE (type
) != INTEGER_TYPE
1800 && TREE_CODE (val
->type
) != INTEGER_TYPE
1801 && !odr_types_equivalent_p (val
->type
, type
,
1802 !flag_ltrans
&& !val
->odr_violated
&& !warned
,
1804 DECL_SOURCE_LOCATION (TYPE_NAME (val
->type
)),
1805 DECL_SOURCE_LOCATION (TYPE_NAME (type
))))
1808 odr_violation_reported
= true;
1809 val
->odr_violated
= true;
1811 gcc_assert (val
->odr_violated
|| !odr_must_violate
);
1812 /* Sanity check that all bases will be build same way again. */
1814 && COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1815 && TREE_CODE (val
->type
) == RECORD_TYPE
1816 && TREE_CODE (type
) == RECORD_TYPE
1817 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1818 && !val
->odr_violated
1819 && !base_mismatch
&& val
->bases
.length ())
1821 unsigned int num_poly_bases
= 0;
1824 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1825 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1826 (TYPE_BINFO (type
), i
)))
1828 gcc_assert (num_poly_bases
== val
->bases
.length ());
1829 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
));
1831 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1832 (TYPE_BINFO (type
), i
)))
1834 odr_type base
= get_odr_type
1836 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1839 gcc_assert (val
->bases
[j
] == base
);
1845 /* Regularize things a little. During LTO same types may come with
1846 different BINFOs. Either because their virtual table was
1847 not merged by tree merging and only later at decl merging or
1848 because one type comes with external vtable, while other
1849 with internal. We want to merge equivalent binfos to conserve
1850 memory and streaming overhead.
1852 The external vtables are more harmful: they contain references
1853 to external declarations of methods that may be defined in the
1854 merged LTO unit. For this reason we absolutely need to remove
1855 them and replace by internal variants. Not doing so will lead
1856 to incomplete answers from possible_polymorphic_call_targets.
1858 FIXME: disable for now; because ODR types are now build during
1859 streaming in, the variants do not need to be linked to the type,
1860 yet. We need to do the merging in cleanup pass to be implemented
1862 if (!flag_ltrans
&& merge
1864 && TREE_CODE (val
->type
) == RECORD_TYPE
1865 && TREE_CODE (type
) == RECORD_TYPE
1866 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1867 && TYPE_MAIN_VARIANT (type
) == type
1868 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1869 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1870 && BINFO_VTABLE (TYPE_BINFO (type
)))
1872 tree master_binfo
= TYPE_BINFO (val
->type
);
1873 tree v1
= BINFO_VTABLE (master_binfo
);
1874 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1876 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1878 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1879 && operand_equal_p (TREE_OPERAND (v1
, 1),
1880 TREE_OPERAND (v2
, 1), 0));
1881 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1882 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1884 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1885 == DECL_ASSEMBLER_NAME (v2
));
1887 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1891 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1892 for (i
= 0; i
< val
->types
->length (); i
++)
1894 if (TYPE_BINFO ((*val
->types
)[i
])
1896 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1898 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1901 set_type_binfo (type
, master_binfo
);
1906 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to.
1907 FOR_DUMP_P is true when being called from the dump routines. */
1910 obj_type_ref_class (const_tree ref
, bool for_dump_p
)
1912 gcc_checking_assert (TREE_CODE (ref
) == OBJ_TYPE_REF
);
1913 ref
= TREE_TYPE (ref
);
1914 gcc_checking_assert (TREE_CODE (ref
) == POINTER_TYPE
);
1915 ref
= TREE_TYPE (ref
);
1916 /* We look for type THIS points to. ObjC also builds
1917 OBJ_TYPE_REF with non-method calls, Their first parameter
1918 ID however also corresponds to class type. */
1919 gcc_checking_assert (TREE_CODE (ref
) == METHOD_TYPE
1920 || TREE_CODE (ref
) == FUNCTION_TYPE
);
1921 ref
= TREE_VALUE (TYPE_ARG_TYPES (ref
));
1922 gcc_checking_assert (TREE_CODE (ref
) == POINTER_TYPE
);
1923 tree ret
= TREE_TYPE (ref
);
1924 if (!in_lto_p
&& !TYPE_STRUCTURAL_EQUALITY_P (ret
))
1925 ret
= TYPE_CANONICAL (ret
);
1926 else if (odr_type ot
= get_odr_type (ret
, !for_dump_p
))
1929 gcc_assert (for_dump_p
);
1933 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1934 possibly new entry. */
1937 get_odr_type (tree type
, bool insert
)
1939 odr_type_d
**slot
= NULL
;
1940 odr_type val
= NULL
;
1942 bool build_bases
= false;
1943 bool insert_to_odr_array
= false;
1946 type
= TYPE_MAIN_VARIANT (type
);
1947 if (!in_lto_p
&& !TYPE_STRUCTURAL_EQUALITY_P (type
))
1948 type
= TYPE_CANONICAL (type
);
1950 gcc_checking_assert (can_be_name_hashed_p (type
));
1952 hash
= hash_odr_name (type
);
1953 slot
= odr_hash
->find_slot_with_hash (type
, hash
,
1954 insert
? INSERT
: NO_INSERT
);
1959 /* See if we already have entry for type. */
1964 if (val
->type
!= type
&& insert
1965 && (!val
->types_set
|| !val
->types_set
->add (type
)))
1966 build_bases
= add_type_duplicate (val
, type
);
1970 val
= ggc_cleared_alloc
<odr_type_d
> ();
1973 val
->derived_types
= vNULL
;
1974 if (type_with_linkage_p (type
))
1975 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
1977 val
->anonymous_namespace
= 0;
1978 build_bases
= COMPLETE_TYPE_P (val
->type
);
1979 insert_to_odr_array
= true;
1983 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1984 && type_with_linkage_p (type
)
1985 && type
== TYPE_MAIN_VARIANT (type
))
1987 tree binfo
= TYPE_BINFO (type
);
1990 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) == type
);
1992 val
->all_derivations_known
= type_all_derivations_known_p (type
);
1993 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
1994 /* For now record only polymorphic types. other are
1995 pointless for devirtualization and we cannot precisely
1996 determine ODR equivalency of these during LTO. */
1997 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
1999 tree base_type
= BINFO_TYPE (BINFO_BASE_BINFO (binfo
, i
));
2000 odr_type base
= get_odr_type (base_type
, true);
2001 gcc_assert (TYPE_MAIN_VARIANT (base_type
) == base_type
);
2002 base
->derived_types
.safe_push (val
);
2003 val
->bases
.safe_push (base
);
2004 if (base
->id
> base_id
)
2008 /* Ensure that type always appears after bases. */
2009 if (insert_to_odr_array
)
2012 val
->id
= odr_types
.length ();
2013 vec_safe_push (odr_types_ptr
, val
);
2015 else if (base_id
> val
->id
)
2017 odr_types
[val
->id
] = 0;
2018 /* Be sure we did not recorded any derived types; these may need
2020 gcc_assert (val
->derived_types
.length() == 0);
2021 val
->id
= odr_types
.length ();
2022 vec_safe_push (odr_types_ptr
, val
);
2027 /* Return type that in ODR type hash prevailed TYPE. Be careful and punt
2028 on ODR violations. */
2031 prevailing_odr_type (tree type
)
2033 odr_type t
= get_odr_type (type
, false);
2034 if (!t
|| t
->odr_violated
)
2039 /* Set tbaa_enabled flag for TYPE. */
2042 enable_odr_based_tbaa (tree type
)
2044 odr_type t
= get_odr_type (type
, true);
2045 t
->tbaa_enabled
= true;
2048 /* True if canonical type of TYPE is determined using ODR name. */
2051 odr_based_tbaa_p (const_tree type
)
2053 if (!RECORD_OR_UNION_TYPE_P (type
))
2057 odr_type t
= get_odr_type (const_cast <tree
> (type
), false);
2058 if (!t
|| !t
->tbaa_enabled
)
2063 /* Set TYPE_CANONICAL of type and all its variants and duplicates
2067 set_type_canonical_for_odr_type (tree type
, tree canonical
)
2069 odr_type t
= get_odr_type (type
, false);
2073 for (tree t2
= t
->type
; t2
; t2
= TYPE_NEXT_VARIANT (t2
))
2074 TYPE_CANONICAL (t2
) = canonical
;
2076 FOR_EACH_VEC_ELT (*t
->types
, i
, tt
)
2077 for (tree t2
= tt
; t2
; t2
= TYPE_NEXT_VARIANT (t2
))
2078 TYPE_CANONICAL (t2
) = canonical
;
2081 /* Return true if we reported some ODR violation on TYPE. */
2084 odr_type_violation_reported_p (tree type
)
2086 return get_odr_type (type
, false)->odr_violated
;
2089 /* Add TYPE of ODR type hash. */
2092 register_odr_type (tree type
)
2095 odr_hash
= new odr_hash_type (23);
2096 if (type
== TYPE_MAIN_VARIANT (type
))
2098 /* To get ODR warnings right, first register all sub-types. */
2099 if (RECORD_OR_UNION_TYPE_P (type
)
2100 && COMPLETE_TYPE_P (type
))
2102 /* Limit recursion on types which are already registered. */
2103 odr_type ot
= get_odr_type (type
, false);
2105 && (ot
->type
== type
2107 && ot
->types_set
->contains (type
))))
2109 for (tree f
= TYPE_FIELDS (type
); f
; f
= TREE_CHAIN (f
))
2110 if (TREE_CODE (f
) == FIELD_DECL
)
2112 tree subtype
= TREE_TYPE (f
);
2114 while (TREE_CODE (subtype
) == ARRAY_TYPE
)
2115 subtype
= TREE_TYPE (subtype
);
2116 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype
)))
2117 register_odr_type (TYPE_MAIN_VARIANT (subtype
));
2119 if (TYPE_BINFO (type
))
2120 for (unsigned int i
= 0;
2121 i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
2122 register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
2123 (TYPE_BINFO (type
), i
)));
2125 get_odr_type (type
, true);
2129 /* Return true if type is known to have no derivations. */
2132 type_known_to_have_no_derivations_p (tree t
)
2134 return (type_all_derivations_known_p (t
)
2135 && (TYPE_FINAL_P (t
)
2137 && !get_odr_type (t
, true)->derived_types
.length())));
2140 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2141 recursive printing. */
2144 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
2147 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
2148 print_generic_expr (f
, t
->type
, TDF_SLIM
);
2149 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
2150 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
2151 if (TYPE_NAME (t
->type
))
2153 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
->type
)))
2154 fprintf (f
, "%*s mangled name: %s\n", indent
* 2, "",
2156 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
->type
))));
2158 if (t
->bases
.length ())
2160 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
2161 for (i
= 0; i
< t
->bases
.length (); i
++)
2162 fprintf (f
, " %i", t
->bases
[i
]->id
);
2165 if (t
->derived_types
.length ())
2167 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
2168 for (i
= 0; i
< t
->derived_types
.length (); i
++)
2169 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
2174 /* Dump the type inheritance graph. */
2177 dump_type_inheritance_graph (FILE *f
)
2180 unsigned int num_all_types
= 0, num_types
= 0, num_duplicates
= 0;
2183 fprintf (f
, "\n\nType inheritance graph:\n");
2184 for (i
= 0; i
< odr_types
.length (); i
++)
2186 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
2187 dump_odr_type (f
, odr_types
[i
]);
2189 for (i
= 0; i
< odr_types
.length (); i
++)
2195 if (!odr_types
[i
]->types
|| !odr_types
[i
]->types
->length ())
2198 /* To aid ODR warnings we also mangle integer constants but do
2199 not consider duplicates there. */
2200 if (TREE_CODE (odr_types
[i
]->type
) == INTEGER_TYPE
)
2203 /* It is normal to have one duplicate and one normal variant. */
2204 if (odr_types
[i
]->types
->length () == 1
2205 && COMPLETE_TYPE_P (odr_types
[i
]->type
)
2206 && !COMPLETE_TYPE_P ((*odr_types
[i
]->types
)[0]))
2212 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
2213 print_node (f
, "", odr_types
[i
]->type
, 0);
2214 print_node (f
, "", TYPE_NAME (odr_types
[i
]->type
), 0);
2216 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2220 fprintf (f
, "duplicate #%i\n", j
);
2221 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
2222 t
= (*odr_types
[i
]->types
)[j
];
2223 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
2225 t
= TYPE_CONTEXT (t
);
2226 print_node (f
, "", t
, 0);
2228 print_node (f
, "", TYPE_NAME ((*odr_types
[i
]->types
)[j
]), 0);
2232 fprintf (f
, "Out of %i types there are %i types with duplicates; "
2233 "%i duplicates overall\n", num_all_types
, num_types
, num_duplicates
);
2236 /* Save some WPA->ltrans streaming by freeing stuff needed only for good
2238 We make TYPE_DECLs to not point back
2239 to the type (which is needed to keep them in the same SCC and preserve
2240 location information to output warnings) and subsequently we make all
2241 TYPE_DECLS of same assembler name equivalent. */
2244 free_odr_warning_data ()
2246 static bool odr_data_freed
= false;
2248 if (odr_data_freed
|| !flag_wpa
|| !odr_types_ptr
)
2251 odr_data_freed
= true;
2253 for (unsigned int i
= 0; i
< odr_types
.length (); i
++)
2256 tree t
= odr_types
[i
]->type
;
2258 TREE_TYPE (TYPE_NAME (t
)) = void_type_node
;
2260 if (odr_types
[i
]->types
)
2261 for (unsigned int j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2263 tree td
= (*odr_types
[i
]->types
)[j
];
2265 TYPE_NAME (td
) = TYPE_NAME (t
);
2268 odr_data_freed
= true;
2271 /* Initialize IPA devirt and build inheritance tree graph. */
2274 build_type_inheritance_graph (void)
2276 struct symtab_node
*n
;
2277 FILE *inheritance_dump_file
;
2282 free_odr_warning_data ();
2285 timevar_push (TV_IPA_INHERITANCE
);
2286 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
2287 odr_hash
= new odr_hash_type (23);
2289 /* We reconstruct the graph starting of types of all methods seen in the
2292 if (is_a
<cgraph_node
*> (n
)
2293 && DECL_VIRTUAL_P (n
->decl
)
2294 && n
->real_symbol_p ())
2295 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
2297 /* Look also for virtual tables of types that do not define any methods.
2299 We need it in a case where class B has virtual base of class A
2300 re-defining its virtual method and there is class C with no virtual
2301 methods with B as virtual base.
2303 Here we output B's virtual method in two variant - for non-virtual
2304 and virtual inheritance. B's virtual table has non-virtual version,
2305 while C's has virtual.
2307 For this reason we need to know about C in order to include both
2308 variants of B. More correctly, record_target_from_binfo should
2309 add both variants of the method when walking B, but we have no
2310 link in between them.
2312 We rely on fact that either the method is exported and thus we
2313 assume it is called externally or C is in anonymous namespace and
2314 thus we will see the vtable. */
2316 else if (is_a
<varpool_node
*> (n
)
2317 && DECL_VIRTUAL_P (n
->decl
)
2318 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
2319 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
2320 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
2321 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
2322 if (inheritance_dump_file
)
2324 dump_type_inheritance_graph (inheritance_dump_file
);
2325 dump_end (TDI_inheritance
, inheritance_dump_file
);
2327 free_odr_warning_data ();
2328 timevar_pop (TV_IPA_INHERITANCE
);
2331 /* Return true if N has reference from live virtual table
2332 (and thus can be a destination of polymorphic call).
2333 Be conservatively correct when callgraph is not built or
2334 if the method may be referred externally. */
2337 referenced_from_vtable_p (struct cgraph_node
*node
)
2340 struct ipa_ref
*ref
;
2343 if (node
->externally_visible
2344 || DECL_EXTERNAL (node
->decl
)
2345 || node
->used_from_other_partition
)
2348 /* Keep this test constant time.
2349 It is unlikely this can happen except for the case where speculative
2350 devirtualization introduced many speculative edges to this node.
2351 In this case the target is very likely alive anyway. */
2352 if (node
->ref_list
.referring
.length () > 100)
2355 /* We need references built. */
2356 if (symtab
->state
<= CONSTRUCTION
)
2359 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
2360 if ((ref
->use
== IPA_REF_ALIAS
2361 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
2362 || (ref
->use
== IPA_REF_ADDR
2363 && VAR_P (ref
->referring
->decl
)
2364 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
2372 /* Return if TARGET is cxa_pure_virtual. */
2375 is_cxa_pure_virtual_p (tree target
)
2377 return target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
2378 && DECL_NAME (target
)
2379 && id_equal (DECL_NAME (target
),
2380 "__cxa_pure_virtual");
2383 /* If TARGET has associated node, record it in the NODES array.
2384 CAN_REFER specify if program can refer to the target directly.
2385 if TARGET is unknown (NULL) or it cannot be inserted (for example because
2386 its body was already removed and there is no way to refer to it), clear
2390 maybe_record_node (vec
<cgraph_node
*> &nodes
,
2391 tree target
, hash_set
<tree
> *inserted
,
2395 struct cgraph_node
*target_node
, *alias_target
;
2396 enum availability avail
;
2397 bool pure_virtual
= is_cxa_pure_virtual_p (target
);
2399 /* __builtin_unreachable do not need to be added into
2400 list of targets; the runtime effect of calling them is undefined.
2401 Only "real" virtual methods should be accounted. */
2402 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
&& !pure_virtual
)
2407 /* The only case when method of anonymous namespace becomes unreferable
2408 is when we completely optimized it out. */
2411 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2419 target_node
= cgraph_node::get (target
);
2421 /* Prefer alias target over aliases, so we do not get confused by
2425 alias_target
= target_node
->ultimate_alias_target (&avail
);
2426 if (target_node
!= alias_target
2427 && avail
>= AVAIL_AVAILABLE
2428 && target_node
->get_availability ())
2429 target_node
= alias_target
;
2432 /* Method can only be called by polymorphic call if any
2433 of vtables referring to it are alive.
2435 While this holds for non-anonymous functions, too, there are
2436 cases where we want to keep them in the list; for example
2437 inline functions with -fno-weak are static, but we still
2438 may devirtualize them when instance comes from other unit.
2439 The same holds for LTO.
2441 Currently we ignore these functions in speculative devirtualization.
2442 ??? Maybe it would make sense to be more aggressive for LTO even
2446 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
2448 || !referenced_from_vtable_p (target_node
)))
2450 /* See if TARGET is useful function we can deal with. */
2451 else if (target_node
!= NULL
2452 && (TREE_PUBLIC (target
)
2453 || DECL_EXTERNAL (target
)
2454 || target_node
->definition
)
2455 && target_node
->real_symbol_p ())
2457 gcc_assert (!target_node
->inlined_to
);
2458 gcc_assert (target_node
->real_symbol_p ());
2459 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2460 by valid program. */
2461 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
2463 /* Only add pure virtual if it is the only possible target. This way
2464 we will preserve the diagnostics about pure virtual called in many
2465 cases without disabling optimization in other. */
2466 else if (pure_virtual
)
2468 if (nodes
.length ())
2471 /* If we found a real target, take away cxa_pure_virtual. */
2472 else if (!pure_virtual
&& nodes
.length () == 1
2473 && is_cxa_pure_virtual_p (nodes
[0]->decl
))
2475 if (pure_virtual
&& nodes
.length ())
2477 if (!inserted
->add (target
))
2479 cached_polymorphic_call_targets
->add (target_node
);
2480 nodes
.safe_push (target_node
);
2483 else if (!completep
)
2485 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2486 optimized out or partitioned to other unit) so we cannot add it. When
2487 not sanitizing, there is nothing to do.
2488 Otherwise declare the list incomplete. */
2489 else if (pure_virtual
)
2491 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
2494 else if (flag_ltrans
2495 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2499 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2500 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2501 method in vtable and insert method to NODES array
2502 or BASES_TO_CONSIDER if this array is non-NULL.
2503 Otherwise recurse to base BINFOs.
2504 This matches what get_binfo_at_offset does, but with offset
2507 TYPE_BINFOS is a stack of BINFOS of types with defined
2508 virtual table seen on way from class type to BINFO.
2510 MATCHED_VTABLES tracks virtual tables we already did lookup
2511 for virtual function in. INSERTED tracks nodes we already
2514 ANONYMOUS is true if BINFO is part of anonymous namespace.
2516 Clear COMPLETEP when we hit unreferable target.
2520 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
2521 vec
<tree
> *bases_to_consider
,
2524 vec
<tree
> &type_binfos
,
2525 HOST_WIDE_INT otr_token
,
2527 HOST_WIDE_INT offset
,
2528 hash_set
<tree
> *inserted
,
2529 hash_set
<tree
> *matched_vtables
,
2533 tree type
= BINFO_TYPE (binfo
);
2538 if (BINFO_VTABLE (binfo
))
2539 type_binfos
.safe_push (binfo
);
2540 if (types_same_for_odr (type
, outer_type
))
2543 tree type_binfo
= NULL
;
2545 /* Look up BINFO with virtual table. For normal types it is always last
2547 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
2548 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
2550 type_binfo
= type_binfos
[i
];
2553 if (BINFO_VTABLE (binfo
))
2555 /* If this is duplicated BINFO for base shared by virtual inheritance,
2556 we may not have its associated vtable. This is not a problem, since
2557 we will walk it on the other path. */
2560 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
2564 gcc_assert (odr_violation_reported
);
2567 /* For types in anonymous namespace first check if the respective vtable
2568 is alive. If not, we know the type can't be called. */
2569 if (!flag_ltrans
&& anonymous
)
2571 tree vtable
= BINFO_VTABLE (inner_binfo
);
2572 varpool_node
*vnode
;
2574 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
2575 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
2576 vnode
= varpool_node::get (vtable
);
2577 if (!vnode
|| !vnode
->definition
)
2580 gcc_assert (inner_binfo
);
2581 if (bases_to_consider
2582 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
2583 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
2586 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2589 if (!bases_to_consider
)
2590 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2591 /* Destructors are never called via construction vtables. */
2592 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
2593 bases_to_consider
->safe_push (target
);
2599 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2600 /* Walking bases that have no virtual method is pointless exercise. */
2601 if (polymorphic_type_binfo_p (base_binfo
))
2602 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
2604 otr_token
, outer_type
, offset
, inserted
,
2605 matched_vtables
, anonymous
, completep
);
2606 if (BINFO_VTABLE (binfo
))
2610 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2611 of TYPE, insert them to NODES, recurse into derived nodes.
2612 INSERTED is used to avoid duplicate insertions of methods into NODES.
2613 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2614 Clear COMPLETEP if unreferable target is found.
2616 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2617 all cases where BASE_SKIPPED is true (because the base is abstract
2621 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
2622 hash_set
<tree
> *inserted
,
2623 hash_set
<tree
> *matched_vtables
,
2626 HOST_WIDE_INT otr_token
,
2628 HOST_WIDE_INT offset
,
2630 vec
<tree
> &bases_to_consider
,
2631 bool consider_construction
)
2633 tree binfo
= TYPE_BINFO (type
->type
);
2635 auto_vec
<tree
, 8> type_binfos
;
2636 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
2638 /* We may need to consider types w/o instances because of possible derived
2639 types using their methods either directly or via construction vtables.
2640 We are safe to skip them when all derivations are known, since we will
2642 This is done by recording them to BASES_TO_CONSIDER array. */
2643 if (possibly_instantiated
|| consider_construction
)
2645 record_target_from_binfo (nodes
,
2646 (!possibly_instantiated
2647 && type_all_derivations_known_p (type
->type
))
2648 ? &bases_to_consider
: NULL
,
2649 binfo
, otr_type
, type_binfos
, otr_token
,
2651 inserted
, matched_vtables
,
2652 type
->anonymous_namespace
, completep
);
2654 for (i
= 0; i
< type
->derived_types
.length (); i
++)
2655 possible_polymorphic_call_targets_1 (nodes
, inserted
,
2658 type
->derived_types
[i
],
2659 otr_token
, outer_type
, offset
, completep
,
2660 bases_to_consider
, consider_construction
);
2663 /* Cache of queries for polymorphic call targets.
2665 Enumerating all call targets may get expensive when there are many
2666 polymorphic calls in the program, so we memoize all the previous
2667 queries and avoid duplicated work. */
2669 class polymorphic_call_target_d
2672 HOST_WIDE_INT otr_token
;
2673 ipa_polymorphic_call_context context
;
2675 vec
<cgraph_node
*> targets
;
2678 unsigned int n_odr_types
;
2683 /* Polymorphic call target cache helpers. */
2685 struct polymorphic_call_target_hasher
2686 : pointer_hash
<polymorphic_call_target_d
>
2688 static inline hashval_t
hash (const polymorphic_call_target_d
*);
2689 static inline bool equal (const polymorphic_call_target_d
*,
2690 const polymorphic_call_target_d
*);
2691 static inline void remove (polymorphic_call_target_d
*);
2694 /* Return the computed hashcode for ODR_QUERY. */
2697 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d
*odr_query
)
2699 inchash::hash
hstate (odr_query
->otr_token
);
2701 hstate
.add_hwi (odr_query
->type
->id
);
2702 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
2703 hstate
.add_hwi (odr_query
->context
.offset
);
2704 hstate
.add_hwi (odr_query
->n_odr_types
);
2706 if (odr_query
->context
.speculative_outer_type
)
2708 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
2709 hstate
.add_hwi (odr_query
->context
.speculative_offset
);
2711 hstate
.add_flag (odr_query
->speculative
);
2712 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
2713 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
2714 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
2715 hstate
.commit_flag ();
2716 return hstate
.end ();
2719 /* Compare cache entries T1 and T2. */
2722 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d
*t1
,
2723 const polymorphic_call_target_d
*t2
)
2725 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
2726 && t1
->speculative
== t2
->speculative
2727 && t1
->context
.offset
== t2
->context
.offset
2728 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
2729 && t1
->context
.outer_type
== t2
->context
.outer_type
2730 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
2731 && t1
->context
.maybe_in_construction
2732 == t2
->context
.maybe_in_construction
2733 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
2734 && (t1
->context
.speculative_maybe_derived_type
2735 == t2
->context
.speculative_maybe_derived_type
)
2736 /* Adding new type may affect outcome of target search. */
2737 && t1
->n_odr_types
== t2
->n_odr_types
);
2740 /* Remove entry in polymorphic call target cache hash. */
2743 polymorphic_call_target_hasher::remove (polymorphic_call_target_d
*v
)
2745 v
->targets
.release ();
2749 /* Polymorphic call target query cache. */
2751 typedef hash_table
<polymorphic_call_target_hasher
>
2752 polymorphic_call_target_hash_type
;
2753 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
2755 /* Destroy polymorphic call target query cache. */
2758 free_polymorphic_call_targets_hash ()
2760 if (cached_polymorphic_call_targets
)
2762 delete polymorphic_call_target_hash
;
2763 polymorphic_call_target_hash
= NULL
;
2764 delete cached_polymorphic_call_targets
;
2765 cached_polymorphic_call_targets
= NULL
;
2769 /* Force rebuilding type inheritance graph from scratch.
2770 This is use to make sure that we do not keep references to types
2771 which was not visible to free_lang_data. */
2774 rebuild_type_inheritance_graph ()
2780 odr_types_ptr
= NULL
;
2781 free_polymorphic_call_targets_hash ();
2784 /* When virtual function is removed, we may need to flush the cache. */
2787 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
2789 if (cached_polymorphic_call_targets
2791 && cached_polymorphic_call_targets
->contains (n
))
2792 free_polymorphic_call_targets_hash ();
2795 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2798 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2801 tree v
= BINFO_VTABLE (binfo
);
2804 unsigned HOST_WIDE_INT this_offset
;
2808 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2811 if (offset
== this_offset
2812 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2816 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2817 if (polymorphic_type_binfo_p (base_binfo
))
2819 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2826 /* T is known constant value of virtual table pointer.
2827 Store virtual table to V and its offset to OFFSET.
2828 Return false if T does not look like virtual table reference. */
2831 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2832 unsigned HOST_WIDE_INT
*offset
)
2834 /* We expect &MEM[(void *)&virtual_table + 16B].
2835 We obtain object's BINFO from the context of the virtual table.
2836 This one contains pointer to virtual table represented via
2837 POINTER_PLUS_EXPR. Verify that this pointer matches what
2838 we propagated through.
2840 In the case of virtual inheritance, the virtual tables may
2841 be nested, i.e. the offset may be different from 16 and we may
2842 need to dive into the type representation. */
2843 if (TREE_CODE (t
) == ADDR_EXPR
2844 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2845 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2846 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2847 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2849 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2850 (TREE_OPERAND (t
, 0), 0), 0)))
2852 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2853 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2857 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2858 We need to handle it when T comes from static variable initializer or
2860 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2862 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2863 t
= TREE_OPERAND (t
, 0);
2868 if (TREE_CODE (t
) != ADDR_EXPR
)
2870 *v
= TREE_OPERAND (t
, 0);
2874 /* T is known constant value of virtual table pointer. Return BINFO of the
2878 vtable_pointer_value_to_binfo (const_tree t
)
2881 unsigned HOST_WIDE_INT offset
;
2883 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2886 /* FIXME: for stores of construction vtables we return NULL,
2887 because we do not have BINFO for those. Eventually we should fix
2888 our representation to allow this case to be handled, too.
2889 In the case we see store of BINFO we however may assume
2890 that standard folding will be able to cope with it. */
2891 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2895 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2896 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2897 and insert them in NODES.
2899 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2902 record_targets_from_bases (tree otr_type
,
2903 HOST_WIDE_INT otr_token
,
2905 HOST_WIDE_INT offset
,
2906 vec
<cgraph_node
*> &nodes
,
2907 hash_set
<tree
> *inserted
,
2908 hash_set
<tree
> *matched_vtables
,
2913 HOST_WIDE_INT pos
, size
;
2917 if (types_same_for_odr (outer_type
, otr_type
))
2920 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
2922 if (TREE_CODE (fld
) != FIELD_DECL
)
2925 pos
= int_bit_position (fld
);
2926 size
= tree_to_shwi (DECL_SIZE (fld
));
2927 if (pos
<= offset
&& (pos
+ size
) > offset
2928 /* Do not get confused by zero sized bases. */
2929 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
2932 /* Within a class type we should always find corresponding fields. */
2933 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
2935 /* Nonbase types should have been stripped by outer_class_type. */
2936 gcc_assert (DECL_ARTIFICIAL (fld
));
2938 outer_type
= TREE_TYPE (fld
);
2941 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
2945 gcc_assert (odr_violation_reported
);
2948 gcc_assert (base_binfo
);
2949 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
2952 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2955 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
2956 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2957 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
2962 /* When virtual table is removed, we may need to flush the cache. */
2965 devirt_variable_node_removal_hook (varpool_node
*n
,
2966 void *d ATTRIBUTE_UNUSED
)
2968 if (cached_polymorphic_call_targets
2969 && DECL_VIRTUAL_P (n
->decl
)
2970 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
2971 free_polymorphic_call_targets_hash ();
2974 /* Record about how many calls would benefit from given type to be final. */
2976 struct odr_type_warn_count
2980 profile_count dyn_count
;
2983 /* Record about how many calls would benefit from given method to be final. */
2985 struct decl_warn_count
2989 profile_count dyn_count
;
2992 /* Information about type and decl warnings. */
2994 class final_warning_record
2997 /* If needed grow type_warnings vector and initialize new decl_warn_count
2998 to have dyn_count set to profile_count::zero (). */
2999 void grow_type_warnings (unsigned newlen
);
3001 profile_count dyn_count
;
3002 auto_vec
<odr_type_warn_count
> type_warnings
;
3003 hash_map
<tree
, decl_warn_count
> decl_warnings
;
3007 final_warning_record::grow_type_warnings (unsigned newlen
)
3009 unsigned len
= type_warnings
.length ();
3012 type_warnings
.safe_grow_cleared (newlen
, true);
3013 for (unsigned i
= len
; i
< newlen
; i
++)
3014 type_warnings
[i
].dyn_count
= profile_count::zero ();
3018 class final_warning_record
*final_warning_records
;
3020 /* Return vector containing possible targets of polymorphic call of type
3021 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3022 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3023 OTR_TYPE and include their virtual method. This is useful for types
3024 possibly in construction or destruction where the virtual table may
3025 temporarily change to one of base types. INCLUDE_DERIVED_TYPES make
3026 us to walk the inheritance graph for all derivations.
3028 If COMPLETEP is non-NULL, store true if the list is complete.
3029 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3030 in the target cache. If user needs to visit every target list
3031 just once, it can memoize them.
3033 If SPECULATIVE is set, the list will not contain targets that
3034 are not speculatively taken.
3036 Returned vector is placed into cache. It is NOT caller's responsibility
3037 to free it. The vector can be freed on cgraph_remove_node call if
3038 the particular node is a virtual function present in the cache. */
3041 possible_polymorphic_call_targets (tree otr_type
,
3042 HOST_WIDE_INT otr_token
,
3043 ipa_polymorphic_call_context context
,
3048 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
3049 vec
<cgraph_node
*> nodes
= vNULL
;
3050 auto_vec
<tree
, 8> bases_to_consider
;
3051 odr_type type
, outer_type
;
3052 polymorphic_call_target_d key
;
3053 polymorphic_call_target_d
**slot
;
3057 bool can_refer
= false;
3058 bool skipped
= false;
3060 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
3062 /* If ODR is not initialized or the context is invalid, return empty
3064 if (!odr_hash
|| context
.invalid
|| !TYPE_BINFO (otr_type
))
3067 *completep
= context
.invalid
;
3069 *cache_token
= NULL
;
3073 /* Do not bother to compute speculative info when user do not asks for it. */
3074 if (!speculative
|| !context
.speculative_outer_type
)
3075 context
.clear_speculation ();
3077 type
= get_odr_type (otr_type
, true);
3079 /* Recording type variants would waste results cache. */
3080 gcc_assert (!context
.outer_type
3081 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3083 /* Look up the outer class type we want to walk.
3084 If we fail to do so, the context is invalid. */
3085 if ((context
.outer_type
|| context
.speculative_outer_type
)
3086 && !context
.restrict_to_inner_class (otr_type
))
3091 *cache_token
= NULL
;
3094 gcc_assert (!context
.invalid
);
3096 /* Check that restrict_to_inner_class kept the main variant. */
3097 gcc_assert (!context
.outer_type
3098 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3100 /* We canonicalize our query, so we do not need extra hashtable entries. */
3102 /* Without outer type, we have no use for offset. Just do the
3103 basic search from inner type. */
3104 if (!context
.outer_type
)
3105 context
.clear_outer_type (otr_type
);
3106 /* We need to update our hierarchy if the type does not exist. */
3107 outer_type
= get_odr_type (context
.outer_type
, true);
3108 /* If the type is complete, there are no derivations. */
3109 if (TYPE_FINAL_P (outer_type
->type
))
3110 context
.maybe_derived_type
= false;
3112 /* Initialize query cache. */
3113 if (!cached_polymorphic_call_targets
)
3115 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
3116 polymorphic_call_target_hash
3117 = new polymorphic_call_target_hash_type (23);
3118 if (!node_removal_hook_holder
)
3120 node_removal_hook_holder
=
3121 symtab
->add_cgraph_removal_hook (&devirt_node_removal_hook
, NULL
);
3122 symtab
->add_varpool_removal_hook (&devirt_variable_node_removal_hook
,
3129 if (context
.outer_type
!= otr_type
)
3131 = get_odr_type (context
.outer_type
, true)->type
;
3132 if (context
.speculative_outer_type
)
3133 context
.speculative_outer_type
3134 = get_odr_type (context
.speculative_outer_type
, true)->type
;
3137 /* Look up cached answer. */
3139 key
.otr_token
= otr_token
;
3140 key
.speculative
= speculative
;
3141 key
.context
= context
;
3142 key
.n_odr_types
= odr_types
.length ();
3143 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
3145 *cache_token
= (void *)*slot
;
3149 *completep
= (*slot
)->complete
;
3150 if ((*slot
)->type_warning
&& final_warning_records
)
3152 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
3153 if (!final_warning_records
->type_warnings
3154 [(*slot
)->type_warning
- 1].dyn_count
.initialized_p ())
3155 final_warning_records
->type_warnings
3156 [(*slot
)->type_warning
- 1].dyn_count
= profile_count::zero ();
3157 if (final_warning_records
->dyn_count
> 0)
3158 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3159 = final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3160 + final_warning_records
->dyn_count
;
3162 if (!speculative
&& (*slot
)->decl_warning
&& final_warning_records
)
3164 struct decl_warn_count
*c
=
3165 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
3167 if (final_warning_records
->dyn_count
> 0)
3168 c
->dyn_count
+= final_warning_records
->dyn_count
;
3170 return (*slot
)->targets
;
3175 /* Do actual search. */
3176 timevar_push (TV_IPA_VIRTUAL_CALL
);
3177 *slot
= XCNEW (polymorphic_call_target_d
);
3179 *cache_token
= (void *)*slot
;
3180 (*slot
)->type
= type
;
3181 (*slot
)->otr_token
= otr_token
;
3182 (*slot
)->context
= context
;
3183 (*slot
)->speculative
= speculative
;
3185 hash_set
<tree
> inserted
;
3186 hash_set
<tree
> matched_vtables
;
3188 /* First insert targets we speculatively identified as likely. */
3189 if (context
.speculative_outer_type
)
3191 odr_type speculative_outer_type
;
3192 bool speculation_complete
= true;
3193 bool check_derived_types
= false;
3195 /* First insert target from type itself and check if it may have
3197 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
3198 if (TYPE_FINAL_P (speculative_outer_type
->type
))
3199 context
.speculative_maybe_derived_type
= false;
3200 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
3201 context
.speculative_offset
, otr_type
);
3203 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3208 /* In the case we get complete method, we don't need
3209 to walk derivations. */
3210 if (target
&& DECL_FINAL_P (target
))
3211 context
.speculative_maybe_derived_type
= false;
3212 if (check_derived_types
3213 ? type_or_derived_type_possibly_instantiated_p
3214 (speculative_outer_type
)
3215 : type_possibly_instantiated_p (speculative_outer_type
->type
))
3216 maybe_record_node (nodes
, target
, &inserted
, can_refer
,
3217 &speculation_complete
);
3219 matched_vtables
.add (BINFO_VTABLE (binfo
));
3222 /* Next walk recursively all derived types. */
3223 if (context
.speculative_maybe_derived_type
)
3224 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
3225 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3228 speculative_outer_type
->derived_types
[i
],
3229 otr_token
, speculative_outer_type
->type
,
3230 context
.speculative_offset
,
3231 &speculation_complete
,
3236 if (!speculative
|| !nodes
.length ())
3238 bool check_derived_types
= false;
3239 /* First see virtual method of type itself. */
3240 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
3241 context
.offset
, otr_type
);
3243 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3247 gcc_assert (odr_violation_reported
);
3251 /* Destructors are never called through construction virtual tables,
3252 because the type is always known. */
3253 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
3254 context
.maybe_in_construction
= false;
3256 /* In the case we get complete method, we don't need
3257 to walk derivations. */
3258 if (target
&& DECL_FINAL_P (target
))
3260 check_derived_types
= true;
3261 context
.maybe_derived_type
= false;
3264 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3265 if (check_derived_types
3266 ? type_or_derived_type_possibly_instantiated_p (outer_type
)
3267 : type_possibly_instantiated_p (outer_type
->type
))
3268 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3273 matched_vtables
.add (BINFO_VTABLE (binfo
));
3275 /* Next walk recursively all derived types. */
3276 if (context
.maybe_derived_type
)
3278 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
3279 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3282 outer_type
->derived_types
[i
],
3283 otr_token
, outer_type
->type
,
3284 context
.offset
, &complete
,
3286 context
.maybe_in_construction
);
3288 if (!outer_type
->all_derivations_known
)
3290 if (!speculative
&& final_warning_records
3291 && nodes
.length () == 1
3292 && TREE_CODE (TREE_TYPE (nodes
[0]->decl
)) == METHOD_TYPE
)
3295 && warn_suggest_final_types
3296 && !outer_type
->derived_types
.length ())
3298 final_warning_records
->grow_type_warnings
3300 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
3301 if (!final_warning_records
->type_warnings
3302 [outer_type
->id
].dyn_count
.initialized_p ())
3303 final_warning_records
->type_warnings
3304 [outer_type
->id
].dyn_count
= profile_count::zero ();
3305 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
3306 += final_warning_records
->dyn_count
;
3307 final_warning_records
->type_warnings
[outer_type
->id
].type
3309 (*slot
)->type_warning
= outer_type
->id
+ 1;
3312 && warn_suggest_final_methods
3313 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
3317 struct decl_warn_count
&c
=
3318 final_warning_records
->decl_warnings
.get_or_insert
3319 (nodes
[0]->decl
, &existed
);
3324 c
.dyn_count
+= final_warning_records
->dyn_count
;
3329 c
.dyn_count
= final_warning_records
->dyn_count
;
3330 c
.decl
= nodes
[0]->decl
;
3332 (*slot
)->decl_warning
= nodes
[0]->decl
;
3341 /* Destructors are never called through construction virtual tables,
3342 because the type is always known. One of entries may be
3343 cxa_pure_virtual so look to at least two of them. */
3344 if (context
.maybe_in_construction
)
3345 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
3346 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
3347 context
.maybe_in_construction
= false;
3348 if (context
.maybe_in_construction
)
3350 if (type
!= outer_type
3352 || (context
.maybe_derived_type
3353 && !type_all_derivations_known_p (outer_type
->type
))))
3354 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
3355 context
.offset
, nodes
, &inserted
,
3356 &matched_vtables
, &complete
);
3358 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3359 for (i
= 0; i
< bases_to_consider
.length(); i
++)
3360 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
3365 (*slot
)->targets
= nodes
;
3366 (*slot
)->complete
= complete
;
3367 (*slot
)->n_odr_types
= odr_types
.length ();
3369 *completep
= complete
;
3371 timevar_pop (TV_IPA_VIRTUAL_CALL
);
3376 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
3377 vec
<const decl_warn_count
*> *vec
)
3379 vec
->safe_push (&value
);
3383 /* Dump target list TARGETS into FILE. */
3386 dump_targets (FILE *f
, vec
<cgraph_node
*> targets
, bool verbose
)
3390 for (i
= 0; i
< targets
.length (); i
++)
3394 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
3395 fprintf (f
, " %s", name
? name
: targets
[i
]->dump_name ());
3398 if (!targets
[i
]->definition
)
3399 fprintf (f
, " (no definition%s)",
3400 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
3402 /* With many targets for every call polymorphic dumps are going to
3403 be quadratic in size. */
3404 if (i
> 10 && !verbose
)
3406 fprintf (f
, " ... and %i more targets\n", targets
.length () - i
);
3413 /* Dump all possible targets of a polymorphic call. */
3416 dump_possible_polymorphic_call_targets (FILE *f
,
3418 HOST_WIDE_INT otr_token
,
3419 const ipa_polymorphic_call_context
&ctx
,
3422 vec
<cgraph_node
*> targets
;
3424 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
3429 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3431 &final
, NULL
, false);
3432 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
3433 print_generic_expr (f
, type
->type
, TDF_SLIM
);
3434 fprintf (f
, " token %i\n", (int)otr_token
);
3438 fprintf (f
, " %s%s%s%s\n ",
3439 final
? "This is a complete list." :
3440 "This is partial list; extra targets may be defined in other units.",
3441 ctx
.maybe_in_construction
? " (base types included)" : "",
3442 ctx
.maybe_derived_type
? " (derived types included)" : "",
3443 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
3444 len
= targets
.length ();
3445 dump_targets (f
, targets
, verbose
);
3447 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3449 &final
, NULL
, true);
3450 if (targets
.length () != len
)
3452 fprintf (f
, " Speculative targets:");
3453 dump_targets (f
, targets
, verbose
);
3455 /* Ugly: during callgraph construction the target cache may get populated
3456 before all targets are found. While this is harmless (because all local
3457 types are discovered and only in those case we devirtualize fully and we
3458 don't do speculative devirtualization before IPA stage) it triggers
3459 assert here when dumping at that stage also populates the case with
3460 speculative targets. Quietly ignore this. */
3461 gcc_assert (symtab
->state
< IPA_SSA
|| targets
.length () <= len
);
3466 /* Return true if N can be possibly target of a polymorphic call of
3467 OTR_TYPE/OTR_TOKEN. */
3470 possible_polymorphic_call_target_p (tree otr_type
,
3471 HOST_WIDE_INT otr_token
,
3472 const ipa_polymorphic_call_context
&ctx
,
3473 struct cgraph_node
*n
)
3475 vec
<cgraph_node
*> targets
;
3479 if (fndecl_built_in_p (n
->decl
, BUILT_IN_NORMAL
)
3480 && (DECL_FUNCTION_CODE (n
->decl
) == BUILT_IN_UNREACHABLE
3481 || DECL_FUNCTION_CODE (n
->decl
) == BUILT_IN_TRAP
3482 || DECL_FUNCTION_CODE (n
->decl
) == BUILT_IN_UNREACHABLE_TRAP
))
3485 if (is_cxa_pure_virtual_p (n
->decl
))
3490 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
3491 for (i
= 0; i
< targets
.length (); i
++)
3492 if (n
->semantically_equivalent_p (targets
[i
]))
3495 /* At a moment we allow middle end to dig out new external declarations
3496 as a targets of polymorphic calls. */
3497 if (!final
&& !n
->definition
)
3504 /* Return true if N can be possibly target of a polymorphic call of
3505 OBJ_TYPE_REF expression REF in STMT. */
3508 possible_polymorphic_call_target_p (tree ref
,
3510 struct cgraph_node
*n
)
3512 ipa_polymorphic_call_context
context (current_function_decl
, ref
, stmt
);
3513 tree call_fn
= gimple_call_fn (stmt
);
3515 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn
),
3517 (OBJ_TYPE_REF_TOKEN (call_fn
)),
3523 /* After callgraph construction new external nodes may appear.
3524 Add them into the graph. */
3527 update_type_inheritance_graph (void)
3529 struct cgraph_node
*n
;
3533 free_polymorphic_call_targets_hash ();
3534 timevar_push (TV_IPA_INHERITANCE
);
3535 /* We reconstruct the graph starting from types of all methods seen in the
3537 FOR_EACH_FUNCTION (n
)
3538 if (DECL_VIRTUAL_P (n
->decl
)
3540 && n
->real_symbol_p ())
3541 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
3542 timevar_pop (TV_IPA_INHERITANCE
);
3546 /* Return true if N looks like likely target of a polymorphic call.
3547 Rule out cxa_pure_virtual, noreturns, function declared cold and
3548 other obvious cases. */
3551 likely_target_p (struct cgraph_node
*n
)
3554 /* cxa_pure_virtual and similar things are not likely. */
3555 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
3557 flags
= flags_from_decl_or_type (n
->decl
);
3558 if (flags
& ECF_NORETURN
)
3560 if (lookup_attribute ("cold",
3561 DECL_ATTRIBUTES (n
->decl
)))
3563 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
3565 /* If there are no live virtual tables referring the target,
3566 the only way the target can be called is an instance coming from other
3567 compilation unit; speculative devirtualization is built around an
3568 assumption that won't happen. */
3569 if (!referenced_from_vtable_p (n
))
3574 /* Compare type warning records P1 and P2 and choose one with larger count;
3575 helper for qsort. */
3578 type_warning_cmp (const void *p1
, const void *p2
)
3580 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
3581 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
3583 if (t1
->dyn_count
< t2
->dyn_count
)
3585 if (t1
->dyn_count
> t2
->dyn_count
)
3587 return t2
->count
- t1
->count
;
3590 /* Compare decl warning records P1 and P2 and choose one with larger count;
3591 helper for qsort. */
3594 decl_warning_cmp (const void *p1
, const void *p2
)
3596 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
3597 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
3599 if (t1
->dyn_count
< t2
->dyn_count
)
3601 if (t1
->dyn_count
> t2
->dyn_count
)
3603 return t2
->count
- t1
->count
;
3607 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3610 struct cgraph_node
*
3611 try_speculative_devirtualization (tree otr_type
, HOST_WIDE_INT otr_token
,
3612 ipa_polymorphic_call_context ctx
)
3614 vec
<cgraph_node
*>targets
3615 = possible_polymorphic_call_targets
3616 (otr_type
, otr_token
, ctx
, NULL
, NULL
, true);
3618 struct cgraph_node
*likely_target
= NULL
;
3620 for (i
= 0; i
< targets
.length (); i
++)
3621 if (likely_target_p (targets
[i
]))
3625 likely_target
= targets
[i
];
3628 ||!likely_target
->definition
3629 || DECL_EXTERNAL (likely_target
->decl
))
3632 /* Don't use an implicitly-declared destructor (c++/58678). */
3633 struct cgraph_node
*non_thunk_target
3634 = likely_target
->function_symbol ();
3635 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3637 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3638 && likely_target
->can_be_discarded_p ())
3640 return likely_target
;
3643 /* The ipa-devirt pass.
3644 When polymorphic call has only one likely target in the unit,
3645 turn it into a speculative call. */
3650 struct cgraph_node
*n
;
3651 hash_set
<void *> bad_call_targets
;
3652 struct cgraph_edge
*e
;
3654 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
3655 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
3656 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
3663 dump_type_inheritance_graph (dump_file
);
3665 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3666 This is implemented by setting up final_warning_records that are updated
3667 by get_polymorphic_call_targets.
3668 We need to clear cache in this case to trigger recomputation of all
3670 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3672 final_warning_records
= new (final_warning_record
);
3673 final_warning_records
->dyn_count
= profile_count::zero ();
3674 final_warning_records
->grow_type_warnings (odr_types
.length ());
3675 free_polymorphic_call_targets_hash ();
3678 FOR_EACH_DEFINED_FUNCTION (n
)
3680 bool update
= false;
3681 if (!opt_for_fn (n
->decl
, flag_devirtualize
))
3683 if (dump_file
&& n
->indirect_calls
)
3684 fprintf (dump_file
, "\n\nProcesing function %s\n",
3686 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
3687 if (e
->indirect_info
->polymorphic
)
3689 struct cgraph_node
*likely_target
= NULL
;
3693 if (final_warning_records
)
3694 final_warning_records
->dyn_count
= e
->count
.ipa ();
3696 vec
<cgraph_node
*>targets
3697 = possible_polymorphic_call_targets
3698 (e
, &final
, &cache_token
, true);
3701 /* Trigger warnings by calculating non-speculative targets. */
3702 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3703 possible_polymorphic_call_targets (e
);
3706 dump_possible_polymorphic_call_targets
3707 (dump_file
, e
, (dump_flags
& TDF_DETAILS
));
3711 /* See if the call can be devirtualized by means of ipa-prop's
3712 polymorphic call context propagation. If not, we can just
3713 forget about this call being polymorphic and avoid some heavy
3714 lifting in remove_unreachable_nodes that will otherwise try to
3715 keep all possible targets alive until inlining and in the inliner
3718 This may need to be revisited once we add further ways to use
3719 the may edges, but it is a reasonable thing to do right now. */
3721 if ((e
->indirect_info
->param_index
== -1
3722 || (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
)
3723 && e
->indirect_info
->vptr_changed
))
3724 && !flag_ltrans_devirtualize
)
3726 e
->indirect_info
->polymorphic
= false;
3729 fprintf (dump_file
, "Dropping polymorphic call info;"
3730 " it cannot be used by ipa-prop\n");
3733 if (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
))
3736 if (!e
->maybe_hot_p ())
3739 fprintf (dump_file
, "Call is cold\n\n");
3746 fprintf (dump_file
, "Call is already speculated\n\n");
3749 /* When dumping see if we agree with speculation. */
3753 if (bad_call_targets
.contains (cache_token
))
3756 fprintf (dump_file
, "Target list is known to be useless\n\n");
3760 for (i
= 0; i
< targets
.length (); i
++)
3761 if (likely_target_p (targets
[i
]))
3765 likely_target
= NULL
;
3767 fprintf (dump_file
, "More than one likely target\n\n");
3771 likely_target
= targets
[i
];
3775 bad_call_targets
.add (cache_token
);
3778 /* This is reached only when dumping; check if we agree or disagree
3779 with the speculation. */
3782 bool found
= e
->speculative_call_for_target (likely_target
);
3785 fprintf (dump_file
, "We agree with speculation\n\n");
3790 fprintf (dump_file
, "We disagree with speculation\n\n");
3795 if (!likely_target
->definition
)
3798 fprintf (dump_file
, "Target is not a definition\n\n");
3802 /* Do not introduce new references to external symbols. While we
3803 can handle these just well, it is common for programs to
3804 incorrectly with headers defining methods they are linked
3806 if (DECL_EXTERNAL (likely_target
->decl
))
3809 fprintf (dump_file
, "Target is external\n\n");
3813 /* Don't use an implicitly-declared destructor (c++/58678). */
3814 struct cgraph_node
*non_thunk_target
3815 = likely_target
->function_symbol ();
3816 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3819 fprintf (dump_file
, "Target is artificial\n\n");
3823 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3824 && likely_target
->can_be_discarded_p ())
3827 fprintf (dump_file
, "Target is overwritable\n\n");
3831 else if (dbg_cnt (devirt
))
3833 if (dump_enabled_p ())
3835 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, e
->call_stmt
,
3836 "speculatively devirtualizing call "
3839 likely_target
->dump_name ());
3841 if (!likely_target
->can_be_discarded_p ())
3844 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
3846 likely_target
= alias
;
3851 (likely_target
, e
->count
.apply_scale (8, 10));
3855 ipa_update_overall_fn_summary (n
);
3857 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3859 if (warn_suggest_final_types
)
3861 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
3862 for (unsigned int i
= 0;
3863 i
< final_warning_records
->type_warnings
.length (); i
++)
3864 if (final_warning_records
->type_warnings
[i
].count
)
3866 tree type
= final_warning_records
->type_warnings
[i
].type
;
3867 int count
= final_warning_records
->type_warnings
[i
].count
;
3868 profile_count dyn_count
3869 = final_warning_records
->type_warnings
[i
].dyn_count
;
3871 if (!(dyn_count
> 0))
3872 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3873 OPT_Wsuggest_final_types
, count
,
3874 "Declaring type %qD final "
3875 "would enable devirtualization of %i call",
3876 "Declaring type %qD final "
3877 "would enable devirtualization of %i calls",
3881 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3882 OPT_Wsuggest_final_types
, count
,
3883 "Declaring type %qD final "
3884 "would enable devirtualization of %i call "
3885 "executed %lli times",
3886 "Declaring type %qD final "
3887 "would enable devirtualization of %i calls "
3888 "executed %lli times",
3891 (long long) dyn_count
.to_gcov_type ());
3895 if (warn_suggest_final_methods
)
3897 auto_vec
<const decl_warn_count
*> decl_warnings_vec
;
3899 final_warning_records
->decl_warnings
.traverse
3900 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3901 decl_warnings_vec
.qsort (decl_warning_cmp
);
3902 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3904 tree decl
= decl_warnings_vec
[i
]->decl
;
3905 int count
= decl_warnings_vec
[i
]->count
;
3906 profile_count dyn_count
3907 = decl_warnings_vec
[i
]->dyn_count
;
3909 if (!(dyn_count
> 0))
3910 if (DECL_CXX_DESTRUCTOR_P (decl
))
3911 warning_n (DECL_SOURCE_LOCATION (decl
),
3912 OPT_Wsuggest_final_methods
, count
,
3913 "Declaring virtual destructor of %qD final "
3914 "would enable devirtualization of %i call",
3915 "Declaring virtual destructor of %qD final "
3916 "would enable devirtualization of %i calls",
3917 DECL_CONTEXT (decl
), count
);
3919 warning_n (DECL_SOURCE_LOCATION (decl
),
3920 OPT_Wsuggest_final_methods
, count
,
3921 "Declaring method %qD final "
3922 "would enable devirtualization of %i call",
3923 "Declaring method %qD final "
3924 "would enable devirtualization of %i calls",
3926 else if (DECL_CXX_DESTRUCTOR_P (decl
))
3927 warning_n (DECL_SOURCE_LOCATION (decl
),
3928 OPT_Wsuggest_final_methods
, count
,
3929 "Declaring virtual destructor of %qD final "
3930 "would enable devirtualization of %i call "
3931 "executed %lli times",
3932 "Declaring virtual destructor of %qD final "
3933 "would enable devirtualization of %i calls "
3934 "executed %lli times",
3935 DECL_CONTEXT (decl
), count
,
3936 (long long)dyn_count
.to_gcov_type ());
3938 warning_n (DECL_SOURCE_LOCATION (decl
),
3939 OPT_Wsuggest_final_methods
, count
,
3940 "Declaring method %qD final "
3941 "would enable devirtualization of %i call "
3942 "executed %lli times",
3943 "Declaring method %qD final "
3944 "would enable devirtualization of %i calls "
3945 "executed %lli times",
3947 (long long)dyn_count
.to_gcov_type ());
3951 delete (final_warning_records
);
3952 final_warning_records
= 0;
3957 "%i polymorphic calls, %i devirtualized,"
3958 " %i speculatively devirtualized, %i cold\n"
3959 "%i have multiple targets, %i overwritable,"
3960 " %i already speculated (%i agree, %i disagree),"
3961 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3962 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3963 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3964 nexternal
, nnotdefined
, nartificial
, ndropped
);
3965 return ndevirtualized
|| ndropped
? TODO_remove_functions
: 0;
3970 const pass_data pass_data_ipa_devirt
=
3972 IPA_PASS
, /* type */
3973 "devirt", /* name */
3974 OPTGROUP_NONE
, /* optinfo_flags */
3975 TV_IPA_DEVIRT
, /* tv_id */
3976 0, /* properties_required */
3977 0, /* properties_provided */
3978 0, /* properties_destroyed */
3979 0, /* todo_flags_start */
3980 ( TODO_dump_symtab
), /* todo_flags_finish */
3983 class pass_ipa_devirt
: public ipa_opt_pass_d
3986 pass_ipa_devirt (gcc::context
*ctxt
)
3987 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3988 NULL
, /* generate_summary */
3989 NULL
, /* write_summary */
3990 NULL
, /* read_summary */
3991 NULL
, /* write_optimization_summary */
3992 NULL
, /* read_optimization_summary */
3993 NULL
, /* stmt_fixup */
3994 0, /* function_transform_todo_flags_start */
3995 NULL
, /* function_transform */
3996 NULL
) /* variable_transform */
3999 /* opt_pass methods: */
4000 bool gate (function
*) final override
4002 /* In LTO, always run the IPA passes and decide on function basis if the
4006 return (flag_devirtualize
4007 && (flag_devirtualize_speculatively
4008 || (warn_suggest_final_methods
4009 || warn_suggest_final_types
))
4013 unsigned int execute (function
*) final override
{ return ipa_devirt (); }
4015 }; // class pass_ipa_devirt
4020 make_pass_ipa_devirt (gcc::context
*ctxt
)
4022 return new pass_ipa_devirt (ctxt
);
4025 /* Print ODR name of a TYPE if available.
4026 Use demangler when option DEMANGLE is used. */
4029 debug_tree_odr_name (tree type
, bool demangle
)
4031 const char *odr
= get_odr_name_for_type (type
);
4034 const int opts
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4035 odr
= cplus_demangle (odr
, opts
);
4038 fprintf (stderr
, "%s\n", odr
);
4041 /* Register ODR enum so we later stream record about its values. */
4044 register_odr_enum (tree t
)
4047 vec_safe_push (odr_enums
, t
);
4050 /* Write ODR enums to LTO stream file. */
4053 ipa_odr_summary_write (void)
4055 if (!odr_enums
&& !odr_enum_map
)
4057 struct output_block
*ob
= create_output_block (LTO_section_odr_types
);
4063 streamer_write_uhwi (ob
, odr_enums
->length ());
4065 /* For every ODR enum stream out
4068 - value names and constant their represent
4069 - bitpack of locations so we can do good diagnostics. */
4070 FOR_EACH_VEC_ELT (*odr_enums
, i
, t
)
4072 streamer_write_string (ob
, ob
->main_stream
,
4074 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
))),
4078 for (tree e
= TYPE_VALUES (t
); e
; e
= TREE_CHAIN (e
))
4080 streamer_write_uhwi (ob
, n
);
4081 for (tree e
= TYPE_VALUES (t
); e
; e
= TREE_CHAIN (e
))
4083 streamer_write_string (ob
, ob
->main_stream
,
4084 IDENTIFIER_POINTER (TREE_PURPOSE (e
)),
4086 streamer_write_wide_int (ob
,
4087 wi::to_wide (DECL_INITIAL
4091 bitpack_d bp
= bitpack_create (ob
->main_stream
);
4092 lto_output_location (ob
, &bp
, DECL_SOURCE_LOCATION (TYPE_NAME (t
)));
4093 for (tree e
= TYPE_VALUES (t
); e
; e
= TREE_CHAIN (e
))
4094 lto_output_location (ob
, &bp
,
4095 DECL_SOURCE_LOCATION (TREE_VALUE (e
)));
4096 streamer_write_bitpack (&bp
);
4098 vec_free (odr_enums
);
4101 /* During LTO incremental linking we already have streamed in types. */
4102 else if (odr_enum_map
)
4104 gcc_checking_assert (!odr_enums
);
4105 streamer_write_uhwi (ob
, odr_enum_map
->elements ());
4107 hash_map
<nofree_string_hash
, odr_enum
>::iterator iter
4108 = odr_enum_map
->begin ();
4109 for (; iter
!= odr_enum_map
->end (); ++iter
)
4111 odr_enum
&this_enum
= (*iter
).second
;
4112 streamer_write_string (ob
, ob
->main_stream
, (*iter
).first
, true);
4114 streamer_write_uhwi (ob
, this_enum
.vals
.length ());
4115 for (unsigned j
= 0; j
< this_enum
.vals
.length (); j
++)
4117 streamer_write_string (ob
, ob
->main_stream
,
4118 this_enum
.vals
[j
].name
, true);
4119 streamer_write_wide_int (ob
, this_enum
.vals
[j
].val
);
4122 bitpack_d bp
= bitpack_create (ob
->main_stream
);
4123 lto_output_location (ob
, &bp
, this_enum
.locus
);
4124 for (unsigned j
= 0; j
< this_enum
.vals
.length (); j
++)
4125 lto_output_location (ob
, &bp
, this_enum
.vals
[j
].locus
);
4126 streamer_write_bitpack (&bp
);
4129 delete odr_enum_map
;
4130 obstack_free (&odr_enum_obstack
, NULL
);
4131 odr_enum_map
= NULL
;
4134 produce_asm (ob
, NULL
);
4135 destroy_output_block (ob
);
4138 /* Write ODR enums from LTO stream file and warn on mismatches. */
4141 ipa_odr_read_section (struct lto_file_decl_data
*file_data
, const char *data
,
4144 const struct lto_function_header
*header
4145 = (const struct lto_function_header
*) data
;
4146 const int cfg_offset
= sizeof (struct lto_function_header
);
4147 const int main_offset
= cfg_offset
+ header
->cfg_size
;
4148 const int string_offset
= main_offset
+ header
->main_size
;
4149 class data_in
*data_in
;
4151 lto_input_block
ib ((const char *) data
+ main_offset
, header
->main_size
,
4155 = lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
4156 header
->string_size
, vNULL
);
4157 unsigned int n
= streamer_read_uhwi (&ib
);
4161 gcc_obstack_init (&odr_enum_obstack
);
4162 odr_enum_map
= new (hash_map
<nofree_string_hash
, odr_enum
>);
4165 for (unsigned i
= 0; i
< n
; i
++)
4167 const char *rname
= streamer_read_string (data_in
, &ib
);
4168 unsigned int nvals
= streamer_read_uhwi (&ib
);
4171 obstack_grow (&odr_enum_obstack
, rname
, strlen (rname
) + 1);
4172 name
= XOBFINISH (&odr_enum_obstack
, char *);
4175 class odr_enum
&this_enum
4176 = odr_enum_map
->get_or_insert (xstrdup (name
), &existed_p
);
4178 /* If this is first time we see the enum, remember its definition. */
4181 this_enum
.vals
.safe_grow_cleared (nvals
, true);
4182 this_enum
.warned
= false;
4184 fprintf (dump_file
, "enum %s\n{\n", name
);
4185 for (unsigned j
= 0; j
< nvals
; j
++)
4187 const char *val_name
= streamer_read_string (data_in
, &ib
);
4188 obstack_grow (&odr_enum_obstack
, val_name
, strlen (val_name
) + 1);
4189 this_enum
.vals
[j
].name
= XOBFINISH (&odr_enum_obstack
, char *);
4190 this_enum
.vals
[j
].val
= streamer_read_wide_int (&ib
);
4192 fprintf (dump_file
, " %s = " HOST_WIDE_INT_PRINT_DEC
",\n",
4193 val_name
, wi::fits_shwi_p (this_enum
.vals
[j
].val
)
4194 ? this_enum
.vals
[j
].val
.to_shwi () : -1);
4196 bitpack_d bp
= streamer_read_bitpack (&ib
);
4197 stream_input_location (&this_enum
.locus
, &bp
, data_in
);
4198 for (unsigned j
= 0; j
< nvals
; j
++)
4199 stream_input_location (&this_enum
.vals
[j
].locus
, &bp
, data_in
);
4200 data_in
->location_cache
.apply_location_cache ();
4202 fprintf (dump_file
, "}\n");
4204 /* If we already have definition, compare it with new one and output
4205 warnings if they differs. */
4208 int do_warning
= -1;
4209 char *warn_name
= NULL
;
4210 wide_int warn_value
= wi::zero (1);
4213 fprintf (dump_file
, "Comparing enum %s\n", name
);
4215 /* Look for differences which we will warn about later once locations
4217 for (unsigned j
= 0; j
< nvals
; j
++)
4219 const char *id
= streamer_read_string (data_in
, &ib
);
4220 wide_int val
= streamer_read_wide_int (&ib
);
4222 if (do_warning
!= -1 || j
>= this_enum
.vals
.length ())
4224 if (strcmp (id
, this_enum
.vals
[j
].name
)
4225 || (val
.get_precision() !=
4226 this_enum
.vals
[j
].val
.get_precision())
4227 || val
!= this_enum
.vals
[j
].val
)
4229 warn_name
= xstrdup (id
);
4233 fprintf (dump_file
, " Different on entry %i\n", j
);
4237 /* Stream in locations, but do not apply them unless we are going
4239 bitpack_d bp
= streamer_read_bitpack (&ib
);
4242 stream_input_location (&locus
, &bp
, data_in
);
4244 /* Did we find a difference? */
4245 if (do_warning
!= -1 || nvals
!= this_enum
.vals
.length ())
4247 data_in
->location_cache
.apply_location_cache ();
4249 const int opts
= DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
;
4250 char *dmgname
= cplus_demangle (name
, opts
);
4251 if (this_enum
.warned
4252 || !warning_at (this_enum
.locus
,
4253 OPT_Wodr
, "type %qs violates the "
4254 "C++ One Definition Rule",
4259 this_enum
.warned
= true;
4260 if (do_warning
== -1)
4262 "an enum with different number of values is defined"
4263 " in another translation unit");
4266 "an enum with different value name"
4267 " is defined in another translation unit");
4270 "an enum with different values"
4271 " is defined in another translation unit");
4275 data_in
->location_cache
.revert_location_cache ();
4277 /* Finally look up for location of the actual value that diverged. */
4278 for (unsigned j
= 0; j
< nvals
; j
++)
4280 location_t id_locus
;
4282 data_in
->location_cache
.revert_location_cache ();
4283 stream_input_location (&id_locus
, &bp
, data_in
);
4285 if ((int) j
== do_warning
)
4287 data_in
->location_cache
.apply_location_cache ();
4289 if (strcmp (warn_name
, this_enum
.vals
[j
].name
))
4290 inform (this_enum
.vals
[j
].locus
,
4291 "name %qs differs from name %qs defined"
4292 " in another translation unit",
4293 this_enum
.vals
[j
].name
, warn_name
);
4294 else if (this_enum
.vals
[j
].val
.get_precision() !=
4295 warn_value
.get_precision())
4296 inform (this_enum
.vals
[j
].locus
,
4297 "name %qs is defined as %u-bit while another "
4298 "translation unit defines it as %u-bit",
4299 warn_name
, this_enum
.vals
[j
].val
.get_precision(),
4300 warn_value
.get_precision());
4301 /* FIXME: In case there is easy way to print wide_ints,
4302 perhaps we could do it here instead of overflow check. */
4303 else if (wi::fits_shwi_p (this_enum
.vals
[j
].val
)
4304 && wi::fits_shwi_p (warn_value
))
4305 inform (this_enum
.vals
[j
].locus
,
4306 "name %qs is defined to %wd while another "
4307 "translation unit defines it as %wd",
4308 warn_name
, this_enum
.vals
[j
].val
.to_shwi (),
4309 warn_value
.to_shwi ());
4311 inform (this_enum
.vals
[j
].locus
,
4312 "name %qs is defined to different value "
4313 "in another translation unit",
4317 "mismatching definition");
4320 data_in
->location_cache
.revert_location_cache ();
4324 obstack_free (&odr_enum_obstack
, name
);
4327 lto_free_section_data (file_data
, LTO_section_ipa_fn_summary
, NULL
, data
,
4329 lto_data_in_delete (data_in
);
4332 /* Read all ODR type sections. */
4335 ipa_odr_summary_read (void)
4337 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
4338 struct lto_file_decl_data
*file_data
;
4341 while ((file_data
= file_data_vec
[j
++]))
4345 = lto_get_summary_section_data (file_data
, LTO_section_odr_types
,
4348 ipa_odr_read_section (file_data
, data
, len
);
4350 /* Enum info is used only to produce warnings. Only case we will need it
4351 again is streaming for incremental LTO. */
4352 if (flag_incremental_link
!= INCREMENTAL_LINK_LTO
)
4354 delete odr_enum_map
;
4355 obstack_free (&odr_enum_obstack
, NULL
);
4356 odr_enum_map
= NULL
;
4362 const pass_data pass_data_ipa_odr
=
4364 IPA_PASS
, /* type */
4366 OPTGROUP_NONE
, /* optinfo_flags */
4367 TV_IPA_ODR
, /* tv_id */
4368 0, /* properties_required */
4369 0, /* properties_provided */
4370 0, /* properties_destroyed */
4371 0, /* todo_flags_start */
4372 0, /* todo_flags_finish */
4375 class pass_ipa_odr
: public ipa_opt_pass_d
4378 pass_ipa_odr (gcc::context
*ctxt
)
4379 : ipa_opt_pass_d (pass_data_ipa_odr
, ctxt
,
4380 NULL
, /* generate_summary */
4381 ipa_odr_summary_write
, /* write_summary */
4382 ipa_odr_summary_read
, /* read_summary */
4383 NULL
, /* write_optimization_summary */
4384 NULL
, /* read_optimization_summary */
4385 NULL
, /* stmt_fixup */
4386 0, /* function_transform_todo_flags_start */
4387 NULL
, /* function_transform */
4388 NULL
) /* variable_transform */
4391 /* opt_pass methods: */
4392 bool gate (function
*) final override
4394 return (in_lto_p
|| flag_lto
);
4397 unsigned int execute (function
*) final override
4402 }; // class pass_ipa_odr
4407 make_pass_ipa_odr (gcc::context
*ctxt
)
4409 return new pass_ipa_odr (ctxt
);
4413 #include "gt-ipa-devirt.h"