2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-devirt.c
blobd7d97ec2422fa9f0695cd140dfaf2d3bfa8fa845
1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2015 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
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
16 for more details.
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/>. */
22 /* Brief vocabulary:
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.
39 OTR = OBJ_TYPE_REF
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.
45 BINFO
46 This is the type inheritance information attached to each tree
47 RECORD_TYPE by the C++ frontend. It provides information about base
48 types and virtual tables.
50 BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
51 BINFO also links to its type by BINFO_TYPE and to the virtual table by
52 BINFO_VTABLE.
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
70 base type.
72 token
73 This is an index of virtual method in virtual table associated
74 to the type defining it. Token can be looked up from OBJ_TYPE_REF
75 or from DECL_VINDEX of a given virtual table.
77 polymorphic (indirect) call
78 This is callgraph representation of virtual method call. Every
79 polymorphic call contains otr_type and otr_token taken from
80 original OBJ_TYPE_REF at callgraph construction time.
82 What we do here:
84 build_type_inheritance_graph triggers a construction of the type inheritance
85 graph.
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.
100 Adding this is easy.
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.
108 #include "config.h"
109 #include "system.h"
110 #include "coretypes.h"
111 #include "tm.h"
112 #include "input.h"
113 #include "alias.h"
114 #include "symtab.h"
115 #include "tree.h"
116 #include "fold-const.h"
117 #include "print-tree.h"
118 #include "calls.h"
119 #include "predict.h"
120 #include "basic-block.h"
121 #include "is-a.h"
122 #include "plugin-api.h"
123 #include "hard-reg-set.h"
124 #include "function.h"
125 #include "ipa-ref.h"
126 #include "cgraph.h"
127 #include "rtl.h"
128 #include "flags.h"
129 #include "insn-config.h"
130 #include "expmed.h"
131 #include "dojump.h"
132 #include "explow.h"
133 #include "emit-rtl.h"
134 #include "varasm.h"
135 #include "stmt.h"
136 #include "expr.h"
137 #include "tree-pass.h"
138 #include "target.h"
139 #include "tree-pretty-print.h"
140 #include "ipa-utils.h"
141 #include "tree-ssa-alias.h"
142 #include "internal-fn.h"
143 #include "gimple-fold.h"
144 #include "gimple-expr.h"
145 #include "gimple.h"
146 #include "alloc-pool.h"
147 #include "symbol-summary.h"
148 #include "ipa-prop.h"
149 #include "ipa-inline.h"
150 #include "diagnostic.h"
151 #include "tree-dfa.h"
152 #include "demangle.h"
153 #include "dbgcnt.h"
154 #include "gimple-pretty-print.h"
155 #include "stor-layout.h"
156 #include "intl.h"
157 #include "streamer-hooks.h"
158 #include "lto-streamer.h"
160 /* Hash based set of pairs of types. */
161 typedef struct
163 tree first;
164 tree second;
165 } type_pair;
167 struct pair_traits : default_hashset_traits
169 static hashval_t
170 hash (type_pair p)
172 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
174 static bool
175 is_empty (type_pair p)
177 return p.first == NULL;
179 static bool
180 is_deleted (type_pair p ATTRIBUTE_UNUSED)
182 return false;
184 static bool
185 equal (const type_pair &a, const type_pair &b)
187 return a.first==b.first && a.second == b.second;
189 static void
190 mark_empty (type_pair &e)
192 e.first = NULL;
196 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
197 hash_set<type_pair,pair_traits> *,
198 location_t, location_t);
200 static bool odr_violation_reported = false;
203 /* Pointer set of all call targets appearing in the cache. */
204 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
206 /* The node of type inheritance graph. For each type unique in
207 One Definition Rule (ODR) sense, we produce one node linking all
208 main variants of types equivalent to it, bases and derived types. */
210 struct GTY(()) odr_type_d
212 /* leader type. */
213 tree type;
214 /* All bases; built only for main variants of types. */
215 vec<odr_type> GTY((skip)) bases;
216 /* All derived types with virtual methods seen in unit;
217 built only for main variants of types. */
218 vec<odr_type> GTY((skip)) derived_types;
220 /* All equivalent types, if more than one. */
221 vec<tree, va_gc> *types;
222 /* Set of all equivalent types, if NON-NULL. */
223 hash_set<tree> * GTY((skip)) types_set;
225 /* Unique ID indexing the type in odr_types array. */
226 int id;
227 /* Is it in anonymous namespace? */
228 bool anonymous_namespace;
229 /* Do we know about all derivations of given type? */
230 bool all_derivations_known;
231 /* Did we report ODR violation here? */
232 bool odr_violated;
233 /* Set when virtual table without RTTI previaled table with. */
234 bool rtti_broken;
237 /* Return true if T is a type with linkage defined. */
239 bool
240 type_with_linkage_p (const_tree t)
242 /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL. */
243 if (!TYPE_CONTEXT (t)
244 || !TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL
245 || !TYPE_STUB_DECL (t))
246 return false;
248 /* In LTO do not get confused by non-C++ produced types or types built
249 with -fno-lto-odr-type-merigng. */
250 if (in_lto_p)
252 /* To support -fno-lto-odr-type-merigng recognize types with vtables
253 to have linkage. */
254 if (RECORD_OR_UNION_TYPE_P (t)
255 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
256 return true;
257 /* Do not accept any other types - we do not know if they were produced
258 by C++ FE. */
259 if (!DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
260 return false;
263 return (RECORD_OR_UNION_TYPE_P (t)
264 || TREE_CODE (t) == ENUMERAL_TYPE);
267 /* Return true if T is in anonymous namespace.
268 This works only on those C++ types with linkage defined. */
270 bool
271 type_in_anonymous_namespace_p (const_tree t)
273 gcc_assert (type_with_linkage_p (t));
275 /* Keep -fno-lto-odr-type-merging working by recognizing classes with vtables
276 properly into anonymous namespaces. */
277 if (RECORD_OR_UNION_TYPE_P (t)
278 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
279 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
281 if (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)))
283 /* C++ FE uses magic <anon> as assembler names of anonymous types.
284 verify that this match with type_in_anonymous_namespace_p. */
285 #ifdef ENABLE_CHECKING
286 if (in_lto_p)
287 gcc_assert (!strcmp ("<anon>",
288 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t)))));
289 #endif
290 return true;
292 return false;
295 /* Return true of T is type with One Definition Rule info attached.
296 It means that either it is anonymous type or it has assembler name
297 set. */
299 bool
300 odr_type_p (const_tree t)
302 /* We do not have this information when not in LTO, but we do not need
303 to care, since it is used only for type merging. */
304 gcc_checking_assert (in_lto_p || flag_lto);
306 /* To support -fno-lto-odr-type-merging consider types with vtables ODR. */
307 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
308 return true;
310 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
311 && (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))))
313 #ifdef ENABLE_CHECKING
314 /* C++ FE uses magic <anon> as assembler names of anonymous types.
315 verify that this match with type_in_anonymous_namespace_p. */
316 gcc_assert (!type_with_linkage_p (t)
317 || strcmp ("<anon>",
318 IDENTIFIER_POINTER
319 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))))
320 || type_in_anonymous_namespace_p (t));
321 #endif
322 return true;
324 return false;
327 /* Return TRUE if all derived types of T are known and thus
328 we may consider the walk of derived type complete.
330 This is typically true only for final anonymous namespace types and types
331 defined within functions (that may be COMDAT and thus shared across units,
332 but with the same set of derived types). */
334 bool
335 type_all_derivations_known_p (const_tree t)
337 if (TYPE_FINAL_P (t))
338 return true;
339 if (flag_ltrans)
340 return false;
341 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
342 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
343 return true;
344 if (type_in_anonymous_namespace_p (t))
345 return true;
346 return (decl_function_context (TYPE_NAME (t)) != NULL);
349 /* Return TRUE if type's constructors are all visible. */
351 static bool
352 type_all_ctors_visible_p (tree t)
354 return !flag_ltrans
355 && symtab->state >= CONSTRUCTION
356 /* We can not always use type_all_derivations_known_p.
357 For function local types we must assume case where
358 the function is COMDAT and shared in between units.
360 TODO: These cases are quite easy to get, but we need
361 to keep track of C++ privatizing via -Wno-weak
362 as well as the IPA privatizing. */
363 && type_in_anonymous_namespace_p (t);
366 /* Return TRUE if type may have instance. */
368 static bool
369 type_possibly_instantiated_p (tree t)
371 tree vtable;
372 varpool_node *vnode;
374 /* TODO: Add abstract types here. */
375 if (!type_all_ctors_visible_p (t))
376 return true;
378 vtable = BINFO_VTABLE (TYPE_BINFO (t));
379 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
380 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
381 vnode = varpool_node::get (vtable);
382 return vnode && vnode->definition;
385 /* Hash used to unify ODR types based on their mangled name and for anonymous
386 namespace types. */
388 struct odr_name_hasher
390 typedef odr_type_d *value_type;
391 typedef union tree_node *compare_type;
392 static inline hashval_t hash (const odr_type_d *);
393 static inline bool equal (const odr_type_d *, const tree_node *);
394 static inline void remove (odr_type_d *);
397 /* Has used to unify ODR types based on their associated virtual table.
398 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
399 only polymorphic types. Types with mangled names are inserted to both. */
401 struct odr_vtable_hasher:odr_name_hasher
403 static inline hashval_t hash (const odr_type_d *);
404 static inline bool equal (const odr_type_d *, const tree_node *);
407 /* Return type that was declared with T's name so that T is an
408 qualified variant of it. */
410 static inline tree
411 main_odr_variant (const_tree t)
413 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
414 return TREE_TYPE (TYPE_NAME (t));
415 /* Unnamed types and non-C++ produced types can be compared by variants. */
416 else
417 return TYPE_MAIN_VARIANT (t);
420 static bool
421 can_be_name_hashed_p (tree t)
423 return (!in_lto_p || odr_type_p (t));
426 /* Hash type by its ODR name. */
428 static hashval_t
429 hash_odr_name (const_tree t)
431 gcc_checking_assert (main_odr_variant (t) == t);
433 /* If not in LTO, all main variants are unique, so we can do
434 pointer hash. */
435 if (!in_lto_p)
436 return htab_hash_pointer (t);
438 /* Anonymous types are unique. */
439 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
440 return htab_hash_pointer (t);
442 gcc_checking_assert (TYPE_NAME (t)
443 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
444 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
447 /* Return the computed hashcode for ODR_TYPE. */
449 inline hashval_t
450 odr_name_hasher::hash (const odr_type_d *odr_type)
452 return hash_odr_name (odr_type->type);
455 static bool
456 can_be_vtable_hashed_p (tree t)
458 /* vtable hashing can distinguish only main variants. */
459 if (TYPE_MAIN_VARIANT (t) != t)
460 return false;
461 /* Anonymous namespace types are always handled by name hash. */
462 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
463 return false;
464 return (TREE_CODE (t) == RECORD_TYPE
465 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
468 /* Hash type by assembler name of its vtable. */
470 static hashval_t
471 hash_odr_vtable (const_tree t)
473 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
474 inchash::hash hstate;
476 gcc_checking_assert (in_lto_p);
477 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
478 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
479 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
480 gcc_checking_assert (main_odr_variant (t) == t);
482 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
484 add_expr (TREE_OPERAND (v, 1), hstate);
485 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
488 hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
489 return hstate.end ();
492 /* Return the computed hashcode for ODR_TYPE. */
494 inline hashval_t
495 odr_vtable_hasher::hash (const odr_type_d *odr_type)
497 return hash_odr_vtable (odr_type->type);
500 /* For languages with One Definition Rule, work out if
501 types are the same based on their name.
503 This is non-trivial for LTO where minor differences in
504 the type representation may have prevented type merging
505 to merge two copies of otherwise equivalent type.
507 Until we start streaming mangled type names, this function works
508 only for polymorphic types.
510 When STRICT is true, we compare types by their names for purposes of
511 ODR violation warnings. When strict is false, we consider variants
512 equivalent, becuase it is all that matters for devirtualization machinery.
515 bool
516 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
518 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
520 type1 = main_odr_variant (type1);
521 type2 = main_odr_variant (type2);
522 if (!strict)
524 type1 = TYPE_MAIN_VARIANT (type1);
525 type2 = TYPE_MAIN_VARIANT (type2);
528 if (type1 == type2)
529 return true;
531 if (!in_lto_p)
532 return false;
534 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
535 on the corresponding TYPE_STUB_DECL. */
536 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
537 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
538 return false;
541 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
543 Ideally we should never need types without ODR names here. It can however
544 happen in two cases:
546 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
547 Here testing for equivalence is safe, since their MAIN_VARIANTs are
548 unique.
549 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
550 establish precise ODR equivalency, but for correctness we care only
551 about equivalency on complete polymorphic types. For these we can
552 compare assembler names of their virtual tables. */
553 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
554 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
556 /* See if types are obviously different (i.e. different codes
557 or polymorphic wrt non-polymorphic). This is not strictly correct
558 for ODR violating programs, but we can't do better without streaming
559 ODR names. */
560 if (TREE_CODE (type1) != TREE_CODE (type2))
561 return false;
562 if (TREE_CODE (type1) == RECORD_TYPE
563 && (TYPE_BINFO (type1) == NULL_TREE)
564 != (TYPE_BINFO (type1) == NULL_TREE))
565 return false;
566 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
567 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
568 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
569 return false;
571 /* At the moment we have no way to establish ODR equivalence at LTO
572 other than comparing virtual table pointers of polymorphic types.
573 Eventually we should start saving mangled names in TYPE_NAME.
574 Then this condition will become non-trivial. */
576 if (TREE_CODE (type1) == RECORD_TYPE
577 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
578 && BINFO_VTABLE (TYPE_BINFO (type1))
579 && BINFO_VTABLE (TYPE_BINFO (type2)))
581 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
582 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
583 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
584 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
585 return (operand_equal_p (TREE_OPERAND (v1, 1),
586 TREE_OPERAND (v2, 1), 0)
587 && DECL_ASSEMBLER_NAME
588 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
589 == DECL_ASSEMBLER_NAME
590 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
592 gcc_unreachable ();
594 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
595 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
598 /* Return true if we can decide on ODR equivalency.
600 In non-LTO it is always decide, in LTO however it depends in the type has
601 ODR info attached.
603 When STRICT is false, compare main variants. */
605 bool
606 types_odr_comparable (tree t1, tree t2, bool strict)
608 return (!in_lto_p
609 || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
610 && main_odr_variant (t1))
611 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
612 || (odr_type_p (t1) && odr_type_p (t2))
613 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
614 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
615 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
616 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
619 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
620 known, be conservative and return false. */
622 bool
623 types_must_be_same_for_odr (tree t1, tree t2)
625 if (types_odr_comparable (t1, t2))
626 return types_same_for_odr (t1, t2);
627 else
628 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
631 /* If T is compound type, return type it is based on. */
633 static tree
634 compound_type_base (const_tree t)
636 if (TREE_CODE (t) == ARRAY_TYPE
637 || POINTER_TYPE_P (t)
638 || TREE_CODE (t) == COMPLEX_TYPE
639 || VECTOR_TYPE_P (t))
640 return TREE_TYPE (t);
641 if (TREE_CODE (t) == METHOD_TYPE)
642 return TYPE_METHOD_BASETYPE (t);
643 if (TREE_CODE (t) == OFFSET_TYPE)
644 return TYPE_OFFSET_BASETYPE (t);
645 return NULL_TREE;
648 /* Return true if T is either ODR type or compound type based from it.
649 If the function return true, we know that T is a type originating from C++
650 source even at link-time. */
652 bool
653 odr_or_derived_type_p (const_tree t)
657 if (odr_type_p (t))
658 return true;
659 /* Function type is a tricky one. Basically we can consider it
660 ODR derived if return type or any of the parameters is.
661 We need to check all parameters because LTO streaming merges
662 common types (such as void) and they are not considered ODR then. */
663 if (TREE_CODE (t) == FUNCTION_TYPE)
665 if (TYPE_METHOD_BASETYPE (t))
666 t = TYPE_METHOD_BASETYPE (t);
667 else
669 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
670 return true;
671 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
672 if (odr_or_derived_type_p (TREE_VALUE (t)))
673 return true;
674 return false;
677 else
678 t = compound_type_base (t);
680 while (t);
681 return t;
684 /* Compare types T1 and T2 and return true if they are
685 equivalent. */
687 inline bool
688 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
690 tree t1 = o1->type;
692 gcc_checking_assert (main_odr_variant (t2) == t2);
693 gcc_checking_assert (main_odr_variant (t1) == t1);
694 if (t1 == t2)
695 return true;
696 if (!in_lto_p)
697 return false;
698 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
699 on the corresponding TYPE_STUB_DECL. */
700 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
701 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
702 return false;
703 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
704 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
705 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
706 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
709 /* Compare types T1 and T2 and return true if they are
710 equivalent. */
712 inline bool
713 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
715 tree t1 = o1->type;
717 gcc_checking_assert (main_odr_variant (t2) == t2);
718 gcc_checking_assert (main_odr_variant (t1) == t1);
719 gcc_checking_assert (in_lto_p);
720 t1 = TYPE_MAIN_VARIANT (t1);
721 t2 = TYPE_MAIN_VARIANT (t2);
722 if (t1 == t2)
723 return true;
724 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
725 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
726 return (operand_equal_p (TREE_OPERAND (v1, 1),
727 TREE_OPERAND (v2, 1), 0)
728 && DECL_ASSEMBLER_NAME
729 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
730 == DECL_ASSEMBLER_NAME
731 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
734 /* Free ODR type V. */
736 inline void
737 odr_name_hasher::remove (odr_type_d *v)
739 v->bases.release ();
740 v->derived_types.release ();
741 if (v->types_set)
742 delete v->types_set;
743 ggc_free (v);
746 /* ODR type hash used to look up ODR type based on tree type node. */
748 typedef hash_table<odr_name_hasher> odr_hash_type;
749 static odr_hash_type *odr_hash;
750 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
751 static odr_vtable_hash_type *odr_vtable_hash;
753 /* ODR types are also stored into ODR_TYPE vector to allow consistent
754 walking. Bases appear before derived types. Vector is garbage collected
755 so we won't end up visiting empty types. */
757 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
758 #define odr_types (*odr_types_ptr)
760 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
761 void
762 set_type_binfo (tree type, tree binfo)
764 for (; type; type = TYPE_NEXT_VARIANT (type))
765 if (COMPLETE_TYPE_P (type))
766 TYPE_BINFO (type) = binfo;
767 else
768 gcc_assert (!TYPE_BINFO (type));
771 /* Compare T2 and T2 based on name or structure. */
773 static bool
774 odr_subtypes_equivalent_p (tree t1, tree t2,
775 hash_set<type_pair,pair_traits> *visited,
776 location_t loc1, location_t loc2)
779 /* This can happen in incomplete types that should be handled earlier. */
780 gcc_assert (t1 && t2);
782 t1 = main_odr_variant (t1);
783 t2 = main_odr_variant (t2);
784 if (t1 == t2)
785 return true;
787 /* Anonymous namespace types must match exactly. */
788 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
789 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
790 return false;
792 /* For ODR types be sure to compare their names.
793 To support -wno-odr-type-merging we allow one type to be non-ODR
794 and other ODR even though it is a violation. */
795 if (types_odr_comparable (t1, t2, true))
797 if (!types_same_for_odr (t1, t2, true))
798 return false;
799 /* Limit recursion: If subtypes are ODR types and we know
800 that they are same, be happy. */
801 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated)
802 return true;
805 /* Component types, builtins and possibly violating ODR types
806 have to be compared structurally. */
807 if (TREE_CODE (t1) != TREE_CODE (t2))
808 return false;
809 if (AGGREGATE_TYPE_P (t1)
810 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
811 return false;
813 type_pair pair={t1,t2};
814 if (TYPE_UID (t1) > TYPE_UID (t2))
816 pair.first = t2;
817 pair.second = t1;
819 if (visited->add (pair))
820 return true;
821 return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
824 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
825 violation warnings. */
827 void
828 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
830 int n1, n2;
832 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
834 odr_violation_reported = true;
835 if (DECL_VIRTUAL_P (prevailing->decl))
837 varpool_node *tmp = prevailing;
838 prevailing = vtable;
839 vtable = tmp;
841 if (warning_at (DECL_SOURCE_LOCATION
842 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
843 OPT_Wodr,
844 "virtual table of type %qD violates one definition rule",
845 DECL_CONTEXT (vtable->decl)))
846 inform (DECL_SOURCE_LOCATION (prevailing->decl),
847 "variable of same assembler name as the virtual table is "
848 "defined in another translation unit");
849 return;
851 if (!prevailing->definition || !vtable->definition)
852 return;
854 /* If we do not stream ODR type info, do not bother to do useful compare. */
855 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
856 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
857 return;
859 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
861 if (class_type->odr_violated)
862 return;
864 for (n1 = 0, n2 = 0; true; n1++, n2++)
866 struct ipa_ref *ref1, *ref2;
867 bool end1, end2;
869 end1 = !prevailing->iterate_reference (n1, ref1);
870 end2 = !vtable->iterate_reference (n2, ref2);
872 /* !DECL_VIRTUAL_P means RTTI entry;
873 We warn when RTTI is lost because non-RTTI previals; we silently
874 accept the other case. */
875 while (!end2
876 && (end1
877 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
878 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
879 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
880 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
882 if (!class_type->rtti_broken
883 && warning_at (DECL_SOURCE_LOCATION
884 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
885 OPT_Wodr,
886 "virtual table of type %qD contains RTTI "
887 "information",
888 DECL_CONTEXT (vtable->decl)))
890 inform (DECL_SOURCE_LOCATION
891 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
892 "but is prevailed by one without from other translation "
893 "unit");
894 inform (DECL_SOURCE_LOCATION
895 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
896 "RTTI will not work on this type");
897 class_type->rtti_broken = true;
899 n2++;
900 end2 = !vtable->iterate_reference (n2, ref2);
902 while (!end1
903 && (end2
904 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
905 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
906 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
907 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
909 n1++;
910 end1 = !prevailing->iterate_reference (n1, ref1);
913 /* Finished? */
914 if (end1 && end2)
916 /* Extra paranoia; compare the sizes. We do not have information
917 about virtual inheritance offsets, so just be sure that these
918 match.
919 Do this as very last check so the not very informative error
920 is not output too often. */
921 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
923 class_type->odr_violated = true;
924 if (warning_at (DECL_SOURCE_LOCATION
925 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
926 OPT_Wodr,
927 "virtual table of type %qD violates "
928 "one definition rule ",
929 DECL_CONTEXT (vtable->decl)))
931 inform (DECL_SOURCE_LOCATION
932 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
933 "the conflicting type defined in another translation "
934 "unit has virtual table of different size");
937 return;
940 if (!end1 && !end2)
942 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
943 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
944 continue;
946 class_type->odr_violated = true;
948 /* If the loops above stopped on non-virtual pointer, we have
949 mismatch in RTTI information mangling. */
950 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
951 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
953 if (warning_at (DECL_SOURCE_LOCATION
954 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
955 OPT_Wodr,
956 "virtual table of type %qD violates "
957 "one definition rule ",
958 DECL_CONTEXT (vtable->decl)))
960 inform (DECL_SOURCE_LOCATION
961 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
962 "the conflicting type defined in another translation "
963 "unit with different RTTI information");
965 return;
967 /* At this point both REF1 and REF2 points either to virtual table
968 or virtual method. If one points to virtual table and other to
969 method we can complain the same way as if one table was shorter
970 than other pointing out the extra method. */
971 if (TREE_CODE (ref1->referred->decl)
972 != TREE_CODE (ref2->referred->decl))
974 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
975 end1 = true;
976 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
977 end2 = true;
981 class_type->odr_violated = true;
983 /* Complain about size mismatch. Either we have too many virutal
984 functions or too many virtual table pointers. */
985 if (end1 || end2)
987 if (end1)
989 varpool_node *tmp = prevailing;
990 prevailing = vtable;
991 vtable = tmp;
992 ref1 = ref2;
994 if (warning_at (DECL_SOURCE_LOCATION
995 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
996 OPT_Wodr,
997 "virtual table of type %qD violates "
998 "one definition rule",
999 DECL_CONTEXT (vtable->decl)))
1001 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
1003 inform (DECL_SOURCE_LOCATION
1004 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1005 "the conflicting type defined in another translation "
1006 "unit");
1007 inform (DECL_SOURCE_LOCATION
1008 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
1009 "contains additional virtual method %qD",
1010 ref1->referred->decl);
1012 else
1014 inform (DECL_SOURCE_LOCATION
1015 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1016 "the conflicting type defined in another translation "
1017 "unit has virtual table table with more entries");
1020 return;
1023 /* And in the last case we have either mistmatch in between two virtual
1024 methods or two virtual table pointers. */
1025 if (warning_at (DECL_SOURCE_LOCATION
1026 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
1027 "virtual table of type %qD violates "
1028 "one definition rule ",
1029 DECL_CONTEXT (vtable->decl)))
1031 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
1033 inform (DECL_SOURCE_LOCATION
1034 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1035 "the conflicting type defined in another translation "
1036 "unit");
1037 gcc_assert (TREE_CODE (ref2->referred->decl)
1038 == FUNCTION_DECL);
1039 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
1040 "virtual method %qD", ref1->referred->decl);
1041 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
1042 "ought to match virtual method %qD but does not",
1043 ref2->referred->decl);
1045 else
1046 inform (DECL_SOURCE_LOCATION
1047 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1048 "the conflicting type defined in another translation "
1049 "unit has virtual table table with different contents");
1050 return;
1055 /* Output ODR violation warning about T1 and T2 with REASON.
1056 Display location of ST1 and ST2 if REASON speaks about field or
1057 method of the type.
1058 If WARN is false, do nothing. Set WARNED if warning was indeed
1059 output. */
1061 void
1062 warn_odr (tree t1, tree t2, tree st1, tree st2,
1063 bool warn, bool *warned, const char *reason)
1065 tree decl2 = TYPE_NAME (t2);
1066 if (warned)
1067 *warned = false;
1069 if (!warn || !TYPE_NAME(t1))
1070 return;
1072 /* ODR warnings are output druing LTO streaming; we must apply location
1073 cache for potential warnings to be output correctly. */
1074 if (lto_location_cache::current_cache)
1075 lto_location_cache::current_cache->apply_location_cache ();
1077 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
1078 "type %qT violates the C++ One Definition Rule",
1079 t1))
1080 return;
1081 if (!st1 && !st2)
1083 /* For FIELD_DECL support also case where one of fields is
1084 NULL - this is used when the structures have mismatching number of
1085 elements. */
1086 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
1088 inform (DECL_SOURCE_LOCATION (decl2),
1089 "a different type is defined in another translation unit");
1090 if (!st1)
1092 st1 = st2;
1093 st2 = NULL;
1095 inform (DECL_SOURCE_LOCATION (st1),
1096 "the first difference of corresponding definitions is field %qD",
1097 st1);
1098 if (st2)
1099 decl2 = st2;
1101 else if (TREE_CODE (st1) == FUNCTION_DECL)
1103 inform (DECL_SOURCE_LOCATION (decl2),
1104 "a different type is defined in another translation unit");
1105 inform (DECL_SOURCE_LOCATION (st1),
1106 "the first difference of corresponding definitions is method %qD",
1107 st1);
1108 decl2 = st2;
1110 else
1111 return;
1112 inform (DECL_SOURCE_LOCATION (decl2), reason);
1114 if (warned)
1115 *warned = true;
1118 /* Return ture if T1 and T2 are incompatible and we want to recusively
1119 dive into them from warn_type_mismatch to give sensible answer. */
1121 static bool
1122 type_mismatch_p (tree t1, tree t2)
1124 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1125 && !odr_types_equivalent_p (t1, t2))
1126 return true;
1127 return !types_compatible_p (t1, t2);
1131 /* Types T1 and T2 was found to be incompatible in a context they can't
1132 (either used to declare a symbol of same assembler name or unified by
1133 ODR rule). We already output warning about this, but if possible, output
1134 extra information on how the types mismatch.
1136 This is hard to do in general. We basically handle the common cases.
1138 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1139 themselves do no thave one.*/
1141 void
1142 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1144 /* Location of type is known only if it has TYPE_NAME and the name is
1145 TYPE_DECL. */
1146 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1147 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1148 : UNKNOWN_LOCATION;
1149 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1150 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1151 : UNKNOWN_LOCATION;
1152 bool loc_t2_useful = false;
1154 /* With LTO it is a common case that the location of both types match.
1155 See if T2 has a location that is different from T1. If so, we will
1156 inform user about the location.
1157 Do not consider the location passed to us in LOC1/LOC2 as those are
1158 already output. */
1159 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1161 if (loc_t1 <= BUILTINS_LOCATION)
1162 loc_t2_useful = true;
1163 else
1165 expanded_location xloc1 = expand_location (loc_t1);
1166 expanded_location xloc2 = expand_location (loc_t2);
1168 if (strcmp (xloc1.file, xloc2.file)
1169 || xloc1.line != xloc2.line
1170 || xloc1.column != xloc2.column)
1171 loc_t2_useful = true;
1175 if (loc_t1 <= BUILTINS_LOCATION)
1176 loc_t1 = loc1;
1177 if (loc_t2 <= BUILTINS_LOCATION)
1178 loc_t2 = loc2;
1180 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1182 /* It is a quite common bug to reference anonymous namespace type in
1183 non-anonymous namespace class. */
1184 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1185 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1187 if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
1189 std::swap (t1, t2);
1190 std::swap (loc_t1, loc_t2);
1192 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1193 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1194 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1195 /* Most of the time, the type names will match, do not be unnecesarily
1196 verbose. */
1197 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
1198 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
1199 inform (loc_t1,
1200 "type %qT defined in anonymous namespace can not match "
1201 "type %qT across the translation unit boundary",
1202 t1, t2);
1203 else
1204 inform (loc_t1,
1205 "type %qT defined in anonymous namespace can not match "
1206 "across the translation unit boundary",
1207 t1);
1208 if (loc_t2_useful)
1209 inform (loc_t2,
1210 "the incompatible type defined in another translation unit");
1211 return;
1213 /* If types have mangled ODR names and they are different, it is most
1214 informative to output those.
1215 This also covers types defined in different namespaces. */
1216 if (TYPE_NAME (t1) && TYPE_NAME (t2)
1217 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1218 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1219 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
1220 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
1221 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
1222 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1224 char *name1 = xstrdup (cplus_demangle
1225 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1226 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1227 char *name2 = cplus_demangle
1228 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1229 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1230 if (name1 && name2 && strcmp (name1, name2))
1232 inform (loc_t1,
1233 "type name %<%s%> should match type name %<%s%>",
1234 name1, name2);
1235 if (loc_t2_useful)
1236 inform (loc_t2,
1237 "the incompatible type is defined here");
1238 free (name1);
1239 return;
1241 free (name1);
1243 /* A tricky case are compound types. Often they appear the same in source
1244 code and the mismatch is dragged in by type they are build from.
1245 Look for those differences in subtypes and try to be informative. In other
1246 cases just output nothing because the source code is probably different
1247 and in this case we already output a all necessary info. */
1248 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1250 if (TREE_CODE (t1) == TREE_CODE (t2))
1252 if (TREE_CODE (t1) == ARRAY_TYPE
1253 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1255 tree i1 = TYPE_DOMAIN (t1);
1256 tree i2 = TYPE_DOMAIN (t2);
1258 if (i1 && i2
1259 && TYPE_MAX_VALUE (i1)
1260 && TYPE_MAX_VALUE (i2)
1261 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1262 TYPE_MAX_VALUE (i2), 0))
1264 inform (loc,
1265 "array types have different bounds");
1266 return;
1269 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1270 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1271 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1272 else if (TREE_CODE (t1) == METHOD_TYPE
1273 || TREE_CODE (t1) == FUNCTION_TYPE)
1275 tree parms1 = NULL, parms2 = NULL;
1276 int count = 1;
1278 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1280 inform (loc, "return value type mismatch");
1281 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1282 loc_t2);
1283 return;
1285 if (prototype_p (t1) && prototype_p (t2))
1286 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1287 parms1 && parms2;
1288 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1289 count++)
1291 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1293 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1294 inform (loc,
1295 "implicit this pointer type mismatch");
1296 else
1297 inform (loc,
1298 "type mismatch in parameter %i",
1299 count - (TREE_CODE (t1) == METHOD_TYPE));
1300 warn_types_mismatch (TREE_VALUE (parms1),
1301 TREE_VALUE (parms2),
1302 loc_t1, loc_t2);
1303 return;
1306 if (parms1 || parms2)
1308 inform (loc,
1309 "types have different parameter counts");
1310 return;
1314 return;
1317 if (types_odr_comparable (t1, t2, true)
1318 && types_same_for_odr (t1, t2, true))
1319 inform (loc_t1,
1320 "type %qT itself violate the C++ One Definition Rule", t1);
1321 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1322 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1323 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1324 return;
1325 else
1326 inform (loc_t1, "type %qT should match type %qT",
1327 t1, t2);
1328 if (loc_t2_useful)
1329 inform (loc_t2, "the incompatible type is defined here");
1332 /* Compare T1 and T2, report ODR violations if WARN is true and set
1333 WARNED to true if anything is reported. Return true if types match.
1334 If true is returned, the types are also compatible in the sense of
1335 gimple_canonical_types_compatible_p.
1336 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1337 about the type if the type itself do not have location. */
1339 static bool
1340 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1341 hash_set<type_pair,pair_traits> *visited,
1342 location_t loc1, location_t loc2)
1344 /* Check first for the obvious case of pointer identity. */
1345 if (t1 == t2)
1346 return true;
1347 gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
1348 gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
1350 /* Can't be the same type if the types don't have the same code. */
1351 if (TREE_CODE (t1) != TREE_CODE (t2))
1353 warn_odr (t1, t2, NULL, NULL, warn, warned,
1354 G_("a different type is defined in another translation unit"));
1355 return false;
1358 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1360 warn_odr (t1, t2, NULL, NULL, warn, warned,
1361 G_("a type with different qualifiers is defined in another "
1362 "translation unit"));
1363 return false;
1366 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1367 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1369 /* We can not trip this when comparing ODR types, only when trying to
1370 match different ODR derivations from different declarations.
1371 So WARN should be always false. */
1372 gcc_assert (!warn);
1373 return false;
1376 if (comp_type_attributes (t1, t2) != 1)
1378 warn_odr (t1, t2, NULL, NULL, warn, warned,
1379 G_("a type with different attributes "
1380 "is defined in another translation unit"));
1381 return false;
1384 if (TREE_CODE (t1) == ENUMERAL_TYPE
1385 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1387 tree v1, v2;
1388 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1389 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1391 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1393 warn_odr (t1, t2, NULL, NULL, warn, warned,
1394 G_("an enum with different value name"
1395 " is defined in another translation unit"));
1396 return false;
1398 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1399 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1400 DECL_INITIAL (TREE_VALUE (v2)), 0))
1402 warn_odr (t1, t2, NULL, NULL, warn, warned,
1403 G_("an enum with different values is defined"
1404 " in another translation unit"));
1405 return false;
1408 if (v1 || v2)
1410 warn_odr (t1, t2, NULL, NULL, warn, warned,
1411 G_("an enum with mismatching number of values "
1412 "is defined in another translation unit"));
1413 return false;
1417 /* Non-aggregate types can be handled cheaply. */
1418 if (INTEGRAL_TYPE_P (t1)
1419 || SCALAR_FLOAT_TYPE_P (t1)
1420 || FIXED_POINT_TYPE_P (t1)
1421 || TREE_CODE (t1) == VECTOR_TYPE
1422 || TREE_CODE (t1) == COMPLEX_TYPE
1423 || TREE_CODE (t1) == OFFSET_TYPE
1424 || POINTER_TYPE_P (t1))
1426 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1428 warn_odr (t1, t2, NULL, NULL, warn, warned,
1429 G_("a type with different precision is defined "
1430 "in another translation unit"));
1431 return false;
1433 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1435 warn_odr (t1, t2, NULL, NULL, warn, warned,
1436 G_("a type with different signedness is defined "
1437 "in another translation unit"));
1438 return false;
1441 if (TREE_CODE (t1) == INTEGER_TYPE
1442 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1444 /* char WRT uint_8? */
1445 warn_odr (t1, t2, NULL, NULL, warn, warned,
1446 G_("a different type is defined in another "
1447 "translation unit"));
1448 return false;
1451 /* For canonical type comparisons we do not want to build SCCs
1452 so we cannot compare pointed-to types. But we can, for now,
1453 require the same pointed-to type kind and match what
1454 useless_type_conversion_p would do. */
1455 if (POINTER_TYPE_P (t1))
1457 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1458 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1460 warn_odr (t1, t2, NULL, NULL, warn, warned,
1461 G_("it is defined as a pointer in different address "
1462 "space in another translation unit"));
1463 return false;
1466 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1467 visited, loc1, loc2))
1469 warn_odr (t1, t2, NULL, NULL, warn, warned,
1470 G_("it is defined as a pointer to different type "
1471 "in another translation unit"));
1472 if (warn && warned)
1473 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1474 loc1, loc2);
1475 return false;
1479 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1480 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1481 visited, loc1, loc2))
1483 /* Probably specific enough. */
1484 warn_odr (t1, t2, NULL, NULL, warn, warned,
1485 G_("a different type is defined "
1486 "in another translation unit"));
1487 if (warn && warned)
1488 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1489 return false;
1492 /* Do type-specific comparisons. */
1493 else switch (TREE_CODE (t1))
1495 case ARRAY_TYPE:
1497 /* Array types are the same if the element types are the same and
1498 the number of elements are the same. */
1499 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1500 visited, loc1, loc2))
1502 warn_odr (t1, t2, NULL, NULL, warn, warned,
1503 G_("a different type is defined in another "
1504 "translation unit"));
1505 if (warn && warned)
1506 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1508 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1509 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1510 == TYPE_NONALIASED_COMPONENT (t2));
1512 tree i1 = TYPE_DOMAIN (t1);
1513 tree i2 = TYPE_DOMAIN (t2);
1515 /* For an incomplete external array, the type domain can be
1516 NULL_TREE. Check this condition also. */
1517 if (i1 == NULL_TREE || i2 == NULL_TREE)
1518 return true;
1520 tree min1 = TYPE_MIN_VALUE (i1);
1521 tree min2 = TYPE_MIN_VALUE (i2);
1522 tree max1 = TYPE_MAX_VALUE (i1);
1523 tree max2 = TYPE_MAX_VALUE (i2);
1525 /* In C++, minimums should be always 0. */
1526 gcc_assert (min1 == min2);
1527 if (!operand_equal_p (max1, max2, 0))
1529 warn_odr (t1, t2, NULL, NULL, warn, warned,
1530 G_("an array of different size is defined "
1531 "in another translation unit"));
1532 return false;
1535 break;
1537 case METHOD_TYPE:
1538 case FUNCTION_TYPE:
1539 /* Function types are the same if the return type and arguments types
1540 are the same. */
1541 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1542 visited, loc1, loc2))
1544 warn_odr (t1, t2, NULL, NULL, warn, warned,
1545 G_("has different return value "
1546 "in another translation unit"));
1547 if (warn && warned)
1548 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1549 return false;
1552 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1553 || !prototype_p (t1) || !prototype_p (t2))
1554 return true;
1555 else
1557 tree parms1, parms2;
1559 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1560 parms1 && parms2;
1561 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1563 if (!odr_subtypes_equivalent_p
1564 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
1565 loc1, loc2))
1567 warn_odr (t1, t2, NULL, NULL, warn, warned,
1568 G_("has different parameters in another "
1569 "translation unit"));
1570 if (warn && warned)
1571 warn_types_mismatch (TREE_VALUE (parms1),
1572 TREE_VALUE (parms2), loc1, loc2);
1573 return false;
1577 if (parms1 || parms2)
1579 warn_odr (t1, t2, NULL, NULL, warn, warned,
1580 G_("has different parameters "
1581 "in another translation unit"));
1582 return false;
1585 return true;
1588 case RECORD_TYPE:
1589 case UNION_TYPE:
1590 case QUAL_UNION_TYPE:
1592 tree f1, f2;
1594 /* For aggregate types, all the fields must be the same. */
1595 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1597 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1598 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1599 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1601 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1602 warn_odr (t1, t2, NULL, NULL, warn, warned,
1603 G_("a type defined in another translation unit "
1604 "is not polymorphic"));
1605 else
1606 warn_odr (t1, t2, NULL, NULL, warn, warned,
1607 G_("a type defined in another translation unit "
1608 "is polymorphic"));
1609 return false;
1611 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1612 f1 || f2;
1613 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1615 /* Skip non-fields. */
1616 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1617 f1 = TREE_CHAIN (f1);
1618 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1619 f2 = TREE_CHAIN (f2);
1620 if (!f1 || !f2)
1621 break;
1622 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1624 warn_odr (t1, t2, NULL, NULL, warn, warned,
1625 G_("a type with different virtual table pointers"
1626 " is defined in another translation unit"));
1627 return false;
1629 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1631 warn_odr (t1, t2, NULL, NULL, warn, warned,
1632 G_("a type with different bases is defined "
1633 "in another translation unit"));
1634 return false;
1636 if (DECL_NAME (f1) != DECL_NAME (f2)
1637 && !DECL_ARTIFICIAL (f1))
1639 warn_odr (t1, t2, f1, f2, warn, warned,
1640 G_("a field with different name is defined "
1641 "in another translation unit"));
1642 return false;
1644 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1645 TREE_TYPE (f2), visited,
1646 loc1, loc2))
1648 /* Do not warn about artificial fields and just go into
1649 generic field mismatch warning. */
1650 if (DECL_ARTIFICIAL (f1))
1651 break;
1653 warn_odr (t1, t2, f1, f2, warn, warned,
1654 G_("a field of same name but different type "
1655 "is defined in another translation unit"));
1656 if (warn && warned)
1657 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1658 return false;
1660 if (!gimple_compare_field_offset (f1, f2))
1662 /* Do not warn about artificial fields and just go into
1663 generic field mismatch warning. */
1664 if (DECL_ARTIFICIAL (f1))
1665 break;
1666 warn_odr (t1, t2, f1, f2, warn, warned,
1667 G_("fields has different layout "
1668 "in another translation unit"));
1669 return false;
1671 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1672 == DECL_NONADDRESSABLE_P (f2));
1675 /* If one aggregate has more fields than the other, they
1676 are not the same. */
1677 if (f1 || f2)
1679 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1680 warn_odr (t1, t2, NULL, NULL, warn, warned,
1681 G_("a type with different virtual table pointers"
1682 " is defined in another translation unit"));
1683 else if ((f1 && DECL_ARTIFICIAL (f1))
1684 || (f2 && DECL_ARTIFICIAL (f2)))
1685 warn_odr (t1, t2, NULL, NULL, warn, warned,
1686 G_("a type with different bases is defined "
1687 "in another translation unit"));
1688 else
1689 warn_odr (t1, t2, f1, f2, warn, warned,
1690 G_("a type with different number of fields "
1691 "is defined in another translation unit"));
1693 return false;
1695 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1696 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1))
1697 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2))
1698 && odr_type_p (TYPE_MAIN_VARIANT (t1))
1699 && odr_type_p (TYPE_MAIN_VARIANT (t2))
1700 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1701 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1703 /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
1704 if it is non-NULL so this loop will never realy execute. */
1705 if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1)) != error_mark_node
1706 && TYPE_METHODS (TYPE_MAIN_VARIANT (t2)) != error_mark_node)
1707 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1708 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1709 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1711 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1713 warn_odr (t1, t2, f1, f2, warn, warned,
1714 G_("a different method of same type "
1715 "is defined in another "
1716 "translation unit"));
1717 return false;
1719 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1721 warn_odr (t1, t2, f1, f2, warn, warned,
1722 G_("s definition that differs by virtual "
1723 "keyword in another translation unit"));
1724 return false;
1726 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1728 warn_odr (t1, t2, f1, f2, warn, warned,
1729 G_("virtual table layout differs "
1730 "in another translation unit"));
1731 return false;
1733 if (odr_subtypes_equivalent_p (TREE_TYPE (f1),
1734 TREE_TYPE (f2), visited,
1735 loc1, loc2))
1737 warn_odr (t1, t2, f1, f2, warn, warned,
1738 G_("method with incompatible type is "
1739 "defined in another translation unit"));
1740 return false;
1743 if ((f1 == NULL) != (f2 == NULL))
1745 warn_odr (t1, t2, NULL, NULL, warn, warned,
1746 G_("a type with different number of methods "
1747 "is defined in another translation unit"));
1748 return false;
1752 break;
1754 case VOID_TYPE:
1755 case NULLPTR_TYPE:
1756 break;
1758 default:
1759 debug_tree (t1);
1760 gcc_unreachable ();
1763 /* Those are better to come last as they are utterly uninformative. */
1764 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1765 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1767 warn_odr (t1, t2, NULL, NULL, warn, warned,
1768 G_("a type with different size "
1769 "is defined in another translation unit"));
1770 return false;
1772 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1773 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1775 warn_odr (t1, t2, NULL, NULL, warn, warned,
1776 G_("a type with different alignment "
1777 "is defined in another translation unit"));
1778 return false;
1780 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1781 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1782 TYPE_SIZE_UNIT (t2), 0));
1783 return true;
1786 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1788 bool
1789 odr_types_equivalent_p (tree type1, tree type2)
1791 hash_set<type_pair,pair_traits> visited;
1793 #ifdef ENABLE_CHECKING
1794 gcc_assert (odr_or_derived_type_p (type1) && odr_or_derived_type_p (type2));
1795 #endif
1796 return odr_types_equivalent_p (type1, type2, false, NULL,
1797 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1800 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1801 from VAL->type. This may happen in LTO where tree merging did not merge
1802 all variants of the same type or due to ODR violation.
1804 Analyze and report ODR violations and add type to duplicate list.
1805 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1806 this is first time we see definition of a class return true so the
1807 base types are analyzed. */
1809 static bool
1810 add_type_duplicate (odr_type val, tree type)
1812 bool build_bases = false;
1813 bool prevail = false;
1814 bool odr_must_violate = false;
1816 if (!val->types_set)
1817 val->types_set = new hash_set<tree>;
1819 /* Chose polymorphic type as leader (this happens only in case of ODR
1820 violations. */
1821 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1822 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1823 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1824 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1826 prevail = true;
1827 build_bases = true;
1829 /* Always prefer complete type to be the leader. */
1830 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1832 prevail = true;
1833 build_bases = TYPE_BINFO (type);
1835 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1837 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1838 && TREE_CODE (type) == ENUMERAL_TYPE
1839 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1840 prevail = true;
1841 else if (TREE_CODE (val->type) == RECORD_TYPE
1842 && TREE_CODE (type) == RECORD_TYPE
1843 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1845 gcc_assert (!val->bases.length ());
1846 build_bases = true;
1847 prevail = true;
1850 if (prevail)
1852 tree tmp = type;
1854 type = val->type;
1855 val->type = tmp;
1858 val->types_set->add (type);
1860 /* If we now have a mangled name, be sure to record it to val->type
1861 so ODR hash can work. */
1863 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1864 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1865 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1867 bool merge = true;
1868 bool base_mismatch = false;
1869 unsigned int i;
1870 bool warned = false;
1871 hash_set<type_pair,pair_traits> visited;
1873 gcc_assert (in_lto_p);
1874 vec_safe_push (val->types, type);
1876 /* If both are class types, compare the bases. */
1877 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1878 && TREE_CODE (val->type) == RECORD_TYPE
1879 && TREE_CODE (type) == RECORD_TYPE
1880 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1882 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1883 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1885 if (!flag_ltrans && !warned && !val->odr_violated)
1887 tree extra_base;
1888 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1889 "a type with the same name but different "
1890 "number of polymorphic bases is "
1891 "defined in another translation unit");
1892 if (warned)
1894 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1895 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1896 extra_base = BINFO_BASE_BINFO
1897 (TYPE_BINFO (type),
1898 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1899 else
1900 extra_base = BINFO_BASE_BINFO
1901 (TYPE_BINFO (val->type),
1902 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1903 tree extra_base_type = BINFO_TYPE (extra_base);
1904 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1905 "the extra base is defined here");
1908 base_mismatch = true;
1910 else
1911 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1913 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1914 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1915 tree type1 = BINFO_TYPE (base1);
1916 tree type2 = BINFO_TYPE (base2);
1918 if (types_odr_comparable (type1, type2))
1920 if (!types_same_for_odr (type1, type2))
1921 base_mismatch = true;
1923 else
1924 if (!odr_types_equivalent_p (type1, type2))
1925 base_mismatch = true;
1926 if (base_mismatch)
1928 if (!warned && !val->odr_violated)
1930 warn_odr (type, val->type, NULL, NULL,
1931 !warned, &warned,
1932 "a type with the same name but different base "
1933 "type is defined in another translation unit");
1934 if (warned)
1935 warn_types_mismatch (type1, type2,
1936 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1938 break;
1940 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1942 base_mismatch = true;
1943 if (!warned && !val->odr_violated)
1944 warn_odr (type, val->type, NULL, NULL,
1945 !warned, &warned,
1946 "a type with the same name but different base "
1947 "layout is defined in another translation unit");
1948 break;
1950 /* One of bases is not of complete type. */
1951 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1953 /* If we have a polymorphic type info specified for TYPE1
1954 but not for TYPE2 we possibly missed a base when recording
1955 VAL->type earlier.
1956 Be sure this does not happen. */
1957 if (TYPE_BINFO (type1)
1958 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1959 && !build_bases)
1960 odr_must_violate = true;
1961 break;
1963 /* One base is polymorphic and the other not.
1964 This ought to be diagnosed earlier, but do not ICE in the
1965 checking bellow. */
1966 else if (TYPE_BINFO (type1)
1967 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1968 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1970 if (!warned && !val->odr_violated)
1971 warn_odr (type, val->type, NULL, NULL,
1972 !warned, &warned,
1973 "a base of the type is polymorphic only in one "
1974 "translation unit");
1975 base_mismatch = true;
1976 break;
1979 if (base_mismatch)
1981 merge = false;
1982 odr_violation_reported = true;
1983 val->odr_violated = true;
1985 if (symtab->dump_file)
1987 fprintf (symtab->dump_file, "ODR base violation\n");
1989 print_node (symtab->dump_file, "", val->type, 0);
1990 putc ('\n',symtab->dump_file);
1991 print_node (symtab->dump_file, "", type, 0);
1992 putc ('\n',symtab->dump_file);
1997 /* Next compare memory layout. */
1998 if (!odr_types_equivalent_p (val->type, type,
1999 !flag_ltrans && !val->odr_violated && !warned,
2000 &warned, &visited,
2001 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
2002 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
2004 merge = false;
2005 odr_violation_reported = true;
2006 val->odr_violated = true;
2007 if (symtab->dump_file)
2009 fprintf (symtab->dump_file, "ODR violation\n");
2011 print_node (symtab->dump_file, "", val->type, 0);
2012 putc ('\n',symtab->dump_file);
2013 print_node (symtab->dump_file, "", type, 0);
2014 putc ('\n',symtab->dump_file);
2017 gcc_assert (val->odr_violated || !odr_must_violate);
2018 /* Sanity check that all bases will be build same way again. */
2019 #ifdef ENABLE_CHECKING
2020 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
2021 && TREE_CODE (val->type) == RECORD_TYPE
2022 && TREE_CODE (type) == RECORD_TYPE
2023 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
2024 && !val->odr_violated
2025 && !base_mismatch && val->bases.length ())
2027 unsigned int num_poly_bases = 0;
2028 unsigned int j;
2030 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
2031 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
2032 (TYPE_BINFO (type), i)))
2033 num_poly_bases++;
2034 gcc_assert (num_poly_bases == val->bases.length ());
2035 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
2036 i++)
2037 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
2038 (TYPE_BINFO (type), i)))
2040 odr_type base = get_odr_type
2041 (BINFO_TYPE
2042 (BINFO_BASE_BINFO (TYPE_BINFO (type),
2043 i)),
2044 true);
2045 gcc_assert (val->bases[j] == base);
2046 j++;
2049 #endif
2052 /* Regularize things a little. During LTO same types may come with
2053 different BINFOs. Either because their virtual table was
2054 not merged by tree merging and only later at decl merging or
2055 because one type comes with external vtable, while other
2056 with internal. We want to merge equivalent binfos to conserve
2057 memory and streaming overhead.
2059 The external vtables are more harmful: they contain references
2060 to external declarations of methods that may be defined in the
2061 merged LTO unit. For this reason we absolutely need to remove
2062 them and replace by internal variants. Not doing so will lead
2063 to incomplete answers from possible_polymorphic_call_targets.
2065 FIXME: disable for now; because ODR types are now build during
2066 streaming in, the variants do not need to be linked to the type,
2067 yet. We need to do the merging in cleanup pass to be implemented
2068 soon. */
2069 if (!flag_ltrans && merge
2070 && 0
2071 && TREE_CODE (val->type) == RECORD_TYPE
2072 && TREE_CODE (type) == RECORD_TYPE
2073 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
2074 && TYPE_MAIN_VARIANT (type) == type
2075 && TYPE_MAIN_VARIANT (val->type) == val->type
2076 && BINFO_VTABLE (TYPE_BINFO (val->type))
2077 && BINFO_VTABLE (TYPE_BINFO (type)))
2079 tree master_binfo = TYPE_BINFO (val->type);
2080 tree v1 = BINFO_VTABLE (master_binfo);
2081 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
2083 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
2085 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
2086 && operand_equal_p (TREE_OPERAND (v1, 1),
2087 TREE_OPERAND (v2, 1), 0));
2088 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
2089 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
2091 gcc_assert (DECL_ASSEMBLER_NAME (v1)
2092 == DECL_ASSEMBLER_NAME (v2));
2094 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
2096 unsigned int i;
2098 set_type_binfo (val->type, TYPE_BINFO (type));
2099 for (i = 0; i < val->types->length (); i++)
2101 if (TYPE_BINFO ((*val->types)[i])
2102 == master_binfo)
2103 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
2105 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
2107 else
2108 set_type_binfo (type, master_binfo);
2110 return build_bases;
2113 /* Get ODR type hash entry for TYPE. If INSERT is true, create
2114 possibly new entry. */
2116 odr_type
2117 get_odr_type (tree type, bool insert)
2119 odr_type_d **slot = NULL;
2120 odr_type_d **vtable_slot = NULL;
2121 odr_type val = NULL;
2122 hashval_t hash;
2123 bool build_bases = false;
2124 bool insert_to_odr_array = false;
2125 int base_id = -1;
2127 type = main_odr_variant (type);
2129 gcc_checking_assert (can_be_name_hashed_p (type)
2130 || can_be_vtable_hashed_p (type));
2132 /* Lookup entry, first try name hash, fallback to vtable hash. */
2133 if (can_be_name_hashed_p (type))
2135 hash = hash_odr_name (type);
2136 slot = odr_hash->find_slot_with_hash (type, hash,
2137 insert ? INSERT : NO_INSERT);
2139 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2141 hash = hash_odr_vtable (type);
2142 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2143 insert ? INSERT : NO_INSERT);
2146 if (!slot && !vtable_slot)
2147 return NULL;
2149 /* See if we already have entry for type. */
2150 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2152 if (slot && *slot)
2154 val = *slot;
2155 #ifdef ENABLE_CHECKING
2156 if (in_lto_p && can_be_vtable_hashed_p (type))
2158 hash = hash_odr_vtable (type);
2159 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2160 NO_INSERT);
2161 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2162 vtable_slot = NULL;
2164 #endif
2166 else if (*vtable_slot)
2167 val = *vtable_slot;
2169 if (val->type != type
2170 && (!val->types_set || !val->types_set->add (type)))
2172 gcc_assert (insert);
2173 /* We have type duplicate, but it may introduce vtable name or
2174 mangled name; be sure to keep hashes in sync. */
2175 if (in_lto_p && can_be_vtable_hashed_p (type)
2176 && (!vtable_slot || !*vtable_slot))
2178 if (!vtable_slot)
2180 hash = hash_odr_vtable (type);
2181 vtable_slot = odr_vtable_hash->find_slot_with_hash
2182 (type, hash, INSERT);
2183 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2185 *vtable_slot = val;
2187 if (slot && !*slot)
2188 *slot = val;
2189 build_bases = add_type_duplicate (val, type);
2192 else
2194 val = ggc_cleared_alloc<odr_type_d> ();
2195 val->type = type;
2196 val->bases = vNULL;
2197 val->derived_types = vNULL;
2198 if (type_with_linkage_p (type))
2199 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2200 else
2201 val->anonymous_namespace = 0;
2202 build_bases = COMPLETE_TYPE_P (val->type);
2203 insert_to_odr_array = true;
2204 if (slot)
2205 *slot = val;
2206 if (vtable_slot)
2207 *vtable_slot = val;
2210 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2211 && type_with_linkage_p (type)
2212 && type == TYPE_MAIN_VARIANT (type))
2214 tree binfo = TYPE_BINFO (type);
2215 unsigned int i;
2217 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2219 val->all_derivations_known = type_all_derivations_known_p (type);
2220 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2221 /* For now record only polymorphic types. other are
2222 pointless for devirtualization and we can not precisely
2223 determine ODR equivalency of these during LTO. */
2224 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2226 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2227 odr_type base = get_odr_type (base_type, true);
2228 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2229 base->derived_types.safe_push (val);
2230 val->bases.safe_push (base);
2231 if (base->id > base_id)
2232 base_id = base->id;
2235 /* Ensure that type always appears after bases. */
2236 if (insert_to_odr_array)
2238 if (odr_types_ptr)
2239 val->id = odr_types.length ();
2240 vec_safe_push (odr_types_ptr, val);
2242 else if (base_id > val->id)
2244 odr_types[val->id] = 0;
2245 /* Be sure we did not recorded any derived types; these may need
2246 renumbering too. */
2247 gcc_assert (val->derived_types.length() == 0);
2248 if (odr_types_ptr)
2249 val->id = odr_types.length ();
2250 vec_safe_push (odr_types_ptr, val);
2252 return val;
2255 /* Add TYPE od ODR type hash. */
2257 void
2258 register_odr_type (tree type)
2260 if (!odr_hash)
2262 odr_hash = new odr_hash_type (23);
2263 if (in_lto_p)
2264 odr_vtable_hash = new odr_vtable_hash_type (23);
2266 /* Arrange things to be nicer and insert main variants first.
2267 ??? fundamental prerecorded types do not have mangled names; this
2268 makes it possible that non-ODR type is main_odr_variant of ODR type.
2269 Things may get smoother if LTO FE set mangled name of those types same
2270 way as C++ FE does. */
2271 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
2272 && odr_type_p (TYPE_MAIN_VARIANT (type)))
2273 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2274 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
2275 get_odr_type (type, true);
2278 /* Return true if type is known to have no derivations. */
2280 bool
2281 type_known_to_have_no_derivations_p (tree t)
2283 return (type_all_derivations_known_p (t)
2284 && (TYPE_FINAL_P (t)
2285 || (odr_hash
2286 && !get_odr_type (t, true)->derived_types.length())));
2289 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2290 recursive printing. */
2292 static void
2293 dump_odr_type (FILE *f, odr_type t, int indent=0)
2295 unsigned int i;
2296 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2297 print_generic_expr (f, t->type, TDF_SLIM);
2298 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2299 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2300 if (TYPE_NAME (t->type))
2302 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2303 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2304 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2305 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2306 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2307 IDENTIFIER_POINTER
2308 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2310 if (t->bases.length ())
2312 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2313 for (i = 0; i < t->bases.length (); i++)
2314 fprintf (f, " %i", t->bases[i]->id);
2315 fprintf (f, "\n");
2317 if (t->derived_types.length ())
2319 fprintf (f, "%*s derived types:\n", indent * 2, "");
2320 for (i = 0; i < t->derived_types.length (); i++)
2321 dump_odr_type (f, t->derived_types[i], indent + 1);
2323 fprintf (f, "\n");
2326 /* Dump the type inheritance graph. */
2328 static void
2329 dump_type_inheritance_graph (FILE *f)
2331 unsigned int i;
2332 if (!odr_types_ptr)
2333 return;
2334 fprintf (f, "\n\nType inheritance graph:\n");
2335 for (i = 0; i < odr_types.length (); i++)
2337 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2338 dump_odr_type (f, odr_types[i]);
2340 for (i = 0; i < odr_types.length (); i++)
2342 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2344 unsigned int j;
2345 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2346 print_node (f, "", odr_types[i]->type, 0);
2347 for (j = 0; j < odr_types[i]->types->length (); j++)
2349 tree t;
2350 fprintf (f, "duplicate #%i\n", j);
2351 print_node (f, "", (*odr_types[i]->types)[j], 0);
2352 t = (*odr_types[i]->types)[j];
2353 while (TYPE_P (t) && TYPE_CONTEXT (t))
2355 t = TYPE_CONTEXT (t);
2356 print_node (f, "", t, 0);
2358 putc ('\n',f);
2364 /* Initialize IPA devirt and build inheritance tree graph. */
2366 void
2367 build_type_inheritance_graph (void)
2369 struct symtab_node *n;
2370 FILE *inheritance_dump_file;
2371 int flags;
2373 if (odr_hash)
2374 return;
2375 timevar_push (TV_IPA_INHERITANCE);
2376 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2377 odr_hash = new odr_hash_type (23);
2378 if (in_lto_p)
2379 odr_vtable_hash = new odr_vtable_hash_type (23);
2381 /* We reconstruct the graph starting of types of all methods seen in the
2382 the unit. */
2383 FOR_EACH_SYMBOL (n)
2384 if (is_a <cgraph_node *> (n)
2385 && DECL_VIRTUAL_P (n->decl)
2386 && n->real_symbol_p ())
2387 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2389 /* Look also for virtual tables of types that do not define any methods.
2391 We need it in a case where class B has virtual base of class A
2392 re-defining its virtual method and there is class C with no virtual
2393 methods with B as virtual base.
2395 Here we output B's virtual method in two variant - for non-virtual
2396 and virtual inheritance. B's virtual table has non-virtual version,
2397 while C's has virtual.
2399 For this reason we need to know about C in order to include both
2400 variants of B. More correctly, record_target_from_binfo should
2401 add both variants of the method when walking B, but we have no
2402 link in between them.
2404 We rely on fact that either the method is exported and thus we
2405 assume it is called externally or C is in anonymous namespace and
2406 thus we will see the vtable. */
2408 else if (is_a <varpool_node *> (n)
2409 && DECL_VIRTUAL_P (n->decl)
2410 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2411 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2412 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2413 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2414 if (inheritance_dump_file)
2416 dump_type_inheritance_graph (inheritance_dump_file);
2417 dump_end (TDI_inheritance, inheritance_dump_file);
2419 timevar_pop (TV_IPA_INHERITANCE);
2422 /* Return true if N has reference from live virtual table
2423 (and thus can be a destination of polymorphic call).
2424 Be conservatively correct when callgraph is not built or
2425 if the method may be referred externally. */
2427 static bool
2428 referenced_from_vtable_p (struct cgraph_node *node)
2430 int i;
2431 struct ipa_ref *ref;
2432 bool found = false;
2434 if (node->externally_visible
2435 || DECL_EXTERNAL (node->decl)
2436 || node->used_from_other_partition)
2437 return true;
2439 /* Keep this test constant time.
2440 It is unlikely this can happen except for the case where speculative
2441 devirtualization introduced many speculative edges to this node.
2442 In this case the target is very likely alive anyway. */
2443 if (node->ref_list.referring.length () > 100)
2444 return true;
2446 /* We need references built. */
2447 if (symtab->state <= CONSTRUCTION)
2448 return true;
2450 for (i = 0; node->iterate_referring (i, ref); i++)
2451 if ((ref->use == IPA_REF_ALIAS
2452 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2453 || (ref->use == IPA_REF_ADDR
2454 && TREE_CODE (ref->referring->decl) == VAR_DECL
2455 && DECL_VIRTUAL_P (ref->referring->decl)))
2457 found = true;
2458 break;
2460 return found;
2463 /* If TARGET has associated node, record it in the NODES array.
2464 CAN_REFER specify if program can refer to the target directly.
2465 if TARGET is unknown (NULL) or it can not be inserted (for example because
2466 its body was already removed and there is no way to refer to it), clear
2467 COMPLETEP. */
2469 static void
2470 maybe_record_node (vec <cgraph_node *> &nodes,
2471 tree target, hash_set<tree> *inserted,
2472 bool can_refer,
2473 bool *completep)
2475 struct cgraph_node *target_node, *alias_target;
2476 enum availability avail;
2478 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2479 list of targets; the runtime effect of calling them is undefined.
2480 Only "real" virtual methods should be accounted. */
2481 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
2482 return;
2484 if (!can_refer)
2486 /* The only case when method of anonymous namespace becomes unreferable
2487 is when we completely optimized it out. */
2488 if (flag_ltrans
2489 || !target
2490 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2491 *completep = false;
2492 return;
2495 if (!target)
2496 return;
2498 target_node = cgraph_node::get (target);
2500 /* Prefer alias target over aliases, so we do not get confused by
2501 fake duplicates. */
2502 if (target_node)
2504 alias_target = target_node->ultimate_alias_target (&avail);
2505 if (target_node != alias_target
2506 && avail >= AVAIL_AVAILABLE
2507 && target_node->get_availability ())
2508 target_node = alias_target;
2511 /* Method can only be called by polymorphic call if any
2512 of vtables referring to it are alive.
2514 While this holds for non-anonymous functions, too, there are
2515 cases where we want to keep them in the list; for example
2516 inline functions with -fno-weak are static, but we still
2517 may devirtualize them when instance comes from other unit.
2518 The same holds for LTO.
2520 Currently we ignore these functions in speculative devirtualization.
2521 ??? Maybe it would make sense to be more aggressive for LTO even
2522 elsewhere. */
2523 if (!flag_ltrans
2524 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2525 && (!target_node
2526 || !referenced_from_vtable_p (target_node)))
2528 /* See if TARGET is useful function we can deal with. */
2529 else if (target_node != NULL
2530 && (TREE_PUBLIC (target)
2531 || DECL_EXTERNAL (target)
2532 || target_node->definition)
2533 && target_node->real_symbol_p ())
2535 gcc_assert (!target_node->global.inlined_to);
2536 gcc_assert (target_node->real_symbol_p ());
2537 if (!inserted->add (target))
2539 cached_polymorphic_call_targets->add (target_node);
2540 nodes.safe_push (target_node);
2543 else if (completep
2544 && (!type_in_anonymous_namespace_p
2545 (DECL_CONTEXT (target))
2546 || flag_ltrans))
2547 *completep = false;
2550 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2551 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2552 method in vtable and insert method to NODES array
2553 or BASES_TO_CONSIDER if this array is non-NULL.
2554 Otherwise recurse to base BINFOs.
2555 This matches what get_binfo_at_offset does, but with offset
2556 being unknown.
2558 TYPE_BINFOS is a stack of BINFOS of types with defined
2559 virtual table seen on way from class type to BINFO.
2561 MATCHED_VTABLES tracks virtual tables we already did lookup
2562 for virtual function in. INSERTED tracks nodes we already
2563 inserted.
2565 ANONYMOUS is true if BINFO is part of anonymous namespace.
2567 Clear COMPLETEP when we hit unreferable target.
2570 static void
2571 record_target_from_binfo (vec <cgraph_node *> &nodes,
2572 vec <tree> *bases_to_consider,
2573 tree binfo,
2574 tree otr_type,
2575 vec <tree> &type_binfos,
2576 HOST_WIDE_INT otr_token,
2577 tree outer_type,
2578 HOST_WIDE_INT offset,
2579 hash_set<tree> *inserted,
2580 hash_set<tree> *matched_vtables,
2581 bool anonymous,
2582 bool *completep)
2584 tree type = BINFO_TYPE (binfo);
2585 int i;
2586 tree base_binfo;
2589 if (BINFO_VTABLE (binfo))
2590 type_binfos.safe_push (binfo);
2591 if (types_same_for_odr (type, outer_type))
2593 int i;
2594 tree type_binfo = NULL;
2596 /* Look up BINFO with virtual table. For normal types it is always last
2597 binfo on stack. */
2598 for (i = type_binfos.length () - 1; i >= 0; i--)
2599 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2601 type_binfo = type_binfos[i];
2602 break;
2604 if (BINFO_VTABLE (binfo))
2605 type_binfos.pop ();
2606 /* If this is duplicated BINFO for base shared by virtual inheritance,
2607 we may not have its associated vtable. This is not a problem, since
2608 we will walk it on the other path. */
2609 if (!type_binfo)
2610 return;
2611 tree inner_binfo = get_binfo_at_offset (type_binfo,
2612 offset, otr_type);
2613 if (!inner_binfo)
2615 gcc_assert (odr_violation_reported);
2616 return;
2618 /* For types in anonymous namespace first check if the respective vtable
2619 is alive. If not, we know the type can't be called. */
2620 if (!flag_ltrans && anonymous)
2622 tree vtable = BINFO_VTABLE (inner_binfo);
2623 varpool_node *vnode;
2625 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2626 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2627 vnode = varpool_node::get (vtable);
2628 if (!vnode || !vnode->definition)
2629 return;
2631 gcc_assert (inner_binfo);
2632 if (bases_to_consider
2633 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2634 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2636 bool can_refer;
2637 tree target = gimple_get_virt_method_for_binfo (otr_token,
2638 inner_binfo,
2639 &can_refer);
2640 if (!bases_to_consider)
2641 maybe_record_node (nodes, target, inserted, can_refer, completep);
2642 /* Destructors are never called via construction vtables. */
2643 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2644 bases_to_consider->safe_push (target);
2646 return;
2649 /* Walk bases. */
2650 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2651 /* Walking bases that have no virtual method is pointless exercise. */
2652 if (polymorphic_type_binfo_p (base_binfo))
2653 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2654 type_binfos,
2655 otr_token, outer_type, offset, inserted,
2656 matched_vtables, anonymous, completep);
2657 if (BINFO_VTABLE (binfo))
2658 type_binfos.pop ();
2661 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2662 of TYPE, insert them to NODES, recurse into derived nodes.
2663 INSERTED is used to avoid duplicate insertions of methods into NODES.
2664 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2665 Clear COMPLETEP if unreferable target is found.
2667 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2668 all cases where BASE_SKIPPED is true (because the base is abstract
2669 class). */
2671 static void
2672 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2673 hash_set<tree> *inserted,
2674 hash_set<tree> *matched_vtables,
2675 tree otr_type,
2676 odr_type type,
2677 HOST_WIDE_INT otr_token,
2678 tree outer_type,
2679 HOST_WIDE_INT offset,
2680 bool *completep,
2681 vec <tree> &bases_to_consider,
2682 bool consider_construction)
2684 tree binfo = TYPE_BINFO (type->type);
2685 unsigned int i;
2686 auto_vec <tree, 8> type_binfos;
2687 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2689 /* We may need to consider types w/o instances because of possible derived
2690 types using their methods either directly or via construction vtables.
2691 We are safe to skip them when all derivations are known, since we will
2692 handle them later.
2693 This is done by recording them to BASES_TO_CONSIDER array. */
2694 if (possibly_instantiated || consider_construction)
2696 record_target_from_binfo (nodes,
2697 (!possibly_instantiated
2698 && type_all_derivations_known_p (type->type))
2699 ? &bases_to_consider : NULL,
2700 binfo, otr_type, type_binfos, otr_token,
2701 outer_type, offset,
2702 inserted, matched_vtables,
2703 type->anonymous_namespace, completep);
2705 for (i = 0; i < type->derived_types.length (); i++)
2706 possible_polymorphic_call_targets_1 (nodes, inserted,
2707 matched_vtables,
2708 otr_type,
2709 type->derived_types[i],
2710 otr_token, outer_type, offset, completep,
2711 bases_to_consider, consider_construction);
2714 /* Cache of queries for polymorphic call targets.
2716 Enumerating all call targets may get expensive when there are many
2717 polymorphic calls in the program, so we memoize all the previous
2718 queries and avoid duplicated work. */
2720 struct polymorphic_call_target_d
2722 HOST_WIDE_INT otr_token;
2723 ipa_polymorphic_call_context context;
2724 odr_type type;
2725 vec <cgraph_node *> targets;
2726 tree decl_warning;
2727 int type_warning;
2728 bool complete;
2729 bool speculative;
2732 /* Polymorphic call target cache helpers. */
2734 struct polymorphic_call_target_hasher
2736 typedef polymorphic_call_target_d *value_type;
2737 typedef polymorphic_call_target_d *compare_type;
2738 static inline hashval_t hash (const polymorphic_call_target_d *);
2739 static inline bool equal (const polymorphic_call_target_d *,
2740 const polymorphic_call_target_d *);
2741 static inline void remove (polymorphic_call_target_d *);
2744 /* Return the computed hashcode for ODR_QUERY. */
2746 inline hashval_t
2747 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2749 inchash::hash hstate (odr_query->otr_token);
2751 hstate.add_wide_int (odr_query->type->id);
2752 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2753 hstate.add_wide_int (odr_query->context.offset);
2755 if (odr_query->context.speculative_outer_type)
2757 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2758 hstate.add_wide_int (odr_query->context.speculative_offset);
2760 hstate.add_flag (odr_query->speculative);
2761 hstate.add_flag (odr_query->context.maybe_in_construction);
2762 hstate.add_flag (odr_query->context.maybe_derived_type);
2763 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2764 hstate.commit_flag ();
2765 return hstate.end ();
2768 /* Compare cache entries T1 and T2. */
2770 inline bool
2771 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2772 const polymorphic_call_target_d *t2)
2774 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2775 && t1->speculative == t2->speculative
2776 && t1->context.offset == t2->context.offset
2777 && t1->context.speculative_offset == t2->context.speculative_offset
2778 && t1->context.outer_type == t2->context.outer_type
2779 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2780 && t1->context.maybe_in_construction
2781 == t2->context.maybe_in_construction
2782 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2783 && (t1->context.speculative_maybe_derived_type
2784 == t2->context.speculative_maybe_derived_type));
2787 /* Remove entry in polymorphic call target cache hash. */
2789 inline void
2790 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2792 v->targets.release ();
2793 free (v);
2796 /* Polymorphic call target query cache. */
2798 typedef hash_table<polymorphic_call_target_hasher>
2799 polymorphic_call_target_hash_type;
2800 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2802 /* Destroy polymorphic call target query cache. */
2804 static void
2805 free_polymorphic_call_targets_hash ()
2807 if (cached_polymorphic_call_targets)
2809 delete polymorphic_call_target_hash;
2810 polymorphic_call_target_hash = NULL;
2811 delete cached_polymorphic_call_targets;
2812 cached_polymorphic_call_targets = NULL;
2816 /* When virtual function is removed, we may need to flush the cache. */
2818 static void
2819 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2821 if (cached_polymorphic_call_targets
2822 && cached_polymorphic_call_targets->contains (n))
2823 free_polymorphic_call_targets_hash ();
2826 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2828 tree
2829 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2830 tree vtable)
2832 tree v = BINFO_VTABLE (binfo);
2833 int i;
2834 tree base_binfo;
2835 unsigned HOST_WIDE_INT this_offset;
2837 if (v)
2839 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2840 gcc_unreachable ();
2842 if (offset == this_offset
2843 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2844 return binfo;
2847 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2848 if (polymorphic_type_binfo_p (base_binfo))
2850 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2851 if (base_binfo)
2852 return base_binfo;
2854 return NULL;
2857 /* T is known constant value of virtual table pointer.
2858 Store virtual table to V and its offset to OFFSET.
2859 Return false if T does not look like virtual table reference. */
2861 bool
2862 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2863 unsigned HOST_WIDE_INT *offset)
2865 /* We expect &MEM[(void *)&virtual_table + 16B].
2866 We obtain object's BINFO from the context of the virtual table.
2867 This one contains pointer to virtual table represented via
2868 POINTER_PLUS_EXPR. Verify that this pointer matches what
2869 we propagated through.
2871 In the case of virtual inheritance, the virtual tables may
2872 be nested, i.e. the offset may be different from 16 and we may
2873 need to dive into the type representation. */
2874 if (TREE_CODE (t) == ADDR_EXPR
2875 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2876 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2877 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2878 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2879 == VAR_DECL)
2880 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2881 (TREE_OPERAND (t, 0), 0), 0)))
2883 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2884 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2885 return true;
2888 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2889 We need to handle it when T comes from static variable initializer or
2890 BINFO. */
2891 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2893 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2894 t = TREE_OPERAND (t, 0);
2896 else
2897 *offset = 0;
2899 if (TREE_CODE (t) != ADDR_EXPR)
2900 return false;
2901 *v = TREE_OPERAND (t, 0);
2902 return true;
2905 /* T is known constant value of virtual table pointer. Return BINFO of the
2906 instance type. */
2908 tree
2909 vtable_pointer_value_to_binfo (const_tree t)
2911 tree vtable;
2912 unsigned HOST_WIDE_INT offset;
2914 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2915 return NULL_TREE;
2917 /* FIXME: for stores of construction vtables we return NULL,
2918 because we do not have BINFO for those. Eventually we should fix
2919 our representation to allow this case to be handled, too.
2920 In the case we see store of BINFO we however may assume
2921 that standard folding will be able to cope with it. */
2922 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2923 offset, vtable);
2926 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2927 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2928 and insert them in NODES.
2930 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2932 static void
2933 record_targets_from_bases (tree otr_type,
2934 HOST_WIDE_INT otr_token,
2935 tree outer_type,
2936 HOST_WIDE_INT offset,
2937 vec <cgraph_node *> &nodes,
2938 hash_set<tree> *inserted,
2939 hash_set<tree> *matched_vtables,
2940 bool *completep)
2942 while (true)
2944 HOST_WIDE_INT pos, size;
2945 tree base_binfo;
2946 tree fld;
2948 if (types_same_for_odr (outer_type, otr_type))
2949 return;
2951 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2953 if (TREE_CODE (fld) != FIELD_DECL)
2954 continue;
2956 pos = int_bit_position (fld);
2957 size = tree_to_shwi (DECL_SIZE (fld));
2958 if (pos <= offset && (pos + size) > offset
2959 /* Do not get confused by zero sized bases. */
2960 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2961 break;
2963 /* Within a class type we should always find corresponding fields. */
2964 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2966 /* Nonbase types should have been stripped by outer_class_type. */
2967 gcc_assert (DECL_ARTIFICIAL (fld));
2969 outer_type = TREE_TYPE (fld);
2970 offset -= pos;
2972 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2973 offset, otr_type);
2974 if (!base_binfo)
2976 gcc_assert (odr_violation_reported);
2977 return;
2979 gcc_assert (base_binfo);
2980 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2982 bool can_refer;
2983 tree target = gimple_get_virt_method_for_binfo (otr_token,
2984 base_binfo,
2985 &can_refer);
2986 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2987 maybe_record_node (nodes, target, inserted, can_refer, completep);
2988 matched_vtables->add (BINFO_VTABLE (base_binfo));
2993 /* When virtual table is removed, we may need to flush the cache. */
2995 static void
2996 devirt_variable_node_removal_hook (varpool_node *n,
2997 void *d ATTRIBUTE_UNUSED)
2999 if (cached_polymorphic_call_targets
3000 && DECL_VIRTUAL_P (n->decl)
3001 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
3002 free_polymorphic_call_targets_hash ();
3005 /* Record about how many calls would benefit from given type to be final. */
3007 struct odr_type_warn_count
3009 tree type;
3010 int count;
3011 gcov_type dyn_count;
3014 /* Record about how many calls would benefit from given method to be final. */
3016 struct decl_warn_count
3018 tree decl;
3019 int count;
3020 gcov_type dyn_count;
3023 /* Information about type and decl warnings. */
3025 struct final_warning_record
3027 gcov_type dyn_count;
3028 vec<odr_type_warn_count> type_warnings;
3029 hash_map<tree, decl_warn_count> decl_warnings;
3031 struct final_warning_record *final_warning_records;
3033 /* Return vector containing possible targets of polymorphic call of type
3034 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3035 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3036 OTR_TYPE and include their virtual method. This is useful for types
3037 possibly in construction or destruction where the virtual table may
3038 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3039 us to walk the inheritance graph for all derivations.
3041 If COMPLETEP is non-NULL, store true if the list is complete.
3042 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3043 in the target cache. If user needs to visit every target list
3044 just once, it can memoize them.
3046 If SPECULATIVE is set, the list will not contain targets that
3047 are not speculatively taken.
3049 Returned vector is placed into cache. It is NOT caller's responsibility
3050 to free it. The vector can be freed on cgraph_remove_node call if
3051 the particular node is a virtual function present in the cache. */
3053 vec <cgraph_node *>
3054 possible_polymorphic_call_targets (tree otr_type,
3055 HOST_WIDE_INT otr_token,
3056 ipa_polymorphic_call_context context,
3057 bool *completep,
3058 void **cache_token,
3059 bool speculative)
3061 static struct cgraph_node_hook_list *node_removal_hook_holder;
3062 vec <cgraph_node *> nodes = vNULL;
3063 auto_vec <tree, 8> bases_to_consider;
3064 odr_type type, outer_type;
3065 polymorphic_call_target_d key;
3066 polymorphic_call_target_d **slot;
3067 unsigned int i;
3068 tree binfo, target;
3069 bool complete;
3070 bool can_refer = false;
3071 bool skipped = false;
3073 otr_type = TYPE_MAIN_VARIANT (otr_type);
3075 /* If ODR is not initialized or the context is invalid, return empty
3076 incomplete list. */
3077 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3079 if (completep)
3080 *completep = context.invalid;
3081 if (cache_token)
3082 *cache_token = NULL;
3083 return nodes;
3086 /* Do not bother to compute speculative info when user do not asks for it. */
3087 if (!speculative || !context.speculative_outer_type)
3088 context.clear_speculation ();
3090 type = get_odr_type (otr_type, true);
3092 /* Recording type variants would waste results cache. */
3093 gcc_assert (!context.outer_type
3094 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3096 /* Look up the outer class type we want to walk.
3097 If we fail to do so, the context is invalid. */
3098 if ((context.outer_type || context.speculative_outer_type)
3099 && !context.restrict_to_inner_class (otr_type))
3101 if (completep)
3102 *completep = true;
3103 if (cache_token)
3104 *cache_token = NULL;
3105 return nodes;
3107 gcc_assert (!context.invalid);
3109 /* Check that restrict_to_inner_class kept the main variant. */
3110 gcc_assert (!context.outer_type
3111 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3113 /* We canonicalize our query, so we do not need extra hashtable entries. */
3115 /* Without outer type, we have no use for offset. Just do the
3116 basic search from inner type. */
3117 if (!context.outer_type)
3118 context.clear_outer_type (otr_type);
3119 /* We need to update our hierarchy if the type does not exist. */
3120 outer_type = get_odr_type (context.outer_type, true);
3121 /* If the type is complete, there are no derivations. */
3122 if (TYPE_FINAL_P (outer_type->type))
3123 context.maybe_derived_type = false;
3125 /* Initialize query cache. */
3126 if (!cached_polymorphic_call_targets)
3128 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3129 polymorphic_call_target_hash
3130 = new polymorphic_call_target_hash_type (23);
3131 if (!node_removal_hook_holder)
3133 node_removal_hook_holder =
3134 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3135 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3136 NULL);
3140 if (in_lto_p)
3142 if (context.outer_type != otr_type)
3143 context.outer_type
3144 = get_odr_type (context.outer_type, true)->type;
3145 if (context.speculative_outer_type)
3146 context.speculative_outer_type
3147 = get_odr_type (context.speculative_outer_type, true)->type;
3150 /* Look up cached answer. */
3151 key.type = type;
3152 key.otr_token = otr_token;
3153 key.speculative = speculative;
3154 key.context = context;
3155 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3156 if (cache_token)
3157 *cache_token = (void *)*slot;
3158 if (*slot)
3160 if (completep)
3161 *completep = (*slot)->complete;
3162 if ((*slot)->type_warning && final_warning_records)
3164 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3165 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3166 += final_warning_records->dyn_count;
3168 if (!speculative && (*slot)->decl_warning && final_warning_records)
3170 struct decl_warn_count *c =
3171 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3172 c->count++;
3173 c->dyn_count += final_warning_records->dyn_count;
3175 return (*slot)->targets;
3178 complete = true;
3180 /* Do actual search. */
3181 timevar_push (TV_IPA_VIRTUAL_CALL);
3182 *slot = XCNEW (polymorphic_call_target_d);
3183 if (cache_token)
3184 *cache_token = (void *)*slot;
3185 (*slot)->type = type;
3186 (*slot)->otr_token = otr_token;
3187 (*slot)->context = context;
3188 (*slot)->speculative = speculative;
3190 hash_set<tree> inserted;
3191 hash_set<tree> matched_vtables;
3193 /* First insert targets we speculatively identified as likely. */
3194 if (context.speculative_outer_type)
3196 odr_type speculative_outer_type;
3197 bool speculation_complete = true;
3199 /* First insert target from type itself and check if it may have
3200 derived types. */
3201 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3202 if (TYPE_FINAL_P (speculative_outer_type->type))
3203 context.speculative_maybe_derived_type = false;
3204 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3205 context.speculative_offset, otr_type);
3206 if (binfo)
3207 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3208 &can_refer);
3209 else
3210 target = NULL;
3212 /* In the case we get complete method, we don't need
3213 to walk derivations. */
3214 if (target && DECL_FINAL_P (target))
3215 context.speculative_maybe_derived_type = false;
3216 if (type_possibly_instantiated_p (speculative_outer_type->type))
3217 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3218 if (binfo)
3219 matched_vtables.add (BINFO_VTABLE (binfo));
3222 /* Next walk recursively all derived types. */
3223 if (context.speculative_maybe_derived_type)
3224 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3225 possible_polymorphic_call_targets_1 (nodes, &inserted,
3226 &matched_vtables,
3227 otr_type,
3228 speculative_outer_type->derived_types[i],
3229 otr_token, speculative_outer_type->type,
3230 context.speculative_offset,
3231 &speculation_complete,
3232 bases_to_consider,
3233 false);
3236 if (!speculative || !nodes.length ())
3238 /* First see virtual method of type itself. */
3239 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3240 context.offset, otr_type);
3241 if (binfo)
3242 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3243 &can_refer);
3244 else
3246 gcc_assert (odr_violation_reported);
3247 target = NULL;
3250 /* Destructors are never called through construction virtual tables,
3251 because the type is always known. */
3252 if (target && DECL_CXX_DESTRUCTOR_P (target))
3253 context.maybe_in_construction = false;
3255 if (target)
3257 /* In the case we get complete method, we don't need
3258 to walk derivations. */
3259 if (DECL_FINAL_P (target))
3260 context.maybe_derived_type = false;
3263 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3264 if (type_possibly_instantiated_p (outer_type->type))
3265 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3266 else
3267 skipped = true;
3269 if (binfo)
3270 matched_vtables.add (BINFO_VTABLE (binfo));
3272 /* Next walk recursively all derived types. */
3273 if (context.maybe_derived_type)
3275 for (i = 0; i < outer_type->derived_types.length(); i++)
3276 possible_polymorphic_call_targets_1 (nodes, &inserted,
3277 &matched_vtables,
3278 otr_type,
3279 outer_type->derived_types[i],
3280 otr_token, outer_type->type,
3281 context.offset, &complete,
3282 bases_to_consider,
3283 context.maybe_in_construction);
3285 if (!outer_type->all_derivations_known)
3287 if (!speculative && final_warning_records)
3289 if (complete
3290 && nodes.length () == 1
3291 && warn_suggest_final_types
3292 && !outer_type->derived_types.length ())
3294 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3295 final_warning_records->type_warnings.safe_grow_cleared
3296 (odr_types.length ());
3297 final_warning_records->type_warnings[outer_type->id].count++;
3298 final_warning_records->type_warnings[outer_type->id].dyn_count
3299 += final_warning_records->dyn_count;
3300 final_warning_records->type_warnings[outer_type->id].type
3301 = outer_type->type;
3302 (*slot)->type_warning = outer_type->id + 1;
3304 if (complete
3305 && warn_suggest_final_methods
3306 && nodes.length () == 1
3307 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3308 outer_type->type))
3310 bool existed;
3311 struct decl_warn_count &c =
3312 final_warning_records->decl_warnings.get_or_insert
3313 (nodes[0]->decl, &existed);
3315 if (existed)
3317 c.count++;
3318 c.dyn_count += final_warning_records->dyn_count;
3320 else
3322 c.count = 1;
3323 c.dyn_count = final_warning_records->dyn_count;
3324 c.decl = nodes[0]->decl;
3326 (*slot)->decl_warning = nodes[0]->decl;
3329 complete = false;
3333 if (!speculative)
3335 /* Destructors are never called through construction virtual tables,
3336 because the type is always known. One of entries may be
3337 cxa_pure_virtual so look to at least two of them. */
3338 if (context.maybe_in_construction)
3339 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3340 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3341 context.maybe_in_construction = false;
3342 if (context.maybe_in_construction)
3344 if (type != outer_type
3345 && (!skipped
3346 || (context.maybe_derived_type
3347 && !type_all_derivations_known_p (outer_type->type))))
3348 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3349 context.offset, nodes, &inserted,
3350 &matched_vtables, &complete);
3351 if (skipped)
3352 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3353 for (i = 0; i < bases_to_consider.length(); i++)
3354 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3359 (*slot)->targets = nodes;
3360 (*slot)->complete = complete;
3361 if (completep)
3362 *completep = complete;
3364 timevar_pop (TV_IPA_VIRTUAL_CALL);
3365 return nodes;
3368 bool
3369 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3370 vec<const decl_warn_count*> *vec)
3372 vec->safe_push (&value);
3373 return true;
3376 /* Dump target list TARGETS into FILE. */
3378 static void
3379 dump_targets (FILE *f, vec <cgraph_node *> targets)
3381 unsigned int i;
3383 for (i = 0; i < targets.length (); i++)
3385 char *name = NULL;
3386 if (in_lto_p)
3387 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3388 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3389 if (in_lto_p)
3390 free (name);
3391 if (!targets[i]->definition)
3392 fprintf (f, " (no definition%s)",
3393 DECL_DECLARED_INLINE_P (targets[i]->decl)
3394 ? " inline" : "");
3396 fprintf (f, "\n");
3399 /* Dump all possible targets of a polymorphic call. */
3401 void
3402 dump_possible_polymorphic_call_targets (FILE *f,
3403 tree otr_type,
3404 HOST_WIDE_INT otr_token,
3405 const ipa_polymorphic_call_context &ctx)
3407 vec <cgraph_node *> targets;
3408 bool final;
3409 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3410 unsigned int len;
3412 if (!type)
3413 return;
3414 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3415 ctx,
3416 &final, NULL, false);
3417 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3418 print_generic_expr (f, type->type, TDF_SLIM);
3419 fprintf (f, " token %i\n", (int)otr_token);
3421 ctx.dump (f);
3423 fprintf (f, " %s%s%s%s\n ",
3424 final ? "This is a complete list." :
3425 "This is partial list; extra targets may be defined in other units.",
3426 ctx.maybe_in_construction ? " (base types included)" : "",
3427 ctx.maybe_derived_type ? " (derived types included)" : "",
3428 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3429 len = targets.length ();
3430 dump_targets (f, targets);
3432 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3433 ctx,
3434 &final, NULL, true);
3435 if (targets.length () != len)
3437 fprintf (f, " Speculative targets:");
3438 dump_targets (f, targets);
3440 gcc_assert (targets.length () <= len);
3441 fprintf (f, "\n");
3445 /* Return true if N can be possibly target of a polymorphic call of
3446 OTR_TYPE/OTR_TOKEN. */
3448 bool
3449 possible_polymorphic_call_target_p (tree otr_type,
3450 HOST_WIDE_INT otr_token,
3451 const ipa_polymorphic_call_context &ctx,
3452 struct cgraph_node *n)
3454 vec <cgraph_node *> targets;
3455 unsigned int i;
3456 enum built_in_function fcode;
3457 bool final;
3459 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3460 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3461 == BUILT_IN_UNREACHABLE
3462 || fcode == BUILT_IN_TRAP))
3463 return true;
3465 if (!odr_hash)
3466 return true;
3467 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3468 for (i = 0; i < targets.length (); i++)
3469 if (n->semantically_equivalent_p (targets[i]))
3470 return true;
3472 /* At a moment we allow middle end to dig out new external declarations
3473 as a targets of polymorphic calls. */
3474 if (!final && !n->definition)
3475 return true;
3476 return false;
3481 /* Return true if N can be possibly target of a polymorphic call of
3482 OBJ_TYPE_REF expression REF in STMT. */
3484 bool
3485 possible_polymorphic_call_target_p (tree ref,
3486 gimple stmt,
3487 struct cgraph_node *n)
3489 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3490 tree call_fn = gimple_call_fn (stmt);
3492 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3493 tree_to_uhwi
3494 (OBJ_TYPE_REF_TOKEN (call_fn)),
3495 context,
3500 /* After callgraph construction new external nodes may appear.
3501 Add them into the graph. */
3503 void
3504 update_type_inheritance_graph (void)
3506 struct cgraph_node *n;
3508 if (!odr_hash)
3509 return;
3510 free_polymorphic_call_targets_hash ();
3511 timevar_push (TV_IPA_INHERITANCE);
3512 /* We reconstruct the graph starting from types of all methods seen in the
3513 the unit. */
3514 FOR_EACH_FUNCTION (n)
3515 if (DECL_VIRTUAL_P (n->decl)
3516 && !n->definition
3517 && n->real_symbol_p ())
3518 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3519 timevar_pop (TV_IPA_INHERITANCE);
3523 /* Return true if N looks like likely target of a polymorphic call.
3524 Rule out cxa_pure_virtual, noreturns, function declared cold and
3525 other obvious cases. */
3527 bool
3528 likely_target_p (struct cgraph_node *n)
3530 int flags;
3531 /* cxa_pure_virtual and similar things are not likely. */
3532 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3533 return false;
3534 flags = flags_from_decl_or_type (n->decl);
3535 if (flags & ECF_NORETURN)
3536 return false;
3537 if (lookup_attribute ("cold",
3538 DECL_ATTRIBUTES (n->decl)))
3539 return false;
3540 if (n->frequency < NODE_FREQUENCY_NORMAL)
3541 return false;
3542 /* If there are no live virtual tables referring the target,
3543 the only way the target can be called is an instance coming from other
3544 compilation unit; speculative devirtualization is built around an
3545 assumption that won't happen. */
3546 if (!referenced_from_vtable_p (n))
3547 return false;
3548 return true;
3551 /* Compare type warning records P1 and P2 and choose one with larger count;
3552 helper for qsort. */
3555 type_warning_cmp (const void *p1, const void *p2)
3557 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3558 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3560 if (t1->dyn_count < t2->dyn_count)
3561 return 1;
3562 if (t1->dyn_count > t2->dyn_count)
3563 return -1;
3564 return t2->count - t1->count;
3567 /* Compare decl warning records P1 and P2 and choose one with larger count;
3568 helper for qsort. */
3571 decl_warning_cmp (const void *p1, const void *p2)
3573 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3574 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3576 if (t1->dyn_count < t2->dyn_count)
3577 return 1;
3578 if (t1->dyn_count > t2->dyn_count)
3579 return -1;
3580 return t2->count - t1->count;
3584 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3585 context CTX. */
3587 struct cgraph_node *
3588 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3589 ipa_polymorphic_call_context ctx)
3591 vec <cgraph_node *>targets
3592 = possible_polymorphic_call_targets
3593 (otr_type, otr_token, ctx, NULL, NULL, true);
3594 unsigned int i;
3595 struct cgraph_node *likely_target = NULL;
3597 for (i = 0; i < targets.length (); i++)
3598 if (likely_target_p (targets[i]))
3600 if (likely_target)
3601 return NULL;
3602 likely_target = targets[i];
3604 if (!likely_target
3605 ||!likely_target->definition
3606 || DECL_EXTERNAL (likely_target->decl))
3607 return NULL;
3609 /* Don't use an implicitly-declared destructor (c++/58678). */
3610 struct cgraph_node *non_thunk_target
3611 = likely_target->function_symbol ();
3612 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3613 return NULL;
3614 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3615 && likely_target->can_be_discarded_p ())
3616 return NULL;
3617 return likely_target;
3620 /* The ipa-devirt pass.
3621 When polymorphic call has only one likely target in the unit,
3622 turn it into a speculative call. */
3624 static unsigned int
3625 ipa_devirt (void)
3627 struct cgraph_node *n;
3628 hash_set<void *> bad_call_targets;
3629 struct cgraph_edge *e;
3631 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3632 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3633 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3634 int ndropped = 0;
3636 if (!odr_types_ptr)
3637 return 0;
3639 if (dump_file)
3640 dump_type_inheritance_graph (dump_file);
3642 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3643 This is implemented by setting up final_warning_records that are updated
3644 by get_polymorphic_call_targets.
3645 We need to clear cache in this case to trigger recomputation of all
3646 entries. */
3647 if (warn_suggest_final_methods || warn_suggest_final_types)
3649 final_warning_records = new (final_warning_record);
3650 final_warning_records->type_warnings = vNULL;
3651 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3652 free_polymorphic_call_targets_hash ();
3655 FOR_EACH_DEFINED_FUNCTION (n)
3657 bool update = false;
3658 if (!opt_for_fn (n->decl, flag_devirtualize))
3659 continue;
3660 if (dump_file && n->indirect_calls)
3661 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3662 n->name (), n->order);
3663 for (e = n->indirect_calls; e; e = e->next_callee)
3664 if (e->indirect_info->polymorphic)
3666 struct cgraph_node *likely_target = NULL;
3667 void *cache_token;
3668 bool final;
3670 if (final_warning_records)
3671 final_warning_records->dyn_count = e->count;
3673 vec <cgraph_node *>targets
3674 = possible_polymorphic_call_targets
3675 (e, &final, &cache_token, true);
3676 unsigned int i;
3678 /* Trigger warnings by calculating non-speculative targets. */
3679 if (warn_suggest_final_methods || warn_suggest_final_types)
3680 possible_polymorphic_call_targets (e);
3682 if (dump_file)
3683 dump_possible_polymorphic_call_targets
3684 (dump_file, e);
3686 npolymorphic++;
3688 /* See if the call can be devirtualized by means of ipa-prop's
3689 polymorphic call context propagation. If not, we can just
3690 forget about this call being polymorphic and avoid some heavy
3691 lifting in remove_unreachable_nodes that will otherwise try to
3692 keep all possible targets alive until inlining and in the inliner
3693 itself.
3695 This may need to be revisited once we add further ways to use
3696 the may edges, but it is a resonable thing to do right now. */
3698 if ((e->indirect_info->param_index == -1
3699 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3700 && e->indirect_info->vptr_changed))
3701 && !flag_ltrans_devirtualize)
3703 e->indirect_info->polymorphic = false;
3704 ndropped++;
3705 if (dump_file)
3706 fprintf (dump_file, "Dropping polymorphic call info;"
3707 " it can not be used by ipa-prop\n");
3710 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3711 continue;
3713 if (!e->maybe_hot_p ())
3715 if (dump_file)
3716 fprintf (dump_file, "Call is cold\n\n");
3717 ncold++;
3718 continue;
3720 if (e->speculative)
3722 if (dump_file)
3723 fprintf (dump_file, "Call is already speculated\n\n");
3724 nspeculated++;
3726 /* When dumping see if we agree with speculation. */
3727 if (!dump_file)
3728 continue;
3730 if (bad_call_targets.contains (cache_token))
3732 if (dump_file)
3733 fprintf (dump_file, "Target list is known to be useless\n\n");
3734 nmultiple++;
3735 continue;
3737 for (i = 0; i < targets.length (); i++)
3738 if (likely_target_p (targets[i]))
3740 if (likely_target)
3742 likely_target = NULL;
3743 if (dump_file)
3744 fprintf (dump_file, "More than one likely target\n\n");
3745 nmultiple++;
3746 break;
3748 likely_target = targets[i];
3750 if (!likely_target)
3752 bad_call_targets.add (cache_token);
3753 continue;
3755 /* This is reached only when dumping; check if we agree or disagree
3756 with the speculation. */
3757 if (e->speculative)
3759 struct cgraph_edge *e2;
3760 struct ipa_ref *ref;
3761 e->speculative_call_info (e2, e, ref);
3762 if (e2->callee->ultimate_alias_target ()
3763 == likely_target->ultimate_alias_target ())
3765 fprintf (dump_file, "We agree with speculation\n\n");
3766 nok++;
3768 else
3770 fprintf (dump_file, "We disagree with speculation\n\n");
3771 nwrong++;
3773 continue;
3775 if (!likely_target->definition)
3777 if (dump_file)
3778 fprintf (dump_file, "Target is not a definition\n\n");
3779 nnotdefined++;
3780 continue;
3782 /* Do not introduce new references to external symbols. While we
3783 can handle these just well, it is common for programs to
3784 incorrectly with headers defining methods they are linked
3785 with. */
3786 if (DECL_EXTERNAL (likely_target->decl))
3788 if (dump_file)
3789 fprintf (dump_file, "Target is external\n\n");
3790 nexternal++;
3791 continue;
3793 /* Don't use an implicitly-declared destructor (c++/58678). */
3794 struct cgraph_node *non_thunk_target
3795 = likely_target->function_symbol ();
3796 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3798 if (dump_file)
3799 fprintf (dump_file, "Target is artificial\n\n");
3800 nartificial++;
3801 continue;
3803 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3804 && likely_target->can_be_discarded_p ())
3806 if (dump_file)
3807 fprintf (dump_file, "Target is overwritable\n\n");
3808 noverwritable++;
3809 continue;
3811 else if (dbg_cnt (devirt))
3813 if (dump_enabled_p ())
3815 location_t locus = gimple_location_safe (e->call_stmt);
3816 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3817 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3818 n->name (), n->order,
3819 likely_target->name (),
3820 likely_target->order);
3822 if (!likely_target->can_be_discarded_p ())
3824 cgraph_node *alias;
3825 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3826 if (alias)
3827 likely_target = alias;
3829 nconverted++;
3830 update = true;
3831 e->make_speculative
3832 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3835 if (update)
3836 inline_update_overall_summary (n);
3838 if (warn_suggest_final_methods || warn_suggest_final_types)
3840 if (warn_suggest_final_types)
3842 final_warning_records->type_warnings.qsort (type_warning_cmp);
3843 for (unsigned int i = 0;
3844 i < final_warning_records->type_warnings.length (); i++)
3845 if (final_warning_records->type_warnings[i].count)
3847 tree type = final_warning_records->type_warnings[i].type;
3848 int count = final_warning_records->type_warnings[i].count;
3849 long long dyn_count
3850 = final_warning_records->type_warnings[i].dyn_count;
3852 if (!dyn_count)
3853 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3854 OPT_Wsuggest_final_types, count,
3855 "Declaring type %qD final "
3856 "would enable devirtualization of %i call",
3857 "Declaring type %qD final "
3858 "would enable devirtualization of %i calls",
3859 type,
3860 count);
3861 else
3862 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3863 OPT_Wsuggest_final_types, count,
3864 "Declaring type %qD final "
3865 "would enable devirtualization of %i call "
3866 "executed %lli times",
3867 "Declaring type %qD final "
3868 "would enable devirtualization of %i calls "
3869 "executed %lli times",
3870 type,
3871 count,
3872 dyn_count);
3876 if (warn_suggest_final_methods)
3878 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
3880 final_warning_records->decl_warnings.traverse
3881 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3882 decl_warnings_vec.qsort (decl_warning_cmp);
3883 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3885 tree decl = decl_warnings_vec[i]->decl;
3886 int count = decl_warnings_vec[i]->count;
3887 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3889 if (!dyn_count)
3890 if (DECL_CXX_DESTRUCTOR_P (decl))
3891 warning_n (DECL_SOURCE_LOCATION (decl),
3892 OPT_Wsuggest_final_methods, count,
3893 "Declaring virtual destructor of %qD final "
3894 "would enable devirtualization of %i call",
3895 "Declaring virtual destructor of %qD final "
3896 "would enable devirtualization of %i calls",
3897 DECL_CONTEXT (decl), count);
3898 else
3899 warning_n (DECL_SOURCE_LOCATION (decl),
3900 OPT_Wsuggest_final_methods, count,
3901 "Declaring method %qD final "
3902 "would enable devirtualization of %i call",
3903 "Declaring method %qD final "
3904 "would enable devirtualization of %i calls",
3905 decl, count);
3906 else if (DECL_CXX_DESTRUCTOR_P (decl))
3907 warning_n (DECL_SOURCE_LOCATION (decl),
3908 OPT_Wsuggest_final_methods, count,
3909 "Declaring virtual destructor of %qD final "
3910 "would enable devirtualization of %i call "
3911 "executed %lli times",
3912 "Declaring virtual destructor of %qD final "
3913 "would enable devirtualization of %i calls "
3914 "executed %lli times",
3915 DECL_CONTEXT (decl), count, dyn_count);
3916 else
3917 warning_n (DECL_SOURCE_LOCATION (decl),
3918 OPT_Wsuggest_final_methods, count,
3919 "Declaring method %qD final "
3920 "would enable devirtualization of %i call "
3921 "executed %lli times",
3922 "Declaring method %qD final "
3923 "would enable devirtualization of %i calls "
3924 "executed %lli times",
3925 decl, count, dyn_count);
3929 delete (final_warning_records);
3930 final_warning_records = 0;
3933 if (dump_file)
3934 fprintf (dump_file,
3935 "%i polymorphic calls, %i devirtualized,"
3936 " %i speculatively devirtualized, %i cold\n"
3937 "%i have multiple targets, %i overwritable,"
3938 " %i already speculated (%i agree, %i disagree),"
3939 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3940 npolymorphic, ndevirtualized, nconverted, ncold,
3941 nmultiple, noverwritable, nspeculated, nok, nwrong,
3942 nexternal, nnotdefined, nartificial, ndropped);
3943 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3946 namespace {
3948 const pass_data pass_data_ipa_devirt =
3950 IPA_PASS, /* type */
3951 "devirt", /* name */
3952 OPTGROUP_NONE, /* optinfo_flags */
3953 TV_IPA_DEVIRT, /* tv_id */
3954 0, /* properties_required */
3955 0, /* properties_provided */
3956 0, /* properties_destroyed */
3957 0, /* todo_flags_start */
3958 ( TODO_dump_symtab ), /* todo_flags_finish */
3961 class pass_ipa_devirt : public ipa_opt_pass_d
3963 public:
3964 pass_ipa_devirt (gcc::context *ctxt)
3965 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3966 NULL, /* generate_summary */
3967 NULL, /* write_summary */
3968 NULL, /* read_summary */
3969 NULL, /* write_optimization_summary */
3970 NULL, /* read_optimization_summary */
3971 NULL, /* stmt_fixup */
3972 0, /* function_transform_todo_flags_start */
3973 NULL, /* function_transform */
3974 NULL) /* variable_transform */
3977 /* opt_pass methods: */
3978 virtual bool gate (function *)
3980 /* In LTO, always run the IPA passes and decide on function basis if the
3981 pass is enabled. */
3982 if (in_lto_p)
3983 return true;
3984 return (flag_devirtualize
3985 && (flag_devirtualize_speculatively
3986 || (warn_suggest_final_methods
3987 || warn_suggest_final_types))
3988 && optimize);
3991 virtual unsigned int execute (function *) { return ipa_devirt (); }
3993 }; // class pass_ipa_devirt
3995 } // anon namespace
3997 ipa_opt_pass_d *
3998 make_pass_ipa_devirt (gcc::context *ctxt)
4000 return new pass_ipa_devirt (ctxt);
4003 #include "gt-ipa-devirt.h"