1 /* Basic IPA utilities for type inheritance graph construction and
3 Copyright (C) 2013-2019 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 ODR = One Definition Rule
24 In short, the ODR states that:
25 1 In any translation unit, a template, type, function, or object can
26 have no more than one definition. Some of these can have any number
27 of declarations. A definition provides an instance.
28 2 In the entire program, an object or non-inline function cannot have
29 more than one definition; if an object or function is used, it must
30 have exactly one definition. You can declare an object or function
31 that is never used, in which case you don't have to provide
32 a definition. In no event can there be more than one definition.
33 3 Some things, like types, templates, and extern inline functions, can
34 be defined in more than one translation unit. For a given entity,
35 each definition must be the same. Non-extern objects and functions
36 in different translation units are different entities, even if their
37 names and types are the same.
40 This is the Gimple representation of type information of a polymorphic call.
41 It contains two parameters:
42 otr_type is a type of class whose method is called.
43 otr_token is the index into virtual table where address is taken.
46 This is the type inheritance information attached to each tree
47 RECORD_TYPE by the C++ frontend. It provides information about base
48 types and virtual tables.
50 BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
51 BINFO also links to its type by BINFO_TYPE and to the virtual table by
54 Base types of a given type are enumerated by BINFO_BASE_BINFO
55 vector. Members of this vectors are not BINFOs associated
56 with a base type. Rather they are new copies of BINFOs
57 (base BINFOs). Their virtual tables may differ from
58 virtual table of the base type. Also BINFO_OFFSET specifies
59 offset of the base within the type.
61 In the case of single inheritance, the virtual table is shared
62 and BINFO_VTABLE of base BINFO is NULL. In the case of multiple
63 inheritance the individual virtual tables are pointer to by
64 BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
65 binfo associated to the base type).
67 BINFO lookup for a given base type and offset can be done by
68 get_binfo_at_offset. It returns proper BINFO whose virtual table
69 can be used for lookup of virtual methods associated with the
73 This is an index of virtual method in virtual table associated
74 to the type defining it. Token can be looked up from OBJ_TYPE_REF
75 or from DECL_VINDEX of a given virtual table.
77 polymorphic (indirect) call
78 This is callgraph representation of virtual method call. Every
79 polymorphic call contains otr_type and otr_token taken from
80 original OBJ_TYPE_REF at callgraph construction time.
84 build_type_inheritance_graph triggers a construction of the type inheritance
87 We reconstruct it based on types of methods we see in the unit.
88 This means that the graph is not complete. Types with no methods are not
89 inserted into the graph. Also types without virtual methods are not
90 represented at all, though it may be easy to add this.
92 The inheritance graph is represented as follows:
94 Vertices are structures odr_type. Every odr_type may correspond
95 to one or more tree type nodes that are equivalent by ODR rule.
96 (the multiple type nodes appear only with linktime optimization)
98 Edges are represented by odr_type->base and odr_type->derived_types.
99 At the moment we do not track offsets of types for multiple inheritance.
102 possible_polymorphic_call_targets returns, given an parameters found in
103 indirect polymorphic edge all possible polymorphic call targets of the call.
105 pass_ipa_devirt performs simple speculative devirtualization.
110 #include "coretypes.h"
115 #include "alloc-pool.h"
116 #include "tree-pass.h"
118 #include "lto-streamer.h"
119 #include "fold-const.h"
120 #include "print-tree.h"
122 #include "ipa-utils.h"
123 #include "gimple-fold.h"
124 #include "symbol-summary.h"
125 #include "tree-vrp.h"
126 #include "ipa-prop.h"
127 #include "ipa-fnsummary.h"
128 #include "demangle.h"
130 #include "gimple-pretty-print.h"
132 #include "stringpool.h"
135 /* Hash based set of pairs of types. */
143 struct default_hash_traits
<type_pair
>
144 : typed_noop_remove
<type_pair
>
146 GTY((skip
)) typedef type_pair value_type
;
147 GTY((skip
)) typedef type_pair compare_type
;
151 return TYPE_UID (p
.first
) ^ TYPE_UID (p
.second
);
154 is_empty (type_pair p
)
156 return p
.first
== NULL
;
159 is_deleted (type_pair p ATTRIBUTE_UNUSED
)
164 equal (const type_pair
&a
, const type_pair
&b
)
166 return a
.first
==b
.first
&& a
.second
== b
.second
;
169 mark_empty (type_pair
&e
)
175 /* HACK alert: this is used to communicate with ipa-inline-transform that
176 thunk is being expanded and there is no need to clear the polymorphic
177 call target cache. */
178 bool thunk_expansion
;
180 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
181 hash_set
<type_pair
> *,
182 location_t
, location_t
);
183 static void warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
184 bool warn
, bool *warned
, const char *reason
);
186 static bool odr_violation_reported
= false;
189 /* Pointer set of all call targets appearing in the cache. */
190 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
192 /* The node of type inheritance graph. For each type unique in
193 One Definition Rule (ODR) sense, we produce one node linking all
194 main variants of types equivalent to it, bases and derived types. */
196 struct GTY(()) odr_type_d
200 /* All bases; built only for main variants of types. */
201 vec
<odr_type
> GTY((skip
)) bases
;
202 /* All derived types with virtual methods seen in unit;
203 built only for main variants of types. */
204 vec
<odr_type
> GTY((skip
)) derived_types
;
206 /* All equivalent types, if more than one. */
207 vec
<tree
, va_gc
> *types
;
208 /* Set of all equivalent types, if NON-NULL. */
209 hash_set
<tree
> * GTY((skip
)) types_set
;
211 /* Unique ID indexing the type in odr_types array. */
213 /* Is it in anonymous namespace? */
214 bool anonymous_namespace
;
215 /* Do we know about all derivations of given type? */
216 bool all_derivations_known
;
217 /* Did we report ODR violation here? */
219 /* Set when virtual table without RTTI previaled table with. */
221 /* Set when the canonical type is determined using the type name. */
225 /* Return TRUE if all derived types of T are known and thus
226 we may consider the walk of derived type complete.
228 This is typically true only for final anonymous namespace types and types
229 defined within functions (that may be COMDAT and thus shared across units,
230 but with the same set of derived types). */
233 type_all_derivations_known_p (const_tree t
)
235 if (TYPE_FINAL_P (t
))
239 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
240 if (!TYPE_NAME (t
) || TREE_CODE (TYPE_NAME (t
)) != TYPE_DECL
)
242 if (type_in_anonymous_namespace_p (t
))
244 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
247 /* Return TRUE if type's constructors are all visible. */
250 type_all_ctors_visible_p (tree t
)
253 && symtab
->state
>= CONSTRUCTION
254 /* We cannot always use type_all_derivations_known_p.
255 For function local types we must assume case where
256 the function is COMDAT and shared in between units.
258 TODO: These cases are quite easy to get, but we need
259 to keep track of C++ privatizing via -Wno-weak
260 as well as the IPA privatizing. */
261 && type_in_anonymous_namespace_p (t
);
264 /* Return TRUE if type may have instance. */
267 type_possibly_instantiated_p (tree t
)
272 /* TODO: Add abstract types here. */
273 if (!type_all_ctors_visible_p (t
))
276 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
277 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
278 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
279 vnode
= varpool_node::get (vtable
);
280 return vnode
&& vnode
->definition
;
283 /* Hash used to unify ODR types based on their mangled name and for anonymous
286 struct odr_name_hasher
: pointer_hash
<odr_type_d
>
288 typedef union tree_node
*compare_type
;
289 static inline hashval_t
hash (const odr_type_d
*);
290 static inline bool equal (const odr_type_d
*, const tree_node
*);
291 static inline void remove (odr_type_d
*);
295 can_be_name_hashed_p (tree t
)
297 return (!in_lto_p
|| odr_type_p (t
));
300 /* Hash type by its ODR name. */
303 hash_odr_name (const_tree t
)
305 gcc_checking_assert (TYPE_MAIN_VARIANT (t
) == t
);
307 /* If not in LTO, all main variants are unique, so we can do
310 return htab_hash_pointer (t
);
312 /* Anonymous types are unique. */
313 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
314 return htab_hash_pointer (t
);
316 gcc_checking_assert (TYPE_NAME (t
)
317 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
)));
318 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t
)));
321 /* Return the computed hashcode for ODR_TYPE. */
324 odr_name_hasher::hash (const odr_type_d
*odr_type
)
326 return hash_odr_name (odr_type
->type
);
329 /* For languages with One Definition Rule, work out if
330 types are the same based on their name.
332 This is non-trivial for LTO where minor differences in
333 the type representation may have prevented type merging
334 to merge two copies of otherwise equivalent type.
336 Until we start streaming mangled type names, this function works
337 only for polymorphic types.
341 types_same_for_odr (const_tree type1
, const_tree type2
)
343 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
345 type1
= TYPE_MAIN_VARIANT (type1
);
346 type2
= TYPE_MAIN_VARIANT (type2
);
354 /* Anonymous namespace types are never duplicated. */
355 if ((type_with_linkage_p (type1
) && type_in_anonymous_namespace_p (type1
))
356 || (type_with_linkage_p (type2
) && type_in_anonymous_namespace_p (type2
)))
359 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1
))
360 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2
)));
363 /* Return true if we can decide on ODR equivalency.
365 In non-LTO it is always decide, in LTO however it depends in the type has
366 ODR info attached. */
369 types_odr_comparable (tree t1
, tree t2
)
372 || TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
)
373 || (odr_type_p (TYPE_MAIN_VARIANT (t1
))
374 && odr_type_p (TYPE_MAIN_VARIANT (t2
))));
377 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
378 known, be conservative and return false. */
381 types_must_be_same_for_odr (tree t1
, tree t2
)
383 if (types_odr_comparable (t1
, t2
))
384 return types_same_for_odr (t1
, t2
);
386 return TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
);
389 /* If T is compound type, return type it is based on. */
392 compound_type_base (const_tree t
)
394 if (TREE_CODE (t
) == ARRAY_TYPE
395 || POINTER_TYPE_P (t
)
396 || TREE_CODE (t
) == COMPLEX_TYPE
397 || VECTOR_TYPE_P (t
))
398 return TREE_TYPE (t
);
399 if (TREE_CODE (t
) == METHOD_TYPE
)
400 return TYPE_METHOD_BASETYPE (t
);
401 if (TREE_CODE (t
) == OFFSET_TYPE
)
402 return TYPE_OFFSET_BASETYPE (t
);
406 /* Return true if T is either ODR type or compound type based from it.
407 If the function return true, we know that T is a type originating from C++
408 source even at link-time. */
411 odr_or_derived_type_p (const_tree t
)
415 if (odr_type_p (TYPE_MAIN_VARIANT (t
)))
417 /* Function type is a tricky one. Basically we can consider it
418 ODR derived if return type or any of the parameters is.
419 We need to check all parameters because LTO streaming merges
420 common types (such as void) and they are not considered ODR then. */
421 if (TREE_CODE (t
) == FUNCTION_TYPE
)
423 if (TYPE_METHOD_BASETYPE (t
))
424 t
= TYPE_METHOD_BASETYPE (t
);
427 if (TREE_TYPE (t
) && odr_or_derived_type_p (TREE_TYPE (t
)))
429 for (t
= TYPE_ARG_TYPES (t
); t
; t
= TREE_CHAIN (t
))
430 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t
))))
436 t
= compound_type_base (t
);
442 /* Compare types T1 and T2 and return true if they are
446 odr_name_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
450 gcc_checking_assert (TYPE_MAIN_VARIANT (t2
) == t2
);
451 gcc_checking_assert (TYPE_MAIN_VARIANT (t1
) == t1
);
456 /* Check for anonymous namespaces. */
457 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
458 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
460 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
)));
461 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
462 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
463 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
466 /* Free ODR type V. */
469 odr_name_hasher::remove (odr_type_d
*v
)
472 v
->derived_types
.release ();
478 /* ODR type hash used to look up ODR type based on tree type node. */
480 typedef hash_table
<odr_name_hasher
> odr_hash_type
;
481 static odr_hash_type
*odr_hash
;
483 /* ODR types are also stored into ODR_TYPE vector to allow consistent
484 walking. Bases appear before derived types. Vector is garbage collected
485 so we won't end up visiting empty types. */
487 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
488 #define odr_types (*odr_types_ptr)
490 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
492 set_type_binfo (tree type
, tree binfo
)
494 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
495 if (COMPLETE_TYPE_P (type
))
496 TYPE_BINFO (type
) = binfo
;
498 gcc_assert (!TYPE_BINFO (type
));
501 /* Return true if type variants match.
502 This assumes that we already verified that T1 and T2 are variants of the
506 type_variants_equivalent_p (tree t1
, tree t2
)
508 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
511 if (comp_type_attributes (t1
, t2
) != 1)
514 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
)
515 && TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
))
521 /* Compare T1 and T2 based on name or structure. */
524 odr_subtypes_equivalent_p (tree t1
, tree t2
,
525 hash_set
<type_pair
> *visited
,
526 location_t loc1
, location_t loc2
)
529 /* This can happen in incomplete types that should be handled earlier. */
530 gcc_assert (t1
&& t2
);
535 /* Anonymous namespace types must match exactly. */
536 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
537 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
538 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
539 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
542 /* For ODR types be sure to compare their names.
543 To support -Wno-odr-type-merging we allow one type to be non-ODR
544 and other ODR even though it is a violation. */
545 if (types_odr_comparable (t1
, t2
))
548 && odr_type_p (TYPE_MAIN_VARIANT (t1
))
549 && get_odr_type (TYPE_MAIN_VARIANT (t1
), true)->odr_violated
)
551 if (!types_same_for_odr (t1
, t2
))
553 if (!type_variants_equivalent_p (t1
, t2
))
555 /* Limit recursion: If subtypes are ODR types and we know
556 that they are same, be happy. */
557 if (odr_type_p (TYPE_MAIN_VARIANT (t1
)))
561 /* Component types, builtins and possibly violating ODR types
562 have to be compared structurally. */
563 if (TREE_CODE (t1
) != TREE_CODE (t2
))
565 if (AGGREGATE_TYPE_P (t1
)
566 && (TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
569 type_pair pair
={TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
)};
570 if (TYPE_UID (TYPE_MAIN_VARIANT (t1
)) > TYPE_UID (TYPE_MAIN_VARIANT (t2
)))
572 pair
.first
= TYPE_MAIN_VARIANT (t2
);
573 pair
.second
= TYPE_MAIN_VARIANT (t1
);
575 if (visited
->add (pair
))
577 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1
), TYPE_MAIN_VARIANT (t2
),
578 false, NULL
, visited
, loc1
, loc2
))
580 if (!type_variants_equivalent_p (t1
, t2
))
585 /* Return true if DECL1 and DECL2 are identical methods. Consider
586 name equivalent to name.localalias.xyz. */
589 methods_equal_p (tree decl1
, tree decl2
)
591 if (DECL_ASSEMBLER_NAME (decl1
) == DECL_ASSEMBLER_NAME (decl2
))
593 const char sep
= symbol_table::symbol_suffix_separator ();
595 const char *name1
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1
));
596 const char *ptr1
= strchr (name1
, sep
);
597 int len1
= ptr1
? ptr1
- name1
: strlen (name1
);
599 const char *name2
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2
));
600 const char *ptr2
= strchr (name2
, sep
);
601 int len2
= ptr2
? ptr2
- name2
: strlen (name2
);
605 return !strncmp (name1
, name2
, len1
);
608 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
609 violation warnings. */
612 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
616 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
618 odr_violation_reported
= true;
619 if (DECL_VIRTUAL_P (prevailing
->decl
))
621 varpool_node
*tmp
= prevailing
;
625 auto_diagnostic_group d
;
626 if (warning_at (DECL_SOURCE_LOCATION
627 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
629 "virtual table of type %qD violates one definition rule",
630 DECL_CONTEXT (vtable
->decl
)))
631 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
632 "variable of same assembler name as the virtual table is "
633 "defined in another translation unit");
636 if (!prevailing
->definition
|| !vtable
->definition
)
639 /* If we do not stream ODR type info, do not bother to do useful compare. */
640 if (!TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))
641 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))))
644 odr_type class_type
= get_odr_type (DECL_CONTEXT (vtable
->decl
), true);
646 if (class_type
->odr_violated
)
649 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
651 struct ipa_ref
*ref1
, *ref2
;
654 end1
= !prevailing
->iterate_reference (n1
, ref1
);
655 end2
= !vtable
->iterate_reference (n2
, ref2
);
657 /* !DECL_VIRTUAL_P means RTTI entry;
658 We warn when RTTI is lost because non-RTTI previals; we silently
659 accept the other case. */
662 || (methods_equal_p (ref1
->referred
->decl
,
663 ref2
->referred
->decl
)
664 && TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
))
665 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
667 if (!class_type
->rtti_broken
)
669 auto_diagnostic_group d
;
670 if (warning_at (DECL_SOURCE_LOCATION
671 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
673 "virtual table of type %qD contains RTTI "
675 DECL_CONTEXT (vtable
->decl
)))
677 inform (DECL_SOURCE_LOCATION
678 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
679 "but is prevailed by one without from other"
680 " translation unit");
681 inform (DECL_SOURCE_LOCATION
682 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
683 "RTTI will not work on this type");
684 class_type
->rtti_broken
= true;
688 end2
= !vtable
->iterate_reference (n2
, ref2
);
692 || (methods_equal_p (ref2
->referred
->decl
, ref1
->referred
->decl
)
693 && TREE_CODE (ref2
->referred
->decl
) == FUNCTION_DECL
))
694 && TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
)
697 end1
= !prevailing
->iterate_reference (n1
, ref1
);
703 /* Extra paranoia; compare the sizes. We do not have information
704 about virtual inheritance offsets, so just be sure that these
706 Do this as very last check so the not very informative error
707 is not output too often. */
708 if (DECL_SIZE (prevailing
->decl
) != DECL_SIZE (vtable
->decl
))
710 class_type
->odr_violated
= true;
711 auto_diagnostic_group d
;
712 tree ctx
= TYPE_NAME (DECL_CONTEXT (vtable
->decl
));
713 if (warning_at (DECL_SOURCE_LOCATION (ctx
), OPT_Wodr
,
714 "virtual table of type %qD violates "
715 "one definition rule",
716 DECL_CONTEXT (vtable
->decl
)))
718 ctx
= TYPE_NAME (DECL_CONTEXT (prevailing
->decl
));
719 inform (DECL_SOURCE_LOCATION (ctx
),
720 "the conflicting type defined in another translation"
721 " unit has virtual table of different size");
729 if (methods_equal_p (ref1
->referred
->decl
, ref2
->referred
->decl
))
732 class_type
->odr_violated
= true;
734 /* If the loops above stopped on non-virtual pointer, we have
735 mismatch in RTTI information mangling. */
736 if (TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
737 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
739 auto_diagnostic_group d
;
740 if (warning_at (DECL_SOURCE_LOCATION
741 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
743 "virtual table of type %qD violates "
744 "one definition rule",
745 DECL_CONTEXT (vtable
->decl
)))
747 inform (DECL_SOURCE_LOCATION
748 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
749 "the conflicting type defined in another translation "
750 "unit with different RTTI information");
754 /* At this point both REF1 and REF2 points either to virtual table
755 or virtual method. If one points to virtual table and other to
756 method we can complain the same way as if one table was shorter
757 than other pointing out the extra method. */
758 if (TREE_CODE (ref1
->referred
->decl
)
759 != TREE_CODE (ref2
->referred
->decl
))
761 if (VAR_P (ref1
->referred
->decl
))
763 else if (VAR_P (ref2
->referred
->decl
))
768 class_type
->odr_violated
= true;
770 /* Complain about size mismatch. Either we have too many virutal
771 functions or too many virtual table pointers. */
776 varpool_node
*tmp
= prevailing
;
781 auto_diagnostic_group d
;
782 if (warning_at (DECL_SOURCE_LOCATION
783 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
785 "virtual table of type %qD violates "
786 "one definition rule",
787 DECL_CONTEXT (vtable
->decl
)))
789 if (TREE_CODE (ref1
->referring
->decl
) == FUNCTION_DECL
)
791 inform (DECL_SOURCE_LOCATION
792 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
793 "the conflicting type defined in another translation "
795 inform (DECL_SOURCE_LOCATION
796 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
797 "contains additional virtual method %qD",
798 ref1
->referred
->decl
);
802 inform (DECL_SOURCE_LOCATION
803 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
804 "the conflicting type defined in another translation "
805 "unit has virtual table with more entries");
811 /* And in the last case we have either mismatch in between two virtual
812 methods or two virtual table pointers. */
813 auto_diagnostic_group d
;
814 if (warning_at (DECL_SOURCE_LOCATION
815 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), OPT_Wodr
,
816 "virtual table of type %qD violates "
817 "one definition rule",
818 DECL_CONTEXT (vtable
->decl
)))
820 if (TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
)
822 inform (DECL_SOURCE_LOCATION
823 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
824 "the conflicting type defined in another translation "
826 gcc_assert (TREE_CODE (ref2
->referred
->decl
)
828 inform (DECL_SOURCE_LOCATION
829 (ref1
->referred
->ultimate_alias_target ()->decl
),
830 "virtual method %qD",
831 ref1
->referred
->ultimate_alias_target ()->decl
);
832 inform (DECL_SOURCE_LOCATION
833 (ref2
->referred
->ultimate_alias_target ()->decl
),
834 "ought to match virtual method %qD but does not",
835 ref2
->referred
->ultimate_alias_target ()->decl
);
838 inform (DECL_SOURCE_LOCATION
839 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
840 "the conflicting type defined in another translation "
841 "unit has virtual table with different contents");
847 /* Output ODR violation warning about T1 and T2 with REASON.
848 Display location of ST1 and ST2 if REASON speaks about field or
850 If WARN is false, do nothing. Set WARNED if warning was indeed
854 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
855 bool warn
, bool *warned
, const char *reason
)
857 tree decl2
= TYPE_NAME (TYPE_MAIN_VARIANT (t2
));
861 if (!warn
|| !TYPE_NAME(TYPE_MAIN_VARIANT (t1
)))
864 /* ODR warnings are output druing LTO streaming; we must apply location
865 cache for potential warnings to be output correctly. */
866 if (lto_location_cache::current_cache
)
867 lto_location_cache::current_cache
->apply_location_cache ();
869 auto_diagnostic_group d
;
870 if (t1
!= TYPE_MAIN_VARIANT (t1
)
871 && TYPE_NAME (t1
) != TYPE_NAME (TYPE_MAIN_VARIANT (t1
)))
873 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1
))),
874 OPT_Wodr
, "type %qT (typedef of %qT) violates the "
875 "C++ One Definition Rule",
876 t1
, TYPE_MAIN_VARIANT (t1
)))
881 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1
))),
882 OPT_Wodr
, "type %qT violates the C++ One Definition Rule",
888 /* For FIELD_DECL support also case where one of fields is
889 NULL - this is used when the structures have mismatching number of
891 else if (!st1
|| TREE_CODE (st1
) == FIELD_DECL
)
893 inform (DECL_SOURCE_LOCATION (decl2
),
894 "a different type is defined in another translation unit");
900 inform (DECL_SOURCE_LOCATION (st1
),
901 "the first difference of corresponding definitions is field %qD",
906 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
908 inform (DECL_SOURCE_LOCATION (decl2
),
909 "a different type is defined in another translation unit");
910 inform (DECL_SOURCE_LOCATION (st1
),
911 "the first difference of corresponding definitions is method %qD",
917 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
923 /* Return ture if T1 and T2 are incompatible and we want to recusively
924 dive into them from warn_type_mismatch to give sensible answer. */
927 type_mismatch_p (tree t1
, tree t2
)
929 if (odr_or_derived_type_p (t1
) && odr_or_derived_type_p (t2
)
930 && !odr_types_equivalent_p (t1
, t2
))
932 return !types_compatible_p (t1
, t2
);
936 /* Types T1 and T2 was found to be incompatible in a context they can't
937 (either used to declare a symbol of same assembler name or unified by
938 ODR rule). We already output warning about this, but if possible, output
939 extra information on how the types mismatch.
941 This is hard to do in general. We basically handle the common cases.
943 If LOC1 and LOC2 are meaningful locations, use it in the case the types
944 themselves do no thave one.*/
947 warn_types_mismatch (tree t1
, tree t2
, location_t loc1
, location_t loc2
)
949 /* Location of type is known only if it has TYPE_NAME and the name is
951 location_t loc_t1
= TYPE_NAME (t1
) && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
952 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1
))
954 location_t loc_t2
= TYPE_NAME (t2
) && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
955 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2
))
957 bool loc_t2_useful
= false;
959 /* With LTO it is a common case that the location of both types match.
960 See if T2 has a location that is different from T1. If so, we will
961 inform user about the location.
962 Do not consider the location passed to us in LOC1/LOC2 as those are
964 if (loc_t2
> BUILTINS_LOCATION
&& loc_t2
!= loc_t1
)
966 if (loc_t1
<= BUILTINS_LOCATION
)
967 loc_t2_useful
= true;
970 expanded_location xloc1
= expand_location (loc_t1
);
971 expanded_location xloc2
= expand_location (loc_t2
);
973 if (strcmp (xloc1
.file
, xloc2
.file
)
974 || xloc1
.line
!= xloc2
.line
975 || xloc1
.column
!= xloc2
.column
)
976 loc_t2_useful
= true;
980 if (loc_t1
<= BUILTINS_LOCATION
)
982 if (loc_t2
<= BUILTINS_LOCATION
)
985 location_t loc
= loc_t1
<= BUILTINS_LOCATION
? loc_t2
: loc_t1
;
987 /* It is a quite common bug to reference anonymous namespace type in
988 non-anonymous namespace class. */
989 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
990 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
991 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
992 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
994 if (!type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
995 || !type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
998 std::swap (loc_t1
, loc_t2
);
1000 gcc_assert (TYPE_NAME (t1
)
1001 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
);
1002 tree n1
= TYPE_NAME (t1
);
1003 tree n2
= TYPE_NAME (t2
) ? TYPE_NAME (t2
) : NULL
;
1005 if (TREE_CODE (n1
) == TYPE_DECL
)
1006 n1
= DECL_NAME (n1
);
1007 if (n2
&& TREE_CODE (n2
) == TYPE_DECL
)
1008 n2
= DECL_NAME (n2
);
1009 /* Most of the time, the type names will match, do not be unnecesarily
1013 "type %qT defined in anonymous namespace cannot match "
1014 "type %qT across the translation unit boundary",
1018 "type %qT defined in anonymous namespace cannot match "
1019 "across the translation unit boundary",
1023 "the incompatible type defined in another translation unit");
1026 tree mt1
= TYPE_MAIN_VARIANT (t1
);
1027 tree mt2
= TYPE_MAIN_VARIANT (t2
);
1028 /* If types have mangled ODR names and they are different, it is most
1029 informative to output those.
1030 This also covers types defined in different namespaces. */
1031 if (TYPE_NAME (mt1
) && TYPE_NAME (mt2
)
1032 && TREE_CODE (TYPE_NAME (mt1
)) == TYPE_DECL
1033 && TREE_CODE (TYPE_NAME (mt2
)) == TYPE_DECL
1034 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt1
))
1035 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt2
))
1036 && DECL_ASSEMBLER_NAME (TYPE_NAME (mt1
))
1037 != DECL_ASSEMBLER_NAME (TYPE_NAME (mt2
)))
1039 char *name1
= xstrdup (cplus_demangle
1040 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt1
))),
1041 DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
));
1042 char *name2
= cplus_demangle
1043 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt2
))),
1044 DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
);
1045 if (name1
&& name2
&& strcmp (name1
, name2
))
1048 "type name %qs should match type name %qs",
1052 "the incompatible type is defined here");
1058 /* A tricky case are compound types. Often they appear the same in source
1059 code and the mismatch is dragged in by type they are build from.
1060 Look for those differences in subtypes and try to be informative. In other
1061 cases just output nothing because the source code is probably different
1062 and in this case we already output a all necessary info. */
1063 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
1065 if (TREE_CODE (t1
) == TREE_CODE (t2
))
1067 if (TREE_CODE (t1
) == ARRAY_TYPE
1068 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1070 tree i1
= TYPE_DOMAIN (t1
);
1071 tree i2
= TYPE_DOMAIN (t2
);
1074 && TYPE_MAX_VALUE (i1
)
1075 && TYPE_MAX_VALUE (i2
)
1076 && !operand_equal_p (TYPE_MAX_VALUE (i1
),
1077 TYPE_MAX_VALUE (i2
), 0))
1080 "array types have different bounds");
1084 if ((POINTER_TYPE_P (t1
) || TREE_CODE (t1
) == ARRAY_TYPE
)
1085 && type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1086 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
, loc_t2
);
1087 else if (TREE_CODE (t1
) == METHOD_TYPE
1088 || TREE_CODE (t1
) == FUNCTION_TYPE
)
1090 tree parms1
= NULL
, parms2
= NULL
;
1093 if (type_mismatch_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1095 inform (loc
, "return value type mismatch");
1096 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc_t1
,
1100 if (prototype_p (t1
) && prototype_p (t2
))
1101 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1103 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
),
1106 if (type_mismatch_p (TREE_VALUE (parms1
), TREE_VALUE (parms2
)))
1108 if (count
== 1 && TREE_CODE (t1
) == METHOD_TYPE
)
1110 "implicit this pointer type mismatch");
1113 "type mismatch in parameter %i",
1114 count
- (TREE_CODE (t1
) == METHOD_TYPE
));
1115 warn_types_mismatch (TREE_VALUE (parms1
),
1116 TREE_VALUE (parms2
),
1121 if (parms1
|| parms2
)
1124 "types have different parameter counts");
1132 if (types_odr_comparable (t1
, t2
)
1133 /* We make assign integers mangled names to be able to handle
1134 signed/unsigned chars. Accepting them here would however lead to
1135 confussing message like
1136 "type ‘const int’ itself violates the C++ One Definition Rule" */
1137 && TREE_CODE (t1
) != INTEGER_TYPE
1138 && types_same_for_odr (t1
, t2
))
1140 "type %qT itself violates the C++ One Definition Rule", t1
);
1141 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1142 else if (TYPE_NAME (t1
) == TYPE_NAME (t2
)
1143 && TREE_CODE (t1
) == TREE_CODE (t2
) && !loc_t2_useful
)
1146 inform (loc_t1
, "type %qT should match type %qT",
1149 inform (loc_t2
, "the incompatible type is defined here");
1152 /* Return true if T should be ignored in TYPE_FIELDS for ODR comparsion. */
1155 skip_in_fields_list_p (tree t
)
1157 if (TREE_CODE (t
) != FIELD_DECL
)
1159 /* C++ FE introduces zero sized fields depending on -std setting, see
1162 && integer_zerop (DECL_SIZE (t
))
1163 && DECL_ARTIFICIAL (t
)
1164 && DECL_IGNORED_P (t
)
1170 /* Compare T1 and T2, report ODR violations if WARN is true and set
1171 WARNED to true if anything is reported. Return true if types match.
1172 If true is returned, the types are also compatible in the sense of
1173 gimple_canonical_types_compatible_p.
1174 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1175 about the type if the type itself do not have location. */
1178 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
,
1179 hash_set
<type_pair
> *visited
,
1180 location_t loc1
, location_t loc2
)
1182 /* Check first for the obvious case of pointer identity. */
1186 /* Can't be the same type if the types don't have the same code. */
1187 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1189 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1190 G_("a different type is defined in another translation unit"));
1194 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1
))
1195 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1
)))
1196 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2
))
1197 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2
))))
1199 /* We cannot trip this when comparing ODR types, only when trying to
1200 match different ODR derivations from different declarations.
1201 So WARN should be always false. */
1206 if (TREE_CODE (t1
) == ENUMERAL_TYPE
1207 && TYPE_VALUES (t1
) && TYPE_VALUES (t2
))
1210 for (v1
= TYPE_VALUES (t1
), v2
= TYPE_VALUES (t2
);
1211 v1
&& v2
; v1
= TREE_CHAIN (v1
), v2
= TREE_CHAIN (v2
))
1213 if (TREE_PURPOSE (v1
) != TREE_PURPOSE (v2
))
1215 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1216 G_("an enum with different value name"
1217 " is defined in another translation unit"));
1220 if (!operand_equal_p (TREE_VALUE (v1
), TREE_VALUE (v2
), 0))
1222 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1223 G_("an enum with different values is defined"
1224 " in another translation unit"));
1230 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1231 G_("an enum with mismatching number of values "
1232 "is defined in another translation unit"));
1237 /* Non-aggregate types can be handled cheaply. */
1238 if (INTEGRAL_TYPE_P (t1
)
1239 || SCALAR_FLOAT_TYPE_P (t1
)
1240 || FIXED_POINT_TYPE_P (t1
)
1241 || TREE_CODE (t1
) == VECTOR_TYPE
1242 || TREE_CODE (t1
) == COMPLEX_TYPE
1243 || TREE_CODE (t1
) == OFFSET_TYPE
1244 || POINTER_TYPE_P (t1
))
1246 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
1248 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1249 G_("a type with different precision is defined "
1250 "in another translation unit"));
1253 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
1255 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1256 G_("a type with different signedness is defined "
1257 "in another translation unit"));
1261 if (TREE_CODE (t1
) == INTEGER_TYPE
1262 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
1264 /* char WRT uint_8? */
1265 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1266 G_("a different type is defined in another "
1267 "translation unit"));
1271 /* For canonical type comparisons we do not want to build SCCs
1272 so we cannot compare pointed-to types. But we can, for now,
1273 require the same pointed-to type kind and match what
1274 useless_type_conversion_p would do. */
1275 if (POINTER_TYPE_P (t1
))
1277 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
1278 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
1280 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1281 G_("it is defined as a pointer in different address "
1282 "space in another translation unit"));
1286 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1287 visited
, loc1
, loc2
))
1289 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1290 G_("it is defined as a pointer to different type "
1291 "in another translation unit"));
1293 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
),
1299 if ((TREE_CODE (t1
) == VECTOR_TYPE
|| TREE_CODE (t1
) == COMPLEX_TYPE
)
1300 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1301 visited
, loc1
, loc2
))
1303 /* Probably specific enough. */
1304 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1305 G_("a different type is defined "
1306 "in another translation unit"));
1308 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1312 /* Do type-specific comparisons. */
1313 else switch (TREE_CODE (t1
))
1317 /* Array types are the same if the element types are the same and
1318 the number of elements are the same. */
1319 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1320 visited
, loc1
, loc2
))
1322 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1323 G_("a different type is defined in another "
1324 "translation unit"));
1326 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1328 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
1329 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
1330 == TYPE_NONALIASED_COMPONENT (t2
));
1332 tree i1
= TYPE_DOMAIN (t1
);
1333 tree i2
= TYPE_DOMAIN (t2
);
1335 /* For an incomplete external array, the type domain can be
1336 NULL_TREE. Check this condition also. */
1337 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
1338 return type_variants_equivalent_p (t1
, t2
);
1340 tree min1
= TYPE_MIN_VALUE (i1
);
1341 tree min2
= TYPE_MIN_VALUE (i2
);
1342 tree max1
= TYPE_MAX_VALUE (i1
);
1343 tree max2
= TYPE_MAX_VALUE (i2
);
1345 /* In C++, minimums should be always 0. */
1346 gcc_assert (min1
== min2
);
1347 if (!operand_equal_p (max1
, max2
, 0))
1349 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1350 G_("an array of different size is defined "
1351 "in another translation unit"));
1359 /* Function types are the same if the return type and arguments types
1361 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1362 visited
, loc1
, loc2
))
1364 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1365 G_("has different return value "
1366 "in another translation unit"));
1368 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
), loc1
, loc2
);
1372 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
)
1373 || !prototype_p (t1
) || !prototype_p (t2
))
1374 return type_variants_equivalent_p (t1
, t2
);
1377 tree parms1
, parms2
;
1379 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1381 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
1383 if (!odr_subtypes_equivalent_p
1384 (TREE_VALUE (parms1
), TREE_VALUE (parms2
),
1385 visited
, loc1
, loc2
))
1387 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1388 G_("has different parameters in another "
1389 "translation unit"));
1391 warn_types_mismatch (TREE_VALUE (parms1
),
1392 TREE_VALUE (parms2
), loc1
, loc2
);
1397 if (parms1
|| parms2
)
1399 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1400 G_("has different parameters "
1401 "in another translation unit"));
1405 return type_variants_equivalent_p (t1
, t2
);
1410 case QUAL_UNION_TYPE
:
1414 /* For aggregate types, all the fields must be the same. */
1415 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1417 if (TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
1418 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
1419 != polymorphic_type_binfo_p (TYPE_BINFO (t2
)))
1421 if (polymorphic_type_binfo_p (TYPE_BINFO (t1
)))
1422 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1423 G_("a type defined in another translation unit "
1424 "is not polymorphic"));
1426 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1427 G_("a type defined in another translation unit "
1431 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1433 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1435 /* Skip non-fields. */
1436 while (f1
&& skip_in_fields_list_p (f1
))
1437 f1
= TREE_CHAIN (f1
);
1438 while (f2
&& skip_in_fields_list_p (f2
))
1439 f2
= TREE_CHAIN (f2
);
1442 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1444 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1445 G_("a type with different virtual table pointers"
1446 " is defined in another translation unit"));
1449 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
1451 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1452 G_("a type with different bases is defined "
1453 "in another translation unit"));
1456 if (DECL_NAME (f1
) != DECL_NAME (f2
)
1457 && !DECL_ARTIFICIAL (f1
))
1459 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1460 G_("a field with different name is defined "
1461 "in another translation unit"));
1464 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1466 visited
, loc1
, loc2
))
1468 /* Do not warn about artificial fields and just go into
1469 generic field mismatch warning. */
1470 if (DECL_ARTIFICIAL (f1
))
1473 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1474 G_("a field of same name but different type "
1475 "is defined in another translation unit"));
1477 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
), loc1
, loc2
);
1480 if (!gimple_compare_field_offset (f1
, f2
))
1482 /* Do not warn about artificial fields and just go into
1483 generic field mismatch warning. */
1484 if (DECL_ARTIFICIAL (f1
))
1486 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1487 G_("fields have different layout "
1488 "in another translation unit"));
1491 if (DECL_BIT_FIELD (f1
) != DECL_BIT_FIELD (f2
))
1493 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1494 G_("one field is a bitfield while the other "
1499 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
1500 == DECL_NONADDRESSABLE_P (f2
));
1503 /* If one aggregate has more fields than the other, they
1504 are not the same. */
1507 if ((f1
&& DECL_VIRTUAL_P (f1
)) || (f2
&& DECL_VIRTUAL_P (f2
)))
1508 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1509 G_("a type with different virtual table pointers"
1510 " is defined in another translation unit"));
1511 else if ((f1
&& DECL_ARTIFICIAL (f1
))
1512 || (f2
&& DECL_ARTIFICIAL (f2
)))
1513 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1514 G_("a type with different bases is defined "
1515 "in another translation unit"));
1517 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1518 G_("a type with different number of fields "
1519 "is defined in another translation unit"));
1535 /* Those are better to come last as they are utterly uninformative. */
1536 if (TYPE_SIZE (t1
) && TYPE_SIZE (t2
)
1537 && !operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0))
1539 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1540 G_("a type with different size "
1541 "is defined in another translation unit"));
1545 gcc_assert (!TYPE_SIZE_UNIT (t1
) || !TYPE_SIZE_UNIT (t2
)
1546 || operand_equal_p (TYPE_SIZE_UNIT (t1
),
1547 TYPE_SIZE_UNIT (t2
), 0));
1548 return type_variants_equivalent_p (t1
, t2
);
1551 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1554 odr_types_equivalent_p (tree type1
, tree type2
)
1556 gcc_checking_assert (odr_or_derived_type_p (type1
)
1557 && odr_or_derived_type_p (type2
));
1559 hash_set
<type_pair
> visited
;
1560 return odr_types_equivalent_p (type1
, type2
, false, NULL
,
1561 &visited
, UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1564 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1565 from VAL->type. This may happen in LTO where tree merging did not merge
1566 all variants of the same type or due to ODR violation.
1568 Analyze and report ODR violations and add type to duplicate list.
1569 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1570 this is first time we see definition of a class return true so the
1571 base types are analyzed. */
1574 add_type_duplicate (odr_type val
, tree type
)
1576 bool build_bases
= false;
1577 bool prevail
= false;
1578 bool odr_must_violate
= false;
1580 if (!val
->types_set
)
1581 val
->types_set
= new hash_set
<tree
>;
1583 /* Chose polymorphic type as leader (this happens only in case of ODR
1585 if ((TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1586 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
1587 && (TREE_CODE (val
->type
) != RECORD_TYPE
|| !TYPE_BINFO (val
->type
)
1588 || !polymorphic_type_binfo_p (TYPE_BINFO (val
->type
))))
1593 /* Always prefer complete type to be the leader. */
1594 else if (!COMPLETE_TYPE_P (val
->type
) && COMPLETE_TYPE_P (type
))
1597 if (TREE_CODE (type
) == RECORD_TYPE
)
1598 build_bases
= TYPE_BINFO (type
);
1600 else if (COMPLETE_TYPE_P (val
->type
) && !COMPLETE_TYPE_P (type
))
1602 else if (TREE_CODE (val
->type
) == ENUMERAL_TYPE
1603 && TREE_CODE (type
) == ENUMERAL_TYPE
1604 && !TYPE_VALUES (val
->type
) && TYPE_VALUES (type
))
1606 else if (TREE_CODE (val
->type
) == RECORD_TYPE
1607 && TREE_CODE (type
) == RECORD_TYPE
1608 && TYPE_BINFO (type
) && !TYPE_BINFO (val
->type
))
1610 gcc_assert (!val
->bases
.length ());
1616 std::swap (val
->type
, type
);
1618 val
->types_set
->add (type
);
1623 gcc_checking_assert (can_be_name_hashed_p (type
)
1624 && can_be_name_hashed_p (val
->type
));
1627 bool base_mismatch
= false;
1629 bool warned
= false;
1630 hash_set
<type_pair
> visited
;
1632 gcc_assert (in_lto_p
);
1633 vec_safe_push (val
->types
, type
);
1635 /* If both are class types, compare the bases. */
1636 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1637 && TREE_CODE (val
->type
) == RECORD_TYPE
1638 && TREE_CODE (type
) == RECORD_TYPE
1639 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1641 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1642 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1644 if (!flag_ltrans
&& !warned
&& !val
->odr_violated
)
1647 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1648 "a type with the same name but different "
1649 "number of polymorphic bases is "
1650 "defined in another translation unit");
1653 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1654 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1655 extra_base
= BINFO_BASE_BINFO
1657 BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)));
1659 extra_base
= BINFO_BASE_BINFO
1660 (TYPE_BINFO (val
->type
),
1661 BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1662 tree extra_base_type
= BINFO_TYPE (extra_base
);
1663 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type
)),
1664 "the extra base is defined here");
1667 base_mismatch
= true;
1670 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1672 tree base1
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
1673 tree base2
= BINFO_BASE_BINFO (TYPE_BINFO (val
->type
), i
);
1674 tree type1
= BINFO_TYPE (base1
);
1675 tree type2
= BINFO_TYPE (base2
);
1677 if (types_odr_comparable (type1
, type2
))
1679 if (!types_same_for_odr (type1
, type2
))
1680 base_mismatch
= true;
1683 if (!odr_types_equivalent_p (type1
, type2
))
1684 base_mismatch
= true;
1687 if (!warned
&& !val
->odr_violated
)
1689 warn_odr (type
, val
->type
, NULL
, NULL
,
1691 "a type with the same name but different base "
1692 "type is defined in another translation unit");
1694 warn_types_mismatch (type1
, type2
,
1695 UNKNOWN_LOCATION
, UNKNOWN_LOCATION
);
1699 if (BINFO_OFFSET (base1
) != BINFO_OFFSET (base2
))
1701 base_mismatch
= true;
1702 if (!warned
&& !val
->odr_violated
)
1703 warn_odr (type
, val
->type
, NULL
, NULL
,
1705 "a type with the same name but different base "
1706 "layout is defined in another translation unit");
1709 /* One of bases is not of complete type. */
1710 if (!TYPE_BINFO (type1
) != !TYPE_BINFO (type2
))
1712 /* If we have a polymorphic type info specified for TYPE1
1713 but not for TYPE2 we possibly missed a base when recording
1715 Be sure this does not happen. */
1716 if (TYPE_BINFO (type1
)
1717 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1719 odr_must_violate
= true;
1722 /* One base is polymorphic and the other not.
1723 This ought to be diagnosed earlier, but do not ICE in the
1725 else if (TYPE_BINFO (type1
)
1726 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1727 != polymorphic_type_binfo_p (TYPE_BINFO (type2
)))
1729 if (!warned
&& !val
->odr_violated
)
1730 warn_odr (type
, val
->type
, NULL
, NULL
,
1732 "a base of the type is polymorphic only in one "
1733 "translation unit");
1734 base_mismatch
= true;
1741 odr_violation_reported
= true;
1742 val
->odr_violated
= true;
1744 if (symtab
->dump_file
)
1746 fprintf (symtab
->dump_file
, "ODR base violation\n");
1748 print_node (symtab
->dump_file
, "", val
->type
, 0);
1749 putc ('\n',symtab
->dump_file
);
1750 print_node (symtab
->dump_file
, "", type
, 0);
1751 putc ('\n',symtab
->dump_file
);
1756 /* Next compare memory layout.
1757 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1758 We must apply the location cache to ensure that they are valid
1759 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1760 if (lto_location_cache::current_cache
)
1761 lto_location_cache::current_cache
->apply_location_cache ();
1762 /* As a special case we stream mangles names of integer types so we can see
1763 if they are believed to be same even though they have different
1764 representation. Avoid bogus warning on mismatches in these. */
1765 if (TREE_CODE (type
) != INTEGER_TYPE
1766 && TREE_CODE (val
->type
) != INTEGER_TYPE
1767 && !odr_types_equivalent_p (val
->type
, type
,
1768 !flag_ltrans
&& !val
->odr_violated
&& !warned
,
1770 DECL_SOURCE_LOCATION (TYPE_NAME (val
->type
)),
1771 DECL_SOURCE_LOCATION (TYPE_NAME (type
))))
1774 odr_violation_reported
= true;
1775 val
->odr_violated
= true;
1777 gcc_assert (val
->odr_violated
|| !odr_must_violate
);
1778 /* Sanity check that all bases will be build same way again. */
1780 && COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1781 && TREE_CODE (val
->type
) == RECORD_TYPE
1782 && TREE_CODE (type
) == RECORD_TYPE
1783 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1784 && !val
->odr_violated
1785 && !base_mismatch
&& val
->bases
.length ())
1787 unsigned int num_poly_bases
= 0;
1790 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1791 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1792 (TYPE_BINFO (type
), i
)))
1794 gcc_assert (num_poly_bases
== val
->bases
.length ());
1795 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
));
1797 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1798 (TYPE_BINFO (type
), i
)))
1800 odr_type base
= get_odr_type
1802 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1805 gcc_assert (val
->bases
[j
] == base
);
1811 /* Regularize things a little. During LTO same types may come with
1812 different BINFOs. Either because their virtual table was
1813 not merged by tree merging and only later at decl merging or
1814 because one type comes with external vtable, while other
1815 with internal. We want to merge equivalent binfos to conserve
1816 memory and streaming overhead.
1818 The external vtables are more harmful: they contain references
1819 to external declarations of methods that may be defined in the
1820 merged LTO unit. For this reason we absolutely need to remove
1821 them and replace by internal variants. Not doing so will lead
1822 to incomplete answers from possible_polymorphic_call_targets.
1824 FIXME: disable for now; because ODR types are now build during
1825 streaming in, the variants do not need to be linked to the type,
1826 yet. We need to do the merging in cleanup pass to be implemented
1828 if (!flag_ltrans
&& merge
1830 && TREE_CODE (val
->type
) == RECORD_TYPE
1831 && TREE_CODE (type
) == RECORD_TYPE
1832 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1833 && TYPE_MAIN_VARIANT (type
) == type
1834 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1835 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1836 && BINFO_VTABLE (TYPE_BINFO (type
)))
1838 tree master_binfo
= TYPE_BINFO (val
->type
);
1839 tree v1
= BINFO_VTABLE (master_binfo
);
1840 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1842 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1844 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1845 && operand_equal_p (TREE_OPERAND (v1
, 1),
1846 TREE_OPERAND (v2
, 1), 0));
1847 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1848 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1850 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1851 == DECL_ASSEMBLER_NAME (v2
));
1853 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1857 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1858 for (i
= 0; i
< val
->types
->length (); i
++)
1860 if (TYPE_BINFO ((*val
->types
)[i
])
1862 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1864 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1867 set_type_binfo (type
, master_binfo
);
1872 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
1875 obj_type_ref_class (const_tree ref
)
1877 gcc_checking_assert (TREE_CODE (ref
) == OBJ_TYPE_REF
);
1878 ref
= TREE_TYPE (ref
);
1879 gcc_checking_assert (TREE_CODE (ref
) == POINTER_TYPE
);
1880 ref
= TREE_TYPE (ref
);
1881 /* We look for type THIS points to. ObjC also builds
1882 OBJ_TYPE_REF with non-method calls, Their first parameter
1883 ID however also corresponds to class type. */
1884 gcc_checking_assert (TREE_CODE (ref
) == METHOD_TYPE
1885 || TREE_CODE (ref
) == FUNCTION_TYPE
);
1886 ref
= TREE_VALUE (TYPE_ARG_TYPES (ref
));
1887 gcc_checking_assert (TREE_CODE (ref
) == POINTER_TYPE
);
1888 tree ret
= TREE_TYPE (ref
);
1889 if (!in_lto_p
&& !TYPE_STRUCTURAL_EQUALITY_P (ret
))
1890 ret
= TYPE_CANONICAL (ret
);
1892 ret
= get_odr_type (ret
)->type
;
1896 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1897 possibly new entry. */
1900 get_odr_type (tree type
, bool insert
)
1902 odr_type_d
**slot
= NULL
;
1903 odr_type val
= NULL
;
1905 bool build_bases
= false;
1906 bool insert_to_odr_array
= false;
1909 type
= TYPE_MAIN_VARIANT (type
);
1910 if (!in_lto_p
&& !TYPE_STRUCTURAL_EQUALITY_P (type
))
1911 type
= TYPE_CANONICAL (type
);
1913 gcc_checking_assert (can_be_name_hashed_p (type
));
1915 hash
= hash_odr_name (type
);
1916 slot
= odr_hash
->find_slot_with_hash (type
, hash
,
1917 insert
? INSERT
: NO_INSERT
);
1922 /* See if we already have entry for type. */
1927 if (val
->type
!= type
&& insert
1928 && (!val
->types_set
|| !val
->types_set
->add (type
)))
1929 build_bases
= add_type_duplicate (val
, type
);
1933 val
= ggc_cleared_alloc
<odr_type_d
> ();
1936 val
->derived_types
= vNULL
;
1937 if (type_with_linkage_p (type
))
1938 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
1940 val
->anonymous_namespace
= 0;
1941 build_bases
= COMPLETE_TYPE_P (val
->type
);
1942 insert_to_odr_array
= true;
1946 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1947 && type_with_linkage_p (type
)
1948 && type
== TYPE_MAIN_VARIANT (type
))
1950 tree binfo
= TYPE_BINFO (type
);
1953 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) == type
);
1955 val
->all_derivations_known
= type_all_derivations_known_p (type
);
1956 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
1957 /* For now record only polymorphic types. other are
1958 pointless for devirtualization and we cannot precisely
1959 determine ODR equivalency of these during LTO. */
1960 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
1962 tree base_type
= BINFO_TYPE (BINFO_BASE_BINFO (binfo
, i
));
1963 odr_type base
= get_odr_type (base_type
, true);
1964 gcc_assert (TYPE_MAIN_VARIANT (base_type
) == base_type
);
1965 base
->derived_types
.safe_push (val
);
1966 val
->bases
.safe_push (base
);
1967 if (base
->id
> base_id
)
1971 /* Ensure that type always appears after bases. */
1972 if (insert_to_odr_array
)
1975 val
->id
= odr_types
.length ();
1976 vec_safe_push (odr_types_ptr
, val
);
1978 else if (base_id
> val
->id
)
1980 odr_types
[val
->id
] = 0;
1981 /* Be sure we did not recorded any derived types; these may need
1983 gcc_assert (val
->derived_types
.length() == 0);
1984 val
->id
= odr_types
.length ();
1985 vec_safe_push (odr_types_ptr
, val
);
1990 /* Return type that in ODR type hash prevailed TYPE. Be careful and punt
1991 on ODR violations. */
1994 prevailing_odr_type (tree type
)
1996 odr_type t
= get_odr_type (type
, false);
1997 if (!t
|| t
->odr_violated
)
2002 /* Set tbaa_enabled flag for TYPE. */
2005 enable_odr_based_tbaa (tree type
)
2007 odr_type t
= get_odr_type (type
, true);
2008 t
->tbaa_enabled
= true;
2011 /* True if canonical type of TYPE is determined using ODR name. */
2014 odr_based_tbaa_p (const_tree type
)
2016 if (!RECORD_OR_UNION_TYPE_P (type
))
2018 odr_type t
= get_odr_type (const_cast <tree
> (type
), false);
2019 if (!t
|| !t
->tbaa_enabled
)
2024 /* Set TYPE_CANONICAL of type and all its variants and duplicates
2028 set_type_canonical_for_odr_type (tree type
, tree canonical
)
2030 odr_type t
= get_odr_type (type
, false);
2034 for (tree t2
= t
->type
; t2
; t2
= TYPE_NEXT_VARIANT (t2
))
2035 TYPE_CANONICAL (t2
) = canonical
;
2037 FOR_EACH_VEC_ELT (*t
->types
, i
, tt
)
2038 for (tree t2
= tt
; t2
; t2
= TYPE_NEXT_VARIANT (t2
))
2039 TYPE_CANONICAL (t2
) = canonical
;
2042 /* Return true if we reported some ODR violation on TYPE. */
2045 odr_type_violation_reported_p (tree type
)
2047 return get_odr_type (type
, false)->odr_violated
;
2050 /* Add TYPE od ODR type hash. */
2053 register_odr_type (tree type
)
2056 odr_hash
= new odr_hash_type (23);
2057 if (type
== TYPE_MAIN_VARIANT (type
))
2059 /* To get ODR warings right, first register all sub-types. */
2060 if (RECORD_OR_UNION_TYPE_P (type
)
2061 && COMPLETE_TYPE_P (type
))
2063 /* Limit recursion on types which are already registered. */
2064 odr_type ot
= get_odr_type (type
, false);
2066 && (ot
->type
== type
2068 && ot
->types_set
->contains (type
))))
2070 for (tree f
= TYPE_FIELDS (type
); f
; f
= TREE_CHAIN (f
))
2071 if (TREE_CODE (f
) == FIELD_DECL
)
2073 tree subtype
= TREE_TYPE (f
);
2075 while (TREE_CODE (subtype
) == ARRAY_TYPE
)
2076 subtype
= TREE_TYPE (subtype
);
2077 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype
)))
2078 register_odr_type (TYPE_MAIN_VARIANT (subtype
));
2080 if (TYPE_BINFO (type
))
2081 for (unsigned int i
= 0;
2082 i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
2083 register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
2084 (TYPE_BINFO (type
), i
)));
2086 get_odr_type (type
, true);
2090 /* Return true if type is known to have no derivations. */
2093 type_known_to_have_no_derivations_p (tree t
)
2095 return (type_all_derivations_known_p (t
)
2096 && (TYPE_FINAL_P (t
)
2098 && !get_odr_type (t
, true)->derived_types
.length())));
2101 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2102 recursive printing. */
2105 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
2108 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
2109 print_generic_expr (f
, t
->type
, TDF_SLIM
);
2110 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
2111 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
2112 if (TYPE_NAME (t
->type
))
2114 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
->type
)))
2115 fprintf (f
, "%*s mangled name: %s\n", indent
* 2, "",
2117 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
->type
))));
2119 if (t
->bases
.length ())
2121 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
2122 for (i
= 0; i
< t
->bases
.length (); i
++)
2123 fprintf (f
, " %i", t
->bases
[i
]->id
);
2126 if (t
->derived_types
.length ())
2128 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
2129 for (i
= 0; i
< t
->derived_types
.length (); i
++)
2130 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
2135 /* Dump the type inheritance graph. */
2138 dump_type_inheritance_graph (FILE *f
)
2141 unsigned int num_all_types
= 0, num_types
= 0, num_duplicates
= 0;
2144 fprintf (f
, "\n\nType inheritance graph:\n");
2145 for (i
= 0; i
< odr_types
.length (); i
++)
2147 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
2148 dump_odr_type (f
, odr_types
[i
]);
2150 for (i
= 0; i
< odr_types
.length (); i
++)
2156 if (!odr_types
[i
]->types
|| !odr_types
[i
]->types
->length ())
2159 /* To aid ODR warnings we also mangle integer constants but do
2160 not consinder duplicates there. */
2161 if (TREE_CODE (odr_types
[i
]->type
) == INTEGER_TYPE
)
2164 /* It is normal to have one duplicate and one normal variant. */
2165 if (odr_types
[i
]->types
->length () == 1
2166 && COMPLETE_TYPE_P (odr_types
[i
]->type
)
2167 && !COMPLETE_TYPE_P ((*odr_types
[i
]->types
)[0]))
2173 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
2174 print_node (f
, "", odr_types
[i
]->type
, 0);
2175 print_node (f
, "", TYPE_NAME (odr_types
[i
]->type
), 0);
2177 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2181 fprintf (f
, "duplicate #%i\n", j
);
2182 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
2183 t
= (*odr_types
[i
]->types
)[j
];
2184 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
2186 t
= TYPE_CONTEXT (t
);
2187 print_node (f
, "", t
, 0);
2189 print_node (f
, "", TYPE_NAME ((*odr_types
[i
]->types
)[j
]), 0);
2193 fprintf (f
, "Out of %i types there are %i types with duplicates; "
2194 "%i duplicates overall\n", num_all_types
, num_types
, num_duplicates
);
2197 /* Save some WPA->ltrans streaming by freeing stuff needed only for good
2199 We free TYPE_VALUES of enums and also make TYPE_DECLs to not point back
2200 to the type (which is needed to keep them in the same SCC and preserve
2201 location information to output warnings) and subsequently we make all
2202 TYPE_DECLS of same assembler name equivalent. */
2205 free_odr_warning_data ()
2207 static bool odr_data_freed
= false;
2209 if (odr_data_freed
|| !flag_wpa
|| !odr_types_ptr
)
2212 odr_data_freed
= true;
2214 for (unsigned int i
= 0; i
< odr_types
.length (); i
++)
2217 tree t
= odr_types
[i
]->type
;
2219 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
2220 TYPE_VALUES (t
) = NULL
;
2221 TREE_TYPE (TYPE_NAME (t
)) = void_type_node
;
2223 if (odr_types
[i
]->types
)
2224 for (unsigned int j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2226 tree td
= (*odr_types
[i
]->types
)[j
];
2228 if (TREE_CODE (td
) == ENUMERAL_TYPE
)
2229 TYPE_VALUES (td
) = NULL
;
2230 TYPE_NAME (td
) = TYPE_NAME (t
);
2233 odr_data_freed
= true;
2236 /* Initialize IPA devirt and build inheritance tree graph. */
2239 build_type_inheritance_graph (void)
2241 struct symtab_node
*n
;
2242 FILE *inheritance_dump_file
;
2247 free_odr_warning_data ();
2250 timevar_push (TV_IPA_INHERITANCE
);
2251 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
2252 odr_hash
= new odr_hash_type (23);
2254 /* We reconstruct the graph starting of types of all methods seen in the
2257 if (is_a
<cgraph_node
*> (n
)
2258 && DECL_VIRTUAL_P (n
->decl
)
2259 && n
->real_symbol_p ())
2260 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
2262 /* Look also for virtual tables of types that do not define any methods.
2264 We need it in a case where class B has virtual base of class A
2265 re-defining its virtual method and there is class C with no virtual
2266 methods with B as virtual base.
2268 Here we output B's virtual method in two variant - for non-virtual
2269 and virtual inheritance. B's virtual table has non-virtual version,
2270 while C's has virtual.
2272 For this reason we need to know about C in order to include both
2273 variants of B. More correctly, record_target_from_binfo should
2274 add both variants of the method when walking B, but we have no
2275 link in between them.
2277 We rely on fact that either the method is exported and thus we
2278 assume it is called externally or C is in anonymous namespace and
2279 thus we will see the vtable. */
2281 else if (is_a
<varpool_node
*> (n
)
2282 && DECL_VIRTUAL_P (n
->decl
)
2283 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
2284 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
2285 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
2286 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
2287 if (inheritance_dump_file
)
2289 dump_type_inheritance_graph (inheritance_dump_file
);
2290 dump_end (TDI_inheritance
, inheritance_dump_file
);
2292 free_odr_warning_data ();
2293 timevar_pop (TV_IPA_INHERITANCE
);
2296 /* Return true if N has reference from live virtual table
2297 (and thus can be a destination of polymorphic call).
2298 Be conservatively correct when callgraph is not built or
2299 if the method may be referred externally. */
2302 referenced_from_vtable_p (struct cgraph_node
*node
)
2305 struct ipa_ref
*ref
;
2308 if (node
->externally_visible
2309 || DECL_EXTERNAL (node
->decl
)
2310 || node
->used_from_other_partition
)
2313 /* Keep this test constant time.
2314 It is unlikely this can happen except for the case where speculative
2315 devirtualization introduced many speculative edges to this node.
2316 In this case the target is very likely alive anyway. */
2317 if (node
->ref_list
.referring
.length () > 100)
2320 /* We need references built. */
2321 if (symtab
->state
<= CONSTRUCTION
)
2324 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
2325 if ((ref
->use
== IPA_REF_ALIAS
2326 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
2327 || (ref
->use
== IPA_REF_ADDR
2328 && VAR_P (ref
->referring
->decl
)
2329 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
2337 /* Return if TARGET is cxa_pure_virtual. */
2340 is_cxa_pure_virtual_p (tree target
)
2342 return target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
2343 && DECL_NAME (target
)
2344 && id_equal (DECL_NAME (target
),
2345 "__cxa_pure_virtual");
2348 /* If TARGET has associated node, record it in the NODES array.
2349 CAN_REFER specify if program can refer to the target directly.
2350 if TARGET is unknown (NULL) or it cannot be inserted (for example because
2351 its body was already removed and there is no way to refer to it), clear
2355 maybe_record_node (vec
<cgraph_node
*> &nodes
,
2356 tree target
, hash_set
<tree
> *inserted
,
2360 struct cgraph_node
*target_node
, *alias_target
;
2361 enum availability avail
;
2362 bool pure_virtual
= is_cxa_pure_virtual_p (target
);
2364 /* __builtin_unreachable do not need to be added into
2365 list of targets; the runtime effect of calling them is undefined.
2366 Only "real" virtual methods should be accounted. */
2367 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
&& !pure_virtual
)
2372 /* The only case when method of anonymous namespace becomes unreferable
2373 is when we completely optimized it out. */
2376 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2384 target_node
= cgraph_node::get (target
);
2386 /* Prefer alias target over aliases, so we do not get confused by
2390 alias_target
= target_node
->ultimate_alias_target (&avail
);
2391 if (target_node
!= alias_target
2392 && avail
>= AVAIL_AVAILABLE
2393 && target_node
->get_availability ())
2394 target_node
= alias_target
;
2397 /* Method can only be called by polymorphic call if any
2398 of vtables referring to it are alive.
2400 While this holds for non-anonymous functions, too, there are
2401 cases where we want to keep them in the list; for example
2402 inline functions with -fno-weak are static, but we still
2403 may devirtualize them when instance comes from other unit.
2404 The same holds for LTO.
2406 Currently we ignore these functions in speculative devirtualization.
2407 ??? Maybe it would make sense to be more aggressive for LTO even
2411 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
2413 || !referenced_from_vtable_p (target_node
)))
2415 /* See if TARGET is useful function we can deal with. */
2416 else if (target_node
!= NULL
2417 && (TREE_PUBLIC (target
)
2418 || DECL_EXTERNAL (target
)
2419 || target_node
->definition
)
2420 && target_node
->real_symbol_p ())
2422 gcc_assert (!target_node
->inlined_to
);
2423 gcc_assert (target_node
->real_symbol_p ());
2424 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2425 by valid program. */
2426 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
2428 /* Only add pure virtual if it is the only possible target. This way
2429 we will preserve the diagnostics about pure virtual called in many
2430 cases without disabling optimization in other. */
2431 else if (pure_virtual
)
2433 if (nodes
.length ())
2436 /* If we found a real target, take away cxa_pure_virtual. */
2437 else if (!pure_virtual
&& nodes
.length () == 1
2438 && is_cxa_pure_virtual_p (nodes
[0]->decl
))
2440 if (pure_virtual
&& nodes
.length ())
2442 if (!inserted
->add (target
))
2444 cached_polymorphic_call_targets
->add (target_node
);
2445 nodes
.safe_push (target_node
);
2448 else if (!completep
)
2450 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2451 optimized out or partitioned to other unit) so we cannot add it. When
2452 not sanitizing, there is nothing to do.
2453 Otherwise declare the list incomplete. */
2454 else if (pure_virtual
)
2456 if (flag_sanitize
& SANITIZE_UNREACHABLE
)
2459 else if (flag_ltrans
2460 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2464 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2465 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2466 method in vtable and insert method to NODES array
2467 or BASES_TO_CONSIDER if this array is non-NULL.
2468 Otherwise recurse to base BINFOs.
2469 This matches what get_binfo_at_offset does, but with offset
2472 TYPE_BINFOS is a stack of BINFOS of types with defined
2473 virtual table seen on way from class type to BINFO.
2475 MATCHED_VTABLES tracks virtual tables we already did lookup
2476 for virtual function in. INSERTED tracks nodes we already
2479 ANONYMOUS is true if BINFO is part of anonymous namespace.
2481 Clear COMPLETEP when we hit unreferable target.
2485 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
2486 vec
<tree
> *bases_to_consider
,
2489 vec
<tree
> &type_binfos
,
2490 HOST_WIDE_INT otr_token
,
2492 HOST_WIDE_INT offset
,
2493 hash_set
<tree
> *inserted
,
2494 hash_set
<tree
> *matched_vtables
,
2498 tree type
= BINFO_TYPE (binfo
);
2503 if (BINFO_VTABLE (binfo
))
2504 type_binfos
.safe_push (binfo
);
2505 if (types_same_for_odr (type
, outer_type
))
2508 tree type_binfo
= NULL
;
2510 /* Look up BINFO with virtual table. For normal types it is always last
2512 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
2513 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
2515 type_binfo
= type_binfos
[i
];
2518 if (BINFO_VTABLE (binfo
))
2520 /* If this is duplicated BINFO for base shared by virtual inheritance,
2521 we may not have its associated vtable. This is not a problem, since
2522 we will walk it on the other path. */
2525 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
2529 gcc_assert (odr_violation_reported
);
2532 /* For types in anonymous namespace first check if the respective vtable
2533 is alive. If not, we know the type can't be called. */
2534 if (!flag_ltrans
&& anonymous
)
2536 tree vtable
= BINFO_VTABLE (inner_binfo
);
2537 varpool_node
*vnode
;
2539 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
2540 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
2541 vnode
= varpool_node::get (vtable
);
2542 if (!vnode
|| !vnode
->definition
)
2545 gcc_assert (inner_binfo
);
2546 if (bases_to_consider
2547 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
2548 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
2551 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2554 if (!bases_to_consider
)
2555 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2556 /* Destructors are never called via construction vtables. */
2557 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
2558 bases_to_consider
->safe_push (target
);
2564 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2565 /* Walking bases that have no virtual method is pointless exercise. */
2566 if (polymorphic_type_binfo_p (base_binfo
))
2567 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
2569 otr_token
, outer_type
, offset
, inserted
,
2570 matched_vtables
, anonymous
, completep
);
2571 if (BINFO_VTABLE (binfo
))
2575 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2576 of TYPE, insert them to NODES, recurse into derived nodes.
2577 INSERTED is used to avoid duplicate insertions of methods into NODES.
2578 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2579 Clear COMPLETEP if unreferable target is found.
2581 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2582 all cases where BASE_SKIPPED is true (because the base is abstract
2586 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
2587 hash_set
<tree
> *inserted
,
2588 hash_set
<tree
> *matched_vtables
,
2591 HOST_WIDE_INT otr_token
,
2593 HOST_WIDE_INT offset
,
2595 vec
<tree
> &bases_to_consider
,
2596 bool consider_construction
)
2598 tree binfo
= TYPE_BINFO (type
->type
);
2600 auto_vec
<tree
, 8> type_binfos
;
2601 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
2603 /* We may need to consider types w/o instances because of possible derived
2604 types using their methods either directly or via construction vtables.
2605 We are safe to skip them when all derivations are known, since we will
2607 This is done by recording them to BASES_TO_CONSIDER array. */
2608 if (possibly_instantiated
|| consider_construction
)
2610 record_target_from_binfo (nodes
,
2611 (!possibly_instantiated
2612 && type_all_derivations_known_p (type
->type
))
2613 ? &bases_to_consider
: NULL
,
2614 binfo
, otr_type
, type_binfos
, otr_token
,
2616 inserted
, matched_vtables
,
2617 type
->anonymous_namespace
, completep
);
2619 for (i
= 0; i
< type
->derived_types
.length (); i
++)
2620 possible_polymorphic_call_targets_1 (nodes
, inserted
,
2623 type
->derived_types
[i
],
2624 otr_token
, outer_type
, offset
, completep
,
2625 bases_to_consider
, consider_construction
);
2628 /* Cache of queries for polymorphic call targets.
2630 Enumerating all call targets may get expensive when there are many
2631 polymorphic calls in the program, so we memoize all the previous
2632 queries and avoid duplicated work. */
2634 class polymorphic_call_target_d
2637 HOST_WIDE_INT otr_token
;
2638 ipa_polymorphic_call_context context
;
2640 vec
<cgraph_node
*> targets
;
2643 unsigned int n_odr_types
;
2648 /* Polymorphic call target cache helpers. */
2650 struct polymorphic_call_target_hasher
2651 : pointer_hash
<polymorphic_call_target_d
>
2653 static inline hashval_t
hash (const polymorphic_call_target_d
*);
2654 static inline bool equal (const polymorphic_call_target_d
*,
2655 const polymorphic_call_target_d
*);
2656 static inline void remove (polymorphic_call_target_d
*);
2659 /* Return the computed hashcode for ODR_QUERY. */
2662 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d
*odr_query
)
2664 inchash::hash
hstate (odr_query
->otr_token
);
2666 hstate
.add_hwi (odr_query
->type
->id
);
2667 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
2668 hstate
.add_hwi (odr_query
->context
.offset
);
2669 hstate
.add_hwi (odr_query
->n_odr_types
);
2671 if (odr_query
->context
.speculative_outer_type
)
2673 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
2674 hstate
.add_hwi (odr_query
->context
.speculative_offset
);
2676 hstate
.add_flag (odr_query
->speculative
);
2677 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
2678 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
2679 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
2680 hstate
.commit_flag ();
2681 return hstate
.end ();
2684 /* Compare cache entries T1 and T2. */
2687 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d
*t1
,
2688 const polymorphic_call_target_d
*t2
)
2690 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
2691 && t1
->speculative
== t2
->speculative
2692 && t1
->context
.offset
== t2
->context
.offset
2693 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
2694 && t1
->context
.outer_type
== t2
->context
.outer_type
2695 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
2696 && t1
->context
.maybe_in_construction
2697 == t2
->context
.maybe_in_construction
2698 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
2699 && (t1
->context
.speculative_maybe_derived_type
2700 == t2
->context
.speculative_maybe_derived_type
)
2701 /* Adding new type may affect outcome of target search. */
2702 && t1
->n_odr_types
== t2
->n_odr_types
);
2705 /* Remove entry in polymorphic call target cache hash. */
2708 polymorphic_call_target_hasher::remove (polymorphic_call_target_d
*v
)
2710 v
->targets
.release ();
2714 /* Polymorphic call target query cache. */
2716 typedef hash_table
<polymorphic_call_target_hasher
>
2717 polymorphic_call_target_hash_type
;
2718 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
2720 /* Destroy polymorphic call target query cache. */
2723 free_polymorphic_call_targets_hash ()
2725 if (cached_polymorphic_call_targets
)
2727 delete polymorphic_call_target_hash
;
2728 polymorphic_call_target_hash
= NULL
;
2729 delete cached_polymorphic_call_targets
;
2730 cached_polymorphic_call_targets
= NULL
;
2734 /* Force rebuilding type inheritance graph from scratch.
2735 This is use to make sure that we do not keep references to types
2736 which was not visible to free_lang_data. */
2739 rebuild_type_inheritance_graph ()
2745 odr_types_ptr
= NULL
;
2746 free_polymorphic_call_targets_hash ();
2749 /* When virtual function is removed, we may need to flush the cache. */
2752 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
2754 if (cached_polymorphic_call_targets
2756 && cached_polymorphic_call_targets
->contains (n
))
2757 free_polymorphic_call_targets_hash ();
2760 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2763 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2766 tree v
= BINFO_VTABLE (binfo
);
2769 unsigned HOST_WIDE_INT this_offset
;
2773 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2776 if (offset
== this_offset
2777 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2781 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2782 if (polymorphic_type_binfo_p (base_binfo
))
2784 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2791 /* T is known constant value of virtual table pointer.
2792 Store virtual table to V and its offset to OFFSET.
2793 Return false if T does not look like virtual table reference. */
2796 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2797 unsigned HOST_WIDE_INT
*offset
)
2799 /* We expect &MEM[(void *)&virtual_table + 16B].
2800 We obtain object's BINFO from the context of the virtual table.
2801 This one contains pointer to virtual table represented via
2802 POINTER_PLUS_EXPR. Verify that this pointer matches what
2803 we propagated through.
2805 In the case of virtual inheritance, the virtual tables may
2806 be nested, i.e. the offset may be different from 16 and we may
2807 need to dive into the type representation. */
2808 if (TREE_CODE (t
) == ADDR_EXPR
2809 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2810 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2811 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2812 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2814 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2815 (TREE_OPERAND (t
, 0), 0), 0)))
2817 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2818 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2822 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2823 We need to handle it when T comes from static variable initializer or
2825 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2827 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2828 t
= TREE_OPERAND (t
, 0);
2833 if (TREE_CODE (t
) != ADDR_EXPR
)
2835 *v
= TREE_OPERAND (t
, 0);
2839 /* T is known constant value of virtual table pointer. Return BINFO of the
2843 vtable_pointer_value_to_binfo (const_tree t
)
2846 unsigned HOST_WIDE_INT offset
;
2848 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2851 /* FIXME: for stores of construction vtables we return NULL,
2852 because we do not have BINFO for those. Eventually we should fix
2853 our representation to allow this case to be handled, too.
2854 In the case we see store of BINFO we however may assume
2855 that standard folding will be able to cope with it. */
2856 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2860 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2861 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2862 and insert them in NODES.
2864 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2867 record_targets_from_bases (tree otr_type
,
2868 HOST_WIDE_INT otr_token
,
2870 HOST_WIDE_INT offset
,
2871 vec
<cgraph_node
*> &nodes
,
2872 hash_set
<tree
> *inserted
,
2873 hash_set
<tree
> *matched_vtables
,
2878 HOST_WIDE_INT pos
, size
;
2882 if (types_same_for_odr (outer_type
, otr_type
))
2885 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
2887 if (TREE_CODE (fld
) != FIELD_DECL
)
2890 pos
= int_bit_position (fld
);
2891 size
= tree_to_shwi (DECL_SIZE (fld
));
2892 if (pos
<= offset
&& (pos
+ size
) > offset
2893 /* Do not get confused by zero sized bases. */
2894 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
2897 /* Within a class type we should always find corresponding fields. */
2898 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
2900 /* Nonbase types should have been stripped by outer_class_type. */
2901 gcc_assert (DECL_ARTIFICIAL (fld
));
2903 outer_type
= TREE_TYPE (fld
);
2906 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
2910 gcc_assert (odr_violation_reported
);
2913 gcc_assert (base_binfo
);
2914 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
2917 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2920 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
2921 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2922 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
2927 /* When virtual table is removed, we may need to flush the cache. */
2930 devirt_variable_node_removal_hook (varpool_node
*n
,
2931 void *d ATTRIBUTE_UNUSED
)
2933 if (cached_polymorphic_call_targets
2934 && DECL_VIRTUAL_P (n
->decl
)
2935 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
2936 free_polymorphic_call_targets_hash ();
2939 /* Record about how many calls would benefit from given type to be final. */
2941 struct odr_type_warn_count
2945 profile_count dyn_count
;
2948 /* Record about how many calls would benefit from given method to be final. */
2950 struct decl_warn_count
2954 profile_count dyn_count
;
2957 /* Information about type and decl warnings. */
2959 class final_warning_record
2962 /* If needed grow type_warnings vector and initialize new decl_warn_count
2963 to have dyn_count set to profile_count::zero (). */
2964 void grow_type_warnings (unsigned newlen
);
2966 profile_count dyn_count
;
2967 auto_vec
<odr_type_warn_count
> type_warnings
;
2968 hash_map
<tree
, decl_warn_count
> decl_warnings
;
2972 final_warning_record::grow_type_warnings (unsigned newlen
)
2974 unsigned len
= type_warnings
.length ();
2977 type_warnings
.safe_grow_cleared (newlen
);
2978 for (unsigned i
= len
; i
< newlen
; i
++)
2979 type_warnings
[i
].dyn_count
= profile_count::zero ();
2983 class final_warning_record
*final_warning_records
;
2985 /* Return vector containing possible targets of polymorphic call of type
2986 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2987 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2988 OTR_TYPE and include their virtual method. This is useful for types
2989 possibly in construction or destruction where the virtual table may
2990 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2991 us to walk the inheritance graph for all derivations.
2993 If COMPLETEP is non-NULL, store true if the list is complete.
2994 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2995 in the target cache. If user needs to visit every target list
2996 just once, it can memoize them.
2998 If SPECULATIVE is set, the list will not contain targets that
2999 are not speculatively taken.
3001 Returned vector is placed into cache. It is NOT caller's responsibility
3002 to free it. The vector can be freed on cgraph_remove_node call if
3003 the particular node is a virtual function present in the cache. */
3006 possible_polymorphic_call_targets (tree otr_type
,
3007 HOST_WIDE_INT otr_token
,
3008 ipa_polymorphic_call_context context
,
3013 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
3014 vec
<cgraph_node
*> nodes
= vNULL
;
3015 auto_vec
<tree
, 8> bases_to_consider
;
3016 odr_type type
, outer_type
;
3017 polymorphic_call_target_d key
;
3018 polymorphic_call_target_d
**slot
;
3022 bool can_refer
= false;
3023 bool skipped
= false;
3025 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
3027 /* If ODR is not initialized or the context is invalid, return empty
3029 if (!odr_hash
|| context
.invalid
|| !TYPE_BINFO (otr_type
))
3032 *completep
= context
.invalid
;
3034 *cache_token
= NULL
;
3038 /* Do not bother to compute speculative info when user do not asks for it. */
3039 if (!speculative
|| !context
.speculative_outer_type
)
3040 context
.clear_speculation ();
3042 type
= get_odr_type (otr_type
, true);
3044 /* Recording type variants would waste results cache. */
3045 gcc_assert (!context
.outer_type
3046 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3048 /* Look up the outer class type we want to walk.
3049 If we fail to do so, the context is invalid. */
3050 if ((context
.outer_type
|| context
.speculative_outer_type
)
3051 && !context
.restrict_to_inner_class (otr_type
))
3056 *cache_token
= NULL
;
3059 gcc_assert (!context
.invalid
);
3061 /* Check that restrict_to_inner_class kept the main variant. */
3062 gcc_assert (!context
.outer_type
3063 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3065 /* We canonicalize our query, so we do not need extra hashtable entries. */
3067 /* Without outer type, we have no use for offset. Just do the
3068 basic search from inner type. */
3069 if (!context
.outer_type
)
3070 context
.clear_outer_type (otr_type
);
3071 /* We need to update our hierarchy if the type does not exist. */
3072 outer_type
= get_odr_type (context
.outer_type
, true);
3073 /* If the type is complete, there are no derivations. */
3074 if (TYPE_FINAL_P (outer_type
->type
))
3075 context
.maybe_derived_type
= false;
3077 /* Initialize query cache. */
3078 if (!cached_polymorphic_call_targets
)
3080 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
3081 polymorphic_call_target_hash
3082 = new polymorphic_call_target_hash_type (23);
3083 if (!node_removal_hook_holder
)
3085 node_removal_hook_holder
=
3086 symtab
->add_cgraph_removal_hook (&devirt_node_removal_hook
, NULL
);
3087 symtab
->add_varpool_removal_hook (&devirt_variable_node_removal_hook
,
3094 if (context
.outer_type
!= otr_type
)
3096 = get_odr_type (context
.outer_type
, true)->type
;
3097 if (context
.speculative_outer_type
)
3098 context
.speculative_outer_type
3099 = get_odr_type (context
.speculative_outer_type
, true)->type
;
3102 /* Look up cached answer. */
3104 key
.otr_token
= otr_token
;
3105 key
.speculative
= speculative
;
3106 key
.context
= context
;
3107 key
.n_odr_types
= odr_types
.length ();
3108 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
3110 *cache_token
= (void *)*slot
;
3114 *completep
= (*slot
)->complete
;
3115 if ((*slot
)->type_warning
&& final_warning_records
)
3117 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
3118 if (!final_warning_records
->type_warnings
3119 [(*slot
)->type_warning
- 1].dyn_count
.initialized_p ())
3120 final_warning_records
->type_warnings
3121 [(*slot
)->type_warning
- 1].dyn_count
= profile_count::zero ();
3122 if (final_warning_records
->dyn_count
> 0)
3123 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3124 = final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3125 + final_warning_records
->dyn_count
;
3127 if (!speculative
&& (*slot
)->decl_warning
&& final_warning_records
)
3129 struct decl_warn_count
*c
=
3130 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
3132 if (final_warning_records
->dyn_count
> 0)
3133 c
->dyn_count
+= final_warning_records
->dyn_count
;
3135 return (*slot
)->targets
;
3140 /* Do actual search. */
3141 timevar_push (TV_IPA_VIRTUAL_CALL
);
3142 *slot
= XCNEW (polymorphic_call_target_d
);
3144 *cache_token
= (void *)*slot
;
3145 (*slot
)->type
= type
;
3146 (*slot
)->otr_token
= otr_token
;
3147 (*slot
)->context
= context
;
3148 (*slot
)->speculative
= speculative
;
3150 hash_set
<tree
> inserted
;
3151 hash_set
<tree
> matched_vtables
;
3153 /* First insert targets we speculatively identified as likely. */
3154 if (context
.speculative_outer_type
)
3156 odr_type speculative_outer_type
;
3157 bool speculation_complete
= true;
3159 /* First insert target from type itself and check if it may have
3161 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
3162 if (TYPE_FINAL_P (speculative_outer_type
->type
))
3163 context
.speculative_maybe_derived_type
= false;
3164 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
3165 context
.speculative_offset
, otr_type
);
3167 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3172 /* In the case we get complete method, we don't need
3173 to walk derivations. */
3174 if (target
&& DECL_FINAL_P (target
))
3175 context
.speculative_maybe_derived_type
= false;
3176 if (type_possibly_instantiated_p (speculative_outer_type
->type
))
3177 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &speculation_complete
);
3179 matched_vtables
.add (BINFO_VTABLE (binfo
));
3182 /* Next walk recursively all derived types. */
3183 if (context
.speculative_maybe_derived_type
)
3184 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
3185 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3188 speculative_outer_type
->derived_types
[i
],
3189 otr_token
, speculative_outer_type
->type
,
3190 context
.speculative_offset
,
3191 &speculation_complete
,
3196 if (!speculative
|| !nodes
.length ())
3198 /* First see virtual method of type itself. */
3199 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
3200 context
.offset
, otr_type
);
3202 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3206 gcc_assert (odr_violation_reported
);
3210 /* Destructors are never called through construction virtual tables,
3211 because the type is always known. */
3212 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
3213 context
.maybe_in_construction
= false;
3217 /* In the case we get complete method, we don't need
3218 to walk derivations. */
3219 if (DECL_FINAL_P (target
))
3220 context
.maybe_derived_type
= false;
3223 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3224 if (type_possibly_instantiated_p (outer_type
->type
))
3225 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3230 matched_vtables
.add (BINFO_VTABLE (binfo
));
3232 /* Next walk recursively all derived types. */
3233 if (context
.maybe_derived_type
)
3235 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
3236 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3239 outer_type
->derived_types
[i
],
3240 otr_token
, outer_type
->type
,
3241 context
.offset
, &complete
,
3243 context
.maybe_in_construction
);
3245 if (!outer_type
->all_derivations_known
)
3247 if (!speculative
&& final_warning_records
3248 && nodes
.length () == 1
3249 && TREE_CODE (TREE_TYPE (nodes
[0]->decl
)) == METHOD_TYPE
)
3252 && warn_suggest_final_types
3253 && !outer_type
->derived_types
.length ())
3255 final_warning_records
->grow_type_warnings
3257 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
3258 if (!final_warning_records
->type_warnings
3259 [outer_type
->id
].dyn_count
.initialized_p ())
3260 final_warning_records
->type_warnings
3261 [outer_type
->id
].dyn_count
= profile_count::zero ();
3262 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
3263 += final_warning_records
->dyn_count
;
3264 final_warning_records
->type_warnings
[outer_type
->id
].type
3266 (*slot
)->type_warning
= outer_type
->id
+ 1;
3269 && warn_suggest_final_methods
3270 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
3274 struct decl_warn_count
&c
=
3275 final_warning_records
->decl_warnings
.get_or_insert
3276 (nodes
[0]->decl
, &existed
);
3281 c
.dyn_count
+= final_warning_records
->dyn_count
;
3286 c
.dyn_count
= final_warning_records
->dyn_count
;
3287 c
.decl
= nodes
[0]->decl
;
3289 (*slot
)->decl_warning
= nodes
[0]->decl
;
3298 /* Destructors are never called through construction virtual tables,
3299 because the type is always known. One of entries may be
3300 cxa_pure_virtual so look to at least two of them. */
3301 if (context
.maybe_in_construction
)
3302 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
3303 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
3304 context
.maybe_in_construction
= false;
3305 if (context
.maybe_in_construction
)
3307 if (type
!= outer_type
3309 || (context
.maybe_derived_type
3310 && !type_all_derivations_known_p (outer_type
->type
))))
3311 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
3312 context
.offset
, nodes
, &inserted
,
3313 &matched_vtables
, &complete
);
3315 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3316 for (i
= 0; i
< bases_to_consider
.length(); i
++)
3317 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
3322 (*slot
)->targets
= nodes
;
3323 (*slot
)->complete
= complete
;
3324 (*slot
)->n_odr_types
= odr_types
.length ();
3326 *completep
= complete
;
3328 timevar_pop (TV_IPA_VIRTUAL_CALL
);
3333 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
3334 vec
<const decl_warn_count
*> *vec
)
3336 vec
->safe_push (&value
);
3340 /* Dump target list TARGETS into FILE. */
3343 dump_targets (FILE *f
, vec
<cgraph_node
*> targets
, bool verbose
)
3347 for (i
= 0; i
< targets
.length (); i
++)
3351 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
3352 fprintf (f
, " %s/%i", name
? name
: targets
[i
]->name (),
3356 if (!targets
[i
]->definition
)
3357 fprintf (f
, " (no definition%s)",
3358 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
3360 /* With many targets for every call polymorphic dumps are going to
3361 be quadratic in size. */
3362 if (i
> 10 && !verbose
)
3364 fprintf (f
, " ... and %i more targets\n", targets
.length () - i
);
3371 /* Dump all possible targets of a polymorphic call. */
3374 dump_possible_polymorphic_call_targets (FILE *f
,
3376 HOST_WIDE_INT otr_token
,
3377 const ipa_polymorphic_call_context
&ctx
,
3380 vec
<cgraph_node
*> targets
;
3382 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
3387 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3389 &final
, NULL
, false);
3390 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
3391 print_generic_expr (f
, type
->type
, TDF_SLIM
);
3392 fprintf (f
, " token %i\n", (int)otr_token
);
3396 fprintf (f
, " %s%s%s%s\n ",
3397 final
? "This is a complete list." :
3398 "This is partial list; extra targets may be defined in other units.",
3399 ctx
.maybe_in_construction
? " (base types included)" : "",
3400 ctx
.maybe_derived_type
? " (derived types included)" : "",
3401 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
3402 len
= targets
.length ();
3403 dump_targets (f
, targets
, verbose
);
3405 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3407 &final
, NULL
, true);
3408 if (targets
.length () != len
)
3410 fprintf (f
, " Speculative targets:");
3411 dump_targets (f
, targets
, verbose
);
3413 /* Ugly: during callgraph construction the target cache may get populated
3414 before all targets are found. While this is harmless (because all local
3415 types are discovered and only in those case we devirtualize fully and we
3416 don't do speculative devirtualization before IPA stage) it triggers
3417 assert here when dumping at that stage also populates the case with
3418 speculative targets. Quietly ignore this. */
3419 gcc_assert (symtab
->state
< IPA_SSA
|| targets
.length () <= len
);
3424 /* Return true if N can be possibly target of a polymorphic call of
3425 OTR_TYPE/OTR_TOKEN. */
3428 possible_polymorphic_call_target_p (tree otr_type
,
3429 HOST_WIDE_INT otr_token
,
3430 const ipa_polymorphic_call_context
&ctx
,
3431 struct cgraph_node
*n
)
3433 vec
<cgraph_node
*> targets
;
3437 if (fndecl_built_in_p (n
->decl
, BUILT_IN_UNREACHABLE
)
3438 || fndecl_built_in_p (n
->decl
, BUILT_IN_TRAP
))
3441 if (is_cxa_pure_virtual_p (n
->decl
))
3446 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
3447 for (i
= 0; i
< targets
.length (); i
++)
3448 if (n
->semantically_equivalent_p (targets
[i
]))
3451 /* At a moment we allow middle end to dig out new external declarations
3452 as a targets of polymorphic calls. */
3453 if (!final
&& !n
->definition
)
3460 /* Return true if N can be possibly target of a polymorphic call of
3461 OBJ_TYPE_REF expression REF in STMT. */
3464 possible_polymorphic_call_target_p (tree ref
,
3466 struct cgraph_node
*n
)
3468 ipa_polymorphic_call_context
context (current_function_decl
, ref
, stmt
);
3469 tree call_fn
= gimple_call_fn (stmt
);
3471 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn
),
3473 (OBJ_TYPE_REF_TOKEN (call_fn
)),
3479 /* After callgraph construction new external nodes may appear.
3480 Add them into the graph. */
3483 update_type_inheritance_graph (void)
3485 struct cgraph_node
*n
;
3489 free_polymorphic_call_targets_hash ();
3490 timevar_push (TV_IPA_INHERITANCE
);
3491 /* We reconstruct the graph starting from types of all methods seen in the
3493 FOR_EACH_FUNCTION (n
)
3494 if (DECL_VIRTUAL_P (n
->decl
)
3496 && n
->real_symbol_p ())
3497 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n
->decl
)), true);
3498 timevar_pop (TV_IPA_INHERITANCE
);
3502 /* Return true if N looks like likely target of a polymorphic call.
3503 Rule out cxa_pure_virtual, noreturns, function declared cold and
3504 other obvious cases. */
3507 likely_target_p (struct cgraph_node
*n
)
3510 /* cxa_pure_virtual and similar things are not likely. */
3511 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
3513 flags
= flags_from_decl_or_type (n
->decl
);
3514 if (flags
& ECF_NORETURN
)
3516 if (lookup_attribute ("cold",
3517 DECL_ATTRIBUTES (n
->decl
)))
3519 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
3521 /* If there are no live virtual tables referring the target,
3522 the only way the target can be called is an instance coming from other
3523 compilation unit; speculative devirtualization is built around an
3524 assumption that won't happen. */
3525 if (!referenced_from_vtable_p (n
))
3530 /* Compare type warning records P1 and P2 and choose one with larger count;
3531 helper for qsort. */
3534 type_warning_cmp (const void *p1
, const void *p2
)
3536 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
3537 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
3539 if (t1
->dyn_count
< t2
->dyn_count
)
3541 if (t1
->dyn_count
> t2
->dyn_count
)
3543 return t2
->count
- t1
->count
;
3546 /* Compare decl warning records P1 and P2 and choose one with larger count;
3547 helper for qsort. */
3550 decl_warning_cmp (const void *p1
, const void *p2
)
3552 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
3553 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
3555 if (t1
->dyn_count
< t2
->dyn_count
)
3557 if (t1
->dyn_count
> t2
->dyn_count
)
3559 return t2
->count
- t1
->count
;
3563 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3566 struct cgraph_node
*
3567 try_speculative_devirtualization (tree otr_type
, HOST_WIDE_INT otr_token
,
3568 ipa_polymorphic_call_context ctx
)
3570 vec
<cgraph_node
*>targets
3571 = possible_polymorphic_call_targets
3572 (otr_type
, otr_token
, ctx
, NULL
, NULL
, true);
3574 struct cgraph_node
*likely_target
= NULL
;
3576 for (i
= 0; i
< targets
.length (); i
++)
3577 if (likely_target_p (targets
[i
]))
3581 likely_target
= targets
[i
];
3584 ||!likely_target
->definition
3585 || DECL_EXTERNAL (likely_target
->decl
))
3588 /* Don't use an implicitly-declared destructor (c++/58678). */
3589 struct cgraph_node
*non_thunk_target
3590 = likely_target
->function_symbol ();
3591 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3593 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3594 && likely_target
->can_be_discarded_p ())
3596 return likely_target
;
3599 /* The ipa-devirt pass.
3600 When polymorphic call has only one likely target in the unit,
3601 turn it into a speculative call. */
3606 struct cgraph_node
*n
;
3607 hash_set
<void *> bad_call_targets
;
3608 struct cgraph_edge
*e
;
3610 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
3611 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
3612 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
3619 dump_type_inheritance_graph (dump_file
);
3621 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3622 This is implemented by setting up final_warning_records that are updated
3623 by get_polymorphic_call_targets.
3624 We need to clear cache in this case to trigger recomputation of all
3626 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3628 final_warning_records
= new (final_warning_record
);
3629 final_warning_records
->dyn_count
= profile_count::zero ();
3630 final_warning_records
->grow_type_warnings (odr_types
.length ());
3631 free_polymorphic_call_targets_hash ();
3634 FOR_EACH_DEFINED_FUNCTION (n
)
3636 bool update
= false;
3637 if (!opt_for_fn (n
->decl
, flag_devirtualize
))
3639 if (dump_file
&& n
->indirect_calls
)
3640 fprintf (dump_file
, "\n\nProcesing function %s\n",
3642 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
3643 if (e
->indirect_info
->polymorphic
)
3645 struct cgraph_node
*likely_target
= NULL
;
3649 if (final_warning_records
)
3650 final_warning_records
->dyn_count
= e
->count
.ipa ();
3652 vec
<cgraph_node
*>targets
3653 = possible_polymorphic_call_targets
3654 (e
, &final
, &cache_token
, true);
3657 /* Trigger warnings by calculating non-speculative targets. */
3658 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3659 possible_polymorphic_call_targets (e
);
3662 dump_possible_polymorphic_call_targets
3663 (dump_file
, e
, (dump_flags
& TDF_DETAILS
));
3667 /* See if the call can be devirtualized by means of ipa-prop's
3668 polymorphic call context propagation. If not, we can just
3669 forget about this call being polymorphic and avoid some heavy
3670 lifting in remove_unreachable_nodes that will otherwise try to
3671 keep all possible targets alive until inlining and in the inliner
3674 This may need to be revisited once we add further ways to use
3675 the may edges, but it is a resonable thing to do right now. */
3677 if ((e
->indirect_info
->param_index
== -1
3678 || (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
)
3679 && e
->indirect_info
->vptr_changed
))
3680 && !flag_ltrans_devirtualize
)
3682 e
->indirect_info
->polymorphic
= false;
3685 fprintf (dump_file
, "Dropping polymorphic call info;"
3686 " it cannot be used by ipa-prop\n");
3689 if (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
))
3692 if (!e
->maybe_hot_p ())
3695 fprintf (dump_file
, "Call is cold\n\n");
3702 fprintf (dump_file
, "Call is already speculated\n\n");
3705 /* When dumping see if we agree with speculation. */
3709 if (bad_call_targets
.contains (cache_token
))
3712 fprintf (dump_file
, "Target list is known to be useless\n\n");
3716 for (i
= 0; i
< targets
.length (); i
++)
3717 if (likely_target_p (targets
[i
]))
3721 likely_target
= NULL
;
3723 fprintf (dump_file
, "More than one likely target\n\n");
3727 likely_target
= targets
[i
];
3731 bad_call_targets
.add (cache_token
);
3734 /* This is reached only when dumping; check if we agree or disagree
3735 with the speculation. */
3738 struct cgraph_edge
*e2
;
3739 struct ipa_ref
*ref
;
3740 e
->speculative_call_info (e2
, e
, ref
);
3741 if (e2
->callee
->ultimate_alias_target ()
3742 == likely_target
->ultimate_alias_target ())
3744 fprintf (dump_file
, "We agree with speculation\n\n");
3749 fprintf (dump_file
, "We disagree with speculation\n\n");
3754 if (!likely_target
->definition
)
3757 fprintf (dump_file
, "Target is not a definition\n\n");
3761 /* Do not introduce new references to external symbols. While we
3762 can handle these just well, it is common for programs to
3763 incorrectly with headers defining methods they are linked
3765 if (DECL_EXTERNAL (likely_target
->decl
))
3768 fprintf (dump_file
, "Target is external\n\n");
3772 /* Don't use an implicitly-declared destructor (c++/58678). */
3773 struct cgraph_node
*non_thunk_target
3774 = likely_target
->function_symbol ();
3775 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3778 fprintf (dump_file
, "Target is artificial\n\n");
3782 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3783 && likely_target
->can_be_discarded_p ())
3786 fprintf (dump_file
, "Target is overwritable\n\n");
3790 else if (dbg_cnt (devirt
))
3792 if (dump_enabled_p ())
3794 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, e
->call_stmt
,
3795 "speculatively devirtualizing call "
3798 likely_target
->dump_name ());
3800 if (!likely_target
->can_be_discarded_p ())
3803 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
3805 likely_target
= alias
;
3810 (likely_target
, e
->count
.apply_scale (8, 10));
3814 ipa_update_overall_fn_summary (n
);
3816 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3818 if (warn_suggest_final_types
)
3820 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
3821 for (unsigned int i
= 0;
3822 i
< final_warning_records
->type_warnings
.length (); i
++)
3823 if (final_warning_records
->type_warnings
[i
].count
)
3825 tree type
= final_warning_records
->type_warnings
[i
].type
;
3826 int count
= final_warning_records
->type_warnings
[i
].count
;
3827 profile_count dyn_count
3828 = final_warning_records
->type_warnings
[i
].dyn_count
;
3830 if (!(dyn_count
> 0))
3831 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3832 OPT_Wsuggest_final_types
, count
,
3833 "Declaring type %qD final "
3834 "would enable devirtualization of %i call",
3835 "Declaring type %qD final "
3836 "would enable devirtualization of %i calls",
3840 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3841 OPT_Wsuggest_final_types
, count
,
3842 "Declaring type %qD final "
3843 "would enable devirtualization of %i call "
3844 "executed %lli times",
3845 "Declaring type %qD final "
3846 "would enable devirtualization of %i calls "
3847 "executed %lli times",
3850 (long long) dyn_count
.to_gcov_type ());
3854 if (warn_suggest_final_methods
)
3856 auto_vec
<const decl_warn_count
*> decl_warnings_vec
;
3858 final_warning_records
->decl_warnings
.traverse
3859 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3860 decl_warnings_vec
.qsort (decl_warning_cmp
);
3861 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3863 tree decl
= decl_warnings_vec
[i
]->decl
;
3864 int count
= decl_warnings_vec
[i
]->count
;
3865 profile_count dyn_count
3866 = decl_warnings_vec
[i
]->dyn_count
;
3868 if (!(dyn_count
> 0))
3869 if (DECL_CXX_DESTRUCTOR_P (decl
))
3870 warning_n (DECL_SOURCE_LOCATION (decl
),
3871 OPT_Wsuggest_final_methods
, count
,
3872 "Declaring virtual destructor of %qD final "
3873 "would enable devirtualization of %i call",
3874 "Declaring virtual destructor of %qD final "
3875 "would enable devirtualization of %i calls",
3876 DECL_CONTEXT (decl
), count
);
3878 warning_n (DECL_SOURCE_LOCATION (decl
),
3879 OPT_Wsuggest_final_methods
, count
,
3880 "Declaring method %qD final "
3881 "would enable devirtualization of %i call",
3882 "Declaring method %qD final "
3883 "would enable devirtualization of %i calls",
3885 else if (DECL_CXX_DESTRUCTOR_P (decl
))
3886 warning_n (DECL_SOURCE_LOCATION (decl
),
3887 OPT_Wsuggest_final_methods
, count
,
3888 "Declaring virtual destructor of %qD final "
3889 "would enable devirtualization of %i call "
3890 "executed %lli times",
3891 "Declaring virtual destructor of %qD final "
3892 "would enable devirtualization of %i calls "
3893 "executed %lli times",
3894 DECL_CONTEXT (decl
), count
,
3895 (long long)dyn_count
.to_gcov_type ());
3897 warning_n (DECL_SOURCE_LOCATION (decl
),
3898 OPT_Wsuggest_final_methods
, count
,
3899 "Declaring method %qD final "
3900 "would enable devirtualization of %i call "
3901 "executed %lli times",
3902 "Declaring method %qD final "
3903 "would enable devirtualization of %i calls "
3904 "executed %lli times",
3906 (long long)dyn_count
.to_gcov_type ());
3910 delete (final_warning_records
);
3911 final_warning_records
= 0;
3916 "%i polymorphic calls, %i devirtualized,"
3917 " %i speculatively devirtualized, %i cold\n"
3918 "%i have multiple targets, %i overwritable,"
3919 " %i already speculated (%i agree, %i disagree),"
3920 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3921 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3922 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3923 nexternal
, nnotdefined
, nartificial
, ndropped
);
3924 return ndevirtualized
|| ndropped
? TODO_remove_functions
: 0;
3929 const pass_data pass_data_ipa_devirt
=
3931 IPA_PASS
, /* type */
3932 "devirt", /* name */
3933 OPTGROUP_NONE
, /* optinfo_flags */
3934 TV_IPA_DEVIRT
, /* tv_id */
3935 0, /* properties_required */
3936 0, /* properties_provided */
3937 0, /* properties_destroyed */
3938 0, /* todo_flags_start */
3939 ( TODO_dump_symtab
), /* todo_flags_finish */
3942 class pass_ipa_devirt
: public ipa_opt_pass_d
3945 pass_ipa_devirt (gcc::context
*ctxt
)
3946 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3947 NULL
, /* generate_summary */
3948 NULL
, /* write_summary */
3949 NULL
, /* read_summary */
3950 NULL
, /* write_optimization_summary */
3951 NULL
, /* read_optimization_summary */
3952 NULL
, /* stmt_fixup */
3953 0, /* function_transform_todo_flags_start */
3954 NULL
, /* function_transform */
3955 NULL
) /* variable_transform */
3958 /* opt_pass methods: */
3959 virtual bool gate (function
*)
3961 /* In LTO, always run the IPA passes and decide on function basis if the
3965 return (flag_devirtualize
3966 && (flag_devirtualize_speculatively
3967 || (warn_suggest_final_methods
3968 || warn_suggest_final_types
))
3972 virtual unsigned int execute (function
*) { return ipa_devirt (); }
3974 }; // class pass_ipa_devirt
3979 make_pass_ipa_devirt (gcc::context
*ctxt
)
3981 return new pass_ipa_devirt (ctxt
);
3984 #include "gt-ipa-devirt.h"