/cp
[official-gcc.git] / gcc / ipa-devirt.c
blobbdda7d6b4a97ef314bfb6ce6a19a602c121a8169
1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2018 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 "tree-vrp.h"
126 #include "ipa-prop.h"
127 #include "ipa-fnsummary.h"
128 #include "demangle.h"
129 #include "dbgcnt.h"
130 #include "gimple-pretty-print.h"
131 #include "intl.h"
132 #include "stringpool.h"
133 #include "attribs.h"
135 /* Hash based set of pairs of types. */
136 struct type_pair
138 tree first;
139 tree second;
142 template <>
143 struct default_hash_traits <type_pair>
144 : typed_noop_remove <type_pair>
146 GTY((skip)) typedef type_pair value_type;
147 GTY((skip)) typedef type_pair compare_type;
148 static hashval_t
149 hash (type_pair p)
151 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
153 static bool
154 is_empty (type_pair p)
156 return p.first == NULL;
158 static bool
159 is_deleted (type_pair p ATTRIBUTE_UNUSED)
161 return false;
163 static bool
164 equal (const type_pair &a, const type_pair &b)
166 return a.first==b.first && a.second == b.second;
168 static void
169 mark_empty (type_pair &e)
171 e.first = NULL;
175 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
176 hash_set<type_pair> *,
177 location_t, location_t);
179 static bool odr_violation_reported = false;
182 /* Pointer set of all call targets appearing in the cache. */
183 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
185 /* The node of type inheritance graph. For each type unique in
186 One Definition Rule (ODR) sense, we produce one node linking all
187 main variants of types equivalent to it, bases and derived types. */
189 struct GTY(()) odr_type_d
191 /* leader type. */
192 tree type;
193 /* All bases; built only for main variants of types. */
194 vec<odr_type> GTY((skip)) bases;
195 /* All derived types with virtual methods seen in unit;
196 built only for main variants of types. */
197 vec<odr_type> GTY((skip)) derived_types;
199 /* All equivalent types, if more than one. */
200 vec<tree, va_gc> *types;
201 /* Set of all equivalent types, if NON-NULL. */
202 hash_set<tree> * GTY((skip)) types_set;
204 /* Unique ID indexing the type in odr_types array. */
205 int id;
206 /* Is it in anonymous namespace? */
207 bool anonymous_namespace;
208 /* Do we know about all derivations of given type? */
209 bool all_derivations_known;
210 /* Did we report ODR violation here? */
211 bool odr_violated;
212 /* Set when virtual table without RTTI previaled table with. */
213 bool rtti_broken;
216 /* Return TRUE if all derived types of T are known and thus
217 we may consider the walk of derived type complete.
219 This is typically true only for final anonymous namespace types and types
220 defined within functions (that may be COMDAT and thus shared across units,
221 but with the same set of derived types). */
223 bool
224 type_all_derivations_known_p (const_tree t)
226 if (TYPE_FINAL_P (t))
227 return true;
228 if (flag_ltrans)
229 return false;
230 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
231 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
232 return true;
233 if (type_in_anonymous_namespace_p (t))
234 return true;
235 return (decl_function_context (TYPE_NAME (t)) != NULL);
238 /* Return TRUE if type's constructors are all visible. */
240 static bool
241 type_all_ctors_visible_p (tree t)
243 return !flag_ltrans
244 && symtab->state >= CONSTRUCTION
245 /* We can not always use type_all_derivations_known_p.
246 For function local types we must assume case where
247 the function is COMDAT and shared in between units.
249 TODO: These cases are quite easy to get, but we need
250 to keep track of C++ privatizing via -Wno-weak
251 as well as the IPA privatizing. */
252 && type_in_anonymous_namespace_p (t);
255 /* Return TRUE if type may have instance. */
257 static bool
258 type_possibly_instantiated_p (tree t)
260 tree vtable;
261 varpool_node *vnode;
263 /* TODO: Add abstract types here. */
264 if (!type_all_ctors_visible_p (t))
265 return true;
267 vtable = BINFO_VTABLE (TYPE_BINFO (t));
268 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
269 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
270 vnode = varpool_node::get (vtable);
271 return vnode && vnode->definition;
274 /* Hash used to unify ODR types based on their mangled name and for anonymous
275 namespace types. */
277 struct odr_name_hasher : pointer_hash <odr_type_d>
279 typedef union tree_node *compare_type;
280 static inline hashval_t hash (const odr_type_d *);
281 static inline bool equal (const odr_type_d *, const tree_node *);
282 static inline void remove (odr_type_d *);
285 /* Has used to unify ODR types based on their associated virtual table.
286 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
287 only polymorphic types. Types with mangled names are inserted to both. */
289 struct odr_vtable_hasher:odr_name_hasher
291 static inline hashval_t hash (const odr_type_d *);
292 static inline bool equal (const odr_type_d *, const tree_node *);
295 /* Return type that was declared with T's name so that T is an
296 qualified variant of it. */
298 static inline tree
299 main_odr_variant (const_tree t)
301 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
302 return TREE_TYPE (TYPE_NAME (t));
303 /* Unnamed types and non-C++ produced types can be compared by variants. */
304 else
305 return TYPE_MAIN_VARIANT (t);
308 static bool
309 can_be_name_hashed_p (tree t)
311 return (!in_lto_p || odr_type_p (t));
314 /* Hash type by its ODR name. */
316 static hashval_t
317 hash_odr_name (const_tree t)
319 gcc_checking_assert (main_odr_variant (t) == t);
321 /* If not in LTO, all main variants are unique, so we can do
322 pointer hash. */
323 if (!in_lto_p)
324 return htab_hash_pointer (t);
326 /* Anonymous types are unique. */
327 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
328 return htab_hash_pointer (t);
330 gcc_checking_assert (TYPE_NAME (t)
331 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
332 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
335 /* Return the computed hashcode for ODR_TYPE. */
337 inline hashval_t
338 odr_name_hasher::hash (const odr_type_d *odr_type)
340 return hash_odr_name (odr_type->type);
343 static bool
344 can_be_vtable_hashed_p (tree t)
346 /* vtable hashing can distinguish only main variants. */
347 if (TYPE_MAIN_VARIANT (t) != t)
348 return false;
349 /* Anonymous namespace types are always handled by name hash. */
350 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
351 return false;
352 return (TREE_CODE (t) == RECORD_TYPE
353 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
356 /* Hash type by assembler name of its vtable. */
358 static hashval_t
359 hash_odr_vtable (const_tree t)
361 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
362 inchash::hash hstate;
364 gcc_checking_assert (in_lto_p);
365 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
366 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
367 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
368 gcc_checking_assert (main_odr_variant (t) == t);
370 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
372 add_expr (TREE_OPERAND (v, 1), hstate);
373 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
376 hstate.add_hwi (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
377 return hstate.end ();
380 /* Return the computed hashcode for ODR_TYPE. */
382 inline hashval_t
383 odr_vtable_hasher::hash (const odr_type_d *odr_type)
385 return hash_odr_vtable (odr_type->type);
388 /* For languages with One Definition Rule, work out if
389 types are the same based on their name.
391 This is non-trivial for LTO where minor differences in
392 the type representation may have prevented type merging
393 to merge two copies of otherwise equivalent type.
395 Until we start streaming mangled type names, this function works
396 only for polymorphic types.
398 When STRICT is true, we compare types by their names for purposes of
399 ODR violation warnings. When strict is false, we consider variants
400 equivalent, because it is all that matters for devirtualization machinery.
403 bool
404 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
406 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
408 type1 = main_odr_variant (type1);
409 type2 = main_odr_variant (type2);
410 if (!strict)
412 type1 = TYPE_MAIN_VARIANT (type1);
413 type2 = TYPE_MAIN_VARIANT (type2);
416 if (type1 == type2)
417 return true;
419 if (!in_lto_p)
420 return false;
422 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
423 on the corresponding TYPE_STUB_DECL. */
424 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
425 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
426 return false;
429 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
431 Ideally we should never need types without ODR names here. It can however
432 happen in two cases:
434 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
435 Here testing for equivalence is safe, since their MAIN_VARIANTs are
436 unique.
437 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
438 establish precise ODR equivalency, but for correctness we care only
439 about equivalency on complete polymorphic types. For these we can
440 compare assembler names of their virtual tables. */
441 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
442 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
444 /* See if types are obviously different (i.e. different codes
445 or polymorphic wrt non-polymorphic). This is not strictly correct
446 for ODR violating programs, but we can't do better without streaming
447 ODR names. */
448 if (TREE_CODE (type1) != TREE_CODE (type2))
449 return false;
450 if (TREE_CODE (type1) == RECORD_TYPE
451 && (TYPE_BINFO (type1) == NULL_TREE)
452 != (TYPE_BINFO (type2) == NULL_TREE))
453 return false;
454 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
455 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
456 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
457 return false;
459 /* At the moment we have no way to establish ODR equivalence at LTO
460 other than comparing virtual table pointers of polymorphic types.
461 Eventually we should start saving mangled names in TYPE_NAME.
462 Then this condition will become non-trivial. */
464 if (TREE_CODE (type1) == RECORD_TYPE
465 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
466 && BINFO_VTABLE (TYPE_BINFO (type1))
467 && BINFO_VTABLE (TYPE_BINFO (type2)))
469 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
470 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
471 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
472 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
473 return (operand_equal_p (TREE_OPERAND (v1, 1),
474 TREE_OPERAND (v2, 1), 0)
475 && DECL_ASSEMBLER_NAME
476 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
477 == DECL_ASSEMBLER_NAME
478 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
480 gcc_unreachable ();
482 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
483 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
486 /* Return true if we can decide on ODR equivalency.
488 In non-LTO it is always decide, in LTO however it depends in the type has
489 ODR info attached.
491 When STRICT is false, compare main variants. */
493 bool
494 types_odr_comparable (tree t1, tree t2, bool strict)
496 return (!in_lto_p
497 || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
498 && main_odr_variant (t1))
499 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
500 || (odr_type_p (t1) && odr_type_p (t2))
501 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
502 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
503 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
504 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
507 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
508 known, be conservative and return false. */
510 bool
511 types_must_be_same_for_odr (tree t1, tree t2)
513 if (types_odr_comparable (t1, t2))
514 return types_same_for_odr (t1, t2);
515 else
516 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
519 /* If T is compound type, return type it is based on. */
521 static tree
522 compound_type_base (const_tree t)
524 if (TREE_CODE (t) == ARRAY_TYPE
525 || POINTER_TYPE_P (t)
526 || TREE_CODE (t) == COMPLEX_TYPE
527 || VECTOR_TYPE_P (t))
528 return TREE_TYPE (t);
529 if (TREE_CODE (t) == METHOD_TYPE)
530 return TYPE_METHOD_BASETYPE (t);
531 if (TREE_CODE (t) == OFFSET_TYPE)
532 return TYPE_OFFSET_BASETYPE (t);
533 return NULL_TREE;
536 /* Return true if T is either ODR type or compound type based from it.
537 If the function return true, we know that T is a type originating from C++
538 source even at link-time. */
540 bool
541 odr_or_derived_type_p (const_tree t)
545 if (odr_type_p (t))
546 return true;
547 /* Function type is a tricky one. Basically we can consider it
548 ODR derived if return type or any of the parameters is.
549 We need to check all parameters because LTO streaming merges
550 common types (such as void) and they are not considered ODR then. */
551 if (TREE_CODE (t) == FUNCTION_TYPE)
553 if (TYPE_METHOD_BASETYPE (t))
554 t = TYPE_METHOD_BASETYPE (t);
555 else
557 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
558 return true;
559 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
560 if (odr_or_derived_type_p (TREE_VALUE (t)))
561 return true;
562 return false;
565 else
566 t = compound_type_base (t);
568 while (t);
569 return t;
572 /* Compare types T1 and T2 and return true if they are
573 equivalent. */
575 inline bool
576 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
578 tree t1 = o1->type;
580 gcc_checking_assert (main_odr_variant (t2) == t2);
581 gcc_checking_assert (main_odr_variant (t1) == t1);
582 if (t1 == t2)
583 return true;
584 if (!in_lto_p)
585 return false;
586 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
587 on the corresponding TYPE_STUB_DECL. */
588 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
589 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
590 return false;
591 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
592 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
593 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
594 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
597 /* Compare types T1 and T2 and return true if they are
598 equivalent. */
600 inline bool
601 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
603 tree t1 = o1->type;
605 gcc_checking_assert (main_odr_variant (t2) == t2);
606 gcc_checking_assert (main_odr_variant (t1) == t1);
607 gcc_checking_assert (in_lto_p);
608 t1 = TYPE_MAIN_VARIANT (t1);
609 t2 = TYPE_MAIN_VARIANT (t2);
610 if (t1 == t2)
611 return true;
612 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
613 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
614 return (operand_equal_p (TREE_OPERAND (v1, 1),
615 TREE_OPERAND (v2, 1), 0)
616 && DECL_ASSEMBLER_NAME
617 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
618 == DECL_ASSEMBLER_NAME
619 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
622 /* Free ODR type V. */
624 inline void
625 odr_name_hasher::remove (odr_type_d *v)
627 v->bases.release ();
628 v->derived_types.release ();
629 if (v->types_set)
630 delete v->types_set;
631 ggc_free (v);
634 /* ODR type hash used to look up ODR type based on tree type node. */
636 typedef hash_table<odr_name_hasher> odr_hash_type;
637 static odr_hash_type *odr_hash;
638 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
639 static odr_vtable_hash_type *odr_vtable_hash;
641 /* ODR types are also stored into ODR_TYPE vector to allow consistent
642 walking. Bases appear before derived types. Vector is garbage collected
643 so we won't end up visiting empty types. */
645 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
646 #define odr_types (*odr_types_ptr)
648 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
649 void
650 set_type_binfo (tree type, tree binfo)
652 for (; type; type = TYPE_NEXT_VARIANT (type))
653 if (COMPLETE_TYPE_P (type))
654 TYPE_BINFO (type) = binfo;
655 else
656 gcc_assert (!TYPE_BINFO (type));
659 /* Compare T1 and T2 based on name or structure. */
661 static bool
662 odr_subtypes_equivalent_p (tree t1, tree t2,
663 hash_set<type_pair> *visited,
664 location_t loc1, location_t loc2)
667 /* This can happen in incomplete types that should be handled earlier. */
668 gcc_assert (t1 && t2);
670 t1 = main_odr_variant (t1);
671 t2 = main_odr_variant (t2);
672 if (t1 == t2)
673 return true;
675 /* Anonymous namespace types must match exactly. */
676 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
677 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
678 return false;
680 /* For ODR types be sure to compare their names.
681 To support -Wno-odr-type-merging we allow one type to be non-ODR
682 and other ODR even though it is a violation. */
683 if (types_odr_comparable (t1, t2, true))
685 if (!types_same_for_odr (t1, t2, true))
686 return false;
687 /* Limit recursion: if subtypes are ODR types and we know that they are
688 same, be happy. We need to call get_odr_type on both subtypes since
689 we don't know which among t1 and t2 defines the common ODR type and
690 therefore which call will report the ODR violation, if any. */
691 if (!odr_type_p (t1)
692 || !odr_type_p (t2)
693 || !COMPLETE_TYPE_P (t1)
694 || !COMPLETE_TYPE_P (t2)
695 || (!get_odr_type (t1, true)->odr_violated
696 && !get_odr_type (t2, true)->odr_violated))
697 return true;
700 /* Component types, builtins and possibly violating ODR types
701 have to be compared structurally. */
702 if (TREE_CODE (t1) != TREE_CODE (t2))
703 return false;
704 if (AGGREGATE_TYPE_P (t1)
705 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
706 return false;
708 type_pair pair={t1,t2};
709 if (TYPE_UID (t1) > TYPE_UID (t2))
711 pair.first = t2;
712 pair.second = t1;
714 if (visited->add (pair))
715 return true;
716 return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
719 /* Return true if DECL1 and DECL2 are identical methods. Consider
720 name equivalent to name.localalias.xyz. */
722 static bool
723 methods_equal_p (tree decl1, tree decl2)
725 if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2))
726 return true;
727 const char sep = symbol_table::symbol_suffix_separator ();
729 const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1));
730 const char *ptr1 = strchr (name1, sep);
731 int len1 = ptr1 ? ptr1 - name1 : strlen (name1);
733 const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2));
734 const char *ptr2 = strchr (name2, sep);
735 int len2 = ptr2 ? ptr2 - name2 : strlen (name2);
737 if (len1 != len2)
738 return false;
739 return !strncmp (name1, name2, len1);
742 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
743 violation warnings. */
745 void
746 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
748 int n1, n2;
750 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
752 odr_violation_reported = true;
753 if (DECL_VIRTUAL_P (prevailing->decl))
755 varpool_node *tmp = prevailing;
756 prevailing = vtable;
757 vtable = tmp;
759 if (warning_at (DECL_SOURCE_LOCATION
760 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
761 OPT_Wodr,
762 "virtual table of type %qD violates one definition rule",
763 DECL_CONTEXT (vtable->decl)))
764 inform (DECL_SOURCE_LOCATION (prevailing->decl),
765 "variable of same assembler name as the virtual table is "
766 "defined in another translation unit");
767 return;
769 if (!prevailing->definition || !vtable->definition)
770 return;
772 /* If we do not stream ODR type info, do not bother to do useful compare. */
773 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
774 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
775 return;
777 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
779 if (class_type->odr_violated)
780 return;
782 for (n1 = 0, n2 = 0; true; n1++, n2++)
784 struct ipa_ref *ref1, *ref2;
785 bool end1, end2;
787 end1 = !prevailing->iterate_reference (n1, ref1);
788 end2 = !vtable->iterate_reference (n2, ref2);
790 /* !DECL_VIRTUAL_P means RTTI entry;
791 We warn when RTTI is lost because non-RTTI previals; we silently
792 accept the other case. */
793 while (!end2
794 && (end1
795 || (methods_equal_p (ref1->referred->decl,
796 ref2->referred->decl)
797 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
798 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
800 if (!class_type->rtti_broken
801 && warning_at (DECL_SOURCE_LOCATION
802 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
803 OPT_Wodr,
804 "virtual table of type %qD contains RTTI "
805 "information",
806 DECL_CONTEXT (vtable->decl)))
808 inform (DECL_SOURCE_LOCATION
809 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
810 "but is prevailed by one without from other translation "
811 "unit");
812 inform (DECL_SOURCE_LOCATION
813 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
814 "RTTI will not work on this type");
815 class_type->rtti_broken = true;
817 n2++;
818 end2 = !vtable->iterate_reference (n2, ref2);
820 while (!end1
821 && (end2
822 || (methods_equal_p (ref2->referred->decl, ref1->referred->decl)
823 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
824 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
826 n1++;
827 end1 = !prevailing->iterate_reference (n1, ref1);
830 /* Finished? */
831 if (end1 && end2)
833 /* Extra paranoia; compare the sizes. We do not have information
834 about virtual inheritance offsets, so just be sure that these
835 match.
836 Do this as very last check so the not very informative error
837 is not output too often. */
838 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
840 class_type->odr_violated = true;
841 if (warning_at (DECL_SOURCE_LOCATION
842 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
843 OPT_Wodr,
844 "virtual table of type %qD violates "
845 "one definition rule ",
846 DECL_CONTEXT (vtable->decl)))
848 inform (DECL_SOURCE_LOCATION
849 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
850 "the conflicting type defined in another translation "
851 "unit has virtual table of different size");
854 return;
857 if (!end1 && !end2)
859 if (methods_equal_p (ref1->referred->decl, ref2->referred->decl))
860 continue;
862 class_type->odr_violated = true;
864 /* If the loops above stopped on non-virtual pointer, we have
865 mismatch in RTTI information mangling. */
866 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
867 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
869 if (warning_at (DECL_SOURCE_LOCATION
870 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
871 OPT_Wodr,
872 "virtual table of type %qD violates "
873 "one definition rule ",
874 DECL_CONTEXT (vtable->decl)))
876 inform (DECL_SOURCE_LOCATION
877 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
878 "the conflicting type defined in another translation "
879 "unit with different RTTI information");
881 return;
883 /* At this point both REF1 and REF2 points either to virtual table
884 or virtual method. If one points to virtual table and other to
885 method we can complain the same way as if one table was shorter
886 than other pointing out the extra method. */
887 if (TREE_CODE (ref1->referred->decl)
888 != TREE_CODE (ref2->referred->decl))
890 if (VAR_P (ref1->referred->decl))
891 end1 = true;
892 else if (VAR_P (ref2->referred->decl))
893 end2 = true;
897 class_type->odr_violated = true;
899 /* Complain about size mismatch. Either we have too many virutal
900 functions or too many virtual table pointers. */
901 if (end1 || end2)
903 if (end1)
905 varpool_node *tmp = prevailing;
906 prevailing = vtable;
907 vtable = tmp;
908 ref1 = ref2;
910 if (warning_at (DECL_SOURCE_LOCATION
911 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
912 OPT_Wodr,
913 "virtual table of type %qD violates "
914 "one definition rule",
915 DECL_CONTEXT (vtable->decl)))
917 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
919 inform (DECL_SOURCE_LOCATION
920 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
921 "the conflicting type defined in another translation "
922 "unit");
923 inform (DECL_SOURCE_LOCATION
924 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
925 "contains additional virtual method %qD",
926 ref1->referred->decl);
928 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 more entries");
936 return;
939 /* And in the last case we have either mistmatch in between two virtual
940 methods or two virtual table pointers. */
941 if (warning_at (DECL_SOURCE_LOCATION
942 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
943 "virtual table of type %qD violates "
944 "one definition rule ",
945 DECL_CONTEXT (vtable->decl)))
947 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
949 inform (DECL_SOURCE_LOCATION
950 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
951 "the conflicting type defined in another translation "
952 "unit");
953 gcc_assert (TREE_CODE (ref2->referred->decl)
954 == FUNCTION_DECL);
955 inform (DECL_SOURCE_LOCATION
956 (ref1->referred->ultimate_alias_target ()->decl),
957 "virtual method %qD",
958 ref1->referred->ultimate_alias_target ()->decl);
959 inform (DECL_SOURCE_LOCATION
960 (ref2->referred->ultimate_alias_target ()->decl),
961 "ought to match virtual method %qD but does not",
962 ref2->referred->ultimate_alias_target ()->decl);
964 else
965 inform (DECL_SOURCE_LOCATION
966 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
967 "the conflicting type defined in another translation "
968 "unit has virtual table with different contents");
969 return;
974 /* Output ODR violation warning about T1 and T2 with REASON.
975 Display location of ST1 and ST2 if REASON speaks about field or
976 method of the type.
977 If WARN is false, do nothing. Set WARNED if warning was indeed
978 output. */
980 void
981 warn_odr (tree t1, tree t2, tree st1, tree st2,
982 bool warn, bool *warned, const char *reason)
984 tree decl2 = TYPE_NAME (t2);
985 if (warned)
986 *warned = false;
988 if (!warn || !TYPE_NAME(t1))
989 return;
991 /* ODR warnings are output druing LTO streaming; we must apply location
992 cache for potential warnings to be output correctly. */
993 if (lto_location_cache::current_cache)
994 lto_location_cache::current_cache->apply_location_cache ();
996 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
997 "type %qT violates the C++ One Definition Rule",
998 t1))
999 return;
1000 if (!st1 && !st2)
1002 /* For FIELD_DECL support also case where one of fields is
1003 NULL - this is used when the structures have mismatching number of
1004 elements. */
1005 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
1007 inform (DECL_SOURCE_LOCATION (decl2),
1008 "a different type is defined in another translation unit");
1009 if (!st1)
1011 st1 = st2;
1012 st2 = NULL;
1014 inform (DECL_SOURCE_LOCATION (st1),
1015 "the first difference of corresponding definitions is field %qD",
1016 st1);
1017 if (st2)
1018 decl2 = st2;
1020 else if (TREE_CODE (st1) == FUNCTION_DECL)
1022 inform (DECL_SOURCE_LOCATION (decl2),
1023 "a different type is defined in another translation unit");
1024 inform (DECL_SOURCE_LOCATION (st1),
1025 "the first difference of corresponding definitions is method %qD",
1026 st1);
1027 decl2 = st2;
1029 else
1030 return;
1031 inform (DECL_SOURCE_LOCATION (decl2), reason);
1033 if (warned)
1034 *warned = true;
1037 /* Return ture if T1 and T2 are incompatible and we want to recusively
1038 dive into them from warn_type_mismatch to give sensible answer. */
1040 static bool
1041 type_mismatch_p (tree t1, tree t2)
1043 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1044 && !odr_types_equivalent_p (t1, t2))
1045 return true;
1046 return !types_compatible_p (t1, t2);
1050 /* Types T1 and T2 was found to be incompatible in a context they can't
1051 (either used to declare a symbol of same assembler name or unified by
1052 ODR rule). We already output warning about this, but if possible, output
1053 extra information on how the types mismatch.
1055 This is hard to do in general. We basically handle the common cases.
1057 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1058 themselves do no thave one.*/
1060 void
1061 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1063 /* Location of type is known only if it has TYPE_NAME and the name is
1064 TYPE_DECL. */
1065 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1066 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1067 : UNKNOWN_LOCATION;
1068 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1069 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1070 : UNKNOWN_LOCATION;
1071 bool loc_t2_useful = false;
1073 /* With LTO it is a common case that the location of both types match.
1074 See if T2 has a location that is different from T1. If so, we will
1075 inform user about the location.
1076 Do not consider the location passed to us in LOC1/LOC2 as those are
1077 already output. */
1078 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1080 if (loc_t1 <= BUILTINS_LOCATION)
1081 loc_t2_useful = true;
1082 else
1084 expanded_location xloc1 = expand_location (loc_t1);
1085 expanded_location xloc2 = expand_location (loc_t2);
1087 if (strcmp (xloc1.file, xloc2.file)
1088 || xloc1.line != xloc2.line
1089 || xloc1.column != xloc2.column)
1090 loc_t2_useful = true;
1094 if (loc_t1 <= BUILTINS_LOCATION)
1095 loc_t1 = loc1;
1096 if (loc_t2 <= BUILTINS_LOCATION)
1097 loc_t2 = loc2;
1099 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1101 /* It is a quite common bug to reference anonymous namespace type in
1102 non-anonymous namespace class. */
1103 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1104 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1106 if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
1108 std::swap (t1, t2);
1109 std::swap (loc_t1, loc_t2);
1111 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1112 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1113 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1114 /* Most of the time, the type names will match, do not be unnecesarily
1115 verbose. */
1116 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
1117 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
1118 inform (loc_t1,
1119 "type %qT defined in anonymous namespace can not match "
1120 "type %qT across the translation unit boundary",
1121 t1, t2);
1122 else
1123 inform (loc_t1,
1124 "type %qT defined in anonymous namespace can not match "
1125 "across the translation unit boundary",
1126 t1);
1127 if (loc_t2_useful)
1128 inform (loc_t2,
1129 "the incompatible type defined in another translation unit");
1130 return;
1132 /* If types have mangled ODR names and they are different, it is most
1133 informative to output those.
1134 This also covers types defined in different namespaces. */
1135 if (TYPE_NAME (t1) && TYPE_NAME (t2)
1136 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1137 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1138 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
1139 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
1140 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
1141 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1143 char *name1 = xstrdup (cplus_demangle
1144 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1145 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1146 char *name2 = cplus_demangle
1147 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1148 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1149 if (name1 && name2 && strcmp (name1, name2))
1151 inform (loc_t1,
1152 "type name %qs should match type name %qs",
1153 name1, name2);
1154 if (loc_t2_useful)
1155 inform (loc_t2,
1156 "the incompatible type is defined here");
1157 free (name1);
1158 return;
1160 free (name1);
1162 /* A tricky case are compound types. Often they appear the same in source
1163 code and the mismatch is dragged in by type they are build from.
1164 Look for those differences in subtypes and try to be informative. In other
1165 cases just output nothing because the source code is probably different
1166 and in this case we already output a all necessary info. */
1167 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1169 if (TREE_CODE (t1) == TREE_CODE (t2))
1171 if (TREE_CODE (t1) == ARRAY_TYPE
1172 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1174 tree i1 = TYPE_DOMAIN (t1);
1175 tree i2 = TYPE_DOMAIN (t2);
1177 if (i1 && i2
1178 && TYPE_MAX_VALUE (i1)
1179 && TYPE_MAX_VALUE (i2)
1180 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1181 TYPE_MAX_VALUE (i2), 0))
1183 inform (loc,
1184 "array types have different bounds");
1185 return;
1188 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1189 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1190 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1191 else if (TREE_CODE (t1) == METHOD_TYPE
1192 || TREE_CODE (t1) == FUNCTION_TYPE)
1194 tree parms1 = NULL, parms2 = NULL;
1195 int count = 1;
1197 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1199 inform (loc, "return value type mismatch");
1200 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1201 loc_t2);
1202 return;
1204 if (prototype_p (t1) && prototype_p (t2))
1205 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1206 parms1 && parms2;
1207 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1208 count++)
1210 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1212 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1213 inform (loc,
1214 "implicit this pointer type mismatch");
1215 else
1216 inform (loc,
1217 "type mismatch in parameter %i",
1218 count - (TREE_CODE (t1) == METHOD_TYPE));
1219 warn_types_mismatch (TREE_VALUE (parms1),
1220 TREE_VALUE (parms2),
1221 loc_t1, loc_t2);
1222 return;
1225 if (parms1 || parms2)
1227 inform (loc,
1228 "types have different parameter counts");
1229 return;
1233 return;
1236 if (types_odr_comparable (t1, t2, true)
1237 && types_same_for_odr (t1, t2, true))
1238 inform (loc_t1,
1239 "type %qT itself violates the C++ One Definition Rule", t1);
1240 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1241 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1242 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1243 return;
1244 else
1245 inform (loc_t1, "type %qT should match type %qT",
1246 t1, t2);
1247 if (loc_t2_useful)
1248 inform (loc_t2, "the incompatible type is defined here");
1251 /* Compare T1 and T2, report ODR violations if WARN is true and set
1252 WARNED to true if anything is reported. Return true if types match.
1253 If true is returned, the types are also compatible in the sense of
1254 gimple_canonical_types_compatible_p.
1255 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1256 about the type if the type itself do not have location. */
1258 static bool
1259 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1260 hash_set<type_pair> *visited,
1261 location_t loc1, location_t loc2)
1263 /* Check first for the obvious case of pointer identity. */
1264 if (t1 == t2)
1265 return true;
1266 gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
1267 gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
1269 /* Can't be the same type if the types don't have the same code. */
1270 if (TREE_CODE (t1) != TREE_CODE (t2))
1272 warn_odr (t1, t2, NULL, NULL, warn, warned,
1273 G_("a different type is defined in another translation unit"));
1274 return false;
1277 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1279 warn_odr (t1, t2, NULL, NULL, warn, warned,
1280 G_("a type with different qualifiers is defined in another "
1281 "translation unit"));
1282 return false;
1285 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1286 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1288 /* We can not trip this when comparing ODR types, only when trying to
1289 match different ODR derivations from different declarations.
1290 So WARN should be always false. */
1291 gcc_assert (!warn);
1292 return false;
1295 if (comp_type_attributes (t1, t2) != 1)
1297 warn_odr (t1, t2, NULL, NULL, warn, warned,
1298 G_("a type with different attributes "
1299 "is defined in another translation unit"));
1300 return false;
1303 if (TREE_CODE (t1) == ENUMERAL_TYPE
1304 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1306 tree v1, v2;
1307 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1308 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1310 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1312 warn_odr (t1, t2, NULL, NULL, warn, warned,
1313 G_("an enum with different value name"
1314 " is defined in another translation unit"));
1315 return false;
1317 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1318 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1319 DECL_INITIAL (TREE_VALUE (v2)), 0))
1321 warn_odr (t1, t2, NULL, NULL, warn, warned,
1322 G_("an enum with different values is defined"
1323 " in another translation unit"));
1324 return false;
1327 if (v1 || v2)
1329 warn_odr (t1, t2, NULL, NULL, warn, warned,
1330 G_("an enum with mismatching number of values "
1331 "is defined in another translation unit"));
1332 return false;
1336 /* Non-aggregate types can be handled cheaply. */
1337 if (INTEGRAL_TYPE_P (t1)
1338 || SCALAR_FLOAT_TYPE_P (t1)
1339 || FIXED_POINT_TYPE_P (t1)
1340 || TREE_CODE (t1) == VECTOR_TYPE
1341 || TREE_CODE (t1) == COMPLEX_TYPE
1342 || TREE_CODE (t1) == OFFSET_TYPE
1343 || POINTER_TYPE_P (t1))
1345 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1347 warn_odr (t1, t2, NULL, NULL, warn, warned,
1348 G_("a type with different precision is defined "
1349 "in another translation unit"));
1350 return false;
1352 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1354 warn_odr (t1, t2, NULL, NULL, warn, warned,
1355 G_("a type with different signedness is defined "
1356 "in another translation unit"));
1357 return false;
1360 if (TREE_CODE (t1) == INTEGER_TYPE
1361 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1363 /* char WRT uint_8? */
1364 warn_odr (t1, t2, NULL, NULL, warn, warned,
1365 G_("a different type is defined in another "
1366 "translation unit"));
1367 return false;
1370 /* For canonical type comparisons we do not want to build SCCs
1371 so we cannot compare pointed-to types. But we can, for now,
1372 require the same pointed-to type kind and match what
1373 useless_type_conversion_p would do. */
1374 if (POINTER_TYPE_P (t1))
1376 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1377 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1379 warn_odr (t1, t2, NULL, NULL, warn, warned,
1380 G_("it is defined as a pointer in different address "
1381 "space in another translation unit"));
1382 return false;
1385 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1386 visited, loc1, loc2))
1388 warn_odr (t1, t2, NULL, NULL, warn, warned,
1389 G_("it is defined as a pointer to different type "
1390 "in another translation unit"));
1391 if (warn && warned)
1392 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1393 loc1, loc2);
1394 return false;
1398 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1399 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1400 visited, loc1, loc2))
1402 /* Probably specific enough. */
1403 warn_odr (t1, t2, NULL, NULL, warn, warned,
1404 G_("a different type is defined "
1405 "in another translation unit"));
1406 if (warn && warned)
1407 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1408 return false;
1411 /* Do type-specific comparisons. */
1412 else switch (TREE_CODE (t1))
1414 case ARRAY_TYPE:
1416 /* Array types are the same if the element types are the same and
1417 the number of elements are the same. */
1418 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1419 visited, loc1, loc2))
1421 warn_odr (t1, t2, NULL, NULL, warn, warned,
1422 G_("a different type is defined in another "
1423 "translation unit"));
1424 if (warn && warned)
1425 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1427 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1428 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1429 == TYPE_NONALIASED_COMPONENT (t2));
1431 tree i1 = TYPE_DOMAIN (t1);
1432 tree i2 = TYPE_DOMAIN (t2);
1434 /* For an incomplete external array, the type domain can be
1435 NULL_TREE. Check this condition also. */
1436 if (i1 == NULL_TREE || i2 == NULL_TREE)
1437 return true;
1439 tree min1 = TYPE_MIN_VALUE (i1);
1440 tree min2 = TYPE_MIN_VALUE (i2);
1441 tree max1 = TYPE_MAX_VALUE (i1);
1442 tree max2 = TYPE_MAX_VALUE (i2);
1444 /* In C++, minimums should be always 0. */
1445 gcc_assert (min1 == min2);
1446 if (!operand_equal_p (max1, max2, 0))
1448 warn_odr (t1, t2, NULL, NULL, warn, warned,
1449 G_("an array of different size is defined "
1450 "in another translation unit"));
1451 return false;
1454 break;
1456 case METHOD_TYPE:
1457 case FUNCTION_TYPE:
1458 /* Function types are the same if the return type and arguments types
1459 are the same. */
1460 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1461 visited, loc1, loc2))
1463 warn_odr (t1, t2, NULL, NULL, warn, warned,
1464 G_("has different return value "
1465 "in another translation unit"));
1466 if (warn && warned)
1467 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1468 return false;
1471 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1472 || !prototype_p (t1) || !prototype_p (t2))
1473 return true;
1474 else
1476 tree parms1, parms2;
1478 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1479 parms1 && parms2;
1480 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1482 if (!odr_subtypes_equivalent_p
1483 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
1484 loc1, loc2))
1486 warn_odr (t1, t2, NULL, NULL, warn, warned,
1487 G_("has different parameters in another "
1488 "translation unit"));
1489 if (warn && warned)
1490 warn_types_mismatch (TREE_VALUE (parms1),
1491 TREE_VALUE (parms2), loc1, loc2);
1492 return false;
1496 if (parms1 || parms2)
1498 warn_odr (t1, t2, NULL, NULL, warn, warned,
1499 G_("has different parameters "
1500 "in another translation unit"));
1501 return false;
1504 return true;
1507 case RECORD_TYPE:
1508 case UNION_TYPE:
1509 case QUAL_UNION_TYPE:
1511 tree f1, f2;
1513 /* For aggregate types, all the fields must be the same. */
1514 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1516 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1517 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1518 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1520 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1521 warn_odr (t1, t2, NULL, NULL, warn, warned,
1522 G_("a type defined in another translation unit "
1523 "is not polymorphic"));
1524 else
1525 warn_odr (t1, t2, NULL, NULL, warn, warned,
1526 G_("a type defined in another translation unit "
1527 "is polymorphic"));
1528 return false;
1530 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1531 f1 || f2;
1532 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1534 /* Skip non-fields. */
1535 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1536 f1 = TREE_CHAIN (f1);
1537 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1538 f2 = TREE_CHAIN (f2);
1539 if (!f1 || !f2)
1540 break;
1541 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1543 warn_odr (t1, t2, NULL, NULL, warn, warned,
1544 G_("a type with different virtual table pointers"
1545 " is defined in another translation unit"));
1546 return false;
1548 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1550 warn_odr (t1, t2, NULL, NULL, warn, warned,
1551 G_("a type with different bases is defined "
1552 "in another translation unit"));
1553 return false;
1555 if (DECL_NAME (f1) != DECL_NAME (f2)
1556 && !DECL_ARTIFICIAL (f1))
1558 warn_odr (t1, t2, f1, f2, warn, warned,
1559 G_("a field with different name is defined "
1560 "in another translation unit"));
1561 return false;
1563 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1564 TREE_TYPE (f2), visited,
1565 loc1, loc2))
1567 /* Do not warn about artificial fields and just go into
1568 generic field mismatch warning. */
1569 if (DECL_ARTIFICIAL (f1))
1570 break;
1572 warn_odr (t1, t2, f1, f2, warn, warned,
1573 G_("a field of same name but different type "
1574 "is defined in another translation unit"));
1575 if (warn && warned)
1576 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1577 return false;
1579 if (!gimple_compare_field_offset (f1, f2))
1581 /* Do not warn about artificial fields and just go into
1582 generic field mismatch warning. */
1583 if (DECL_ARTIFICIAL (f1))
1584 break;
1585 warn_odr (t1, t2, f1, f2, warn, warned,
1586 G_("fields have different layout "
1587 "in another translation unit"));
1588 return false;
1590 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
1592 warn_odr (t1, t2, f1, f2, warn, warned,
1593 G_("one field is bitfield while other is not"));
1594 return false;
1596 else
1597 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1598 == DECL_NONADDRESSABLE_P (f2));
1601 /* If one aggregate has more fields than the other, they
1602 are not the same. */
1603 if (f1 || f2)
1605 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1606 warn_odr (t1, t2, NULL, NULL, warn, warned,
1607 G_("a type with different virtual table pointers"
1608 " is defined in another translation unit"));
1609 else if ((f1 && DECL_ARTIFICIAL (f1))
1610 || (f2 && DECL_ARTIFICIAL (f2)))
1611 warn_odr (t1, t2, NULL, NULL, warn, warned,
1612 G_("a type with different bases is defined "
1613 "in another translation unit"));
1614 else
1615 warn_odr (t1, t2, f1, f2, warn, warned,
1616 G_("a type with different number of fields "
1617 "is defined in another translation unit"));
1619 return false;
1622 break;
1624 case VOID_TYPE:
1625 case NULLPTR_TYPE:
1626 break;
1628 default:
1629 debug_tree (t1);
1630 gcc_unreachable ();
1633 /* Those are better to come last as they are utterly uninformative. */
1634 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1635 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1637 warn_odr (t1, t2, NULL, NULL, warn, warned,
1638 G_("a type with different size "
1639 "is defined in another translation unit"));
1640 return false;
1642 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1643 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1645 warn_odr (t1, t2, NULL, NULL, warn, warned,
1646 G_("a type with different alignment "
1647 "is defined in another translation unit"));
1648 return false;
1650 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1651 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1652 TYPE_SIZE_UNIT (t2), 0));
1653 return true;
1656 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1658 bool
1659 odr_types_equivalent_p (tree type1, tree type2)
1661 gcc_checking_assert (odr_or_derived_type_p (type1)
1662 && odr_or_derived_type_p (type2));
1664 hash_set<type_pair> visited;
1665 return odr_types_equivalent_p (type1, type2, false, NULL,
1666 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1669 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1670 from VAL->type. This may happen in LTO where tree merging did not merge
1671 all variants of the same type or due to ODR violation.
1673 Analyze and report ODR violations and add type to duplicate list.
1674 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1675 this is first time we see definition of a class return true so the
1676 base types are analyzed. */
1678 static bool
1679 add_type_duplicate (odr_type val, tree type)
1681 bool build_bases = false;
1682 bool prevail = false;
1683 bool odr_must_violate = false;
1685 if (!val->types_set)
1686 val->types_set = new hash_set<tree>;
1688 /* Chose polymorphic type as leader (this happens only in case of ODR
1689 violations. */
1690 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1691 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1692 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1693 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1695 prevail = true;
1696 build_bases = true;
1698 /* Always prefer complete type to be the leader. */
1699 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1701 prevail = true;
1702 build_bases = TYPE_BINFO (type);
1704 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1706 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1707 && TREE_CODE (type) == ENUMERAL_TYPE
1708 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1709 prevail = true;
1710 else if (TREE_CODE (val->type) == RECORD_TYPE
1711 && TREE_CODE (type) == RECORD_TYPE
1712 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1714 gcc_assert (!val->bases.length ());
1715 build_bases = true;
1716 prevail = true;
1719 if (prevail)
1720 std::swap (val->type, type);
1722 val->types_set->add (type);
1724 /* If we now have a mangled name, be sure to record it to val->type
1725 so ODR hash can work. */
1727 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1728 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1729 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1731 bool merge = true;
1732 bool base_mismatch = false;
1733 unsigned int i;
1734 bool warned = false;
1735 hash_set<type_pair> visited;
1737 gcc_assert (in_lto_p);
1738 vec_safe_push (val->types, type);
1740 /* If both are class types, compare the bases. */
1741 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1742 && TREE_CODE (val->type) == RECORD_TYPE
1743 && TREE_CODE (type) == RECORD_TYPE
1744 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1746 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1747 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1749 if (!flag_ltrans && !warned && !val->odr_violated)
1751 tree extra_base;
1752 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1753 "a type with the same name but different "
1754 "number of polymorphic bases is "
1755 "defined in another translation unit");
1756 if (warned)
1758 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1759 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1760 extra_base = BINFO_BASE_BINFO
1761 (TYPE_BINFO (type),
1762 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1763 else
1764 extra_base = BINFO_BASE_BINFO
1765 (TYPE_BINFO (val->type),
1766 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1767 tree extra_base_type = BINFO_TYPE (extra_base);
1768 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1769 "the extra base is defined here");
1772 base_mismatch = true;
1774 else
1775 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1777 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1778 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1779 tree type1 = BINFO_TYPE (base1);
1780 tree type2 = BINFO_TYPE (base2);
1782 if (types_odr_comparable (type1, type2))
1784 if (!types_same_for_odr (type1, type2))
1785 base_mismatch = true;
1787 else
1788 if (!odr_types_equivalent_p (type1, type2))
1789 base_mismatch = true;
1790 if (base_mismatch)
1792 if (!warned && !val->odr_violated)
1794 warn_odr (type, val->type, NULL, NULL,
1795 !warned, &warned,
1796 "a type with the same name but different base "
1797 "type is defined in another translation unit");
1798 if (warned)
1799 warn_types_mismatch (type1, type2,
1800 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1802 break;
1804 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1806 base_mismatch = true;
1807 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 "layout is defined in another translation unit");
1812 break;
1814 /* One of bases is not of complete type. */
1815 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1817 /* If we have a polymorphic type info specified for TYPE1
1818 but not for TYPE2 we possibly missed a base when recording
1819 VAL->type earlier.
1820 Be sure this does not happen. */
1821 if (TYPE_BINFO (type1)
1822 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1823 && !build_bases)
1824 odr_must_violate = true;
1825 break;
1827 /* One base is polymorphic and the other not.
1828 This ought to be diagnosed earlier, but do not ICE in the
1829 checking bellow. */
1830 else if (TYPE_BINFO (type1)
1831 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1832 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1834 if (!warned && !val->odr_violated)
1835 warn_odr (type, val->type, NULL, NULL,
1836 !warned, &warned,
1837 "a base of the type is polymorphic only in one "
1838 "translation unit");
1839 base_mismatch = true;
1840 break;
1843 if (base_mismatch)
1845 merge = false;
1846 odr_violation_reported = true;
1847 val->odr_violated = true;
1849 if (symtab->dump_file)
1851 fprintf (symtab->dump_file, "ODR base violation\n");
1853 print_node (symtab->dump_file, "", val->type, 0);
1854 putc ('\n',symtab->dump_file);
1855 print_node (symtab->dump_file, "", type, 0);
1856 putc ('\n',symtab->dump_file);
1861 /* Next compare memory layout.
1862 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1863 We must apply the location cache to ensure that they are valid
1864 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1865 if (lto_location_cache::current_cache)
1866 lto_location_cache::current_cache->apply_location_cache ();
1867 if (!odr_types_equivalent_p (val->type, type,
1868 !flag_ltrans && !val->odr_violated && !warned,
1869 &warned, &visited,
1870 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1871 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1873 merge = false;
1874 odr_violation_reported = true;
1875 val->odr_violated = true;
1877 gcc_assert (val->odr_violated || !odr_must_violate);
1878 /* Sanity check that all bases will be build same way again. */
1879 if (flag_checking
1880 && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1881 && TREE_CODE (val->type) == RECORD_TYPE
1882 && TREE_CODE (type) == RECORD_TYPE
1883 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1884 && !val->odr_violated
1885 && !base_mismatch && val->bases.length ())
1887 unsigned int num_poly_bases = 0;
1888 unsigned int j;
1890 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1891 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1892 (TYPE_BINFO (type), i)))
1893 num_poly_bases++;
1894 gcc_assert (num_poly_bases == val->bases.length ());
1895 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1896 i++)
1897 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1898 (TYPE_BINFO (type), i)))
1900 odr_type base = get_odr_type
1901 (BINFO_TYPE
1902 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1903 i)),
1904 true);
1905 gcc_assert (val->bases[j] == base);
1906 j++;
1911 /* Regularize things a little. During LTO same types may come with
1912 different BINFOs. Either because their virtual table was
1913 not merged by tree merging and only later at decl merging or
1914 because one type comes with external vtable, while other
1915 with internal. We want to merge equivalent binfos to conserve
1916 memory and streaming overhead.
1918 The external vtables are more harmful: they contain references
1919 to external declarations of methods that may be defined in the
1920 merged LTO unit. For this reason we absolutely need to remove
1921 them and replace by internal variants. Not doing so will lead
1922 to incomplete answers from possible_polymorphic_call_targets.
1924 FIXME: disable for now; because ODR types are now build during
1925 streaming in, the variants do not need to be linked to the type,
1926 yet. We need to do the merging in cleanup pass to be implemented
1927 soon. */
1928 if (!flag_ltrans && merge
1929 && 0
1930 && TREE_CODE (val->type) == RECORD_TYPE
1931 && TREE_CODE (type) == RECORD_TYPE
1932 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1933 && TYPE_MAIN_VARIANT (type) == type
1934 && TYPE_MAIN_VARIANT (val->type) == val->type
1935 && BINFO_VTABLE (TYPE_BINFO (val->type))
1936 && BINFO_VTABLE (TYPE_BINFO (type)))
1938 tree master_binfo = TYPE_BINFO (val->type);
1939 tree v1 = BINFO_VTABLE (master_binfo);
1940 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1942 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1944 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1945 && operand_equal_p (TREE_OPERAND (v1, 1),
1946 TREE_OPERAND (v2, 1), 0));
1947 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1948 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1950 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1951 == DECL_ASSEMBLER_NAME (v2));
1953 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1955 unsigned int i;
1957 set_type_binfo (val->type, TYPE_BINFO (type));
1958 for (i = 0; i < val->types->length (); i++)
1960 if (TYPE_BINFO ((*val->types)[i])
1961 == master_binfo)
1962 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1964 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1966 else
1967 set_type_binfo (type, master_binfo);
1969 return build_bases;
1972 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1973 possibly new entry. */
1975 odr_type
1976 get_odr_type (tree type, bool insert)
1978 odr_type_d **slot = NULL;
1979 odr_type_d **vtable_slot = NULL;
1980 odr_type val = NULL;
1981 hashval_t hash;
1982 bool build_bases = false;
1983 bool insert_to_odr_array = false;
1984 int base_id = -1;
1986 type = main_odr_variant (type);
1988 gcc_checking_assert (can_be_name_hashed_p (type)
1989 || can_be_vtable_hashed_p (type));
1991 /* Lookup entry, first try name hash, fallback to vtable hash. */
1992 if (can_be_name_hashed_p (type))
1994 hash = hash_odr_name (type);
1995 slot = odr_hash->find_slot_with_hash (type, hash,
1996 insert ? INSERT : NO_INSERT);
1998 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2000 hash = hash_odr_vtable (type);
2001 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2002 insert ? INSERT : NO_INSERT);
2005 if (!slot && !vtable_slot)
2006 return NULL;
2008 /* See if we already have entry for type. */
2009 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2011 if (slot && *slot)
2013 val = *slot;
2014 if (flag_checking
2015 && in_lto_p && can_be_vtable_hashed_p (type))
2017 hash = hash_odr_vtable (type);
2018 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2019 NO_INSERT);
2020 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2021 vtable_slot = NULL;
2024 else if (*vtable_slot)
2025 val = *vtable_slot;
2027 if (val->type != type
2028 && (!val->types_set || !val->types_set->add (type)))
2030 gcc_assert (insert);
2031 /* We have type duplicate, but it may introduce vtable name or
2032 mangled name; be sure to keep hashes in sync. */
2033 if (in_lto_p && can_be_vtable_hashed_p (type)
2034 && (!vtable_slot || !*vtable_slot))
2036 if (!vtable_slot)
2038 hash = hash_odr_vtable (type);
2039 vtable_slot = odr_vtable_hash->find_slot_with_hash
2040 (type, hash, INSERT);
2041 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2043 *vtable_slot = val;
2045 if (slot && !*slot)
2046 *slot = val;
2047 build_bases = add_type_duplicate (val, type);
2050 else
2052 val = ggc_cleared_alloc<odr_type_d> ();
2053 val->type = type;
2054 val->bases = vNULL;
2055 val->derived_types = vNULL;
2056 if (type_with_linkage_p (type))
2057 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2058 else
2059 val->anonymous_namespace = 0;
2060 build_bases = COMPLETE_TYPE_P (val->type);
2061 insert_to_odr_array = true;
2062 if (slot)
2063 *slot = val;
2064 if (vtable_slot)
2065 *vtable_slot = val;
2068 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2069 && type_with_linkage_p (type)
2070 && type == TYPE_MAIN_VARIANT (type))
2072 tree binfo = TYPE_BINFO (type);
2073 unsigned int i;
2075 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2077 val->all_derivations_known = type_all_derivations_known_p (type);
2078 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2079 /* For now record only polymorphic types. other are
2080 pointless for devirtualization and we can not precisely
2081 determine ODR equivalency of these during LTO. */
2082 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2084 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2085 odr_type base = get_odr_type (base_type, true);
2086 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2087 base->derived_types.safe_push (val);
2088 val->bases.safe_push (base);
2089 if (base->id > base_id)
2090 base_id = base->id;
2093 /* Ensure that type always appears after bases. */
2094 if (insert_to_odr_array)
2096 if (odr_types_ptr)
2097 val->id = odr_types.length ();
2098 vec_safe_push (odr_types_ptr, val);
2100 else if (base_id > val->id)
2102 odr_types[val->id] = 0;
2103 /* Be sure we did not recorded any derived types; these may need
2104 renumbering too. */
2105 gcc_assert (val->derived_types.length() == 0);
2106 val->id = odr_types.length ();
2107 vec_safe_push (odr_types_ptr, val);
2109 return val;
2112 /* Add TYPE od ODR type hash. */
2114 void
2115 register_odr_type (tree type)
2117 if (!odr_hash)
2119 odr_hash = new odr_hash_type (23);
2120 if (in_lto_p)
2121 odr_vtable_hash = new odr_vtable_hash_type (23);
2123 /* Arrange things to be nicer and insert main variants first.
2124 ??? fundamental prerecorded types do not have mangled names; this
2125 makes it possible that non-ODR type is main_odr_variant of ODR type.
2126 Things may get smoother if LTO FE set mangled name of those types same
2127 way as C++ FE does. */
2128 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
2129 && odr_type_p (TYPE_MAIN_VARIANT (type)))
2130 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2131 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
2132 get_odr_type (type, true);
2135 /* Return true if type is known to have no derivations. */
2137 bool
2138 type_known_to_have_no_derivations_p (tree t)
2140 return (type_all_derivations_known_p (t)
2141 && (TYPE_FINAL_P (t)
2142 || (odr_hash
2143 && !get_odr_type (t, true)->derived_types.length())));
2146 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2147 recursive printing. */
2149 static void
2150 dump_odr_type (FILE *f, odr_type t, int indent=0)
2152 unsigned int i;
2153 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2154 print_generic_expr (f, t->type, TDF_SLIM);
2155 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2156 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2157 if (TYPE_NAME (t->type))
2159 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2160 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2161 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2162 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2163 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2164 IDENTIFIER_POINTER
2165 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2167 if (t->bases.length ())
2169 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2170 for (i = 0; i < t->bases.length (); i++)
2171 fprintf (f, " %i", t->bases[i]->id);
2172 fprintf (f, "\n");
2174 if (t->derived_types.length ())
2176 fprintf (f, "%*s derived types:\n", indent * 2, "");
2177 for (i = 0; i < t->derived_types.length (); i++)
2178 dump_odr_type (f, t->derived_types[i], indent + 1);
2180 fprintf (f, "\n");
2183 /* Dump the type inheritance graph. */
2185 static void
2186 dump_type_inheritance_graph (FILE *f)
2188 unsigned int i;
2189 if (!odr_types_ptr)
2190 return;
2191 fprintf (f, "\n\nType inheritance graph:\n");
2192 for (i = 0; i < odr_types.length (); i++)
2194 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2195 dump_odr_type (f, odr_types[i]);
2197 for (i = 0; i < odr_types.length (); i++)
2199 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2201 unsigned int j;
2202 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2203 print_node (f, "", odr_types[i]->type, 0);
2204 for (j = 0; j < odr_types[i]->types->length (); j++)
2206 tree t;
2207 fprintf (f, "duplicate #%i\n", j);
2208 print_node (f, "", (*odr_types[i]->types)[j], 0);
2209 t = (*odr_types[i]->types)[j];
2210 while (TYPE_P (t) && TYPE_CONTEXT (t))
2212 t = TYPE_CONTEXT (t);
2213 print_node (f, "", t, 0);
2215 putc ('\n',f);
2221 /* Initialize IPA devirt and build inheritance tree graph. */
2223 void
2224 build_type_inheritance_graph (void)
2226 struct symtab_node *n;
2227 FILE *inheritance_dump_file;
2228 dump_flags_t flags;
2230 if (odr_hash)
2231 return;
2232 timevar_push (TV_IPA_INHERITANCE);
2233 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2234 odr_hash = new odr_hash_type (23);
2235 if (in_lto_p)
2236 odr_vtable_hash = new odr_vtable_hash_type (23);
2238 /* We reconstruct the graph starting of types of all methods seen in the
2239 unit. */
2240 FOR_EACH_SYMBOL (n)
2241 if (is_a <cgraph_node *> (n)
2242 && DECL_VIRTUAL_P (n->decl)
2243 && n->real_symbol_p ())
2244 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2246 /* Look also for virtual tables of types that do not define any methods.
2248 We need it in a case where class B has virtual base of class A
2249 re-defining its virtual method and there is class C with no virtual
2250 methods with B as virtual base.
2252 Here we output B's virtual method in two variant - for non-virtual
2253 and virtual inheritance. B's virtual table has non-virtual version,
2254 while C's has virtual.
2256 For this reason we need to know about C in order to include both
2257 variants of B. More correctly, record_target_from_binfo should
2258 add both variants of the method when walking B, but we have no
2259 link in between them.
2261 We rely on fact that either the method is exported and thus we
2262 assume it is called externally or C is in anonymous namespace and
2263 thus we will see the vtable. */
2265 else if (is_a <varpool_node *> (n)
2266 && DECL_VIRTUAL_P (n->decl)
2267 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2268 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2269 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2270 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2271 if (inheritance_dump_file)
2273 dump_type_inheritance_graph (inheritance_dump_file);
2274 dump_end (TDI_inheritance, inheritance_dump_file);
2276 timevar_pop (TV_IPA_INHERITANCE);
2279 /* Return true if N has reference from live virtual table
2280 (and thus can be a destination of polymorphic call).
2281 Be conservatively correct when callgraph is not built or
2282 if the method may be referred externally. */
2284 static bool
2285 referenced_from_vtable_p (struct cgraph_node *node)
2287 int i;
2288 struct ipa_ref *ref;
2289 bool found = false;
2291 if (node->externally_visible
2292 || DECL_EXTERNAL (node->decl)
2293 || node->used_from_other_partition)
2294 return true;
2296 /* Keep this test constant time.
2297 It is unlikely this can happen except for the case where speculative
2298 devirtualization introduced many speculative edges to this node.
2299 In this case the target is very likely alive anyway. */
2300 if (node->ref_list.referring.length () > 100)
2301 return true;
2303 /* We need references built. */
2304 if (symtab->state <= CONSTRUCTION)
2305 return true;
2307 for (i = 0; node->iterate_referring (i, ref); i++)
2308 if ((ref->use == IPA_REF_ALIAS
2309 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2310 || (ref->use == IPA_REF_ADDR
2311 && VAR_P (ref->referring->decl)
2312 && DECL_VIRTUAL_P (ref->referring->decl)))
2314 found = true;
2315 break;
2317 return found;
2320 /* Return if TARGET is cxa_pure_virtual. */
2322 static bool
2323 is_cxa_pure_virtual_p (tree target)
2325 return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
2326 && DECL_NAME (target)
2327 && id_equal (DECL_NAME (target),
2328 "__cxa_pure_virtual");
2331 /* If TARGET has associated node, record it in the NODES array.
2332 CAN_REFER specify if program can refer to the target directly.
2333 if TARGET is unknown (NULL) or it can not be inserted (for example because
2334 its body was already removed and there is no way to refer to it), clear
2335 COMPLETEP. */
2337 static void
2338 maybe_record_node (vec <cgraph_node *> &nodes,
2339 tree target, hash_set<tree> *inserted,
2340 bool can_refer,
2341 bool *completep)
2343 struct cgraph_node *target_node, *alias_target;
2344 enum availability avail;
2345 bool pure_virtual = is_cxa_pure_virtual_p (target);
2347 /* __builtin_unreachable do not need to be added into
2348 list of targets; the runtime effect of calling them is undefined.
2349 Only "real" virtual methods should be accounted. */
2350 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
2351 return;
2353 if (!can_refer)
2355 /* The only case when method of anonymous namespace becomes unreferable
2356 is when we completely optimized it out. */
2357 if (flag_ltrans
2358 || !target
2359 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2360 *completep = false;
2361 return;
2364 if (!target)
2365 return;
2367 target_node = cgraph_node::get (target);
2369 /* Prefer alias target over aliases, so we do not get confused by
2370 fake duplicates. */
2371 if (target_node)
2373 alias_target = target_node->ultimate_alias_target (&avail);
2374 if (target_node != alias_target
2375 && avail >= AVAIL_AVAILABLE
2376 && target_node->get_availability ())
2377 target_node = alias_target;
2380 /* Method can only be called by polymorphic call if any
2381 of vtables referring to it are alive.
2383 While this holds for non-anonymous functions, too, there are
2384 cases where we want to keep them in the list; for example
2385 inline functions with -fno-weak are static, but we still
2386 may devirtualize them when instance comes from other unit.
2387 The same holds for LTO.
2389 Currently we ignore these functions in speculative devirtualization.
2390 ??? Maybe it would make sense to be more aggressive for LTO even
2391 elsewhere. */
2392 if (!flag_ltrans
2393 && !pure_virtual
2394 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2395 && (!target_node
2396 || !referenced_from_vtable_p (target_node)))
2398 /* See if TARGET is useful function we can deal with. */
2399 else if (target_node != NULL
2400 && (TREE_PUBLIC (target)
2401 || DECL_EXTERNAL (target)
2402 || target_node->definition)
2403 && target_node->real_symbol_p ())
2405 gcc_assert (!target_node->global.inlined_to);
2406 gcc_assert (target_node->real_symbol_p ());
2407 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2408 by valid program. */
2409 if (flag_sanitize & SANITIZE_UNREACHABLE)
2411 /* Only add pure virtual if it is the only possible target. This way
2412 we will preserve the diagnostics about pure virtual called in many
2413 cases without disabling optimization in other. */
2414 else if (pure_virtual)
2416 if (nodes.length ())
2417 return;
2419 /* If we found a real target, take away cxa_pure_virtual. */
2420 else if (!pure_virtual && nodes.length () == 1
2421 && is_cxa_pure_virtual_p (nodes[0]->decl))
2422 nodes.pop ();
2423 if (pure_virtual && nodes.length ())
2424 return;
2425 if (!inserted->add (target))
2427 cached_polymorphic_call_targets->add (target_node);
2428 nodes.safe_push (target_node);
2431 else if (!completep)
2433 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2434 optimized out or partitioned to other unit) so we can not add it. When
2435 not sanitizing, there is nothing to do.
2436 Otherwise declare the list incomplete. */
2437 else if (pure_virtual)
2439 if (flag_sanitize & SANITIZE_UNREACHABLE)
2440 *completep = false;
2442 else if (flag_ltrans
2443 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2444 *completep = false;
2447 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2448 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2449 method in vtable and insert method to NODES array
2450 or BASES_TO_CONSIDER if this array is non-NULL.
2451 Otherwise recurse to base BINFOs.
2452 This matches what get_binfo_at_offset does, but with offset
2453 being unknown.
2455 TYPE_BINFOS is a stack of BINFOS of types with defined
2456 virtual table seen on way from class type to BINFO.
2458 MATCHED_VTABLES tracks virtual tables we already did lookup
2459 for virtual function in. INSERTED tracks nodes we already
2460 inserted.
2462 ANONYMOUS is true if BINFO is part of anonymous namespace.
2464 Clear COMPLETEP when we hit unreferable target.
2467 static void
2468 record_target_from_binfo (vec <cgraph_node *> &nodes,
2469 vec <tree> *bases_to_consider,
2470 tree binfo,
2471 tree otr_type,
2472 vec <tree> &type_binfos,
2473 HOST_WIDE_INT otr_token,
2474 tree outer_type,
2475 HOST_WIDE_INT offset,
2476 hash_set<tree> *inserted,
2477 hash_set<tree> *matched_vtables,
2478 bool anonymous,
2479 bool *completep)
2481 tree type = BINFO_TYPE (binfo);
2482 int i;
2483 tree base_binfo;
2486 if (BINFO_VTABLE (binfo))
2487 type_binfos.safe_push (binfo);
2488 if (types_same_for_odr (type, outer_type))
2490 int i;
2491 tree type_binfo = NULL;
2493 /* Look up BINFO with virtual table. For normal types it is always last
2494 binfo on stack. */
2495 for (i = type_binfos.length () - 1; i >= 0; i--)
2496 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2498 type_binfo = type_binfos[i];
2499 break;
2501 if (BINFO_VTABLE (binfo))
2502 type_binfos.pop ();
2503 /* If this is duplicated BINFO for base shared by virtual inheritance,
2504 we may not have its associated vtable. This is not a problem, since
2505 we will walk it on the other path. */
2506 if (!type_binfo)
2507 return;
2508 tree inner_binfo = get_binfo_at_offset (type_binfo,
2509 offset, otr_type);
2510 if (!inner_binfo)
2512 gcc_assert (odr_violation_reported);
2513 return;
2515 /* For types in anonymous namespace first check if the respective vtable
2516 is alive. If not, we know the type can't be called. */
2517 if (!flag_ltrans && anonymous)
2519 tree vtable = BINFO_VTABLE (inner_binfo);
2520 varpool_node *vnode;
2522 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2523 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2524 vnode = varpool_node::get (vtable);
2525 if (!vnode || !vnode->definition)
2526 return;
2528 gcc_assert (inner_binfo);
2529 if (bases_to_consider
2530 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2531 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2533 bool can_refer;
2534 tree target = gimple_get_virt_method_for_binfo (otr_token,
2535 inner_binfo,
2536 &can_refer);
2537 if (!bases_to_consider)
2538 maybe_record_node (nodes, target, inserted, can_refer, completep);
2539 /* Destructors are never called via construction vtables. */
2540 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2541 bases_to_consider->safe_push (target);
2543 return;
2546 /* Walk bases. */
2547 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2548 /* Walking bases that have no virtual method is pointless exercise. */
2549 if (polymorphic_type_binfo_p (base_binfo))
2550 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2551 type_binfos,
2552 otr_token, outer_type, offset, inserted,
2553 matched_vtables, anonymous, completep);
2554 if (BINFO_VTABLE (binfo))
2555 type_binfos.pop ();
2558 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2559 of TYPE, insert them to NODES, recurse into derived nodes.
2560 INSERTED is used to avoid duplicate insertions of methods into NODES.
2561 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2562 Clear COMPLETEP if unreferable target is found.
2564 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2565 all cases where BASE_SKIPPED is true (because the base is abstract
2566 class). */
2568 static void
2569 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2570 hash_set<tree> *inserted,
2571 hash_set<tree> *matched_vtables,
2572 tree otr_type,
2573 odr_type type,
2574 HOST_WIDE_INT otr_token,
2575 tree outer_type,
2576 HOST_WIDE_INT offset,
2577 bool *completep,
2578 vec <tree> &bases_to_consider,
2579 bool consider_construction)
2581 tree binfo = TYPE_BINFO (type->type);
2582 unsigned int i;
2583 auto_vec <tree, 8> type_binfos;
2584 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2586 /* We may need to consider types w/o instances because of possible derived
2587 types using their methods either directly or via construction vtables.
2588 We are safe to skip them when all derivations are known, since we will
2589 handle them later.
2590 This is done by recording them to BASES_TO_CONSIDER array. */
2591 if (possibly_instantiated || consider_construction)
2593 record_target_from_binfo (nodes,
2594 (!possibly_instantiated
2595 && type_all_derivations_known_p (type->type))
2596 ? &bases_to_consider : NULL,
2597 binfo, otr_type, type_binfos, otr_token,
2598 outer_type, offset,
2599 inserted, matched_vtables,
2600 type->anonymous_namespace, completep);
2602 for (i = 0; i < type->derived_types.length (); i++)
2603 possible_polymorphic_call_targets_1 (nodes, inserted,
2604 matched_vtables,
2605 otr_type,
2606 type->derived_types[i],
2607 otr_token, outer_type, offset, completep,
2608 bases_to_consider, consider_construction);
2611 /* Cache of queries for polymorphic call targets.
2613 Enumerating all call targets may get expensive when there are many
2614 polymorphic calls in the program, so we memoize all the previous
2615 queries and avoid duplicated work. */
2617 struct polymorphic_call_target_d
2619 HOST_WIDE_INT otr_token;
2620 ipa_polymorphic_call_context context;
2621 odr_type type;
2622 vec <cgraph_node *> targets;
2623 tree decl_warning;
2624 int type_warning;
2625 bool complete;
2626 bool speculative;
2629 /* Polymorphic call target cache helpers. */
2631 struct polymorphic_call_target_hasher
2632 : pointer_hash <polymorphic_call_target_d>
2634 static inline hashval_t hash (const polymorphic_call_target_d *);
2635 static inline bool equal (const polymorphic_call_target_d *,
2636 const polymorphic_call_target_d *);
2637 static inline void remove (polymorphic_call_target_d *);
2640 /* Return the computed hashcode for ODR_QUERY. */
2642 inline hashval_t
2643 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2645 inchash::hash hstate (odr_query->otr_token);
2647 hstate.add_hwi (odr_query->type->id);
2648 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2649 hstate.add_hwi (odr_query->context.offset);
2651 if (odr_query->context.speculative_outer_type)
2653 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2654 hstate.add_hwi (odr_query->context.speculative_offset);
2656 hstate.add_flag (odr_query->speculative);
2657 hstate.add_flag (odr_query->context.maybe_in_construction);
2658 hstate.add_flag (odr_query->context.maybe_derived_type);
2659 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2660 hstate.commit_flag ();
2661 return hstate.end ();
2664 /* Compare cache entries T1 and T2. */
2666 inline bool
2667 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2668 const polymorphic_call_target_d *t2)
2670 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2671 && t1->speculative == t2->speculative
2672 && t1->context.offset == t2->context.offset
2673 && t1->context.speculative_offset == t2->context.speculative_offset
2674 && t1->context.outer_type == t2->context.outer_type
2675 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2676 && t1->context.maybe_in_construction
2677 == t2->context.maybe_in_construction
2678 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2679 && (t1->context.speculative_maybe_derived_type
2680 == t2->context.speculative_maybe_derived_type));
2683 /* Remove entry in polymorphic call target cache hash. */
2685 inline void
2686 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2688 v->targets.release ();
2689 free (v);
2692 /* Polymorphic call target query cache. */
2694 typedef hash_table<polymorphic_call_target_hasher>
2695 polymorphic_call_target_hash_type;
2696 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2698 /* Destroy polymorphic call target query cache. */
2700 static void
2701 free_polymorphic_call_targets_hash ()
2703 if (cached_polymorphic_call_targets)
2705 delete polymorphic_call_target_hash;
2706 polymorphic_call_target_hash = NULL;
2707 delete cached_polymorphic_call_targets;
2708 cached_polymorphic_call_targets = NULL;
2712 /* Force rebuilding type inheritance graph from scratch.
2713 This is use to make sure that we do not keep references to types
2714 which was not visible to free_lang_data. */
2716 void
2717 rebuild_type_inheritance_graph ()
2719 if (!odr_hash)
2720 return;
2721 delete odr_hash;
2722 if (in_lto_p)
2723 delete odr_vtable_hash;
2724 odr_hash = NULL;
2725 odr_vtable_hash = NULL;
2726 odr_types_ptr = NULL;
2727 free_polymorphic_call_targets_hash ();
2730 /* When virtual function is removed, we may need to flush the cache. */
2732 static void
2733 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2735 if (cached_polymorphic_call_targets
2736 && cached_polymorphic_call_targets->contains (n))
2737 free_polymorphic_call_targets_hash ();
2740 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2742 tree
2743 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2744 tree vtable)
2746 tree v = BINFO_VTABLE (binfo);
2747 int i;
2748 tree base_binfo;
2749 unsigned HOST_WIDE_INT this_offset;
2751 if (v)
2753 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2754 gcc_unreachable ();
2756 if (offset == this_offset
2757 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2758 return binfo;
2761 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2762 if (polymorphic_type_binfo_p (base_binfo))
2764 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2765 if (base_binfo)
2766 return base_binfo;
2768 return NULL;
2771 /* T is known constant value of virtual table pointer.
2772 Store virtual table to V and its offset to OFFSET.
2773 Return false if T does not look like virtual table reference. */
2775 bool
2776 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2777 unsigned HOST_WIDE_INT *offset)
2779 /* We expect &MEM[(void *)&virtual_table + 16B].
2780 We obtain object's BINFO from the context of the virtual table.
2781 This one contains pointer to virtual table represented via
2782 POINTER_PLUS_EXPR. Verify that this pointer matches what
2783 we propagated through.
2785 In the case of virtual inheritance, the virtual tables may
2786 be nested, i.e. the offset may be different from 16 and we may
2787 need to dive into the type representation. */
2788 if (TREE_CODE (t) == ADDR_EXPR
2789 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2790 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2791 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2792 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2793 == VAR_DECL)
2794 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2795 (TREE_OPERAND (t, 0), 0), 0)))
2797 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2798 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2799 return true;
2802 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2803 We need to handle it when T comes from static variable initializer or
2804 BINFO. */
2805 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2807 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2808 t = TREE_OPERAND (t, 0);
2810 else
2811 *offset = 0;
2813 if (TREE_CODE (t) != ADDR_EXPR)
2814 return false;
2815 *v = TREE_OPERAND (t, 0);
2816 return true;
2819 /* T is known constant value of virtual table pointer. Return BINFO of the
2820 instance type. */
2822 tree
2823 vtable_pointer_value_to_binfo (const_tree t)
2825 tree vtable;
2826 unsigned HOST_WIDE_INT offset;
2828 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2829 return NULL_TREE;
2831 /* FIXME: for stores of construction vtables we return NULL,
2832 because we do not have BINFO for those. Eventually we should fix
2833 our representation to allow this case to be handled, too.
2834 In the case we see store of BINFO we however may assume
2835 that standard folding will be able to cope with it. */
2836 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2837 offset, vtable);
2840 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2841 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2842 and insert them in NODES.
2844 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2846 static void
2847 record_targets_from_bases (tree otr_type,
2848 HOST_WIDE_INT otr_token,
2849 tree outer_type,
2850 HOST_WIDE_INT offset,
2851 vec <cgraph_node *> &nodes,
2852 hash_set<tree> *inserted,
2853 hash_set<tree> *matched_vtables,
2854 bool *completep)
2856 while (true)
2858 HOST_WIDE_INT pos, size;
2859 tree base_binfo;
2860 tree fld;
2862 if (types_same_for_odr (outer_type, otr_type))
2863 return;
2865 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2867 if (TREE_CODE (fld) != FIELD_DECL)
2868 continue;
2870 pos = int_bit_position (fld);
2871 size = tree_to_shwi (DECL_SIZE (fld));
2872 if (pos <= offset && (pos + size) > offset
2873 /* Do not get confused by zero sized bases. */
2874 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2875 break;
2877 /* Within a class type we should always find corresponding fields. */
2878 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2880 /* Nonbase types should have been stripped by outer_class_type. */
2881 gcc_assert (DECL_ARTIFICIAL (fld));
2883 outer_type = TREE_TYPE (fld);
2884 offset -= pos;
2886 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2887 offset, otr_type);
2888 if (!base_binfo)
2890 gcc_assert (odr_violation_reported);
2891 return;
2893 gcc_assert (base_binfo);
2894 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2896 bool can_refer;
2897 tree target = gimple_get_virt_method_for_binfo (otr_token,
2898 base_binfo,
2899 &can_refer);
2900 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2901 maybe_record_node (nodes, target, inserted, can_refer, completep);
2902 matched_vtables->add (BINFO_VTABLE (base_binfo));
2907 /* When virtual table is removed, we may need to flush the cache. */
2909 static void
2910 devirt_variable_node_removal_hook (varpool_node *n,
2911 void *d ATTRIBUTE_UNUSED)
2913 if (cached_polymorphic_call_targets
2914 && DECL_VIRTUAL_P (n->decl)
2915 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2916 free_polymorphic_call_targets_hash ();
2919 /* Record about how many calls would benefit from given type to be final. */
2921 struct odr_type_warn_count
2923 tree type;
2924 int count;
2925 profile_count dyn_count;
2928 /* Record about how many calls would benefit from given method to be final. */
2930 struct decl_warn_count
2932 tree decl;
2933 int count;
2934 profile_count dyn_count;
2937 /* Information about type and decl warnings. */
2939 struct final_warning_record
2941 /* If needed grow type_warnings vector and initialize new decl_warn_count
2942 to have dyn_count set to profile_count::zero (). */
2943 void grow_type_warnings (unsigned newlen);
2945 profile_count dyn_count;
2946 auto_vec<odr_type_warn_count> type_warnings;
2947 hash_map<tree, decl_warn_count> decl_warnings;
2950 void
2951 final_warning_record::grow_type_warnings (unsigned newlen)
2953 unsigned len = type_warnings.length ();
2954 if (newlen > len)
2956 type_warnings.safe_grow_cleared (newlen);
2957 for (unsigned i = len; i < newlen; i++)
2958 type_warnings[i].dyn_count = profile_count::zero ();
2962 struct final_warning_record *final_warning_records;
2964 /* Return vector containing possible targets of polymorphic call of type
2965 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2966 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2967 OTR_TYPE and include their virtual method. This is useful for types
2968 possibly in construction or destruction where the virtual table may
2969 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2970 us to walk the inheritance graph for all derivations.
2972 If COMPLETEP is non-NULL, store true if the list is complete.
2973 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2974 in the target cache. If user needs to visit every target list
2975 just once, it can memoize them.
2977 If SPECULATIVE is set, the list will not contain targets that
2978 are not speculatively taken.
2980 Returned vector is placed into cache. It is NOT caller's responsibility
2981 to free it. The vector can be freed on cgraph_remove_node call if
2982 the particular node is a virtual function present in the cache. */
2984 vec <cgraph_node *>
2985 possible_polymorphic_call_targets (tree otr_type,
2986 HOST_WIDE_INT otr_token,
2987 ipa_polymorphic_call_context context,
2988 bool *completep,
2989 void **cache_token,
2990 bool speculative)
2992 static struct cgraph_node_hook_list *node_removal_hook_holder;
2993 vec <cgraph_node *> nodes = vNULL;
2994 auto_vec <tree, 8> bases_to_consider;
2995 odr_type type, outer_type;
2996 polymorphic_call_target_d key;
2997 polymorphic_call_target_d **slot;
2998 unsigned int i;
2999 tree binfo, target;
3000 bool complete;
3001 bool can_refer = false;
3002 bool skipped = false;
3004 otr_type = TYPE_MAIN_VARIANT (otr_type);
3006 /* If ODR is not initialized or the context is invalid, return empty
3007 incomplete list. */
3008 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3010 if (completep)
3011 *completep = context.invalid;
3012 if (cache_token)
3013 *cache_token = NULL;
3014 return nodes;
3017 /* Do not bother to compute speculative info when user do not asks for it. */
3018 if (!speculative || !context.speculative_outer_type)
3019 context.clear_speculation ();
3021 type = get_odr_type (otr_type, true);
3023 /* Recording type variants would waste results cache. */
3024 gcc_assert (!context.outer_type
3025 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3027 /* Look up the outer class type we want to walk.
3028 If we fail to do so, the context is invalid. */
3029 if ((context.outer_type || context.speculative_outer_type)
3030 && !context.restrict_to_inner_class (otr_type))
3032 if (completep)
3033 *completep = true;
3034 if (cache_token)
3035 *cache_token = NULL;
3036 return nodes;
3038 gcc_assert (!context.invalid);
3040 /* Check that restrict_to_inner_class kept the main variant. */
3041 gcc_assert (!context.outer_type
3042 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3044 /* We canonicalize our query, so we do not need extra hashtable entries. */
3046 /* Without outer type, we have no use for offset. Just do the
3047 basic search from inner type. */
3048 if (!context.outer_type)
3049 context.clear_outer_type (otr_type);
3050 /* We need to update our hierarchy if the type does not exist. */
3051 outer_type = get_odr_type (context.outer_type, true);
3052 /* If the type is complete, there are no derivations. */
3053 if (TYPE_FINAL_P (outer_type->type))
3054 context.maybe_derived_type = false;
3056 /* Initialize query cache. */
3057 if (!cached_polymorphic_call_targets)
3059 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3060 polymorphic_call_target_hash
3061 = new polymorphic_call_target_hash_type (23);
3062 if (!node_removal_hook_holder)
3064 node_removal_hook_holder =
3065 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3066 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3067 NULL);
3071 if (in_lto_p)
3073 if (context.outer_type != otr_type)
3074 context.outer_type
3075 = get_odr_type (context.outer_type, true)->type;
3076 if (context.speculative_outer_type)
3077 context.speculative_outer_type
3078 = get_odr_type (context.speculative_outer_type, true)->type;
3081 /* Look up cached answer. */
3082 key.type = type;
3083 key.otr_token = otr_token;
3084 key.speculative = speculative;
3085 key.context = context;
3086 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3087 if (cache_token)
3088 *cache_token = (void *)*slot;
3089 if (*slot)
3091 if (completep)
3092 *completep = (*slot)->complete;
3093 if ((*slot)->type_warning && final_warning_records)
3095 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3096 if (!final_warning_records->type_warnings
3097 [(*slot)->type_warning - 1].dyn_count.initialized_p ())
3098 final_warning_records->type_warnings
3099 [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
3100 if (final_warning_records->dyn_count > 0)
3101 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3102 = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3103 + final_warning_records->dyn_count;
3105 if (!speculative && (*slot)->decl_warning && final_warning_records)
3107 struct decl_warn_count *c =
3108 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3109 c->count++;
3110 if (final_warning_records->dyn_count > 0)
3111 c->dyn_count += final_warning_records->dyn_count;
3113 return (*slot)->targets;
3116 complete = true;
3118 /* Do actual search. */
3119 timevar_push (TV_IPA_VIRTUAL_CALL);
3120 *slot = XCNEW (polymorphic_call_target_d);
3121 if (cache_token)
3122 *cache_token = (void *)*slot;
3123 (*slot)->type = type;
3124 (*slot)->otr_token = otr_token;
3125 (*slot)->context = context;
3126 (*slot)->speculative = speculative;
3128 hash_set<tree> inserted;
3129 hash_set<tree> matched_vtables;
3131 /* First insert targets we speculatively identified as likely. */
3132 if (context.speculative_outer_type)
3134 odr_type speculative_outer_type;
3135 bool speculation_complete = true;
3137 /* First insert target from type itself and check if it may have
3138 derived types. */
3139 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3140 if (TYPE_FINAL_P (speculative_outer_type->type))
3141 context.speculative_maybe_derived_type = false;
3142 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3143 context.speculative_offset, otr_type);
3144 if (binfo)
3145 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3146 &can_refer);
3147 else
3148 target = NULL;
3150 /* In the case we get complete method, we don't need
3151 to walk derivations. */
3152 if (target && DECL_FINAL_P (target))
3153 context.speculative_maybe_derived_type = false;
3154 if (type_possibly_instantiated_p (speculative_outer_type->type))
3155 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3156 if (binfo)
3157 matched_vtables.add (BINFO_VTABLE (binfo));
3160 /* Next walk recursively all derived types. */
3161 if (context.speculative_maybe_derived_type)
3162 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3163 possible_polymorphic_call_targets_1 (nodes, &inserted,
3164 &matched_vtables,
3165 otr_type,
3166 speculative_outer_type->derived_types[i],
3167 otr_token, speculative_outer_type->type,
3168 context.speculative_offset,
3169 &speculation_complete,
3170 bases_to_consider,
3171 false);
3174 if (!speculative || !nodes.length ())
3176 /* First see virtual method of type itself. */
3177 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3178 context.offset, otr_type);
3179 if (binfo)
3180 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3181 &can_refer);
3182 else
3184 gcc_assert (odr_violation_reported);
3185 target = NULL;
3188 /* Destructors are never called through construction virtual tables,
3189 because the type is always known. */
3190 if (target && DECL_CXX_DESTRUCTOR_P (target))
3191 context.maybe_in_construction = false;
3193 if (target)
3195 /* In the case we get complete method, we don't need
3196 to walk derivations. */
3197 if (DECL_FINAL_P (target))
3198 context.maybe_derived_type = false;
3201 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3202 if (type_possibly_instantiated_p (outer_type->type))
3203 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3204 else
3205 skipped = true;
3207 if (binfo)
3208 matched_vtables.add (BINFO_VTABLE (binfo));
3210 /* Next walk recursively all derived types. */
3211 if (context.maybe_derived_type)
3213 for (i = 0; i < outer_type->derived_types.length(); i++)
3214 possible_polymorphic_call_targets_1 (nodes, &inserted,
3215 &matched_vtables,
3216 otr_type,
3217 outer_type->derived_types[i],
3218 otr_token, outer_type->type,
3219 context.offset, &complete,
3220 bases_to_consider,
3221 context.maybe_in_construction);
3223 if (!outer_type->all_derivations_known)
3225 if (!speculative && final_warning_records
3226 && nodes.length () == 1
3227 && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
3229 if (complete
3230 && warn_suggest_final_types
3231 && !outer_type->derived_types.length ())
3233 final_warning_records->grow_type_warnings
3234 (outer_type->id);
3235 final_warning_records->type_warnings[outer_type->id].count++;
3236 if (!final_warning_records->type_warnings
3237 [outer_type->id].dyn_count.initialized_p ())
3238 final_warning_records->type_warnings
3239 [outer_type->id].dyn_count = profile_count::zero ();
3240 final_warning_records->type_warnings[outer_type->id].dyn_count
3241 += final_warning_records->dyn_count;
3242 final_warning_records->type_warnings[outer_type->id].type
3243 = outer_type->type;
3244 (*slot)->type_warning = outer_type->id + 1;
3246 if (complete
3247 && warn_suggest_final_methods
3248 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3249 outer_type->type))
3251 bool existed;
3252 struct decl_warn_count &c =
3253 final_warning_records->decl_warnings.get_or_insert
3254 (nodes[0]->decl, &existed);
3256 if (existed)
3258 c.count++;
3259 c.dyn_count += final_warning_records->dyn_count;
3261 else
3263 c.count = 1;
3264 c.dyn_count = final_warning_records->dyn_count;
3265 c.decl = nodes[0]->decl;
3267 (*slot)->decl_warning = nodes[0]->decl;
3270 complete = false;
3274 if (!speculative)
3276 /* Destructors are never called through construction virtual tables,
3277 because the type is always known. One of entries may be
3278 cxa_pure_virtual so look to at least two of them. */
3279 if (context.maybe_in_construction)
3280 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3281 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3282 context.maybe_in_construction = false;
3283 if (context.maybe_in_construction)
3285 if (type != outer_type
3286 && (!skipped
3287 || (context.maybe_derived_type
3288 && !type_all_derivations_known_p (outer_type->type))))
3289 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3290 context.offset, nodes, &inserted,
3291 &matched_vtables, &complete);
3292 if (skipped)
3293 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3294 for (i = 0; i < bases_to_consider.length(); i++)
3295 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3300 (*slot)->targets = nodes;
3301 (*slot)->complete = complete;
3302 if (completep)
3303 *completep = complete;
3305 timevar_pop (TV_IPA_VIRTUAL_CALL);
3306 return nodes;
3309 bool
3310 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3311 vec<const decl_warn_count*> *vec)
3313 vec->safe_push (&value);
3314 return true;
3317 /* Dump target list TARGETS into FILE. */
3319 static void
3320 dump_targets (FILE *f, vec <cgraph_node *> targets)
3322 unsigned int i;
3324 for (i = 0; i < targets.length (); i++)
3326 char *name = NULL;
3327 if (in_lto_p)
3328 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3329 fprintf (f, " %s/%i", name ? name : targets[i]->name (),
3330 targets[i]->order);
3331 if (in_lto_p)
3332 free (name);
3333 if (!targets[i]->definition)
3334 fprintf (f, " (no definition%s)",
3335 DECL_DECLARED_INLINE_P (targets[i]->decl)
3336 ? " inline" : "");
3338 fprintf (f, "\n");
3341 /* Dump all possible targets of a polymorphic call. */
3343 void
3344 dump_possible_polymorphic_call_targets (FILE *f,
3345 tree otr_type,
3346 HOST_WIDE_INT otr_token,
3347 const ipa_polymorphic_call_context &ctx)
3349 vec <cgraph_node *> targets;
3350 bool final;
3351 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3352 unsigned int len;
3354 if (!type)
3355 return;
3356 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3357 ctx,
3358 &final, NULL, false);
3359 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3360 print_generic_expr (f, type->type, TDF_SLIM);
3361 fprintf (f, " token %i\n", (int)otr_token);
3363 ctx.dump (f);
3365 fprintf (f, " %s%s%s%s\n ",
3366 final ? "This is a complete list." :
3367 "This is partial list; extra targets may be defined in other units.",
3368 ctx.maybe_in_construction ? " (base types included)" : "",
3369 ctx.maybe_derived_type ? " (derived types included)" : "",
3370 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3371 len = targets.length ();
3372 dump_targets (f, targets);
3374 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3375 ctx,
3376 &final, NULL, true);
3377 if (targets.length () != len)
3379 fprintf (f, " Speculative targets:");
3380 dump_targets (f, targets);
3382 /* Ugly: during callgraph construction the target cache may get populated
3383 before all targets are found. While this is harmless (because all local
3384 types are discovered and only in those case we devirtualize fully and we
3385 don't do speculative devirtualization before IPA stage) it triggers
3386 assert here when dumping at that stage also populates the case with
3387 speculative targets. Quietly ignore this. */
3388 gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
3389 fprintf (f, "\n");
3393 /* Return true if N can be possibly target of a polymorphic call of
3394 OTR_TYPE/OTR_TOKEN. */
3396 bool
3397 possible_polymorphic_call_target_p (tree otr_type,
3398 HOST_WIDE_INT otr_token,
3399 const ipa_polymorphic_call_context &ctx,
3400 struct cgraph_node *n)
3402 vec <cgraph_node *> targets;
3403 unsigned int i;
3404 enum built_in_function fcode;
3405 bool final;
3407 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3408 && ((fcode = DECL_FUNCTION_CODE (n->decl)) == BUILT_IN_UNREACHABLE
3409 || fcode == BUILT_IN_TRAP))
3410 return true;
3412 if (is_cxa_pure_virtual_p (n->decl))
3413 return true;
3415 if (!odr_hash)
3416 return true;
3417 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3418 for (i = 0; i < targets.length (); i++)
3419 if (n->semantically_equivalent_p (targets[i]))
3420 return true;
3422 /* At a moment we allow middle end to dig out new external declarations
3423 as a targets of polymorphic calls. */
3424 if (!final && !n->definition)
3425 return true;
3426 return false;
3431 /* Return true if N can be possibly target of a polymorphic call of
3432 OBJ_TYPE_REF expression REF in STMT. */
3434 bool
3435 possible_polymorphic_call_target_p (tree ref,
3436 gimple *stmt,
3437 struct cgraph_node *n)
3439 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3440 tree call_fn = gimple_call_fn (stmt);
3442 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3443 tree_to_uhwi
3444 (OBJ_TYPE_REF_TOKEN (call_fn)),
3445 context,
3450 /* After callgraph construction new external nodes may appear.
3451 Add them into the graph. */
3453 void
3454 update_type_inheritance_graph (void)
3456 struct cgraph_node *n;
3458 if (!odr_hash)
3459 return;
3460 free_polymorphic_call_targets_hash ();
3461 timevar_push (TV_IPA_INHERITANCE);
3462 /* We reconstruct the graph starting from types of all methods seen in the
3463 unit. */
3464 FOR_EACH_FUNCTION (n)
3465 if (DECL_VIRTUAL_P (n->decl)
3466 && !n->definition
3467 && n->real_symbol_p ())
3468 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3469 timevar_pop (TV_IPA_INHERITANCE);
3473 /* Return true if N looks like likely target of a polymorphic call.
3474 Rule out cxa_pure_virtual, noreturns, function declared cold and
3475 other obvious cases. */
3477 bool
3478 likely_target_p (struct cgraph_node *n)
3480 int flags;
3481 /* cxa_pure_virtual and similar things are not likely. */
3482 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3483 return false;
3484 flags = flags_from_decl_or_type (n->decl);
3485 if (flags & ECF_NORETURN)
3486 return false;
3487 if (lookup_attribute ("cold",
3488 DECL_ATTRIBUTES (n->decl)))
3489 return false;
3490 if (n->frequency < NODE_FREQUENCY_NORMAL)
3491 return false;
3492 /* If there are no live virtual tables referring the target,
3493 the only way the target can be called is an instance coming from other
3494 compilation unit; speculative devirtualization is built around an
3495 assumption that won't happen. */
3496 if (!referenced_from_vtable_p (n))
3497 return false;
3498 return true;
3501 /* Compare type warning records P1 and P2 and choose one with larger count;
3502 helper for qsort. */
3505 type_warning_cmp (const void *p1, const void *p2)
3507 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3508 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3510 if (t1->dyn_count < t2->dyn_count)
3511 return 1;
3512 if (t1->dyn_count > t2->dyn_count)
3513 return -1;
3514 return t2->count - t1->count;
3517 /* Compare decl warning records P1 and P2 and choose one with larger count;
3518 helper for qsort. */
3521 decl_warning_cmp (const void *p1, const void *p2)
3523 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3524 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3526 if (t1->dyn_count < t2->dyn_count)
3527 return 1;
3528 if (t1->dyn_count > t2->dyn_count)
3529 return -1;
3530 return t2->count - t1->count;
3534 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3535 context CTX. */
3537 struct cgraph_node *
3538 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3539 ipa_polymorphic_call_context ctx)
3541 vec <cgraph_node *>targets
3542 = possible_polymorphic_call_targets
3543 (otr_type, otr_token, ctx, NULL, NULL, true);
3544 unsigned int i;
3545 struct cgraph_node *likely_target = NULL;
3547 for (i = 0; i < targets.length (); i++)
3548 if (likely_target_p (targets[i]))
3550 if (likely_target)
3551 return NULL;
3552 likely_target = targets[i];
3554 if (!likely_target
3555 ||!likely_target->definition
3556 || DECL_EXTERNAL (likely_target->decl))
3557 return NULL;
3559 /* Don't use an implicitly-declared destructor (c++/58678). */
3560 struct cgraph_node *non_thunk_target
3561 = likely_target->function_symbol ();
3562 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3563 return NULL;
3564 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3565 && likely_target->can_be_discarded_p ())
3566 return NULL;
3567 return likely_target;
3570 /* The ipa-devirt pass.
3571 When polymorphic call has only one likely target in the unit,
3572 turn it into a speculative call. */
3574 static unsigned int
3575 ipa_devirt (void)
3577 struct cgraph_node *n;
3578 hash_set<void *> bad_call_targets;
3579 struct cgraph_edge *e;
3581 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3582 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3583 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3584 int ndropped = 0;
3586 if (!odr_types_ptr)
3587 return 0;
3589 if (dump_file)
3590 dump_type_inheritance_graph (dump_file);
3592 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3593 This is implemented by setting up final_warning_records that are updated
3594 by get_polymorphic_call_targets.
3595 We need to clear cache in this case to trigger recomputation of all
3596 entries. */
3597 if (warn_suggest_final_methods || warn_suggest_final_types)
3599 final_warning_records = new (final_warning_record);
3600 final_warning_records->dyn_count = profile_count::zero ();
3601 final_warning_records->grow_type_warnings (odr_types.length ());
3602 free_polymorphic_call_targets_hash ();
3605 FOR_EACH_DEFINED_FUNCTION (n)
3607 bool update = false;
3608 if (!opt_for_fn (n->decl, flag_devirtualize))
3609 continue;
3610 if (dump_file && n->indirect_calls)
3611 fprintf (dump_file, "\n\nProcesing function %s\n",
3612 n->dump_name ());
3613 for (e = n->indirect_calls; e; e = e->next_callee)
3614 if (e->indirect_info->polymorphic)
3616 struct cgraph_node *likely_target = NULL;
3617 void *cache_token;
3618 bool final;
3620 if (final_warning_records)
3621 final_warning_records->dyn_count = e->count.ipa ();
3623 vec <cgraph_node *>targets
3624 = possible_polymorphic_call_targets
3625 (e, &final, &cache_token, true);
3626 unsigned int i;
3628 /* Trigger warnings by calculating non-speculative targets. */
3629 if (warn_suggest_final_methods || warn_suggest_final_types)
3630 possible_polymorphic_call_targets (e);
3632 if (dump_file)
3633 dump_possible_polymorphic_call_targets
3634 (dump_file, e);
3636 npolymorphic++;
3638 /* See if the call can be devirtualized by means of ipa-prop's
3639 polymorphic call context propagation. If not, we can just
3640 forget about this call being polymorphic and avoid some heavy
3641 lifting in remove_unreachable_nodes that will otherwise try to
3642 keep all possible targets alive until inlining and in the inliner
3643 itself.
3645 This may need to be revisited once we add further ways to use
3646 the may edges, but it is a resonable thing to do right now. */
3648 if ((e->indirect_info->param_index == -1
3649 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3650 && e->indirect_info->vptr_changed))
3651 && !flag_ltrans_devirtualize)
3653 e->indirect_info->polymorphic = false;
3654 ndropped++;
3655 if (dump_file)
3656 fprintf (dump_file, "Dropping polymorphic call info;"
3657 " it can not be used by ipa-prop\n");
3660 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3661 continue;
3663 if (!e->maybe_hot_p ())
3665 if (dump_file)
3666 fprintf (dump_file, "Call is cold\n\n");
3667 ncold++;
3668 continue;
3670 if (e->speculative)
3672 if (dump_file)
3673 fprintf (dump_file, "Call is already speculated\n\n");
3674 nspeculated++;
3676 /* When dumping see if we agree with speculation. */
3677 if (!dump_file)
3678 continue;
3680 if (bad_call_targets.contains (cache_token))
3682 if (dump_file)
3683 fprintf (dump_file, "Target list is known to be useless\n\n");
3684 nmultiple++;
3685 continue;
3687 for (i = 0; i < targets.length (); i++)
3688 if (likely_target_p (targets[i]))
3690 if (likely_target)
3692 likely_target = NULL;
3693 if (dump_file)
3694 fprintf (dump_file, "More than one likely target\n\n");
3695 nmultiple++;
3696 break;
3698 likely_target = targets[i];
3700 if (!likely_target)
3702 bad_call_targets.add (cache_token);
3703 continue;
3705 /* This is reached only when dumping; check if we agree or disagree
3706 with the speculation. */
3707 if (e->speculative)
3709 struct cgraph_edge *e2;
3710 struct ipa_ref *ref;
3711 e->speculative_call_info (e2, e, ref);
3712 if (e2->callee->ultimate_alias_target ()
3713 == likely_target->ultimate_alias_target ())
3715 fprintf (dump_file, "We agree with speculation\n\n");
3716 nok++;
3718 else
3720 fprintf (dump_file, "We disagree with speculation\n\n");
3721 nwrong++;
3723 continue;
3725 if (!likely_target->definition)
3727 if (dump_file)
3728 fprintf (dump_file, "Target is not a definition\n\n");
3729 nnotdefined++;
3730 continue;
3732 /* Do not introduce new references to external symbols. While we
3733 can handle these just well, it is common for programs to
3734 incorrectly with headers defining methods they are linked
3735 with. */
3736 if (DECL_EXTERNAL (likely_target->decl))
3738 if (dump_file)
3739 fprintf (dump_file, "Target is external\n\n");
3740 nexternal++;
3741 continue;
3743 /* Don't use an implicitly-declared destructor (c++/58678). */
3744 struct cgraph_node *non_thunk_target
3745 = likely_target->function_symbol ();
3746 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3748 if (dump_file)
3749 fprintf (dump_file, "Target is artificial\n\n");
3750 nartificial++;
3751 continue;
3753 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3754 && likely_target->can_be_discarded_p ())
3756 if (dump_file)
3757 fprintf (dump_file, "Target is overwritable\n\n");
3758 noverwritable++;
3759 continue;
3761 else if (dbg_cnt (devirt))
3763 if (dump_enabled_p ())
3765 location_t locus = gimple_location_safe (e->call_stmt);
3766 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3767 "speculatively devirtualizing call "
3768 "in %s to %s\n",
3769 n->dump_name (),
3770 likely_target->dump_name ());
3772 if (!likely_target->can_be_discarded_p ())
3774 cgraph_node *alias;
3775 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3776 if (alias)
3777 likely_target = alias;
3779 nconverted++;
3780 update = true;
3781 e->make_speculative
3782 (likely_target, e->count.apply_scale (8, 10));
3785 if (update)
3786 ipa_update_overall_fn_summary (n);
3788 if (warn_suggest_final_methods || warn_suggest_final_types)
3790 if (warn_suggest_final_types)
3792 final_warning_records->type_warnings.qsort (type_warning_cmp);
3793 for (unsigned int i = 0;
3794 i < final_warning_records->type_warnings.length (); i++)
3795 if (final_warning_records->type_warnings[i].count)
3797 tree type = final_warning_records->type_warnings[i].type;
3798 int count = final_warning_records->type_warnings[i].count;
3799 profile_count dyn_count
3800 = final_warning_records->type_warnings[i].dyn_count;
3802 if (!(dyn_count > 0))
3803 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3804 OPT_Wsuggest_final_types, count,
3805 "Declaring type %qD final "
3806 "would enable devirtualization of %i call",
3807 "Declaring type %qD final "
3808 "would enable devirtualization of %i calls",
3809 type,
3810 count);
3811 else
3812 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3813 OPT_Wsuggest_final_types, count,
3814 "Declaring type %qD final "
3815 "would enable devirtualization of %i call "
3816 "executed %lli times",
3817 "Declaring type %qD final "
3818 "would enable devirtualization of %i calls "
3819 "executed %lli times",
3820 type,
3821 count,
3822 (long long) dyn_count.to_gcov_type ());
3826 if (warn_suggest_final_methods)
3828 auto_vec<const decl_warn_count*> decl_warnings_vec;
3830 final_warning_records->decl_warnings.traverse
3831 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3832 decl_warnings_vec.qsort (decl_warning_cmp);
3833 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3835 tree decl = decl_warnings_vec[i]->decl;
3836 int count = decl_warnings_vec[i]->count;
3837 profile_count dyn_count
3838 = decl_warnings_vec[i]->dyn_count;
3840 if (!(dyn_count > 0))
3841 if (DECL_CXX_DESTRUCTOR_P (decl))
3842 warning_n (DECL_SOURCE_LOCATION (decl),
3843 OPT_Wsuggest_final_methods, count,
3844 "Declaring virtual destructor of %qD final "
3845 "would enable devirtualization of %i call",
3846 "Declaring virtual destructor of %qD final "
3847 "would enable devirtualization of %i calls",
3848 DECL_CONTEXT (decl), count);
3849 else
3850 warning_n (DECL_SOURCE_LOCATION (decl),
3851 OPT_Wsuggest_final_methods, count,
3852 "Declaring method %qD final "
3853 "would enable devirtualization of %i call",
3854 "Declaring method %qD final "
3855 "would enable devirtualization of %i calls",
3856 decl, count);
3857 else if (DECL_CXX_DESTRUCTOR_P (decl))
3858 warning_n (DECL_SOURCE_LOCATION (decl),
3859 OPT_Wsuggest_final_methods, count,
3860 "Declaring virtual destructor of %qD final "
3861 "would enable devirtualization of %i call "
3862 "executed %lli times",
3863 "Declaring virtual destructor of %qD final "
3864 "would enable devirtualization of %i calls "
3865 "executed %lli times",
3866 DECL_CONTEXT (decl), count,
3867 (long long)dyn_count.to_gcov_type ());
3868 else
3869 warning_n (DECL_SOURCE_LOCATION (decl),
3870 OPT_Wsuggest_final_methods, count,
3871 "Declaring method %qD final "
3872 "would enable devirtualization of %i call "
3873 "executed %lli times",
3874 "Declaring method %qD final "
3875 "would enable devirtualization of %i calls "
3876 "executed %lli times",
3877 decl, count,
3878 (long long)dyn_count.to_gcov_type ());
3882 delete (final_warning_records);
3883 final_warning_records = 0;
3886 if (dump_file)
3887 fprintf (dump_file,
3888 "%i polymorphic calls, %i devirtualized,"
3889 " %i speculatively devirtualized, %i cold\n"
3890 "%i have multiple targets, %i overwritable,"
3891 " %i already speculated (%i agree, %i disagree),"
3892 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3893 npolymorphic, ndevirtualized, nconverted, ncold,
3894 nmultiple, noverwritable, nspeculated, nok, nwrong,
3895 nexternal, nnotdefined, nartificial, ndropped);
3896 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3899 namespace {
3901 const pass_data pass_data_ipa_devirt =
3903 IPA_PASS, /* type */
3904 "devirt", /* name */
3905 OPTGROUP_NONE, /* optinfo_flags */
3906 TV_IPA_DEVIRT, /* tv_id */
3907 0, /* properties_required */
3908 0, /* properties_provided */
3909 0, /* properties_destroyed */
3910 0, /* todo_flags_start */
3911 ( TODO_dump_symtab ), /* todo_flags_finish */
3914 class pass_ipa_devirt : public ipa_opt_pass_d
3916 public:
3917 pass_ipa_devirt (gcc::context *ctxt)
3918 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3919 NULL, /* generate_summary */
3920 NULL, /* write_summary */
3921 NULL, /* read_summary */
3922 NULL, /* write_optimization_summary */
3923 NULL, /* read_optimization_summary */
3924 NULL, /* stmt_fixup */
3925 0, /* function_transform_todo_flags_start */
3926 NULL, /* function_transform */
3927 NULL) /* variable_transform */
3930 /* opt_pass methods: */
3931 virtual bool gate (function *)
3933 /* In LTO, always run the IPA passes and decide on function basis if the
3934 pass is enabled. */
3935 if (in_lto_p)
3936 return true;
3937 return (flag_devirtualize
3938 && (flag_devirtualize_speculatively
3939 || (warn_suggest_final_methods
3940 || warn_suggest_final_types))
3941 && optimize);
3944 virtual unsigned int execute (function *) { return ipa_devirt (); }
3946 }; // class pass_ipa_devirt
3948 } // anon namespace
3950 ipa_opt_pass_d *
3951 make_pass_ipa_devirt (gcc::context *ctxt)
3953 return new pass_ipa_devirt (ctxt);
3956 #include "gt-ipa-devirt.h"