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"
132 #include "function.h"
136 #include "tree-pass.h"
138 #include "hash-table.h"
140 #include "tree-pretty-print.h"
141 #include "ipa-utils.h"
142 #include "tree-ssa-alias.h"
143 #include "internal-fn.h"
144 #include "gimple-fold.h"
145 #include "gimple-expr.h"
147 #include "alloc-pool.h"
148 #include "symbol-summary.h"
149 #include "ipa-prop.h"
150 #include "ipa-inline.h"
151 #include "diagnostic.h"
152 #include "tree-dfa.h"
153 #include "demangle.h"
155 #include "gimple-pretty-print.h"
156 #include "stor-layout.h"
159 /* Hash based set of pairs of types. */
166 struct pair_traits
: default_hashset_traits
171 return TYPE_UID (p
.first
) ^ TYPE_UID (p
.second
);
174 is_empty (type_pair p
)
176 return p
.first
== NULL
;
179 is_deleted (type_pair p ATTRIBUTE_UNUSED
)
184 equal (const type_pair
&a
, const type_pair
&b
)
186 return a
.first
==b
.first
&& a
.second
== b
.second
;
189 mark_empty (type_pair
&e
)
195 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
196 hash_set
<type_pair
,pair_traits
> *);
198 static bool odr_violation_reported
= false;
201 /* Pointer set of all call targets appearing in the cache. */
202 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
204 /* The node of type inheritance graph. For each type unique in
205 One Definition Rule (ODR) sense, we produce one node linking all
206 main variants of types equivalent to it, bases and derived types. */
208 struct GTY(()) odr_type_d
212 /* All bases; built only for main variants of types. */
213 vec
<odr_type
> GTY((skip
)) bases
;
214 /* All derived types with virtual methods seen in unit;
215 built only for main variants of types. */
216 vec
<odr_type
> GTY((skip
)) derived_types
;
218 /* All equivalent types, if more than one. */
219 vec
<tree
, va_gc
> *types
;
220 /* Set of all equivalent types, if NON-NULL. */
221 hash_set
<tree
> * GTY((skip
)) types_set
;
223 /* Unique ID indexing the type in odr_types array. */
225 /* Is it in anonymous namespace? */
226 bool anonymous_namespace
;
227 /* Do we know about all derivations of given type? */
228 bool all_derivations_known
;
229 /* Did we report ODR violation here? */
233 /* Return TRUE if all derived types of T are known and thus
234 we may consider the walk of derived type complete.
236 This is typically true only for final anonymous namespace types and types
237 defined within functions (that may be COMDAT and thus shared across units,
238 but with the same set of derived types). */
241 type_all_derivations_known_p (const_tree t
)
243 if (TYPE_FINAL_P (t
))
247 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
248 if (!TYPE_NAME (t
) || TREE_CODE (TYPE_NAME (t
)) != TYPE_DECL
)
250 if (type_in_anonymous_namespace_p (t
))
252 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
255 /* Return TRUE if type's constructors are all visible. */
258 type_all_ctors_visible_p (tree t
)
261 && symtab
->state
>= CONSTRUCTION
262 /* We can not always use type_all_derivations_known_p.
263 For function local types we must assume case where
264 the function is COMDAT and shared in between units.
266 TODO: These cases are quite easy to get, but we need
267 to keep track of C++ privatizing via -Wno-weak
268 as well as the IPA privatizing. */
269 && type_in_anonymous_namespace_p (t
);
272 /* Return TRUE if type may have instance. */
275 type_possibly_instantiated_p (tree t
)
280 /* TODO: Add abstract types here. */
281 if (!type_all_ctors_visible_p (t
))
284 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
285 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
286 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
287 vnode
= varpool_node::get (vtable
);
288 return vnode
&& vnode
->definition
;
291 /* One Definition Rule hashtable helpers. */
295 typedef odr_type_d value_type
;
296 typedef union tree_node compare_type
;
297 static inline hashval_t
hash (const value_type
*);
298 static inline bool equal (const value_type
*, const compare_type
*);
299 static inline void remove (value_type
*);
302 /* Return type that was declared with T's name so that T is an
303 qualified variant of it. */
306 main_odr_variant (const_tree t
)
308 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
309 return TREE_TYPE (TYPE_NAME (t
));
310 /* Unnamed types and non-C++ produced types can be compared by variants. */
312 return TYPE_MAIN_VARIANT (t
);
315 /* Produce hash based on type name. */
318 hash_type_name (tree t
)
320 gcc_checking_assert (main_odr_variant (t
) == t
);
322 /* If not in LTO, all main variants are unique, so we can do
325 return htab_hash_pointer (t
);
327 /* Anonymous types are unique. */
328 if (type_in_anonymous_namespace_p (t
))
329 return htab_hash_pointer (t
);
331 /* ODR types have name specified. */
333 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t
)))
334 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t
)));
336 /* For polymorphic types that was compiled with -fno-lto-odr-type-merging
337 we can simply hash the virtual table. */
338 if (TREE_CODE (t
) == RECORD_TYPE
339 && TYPE_BINFO (t
) && BINFO_VTABLE (TYPE_BINFO (t
)))
341 tree v
= BINFO_VTABLE (TYPE_BINFO (t
));
344 if (TREE_CODE (v
) == POINTER_PLUS_EXPR
)
346 hash
= TREE_INT_CST_LOW (TREE_OPERAND (v
, 1));
347 v
= TREE_OPERAND (TREE_OPERAND (v
, 0), 0);
350 v
= DECL_ASSEMBLER_NAME (v
);
351 hash
= iterative_hash_hashval_t (hash
, htab_hash_pointer (v
));
355 /* Builtin types may appear as main variants of ODR types and are unique.
356 Sanity check we do not get anything that looks non-builtin. */
357 gcc_checking_assert (TREE_CODE (t
) == INTEGER_TYPE
358 || TREE_CODE (t
) == VOID_TYPE
359 || TREE_CODE (t
) == COMPLEX_TYPE
360 || TREE_CODE (t
) == REAL_TYPE
361 || TREE_CODE (t
) == POINTER_TYPE
);
362 return htab_hash_pointer (t
);
365 /* Return the computed hashcode for ODR_TYPE. */
368 odr_hasher::hash (const value_type
*odr_type
)
370 return hash_type_name (odr_type
->type
);
373 /* For languages with One Definition Rule, work out if
374 types are the same based on their name.
376 This is non-trivial for LTO where minor differences in
377 the type representation may have prevented type merging
378 to merge two copies of otherwise equivalent type.
380 Until we start streaming mangled type names, this function works
381 only for polymorphic types. */
384 types_same_for_odr (const_tree type1
, const_tree type2
)
386 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
388 type1
= main_odr_variant (type1
);
389 type2
= main_odr_variant (type2
);
397 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
398 on the corresponding TYPE_STUB_DECL. */
399 if (type_in_anonymous_namespace_p (type1
)
400 || type_in_anonymous_namespace_p (type2
))
404 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
406 Ideally we should never need types without ODR names here. It can however
409 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
410 Here testing for equivalence is safe, since their MAIN_VARIANTs are
412 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
413 establish precise ODR equivalency, but for correctness we care only
414 about equivalency on complete polymorphic types. For these we can
415 compare assembler names of their virtual tables. */
416 if ((!TYPE_NAME (type1
) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1
)))
417 || (!TYPE_NAME (type2
) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2
))))
419 /* See if types are obviously different (i.e. different codes
420 or polymorphic wrt non-polymorphic). This is not strictly correct
421 for ODR violating programs, but we can't do better without streaming
423 if (TREE_CODE (type1
) != TREE_CODE (type2
))
425 if (TREE_CODE (type1
) == RECORD_TYPE
426 && (TYPE_BINFO (type1
) == NULL_TREE
) != (TYPE_BINFO (type1
) == NULL_TREE
))
428 if (TREE_CODE (type1
) == RECORD_TYPE
&& TYPE_BINFO (type1
)
429 && (BINFO_VTABLE (TYPE_BINFO (type1
)) == NULL_TREE
)
430 != (BINFO_VTABLE (TYPE_BINFO (type2
)) == NULL_TREE
))
433 /* At the moment we have no way to establish ODR equivalence at LTO
434 other than comparing virtual table pointers of polymorphic types.
435 Eventually we should start saving mangled names in TYPE_NAME.
436 Then this condition will become non-trivial. */
438 if (TREE_CODE (type1
) == RECORD_TYPE
439 && TYPE_BINFO (type1
) && TYPE_BINFO (type2
)
440 && BINFO_VTABLE (TYPE_BINFO (type1
))
441 && BINFO_VTABLE (TYPE_BINFO (type2
)))
443 tree v1
= BINFO_VTABLE (TYPE_BINFO (type1
));
444 tree v2
= BINFO_VTABLE (TYPE_BINFO (type2
));
445 gcc_assert (TREE_CODE (v1
) == POINTER_PLUS_EXPR
446 && TREE_CODE (v2
) == POINTER_PLUS_EXPR
);
447 return (operand_equal_p (TREE_OPERAND (v1
, 1),
448 TREE_OPERAND (v2
, 1), 0)
449 && DECL_ASSEMBLER_NAME
450 (TREE_OPERAND (TREE_OPERAND (v1
, 0), 0))
451 == DECL_ASSEMBLER_NAME
452 (TREE_OPERAND (TREE_OPERAND (v2
, 0), 0)));
456 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1
))
457 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2
)));
460 /* Return true if we can decide on ODR equivalency.
462 In non-LTO it is always decide, in LTO however it depends in the type has
463 ODR info attached. */
466 types_odr_comparable (tree t1
, tree t2
)
469 || main_odr_variant (t1
) == main_odr_variant (t2
)
470 || (odr_type_p (t1
) && odr_type_p (t2
))
471 || (TREE_CODE (t1
) == RECORD_TYPE
&& TREE_CODE (t2
) == RECORD_TYPE
472 && TYPE_BINFO (t1
) && TYPE_BINFO (t2
)
473 && polymorphic_type_binfo_p (TYPE_BINFO (t1
))
474 && polymorphic_type_binfo_p (TYPE_BINFO (t2
))));
477 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
478 known, be conservative and return false. */
481 types_must_be_same_for_odr (tree t1
, tree t2
)
483 if (types_odr_comparable (t1
, t2
))
484 return types_same_for_odr (t1
, t2
);
486 return main_odr_variant (t1
) == main_odr_variant (t2
);
489 /* Compare types T1 and T2 and return true if they are
493 odr_hasher::equal (const value_type
*t1
, const compare_type
*ct2
)
495 tree t2
= const_cast <tree
> (ct2
);
497 gcc_checking_assert (main_odr_variant (t2
) == t2
);
502 return types_same_for_odr (t1
->type
, t2
);
505 /* Free ODR type V. */
508 odr_hasher::remove (value_type
*v
)
511 v
->derived_types
.release ();
517 /* ODR type hash used to look up ODR type based on tree type node. */
519 typedef hash_table
<odr_hasher
> odr_hash_type
;
520 static odr_hash_type
*odr_hash
;
522 /* ODR types are also stored into ODR_TYPE vector to allow consistent
523 walking. Bases appear before derived types. Vector is garbage collected
524 so we won't end up visiting empty types. */
526 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
527 #define odr_types (*odr_types_ptr)
529 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
531 set_type_binfo (tree type
, tree binfo
)
533 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
534 if (COMPLETE_TYPE_P (type
))
535 TYPE_BINFO (type
) = binfo
;
537 gcc_assert (!TYPE_BINFO (type
));
540 /* Compare T2 and T2 based on name or structure. */
543 odr_subtypes_equivalent_p (tree t1
, tree t2
, hash_set
<type_pair
,pair_traits
> *visited
)
547 /* This can happen in incomplete types that should be handled earlier. */
548 gcc_assert (t1
&& t2
);
550 t1
= main_odr_variant (t1
);
551 t2
= main_odr_variant (t2
);
555 /* Anonymous namespace types must match exactly. */
556 an1
= type_in_anonymous_namespace_p (t1
);
557 an2
= type_in_anonymous_namespace_p (t2
);
558 if (an1
!= an2
|| an1
)
561 /* For ODR types be sure to compare their names.
562 To support -wno-odr-type-merging we allow one type to be non-ODR
563 and other ODR even though it is a violation. */
564 if (types_odr_comparable (t1
, t2
))
566 if (!types_same_for_odr (t1
, t2
))
568 /* Limit recursion: If subtypes are ODR types and we know
569 that they are same, be happy. */
570 if (!get_odr_type (t1
, true)->odr_violated
)
574 /* Component types, builtins and possibly violating ODR types
575 have to be compared structurally. */
576 if (TREE_CODE (t1
) != TREE_CODE (t2
))
578 if ((TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
580 if (TYPE_NAME (t1
) && DECL_NAME (TYPE_NAME (t1
)) != DECL_NAME (TYPE_NAME (t2
)))
583 type_pair pair
={t1
,t2
};
584 if (TYPE_UID (t1
) > TYPE_UID (t2
))
589 if (visited
->add (pair
))
591 return odr_types_equivalent_p (t1
, t2
, false, NULL
, visited
);
594 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
595 violation warnings. */
598 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
601 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
603 odr_violation_reported
= true;
604 if (DECL_VIRTUAL_P (prevailing
->decl
))
606 varpool_node
*tmp
= prevailing
;
610 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
612 "virtual table of type %qD violates one definition rule",
613 DECL_CONTEXT (vtable
->decl
)))
614 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
615 "variable of same assembler name as the virtual table is "
616 "defined in another translation unit");
619 if (!prevailing
->definition
|| !vtable
->definition
)
621 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
623 struct ipa_ref
*ref1
, *ref2
;
625 end1
= !prevailing
->iterate_reference (n1
, ref1
);
626 end2
= !vtable
->iterate_reference (n2
, ref2
);
630 && DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
631 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
633 && !DECL_VIRTUAL_P (ref2
->referred
->decl
)
634 && DECL_VIRTUAL_P (ref1
->referred
->decl
))
636 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), 0,
637 "virtual table of type %qD contains RTTI information",
638 DECL_CONTEXT (vtable
->decl
)))
640 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
641 "but is prevailed by one without from other translation unit");
642 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
643 "RTTI will not work on this type");
646 end2
= !vtable
->iterate_reference (n2
, ref2
);
649 && DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
650 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
652 && !DECL_VIRTUAL_P (ref1
->referred
->decl
)
653 && DECL_VIRTUAL_P (ref2
->referred
->decl
))
656 end1
= !vtable
->iterate_reference (n1
, ref1
);
662 varpool_node
*tmp
= prevailing
;
667 if (warning_at (DECL_SOURCE_LOCATION
668 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), 0,
669 "virtual table of type %qD violates "
670 "one definition rule",
671 DECL_CONTEXT (vtable
->decl
)))
673 inform (DECL_SOURCE_LOCATION
674 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
675 "the conflicting type defined in another translation "
677 inform (DECL_SOURCE_LOCATION
678 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
679 "contains additional virtual method %qD",
680 ref1
->referred
->decl
);
684 if (DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
685 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
))
687 if (warning_at (DECL_SOURCE_LOCATION
688 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), 0,
689 "virtual table of type %qD violates "
690 "one definition rule ",
691 DECL_CONTEXT (vtable
->decl
)))
693 inform (DECL_SOURCE_LOCATION
694 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
695 "the conflicting type defined in another translation "
697 inform (DECL_SOURCE_LOCATION (ref1
->referred
->decl
),
698 "virtual method %qD", ref1
->referred
->decl
);
699 inform (DECL_SOURCE_LOCATION (ref2
->referred
->decl
),
700 "ought to match virtual method %qD but does not",
701 ref2
->referred
->decl
);
708 /* Output ODR violation warning about T1 and T2 with REASON.
709 Display location of ST1 and ST2 if REASON speaks about field or
711 If WARN is false, do nothing. Set WARNED if warning was indeed
715 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
716 bool warn
, bool *warned
, const char *reason
)
718 tree decl2
= TYPE_NAME (t2
);
722 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)), OPT_Wodr
,
723 "type %qT violates one definition rule",
728 /* For FIELD_DECL support also case where one of fields is
729 NULL - this is used when the structures have mismatching number of
731 else if (!st1
|| TREE_CODE (st1
) == FIELD_DECL
)
733 inform (DECL_SOURCE_LOCATION (decl2
),
734 "a different type is defined in another translation unit");
740 inform (DECL_SOURCE_LOCATION (st1
),
741 "the first difference of corresponding definitions is field %qD",
746 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
748 inform (DECL_SOURCE_LOCATION (decl2
),
749 "a different type is defined in another translation unit");
750 inform (DECL_SOURCE_LOCATION (st1
),
751 "the first difference of corresponding definitions is method %qD",
757 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
763 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
764 because they are used on same place in ODR matching types.
765 They are not; inform the user. */
768 warn_types_mismatch (tree t1
, tree t2
)
770 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
772 /* In Firefox it is a common bug to have same types but in
773 different namespaces. Be a bit more informative on
775 if (TYPE_CONTEXT (t1
) && TYPE_CONTEXT (t2
)
776 && (((TREE_CODE (TYPE_CONTEXT (t1
)) == NAMESPACE_DECL
)
777 != (TREE_CODE (TYPE_CONTEXT (t2
)) == NAMESPACE_DECL
))
778 || (TREE_CODE (TYPE_CONTEXT (t1
)) == NAMESPACE_DECL
779 && (DECL_NAME (TYPE_CONTEXT (t1
)) !=
780 DECL_NAME (TYPE_CONTEXT (t2
))))))
781 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
782 "type %qT should match type %qT but is defined "
783 "in different namespace ",
786 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
787 "type %qT should match type %qT",
789 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2
)),
790 "the incompatible type is defined here");
793 /* Compare T1 and T2, report ODR violations if WARN is true and set
794 WARNED to true if anything is reported. Return true if types match.
795 If true is returned, the types are also compatible in the sense of
796 gimple_canonical_types_compatible_p. */
799 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
, hash_set
<type_pair
,pair_traits
> *visited
)
801 /* Check first for the obvious case of pointer identity. */
804 gcc_assert (!type_in_anonymous_namespace_p (t1
));
805 gcc_assert (!type_in_anonymous_namespace_p (t2
));
807 /* Can't be the same type if the types don't have the same code. */
808 if (TREE_CODE (t1
) != TREE_CODE (t2
))
810 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
811 G_("a different type is defined in another translation unit"));
815 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
817 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
818 G_("a type with different qualifiers is defined in another "
819 "translation unit"));
823 if (comp_type_attributes (t1
, t2
) != 1)
825 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
826 G_("a type with attributes "
827 "is defined in another translation unit"));
831 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
834 for (v1
= TYPE_VALUES (t1
), v2
= TYPE_VALUES (t2
);
835 v1
&& v2
; v1
= TREE_CHAIN (v1
), v2
= TREE_CHAIN (v2
))
837 if (TREE_PURPOSE (v1
) != TREE_PURPOSE (v2
))
839 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
840 G_("an enum with different value name"
841 " is defined in another translation unit"));
844 if (TREE_VALUE (v1
) != TREE_VALUE (v2
)
845 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1
)),
846 DECL_INITIAL (TREE_VALUE (v2
)), 0))
848 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
849 G_("an enum with different values is defined"
850 " in another translation unit"));
856 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
857 G_("an enum with mismatching number of values "
858 "is defined in another translation unit"));
863 /* Non-aggregate types can be handled cheaply. */
864 if (INTEGRAL_TYPE_P (t1
)
865 || SCALAR_FLOAT_TYPE_P (t1
)
866 || FIXED_POINT_TYPE_P (t1
)
867 || TREE_CODE (t1
) == VECTOR_TYPE
868 || TREE_CODE (t1
) == COMPLEX_TYPE
869 || TREE_CODE (t1
) == OFFSET_TYPE
870 || POINTER_TYPE_P (t1
))
872 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
874 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
875 G_("a type with different precision is defined "
876 "in another translation unit"));
879 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
881 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
882 G_("a type with different signedness is defined "
883 "in another translation unit"));
887 if (TREE_CODE (t1
) == INTEGER_TYPE
888 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
890 /* char WRT uint_8? */
891 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
892 G_("a different type is defined in another "
893 "translation unit"));
897 /* For canonical type comparisons we do not want to build SCCs
898 so we cannot compare pointed-to types. But we can, for now,
899 require the same pointed-to type kind and match what
900 useless_type_conversion_p would do. */
901 if (POINTER_TYPE_P (t1
))
903 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
904 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
906 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
907 G_("it is defined as a pointer in different address "
908 "space in another translation unit"));
912 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
914 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
915 G_("it is defined as a pointer to different type "
916 "in another translation unit"));
918 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
923 if ((TREE_CODE (t1
) == VECTOR_TYPE
|| TREE_CODE (t1
) == COMPLEX_TYPE
)
924 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
926 /* Probably specific enough. */
927 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
928 G_("a different type is defined "
929 "in another translation unit"));
931 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
935 /* Do type-specific comparisons. */
936 else switch (TREE_CODE (t1
))
940 /* Array types are the same if the element types are the same and
941 the number of elements are the same. */
942 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
944 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
945 G_("a different type is defined in another "
946 "translation unit"));
948 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
950 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
951 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
952 == TYPE_NONALIASED_COMPONENT (t2
));
954 tree i1
= TYPE_DOMAIN (t1
);
955 tree i2
= TYPE_DOMAIN (t2
);
957 /* For an incomplete external array, the type domain can be
958 NULL_TREE. Check this condition also. */
959 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
962 tree min1
= TYPE_MIN_VALUE (i1
);
963 tree min2
= TYPE_MIN_VALUE (i2
);
964 tree max1
= TYPE_MAX_VALUE (i1
);
965 tree max2
= TYPE_MAX_VALUE (i2
);
967 /* In C++, minimums should be always 0. */
968 gcc_assert (min1
== min2
);
969 if (!operand_equal_p (max1
, max2
, 0))
971 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
972 G_("an array of different size is defined "
973 "in another translation unit"));
981 /* Function types are the same if the return type and arguments types
983 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
985 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
986 G_("has different return value "
987 "in another translation unit"));
989 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
993 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
))
999 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
1001 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
1003 if (!odr_subtypes_equivalent_p
1004 (TREE_VALUE (parms1
), TREE_VALUE (parms2
), visited
))
1006 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1007 G_("has different parameters in another "
1008 "translation unit"));
1010 warn_types_mismatch (TREE_VALUE (parms1
),
1011 TREE_VALUE (parms2
));
1016 if (parms1
|| parms2
)
1018 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1019 G_("has different parameters "
1020 "in another translation unit"));
1029 case QUAL_UNION_TYPE
:
1033 /* For aggregate types, all the fields must be the same. */
1034 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
1036 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
1038 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
1040 /* Skip non-fields. */
1041 while (f1
&& TREE_CODE (f1
) != FIELD_DECL
)
1042 f1
= TREE_CHAIN (f1
);
1043 while (f2
&& TREE_CODE (f2
) != FIELD_DECL
)
1044 f2
= TREE_CHAIN (f2
);
1047 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
1049 if (DECL_NAME (f1
) != DECL_NAME (f2
)
1050 && !DECL_ARTIFICIAL (f1
))
1052 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1053 G_("a field with different name is defined "
1054 "in another translation unit"));
1057 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
), TREE_TYPE (f2
), visited
))
1059 /* Do not warn about artificial fields and just go into generic
1060 field mismatch warning. */
1061 if (DECL_ARTIFICIAL (f1
))
1064 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1065 G_("a field of same name but different type "
1066 "is defined in another translation unit"));
1068 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
));
1071 if (!gimple_compare_field_offset (f1
, f2
))
1073 /* Do not warn about artificial fields and just go into generic
1074 field mismatch warning. */
1075 if (DECL_ARTIFICIAL (f1
))
1077 warn_odr (t1
, t2
, t1
, t2
, warn
, warned
,
1078 G_("fields has different layout "
1079 "in another translation unit"));
1082 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
1083 == DECL_NONADDRESSABLE_P (f2
));
1086 /* If one aggregate has more fields than the other, they
1087 are not the same. */
1090 if (f1
&& DECL_ARTIFICIAL (f1
))
1092 if (f2
&& DECL_ARTIFICIAL (f2
))
1095 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1096 G_("a type with different number of fields "
1097 "is defined in another translation unit"));
1098 /* Ideally we should never get this generic message. */
1100 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1101 G_("a type with different memory representation "
1102 "is defined in another translation unit"));
1106 if ((TYPE_MAIN_VARIANT (t1
) == t1
|| TYPE_MAIN_VARIANT (t2
) == t2
)
1107 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1
))
1108 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2
))))
1110 for (f1
= TYPE_METHODS (TYPE_MAIN_VARIANT (t1
)),
1111 f2
= TYPE_METHODS (TYPE_MAIN_VARIANT (t2
));
1112 f1
&& f2
; f1
= DECL_CHAIN (f1
), f2
= DECL_CHAIN (f2
))
1114 if (DECL_ASSEMBLER_NAME (f1
) != DECL_ASSEMBLER_NAME (f2
))
1116 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1117 G_("a different method of same type "
1118 "is defined in another translation unit"));
1121 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1123 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1124 G_("s definition that differs by virtual "
1125 "keyword in another translation unit"));
1128 if (DECL_VINDEX (f1
) != DECL_VINDEX (f2
))
1130 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1131 G_("virtual table layout differs in another "
1132 "translation unit"));
1135 if (odr_subtypes_equivalent_p (TREE_TYPE (f1
), TREE_TYPE (f2
), visited
))
1137 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1138 G_("method with incompatible type is defined "
1139 "in another translation unit"));
1145 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1146 G_("a type with different number of methods "
1147 "is defined in another translation unit"));
1162 /* Those are better to come last as they are utterly uninformative. */
1163 if (TYPE_SIZE (t1
) && TYPE_SIZE (t2
)
1164 && !operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0))
1166 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1167 G_("a type with different size "
1168 "is defined in another translation unit"));
1171 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
)
1172 && TYPE_ALIGN (t1
) != TYPE_ALIGN (t2
))
1174 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1175 G_("a type with different alignment "
1176 "is defined in another translation unit"));
1179 gcc_assert (!TYPE_SIZE_UNIT (t1
) || !TYPE_SIZE_UNIT (t2
)
1180 || operand_equal_p (TYPE_SIZE_UNIT (t1
),
1181 TYPE_SIZE_UNIT (t2
), 0));
1185 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1186 from VAL->type. This may happen in LTO where tree merging did not merge
1187 all variants of the same type. It may or may not mean the ODR violation.
1188 Add it to the list of duplicates and warn on some violations. */
1191 add_type_duplicate (odr_type val
, tree type
)
1193 bool build_bases
= false;
1194 if (!val
->types_set
)
1195 val
->types_set
= new hash_set
<tree
>;
1197 /* Always prefer complete type to be the leader. */
1198 if (!COMPLETE_TYPE_P (val
->type
)
1199 && COMPLETE_TYPE_P (type
))
1208 /* See if this duplicate is new. */
1209 if (!val
->types_set
->add (type
))
1212 bool base_mismatch
= false;
1214 bool warned
= false;
1215 hash_set
<type_pair
,pair_traits
> visited
;
1217 gcc_assert (in_lto_p
);
1218 vec_safe_push (val
->types
, type
);
1220 /* First we compare memory layout. */
1221 if (!odr_types_equivalent_p (val
->type
, type
, !flag_ltrans
&& !val
->odr_violated
,
1225 odr_violation_reported
= true;
1226 val
->odr_violated
= true;
1227 if (symtab
->dump_file
)
1229 fprintf (symtab
->dump_file
, "ODR violation\n");
1231 print_node (symtab
->dump_file
, "", val
->type
, 0);
1232 putc ('\n',symtab
->dump_file
);
1233 print_node (symtab
->dump_file
, "", type
, 0);
1234 putc ('\n',symtab
->dump_file
);
1238 /* Next sanity check that bases are the same. If not, we will end
1239 up producing wrong answers. */
1240 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1241 && TREE_CODE (val
->type
) == RECORD_TYPE
1242 && TREE_CODE (type
) == RECORD_TYPE
1243 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1245 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1246 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type
), i
)))
1248 odr_type base
= get_odr_type
1250 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1253 if (val
->bases
.length () <= j
|| val
->bases
[j
] != base
)
1254 base_mismatch
= true;
1260 odr_violation_reported
= true;
1262 if (!warned
&& !val
->odr_violated
)
1263 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1264 "a type with the same name but different bases is "
1265 "defined in another translation unit");
1266 val
->odr_violated
= true;
1267 if (symtab
->dump_file
)
1269 fprintf (symtab
->dump_file
, "ODR bse violation or merging bug?\n");
1271 print_node (symtab
->dump_file
, "", val
->type
, 0);
1272 putc ('\n',symtab
->dump_file
);
1273 print_node (symtab
->dump_file
, "", type
, 0);
1274 putc ('\n',symtab
->dump_file
);
1279 /* Regularize things a little. During LTO same types may come with
1280 different BINFOs. Either because their virtual table was
1281 not merged by tree merging and only later at decl merging or
1282 because one type comes with external vtable, while other
1283 with internal. We want to merge equivalent binfos to conserve
1284 memory and streaming overhead.
1286 The external vtables are more harmful: they contain references
1287 to external declarations of methods that may be defined in the
1288 merged LTO unit. For this reason we absolutely need to remove
1289 them and replace by internal variants. Not doing so will lead
1290 to incomplete answers from possible_polymorphic_call_targets.
1292 FIXME: disable for now; because ODR types are now build during
1293 streaming in, the variants do not need to be linked to the type,
1294 yet. We need to do the merging in cleanup pass to be implemented
1296 if (!flag_ltrans
&& merge
1298 && TREE_CODE (val
->type
) == RECORD_TYPE
1299 && TREE_CODE (type
) == RECORD_TYPE
1300 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1301 && TYPE_MAIN_VARIANT (type
) == type
1302 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1303 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1304 && BINFO_VTABLE (TYPE_BINFO (type
)))
1306 tree master_binfo
= TYPE_BINFO (val
->type
);
1307 tree v1
= BINFO_VTABLE (master_binfo
);
1308 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1310 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1312 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1313 && operand_equal_p (TREE_OPERAND (v1
, 1),
1314 TREE_OPERAND (v2
, 1), 0));
1315 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1316 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1318 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1319 == DECL_ASSEMBLER_NAME (v2
));
1321 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1325 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1326 for (i
= 0; i
< val
->types
->length (); i
++)
1328 if (TYPE_BINFO ((*val
->types
)[i
])
1330 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1332 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1335 set_type_binfo (type
, master_binfo
);
1341 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1342 possibly new entry. */
1345 get_odr_type (tree type
, bool insert
)
1350 bool build_bases
= false;
1351 bool insert_to_odr_array
= false;
1354 type
= main_odr_variant (type
);
1356 hash
= hash_type_name (type
);
1357 slot
= odr_hash
->find_slot_with_hash (type
, hash
,
1358 insert
? INSERT
: NO_INSERT
);
1362 /* See if we already have entry for type. */
1367 /* With LTO we need to support multiple tree representation of
1368 the same ODR type. */
1369 if (val
->type
!= type
)
1370 build_bases
= add_type_duplicate (val
, type
);
1374 val
= ggc_cleared_alloc
<odr_type_d
> ();
1377 val
->derived_types
= vNULL
;
1378 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
1379 build_bases
= COMPLETE_TYPE_P (val
->type
);
1380 insert_to_odr_array
= true;
1383 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1384 && type
== TYPE_MAIN_VARIANT (type
))
1386 tree binfo
= TYPE_BINFO (type
);
1389 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) = type
);
1391 val
->all_derivations_known
= type_all_derivations_known_p (type
);
1393 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
1394 /* For now record only polymorphic types. other are
1395 pointless for devirtualization and we can not precisely
1396 determine ODR equivalency of these during LTO. */
1397 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
1399 odr_type base
= get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo
,
1402 gcc_assert (TYPE_MAIN_VARIANT (base
->type
) == base
->type
);
1403 base
->derived_types
.safe_push (val
);
1404 val
->bases
.safe_push (base
);
1405 if (base
->id
> base_id
)
1409 /* Ensure that type always appears after bases. */
1410 if (insert_to_odr_array
)
1413 val
->id
= odr_types
.length ();
1414 vec_safe_push (odr_types_ptr
, val
);
1416 else if (base_id
> val
->id
)
1418 odr_types
[val
->id
] = 0;
1419 /* Be sure we did not recorded any derived types; these may need
1421 gcc_assert (val
->derived_types
.length() == 0);
1423 val
->id
= odr_types
.length ();
1424 vec_safe_push (odr_types_ptr
, val
);
1429 /* Add TYPE od ODR type hash. */
1432 register_odr_type (tree type
)
1435 odr_hash
= new odr_hash_type (23);
1436 /* Arrange things to be nicer and insert main variants first. */
1437 if (odr_type_p (TYPE_MAIN_VARIANT (type
)))
1438 get_odr_type (TYPE_MAIN_VARIANT (type
), true);
1439 if (TYPE_MAIN_VARIANT (type
) != type
)
1440 get_odr_type (type
, true);
1443 /* Return true if type is known to have no derivations. */
1446 type_known_to_have_no_deriavations_p (tree t
)
1448 return (type_all_derivations_known_p (t
)
1449 && (TYPE_FINAL_P (t
)
1451 && !get_odr_type (t
, true)->derived_types
.length())));
1454 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
1455 recursive printing. */
1458 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
1461 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
1462 print_generic_expr (f
, t
->type
, TDF_SLIM
);
1463 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
1464 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
1465 if (TYPE_NAME (t
->type
))
1467 fprintf (f
, "%*s defined at: %s:%i\n", indent
* 2, "",
1468 DECL_SOURCE_FILE (TYPE_NAME (t
->type
)),
1469 DECL_SOURCE_LINE (TYPE_NAME (t
->type
)));
1471 if (t
->bases
.length ())
1473 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
1474 for (i
= 0; i
< t
->bases
.length (); i
++)
1475 fprintf (f
, " %i", t
->bases
[i
]->id
);
1478 if (t
->derived_types
.length ())
1480 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
1481 for (i
= 0; i
< t
->derived_types
.length (); i
++)
1482 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
1487 /* Dump the type inheritance graph. */
1490 dump_type_inheritance_graph (FILE *f
)
1495 fprintf (f
, "\n\nType inheritance graph:\n");
1496 for (i
= 0; i
< odr_types
.length (); i
++)
1498 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
1499 dump_odr_type (f
, odr_types
[i
]);
1501 for (i
= 0; i
< odr_types
.length (); i
++)
1503 if (odr_types
[i
] && odr_types
[i
]->types
&& odr_types
[i
]->types
->length ())
1506 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
1507 print_node (f
, "", odr_types
[i
]->type
, 0);
1508 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
1511 fprintf (f
, "duplicate #%i\n", j
);
1512 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
1513 t
= (*odr_types
[i
]->types
)[j
];
1514 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
1516 t
= TYPE_CONTEXT (t
);
1517 print_node (f
, "", t
, 0);
1525 /* Given method type T, return type of class it belongs to.
1526 Look up this pointer and get its type. */
1529 method_class_type (const_tree t
)
1531 tree first_parm_type
= TREE_VALUE (TYPE_ARG_TYPES (t
));
1532 gcc_assert (TREE_CODE (t
) == METHOD_TYPE
);
1534 return TREE_TYPE (first_parm_type
);
1537 /* Initialize IPA devirt and build inheritance tree graph. */
1540 build_type_inheritance_graph (void)
1542 struct symtab_node
*n
;
1543 FILE *inheritance_dump_file
;
1548 timevar_push (TV_IPA_INHERITANCE
);
1549 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
1550 odr_hash
= new odr_hash_type (23);
1552 /* We reconstruct the graph starting of types of all methods seen in the
1555 if (is_a
<cgraph_node
*> (n
)
1556 && DECL_VIRTUAL_P (n
->decl
)
1557 && n
->real_symbol_p ())
1558 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n
->decl
))),
1561 /* Look also for virtual tables of types that do not define any methods.
1563 We need it in a case where class B has virtual base of class A
1564 re-defining its virtual method and there is class C with no virtual
1565 methods with B as virtual base.
1567 Here we output B's virtual method in two variant - for non-virtual
1568 and virtual inheritance. B's virtual table has non-virtual version,
1569 while C's has virtual.
1571 For this reason we need to know about C in order to include both
1572 variants of B. More correctly, record_target_from_binfo should
1573 add both variants of the method when walking B, but we have no
1574 link in between them.
1576 We rely on fact that either the method is exported and thus we
1577 assume it is called externally or C is in anonymous namespace and
1578 thus we will see the vtable. */
1580 else if (is_a
<varpool_node
*> (n
)
1581 && DECL_VIRTUAL_P (n
->decl
)
1582 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
1583 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
1584 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
1585 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
1586 if (inheritance_dump_file
)
1588 dump_type_inheritance_graph (inheritance_dump_file
);
1589 dump_end (TDI_inheritance
, inheritance_dump_file
);
1591 timevar_pop (TV_IPA_INHERITANCE
);
1594 /* Return true if N has reference from live virtual table
1595 (and thus can be a destination of polymorphic call).
1596 Be conservatively correct when callgraph is not built or
1597 if the method may be referred externally. */
1600 referenced_from_vtable_p (struct cgraph_node
*node
)
1603 struct ipa_ref
*ref
;
1606 if (node
->externally_visible
1607 || DECL_EXTERNAL (node
->decl
)
1608 || node
->used_from_other_partition
)
1611 /* Keep this test constant time.
1612 It is unlikely this can happen except for the case where speculative
1613 devirtualization introduced many speculative edges to this node.
1614 In this case the target is very likely alive anyway. */
1615 if (node
->ref_list
.referring
.length () > 100)
1618 /* We need references built. */
1619 if (symtab
->state
<= CONSTRUCTION
)
1622 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
1624 if ((ref
->use
== IPA_REF_ALIAS
1625 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
1626 || (ref
->use
== IPA_REF_ADDR
1627 && TREE_CODE (ref
->referring
->decl
) == VAR_DECL
1628 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
1636 /* If TARGET has associated node, record it in the NODES array.
1637 CAN_REFER specify if program can refer to the target directly.
1638 if TARGET is unknown (NULL) or it can not be inserted (for example because
1639 its body was already removed and there is no way to refer to it), clear
1643 maybe_record_node (vec
<cgraph_node
*> &nodes
,
1644 tree target
, hash_set
<tree
> *inserted
,
1648 struct cgraph_node
*target_node
, *alias_target
;
1649 enum availability avail
;
1651 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
1652 list of targets; the runtime effect of calling them is undefined.
1653 Only "real" virtual methods should be accounted. */
1654 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
)
1659 /* The only case when method of anonymous namespace becomes unreferable
1660 is when we completely optimized it out. */
1663 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
1671 target_node
= cgraph_node::get (target
);
1673 /* Prefer alias target over aliases, so we do not get confused by
1677 alias_target
= target_node
->ultimate_alias_target (&avail
);
1678 if (target_node
!= alias_target
1679 && avail
>= AVAIL_AVAILABLE
1680 && target_node
->get_availability ())
1681 target_node
= alias_target
;
1684 /* Method can only be called by polymorphic call if any
1685 of vtables referring to it are alive.
1687 While this holds for non-anonymous functions, too, there are
1688 cases where we want to keep them in the list; for example
1689 inline functions with -fno-weak are static, but we still
1690 may devirtualize them when instance comes from other unit.
1691 The same holds for LTO.
1693 Currently we ignore these functions in speculative devirtualization.
1694 ??? Maybe it would make sense to be more aggressive for LTO even
1697 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
1699 || !referenced_from_vtable_p (target_node
)))
1701 /* See if TARGET is useful function we can deal with. */
1702 else if (target_node
!= NULL
1703 && (TREE_PUBLIC (target
)
1704 || DECL_EXTERNAL (target
)
1705 || target_node
->definition
)
1706 && target_node
->real_symbol_p ())
1708 gcc_assert (!target_node
->global
.inlined_to
);
1709 gcc_assert (target_node
->real_symbol_p ());
1710 if (!inserted
->add (target
))
1712 cached_polymorphic_call_targets
->add (target_node
);
1713 nodes
.safe_push (target_node
);
1717 && (!type_in_anonymous_namespace_p
1718 (DECL_CONTEXT (target
))
1723 /* See if BINFO's type matches OUTER_TYPE. If so, look up
1724 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
1725 method in vtable and insert method to NODES array
1726 or BASES_TO_CONSIDER if this array is non-NULL.
1727 Otherwise recurse to base BINFOs.
1728 This matches what get_binfo_at_offset does, but with offset
1731 TYPE_BINFOS is a stack of BINFOS of types with defined
1732 virtual table seen on way from class type to BINFO.
1734 MATCHED_VTABLES tracks virtual tables we already did lookup
1735 for virtual function in. INSERTED tracks nodes we already
1738 ANONYMOUS is true if BINFO is part of anonymous namespace.
1740 Clear COMPLETEP when we hit unreferable target.
1744 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
1745 vec
<tree
> *bases_to_consider
,
1748 vec
<tree
> &type_binfos
,
1749 HOST_WIDE_INT otr_token
,
1751 HOST_WIDE_INT offset
,
1752 hash_set
<tree
> *inserted
,
1753 hash_set
<tree
> *matched_vtables
,
1757 tree type
= BINFO_TYPE (binfo
);
1762 if (BINFO_VTABLE (binfo
))
1763 type_binfos
.safe_push (binfo
);
1764 if (types_same_for_odr (type
, outer_type
))
1767 tree type_binfo
= NULL
;
1769 /* Look up BINFO with virtual table. For normal types it is always last
1771 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
1772 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
1774 type_binfo
= type_binfos
[i
];
1777 if (BINFO_VTABLE (binfo
))
1779 /* If this is duplicated BINFO for base shared by virtual inheritance,
1780 we may not have its associated vtable. This is not a problem, since
1781 we will walk it on the other path. */
1784 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
1788 gcc_assert (odr_violation_reported
);
1791 /* For types in anonymous namespace first check if the respective vtable
1792 is alive. If not, we know the type can't be called. */
1793 if (!flag_ltrans
&& anonymous
)
1795 tree vtable
= BINFO_VTABLE (inner_binfo
);
1796 varpool_node
*vnode
;
1798 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
1799 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
1800 vnode
= varpool_node::get (vtable
);
1801 if (!vnode
|| !vnode
->definition
)
1804 gcc_assert (inner_binfo
);
1805 if (bases_to_consider
1806 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
1807 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
1810 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
1813 if (!bases_to_consider
)
1814 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
1815 /* Destructors are never called via construction vtables. */
1816 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
1817 bases_to_consider
->safe_push (target
);
1823 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1824 /* Walking bases that have no virtual method is pointless exercise. */
1825 if (polymorphic_type_binfo_p (base_binfo
))
1826 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
1828 otr_token
, outer_type
, offset
, inserted
,
1829 matched_vtables
, anonymous
, completep
);
1830 if (BINFO_VTABLE (binfo
))
1834 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
1835 of TYPE, insert them to NODES, recurse into derived nodes.
1836 INSERTED is used to avoid duplicate insertions of methods into NODES.
1837 MATCHED_VTABLES are used to avoid duplicate walking vtables.
1838 Clear COMPLETEP if unreferable target is found.
1840 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
1841 all cases where BASE_SKIPPED is true (because the base is abstract
1845 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
1846 hash_set
<tree
> *inserted
,
1847 hash_set
<tree
> *matched_vtables
,
1850 HOST_WIDE_INT otr_token
,
1852 HOST_WIDE_INT offset
,
1854 vec
<tree
> &bases_to_consider
,
1855 bool consider_construction
)
1857 tree binfo
= TYPE_BINFO (type
->type
);
1859 auto_vec
<tree
, 8> type_binfos
;
1860 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
1862 /* We may need to consider types w/o instances because of possible derived
1863 types using their methods either directly or via construction vtables.
1864 We are safe to skip them when all derivations are known, since we will
1866 This is done by recording them to BASES_TO_CONSIDER array. */
1867 if (possibly_instantiated
|| consider_construction
)
1869 record_target_from_binfo (nodes
,
1870 (!possibly_instantiated
1871 && type_all_derivations_known_p (type
->type
))
1872 ? &bases_to_consider
: NULL
,
1873 binfo
, otr_type
, type_binfos
, otr_token
,
1875 inserted
, matched_vtables
,
1876 type
->anonymous_namespace
, completep
);
1878 for (i
= 0; i
< type
->derived_types
.length (); i
++)
1879 possible_polymorphic_call_targets_1 (nodes
, inserted
,
1882 type
->derived_types
[i
],
1883 otr_token
, outer_type
, offset
, completep
,
1884 bases_to_consider
, consider_construction
);
1887 /* Cache of queries for polymorphic call targets.
1889 Enumerating all call targets may get expensive when there are many
1890 polymorphic calls in the program, so we memoize all the previous
1891 queries and avoid duplicated work. */
1893 struct polymorphic_call_target_d
1895 HOST_WIDE_INT otr_token
;
1896 ipa_polymorphic_call_context context
;
1898 vec
<cgraph_node
*> targets
;
1905 /* Polymorphic call target cache helpers. */
1907 struct polymorphic_call_target_hasher
1909 typedef polymorphic_call_target_d value_type
;
1910 typedef polymorphic_call_target_d compare_type
;
1911 static inline hashval_t
hash (const value_type
*);
1912 static inline bool equal (const value_type
*, const compare_type
*);
1913 static inline void remove (value_type
*);
1916 /* Return the computed hashcode for ODR_QUERY. */
1919 polymorphic_call_target_hasher::hash (const value_type
*odr_query
)
1921 inchash::hash
hstate (odr_query
->otr_token
);
1923 hstate
.add_wide_int (odr_query
->type
->id
);
1924 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
1925 hstate
.add_wide_int (odr_query
->context
.offset
);
1927 if (odr_query
->context
.speculative_outer_type
)
1929 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
1930 hstate
.add_wide_int (odr_query
->context
.speculative_offset
);
1932 hstate
.add_flag (odr_query
->speculative
);
1933 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
1934 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
1935 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
1936 hstate
.commit_flag ();
1937 return hstate
.end ();
1940 /* Compare cache entries T1 and T2. */
1943 polymorphic_call_target_hasher::equal (const value_type
*t1
,
1944 const compare_type
*t2
)
1946 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
1947 && t1
->speculative
== t2
->speculative
1948 && t1
->context
.offset
== t2
->context
.offset
1949 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
1950 && t1
->context
.outer_type
== t2
->context
.outer_type
1951 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
1952 && t1
->context
.maybe_in_construction
1953 == t2
->context
.maybe_in_construction
1954 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
1955 && (t1
->context
.speculative_maybe_derived_type
1956 == t2
->context
.speculative_maybe_derived_type
));
1959 /* Remove entry in polymorphic call target cache hash. */
1962 polymorphic_call_target_hasher::remove (value_type
*v
)
1964 v
->targets
.release ();
1968 /* Polymorphic call target query cache. */
1970 typedef hash_table
<polymorphic_call_target_hasher
>
1971 polymorphic_call_target_hash_type
;
1972 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
1974 /* Destroy polymorphic call target query cache. */
1977 free_polymorphic_call_targets_hash ()
1979 if (cached_polymorphic_call_targets
)
1981 delete polymorphic_call_target_hash
;
1982 polymorphic_call_target_hash
= NULL
;
1983 delete cached_polymorphic_call_targets
;
1984 cached_polymorphic_call_targets
= NULL
;
1988 /* When virtual function is removed, we may need to flush the cache. */
1991 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
1993 if (cached_polymorphic_call_targets
1994 && cached_polymorphic_call_targets
->contains (n
))
1995 free_polymorphic_call_targets_hash ();
1998 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2001 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2004 tree v
= BINFO_VTABLE (binfo
);
2007 unsigned HOST_WIDE_INT this_offset
;
2011 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2014 if (offset
== this_offset
2015 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2019 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2020 if (polymorphic_type_binfo_p (base_binfo
))
2022 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2029 /* T is known constant value of virtual table pointer.
2030 Store virtual table to V and its offset to OFFSET.
2031 Return false if T does not look like virtual table reference. */
2034 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2035 unsigned HOST_WIDE_INT
*offset
)
2037 /* We expect &MEM[(void *)&virtual_table + 16B].
2038 We obtain object's BINFO from the context of the virtual table.
2039 This one contains pointer to virtual table represented via
2040 POINTER_PLUS_EXPR. Verify that this pointer matches what
2041 we propagated through.
2043 In the case of virtual inheritance, the virtual tables may
2044 be nested, i.e. the offset may be different from 16 and we may
2045 need to dive into the type representation. */
2046 if (TREE_CODE (t
) == ADDR_EXPR
2047 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2048 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2049 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2050 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2052 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2053 (TREE_OPERAND (t
, 0), 0), 0)))
2055 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2056 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2060 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2061 We need to handle it when T comes from static variable initializer or
2063 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2065 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2066 t
= TREE_OPERAND (t
, 0);
2071 if (TREE_CODE (t
) != ADDR_EXPR
)
2073 *v
= TREE_OPERAND (t
, 0);
2077 /* T is known constant value of virtual table pointer. Return BINFO of the
2081 vtable_pointer_value_to_binfo (const_tree t
)
2084 unsigned HOST_WIDE_INT offset
;
2086 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2089 /* FIXME: for stores of construction vtables we return NULL,
2090 because we do not have BINFO for those. Eventually we should fix
2091 our representation to allow this case to be handled, too.
2092 In the case we see store of BINFO we however may assume
2093 that standard folding will be able to cope with it. */
2094 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2098 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2099 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2100 and insert them in NODES.
2102 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2105 record_targets_from_bases (tree otr_type
,
2106 HOST_WIDE_INT otr_token
,
2108 HOST_WIDE_INT offset
,
2109 vec
<cgraph_node
*> &nodes
,
2110 hash_set
<tree
> *inserted
,
2111 hash_set
<tree
> *matched_vtables
,
2116 HOST_WIDE_INT pos
, size
;
2120 if (types_same_for_odr (outer_type
, otr_type
))
2123 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
2125 if (TREE_CODE (fld
) != FIELD_DECL
)
2128 pos
= int_bit_position (fld
);
2129 size
= tree_to_shwi (DECL_SIZE (fld
));
2130 if (pos
<= offset
&& (pos
+ size
) > offset
2131 /* Do not get confused by zero sized bases. */
2132 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
2135 /* Within a class type we should always find corresponding fields. */
2136 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
2138 /* Nonbase types should have been stripped by outer_class_type. */
2139 gcc_assert (DECL_ARTIFICIAL (fld
));
2141 outer_type
= TREE_TYPE (fld
);
2144 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
2148 gcc_assert (odr_violation_reported
);
2151 gcc_assert (base_binfo
);
2152 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
2155 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
2158 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
2159 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
2160 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
2165 /* When virtual table is removed, we may need to flush the cache. */
2168 devirt_variable_node_removal_hook (varpool_node
*n
,
2169 void *d ATTRIBUTE_UNUSED
)
2171 if (cached_polymorphic_call_targets
2172 && DECL_VIRTUAL_P (n
->decl
)
2173 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
2174 free_polymorphic_call_targets_hash ();
2177 /* Record about how many calls would benefit from given type to be final. */
2179 struct odr_type_warn_count
2183 gcov_type dyn_count
;
2186 /* Record about how many calls would benefit from given method to be final. */
2188 struct decl_warn_count
2192 gcov_type dyn_count
;
2195 /* Information about type and decl warnings. */
2197 struct final_warning_record
2199 gcov_type dyn_count
;
2200 vec
<odr_type_warn_count
> type_warnings
;
2201 hash_map
<tree
, decl_warn_count
> decl_warnings
;
2203 struct final_warning_record
*final_warning_records
;
2205 /* Return vector containing possible targets of polymorphic call of type
2206 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2207 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2208 OTR_TYPE and include their virtual method. This is useful for types
2209 possibly in construction or destruction where the virtual table may
2210 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2211 us to walk the inheritance graph for all derivations.
2213 If COMPLETEP is non-NULL, store true if the list is complete.
2214 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2215 in the target cache. If user needs to visit every target list
2216 just once, it can memoize them.
2218 If SPECULATIVE is set, the list will not contain targets that
2219 are not speculatively taken.
2221 Returned vector is placed into cache. It is NOT caller's responsibility
2222 to free it. The vector can be freed on cgraph_remove_node call if
2223 the particular node is a virtual function present in the cache. */
2226 possible_polymorphic_call_targets (tree otr_type
,
2227 HOST_WIDE_INT otr_token
,
2228 ipa_polymorphic_call_context context
,
2233 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
2234 vec
<cgraph_node
*> nodes
= vNULL
;
2235 auto_vec
<tree
, 8> bases_to_consider
;
2236 odr_type type
, outer_type
;
2237 polymorphic_call_target_d key
;
2238 polymorphic_call_target_d
**slot
;
2242 bool can_refer
= false;
2243 bool skipped
= false;
2245 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
2247 /* If ODR is not initialized or the context is invalid, return empty
2249 if (!odr_hash
|| context
.invalid
|| !TYPE_BINFO (otr_type
))
2252 *completep
= context
.invalid
;
2254 *cache_token
= NULL
;
2258 /* Do not bother to compute speculative info when user do not asks for it. */
2259 if (!speculative
|| !context
.speculative_outer_type
)
2260 context
.clear_speculation ();
2262 type
= get_odr_type (otr_type
, true);
2264 /* Recording type variants would waste results cache. */
2265 gcc_assert (!context
.outer_type
2266 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
2268 /* Look up the outer class type we want to walk.
2269 If we fail to do so, the context is invalid. */
2270 if ((context
.outer_type
|| context
.speculative_outer_type
)
2271 && !context
.restrict_to_inner_class (otr_type
))
2276 *cache_token
= NULL
;
2279 gcc_assert (!context
.invalid
);
2281 /* Check that restrict_to_inner_class kept the main variant. */
2282 gcc_assert (!context
.outer_type
2283 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
2285 /* We canonicalize our query, so we do not need extra hashtable entries. */
2287 /* Without outer type, we have no use for offset. Just do the
2288 basic search from inner type. */
2289 if (!context
.outer_type
)
2290 context
.clear_outer_type (otr_type
);
2291 /* We need to update our hierarchy if the type does not exist. */
2292 outer_type
= get_odr_type (context
.outer_type
, true);
2293 /* If the type is complete, there are no derivations. */
2294 if (TYPE_FINAL_P (outer_type
->type
))
2295 context
.maybe_derived_type
= false;
2297 /* Initialize query cache. */
2298 if (!cached_polymorphic_call_targets
)
2300 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
2301 polymorphic_call_target_hash
2302 = new polymorphic_call_target_hash_type (23);
2303 if (!node_removal_hook_holder
)
2305 node_removal_hook_holder
=
2306 symtab
->add_cgraph_removal_hook (&devirt_node_removal_hook
, NULL
);
2307 symtab
->add_varpool_removal_hook (&devirt_variable_node_removal_hook
,
2314 if (context
.outer_type
!= otr_type
)
2316 = get_odr_type (context
.outer_type
, true)->type
;
2317 if (context
.speculative_outer_type
)
2318 context
.speculative_outer_type
2319 = get_odr_type (context
.speculative_outer_type
, true)->type
;
2322 /* Look up cached answer. */
2324 key
.otr_token
= otr_token
;
2325 key
.speculative
= speculative
;
2326 key
.context
= context
;
2327 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
2329 *cache_token
= (void *)*slot
;
2333 *completep
= (*slot
)->complete
;
2334 if ((*slot
)->type_warning
&& final_warning_records
)
2336 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
2337 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
2338 += final_warning_records
->dyn_count
;
2340 if (!speculative
&& (*slot
)->decl_warning
&& final_warning_records
)
2342 struct decl_warn_count
*c
=
2343 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
2345 c
->dyn_count
+= final_warning_records
->dyn_count
;
2347 return (*slot
)->targets
;
2352 /* Do actual search. */
2353 timevar_push (TV_IPA_VIRTUAL_CALL
);
2354 *slot
= XCNEW (polymorphic_call_target_d
);
2356 *cache_token
= (void *)*slot
;
2357 (*slot
)->type
= type
;
2358 (*slot
)->otr_token
= otr_token
;
2359 (*slot
)->context
= context
;
2360 (*slot
)->speculative
= speculative
;
2362 hash_set
<tree
> inserted
;
2363 hash_set
<tree
> matched_vtables
;
2365 /* First insert targets we speculatively identified as likely. */
2366 if (context
.speculative_outer_type
)
2368 odr_type speculative_outer_type
;
2369 bool speculation_complete
= true;
2371 /* First insert target from type itself and check if it may have
2373 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
2374 if (TYPE_FINAL_P (speculative_outer_type
->type
))
2375 context
.speculative_maybe_derived_type
= false;
2376 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
2377 context
.speculative_offset
, otr_type
);
2379 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
2384 /* In the case we get complete method, we don't need
2385 to walk derivations. */
2386 if (target
&& DECL_FINAL_P (target
))
2387 context
.speculative_maybe_derived_type
= false;
2388 if (type_possibly_instantiated_p (speculative_outer_type
->type
))
2389 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &speculation_complete
);
2391 matched_vtables
.add (BINFO_VTABLE (binfo
));
2394 /* Next walk recursively all derived types. */
2395 if (context
.speculative_maybe_derived_type
)
2396 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
2397 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
2400 speculative_outer_type
->derived_types
[i
],
2401 otr_token
, speculative_outer_type
->type
,
2402 context
.speculative_offset
,
2403 &speculation_complete
,
2408 if (!speculative
|| !nodes
.length ())
2410 /* First see virtual method of type itself. */
2411 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
2412 context
.offset
, otr_type
);
2414 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
2418 gcc_assert (odr_violation_reported
);
2422 /* Destructors are never called through construction virtual tables,
2423 because the type is always known. */
2424 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
2425 context
.maybe_in_construction
= false;
2429 /* In the case we get complete method, we don't need
2430 to walk derivations. */
2431 if (DECL_FINAL_P (target
))
2432 context
.maybe_derived_type
= false;
2435 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
2436 if (type_possibly_instantiated_p (outer_type
->type
))
2437 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
2442 matched_vtables
.add (BINFO_VTABLE (binfo
));
2444 /* Next walk recursively all derived types. */
2445 if (context
.maybe_derived_type
)
2447 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
2448 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
2451 outer_type
->derived_types
[i
],
2452 otr_token
, outer_type
->type
,
2453 context
.offset
, &complete
,
2455 context
.maybe_in_construction
);
2457 if (!outer_type
->all_derivations_known
)
2459 if (!speculative
&& final_warning_records
)
2462 && nodes
.length () == 1
2463 && warn_suggest_final_types
2464 && !outer_type
->derived_types
.length ())
2466 if (outer_type
->id
>= (int)final_warning_records
->type_warnings
.length ())
2467 final_warning_records
->type_warnings
.safe_grow_cleared
2468 (odr_types
.length ());
2469 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
2470 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
2471 += final_warning_records
->dyn_count
;
2472 final_warning_records
->type_warnings
[outer_type
->id
].type
2474 (*slot
)->type_warning
= outer_type
->id
+ 1;
2477 && warn_suggest_final_methods
2478 && nodes
.length () == 1
2479 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
2483 struct decl_warn_count
&c
=
2484 final_warning_records
->decl_warnings
.get_or_insert
2485 (nodes
[0]->decl
, &existed
);
2490 c
.dyn_count
+= final_warning_records
->dyn_count
;
2495 c
.dyn_count
= final_warning_records
->dyn_count
;
2496 c
.decl
= nodes
[0]->decl
;
2498 (*slot
)->decl_warning
= nodes
[0]->decl
;
2507 /* Destructors are never called through construction virtual tables,
2508 because the type is always known. One of entries may be
2509 cxa_pure_virtual so look to at least two of them. */
2510 if (context
.maybe_in_construction
)
2511 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
2512 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
2513 context
.maybe_in_construction
= false;
2514 if (context
.maybe_in_construction
)
2516 if (type
!= outer_type
2518 || (context
.maybe_derived_type
2519 && !type_all_derivations_known_p (outer_type
->type
))))
2520 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
2521 context
.offset
, nodes
, &inserted
,
2522 &matched_vtables
, &complete
);
2524 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
2525 for (i
= 0; i
< bases_to_consider
.length(); i
++)
2526 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
2531 (*slot
)->targets
= nodes
;
2532 (*slot
)->complete
= complete
;
2534 *completep
= complete
;
2536 timevar_pop (TV_IPA_VIRTUAL_CALL
);
2541 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
2542 vec
<const decl_warn_count
*> *vec
)
2544 vec
->safe_push (&value
);
2548 /* Dump target list TARGETS into FILE. */
2551 dump_targets (FILE *f
, vec
<cgraph_node
*> targets
)
2555 for (i
= 0; i
< targets
.length (); i
++)
2559 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
2560 fprintf (f
, " %s/%i", name
? name
: targets
[i
]->name (), targets
[i
]->order
);
2563 if (!targets
[i
]->definition
)
2564 fprintf (f
, " (no definition%s)",
2565 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
2571 /* Dump all possible targets of a polymorphic call. */
2574 dump_possible_polymorphic_call_targets (FILE *f
,
2576 HOST_WIDE_INT otr_token
,
2577 const ipa_polymorphic_call_context
&ctx
)
2579 vec
<cgraph_node
*> targets
;
2581 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
2586 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
2588 &final
, NULL
, false);
2589 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
2590 print_generic_expr (f
, type
->type
, TDF_SLIM
);
2591 fprintf (f
, " token %i\n", (int)otr_token
);
2595 fprintf (f
, " %s%s%s%s\n ",
2596 final
? "This is a complete list." :
2597 "This is partial list; extra targets may be defined in other units.",
2598 ctx
.maybe_in_construction
? " (base types included)" : "",
2599 ctx
.maybe_derived_type
? " (derived types included)" : "",
2600 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
2601 len
= targets
.length ();
2602 dump_targets (f
, targets
);
2604 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
2606 &final
, NULL
, true);
2607 gcc_assert (targets
.length () <= len
);
2608 if (targets
.length () != len
)
2610 fprintf (f
, " Speculative targets:");
2611 dump_targets (f
, targets
);
2617 /* Return true if N can be possibly target of a polymorphic call of
2618 OTR_TYPE/OTR_TOKEN. */
2621 possible_polymorphic_call_target_p (tree otr_type
,
2622 HOST_WIDE_INT otr_token
,
2623 const ipa_polymorphic_call_context
&ctx
,
2624 struct cgraph_node
*n
)
2626 vec
<cgraph_node
*> targets
;
2628 enum built_in_function fcode
;
2631 if (TREE_CODE (TREE_TYPE (n
->decl
)) == FUNCTION_TYPE
2632 && ((fcode
= DECL_FUNCTION_CODE (n
->decl
))
2633 == BUILT_IN_UNREACHABLE
2634 || fcode
== BUILT_IN_TRAP
))
2639 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
2640 for (i
= 0; i
< targets
.length (); i
++)
2641 if (n
->semantically_equivalent_p (targets
[i
]))
2644 /* At a moment we allow middle end to dig out new external declarations
2645 as a targets of polymorphic calls. */
2646 if (!final
&& !n
->definition
)
2653 /* Return true if N can be possibly target of a polymorphic call of
2654 OBJ_TYPE_REF expression REF in STMT. */
2657 possible_polymorphic_call_target_p (tree ref
,
2659 struct cgraph_node
*n
)
2661 ipa_polymorphic_call_context
context (current_function_decl
, ref
, stmt
);
2662 tree call_fn
= gimple_call_fn (stmt
);
2664 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn
),
2666 (OBJ_TYPE_REF_TOKEN (call_fn
)),
2672 /* After callgraph construction new external nodes may appear.
2673 Add them into the graph. */
2676 update_type_inheritance_graph (void)
2678 struct cgraph_node
*n
;
2682 free_polymorphic_call_targets_hash ();
2683 timevar_push (TV_IPA_INHERITANCE
);
2684 /* We reconstruct the graph starting from types of all methods seen in the
2686 FOR_EACH_FUNCTION (n
)
2687 if (DECL_VIRTUAL_P (n
->decl
)
2689 && n
->real_symbol_p ())
2690 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n
->decl
))),
2692 timevar_pop (TV_IPA_INHERITANCE
);
2696 /* Return true if N looks like likely target of a polymorphic call.
2697 Rule out cxa_pure_virtual, noreturns, function declared cold and
2698 other obvious cases. */
2701 likely_target_p (struct cgraph_node
*n
)
2704 /* cxa_pure_virtual and similar things are not likely. */
2705 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
2707 flags
= flags_from_decl_or_type (n
->decl
);
2708 if (flags
& ECF_NORETURN
)
2710 if (lookup_attribute ("cold",
2711 DECL_ATTRIBUTES (n
->decl
)))
2713 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
2715 /* If there are no live virtual tables referring the target,
2716 the only way the target can be called is an instance coming from other
2717 compilation unit; speculative devirtualization is built around an
2718 assumption that won't happen. */
2719 if (!referenced_from_vtable_p (n
))
2724 /* Compare type warning records P1 and P2 and choose one with larger count;
2725 helper for qsort. */
2728 type_warning_cmp (const void *p1
, const void *p2
)
2730 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
2731 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
2733 if (t1
->dyn_count
< t2
->dyn_count
)
2735 if (t1
->dyn_count
> t2
->dyn_count
)
2737 return t2
->count
- t1
->count
;
2740 /* Compare decl warning records P1 and P2 and choose one with larger count;
2741 helper for qsort. */
2744 decl_warning_cmp (const void *p1
, const void *p2
)
2746 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
2747 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
2749 if (t1
->dyn_count
< t2
->dyn_count
)
2751 if (t1
->dyn_count
> t2
->dyn_count
)
2753 return t2
->count
- t1
->count
;
2757 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
2760 struct cgraph_node
*
2761 try_speculative_devirtualization (tree otr_type
, HOST_WIDE_INT otr_token
,
2762 ipa_polymorphic_call_context ctx
)
2764 vec
<cgraph_node
*>targets
2765 = possible_polymorphic_call_targets
2766 (otr_type
, otr_token
, ctx
, NULL
, NULL
, true);
2768 struct cgraph_node
*likely_target
= NULL
;
2770 for (i
= 0; i
< targets
.length (); i
++)
2771 if (likely_target_p (targets
[i
]))
2775 likely_target
= targets
[i
];
2778 ||!likely_target
->definition
2779 || DECL_EXTERNAL (likely_target
->decl
))
2782 /* Don't use an implicitly-declared destructor (c++/58678). */
2783 struct cgraph_node
*non_thunk_target
2784 = likely_target
->function_symbol ();
2785 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
2787 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
2788 && likely_target
->can_be_discarded_p ())
2790 return likely_target
;
2793 /* The ipa-devirt pass.
2794 When polymorphic call has only one likely target in the unit,
2795 turn it into a speculative call. */
2800 struct cgraph_node
*n
;
2801 hash_set
<void *> bad_call_targets
;
2802 struct cgraph_edge
*e
;
2804 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
2805 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
2806 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
2811 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
2812 This is implemented by setting up final_warning_records that are updated
2813 by get_polymorphic_call_targets.
2814 We need to clear cache in this case to trigger recomputation of all
2816 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
2818 final_warning_records
= new (final_warning_record
);
2819 final_warning_records
->type_warnings
= vNULL
;
2820 final_warning_records
->type_warnings
.safe_grow_cleared (odr_types
.length ());
2821 free_polymorphic_call_targets_hash ();
2824 FOR_EACH_DEFINED_FUNCTION (n
)
2826 bool update
= false;
2827 if (!opt_for_fn (n
->decl
, flag_devirtualize
))
2829 if (dump_file
&& n
->indirect_calls
)
2830 fprintf (dump_file
, "\n\nProcesing function %s/%i\n",
2831 n
->name (), n
->order
);
2832 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
2833 if (e
->indirect_info
->polymorphic
)
2835 struct cgraph_node
*likely_target
= NULL
;
2839 if (final_warning_records
)
2840 final_warning_records
->dyn_count
= e
->count
;
2842 vec
<cgraph_node
*>targets
2843 = possible_polymorphic_call_targets
2844 (e
, &final
, &cache_token
, true);
2847 /* Trigger warnings by calculating non-speculative targets. */
2848 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
2849 possible_polymorphic_call_targets (e
);
2852 dump_possible_polymorphic_call_targets
2857 if (!opt_for_fn (n
->decl
, flag_devirtualize_speculatively
))
2860 if (!e
->maybe_hot_p ())
2863 fprintf (dump_file
, "Call is cold\n\n");
2870 fprintf (dump_file
, "Call is already speculated\n\n");
2873 /* When dumping see if we agree with speculation. */
2877 if (bad_call_targets
.contains (cache_token
))
2880 fprintf (dump_file
, "Target list is known to be useless\n\n");
2884 for (i
= 0; i
< targets
.length (); i
++)
2885 if (likely_target_p (targets
[i
]))
2889 likely_target
= NULL
;
2891 fprintf (dump_file
, "More than one likely target\n\n");
2895 likely_target
= targets
[i
];
2899 bad_call_targets
.add (cache_token
);
2902 /* This is reached only when dumping; check if we agree or disagree
2903 with the speculation. */
2906 struct cgraph_edge
*e2
;
2907 struct ipa_ref
*ref
;
2908 e
->speculative_call_info (e2
, e
, ref
);
2909 if (e2
->callee
->ultimate_alias_target ()
2910 == likely_target
->ultimate_alias_target ())
2912 fprintf (dump_file
, "We agree with speculation\n\n");
2917 fprintf (dump_file
, "We disagree with speculation\n\n");
2922 if (!likely_target
->definition
)
2925 fprintf (dump_file
, "Target is not a definition\n\n");
2929 /* Do not introduce new references to external symbols. While we
2930 can handle these just well, it is common for programs to
2931 incorrectly with headers defining methods they are linked
2933 if (DECL_EXTERNAL (likely_target
->decl
))
2936 fprintf (dump_file
, "Target is external\n\n");
2940 /* Don't use an implicitly-declared destructor (c++/58678). */
2941 struct cgraph_node
*non_thunk_target
2942 = likely_target
->function_symbol ();
2943 if (DECL_ARTIFICIAL (non_thunk_target
->decl
))
2946 fprintf (dump_file
, "Target is artificial\n\n");
2950 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
2951 && likely_target
->can_be_discarded_p ())
2954 fprintf (dump_file
, "Target is overwritable\n\n");
2958 else if (dbg_cnt (devirt
))
2960 if (dump_enabled_p ())
2962 location_t locus
= gimple_location_safe (e
->call_stmt
);
2963 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
2964 "speculatively devirtualizing call in %s/%i to %s/%i\n",
2965 n
->name (), n
->order
,
2966 likely_target
->name (),
2967 likely_target
->order
);
2969 if (!likely_target
->can_be_discarded_p ())
2972 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
2974 likely_target
= alias
;
2979 (likely_target
, e
->count
* 8 / 10, e
->frequency
* 8 / 10);
2983 inline_update_overall_summary (n
);
2985 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
2987 if (warn_suggest_final_types
)
2989 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
2990 for (unsigned int i
= 0;
2991 i
< final_warning_records
->type_warnings
.length (); i
++)
2992 if (final_warning_records
->type_warnings
[i
].count
)
2994 tree type
= final_warning_records
->type_warnings
[i
].type
;
2995 int count
= final_warning_records
->type_warnings
[i
].count
;
2997 = final_warning_records
->type_warnings
[i
].dyn_count
;
3000 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3001 OPT_Wsuggest_final_types
, count
,
3002 "Declaring type %qD final "
3003 "would enable devirtualization of %i call",
3004 "Declaring type %qD final "
3005 "would enable devirtualization of %i calls",
3009 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3010 OPT_Wsuggest_final_types
, count
,
3011 "Declaring type %qD final "
3012 "would enable devirtualization of %i call "
3013 "executed %lli times",
3014 "Declaring type %qD final "
3015 "would enable devirtualization of %i calls "
3016 "executed %lli times",
3023 if (warn_suggest_final_methods
)
3025 vec
<const decl_warn_count
*> decl_warnings_vec
= vNULL
;
3027 final_warning_records
->decl_warnings
.traverse
3028 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3029 decl_warnings_vec
.qsort (decl_warning_cmp
);
3030 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3032 tree decl
= decl_warnings_vec
[i
]->decl
;
3033 int count
= decl_warnings_vec
[i
]->count
;
3034 long long dyn_count
= decl_warnings_vec
[i
]->dyn_count
;
3037 if (DECL_CXX_DESTRUCTOR_P (decl
))
3038 warning_n (DECL_SOURCE_LOCATION (decl
),
3039 OPT_Wsuggest_final_methods
, count
,
3040 "Declaring virtual destructor of %qD final "
3041 "would enable devirtualization of %i call",
3042 "Declaring virtual destructor of %qD final "
3043 "would enable devirtualization of %i calls",
3044 DECL_CONTEXT (decl
), count
);
3046 warning_n (DECL_SOURCE_LOCATION (decl
),
3047 OPT_Wsuggest_final_methods
, count
,
3048 "Declaring method %qD final "
3049 "would enable devirtualization of %i call",
3050 "Declaring method %qD final "
3051 "would enable devirtualization of %i calls",
3053 else if (DECL_CXX_DESTRUCTOR_P (decl
))
3054 warning_n (DECL_SOURCE_LOCATION (decl
),
3055 OPT_Wsuggest_final_methods
, count
,
3056 "Declaring virtual destructor of %qD final "
3057 "would enable devirtualization of %i call "
3058 "executed %lli times",
3059 "Declaring virtual destructor of %qD final "
3060 "would enable devirtualization of %i calls "
3061 "executed %lli times",
3062 DECL_CONTEXT (decl
), count
, dyn_count
);
3064 warning_n (DECL_SOURCE_LOCATION (decl
),
3065 OPT_Wsuggest_final_methods
, count
,
3066 "Declaring method %qD final "
3067 "would enable devirtualization of %i call "
3068 "executed %lli times",
3069 "Declaring method %qD final "
3070 "would enable devirtualization of %i calls "
3071 "executed %lli times",
3072 decl
, count
, dyn_count
);
3076 delete (final_warning_records
);
3077 final_warning_records
= 0;
3082 "%i polymorphic calls, %i devirtualized,"
3083 " %i speculatively devirtualized, %i cold\n"
3084 "%i have multiple targets, %i overwritable,"
3085 " %i already speculated (%i agree, %i disagree),"
3086 " %i external, %i not defined, %i artificial\n",
3087 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3088 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3089 nexternal
, nnotdefined
, nartificial
);
3090 return ndevirtualized
? TODO_remove_functions
: 0;
3095 const pass_data pass_data_ipa_devirt
=
3097 IPA_PASS
, /* type */
3098 "devirt", /* name */
3099 OPTGROUP_NONE
, /* optinfo_flags */
3100 TV_IPA_DEVIRT
, /* tv_id */
3101 0, /* properties_required */
3102 0, /* properties_provided */
3103 0, /* properties_destroyed */
3104 0, /* todo_flags_start */
3105 ( TODO_dump_symtab
), /* todo_flags_finish */
3108 class pass_ipa_devirt
: public ipa_opt_pass_d
3111 pass_ipa_devirt (gcc::context
*ctxt
)
3112 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3113 NULL
, /* generate_summary */
3114 NULL
, /* write_summary */
3115 NULL
, /* read_summary */
3116 NULL
, /* write_optimization_summary */
3117 NULL
, /* read_optimization_summary */
3118 NULL
, /* stmt_fixup */
3119 0, /* function_transform_todo_flags_start */
3120 NULL
, /* function_transform */
3121 NULL
) /* variable_transform */
3124 /* opt_pass methods: */
3125 virtual bool gate (function
*)
3127 /* In LTO, always run the IPA passes and decide on function basis if the
3131 return (flag_devirtualize
3132 && (flag_devirtualize_speculatively
3133 || (warn_suggest_final_methods
3134 || warn_suggest_final_types
))
3138 virtual unsigned int execute (function
*) { return ipa_devirt (); }
3140 }; // class pass_ipa_devirt
3145 make_pass_ipa_devirt (gcc::context
*ctxt
)
3147 return new pass_ipa_devirt (ctxt
);
3150 #include "gt-ipa-devirt.h"