1 /* Basic IPA utilities for type inheritance graph construction and
3 Copyright (C) 2013-2015 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 ODR = One Definition Rule
24 In short, the ODR states that:
25 1 In any translation unit, a template, type, function, or object can
26 have no more than one definition. Some of these can have any number
27 of declarations. A definition provides an instance.
28 2 In the entire program, an object or non-inline function cannot have
29 more than one definition; if an object or function is used, it must
30 have exactly one definition. You can declare an object or function
31 that is never used, in which case you don't have to provide
32 a definition. In no event can there be more than one definition.
33 3 Some things, like types, templates, and extern inline functions, can
34 be defined in more than one translation unit. For a given entity,
35 each definition must be the same. Non-extern objects and functions
36 in different translation units are different entities, even if their
37 names and types are the same.
40 This is the Gimple representation of type information of a polymorphic call.
41 It contains two parameters:
42 otr_type is a type of class whose method is called.
43 otr_token is the index into virtual table where address is taken.
46 This is the type inheritance information attached to each tree
47 RECORD_TYPE by the C++ frontend. It provides information about base
48 types and virtual tables.
50 BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
51 BINFO also links to its type by BINFO_TYPE and to the virtual table by
54 Base types of a given type are enumerated by BINFO_BASE_BINFO
55 vector. Members of this vectors are not BINFOs associated
56 with a base type. Rather they are new copies of BINFOs
57 (base BINFOs). Their virtual tables may differ from
58 virtual table of the base type. Also BINFO_OFFSET specifies
59 offset of the base within the type.
61 In the case of single inheritance, the virtual table is shared
62 and BINFO_VTABLE of base BINFO is NULL. In the case of multiple
63 inheritance the individual virtual tables are pointer to by
64 BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
65 binfo associated to the base type).
67 BINFO lookup for a given base type and offset can be done by
68 get_binfo_at_offset. It returns proper BINFO whose virtual table
69 can be used for lookup of virtual methods associated with the
73 This is an index of virtual method in virtual table associated
74 to the type defining it. Token can be looked up from OBJ_TYPE_REF
75 or from DECL_VINDEX of a given virtual table.
77 polymorphic (indirect) call
78 This is callgraph representation of virtual method call. Every
79 polymorphic call contains otr_type and otr_token taken from
80 original OBJ_TYPE_REF at callgraph construction time.
84 build_type_inheritance_graph triggers a construction of the type inheritance
87 We reconstruct it based on types of methods we see in the unit.
88 This means that the graph is not complete. Types with no methods are not
89 inserted into the graph. Also types without virtual methods are not
90 represented at all, though it may be easy to add this.
92 The inheritance graph is represented as follows:
94 Vertices are structures odr_type. Every odr_type may correspond
95 to one or more tree type nodes that are equivalent by ODR rule.
96 (the multiple type nodes appear only with linktime optimization)
98 Edges are represented by odr_type->base and odr_type->derived_types.
99 At the moment we do not track offsets of types for multiple inheritance.
102 possible_polymorphic_call_targets returns, given an parameters found in
103 indirect polymorphic edge all possible polymorphic call targets of the call.
105 pass_ipa_devirt performs simple speculative devirtualization.
110 #include "coretypes.h"
112 #include "hash-set.h"
113 #include "machmode.h"
114 #include "hash-map.h"
116 #include "double-int.h"
120 #include "wide-int.h"
123 #include "fold-const.h"
124 #include "print-tree.h"
127 #include "basic-block.h"
129 #include "plugin-api.h"
130 #include "hard-reg-set.h"
131 #include "function.h"
137 #include "statistics.h"
139 #include "fixed-value.h"
140 #include "insn-config.h"
144 #include "emit-rtl.h"
148 #include "tree-pass.h"
150 #include "hash-table.h"
151 #include "tree-pretty-print.h"
152 #include "ipa-utils.h"
153 #include "tree-ssa-alias.h"
154 #include "internal-fn.h"
155 #include "gimple-fold.h"
156 #include "gimple-expr.h"
158 #include "alloc-pool.h"
159 #include "symbol-summary.h"
160 #include "ipa-prop.h"
161 #include "ipa-inline.h"
162 #include "diagnostic.h"
163 #include "tree-dfa.h"
164 #include "demangle.h"
166 #include "gimple-pretty-print.h"
167 #include "stor-layout.h"
169 #include "streamer-hooks.h"
170 #include "lto-streamer.h"
172 /* Hash based set of pairs of types. */
179 struct pair_traits
: default_hashset_traits
184 return TYPE_UID (p
.first
) ^ TYPE_UID (p
.second
);
187 is_empty (type_pair p
)
189 return p
.first
== NULL
;
192 is_deleted (type_pair p ATTRIBUTE_UNUSED
)
197 equal (const type_pair
&a
, const type_pair
&b
)
199 return a
.first
==b
.first
&& a
.second
== b
.second
;
202 mark_empty (type_pair
&e
)
208 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
209 hash_set
<type_pair
,pair_traits
> *);
211 static bool odr_violation_reported
= false;
214 /* Pointer set of all call targets appearing in the cache. */
215 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
217 /* The node of type inheritance graph. For each type unique in
218 One Definition Rule (ODR) sense, we produce one node linking all
219 main variants of types equivalent to it, bases and derived types. */
221 struct GTY(()) odr_type_d
225 /* All bases; built only for main variants of types. */
226 vec
<odr_type
> GTY((skip
)) bases
;
227 /* All derived types with virtual methods seen in unit;
228 built only for main variants of types. */
229 vec
<odr_type
> GTY((skip
)) derived_types
;
231 /* All equivalent types, if more than one. */
232 vec
<tree
, va_gc
> *types
;
233 /* Set of all equivalent types, if NON-NULL. */
234 hash_set
<tree
> * GTY((skip
)) types_set
;
236 /* Unique ID indexing the type in odr_types array. */
238 /* Is it in anonymous namespace? */
239 bool anonymous_namespace
;
240 /* Do we know about all derivations of given type? */
241 bool all_derivations_known
;
242 /* Did we report ODR violation here? */
244 /* Set when virtual table without RTTI previaled table with. */
248 /* Return true if T is a type with linkage defined. */
251 type_with_linkage_p (const_tree t
)
253 return (RECORD_OR_UNION_TYPE_P (t
)
254 || TREE_CODE (t
) == ENUMERAL_TYPE
);
257 /* Return true if T is in anonymous namespace.
258 This works only on those C++ types with linkage defined. */
261 type_in_anonymous_namespace_p (const_tree t
)
263 gcc_assert (type_with_linkage_p (t
));
264 /* TREE_PUBLIC of TYPE_STUB_DECL may not be properly set for
265 backend produced types (such as va_arg_type); those have CONTEXT NULL
266 and never are considered anonymoius. */
267 if (!TYPE_CONTEXT (t
))
269 return (TYPE_STUB_DECL (t
) && !TREE_PUBLIC (TYPE_STUB_DECL (t
)));
272 /* Return true of T is type with One Definition Rule info attached.
273 It means that either it is anonymous type or it has assembler name
277 odr_type_p (const_tree t
)
279 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
281 /* We do not have this information when not in LTO, but we do not need
282 to care, since it is used only for type merging. */
283 gcc_checking_assert (in_lto_p
|| flag_lto
);
285 return (TYPE_NAME (t
)
286 && (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
))));
289 /* Return TRUE if all derived types of T are known and thus
290 we may consider the walk of derived type complete.
292 This is typically true only for final anonymous namespace types and types
293 defined within functions (that may be COMDAT and thus shared across units,
294 but with the same set of derived types). */
297 type_all_derivations_known_p (const_tree t
)
299 if (TYPE_FINAL_P (t
))
303 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
304 if (!TYPE_NAME (t
) || TREE_CODE (TYPE_NAME (t
)) != TYPE_DECL
)
306 if (type_in_anonymous_namespace_p (t
))
308 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
311 /* Return TRUE if type's constructors are all visible. */
314 type_all_ctors_visible_p (tree t
)
317 && symtab
->state
>= CONSTRUCTION
318 /* We can not always use type_all_derivations_known_p.
319 For function local types we must assume case where
320 the function is COMDAT and shared in between units.
322 TODO: These cases are quite easy to get, but we need
323 to keep track of C++ privatizing via -Wno-weak
324 as well as the IPA privatizing. */
325 && type_in_anonymous_namespace_p (t
);
328 /* Return TRUE if type may have instance. */
331 type_possibly_instantiated_p (tree t
)
336 /* TODO: Add abstract types here. */
337 if (!type_all_ctors_visible_p (t
))
340 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
341 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
342 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
343 vnode
= varpool_node::get (vtable
);
344 return vnode
&& vnode
->definition
;
347 /* Hash used to unify ODR types based on their mangled name and for anonymous
350 struct odr_name_hasher
352 typedef odr_type_d
*value_type
;
353 typedef union tree_node
*compare_type
;
354 static inline hashval_t
hash (const odr_type_d
*);
355 static inline bool equal (const odr_type_d
*, const tree_node
*);
356 static inline void remove (odr_type_d
*);
359 /* Has used to unify ODR types based on their associated virtual table.
360 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
361 only polymorphic types. Types with mangled names are inserted to both. */
363 struct odr_vtable_hasher
:odr_name_hasher
365 static inline hashval_t
hash (const odr_type_d
*);
366 static inline bool equal (const odr_type_d
*, const tree_node
*);
369 /* Return type that was declared with T's name so that T is an
370 qualified variant of it. */
373 main_odr_variant (const_tree t
)
375 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
376 return TREE_TYPE (TYPE_NAME (t
));
377 /* Unnamed types and non-C++ produced types can be compared by variants. */
379 return TYPE_MAIN_VARIANT (t
);
383 can_be_name_hashed_p (tree t
)
385 return (!in_lto_p
|| odr_type_p (t
));
388 /* Hash type by its ODR name. */
391 hash_odr_name (const_tree t
)
393 gcc_checking_assert (main_odr_variant (t
) == t
);
395 /* If not in LTO, all main variants are unique, so we can do
398 return htab_hash_pointer (t
);
400 /* Anonymous types are unique. */
401 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
402 return htab_hash_pointer (t
);
404 gcc_checking_assert (TYPE_NAME (t
)
405 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
)));
406 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t
)));
409 /* Return the computed hashcode for ODR_TYPE. */
412 odr_name_hasher::hash (const odr_type_d
*odr_type
)
414 return hash_odr_name (odr_type
->type
);
418 can_be_vtable_hashed_p (tree t
)
420 /* vtable hashing can distinguish only main variants. */
421 if (TYPE_MAIN_VARIANT (t
) != t
)
423 /* Anonymous namespace types are always handled by name hash. */
424 if (type_with_linkage_p (t
) && type_in_anonymous_namespace_p (t
))
426 return (TREE_CODE (t
) == RECORD_TYPE
427 && TYPE_BINFO (t
) && BINFO_VTABLE (TYPE_BINFO (t
)));
430 /* Hash type by assembler name of its vtable. */
433 hash_odr_vtable (const_tree t
)
435 tree v
= BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t
)));
436 inchash::hash hstate
;
438 gcc_checking_assert (in_lto_p
);
439 gcc_checking_assert (!type_in_anonymous_namespace_p (t
));
440 gcc_checking_assert (TREE_CODE (t
) == RECORD_TYPE
441 && TYPE_BINFO (t
) && BINFO_VTABLE (TYPE_BINFO (t
)));
442 gcc_checking_assert (main_odr_variant (t
) == t
);
444 if (TREE_CODE (v
) == POINTER_PLUS_EXPR
)
446 add_expr (TREE_OPERAND (v
, 1), hstate
);
447 v
= TREE_OPERAND (TREE_OPERAND (v
, 0), 0);
450 hstate
.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v
)));
451 return hstate
.end ();
454 /* Return the computed hashcode for ODR_TYPE. */
457 odr_vtable_hasher::hash (const odr_type_d
*odr_type
)
459 return hash_odr_vtable (odr_type
->type
);
462 /* For languages with One Definition Rule, work out if
463 types are the same based on their name.
465 This is non-trivial for LTO where minor differences in
466 the type representation may have prevented type merging
467 to merge two copies of otherwise equivalent type.
469 Until we start streaming mangled type names, this function works
470 only for polymorphic types.
472 When STRICT is true, we compare types by their names for purposes of
473 ODR violation warnings. When strict is false, we consider variants
474 equivalent, becuase it is all that matters for devirtualization machinery.
478 types_same_for_odr (const_tree type1
, const_tree type2
, bool strict
)
480 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
482 type1
= main_odr_variant (type1
);
483 type2
= main_odr_variant (type2
);
486 type1
= TYPE_MAIN_VARIANT (type1
);
487 type2
= TYPE_MAIN_VARIANT (type2
);
496 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
497 on the corresponding TYPE_STUB_DECL. */
498 if ((type_with_linkage_p (type1
) && type_in_anonymous_namespace_p (type1
))
499 || (type_with_linkage_p (type2
) && type_in_anonymous_namespace_p (type2
)))
503 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
505 Ideally we should never need types without ODR names here. It can however
508 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
509 Here testing for equivalence is safe, since their MAIN_VARIANTs are
511 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
512 establish precise ODR equivalency, but for correctness we care only
513 about equivalency on complete polymorphic types. For these we can
514 compare assembler names of their virtual tables. */
515 if ((!TYPE_NAME (type1
) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1
)))
516 || (!TYPE_NAME (type2
) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2
))))
518 /* See if types are obviously different (i.e. different codes
519 or polymorphic wrt non-polymorphic). This is not strictly correct
520 for ODR violating programs, but we can't do better without streaming
522 if (TREE_CODE (type1
) != TREE_CODE (type2
))
524 if (TREE_CODE (type1
) == RECORD_TYPE
525 && (TYPE_BINFO (type1
) == NULL_TREE
)
526 != (TYPE_BINFO (type1
) == NULL_TREE
))
528 if (TREE_CODE (type1
) == RECORD_TYPE
&& TYPE_BINFO (type1
)
529 && (BINFO_VTABLE (TYPE_BINFO (type1
)) == NULL_TREE
)
530 != (BINFO_VTABLE (TYPE_BINFO (type2
)) == NULL_TREE
))
533 /* At the moment we have no way to establish ODR equivalence at LTO
534 other than comparing virtual table pointers of polymorphic types.
535 Eventually we should start saving mangled names in TYPE_NAME.
536 Then this condition will become non-trivial. */
538 if (TREE_CODE (type1
) == RECORD_TYPE
539 && TYPE_BINFO (type1
) && TYPE_BINFO (type2
)
540 && BINFO_VTABLE (TYPE_BINFO (type1
))
541 && BINFO_VTABLE (TYPE_BINFO (type2
)))
543 tree v1
= BINFO_VTABLE (TYPE_BINFO (type1
));
544 tree v2
= BINFO_VTABLE (TYPE_BINFO (type2
));
545 gcc_assert (TREE_CODE (v1
) == POINTER_PLUS_EXPR
546 && TREE_CODE (v2
) == POINTER_PLUS_EXPR
);
547 return (operand_equal_p (TREE_OPERAND (v1
, 1),
548 TREE_OPERAND (v2
, 1), 0)
549 && DECL_ASSEMBLER_NAME
550 (TREE_OPERAND (TREE_OPERAND (v1
, 0), 0))
551 == DECL_ASSEMBLER_NAME
552 (TREE_OPERAND (TREE_OPERAND (v2
, 0), 0)));
556 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1
))
557 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2
)));
560 /* Return true if we can decide on ODR equivalency.
562 In non-LTO it is always decide, in LTO however it depends in the type has
565 When STRICT is false, compare main variants. */
568 types_odr_comparable (tree t1
, tree t2
, bool strict
)
571 || (strict
? main_odr_variant (t1
) == main_odr_variant (t2
)
572 : TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
573 || (odr_type_p (t1
) && odr_type_p (t2
))
574 || (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
575 && TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
576 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
577 && polymorphic_type_binfo_p (TYPE_BINFO (t2
))));
580 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
581 known, be conservative and return false. */
584 types_must_be_same_for_odr (tree t1
, tree t2
)
586 if (types_odr_comparable (t1
, t2
))
587 return types_same_for_odr (t1
, t2
);
589 return TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
);
592 /* Compare types T1 and T2 and return true if they are
596 odr_name_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
600 gcc_checking_assert (main_odr_variant (t2
) == t2
);
601 gcc_checking_assert (main_odr_variant (t1
) == t1
);
606 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
607 on the corresponding TYPE_STUB_DECL. */
608 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
609 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
611 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
)));
612 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
613 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
614 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)));
617 /* Compare types T1 and T2 and return true if they are
621 odr_vtable_hasher::equal (const odr_type_d
*o1
, const tree_node
*t2
)
625 gcc_checking_assert (main_odr_variant (t2
) == t2
);
626 gcc_checking_assert (main_odr_variant (t1
) == t1
);
627 gcc_checking_assert (in_lto_p
);
628 t1
= TYPE_MAIN_VARIANT (t1
);
629 t2
= TYPE_MAIN_VARIANT (t2
);
632 tree v1
= BINFO_VTABLE (TYPE_BINFO (t1
));
633 tree v2
= BINFO_VTABLE (TYPE_BINFO (t2
));
634 return (operand_equal_p (TREE_OPERAND (v1
, 1),
635 TREE_OPERAND (v2
, 1), 0)
636 && DECL_ASSEMBLER_NAME
637 (TREE_OPERAND (TREE_OPERAND (v1
, 0), 0))
638 == DECL_ASSEMBLER_NAME
639 (TREE_OPERAND (TREE_OPERAND (v2
, 0), 0)));
642 /* Free ODR type V. */
645 odr_name_hasher::remove (odr_type_d
*v
)
648 v
->derived_types
.release ();
654 /* ODR type hash used to look up ODR type based on tree type node. */
656 typedef hash_table
<odr_name_hasher
> odr_hash_type
;
657 static odr_hash_type
*odr_hash
;
658 typedef hash_table
<odr_vtable_hasher
> odr_vtable_hash_type
;
659 static odr_vtable_hash_type
*odr_vtable_hash
;
661 /* ODR types are also stored into ODR_TYPE vector to allow consistent
662 walking. Bases appear before derived types. Vector is garbage collected
663 so we won't end up visiting empty types. */
665 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
666 #define odr_types (*odr_types_ptr)
668 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
670 set_type_binfo (tree type
, tree binfo
)
672 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
673 if (COMPLETE_TYPE_P (type
))
674 TYPE_BINFO (type
) = binfo
;
676 gcc_assert (!TYPE_BINFO (type
));
679 /* Compare T2 and T2 based on name or structure. */
682 odr_subtypes_equivalent_p (tree t1
, tree t2
,
683 hash_set
<type_pair
,pair_traits
> *visited
)
686 /* This can happen in incomplete types that should be handled earlier. */
687 gcc_assert (t1
&& t2
);
689 t1
= main_odr_variant (t1
);
690 t2
= main_odr_variant (t2
);
694 /* Anonymous namespace types must match exactly. */
695 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
696 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
699 /* For ODR types be sure to compare their names.
700 To support -wno-odr-type-merging we allow one type to be non-ODR
701 and other ODR even though it is a violation. */
702 if (types_odr_comparable (t1
, t2
, true))
704 if (!types_same_for_odr (t1
, t2
, true))
706 /* Limit recursion: If subtypes are ODR types and we know
707 that they are same, be happy. */
708 if (!get_odr_type (t1
, true)->odr_violated
)
712 /* Component types, builtins and possibly violating ODR types
713 have to be compared structurally. */
714 if (TREE_CODE (t1
) != TREE_CODE (t2
))
716 if (AGGREGATE_TYPE_P (t1
)
717 && (TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
720 type_pair pair
={t1
,t2
};
721 if (TYPE_UID (t1
) > TYPE_UID (t2
))
726 if (visited
->add (pair
))
728 return odr_types_equivalent_p (t1
, t2
, false, NULL
, visited
);
731 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
732 violation warnings. */
735 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
739 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
741 odr_violation_reported
= true;
742 if (DECL_VIRTUAL_P (prevailing
->decl
))
744 varpool_node
*tmp
= prevailing
;
748 if (warning_at (DECL_SOURCE_LOCATION
749 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
751 "virtual table of type %qD violates one definition rule",
752 DECL_CONTEXT (vtable
->decl
)))
753 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
754 "variable of same assembler name as the virtual table is "
755 "defined in another translation unit");
758 if (!prevailing
->definition
|| !vtable
->definition
)
761 /* If we do not stream ODR type info, do not bother to do useful compare. */
762 if (!TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))
763 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable
->decl
))))
766 odr_type class_type
= get_odr_type (DECL_CONTEXT (vtable
->decl
), true);
768 if (class_type
->odr_violated
)
771 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
773 struct ipa_ref
*ref1
, *ref2
;
776 end1
= !prevailing
->iterate_reference (n1
, ref1
);
777 end2
= !vtable
->iterate_reference (n2
, ref2
);
779 /* !DECL_VIRTUAL_P means RTTI entry;
780 We warn when RTTI is lost because non-RTTI previals; we silently
781 accept the other case. */
784 || (DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
785 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
786 && TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
))
787 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
789 if (!class_type
->rtti_broken
790 && warning_at (DECL_SOURCE_LOCATION
791 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
793 "virtual table of type %qD contains RTTI "
795 DECL_CONTEXT (vtable
->decl
)))
797 inform (DECL_SOURCE_LOCATION
798 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
799 "but is prevailed by one without from other translation "
801 inform (DECL_SOURCE_LOCATION
802 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
803 "RTTI will not work on this type");
804 class_type
->rtti_broken
= true;
807 end2
= !vtable
->iterate_reference (n2
, ref2
);
811 || (DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
812 != DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
813 && TREE_CODE (ref2
->referred
->decl
) == FUNCTION_DECL
))
814 && TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
)
817 end1
= !prevailing
->iterate_reference (n1
, ref1
);
823 /* Extra paranoia; compare the sizes. We do not have information
824 about virtual inheritance offsets, so just be sure that these
826 Do this as very last check so the not very informative error
827 is not output too often. */
828 if (DECL_SIZE (prevailing
->decl
) != DECL_SIZE (vtable
->decl
))
830 class_type
->odr_violated
= true;
831 if (warning_at (DECL_SOURCE_LOCATION
832 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
834 "virtual table of type %qD violates "
835 "one definition rule ",
836 DECL_CONTEXT (vtable
->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 of different size");
849 if (DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
850 == DECL_ASSEMBLER_NAME (ref2
->referred
->decl
))
853 class_type
->odr_violated
= true;
855 /* If the loops above stopped on non-virtual pointer, we have
856 mismatch in RTTI information mangling. */
857 if (TREE_CODE (ref1
->referred
->decl
) != FUNCTION_DECL
858 && TREE_CODE (ref2
->referred
->decl
) != FUNCTION_DECL
)
860 if (warning_at (DECL_SOURCE_LOCATION
861 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
863 "virtual table of type %qD violates "
864 "one definition rule ",
865 DECL_CONTEXT (vtable
->decl
)))
867 inform (DECL_SOURCE_LOCATION
868 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
869 "the conflicting type defined in another translation "
870 "unit with different RTTI information");
874 /* At this point both REF1 and REF2 points either to virtual table
875 or virtual method. If one points to virtual table and other to
876 method we can complain the same way as if one table was shorter
877 than other pointing out the extra method. */
878 if (TREE_CODE (ref1
->referred
->decl
)
879 != TREE_CODE (ref2
->referred
->decl
))
881 if (TREE_CODE (ref1
->referred
->decl
) == VAR_DECL
)
883 else if (TREE_CODE (ref2
->referred
->decl
) == VAR_DECL
)
888 class_type
->odr_violated
= true;
890 /* Complain about size mismatch. Either we have too many virutal
891 functions or too many virtual table pointers. */
896 varpool_node
*tmp
= prevailing
;
901 if (warning_at (DECL_SOURCE_LOCATION
902 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
904 "virtual table of type %qD violates "
905 "one definition rule",
906 DECL_CONTEXT (vtable
->decl
)))
908 if (TREE_CODE (ref1
->referring
->decl
) == FUNCTION_DECL
)
910 inform (DECL_SOURCE_LOCATION
911 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
912 "the conflicting type defined in another translation "
914 inform (DECL_SOURCE_LOCATION
915 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
916 "contains additional virtual method %qD",
917 ref1
->referred
->decl
);
921 inform (DECL_SOURCE_LOCATION
922 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
923 "the conflicting type defined in another translation "
924 "unit has virtual table table with more entries");
930 /* And in the last case we have either mistmatch in between two virtual
931 methods or two virtual table pointers. */
932 if (warning_at (DECL_SOURCE_LOCATION
933 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), OPT_Wodr
,
934 "virtual table of type %qD violates "
935 "one definition rule ",
936 DECL_CONTEXT (vtable
->decl
)))
938 if (TREE_CODE (ref1
->referred
->decl
) == FUNCTION_DECL
)
940 inform (DECL_SOURCE_LOCATION
941 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
942 "the conflicting type defined in another translation "
944 gcc_assert (TREE_CODE (ref2
->referred
->decl
)
946 inform (DECL_SOURCE_LOCATION (ref1
->referred
->decl
),
947 "virtual method %qD", ref1
->referred
->decl
);
948 inform (DECL_SOURCE_LOCATION (ref2
->referred
->decl
),
949 "ought to match virtual method %qD but does not",
950 ref2
->referred
->decl
);
953 inform (DECL_SOURCE_LOCATION
954 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
955 "the conflicting type defined in another translation "
956 "unit has virtual table table with different contents");
962 /* Output ODR violation warning about T1 and T2 with REASON.
963 Display location of ST1 and ST2 if REASON speaks about field or
965 If WARN is false, do nothing. Set WARNED if warning was indeed
969 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
970 bool warn
, bool *warned
, const char *reason
)
972 tree decl2
= TYPE_NAME (t2
);
976 if (!warn
|| !TYPE_NAME(t1
))
979 /* ODR warnings are output druing LTO streaming; we must apply location
980 cache for potential warnings to be output correctly. */
981 if (lto_location_cache::current_cache
)
982 lto_location_cache::current_cache
->apply_location_cache ();
984 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)), OPT_Wodr
,
985 "type %qT violates one definition rule",
990 /* For FIELD_DECL support also case where one of fields is
991 NULL - this is used when the structures have mismatching number of
993 else if (!st1
|| TREE_CODE (st1
) == FIELD_DECL
)
995 inform (DECL_SOURCE_LOCATION (decl2
),
996 "a different type is defined in another translation unit");
1002 inform (DECL_SOURCE_LOCATION (st1
),
1003 "the first difference of corresponding definitions is field %qD",
1008 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
1010 inform (DECL_SOURCE_LOCATION (decl2
),
1011 "a different type is defined in another translation unit");
1012 inform (DECL_SOURCE_LOCATION (st1
),
1013 "the first difference of corresponding definitions is method %qD",
1019 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
1025 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
1026 because they are used on same place in ODR matching types.
1027 They are not; inform the user. */
1030 warn_types_mismatch (tree t1
, tree t2
)
1032 /* If types have names and they are different, it is most informative to
1034 if (TYPE_NAME (t1
) && TYPE_NAME (t2
)
1035 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1
))
1036 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2
))
1037 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))
1038 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2
)))
1040 char *name1
= xstrdup (cplus_demangle
1041 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1
))),
1042 DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
));
1043 char *name2
= cplus_demangle
1044 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2
))),
1045 DMGL_PARAMS
| DMGL_ANSI
| DMGL_TYPES
);
1046 if (name1
&& name2
&& strcmp (name1
, name2
))
1048 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
1049 "type name %<%s%> should match type name %<%s%>",
1051 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2
)),
1052 "the incompatible type is defined here");
1058 /* It is a quite common bug to reference anonymous namespace type in
1059 non-anonymous namespace class. */
1060 if ((type_with_linkage_p (t1
) && type_in_anonymous_namespace_p (t1
))
1061 || (type_with_linkage_p (t2
) && type_in_anonymous_namespace_p (t2
)))
1063 if (type_with_linkage_p (t1
) && !type_in_anonymous_namespace_p (t1
))
1069 if (TYPE_NAME (t1
) && TYPE_NAME (t2
)
1070 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1071 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
)
1073 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
1074 "type %qT defined in anonymous namespace can not match "
1077 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2
)),
1078 "the incompatible type defined in anonymous namespace in "
1079 "another translation unit");
1082 inform (UNKNOWN_LOCATION
,
1083 "types in anonymous namespace does not match across "
1084 "translation unit boundary");
1087 /* A tricky case are component types. Often they appear the same in source
1088 code and the mismatch is dragged in by type they are build from.
1089 Look for those differences in subtypes and try to be informative. In other
1090 cases just output nothing because the source code is probably different
1091 and in this case we already output a all necessary info. */
1092 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
1094 if (TREE_CODE (t1
) == TREE_CODE (t2
))
1096 hash_set
<type_pair
,pair_traits
> visited
;
1097 if (TREE_CODE (t1
) == ARRAY_TYPE
1098 && COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1100 tree i1
= TYPE_DOMAIN (t1
);
1101 tree i2
= TYPE_DOMAIN (t2
);
1104 && TYPE_MAX_VALUE (i1
)
1105 && TYPE_MAX_VALUE (i2
)
1106 && !operand_equal_p (TYPE_MAX_VALUE (i1
),
1107 TYPE_MAX_VALUE (i2
), 0))
1109 inform (UNKNOWN_LOCATION
,
1110 "array types have different bounds");
1114 if ((POINTER_TYPE_P (t1
) || TREE_CODE (t1
) == ARRAY_TYPE
)
1115 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
),
1118 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
1119 else if (TREE_CODE (t1
) == METHOD_TYPE
1120 || TREE_CODE (t1
) == FUNCTION_TYPE
)
1122 tree parms1
= NULL
, parms2
= NULL
;
1125 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
1128 inform (UNKNOWN_LOCATION
, "return value type mismatch");
1129 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
1132 if (prototype_p (t1
) && prototype_p (t2
))
1133 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1135 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
),
1138 if (!odr_subtypes_equivalent_p
1139 (TREE_VALUE (parms1
), TREE_VALUE (parms2
), &visited
))
1141 if (count
== 1 && TREE_CODE (t1
) == METHOD_TYPE
)
1142 inform (UNKNOWN_LOCATION
,
1143 "implicit this pointer type mismatch");
1145 inform (UNKNOWN_LOCATION
,
1146 "type mismatch in parameter %i",
1147 count
- (TREE_CODE (t1
) == METHOD_TYPE
));
1148 warn_types_mismatch (TREE_VALUE (parms1
),
1149 TREE_VALUE (parms2
));
1153 if (parms1
|| parms2
)
1155 inform (UNKNOWN_LOCATION
,
1156 "types have different parameter counts");
1163 /* This should not happen but if it does, the warning would not be helpful.
1164 TODO: turn it into assert next stage1. */
1165 if (TYPE_NAME (t1
) == TYPE_NAME (t2
))
1167 /* In Firefox it is a common bug to have same types but in
1168 different namespaces. Be a bit more informative on
1170 if (TYPE_CONTEXT (t1
) && TYPE_CONTEXT (t2
)
1171 && (((TREE_CODE (TYPE_CONTEXT (t1
)) == NAMESPACE_DECL
)
1172 != (TREE_CODE (TYPE_CONTEXT (t2
)) == NAMESPACE_DECL
))
1173 || (TREE_CODE (TYPE_CONTEXT (t1
)) == NAMESPACE_DECL
1174 && (DECL_NAME (TYPE_CONTEXT (t1
)) !=
1175 DECL_NAME (TYPE_CONTEXT (t2
))))))
1176 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
1177 "type %qT should match type %qT but is defined "
1178 "in different namespace ",
1180 else if (types_odr_comparable (t1
, t2
, true)
1181 && types_same_for_odr (t1
, t2
, true))
1182 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
1183 "type %qT should match type %qT that itself violate "
1184 "one definition rule",
1187 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
1188 "type %qT should match type %qT",
1190 if (DECL_SOURCE_LOCATION (TYPE_NAME (t2
)) > BUILTINS_LOCATION
)
1191 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2
)),
1192 "the incompatible type is defined here");
1195 /* Compare T1 and T2, report ODR violations if WARN is true and set
1196 WARNED to true if anything is reported. Return true if types match.
1197 If true is returned, the types are also compatible in the sense of
1198 gimple_canonical_types_compatible_p. */
1201 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
,
1202 hash_set
<type_pair
,pair_traits
> *visited
)
1204 /* Check first for the obvious case of pointer identity. */
1207 gcc_assert (!type_with_linkage_p (t1
) || !type_in_anonymous_namespace_p (t1
));
1208 gcc_assert (!type_with_linkage_p (t2
) || !type_in_anonymous_namespace_p (t2
));
1210 /* Can't be the same type if the types don't have the same code. */
1211 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1213 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1214 G_("a different type is defined in another translation unit"));
1218 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1220 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1221 G_("a type with different qualifiers is defined in another "
1222 "translation unit"));
1226 if (comp_type_attributes (t1
, t2
) != 1)
1228 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1229 G_("a type with different attributes "
1230 "is defined in another translation unit"));
1234 if (TREE_CODE (t1
) == ENUMERAL_TYPE
1235 && TYPE_VALUES (t1
) && TYPE_VALUES (t2
))
1238 for (v1
= TYPE_VALUES (t1
), v2
= TYPE_VALUES (t2
);
1239 v1
&& v2
; v1
= TREE_CHAIN (v1
), v2
= TREE_CHAIN (v2
))
1241 if (TREE_PURPOSE (v1
) != TREE_PURPOSE (v2
))
1243 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1244 G_("an enum with different value name"
1245 " is defined in another translation unit"));
1248 if (TREE_VALUE (v1
) != TREE_VALUE (v2
)
1249 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1
)),
1250 DECL_INITIAL (TREE_VALUE (v2
)), 0))
1252 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1253 G_("an enum with different values is defined"
1254 " in another translation unit"));
1260 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1261 G_("an enum with mismatching number of values "
1262 "is defined in another translation unit"));
1267 /* Non-aggregate types can be handled cheaply. */
1268 if (INTEGRAL_TYPE_P (t1
)
1269 || SCALAR_FLOAT_TYPE_P (t1
)
1270 || FIXED_POINT_TYPE_P (t1
)
1271 || TREE_CODE (t1
) == VECTOR_TYPE
1272 || TREE_CODE (t1
) == COMPLEX_TYPE
1273 || TREE_CODE (t1
) == OFFSET_TYPE
1274 || POINTER_TYPE_P (t1
))
1276 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
1278 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1279 G_("a type with different precision is defined "
1280 "in another translation unit"));
1283 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
1285 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1286 G_("a type with different signedness is defined "
1287 "in another translation unit"));
1291 if (TREE_CODE (t1
) == INTEGER_TYPE
1292 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
1294 /* char WRT uint_8? */
1295 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1296 G_("a different type is defined in another "
1297 "translation unit"));
1301 /* For canonical type comparisons we do not want to build SCCs
1302 so we cannot compare pointed-to types. But we can, for now,
1303 require the same pointed-to type kind and match what
1304 useless_type_conversion_p would do. */
1305 if (POINTER_TYPE_P (t1
))
1307 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
1308 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
1310 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1311 G_("it is defined as a pointer in different address "
1312 "space in another translation unit"));
1316 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
1318 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1319 G_("it is defined as a pointer to different type "
1320 "in another translation unit"));
1322 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
1327 if ((TREE_CODE (t1
) == VECTOR_TYPE
|| TREE_CODE (t1
) == COMPLEX_TYPE
)
1328 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
1330 /* Probably specific enough. */
1331 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1332 G_("a different type is defined "
1333 "in another translation unit"));
1335 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
1339 /* Do type-specific comparisons. */
1340 else switch (TREE_CODE (t1
))
1344 /* Array types are the same if the element types are the same and
1345 the number of elements are the same. */
1346 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
1348 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1349 G_("a different type is defined in another "
1350 "translation unit"));
1352 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
1354 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
1355 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
1356 == TYPE_NONALIASED_COMPONENT (t2
));
1358 tree i1
= TYPE_DOMAIN (t1
);
1359 tree i2
= TYPE_DOMAIN (t2
);
1361 /* For an incomplete external array, the type domain can be
1362 NULL_TREE. Check this condition also. */
1363 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
1366 tree min1
= TYPE_MIN_VALUE (i1
);
1367 tree min2
= TYPE_MIN_VALUE (i2
);
1368 tree max1
= TYPE_MAX_VALUE (i1
);
1369 tree max2
= TYPE_MAX_VALUE (i2
);
1371 /* In C++, minimums should be always 0. */
1372 gcc_assert (min1
== min2
);
1373 if (!operand_equal_p (max1
, max2
, 0))
1375 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1376 G_("an array of different size is defined "
1377 "in another translation unit"));
1385 /* Function types are the same if the return type and arguments types
1387 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
1389 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1390 G_("has different return value "
1391 "in another translation unit"));
1393 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
1397 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
)
1398 || !prototype_p (t1
) || !prototype_p (t2
))
1402 tree parms1
, parms2
;
1404 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1406 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
1408 if (!odr_subtypes_equivalent_p
1409 (TREE_VALUE (parms1
), TREE_VALUE (parms2
), visited
))
1411 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1412 G_("has different parameters in another "
1413 "translation unit"));
1415 warn_types_mismatch (TREE_VALUE (parms1
),
1416 TREE_VALUE (parms2
));
1421 if (parms1
|| parms2
)
1423 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1424 G_("has different parameters "
1425 "in another translation unit"));
1434 case QUAL_UNION_TYPE
:
1438 /* For aggregate types, all the fields must be the same. */
1439 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1441 if (TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
1442 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
1443 != polymorphic_type_binfo_p (TYPE_BINFO (t2
)))
1445 if (polymorphic_type_binfo_p (TYPE_BINFO (t1
)))
1446 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1447 G_("a type defined in another translation unit "
1448 "is not polymorphic"));
1450 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1451 G_("a type defined in another translation unit "
1455 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1457 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1459 /* Skip non-fields. */
1460 while (f1
&& TREE_CODE (f1
) != FIELD_DECL
)
1461 f1
= TREE_CHAIN (f1
);
1462 while (f2
&& TREE_CODE (f2
) != FIELD_DECL
)
1463 f2
= TREE_CHAIN (f2
);
1466 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1468 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1469 G_("a type with different virtual table pointers"
1470 " is defined in another translation unit"));
1473 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
1475 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1476 G_("a type with different bases is defined "
1477 "in another translation unit"));
1480 if (DECL_NAME (f1
) != DECL_NAME (f2
)
1481 && !DECL_ARTIFICIAL (f1
))
1483 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1484 G_("a field with different name is defined "
1485 "in another translation unit"));
1488 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1489 TREE_TYPE (f2
), visited
))
1491 /* Do not warn about artificial fields and just go into
1492 generic field mismatch warning. */
1493 if (DECL_ARTIFICIAL (f1
))
1496 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1497 G_("a field of same name but different type "
1498 "is defined in another translation unit"));
1500 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
));
1503 if (!gimple_compare_field_offset (f1
, f2
))
1505 /* Do not warn about artificial fields and just go into
1506 generic field mismatch warning. */
1507 if (DECL_ARTIFICIAL (f1
))
1509 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1510 G_("fields has different layout "
1511 "in another translation unit"));
1514 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
1515 == DECL_NONADDRESSABLE_P (f2
));
1518 /* If one aggregate has more fields than the other, they
1519 are not the same. */
1522 if ((f1
&& DECL_VIRTUAL_P (f1
)) || (f2
&& DECL_VIRTUAL_P (f2
)))
1523 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1524 G_("a type with different virtual table pointers"
1525 " is defined in another translation unit"));
1526 else if ((f1
&& DECL_ARTIFICIAL (f1
))
1527 || (f2
&& DECL_ARTIFICIAL (f2
)))
1528 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1529 G_("a type with different bases is defined "
1530 "in another translation unit"));
1532 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1533 G_("a type with different number of fields "
1534 "is defined in another translation unit"));
1538 if ((TYPE_MAIN_VARIANT (t1
) == t1
|| TYPE_MAIN_VARIANT (t2
) == t2
)
1539 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1
))
1540 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2
))
1541 && odr_type_p (TYPE_MAIN_VARIANT (t1
))
1542 && odr_type_p (TYPE_MAIN_VARIANT (t2
))
1543 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1
))
1544 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2
))))
1546 /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
1547 if it is non-NULL so this loop will never realy execute. */
1548 if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1
)) != error_mark_node
1549 && TYPE_METHODS (TYPE_MAIN_VARIANT (t2
)) != error_mark_node
)
1550 for (f1
= TYPE_METHODS (TYPE_MAIN_VARIANT (t1
)),
1551 f2
= TYPE_METHODS (TYPE_MAIN_VARIANT (t2
));
1552 f1
&& f2
; f1
= DECL_CHAIN (f1
), f2
= DECL_CHAIN (f2
))
1554 if (DECL_ASSEMBLER_NAME (f1
) != DECL_ASSEMBLER_NAME (f2
))
1556 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1557 G_("a different method of same type "
1558 "is defined in another "
1559 "translation unit"));
1562 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1564 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1565 G_("s definition that differs by virtual "
1566 "keyword in another translation unit"));
1569 if (DECL_VINDEX (f1
) != DECL_VINDEX (f2
))
1571 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1572 G_("virtual table layout differs "
1573 "in another translation unit"));
1576 if (odr_subtypes_equivalent_p (TREE_TYPE (f1
),
1577 TREE_TYPE (f2
), visited
))
1579 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1580 G_("method with incompatible type is "
1581 "defined in another translation unit"));
1585 if ((f1
== NULL
) != (f2
== NULL
))
1587 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1588 G_("a type with different number of methods "
1589 "is defined in another translation unit"));
1605 /* Those are better to come last as they are utterly uninformative. */
1606 if (TYPE_SIZE (t1
) && TYPE_SIZE (t2
)
1607 && !operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0))
1609 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1610 G_("a type with different size "
1611 "is defined in another translation unit"));
1614 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
)
1615 && TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
))
1617 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1618 G_("a type with different alignment "
1619 "is defined in another translation unit"));
1622 gcc_assert (!TYPE_SIZE_UNIT (t1
) || !TYPE_SIZE_UNIT (t2
)
1623 || operand_equal_p (TYPE_SIZE_UNIT (t1
),
1624 TYPE_SIZE_UNIT (t2
), 0));
1628 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1629 from VAL->type. This may happen in LTO where tree merging did not merge
1630 all variants of the same type or due to ODR violation.
1632 Analyze and report ODR violations and add type to duplicate list.
1633 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1634 this is first time we see definition of a class return true so the
1635 base types are analyzed. */
1638 add_type_duplicate (odr_type val
, tree type
)
1640 bool build_bases
= false;
1641 bool prevail
= false;
1642 bool odr_must_violate
= false;
1644 if (!val
->types_set
)
1645 val
->types_set
= new hash_set
<tree
>;
1647 /* Chose polymorphic type as leader (this happens only in case of ODR
1649 if ((TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1650 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
1651 && (TREE_CODE (val
->type
) != RECORD_TYPE
|| !TYPE_BINFO (val
->type
)
1652 || !polymorphic_type_binfo_p (TYPE_BINFO (val
->type
))))
1657 /* Always prefer complete type to be the leader. */
1658 else if (!COMPLETE_TYPE_P (val
->type
) && COMPLETE_TYPE_P (type
))
1661 build_bases
= TYPE_BINFO (type
);
1663 else if (COMPLETE_TYPE_P (val
->type
) && !COMPLETE_TYPE_P (type
))
1665 else if (TREE_CODE (val
->type
) == ENUMERAL_TYPE
1666 && TREE_CODE (type
) == ENUMERAL_TYPE
1667 && !TYPE_VALUES (val
->type
) && TYPE_VALUES (type
))
1669 else if (TREE_CODE (val
->type
) == RECORD_TYPE
1670 && TREE_CODE (type
) == RECORD_TYPE
1671 && TYPE_BINFO (type
) && !TYPE_BINFO (val
->type
))
1673 gcc_assert (!val
->bases
.length ());
1686 val
->types_set
->add (type
);
1688 /* If we now have a mangled name, be sure to record it to val->type
1689 so ODR hash can work. */
1691 if (can_be_name_hashed_p (type
) && !can_be_name_hashed_p (val
->type
))
1692 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val
->type
),
1693 DECL_ASSEMBLER_NAME (TYPE_NAME (type
)));
1696 bool base_mismatch
= false;
1698 bool warned
= false;
1699 hash_set
<type_pair
,pair_traits
> visited
;
1701 gcc_assert (in_lto_p
);
1702 vec_safe_push (val
->types
, type
);
1704 /* If both are class types, compare the bases. */
1705 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1706 && TREE_CODE (val
->type
) == RECORD_TYPE
1707 && TREE_CODE (type
) == RECORD_TYPE
1708 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1710 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1711 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1713 if (!flag_ltrans
&& !warned
&& !val
->odr_violated
)
1716 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1717 "a type with the same name but different "
1718 "number of polymorphic bases is "
1719 "defined in another translation unit");
1722 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type
))
1723 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)))
1724 extra_base
= BINFO_BASE_BINFO
1726 BINFO_N_BASE_BINFOS (TYPE_BINFO (val
->type
)));
1728 extra_base
= BINFO_BASE_BINFO
1729 (TYPE_BINFO (val
->type
),
1730 BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1731 tree extra_base_type
= BINFO_TYPE (extra_base
);
1732 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type
)),
1733 "the extra base is defined here");
1736 base_mismatch
= true;
1739 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1741 tree base1
= BINFO_BASE_BINFO (TYPE_BINFO (type
), i
);
1742 tree base2
= BINFO_BASE_BINFO (TYPE_BINFO (val
->type
), i
);
1743 tree type1
= BINFO_TYPE (base1
);
1744 tree type2
= BINFO_TYPE (base2
);
1746 if (types_odr_comparable (type1
, type2
))
1748 if (!types_same_for_odr (type1
, type2
))
1749 base_mismatch
= true;
1753 hash_set
<type_pair
,pair_traits
> visited
;
1754 if (!odr_types_equivalent_p (type1
, type2
, false, NULL
,
1756 base_mismatch
= true;
1760 if (!warned
&& !val
->odr_violated
)
1762 warn_odr (type
, val
->type
, NULL
, NULL
,
1764 "a type with the same name but different base "
1765 "type is defined in another translation unit");
1767 warn_types_mismatch (type1
, type2
);
1771 if (BINFO_OFFSET (base1
) != BINFO_OFFSET (base2
))
1773 base_mismatch
= true;
1774 if (!warned
&& !val
->odr_violated
)
1775 warn_odr (type
, val
->type
, NULL
, NULL
,
1777 "a type with the same name but different base "
1778 "layout is defined in another translation unit");
1781 /* One of bases is not of complete type. */
1782 if (!TYPE_BINFO (type1
) != !TYPE_BINFO (type2
))
1784 /* If we have a polymorphic type info specified for TYPE1
1785 but not for TYPE2 we possibly missed a base when recording
1787 Be sure this does not happen. */
1788 if (TYPE_BINFO (type1
)
1789 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1791 odr_must_violate
= true;
1794 /* One base is polymorphic and the other not.
1795 This ought to be diagnosed earlier, but do not ICE in the
1797 else if (TYPE_BINFO (type1
)
1798 && polymorphic_type_binfo_p (TYPE_BINFO (type1
))
1799 != polymorphic_type_binfo_p (TYPE_BINFO (type2
)))
1801 if (!warned
&& !val
->odr_violated
)
1802 warn_odr (type
, val
->type
, NULL
, NULL
,
1804 "a base of the type is polymorphic only in one "
1805 "translation unit");
1806 base_mismatch
= true;
1813 odr_violation_reported
= true;
1814 val
->odr_violated
= true;
1816 if (symtab
->dump_file
)
1818 fprintf (symtab
->dump_file
, "ODR base violation\n");
1820 print_node (symtab
->dump_file
, "", val
->type
, 0);
1821 putc ('\n',symtab
->dump_file
);
1822 print_node (symtab
->dump_file
, "", type
, 0);
1823 putc ('\n',symtab
->dump_file
);
1828 /* Next compare memory layout. */
1829 if (!odr_types_equivalent_p (val
->type
, type
,
1830 !flag_ltrans
&& !val
->odr_violated
&& !warned
,
1834 odr_violation_reported
= true;
1835 val
->odr_violated
= true;
1836 if (symtab
->dump_file
)
1838 fprintf (symtab
->dump_file
, "ODR violation\n");
1840 print_node (symtab
->dump_file
, "", val
->type
, 0);
1841 putc ('\n',symtab
->dump_file
);
1842 print_node (symtab
->dump_file
, "", type
, 0);
1843 putc ('\n',symtab
->dump_file
);
1846 gcc_assert (val
->odr_violated
|| !odr_must_violate
);
1847 /* Sanity check that all bases will be build same way again. */
1848 #ifdef ENABLE_CHECKING
1849 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1850 && TREE_CODE (val
->type
) == RECORD_TYPE
1851 && TREE_CODE (type
) == RECORD_TYPE
1852 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1853 && !val
->odr_violated
1854 && !base_mismatch
&& val
->bases
.length ())
1856 unsigned int num_poly_bases
= 0;
1859 for (i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1860 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1861 (TYPE_BINFO (type
), i
)))
1863 gcc_assert (num_poly_bases
== val
->bases
.length ());
1864 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
));
1866 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1867 (TYPE_BINFO (type
), i
)))
1869 odr_type base
= get_odr_type
1871 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1874 gcc_assert (val
->bases
[j
] == base
);
1881 /* Regularize things a little. During LTO same types may come with
1882 different BINFOs. Either because their virtual table was
1883 not merged by tree merging and only later at decl merging or
1884 because one type comes with external vtable, while other
1885 with internal. We want to merge equivalent binfos to conserve
1886 memory and streaming overhead.
1888 The external vtables are more harmful: they contain references
1889 to external declarations of methods that may be defined in the
1890 merged LTO unit. For this reason we absolutely need to remove
1891 them and replace by internal variants. Not doing so will lead
1892 to incomplete answers from possible_polymorphic_call_targets.
1894 FIXME: disable for now; because ODR types are now build during
1895 streaming in, the variants do not need to be linked to the type,
1896 yet. We need to do the merging in cleanup pass to be implemented
1898 if (!flag_ltrans
&& merge
1900 && TREE_CODE (val
->type
) == RECORD_TYPE
1901 && TREE_CODE (type
) == RECORD_TYPE
1902 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1903 && TYPE_MAIN_VARIANT (type
) == type
1904 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1905 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1906 && BINFO_VTABLE (TYPE_BINFO (type
)))
1908 tree master_binfo
= TYPE_BINFO (val
->type
);
1909 tree v1
= BINFO_VTABLE (master_binfo
);
1910 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1912 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1914 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1915 && operand_equal_p (TREE_OPERAND (v1
, 1),
1916 TREE_OPERAND (v2
, 1), 0));
1917 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1918 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1920 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1921 == DECL_ASSEMBLER_NAME (v2
));
1923 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1927 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1928 for (i
= 0; i
< val
->types
->length (); i
++)
1930 if (TYPE_BINFO ((*val
->types
)[i
])
1932 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1934 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1937 set_type_binfo (type
, master_binfo
);
1942 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1943 possibly new entry. */
1946 get_odr_type (tree type
, bool insert
)
1948 odr_type_d
**slot
= NULL
;
1949 odr_type_d
**vtable_slot
= NULL
;
1950 odr_type val
= NULL
;
1952 bool build_bases
= false;
1953 bool insert_to_odr_array
= false;
1956 type
= main_odr_variant (type
);
1958 gcc_checking_assert (can_be_name_hashed_p (type
)
1959 || can_be_vtable_hashed_p (type
));
1961 /* Lookup entry, first try name hash, fallback to vtable hash. */
1962 if (can_be_name_hashed_p (type
))
1964 hash
= hash_odr_name (type
);
1965 slot
= odr_hash
->find_slot_with_hash (type
, hash
,
1966 insert
? INSERT
: NO_INSERT
);
1968 if ((!slot
|| !*slot
) && in_lto_p
&& can_be_vtable_hashed_p (type
))
1970 hash
= hash_odr_vtable (type
);
1971 vtable_slot
= odr_vtable_hash
->find_slot_with_hash (type
, hash
,
1972 insert
? INSERT
: NO_INSERT
);
1975 if (!slot
&& !vtable_slot
)
1978 /* See if we already have entry for type. */
1979 if ((slot
&& *slot
) || (vtable_slot
&& *vtable_slot
))
1984 #ifdef ENABLE_CHECKING
1985 if (in_lto_p
&& can_be_vtable_hashed_p (type
))
1987 hash
= hash_odr_vtable (type
);
1988 vtable_slot
= odr_vtable_hash
->find_slot_with_hash (type
, hash
,
1990 gcc_assert (!vtable_slot
|| *vtable_slot
== *slot
);
1995 else if (*vtable_slot
)
1998 if (val
->type
!= type
1999 && (!val
->types_set
|| !val
->types_set
->add (type
)))
2001 gcc_assert (insert
);
2002 /* We have type duplicate, but it may introduce vtable name or
2003 mangled name; be sure to keep hashes in sync. */
2004 if (in_lto_p
&& can_be_vtable_hashed_p (type
)
2005 && (!vtable_slot
|| !*vtable_slot
))
2009 hash
= hash_odr_vtable (type
);
2010 vtable_slot
= odr_vtable_hash
->find_slot_with_hash
2011 (type
, hash
, INSERT
);
2012 gcc_checking_assert (!*vtable_slot
|| *vtable_slot
== val
);
2018 build_bases
= add_type_duplicate (val
, type
);
2023 val
= ggc_cleared_alloc
<odr_type_d
> ();
2026 val
->derived_types
= vNULL
;
2027 if (type_with_linkage_p (type
))
2028 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
2030 val
->anonymous_namespace
= 0;
2031 build_bases
= COMPLETE_TYPE_P (val
->type
);
2032 insert_to_odr_array
= true;
2039 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
2040 && type
== TYPE_MAIN_VARIANT (type
))
2042 tree binfo
= TYPE_BINFO (type
);
2045 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) == type
);
2047 val
->all_derivations_known
= type_all_derivations_known_p (type
);
2048 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
2049 /* For now record only polymorphic types. other are
2050 pointless for devirtualization and we can not precisely
2051 determine ODR equivalency of these during LTO. */
2052 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
2054 tree base_type
= BINFO_TYPE (BINFO_BASE_BINFO (binfo
, i
));
2055 odr_type base
= get_odr_type (base_type
, true);
2056 gcc_assert (TYPE_MAIN_VARIANT (base_type
) == base_type
);
2057 base
->derived_types
.safe_push (val
);
2058 val
->bases
.safe_push (base
);
2059 if (base
->id
> base_id
)
2063 /* Ensure that type always appears after bases. */
2064 if (insert_to_odr_array
)
2067 val
->id
= odr_types
.length ();
2068 vec_safe_push (odr_types_ptr
, val
);
2070 else if (base_id
> val
->id
)
2072 odr_types
[val
->id
] = 0;
2073 /* Be sure we did not recorded any derived types; these may need
2075 gcc_assert (val
->derived_types
.length() == 0);
2077 val
->id
= odr_types
.length ();
2078 vec_safe_push (odr_types_ptr
, val
);
2083 /* Add TYPE od ODR type hash. */
2086 register_odr_type (tree type
)
2090 odr_hash
= new odr_hash_type (23);
2092 odr_vtable_hash
= new odr_vtable_hash_type (23);
2094 /* Arrange things to be nicer and insert main variants first.
2095 ??? fundamental prerecorded types do not have mangled names; this
2096 makes it possible that non-ODR type is main_odr_variant of ODR type.
2097 Things may get smoother if LTO FE set mangled name of those types same
2098 way as C++ FE does. */
2099 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type
))))
2100 get_odr_type (TYPE_MAIN_VARIANT (type
), true);
2101 if (TYPE_MAIN_VARIANT (type
) != type
&& odr_type_p (main_odr_variant (type
)))
2102 get_odr_type (type
, true);
2105 /* Return true if type is known to have no derivations. */
2108 type_known_to_have_no_deriavations_p (tree t
)
2110 return (type_all_derivations_known_p (t
)
2111 && (TYPE_FINAL_P (t
)
2113 && !get_odr_type (t
, true)->derived_types
.length())));
2116 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2117 recursive printing. */
2120 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
2123 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
2124 print_generic_expr (f
, t
->type
, TDF_SLIM
);
2125 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
2126 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
2127 if (TYPE_NAME (t
->type
))
2129 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2130 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2131 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2132 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
->type
)))
2133 fprintf (f
, "%*s mangled name: %s\n", indent
* 2, "",
2135 (DECL_ASSEMBLER_NAME (TYPE_NAME (t
->type
))));
2137 if (t
->bases
.length ())
2139 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
2140 for (i
= 0; i
< t
->bases
.length (); i
++)
2141 fprintf (f
, " %i", t
->bases
[i
]->id
);
2144 if (t
->derived_types
.length ())
2146 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
2147 for (i
= 0; i
< t
->derived_types
.length (); i
++)
2148 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
2153 /* Dump the type inheritance graph. */
2156 dump_type_inheritance_graph (FILE *f
)
2161 fprintf (f
, "\n\nType inheritance graph:\n");
2162 for (i
= 0; i
< odr_types
.length (); i
++)
2164 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
2165 dump_odr_type (f
, odr_types
[i
]);
2167 for (i
= 0; i
< odr_types
.length (); i
++)
2169 if (odr_types
[i
] && odr_types
[i
]->types
&& odr_types
[i
]->types
->length ())
2172 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
2173 print_node (f
, "", odr_types
[i
]->type
, 0);
2174 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
2177 fprintf (f
, "duplicate #%i\n", j
);
2178 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
2179 t
= (*odr_types
[i
]->types
)[j
];
2180 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
2182 t
= TYPE_CONTEXT (t
);
2183 print_node (f
, "", t
, 0);
2191 /* Given method type T, return type of class it belongs to.
2192 Look up this pointer and get its type. */
2195 method_class_type (const_tree t
)
2197 tree first_parm_type
= TREE_VALUE (TYPE_ARG_TYPES (t
));
2198 gcc_assert (TREE_CODE (t
) == METHOD_TYPE
);
2200 return TREE_TYPE (first_parm_type
);
2203 /* Initialize IPA devirt and build inheritance tree graph. */
2206 build_type_inheritance_graph (void)
2208 struct symtab_node
*n
;
2209 FILE *inheritance_dump_file
;
2214 timevar_push (TV_IPA_INHERITANCE
);
2215 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
2216 odr_hash
= new odr_hash_type (23);
2218 odr_vtable_hash
= new odr_vtable_hash_type (23);
2220 /* We reconstruct the graph starting of types of all methods seen in the
2223 if (is_a
<cgraph_node
*> (n
)
2224 && DECL_VIRTUAL_P (n
->decl
)
2225 && n
->real_symbol_p ())
2226 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n
->decl
))),
2229 /* Look also for virtual tables of types that do not define any methods.
2231 We need it in a case where class B has virtual base of class A
2232 re-defining its virtual method and there is class C with no virtual
2233 methods with B as virtual base.
2235 Here we output B's virtual method in two variant - for non-virtual
2236 and virtual inheritance. B's virtual table has non-virtual version,
2237 while C's has virtual.
2239 For this reason we need to know about C in order to include both
2240 variants of B. More correctly, record_target_from_binfo should
2241 add both variants of the method when walking B, but we have no
2242 link in between them.
2244 We rely on fact that either the method is exported and thus we
2245 assume it is called externally or C is in anonymous namespace and
2246 thus we will see the vtable. */
2248 else if (is_a
<varpool_node
*> (n
)
2249 && DECL_VIRTUAL_P (n
->decl
)
2250 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
2251 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
2252 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
2253 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
2254 if (inheritance_dump_file
)
2256 dump_type_inheritance_graph (inheritance_dump_file
);
2257 dump_end (TDI_inheritance
, inheritance_dump_file
);
2259 timevar_pop (TV_IPA_INHERITANCE
);
2262 /* Return true if N has reference from live virtual table
2263 (and thus can be a destination of polymorphic call).
2264 Be conservatively correct when callgraph is not built or
2265 if the method may be referred externally. */
2268 referenced_from_vtable_p (struct cgraph_node
*node
)
2271 struct ipa_ref
*ref
;
2274 if (node
->externally_visible
2275 || DECL_EXTERNAL (node
->decl
)
2276 || node
->used_from_other_partition
)
2279 /* Keep this test constant time.
2280 It is unlikely this can happen except for the case where speculative
2281 devirtualization introduced many speculative edges to this node.
2282 In this case the target is very likely alive anyway. */
2283 if (node
->ref_list
.referring
.length () > 100)
2286 /* We need references built. */
2287 if (symtab
->state
<= CONSTRUCTION
)
2290 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
2291 if ((ref
->use
== IPA_REF_ALIAS
2292 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
2293 || (ref
->use
== IPA_REF_ADDR
2294 && TREE_CODE (ref
->referring
->decl
) == VAR_DECL
2295 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
2303 /* If TARGET has associated node, record it in the NODES array.
2304 CAN_REFER specify if program can refer to the target directly.
2305 if TARGET is unknown (NULL) or it can not be inserted (for example because
2306 its body was already removed and there is no way to refer to it), clear
2310 maybe_record_node (vec
<cgraph_node
*> &nodes
,
2311 tree target
, hash_set
<tree
> *inserted
,
2315 struct cgraph_node
*target_node
, *alias_target
;
2316 enum availability avail
;
2318 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2319 list of targets; the runtime effect of calling them is undefined.
2320 Only "real" virtual methods should be accounted. */
2321 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
)
2326 /* The only case when method of anonymous namespace becomes unreferable
2327 is when we completely optimized it out. */
2330 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
2338 target_node
= cgraph_node::get (target
);
2340 /* Prefer alias target over aliases, so we do not get confused by
2344 alias_target
= target_node
->ultimate_alias_target (&avail
);
2345 if (target_node
!= alias_target
2346 && avail
>= AVAIL_AVAILABLE
2347 && target_node
->get_availability ())
2348 target_node
= alias_target
;
2351 /* Method can only be called by polymorphic call if any
2352 of vtables referring to it are alive.
2354 While this holds for non-anonymous functions, too, there are
2355 cases where we want to keep them in the list; for example
2356 inline functions with -fno-weak are static, but we still
2357 may devirtualize them when instance comes from other unit.
2358 The same holds for LTO.
2360 Currently we ignore these functions in speculative devirtualization.
2361 ??? Maybe it would make sense to be more aggressive for LTO even
2364 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
2366 || !referenced_from_vtable_p (target_node
)))
2368 /* See if TARGET is useful function we can deal with. */
2369 else if (target_node
!= NULL
2370 && (TREE_PUBLIC (target
)
2371 || DECL_EXTERNAL (target
)
2372 || target_node
->definition
)
2373 && target_node
->real_symbol_p ())
2375 gcc_assert (!target_node
->global
.inlined_to
);
2376 gcc_assert (target_node
->real_symbol_p ());
2377 if (!inserted
->add (target
))
2379 cached_polymorphic_call_targets
->add (target_node
);
2380 nodes
.safe_push (target_node
);
2384 && (!type_in_anonymous_namespace_p
2385 (DECL_CONTEXT (target
))
2390 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2391 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2392 method in vtable and insert method to NODES array
2393 or BASES_TO_CONSIDER if this array is non-NULL.
2394 Otherwise recurse to base BINFOs.
2395 This matches what get_binfo_at_offset does, but with offset
2398 TYPE_BINFOS is a stack of BINFOS of types with defined
2399 virtual table seen on way from class type to BINFO.
2401 MATCHED_VTABLES tracks virtual tables we already did lookup
2402 for virtual function in. INSERTED tracks nodes we already
2405 ANONYMOUS is true if BINFO is part of anonymous namespace.
2407 Clear COMPLETEP when we hit unreferable target.
2411 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
2412 vec
<tree
> *bases_to_consider
,
2415 vec
<tree
> &type_binfos
,
2416 HOST_WIDE_INT otr_token
,
2418 HOST_WIDE_INT offset
,
2419 hash_set
<tree
> *inserted
,
2420 hash_set
<tree
> *matched_vtables
,
2424 tree type
= BINFO_TYPE (binfo
);
2429 if (BINFO_VTABLE (binfo
))
2430 type_binfos
.safe_push (binfo
);
2431 if (types_same_for_odr (type
, outer_type
))
2434 tree type_binfo
= NULL
;
2436 /* Look up BINFO with virtual table. For normal types it is always last
2438 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
2439 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
2441 type_binfo
= type_binfos
[i
];
2444 if (BINFO_VTABLE (binfo
))
2446 /* If this is duplicated BINFO for base shared by virtual inheritance,
2447 we may not have its associated vtable. This is not a problem, since
2448 we will walk it on the other path. */
2451 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
2455 gcc_assert (odr_violation_reported
);
2458 /* For types in anonymous namespace first check if the respective vtable
2459 is alive. If not, we know the type can't be called. */
2460 if (!flag_ltrans
&& anonymous
)
2462 tree vtable
= BINFO_VTABLE (inner_binfo
);
2463 varpool_node
*vnode
;
2465 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
2466 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
2467 vnode
= varpool_node::get (vtable
);
2468 if (!vnode
|| !vnode
->definition
)
2471 gcc_assert (inner_binfo
);
2472 if (bases_to_consider
2473 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
2474 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
2477 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2480 if (!bases_to_consider
)
2481 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2482 /* Destructors are never called via construction vtables. */
2483 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
2484 bases_to_consider
->safe_push (target
);
2490 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2491 /* Walking bases that have no virtual method is pointless exercise. */
2492 if (polymorphic_type_binfo_p (base_binfo
))
2493 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
2495 otr_token
, outer_type
, offset
, inserted
,
2496 matched_vtables
, anonymous
, completep
);
2497 if (BINFO_VTABLE (binfo
))
2501 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2502 of TYPE, insert them to NODES, recurse into derived nodes.
2503 INSERTED is used to avoid duplicate insertions of methods into NODES.
2504 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2505 Clear COMPLETEP if unreferable target is found.
2507 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2508 all cases where BASE_SKIPPED is true (because the base is abstract
2512 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
2513 hash_set
<tree
> *inserted
,
2514 hash_set
<tree
> *matched_vtables
,
2517 HOST_WIDE_INT otr_token
,
2519 HOST_WIDE_INT offset
,
2521 vec
<tree
> &bases_to_consider
,
2522 bool consider_construction
)
2524 tree binfo
= TYPE_BINFO (type
->type
);
2526 auto_vec
<tree
, 8> type_binfos
;
2527 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
2529 /* We may need to consider types w/o instances because of possible derived
2530 types using their methods either directly or via construction vtables.
2531 We are safe to skip them when all derivations are known, since we will
2533 This is done by recording them to BASES_TO_CONSIDER array. */
2534 if (possibly_instantiated
|| consider_construction
)
2536 record_target_from_binfo (nodes
,
2537 (!possibly_instantiated
2538 && type_all_derivations_known_p (type
->type
))
2539 ? &bases_to_consider
: NULL
,
2540 binfo
, otr_type
, type_binfos
, otr_token
,
2542 inserted
, matched_vtables
,
2543 type
->anonymous_namespace
, completep
);
2545 for (i
= 0; i
< type
->derived_types
.length (); i
++)
2546 possible_polymorphic_call_targets_1 (nodes
, inserted
,
2549 type
->derived_types
[i
],
2550 otr_token
, outer_type
, offset
, completep
,
2551 bases_to_consider
, consider_construction
);
2554 /* Cache of queries for polymorphic call targets.
2556 Enumerating all call targets may get expensive when there are many
2557 polymorphic calls in the program, so we memoize all the previous
2558 queries and avoid duplicated work. */
2560 struct polymorphic_call_target_d
2562 HOST_WIDE_INT otr_token
;
2563 ipa_polymorphic_call_context context
;
2565 vec
<cgraph_node
*> targets
;
2572 /* Polymorphic call target cache helpers. */
2574 struct polymorphic_call_target_hasher
2576 typedef polymorphic_call_target_d
*value_type
;
2577 typedef polymorphic_call_target_d
*compare_type
;
2578 static inline hashval_t
hash (const polymorphic_call_target_d
*);
2579 static inline bool equal (const polymorphic_call_target_d
*,
2580 const polymorphic_call_target_d
*);
2581 static inline void remove (polymorphic_call_target_d
*);
2584 /* Return the computed hashcode for ODR_QUERY. */
2587 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d
*odr_query
)
2589 inchash::hash
hstate (odr_query
->otr_token
);
2591 hstate
.add_wide_int (odr_query
->type
->id
);
2592 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
2593 hstate
.add_wide_int (odr_query
->context
.offset
);
2595 if (odr_query
->context
.speculative_outer_type
)
2597 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
2598 hstate
.add_wide_int (odr_query
->context
.speculative_offset
);
2600 hstate
.add_flag (odr_query
->speculative
);
2601 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
2602 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
2603 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
2604 hstate
.commit_flag ();
2605 return hstate
.end ();
2608 /* Compare cache entries T1 and T2. */
2611 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d
*t1
,
2612 const polymorphic_call_target_d
*t2
)
2614 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
2615 && t1
->speculative
== t2
->speculative
2616 && t1
->context
.offset
== t2
->context
.offset
2617 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
2618 && t1
->context
.outer_type
== t2
->context
.outer_type
2619 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
2620 && t1
->context
.maybe_in_construction
2621 == t2
->context
.maybe_in_construction
2622 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
2623 && (t1
->context
.speculative_maybe_derived_type
2624 == t2
->context
.speculative_maybe_derived_type
));
2627 /* Remove entry in polymorphic call target cache hash. */
2630 polymorphic_call_target_hasher::remove (polymorphic_call_target_d
*v
)
2632 v
->targets
.release ();
2636 /* Polymorphic call target query cache. */
2638 typedef hash_table
<polymorphic_call_target_hasher
>
2639 polymorphic_call_target_hash_type
;
2640 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
2642 /* Destroy polymorphic call target query cache. */
2645 free_polymorphic_call_targets_hash ()
2647 if (cached_polymorphic_call_targets
)
2649 delete polymorphic_call_target_hash
;
2650 polymorphic_call_target_hash
= NULL
;
2651 delete cached_polymorphic_call_targets
;
2652 cached_polymorphic_call_targets
= NULL
;
2656 /* When virtual function is removed, we may need to flush the cache. */
2659 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
2661 if (cached_polymorphic_call_targets
2662 && cached_polymorphic_call_targets
->contains (n
))
2663 free_polymorphic_call_targets_hash ();
2666 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2669 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2672 tree v
= BINFO_VTABLE (binfo
);
2675 unsigned HOST_WIDE_INT this_offset
;
2679 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2682 if (offset
== this_offset
2683 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2687 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2688 if (polymorphic_type_binfo_p (base_binfo
))
2690 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2697 /* T is known constant value of virtual table pointer.
2698 Store virtual table to V and its offset to OFFSET.
2699 Return false if T does not look like virtual table reference. */
2702 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2703 unsigned HOST_WIDE_INT
*offset
)
2705 /* We expect &MEM[(void *)&virtual_table + 16B].
2706 We obtain object's BINFO from the context of the virtual table.
2707 This one contains pointer to virtual table represented via
2708 POINTER_PLUS_EXPR. Verify that this pointer matches what
2709 we propagated through.
2711 In the case of virtual inheritance, the virtual tables may
2712 be nested, i.e. the offset may be different from 16 and we may
2713 need to dive into the type representation. */
2714 if (TREE_CODE (t
) == ADDR_EXPR
2715 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2716 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2717 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2718 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2720 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2721 (TREE_OPERAND (t
, 0), 0), 0)))
2723 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2724 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2728 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2729 We need to handle it when T comes from static variable initializer or
2731 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2733 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2734 t
= TREE_OPERAND (t
, 0);
2739 if (TREE_CODE (t
) != ADDR_EXPR
)
2741 *v
= TREE_OPERAND (t
, 0);
2745 /* T is known constant value of virtual table pointer. Return BINFO of the
2749 vtable_pointer_value_to_binfo (const_tree t
)
2752 unsigned HOST_WIDE_INT offset
;
2754 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2757 /* FIXME: for stores of construction vtables we return NULL,
2758 because we do not have BINFO for those. Eventually we should fix
2759 our representation to allow this case to be handled, too.
2760 In the case we see store of BINFO we however may assume
2761 that standard folding will be able to cope with it. */
2762 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2766 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2767 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2768 and insert them in NODES.
2770 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2773 record_targets_from_bases (tree otr_type
,
2774 HOST_WIDE_INT otr_token
,
2776 HOST_WIDE_INT offset
,
2777 vec
<cgraph_node
*> &nodes
,
2778 hash_set
<tree
> *inserted
,
2779 hash_set
<tree
> *matched_vtables
,
2784 HOST_WIDE_INT pos
, size
;
2788 if (types_same_for_odr (outer_type
, otr_type
))
2791 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
2793 if (TREE_CODE (fld
) != FIELD_DECL
)
2796 pos
= int_bit_position (fld
);
2797 size
= tree_to_shwi (DECL_SIZE (fld
));
2798 if (pos
<= offset
&& (pos
+ size
) > offset
2799 /* Do not get confused by zero sized bases. */
2800 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
2803 /* Within a class type we should always find corresponding fields. */
2804 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
2806 /* Nonbase types should have been stripped by outer_class_type. */
2807 gcc_assert (DECL_ARTIFICIAL (fld
));
2809 outer_type
= TREE_TYPE (fld
);
2812 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
2816 gcc_assert (odr_violation_reported
);
2819 gcc_assert (base_binfo
);
2820 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
2823 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2826 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
2827 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2828 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
2833 /* When virtual table is removed, we may need to flush the cache. */
2836 devirt_variable_node_removal_hook (varpool_node
*n
,
2837 void *d ATTRIBUTE_UNUSED
)
2839 if (cached_polymorphic_call_targets
2840 && DECL_VIRTUAL_P (n
->decl
)
2841 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
2842 free_polymorphic_call_targets_hash ();
2845 /* Record about how many calls would benefit from given type to be final. */
2847 struct odr_type_warn_count
2851 gcov_type dyn_count
;
2854 /* Record about how many calls would benefit from given method to be final. */
2856 struct decl_warn_count
2860 gcov_type dyn_count
;
2863 /* Information about type and decl warnings. */
2865 struct final_warning_record
2867 gcov_type dyn_count
;
2868 vec
<odr_type_warn_count
> type_warnings
;
2869 hash_map
<tree
, decl_warn_count
> decl_warnings
;
2871 struct final_warning_record
*final_warning_records
;
2873 /* Return vector containing possible targets of polymorphic call of type
2874 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2875 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2876 OTR_TYPE and include their virtual method. This is useful for types
2877 possibly in construction or destruction where the virtual table may
2878 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2879 us to walk the inheritance graph for all derivations.
2881 If COMPLETEP is non-NULL, store true if the list is complete.
2882 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2883 in the target cache. If user needs to visit every target list
2884 just once, it can memoize them.
2886 If SPECULATIVE is set, the list will not contain targets that
2887 are not speculatively taken.
2889 Returned vector is placed into cache. It is NOT caller's responsibility
2890 to free it. The vector can be freed on cgraph_remove_node call if
2891 the particular node is a virtual function present in the cache. */
2894 possible_polymorphic_call_targets (tree otr_type
,
2895 HOST_WIDE_INT otr_token
,
2896 ipa_polymorphic_call_context context
,
2901 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
2902 vec
<cgraph_node
*> nodes
= vNULL
;
2903 auto_vec
<tree
, 8> bases_to_consider
;
2904 odr_type type
, outer_type
;
2905 polymorphic_call_target_d key
;
2906 polymorphic_call_target_d
**slot
;
2910 bool can_refer
= false;
2911 bool skipped
= false;
2913 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
2915 /* If ODR is not initialized or the context is invalid, return empty
2917 if (!odr_hash
|| context
.invalid
|| !TYPE_BINFO (otr_type
))
2920 *completep
= context
.invalid
;
2922 *cache_token
= NULL
;
2926 /* Do not bother to compute speculative info when user do not asks for it. */
2927 if (!speculative
|| !context
.speculative_outer_type
)
2928 context
.clear_speculation ();
2930 type
= get_odr_type (otr_type
, true);
2932 /* Recording type variants would waste results cache. */
2933 gcc_assert (!context
.outer_type
2934 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
2936 /* Look up the outer class type we want to walk.
2937 If we fail to do so, the context is invalid. */
2938 if ((context
.outer_type
|| context
.speculative_outer_type
)
2939 && !context
.restrict_to_inner_class (otr_type
))
2944 *cache_token
= NULL
;
2947 gcc_assert (!context
.invalid
);
2949 /* Check that restrict_to_inner_class kept the main variant. */
2950 gcc_assert (!context
.outer_type
2951 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
2953 /* We canonicalize our query, so we do not need extra hashtable entries. */
2955 /* Without outer type, we have no use for offset. Just do the
2956 basic search from inner type. */
2957 if (!context
.outer_type
)
2958 context
.clear_outer_type (otr_type
);
2959 /* We need to update our hierarchy if the type does not exist. */
2960 outer_type
= get_odr_type (context
.outer_type
, true);
2961 /* If the type is complete, there are no derivations. */
2962 if (TYPE_FINAL_P (outer_type
->type
))
2963 context
.maybe_derived_type
= false;
2965 /* Initialize query cache. */
2966 if (!cached_polymorphic_call_targets
)
2968 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
2969 polymorphic_call_target_hash
2970 = new polymorphic_call_target_hash_type (23);
2971 if (!node_removal_hook_holder
)
2973 node_removal_hook_holder
=
2974 symtab
->add_cgraph_removal_hook (&devirt_node_removal_hook
, NULL
);
2975 symtab
->add_varpool_removal_hook (&devirt_variable_node_removal_hook
,
2982 if (context
.outer_type
!= otr_type
)
2984 = get_odr_type (context
.outer_type
, true)->type
;
2985 if (context
.speculative_outer_type
)
2986 context
.speculative_outer_type
2987 = get_odr_type (context
.speculative_outer_type
, true)->type
;
2990 /* Look up cached answer. */
2992 key
.otr_token
= otr_token
;
2993 key
.speculative
= speculative
;
2994 key
.context
= context
;
2995 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
2997 *cache_token
= (void *)*slot
;
3001 *completep
= (*slot
)->complete
;
3002 if ((*slot
)->type_warning
&& final_warning_records
)
3004 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
3005 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3006 += final_warning_records
->dyn_count
;
3008 if (!speculative
&& (*slot
)->decl_warning
&& final_warning_records
)
3010 struct decl_warn_count
*c
=
3011 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
3013 c
->dyn_count
+= final_warning_records
->dyn_count
;
3015 return (*slot
)->targets
;
3020 /* Do actual search. */
3021 timevar_push (TV_IPA_VIRTUAL_CALL
);
3022 *slot
= XCNEW (polymorphic_call_target_d
);
3024 *cache_token
= (void *)*slot
;
3025 (*slot
)->type
= type
;
3026 (*slot
)->otr_token
= otr_token
;
3027 (*slot
)->context
= context
;
3028 (*slot
)->speculative
= speculative
;
3030 hash_set
<tree
> inserted
;
3031 hash_set
<tree
> matched_vtables
;
3033 /* First insert targets we speculatively identified as likely. */
3034 if (context
.speculative_outer_type
)
3036 odr_type speculative_outer_type
;
3037 bool speculation_complete
= true;
3039 /* First insert target from type itself and check if it may have
3041 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
3042 if (TYPE_FINAL_P (speculative_outer_type
->type
))
3043 context
.speculative_maybe_derived_type
= false;
3044 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
3045 context
.speculative_offset
, otr_type
);
3047 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3052 /* In the case we get complete method, we don't need
3053 to walk derivations. */
3054 if (target
&& DECL_FINAL_P (target
))
3055 context
.speculative_maybe_derived_type
= false;
3056 if (type_possibly_instantiated_p (speculative_outer_type
->type
))
3057 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &speculation_complete
);
3059 matched_vtables
.add (BINFO_VTABLE (binfo
));
3062 /* Next walk recursively all derived types. */
3063 if (context
.speculative_maybe_derived_type
)
3064 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
3065 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3068 speculative_outer_type
->derived_types
[i
],
3069 otr_token
, speculative_outer_type
->type
,
3070 context
.speculative_offset
,
3071 &speculation_complete
,
3076 if (!speculative
|| !nodes
.length ())
3078 /* First see virtual method of type itself. */
3079 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
3080 context
.offset
, otr_type
);
3082 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3086 gcc_assert (odr_violation_reported
);
3090 /* Destructors are never called through construction virtual tables,
3091 because the type is always known. */
3092 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
3093 context
.maybe_in_construction
= false;
3097 /* In the case we get complete method, we don't need
3098 to walk derivations. */
3099 if (DECL_FINAL_P (target
))
3100 context
.maybe_derived_type
= false;
3103 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3104 if (type_possibly_instantiated_p (outer_type
->type
))
3105 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3110 matched_vtables
.add (BINFO_VTABLE (binfo
));
3112 /* Next walk recursively all derived types. */
3113 if (context
.maybe_derived_type
)
3115 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
3116 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3119 outer_type
->derived_types
[i
],
3120 otr_token
, outer_type
->type
,
3121 context
.offset
, &complete
,
3123 context
.maybe_in_construction
);
3125 if (!outer_type
->all_derivations_known
)
3127 if (!speculative
&& final_warning_records
)
3130 && nodes
.length () == 1
3131 && warn_suggest_final_types
3132 && !outer_type
->derived_types
.length ())
3134 if (outer_type
->id
>= (int)final_warning_records
->type_warnings
.length ())
3135 final_warning_records
->type_warnings
.safe_grow_cleared
3136 (odr_types
.length ());
3137 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
3138 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
3139 += final_warning_records
->dyn_count
;
3140 final_warning_records
->type_warnings
[outer_type
->id
].type
3142 (*slot
)->type_warning
= outer_type
->id
+ 1;
3145 && warn_suggest_final_methods
3146 && nodes
.length () == 1
3147 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
3151 struct decl_warn_count
&c
=
3152 final_warning_records
->decl_warnings
.get_or_insert
3153 (nodes
[0]->decl
, &existed
);
3158 c
.dyn_count
+= final_warning_records
->dyn_count
;
3163 c
.dyn_count
= final_warning_records
->dyn_count
;
3164 c
.decl
= nodes
[0]->decl
;
3166 (*slot
)->decl_warning
= nodes
[0]->decl
;
3175 /* Destructors are never called through construction virtual tables,
3176 because the type is always known. One of entries may be
3177 cxa_pure_virtual so look to at least two of them. */
3178 if (context
.maybe_in_construction
)
3179 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
3180 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
3181 context
.maybe_in_construction
= false;
3182 if (context
.maybe_in_construction
)
3184 if (type
!= outer_type
3186 || (context
.maybe_derived_type
3187 && !type_all_derivations_known_p (outer_type
->type
))))
3188 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
3189 context
.offset
, nodes
, &inserted
,
3190 &matched_vtables
, &complete
);
3192 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3193 for (i
= 0; i
< bases_to_consider
.length(); i
++)
3194 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
3199 (*slot
)->targets
= nodes
;
3200 (*slot
)->complete
= complete
;
3202 *completep
= complete
;
3204 timevar_pop (TV_IPA_VIRTUAL_CALL
);
3209 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
3210 vec
<const decl_warn_count
*> *vec
)
3212 vec
->safe_push (&value
);
3216 /* Dump target list TARGETS into FILE. */
3219 dump_targets (FILE *f
, vec
<cgraph_node
*> targets
)
3223 for (i
= 0; i
< targets
.length (); i
++)
3227 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
3228 fprintf (f
, " %s/%i", name
? name
: targets
[i
]->name (), targets
[i
]->order
);
3231 if (!targets
[i
]->definition
)
3232 fprintf (f
, " (no definition%s)",
3233 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
3239 /* Dump all possible targets of a polymorphic call. */
3242 dump_possible_polymorphic_call_targets (FILE *f
,
3244 HOST_WIDE_INT otr_token
,
3245 const ipa_polymorphic_call_context
&ctx
)
3247 vec
<cgraph_node
*> targets
;
3249 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
3254 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3256 &final
, NULL
, false);
3257 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
3258 print_generic_expr (f
, type
->type
, TDF_SLIM
);
3259 fprintf (f
, " token %i\n", (int)otr_token
);
3263 fprintf (f
, " %s%s%s%s\n ",
3264 final
? "This is a complete list." :
3265 "This is partial list; extra targets may be defined in other units.",
3266 ctx
.maybe_in_construction
? " (base types included)" : "",
3267 ctx
.maybe_derived_type
? " (derived types included)" : "",
3268 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
3269 len
= targets
.length ();
3270 dump_targets (f
, targets
);
3272 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3274 &final
, NULL
, true);
3275 if (targets
.length () != len
)
3277 fprintf (f
, " Speculative targets:");
3278 dump_targets (f
, targets
);
3280 gcc_assert (targets
.length () <= len
);
3285 /* Return true if N can be possibly target of a polymorphic call of
3286 OTR_TYPE/OTR_TOKEN. */
3289 possible_polymorphic_call_target_p (tree otr_type
,
3290 HOST_WIDE_INT otr_token
,
3291 const ipa_polymorphic_call_context
&ctx
,
3292 struct cgraph_node
*n
)
3294 vec
<cgraph_node
*> targets
;
3296 enum built_in_function fcode
;
3299 if (TREE_CODE (TREE_TYPE (n
->decl
)) == FUNCTION_TYPE
3300 && ((fcode
= DECL_FUNCTION_CODE (n
->decl
))
3301 == BUILT_IN_UNREACHABLE
3302 || fcode
== BUILT_IN_TRAP
))
3307 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
3308 for (i
= 0; i
< targets
.length (); i
++)
3309 if (n
->semantically_equivalent_p (targets
[i
]))
3312 /* At a moment we allow middle end to dig out new external declarations
3313 as a targets of polymorphic calls. */
3314 if (!final
&& !n
->definition
)
3321 /* Return true if N can be possibly target of a polymorphic call of
3322 OBJ_TYPE_REF expression REF in STMT. */
3325 possible_polymorphic_call_target_p (tree ref
,
3327 struct cgraph_node
*n
)
3329 ipa_polymorphic_call_context
context (current_function_decl
, ref
, stmt
);
3330 tree call_fn
= gimple_call_fn (stmt
);
3332 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn
),
3334 (OBJ_TYPE_REF_TOKEN (call_fn
)),
3340 /* After callgraph construction new external nodes may appear.
3341 Add them into the graph. */
3344 update_type_inheritance_graph (void)
3346 struct cgraph_node
*n
;
3350 free_polymorphic_call_targets_hash ();
3351 timevar_push (TV_IPA_INHERITANCE
);
3352 /* We reconstruct the graph starting from types of all methods seen in the
3354 FOR_EACH_FUNCTION (n
)
3355 if (DECL_VIRTUAL_P (n
->decl
)
3357 && n
->real_symbol_p ())
3358 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n
->decl
))),
3360 timevar_pop (TV_IPA_INHERITANCE
);
3364 /* Return true if N looks like likely target of a polymorphic call.
3365 Rule out cxa_pure_virtual, noreturns, function declared cold and
3366 other obvious cases. */
3369 likely_target_p (struct cgraph_node
*n
)
3372 /* cxa_pure_virtual and similar things are not likely. */
3373 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
3375 flags
= flags_from_decl_or_type (n
->decl
);
3376 if (flags
& ECF_NORETURN
)
3378 if (lookup_attribute ("cold",
3379 DECL_ATTRIBUTES (n
->decl
)))
3381 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
3383 /* If there are no live virtual tables referring the target,
3384 the only way the target can be called is an instance coming from other
3385 compilation unit; speculative devirtualization is built around an
3386 assumption that won't happen. */
3387 if (!referenced_from_vtable_p (n
))
3392 /* Compare type warning records P1 and P2 and choose one with larger count;
3393 helper for qsort. */
3396 type_warning_cmp (const void *p1
, const void *p2
)
3398 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
3399 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
3401 if (t1
->dyn_count
< t2
->dyn_count
)
3403 if (t1
->dyn_count
> t2
->dyn_count
)
3405 return t2
->count
- t1
->count
;
3408 /* Compare decl warning records P1 and P2 and choose one with larger count;
3409 helper for qsort. */
3412 decl_warning_cmp (const void *p1
, const void *p2
)
3414 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
3415 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
3417 if (t1
->dyn_count
< t2
->dyn_count
)
3419 if (t1
->dyn_count
> t2
->dyn_count
)
3421 return t2
->count
- t1
->count
;
3425 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3428 struct cgraph_node
*
3429 try_speculative_devirtualization (tree otr_type
, HOST_WIDE_INT otr_token
,
3430 ipa_polymorphic_call_context ctx
)
3432 vec
<cgraph_node
*>targets
3433 = possible_polymorphic_call_targets
3434 (otr_type
, otr_token
, ctx
, NULL
, NULL
, true);
3436 struct cgraph_node
*likely_target
= NULL
;
3438 for (i
= 0; i
< targets
.length (); i
++)
3439 if (likely_target_p (targets
[i
]))
3443 likely_target
= targets
[i
];
3446 ||!likely_target
->definition
3447 || DECL_EXTERNAL (likely_target
->decl
))
3450 /* Don't use an implicitly-declared destructor (c++/58678). */
3451 struct cgraph_node
*non_thunk_target
3452 = likely_target
->function_symbol ();
3453 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3455 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3456 && likely_target
->can_be_discarded_p ())
3458 return likely_target
;
3461 /* The ipa-devirt pass.
3462 When polymorphic call has only one likely target in the unit,
3463 turn it into a speculative call. */
3468 struct cgraph_node
*n
;
3469 hash_set
<void *> bad_call_targets
;
3470 struct cgraph_edge
*e
;
3472 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
3473 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
3474 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
3481 dump_type_inheritance_graph (dump_file
);
3483 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3484 This is implemented by setting up final_warning_records that are updated
3485 by get_polymorphic_call_targets.
3486 We need to clear cache in this case to trigger recomputation of all
3488 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3490 final_warning_records
= new (final_warning_record
);
3491 final_warning_records
->type_warnings
= vNULL
;
3492 final_warning_records
->type_warnings
.safe_grow_cleared (odr_types
.length ());
3493 free_polymorphic_call_targets_hash ();
3496 FOR_EACH_DEFINED_FUNCTION (n
)
3498 bool update
= false;
3499 if (!opt_for_fn (n
->decl
, flag_devirtualize
))
3501 if (dump_file
&& n
->indirect_calls
)
3502 fprintf (dump_file
, "\n\nProcesing function %s/%i\n",
3503 n
->name (), n
->order
);
3504 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
3505 if (e
->indirect_info
->polymorphic
)
3507 struct cgraph_node
*likely_target
= NULL
;
3511 if (final_warning_records
)
3512 final_warning_records
->dyn_count
= e
->count
;
3514 vec
<cgraph_node
*>targets
3515 = possible_polymorphic_call_targets
3516 (e
, &final
, &cache_token
, true);
3519 /* Trigger warnings by calculating non-speculative targets. */
3520 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3521 possible_polymorphic_call_targets (e
);
3524 dump_possible_polymorphic_call_targets
3529 /* See if the call can be devirtualized by means of ipa-prop's
3530 polymorphic call context propagation. If not, we can just
3531 forget about this call being polymorphic and avoid some heavy
3532 lifting in remove_unreachable_nodes that will otherwise try to
3533 keep all possible targets alive until inlining and in the inliner
3536 This may need to be revisited once we add further ways to use
3537 the may edges, but it is a resonable thing to do right now. */
3539 if ((e
->indirect_info
->param_index
== -1
3540 || (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
)
3541 && e
->indirect_info
->vptr_changed
))
3542 && !flag_ltrans_devirtualize
)
3544 e
->indirect_info
->polymorphic
= false;
3547 fprintf (dump_file
, "Dropping polymorphic call info;"
3548 " it can not be used by ipa-prop\n");
3551 if (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
))
3554 if (!e
->maybe_hot_p ())
3557 fprintf (dump_file
, "Call is cold\n\n");
3564 fprintf (dump_file
, "Call is already speculated\n\n");
3567 /* When dumping see if we agree with speculation. */
3571 if (bad_call_targets
.contains (cache_token
))
3574 fprintf (dump_file
, "Target list is known to be useless\n\n");
3578 for (i
= 0; i
< targets
.length (); i
++)
3579 if (likely_target_p (targets
[i
]))
3583 likely_target
= NULL
;
3585 fprintf (dump_file
, "More than one likely target\n\n");
3589 likely_target
= targets
[i
];
3593 bad_call_targets
.add (cache_token
);
3596 /* This is reached only when dumping; check if we agree or disagree
3597 with the speculation. */
3600 struct cgraph_edge
*e2
;
3601 struct ipa_ref
*ref
;
3602 e
->speculative_call_info (e2
, e
, ref
);
3603 if (e2
->callee
->ultimate_alias_target ()
3604 == likely_target
->ultimate_alias_target ())
3606 fprintf (dump_file
, "We agree with speculation\n\n");
3611 fprintf (dump_file
, "We disagree with speculation\n\n");
3616 if (!likely_target
->definition
)
3619 fprintf (dump_file
, "Target is not a definition\n\n");
3623 /* Do not introduce new references to external symbols. While we
3624 can handle these just well, it is common for programs to
3625 incorrectly with headers defining methods they are linked
3627 if (DECL_EXTERNAL (likely_target
->decl
))
3630 fprintf (dump_file
, "Target is external\n\n");
3634 /* Don't use an implicitly-declared destructor (c++/58678). */
3635 struct cgraph_node
*non_thunk_target
3636 = likely_target
->function_symbol ();
3637 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
3640 fprintf (dump_file
, "Target is artificial\n\n");
3644 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3645 && likely_target
->can_be_discarded_p ())
3648 fprintf (dump_file
, "Target is overwritable\n\n");
3652 else if (dbg_cnt (devirt
))
3654 if (dump_enabled_p ())
3656 location_t locus
= gimple_location_safe (e
->call_stmt
);
3657 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
3658 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3659 n
->name (), n
->order
,
3660 likely_target
->name (),
3661 likely_target
->order
);
3663 if (!likely_target
->can_be_discarded_p ())
3666 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
3668 likely_target
= alias
;
3673 (likely_target
, e
->count
* 8 / 10, e
->frequency
* 8 / 10);
3677 inline_update_overall_summary (n
);
3679 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3681 if (warn_suggest_final_types
)
3683 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
3684 for (unsigned int i
= 0;
3685 i
< final_warning_records
->type_warnings
.length (); i
++)
3686 if (final_warning_records
->type_warnings
[i
].count
)
3688 tree type
= final_warning_records
->type_warnings
[i
].type
;
3689 int count
= final_warning_records
->type_warnings
[i
].count
;
3691 = final_warning_records
->type_warnings
[i
].dyn_count
;
3694 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3695 OPT_Wsuggest_final_types
, count
,
3696 "Declaring type %qD final "
3697 "would enable devirtualization of %i call",
3698 "Declaring type %qD final "
3699 "would enable devirtualization of %i calls",
3703 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3704 OPT_Wsuggest_final_types
, count
,
3705 "Declaring type %qD final "
3706 "would enable devirtualization of %i call "
3707 "executed %lli times",
3708 "Declaring type %qD final "
3709 "would enable devirtualization of %i calls "
3710 "executed %lli times",
3717 if (warn_suggest_final_methods
)
3719 vec
<const decl_warn_count
*> decl_warnings_vec
= vNULL
;
3721 final_warning_records
->decl_warnings
.traverse
3722 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3723 decl_warnings_vec
.qsort (decl_warning_cmp
);
3724 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3726 tree decl
= decl_warnings_vec
[i
]->decl
;
3727 int count
= decl_warnings_vec
[i
]->count
;
3728 long long dyn_count
= decl_warnings_vec
[i
]->dyn_count
;
3731 if (DECL_CXX_DESTRUCTOR_P (decl
))
3732 warning_n (DECL_SOURCE_LOCATION (decl
),
3733 OPT_Wsuggest_final_methods
, count
,
3734 "Declaring virtual destructor of %qD final "
3735 "would enable devirtualization of %i call",
3736 "Declaring virtual destructor of %qD final "
3737 "would enable devirtualization of %i calls",
3738 DECL_CONTEXT (decl
), count
);
3740 warning_n (DECL_SOURCE_LOCATION (decl
),
3741 OPT_Wsuggest_final_methods
, count
,
3742 "Declaring method %qD final "
3743 "would enable devirtualization of %i call",
3744 "Declaring method %qD final "
3745 "would enable devirtualization of %i calls",
3747 else if (DECL_CXX_DESTRUCTOR_P (decl
))
3748 warning_n (DECL_SOURCE_LOCATION (decl
),
3749 OPT_Wsuggest_final_methods
, count
,
3750 "Declaring virtual destructor of %qD final "
3751 "would enable devirtualization of %i call "
3752 "executed %lli times",
3753 "Declaring virtual destructor of %qD final "
3754 "would enable devirtualization of %i calls "
3755 "executed %lli times",
3756 DECL_CONTEXT (decl
), count
, dyn_count
);
3758 warning_n (DECL_SOURCE_LOCATION (decl
),
3759 OPT_Wsuggest_final_methods
, count
,
3760 "Declaring method %qD final "
3761 "would enable devirtualization of %i call "
3762 "executed %lli times",
3763 "Declaring method %qD final "
3764 "would enable devirtualization of %i calls "
3765 "executed %lli times",
3766 decl
, count
, dyn_count
);
3770 delete (final_warning_records
);
3771 final_warning_records
= 0;
3776 "%i polymorphic calls, %i devirtualized,"
3777 " %i speculatively devirtualized, %i cold\n"
3778 "%i have multiple targets, %i overwritable,"
3779 " %i already speculated (%i agree, %i disagree),"
3780 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3781 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3782 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3783 nexternal
, nnotdefined
, nartificial
, ndropped
);
3784 return ndevirtualized
|| ndropped
? TODO_remove_functions
: 0;
3789 const pass_data pass_data_ipa_devirt
=
3791 IPA_PASS
, /* type */
3792 "devirt", /* name */
3793 OPTGROUP_NONE
, /* optinfo_flags */
3794 TV_IPA_DEVIRT
, /* tv_id */
3795 0, /* properties_required */
3796 0, /* properties_provided */
3797 0, /* properties_destroyed */
3798 0, /* todo_flags_start */
3799 ( TODO_dump_symtab
), /* todo_flags_finish */
3802 class pass_ipa_devirt
: public ipa_opt_pass_d
3805 pass_ipa_devirt (gcc::context
*ctxt
)
3806 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3807 NULL
, /* generate_summary */
3808 NULL
, /* write_summary */
3809 NULL
, /* read_summary */
3810 NULL
, /* write_optimization_summary */
3811 NULL
, /* read_optimization_summary */
3812 NULL
, /* stmt_fixup */
3813 0, /* function_transform_todo_flags_start */
3814 NULL
, /* function_transform */
3815 NULL
) /* variable_transform */
3818 /* opt_pass methods: */
3819 virtual bool gate (function
*)
3821 /* In LTO, always run the IPA passes and decide on function basis if the
3825 return (flag_devirtualize
3826 && (flag_devirtualize_speculatively
3827 || (warn_suggest_final_methods
3828 || warn_suggest_final_types
))
3832 virtual unsigned int execute (function
*) { return ipa_devirt (); }
3834 }; // class pass_ipa_devirt
3839 make_pass_ipa_devirt (gcc::context
*ctxt
)
3841 return new pass_ipa_devirt (ctxt
);
3844 #include "gt-ipa-devirt.h"