1 /* Basic IPA utilities for type inheritance graph construction and
3 Copyright (C) 2013-2014 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++ frotend. 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 represention 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"
113 #include "print-tree.h"
117 #include "tree-pass.h"
118 #include "hash-set.h"
120 #include "hash-table.h"
122 #include "tree-pretty-print.h"
123 #include "ipa-utils.h"
124 #include "tree-ssa-alias.h"
125 #include "internal-fn.h"
126 #include "gimple-fold.h"
127 #include "gimple-expr.h"
129 #include "ipa-inline.h"
130 #include "diagnostic.h"
131 #include "tree-dfa.h"
132 #include "demangle.h"
134 #include "gimple-pretty-print.h"
135 #include "stor-layout.h"
137 #include "hash-map.h"
139 static bool odr_types_equivalent_p (tree
, tree
, bool, bool *,
142 static bool odr_violation_reported
= false;
144 /* Dummy polymorphic call context. */
146 const ipa_polymorphic_call_context ipa_dummy_polymorphic_call_context
147 = {0, 0, NULL
, NULL
, false, true, true};
149 /* Pointer set of all call targets appearing in the cache. */
150 static hash_set
<cgraph_node
*> *cached_polymorphic_call_targets
;
152 /* The node of type inheritance graph. For each type unique in
153 One Defintion Rule (ODR) sense, we produce one node linking all
154 main variants of types equivalent to it, bases and derived types. */
156 struct GTY(()) odr_type_d
160 /* All bases; built only for main variants of types */
161 vec
<odr_type
> GTY((skip
)) bases
;
162 /* All derrived types with virtual methods seen in unit;
163 built only for main variants oftypes */
164 vec
<odr_type
> GTY((skip
)) derived_types
;
166 /* All equivalent types, if more than one. */
167 vec
<tree
, va_gc
> *types
;
168 /* Set of all equivalent types, if NON-NULL. */
169 hash_set
<tree
> * GTY((skip
)) types_set
;
171 /* Unique ID indexing the type in odr_types array. */
173 /* Is it in anonymous namespace? */
174 bool anonymous_namespace
;
175 /* Do we know about all derivations of given type? */
176 bool all_derivations_known
;
177 /* Did we report ODR violation here? */
181 static bool contains_type_p (tree
, HOST_WIDE_INT
, tree
);
184 /* Return true if BINFO corresponds to a type with virtual methods.
186 Every type has several BINFOs. One is the BINFO associated by the type
187 while other represents bases of derived types. The BINFOs representing
188 bases do not have BINFO_VTABLE pointer set when this is the single
189 inheritance (because vtables are shared). Look up the BINFO of type
190 and check presence of its vtable. */
193 polymorphic_type_binfo_p (tree binfo
)
195 /* See if BINFO's type has an virtual table associtated with it. */
196 return BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (binfo
)));
199 /* Return TRUE if all derived types of T are known and thus
200 we may consider the walk of derived type complete.
202 This is typically true only for final anonymous namespace types and types
203 defined within functions (that may be COMDAT and thus shared across units,
204 but with the same set of derived types). */
207 type_all_derivations_known_p (tree t
)
209 if (TYPE_FINAL_P (t
))
213 if (type_in_anonymous_namespace_p (t
))
215 return (decl_function_context (TYPE_NAME (t
)) != NULL
);
218 /* Return TURE if type's constructors are all visible. */
221 type_all_ctors_visible_p (tree t
)
224 && cgraph_state
>= CGRAPH_STATE_CONSTRUCTION
225 /* We can not always use type_all_derivations_known_p.
226 For function local types we must assume case where
227 the function is COMDAT and shared in between units.
229 TODO: These cases are quite easy to get, but we need
230 to keep track of C++ privatizing via -Wno-weak
231 as well as the IPA privatizing. */
232 && type_in_anonymous_namespace_p (t
);
235 /* Return TRUE if type may have instance. */
238 type_possibly_instantiated_p (tree t
)
243 /* TODO: Add abstract types here. */
244 if (!type_all_ctors_visible_p (t
))
247 vtable
= BINFO_VTABLE (TYPE_BINFO (t
));
248 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
249 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
250 vnode
= varpool_node::get (vtable
);
251 return vnode
&& vnode
->definition
;
254 /* One Definition Rule hashtable helpers. */
258 typedef odr_type_d value_type
;
259 typedef union tree_node compare_type
;
260 static inline hashval_t
hash (const value_type
*);
261 static inline bool equal (const value_type
*, const compare_type
*);
262 static inline void remove (value_type
*);
265 /* Return type that was declared with T's name so that T is an
266 qualified variant of it. */
269 main_odr_variant (const_tree t
)
271 if (TYPE_NAME (t
) && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
)
272 return TREE_TYPE (TYPE_NAME (t
));
273 /* Unnamed types and non-C++ produced types can be compared by variants. */
275 return TYPE_MAIN_VARIANT (t
);
278 /* Produce hash based on type name. */
281 hash_type_name (tree t
)
283 gcc_checking_assert (main_odr_variant (t
) == t
);
285 /* If not in LTO, all main variants are unique, so we can do
288 return htab_hash_pointer (t
);
290 /* Anonymous types are unique. */
291 if (type_in_anonymous_namespace_p (t
))
292 return htab_hash_pointer (t
);
294 /* For polymorphic types, we can simply hash the virtual table. */
295 if (TREE_CODE (t
) == RECORD_TYPE
296 && TYPE_BINFO (t
) && BINFO_VTABLE (TYPE_BINFO (t
)))
298 tree v
= BINFO_VTABLE (TYPE_BINFO (t
));
301 if (TREE_CODE (v
) == POINTER_PLUS_EXPR
)
303 hash
= TREE_INT_CST_LOW (TREE_OPERAND (v
, 1));
304 v
= TREE_OPERAND (TREE_OPERAND (v
, 0), 0);
307 v
= DECL_ASSEMBLER_NAME (v
);
308 hash
= iterative_hash_hashval_t (hash
, htab_hash_pointer (v
));
312 /* Rest is not implemented yet. */
316 /* Return the computed hashcode for ODR_TYPE. */
319 odr_hasher::hash (const value_type
*odr_type
)
321 return hash_type_name (odr_type
->type
);
324 /* For languages with One Definition Rule, work out if
325 types are the same based on their name.
327 This is non-trivial for LTO where minnor differences in
328 the type representation may have prevented type merging
329 to merge two copies of otherwise equivalent type.
331 Until we start streaming mangled type names, this function works
332 only for polymorphic types. */
335 types_same_for_odr (const_tree type1
, const_tree type2
)
337 gcc_checking_assert (TYPE_P (type1
) && TYPE_P (type2
));
339 type1
= main_odr_variant (type1
);
340 type2
= main_odr_variant (type2
);
348 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
349 on the corresponding TYPE_STUB_DECL. */
350 if (type_in_anonymous_namespace_p (type1
)
351 || type_in_anonymous_namespace_p (type2
))
354 /* See if types are obvoiusly different (i.e. different codes
355 or polymorphis wrt non-polymorphic). This is not strictly correct
356 for ODR violating programs, but we can't do better without streaming
358 if (TREE_CODE (type1
) != TREE_CODE (type2
))
360 if (TREE_CODE (type1
) == RECORD_TYPE
361 && (TYPE_BINFO (type1
) == NULL_TREE
) != (TYPE_BINFO (type1
) == NULL_TREE
))
363 if (TREE_CODE (type1
) == RECORD_TYPE
&& TYPE_BINFO (type1
)
364 && (BINFO_VTABLE (TYPE_BINFO (type1
)) == NULL_TREE
)
365 != (BINFO_VTABLE (TYPE_BINFO (type2
)) == NULL_TREE
))
368 /* At the moment we have no way to establish ODR equivlaence at LTO
369 other than comparing virtual table pointrs of polymorphic types.
370 Eventually we should start saving mangled names in TYPE_NAME.
371 Then this condition will become non-trivial. */
373 if (TREE_CODE (type1
) == RECORD_TYPE
374 && TYPE_BINFO (type1
) && TYPE_BINFO (type2
)
375 && BINFO_VTABLE (TYPE_BINFO (type1
))
376 && BINFO_VTABLE (TYPE_BINFO (type2
)))
378 tree v1
= BINFO_VTABLE (TYPE_BINFO (type1
));
379 tree v2
= BINFO_VTABLE (TYPE_BINFO (type2
));
380 gcc_assert (TREE_CODE (v1
) == POINTER_PLUS_EXPR
381 && TREE_CODE (v2
) == POINTER_PLUS_EXPR
);
382 return (operand_equal_p (TREE_OPERAND (v1
, 1),
383 TREE_OPERAND (v2
, 1), 0)
384 && DECL_ASSEMBLER_NAME
385 (TREE_OPERAND (TREE_OPERAND (v1
, 0), 0))
386 == DECL_ASSEMBLER_NAME
387 (TREE_OPERAND (TREE_OPERAND (v2
, 0), 0)));
393 /* Compare types T1 and T2 and return true if they are
397 odr_hasher::equal (const value_type
*t1
, const compare_type
*ct2
)
399 tree t2
= const_cast <tree
> (ct2
);
401 gcc_checking_assert (main_odr_variant (t2
) == t2
);
406 return types_same_for_odr (t1
->type
, t2
);
409 /* Free ODR type V. */
412 odr_hasher::remove (value_type
*v
)
415 v
->derived_types
.release ();
421 /* ODR type hash used to lookup ODR type based on tree type node. */
423 typedef hash_table
<odr_hasher
> odr_hash_type
;
424 static odr_hash_type
*odr_hash
;
426 /* ODR types are also stored into ODR_TYPE vector to allow consistent
427 walking. Bases appear before derived types. Vector is garbage collected
428 so we won't end up visiting empty types. */
430 static GTY(()) vec
<odr_type
, va_gc
> *odr_types_ptr
;
431 #define odr_types (*odr_types_ptr)
433 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
435 set_type_binfo (tree type
, tree binfo
)
437 for (; type
; type
= TYPE_NEXT_VARIANT (type
))
438 if (COMPLETE_TYPE_P (type
))
439 TYPE_BINFO (type
) = binfo
;
441 gcc_assert (!TYPE_BINFO (type
));
444 /* Compare T2 and T2 based on name or structure. */
447 odr_subtypes_equivalent_p (tree t1
, tree t2
, hash_set
<tree
> *visited
)
451 /* This can happen in incomplete types that should be handled earlier. */
452 gcc_assert (t1
&& t2
);
454 t1
= main_odr_variant (t1
);
455 t2
= main_odr_variant (t2
);
458 if (TREE_CODE (t1
) != TREE_CODE (t2
))
460 if ((TYPE_NAME (t1
) == NULL_TREE
) != (TYPE_NAME (t2
) == NULL_TREE
))
462 if (TYPE_NAME (t1
) && DECL_NAME (TYPE_NAME (t1
)) != DECL_NAME (TYPE_NAME (t2
)))
465 /* Anonymous namespace types must match exactly. */
466 an1
= type_in_anonymous_namespace_p (t1
);
467 an2
= type_in_anonymous_namespace_p (t2
);
468 if (an1
!= an2
|| an1
)
471 /* For types where we can not establish ODR equivalency, recurse and deeply
473 if (TREE_CODE (t1
) != RECORD_TYPE
474 || !TYPE_BINFO (t1
) || !TYPE_BINFO (t2
)
475 || !polymorphic_type_binfo_p (TYPE_BINFO (t1
))
476 || !polymorphic_type_binfo_p (TYPE_BINFO (t2
)))
478 /* This should really be a pair hash, but for the moment we do not need
479 100% reliability and it would be better to compare all ODR types so
480 recursion here is needed only for component types. */
481 if (visited
->add (t1
))
483 return odr_types_equivalent_p (t1
, t2
, false, NULL
, visited
);
485 return types_same_for_odr (t1
, t2
);
488 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
489 violation warings. */
492 compare_virtual_tables (varpool_node
*prevailing
, varpool_node
*vtable
)
495 if (DECL_VIRTUAL_P (prevailing
->decl
) != DECL_VIRTUAL_P (vtable
->decl
))
497 odr_violation_reported
= true;
498 if (DECL_VIRTUAL_P (prevailing
->decl
))
500 varpool_node
*tmp
= prevailing
;
504 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))),
506 "virtual table of type %qD violates one definition rule",
507 DECL_CONTEXT (vtable
->decl
)))
508 inform (DECL_SOURCE_LOCATION (prevailing
->decl
),
509 "variable of same assembler name as the virtual table is "
510 "defined in another translation unit");
513 if (!prevailing
->definition
|| !vtable
->definition
)
515 for (n1
= 0, n2
= 0; true; n1
++, n2
++)
517 struct ipa_ref
*ref1
, *ref2
;
519 end1
= !prevailing
->iterate_reference (n1
, ref1
);
520 end2
= !vtable
->iterate_reference (n2
, ref2
);
524 && DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
525 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
527 && !DECL_VIRTUAL_P (ref2
->referred
->decl
)
528 && DECL_VIRTUAL_P (ref1
->referred
->decl
))
530 if (warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), 0,
531 "virtual table of type %qD contains RTTI information",
532 DECL_CONTEXT (vtable
->decl
)))
534 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
535 "but is prevailed by one without from other translation unit");
536 inform (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
537 "RTTI will not work on this type");
540 end2
= !vtable
->iterate_reference (n2
, ref2
);
543 && DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
544 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
)
546 && !DECL_VIRTUAL_P (ref1
->referred
->decl
)
547 && DECL_VIRTUAL_P (ref2
->referred
->decl
))
550 end1
= !vtable
->iterate_reference (n1
, ref1
);
556 varpool_node
*tmp
= prevailing
;
561 if (warning_at (DECL_SOURCE_LOCATION
562 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), 0,
563 "virtual table of type %qD violates "
564 "one definition rule",
565 DECL_CONTEXT (vtable
->decl
)))
567 inform (DECL_SOURCE_LOCATION
568 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
569 "the conflicting type defined in another translation "
571 inform (DECL_SOURCE_LOCATION
572 (TYPE_NAME (DECL_CONTEXT (ref1
->referring
->decl
))),
573 "contains additional virtual method %qD",
574 ref1
->referred
->decl
);
578 if (DECL_ASSEMBLER_NAME (ref1
->referred
->decl
)
579 != DECL_ASSEMBLER_NAME (ref2
->referred
->decl
))
581 if (warning_at (DECL_SOURCE_LOCATION
582 (TYPE_NAME (DECL_CONTEXT (vtable
->decl
))), 0,
583 "virtual table of type %qD violates "
584 "one definition rule ",
585 DECL_CONTEXT (vtable
->decl
)))
587 inform (DECL_SOURCE_LOCATION
588 (TYPE_NAME (DECL_CONTEXT (prevailing
->decl
))),
589 "the conflicting type defined in another translation "
591 inform (DECL_SOURCE_LOCATION (ref1
->referred
->decl
),
592 "virtual method %qD", ref1
->referred
->decl
);
593 inform (DECL_SOURCE_LOCATION (ref2
->referred
->decl
),
594 "ought to match virtual method %qD but does not",
595 ref2
->referred
->decl
);
602 /* Output ODR violation warning about T1 and T2 with REASON.
603 Display location of ST1 and ST2 if REASON speaks about field or
605 If WARN is false, do nothing. Set WARNED if warning was indeed
609 warn_odr (tree t1
, tree t2
, tree st1
, tree st2
,
610 bool warn
, bool *warned
, const char *reason
)
612 tree decl2
= TYPE_NAME (t2
);
616 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)), OPT_Wodr
,
617 "type %qT violates one definition rule",
622 else if (TREE_CODE (st1
) == FIELD_DECL
)
624 inform (DECL_SOURCE_LOCATION (decl2
),
625 "a different type is defined in another translation unit");
626 inform (DECL_SOURCE_LOCATION (st1
),
627 "the first difference of corresponding definitions is field %qD",
631 else if (TREE_CODE (st1
) == FUNCTION_DECL
)
633 inform (DECL_SOURCE_LOCATION (decl2
),
634 "a different type is defined in another translation unit");
635 inform (DECL_SOURCE_LOCATION (st1
),
636 "the first difference of corresponding definitions is method %qD",
642 inform (DECL_SOURCE_LOCATION (decl2
), reason
);
648 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
649 because they are used on same place in ODR matching types.
650 They are not; inform the user. */
653 warn_types_mismatch (tree t1
, tree t2
)
655 if (!TYPE_NAME (t1
) || !TYPE_NAME (t2
))
657 /* In Firefox it is a common bug to have same types but in
658 different namespaces. Be a bit more informative on
660 if (TYPE_CONTEXT (t1
) && TYPE_CONTEXT (t2
)
661 && (((TREE_CODE (TYPE_CONTEXT (t1
)) == NAMESPACE_DECL
)
662 != (TREE_CODE (TYPE_CONTEXT (t2
)) == NAMESPACE_DECL
))
663 || (TREE_CODE (TYPE_CONTEXT (t1
)) == NAMESPACE_DECL
664 && (DECL_NAME (TYPE_CONTEXT (t1
)) !=
665 DECL_NAME (TYPE_CONTEXT (t2
))))))
666 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
667 "type %qT should match type %qT but is defined "
668 "in different namespace ",
671 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1
)),
672 "type %qT should match type %qT",
674 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2
)),
675 "the incompatible type is defined here");
678 /* Compare T1 and T2, report ODR violations if WARN is true and set
679 WARNED to true if anything is reported. Return true if types match.
680 If true is returned, the types are also compatible in the sense of
681 gimple_canonical_types_compatible_p. */
684 odr_types_equivalent_p (tree t1
, tree t2
, bool warn
, bool *warned
, hash_set
<tree
> *visited
)
686 /* Check first for the obvious case of pointer identity. */
689 gcc_assert (!type_in_anonymous_namespace_p (t1
));
690 gcc_assert (!type_in_anonymous_namespace_p (t2
));
692 /* Can't be the same type if the types don't have the same code. */
693 if (TREE_CODE (t1
) != TREE_CODE (t2
))
695 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
696 G_("a different type is defined in another translation unit"));
700 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
702 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
703 G_("a type with different qualifiers is defined in another "
704 "translation unit"));
708 if (comp_type_attributes (t1
, t2
) != 1)
710 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
711 G_("a type with attributes "
712 "is defined in another translation unit"));
716 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
719 for (v1
= TYPE_VALUES (t1
), v2
= TYPE_VALUES (t2
);
720 v1
&& v2
; v1
= TREE_CHAIN (v1
), v2
= TREE_CHAIN (v2
))
722 if (TREE_PURPOSE (v1
) != TREE_PURPOSE (v2
))
724 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
725 G_("an enum with different value name"
726 " is defined in another translation unit"));
729 if (TREE_VALUE (v1
) != TREE_VALUE (v2
)
730 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1
)),
731 DECL_INITIAL (TREE_VALUE (v2
)), 0))
733 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
734 G_("an enum with different values is defined"
735 " in another translation unit"));
741 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
742 G_("an enum with mismatching number of values "
743 "is defined in another translation unit"));
748 /* Non-aggregate types can be handled cheaply. */
749 if (INTEGRAL_TYPE_P (t1
)
750 || SCALAR_FLOAT_TYPE_P (t1
)
751 || FIXED_POINT_TYPE_P (t1
)
752 || TREE_CODE (t1
) == VECTOR_TYPE
753 || TREE_CODE (t1
) == COMPLEX_TYPE
754 || TREE_CODE (t1
) == OFFSET_TYPE
755 || POINTER_TYPE_P (t1
))
757 if (TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
))
759 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
760 G_("a type with different precision is defined "
761 "in another translation unit"));
764 if (TYPE_UNSIGNED (t1
) != TYPE_UNSIGNED (t2
))
766 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
767 G_("a type with different signedness is defined "
768 "in another translation unit"));
772 if (TREE_CODE (t1
) == INTEGER_TYPE
773 && TYPE_STRING_FLAG (t1
) != TYPE_STRING_FLAG (t2
))
775 /* char WRT uint_8? */
776 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
777 G_("a different type is defined in another "
778 "translation unit"));
782 /* For canonical type comparisons we do not want to build SCCs
783 so we cannot compare pointed-to types. But we can, for now,
784 require the same pointed-to type kind and match what
785 useless_type_conversion_p would do. */
786 if (POINTER_TYPE_P (t1
))
788 if (TYPE_ADDR_SPACE (TREE_TYPE (t1
))
789 != TYPE_ADDR_SPACE (TREE_TYPE (t2
)))
791 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
792 G_("it is defined as a pointer in different address "
793 "space in another translation unit"));
797 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
799 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
800 G_("it is defined as a pointer to different type "
801 "in another translation unit"));
803 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
808 /* Tail-recurse to components. */
809 if ((TREE_CODE (t1
) == VECTOR_TYPE
|| TREE_CODE (t1
) == COMPLEX_TYPE
)
810 && !odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
812 /* Probably specific enough. */
813 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
814 G_("a different type is defined "
815 "in another translation unit"));
817 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
821 gcc_assert (operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0));
822 gcc_assert (operand_equal_p (TYPE_SIZE_UNIT (t1
),
823 TYPE_SIZE_UNIT (t2
), 0));
824 gcc_assert (TYPE_MODE (t1
) == TYPE_MODE (t2
));
829 /* Do type-specific comparisons. */
830 switch (TREE_CODE (t1
))
834 /* Array types are the same if the element types are the same and
835 the number of elements are the same. */
836 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
838 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
839 G_("a different type is defined in another "
840 "translation unit"));
842 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
844 gcc_assert (TYPE_STRING_FLAG (t1
) == TYPE_STRING_FLAG (t2
));
845 gcc_assert (TYPE_NONALIASED_COMPONENT (t1
)
846 == TYPE_NONALIASED_COMPONENT (t2
));
848 tree i1
= TYPE_DOMAIN (t1
);
849 tree i2
= TYPE_DOMAIN (t2
);
851 /* For an incomplete external array, the type domain can be
852 NULL_TREE. Check this condition also. */
853 if (i1
== NULL_TREE
|| i2
== NULL_TREE
)
856 tree min1
= TYPE_MIN_VALUE (i1
);
857 tree min2
= TYPE_MIN_VALUE (i2
);
858 tree max1
= TYPE_MAX_VALUE (i1
);
859 tree max2
= TYPE_MAX_VALUE (i2
);
861 /* In C++, minimums should be always 0. */
862 gcc_assert (min1
== min2
);
863 if (!operand_equal_p (max1
, max2
, 0))
865 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
866 G_("an array of different size is defined "
867 "in another translation unit"));
870 gcc_assert (operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0));
871 gcc_assert (operand_equal_p (TYPE_SIZE_UNIT (t1
),
872 TYPE_SIZE_UNIT (t2
), 0));
878 /* Function types are the same if the return type and arguments types
880 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1
), TREE_TYPE (t2
), visited
))
882 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
883 G_("has different return value "
884 "in another translation unit"));
886 warn_types_mismatch (TREE_TYPE (t1
), TREE_TYPE (t2
));
890 if (TYPE_ARG_TYPES (t1
) == TYPE_ARG_TYPES (t2
))
896 for (parms1
= TYPE_ARG_TYPES (t1
), parms2
= TYPE_ARG_TYPES (t2
);
898 parms1
= TREE_CHAIN (parms1
), parms2
= TREE_CHAIN (parms2
))
900 if (!odr_subtypes_equivalent_p
901 (TREE_VALUE (parms1
), TREE_VALUE (parms2
), visited
))
903 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
904 G_("has different parameters in another "
905 "translation unit"));
907 warn_types_mismatch (TREE_VALUE (parms1
),
908 TREE_VALUE (parms2
));
913 if (parms1
|| parms2
)
915 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
916 G_("has different parameters "
917 "in another translation unit"));
926 case QUAL_UNION_TYPE
:
930 /* For aggregate types, all the fields must be the same. */
931 if (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
))
933 for (f1
= TYPE_FIELDS (t1
), f2
= TYPE_FIELDS (t2
);
935 f1
= TREE_CHAIN (f1
), f2
= TREE_CHAIN (f2
))
937 /* Skip non-fields. */
938 while (f1
&& TREE_CODE (f1
) != FIELD_DECL
)
939 f1
= TREE_CHAIN (f1
);
940 while (f2
&& TREE_CODE (f2
) != FIELD_DECL
)
941 f2
= TREE_CHAIN (f2
);
944 if (DECL_ARTIFICIAL (f1
) != DECL_ARTIFICIAL (f2
))
946 if (DECL_NAME (f1
) != DECL_NAME (f2
)
947 && !DECL_ARTIFICIAL (f1
))
949 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
950 G_("a field with different name is defined "
951 "in another translation unit"));
954 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1
), TREE_TYPE (f2
), visited
))
956 /* Do not warn about artificial fields and just go into generic
957 field mismatch warning. */
958 if (DECL_ARTIFICIAL (f1
))
961 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
962 G_("a field of same name but different type "
963 "is defined in another translation unit"));
965 warn_types_mismatch (TREE_TYPE (f1
), TREE_TYPE (f2
));
968 if (!gimple_compare_field_offset (f1
, f2
))
970 /* Do not warn about artificial fields and just go into generic
971 field mismatch warning. */
972 if (DECL_ARTIFICIAL (f1
))
974 warn_odr (t1
, t2
, t1
, t2
, warn
, warned
,
975 G_("fields has different layout "
976 "in another translation unit"));
979 gcc_assert (DECL_NONADDRESSABLE_P (f1
)
980 == DECL_NONADDRESSABLE_P (f2
));
983 /* If one aggregate has more fields than the other, they
987 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
988 G_("a type with different number of fields "
989 "is defined in another translation unit"));
992 if ((TYPE_MAIN_VARIANT (t1
) == t1
|| TYPE_MAIN_VARIANT (t2
) == t2
)
993 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1
))
994 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2
))))
996 for (f1
= TYPE_METHODS (TYPE_MAIN_VARIANT (t1
)),
997 f2
= TYPE_METHODS (TYPE_MAIN_VARIANT (t2
));
998 f1
&& f2
; f1
= DECL_CHAIN (f1
), f2
= DECL_CHAIN (f2
))
1000 if (DECL_ASSEMBLER_NAME (f1
) != DECL_ASSEMBLER_NAME (f2
))
1002 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1003 G_("a different method of same type "
1004 "is defined in another translation unit"));
1007 if (DECL_VIRTUAL_P (f1
) != DECL_VIRTUAL_P (f2
))
1009 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1010 G_("s definition that differs by virtual "
1011 "keyword in another translation unit"));
1014 if (DECL_VINDEX (f1
) != DECL_VINDEX (f2
))
1016 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1017 G_("virtual table layout differs in another "
1018 "translation unit"));
1021 if (odr_subtypes_equivalent_p (TREE_TYPE (f1
), TREE_TYPE (f2
), visited
))
1023 warn_odr (t1
, t2
, f1
, f2
, warn
, warned
,
1024 G_("method with incompatible type is defined "
1025 "in another translation unit"));
1031 warn_odr (t1
, t2
, NULL
, NULL
, warn
, warned
,
1032 G_("a type with different number of methods "
1033 "is defined in another translation unit"));
1037 gcc_assert (operand_equal_p (TYPE_SIZE (t1
), TYPE_SIZE (t2
), 0));
1038 gcc_assert (operand_equal_p (TYPE_SIZE_UNIT (t1
),
1039 TYPE_SIZE_UNIT (t2
), 0));
1050 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1051 from VAL->type. This may happen in LTO where tree merging did not merge
1052 all variants of the same type. It may or may not mean the ODR violation.
1053 Add it to the list of duplicates and warn on some violations. */
1056 add_type_duplicate (odr_type val
, tree type
)
1058 bool build_bases
= false;
1059 if (!val
->types_set
)
1060 val
->types_set
= new hash_set
<tree
>;
1062 /* Always prefer complete type to be the leader. */
1063 if (!COMPLETE_TYPE_P (val
->type
)
1064 && COMPLETE_TYPE_P (type
))
1073 /* See if this duplicate is new. */
1074 if (!val
->types_set
->add (type
))
1077 bool base_mismatch
= false;
1079 bool warned
= false;
1080 hash_set
<tree
> visited
;
1082 gcc_assert (in_lto_p
);
1083 vec_safe_push (val
->types
, type
);
1085 /* First we compare memory layout. */
1086 if (!odr_types_equivalent_p (val
->type
, type
, !flag_ltrans
&& !val
->odr_violated
,
1090 odr_violation_reported
= true;
1091 val
->odr_violated
= true;
1092 if (cgraph_dump_file
)
1094 fprintf (cgraph_dump_file
, "ODR violation\n");
1096 print_node (cgraph_dump_file
, "", val
->type
, 0);
1097 putc ('\n',cgraph_dump_file
);
1098 print_node (cgraph_dump_file
, "", type
, 0);
1099 putc ('\n',cgraph_dump_file
);
1103 /* Next sanity check that bases are the same. If not, we will end
1104 up producing wrong answers. */
1105 if (COMPLETE_TYPE_P (type
) && COMPLETE_TYPE_P (val
->type
)
1106 && TREE_CODE (val
->type
) == RECORD_TYPE
1107 && TREE_CODE (type
) == RECORD_TYPE
1108 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
))
1110 for (j
= 0, i
= 0; i
< BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)); i
++)
1111 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (TYPE_BINFO (type
), i
)))
1113 odr_type base
= get_odr_type
1115 (BINFO_BASE_BINFO (TYPE_BINFO (type
),
1118 if (val
->bases
.length () <= j
|| val
->bases
[j
] != base
)
1119 base_mismatch
= true;
1125 odr_violation_reported
= true;
1127 if (!warned
&& !val
->odr_violated
)
1128 warn_odr (type
, val
->type
, NULL
, NULL
, !warned
, &warned
,
1129 "a type with the same name but different bases is "
1130 "defined in another translation unit");
1131 val
->odr_violated
= true;
1132 if (cgraph_dump_file
)
1134 fprintf (cgraph_dump_file
, "ODR bse violation or merging bug?\n");
1136 print_node (cgraph_dump_file
, "", val
->type
, 0);
1137 putc ('\n',cgraph_dump_file
);
1138 print_node (cgraph_dump_file
, "", type
, 0);
1139 putc ('\n',cgraph_dump_file
);
1144 /* Regularize things a little. During LTO same types may come with
1145 different BINFOs. Either because their virtual table was
1146 not merged by tree merging and only later at decl merging or
1147 because one type comes with external vtable, while other
1148 with internal. We want to merge equivalent binfos to conserve
1149 memory and streaming overhead.
1151 The external vtables are more harmful: they contain references
1152 to external declarations of methods that may be defined in the
1153 merged LTO unit. For this reason we absolutely need to remove
1154 them and replace by internal variants. Not doing so will lead
1155 to incomplete answers from possible_polymorphic_call_targets. */
1156 if (!flag_ltrans
&& merge
1157 && TREE_CODE (val
->type
) == RECORD_TYPE
1158 && TREE_CODE (type
) == RECORD_TYPE
1159 && TYPE_BINFO (val
->type
) && TYPE_BINFO (type
)
1160 && TYPE_MAIN_VARIANT (type
) == type
1161 && TYPE_MAIN_VARIANT (val
->type
) == val
->type
1162 && BINFO_VTABLE (TYPE_BINFO (val
->type
))
1163 && BINFO_VTABLE (TYPE_BINFO (type
)))
1165 tree master_binfo
= TYPE_BINFO (val
->type
);
1166 tree v1
= BINFO_VTABLE (master_binfo
);
1167 tree v2
= BINFO_VTABLE (TYPE_BINFO (type
));
1169 if (TREE_CODE (v1
) == POINTER_PLUS_EXPR
)
1171 gcc_assert (TREE_CODE (v2
) == POINTER_PLUS_EXPR
1172 && operand_equal_p (TREE_OPERAND (v1
, 1),
1173 TREE_OPERAND (v2
, 1), 0));
1174 v1
= TREE_OPERAND (TREE_OPERAND (v1
, 0), 0);
1175 v2
= TREE_OPERAND (TREE_OPERAND (v2
, 0), 0);
1177 gcc_assert (DECL_ASSEMBLER_NAME (v1
)
1178 == DECL_ASSEMBLER_NAME (v2
));
1180 if (DECL_EXTERNAL (v1
) && !DECL_EXTERNAL (v2
))
1184 set_type_binfo (val
->type
, TYPE_BINFO (type
));
1185 for (i
= 0; i
< val
->types
->length (); i
++)
1187 if (TYPE_BINFO ((*val
->types
)[i
])
1189 set_type_binfo ((*val
->types
)[i
], TYPE_BINFO (type
));
1191 BINFO_TYPE (TYPE_BINFO (type
)) = val
->type
;
1194 set_type_binfo (type
, master_binfo
);
1200 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1201 possibly new entry. */
1204 get_odr_type (tree type
, bool insert
)
1209 bool build_bases
= false;
1210 bool insert_to_odr_array
= false;
1213 type
= main_odr_variant (type
);
1215 hash
= hash_type_name (type
);
1217 = odr_hash
->find_slot_with_hash (type
, hash
, insert
? INSERT
: NO_INSERT
);
1221 /* See if we already have entry for type. */
1226 /* With LTO we need to support multiple tree representation of
1227 the same ODR type. */
1228 if (val
->type
!= type
)
1229 build_bases
= add_type_duplicate (val
, type
);
1233 val
= ggc_cleared_alloc
<odr_type_d
> ();
1236 val
->derived_types
= vNULL
;
1237 val
->anonymous_namespace
= type_in_anonymous_namespace_p (type
);
1238 build_bases
= COMPLETE_TYPE_P (val
->type
);
1239 insert_to_odr_array
= true;
1242 if (build_bases
&& TREE_CODE (type
) == RECORD_TYPE
&& TYPE_BINFO (type
)
1243 && type
== TYPE_MAIN_VARIANT (type
))
1245 tree binfo
= TYPE_BINFO (type
);
1248 gcc_assert (BINFO_TYPE (TYPE_BINFO (val
->type
)) = type
);
1250 val
->all_derivations_known
= type_all_derivations_known_p (type
);
1252 for (i
= 0; i
< BINFO_N_BASE_BINFOS (binfo
); i
++)
1253 /* For now record only polymorphic types. other are
1254 pointless for devirtualization and we can not precisely
1255 determine ODR equivalency of these during LTO. */
1256 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo
, i
)))
1258 odr_type base
= get_odr_type (BINFO_TYPE (BINFO_BASE_BINFO (binfo
,
1261 gcc_assert (TYPE_MAIN_VARIANT (base
->type
) == base
->type
);
1262 base
->derived_types
.safe_push (val
);
1263 val
->bases
.safe_push (base
);
1264 if (base
->id
> base_id
)
1268 /* Ensure that type always appears after bases. */
1269 if (insert_to_odr_array
)
1272 val
->id
= odr_types
.length ();
1273 vec_safe_push (odr_types_ptr
, val
);
1275 else if (base_id
> val
->id
)
1277 odr_types
[val
->id
] = 0;
1278 /* Be sure we did not recorded any derived types; these may need
1280 gcc_assert (val
->derived_types
.length() == 0);
1282 val
->id
= odr_types
.length ();
1283 vec_safe_push (odr_types_ptr
, val
);
1288 /* Dump ODR type T and all its derrived type. INDENT specify indentation for
1289 recusive printing. */
1292 dump_odr_type (FILE *f
, odr_type t
, int indent
=0)
1295 fprintf (f
, "%*s type %i: ", indent
* 2, "", t
->id
);
1296 print_generic_expr (f
, t
->type
, TDF_SLIM
);
1297 fprintf (f
, "%s", t
->anonymous_namespace
? " (anonymous namespace)":"");
1298 fprintf (f
, "%s\n", t
->all_derivations_known
? " (derivations known)":"");
1299 if (TYPE_NAME (t
->type
))
1301 fprintf (f
, "%*s defined at: %s:%i\n", indent
* 2, "",
1302 DECL_SOURCE_FILE (TYPE_NAME (t
->type
)),
1303 DECL_SOURCE_LINE (TYPE_NAME (t
->type
)));
1305 if (t
->bases
.length ())
1307 fprintf (f
, "%*s base odr type ids: ", indent
* 2, "");
1308 for (i
= 0; i
< t
->bases
.length (); i
++)
1309 fprintf (f
, " %i", t
->bases
[i
]->id
);
1312 if (t
->derived_types
.length ())
1314 fprintf (f
, "%*s derived types:\n", indent
* 2, "");
1315 for (i
= 0; i
< t
->derived_types
.length (); i
++)
1316 dump_odr_type (f
, t
->derived_types
[i
], indent
+ 1);
1321 /* Dump the type inheritance graph. */
1324 dump_type_inheritance_graph (FILE *f
)
1329 fprintf (f
, "\n\nType inheritance graph:\n");
1330 for (i
= 0; i
< odr_types
.length (); i
++)
1332 if (odr_types
[i
] && odr_types
[i
]->bases
.length () == 0)
1333 dump_odr_type (f
, odr_types
[i
]);
1335 for (i
= 0; i
< odr_types
.length (); i
++)
1337 if (odr_types
[i
] && odr_types
[i
]->types
&& odr_types
[i
]->types
->length ())
1340 fprintf (f
, "Duplicate tree types for odr type %i\n", i
);
1341 print_node (f
, "", odr_types
[i
]->type
, 0);
1342 for (j
= 0; j
< odr_types
[i
]->types
->length (); j
++)
1345 fprintf (f
, "duplicate #%i\n", j
);
1346 print_node (f
, "", (*odr_types
[i
]->types
)[j
], 0);
1347 t
= (*odr_types
[i
]->types
)[j
];
1348 while (TYPE_P (t
) && TYPE_CONTEXT (t
))
1350 t
= TYPE_CONTEXT (t
);
1351 print_node (f
, "", t
, 0);
1359 /* Given method type T, return type of class it belongs to.
1360 Lookup this pointer and get its type. */
1363 method_class_type (const_tree t
)
1365 tree first_parm_type
= TREE_VALUE (TYPE_ARG_TYPES (t
));
1366 gcc_assert (TREE_CODE (t
) == METHOD_TYPE
);
1368 return TREE_TYPE (first_parm_type
);
1371 /* Initialize IPA devirt and build inheritance tree graph. */
1374 build_type_inheritance_graph (void)
1376 struct symtab_node
*n
;
1377 FILE *inheritance_dump_file
;
1382 timevar_push (TV_IPA_INHERITANCE
);
1383 inheritance_dump_file
= dump_begin (TDI_inheritance
, &flags
);
1384 odr_hash
= new odr_hash_type (23);
1386 /* We reconstruct the graph starting of types of all methods seen in the
1389 if (is_a
<cgraph_node
*> (n
)
1390 && DECL_VIRTUAL_P (n
->decl
)
1391 && n
->real_symbol_p ())
1392 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n
->decl
))),
1395 /* Look also for virtual tables of types that do not define any methods.
1397 We need it in a case where class B has virtual base of class A
1398 re-defining its virtual method and there is class C with no virtual
1399 methods with B as virtual base.
1401 Here we output B's virtual method in two variant - for non-virtual
1402 and virtual inheritance. B's virtual table has non-virtual version,
1403 while C's has virtual.
1405 For this reason we need to know about C in order to include both
1406 variants of B. More correctly, record_target_from_binfo should
1407 add both variants of the method when walking B, but we have no
1408 link in between them.
1410 We rely on fact that either the method is exported and thus we
1411 assume it is called externally or C is in anonymous namespace and
1412 thus we will see the vtable. */
1414 else if (is_a
<varpool_node
*> (n
)
1415 && DECL_VIRTUAL_P (n
->decl
)
1416 && TREE_CODE (DECL_CONTEXT (n
->decl
)) == RECORD_TYPE
1417 && TYPE_BINFO (DECL_CONTEXT (n
->decl
))
1418 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n
->decl
))))
1419 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n
->decl
)), true);
1420 if (inheritance_dump_file
)
1422 dump_type_inheritance_graph (inheritance_dump_file
);
1423 dump_end (TDI_inheritance
, inheritance_dump_file
);
1425 timevar_pop (TV_IPA_INHERITANCE
);
1428 /* Return true if N has reference from live virtual table
1429 (and thus can be a destination of polymorphic call).
1430 Be conservatively correct when callgraph is not built or
1431 if the method may be referred externally. */
1434 referenced_from_vtable_p (struct cgraph_node
*node
)
1437 struct ipa_ref
*ref
;
1440 if (node
->externally_visible
1441 || DECL_EXTERNAL (node
->decl
)
1442 || node
->used_from_other_partition
)
1445 /* Keep this test constant time.
1446 It is unlikely this can happen except for the case where speculative
1447 devirtualization introduced many speculative edges to this node.
1448 In this case the target is very likely alive anyway. */
1449 if (node
->ref_list
.referring
.length () > 100)
1452 /* We need references built. */
1453 if (cgraph_state
<= CGRAPH_STATE_CONSTRUCTION
)
1456 for (i
= 0; node
->iterate_referring (i
, ref
); i
++)
1458 if ((ref
->use
== IPA_REF_ALIAS
1459 && referenced_from_vtable_p (dyn_cast
<cgraph_node
*> (ref
->referring
)))
1460 || (ref
->use
== IPA_REF_ADDR
1461 && TREE_CODE (ref
->referring
->decl
) == VAR_DECL
1462 && DECL_VIRTUAL_P (ref
->referring
->decl
)))
1470 /* If TARGET has associated node, record it in the NODES array.
1471 CAN_REFER specify if program can refer to the target directly.
1472 if TARGET is unknown (NULL) or it can not be inserted (for example because
1473 its body was already removed and there is no way to refer to it), clear
1477 maybe_record_node (vec
<cgraph_node
*> &nodes
,
1478 tree target
, hash_set
<tree
> *inserted
,
1482 struct cgraph_node
*target_node
, *alias_target
;
1483 enum availability avail
;
1485 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
1486 list of targets; the runtime effect of calling them is undefined.
1487 Only "real" virtual methods should be accounted. */
1488 if (target
&& TREE_CODE (TREE_TYPE (target
)) != METHOD_TYPE
)
1493 /* The only case when method of anonymous namespace becomes unreferable
1494 is when we completely optimized it out. */
1497 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target
)))
1505 target_node
= cgraph_node::get (target
);
1507 /* Preffer alias target over aliases, so we do not get confused by
1511 alias_target
= target_node
->ultimate_alias_target (&avail
);
1512 if (target_node
!= alias_target
1513 && avail
>= AVAIL_AVAILABLE
1514 && target_node
->get_availability ())
1515 target_node
= alias_target
;
1518 /* Method can only be called by polymorphic call if any
1519 of vtables refering to it are alive.
1521 While this holds for non-anonymous functions, too, there are
1522 cases where we want to keep them in the list; for example
1523 inline functions with -fno-weak are static, but we still
1524 may devirtualize them when instance comes from other unit.
1525 The same holds for LTO.
1527 Currently we ignore these functions in speculative devirtualization.
1528 ??? Maybe it would make sense to be more aggressive for LTO even
1531 && type_in_anonymous_namespace_p (DECL_CONTEXT (target
))
1533 || !referenced_from_vtable_p (target_node
)))
1535 /* See if TARGET is useful function we can deal with. */
1536 else if (target_node
!= NULL
1537 && (TREE_PUBLIC (target
)
1538 || DECL_EXTERNAL (target
)
1539 || target_node
->definition
)
1540 && target_node
->real_symbol_p ())
1542 gcc_assert (!target_node
->global
.inlined_to
);
1543 gcc_assert (target_node
->real_symbol_p ());
1544 if (!inserted
->add (target
))
1546 cached_polymorphic_call_targets
->add (target_node
);
1547 nodes
.safe_push (target_node
);
1551 && (!type_in_anonymous_namespace_p
1552 (DECL_CONTEXT (target
))
1557 /* See if BINFO's type match OUTER_TYPE. If so, lookup
1558 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
1559 method in vtable and insert method to NODES array
1560 or BASES_TO_CONSIDER if this array is non-NULL.
1561 Otherwise recurse to base BINFOs.
1562 This match what get_binfo_at_offset does, but with offset
1565 TYPE_BINFOS is a stack of BINFOS of types with defined
1566 virtual table seen on way from class type to BINFO.
1568 MATCHED_VTABLES tracks virtual tables we already did lookup
1569 for virtual function in. INSERTED tracks nodes we already
1572 ANONYMOUS is true if BINFO is part of anonymous namespace.
1574 Clear COMPLETEP when we hit unreferable target.
1578 record_target_from_binfo (vec
<cgraph_node
*> &nodes
,
1579 vec
<tree
> *bases_to_consider
,
1582 vec
<tree
> &type_binfos
,
1583 HOST_WIDE_INT otr_token
,
1585 HOST_WIDE_INT offset
,
1586 hash_set
<tree
> *inserted
,
1587 hash_set
<tree
> *matched_vtables
,
1591 tree type
= BINFO_TYPE (binfo
);
1596 if (BINFO_VTABLE (binfo
))
1597 type_binfos
.safe_push (binfo
);
1598 if (types_same_for_odr (type
, outer_type
))
1601 tree type_binfo
= NULL
;
1603 /* Lookup BINFO with virtual table. For normal types it is always last
1605 for (i
= type_binfos
.length () - 1; i
>= 0; i
--)
1606 if (BINFO_OFFSET (type_binfos
[i
]) == BINFO_OFFSET (binfo
))
1608 type_binfo
= type_binfos
[i
];
1611 if (BINFO_VTABLE (binfo
))
1613 /* If this is duplicated BINFO for base shared by virtual inheritance,
1614 we may not have its associated vtable. This is not a problem, since
1615 we will walk it on the other path. */
1618 tree inner_binfo
= get_binfo_at_offset (type_binfo
,
1622 gcc_assert (odr_violation_reported
);
1625 /* For types in anonymous namespace first check if the respective vtable
1626 is alive. If not, we know the type can't be called. */
1627 if (!flag_ltrans
&& anonymous
)
1629 tree vtable
= BINFO_VTABLE (inner_binfo
);
1630 varpool_node
*vnode
;
1632 if (TREE_CODE (vtable
) == POINTER_PLUS_EXPR
)
1633 vtable
= TREE_OPERAND (TREE_OPERAND (vtable
, 0), 0);
1634 vnode
= varpool_node::get (vtable
);
1635 if (!vnode
|| !vnode
->definition
)
1638 gcc_assert (inner_binfo
);
1639 if (bases_to_consider
1640 ? !matched_vtables
->contains (BINFO_VTABLE (inner_binfo
))
1641 : !matched_vtables
->add (BINFO_VTABLE (inner_binfo
)))
1644 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
1647 if (!bases_to_consider
)
1648 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
1649 /* Destructors are never called via construction vtables. */
1650 else if (!target
|| !DECL_CXX_DESTRUCTOR_P (target
))
1651 bases_to_consider
->safe_push (target
);
1657 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1658 /* Walking bases that have no virtual method is pointless excercise. */
1659 if (polymorphic_type_binfo_p (base_binfo
))
1660 record_target_from_binfo (nodes
, bases_to_consider
, base_binfo
, otr_type
,
1662 otr_token
, outer_type
, offset
, inserted
,
1663 matched_vtables
, anonymous
, completep
);
1664 if (BINFO_VTABLE (binfo
))
1668 /* Lookup virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
1669 of TYPE, insert them to NODES, recurse into derived nodes.
1670 INSERTED is used to avoid duplicate insertions of methods into NODES.
1671 MATCHED_VTABLES are used to avoid duplicate walking vtables.
1672 Clear COMPLETEP if unreferable target is found.
1674 If CONSIDER_CONSTURCTION is true, record to BASES_TO_CONSDIER
1675 all cases where BASE_SKIPPED is true (because the base is abstract
1679 possible_polymorphic_call_targets_1 (vec
<cgraph_node
*> &nodes
,
1680 hash_set
<tree
> *inserted
,
1681 hash_set
<tree
> *matched_vtables
,
1684 HOST_WIDE_INT otr_token
,
1686 HOST_WIDE_INT offset
,
1688 vec
<tree
> &bases_to_consider
,
1689 bool consider_construction
)
1691 tree binfo
= TYPE_BINFO (type
->type
);
1693 vec
<tree
> type_binfos
= vNULL
;
1694 bool possibly_instantiated
= type_possibly_instantiated_p (type
->type
);
1696 /* We may need to consider types w/o instances because of possible derived
1697 types using their methods either directly or via construction vtables.
1698 We are safe to skip them when all derivations are known, since we will
1700 This is done by recording them to BASES_TO_CONSIDER array. */
1701 if (possibly_instantiated
|| consider_construction
)
1703 record_target_from_binfo (nodes
,
1704 (!possibly_instantiated
1705 && type_all_derivations_known_p (type
->type
))
1706 ? &bases_to_consider
: NULL
,
1707 binfo
, otr_type
, type_binfos
, otr_token
,
1709 inserted
, matched_vtables
,
1710 type
->anonymous_namespace
, completep
);
1712 type_binfos
.release ();
1713 for (i
= 0; i
< type
->derived_types
.length (); i
++)
1714 possible_polymorphic_call_targets_1 (nodes
, inserted
,
1717 type
->derived_types
[i
],
1718 otr_token
, outer_type
, offset
, completep
,
1719 bases_to_consider
, consider_construction
);
1722 /* Cache of queries for polymorphic call targets.
1724 Enumerating all call targets may get expensive when there are many
1725 polymorphic calls in the program, so we memoize all the previous
1726 queries and avoid duplicated work. */
1728 struct polymorphic_call_target_d
1730 HOST_WIDE_INT otr_token
;
1731 ipa_polymorphic_call_context context
;
1733 vec
<cgraph_node
*> targets
;
1734 int speculative_targets
;
1740 /* Polymorphic call target cache helpers. */
1742 struct polymorphic_call_target_hasher
1744 typedef polymorphic_call_target_d value_type
;
1745 typedef polymorphic_call_target_d compare_type
;
1746 static inline hashval_t
hash (const value_type
*);
1747 static inline bool equal (const value_type
*, const compare_type
*);
1748 static inline void remove (value_type
*);
1751 /* Return the computed hashcode for ODR_QUERY. */
1754 polymorphic_call_target_hasher::hash (const value_type
*odr_query
)
1756 inchash::hash
hstate (odr_query
->otr_token
);
1758 hstate
.add_wide_int (odr_query
->type
->id
);
1759 hstate
.merge_hash (TYPE_UID (odr_query
->context
.outer_type
));
1760 hstate
.add_wide_int (odr_query
->context
.offset
);
1762 if (odr_query
->context
.speculative_outer_type
)
1764 hstate
.merge_hash (TYPE_UID (odr_query
->context
.speculative_outer_type
));
1765 hstate
.add_wide_int (odr_query
->context
.speculative_offset
);
1767 hstate
.add_flag (odr_query
->context
.maybe_in_construction
);
1768 hstate
.add_flag (odr_query
->context
.maybe_derived_type
);
1769 hstate
.add_flag (odr_query
->context
.speculative_maybe_derived_type
);
1770 hstate
.commit_flag ();
1771 return hstate
.end ();
1774 /* Compare cache entries T1 and T2. */
1777 polymorphic_call_target_hasher::equal (const value_type
*t1
,
1778 const compare_type
*t2
)
1780 return (t1
->type
== t2
->type
&& t1
->otr_token
== t2
->otr_token
1781 && t1
->context
.offset
== t2
->context
.offset
1782 && t1
->context
.speculative_offset
== t2
->context
.speculative_offset
1783 && t1
->context
.outer_type
== t2
->context
.outer_type
1784 && t1
->context
.speculative_outer_type
== t2
->context
.speculative_outer_type
1785 && t1
->context
.maybe_in_construction
1786 == t2
->context
.maybe_in_construction
1787 && t1
->context
.maybe_derived_type
== t2
->context
.maybe_derived_type
1788 && (t1
->context
.speculative_maybe_derived_type
1789 == t2
->context
.speculative_maybe_derived_type
));
1792 /* Remove entry in polymorphic call target cache hash. */
1795 polymorphic_call_target_hasher::remove (value_type
*v
)
1797 v
->targets
.release ();
1801 /* Polymorphic call target query cache. */
1803 typedef hash_table
<polymorphic_call_target_hasher
>
1804 polymorphic_call_target_hash_type
;
1805 static polymorphic_call_target_hash_type
*polymorphic_call_target_hash
;
1807 /* Destroy polymorphic call target query cache. */
1810 free_polymorphic_call_targets_hash ()
1812 if (cached_polymorphic_call_targets
)
1814 delete polymorphic_call_target_hash
;
1815 polymorphic_call_target_hash
= NULL
;
1816 delete cached_polymorphic_call_targets
;
1817 cached_polymorphic_call_targets
= NULL
;
1821 /* When virtual function is removed, we may need to flush the cache. */
1824 devirt_node_removal_hook (struct cgraph_node
*n
, void *d ATTRIBUTE_UNUSED
)
1826 if (cached_polymorphic_call_targets
1827 && cached_polymorphic_call_targets
->contains (n
))
1828 free_polymorphic_call_targets_hash ();
1831 /* Return true when TYPE contains an polymorphic type and thus is interesting
1832 for devirtualization machinery. */
1835 contains_polymorphic_type_p (const_tree type
)
1837 type
= TYPE_MAIN_VARIANT (type
);
1839 if (RECORD_OR_UNION_TYPE_P (type
))
1841 if (TYPE_BINFO (type
)
1842 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
1844 for (tree fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
1845 if (TREE_CODE (fld
) == FIELD_DECL
1846 && !DECL_ARTIFICIAL (fld
)
1847 && contains_polymorphic_type_p (TREE_TYPE (fld
)))
1851 if (TREE_CODE (type
) == ARRAY_TYPE
)
1852 return contains_polymorphic_type_p (TREE_TYPE (type
));
1856 /* Clear speculative info from CONTEXT. */
1859 clear_speculation (ipa_polymorphic_call_context
*context
)
1861 context
->speculative_outer_type
= NULL
;
1862 context
->speculative_offset
= 0;
1863 context
->speculative_maybe_derived_type
= false;
1866 /* CONTEXT->OUTER_TYPE is a type of memory object where object of EXPECTED_TYPE
1867 is contained at CONTEXT->OFFSET. Walk the memory representation of
1868 CONTEXT->OUTER_TYPE and find the outermost class type that match
1869 EXPECTED_TYPE or contain EXPECTED_TYPE as a base. Update CONTEXT
1872 For example when CONTEXT represents type
1878 and we look for type at offset sizeof(int), we end up with B and offset 0.
1879 If the same is produced by multiple inheritance, we end up with A and offset
1882 If we can not find corresponding class, give up by setting
1883 CONTEXT->OUTER_TYPE to EXPECTED_TYPE and CONTEXT->OFFSET to NULL.
1884 Return true when lookup was sucesful. */
1887 get_class_context (ipa_polymorphic_call_context
*context
,
1890 tree type
= context
->outer_type
;
1891 HOST_WIDE_INT offset
= context
->offset
;
1892 bool speculative
= false;
1893 bool speculation_valid
= false;
1896 if (!context
->outer_type
)
1898 type
= context
->outer_type
= expected_type
;
1899 context
->offset
= offset
= 0;
1902 if (context
->speculative_outer_type
== context
->outer_type
1903 && (!context
->maybe_derived_type
1904 || context
->speculative_maybe_derived_type
))
1906 context
->speculative_outer_type
= NULL
;
1907 context
->speculative_offset
= 0;
1908 context
->speculative_maybe_derived_type
= false;
1911 /* See if speculative type seem to be derrived from outer_type.
1912 Then speculation is valid only if it really is a derivate and derived types
1915 The test does not really look for derivate, but also accepts the case where
1916 outer_type is a field of speculative_outer_type. In this case eiter
1917 MAYBE_DERIVED_TYPE is false and we have full non-speculative information or
1918 the loop bellow will correctly update SPECULATIVE_OUTER_TYPE
1919 and SPECULATIVE_MAYBE_DERIVED_TYPE. */
1920 if (context
->speculative_outer_type
1921 && context
->speculative_offset
>= context
->offset
1922 && contains_type_p (context
->speculative_outer_type
,
1923 context
->offset
- context
->speculative_offset
,
1924 context
->outer_type
))
1925 speculation_valid
= context
->maybe_derived_type
;
1927 clear_speculation (context
);
1929 /* Find the sub-object the constant actually refers to and mark whether it is
1930 an artificial one (as opposed to a user-defined one).
1932 This loop is performed twice; first time for outer_type and second time
1933 for speculative_outer_type. The second iteration has SPECULATIVE set. */
1936 HOST_WIDE_INT pos
, size
;
1939 /* On a match, just return what we found. */
1940 if (TREE_CODE (type
) == TREE_CODE (expected_type
)
1942 || (TREE_CODE (type
) == RECORD_TYPE
1943 && TYPE_BINFO (type
)
1944 && polymorphic_type_binfo_p (TYPE_BINFO (type
))))
1945 && types_same_for_odr (type
, expected_type
))
1949 gcc_assert (speculation_valid
);
1952 /* If we did not match the offset, just give up on speculation. */
1954 || (types_same_for_odr (context
->speculative_outer_type
,
1955 context
->outer_type
)
1956 && (context
->maybe_derived_type
1957 == context
->speculative_maybe_derived_type
)))
1958 clear_speculation (context
);
1963 /* Type can not contain itself on an non-zero offset. In that case
1971 /* If speculation is not valid or we determined type precisely,
1973 if (!speculation_valid
1974 || !context
->maybe_derived_type
)
1976 clear_speculation (context
);
1979 /* Otherwise look into speculation now. */
1983 type
= context
->speculative_outer_type
;
1984 offset
= context
->speculative_offset
;
1990 /* Walk fields and find corresponding on at OFFSET. */
1991 if (TREE_CODE (type
) == RECORD_TYPE
)
1993 for (fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
1995 if (TREE_CODE (fld
) != FIELD_DECL
)
1998 pos
= int_bit_position (fld
);
1999 size
= tree_to_uhwi (DECL_SIZE (fld
));
2000 if (pos
<= offset
&& (pos
+ size
) > offset
)
2007 type
= TYPE_MAIN_VARIANT (TREE_TYPE (fld
));
2009 /* DECL_ARTIFICIAL represents a basetype. */
2010 if (!DECL_ARTIFICIAL (fld
))
2014 context
->outer_type
= type
;
2015 context
->offset
= offset
;
2016 /* As soon as we se an field containing the type,
2017 we know we are not looking for derivations. */
2018 context
->maybe_derived_type
= false;
2022 context
->speculative_outer_type
= type
;
2023 context
->speculative_offset
= offset
;
2024 context
->speculative_maybe_derived_type
= false;
2028 else if (TREE_CODE (type
) == ARRAY_TYPE
)
2030 tree subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
2032 /* Give up if we don't know array size. */
2033 if (!tree_fits_shwi_p (TYPE_SIZE (subtype
))
2034 || !tree_to_shwi (TYPE_SIZE (subtype
)) <= 0)
2036 offset
= offset
% tree_to_shwi (TYPE_SIZE (subtype
));
2040 context
->outer_type
= type
;
2041 context
->offset
= offset
;
2042 context
->maybe_derived_type
= false;
2046 context
->speculative_outer_type
= type
;
2047 context
->speculative_offset
= offset
;
2048 context
->speculative_maybe_derived_type
= false;
2051 /* Give up on anything else. */
2056 /* If we failed to find subtype we look for, give up and fall back to the
2057 most generic query. */
2059 clear_speculation (context
);
2062 context
->outer_type
= expected_type
;
2063 context
->offset
= 0;
2064 context
->maybe_derived_type
= true;
2065 context
->maybe_in_construction
= true;
2066 /* POD can be changed to an instance of a polymorphic type by
2067 placement new. Here we play safe and assume that any
2068 non-polymorphic type is POD. */
2069 if ((TREE_CODE (type
) != RECORD_TYPE
2070 || !TYPE_BINFO (type
)
2071 || !polymorphic_type_binfo_p (TYPE_BINFO (type
)))
2072 && (!TYPE_SIZE (type
)
2073 || TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
2074 || (offset
+ tree_to_uhwi (TYPE_SIZE (expected_type
)) <=
2075 tree_to_uhwi (TYPE_SIZE (type
)))))
2080 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET. */
2083 contains_type_p (tree outer_type
, HOST_WIDE_INT offset
,
2086 ipa_polymorphic_call_context context
= {offset
, 0,
2087 TYPE_MAIN_VARIANT (outer_type
),
2088 NULL
, false, true, false};
2089 return get_class_context (&context
, otr_type
);
2092 /* Lookup base of BINFO that has virtual table VTABLE with OFFSET. */
2095 subbinfo_with_vtable_at_offset (tree binfo
, unsigned HOST_WIDE_INT offset
,
2098 tree v
= BINFO_VTABLE (binfo
);
2101 unsigned HOST_WIDE_INT this_offset
;
2105 if (!vtable_pointer_value_to_vtable (v
, &v
, &this_offset
))
2108 if (offset
== this_offset
2109 && DECL_ASSEMBLER_NAME (v
) == DECL_ASSEMBLER_NAME (vtable
))
2113 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
2114 if (polymorphic_type_binfo_p (base_binfo
))
2116 base_binfo
= subbinfo_with_vtable_at_offset (base_binfo
, offset
, vtable
);
2123 /* T is known constant value of virtual table pointer.
2124 Store virtual table to V and its offset to OFFSET.
2125 Return false if T does not look like virtual table reference. */
2128 vtable_pointer_value_to_vtable (const_tree t
, tree
*v
,
2129 unsigned HOST_WIDE_INT
*offset
)
2131 /* We expect &MEM[(void *)&virtual_table + 16B].
2132 We obtain object's BINFO from the context of the virtual table.
2133 This one contains pointer to virtual table represented via
2134 POINTER_PLUS_EXPR. Verify that this pointer match to what
2135 we propagated through.
2137 In the case of virtual inheritance, the virtual tables may
2138 be nested, i.e. the offset may be different from 16 and we may
2139 need to dive into the type representation. */
2140 if (TREE_CODE (t
) == ADDR_EXPR
2141 && TREE_CODE (TREE_OPERAND (t
, 0)) == MEM_REF
2142 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == ADDR_EXPR
2143 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 1)) == INTEGER_CST
2144 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0))
2146 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2147 (TREE_OPERAND (t
, 0), 0), 0)))
2149 *v
= TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 0);
2150 *offset
= tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t
, 0), 1));
2154 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2155 We need to handle it when T comes from static variable initializer or
2157 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
2159 *offset
= tree_to_uhwi (TREE_OPERAND (t
, 1));
2160 t
= TREE_OPERAND (t
, 0);
2165 if (TREE_CODE (t
) != ADDR_EXPR
)
2167 *v
= TREE_OPERAND (t
, 0);
2171 /* T is known constant value of virtual table pointer. Return BINFO of the
2175 vtable_pointer_value_to_binfo (const_tree t
)
2178 unsigned HOST_WIDE_INT offset
;
2180 if (!vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2183 /* FIXME: for stores of construction vtables we return NULL,
2184 because we do not have BINFO for those. Eventually we should fix
2185 our representation to allow this case to be handled, too.
2186 In the case we see store of BINFO we however may assume
2187 that standard folding will be ale to cope with it. */
2188 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
2192 /* We know that the instance is stored in variable or parameter
2193 (not dynamically allocated) and we want to disprove the fact
2194 that it may be in construction at invocation of CALL.
2196 For the variable to be in construction we actually need to
2197 be in constructor of corresponding global variable or
2198 the inline stack of CALL must contain the constructor.
2199 Check this condition. This check works safely only before
2200 IPA passes, because inline stacks may become out of date
2204 decl_maybe_in_construction_p (tree base
, tree outer_type
,
2205 gimple call
, tree function
)
2207 outer_type
= TYPE_MAIN_VARIANT (outer_type
);
2208 gcc_assert (DECL_P (base
));
2210 /* After inlining the code unification optimizations may invalidate
2211 inline stacks. Also we need to give up on global variables after
2212 IPA, because addresses of these may have been propagated to their
2214 if (DECL_STRUCT_FUNCTION (function
)->after_inlining
)
2217 /* Pure functions can not do any changes on the dynamic type;
2218 that require writting to memory. */
2219 if (!auto_var_in_fn_p (base
, function
)
2220 && flags_from_decl_or_type (function
) & (ECF_PURE
| ECF_CONST
))
2223 for (tree block
= gimple_block (call
); block
&& TREE_CODE (block
) == BLOCK
;
2224 block
= BLOCK_SUPERCONTEXT (block
))
2225 if (BLOCK_ABSTRACT_ORIGIN (block
)
2226 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
)) == FUNCTION_DECL
)
2228 tree fn
= BLOCK_ABSTRACT_ORIGIN (block
);
2230 if (TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
2231 || (!DECL_CXX_CONSTRUCTOR_P (fn
)
2232 && !DECL_CXX_DESTRUCTOR_P (fn
)))
2234 /* Watch for clones where we constant propagated the first
2235 argument (pointer to the instance). */
2236 fn
= DECL_ABSTRACT_ORIGIN (fn
);
2238 || !is_global_var (base
)
2239 || TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
2240 || (!DECL_CXX_CONSTRUCTOR_P (fn
)
2241 && !DECL_CXX_DESTRUCTOR_P (fn
)))
2244 if (flags_from_decl_or_type (fn
) & (ECF_PURE
| ECF_CONST
))
2247 /* FIXME: this can go away once we have ODR types equivalency on
2249 if (in_lto_p
&& !polymorphic_type_binfo_p (TYPE_BINFO (outer_type
)))
2251 tree type
= TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn
)));
2252 if (types_same_for_odr (type
, outer_type
))
2256 if (TREE_CODE (base
) == VAR_DECL
2257 && is_global_var (base
))
2259 if (TREE_CODE (TREE_TYPE (function
)) != METHOD_TYPE
2260 || (!DECL_CXX_CONSTRUCTOR_P (function
)
2261 && !DECL_CXX_DESTRUCTOR_P (function
)))
2263 if (!DECL_ABSTRACT_ORIGIN (function
))
2265 /* Watch for clones where we constant propagated the first
2266 argument (pointer to the instance). */
2267 function
= DECL_ABSTRACT_ORIGIN (function
);
2269 || TREE_CODE (TREE_TYPE (function
)) != METHOD_TYPE
2270 || (!DECL_CXX_CONSTRUCTOR_P (function
)
2271 && !DECL_CXX_DESTRUCTOR_P (function
)))
2274 /* FIXME: this can go away once we have ODR types equivalency on
2276 if (in_lto_p
&& !polymorphic_type_binfo_p (TYPE_BINFO (outer_type
)))
2278 tree type
= TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function
)));
2279 if (types_same_for_odr (type
, outer_type
))
2285 /* Proudce polymorphic call context for call method of instance
2286 that is located within BASE (that is assumed to be a decl) at OFFSET. */
2289 get_polymorphic_call_info_for_decl (ipa_polymorphic_call_context
*context
,
2290 tree base
, HOST_WIDE_INT offset
)
2292 gcc_assert (DECL_P (base
));
2294 context
->outer_type
= TYPE_MAIN_VARIANT (TREE_TYPE (base
));
2295 context
->offset
= offset
;
2296 context
->speculative_outer_type
= NULL
;
2297 context
->speculative_offset
= 0;
2298 context
->speculative_maybe_derived_type
= true;
2299 /* Make very conservative assumption that all objects
2300 may be in construction.
2301 TODO: ipa-prop already contains code to tell better.
2303 context
->maybe_in_construction
= true;
2304 context
->maybe_derived_type
= false;
2307 /* CST is an invariant (address of decl), try to get meaningful
2308 polymorphic call context for polymorphic call of method
2309 if instance of OTR_TYPE that is located at OFFSET of this invariant.
2310 Return FALSE if nothing meaningful can be found. */
2313 get_polymorphic_call_info_from_invariant (ipa_polymorphic_call_context
*context
,
2316 HOST_WIDE_INT offset
)
2318 HOST_WIDE_INT offset2
, size
, max_size
;
2321 if (TREE_CODE (cst
) != ADDR_EXPR
)
2324 cst
= TREE_OPERAND (cst
, 0);
2325 base
= get_ref_base_and_extent (cst
, &offset2
, &size
, &max_size
);
2326 if (!DECL_P (base
) || max_size
== -1 || max_size
!= size
)
2329 /* Only type inconsistent programs can have otr_type that is
2330 not part of outer type. */
2331 if (!contains_type_p (TREE_TYPE (base
), offset
, otr_type
))
2334 get_polymorphic_call_info_for_decl (context
, base
, offset
);
2338 /* See if OP is SSA name initialized as a copy or by single assignment.
2339 If so, walk the SSA graph up. */
2342 walk_ssa_copies (tree op
)
2345 while (TREE_CODE (op
) == SSA_NAME
2346 && !SSA_NAME_IS_DEFAULT_DEF (op
)
2347 && SSA_NAME_DEF_STMT (op
)
2348 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op
)))
2350 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op
)))
2352 op
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op
));
2358 /* Given REF call in FNDECL, determine class of the polymorphic
2359 call (OTR_TYPE), its token (OTR_TOKEN) and CONTEXT.
2360 CALL is optional argument giving the actual statement (usually call) where
2361 the context is used.
2362 Return pointer to object described by the context or an declaration if
2363 we found the instance to be stored in the static storage. */
2366 get_polymorphic_call_info (tree fndecl
,
2369 HOST_WIDE_INT
*otr_token
,
2370 ipa_polymorphic_call_context
*context
,
2374 *otr_type
= obj_type_ref_class (ref
);
2375 *otr_token
= tree_to_uhwi (OBJ_TYPE_REF_TOKEN (ref
));
2377 /* Set up basic info in case we find nothing interesting in the analysis. */
2378 context
->speculative_outer_type
= NULL
;
2379 context
->speculative_offset
= 0;
2380 context
->speculative_maybe_derived_type
= true;
2381 context
->outer_type
= TYPE_MAIN_VARIANT (*otr_type
);
2382 context
->offset
= 0;
2383 base_pointer
= OBJ_TYPE_REF_OBJECT (ref
);
2384 context
->maybe_derived_type
= true;
2385 context
->maybe_in_construction
= true;
2387 /* Walk SSA for outer object. */
2390 base_pointer
= walk_ssa_copies (base_pointer
);
2391 if (TREE_CODE (base_pointer
) == ADDR_EXPR
)
2393 HOST_WIDE_INT size
, max_size
;
2394 HOST_WIDE_INT offset2
;
2395 tree base
= get_ref_base_and_extent (TREE_OPERAND (base_pointer
, 0),
2396 &offset2
, &size
, &max_size
);
2398 /* If this is a varying address, punt. */
2399 if ((TREE_CODE (base
) == MEM_REF
|| DECL_P (base
))
2401 && max_size
== size
)
2403 /* We found dereference of a pointer. Type of the pointer
2404 and MEM_REF is meaningless, but we can look futher. */
2405 if (TREE_CODE (base
) == MEM_REF
)
2407 base_pointer
= TREE_OPERAND (base
, 0);
2409 += offset2
+ mem_ref_offset (base
).to_short_addr () * BITS_PER_UNIT
;
2410 context
->outer_type
= NULL
;
2412 /* We found base object. In this case the outer_type
2414 else if (DECL_P (base
))
2416 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (base
)));
2418 /* Only type inconsistent programs can have otr_type that is
2419 not part of outer type. */
2420 if (!contains_type_p (TREE_TYPE (base
),
2421 context
->offset
+ offset2
, *otr_type
))
2423 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
2424 code sequences; we arrange the calls to be builtin_unreachable
2426 *otr_token
= INT_MAX
;
2427 return base_pointer
;
2429 get_polymorphic_call_info_for_decl (context
, base
,
2430 context
->offset
+ offset2
);
2431 if (context
->maybe_in_construction
&& call
)
2432 context
->maybe_in_construction
2433 = decl_maybe_in_construction_p (base
,
2434 context
->outer_type
,
2445 else if (TREE_CODE (base_pointer
) == POINTER_PLUS_EXPR
2446 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer
, 1)))
2448 context
->offset
+= tree_to_shwi (TREE_OPERAND (base_pointer
, 1))
2450 base_pointer
= TREE_OPERAND (base_pointer
, 0);
2457 /* Try to determine type of the outer object. */
2458 if (TREE_CODE (base_pointer
) == SSA_NAME
2459 && SSA_NAME_IS_DEFAULT_DEF (base_pointer
)
2460 && TREE_CODE (SSA_NAME_VAR (base_pointer
)) == PARM_DECL
)
2462 /* See if parameter is THIS pointer of a method. */
2463 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
2464 && SSA_NAME_VAR (base_pointer
) == DECL_ARGUMENTS (fndecl
))
2467 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer
)));
2468 gcc_assert (TREE_CODE (context
->outer_type
) == RECORD_TYPE
);
2470 /* Dynamic casting has possibly upcasted the type
2471 in the hiearchy. In this case outer type is less
2472 informative than inner type and we should forget
2474 if (!contains_type_p (context
->outer_type
, context
->offset
,
2477 context
->outer_type
= NULL
;
2478 return base_pointer
;
2481 /* If the function is constructor or destructor, then
2482 the type is possibly in construction, but we know
2483 it is not derived type. */
2484 if (DECL_CXX_CONSTRUCTOR_P (fndecl
)
2485 || DECL_CXX_DESTRUCTOR_P (fndecl
))
2487 context
->maybe_in_construction
= true;
2488 context
->maybe_derived_type
= false;
2492 context
->maybe_derived_type
= true;
2493 context
->maybe_in_construction
= false;
2495 return base_pointer
;
2497 /* Non-PODs passed by value are really passed by invisible
2498 reference. In this case we also know the type of the
2500 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer
)))
2503 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer
)));
2504 gcc_assert (!POINTER_TYPE_P (context
->outer_type
));
2505 /* Only type inconsistent programs can have otr_type that is
2506 not part of outer type. */
2507 if (!contains_type_p (context
->outer_type
, context
->offset
,
2510 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
2511 code sequences; we arrange the calls to be builtin_unreachable
2513 *otr_token
= INT_MAX
;
2514 return base_pointer
;
2516 context
->maybe_derived_type
= false;
2517 context
->maybe_in_construction
= false;
2518 return base_pointer
;
2522 tree base_type
= TREE_TYPE (base_pointer
);
2524 if (TREE_CODE (base_pointer
) == SSA_NAME
2525 && SSA_NAME_IS_DEFAULT_DEF (base_pointer
)
2526 && TREE_CODE (SSA_NAME_VAR (base_pointer
)) != PARM_DECL
)
2528 /* Use OTR_TOKEN = INT_MAX as a marker of probably type inconsistent
2529 code sequences; we arrange the calls to be builtin_unreachable
2531 *otr_token
= INT_MAX
;
2532 return base_pointer
;
2534 if (TREE_CODE (base_pointer
) == SSA_NAME
2535 && SSA_NAME_DEF_STMT (base_pointer
)
2536 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer
)))
2537 base_type
= TREE_TYPE (gimple_assign_rhs1
2538 (SSA_NAME_DEF_STMT (base_pointer
)));
2540 if (POINTER_TYPE_P (base_type
)
2541 && contains_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)),
2545 context
->speculative_outer_type
= TYPE_MAIN_VARIANT
2546 (TREE_TYPE (base_type
));
2547 context
->speculative_offset
= context
->offset
;
2548 context
->speculative_maybe_derived_type
= true;
2550 /* TODO: There are multiple ways to derive a type. For instance
2551 if BASE_POINTER is passed to an constructor call prior our refernece.
2552 We do not make this type of flow sensitive analysis yet. */
2553 return base_pointer
;
2556 /* Structure to be passed in between detect_type_change and
2557 check_stmt_for_type_change. */
2559 struct type_change_info
2561 /* Offset into the object where there is the virtual method pointer we are
2563 HOST_WIDE_INT offset
;
2564 /* The declaration or SSA_NAME pointer of the base that we are checking for
2567 /* The reference to virtual table pointer used. */
2570 /* If we actually can tell the type that the object has changed to, it is
2571 stored in this field. Otherwise it remains NULL_TREE. */
2572 tree known_current_type
;
2573 HOST_WIDE_INT known_current_offset
;
2575 /* Set to true if dynamic type change has been detected. */
2576 bool type_maybe_changed
;
2577 /* Set to true if multiple types have been encountered. known_current_type
2578 must be disregarded in that case. */
2579 bool multiple_types_encountered
;
2580 /* Set to true if we possibly missed some dynamic type changes and we should
2581 consider the set to be speculative. */
2583 bool seen_unanalyzed_store
;
2586 /* Return true if STMT is not call and can modify a virtual method table pointer.
2587 We take advantage of fact that vtable stores must appear within constructor
2588 and destructor functions. */
2591 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt
)
2593 if (is_gimple_assign (stmt
))
2595 tree lhs
= gimple_assign_lhs (stmt
);
2597 if (gimple_clobber_p (stmt
))
2599 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs
)))
2601 if (flag_strict_aliasing
2602 && !POINTER_TYPE_P (TREE_TYPE (lhs
)))
2605 if (TREE_CODE (lhs
) == COMPONENT_REF
2606 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs
, 1)))
2608 /* In the future we might want to use get_base_ref_and_offset to find
2609 if there is a field corresponding to the offset and if so, proceed
2610 almost like if it was a component ref. */
2614 /* Code unification may mess with inline stacks. */
2615 if (cfun
->after_inlining
)
2618 /* Walk the inline stack and watch out for ctors/dtors.
2619 TODO: Maybe we can require the store to appear in toplevel
2620 block of CTOR/DTOR. */
2621 for (tree block
= gimple_block (stmt
); block
&& TREE_CODE (block
) == BLOCK
;
2622 block
= BLOCK_SUPERCONTEXT (block
))
2623 if (BLOCK_ABSTRACT_ORIGIN (block
)
2624 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
)) == FUNCTION_DECL
)
2626 tree fn
= BLOCK_ABSTRACT_ORIGIN (block
);
2628 if (flags_from_decl_or_type (fn
) & (ECF_PURE
| ECF_CONST
))
2630 return (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
2631 && (DECL_CXX_CONSTRUCTOR_P (fn
)
2632 || DECL_CXX_DESTRUCTOR_P (fn
)));
2634 return (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
2635 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl
)
2636 || DECL_CXX_DESTRUCTOR_P (current_function_decl
)));
2639 /* If STMT can be proved to be an assignment to the virtual method table
2640 pointer of ANALYZED_OBJ and the type associated with the new table
2641 identified, return the type. Otherwise return NULL_TREE. */
2644 extr_type_from_vtbl_ptr_store (gimple stmt
, struct type_change_info
*tci
,
2645 HOST_WIDE_INT
*type_offset
)
2647 HOST_WIDE_INT offset
, size
, max_size
;
2648 tree lhs
, rhs
, base
, binfo
;
2650 if (!gimple_assign_single_p (stmt
))
2653 lhs
= gimple_assign_lhs (stmt
);
2654 rhs
= gimple_assign_rhs1 (stmt
);
2655 if (TREE_CODE (lhs
) != COMPONENT_REF
2656 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs
, 1)))
2659 if (tci
->vtbl_ptr_ref
&& operand_equal_p (lhs
, tci
->vtbl_ptr_ref
, 0))
2663 base
= get_ref_base_and_extent (lhs
, &offset
, &size
, &max_size
);
2664 if (offset
!= tci
->offset
2665 || size
!= POINTER_SIZE
2666 || max_size
!= POINTER_SIZE
)
2668 if (DECL_P (tci
->instance
))
2670 if (base
!= tci
->instance
)
2673 else if (TREE_CODE (base
) == MEM_REF
)
2675 if (!operand_equal_p (tci
->instance
, TREE_OPERAND (base
, 0), 0)
2676 || !integer_zerop (TREE_OPERAND (base
, 1)))
2679 else if (!operand_equal_p (tci
->instance
, base
, 0)
2684 binfo
= vtable_pointer_value_to_binfo (rhs
);
2688 *type_offset
= tree_to_shwi (BINFO_OFFSET (binfo
)) * BITS_PER_UNIT
;
2689 if (TYPE_BINFO (BINFO_TYPE (binfo
)) == binfo
)
2690 return BINFO_TYPE (binfo
);
2692 /* TODO: Figure out the type containing BINFO. */
2696 /* Record dynamic type change of TCI to TYPE. */
2699 record_known_type (struct type_change_info
*tci
, tree type
, HOST_WIDE_INT offset
)
2705 fprintf (dump_file
, " Recording type: ");
2706 print_generic_expr (dump_file
, type
, TDF_SLIM
);
2707 fprintf (dump_file
, " at offset %i\n", (int)offset
);
2710 fprintf (dump_file
, " Recording unknown type\n");
2712 if (tci
->type_maybe_changed
2713 && (type
!= tci
->known_current_type
2714 || offset
!= tci
->known_current_offset
))
2715 tci
->multiple_types_encountered
= true;
2716 tci
->known_current_type
= type
;
2717 tci
->known_current_offset
= offset
;
2718 tci
->type_maybe_changed
= true;
2721 /* Callback of walk_aliased_vdefs and a helper function for
2722 detect_type_change to check whether a particular statement may modify
2723 the virtual table pointer, and if possible also determine the new type of
2724 the (sub-)object. It stores its result into DATA, which points to a
2725 type_change_info structure. */
2728 check_stmt_for_type_change (ao_ref
*ao ATTRIBUTE_UNUSED
, tree vdef
, void *data
)
2730 gimple stmt
= SSA_NAME_DEF_STMT (vdef
);
2731 struct type_change_info
*tci
= (struct type_change_info
*) data
;
2734 /* If we already gave up, just terminate the rest of walk. */
2735 if (tci
->multiple_types_encountered
)
2738 if (is_gimple_call (stmt
))
2740 if (gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
))
2743 /* Check for a constructor call. */
2744 if ((fn
= gimple_call_fndecl (stmt
)) != NULL_TREE
2745 && DECL_CXX_CONSTRUCTOR_P (fn
)
2746 && TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
2747 && gimple_call_num_args (stmt
))
2749 tree op
= walk_ssa_copies (gimple_call_arg (stmt
, 0));
2750 tree type
= method_class_type (TREE_TYPE (fn
));
2751 HOST_WIDE_INT offset
= 0, size
, max_size
;
2755 fprintf (dump_file
, " Checking constructor call: ");
2756 print_gimple_stmt (dump_file
, stmt
, 0, 0);
2759 /* See if THIS parameter seems like instance pointer. */
2760 if (TREE_CODE (op
) == ADDR_EXPR
)
2762 op
= get_ref_base_and_extent (TREE_OPERAND (op
, 0),
2763 &offset
, &size
, &max_size
);
2764 if (size
!= max_size
|| max_size
== -1)
2766 tci
->speculative
= true;
2769 if (op
&& TREE_CODE (op
) == MEM_REF
)
2771 if (!tree_fits_shwi_p (TREE_OPERAND (op
, 1)))
2773 tci
->speculative
= true;
2776 offset
+= tree_to_shwi (TREE_OPERAND (op
, 1))
2778 op
= TREE_OPERAND (op
, 0);
2782 tci
->speculative
= true;
2785 op
= walk_ssa_copies (op
);
2787 if (operand_equal_p (op
, tci
->instance
, 0)
2789 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
2790 && tree_fits_shwi_p (TYPE_SIZE (type
))
2791 && tree_to_shwi (TYPE_SIZE (type
)) + offset
> tci
->offset
)
2793 record_known_type (tci
, type
, tci
->offset
- offset
);
2797 /* Calls may possibly change dynamic type by placement new. Assume
2798 it will not happen, but make result speculative only. */
2801 fprintf (dump_file
, " Function call may change dynamic type:");
2802 print_gimple_stmt (dump_file
, stmt
, 0, 0);
2804 tci
->speculative
= true;
2807 /* Check for inlined virtual table store. */
2808 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt
))
2811 HOST_WIDE_INT offset
= 0;
2814 fprintf (dump_file
, " Checking vtbl store: ");
2815 print_gimple_stmt (dump_file
, stmt
, 0, 0);
2818 type
= extr_type_from_vtbl_ptr_store (stmt
, tci
, &offset
);
2819 gcc_assert (!type
|| TYPE_MAIN_VARIANT (type
) == type
);
2823 fprintf (dump_file
, " Unanalyzed store may change type.\n");
2824 tci
->seen_unanalyzed_store
= true;
2825 tci
->speculative
= true;
2828 record_known_type (tci
, type
, offset
);
2835 /* CONTEXT is polymorphic call context obtained from get_polymorphic_context.
2836 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
2837 INSTANCE is pointer to the outer instance as returned by
2838 get_polymorphic_context. To avoid creation of temporary expressions,
2839 INSTANCE may also be an declaration of get_polymorphic_context found the
2840 value to be in static storage.
2842 If the type of instance is not fully determined
2843 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
2844 is set), try to walk memory writes and find the actual construction of the
2847 We do not include this analysis in the context analysis itself, because
2848 it needs memory SSA to be fully built and the walk may be expensive.
2849 So it is not suitable for use withing fold_stmt and similar uses. */
2852 get_dynamic_type (tree instance
,
2853 ipa_polymorphic_call_context
*context
,
2858 struct type_change_info tci
;
2860 bool function_entry_reached
= false;
2861 tree instance_ref
= NULL
;
2864 if (!context
->maybe_in_construction
&& !context
->maybe_derived_type
)
2867 /* We need to obtain refernce to virtual table pointer. It is better
2868 to look it up in the code rather than build our own. This require bit
2869 of pattern matching, but we end up verifying that what we found is
2872 What we pattern match is:
2874 tmp = instance->_vptr.A; // vtbl ptr load
2875 tmp2 = tmp[otr_token]; // vtable lookup
2876 OBJ_TYPE_REF(tmp2;instance->0) (instance);
2878 We want to start alias oracle walk from vtbl pointer load,
2879 but we may not be able to identify it, for example, when PRE moved the
2882 if (gimple_code (call
) == GIMPLE_CALL
)
2884 tree ref
= gimple_call_fn (call
);
2885 HOST_WIDE_INT offset2
, size
, max_size
;
2887 if (TREE_CODE (ref
) == OBJ_TYPE_REF
)
2889 ref
= OBJ_TYPE_REF_EXPR (ref
);
2890 ref
= walk_ssa_copies (ref
);
2892 /* Check if definition looks like vtable lookup. */
2893 if (TREE_CODE (ref
) == SSA_NAME
2894 && !SSA_NAME_IS_DEFAULT_DEF (ref
)
2895 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref
))
2896 && TREE_CODE (gimple_assign_rhs1
2897 (SSA_NAME_DEF_STMT (ref
))) == MEM_REF
)
2899 ref
= get_base_address
2900 (TREE_OPERAND (gimple_assign_rhs1
2901 (SSA_NAME_DEF_STMT (ref
)), 0));
2902 ref
= walk_ssa_copies (ref
);
2903 /* Find base address of the lookup and see if it looks like
2905 if (TREE_CODE (ref
) == SSA_NAME
2906 && !SSA_NAME_IS_DEFAULT_DEF (ref
)
2907 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref
)))
2909 tree ref_exp
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref
));
2910 tree base_ref
= get_ref_base_and_extent
2911 (ref_exp
, &offset2
, &size
, &max_size
);
2913 /* Finally verify that what we found looks like read from OTR_OBJECT
2914 or from INSTANCE with offset OFFSET. */
2916 && ((TREE_CODE (base_ref
) == MEM_REF
2917 && ((offset2
== context
->offset
2918 && TREE_OPERAND (base_ref
, 0) == instance
)
2919 || (!offset2
&& TREE_OPERAND (base_ref
, 0) == otr_object
)))
2920 || (DECL_P (instance
) && base_ref
== instance
2921 && offset2
== context
->offset
)))
2923 stmt
= SSA_NAME_DEF_STMT (ref
);
2924 instance_ref
= ref_exp
;
2931 /* If we failed to look up the refernece in code, build our own. */
2934 /* If the statement in question does not use memory, we can't tell
2936 if (!gimple_vuse (stmt
))
2938 ao_ref_init_from_ptr_and_size (&ao
, otr_object
, NULL
);
2941 /* Otherwise use the real reference. */
2942 ao_ref_init (&ao
, instance_ref
);
2944 /* We look for vtbl pointer read. */
2945 ao
.size
= POINTER_SIZE
;
2946 ao
.max_size
= ao
.size
;
2948 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type
))));
2952 fprintf (dump_file
, "Determining dynamic type for call: ");
2953 print_gimple_stmt (dump_file
, call
, 0, 0);
2954 fprintf (dump_file
, " Starting walk at: ");
2955 print_gimple_stmt (dump_file
, stmt
, 0, 0);
2956 fprintf (dump_file
, " instance pointer: ");
2957 print_generic_expr (dump_file
, otr_object
, TDF_SLIM
);
2958 fprintf (dump_file
, " Outer instance pointer: ");
2959 print_generic_expr (dump_file
, instance
, TDF_SLIM
);
2960 fprintf (dump_file
, " offset: %i (bits)", (int)context
->offset
);
2961 fprintf (dump_file
, " vtbl reference: ");
2962 print_generic_expr (dump_file
, instance_ref
, TDF_SLIM
);
2963 fprintf (dump_file
, "\n");
2966 tci
.offset
= context
->offset
;
2967 tci
.instance
= instance
;
2968 tci
.vtbl_ptr_ref
= instance_ref
;
2969 gcc_assert (TREE_CODE (instance
) != MEM_REF
);
2970 tci
.known_current_type
= NULL_TREE
;
2971 tci
.known_current_offset
= 0;
2972 tci
.otr_type
= otr_type
;
2973 tci
.type_maybe_changed
= false;
2974 tci
.multiple_types_encountered
= false;
2975 tci
.speculative
= false;
2976 tci
.seen_unanalyzed_store
= false;
2978 walk_aliased_vdefs (&ao
, gimple_vuse (stmt
), check_stmt_for_type_change
,
2979 &tci
, NULL
, &function_entry_reached
);
2981 /* If we did not find any type changing statements, we may still drop
2982 maybe_in_construction flag if the context already have outer type.
2984 Here we make special assumptions about both constructors and
2985 destructors which are all the functions that are allowed to alter the
2986 VMT pointers. It assumes that destructors begin with assignment into
2987 all VMT pointers and that constructors essentially look in the
2990 1) The very first thing they do is that they call constructors of
2991 ancestor sub-objects that have them.
2993 2) Then VMT pointers of this and all its ancestors is set to new
2994 values corresponding to the type corresponding to the constructor.
2996 3) Only afterwards, other stuff such as constructor of member
2997 sub-objects and the code written by the user is run. Only this may
2998 include calling virtual functions, directly or indirectly.
3000 4) placement new can not be used to change type of non-POD statically
3001 allocated variables.
3003 There is no way to call a constructor of an ancestor sub-object in any
3006 This means that we do not have to care whether constructors get the
3007 correct type information because they will always change it (in fact,
3008 if we define the type to be given by the VMT pointer, it is undefined).
3010 The most important fact to derive from the above is that if, for some
3011 statement in the section 3, we try to detect whether the dynamic type
3012 has changed, we can safely ignore all calls as we examine the function
3013 body backwards until we reach statements in section 2 because these
3014 calls cannot be ancestor constructors or destructors (if the input is
3015 not bogus) and so do not change the dynamic type (this holds true only
3016 for automatically allocated objects but at the moment we devirtualize
3017 only these). We then must detect that statements in section 2 change
3018 the dynamic type and can try to derive the new type. That is enough
3019 and we can stop, we will never see the calls into constructors of
3020 sub-objects in this code.
3022 Therefore if the static outer type was found (context->outer_type)
3023 we can safely ignore tci.speculative that is set on calls and give up
3024 only if there was dyanmic type store that may affect given variable
3025 (seen_unanalyzed_store) */
3027 if (!tci
.type_maybe_changed
)
3029 if (!context
->outer_type
|| tci
.seen_unanalyzed_store
)
3031 if (context
->maybe_in_construction
)
3032 context
->maybe_in_construction
= false;
3034 fprintf (dump_file
, " No dynamic type change found.\n");
3038 if (tci
.known_current_type
3039 && !function_entry_reached
3040 && !tci
.multiple_types_encountered
)
3042 if (!tci
.speculative
3043 /* Again in instances located in static storage we are interested only
3044 in constructor stores. */
3045 || (context
->outer_type
3046 && !tci
.seen_unanalyzed_store
3047 && context
->offset
== tci
.offset
3048 && types_same_for_odr (tci
.known_current_type
,
3049 context
->outer_type
)))
3051 context
->outer_type
= tci
.known_current_type
;
3052 context
->offset
= tci
.known_current_offset
;
3053 context
->maybe_in_construction
= false;
3054 context
->maybe_derived_type
= false;
3056 fprintf (dump_file
, " Determined dynamic type.\n");
3058 else if (!context
->speculative_outer_type
3059 || context
->speculative_maybe_derived_type
)
3061 context
->speculative_outer_type
= tci
.known_current_type
;
3062 context
->speculative_offset
= tci
.known_current_offset
;
3063 context
->speculative_maybe_derived_type
= false;
3065 fprintf (dump_file
, " Determined speculative dynamic type.\n");
3069 fprintf (dump_file
, " Found multiple types.\n");
3074 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
3075 Lookup their respecitve virtual methods for OTR_TOKEN and OTR_TYPE
3076 and insert them to NODES.
3078 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
3081 record_targets_from_bases (tree otr_type
,
3082 HOST_WIDE_INT otr_token
,
3084 HOST_WIDE_INT offset
,
3085 vec
<cgraph_node
*> &nodes
,
3086 hash_set
<tree
> *inserted
,
3087 hash_set
<tree
> *matched_vtables
,
3092 HOST_WIDE_INT pos
, size
;
3096 if (types_same_for_odr (outer_type
, otr_type
))
3099 for (fld
= TYPE_FIELDS (outer_type
); fld
; fld
= DECL_CHAIN (fld
))
3101 if (TREE_CODE (fld
) != FIELD_DECL
)
3104 pos
= int_bit_position (fld
);
3105 size
= tree_to_shwi (DECL_SIZE (fld
));
3106 if (pos
<= offset
&& (pos
+ size
) > offset
3107 /* Do not get confused by zero sized bases. */
3108 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld
))))
3111 /* Within a class type we should always find correcponding fields. */
3112 gcc_assert (fld
&& TREE_CODE (TREE_TYPE (fld
)) == RECORD_TYPE
);
3114 /* Nonbasetypes should have been stripped by outer_class_type. */
3115 gcc_assert (DECL_ARTIFICIAL (fld
));
3117 outer_type
= TREE_TYPE (fld
);
3120 base_binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
),
3124 gcc_assert (odr_violation_reported
);
3127 gcc_assert (base_binfo
);
3128 if (!matched_vtables
->add (BINFO_VTABLE (base_binfo
)))
3131 tree target
= gimple_get_virt_method_for_binfo (otr_token
,
3134 if (!target
|| ! DECL_CXX_DESTRUCTOR_P (target
))
3135 maybe_record_node (nodes
, target
, inserted
, can_refer
, completep
);
3136 matched_vtables
->add (BINFO_VTABLE (base_binfo
));
3141 /* When virtual table is removed, we may need to flush the cache. */
3144 devirt_variable_node_removal_hook (varpool_node
*n
,
3145 void *d ATTRIBUTE_UNUSED
)
3147 if (cached_polymorphic_call_targets
3148 && DECL_VIRTUAL_P (n
->decl
)
3149 && type_in_anonymous_namespace_p (DECL_CONTEXT (n
->decl
)))
3150 free_polymorphic_call_targets_hash ();
3153 /* Record about how many calls would benefit from given type to be final. */
3155 struct odr_type_warn_count
3159 gcov_type dyn_count
;
3162 /* Record about how many calls would benefit from given method to be final. */
3164 struct decl_warn_count
3168 gcov_type dyn_count
;
3171 /* Information about type and decl warnings. */
3173 struct final_warning_record
3175 gcov_type dyn_count
;
3176 vec
<odr_type_warn_count
> type_warnings
;
3177 hash_map
<tree
, decl_warn_count
> decl_warnings
;
3179 struct final_warning_record
*final_warning_records
;
3181 /* Return vector containing possible targets of polymorphic call of type
3182 OTR_TYPE caling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3183 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containig
3184 OTR_TYPE and include their virtual method. This is useful for types
3185 possibly in construction or destruction where the virtual table may
3186 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3187 us to walk the inheritance graph for all derivations.
3189 OTR_TOKEN == INT_MAX is used to mark calls that are provably
3190 undefined and should be redirected to unreachable.
3192 If COMPLETEP is non-NULL, store true if the list is complete.
3193 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3194 in the target cache. If user needs to visit every target list
3195 just once, it can memoize them.
3197 SPECULATION_TARGETS specify number of targets that are speculatively
3198 likely. These include targets specified by the speculative part
3199 of polymoprhic call context and also exclude all targets for classes
3202 Returned vector is placed into cache. It is NOT caller's responsibility
3203 to free it. The vector can be freed on cgraph_remove_node call if
3204 the particular node is a virtual function present in the cache. */
3207 possible_polymorphic_call_targets (tree otr_type
,
3208 HOST_WIDE_INT otr_token
,
3209 ipa_polymorphic_call_context context
,
3212 int *speculative_targetsp
)
3214 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
3215 vec
<cgraph_node
*> nodes
= vNULL
;
3216 vec
<tree
> bases_to_consider
= vNULL
;
3217 odr_type type
, outer_type
;
3218 polymorphic_call_target_d key
;
3219 polymorphic_call_target_d
**slot
;
3224 bool skipped
= false;
3226 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
3228 /* If ODR is not initialized, return empty incomplete list. */
3234 *cache_token
= NULL
;
3235 if (speculative_targetsp
)
3236 *speculative_targetsp
= 0;
3240 /* If we hit type inconsistency, just return empty list of targets. */
3241 if (otr_token
== INT_MAX
)
3246 *cache_token
= NULL
;
3247 if (speculative_targetsp
)
3248 *speculative_targetsp
= 0;
3252 /* Do not bother to compute speculative info when user do not asks for it. */
3253 if (!speculative_targetsp
|| !context
.speculative_outer_type
)
3254 clear_speculation (&context
);
3256 type
= get_odr_type (otr_type
, true);
3258 /* Recording type variants would wast results cache. */
3259 gcc_assert (!context
.outer_type
3260 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3262 /* Lookup the outer class type we want to walk. */
3263 if ((context
.outer_type
|| context
.speculative_outer_type
)
3264 && !get_class_context (&context
, otr_type
))
3269 *cache_token
= NULL
;
3270 if (speculative_targetsp
)
3271 *speculative_targetsp
= 0;
3275 /* Check that get_class_context kept the main variant. */
3276 gcc_assert (!context
.outer_type
3277 || TYPE_MAIN_VARIANT (context
.outer_type
) == context
.outer_type
);
3279 /* We canonicalize our query, so we do not need extra hashtable entries. */
3281 /* Without outer type, we have no use for offset. Just do the
3282 basic search from innter type */
3283 if (!context
.outer_type
)
3285 context
.outer_type
= otr_type
;
3288 /* We need to update our hiearchy if the type does not exist. */
3289 outer_type
= get_odr_type (context
.outer_type
, true);
3290 /* If the type is complete, there are no derivations. */
3291 if (TYPE_FINAL_P (outer_type
->type
))
3292 context
.maybe_derived_type
= false;
3294 /* Initialize query cache. */
3295 if (!cached_polymorphic_call_targets
)
3297 cached_polymorphic_call_targets
= new hash_set
<cgraph_node
*>;
3298 polymorphic_call_target_hash
3299 = new polymorphic_call_target_hash_type (23);
3300 if (!node_removal_hook_holder
)
3302 node_removal_hook_holder
=
3303 cgraph_add_node_removal_hook (&devirt_node_removal_hook
, NULL
);
3304 varpool_add_node_removal_hook (&devirt_variable_node_removal_hook
,
3309 /* Lookup cached answer. */
3311 key
.otr_token
= otr_token
;
3312 key
.context
= context
;
3313 slot
= polymorphic_call_target_hash
->find_slot (&key
, INSERT
);
3315 *cache_token
= (void *)*slot
;
3319 *completep
= (*slot
)->complete
;
3320 if (speculative_targetsp
)
3321 *speculative_targetsp
= (*slot
)->speculative_targets
;
3322 if ((*slot
)->type_warning
&& final_warning_records
)
3324 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].count
++;
3325 final_warning_records
->type_warnings
[(*slot
)->type_warning
- 1].dyn_count
3326 += final_warning_records
->dyn_count
;
3328 if ((*slot
)->decl_warning
&& final_warning_records
)
3330 struct decl_warn_count
*c
=
3331 final_warning_records
->decl_warnings
.get ((*slot
)->decl_warning
);
3333 c
->dyn_count
+= final_warning_records
->dyn_count
;
3335 return (*slot
)->targets
;
3340 /* Do actual search. */
3341 timevar_push (TV_IPA_VIRTUAL_CALL
);
3342 *slot
= XCNEW (polymorphic_call_target_d
);
3344 *cache_token
= (void *)*slot
;
3345 (*slot
)->type
= type
;
3346 (*slot
)->otr_token
= otr_token
;
3347 (*slot
)->context
= context
;
3348 (*slot
)->speculative_targets
= 0;
3350 hash_set
<tree
> inserted
;
3351 hash_set
<tree
> matched_vtables
;
3353 /* First insert targets we speculatively identified as likely. */
3354 if (context
.speculative_outer_type
)
3356 odr_type speculative_outer_type
;
3357 bool speculation_complete
= true;
3359 /* First insert target from type itself and check if it may have derived types. */
3360 speculative_outer_type
= get_odr_type (context
.speculative_outer_type
, true);
3361 if (TYPE_FINAL_P (speculative_outer_type
->type
))
3362 context
.speculative_maybe_derived_type
= false;
3363 binfo
= get_binfo_at_offset (TYPE_BINFO (speculative_outer_type
->type
),
3364 context
.speculative_offset
, otr_type
);
3366 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3371 /* In the case we get complete method, we don't need
3372 to walk derivations. */
3373 if (target
&& DECL_FINAL_P (target
))
3374 context
.speculative_maybe_derived_type
= false;
3375 if (type_possibly_instantiated_p (speculative_outer_type
->type
))
3376 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &speculation_complete
);
3378 matched_vtables
.add (BINFO_VTABLE (binfo
));
3381 /* Next walk recursively all derived types. */
3382 if (context
.speculative_maybe_derived_type
)
3383 for (i
= 0; i
< speculative_outer_type
->derived_types
.length(); i
++)
3384 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3387 speculative_outer_type
->derived_types
[i
],
3388 otr_token
, speculative_outer_type
->type
,
3389 context
.speculative_offset
,
3390 &speculation_complete
,
3393 (*slot
)->speculative_targets
= nodes
.length();
3396 /* First see virtual method of type itself. */
3397 binfo
= get_binfo_at_offset (TYPE_BINFO (outer_type
->type
),
3398 context
.offset
, otr_type
);
3400 target
= gimple_get_virt_method_for_binfo (otr_token
, binfo
,
3404 gcc_assert (odr_violation_reported
);
3408 /* Destructors are never called through construction virtual tables,
3409 because the type is always known. */
3410 if (target
&& DECL_CXX_DESTRUCTOR_P (target
))
3411 context
.maybe_in_construction
= false;
3415 /* In the case we get complete method, we don't need
3416 to walk derivations. */
3417 if (DECL_FINAL_P (target
))
3418 context
.maybe_derived_type
= false;
3421 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3422 if (type_possibly_instantiated_p (outer_type
->type
))
3423 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3427 gcc_assert (in_lto_p
|| context
.maybe_derived_type
);
3431 matched_vtables
.add (BINFO_VTABLE (binfo
));
3433 /* Next walk recursively all derived types. */
3434 if (context
.maybe_derived_type
)
3436 for (i
= 0; i
< outer_type
->derived_types
.length(); i
++)
3437 possible_polymorphic_call_targets_1 (nodes
, &inserted
,
3440 outer_type
->derived_types
[i
],
3441 otr_token
, outer_type
->type
,
3442 context
.offset
, &complete
,
3444 context
.maybe_in_construction
);
3446 if (!outer_type
->all_derivations_known
)
3448 if (final_warning_records
)
3451 && nodes
.length () == 1
3452 && warn_suggest_final_types
3453 && !outer_type
->derived_types
.length ())
3455 if (outer_type
->id
>= (int)final_warning_records
->type_warnings
.length ())
3456 final_warning_records
->type_warnings
.safe_grow_cleared
3457 (odr_types
.length ());
3458 final_warning_records
->type_warnings
[outer_type
->id
].count
++;
3459 final_warning_records
->type_warnings
[outer_type
->id
].dyn_count
3460 += final_warning_records
->dyn_count
;
3461 final_warning_records
->type_warnings
[outer_type
->id
].type
3463 (*slot
)->type_warning
= outer_type
->id
+ 1;
3466 && warn_suggest_final_methods
3467 && nodes
.length () == 1
3468 && types_same_for_odr (DECL_CONTEXT (nodes
[0]->decl
),
3472 struct decl_warn_count
&c
=
3473 final_warning_records
->decl_warnings
.get_or_insert
3474 (nodes
[0]->decl
, &existed
);
3479 c
.dyn_count
+= final_warning_records
->dyn_count
;
3484 c
.dyn_count
= final_warning_records
->dyn_count
;
3485 c
.decl
= nodes
[0]->decl
;
3487 (*slot
)->decl_warning
= nodes
[0]->decl
;
3494 /* Finally walk bases, if asked to. */
3495 if (!(*slot
)->speculative_targets
)
3496 (*slot
)->speculative_targets
= nodes
.length();
3498 /* Destructors are never called through construction virtual tables,
3499 because the type is always known. One of entries may be cxa_pure_virtual
3500 so look to at least two of them. */
3501 if (context
.maybe_in_construction
)
3502 for (i
=0 ; i
< MIN (nodes
.length (), 2); i
++)
3503 if (DECL_CXX_DESTRUCTOR_P (nodes
[i
]->decl
))
3504 context
.maybe_in_construction
= false;
3505 if (context
.maybe_in_construction
)
3507 if (type
!= outer_type
3509 || (context
.maybe_derived_type
3510 && !type_all_derivations_known_p (outer_type
->type
))))
3511 record_targets_from_bases (otr_type
, otr_token
, outer_type
->type
,
3512 context
.offset
, nodes
, &inserted
,
3513 &matched_vtables
, &complete
);
3515 maybe_record_node (nodes
, target
, &inserted
, can_refer
, &complete
);
3516 for (i
= 0; i
< bases_to_consider
.length(); i
++)
3517 maybe_record_node (nodes
, bases_to_consider
[i
], &inserted
, can_refer
, &complete
);
3519 bases_to_consider
.release();
3521 (*slot
)->targets
= nodes
;
3522 (*slot
)->complete
= complete
;
3524 *completep
= complete
;
3525 if (speculative_targetsp
)
3526 *speculative_targetsp
= (*slot
)->speculative_targets
;
3528 timevar_pop (TV_IPA_VIRTUAL_CALL
);
3533 add_decl_warning (const tree
&key ATTRIBUTE_UNUSED
, const decl_warn_count
&value
,
3534 vec
<const decl_warn_count
*> *vec
)
3536 vec
->safe_push (&value
);
3540 /* Dump all possible targets of a polymorphic call. */
3543 dump_possible_polymorphic_call_targets (FILE *f
,
3545 HOST_WIDE_INT otr_token
,
3546 const ipa_polymorphic_call_context
&ctx
)
3548 vec
<cgraph_node
*> targets
;
3550 odr_type type
= get_odr_type (TYPE_MAIN_VARIANT (otr_type
), false);
3556 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
,
3558 &final
, NULL
, &speculative
);
3559 fprintf (f
, " Targets of polymorphic call of type %i:", type
->id
);
3560 print_generic_expr (f
, type
->type
, TDF_SLIM
);
3561 fprintf (f
, " token %i\n", (int)otr_token
);
3562 if (ctx
.outer_type
|| ctx
.offset
)
3564 fprintf (f
, " Contained in type:");
3565 print_generic_expr (f
, ctx
.outer_type
, TDF_SLIM
);
3566 fprintf (f
, " at offset "HOST_WIDE_INT_PRINT_DEC
"\n",
3569 if (ctx
.speculative_outer_type
)
3571 fprintf (f
, " Speculatively contained in type:");
3572 print_generic_expr (f
, ctx
.speculative_outer_type
, TDF_SLIM
);
3573 fprintf (f
, " at offset "HOST_WIDE_INT_PRINT_DEC
"\n",
3574 ctx
.speculative_offset
);
3577 fprintf (f
, " %s%s%s%s\n ",
3578 final
? "This is a complete list." :
3579 "This is partial list; extra targets may be defined in other units.",
3580 ctx
.maybe_in_construction
? " (base types included)" : "",
3581 ctx
.maybe_derived_type
? " (derived types included)" : "",
3582 ctx
.speculative_maybe_derived_type
? " (speculative derived types included)" : "");
3583 for (i
= 0; i
< targets
.length (); i
++)
3586 if (i
== (unsigned)speculative
)
3587 fprintf (f
, "\n Targets that are not likely:\n"
3590 name
= cplus_demangle_v3 (targets
[i
]->asm_name (), 0);
3591 fprintf (f
, " %s/%i", name
? name
: targets
[i
]->name (), targets
[i
]->order
);
3594 if (!targets
[i
]->definition
)
3595 fprintf (f
, " (no definition%s)",
3596 DECL_DECLARED_INLINE_P (targets
[i
]->decl
)
3599 fprintf (f
, "\n\n");
3603 /* Return true if N can be possibly target of a polymorphic call of
3604 OTR_TYPE/OTR_TOKEN. */
3607 possible_polymorphic_call_target_p (tree otr_type
,
3608 HOST_WIDE_INT otr_token
,
3609 const ipa_polymorphic_call_context
&ctx
,
3610 struct cgraph_node
*n
)
3612 vec
<cgraph_node
*> targets
;
3614 enum built_in_function fcode
;
3617 if (TREE_CODE (TREE_TYPE (n
->decl
)) == FUNCTION_TYPE
3618 && ((fcode
= DECL_FUNCTION_CODE (n
->decl
))
3619 == BUILT_IN_UNREACHABLE
3620 || fcode
== BUILT_IN_TRAP
))
3625 targets
= possible_polymorphic_call_targets (otr_type
, otr_token
, ctx
, &final
);
3626 for (i
= 0; i
< targets
.length (); i
++)
3627 if (n
->semantically_equivalent_p (targets
[i
]))
3630 /* At a moment we allow middle end to dig out new external declarations
3631 as a targets of polymorphic calls. */
3632 if (!final
&& !n
->definition
)
3638 /* After callgraph construction new external nodes may appear.
3639 Add them into the graph. */
3642 update_type_inheritance_graph (void)
3644 struct cgraph_node
*n
;
3648 free_polymorphic_call_targets_hash ();
3649 timevar_push (TV_IPA_INHERITANCE
);
3650 /* We reconstruct the graph starting from types of all methods seen in the
3652 FOR_EACH_FUNCTION (n
)
3653 if (DECL_VIRTUAL_P (n
->decl
)
3655 && n
->real_symbol_p ())
3656 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n
->decl
))),
3658 timevar_pop (TV_IPA_INHERITANCE
);
3662 /* Return true if N looks like likely target of a polymorphic call.
3663 Rule out cxa_pure_virtual, noreturns, function declared cold and
3664 other obvious cases. */
3667 likely_target_p (struct cgraph_node
*n
)
3670 /* cxa_pure_virtual and similar things are not likely. */
3671 if (TREE_CODE (TREE_TYPE (n
->decl
)) != METHOD_TYPE
)
3673 flags
= flags_from_decl_or_type (n
->decl
);
3674 if (flags
& ECF_NORETURN
)
3676 if (lookup_attribute ("cold",
3677 DECL_ATTRIBUTES (n
->decl
)))
3679 if (n
->frequency
< NODE_FREQUENCY_NORMAL
)
3681 /* If there are no virtual tables refering the target alive,
3682 the only way the target can be called is an instance comming from other
3683 compilation unit; speculative devirtualization is build around an
3684 assumption that won't happen. */
3685 if (!referenced_from_vtable_p (n
))
3690 /* Compare type warning records P1 and P2 and chose one with larger count;
3691 helper for qsort. */
3694 type_warning_cmp (const void *p1
, const void *p2
)
3696 const odr_type_warn_count
*t1
= (const odr_type_warn_count
*)p1
;
3697 const odr_type_warn_count
*t2
= (const odr_type_warn_count
*)p2
;
3699 if (t1
->dyn_count
< t2
->dyn_count
)
3701 if (t1
->dyn_count
> t2
->dyn_count
)
3703 return t2
->count
- t1
->count
;
3706 /* Compare decl warning records P1 and P2 and chose one with larger count;
3707 helper for qsort. */
3710 decl_warning_cmp (const void *p1
, const void *p2
)
3712 const decl_warn_count
*t1
= *(const decl_warn_count
* const *)p1
;
3713 const decl_warn_count
*t2
= *(const decl_warn_count
* const *)p2
;
3715 if (t1
->dyn_count
< t2
->dyn_count
)
3717 if (t1
->dyn_count
> t2
->dyn_count
)
3719 return t2
->count
- t1
->count
;
3722 /* The ipa-devirt pass.
3723 When polymorphic call has only one likely target in the unit,
3724 turn it into speculative call. */
3729 struct cgraph_node
*n
;
3730 hash_set
<void *> bad_call_targets
;
3731 struct cgraph_edge
*e
;
3733 int npolymorphic
= 0, nspeculated
= 0, nconverted
= 0, ncold
= 0;
3734 int nmultiple
= 0, noverwritable
= 0, ndevirtualized
= 0, nnotdefined
= 0;
3735 int nwrong
= 0, nok
= 0, nexternal
= 0, nartificial
= 0;
3737 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3738 This is implemented by setting up final_warning_records that are updated
3739 by get_polymorphic_call_targets.
3740 We need to clear cache in this case to trigger recomputation of all
3742 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3744 final_warning_records
= new (final_warning_record
);
3745 final_warning_records
->type_warnings
= vNULL
;
3746 final_warning_records
->type_warnings
.safe_grow_cleared (odr_types
.length ());
3747 free_polymorphic_call_targets_hash ();
3750 FOR_EACH_DEFINED_FUNCTION (n
)
3752 bool update
= false;
3753 if (dump_file
&& n
->indirect_calls
)
3754 fprintf (dump_file
, "\n\nProcesing function %s/%i\n",
3755 n
->name (), n
->order
);
3756 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
3757 if (e
->indirect_info
->polymorphic
)
3759 struct cgraph_node
*likely_target
= NULL
;
3762 int speculative_targets
;
3764 if (final_warning_records
)
3765 final_warning_records
->dyn_count
= e
->count
;
3767 vec
<cgraph_node
*>targets
3768 = possible_polymorphic_call_targets
3769 (e
, &final
, &cache_token
, &speculative_targets
);
3773 dump_possible_polymorphic_call_targets
3778 if (!flag_devirtualize_speculatively
)
3781 if (!cgraph_maybe_hot_edge_p (e
))
3784 fprintf (dump_file
, "Call is cold\n\n");
3791 fprintf (dump_file
, "Call is aready speculated\n\n");
3794 /* When dumping see if we agree with speculation. */
3798 if (bad_call_targets
.contains (cache_token
))
3801 fprintf (dump_file
, "Target list is known to be useless\n\n");
3805 for (i
= 0; i
< targets
.length (); i
++)
3806 if (likely_target_p (targets
[i
]))
3810 if (i
< (unsigned) speculative_targets
)
3812 likely_target
= NULL
;
3814 fprintf (dump_file
, "More than one likely target\n\n");
3819 likely_target
= targets
[i
];
3823 bad_call_targets
.add (cache_token
);
3826 /* This is reached only when dumping; check if we agree or disagree
3827 with the speculation. */
3830 struct cgraph_edge
*e2
;
3831 struct ipa_ref
*ref
;
3832 cgraph_speculative_call_info (e
, e2
, e
, ref
);
3833 if (e2
->callee
->ultimate_alias_target ()
3834 == likely_target
->ultimate_alias_target ())
3836 fprintf (dump_file
, "We agree with speculation\n\n");
3841 fprintf (dump_file
, "We disagree with speculation\n\n");
3846 if (!likely_target
->definition
)
3849 fprintf (dump_file
, "Target is not an definition\n\n");
3853 /* Do not introduce new references to external symbols. While we
3854 can handle these just well, it is common for programs to
3855 incorrectly with headers defining methods they are linked
3857 if (DECL_EXTERNAL (likely_target
->decl
))
3860 fprintf (dump_file
, "Target is external\n\n");
3864 /* Don't use an implicitly-declared destructor (c++/58678). */
3865 struct cgraph_node
*non_thunk_target
3866 = likely_target
->function_symbol ();
3867 if (DECL_ARTIFICIAL (non_thunk_target
->decl
)
3868 && DECL_COMDAT (non_thunk_target
->decl
))
3871 fprintf (dump_file
, "Target is artificial\n\n");
3875 if (likely_target
->get_availability () <= AVAIL_INTERPOSABLE
3876 && likely_target
->can_be_discarded_p ())
3879 fprintf (dump_file
, "Target is overwritable\n\n");
3883 else if (dbg_cnt (devirt
))
3885 if (dump_enabled_p ())
3887 location_t locus
= gimple_location_safe (e
->call_stmt
);
3888 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
3889 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3890 n
->name (), n
->order
,
3891 likely_target
->name (),
3892 likely_target
->order
);
3894 if (!likely_target
->can_be_discarded_p ())
3897 alias
= dyn_cast
<cgraph_node
*> (likely_target
->noninterposable_alias ());
3899 likely_target
= alias
;
3903 cgraph_turn_edge_to_speculative
3904 (e
, likely_target
, e
->count
* 8 / 10, e
->frequency
* 8 / 10);
3908 inline_update_overall_summary (n
);
3910 if (warn_suggest_final_methods
|| warn_suggest_final_types
)
3912 if (warn_suggest_final_types
)
3914 final_warning_records
->type_warnings
.qsort (type_warning_cmp
);
3915 for (unsigned int i
= 0;
3916 i
< final_warning_records
->type_warnings
.length (); i
++)
3917 if (final_warning_records
->type_warnings
[i
].count
)
3919 tree type
= final_warning_records
->type_warnings
[i
].type
;
3920 warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
3921 OPT_Wsuggest_final_types
,
3922 "Declaring type %qD final "
3923 "would enable devirtualization of %i calls",
3925 final_warning_records
->type_warnings
[i
].count
);
3929 if (warn_suggest_final_methods
)
3931 vec
<const decl_warn_count
*> decl_warnings_vec
= vNULL
;
3933 final_warning_records
->decl_warnings
.traverse
3934 <vec
<const decl_warn_count
*> *, add_decl_warning
> (&decl_warnings_vec
);
3935 decl_warnings_vec
.qsort (decl_warning_cmp
);
3936 for (unsigned int i
= 0; i
< decl_warnings_vec
.length (); i
++)
3938 tree decl
= decl_warnings_vec
[i
]->decl
;
3939 int count
= decl_warnings_vec
[i
]->count
;
3941 if (DECL_CXX_DESTRUCTOR_P (decl
))
3942 warning_at (DECL_SOURCE_LOCATION (decl
),
3943 OPT_Wsuggest_final_methods
,
3944 "Declaring virtual destructor of %qD final "
3945 "would enable devirtualization of %i calls",
3946 DECL_CONTEXT (decl
), count
);
3948 warning_at (DECL_SOURCE_LOCATION (decl
),
3949 OPT_Wsuggest_final_methods
,
3950 "Declaring method %qD final "
3951 "would enable devirtualization of %i calls",
3956 delete (final_warning_records
);
3957 final_warning_records
= 0;
3962 "%i polymorphic calls, %i devirtualized,"
3963 " %i speculatively devirtualized, %i cold\n"
3964 "%i have multiple targets, %i overwritable,"
3965 " %i already speculated (%i agree, %i disagree),"
3966 " %i external, %i not defined, %i artificial\n",
3967 npolymorphic
, ndevirtualized
, nconverted
, ncold
,
3968 nmultiple
, noverwritable
, nspeculated
, nok
, nwrong
,
3969 nexternal
, nnotdefined
, nartificial
);
3970 return ndevirtualized
? TODO_remove_functions
: 0;
3975 const pass_data pass_data_ipa_devirt
=
3977 IPA_PASS
, /* type */
3978 "devirt", /* name */
3979 OPTGROUP_NONE
, /* optinfo_flags */
3980 TV_IPA_DEVIRT
, /* tv_id */
3981 0, /* properties_required */
3982 0, /* properties_provided */
3983 0, /* properties_destroyed */
3984 0, /* todo_flags_start */
3985 ( TODO_dump_symtab
), /* todo_flags_finish */
3988 class pass_ipa_devirt
: public ipa_opt_pass_d
3991 pass_ipa_devirt (gcc::context
*ctxt
)
3992 : ipa_opt_pass_d (pass_data_ipa_devirt
, ctxt
,
3993 NULL
, /* generate_summary */
3994 NULL
, /* write_summary */
3995 NULL
, /* read_summary */
3996 NULL
, /* write_optimization_summary */
3997 NULL
, /* read_optimization_summary */
3998 NULL
, /* stmt_fixup */
3999 0, /* function_transform_todo_flags_start */
4000 NULL
, /* function_transform */
4001 NULL
) /* variable_transform */
4004 /* opt_pass methods: */
4005 virtual bool gate (function
*)
4007 return (flag_devirtualize
4008 && (flag_devirtualize_speculatively
4009 || (warn_suggest_final_methods
4010 || warn_suggest_final_types
))
4014 virtual unsigned int execute (function
*) { return ipa_devirt (); }
4016 }; // class pass_ipa_devirt
4021 make_pass_ipa_devirt (gcc::context
*ctxt
)
4023 return new pass_ipa_devirt (ctxt
);
4026 #include "gt-ipa-devirt.h"