2016-01-26 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-devirt.c
blobb2036d550f0494d769abacf699bbd346e6d48bca
1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2016 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 all derived types of T are known and thus
213 we may consider the walk of derived type complete.
215 This is typically true only for final anonymous namespace types and types
216 defined within functions (that may be COMDAT and thus shared across units,
217 but with the same set of derived types). */
219 bool
220 type_all_derivations_known_p (const_tree t)
222 if (TYPE_FINAL_P (t))
223 return true;
224 if (flag_ltrans)
225 return false;
226 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
227 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
228 return true;
229 if (type_in_anonymous_namespace_p (t))
230 return true;
231 return (decl_function_context (TYPE_NAME (t)) != NULL);
234 /* Return TRUE if type's constructors are all visible. */
236 static bool
237 type_all_ctors_visible_p (tree t)
239 return !flag_ltrans
240 && symtab->state >= CONSTRUCTION
241 /* We can not always use type_all_derivations_known_p.
242 For function local types we must assume case where
243 the function is COMDAT and shared in between units.
245 TODO: These cases are quite easy to get, but we need
246 to keep track of C++ privatizing via -Wno-weak
247 as well as the IPA privatizing. */
248 && type_in_anonymous_namespace_p (t);
251 /* Return TRUE if type may have instance. */
253 static bool
254 type_possibly_instantiated_p (tree t)
256 tree vtable;
257 varpool_node *vnode;
259 /* TODO: Add abstract types here. */
260 if (!type_all_ctors_visible_p (t))
261 return true;
263 vtable = BINFO_VTABLE (TYPE_BINFO (t));
264 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
265 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
266 vnode = varpool_node::get (vtable);
267 return vnode && vnode->definition;
270 /* Hash used to unify ODR types based on their mangled name and for anonymous
271 namespace types. */
273 struct odr_name_hasher : pointer_hash <odr_type_d>
275 typedef union tree_node *compare_type;
276 static inline hashval_t hash (const odr_type_d *);
277 static inline bool equal (const odr_type_d *, const tree_node *);
278 static inline void remove (odr_type_d *);
281 /* Has used to unify ODR types based on their associated virtual table.
282 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
283 only polymorphic types. Types with mangled names are inserted to both. */
285 struct odr_vtable_hasher:odr_name_hasher
287 static inline hashval_t hash (const odr_type_d *);
288 static inline bool equal (const odr_type_d *, const tree_node *);
291 /* Return type that was declared with T's name so that T is an
292 qualified variant of it. */
294 static inline tree
295 main_odr_variant (const_tree t)
297 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
298 return TREE_TYPE (TYPE_NAME (t));
299 /* Unnamed types and non-C++ produced types can be compared by variants. */
300 else
301 return TYPE_MAIN_VARIANT (t);
304 static bool
305 can_be_name_hashed_p (tree t)
307 return (!in_lto_p || odr_type_p (t));
310 /* Hash type by its ODR name. */
312 static hashval_t
313 hash_odr_name (const_tree t)
315 gcc_checking_assert (main_odr_variant (t) == t);
317 /* If not in LTO, all main variants are unique, so we can do
318 pointer hash. */
319 if (!in_lto_p)
320 return htab_hash_pointer (t);
322 /* Anonymous types are unique. */
323 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
324 return htab_hash_pointer (t);
326 gcc_checking_assert (TYPE_NAME (t)
327 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
328 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
331 /* Return the computed hashcode for ODR_TYPE. */
333 inline hashval_t
334 odr_name_hasher::hash (const odr_type_d *odr_type)
336 return hash_odr_name (odr_type->type);
339 static bool
340 can_be_vtable_hashed_p (tree t)
342 /* vtable hashing can distinguish only main variants. */
343 if (TYPE_MAIN_VARIANT (t) != t)
344 return false;
345 /* Anonymous namespace types are always handled by name hash. */
346 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
347 return false;
348 return (TREE_CODE (t) == RECORD_TYPE
349 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
352 /* Hash type by assembler name of its vtable. */
354 static hashval_t
355 hash_odr_vtable (const_tree t)
357 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
358 inchash::hash hstate;
360 gcc_checking_assert (in_lto_p);
361 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
362 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
363 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
364 gcc_checking_assert (main_odr_variant (t) == t);
366 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
368 add_expr (TREE_OPERAND (v, 1), hstate);
369 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
372 hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
373 return hstate.end ();
376 /* Return the computed hashcode for ODR_TYPE. */
378 inline hashval_t
379 odr_vtable_hasher::hash (const odr_type_d *odr_type)
381 return hash_odr_vtable (odr_type->type);
384 /* For languages with One Definition Rule, work out if
385 types are the same based on their name.
387 This is non-trivial for LTO where minor differences in
388 the type representation may have prevented type merging
389 to merge two copies of otherwise equivalent type.
391 Until we start streaming mangled type names, this function works
392 only for polymorphic types.
394 When STRICT is true, we compare types by their names for purposes of
395 ODR violation warnings. When strict is false, we consider variants
396 equivalent, becuase it is all that matters for devirtualization machinery.
399 bool
400 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
402 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
404 type1 = main_odr_variant (type1);
405 type2 = main_odr_variant (type2);
406 if (!strict)
408 type1 = TYPE_MAIN_VARIANT (type1);
409 type2 = TYPE_MAIN_VARIANT (type2);
412 if (type1 == type2)
413 return true;
415 if (!in_lto_p)
416 return false;
418 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
419 on the corresponding TYPE_STUB_DECL. */
420 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
421 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
422 return false;
425 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
427 Ideally we should never need types without ODR names here. It can however
428 happen in two cases:
430 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
431 Here testing for equivalence is safe, since their MAIN_VARIANTs are
432 unique.
433 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
434 establish precise ODR equivalency, but for correctness we care only
435 about equivalency on complete polymorphic types. For these we can
436 compare assembler names of their virtual tables. */
437 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
438 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
440 /* See if types are obviously different (i.e. different codes
441 or polymorphic wrt non-polymorphic). This is not strictly correct
442 for ODR violating programs, but we can't do better without streaming
443 ODR names. */
444 if (TREE_CODE (type1) != TREE_CODE (type2))
445 return false;
446 if (TREE_CODE (type1) == RECORD_TYPE
447 && (TYPE_BINFO (type1) == NULL_TREE)
448 != (TYPE_BINFO (type2) == NULL_TREE))
449 return false;
450 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
451 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
452 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
453 return false;
455 /* At the moment we have no way to establish ODR equivalence at LTO
456 other than comparing virtual table pointers of polymorphic types.
457 Eventually we should start saving mangled names in TYPE_NAME.
458 Then this condition will become non-trivial. */
460 if (TREE_CODE (type1) == RECORD_TYPE
461 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
462 && BINFO_VTABLE (TYPE_BINFO (type1))
463 && BINFO_VTABLE (TYPE_BINFO (type2)))
465 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
466 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
467 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
468 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
469 return (operand_equal_p (TREE_OPERAND (v1, 1),
470 TREE_OPERAND (v2, 1), 0)
471 && DECL_ASSEMBLER_NAME
472 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
473 == DECL_ASSEMBLER_NAME
474 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
476 gcc_unreachable ();
478 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
479 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
482 /* Return true if we can decide on ODR equivalency.
484 In non-LTO it is always decide, in LTO however it depends in the type has
485 ODR info attached.
487 When STRICT is false, compare main variants. */
489 bool
490 types_odr_comparable (tree t1, tree t2, bool strict)
492 return (!in_lto_p
493 || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
494 && main_odr_variant (t1))
495 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
496 || (odr_type_p (t1) && odr_type_p (t2))
497 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
498 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
499 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
500 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
503 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
504 known, be conservative and return false. */
506 bool
507 types_must_be_same_for_odr (tree t1, tree t2)
509 if (types_odr_comparable (t1, t2))
510 return types_same_for_odr (t1, t2);
511 else
512 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
515 /* If T is compound type, return type it is based on. */
517 static tree
518 compound_type_base (const_tree t)
520 if (TREE_CODE (t) == ARRAY_TYPE
521 || POINTER_TYPE_P (t)
522 || TREE_CODE (t) == COMPLEX_TYPE
523 || VECTOR_TYPE_P (t))
524 return TREE_TYPE (t);
525 if (TREE_CODE (t) == METHOD_TYPE)
526 return TYPE_METHOD_BASETYPE (t);
527 if (TREE_CODE (t) == OFFSET_TYPE)
528 return TYPE_OFFSET_BASETYPE (t);
529 return NULL_TREE;
532 /* Return true if T is either ODR type or compound type based from it.
533 If the function return true, we know that T is a type originating from C++
534 source even at link-time. */
536 bool
537 odr_or_derived_type_p (const_tree t)
541 if (odr_type_p (t))
542 return true;
543 /* Function type is a tricky one. Basically we can consider it
544 ODR derived if return type or any of the parameters is.
545 We need to check all parameters because LTO streaming merges
546 common types (such as void) and they are not considered ODR then. */
547 if (TREE_CODE (t) == FUNCTION_TYPE)
549 if (TYPE_METHOD_BASETYPE (t))
550 t = TYPE_METHOD_BASETYPE (t);
551 else
553 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
554 return true;
555 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
556 if (odr_or_derived_type_p (TREE_VALUE (t)))
557 return true;
558 return false;
561 else
562 t = compound_type_base (t);
564 while (t);
565 return t;
568 /* Compare types T1 and T2 and return true if they are
569 equivalent. */
571 inline bool
572 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
574 tree t1 = o1->type;
576 gcc_checking_assert (main_odr_variant (t2) == t2);
577 gcc_checking_assert (main_odr_variant (t1) == t1);
578 if (t1 == t2)
579 return true;
580 if (!in_lto_p)
581 return false;
582 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
583 on the corresponding TYPE_STUB_DECL. */
584 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
585 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
586 return false;
587 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
588 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
589 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
590 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
593 /* Compare types T1 and T2 and return true if they are
594 equivalent. */
596 inline bool
597 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
599 tree t1 = o1->type;
601 gcc_checking_assert (main_odr_variant (t2) == t2);
602 gcc_checking_assert (main_odr_variant (t1) == t1);
603 gcc_checking_assert (in_lto_p);
604 t1 = TYPE_MAIN_VARIANT (t1);
605 t2 = TYPE_MAIN_VARIANT (t2);
606 if (t1 == t2)
607 return true;
608 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
609 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
610 return (operand_equal_p (TREE_OPERAND (v1, 1),
611 TREE_OPERAND (v2, 1), 0)
612 && DECL_ASSEMBLER_NAME
613 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
614 == DECL_ASSEMBLER_NAME
615 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
618 /* Free ODR type V. */
620 inline void
621 odr_name_hasher::remove (odr_type_d *v)
623 v->bases.release ();
624 v->derived_types.release ();
625 if (v->types_set)
626 delete v->types_set;
627 ggc_free (v);
630 /* ODR type hash used to look up ODR type based on tree type node. */
632 typedef hash_table<odr_name_hasher> odr_hash_type;
633 static odr_hash_type *odr_hash;
634 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
635 static odr_vtable_hash_type *odr_vtable_hash;
637 /* ODR types are also stored into ODR_TYPE vector to allow consistent
638 walking. Bases appear before derived types. Vector is garbage collected
639 so we won't end up visiting empty types. */
641 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
642 #define odr_types (*odr_types_ptr)
644 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
645 void
646 set_type_binfo (tree type, tree binfo)
648 for (; type; type = TYPE_NEXT_VARIANT (type))
649 if (COMPLETE_TYPE_P (type))
650 TYPE_BINFO (type) = binfo;
651 else
652 gcc_assert (!TYPE_BINFO (type));
655 /* Compare T2 and T2 based on name or structure. */
657 static bool
658 odr_subtypes_equivalent_p (tree t1, tree t2,
659 hash_set<type_pair> *visited,
660 location_t loc1, location_t loc2)
663 /* This can happen in incomplete types that should be handled earlier. */
664 gcc_assert (t1 && t2);
666 t1 = main_odr_variant (t1);
667 t2 = main_odr_variant (t2);
668 if (t1 == t2)
669 return true;
671 /* Anonymous namespace types must match exactly. */
672 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
673 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
674 return false;
676 /* For ODR types be sure to compare their names.
677 To support -wno-odr-type-merging we allow one type to be non-ODR
678 and other ODR even though it is a violation. */
679 if (types_odr_comparable (t1, t2, true))
681 if (!types_same_for_odr (t1, t2, true))
682 return false;
683 /* Limit recursion: If subtypes are ODR types and we know
684 that they are same, be happy. */
685 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated)
686 return true;
689 /* Component types, builtins and possibly violating ODR types
690 have to be compared structurally. */
691 if (TREE_CODE (t1) != TREE_CODE (t2))
692 return false;
693 if (AGGREGATE_TYPE_P (t1)
694 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
695 return false;
697 type_pair pair={t1,t2};
698 if (TYPE_UID (t1) > TYPE_UID (t2))
700 pair.first = t2;
701 pair.second = t1;
703 if (visited->add (pair))
704 return true;
705 return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
708 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
709 violation warnings. */
711 void
712 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
714 int n1, n2;
716 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
718 odr_violation_reported = true;
719 if (DECL_VIRTUAL_P (prevailing->decl))
721 varpool_node *tmp = prevailing;
722 prevailing = vtable;
723 vtable = tmp;
725 if (warning_at (DECL_SOURCE_LOCATION
726 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
727 OPT_Wodr,
728 "virtual table of type %qD violates one definition rule",
729 DECL_CONTEXT (vtable->decl)))
730 inform (DECL_SOURCE_LOCATION (prevailing->decl),
731 "variable of same assembler name as the virtual table is "
732 "defined in another translation unit");
733 return;
735 if (!prevailing->definition || !vtable->definition)
736 return;
738 /* If we do not stream ODR type info, do not bother to do useful compare. */
739 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
740 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
741 return;
743 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
745 if (class_type->odr_violated)
746 return;
748 for (n1 = 0, n2 = 0; true; n1++, n2++)
750 struct ipa_ref *ref1, *ref2;
751 bool end1, end2;
753 end1 = !prevailing->iterate_reference (n1, ref1);
754 end2 = !vtable->iterate_reference (n2, ref2);
756 /* !DECL_VIRTUAL_P means RTTI entry;
757 We warn when RTTI is lost because non-RTTI previals; we silently
758 accept the other case. */
759 while (!end2
760 && (end1
761 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
762 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
763 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
764 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
766 if (!class_type->rtti_broken
767 && warning_at (DECL_SOURCE_LOCATION
768 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
769 OPT_Wodr,
770 "virtual table of type %qD contains RTTI "
771 "information",
772 DECL_CONTEXT (vtable->decl)))
774 inform (DECL_SOURCE_LOCATION
775 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
776 "but is prevailed by one without from other translation "
777 "unit");
778 inform (DECL_SOURCE_LOCATION
779 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
780 "RTTI will not work on this type");
781 class_type->rtti_broken = true;
783 n2++;
784 end2 = !vtable->iterate_reference (n2, ref2);
786 while (!end1
787 && (end2
788 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
789 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
790 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
791 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
793 n1++;
794 end1 = !prevailing->iterate_reference (n1, ref1);
797 /* Finished? */
798 if (end1 && end2)
800 /* Extra paranoia; compare the sizes. We do not have information
801 about virtual inheritance offsets, so just be sure that these
802 match.
803 Do this as very last check so the not very informative error
804 is not output too often. */
805 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
807 class_type->odr_violated = true;
808 if (warning_at (DECL_SOURCE_LOCATION
809 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
810 OPT_Wodr,
811 "virtual table of type %qD violates "
812 "one definition rule ",
813 DECL_CONTEXT (vtable->decl)))
815 inform (DECL_SOURCE_LOCATION
816 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
817 "the conflicting type defined in another translation "
818 "unit has virtual table of different size");
821 return;
824 if (!end1 && !end2)
826 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
827 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
828 continue;
830 class_type->odr_violated = true;
832 /* If the loops above stopped on non-virtual pointer, we have
833 mismatch in RTTI information mangling. */
834 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
835 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
837 if (warning_at (DECL_SOURCE_LOCATION
838 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
839 OPT_Wodr,
840 "virtual table of type %qD violates "
841 "one definition rule ",
842 DECL_CONTEXT (vtable->decl)))
844 inform (DECL_SOURCE_LOCATION
845 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
846 "the conflicting type defined in another translation "
847 "unit with different RTTI information");
849 return;
851 /* At this point both REF1 and REF2 points either to virtual table
852 or virtual method. If one points to virtual table and other to
853 method we can complain the same way as if one table was shorter
854 than other pointing out the extra method. */
855 if (TREE_CODE (ref1->referred->decl)
856 != TREE_CODE (ref2->referred->decl))
858 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
859 end1 = true;
860 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
861 end2 = true;
865 class_type->odr_violated = true;
867 /* Complain about size mismatch. Either we have too many virutal
868 functions or too many virtual table pointers. */
869 if (end1 || end2)
871 if (end1)
873 varpool_node *tmp = prevailing;
874 prevailing = vtable;
875 vtable = tmp;
876 ref1 = ref2;
878 if (warning_at (DECL_SOURCE_LOCATION
879 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
880 OPT_Wodr,
881 "virtual table of type %qD violates "
882 "one definition rule",
883 DECL_CONTEXT (vtable->decl)))
885 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
887 inform (DECL_SOURCE_LOCATION
888 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
889 "the conflicting type defined in another translation "
890 "unit");
891 inform (DECL_SOURCE_LOCATION
892 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
893 "contains additional virtual method %qD",
894 ref1->referred->decl);
896 else
898 inform (DECL_SOURCE_LOCATION
899 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
900 "the conflicting type defined in another translation "
901 "unit has virtual table with more entries");
904 return;
907 /* And in the last case we have either mistmatch in between two virtual
908 methods or two virtual table pointers. */
909 if (warning_at (DECL_SOURCE_LOCATION
910 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
911 "virtual table of type %qD violates "
912 "one definition rule ",
913 DECL_CONTEXT (vtable->decl)))
915 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
917 inform (DECL_SOURCE_LOCATION
918 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
919 "the conflicting type defined in another translation "
920 "unit");
921 gcc_assert (TREE_CODE (ref2->referred->decl)
922 == FUNCTION_DECL);
923 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
924 "virtual method %qD", ref1->referred->decl);
925 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
926 "ought to match virtual method %qD but does not",
927 ref2->referred->decl);
929 else
930 inform (DECL_SOURCE_LOCATION
931 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
932 "the conflicting type defined in another translation "
933 "unit has virtual table with different contents");
934 return;
939 /* Output ODR violation warning about T1 and T2 with REASON.
940 Display location of ST1 and ST2 if REASON speaks about field or
941 method of the type.
942 If WARN is false, do nothing. Set WARNED if warning was indeed
943 output. */
945 void
946 warn_odr (tree t1, tree t2, tree st1, tree st2,
947 bool warn, bool *warned, const char *reason)
949 tree decl2 = TYPE_NAME (t2);
950 if (warned)
951 *warned = false;
953 if (!warn || !TYPE_NAME(t1))
954 return;
956 /* ODR warnings are output druing LTO streaming; we must apply location
957 cache for potential warnings to be output correctly. */
958 if (lto_location_cache::current_cache)
959 lto_location_cache::current_cache->apply_location_cache ();
961 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
962 "type %qT violates the C++ One Definition Rule",
963 t1))
964 return;
965 if (!st1 && !st2)
967 /* For FIELD_DECL support also case where one of fields is
968 NULL - this is used when the structures have mismatching number of
969 elements. */
970 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
972 inform (DECL_SOURCE_LOCATION (decl2),
973 "a different type is defined in another translation unit");
974 if (!st1)
976 st1 = st2;
977 st2 = NULL;
979 inform (DECL_SOURCE_LOCATION (st1),
980 "the first difference of corresponding definitions is field %qD",
981 st1);
982 if (st2)
983 decl2 = st2;
985 else if (TREE_CODE (st1) == FUNCTION_DECL)
987 inform (DECL_SOURCE_LOCATION (decl2),
988 "a different type is defined in another translation unit");
989 inform (DECL_SOURCE_LOCATION (st1),
990 "the first difference of corresponding definitions is method %qD",
991 st1);
992 decl2 = st2;
994 else
995 return;
996 inform (DECL_SOURCE_LOCATION (decl2), reason);
998 if (warned)
999 *warned = true;
1002 /* Return ture if T1 and T2 are incompatible and we want to recusively
1003 dive into them from warn_type_mismatch to give sensible answer. */
1005 static bool
1006 type_mismatch_p (tree t1, tree t2)
1008 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1009 && !odr_types_equivalent_p (t1, t2))
1010 return true;
1011 return !types_compatible_p (t1, t2);
1015 /* Types T1 and T2 was found to be incompatible in a context they can't
1016 (either used to declare a symbol of same assembler name or unified by
1017 ODR rule). We already output warning about this, but if possible, output
1018 extra information on how the types mismatch.
1020 This is hard to do in general. We basically handle the common cases.
1022 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1023 themselves do no thave one.*/
1025 void
1026 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1028 /* Location of type is known only if it has TYPE_NAME and the name is
1029 TYPE_DECL. */
1030 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1031 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1032 : UNKNOWN_LOCATION;
1033 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1034 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1035 : UNKNOWN_LOCATION;
1036 bool loc_t2_useful = false;
1038 /* With LTO it is a common case that the location of both types match.
1039 See if T2 has a location that is different from T1. If so, we will
1040 inform user about the location.
1041 Do not consider the location passed to us in LOC1/LOC2 as those are
1042 already output. */
1043 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1045 if (loc_t1 <= BUILTINS_LOCATION)
1046 loc_t2_useful = true;
1047 else
1049 expanded_location xloc1 = expand_location (loc_t1);
1050 expanded_location xloc2 = expand_location (loc_t2);
1052 if (strcmp (xloc1.file, xloc2.file)
1053 || xloc1.line != xloc2.line
1054 || xloc1.column != xloc2.column)
1055 loc_t2_useful = true;
1059 if (loc_t1 <= BUILTINS_LOCATION)
1060 loc_t1 = loc1;
1061 if (loc_t2 <= BUILTINS_LOCATION)
1062 loc_t2 = loc2;
1064 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1066 /* It is a quite common bug to reference anonymous namespace type in
1067 non-anonymous namespace class. */
1068 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1069 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1071 if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
1073 std::swap (t1, t2);
1074 std::swap (loc_t1, loc_t2);
1076 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1077 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1078 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1079 /* Most of the time, the type names will match, do not be unnecesarily
1080 verbose. */
1081 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
1082 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
1083 inform (loc_t1,
1084 "type %qT defined in anonymous namespace can not match "
1085 "type %qT across the translation unit boundary",
1086 t1, t2);
1087 else
1088 inform (loc_t1,
1089 "type %qT defined in anonymous namespace can not match "
1090 "across the translation unit boundary",
1091 t1);
1092 if (loc_t2_useful)
1093 inform (loc_t2,
1094 "the incompatible type defined in another translation unit");
1095 return;
1097 /* If types have mangled ODR names and they are different, it is most
1098 informative to output those.
1099 This also covers types defined in different namespaces. */
1100 if (TYPE_NAME (t1) && TYPE_NAME (t2)
1101 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1102 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1103 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
1104 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
1105 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
1106 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1108 char *name1 = xstrdup (cplus_demangle
1109 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1110 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1111 char *name2 = cplus_demangle
1112 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1113 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1114 if (name1 && name2 && strcmp (name1, name2))
1116 inform (loc_t1,
1117 "type name %<%s%> should match type name %<%s%>",
1118 name1, name2);
1119 if (loc_t2_useful)
1120 inform (loc_t2,
1121 "the incompatible type is defined here");
1122 free (name1);
1123 return;
1125 free (name1);
1127 /* A tricky case are compound types. Often they appear the same in source
1128 code and the mismatch is dragged in by type they are build from.
1129 Look for those differences in subtypes and try to be informative. In other
1130 cases just output nothing because the source code is probably different
1131 and in this case we already output a all necessary info. */
1132 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1134 if (TREE_CODE (t1) == TREE_CODE (t2))
1136 if (TREE_CODE (t1) == ARRAY_TYPE
1137 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1139 tree i1 = TYPE_DOMAIN (t1);
1140 tree i2 = TYPE_DOMAIN (t2);
1142 if (i1 && i2
1143 && TYPE_MAX_VALUE (i1)
1144 && TYPE_MAX_VALUE (i2)
1145 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1146 TYPE_MAX_VALUE (i2), 0))
1148 inform (loc,
1149 "array types have different bounds");
1150 return;
1153 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1154 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1155 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1156 else if (TREE_CODE (t1) == METHOD_TYPE
1157 || TREE_CODE (t1) == FUNCTION_TYPE)
1159 tree parms1 = NULL, parms2 = NULL;
1160 int count = 1;
1162 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1164 inform (loc, "return value type mismatch");
1165 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1166 loc_t2);
1167 return;
1169 if (prototype_p (t1) && prototype_p (t2))
1170 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1171 parms1 && parms2;
1172 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1173 count++)
1175 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1177 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1178 inform (loc,
1179 "implicit this pointer type mismatch");
1180 else
1181 inform (loc,
1182 "type mismatch in parameter %i",
1183 count - (TREE_CODE (t1) == METHOD_TYPE));
1184 warn_types_mismatch (TREE_VALUE (parms1),
1185 TREE_VALUE (parms2),
1186 loc_t1, loc_t2);
1187 return;
1190 if (parms1 || parms2)
1192 inform (loc,
1193 "types have different parameter counts");
1194 return;
1198 return;
1201 if (types_odr_comparable (t1, t2, true)
1202 && types_same_for_odr (t1, t2, true))
1203 inform (loc_t1,
1204 "type %qT itself violate the C++ One Definition Rule", t1);
1205 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1206 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1207 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1208 return;
1209 else
1210 inform (loc_t1, "type %qT should match type %qT",
1211 t1, t2);
1212 if (loc_t2_useful)
1213 inform (loc_t2, "the incompatible type is defined here");
1216 /* Compare T1 and T2, report ODR violations if WARN is true and set
1217 WARNED to true if anything is reported. Return true if types match.
1218 If true is returned, the types are also compatible in the sense of
1219 gimple_canonical_types_compatible_p.
1220 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1221 about the type if the type itself do not have location. */
1223 static bool
1224 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1225 hash_set<type_pair> *visited,
1226 location_t loc1, location_t loc2)
1228 /* Check first for the obvious case of pointer identity. */
1229 if (t1 == t2)
1230 return true;
1231 gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
1232 gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
1234 /* Can't be the same type if the types don't have the same code. */
1235 if (TREE_CODE (t1) != TREE_CODE (t2))
1237 warn_odr (t1, t2, NULL, NULL, warn, warned,
1238 G_("a different type is defined in another translation unit"));
1239 return false;
1242 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1244 warn_odr (t1, t2, NULL, NULL, warn, warned,
1245 G_("a type with different qualifiers is defined in another "
1246 "translation unit"));
1247 return false;
1250 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1251 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1253 /* We can not trip this when comparing ODR types, only when trying to
1254 match different ODR derivations from different declarations.
1255 So WARN should be always false. */
1256 gcc_assert (!warn);
1257 return false;
1260 if (comp_type_attributes (t1, t2) != 1)
1262 warn_odr (t1, t2, NULL, NULL, warn, warned,
1263 G_("a type with different attributes "
1264 "is defined in another translation unit"));
1265 return false;
1268 if (TREE_CODE (t1) == ENUMERAL_TYPE
1269 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1271 tree v1, v2;
1272 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1273 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1275 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1277 warn_odr (t1, t2, NULL, NULL, warn, warned,
1278 G_("an enum with different value name"
1279 " is defined in another translation unit"));
1280 return false;
1282 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1283 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1284 DECL_INITIAL (TREE_VALUE (v2)), 0))
1286 warn_odr (t1, t2, NULL, NULL, warn, warned,
1287 G_("an enum with different values is defined"
1288 " in another translation unit"));
1289 return false;
1292 if (v1 || v2)
1294 warn_odr (t1, t2, NULL, NULL, warn, warned,
1295 G_("an enum with mismatching number of values "
1296 "is defined in another translation unit"));
1297 return false;
1301 /* Non-aggregate types can be handled cheaply. */
1302 if (INTEGRAL_TYPE_P (t1)
1303 || SCALAR_FLOAT_TYPE_P (t1)
1304 || FIXED_POINT_TYPE_P (t1)
1305 || TREE_CODE (t1) == VECTOR_TYPE
1306 || TREE_CODE (t1) == COMPLEX_TYPE
1307 || TREE_CODE (t1) == OFFSET_TYPE
1308 || POINTER_TYPE_P (t1))
1310 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1312 warn_odr (t1, t2, NULL, NULL, warn, warned,
1313 G_("a type with different precision is defined "
1314 "in another translation unit"));
1315 return false;
1317 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1319 warn_odr (t1, t2, NULL, NULL, warn, warned,
1320 G_("a type with different signedness is defined "
1321 "in another translation unit"));
1322 return false;
1325 if (TREE_CODE (t1) == INTEGER_TYPE
1326 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1328 /* char WRT uint_8? */
1329 warn_odr (t1, t2, NULL, NULL, warn, warned,
1330 G_("a different type is defined in another "
1331 "translation unit"));
1332 return false;
1335 /* For canonical type comparisons we do not want to build SCCs
1336 so we cannot compare pointed-to types. But we can, for now,
1337 require the same pointed-to type kind and match what
1338 useless_type_conversion_p would do. */
1339 if (POINTER_TYPE_P (t1))
1341 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1342 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1344 warn_odr (t1, t2, NULL, NULL, warn, warned,
1345 G_("it is defined as a pointer in different address "
1346 "space in another translation unit"));
1347 return false;
1350 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1351 visited, loc1, loc2))
1353 warn_odr (t1, t2, NULL, NULL, warn, warned,
1354 G_("it is defined as a pointer to different type "
1355 "in another translation unit"));
1356 if (warn && warned)
1357 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1358 loc1, loc2);
1359 return false;
1363 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1364 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1365 visited, loc1, loc2))
1367 /* Probably specific enough. */
1368 warn_odr (t1, t2, NULL, NULL, warn, warned,
1369 G_("a different type is defined "
1370 "in another translation unit"));
1371 if (warn && warned)
1372 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1373 return false;
1376 /* Do type-specific comparisons. */
1377 else switch (TREE_CODE (t1))
1379 case ARRAY_TYPE:
1381 /* Array types are the same if the element types are the same and
1382 the number of elements are the same. */
1383 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1384 visited, loc1, loc2))
1386 warn_odr (t1, t2, NULL, NULL, warn, warned,
1387 G_("a different type is defined in another "
1388 "translation unit"));
1389 if (warn && warned)
1390 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1392 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1393 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1394 == TYPE_NONALIASED_COMPONENT (t2));
1396 tree i1 = TYPE_DOMAIN (t1);
1397 tree i2 = TYPE_DOMAIN (t2);
1399 /* For an incomplete external array, the type domain can be
1400 NULL_TREE. Check this condition also. */
1401 if (i1 == NULL_TREE || i2 == NULL_TREE)
1402 return true;
1404 tree min1 = TYPE_MIN_VALUE (i1);
1405 tree min2 = TYPE_MIN_VALUE (i2);
1406 tree max1 = TYPE_MAX_VALUE (i1);
1407 tree max2 = TYPE_MAX_VALUE (i2);
1409 /* In C++, minimums should be always 0. */
1410 gcc_assert (min1 == min2);
1411 if (!operand_equal_p (max1, max2, 0))
1413 warn_odr (t1, t2, NULL, NULL, warn, warned,
1414 G_("an array of different size is defined "
1415 "in another translation unit"));
1416 return false;
1419 break;
1421 case METHOD_TYPE:
1422 case FUNCTION_TYPE:
1423 /* Function types are the same if the return type and arguments types
1424 are the same. */
1425 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1426 visited, loc1, loc2))
1428 warn_odr (t1, t2, NULL, NULL, warn, warned,
1429 G_("has different return value "
1430 "in another translation unit"));
1431 if (warn && warned)
1432 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1433 return false;
1436 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1437 || !prototype_p (t1) || !prototype_p (t2))
1438 return true;
1439 else
1441 tree parms1, parms2;
1443 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1444 parms1 && parms2;
1445 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1447 if (!odr_subtypes_equivalent_p
1448 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
1449 loc1, loc2))
1451 warn_odr (t1, t2, NULL, NULL, warn, warned,
1452 G_("has different parameters in another "
1453 "translation unit"));
1454 if (warn && warned)
1455 warn_types_mismatch (TREE_VALUE (parms1),
1456 TREE_VALUE (parms2), loc1, loc2);
1457 return false;
1461 if (parms1 || parms2)
1463 warn_odr (t1, t2, NULL, NULL, warn, warned,
1464 G_("has different parameters "
1465 "in another translation unit"));
1466 return false;
1469 return true;
1472 case RECORD_TYPE:
1473 case UNION_TYPE:
1474 case QUAL_UNION_TYPE:
1476 tree f1, f2;
1478 /* For aggregate types, all the fields must be the same. */
1479 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1481 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1482 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1483 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1485 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1486 warn_odr (t1, t2, NULL, NULL, warn, warned,
1487 G_("a type defined in another translation unit "
1488 "is not polymorphic"));
1489 else
1490 warn_odr (t1, t2, NULL, NULL, warn, warned,
1491 G_("a type defined in another translation unit "
1492 "is polymorphic"));
1493 return false;
1495 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1496 f1 || f2;
1497 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1499 /* Skip non-fields. */
1500 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1501 f1 = TREE_CHAIN (f1);
1502 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1503 f2 = TREE_CHAIN (f2);
1504 if (!f1 || !f2)
1505 break;
1506 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1508 warn_odr (t1, t2, NULL, NULL, warn, warned,
1509 G_("a type with different virtual table pointers"
1510 " is defined in another translation unit"));
1511 return false;
1513 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1515 warn_odr (t1, t2, NULL, NULL, warn, warned,
1516 G_("a type with different bases is defined "
1517 "in another translation unit"));
1518 return false;
1520 if (DECL_NAME (f1) != DECL_NAME (f2)
1521 && !DECL_ARTIFICIAL (f1))
1523 warn_odr (t1, t2, f1, f2, warn, warned,
1524 G_("a field with different name is defined "
1525 "in another translation unit"));
1526 return false;
1528 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1529 TREE_TYPE (f2), visited,
1530 loc1, loc2))
1532 /* Do not warn about artificial fields and just go into
1533 generic field mismatch warning. */
1534 if (DECL_ARTIFICIAL (f1))
1535 break;
1537 warn_odr (t1, t2, f1, f2, warn, warned,
1538 G_("a field of same name but different type "
1539 "is defined in another translation unit"));
1540 if (warn && warned)
1541 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1542 return false;
1544 if (!gimple_compare_field_offset (f1, f2))
1546 /* Do not warn about artificial fields and just go into
1547 generic field mismatch warning. */
1548 if (DECL_ARTIFICIAL (f1))
1549 break;
1550 warn_odr (t1, t2, f1, f2, warn, warned,
1551 G_("fields has different layout "
1552 "in another translation unit"));
1553 return false;
1555 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1556 == DECL_NONADDRESSABLE_P (f2));
1559 /* If one aggregate has more fields than the other, they
1560 are not the same. */
1561 if (f1 || f2)
1563 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1564 warn_odr (t1, t2, NULL, NULL, warn, warned,
1565 G_("a type with different virtual table pointers"
1566 " is defined in another translation unit"));
1567 else if ((f1 && DECL_ARTIFICIAL (f1))
1568 || (f2 && DECL_ARTIFICIAL (f2)))
1569 warn_odr (t1, t2, NULL, NULL, warn, warned,
1570 G_("a type with different bases is defined "
1571 "in another translation unit"));
1572 else
1573 warn_odr (t1, t2, f1, f2, warn, warned,
1574 G_("a type with different number of fields "
1575 "is defined in another translation unit"));
1577 return false;
1579 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1580 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1))
1581 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2))
1582 && odr_type_p (TYPE_MAIN_VARIANT (t1))
1583 && odr_type_p (TYPE_MAIN_VARIANT (t2))
1584 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1585 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1587 /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
1588 if it is non-NULL so this loop will never realy execute. */
1589 if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1)) != error_mark_node
1590 && TYPE_METHODS (TYPE_MAIN_VARIANT (t2)) != error_mark_node)
1591 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1592 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1593 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1595 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1597 warn_odr (t1, t2, f1, f2, warn, warned,
1598 G_("a different method of same type "
1599 "is defined in another "
1600 "translation unit"));
1601 return false;
1603 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1605 warn_odr (t1, t2, f1, f2, warn, warned,
1606 G_("s definition that differs by virtual "
1607 "keyword in another translation unit"));
1608 return false;
1610 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1612 warn_odr (t1, t2, f1, f2, warn, warned,
1613 G_("virtual table layout differs "
1614 "in another translation unit"));
1615 return false;
1617 if (odr_subtypes_equivalent_p (TREE_TYPE (f1),
1618 TREE_TYPE (f2), visited,
1619 loc1, loc2))
1621 warn_odr (t1, t2, f1, f2, warn, warned,
1622 G_("method with incompatible type is "
1623 "defined in another translation unit"));
1624 return false;
1627 if ((f1 == NULL) != (f2 == NULL))
1629 warn_odr (t1, t2, NULL, NULL, warn, warned,
1630 G_("a type with different number of methods "
1631 "is defined in another translation unit"));
1632 return false;
1636 break;
1638 case VOID_TYPE:
1639 case NULLPTR_TYPE:
1640 break;
1642 default:
1643 debug_tree (t1);
1644 gcc_unreachable ();
1647 /* Those are better to come last as they are utterly uninformative. */
1648 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1649 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1651 warn_odr (t1, t2, NULL, NULL, warn, warned,
1652 G_("a type with different size "
1653 "is defined in another translation unit"));
1654 return false;
1656 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1657 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1659 warn_odr (t1, t2, NULL, NULL, warn, warned,
1660 G_("a type with different alignment "
1661 "is defined in another translation unit"));
1662 return false;
1664 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1665 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1666 TYPE_SIZE_UNIT (t2), 0));
1667 return true;
1670 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1672 bool
1673 odr_types_equivalent_p (tree type1, tree type2)
1675 gcc_checking_assert (odr_or_derived_type_p (type1)
1676 && odr_or_derived_type_p (type2));
1678 hash_set<type_pair> visited;
1679 return odr_types_equivalent_p (type1, type2, false, NULL,
1680 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1683 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1684 from VAL->type. This may happen in LTO where tree merging did not merge
1685 all variants of the same type or due to ODR violation.
1687 Analyze and report ODR violations and add type to duplicate list.
1688 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1689 this is first time we see definition of a class return true so the
1690 base types are analyzed. */
1692 static bool
1693 add_type_duplicate (odr_type val, tree type)
1695 bool build_bases = false;
1696 bool prevail = false;
1697 bool odr_must_violate = false;
1699 if (!val->types_set)
1700 val->types_set = new hash_set<tree>;
1702 /* Chose polymorphic type as leader (this happens only in case of ODR
1703 violations. */
1704 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1705 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1706 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1707 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1709 prevail = true;
1710 build_bases = true;
1712 /* Always prefer complete type to be the leader. */
1713 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1715 prevail = true;
1716 build_bases = TYPE_BINFO (type);
1718 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1720 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1721 && TREE_CODE (type) == ENUMERAL_TYPE
1722 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1723 prevail = true;
1724 else if (TREE_CODE (val->type) == RECORD_TYPE
1725 && TREE_CODE (type) == RECORD_TYPE
1726 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1728 gcc_assert (!val->bases.length ());
1729 build_bases = true;
1730 prevail = true;
1733 if (prevail)
1734 std::swap (val->type, type);
1736 val->types_set->add (type);
1738 /* If we now have a mangled name, be sure to record it to val->type
1739 so ODR hash can work. */
1741 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1742 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1743 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1745 bool merge = true;
1746 bool base_mismatch = false;
1747 unsigned int i;
1748 bool warned = false;
1749 hash_set<type_pair> visited;
1751 gcc_assert (in_lto_p);
1752 vec_safe_push (val->types, type);
1754 /* If both are class types, compare the bases. */
1755 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1756 && TREE_CODE (val->type) == RECORD_TYPE
1757 && TREE_CODE (type) == RECORD_TYPE
1758 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1760 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1761 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1763 if (!flag_ltrans && !warned && !val->odr_violated)
1765 tree extra_base;
1766 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1767 "a type with the same name but different "
1768 "number of polymorphic bases is "
1769 "defined in another translation unit");
1770 if (warned)
1772 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1773 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1774 extra_base = BINFO_BASE_BINFO
1775 (TYPE_BINFO (type),
1776 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1777 else
1778 extra_base = BINFO_BASE_BINFO
1779 (TYPE_BINFO (val->type),
1780 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1781 tree extra_base_type = BINFO_TYPE (extra_base);
1782 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1783 "the extra base is defined here");
1786 base_mismatch = true;
1788 else
1789 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1791 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1792 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1793 tree type1 = BINFO_TYPE (base1);
1794 tree type2 = BINFO_TYPE (base2);
1796 if (types_odr_comparable (type1, type2))
1798 if (!types_same_for_odr (type1, type2))
1799 base_mismatch = true;
1801 else
1802 if (!odr_types_equivalent_p (type1, type2))
1803 base_mismatch = true;
1804 if (base_mismatch)
1806 if (!warned && !val->odr_violated)
1808 warn_odr (type, val->type, NULL, NULL,
1809 !warned, &warned,
1810 "a type with the same name but different base "
1811 "type is defined in another translation unit");
1812 if (warned)
1813 warn_types_mismatch (type1, type2,
1814 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1816 break;
1818 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1820 base_mismatch = true;
1821 if (!warned && !val->odr_violated)
1822 warn_odr (type, val->type, NULL, NULL,
1823 !warned, &warned,
1824 "a type with the same name but different base "
1825 "layout is defined in another translation unit");
1826 break;
1828 /* One of bases is not of complete type. */
1829 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1831 /* If we have a polymorphic type info specified for TYPE1
1832 but not for TYPE2 we possibly missed a base when recording
1833 VAL->type earlier.
1834 Be sure this does not happen. */
1835 if (TYPE_BINFO (type1)
1836 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1837 && !build_bases)
1838 odr_must_violate = true;
1839 break;
1841 /* One base is polymorphic and the other not.
1842 This ought to be diagnosed earlier, but do not ICE in the
1843 checking bellow. */
1844 else if (TYPE_BINFO (type1)
1845 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1846 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1848 if (!warned && !val->odr_violated)
1849 warn_odr (type, val->type, NULL, NULL,
1850 !warned, &warned,
1851 "a base of the type is polymorphic only in one "
1852 "translation unit");
1853 base_mismatch = true;
1854 break;
1857 if (base_mismatch)
1859 merge = false;
1860 odr_violation_reported = true;
1861 val->odr_violated = true;
1863 if (symtab->dump_file)
1865 fprintf (symtab->dump_file, "ODR base violation\n");
1867 print_node (symtab->dump_file, "", val->type, 0);
1868 putc ('\n',symtab->dump_file);
1869 print_node (symtab->dump_file, "", type, 0);
1870 putc ('\n',symtab->dump_file);
1875 /* Next compare memory layout. */
1876 if (!odr_types_equivalent_p (val->type, type,
1877 !flag_ltrans && !val->odr_violated && !warned,
1878 &warned, &visited,
1879 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1880 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1882 merge = false;
1883 odr_violation_reported = true;
1884 val->odr_violated = true;
1886 gcc_assert (val->odr_violated || !odr_must_violate);
1887 /* Sanity check that all bases will be build same way again. */
1888 if (flag_checking
1889 && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1890 && TREE_CODE (val->type) == RECORD_TYPE
1891 && TREE_CODE (type) == RECORD_TYPE
1892 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1893 && !val->odr_violated
1894 && !base_mismatch && val->bases.length ())
1896 unsigned int num_poly_bases = 0;
1897 unsigned int j;
1899 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1900 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1901 (TYPE_BINFO (type), i)))
1902 num_poly_bases++;
1903 gcc_assert (num_poly_bases == val->bases.length ());
1904 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1905 i++)
1906 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1907 (TYPE_BINFO (type), i)))
1909 odr_type base = get_odr_type
1910 (BINFO_TYPE
1911 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1912 i)),
1913 true);
1914 gcc_assert (val->bases[j] == base);
1915 j++;
1920 /* Regularize things a little. During LTO same types may come with
1921 different BINFOs. Either because their virtual table was
1922 not merged by tree merging and only later at decl merging or
1923 because one type comes with external vtable, while other
1924 with internal. We want to merge equivalent binfos to conserve
1925 memory and streaming overhead.
1927 The external vtables are more harmful: they contain references
1928 to external declarations of methods that may be defined in the
1929 merged LTO unit. For this reason we absolutely need to remove
1930 them and replace by internal variants. Not doing so will lead
1931 to incomplete answers from possible_polymorphic_call_targets.
1933 FIXME: disable for now; because ODR types are now build during
1934 streaming in, the variants do not need to be linked to the type,
1935 yet. We need to do the merging in cleanup pass to be implemented
1936 soon. */
1937 if (!flag_ltrans && merge
1938 && 0
1939 && TREE_CODE (val->type) == RECORD_TYPE
1940 && TREE_CODE (type) == RECORD_TYPE
1941 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1942 && TYPE_MAIN_VARIANT (type) == type
1943 && TYPE_MAIN_VARIANT (val->type) == val->type
1944 && BINFO_VTABLE (TYPE_BINFO (val->type))
1945 && BINFO_VTABLE (TYPE_BINFO (type)))
1947 tree master_binfo = TYPE_BINFO (val->type);
1948 tree v1 = BINFO_VTABLE (master_binfo);
1949 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1951 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1953 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1954 && operand_equal_p (TREE_OPERAND (v1, 1),
1955 TREE_OPERAND (v2, 1), 0));
1956 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1957 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1959 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1960 == DECL_ASSEMBLER_NAME (v2));
1962 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1964 unsigned int i;
1966 set_type_binfo (val->type, TYPE_BINFO (type));
1967 for (i = 0; i < val->types->length (); i++)
1969 if (TYPE_BINFO ((*val->types)[i])
1970 == master_binfo)
1971 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1973 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1975 else
1976 set_type_binfo (type, master_binfo);
1978 return build_bases;
1981 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1982 possibly new entry. */
1984 odr_type
1985 get_odr_type (tree type, bool insert)
1987 odr_type_d **slot = NULL;
1988 odr_type_d **vtable_slot = NULL;
1989 odr_type val = NULL;
1990 hashval_t hash;
1991 bool build_bases = false;
1992 bool insert_to_odr_array = false;
1993 int base_id = -1;
1995 type = main_odr_variant (type);
1997 gcc_checking_assert (can_be_name_hashed_p (type)
1998 || can_be_vtable_hashed_p (type));
2000 /* Lookup entry, first try name hash, fallback to vtable hash. */
2001 if (can_be_name_hashed_p (type))
2003 hash = hash_odr_name (type);
2004 slot = odr_hash->find_slot_with_hash (type, hash,
2005 insert ? INSERT : NO_INSERT);
2007 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2009 hash = hash_odr_vtable (type);
2010 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2011 insert ? INSERT : NO_INSERT);
2014 if (!slot && !vtable_slot)
2015 return NULL;
2017 /* See if we already have entry for type. */
2018 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2020 if (slot && *slot)
2022 val = *slot;
2023 if (flag_checking
2024 && in_lto_p && can_be_vtable_hashed_p (type))
2026 hash = hash_odr_vtable (type);
2027 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2028 NO_INSERT);
2029 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2030 vtable_slot = NULL;
2033 else if (*vtable_slot)
2034 val = *vtable_slot;
2036 if (val->type != type
2037 && (!val->types_set || !val->types_set->add (type)))
2039 gcc_assert (insert);
2040 /* We have type duplicate, but it may introduce vtable name or
2041 mangled name; be sure to keep hashes in sync. */
2042 if (in_lto_p && can_be_vtable_hashed_p (type)
2043 && (!vtable_slot || !*vtable_slot))
2045 if (!vtable_slot)
2047 hash = hash_odr_vtable (type);
2048 vtable_slot = odr_vtable_hash->find_slot_with_hash
2049 (type, hash, INSERT);
2050 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2052 *vtable_slot = val;
2054 if (slot && !*slot)
2055 *slot = val;
2056 build_bases = add_type_duplicate (val, type);
2059 else
2061 val = ggc_cleared_alloc<odr_type_d> ();
2062 val->type = type;
2063 val->bases = vNULL;
2064 val->derived_types = vNULL;
2065 if (type_with_linkage_p (type))
2066 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2067 else
2068 val->anonymous_namespace = 0;
2069 build_bases = COMPLETE_TYPE_P (val->type);
2070 insert_to_odr_array = true;
2071 if (slot)
2072 *slot = val;
2073 if (vtable_slot)
2074 *vtable_slot = val;
2077 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2078 && type_with_linkage_p (type)
2079 && type == TYPE_MAIN_VARIANT (type))
2081 tree binfo = TYPE_BINFO (type);
2082 unsigned int i;
2084 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2086 val->all_derivations_known = type_all_derivations_known_p (type);
2087 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2088 /* For now record only polymorphic types. other are
2089 pointless for devirtualization and we can not precisely
2090 determine ODR equivalency of these during LTO. */
2091 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2093 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2094 odr_type base = get_odr_type (base_type, true);
2095 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2096 base->derived_types.safe_push (val);
2097 val->bases.safe_push (base);
2098 if (base->id > base_id)
2099 base_id = base->id;
2102 /* Ensure that type always appears after bases. */
2103 if (insert_to_odr_array)
2105 if (odr_types_ptr)
2106 val->id = odr_types.length ();
2107 vec_safe_push (odr_types_ptr, val);
2109 else if (base_id > val->id)
2111 odr_types[val->id] = 0;
2112 /* Be sure we did not recorded any derived types; these may need
2113 renumbering too. */
2114 gcc_assert (val->derived_types.length() == 0);
2115 if (odr_types_ptr)
2116 val->id = odr_types.length ();
2117 vec_safe_push (odr_types_ptr, val);
2119 return val;
2122 /* Add TYPE od ODR type hash. */
2124 void
2125 register_odr_type (tree type)
2127 if (!odr_hash)
2129 odr_hash = new odr_hash_type (23);
2130 if (in_lto_p)
2131 odr_vtable_hash = new odr_vtable_hash_type (23);
2133 /* Arrange things to be nicer and insert main variants first.
2134 ??? fundamental prerecorded types do not have mangled names; this
2135 makes it possible that non-ODR type is main_odr_variant of ODR type.
2136 Things may get smoother if LTO FE set mangled name of those types same
2137 way as C++ FE does. */
2138 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
2139 && odr_type_p (TYPE_MAIN_VARIANT (type)))
2140 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2141 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
2142 get_odr_type (type, true);
2145 /* Return true if type is known to have no derivations. */
2147 bool
2148 type_known_to_have_no_derivations_p (tree t)
2150 return (type_all_derivations_known_p (t)
2151 && (TYPE_FINAL_P (t)
2152 || (odr_hash
2153 && !get_odr_type (t, true)->derived_types.length())));
2156 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2157 recursive printing. */
2159 static void
2160 dump_odr_type (FILE *f, odr_type t, int indent=0)
2162 unsigned int i;
2163 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2164 print_generic_expr (f, t->type, TDF_SLIM);
2165 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2166 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2167 if (TYPE_NAME (t->type))
2169 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2170 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2171 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2172 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2173 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2174 IDENTIFIER_POINTER
2175 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2177 if (t->bases.length ())
2179 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2180 for (i = 0; i < t->bases.length (); i++)
2181 fprintf (f, " %i", t->bases[i]->id);
2182 fprintf (f, "\n");
2184 if (t->derived_types.length ())
2186 fprintf (f, "%*s derived types:\n", indent * 2, "");
2187 for (i = 0; i < t->derived_types.length (); i++)
2188 dump_odr_type (f, t->derived_types[i], indent + 1);
2190 fprintf (f, "\n");
2193 /* Dump the type inheritance graph. */
2195 static void
2196 dump_type_inheritance_graph (FILE *f)
2198 unsigned int i;
2199 if (!odr_types_ptr)
2200 return;
2201 fprintf (f, "\n\nType inheritance graph:\n");
2202 for (i = 0; i < odr_types.length (); i++)
2204 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2205 dump_odr_type (f, odr_types[i]);
2207 for (i = 0; i < odr_types.length (); i++)
2209 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2211 unsigned int j;
2212 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2213 print_node (f, "", odr_types[i]->type, 0);
2214 for (j = 0; j < odr_types[i]->types->length (); j++)
2216 tree t;
2217 fprintf (f, "duplicate #%i\n", j);
2218 print_node (f, "", (*odr_types[i]->types)[j], 0);
2219 t = (*odr_types[i]->types)[j];
2220 while (TYPE_P (t) && TYPE_CONTEXT (t))
2222 t = TYPE_CONTEXT (t);
2223 print_node (f, "", t, 0);
2225 putc ('\n',f);
2231 /* Initialize IPA devirt and build inheritance tree graph. */
2233 void
2234 build_type_inheritance_graph (void)
2236 struct symtab_node *n;
2237 FILE *inheritance_dump_file;
2238 int flags;
2240 if (odr_hash)
2241 return;
2242 timevar_push (TV_IPA_INHERITANCE);
2243 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2244 odr_hash = new odr_hash_type (23);
2245 if (in_lto_p)
2246 odr_vtable_hash = new odr_vtable_hash_type (23);
2248 /* We reconstruct the graph starting of types of all methods seen in the
2249 unit. */
2250 FOR_EACH_SYMBOL (n)
2251 if (is_a <cgraph_node *> (n)
2252 && DECL_VIRTUAL_P (n->decl)
2253 && n->real_symbol_p ())
2254 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2256 /* Look also for virtual tables of types that do not define any methods.
2258 We need it in a case where class B has virtual base of class A
2259 re-defining its virtual method and there is class C with no virtual
2260 methods with B as virtual base.
2262 Here we output B's virtual method in two variant - for non-virtual
2263 and virtual inheritance. B's virtual table has non-virtual version,
2264 while C's has virtual.
2266 For this reason we need to know about C in order to include both
2267 variants of B. More correctly, record_target_from_binfo should
2268 add both variants of the method when walking B, but we have no
2269 link in between them.
2271 We rely on fact that either the method is exported and thus we
2272 assume it is called externally or C is in anonymous namespace and
2273 thus we will see the vtable. */
2275 else if (is_a <varpool_node *> (n)
2276 && DECL_VIRTUAL_P (n->decl)
2277 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2278 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2279 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2280 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2281 if (inheritance_dump_file)
2283 dump_type_inheritance_graph (inheritance_dump_file);
2284 dump_end (TDI_inheritance, inheritance_dump_file);
2286 timevar_pop (TV_IPA_INHERITANCE);
2289 /* Return true if N has reference from live virtual table
2290 (and thus can be a destination of polymorphic call).
2291 Be conservatively correct when callgraph is not built or
2292 if the method may be referred externally. */
2294 static bool
2295 referenced_from_vtable_p (struct cgraph_node *node)
2297 int i;
2298 struct ipa_ref *ref;
2299 bool found = false;
2301 if (node->externally_visible
2302 || DECL_EXTERNAL (node->decl)
2303 || node->used_from_other_partition)
2304 return true;
2306 /* Keep this test constant time.
2307 It is unlikely this can happen except for the case where speculative
2308 devirtualization introduced many speculative edges to this node.
2309 In this case the target is very likely alive anyway. */
2310 if (node->ref_list.referring.length () > 100)
2311 return true;
2313 /* We need references built. */
2314 if (symtab->state <= CONSTRUCTION)
2315 return true;
2317 for (i = 0; node->iterate_referring (i, ref); i++)
2318 if ((ref->use == IPA_REF_ALIAS
2319 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2320 || (ref->use == IPA_REF_ADDR
2321 && TREE_CODE (ref->referring->decl) == VAR_DECL
2322 && DECL_VIRTUAL_P (ref->referring->decl)))
2324 found = true;
2325 break;
2327 return found;
2330 /* Return if TARGET is cxa_pure_virtual. */
2332 static bool
2333 is_cxa_pure_virtual_p (tree target)
2335 return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
2336 && DECL_NAME (target)
2337 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (target)),
2338 "__cxa_pure_virtual");
2341 /* If TARGET has associated node, record it in the NODES array.
2342 CAN_REFER specify if program can refer to the target directly.
2343 if TARGET is unknown (NULL) or it can not be inserted (for example because
2344 its body was already removed and there is no way to refer to it), clear
2345 COMPLETEP. */
2347 static void
2348 maybe_record_node (vec <cgraph_node *> &nodes,
2349 tree target, hash_set<tree> *inserted,
2350 bool can_refer,
2351 bool *completep)
2353 struct cgraph_node *target_node, *alias_target;
2354 enum availability avail;
2355 bool pure_virtual = is_cxa_pure_virtual_p (target);
2357 /* __builtin_unreachable do not need to be added into
2358 list of targets; the runtime effect of calling them is undefined.
2359 Only "real" virtual methods should be accounted. */
2360 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
2361 return;
2363 if (!can_refer)
2365 /* The only case when method of anonymous namespace becomes unreferable
2366 is when we completely optimized it out. */
2367 if (flag_ltrans
2368 || !target
2369 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2370 *completep = false;
2371 return;
2374 if (!target)
2375 return;
2377 target_node = cgraph_node::get (target);
2379 /* Prefer alias target over aliases, so we do not get confused by
2380 fake duplicates. */
2381 if (target_node)
2383 alias_target = target_node->ultimate_alias_target (&avail);
2384 if (target_node != alias_target
2385 && avail >= AVAIL_AVAILABLE
2386 && target_node->get_availability ())
2387 target_node = alias_target;
2390 /* Method can only be called by polymorphic call if any
2391 of vtables referring to it are alive.
2393 While this holds for non-anonymous functions, too, there are
2394 cases where we want to keep them in the list; for example
2395 inline functions with -fno-weak are static, but we still
2396 may devirtualize them when instance comes from other unit.
2397 The same holds for LTO.
2399 Currently we ignore these functions in speculative devirtualization.
2400 ??? Maybe it would make sense to be more aggressive for LTO even
2401 elsewhere. */
2402 if (!flag_ltrans
2403 && !pure_virtual
2404 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2405 && (!target_node
2406 || !referenced_from_vtable_p (target_node)))
2408 /* See if TARGET is useful function we can deal with. */
2409 else if (target_node != NULL
2410 && (TREE_PUBLIC (target)
2411 || DECL_EXTERNAL (target)
2412 || target_node->definition)
2413 && target_node->real_symbol_p ())
2415 gcc_assert (!target_node->global.inlined_to);
2416 gcc_assert (target_node->real_symbol_p ());
2417 /* Only add pure virtual if it is the only possible target. This way
2418 we will preserve the diagnostics about pure virtual called in many
2419 cases without disabling optimization in other. */
2420 if (pure_virtual)
2422 if (nodes.length ())
2423 return;
2425 /* If we found a real target, take away cxa_pure_virtual. */
2426 else if (!pure_virtual && nodes.length () == 1
2427 && is_cxa_pure_virtual_p (nodes[0]->decl))
2428 nodes.pop ();
2429 if (pure_virtual && nodes.length ())
2430 return;
2431 if (!inserted->add (target))
2433 cached_polymorphic_call_targets->add (target_node);
2434 nodes.safe_push (target_node);
2437 else if (completep
2438 && (!type_in_anonymous_namespace_p
2439 (DECL_CONTEXT (target))
2440 || flag_ltrans))
2441 *completep = false;
2444 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2445 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2446 method in vtable and insert method to NODES array
2447 or BASES_TO_CONSIDER if this array is non-NULL.
2448 Otherwise recurse to base BINFOs.
2449 This matches what get_binfo_at_offset does, but with offset
2450 being unknown.
2452 TYPE_BINFOS is a stack of BINFOS of types with defined
2453 virtual table seen on way from class type to BINFO.
2455 MATCHED_VTABLES tracks virtual tables we already did lookup
2456 for virtual function in. INSERTED tracks nodes we already
2457 inserted.
2459 ANONYMOUS is true if BINFO is part of anonymous namespace.
2461 Clear COMPLETEP when we hit unreferable target.
2464 static void
2465 record_target_from_binfo (vec <cgraph_node *> &nodes,
2466 vec <tree> *bases_to_consider,
2467 tree binfo,
2468 tree otr_type,
2469 vec <tree> &type_binfos,
2470 HOST_WIDE_INT otr_token,
2471 tree outer_type,
2472 HOST_WIDE_INT offset,
2473 hash_set<tree> *inserted,
2474 hash_set<tree> *matched_vtables,
2475 bool anonymous,
2476 bool *completep)
2478 tree type = BINFO_TYPE (binfo);
2479 int i;
2480 tree base_binfo;
2483 if (BINFO_VTABLE (binfo))
2484 type_binfos.safe_push (binfo);
2485 if (types_same_for_odr (type, outer_type))
2487 int i;
2488 tree type_binfo = NULL;
2490 /* Look up BINFO with virtual table. For normal types it is always last
2491 binfo on stack. */
2492 for (i = type_binfos.length () - 1; i >= 0; i--)
2493 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2495 type_binfo = type_binfos[i];
2496 break;
2498 if (BINFO_VTABLE (binfo))
2499 type_binfos.pop ();
2500 /* If this is duplicated BINFO for base shared by virtual inheritance,
2501 we may not have its associated vtable. This is not a problem, since
2502 we will walk it on the other path. */
2503 if (!type_binfo)
2504 return;
2505 tree inner_binfo = get_binfo_at_offset (type_binfo,
2506 offset, otr_type);
2507 if (!inner_binfo)
2509 gcc_assert (odr_violation_reported);
2510 return;
2512 /* For types in anonymous namespace first check if the respective vtable
2513 is alive. If not, we know the type can't be called. */
2514 if (!flag_ltrans && anonymous)
2516 tree vtable = BINFO_VTABLE (inner_binfo);
2517 varpool_node *vnode;
2519 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2520 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2521 vnode = varpool_node::get (vtable);
2522 if (!vnode || !vnode->definition)
2523 return;
2525 gcc_assert (inner_binfo);
2526 if (bases_to_consider
2527 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2528 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2530 bool can_refer;
2531 tree target = gimple_get_virt_method_for_binfo (otr_token,
2532 inner_binfo,
2533 &can_refer);
2534 if (!bases_to_consider)
2535 maybe_record_node (nodes, target, inserted, can_refer, completep);
2536 /* Destructors are never called via construction vtables. */
2537 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2538 bases_to_consider->safe_push (target);
2540 return;
2543 /* Walk bases. */
2544 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2545 /* Walking bases that have no virtual method is pointless exercise. */
2546 if (polymorphic_type_binfo_p (base_binfo))
2547 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2548 type_binfos,
2549 otr_token, outer_type, offset, inserted,
2550 matched_vtables, anonymous, completep);
2551 if (BINFO_VTABLE (binfo))
2552 type_binfos.pop ();
2555 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2556 of TYPE, insert them to NODES, recurse into derived nodes.
2557 INSERTED is used to avoid duplicate insertions of methods into NODES.
2558 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2559 Clear COMPLETEP if unreferable target is found.
2561 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2562 all cases where BASE_SKIPPED is true (because the base is abstract
2563 class). */
2565 static void
2566 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2567 hash_set<tree> *inserted,
2568 hash_set<tree> *matched_vtables,
2569 tree otr_type,
2570 odr_type type,
2571 HOST_WIDE_INT otr_token,
2572 tree outer_type,
2573 HOST_WIDE_INT offset,
2574 bool *completep,
2575 vec <tree> &bases_to_consider,
2576 bool consider_construction)
2578 tree binfo = TYPE_BINFO (type->type);
2579 unsigned int i;
2580 auto_vec <tree, 8> type_binfos;
2581 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2583 /* We may need to consider types w/o instances because of possible derived
2584 types using their methods either directly or via construction vtables.
2585 We are safe to skip them when all derivations are known, since we will
2586 handle them later.
2587 This is done by recording them to BASES_TO_CONSIDER array. */
2588 if (possibly_instantiated || consider_construction)
2590 record_target_from_binfo (nodes,
2591 (!possibly_instantiated
2592 && type_all_derivations_known_p (type->type))
2593 ? &bases_to_consider : NULL,
2594 binfo, otr_type, type_binfos, otr_token,
2595 outer_type, offset,
2596 inserted, matched_vtables,
2597 type->anonymous_namespace, completep);
2599 for (i = 0; i < type->derived_types.length (); i++)
2600 possible_polymorphic_call_targets_1 (nodes, inserted,
2601 matched_vtables,
2602 otr_type,
2603 type->derived_types[i],
2604 otr_token, outer_type, offset, completep,
2605 bases_to_consider, consider_construction);
2608 /* Cache of queries for polymorphic call targets.
2610 Enumerating all call targets may get expensive when there are many
2611 polymorphic calls in the program, so we memoize all the previous
2612 queries and avoid duplicated work. */
2614 struct polymorphic_call_target_d
2616 HOST_WIDE_INT otr_token;
2617 ipa_polymorphic_call_context context;
2618 odr_type type;
2619 vec <cgraph_node *> targets;
2620 tree decl_warning;
2621 int type_warning;
2622 bool complete;
2623 bool speculative;
2626 /* Polymorphic call target cache helpers. */
2628 struct polymorphic_call_target_hasher
2629 : pointer_hash <polymorphic_call_target_d>
2631 static inline hashval_t hash (const polymorphic_call_target_d *);
2632 static inline bool equal (const polymorphic_call_target_d *,
2633 const polymorphic_call_target_d *);
2634 static inline void remove (polymorphic_call_target_d *);
2637 /* Return the computed hashcode for ODR_QUERY. */
2639 inline hashval_t
2640 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2642 inchash::hash hstate (odr_query->otr_token);
2644 hstate.add_wide_int (odr_query->type->id);
2645 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2646 hstate.add_wide_int (odr_query->context.offset);
2648 if (odr_query->context.speculative_outer_type)
2650 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2651 hstate.add_wide_int (odr_query->context.speculative_offset);
2653 hstate.add_flag (odr_query->speculative);
2654 hstate.add_flag (odr_query->context.maybe_in_construction);
2655 hstate.add_flag (odr_query->context.maybe_derived_type);
2656 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2657 hstate.commit_flag ();
2658 return hstate.end ();
2661 /* Compare cache entries T1 and T2. */
2663 inline bool
2664 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2665 const polymorphic_call_target_d *t2)
2667 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2668 && t1->speculative == t2->speculative
2669 && t1->context.offset == t2->context.offset
2670 && t1->context.speculative_offset == t2->context.speculative_offset
2671 && t1->context.outer_type == t2->context.outer_type
2672 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2673 && t1->context.maybe_in_construction
2674 == t2->context.maybe_in_construction
2675 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2676 && (t1->context.speculative_maybe_derived_type
2677 == t2->context.speculative_maybe_derived_type));
2680 /* Remove entry in polymorphic call target cache hash. */
2682 inline void
2683 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2685 v->targets.release ();
2686 free (v);
2689 /* Polymorphic call target query cache. */
2691 typedef hash_table<polymorphic_call_target_hasher>
2692 polymorphic_call_target_hash_type;
2693 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2695 /* Destroy polymorphic call target query cache. */
2697 static void
2698 free_polymorphic_call_targets_hash ()
2700 if (cached_polymorphic_call_targets)
2702 delete polymorphic_call_target_hash;
2703 polymorphic_call_target_hash = NULL;
2704 delete cached_polymorphic_call_targets;
2705 cached_polymorphic_call_targets = NULL;
2709 /* When virtual function is removed, we may need to flush the cache. */
2711 static void
2712 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2714 if (cached_polymorphic_call_targets
2715 && cached_polymorphic_call_targets->contains (n))
2716 free_polymorphic_call_targets_hash ();
2719 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2721 tree
2722 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2723 tree vtable)
2725 tree v = BINFO_VTABLE (binfo);
2726 int i;
2727 tree base_binfo;
2728 unsigned HOST_WIDE_INT this_offset;
2730 if (v)
2732 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2733 gcc_unreachable ();
2735 if (offset == this_offset
2736 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2737 return binfo;
2740 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2741 if (polymorphic_type_binfo_p (base_binfo))
2743 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2744 if (base_binfo)
2745 return base_binfo;
2747 return NULL;
2750 /* T is known constant value of virtual table pointer.
2751 Store virtual table to V and its offset to OFFSET.
2752 Return false if T does not look like virtual table reference. */
2754 bool
2755 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2756 unsigned HOST_WIDE_INT *offset)
2758 /* We expect &MEM[(void *)&virtual_table + 16B].
2759 We obtain object's BINFO from the context of the virtual table.
2760 This one contains pointer to virtual table represented via
2761 POINTER_PLUS_EXPR. Verify that this pointer matches what
2762 we propagated through.
2764 In the case of virtual inheritance, the virtual tables may
2765 be nested, i.e. the offset may be different from 16 and we may
2766 need to dive into the type representation. */
2767 if (TREE_CODE (t) == ADDR_EXPR
2768 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2769 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2770 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2771 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2772 == VAR_DECL)
2773 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2774 (TREE_OPERAND (t, 0), 0), 0)))
2776 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2777 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2778 return true;
2781 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2782 We need to handle it when T comes from static variable initializer or
2783 BINFO. */
2784 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2786 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2787 t = TREE_OPERAND (t, 0);
2789 else
2790 *offset = 0;
2792 if (TREE_CODE (t) != ADDR_EXPR)
2793 return false;
2794 *v = TREE_OPERAND (t, 0);
2795 return true;
2798 /* T is known constant value of virtual table pointer. Return BINFO of the
2799 instance type. */
2801 tree
2802 vtable_pointer_value_to_binfo (const_tree t)
2804 tree vtable;
2805 unsigned HOST_WIDE_INT offset;
2807 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2808 return NULL_TREE;
2810 /* FIXME: for stores of construction vtables we return NULL,
2811 because we do not have BINFO for those. Eventually we should fix
2812 our representation to allow this case to be handled, too.
2813 In the case we see store of BINFO we however may assume
2814 that standard folding will be able to cope with it. */
2815 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2816 offset, vtable);
2819 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2820 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2821 and insert them in NODES.
2823 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2825 static void
2826 record_targets_from_bases (tree otr_type,
2827 HOST_WIDE_INT otr_token,
2828 tree outer_type,
2829 HOST_WIDE_INT offset,
2830 vec <cgraph_node *> &nodes,
2831 hash_set<tree> *inserted,
2832 hash_set<tree> *matched_vtables,
2833 bool *completep)
2835 while (true)
2837 HOST_WIDE_INT pos, size;
2838 tree base_binfo;
2839 tree fld;
2841 if (types_same_for_odr (outer_type, otr_type))
2842 return;
2844 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2846 if (TREE_CODE (fld) != FIELD_DECL)
2847 continue;
2849 pos = int_bit_position (fld);
2850 size = tree_to_shwi (DECL_SIZE (fld));
2851 if (pos <= offset && (pos + size) > offset
2852 /* Do not get confused by zero sized bases. */
2853 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2854 break;
2856 /* Within a class type we should always find corresponding fields. */
2857 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2859 /* Nonbase types should have been stripped by outer_class_type. */
2860 gcc_assert (DECL_ARTIFICIAL (fld));
2862 outer_type = TREE_TYPE (fld);
2863 offset -= pos;
2865 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2866 offset, otr_type);
2867 if (!base_binfo)
2869 gcc_assert (odr_violation_reported);
2870 return;
2872 gcc_assert (base_binfo);
2873 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2875 bool can_refer;
2876 tree target = gimple_get_virt_method_for_binfo (otr_token,
2877 base_binfo,
2878 &can_refer);
2879 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2880 maybe_record_node (nodes, target, inserted, can_refer, completep);
2881 matched_vtables->add (BINFO_VTABLE (base_binfo));
2886 /* When virtual table is removed, we may need to flush the cache. */
2888 static void
2889 devirt_variable_node_removal_hook (varpool_node *n,
2890 void *d ATTRIBUTE_UNUSED)
2892 if (cached_polymorphic_call_targets
2893 && DECL_VIRTUAL_P (n->decl)
2894 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2895 free_polymorphic_call_targets_hash ();
2898 /* Record about how many calls would benefit from given type to be final. */
2900 struct odr_type_warn_count
2902 tree type;
2903 int count;
2904 gcov_type dyn_count;
2907 /* Record about how many calls would benefit from given method to be final. */
2909 struct decl_warn_count
2911 tree decl;
2912 int count;
2913 gcov_type dyn_count;
2916 /* Information about type and decl warnings. */
2918 struct final_warning_record
2920 gcov_type dyn_count;
2921 auto_vec<odr_type_warn_count> type_warnings;
2922 hash_map<tree, decl_warn_count> decl_warnings;
2924 struct final_warning_record *final_warning_records;
2926 /* Return vector containing possible targets of polymorphic call of type
2927 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2928 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2929 OTR_TYPE and include their virtual method. This is useful for types
2930 possibly in construction or destruction where the virtual table may
2931 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2932 us to walk the inheritance graph for all derivations.
2934 If COMPLETEP is non-NULL, store true if the list is complete.
2935 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2936 in the target cache. If user needs to visit every target list
2937 just once, it can memoize them.
2939 If SPECULATIVE is set, the list will not contain targets that
2940 are not speculatively taken.
2942 Returned vector is placed into cache. It is NOT caller's responsibility
2943 to free it. The vector can be freed on cgraph_remove_node call if
2944 the particular node is a virtual function present in the cache. */
2946 vec <cgraph_node *>
2947 possible_polymorphic_call_targets (tree otr_type,
2948 HOST_WIDE_INT otr_token,
2949 ipa_polymorphic_call_context context,
2950 bool *completep,
2951 void **cache_token,
2952 bool speculative)
2954 static struct cgraph_node_hook_list *node_removal_hook_holder;
2955 vec <cgraph_node *> nodes = vNULL;
2956 auto_vec <tree, 8> bases_to_consider;
2957 odr_type type, outer_type;
2958 polymorphic_call_target_d key;
2959 polymorphic_call_target_d **slot;
2960 unsigned int i;
2961 tree binfo, target;
2962 bool complete;
2963 bool can_refer = false;
2964 bool skipped = false;
2966 otr_type = TYPE_MAIN_VARIANT (otr_type);
2968 /* If ODR is not initialized or the context is invalid, return empty
2969 incomplete list. */
2970 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
2972 if (completep)
2973 *completep = context.invalid;
2974 if (cache_token)
2975 *cache_token = NULL;
2976 return nodes;
2979 /* Do not bother to compute speculative info when user do not asks for it. */
2980 if (!speculative || !context.speculative_outer_type)
2981 context.clear_speculation ();
2983 type = get_odr_type (otr_type, true);
2985 /* Recording type variants would waste results cache. */
2986 gcc_assert (!context.outer_type
2987 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2989 /* Look up the outer class type we want to walk.
2990 If we fail to do so, the context is invalid. */
2991 if ((context.outer_type || context.speculative_outer_type)
2992 && !context.restrict_to_inner_class (otr_type))
2994 if (completep)
2995 *completep = true;
2996 if (cache_token)
2997 *cache_token = NULL;
2998 return nodes;
3000 gcc_assert (!context.invalid);
3002 /* Check that restrict_to_inner_class kept the main variant. */
3003 gcc_assert (!context.outer_type
3004 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3006 /* We canonicalize our query, so we do not need extra hashtable entries. */
3008 /* Without outer type, we have no use for offset. Just do the
3009 basic search from inner type. */
3010 if (!context.outer_type)
3011 context.clear_outer_type (otr_type);
3012 /* We need to update our hierarchy if the type does not exist. */
3013 outer_type = get_odr_type (context.outer_type, true);
3014 /* If the type is complete, there are no derivations. */
3015 if (TYPE_FINAL_P (outer_type->type))
3016 context.maybe_derived_type = false;
3018 /* Initialize query cache. */
3019 if (!cached_polymorphic_call_targets)
3021 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3022 polymorphic_call_target_hash
3023 = new polymorphic_call_target_hash_type (23);
3024 if (!node_removal_hook_holder)
3026 node_removal_hook_holder =
3027 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3028 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3029 NULL);
3033 if (in_lto_p)
3035 if (context.outer_type != otr_type)
3036 context.outer_type
3037 = get_odr_type (context.outer_type, true)->type;
3038 if (context.speculative_outer_type)
3039 context.speculative_outer_type
3040 = get_odr_type (context.speculative_outer_type, true)->type;
3043 /* Look up cached answer. */
3044 key.type = type;
3045 key.otr_token = otr_token;
3046 key.speculative = speculative;
3047 key.context = context;
3048 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3049 if (cache_token)
3050 *cache_token = (void *)*slot;
3051 if (*slot)
3053 if (completep)
3054 *completep = (*slot)->complete;
3055 if ((*slot)->type_warning && final_warning_records)
3057 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3058 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3059 += final_warning_records->dyn_count;
3061 if (!speculative && (*slot)->decl_warning && final_warning_records)
3063 struct decl_warn_count *c =
3064 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3065 c->count++;
3066 c->dyn_count += final_warning_records->dyn_count;
3068 return (*slot)->targets;
3071 complete = true;
3073 /* Do actual search. */
3074 timevar_push (TV_IPA_VIRTUAL_CALL);
3075 *slot = XCNEW (polymorphic_call_target_d);
3076 if (cache_token)
3077 *cache_token = (void *)*slot;
3078 (*slot)->type = type;
3079 (*slot)->otr_token = otr_token;
3080 (*slot)->context = context;
3081 (*slot)->speculative = speculative;
3083 hash_set<tree> inserted;
3084 hash_set<tree> matched_vtables;
3086 /* First insert targets we speculatively identified as likely. */
3087 if (context.speculative_outer_type)
3089 odr_type speculative_outer_type;
3090 bool speculation_complete = true;
3092 /* First insert target from type itself and check if it may have
3093 derived types. */
3094 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3095 if (TYPE_FINAL_P (speculative_outer_type->type))
3096 context.speculative_maybe_derived_type = false;
3097 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3098 context.speculative_offset, otr_type);
3099 if (binfo)
3100 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3101 &can_refer);
3102 else
3103 target = NULL;
3105 /* In the case we get complete method, we don't need
3106 to walk derivations. */
3107 if (target && DECL_FINAL_P (target))
3108 context.speculative_maybe_derived_type = false;
3109 if (type_possibly_instantiated_p (speculative_outer_type->type))
3110 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3111 if (binfo)
3112 matched_vtables.add (BINFO_VTABLE (binfo));
3115 /* Next walk recursively all derived types. */
3116 if (context.speculative_maybe_derived_type)
3117 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3118 possible_polymorphic_call_targets_1 (nodes, &inserted,
3119 &matched_vtables,
3120 otr_type,
3121 speculative_outer_type->derived_types[i],
3122 otr_token, speculative_outer_type->type,
3123 context.speculative_offset,
3124 &speculation_complete,
3125 bases_to_consider,
3126 false);
3129 if (!speculative || !nodes.length ())
3131 /* First see virtual method of type itself. */
3132 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3133 context.offset, otr_type);
3134 if (binfo)
3135 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3136 &can_refer);
3137 else
3139 gcc_assert (odr_violation_reported);
3140 target = NULL;
3143 /* Destructors are never called through construction virtual tables,
3144 because the type is always known. */
3145 if (target && DECL_CXX_DESTRUCTOR_P (target))
3146 context.maybe_in_construction = false;
3148 if (target)
3150 /* In the case we get complete method, we don't need
3151 to walk derivations. */
3152 if (DECL_FINAL_P (target))
3153 context.maybe_derived_type = false;
3156 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3157 if (type_possibly_instantiated_p (outer_type->type))
3158 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3159 else
3160 skipped = true;
3162 if (binfo)
3163 matched_vtables.add (BINFO_VTABLE (binfo));
3165 /* Next walk recursively all derived types. */
3166 if (context.maybe_derived_type)
3168 for (i = 0; i < outer_type->derived_types.length(); i++)
3169 possible_polymorphic_call_targets_1 (nodes, &inserted,
3170 &matched_vtables,
3171 otr_type,
3172 outer_type->derived_types[i],
3173 otr_token, outer_type->type,
3174 context.offset, &complete,
3175 bases_to_consider,
3176 context.maybe_in_construction);
3178 if (!outer_type->all_derivations_known)
3180 if (!speculative && final_warning_records)
3182 if (complete
3183 && nodes.length () == 1
3184 && warn_suggest_final_types
3185 && !outer_type->derived_types.length ())
3187 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3188 final_warning_records->type_warnings.safe_grow_cleared
3189 (odr_types.length ());
3190 final_warning_records->type_warnings[outer_type->id].count++;
3191 final_warning_records->type_warnings[outer_type->id].dyn_count
3192 += final_warning_records->dyn_count;
3193 final_warning_records->type_warnings[outer_type->id].type
3194 = outer_type->type;
3195 (*slot)->type_warning = outer_type->id + 1;
3197 if (complete
3198 && warn_suggest_final_methods
3199 && nodes.length () == 1
3200 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3201 outer_type->type))
3203 bool existed;
3204 struct decl_warn_count &c =
3205 final_warning_records->decl_warnings.get_or_insert
3206 (nodes[0]->decl, &existed);
3208 if (existed)
3210 c.count++;
3211 c.dyn_count += final_warning_records->dyn_count;
3213 else
3215 c.count = 1;
3216 c.dyn_count = final_warning_records->dyn_count;
3217 c.decl = nodes[0]->decl;
3219 (*slot)->decl_warning = nodes[0]->decl;
3222 complete = false;
3226 if (!speculative)
3228 /* Destructors are never called through construction virtual tables,
3229 because the type is always known. One of entries may be
3230 cxa_pure_virtual so look to at least two of them. */
3231 if (context.maybe_in_construction)
3232 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3233 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3234 context.maybe_in_construction = false;
3235 if (context.maybe_in_construction)
3237 if (type != outer_type
3238 && (!skipped
3239 || (context.maybe_derived_type
3240 && !type_all_derivations_known_p (outer_type->type))))
3241 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3242 context.offset, nodes, &inserted,
3243 &matched_vtables, &complete);
3244 if (skipped)
3245 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3246 for (i = 0; i < bases_to_consider.length(); i++)
3247 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3252 (*slot)->targets = nodes;
3253 (*slot)->complete = complete;
3254 if (completep)
3255 *completep = complete;
3257 timevar_pop (TV_IPA_VIRTUAL_CALL);
3258 return nodes;
3261 bool
3262 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3263 vec<const decl_warn_count*> *vec)
3265 vec->safe_push (&value);
3266 return true;
3269 /* Dump target list TARGETS into FILE. */
3271 static void
3272 dump_targets (FILE *f, vec <cgraph_node *> targets)
3274 unsigned int i;
3276 for (i = 0; i < targets.length (); i++)
3278 char *name = NULL;
3279 if (in_lto_p)
3280 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3281 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3282 if (in_lto_p)
3283 free (name);
3284 if (!targets[i]->definition)
3285 fprintf (f, " (no definition%s)",
3286 DECL_DECLARED_INLINE_P (targets[i]->decl)
3287 ? " inline" : "");
3289 fprintf (f, "\n");
3292 /* Dump all possible targets of a polymorphic call. */
3294 void
3295 dump_possible_polymorphic_call_targets (FILE *f,
3296 tree otr_type,
3297 HOST_WIDE_INT otr_token,
3298 const ipa_polymorphic_call_context &ctx)
3300 vec <cgraph_node *> targets;
3301 bool final;
3302 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3303 unsigned int len;
3305 if (!type)
3306 return;
3307 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3308 ctx,
3309 &final, NULL, false);
3310 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3311 print_generic_expr (f, type->type, TDF_SLIM);
3312 fprintf (f, " token %i\n", (int)otr_token);
3314 ctx.dump (f);
3316 fprintf (f, " %s%s%s%s\n ",
3317 final ? "This is a complete list." :
3318 "This is partial list; extra targets may be defined in other units.",
3319 ctx.maybe_in_construction ? " (base types included)" : "",
3320 ctx.maybe_derived_type ? " (derived types included)" : "",
3321 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3322 len = targets.length ();
3323 dump_targets (f, targets);
3325 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3326 ctx,
3327 &final, NULL, true);
3328 if (targets.length () != len)
3330 fprintf (f, " Speculative targets:");
3331 dump_targets (f, targets);
3333 gcc_assert (targets.length () <= len);
3334 fprintf (f, "\n");
3338 /* Return true if N can be possibly target of a polymorphic call of
3339 OTR_TYPE/OTR_TOKEN. */
3341 bool
3342 possible_polymorphic_call_target_p (tree otr_type,
3343 HOST_WIDE_INT otr_token,
3344 const ipa_polymorphic_call_context &ctx,
3345 struct cgraph_node *n)
3347 vec <cgraph_node *> targets;
3348 unsigned int i;
3349 enum built_in_function fcode;
3350 bool final;
3352 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3353 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3354 == BUILT_IN_UNREACHABLE
3355 || fcode == BUILT_IN_TRAP))
3356 return true;
3358 if (is_cxa_pure_virtual_p (n->decl))
3359 return true;
3361 if (!odr_hash)
3362 return true;
3363 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3364 for (i = 0; i < targets.length (); i++)
3365 if (n->semantically_equivalent_p (targets[i]))
3366 return true;
3368 /* At a moment we allow middle end to dig out new external declarations
3369 as a targets of polymorphic calls. */
3370 if (!final && !n->definition)
3371 return true;
3372 return false;
3377 /* Return true if N can be possibly target of a polymorphic call of
3378 OBJ_TYPE_REF expression REF in STMT. */
3380 bool
3381 possible_polymorphic_call_target_p (tree ref,
3382 gimple *stmt,
3383 struct cgraph_node *n)
3385 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3386 tree call_fn = gimple_call_fn (stmt);
3388 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3389 tree_to_uhwi
3390 (OBJ_TYPE_REF_TOKEN (call_fn)),
3391 context,
3396 /* After callgraph construction new external nodes may appear.
3397 Add them into the graph. */
3399 void
3400 update_type_inheritance_graph (void)
3402 struct cgraph_node *n;
3404 if (!odr_hash)
3405 return;
3406 free_polymorphic_call_targets_hash ();
3407 timevar_push (TV_IPA_INHERITANCE);
3408 /* We reconstruct the graph starting from types of all methods seen in the
3409 unit. */
3410 FOR_EACH_FUNCTION (n)
3411 if (DECL_VIRTUAL_P (n->decl)
3412 && !n->definition
3413 && n->real_symbol_p ())
3414 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3415 timevar_pop (TV_IPA_INHERITANCE);
3419 /* Return true if N looks like likely target of a polymorphic call.
3420 Rule out cxa_pure_virtual, noreturns, function declared cold and
3421 other obvious cases. */
3423 bool
3424 likely_target_p (struct cgraph_node *n)
3426 int flags;
3427 /* cxa_pure_virtual and similar things are not likely. */
3428 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3429 return false;
3430 flags = flags_from_decl_or_type (n->decl);
3431 if (flags & ECF_NORETURN)
3432 return false;
3433 if (lookup_attribute ("cold",
3434 DECL_ATTRIBUTES (n->decl)))
3435 return false;
3436 if (n->frequency < NODE_FREQUENCY_NORMAL)
3437 return false;
3438 /* If there are no live virtual tables referring the target,
3439 the only way the target can be called is an instance coming from other
3440 compilation unit; speculative devirtualization is built around an
3441 assumption that won't happen. */
3442 if (!referenced_from_vtable_p (n))
3443 return false;
3444 return true;
3447 /* Compare type warning records P1 and P2 and choose one with larger count;
3448 helper for qsort. */
3451 type_warning_cmp (const void *p1, const void *p2)
3453 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3454 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3456 if (t1->dyn_count < t2->dyn_count)
3457 return 1;
3458 if (t1->dyn_count > t2->dyn_count)
3459 return -1;
3460 return t2->count - t1->count;
3463 /* Compare decl warning records P1 and P2 and choose one with larger count;
3464 helper for qsort. */
3467 decl_warning_cmp (const void *p1, const void *p2)
3469 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3470 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3472 if (t1->dyn_count < t2->dyn_count)
3473 return 1;
3474 if (t1->dyn_count > t2->dyn_count)
3475 return -1;
3476 return t2->count - t1->count;
3480 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3481 context CTX. */
3483 struct cgraph_node *
3484 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3485 ipa_polymorphic_call_context ctx)
3487 vec <cgraph_node *>targets
3488 = possible_polymorphic_call_targets
3489 (otr_type, otr_token, ctx, NULL, NULL, true);
3490 unsigned int i;
3491 struct cgraph_node *likely_target = NULL;
3493 for (i = 0; i < targets.length (); i++)
3494 if (likely_target_p (targets[i]))
3496 if (likely_target)
3497 return NULL;
3498 likely_target = targets[i];
3500 if (!likely_target
3501 ||!likely_target->definition
3502 || DECL_EXTERNAL (likely_target->decl))
3503 return NULL;
3505 /* Don't use an implicitly-declared destructor (c++/58678). */
3506 struct cgraph_node *non_thunk_target
3507 = likely_target->function_symbol ();
3508 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3509 return NULL;
3510 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3511 && likely_target->can_be_discarded_p ())
3512 return NULL;
3513 return likely_target;
3516 /* The ipa-devirt pass.
3517 When polymorphic call has only one likely target in the unit,
3518 turn it into a speculative call. */
3520 static unsigned int
3521 ipa_devirt (void)
3523 struct cgraph_node *n;
3524 hash_set<void *> bad_call_targets;
3525 struct cgraph_edge *e;
3527 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3528 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3529 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3530 int ndropped = 0;
3532 if (!odr_types_ptr)
3533 return 0;
3535 if (dump_file)
3536 dump_type_inheritance_graph (dump_file);
3538 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3539 This is implemented by setting up final_warning_records that are updated
3540 by get_polymorphic_call_targets.
3541 We need to clear cache in this case to trigger recomputation of all
3542 entries. */
3543 if (warn_suggest_final_methods || warn_suggest_final_types)
3545 final_warning_records = new (final_warning_record);
3546 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3547 free_polymorphic_call_targets_hash ();
3550 FOR_EACH_DEFINED_FUNCTION (n)
3552 bool update = false;
3553 if (!opt_for_fn (n->decl, flag_devirtualize))
3554 continue;
3555 if (dump_file && n->indirect_calls)
3556 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3557 n->name (), n->order);
3558 for (e = n->indirect_calls; e; e = e->next_callee)
3559 if (e->indirect_info->polymorphic)
3561 struct cgraph_node *likely_target = NULL;
3562 void *cache_token;
3563 bool final;
3565 if (final_warning_records)
3566 final_warning_records->dyn_count = e->count;
3568 vec <cgraph_node *>targets
3569 = possible_polymorphic_call_targets
3570 (e, &final, &cache_token, true);
3571 unsigned int i;
3573 /* Trigger warnings by calculating non-speculative targets. */
3574 if (warn_suggest_final_methods || warn_suggest_final_types)
3575 possible_polymorphic_call_targets (e);
3577 if (dump_file)
3578 dump_possible_polymorphic_call_targets
3579 (dump_file, e);
3581 npolymorphic++;
3583 /* See if the call can be devirtualized by means of ipa-prop's
3584 polymorphic call context propagation. If not, we can just
3585 forget about this call being polymorphic and avoid some heavy
3586 lifting in remove_unreachable_nodes that will otherwise try to
3587 keep all possible targets alive until inlining and in the inliner
3588 itself.
3590 This may need to be revisited once we add further ways to use
3591 the may edges, but it is a resonable thing to do right now. */
3593 if ((e->indirect_info->param_index == -1
3594 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3595 && e->indirect_info->vptr_changed))
3596 && !flag_ltrans_devirtualize)
3598 e->indirect_info->polymorphic = false;
3599 ndropped++;
3600 if (dump_file)
3601 fprintf (dump_file, "Dropping polymorphic call info;"
3602 " it can not be used by ipa-prop\n");
3605 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3606 continue;
3608 if (!e->maybe_hot_p ())
3610 if (dump_file)
3611 fprintf (dump_file, "Call is cold\n\n");
3612 ncold++;
3613 continue;
3615 if (e->speculative)
3617 if (dump_file)
3618 fprintf (dump_file, "Call is already speculated\n\n");
3619 nspeculated++;
3621 /* When dumping see if we agree with speculation. */
3622 if (!dump_file)
3623 continue;
3625 if (bad_call_targets.contains (cache_token))
3627 if (dump_file)
3628 fprintf (dump_file, "Target list is known to be useless\n\n");
3629 nmultiple++;
3630 continue;
3632 for (i = 0; i < targets.length (); i++)
3633 if (likely_target_p (targets[i]))
3635 if (likely_target)
3637 likely_target = NULL;
3638 if (dump_file)
3639 fprintf (dump_file, "More than one likely target\n\n");
3640 nmultiple++;
3641 break;
3643 likely_target = targets[i];
3645 if (!likely_target)
3647 bad_call_targets.add (cache_token);
3648 continue;
3650 /* This is reached only when dumping; check if we agree or disagree
3651 with the speculation. */
3652 if (e->speculative)
3654 struct cgraph_edge *e2;
3655 struct ipa_ref *ref;
3656 e->speculative_call_info (e2, e, ref);
3657 if (e2->callee->ultimate_alias_target ()
3658 == likely_target->ultimate_alias_target ())
3660 fprintf (dump_file, "We agree with speculation\n\n");
3661 nok++;
3663 else
3665 fprintf (dump_file, "We disagree with speculation\n\n");
3666 nwrong++;
3668 continue;
3670 if (!likely_target->definition)
3672 if (dump_file)
3673 fprintf (dump_file, "Target is not a definition\n\n");
3674 nnotdefined++;
3675 continue;
3677 /* Do not introduce new references to external symbols. While we
3678 can handle these just well, it is common for programs to
3679 incorrectly with headers defining methods they are linked
3680 with. */
3681 if (DECL_EXTERNAL (likely_target->decl))
3683 if (dump_file)
3684 fprintf (dump_file, "Target is external\n\n");
3685 nexternal++;
3686 continue;
3688 /* Don't use an implicitly-declared destructor (c++/58678). */
3689 struct cgraph_node *non_thunk_target
3690 = likely_target->function_symbol ();
3691 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3693 if (dump_file)
3694 fprintf (dump_file, "Target is artificial\n\n");
3695 nartificial++;
3696 continue;
3698 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3699 && likely_target->can_be_discarded_p ())
3701 if (dump_file)
3702 fprintf (dump_file, "Target is overwritable\n\n");
3703 noverwritable++;
3704 continue;
3706 else if (dbg_cnt (devirt))
3708 if (dump_enabled_p ())
3710 location_t locus = gimple_location_safe (e->call_stmt);
3711 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3712 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3713 n->name (), n->order,
3714 likely_target->name (),
3715 likely_target->order);
3717 if (!likely_target->can_be_discarded_p ())
3719 cgraph_node *alias;
3720 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3721 if (alias)
3722 likely_target = alias;
3724 nconverted++;
3725 update = true;
3726 e->make_speculative
3727 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3730 if (update)
3731 inline_update_overall_summary (n);
3733 if (warn_suggest_final_methods || warn_suggest_final_types)
3735 if (warn_suggest_final_types)
3737 final_warning_records->type_warnings.qsort (type_warning_cmp);
3738 for (unsigned int i = 0;
3739 i < final_warning_records->type_warnings.length (); i++)
3740 if (final_warning_records->type_warnings[i].count)
3742 tree type = final_warning_records->type_warnings[i].type;
3743 int count = final_warning_records->type_warnings[i].count;
3744 long long dyn_count
3745 = final_warning_records->type_warnings[i].dyn_count;
3747 if (!dyn_count)
3748 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3749 OPT_Wsuggest_final_types, count,
3750 "Declaring type %qD final "
3751 "would enable devirtualization of %i call",
3752 "Declaring type %qD final "
3753 "would enable devirtualization of %i calls",
3754 type,
3755 count);
3756 else
3757 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3758 OPT_Wsuggest_final_types, count,
3759 "Declaring type %qD final "
3760 "would enable devirtualization of %i call "
3761 "executed %lli times",
3762 "Declaring type %qD final "
3763 "would enable devirtualization of %i calls "
3764 "executed %lli times",
3765 type,
3766 count,
3767 dyn_count);
3771 if (warn_suggest_final_methods)
3773 auto_vec<const decl_warn_count*> decl_warnings_vec;
3775 final_warning_records->decl_warnings.traverse
3776 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3777 decl_warnings_vec.qsort (decl_warning_cmp);
3778 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3780 tree decl = decl_warnings_vec[i]->decl;
3781 int count = decl_warnings_vec[i]->count;
3782 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3784 if (!dyn_count)
3785 if (DECL_CXX_DESTRUCTOR_P (decl))
3786 warning_n (DECL_SOURCE_LOCATION (decl),
3787 OPT_Wsuggest_final_methods, count,
3788 "Declaring virtual destructor of %qD final "
3789 "would enable devirtualization of %i call",
3790 "Declaring virtual destructor of %qD final "
3791 "would enable devirtualization of %i calls",
3792 DECL_CONTEXT (decl), count);
3793 else
3794 warning_n (DECL_SOURCE_LOCATION (decl),
3795 OPT_Wsuggest_final_methods, count,
3796 "Declaring method %qD final "
3797 "would enable devirtualization of %i call",
3798 "Declaring method %qD final "
3799 "would enable devirtualization of %i calls",
3800 decl, count);
3801 else if (DECL_CXX_DESTRUCTOR_P (decl))
3802 warning_n (DECL_SOURCE_LOCATION (decl),
3803 OPT_Wsuggest_final_methods, count,
3804 "Declaring virtual destructor of %qD final "
3805 "would enable devirtualization of %i call "
3806 "executed %lli times",
3807 "Declaring virtual destructor of %qD final "
3808 "would enable devirtualization of %i calls "
3809 "executed %lli times",
3810 DECL_CONTEXT (decl), count, dyn_count);
3811 else
3812 warning_n (DECL_SOURCE_LOCATION (decl),
3813 OPT_Wsuggest_final_methods, count,
3814 "Declaring method %qD final "
3815 "would enable devirtualization of %i call "
3816 "executed %lli times",
3817 "Declaring method %qD final "
3818 "would enable devirtualization of %i calls "
3819 "executed %lli times",
3820 decl, count, dyn_count);
3824 delete (final_warning_records);
3825 final_warning_records = 0;
3828 if (dump_file)
3829 fprintf (dump_file,
3830 "%i polymorphic calls, %i devirtualized,"
3831 " %i speculatively devirtualized, %i cold\n"
3832 "%i have multiple targets, %i overwritable,"
3833 " %i already speculated (%i agree, %i disagree),"
3834 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3835 npolymorphic, ndevirtualized, nconverted, ncold,
3836 nmultiple, noverwritable, nspeculated, nok, nwrong,
3837 nexternal, nnotdefined, nartificial, ndropped);
3838 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3841 namespace {
3843 const pass_data pass_data_ipa_devirt =
3845 IPA_PASS, /* type */
3846 "devirt", /* name */
3847 OPTGROUP_NONE, /* optinfo_flags */
3848 TV_IPA_DEVIRT, /* tv_id */
3849 0, /* properties_required */
3850 0, /* properties_provided */
3851 0, /* properties_destroyed */
3852 0, /* todo_flags_start */
3853 ( TODO_dump_symtab ), /* todo_flags_finish */
3856 class pass_ipa_devirt : public ipa_opt_pass_d
3858 public:
3859 pass_ipa_devirt (gcc::context *ctxt)
3860 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3861 NULL, /* generate_summary */
3862 NULL, /* write_summary */
3863 NULL, /* read_summary */
3864 NULL, /* write_optimization_summary */
3865 NULL, /* read_optimization_summary */
3866 NULL, /* stmt_fixup */
3867 0, /* function_transform_todo_flags_start */
3868 NULL, /* function_transform */
3869 NULL) /* variable_transform */
3872 /* opt_pass methods: */
3873 virtual bool gate (function *)
3875 /* In LTO, always run the IPA passes and decide on function basis if the
3876 pass is enabled. */
3877 if (in_lto_p)
3878 return true;
3879 return (flag_devirtualize
3880 && (flag_devirtualize_speculatively
3881 || (warn_suggest_final_methods
3882 || warn_suggest_final_types))
3883 && optimize);
3886 virtual unsigned int execute (function *) { return ipa_devirt (); }
3888 }; // class pass_ipa_devirt
3890 } // anon namespace
3892 ipa_opt_pass_d *
3893 make_pass_ipa_devirt (gcc::context *ctxt)
3895 return new pass_ipa_devirt (ctxt);
3898 #include "gt-ipa-devirt.h"