[RTL-ifcvt] PR rtl-optimization/68506: Fix emitting order of insns in IF-THEN-JOIN...
[official-gcc.git] / gcc / ipa-devirt.c
blob1539bb93906396b2222413fbdaf182a224cc3a32
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 "backend.h"
112 #include "rtl.h"
113 #include "tree.h"
114 #include "gimple.h"
115 #include "alloc-pool.h"
116 #include "tree-pass.h"
117 #include "cgraph.h"
118 #include "lto-streamer.h"
119 #include "fold-const.h"
120 #include "print-tree.h"
121 #include "calls.h"
122 #include "ipa-utils.h"
123 #include "gimple-fold.h"
124 #include "symbol-summary.h"
125 #include "ipa-prop.h"
126 #include "ipa-inline.h"
127 #include "demangle.h"
128 #include "dbgcnt.h"
129 #include "gimple-pretty-print.h"
130 #include "intl.h"
132 /* Hash based set of pairs of types. */
133 struct type_pair
135 tree first;
136 tree second;
139 template <>
140 struct default_hash_traits <type_pair> : typed_noop_remove <type_pair>
142 typedef type_pair value_type;
143 typedef type_pair compare_type;
144 static hashval_t
145 hash (type_pair p)
147 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
149 static bool
150 is_empty (type_pair p)
152 return p.first == NULL;
154 static bool
155 is_deleted (type_pair p ATTRIBUTE_UNUSED)
157 return false;
159 static bool
160 equal (const type_pair &a, const type_pair &b)
162 return a.first==b.first && a.second == b.second;
164 static void
165 mark_empty (type_pair &e)
167 e.first = NULL;
171 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
172 hash_set<type_pair> *,
173 location_t, location_t);
175 static bool odr_violation_reported = false;
178 /* Pointer set of all call targets appearing in the cache. */
179 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
181 /* The node of type inheritance graph. For each type unique in
182 One Definition Rule (ODR) sense, we produce one node linking all
183 main variants of types equivalent to it, bases and derived types. */
185 struct GTY(()) odr_type_d
187 /* leader type. */
188 tree type;
189 /* All bases; built only for main variants of types. */
190 vec<odr_type> GTY((skip)) bases;
191 /* All derived types with virtual methods seen in unit;
192 built only for main variants of types. */
193 vec<odr_type> GTY((skip)) derived_types;
195 /* All equivalent types, if more than one. */
196 vec<tree, va_gc> *types;
197 /* Set of all equivalent types, if NON-NULL. */
198 hash_set<tree> * GTY((skip)) types_set;
200 /* Unique ID indexing the type in odr_types array. */
201 int id;
202 /* Is it in anonymous namespace? */
203 bool anonymous_namespace;
204 /* Do we know about all derivations of given type? */
205 bool all_derivations_known;
206 /* Did we report ODR violation here? */
207 bool odr_violated;
208 /* Set when virtual table without RTTI previaled table with. */
209 bool rtti_broken;
212 /* Return true if T is a type with linkage defined. */
214 bool
215 type_with_linkage_p (const_tree t)
217 /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL. */
218 if (!TYPE_CONTEXT (t)
219 || !TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL
220 || !TYPE_STUB_DECL (t))
221 return false;
223 /* In LTO do not get confused by non-C++ produced types or types built
224 with -fno-lto-odr-type-merigng. */
225 if (in_lto_p)
227 /* To support -fno-lto-odr-type-merigng recognize types with vtables
228 to have linkage. */
229 if (RECORD_OR_UNION_TYPE_P (t)
230 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
231 return true;
232 /* Do not accept any other types - we do not know if they were produced
233 by C++ FE. */
234 if (!DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
235 return false;
238 return (RECORD_OR_UNION_TYPE_P (t)
239 || TREE_CODE (t) == ENUMERAL_TYPE);
242 /* Return true if T is in anonymous namespace.
243 This works only on those C++ types with linkage defined. */
245 bool
246 type_in_anonymous_namespace_p (const_tree t)
248 gcc_assert (type_with_linkage_p (t));
250 /* Keep -fno-lto-odr-type-merging working by recognizing classes with vtables
251 properly into anonymous namespaces. */
252 if (RECORD_OR_UNION_TYPE_P (t)
253 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
254 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
256 if (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)))
258 /* C++ FE uses magic <anon> as assembler names of anonymous types.
259 verify that this match with type_in_anonymous_namespace_p. */
260 if (in_lto_p)
261 gcc_checking_assert (!strcmp ("<anon>",
262 IDENTIFIER_POINTER
263 (DECL_ASSEMBLER_NAME (TYPE_NAME (t)))));
264 return true;
266 return false;
269 /* Return true of T is type with One Definition Rule info attached.
270 It means that either it is anonymous type or it has assembler name
271 set. */
273 bool
274 odr_type_p (const_tree t)
276 /* We do not have this information when not in LTO, but we do not need
277 to care, since it is used only for type merging. */
278 gcc_checking_assert (in_lto_p || flag_lto);
280 /* To support -fno-lto-odr-type-merging consider types with vtables ODR. */
281 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
282 return true;
284 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
285 && (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))))
287 /* C++ FE uses magic <anon> as assembler names of anonymous types.
288 verify that this match with type_in_anonymous_namespace_p. */
289 gcc_checking_assert (!type_with_linkage_p (t)
290 || strcmp ("<anon>",
291 IDENTIFIER_POINTER
292 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))))
293 || type_in_anonymous_namespace_p (t));
294 return true;
296 return false;
299 /* Return TRUE if all derived types of T are known and thus
300 we may consider the walk of derived type complete.
302 This is typically true only for final anonymous namespace types and types
303 defined within functions (that may be COMDAT and thus shared across units,
304 but with the same set of derived types). */
306 bool
307 type_all_derivations_known_p (const_tree t)
309 if (TYPE_FINAL_P (t))
310 return true;
311 if (flag_ltrans)
312 return false;
313 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
314 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
315 return true;
316 if (type_in_anonymous_namespace_p (t))
317 return true;
318 return (decl_function_context (TYPE_NAME (t)) != NULL);
321 /* Return TRUE if type's constructors are all visible. */
323 static bool
324 type_all_ctors_visible_p (tree t)
326 return !flag_ltrans
327 && symtab->state >= CONSTRUCTION
328 /* We can not always use type_all_derivations_known_p.
329 For function local types we must assume case where
330 the function is COMDAT and shared in between units.
332 TODO: These cases are quite easy to get, but we need
333 to keep track of C++ privatizing via -Wno-weak
334 as well as the IPA privatizing. */
335 && type_in_anonymous_namespace_p (t);
338 /* Return TRUE if type may have instance. */
340 static bool
341 type_possibly_instantiated_p (tree t)
343 tree vtable;
344 varpool_node *vnode;
346 /* TODO: Add abstract types here. */
347 if (!type_all_ctors_visible_p (t))
348 return true;
350 vtable = BINFO_VTABLE (TYPE_BINFO (t));
351 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
352 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
353 vnode = varpool_node::get (vtable);
354 return vnode && vnode->definition;
357 /* Hash used to unify ODR types based on their mangled name and for anonymous
358 namespace types. */
360 struct odr_name_hasher : pointer_hash <odr_type_d>
362 typedef union tree_node *compare_type;
363 static inline hashval_t hash (const odr_type_d *);
364 static inline bool equal (const odr_type_d *, const tree_node *);
365 static inline void remove (odr_type_d *);
368 /* Has used to unify ODR types based on their associated virtual table.
369 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
370 only polymorphic types. Types with mangled names are inserted to both. */
372 struct odr_vtable_hasher:odr_name_hasher
374 static inline hashval_t hash (const odr_type_d *);
375 static inline bool equal (const odr_type_d *, const tree_node *);
378 /* Return type that was declared with T's name so that T is an
379 qualified variant of it. */
381 static inline tree
382 main_odr_variant (const_tree t)
384 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
385 return TREE_TYPE (TYPE_NAME (t));
386 /* Unnamed types and non-C++ produced types can be compared by variants. */
387 else
388 return TYPE_MAIN_VARIANT (t);
391 static bool
392 can_be_name_hashed_p (tree t)
394 return (!in_lto_p || odr_type_p (t));
397 /* Hash type by its ODR name. */
399 static hashval_t
400 hash_odr_name (const_tree t)
402 gcc_checking_assert (main_odr_variant (t) == t);
404 /* If not in LTO, all main variants are unique, so we can do
405 pointer hash. */
406 if (!in_lto_p)
407 return htab_hash_pointer (t);
409 /* Anonymous types are unique. */
410 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
411 return htab_hash_pointer (t);
413 gcc_checking_assert (TYPE_NAME (t)
414 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
415 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
418 /* Return the computed hashcode for ODR_TYPE. */
420 inline hashval_t
421 odr_name_hasher::hash (const odr_type_d *odr_type)
423 return hash_odr_name (odr_type->type);
426 static bool
427 can_be_vtable_hashed_p (tree t)
429 /* vtable hashing can distinguish only main variants. */
430 if (TYPE_MAIN_VARIANT (t) != t)
431 return false;
432 /* Anonymous namespace types are always handled by name hash. */
433 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
434 return false;
435 return (TREE_CODE (t) == RECORD_TYPE
436 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
439 /* Hash type by assembler name of its vtable. */
441 static hashval_t
442 hash_odr_vtable (const_tree t)
444 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
445 inchash::hash hstate;
447 gcc_checking_assert (in_lto_p);
448 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
449 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
450 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
451 gcc_checking_assert (main_odr_variant (t) == t);
453 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
455 add_expr (TREE_OPERAND (v, 1), hstate);
456 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
459 hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
460 return hstate.end ();
463 /* Return the computed hashcode for ODR_TYPE. */
465 inline hashval_t
466 odr_vtable_hasher::hash (const odr_type_d *odr_type)
468 return hash_odr_vtable (odr_type->type);
471 /* For languages with One Definition Rule, work out if
472 types are the same based on their name.
474 This is non-trivial for LTO where minor differences in
475 the type representation may have prevented type merging
476 to merge two copies of otherwise equivalent type.
478 Until we start streaming mangled type names, this function works
479 only for polymorphic types.
481 When STRICT is true, we compare types by their names for purposes of
482 ODR violation warnings. When strict is false, we consider variants
483 equivalent, becuase it is all that matters for devirtualization machinery.
486 bool
487 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
489 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
491 type1 = main_odr_variant (type1);
492 type2 = main_odr_variant (type2);
493 if (!strict)
495 type1 = TYPE_MAIN_VARIANT (type1);
496 type2 = TYPE_MAIN_VARIANT (type2);
499 if (type1 == type2)
500 return true;
502 if (!in_lto_p)
503 return false;
505 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
506 on the corresponding TYPE_STUB_DECL. */
507 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
508 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
509 return false;
512 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
514 Ideally we should never need types without ODR names here. It can however
515 happen in two cases:
517 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
518 Here testing for equivalence is safe, since their MAIN_VARIANTs are
519 unique.
520 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
521 establish precise ODR equivalency, but for correctness we care only
522 about equivalency on complete polymorphic types. For these we can
523 compare assembler names of their virtual tables. */
524 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
525 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
527 /* See if types are obviously different (i.e. different codes
528 or polymorphic wrt non-polymorphic). This is not strictly correct
529 for ODR violating programs, but we can't do better without streaming
530 ODR names. */
531 if (TREE_CODE (type1) != TREE_CODE (type2))
532 return false;
533 if (TREE_CODE (type1) == RECORD_TYPE
534 && (TYPE_BINFO (type1) == NULL_TREE)
535 != (TYPE_BINFO (type2) == NULL_TREE))
536 return false;
537 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
538 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
539 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
540 return false;
542 /* At the moment we have no way to establish ODR equivalence at LTO
543 other than comparing virtual table pointers of polymorphic types.
544 Eventually we should start saving mangled names in TYPE_NAME.
545 Then this condition will become non-trivial. */
547 if (TREE_CODE (type1) == RECORD_TYPE
548 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
549 && BINFO_VTABLE (TYPE_BINFO (type1))
550 && BINFO_VTABLE (TYPE_BINFO (type2)))
552 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
553 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
554 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
555 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
556 return (operand_equal_p (TREE_OPERAND (v1, 1),
557 TREE_OPERAND (v2, 1), 0)
558 && DECL_ASSEMBLER_NAME
559 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
560 == DECL_ASSEMBLER_NAME
561 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
563 gcc_unreachable ();
565 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
566 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
569 /* Return true if we can decide on ODR equivalency.
571 In non-LTO it is always decide, in LTO however it depends in the type has
572 ODR info attached.
574 When STRICT is false, compare main variants. */
576 bool
577 types_odr_comparable (tree t1, tree t2, bool strict)
579 return (!in_lto_p
580 || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
581 && main_odr_variant (t1))
582 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
583 || (odr_type_p (t1) && odr_type_p (t2))
584 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
585 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
586 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
587 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
590 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
591 known, be conservative and return false. */
593 bool
594 types_must_be_same_for_odr (tree t1, tree t2)
596 if (types_odr_comparable (t1, t2))
597 return types_same_for_odr (t1, t2);
598 else
599 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
602 /* If T is compound type, return type it is based on. */
604 static tree
605 compound_type_base (const_tree t)
607 if (TREE_CODE (t) == ARRAY_TYPE
608 || POINTER_TYPE_P (t)
609 || TREE_CODE (t) == COMPLEX_TYPE
610 || VECTOR_TYPE_P (t))
611 return TREE_TYPE (t);
612 if (TREE_CODE (t) == METHOD_TYPE)
613 return TYPE_METHOD_BASETYPE (t);
614 if (TREE_CODE (t) == OFFSET_TYPE)
615 return TYPE_OFFSET_BASETYPE (t);
616 return NULL_TREE;
619 /* Return true if T is either ODR type or compound type based from it.
620 If the function return true, we know that T is a type originating from C++
621 source even at link-time. */
623 bool
624 odr_or_derived_type_p (const_tree t)
628 if (odr_type_p (t))
629 return true;
630 /* Function type is a tricky one. Basically we can consider it
631 ODR derived if return type or any of the parameters is.
632 We need to check all parameters because LTO streaming merges
633 common types (such as void) and they are not considered ODR then. */
634 if (TREE_CODE (t) == FUNCTION_TYPE)
636 if (TYPE_METHOD_BASETYPE (t))
637 t = TYPE_METHOD_BASETYPE (t);
638 else
640 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
641 return true;
642 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
643 if (odr_or_derived_type_p (TREE_VALUE (t)))
644 return true;
645 return false;
648 else
649 t = compound_type_base (t);
651 while (t);
652 return t;
655 /* Compare types T1 and T2 and return true if they are
656 equivalent. */
658 inline bool
659 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
661 tree t1 = o1->type;
663 gcc_checking_assert (main_odr_variant (t2) == t2);
664 gcc_checking_assert (main_odr_variant (t1) == t1);
665 if (t1 == t2)
666 return true;
667 if (!in_lto_p)
668 return false;
669 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
670 on the corresponding TYPE_STUB_DECL. */
671 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
672 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
673 return false;
674 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
675 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
676 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
677 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
680 /* Compare types T1 and T2 and return true if they are
681 equivalent. */
683 inline bool
684 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
686 tree t1 = o1->type;
688 gcc_checking_assert (main_odr_variant (t2) == t2);
689 gcc_checking_assert (main_odr_variant (t1) == t1);
690 gcc_checking_assert (in_lto_p);
691 t1 = TYPE_MAIN_VARIANT (t1);
692 t2 = TYPE_MAIN_VARIANT (t2);
693 if (t1 == t2)
694 return true;
695 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
696 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
697 return (operand_equal_p (TREE_OPERAND (v1, 1),
698 TREE_OPERAND (v2, 1), 0)
699 && DECL_ASSEMBLER_NAME
700 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
701 == DECL_ASSEMBLER_NAME
702 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
705 /* Free ODR type V. */
707 inline void
708 odr_name_hasher::remove (odr_type_d *v)
710 v->bases.release ();
711 v->derived_types.release ();
712 if (v->types_set)
713 delete v->types_set;
714 ggc_free (v);
717 /* ODR type hash used to look up ODR type based on tree type node. */
719 typedef hash_table<odr_name_hasher> odr_hash_type;
720 static odr_hash_type *odr_hash;
721 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
722 static odr_vtable_hash_type *odr_vtable_hash;
724 /* ODR types are also stored into ODR_TYPE vector to allow consistent
725 walking. Bases appear before derived types. Vector is garbage collected
726 so we won't end up visiting empty types. */
728 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
729 #define odr_types (*odr_types_ptr)
731 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
732 void
733 set_type_binfo (tree type, tree binfo)
735 for (; type; type = TYPE_NEXT_VARIANT (type))
736 if (COMPLETE_TYPE_P (type))
737 TYPE_BINFO (type) = binfo;
738 else
739 gcc_assert (!TYPE_BINFO (type));
742 /* Compare T2 and T2 based on name or structure. */
744 static bool
745 odr_subtypes_equivalent_p (tree t1, tree t2,
746 hash_set<type_pair> *visited,
747 location_t loc1, location_t loc2)
750 /* This can happen in incomplete types that should be handled earlier. */
751 gcc_assert (t1 && t2);
753 t1 = main_odr_variant (t1);
754 t2 = main_odr_variant (t2);
755 if (t1 == t2)
756 return true;
758 /* Anonymous namespace types must match exactly. */
759 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
760 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
761 return false;
763 /* For ODR types be sure to compare their names.
764 To support -wno-odr-type-merging we allow one type to be non-ODR
765 and other ODR even though it is a violation. */
766 if (types_odr_comparable (t1, t2, true))
768 if (!types_same_for_odr (t1, t2, true))
769 return false;
770 /* Limit recursion: If subtypes are ODR types and we know
771 that they are same, be happy. */
772 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated)
773 return true;
776 /* Component types, builtins and possibly violating ODR types
777 have to be compared structurally. */
778 if (TREE_CODE (t1) != TREE_CODE (t2))
779 return false;
780 if (AGGREGATE_TYPE_P (t1)
781 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
782 return false;
784 type_pair pair={t1,t2};
785 if (TYPE_UID (t1) > TYPE_UID (t2))
787 pair.first = t2;
788 pair.second = t1;
790 if (visited->add (pair))
791 return true;
792 return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
795 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
796 violation warnings. */
798 void
799 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
801 int n1, n2;
803 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
805 odr_violation_reported = true;
806 if (DECL_VIRTUAL_P (prevailing->decl))
808 varpool_node *tmp = prevailing;
809 prevailing = vtable;
810 vtable = tmp;
812 if (warning_at (DECL_SOURCE_LOCATION
813 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
814 OPT_Wodr,
815 "virtual table of type %qD violates one definition rule",
816 DECL_CONTEXT (vtable->decl)))
817 inform (DECL_SOURCE_LOCATION (prevailing->decl),
818 "variable of same assembler name as the virtual table is "
819 "defined in another translation unit");
820 return;
822 if (!prevailing->definition || !vtable->definition)
823 return;
825 /* If we do not stream ODR type info, do not bother to do useful compare. */
826 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
827 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
828 return;
830 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
832 if (class_type->odr_violated)
833 return;
835 for (n1 = 0, n2 = 0; true; n1++, n2++)
837 struct ipa_ref *ref1, *ref2;
838 bool end1, end2;
840 end1 = !prevailing->iterate_reference (n1, ref1);
841 end2 = !vtable->iterate_reference (n2, ref2);
843 /* !DECL_VIRTUAL_P means RTTI entry;
844 We warn when RTTI is lost because non-RTTI previals; we silently
845 accept the other case. */
846 while (!end2
847 && (end1
848 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
849 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
850 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
851 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
853 if (!class_type->rtti_broken
854 && warning_at (DECL_SOURCE_LOCATION
855 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
856 OPT_Wodr,
857 "virtual table of type %qD contains RTTI "
858 "information",
859 DECL_CONTEXT (vtable->decl)))
861 inform (DECL_SOURCE_LOCATION
862 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
863 "but is prevailed by one without from other translation "
864 "unit");
865 inform (DECL_SOURCE_LOCATION
866 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
867 "RTTI will not work on this type");
868 class_type->rtti_broken = true;
870 n2++;
871 end2 = !vtable->iterate_reference (n2, ref2);
873 while (!end1
874 && (end2
875 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
876 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
877 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
878 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
880 n1++;
881 end1 = !prevailing->iterate_reference (n1, ref1);
884 /* Finished? */
885 if (end1 && end2)
887 /* Extra paranoia; compare the sizes. We do not have information
888 about virtual inheritance offsets, so just be sure that these
889 match.
890 Do this as very last check so the not very informative error
891 is not output too often. */
892 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
894 class_type->odr_violated = true;
895 if (warning_at (DECL_SOURCE_LOCATION
896 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
897 OPT_Wodr,
898 "virtual table of type %qD violates "
899 "one definition rule ",
900 DECL_CONTEXT (vtable->decl)))
902 inform (DECL_SOURCE_LOCATION
903 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
904 "the conflicting type defined in another translation "
905 "unit has virtual table of different size");
908 return;
911 if (!end1 && !end2)
913 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
914 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
915 continue;
917 class_type->odr_violated = true;
919 /* If the loops above stopped on non-virtual pointer, we have
920 mismatch in RTTI information mangling. */
921 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
922 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
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 with different RTTI information");
936 return;
938 /* At this point both REF1 and REF2 points either to virtual table
939 or virtual method. If one points to virtual table and other to
940 method we can complain the same way as if one table was shorter
941 than other pointing out the extra method. */
942 if (TREE_CODE (ref1->referred->decl)
943 != TREE_CODE (ref2->referred->decl))
945 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
946 end1 = true;
947 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
948 end2 = true;
952 class_type->odr_violated = true;
954 /* Complain about size mismatch. Either we have too many virutal
955 functions or too many virtual table pointers. */
956 if (end1 || end2)
958 if (end1)
960 varpool_node *tmp = prevailing;
961 prevailing = vtable;
962 vtable = tmp;
963 ref1 = ref2;
965 if (warning_at (DECL_SOURCE_LOCATION
966 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
967 OPT_Wodr,
968 "virtual table of type %qD violates "
969 "one definition rule",
970 DECL_CONTEXT (vtable->decl)))
972 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
974 inform (DECL_SOURCE_LOCATION
975 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
976 "the conflicting type defined in another translation "
977 "unit");
978 inform (DECL_SOURCE_LOCATION
979 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
980 "contains additional virtual method %qD",
981 ref1->referred->decl);
983 else
985 inform (DECL_SOURCE_LOCATION
986 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
987 "the conflicting type defined in another translation "
988 "unit has virtual table with more entries");
991 return;
994 /* And in the last case we have either mistmatch in between two virtual
995 methods or two virtual table pointers. */
996 if (warning_at (DECL_SOURCE_LOCATION
997 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
998 "virtual table of type %qD violates "
999 "one definition rule ",
1000 DECL_CONTEXT (vtable->decl)))
1002 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
1004 inform (DECL_SOURCE_LOCATION
1005 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1006 "the conflicting type defined in another translation "
1007 "unit");
1008 gcc_assert (TREE_CODE (ref2->referred->decl)
1009 == FUNCTION_DECL);
1010 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
1011 "virtual method %qD", ref1->referred->decl);
1012 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
1013 "ought to match virtual method %qD but does not",
1014 ref2->referred->decl);
1016 else
1017 inform (DECL_SOURCE_LOCATION
1018 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1019 "the conflicting type defined in another translation "
1020 "unit has virtual table with different contents");
1021 return;
1026 /* Output ODR violation warning about T1 and T2 with REASON.
1027 Display location of ST1 and ST2 if REASON speaks about field or
1028 method of the type.
1029 If WARN is false, do nothing. Set WARNED if warning was indeed
1030 output. */
1032 void
1033 warn_odr (tree t1, tree t2, tree st1, tree st2,
1034 bool warn, bool *warned, const char *reason)
1036 tree decl2 = TYPE_NAME (t2);
1037 if (warned)
1038 *warned = false;
1040 if (!warn || !TYPE_NAME(t1))
1041 return;
1043 /* ODR warnings are output druing LTO streaming; we must apply location
1044 cache for potential warnings to be output correctly. */
1045 if (lto_location_cache::current_cache)
1046 lto_location_cache::current_cache->apply_location_cache ();
1048 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
1049 "type %qT violates the C++ One Definition Rule",
1050 t1))
1051 return;
1052 if (!st1 && !st2)
1054 /* For FIELD_DECL support also case where one of fields is
1055 NULL - this is used when the structures have mismatching number of
1056 elements. */
1057 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
1059 inform (DECL_SOURCE_LOCATION (decl2),
1060 "a different type is defined in another translation unit");
1061 if (!st1)
1063 st1 = st2;
1064 st2 = NULL;
1066 inform (DECL_SOURCE_LOCATION (st1),
1067 "the first difference of corresponding definitions is field %qD",
1068 st1);
1069 if (st2)
1070 decl2 = st2;
1072 else if (TREE_CODE (st1) == FUNCTION_DECL)
1074 inform (DECL_SOURCE_LOCATION (decl2),
1075 "a different type is defined in another translation unit");
1076 inform (DECL_SOURCE_LOCATION (st1),
1077 "the first difference of corresponding definitions is method %qD",
1078 st1);
1079 decl2 = st2;
1081 else
1082 return;
1083 inform (DECL_SOURCE_LOCATION (decl2), reason);
1085 if (warned)
1086 *warned = true;
1089 /* Return ture if T1 and T2 are incompatible and we want to recusively
1090 dive into them from warn_type_mismatch to give sensible answer. */
1092 static bool
1093 type_mismatch_p (tree t1, tree t2)
1095 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1096 && !odr_types_equivalent_p (t1, t2))
1097 return true;
1098 return !types_compatible_p (t1, t2);
1102 /* Types T1 and T2 was found to be incompatible in a context they can't
1103 (either used to declare a symbol of same assembler name or unified by
1104 ODR rule). We already output warning about this, but if possible, output
1105 extra information on how the types mismatch.
1107 This is hard to do in general. We basically handle the common cases.
1109 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1110 themselves do no thave one.*/
1112 void
1113 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1115 /* Location of type is known only if it has TYPE_NAME and the name is
1116 TYPE_DECL. */
1117 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1118 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1119 : UNKNOWN_LOCATION;
1120 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1121 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1122 : UNKNOWN_LOCATION;
1123 bool loc_t2_useful = false;
1125 /* With LTO it is a common case that the location of both types match.
1126 See if T2 has a location that is different from T1. If so, we will
1127 inform user about the location.
1128 Do not consider the location passed to us in LOC1/LOC2 as those are
1129 already output. */
1130 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1132 if (loc_t1 <= BUILTINS_LOCATION)
1133 loc_t2_useful = true;
1134 else
1136 expanded_location xloc1 = expand_location (loc_t1);
1137 expanded_location xloc2 = expand_location (loc_t2);
1139 if (strcmp (xloc1.file, xloc2.file)
1140 || xloc1.line != xloc2.line
1141 || xloc1.column != xloc2.column)
1142 loc_t2_useful = true;
1146 if (loc_t1 <= BUILTINS_LOCATION)
1147 loc_t1 = loc1;
1148 if (loc_t2 <= BUILTINS_LOCATION)
1149 loc_t2 = loc2;
1151 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1153 /* It is a quite common bug to reference anonymous namespace type in
1154 non-anonymous namespace class. */
1155 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1156 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1158 if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
1160 std::swap (t1, t2);
1161 std::swap (loc_t1, loc_t2);
1163 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1164 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1165 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1166 /* Most of the time, the type names will match, do not be unnecesarily
1167 verbose. */
1168 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
1169 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
1170 inform (loc_t1,
1171 "type %qT defined in anonymous namespace can not match "
1172 "type %qT across the translation unit boundary",
1173 t1, t2);
1174 else
1175 inform (loc_t1,
1176 "type %qT defined in anonymous namespace can not match "
1177 "across the translation unit boundary",
1178 t1);
1179 if (loc_t2_useful)
1180 inform (loc_t2,
1181 "the incompatible type defined in another translation unit");
1182 return;
1184 /* If types have mangled ODR names and they are different, it is most
1185 informative to output those.
1186 This also covers types defined in different namespaces. */
1187 if (TYPE_NAME (t1) && TYPE_NAME (t2)
1188 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1189 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1190 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
1191 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
1192 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
1193 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1195 char *name1 = xstrdup (cplus_demangle
1196 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1197 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1198 char *name2 = cplus_demangle
1199 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1200 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1201 if (name1 && name2 && strcmp (name1, name2))
1203 inform (loc_t1,
1204 "type name %<%s%> should match type name %<%s%>",
1205 name1, name2);
1206 if (loc_t2_useful)
1207 inform (loc_t2,
1208 "the incompatible type is defined here");
1209 free (name1);
1210 return;
1212 free (name1);
1214 /* A tricky case are compound types. Often they appear the same in source
1215 code and the mismatch is dragged in by type they are build from.
1216 Look for those differences in subtypes and try to be informative. In other
1217 cases just output nothing because the source code is probably different
1218 and in this case we already output a all necessary info. */
1219 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1221 if (TREE_CODE (t1) == TREE_CODE (t2))
1223 if (TREE_CODE (t1) == ARRAY_TYPE
1224 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1226 tree i1 = TYPE_DOMAIN (t1);
1227 tree i2 = TYPE_DOMAIN (t2);
1229 if (i1 && i2
1230 && TYPE_MAX_VALUE (i1)
1231 && TYPE_MAX_VALUE (i2)
1232 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1233 TYPE_MAX_VALUE (i2), 0))
1235 inform (loc,
1236 "array types have different bounds");
1237 return;
1240 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1241 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1242 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1243 else if (TREE_CODE (t1) == METHOD_TYPE
1244 || TREE_CODE (t1) == FUNCTION_TYPE)
1246 tree parms1 = NULL, parms2 = NULL;
1247 int count = 1;
1249 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1251 inform (loc, "return value type mismatch");
1252 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1253 loc_t2);
1254 return;
1256 if (prototype_p (t1) && prototype_p (t2))
1257 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1258 parms1 && parms2;
1259 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1260 count++)
1262 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1264 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1265 inform (loc,
1266 "implicit this pointer type mismatch");
1267 else
1268 inform (loc,
1269 "type mismatch in parameter %i",
1270 count - (TREE_CODE (t1) == METHOD_TYPE));
1271 warn_types_mismatch (TREE_VALUE (parms1),
1272 TREE_VALUE (parms2),
1273 loc_t1, loc_t2);
1274 return;
1277 if (parms1 || parms2)
1279 inform (loc,
1280 "types have different parameter counts");
1281 return;
1285 return;
1288 if (types_odr_comparable (t1, t2, true)
1289 && types_same_for_odr (t1, t2, true))
1290 inform (loc_t1,
1291 "type %qT itself violate the C++ One Definition Rule", t1);
1292 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1293 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1294 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1295 return;
1296 else
1297 inform (loc_t1, "type %qT should match type %qT",
1298 t1, t2);
1299 if (loc_t2_useful)
1300 inform (loc_t2, "the incompatible type is defined here");
1303 /* Compare T1 and T2, report ODR violations if WARN is true and set
1304 WARNED to true if anything is reported. Return true if types match.
1305 If true is returned, the types are also compatible in the sense of
1306 gimple_canonical_types_compatible_p.
1307 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1308 about the type if the type itself do not have location. */
1310 static bool
1311 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1312 hash_set<type_pair> *visited,
1313 location_t loc1, location_t loc2)
1315 /* Check first for the obvious case of pointer identity. */
1316 if (t1 == t2)
1317 return true;
1318 gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
1319 gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
1321 /* Can't be the same type if the types don't have the same code. */
1322 if (TREE_CODE (t1) != TREE_CODE (t2))
1324 warn_odr (t1, t2, NULL, NULL, warn, warned,
1325 G_("a different type is defined in another translation unit"));
1326 return false;
1329 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1331 warn_odr (t1, t2, NULL, NULL, warn, warned,
1332 G_("a type with different qualifiers is defined in another "
1333 "translation unit"));
1334 return false;
1337 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1338 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1340 /* We can not trip this when comparing ODR types, only when trying to
1341 match different ODR derivations from different declarations.
1342 So WARN should be always false. */
1343 gcc_assert (!warn);
1344 return false;
1347 if (comp_type_attributes (t1, t2) != 1)
1349 warn_odr (t1, t2, NULL, NULL, warn, warned,
1350 G_("a type with different attributes "
1351 "is defined in another translation unit"));
1352 return false;
1355 if (TREE_CODE (t1) == ENUMERAL_TYPE
1356 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1358 tree v1, v2;
1359 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1360 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1362 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1364 warn_odr (t1, t2, NULL, NULL, warn, warned,
1365 G_("an enum with different value name"
1366 " is defined in another translation unit"));
1367 return false;
1369 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1370 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1371 DECL_INITIAL (TREE_VALUE (v2)), 0))
1373 warn_odr (t1, t2, NULL, NULL, warn, warned,
1374 G_("an enum with different values is defined"
1375 " in another translation unit"));
1376 return false;
1379 if (v1 || v2)
1381 warn_odr (t1, t2, NULL, NULL, warn, warned,
1382 G_("an enum with mismatching number of values "
1383 "is defined in another translation unit"));
1384 return false;
1388 /* Non-aggregate types can be handled cheaply. */
1389 if (INTEGRAL_TYPE_P (t1)
1390 || SCALAR_FLOAT_TYPE_P (t1)
1391 || FIXED_POINT_TYPE_P (t1)
1392 || TREE_CODE (t1) == VECTOR_TYPE
1393 || TREE_CODE (t1) == COMPLEX_TYPE
1394 || TREE_CODE (t1) == OFFSET_TYPE
1395 || POINTER_TYPE_P (t1))
1397 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1399 warn_odr (t1, t2, NULL, NULL, warn, warned,
1400 G_("a type with different precision is defined "
1401 "in another translation unit"));
1402 return false;
1404 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1406 warn_odr (t1, t2, NULL, NULL, warn, warned,
1407 G_("a type with different signedness is defined "
1408 "in another translation unit"));
1409 return false;
1412 if (TREE_CODE (t1) == INTEGER_TYPE
1413 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1415 /* char WRT uint_8? */
1416 warn_odr (t1, t2, NULL, NULL, warn, warned,
1417 G_("a different type is defined in another "
1418 "translation unit"));
1419 return false;
1422 /* For canonical type comparisons we do not want to build SCCs
1423 so we cannot compare pointed-to types. But we can, for now,
1424 require the same pointed-to type kind and match what
1425 useless_type_conversion_p would do. */
1426 if (POINTER_TYPE_P (t1))
1428 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1429 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1431 warn_odr (t1, t2, NULL, NULL, warn, warned,
1432 G_("it is defined as a pointer in different address "
1433 "space in another translation unit"));
1434 return false;
1437 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1438 visited, loc1, loc2))
1440 warn_odr (t1, t2, NULL, NULL, warn, warned,
1441 G_("it is defined as a pointer to different type "
1442 "in another translation unit"));
1443 if (warn && warned)
1444 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1445 loc1, loc2);
1446 return false;
1450 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1451 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1452 visited, loc1, loc2))
1454 /* Probably specific enough. */
1455 warn_odr (t1, t2, NULL, NULL, warn, warned,
1456 G_("a different type is defined "
1457 "in another translation unit"));
1458 if (warn && warned)
1459 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1460 return false;
1463 /* Do type-specific comparisons. */
1464 else switch (TREE_CODE (t1))
1466 case ARRAY_TYPE:
1468 /* Array types are the same if the element types are the same and
1469 the number of elements are the same. */
1470 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1471 visited, loc1, loc2))
1473 warn_odr (t1, t2, NULL, NULL, warn, warned,
1474 G_("a different type is defined in another "
1475 "translation unit"));
1476 if (warn && warned)
1477 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1479 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1480 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1481 == TYPE_NONALIASED_COMPONENT (t2));
1483 tree i1 = TYPE_DOMAIN (t1);
1484 tree i2 = TYPE_DOMAIN (t2);
1486 /* For an incomplete external array, the type domain can be
1487 NULL_TREE. Check this condition also. */
1488 if (i1 == NULL_TREE || i2 == NULL_TREE)
1489 return true;
1491 tree min1 = TYPE_MIN_VALUE (i1);
1492 tree min2 = TYPE_MIN_VALUE (i2);
1493 tree max1 = TYPE_MAX_VALUE (i1);
1494 tree max2 = TYPE_MAX_VALUE (i2);
1496 /* In C++, minimums should be always 0. */
1497 gcc_assert (min1 == min2);
1498 if (!operand_equal_p (max1, max2, 0))
1500 warn_odr (t1, t2, NULL, NULL, warn, warned,
1501 G_("an array of different size is defined "
1502 "in another translation unit"));
1503 return false;
1506 break;
1508 case METHOD_TYPE:
1509 case FUNCTION_TYPE:
1510 /* Function types are the same if the return type and arguments types
1511 are the same. */
1512 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1513 visited, loc1, loc2))
1515 warn_odr (t1, t2, NULL, NULL, warn, warned,
1516 G_("has different return value "
1517 "in another translation unit"));
1518 if (warn && warned)
1519 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1520 return false;
1523 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1524 || !prototype_p (t1) || !prototype_p (t2))
1525 return true;
1526 else
1528 tree parms1, parms2;
1530 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1531 parms1 && parms2;
1532 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1534 if (!odr_subtypes_equivalent_p
1535 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
1536 loc1, loc2))
1538 warn_odr (t1, t2, NULL, NULL, warn, warned,
1539 G_("has different parameters in another "
1540 "translation unit"));
1541 if (warn && warned)
1542 warn_types_mismatch (TREE_VALUE (parms1),
1543 TREE_VALUE (parms2), loc1, loc2);
1544 return false;
1548 if (parms1 || parms2)
1550 warn_odr (t1, t2, NULL, NULL, warn, warned,
1551 G_("has different parameters "
1552 "in another translation unit"));
1553 return false;
1556 return true;
1559 case RECORD_TYPE:
1560 case UNION_TYPE:
1561 case QUAL_UNION_TYPE:
1563 tree f1, f2;
1565 /* For aggregate types, all the fields must be the same. */
1566 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1568 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1569 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1570 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1572 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1573 warn_odr (t1, t2, NULL, NULL, warn, warned,
1574 G_("a type defined in another translation unit "
1575 "is not polymorphic"));
1576 else
1577 warn_odr (t1, t2, NULL, NULL, warn, warned,
1578 G_("a type defined in another translation unit "
1579 "is polymorphic"));
1580 return false;
1582 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1583 f1 || f2;
1584 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1586 /* Skip non-fields. */
1587 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1588 f1 = TREE_CHAIN (f1);
1589 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1590 f2 = TREE_CHAIN (f2);
1591 if (!f1 || !f2)
1592 break;
1593 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1595 warn_odr (t1, t2, NULL, NULL, warn, warned,
1596 G_("a type with different virtual table pointers"
1597 " is defined in another translation unit"));
1598 return false;
1600 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1602 warn_odr (t1, t2, NULL, NULL, warn, warned,
1603 G_("a type with different bases is defined "
1604 "in another translation unit"));
1605 return false;
1607 if (DECL_NAME (f1) != DECL_NAME (f2)
1608 && !DECL_ARTIFICIAL (f1))
1610 warn_odr (t1, t2, f1, f2, warn, warned,
1611 G_("a field with different name is defined "
1612 "in another translation unit"));
1613 return false;
1615 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1616 TREE_TYPE (f2), visited,
1617 loc1, loc2))
1619 /* Do not warn about artificial fields and just go into
1620 generic field mismatch warning. */
1621 if (DECL_ARTIFICIAL (f1))
1622 break;
1624 warn_odr (t1, t2, f1, f2, warn, warned,
1625 G_("a field of same name but different type "
1626 "is defined in another translation unit"));
1627 if (warn && warned)
1628 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1629 return false;
1631 if (!gimple_compare_field_offset (f1, f2))
1633 /* Do not warn about artificial fields and just go into
1634 generic field mismatch warning. */
1635 if (DECL_ARTIFICIAL (f1))
1636 break;
1637 warn_odr (t1, t2, f1, f2, warn, warned,
1638 G_("fields has different layout "
1639 "in another translation unit"));
1640 return false;
1642 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1643 == DECL_NONADDRESSABLE_P (f2));
1646 /* If one aggregate has more fields than the other, they
1647 are not the same. */
1648 if (f1 || f2)
1650 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1651 warn_odr (t1, t2, NULL, NULL, warn, warned,
1652 G_("a type with different virtual table pointers"
1653 " is defined in another translation unit"));
1654 else if ((f1 && DECL_ARTIFICIAL (f1))
1655 || (f2 && DECL_ARTIFICIAL (f2)))
1656 warn_odr (t1, t2, NULL, NULL, warn, warned,
1657 G_("a type with different bases is defined "
1658 "in another translation unit"));
1659 else
1660 warn_odr (t1, t2, f1, f2, warn, warned,
1661 G_("a type with different number of fields "
1662 "is defined in another translation unit"));
1664 return false;
1666 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1667 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1))
1668 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2))
1669 && odr_type_p (TYPE_MAIN_VARIANT (t1))
1670 && odr_type_p (TYPE_MAIN_VARIANT (t2))
1671 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1672 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1674 /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
1675 if it is non-NULL so this loop will never realy execute. */
1676 if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1)) != error_mark_node
1677 && TYPE_METHODS (TYPE_MAIN_VARIANT (t2)) != error_mark_node)
1678 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1679 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1680 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1682 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1684 warn_odr (t1, t2, f1, f2, warn, warned,
1685 G_("a different method of same type "
1686 "is defined in another "
1687 "translation unit"));
1688 return false;
1690 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1692 warn_odr (t1, t2, f1, f2, warn, warned,
1693 G_("s definition that differs by virtual "
1694 "keyword in another translation unit"));
1695 return false;
1697 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1699 warn_odr (t1, t2, f1, f2, warn, warned,
1700 G_("virtual table layout differs "
1701 "in another translation unit"));
1702 return false;
1704 if (odr_subtypes_equivalent_p (TREE_TYPE (f1),
1705 TREE_TYPE (f2), visited,
1706 loc1, loc2))
1708 warn_odr (t1, t2, f1, f2, warn, warned,
1709 G_("method with incompatible type is "
1710 "defined in another translation unit"));
1711 return false;
1714 if ((f1 == NULL) != (f2 == NULL))
1716 warn_odr (t1, t2, NULL, NULL, warn, warned,
1717 G_("a type with different number of methods "
1718 "is defined in another translation unit"));
1719 return false;
1723 break;
1725 case VOID_TYPE:
1726 case NULLPTR_TYPE:
1727 break;
1729 default:
1730 debug_tree (t1);
1731 gcc_unreachable ();
1734 /* Those are better to come last as they are utterly uninformative. */
1735 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1736 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1738 warn_odr (t1, t2, NULL, NULL, warn, warned,
1739 G_("a type with different size "
1740 "is defined in another translation unit"));
1741 return false;
1743 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1744 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1746 warn_odr (t1, t2, NULL, NULL, warn, warned,
1747 G_("a type with different alignment "
1748 "is defined in another translation unit"));
1749 return false;
1751 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1752 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1753 TYPE_SIZE_UNIT (t2), 0));
1754 return true;
1757 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1759 bool
1760 odr_types_equivalent_p (tree type1, tree type2)
1762 gcc_checking_assert (odr_or_derived_type_p (type1)
1763 && odr_or_derived_type_p (type2));
1765 hash_set<type_pair> visited;
1766 return odr_types_equivalent_p (type1, type2, false, NULL,
1767 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1770 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1771 from VAL->type. This may happen in LTO where tree merging did not merge
1772 all variants of the same type or due to ODR violation.
1774 Analyze and report ODR violations and add type to duplicate list.
1775 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1776 this is first time we see definition of a class return true so the
1777 base types are analyzed. */
1779 static bool
1780 add_type_duplicate (odr_type val, tree type)
1782 bool build_bases = false;
1783 bool prevail = false;
1784 bool odr_must_violate = false;
1786 if (!val->types_set)
1787 val->types_set = new hash_set<tree>;
1789 /* Chose polymorphic type as leader (this happens only in case of ODR
1790 violations. */
1791 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1792 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1793 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1794 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1796 prevail = true;
1797 build_bases = true;
1799 /* Always prefer complete type to be the leader. */
1800 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1802 prevail = true;
1803 build_bases = TYPE_BINFO (type);
1805 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1807 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1808 && TREE_CODE (type) == ENUMERAL_TYPE
1809 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1810 prevail = true;
1811 else if (TREE_CODE (val->type) == RECORD_TYPE
1812 && TREE_CODE (type) == RECORD_TYPE
1813 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1815 gcc_assert (!val->bases.length ());
1816 build_bases = true;
1817 prevail = true;
1820 if (prevail)
1821 std::swap (val->type, type);
1823 val->types_set->add (type);
1825 /* If we now have a mangled name, be sure to record it to val->type
1826 so ODR hash can work. */
1828 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1829 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1830 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1832 bool merge = true;
1833 bool base_mismatch = false;
1834 unsigned int i;
1835 bool warned = false;
1836 hash_set<type_pair> visited;
1838 gcc_assert (in_lto_p);
1839 vec_safe_push (val->types, type);
1841 /* If both are class types, compare the bases. */
1842 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1843 && TREE_CODE (val->type) == RECORD_TYPE
1844 && TREE_CODE (type) == RECORD_TYPE
1845 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1847 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1848 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1850 if (!flag_ltrans && !warned && !val->odr_violated)
1852 tree extra_base;
1853 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1854 "a type with the same name but different "
1855 "number of polymorphic bases is "
1856 "defined in another translation unit");
1857 if (warned)
1859 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1860 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1861 extra_base = BINFO_BASE_BINFO
1862 (TYPE_BINFO (type),
1863 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1864 else
1865 extra_base = BINFO_BASE_BINFO
1866 (TYPE_BINFO (val->type),
1867 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1868 tree extra_base_type = BINFO_TYPE (extra_base);
1869 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1870 "the extra base is defined here");
1873 base_mismatch = true;
1875 else
1876 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1878 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1879 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1880 tree type1 = BINFO_TYPE (base1);
1881 tree type2 = BINFO_TYPE (base2);
1883 if (types_odr_comparable (type1, type2))
1885 if (!types_same_for_odr (type1, type2))
1886 base_mismatch = true;
1888 else
1889 if (!odr_types_equivalent_p (type1, type2))
1890 base_mismatch = true;
1891 if (base_mismatch)
1893 if (!warned && !val->odr_violated)
1895 warn_odr (type, val->type, NULL, NULL,
1896 !warned, &warned,
1897 "a type with the same name but different base "
1898 "type is defined in another translation unit");
1899 if (warned)
1900 warn_types_mismatch (type1, type2,
1901 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1903 break;
1905 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1907 base_mismatch = true;
1908 if (!warned && !val->odr_violated)
1909 warn_odr (type, val->type, NULL, NULL,
1910 !warned, &warned,
1911 "a type with the same name but different base "
1912 "layout is defined in another translation unit");
1913 break;
1915 /* One of bases is not of complete type. */
1916 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1918 /* If we have a polymorphic type info specified for TYPE1
1919 but not for TYPE2 we possibly missed a base when recording
1920 VAL->type earlier.
1921 Be sure this does not happen. */
1922 if (TYPE_BINFO (type1)
1923 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1924 && !build_bases)
1925 odr_must_violate = true;
1926 break;
1928 /* One base is polymorphic and the other not.
1929 This ought to be diagnosed earlier, but do not ICE in the
1930 checking bellow. */
1931 else if (TYPE_BINFO (type1)
1932 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1933 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1935 if (!warned && !val->odr_violated)
1936 warn_odr (type, val->type, NULL, NULL,
1937 !warned, &warned,
1938 "a base of the type is polymorphic only in one "
1939 "translation unit");
1940 base_mismatch = true;
1941 break;
1944 if (base_mismatch)
1946 merge = false;
1947 odr_violation_reported = true;
1948 val->odr_violated = true;
1950 if (symtab->dump_file)
1952 fprintf (symtab->dump_file, "ODR base violation\n");
1954 print_node (symtab->dump_file, "", val->type, 0);
1955 putc ('\n',symtab->dump_file);
1956 print_node (symtab->dump_file, "", type, 0);
1957 putc ('\n',symtab->dump_file);
1962 /* Next compare memory layout. */
1963 if (!odr_types_equivalent_p (val->type, type,
1964 !flag_ltrans && !val->odr_violated && !warned,
1965 &warned, &visited,
1966 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1967 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1969 merge = false;
1970 odr_violation_reported = true;
1971 val->odr_violated = true;
1972 if (symtab->dump_file)
1974 fprintf (symtab->dump_file, "ODR violation\n");
1976 print_node (symtab->dump_file, "", val->type, 0);
1977 putc ('\n',symtab->dump_file);
1978 print_node (symtab->dump_file, "", type, 0);
1979 putc ('\n',symtab->dump_file);
1982 gcc_assert (val->odr_violated || !odr_must_violate);
1983 /* Sanity check that all bases will be build same way again. */
1984 if (flag_checking
1985 && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1986 && TREE_CODE (val->type) == RECORD_TYPE
1987 && TREE_CODE (type) == RECORD_TYPE
1988 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1989 && !val->odr_violated
1990 && !base_mismatch && val->bases.length ())
1992 unsigned int num_poly_bases = 0;
1993 unsigned int j;
1995 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1996 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1997 (TYPE_BINFO (type), i)))
1998 num_poly_bases++;
1999 gcc_assert (num_poly_bases == val->bases.length ());
2000 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
2001 i++)
2002 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
2003 (TYPE_BINFO (type), i)))
2005 odr_type base = get_odr_type
2006 (BINFO_TYPE
2007 (BINFO_BASE_BINFO (TYPE_BINFO (type),
2008 i)),
2009 true);
2010 gcc_assert (val->bases[j] == base);
2011 j++;
2016 /* Regularize things a little. During LTO same types may come with
2017 different BINFOs. Either because their virtual table was
2018 not merged by tree merging and only later at decl merging or
2019 because one type comes with external vtable, while other
2020 with internal. We want to merge equivalent binfos to conserve
2021 memory and streaming overhead.
2023 The external vtables are more harmful: they contain references
2024 to external declarations of methods that may be defined in the
2025 merged LTO unit. For this reason we absolutely need to remove
2026 them and replace by internal variants. Not doing so will lead
2027 to incomplete answers from possible_polymorphic_call_targets.
2029 FIXME: disable for now; because ODR types are now build during
2030 streaming in, the variants do not need to be linked to the type,
2031 yet. We need to do the merging in cleanup pass to be implemented
2032 soon. */
2033 if (!flag_ltrans && merge
2034 && 0
2035 && TREE_CODE (val->type) == RECORD_TYPE
2036 && TREE_CODE (type) == RECORD_TYPE
2037 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
2038 && TYPE_MAIN_VARIANT (type) == type
2039 && TYPE_MAIN_VARIANT (val->type) == val->type
2040 && BINFO_VTABLE (TYPE_BINFO (val->type))
2041 && BINFO_VTABLE (TYPE_BINFO (type)))
2043 tree master_binfo = TYPE_BINFO (val->type);
2044 tree v1 = BINFO_VTABLE (master_binfo);
2045 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
2047 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
2049 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
2050 && operand_equal_p (TREE_OPERAND (v1, 1),
2051 TREE_OPERAND (v2, 1), 0));
2052 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
2053 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
2055 gcc_assert (DECL_ASSEMBLER_NAME (v1)
2056 == DECL_ASSEMBLER_NAME (v2));
2058 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
2060 unsigned int i;
2062 set_type_binfo (val->type, TYPE_BINFO (type));
2063 for (i = 0; i < val->types->length (); i++)
2065 if (TYPE_BINFO ((*val->types)[i])
2066 == master_binfo)
2067 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
2069 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
2071 else
2072 set_type_binfo (type, master_binfo);
2074 return build_bases;
2077 /* Get ODR type hash entry for TYPE. If INSERT is true, create
2078 possibly new entry. */
2080 odr_type
2081 get_odr_type (tree type, bool insert)
2083 odr_type_d **slot = NULL;
2084 odr_type_d **vtable_slot = NULL;
2085 odr_type val = NULL;
2086 hashval_t hash;
2087 bool build_bases = false;
2088 bool insert_to_odr_array = false;
2089 int base_id = -1;
2091 type = main_odr_variant (type);
2093 gcc_checking_assert (can_be_name_hashed_p (type)
2094 || can_be_vtable_hashed_p (type));
2096 /* Lookup entry, first try name hash, fallback to vtable hash. */
2097 if (can_be_name_hashed_p (type))
2099 hash = hash_odr_name (type);
2100 slot = odr_hash->find_slot_with_hash (type, hash,
2101 insert ? INSERT : NO_INSERT);
2103 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2105 hash = hash_odr_vtable (type);
2106 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2107 insert ? INSERT : NO_INSERT);
2110 if (!slot && !vtable_slot)
2111 return NULL;
2113 /* See if we already have entry for type. */
2114 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2116 if (slot && *slot)
2118 val = *slot;
2119 if (flag_checking
2120 && in_lto_p && can_be_vtable_hashed_p (type))
2122 hash = hash_odr_vtable (type);
2123 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2124 NO_INSERT);
2125 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2126 vtable_slot = NULL;
2129 else if (*vtable_slot)
2130 val = *vtable_slot;
2132 if (val->type != type
2133 && (!val->types_set || !val->types_set->add (type)))
2135 gcc_assert (insert);
2136 /* We have type duplicate, but it may introduce vtable name or
2137 mangled name; be sure to keep hashes in sync. */
2138 if (in_lto_p && can_be_vtable_hashed_p (type)
2139 && (!vtable_slot || !*vtable_slot))
2141 if (!vtable_slot)
2143 hash = hash_odr_vtable (type);
2144 vtable_slot = odr_vtable_hash->find_slot_with_hash
2145 (type, hash, INSERT);
2146 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2148 *vtable_slot = val;
2150 if (slot && !*slot)
2151 *slot = val;
2152 build_bases = add_type_duplicate (val, type);
2155 else
2157 val = ggc_cleared_alloc<odr_type_d> ();
2158 val->type = type;
2159 val->bases = vNULL;
2160 val->derived_types = vNULL;
2161 if (type_with_linkage_p (type))
2162 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2163 else
2164 val->anonymous_namespace = 0;
2165 build_bases = COMPLETE_TYPE_P (val->type);
2166 insert_to_odr_array = true;
2167 if (slot)
2168 *slot = val;
2169 if (vtable_slot)
2170 *vtable_slot = val;
2173 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2174 && type_with_linkage_p (type)
2175 && type == TYPE_MAIN_VARIANT (type))
2177 tree binfo = TYPE_BINFO (type);
2178 unsigned int i;
2180 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2182 val->all_derivations_known = type_all_derivations_known_p (type);
2183 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2184 /* For now record only polymorphic types. other are
2185 pointless for devirtualization and we can not precisely
2186 determine ODR equivalency of these during LTO. */
2187 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2189 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2190 odr_type base = get_odr_type (base_type, true);
2191 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2192 base->derived_types.safe_push (val);
2193 val->bases.safe_push (base);
2194 if (base->id > base_id)
2195 base_id = base->id;
2198 /* Ensure that type always appears after bases. */
2199 if (insert_to_odr_array)
2201 if (odr_types_ptr)
2202 val->id = odr_types.length ();
2203 vec_safe_push (odr_types_ptr, val);
2205 else if (base_id > val->id)
2207 odr_types[val->id] = 0;
2208 /* Be sure we did not recorded any derived types; these may need
2209 renumbering too. */
2210 gcc_assert (val->derived_types.length() == 0);
2211 if (odr_types_ptr)
2212 val->id = odr_types.length ();
2213 vec_safe_push (odr_types_ptr, val);
2215 return val;
2218 /* Add TYPE od ODR type hash. */
2220 void
2221 register_odr_type (tree type)
2223 if (!odr_hash)
2225 odr_hash = new odr_hash_type (23);
2226 if (in_lto_p)
2227 odr_vtable_hash = new odr_vtable_hash_type (23);
2229 /* Arrange things to be nicer and insert main variants first.
2230 ??? fundamental prerecorded types do not have mangled names; this
2231 makes it possible that non-ODR type is main_odr_variant of ODR type.
2232 Things may get smoother if LTO FE set mangled name of those types same
2233 way as C++ FE does. */
2234 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
2235 && odr_type_p (TYPE_MAIN_VARIANT (type)))
2236 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2237 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
2238 get_odr_type (type, true);
2241 /* Return true if type is known to have no derivations. */
2243 bool
2244 type_known_to_have_no_derivations_p (tree t)
2246 return (type_all_derivations_known_p (t)
2247 && (TYPE_FINAL_P (t)
2248 || (odr_hash
2249 && !get_odr_type (t, true)->derived_types.length())));
2252 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2253 recursive printing. */
2255 static void
2256 dump_odr_type (FILE *f, odr_type t, int indent=0)
2258 unsigned int i;
2259 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2260 print_generic_expr (f, t->type, TDF_SLIM);
2261 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2262 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2263 if (TYPE_NAME (t->type))
2265 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2266 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2267 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2268 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2269 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2270 IDENTIFIER_POINTER
2271 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2273 if (t->bases.length ())
2275 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2276 for (i = 0; i < t->bases.length (); i++)
2277 fprintf (f, " %i", t->bases[i]->id);
2278 fprintf (f, "\n");
2280 if (t->derived_types.length ())
2282 fprintf (f, "%*s derived types:\n", indent * 2, "");
2283 for (i = 0; i < t->derived_types.length (); i++)
2284 dump_odr_type (f, t->derived_types[i], indent + 1);
2286 fprintf (f, "\n");
2289 /* Dump the type inheritance graph. */
2291 static void
2292 dump_type_inheritance_graph (FILE *f)
2294 unsigned int i;
2295 if (!odr_types_ptr)
2296 return;
2297 fprintf (f, "\n\nType inheritance graph:\n");
2298 for (i = 0; i < odr_types.length (); i++)
2300 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2301 dump_odr_type (f, odr_types[i]);
2303 for (i = 0; i < odr_types.length (); i++)
2305 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2307 unsigned int j;
2308 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2309 print_node (f, "", odr_types[i]->type, 0);
2310 for (j = 0; j < odr_types[i]->types->length (); j++)
2312 tree t;
2313 fprintf (f, "duplicate #%i\n", j);
2314 print_node (f, "", (*odr_types[i]->types)[j], 0);
2315 t = (*odr_types[i]->types)[j];
2316 while (TYPE_P (t) && TYPE_CONTEXT (t))
2318 t = TYPE_CONTEXT (t);
2319 print_node (f, "", t, 0);
2321 putc ('\n',f);
2327 /* Initialize IPA devirt and build inheritance tree graph. */
2329 void
2330 build_type_inheritance_graph (void)
2332 struct symtab_node *n;
2333 FILE *inheritance_dump_file;
2334 int flags;
2336 if (odr_hash)
2337 return;
2338 timevar_push (TV_IPA_INHERITANCE);
2339 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2340 odr_hash = new odr_hash_type (23);
2341 if (in_lto_p)
2342 odr_vtable_hash = new odr_vtable_hash_type (23);
2344 /* We reconstruct the graph starting of types of all methods seen in the
2345 the unit. */
2346 FOR_EACH_SYMBOL (n)
2347 if (is_a <cgraph_node *> (n)
2348 && DECL_VIRTUAL_P (n->decl)
2349 && n->real_symbol_p ())
2350 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2352 /* Look also for virtual tables of types that do not define any methods.
2354 We need it in a case where class B has virtual base of class A
2355 re-defining its virtual method and there is class C with no virtual
2356 methods with B as virtual base.
2358 Here we output B's virtual method in two variant - for non-virtual
2359 and virtual inheritance. B's virtual table has non-virtual version,
2360 while C's has virtual.
2362 For this reason we need to know about C in order to include both
2363 variants of B. More correctly, record_target_from_binfo should
2364 add both variants of the method when walking B, but we have no
2365 link in between them.
2367 We rely on fact that either the method is exported and thus we
2368 assume it is called externally or C is in anonymous namespace and
2369 thus we will see the vtable. */
2371 else if (is_a <varpool_node *> (n)
2372 && DECL_VIRTUAL_P (n->decl)
2373 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2374 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2375 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2376 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2377 if (inheritance_dump_file)
2379 dump_type_inheritance_graph (inheritance_dump_file);
2380 dump_end (TDI_inheritance, inheritance_dump_file);
2382 timevar_pop (TV_IPA_INHERITANCE);
2385 /* Return true if N has reference from live virtual table
2386 (and thus can be a destination of polymorphic call).
2387 Be conservatively correct when callgraph is not built or
2388 if the method may be referred externally. */
2390 static bool
2391 referenced_from_vtable_p (struct cgraph_node *node)
2393 int i;
2394 struct ipa_ref *ref;
2395 bool found = false;
2397 if (node->externally_visible
2398 || DECL_EXTERNAL (node->decl)
2399 || node->used_from_other_partition)
2400 return true;
2402 /* Keep this test constant time.
2403 It is unlikely this can happen except for the case where speculative
2404 devirtualization introduced many speculative edges to this node.
2405 In this case the target is very likely alive anyway. */
2406 if (node->ref_list.referring.length () > 100)
2407 return true;
2409 /* We need references built. */
2410 if (symtab->state <= CONSTRUCTION)
2411 return true;
2413 for (i = 0; node->iterate_referring (i, ref); i++)
2414 if ((ref->use == IPA_REF_ALIAS
2415 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2416 || (ref->use == IPA_REF_ADDR
2417 && TREE_CODE (ref->referring->decl) == VAR_DECL
2418 && DECL_VIRTUAL_P (ref->referring->decl)))
2420 found = true;
2421 break;
2423 return found;
2426 /* If TARGET has associated node, record it in the NODES array.
2427 CAN_REFER specify if program can refer to the target directly.
2428 if TARGET is unknown (NULL) or it can not be inserted (for example because
2429 its body was already removed and there is no way to refer to it), clear
2430 COMPLETEP. */
2432 static void
2433 maybe_record_node (vec <cgraph_node *> &nodes,
2434 tree target, hash_set<tree> *inserted,
2435 bool can_refer,
2436 bool *completep)
2438 struct cgraph_node *target_node, *alias_target;
2439 enum availability avail;
2441 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2442 list of targets; the runtime effect of calling them is undefined.
2443 Only "real" virtual methods should be accounted. */
2444 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
2445 return;
2447 if (!can_refer)
2449 /* The only case when method of anonymous namespace becomes unreferable
2450 is when we completely optimized it out. */
2451 if (flag_ltrans
2452 || !target
2453 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2454 *completep = false;
2455 return;
2458 if (!target)
2459 return;
2461 target_node = cgraph_node::get (target);
2463 /* Prefer alias target over aliases, so we do not get confused by
2464 fake duplicates. */
2465 if (target_node)
2467 alias_target = target_node->ultimate_alias_target (&avail);
2468 if (target_node != alias_target
2469 && avail >= AVAIL_AVAILABLE
2470 && target_node->get_availability ())
2471 target_node = alias_target;
2474 /* Method can only be called by polymorphic call if any
2475 of vtables referring to it are alive.
2477 While this holds for non-anonymous functions, too, there are
2478 cases where we want to keep them in the list; for example
2479 inline functions with -fno-weak are static, but we still
2480 may devirtualize them when instance comes from other unit.
2481 The same holds for LTO.
2483 Currently we ignore these functions in speculative devirtualization.
2484 ??? Maybe it would make sense to be more aggressive for LTO even
2485 elsewhere. */
2486 if (!flag_ltrans
2487 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2488 && (!target_node
2489 || !referenced_from_vtable_p (target_node)))
2491 /* See if TARGET is useful function we can deal with. */
2492 else if (target_node != NULL
2493 && (TREE_PUBLIC (target)
2494 || DECL_EXTERNAL (target)
2495 || target_node->definition)
2496 && target_node->real_symbol_p ())
2498 gcc_assert (!target_node->global.inlined_to);
2499 gcc_assert (target_node->real_symbol_p ());
2500 if (!inserted->add (target))
2502 cached_polymorphic_call_targets->add (target_node);
2503 nodes.safe_push (target_node);
2506 else if (completep
2507 && (!type_in_anonymous_namespace_p
2508 (DECL_CONTEXT (target))
2509 || flag_ltrans))
2510 *completep = false;
2513 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2514 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2515 method in vtable and insert method to NODES array
2516 or BASES_TO_CONSIDER if this array is non-NULL.
2517 Otherwise recurse to base BINFOs.
2518 This matches what get_binfo_at_offset does, but with offset
2519 being unknown.
2521 TYPE_BINFOS is a stack of BINFOS of types with defined
2522 virtual table seen on way from class type to BINFO.
2524 MATCHED_VTABLES tracks virtual tables we already did lookup
2525 for virtual function in. INSERTED tracks nodes we already
2526 inserted.
2528 ANONYMOUS is true if BINFO is part of anonymous namespace.
2530 Clear COMPLETEP when we hit unreferable target.
2533 static void
2534 record_target_from_binfo (vec <cgraph_node *> &nodes,
2535 vec <tree> *bases_to_consider,
2536 tree binfo,
2537 tree otr_type,
2538 vec <tree> &type_binfos,
2539 HOST_WIDE_INT otr_token,
2540 tree outer_type,
2541 HOST_WIDE_INT offset,
2542 hash_set<tree> *inserted,
2543 hash_set<tree> *matched_vtables,
2544 bool anonymous,
2545 bool *completep)
2547 tree type = BINFO_TYPE (binfo);
2548 int i;
2549 tree base_binfo;
2552 if (BINFO_VTABLE (binfo))
2553 type_binfos.safe_push (binfo);
2554 if (types_same_for_odr (type, outer_type))
2556 int i;
2557 tree type_binfo = NULL;
2559 /* Look up BINFO with virtual table. For normal types it is always last
2560 binfo on stack. */
2561 for (i = type_binfos.length () - 1; i >= 0; i--)
2562 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2564 type_binfo = type_binfos[i];
2565 break;
2567 if (BINFO_VTABLE (binfo))
2568 type_binfos.pop ();
2569 /* If this is duplicated BINFO for base shared by virtual inheritance,
2570 we may not have its associated vtable. This is not a problem, since
2571 we will walk it on the other path. */
2572 if (!type_binfo)
2573 return;
2574 tree inner_binfo = get_binfo_at_offset (type_binfo,
2575 offset, otr_type);
2576 if (!inner_binfo)
2578 gcc_assert (odr_violation_reported);
2579 return;
2581 /* For types in anonymous namespace first check if the respective vtable
2582 is alive. If not, we know the type can't be called. */
2583 if (!flag_ltrans && anonymous)
2585 tree vtable = BINFO_VTABLE (inner_binfo);
2586 varpool_node *vnode;
2588 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2589 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2590 vnode = varpool_node::get (vtable);
2591 if (!vnode || !vnode->definition)
2592 return;
2594 gcc_assert (inner_binfo);
2595 if (bases_to_consider
2596 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2597 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2599 bool can_refer;
2600 tree target = gimple_get_virt_method_for_binfo (otr_token,
2601 inner_binfo,
2602 &can_refer);
2603 if (!bases_to_consider)
2604 maybe_record_node (nodes, target, inserted, can_refer, completep);
2605 /* Destructors are never called via construction vtables. */
2606 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2607 bases_to_consider->safe_push (target);
2609 return;
2612 /* Walk bases. */
2613 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2614 /* Walking bases that have no virtual method is pointless exercise. */
2615 if (polymorphic_type_binfo_p (base_binfo))
2616 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2617 type_binfos,
2618 otr_token, outer_type, offset, inserted,
2619 matched_vtables, anonymous, completep);
2620 if (BINFO_VTABLE (binfo))
2621 type_binfos.pop ();
2624 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2625 of TYPE, insert them to NODES, recurse into derived nodes.
2626 INSERTED is used to avoid duplicate insertions of methods into NODES.
2627 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2628 Clear COMPLETEP if unreferable target is found.
2630 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2631 all cases where BASE_SKIPPED is true (because the base is abstract
2632 class). */
2634 static void
2635 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2636 hash_set<tree> *inserted,
2637 hash_set<tree> *matched_vtables,
2638 tree otr_type,
2639 odr_type type,
2640 HOST_WIDE_INT otr_token,
2641 tree outer_type,
2642 HOST_WIDE_INT offset,
2643 bool *completep,
2644 vec <tree> &bases_to_consider,
2645 bool consider_construction)
2647 tree binfo = TYPE_BINFO (type->type);
2648 unsigned int i;
2649 auto_vec <tree, 8> type_binfos;
2650 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2652 /* We may need to consider types w/o instances because of possible derived
2653 types using their methods either directly or via construction vtables.
2654 We are safe to skip them when all derivations are known, since we will
2655 handle them later.
2656 This is done by recording them to BASES_TO_CONSIDER array. */
2657 if (possibly_instantiated || consider_construction)
2659 record_target_from_binfo (nodes,
2660 (!possibly_instantiated
2661 && type_all_derivations_known_p (type->type))
2662 ? &bases_to_consider : NULL,
2663 binfo, otr_type, type_binfos, otr_token,
2664 outer_type, offset,
2665 inserted, matched_vtables,
2666 type->anonymous_namespace, completep);
2668 for (i = 0; i < type->derived_types.length (); i++)
2669 possible_polymorphic_call_targets_1 (nodes, inserted,
2670 matched_vtables,
2671 otr_type,
2672 type->derived_types[i],
2673 otr_token, outer_type, offset, completep,
2674 bases_to_consider, consider_construction);
2677 /* Cache of queries for polymorphic call targets.
2679 Enumerating all call targets may get expensive when there are many
2680 polymorphic calls in the program, so we memoize all the previous
2681 queries and avoid duplicated work. */
2683 struct polymorphic_call_target_d
2685 HOST_WIDE_INT otr_token;
2686 ipa_polymorphic_call_context context;
2687 odr_type type;
2688 vec <cgraph_node *> targets;
2689 tree decl_warning;
2690 int type_warning;
2691 bool complete;
2692 bool speculative;
2695 /* Polymorphic call target cache helpers. */
2697 struct polymorphic_call_target_hasher
2698 : pointer_hash <polymorphic_call_target_d>
2700 static inline hashval_t hash (const polymorphic_call_target_d *);
2701 static inline bool equal (const polymorphic_call_target_d *,
2702 const polymorphic_call_target_d *);
2703 static inline void remove (polymorphic_call_target_d *);
2706 /* Return the computed hashcode for ODR_QUERY. */
2708 inline hashval_t
2709 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2711 inchash::hash hstate (odr_query->otr_token);
2713 hstate.add_wide_int (odr_query->type->id);
2714 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2715 hstate.add_wide_int (odr_query->context.offset);
2717 if (odr_query->context.speculative_outer_type)
2719 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2720 hstate.add_wide_int (odr_query->context.speculative_offset);
2722 hstate.add_flag (odr_query->speculative);
2723 hstate.add_flag (odr_query->context.maybe_in_construction);
2724 hstate.add_flag (odr_query->context.maybe_derived_type);
2725 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2726 hstate.commit_flag ();
2727 return hstate.end ();
2730 /* Compare cache entries T1 and T2. */
2732 inline bool
2733 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2734 const polymorphic_call_target_d *t2)
2736 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2737 && t1->speculative == t2->speculative
2738 && t1->context.offset == t2->context.offset
2739 && t1->context.speculative_offset == t2->context.speculative_offset
2740 && t1->context.outer_type == t2->context.outer_type
2741 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2742 && t1->context.maybe_in_construction
2743 == t2->context.maybe_in_construction
2744 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2745 && (t1->context.speculative_maybe_derived_type
2746 == t2->context.speculative_maybe_derived_type));
2749 /* Remove entry in polymorphic call target cache hash. */
2751 inline void
2752 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2754 v->targets.release ();
2755 free (v);
2758 /* Polymorphic call target query cache. */
2760 typedef hash_table<polymorphic_call_target_hasher>
2761 polymorphic_call_target_hash_type;
2762 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2764 /* Destroy polymorphic call target query cache. */
2766 static void
2767 free_polymorphic_call_targets_hash ()
2769 if (cached_polymorphic_call_targets)
2771 delete polymorphic_call_target_hash;
2772 polymorphic_call_target_hash = NULL;
2773 delete cached_polymorphic_call_targets;
2774 cached_polymorphic_call_targets = NULL;
2778 /* When virtual function is removed, we may need to flush the cache. */
2780 static void
2781 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2783 if (cached_polymorphic_call_targets
2784 && cached_polymorphic_call_targets->contains (n))
2785 free_polymorphic_call_targets_hash ();
2788 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2790 tree
2791 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2792 tree vtable)
2794 tree v = BINFO_VTABLE (binfo);
2795 int i;
2796 tree base_binfo;
2797 unsigned HOST_WIDE_INT this_offset;
2799 if (v)
2801 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2802 gcc_unreachable ();
2804 if (offset == this_offset
2805 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2806 return binfo;
2809 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2810 if (polymorphic_type_binfo_p (base_binfo))
2812 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2813 if (base_binfo)
2814 return base_binfo;
2816 return NULL;
2819 /* T is known constant value of virtual table pointer.
2820 Store virtual table to V and its offset to OFFSET.
2821 Return false if T does not look like virtual table reference. */
2823 bool
2824 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2825 unsigned HOST_WIDE_INT *offset)
2827 /* We expect &MEM[(void *)&virtual_table + 16B].
2828 We obtain object's BINFO from the context of the virtual table.
2829 This one contains pointer to virtual table represented via
2830 POINTER_PLUS_EXPR. Verify that this pointer matches what
2831 we propagated through.
2833 In the case of virtual inheritance, the virtual tables may
2834 be nested, i.e. the offset may be different from 16 and we may
2835 need to dive into the type representation. */
2836 if (TREE_CODE (t) == ADDR_EXPR
2837 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2838 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2839 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2840 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2841 == VAR_DECL)
2842 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2843 (TREE_OPERAND (t, 0), 0), 0)))
2845 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2846 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2847 return true;
2850 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2851 We need to handle it when T comes from static variable initializer or
2852 BINFO. */
2853 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2855 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2856 t = TREE_OPERAND (t, 0);
2858 else
2859 *offset = 0;
2861 if (TREE_CODE (t) != ADDR_EXPR)
2862 return false;
2863 *v = TREE_OPERAND (t, 0);
2864 return true;
2867 /* T is known constant value of virtual table pointer. Return BINFO of the
2868 instance type. */
2870 tree
2871 vtable_pointer_value_to_binfo (const_tree t)
2873 tree vtable;
2874 unsigned HOST_WIDE_INT offset;
2876 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2877 return NULL_TREE;
2879 /* FIXME: for stores of construction vtables we return NULL,
2880 because we do not have BINFO for those. Eventually we should fix
2881 our representation to allow this case to be handled, too.
2882 In the case we see store of BINFO we however may assume
2883 that standard folding will be able to cope with it. */
2884 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2885 offset, vtable);
2888 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2889 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2890 and insert them in NODES.
2892 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2894 static void
2895 record_targets_from_bases (tree otr_type,
2896 HOST_WIDE_INT otr_token,
2897 tree outer_type,
2898 HOST_WIDE_INT offset,
2899 vec <cgraph_node *> &nodes,
2900 hash_set<tree> *inserted,
2901 hash_set<tree> *matched_vtables,
2902 bool *completep)
2904 while (true)
2906 HOST_WIDE_INT pos, size;
2907 tree base_binfo;
2908 tree fld;
2910 if (types_same_for_odr (outer_type, otr_type))
2911 return;
2913 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2915 if (TREE_CODE (fld) != FIELD_DECL)
2916 continue;
2918 pos = int_bit_position (fld);
2919 size = tree_to_shwi (DECL_SIZE (fld));
2920 if (pos <= offset && (pos + size) > offset
2921 /* Do not get confused by zero sized bases. */
2922 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2923 break;
2925 /* Within a class type we should always find corresponding fields. */
2926 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2928 /* Nonbase types should have been stripped by outer_class_type. */
2929 gcc_assert (DECL_ARTIFICIAL (fld));
2931 outer_type = TREE_TYPE (fld);
2932 offset -= pos;
2934 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2935 offset, otr_type);
2936 if (!base_binfo)
2938 gcc_assert (odr_violation_reported);
2939 return;
2941 gcc_assert (base_binfo);
2942 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2944 bool can_refer;
2945 tree target = gimple_get_virt_method_for_binfo (otr_token,
2946 base_binfo,
2947 &can_refer);
2948 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2949 maybe_record_node (nodes, target, inserted, can_refer, completep);
2950 matched_vtables->add (BINFO_VTABLE (base_binfo));
2955 /* When virtual table is removed, we may need to flush the cache. */
2957 static void
2958 devirt_variable_node_removal_hook (varpool_node *n,
2959 void *d ATTRIBUTE_UNUSED)
2961 if (cached_polymorphic_call_targets
2962 && DECL_VIRTUAL_P (n->decl)
2963 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2964 free_polymorphic_call_targets_hash ();
2967 /* Record about how many calls would benefit from given type to be final. */
2969 struct odr_type_warn_count
2971 tree type;
2972 int count;
2973 gcov_type dyn_count;
2976 /* Record about how many calls would benefit from given method to be final. */
2978 struct decl_warn_count
2980 tree decl;
2981 int count;
2982 gcov_type dyn_count;
2985 /* Information about type and decl warnings. */
2987 struct final_warning_record
2989 gcov_type dyn_count;
2990 auto_vec<odr_type_warn_count> type_warnings;
2991 hash_map<tree, decl_warn_count> decl_warnings;
2993 struct final_warning_record *final_warning_records;
2995 /* Return vector containing possible targets of polymorphic call of type
2996 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2997 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2998 OTR_TYPE and include their virtual method. This is useful for types
2999 possibly in construction or destruction where the virtual table may
3000 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3001 us to walk the inheritance graph for all derivations.
3003 If COMPLETEP is non-NULL, store true if the list is complete.
3004 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3005 in the target cache. If user needs to visit every target list
3006 just once, it can memoize them.
3008 If SPECULATIVE is set, the list will not contain targets that
3009 are not speculatively taken.
3011 Returned vector is placed into cache. It is NOT caller's responsibility
3012 to free it. The vector can be freed on cgraph_remove_node call if
3013 the particular node is a virtual function present in the cache. */
3015 vec <cgraph_node *>
3016 possible_polymorphic_call_targets (tree otr_type,
3017 HOST_WIDE_INT otr_token,
3018 ipa_polymorphic_call_context context,
3019 bool *completep,
3020 void **cache_token,
3021 bool speculative)
3023 static struct cgraph_node_hook_list *node_removal_hook_holder;
3024 vec <cgraph_node *> nodes = vNULL;
3025 auto_vec <tree, 8> bases_to_consider;
3026 odr_type type, outer_type;
3027 polymorphic_call_target_d key;
3028 polymorphic_call_target_d **slot;
3029 unsigned int i;
3030 tree binfo, target;
3031 bool complete;
3032 bool can_refer = false;
3033 bool skipped = false;
3035 otr_type = TYPE_MAIN_VARIANT (otr_type);
3037 /* If ODR is not initialized or the context is invalid, return empty
3038 incomplete list. */
3039 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3041 if (completep)
3042 *completep = context.invalid;
3043 if (cache_token)
3044 *cache_token = NULL;
3045 return nodes;
3048 /* Do not bother to compute speculative info when user do not asks for it. */
3049 if (!speculative || !context.speculative_outer_type)
3050 context.clear_speculation ();
3052 type = get_odr_type (otr_type, true);
3054 /* Recording type variants would waste results cache. */
3055 gcc_assert (!context.outer_type
3056 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3058 /* Look up the outer class type we want to walk.
3059 If we fail to do so, the context is invalid. */
3060 if ((context.outer_type || context.speculative_outer_type)
3061 && !context.restrict_to_inner_class (otr_type))
3063 if (completep)
3064 *completep = true;
3065 if (cache_token)
3066 *cache_token = NULL;
3067 return nodes;
3069 gcc_assert (!context.invalid);
3071 /* Check that restrict_to_inner_class kept the main variant. */
3072 gcc_assert (!context.outer_type
3073 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3075 /* We canonicalize our query, so we do not need extra hashtable entries. */
3077 /* Without outer type, we have no use for offset. Just do the
3078 basic search from inner type. */
3079 if (!context.outer_type)
3080 context.clear_outer_type (otr_type);
3081 /* We need to update our hierarchy if the type does not exist. */
3082 outer_type = get_odr_type (context.outer_type, true);
3083 /* If the type is complete, there are no derivations. */
3084 if (TYPE_FINAL_P (outer_type->type))
3085 context.maybe_derived_type = false;
3087 /* Initialize query cache. */
3088 if (!cached_polymorphic_call_targets)
3090 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3091 polymorphic_call_target_hash
3092 = new polymorphic_call_target_hash_type (23);
3093 if (!node_removal_hook_holder)
3095 node_removal_hook_holder =
3096 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3097 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3098 NULL);
3102 if (in_lto_p)
3104 if (context.outer_type != otr_type)
3105 context.outer_type
3106 = get_odr_type (context.outer_type, true)->type;
3107 if (context.speculative_outer_type)
3108 context.speculative_outer_type
3109 = get_odr_type (context.speculative_outer_type, true)->type;
3112 /* Look up cached answer. */
3113 key.type = type;
3114 key.otr_token = otr_token;
3115 key.speculative = speculative;
3116 key.context = context;
3117 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3118 if (cache_token)
3119 *cache_token = (void *)*slot;
3120 if (*slot)
3122 if (completep)
3123 *completep = (*slot)->complete;
3124 if ((*slot)->type_warning && final_warning_records)
3126 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3127 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3128 += final_warning_records->dyn_count;
3130 if (!speculative && (*slot)->decl_warning && final_warning_records)
3132 struct decl_warn_count *c =
3133 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3134 c->count++;
3135 c->dyn_count += final_warning_records->dyn_count;
3137 return (*slot)->targets;
3140 complete = true;
3142 /* Do actual search. */
3143 timevar_push (TV_IPA_VIRTUAL_CALL);
3144 *slot = XCNEW (polymorphic_call_target_d);
3145 if (cache_token)
3146 *cache_token = (void *)*slot;
3147 (*slot)->type = type;
3148 (*slot)->otr_token = otr_token;
3149 (*slot)->context = context;
3150 (*slot)->speculative = speculative;
3152 hash_set<tree> inserted;
3153 hash_set<tree> matched_vtables;
3155 /* First insert targets we speculatively identified as likely. */
3156 if (context.speculative_outer_type)
3158 odr_type speculative_outer_type;
3159 bool speculation_complete = true;
3161 /* First insert target from type itself and check if it may have
3162 derived types. */
3163 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3164 if (TYPE_FINAL_P (speculative_outer_type->type))
3165 context.speculative_maybe_derived_type = false;
3166 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3167 context.speculative_offset, otr_type);
3168 if (binfo)
3169 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3170 &can_refer);
3171 else
3172 target = NULL;
3174 /* In the case we get complete method, we don't need
3175 to walk derivations. */
3176 if (target && DECL_FINAL_P (target))
3177 context.speculative_maybe_derived_type = false;
3178 if (type_possibly_instantiated_p (speculative_outer_type->type))
3179 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3180 if (binfo)
3181 matched_vtables.add (BINFO_VTABLE (binfo));
3184 /* Next walk recursively all derived types. */
3185 if (context.speculative_maybe_derived_type)
3186 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3187 possible_polymorphic_call_targets_1 (nodes, &inserted,
3188 &matched_vtables,
3189 otr_type,
3190 speculative_outer_type->derived_types[i],
3191 otr_token, speculative_outer_type->type,
3192 context.speculative_offset,
3193 &speculation_complete,
3194 bases_to_consider,
3195 false);
3198 if (!speculative || !nodes.length ())
3200 /* First see virtual method of type itself. */
3201 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3202 context.offset, otr_type);
3203 if (binfo)
3204 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3205 &can_refer);
3206 else
3208 gcc_assert (odr_violation_reported);
3209 target = NULL;
3212 /* Destructors are never called through construction virtual tables,
3213 because the type is always known. */
3214 if (target && DECL_CXX_DESTRUCTOR_P (target))
3215 context.maybe_in_construction = false;
3217 if (target)
3219 /* In the case we get complete method, we don't need
3220 to walk derivations. */
3221 if (DECL_FINAL_P (target))
3222 context.maybe_derived_type = false;
3225 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3226 if (type_possibly_instantiated_p (outer_type->type))
3227 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3228 else
3229 skipped = true;
3231 if (binfo)
3232 matched_vtables.add (BINFO_VTABLE (binfo));
3234 /* Next walk recursively all derived types. */
3235 if (context.maybe_derived_type)
3237 for (i = 0; i < outer_type->derived_types.length(); i++)
3238 possible_polymorphic_call_targets_1 (nodes, &inserted,
3239 &matched_vtables,
3240 otr_type,
3241 outer_type->derived_types[i],
3242 otr_token, outer_type->type,
3243 context.offset, &complete,
3244 bases_to_consider,
3245 context.maybe_in_construction);
3247 if (!outer_type->all_derivations_known)
3249 if (!speculative && final_warning_records)
3251 if (complete
3252 && nodes.length () == 1
3253 && warn_suggest_final_types
3254 && !outer_type->derived_types.length ())
3256 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3257 final_warning_records->type_warnings.safe_grow_cleared
3258 (odr_types.length ());
3259 final_warning_records->type_warnings[outer_type->id].count++;
3260 final_warning_records->type_warnings[outer_type->id].dyn_count
3261 += final_warning_records->dyn_count;
3262 final_warning_records->type_warnings[outer_type->id].type
3263 = outer_type->type;
3264 (*slot)->type_warning = outer_type->id + 1;
3266 if (complete
3267 && warn_suggest_final_methods
3268 && nodes.length () == 1
3269 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3270 outer_type->type))
3272 bool existed;
3273 struct decl_warn_count &c =
3274 final_warning_records->decl_warnings.get_or_insert
3275 (nodes[0]->decl, &existed);
3277 if (existed)
3279 c.count++;
3280 c.dyn_count += final_warning_records->dyn_count;
3282 else
3284 c.count = 1;
3285 c.dyn_count = final_warning_records->dyn_count;
3286 c.decl = nodes[0]->decl;
3288 (*slot)->decl_warning = nodes[0]->decl;
3291 complete = false;
3295 if (!speculative)
3297 /* Destructors are never called through construction virtual tables,
3298 because the type is always known. One of entries may be
3299 cxa_pure_virtual so look to at least two of them. */
3300 if (context.maybe_in_construction)
3301 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3302 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3303 context.maybe_in_construction = false;
3304 if (context.maybe_in_construction)
3306 if (type != outer_type
3307 && (!skipped
3308 || (context.maybe_derived_type
3309 && !type_all_derivations_known_p (outer_type->type))))
3310 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3311 context.offset, nodes, &inserted,
3312 &matched_vtables, &complete);
3313 if (skipped)
3314 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3315 for (i = 0; i < bases_to_consider.length(); i++)
3316 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3321 (*slot)->targets = nodes;
3322 (*slot)->complete = complete;
3323 if (completep)
3324 *completep = complete;
3326 timevar_pop (TV_IPA_VIRTUAL_CALL);
3327 return nodes;
3330 bool
3331 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3332 vec<const decl_warn_count*> *vec)
3334 vec->safe_push (&value);
3335 return true;
3338 /* Dump target list TARGETS into FILE. */
3340 static void
3341 dump_targets (FILE *f, vec <cgraph_node *> targets)
3343 unsigned int i;
3345 for (i = 0; i < targets.length (); i++)
3347 char *name = NULL;
3348 if (in_lto_p)
3349 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3350 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3351 if (in_lto_p)
3352 free (name);
3353 if (!targets[i]->definition)
3354 fprintf (f, " (no definition%s)",
3355 DECL_DECLARED_INLINE_P (targets[i]->decl)
3356 ? " inline" : "");
3358 fprintf (f, "\n");
3361 /* Dump all possible targets of a polymorphic call. */
3363 void
3364 dump_possible_polymorphic_call_targets (FILE *f,
3365 tree otr_type,
3366 HOST_WIDE_INT otr_token,
3367 const ipa_polymorphic_call_context &ctx)
3369 vec <cgraph_node *> targets;
3370 bool final;
3371 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3372 unsigned int len;
3374 if (!type)
3375 return;
3376 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3377 ctx,
3378 &final, NULL, false);
3379 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3380 print_generic_expr (f, type->type, TDF_SLIM);
3381 fprintf (f, " token %i\n", (int)otr_token);
3383 ctx.dump (f);
3385 fprintf (f, " %s%s%s%s\n ",
3386 final ? "This is a complete list." :
3387 "This is partial list; extra targets may be defined in other units.",
3388 ctx.maybe_in_construction ? " (base types included)" : "",
3389 ctx.maybe_derived_type ? " (derived types included)" : "",
3390 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3391 len = targets.length ();
3392 dump_targets (f, targets);
3394 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3395 ctx,
3396 &final, NULL, true);
3397 if (targets.length () != len)
3399 fprintf (f, " Speculative targets:");
3400 dump_targets (f, targets);
3402 gcc_assert (targets.length () <= len);
3403 fprintf (f, "\n");
3407 /* Return true if N can be possibly target of a polymorphic call of
3408 OTR_TYPE/OTR_TOKEN. */
3410 bool
3411 possible_polymorphic_call_target_p (tree otr_type,
3412 HOST_WIDE_INT otr_token,
3413 const ipa_polymorphic_call_context &ctx,
3414 struct cgraph_node *n)
3416 vec <cgraph_node *> targets;
3417 unsigned int i;
3418 enum built_in_function fcode;
3419 bool final;
3421 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3422 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3423 == BUILT_IN_UNREACHABLE
3424 || fcode == BUILT_IN_TRAP))
3425 return true;
3427 if (!odr_hash)
3428 return true;
3429 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3430 for (i = 0; i < targets.length (); i++)
3431 if (n->semantically_equivalent_p (targets[i]))
3432 return true;
3434 /* At a moment we allow middle end to dig out new external declarations
3435 as a targets of polymorphic calls. */
3436 if (!final && !n->definition)
3437 return true;
3438 return false;
3443 /* Return true if N can be possibly target of a polymorphic call of
3444 OBJ_TYPE_REF expression REF in STMT. */
3446 bool
3447 possible_polymorphic_call_target_p (tree ref,
3448 gimple *stmt,
3449 struct cgraph_node *n)
3451 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3452 tree call_fn = gimple_call_fn (stmt);
3454 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3455 tree_to_uhwi
3456 (OBJ_TYPE_REF_TOKEN (call_fn)),
3457 context,
3462 /* After callgraph construction new external nodes may appear.
3463 Add them into the graph. */
3465 void
3466 update_type_inheritance_graph (void)
3468 struct cgraph_node *n;
3470 if (!odr_hash)
3471 return;
3472 free_polymorphic_call_targets_hash ();
3473 timevar_push (TV_IPA_INHERITANCE);
3474 /* We reconstruct the graph starting from types of all methods seen in the
3475 the unit. */
3476 FOR_EACH_FUNCTION (n)
3477 if (DECL_VIRTUAL_P (n->decl)
3478 && !n->definition
3479 && n->real_symbol_p ())
3480 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3481 timevar_pop (TV_IPA_INHERITANCE);
3485 /* Return true if N looks like likely target of a polymorphic call.
3486 Rule out cxa_pure_virtual, noreturns, function declared cold and
3487 other obvious cases. */
3489 bool
3490 likely_target_p (struct cgraph_node *n)
3492 int flags;
3493 /* cxa_pure_virtual and similar things are not likely. */
3494 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3495 return false;
3496 flags = flags_from_decl_or_type (n->decl);
3497 if (flags & ECF_NORETURN)
3498 return false;
3499 if (lookup_attribute ("cold",
3500 DECL_ATTRIBUTES (n->decl)))
3501 return false;
3502 if (n->frequency < NODE_FREQUENCY_NORMAL)
3503 return false;
3504 /* If there are no live virtual tables referring the target,
3505 the only way the target can be called is an instance coming from other
3506 compilation unit; speculative devirtualization is built around an
3507 assumption that won't happen. */
3508 if (!referenced_from_vtable_p (n))
3509 return false;
3510 return true;
3513 /* Compare type warning records P1 and P2 and choose one with larger count;
3514 helper for qsort. */
3517 type_warning_cmp (const void *p1, const void *p2)
3519 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3520 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3522 if (t1->dyn_count < t2->dyn_count)
3523 return 1;
3524 if (t1->dyn_count > t2->dyn_count)
3525 return -1;
3526 return t2->count - t1->count;
3529 /* Compare decl warning records P1 and P2 and choose one with larger count;
3530 helper for qsort. */
3533 decl_warning_cmp (const void *p1, const void *p2)
3535 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3536 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3538 if (t1->dyn_count < t2->dyn_count)
3539 return 1;
3540 if (t1->dyn_count > t2->dyn_count)
3541 return -1;
3542 return t2->count - t1->count;
3546 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3547 context CTX. */
3549 struct cgraph_node *
3550 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3551 ipa_polymorphic_call_context ctx)
3553 vec <cgraph_node *>targets
3554 = possible_polymorphic_call_targets
3555 (otr_type, otr_token, ctx, NULL, NULL, true);
3556 unsigned int i;
3557 struct cgraph_node *likely_target = NULL;
3559 for (i = 0; i < targets.length (); i++)
3560 if (likely_target_p (targets[i]))
3562 if (likely_target)
3563 return NULL;
3564 likely_target = targets[i];
3566 if (!likely_target
3567 ||!likely_target->definition
3568 || DECL_EXTERNAL (likely_target->decl))
3569 return NULL;
3571 /* Don't use an implicitly-declared destructor (c++/58678). */
3572 struct cgraph_node *non_thunk_target
3573 = likely_target->function_symbol ();
3574 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3575 return NULL;
3576 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3577 && likely_target->can_be_discarded_p ())
3578 return NULL;
3579 return likely_target;
3582 /* The ipa-devirt pass.
3583 When polymorphic call has only one likely target in the unit,
3584 turn it into a speculative call. */
3586 static unsigned int
3587 ipa_devirt (void)
3589 struct cgraph_node *n;
3590 hash_set<void *> bad_call_targets;
3591 struct cgraph_edge *e;
3593 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3594 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3595 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3596 int ndropped = 0;
3598 if (!odr_types_ptr)
3599 return 0;
3601 if (dump_file)
3602 dump_type_inheritance_graph (dump_file);
3604 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3605 This is implemented by setting up final_warning_records that are updated
3606 by get_polymorphic_call_targets.
3607 We need to clear cache in this case to trigger recomputation of all
3608 entries. */
3609 if (warn_suggest_final_methods || warn_suggest_final_types)
3611 final_warning_records = new (final_warning_record);
3612 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3613 free_polymorphic_call_targets_hash ();
3616 FOR_EACH_DEFINED_FUNCTION (n)
3618 bool update = false;
3619 if (!opt_for_fn (n->decl, flag_devirtualize))
3620 continue;
3621 if (dump_file && n->indirect_calls)
3622 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3623 n->name (), n->order);
3624 for (e = n->indirect_calls; e; e = e->next_callee)
3625 if (e->indirect_info->polymorphic)
3627 struct cgraph_node *likely_target = NULL;
3628 void *cache_token;
3629 bool final;
3631 if (final_warning_records)
3632 final_warning_records->dyn_count = e->count;
3634 vec <cgraph_node *>targets
3635 = possible_polymorphic_call_targets
3636 (e, &final, &cache_token, true);
3637 unsigned int i;
3639 /* Trigger warnings by calculating non-speculative targets. */
3640 if (warn_suggest_final_methods || warn_suggest_final_types)
3641 possible_polymorphic_call_targets (e);
3643 if (dump_file)
3644 dump_possible_polymorphic_call_targets
3645 (dump_file, e);
3647 npolymorphic++;
3649 /* See if the call can be devirtualized by means of ipa-prop's
3650 polymorphic call context propagation. If not, we can just
3651 forget about this call being polymorphic and avoid some heavy
3652 lifting in remove_unreachable_nodes that will otherwise try to
3653 keep all possible targets alive until inlining and in the inliner
3654 itself.
3656 This may need to be revisited once we add further ways to use
3657 the may edges, but it is a resonable thing to do right now. */
3659 if ((e->indirect_info->param_index == -1
3660 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3661 && e->indirect_info->vptr_changed))
3662 && !flag_ltrans_devirtualize)
3664 e->indirect_info->polymorphic = false;
3665 ndropped++;
3666 if (dump_file)
3667 fprintf (dump_file, "Dropping polymorphic call info;"
3668 " it can not be used by ipa-prop\n");
3671 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3672 continue;
3674 if (!e->maybe_hot_p ())
3676 if (dump_file)
3677 fprintf (dump_file, "Call is cold\n\n");
3678 ncold++;
3679 continue;
3681 if (e->speculative)
3683 if (dump_file)
3684 fprintf (dump_file, "Call is already speculated\n\n");
3685 nspeculated++;
3687 /* When dumping see if we agree with speculation. */
3688 if (!dump_file)
3689 continue;
3691 if (bad_call_targets.contains (cache_token))
3693 if (dump_file)
3694 fprintf (dump_file, "Target list is known to be useless\n\n");
3695 nmultiple++;
3696 continue;
3698 for (i = 0; i < targets.length (); i++)
3699 if (likely_target_p (targets[i]))
3701 if (likely_target)
3703 likely_target = NULL;
3704 if (dump_file)
3705 fprintf (dump_file, "More than one likely target\n\n");
3706 nmultiple++;
3707 break;
3709 likely_target = targets[i];
3711 if (!likely_target)
3713 bad_call_targets.add (cache_token);
3714 continue;
3716 /* This is reached only when dumping; check if we agree or disagree
3717 with the speculation. */
3718 if (e->speculative)
3720 struct cgraph_edge *e2;
3721 struct ipa_ref *ref;
3722 e->speculative_call_info (e2, e, ref);
3723 if (e2->callee->ultimate_alias_target ()
3724 == likely_target->ultimate_alias_target ())
3726 fprintf (dump_file, "We agree with speculation\n\n");
3727 nok++;
3729 else
3731 fprintf (dump_file, "We disagree with speculation\n\n");
3732 nwrong++;
3734 continue;
3736 if (!likely_target->definition)
3738 if (dump_file)
3739 fprintf (dump_file, "Target is not a definition\n\n");
3740 nnotdefined++;
3741 continue;
3743 /* Do not introduce new references to external symbols. While we
3744 can handle these just well, it is common for programs to
3745 incorrectly with headers defining methods they are linked
3746 with. */
3747 if (DECL_EXTERNAL (likely_target->decl))
3749 if (dump_file)
3750 fprintf (dump_file, "Target is external\n\n");
3751 nexternal++;
3752 continue;
3754 /* Don't use an implicitly-declared destructor (c++/58678). */
3755 struct cgraph_node *non_thunk_target
3756 = likely_target->function_symbol ();
3757 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3759 if (dump_file)
3760 fprintf (dump_file, "Target is artificial\n\n");
3761 nartificial++;
3762 continue;
3764 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3765 && likely_target->can_be_discarded_p ())
3767 if (dump_file)
3768 fprintf (dump_file, "Target is overwritable\n\n");
3769 noverwritable++;
3770 continue;
3772 else if (dbg_cnt (devirt))
3774 if (dump_enabled_p ())
3776 location_t locus = gimple_location_safe (e->call_stmt);
3777 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3778 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3779 n->name (), n->order,
3780 likely_target->name (),
3781 likely_target->order);
3783 if (!likely_target->can_be_discarded_p ())
3785 cgraph_node *alias;
3786 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3787 if (alias)
3788 likely_target = alias;
3790 nconverted++;
3791 update = true;
3792 e->make_speculative
3793 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3796 if (update)
3797 inline_update_overall_summary (n);
3799 if (warn_suggest_final_methods || warn_suggest_final_types)
3801 if (warn_suggest_final_types)
3803 final_warning_records->type_warnings.qsort (type_warning_cmp);
3804 for (unsigned int i = 0;
3805 i < final_warning_records->type_warnings.length (); i++)
3806 if (final_warning_records->type_warnings[i].count)
3808 tree type = final_warning_records->type_warnings[i].type;
3809 int count = final_warning_records->type_warnings[i].count;
3810 long long dyn_count
3811 = final_warning_records->type_warnings[i].dyn_count;
3813 if (!dyn_count)
3814 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3815 OPT_Wsuggest_final_types, count,
3816 "Declaring type %qD final "
3817 "would enable devirtualization of %i call",
3818 "Declaring type %qD final "
3819 "would enable devirtualization of %i calls",
3820 type,
3821 count);
3822 else
3823 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3824 OPT_Wsuggest_final_types, count,
3825 "Declaring type %qD final "
3826 "would enable devirtualization of %i call "
3827 "executed %lli times",
3828 "Declaring type %qD final "
3829 "would enable devirtualization of %i calls "
3830 "executed %lli times",
3831 type,
3832 count,
3833 dyn_count);
3837 if (warn_suggest_final_methods)
3839 auto_vec<const decl_warn_count*> decl_warnings_vec;
3841 final_warning_records->decl_warnings.traverse
3842 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3843 decl_warnings_vec.qsort (decl_warning_cmp);
3844 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3846 tree decl = decl_warnings_vec[i]->decl;
3847 int count = decl_warnings_vec[i]->count;
3848 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3850 if (!dyn_count)
3851 if (DECL_CXX_DESTRUCTOR_P (decl))
3852 warning_n (DECL_SOURCE_LOCATION (decl),
3853 OPT_Wsuggest_final_methods, count,
3854 "Declaring virtual destructor of %qD final "
3855 "would enable devirtualization of %i call",
3856 "Declaring virtual destructor of %qD final "
3857 "would enable devirtualization of %i calls",
3858 DECL_CONTEXT (decl), count);
3859 else
3860 warning_n (DECL_SOURCE_LOCATION (decl),
3861 OPT_Wsuggest_final_methods, count,
3862 "Declaring method %qD final "
3863 "would enable devirtualization of %i call",
3864 "Declaring method %qD final "
3865 "would enable devirtualization of %i calls",
3866 decl, count);
3867 else if (DECL_CXX_DESTRUCTOR_P (decl))
3868 warning_n (DECL_SOURCE_LOCATION (decl),
3869 OPT_Wsuggest_final_methods, count,
3870 "Declaring virtual destructor of %qD final "
3871 "would enable devirtualization of %i call "
3872 "executed %lli times",
3873 "Declaring virtual destructor of %qD final "
3874 "would enable devirtualization of %i calls "
3875 "executed %lli times",
3876 DECL_CONTEXT (decl), count, dyn_count);
3877 else
3878 warning_n (DECL_SOURCE_LOCATION (decl),
3879 OPT_Wsuggest_final_methods, count,
3880 "Declaring method %qD final "
3881 "would enable devirtualization of %i call "
3882 "executed %lli times",
3883 "Declaring method %qD final "
3884 "would enable devirtualization of %i calls "
3885 "executed %lli times",
3886 decl, count, dyn_count);
3890 delete (final_warning_records);
3891 final_warning_records = 0;
3894 if (dump_file)
3895 fprintf (dump_file,
3896 "%i polymorphic calls, %i devirtualized,"
3897 " %i speculatively devirtualized, %i cold\n"
3898 "%i have multiple targets, %i overwritable,"
3899 " %i already speculated (%i agree, %i disagree),"
3900 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3901 npolymorphic, ndevirtualized, nconverted, ncold,
3902 nmultiple, noverwritable, nspeculated, nok, nwrong,
3903 nexternal, nnotdefined, nartificial, ndropped);
3904 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3907 namespace {
3909 const pass_data pass_data_ipa_devirt =
3911 IPA_PASS, /* type */
3912 "devirt", /* name */
3913 OPTGROUP_NONE, /* optinfo_flags */
3914 TV_IPA_DEVIRT, /* tv_id */
3915 0, /* properties_required */
3916 0, /* properties_provided */
3917 0, /* properties_destroyed */
3918 0, /* todo_flags_start */
3919 ( TODO_dump_symtab ), /* todo_flags_finish */
3922 class pass_ipa_devirt : public ipa_opt_pass_d
3924 public:
3925 pass_ipa_devirt (gcc::context *ctxt)
3926 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3927 NULL, /* generate_summary */
3928 NULL, /* write_summary */
3929 NULL, /* read_summary */
3930 NULL, /* write_optimization_summary */
3931 NULL, /* read_optimization_summary */
3932 NULL, /* stmt_fixup */
3933 0, /* function_transform_todo_flags_start */
3934 NULL, /* function_transform */
3935 NULL) /* variable_transform */
3938 /* opt_pass methods: */
3939 virtual bool gate (function *)
3941 /* In LTO, always run the IPA passes and decide on function basis if the
3942 pass is enabled. */
3943 if (in_lto_p)
3944 return true;
3945 return (flag_devirtualize
3946 && (flag_devirtualize_speculatively
3947 || (warn_suggest_final_methods
3948 || warn_suggest_final_types))
3949 && optimize);
3952 virtual unsigned int execute (function *) { return ipa_devirt (); }
3954 }; // class pass_ipa_devirt
3956 } // anon namespace
3958 ipa_opt_pass_d *
3959 make_pass_ipa_devirt (gcc::context *ctxt)
3961 return new pass_ipa_devirt (ctxt);
3964 #include "gt-ipa-devirt.h"