2018-11-09 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-devirt.c
blob4676bdbdf93806a30cb28472fe050df312a5373d
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);
178 static void warn_odr (tree t1, tree t2, tree st1, tree st2,
179 bool warn, bool *warned, const char *reason);
181 static bool odr_violation_reported = false;
184 /* Pointer set of all call targets appearing in the cache. */
185 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
187 /* The node of type inheritance graph. For each type unique in
188 One Definition Rule (ODR) sense, we produce one node linking all
189 main variants of types equivalent to it, bases and derived types. */
191 struct GTY(()) odr_type_d
193 /* leader type. */
194 tree type;
195 /* All bases; built only for main variants of types. */
196 vec<odr_type> GTY((skip)) bases;
197 /* All derived types with virtual methods seen in unit;
198 built only for main variants of types. */
199 vec<odr_type> GTY((skip)) derived_types;
201 /* All equivalent types, if more than one. */
202 vec<tree, va_gc> *types;
203 /* Set of all equivalent types, if NON-NULL. */
204 hash_set<tree> * GTY((skip)) types_set;
206 /* Unique ID indexing the type in odr_types array. */
207 int id;
208 /* Is it in anonymous namespace? */
209 bool anonymous_namespace;
210 /* Do we know about all derivations of given type? */
211 bool all_derivations_known;
212 /* Did we report ODR violation here? */
213 bool odr_violated;
214 /* Set when virtual table without RTTI previaled table with. */
215 bool rtti_broken;
218 /* Return TRUE if all derived types of T are known and thus
219 we may consider the walk of derived type complete.
221 This is typically true only for final anonymous namespace types and types
222 defined within functions (that may be COMDAT and thus shared across units,
223 but with the same set of derived types). */
225 bool
226 type_all_derivations_known_p (const_tree t)
228 if (TYPE_FINAL_P (t))
229 return true;
230 if (flag_ltrans)
231 return false;
232 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
233 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
234 return true;
235 if (type_in_anonymous_namespace_p (t))
236 return true;
237 return (decl_function_context (TYPE_NAME (t)) != NULL);
240 /* Return TRUE if type's constructors are all visible. */
242 static bool
243 type_all_ctors_visible_p (tree t)
245 return !flag_ltrans
246 && symtab->state >= CONSTRUCTION
247 /* We can not always use type_all_derivations_known_p.
248 For function local types we must assume case where
249 the function is COMDAT and shared in between units.
251 TODO: These cases are quite easy to get, but we need
252 to keep track of C++ privatizing via -Wno-weak
253 as well as the IPA privatizing. */
254 && type_in_anonymous_namespace_p (t);
257 /* Return TRUE if type may have instance. */
259 static bool
260 type_possibly_instantiated_p (tree t)
262 tree vtable;
263 varpool_node *vnode;
265 /* TODO: Add abstract types here. */
266 if (!type_all_ctors_visible_p (t))
267 return true;
269 vtable = BINFO_VTABLE (TYPE_BINFO (t));
270 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
271 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
272 vnode = varpool_node::get (vtable);
273 return vnode && vnode->definition;
276 /* Hash used to unify ODR types based on their mangled name and for anonymous
277 namespace types. */
279 struct odr_name_hasher : pointer_hash <odr_type_d>
281 typedef union tree_node *compare_type;
282 static inline hashval_t hash (const odr_type_d *);
283 static inline bool equal (const odr_type_d *, const tree_node *);
284 static inline void remove (odr_type_d *);
287 /* Has used to unify ODR types based on their associated virtual table.
288 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
289 only polymorphic types. Types with mangled names are inserted to both. */
291 struct odr_vtable_hasher:odr_name_hasher
293 static inline hashval_t hash (const odr_type_d *);
294 static inline bool equal (const odr_type_d *, const tree_node *);
297 static bool
298 can_be_name_hashed_p (tree t)
300 return (!in_lto_p || odr_type_p (t));
303 /* Hash type by its ODR name. */
305 static hashval_t
306 hash_odr_name (const_tree t)
308 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
310 /* If not in LTO, all main variants are unique, so we can do
311 pointer hash. */
312 if (!in_lto_p)
313 return htab_hash_pointer (t);
315 /* Anonymous types are unique. */
316 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
317 return htab_hash_pointer (t);
319 gcc_checking_assert (TYPE_NAME (t)
320 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
321 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
324 /* Return the computed hashcode for ODR_TYPE. */
326 inline hashval_t
327 odr_name_hasher::hash (const odr_type_d *odr_type)
329 return hash_odr_name (odr_type->type);
332 static bool
333 can_be_vtable_hashed_p (tree t)
335 /* vtable hashing can distinguish only main variants. */
336 if (TYPE_MAIN_VARIANT (t) != t)
337 return false;
338 /* Anonymous namespace types are always handled by name hash. */
339 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
340 return false;
341 return (TREE_CODE (t) == RECORD_TYPE
342 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
345 /* Hash type by assembler name of its vtable. */
347 static hashval_t
348 hash_odr_vtable (const_tree t)
350 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
351 inchash::hash hstate;
353 gcc_checking_assert (in_lto_p);
354 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
355 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
356 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
357 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
359 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
361 add_expr (TREE_OPERAND (v, 1), hstate);
362 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
365 hstate.add_hwi (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
366 return hstate.end ();
369 /* Return the computed hashcode for ODR_TYPE. */
371 inline hashval_t
372 odr_vtable_hasher::hash (const odr_type_d *odr_type)
374 return hash_odr_vtable (odr_type->type);
377 /* For languages with One Definition Rule, work out if
378 types are the same based on their name.
380 This is non-trivial for LTO where minor differences in
381 the type representation may have prevented type merging
382 to merge two copies of otherwise equivalent type.
384 Until we start streaming mangled type names, this function works
385 only for polymorphic types.
388 bool
389 types_same_for_odr (const_tree type1, const_tree type2)
391 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
393 type1 = TYPE_MAIN_VARIANT (type1);
394 type2 = TYPE_MAIN_VARIANT (type2);
396 if (type1 == type2)
397 return true;
399 if (!in_lto_p)
400 return false;
402 /* Anonymous namespace types are never duplicated. */
403 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
404 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
405 return false;
408 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
410 Ideally we should never need types without ODR names here. It can however
411 happen in two cases:
413 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
414 Here testing for equivalence is safe, since their MAIN_VARIANTs are
415 unique.
416 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
417 establish precise ODR equivalency, but for correctness we care only
418 about equivalency on complete polymorphic types. For these we can
419 compare assembler names of their virtual tables. */
420 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
421 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
423 /* See if types are obviously different (i.e. different codes
424 or polymorphic wrt non-polymorphic). This is not strictly correct
425 for ODR violating programs, but we can't do better without streaming
426 ODR names. */
427 if (TREE_CODE (type1) != TREE_CODE (type2))
428 return false;
429 if (TREE_CODE (type1) == RECORD_TYPE
430 && (TYPE_BINFO (type1) == NULL_TREE)
431 != (TYPE_BINFO (type2) == NULL_TREE))
432 return false;
433 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
434 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
435 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
436 return false;
438 /* At the moment we have no way to establish ODR equivalence at LTO
439 other than comparing virtual table pointers of polymorphic types.
440 Eventually we should start saving mangled names in TYPE_NAME.
441 Then this condition will become non-trivial. */
443 if (TREE_CODE (type1) == RECORD_TYPE
444 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
445 && BINFO_VTABLE (TYPE_BINFO (type1))
446 && BINFO_VTABLE (TYPE_BINFO (type2)))
448 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
449 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
450 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
451 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
452 return (operand_equal_p (TREE_OPERAND (v1, 1),
453 TREE_OPERAND (v2, 1), 0)
454 && DECL_ASSEMBLER_NAME
455 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
456 == DECL_ASSEMBLER_NAME
457 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
459 gcc_unreachable ();
461 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
462 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
465 /* Return true if we can decide on ODR equivalency.
467 In non-LTO it is always decide, in LTO however it depends in the type has
468 ODR info attached. */
470 bool
471 types_odr_comparable (tree t1, tree t2)
473 return (!in_lto_p
474 || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
475 || (odr_type_p (TYPE_MAIN_VARIANT (t1))
476 && odr_type_p (TYPE_MAIN_VARIANT (t2)))
477 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
478 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
479 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
480 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
483 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
484 known, be conservative and return false. */
486 bool
487 types_must_be_same_for_odr (tree t1, tree t2)
489 if (types_odr_comparable (t1, t2))
490 return types_same_for_odr (t1, t2);
491 else
492 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
495 /* If T is compound type, return type it is based on. */
497 static tree
498 compound_type_base (const_tree t)
500 if (TREE_CODE (t) == ARRAY_TYPE
501 || POINTER_TYPE_P (t)
502 || TREE_CODE (t) == COMPLEX_TYPE
503 || VECTOR_TYPE_P (t))
504 return TREE_TYPE (t);
505 if (TREE_CODE (t) == METHOD_TYPE)
506 return TYPE_METHOD_BASETYPE (t);
507 if (TREE_CODE (t) == OFFSET_TYPE)
508 return TYPE_OFFSET_BASETYPE (t);
509 return NULL_TREE;
512 /* Return true if T is either ODR type or compound type based from it.
513 If the function return true, we know that T is a type originating from C++
514 source even at link-time. */
516 bool
517 odr_or_derived_type_p (const_tree t)
521 if (odr_type_p (TYPE_MAIN_VARIANT (t)))
522 return true;
523 /* Function type is a tricky one. Basically we can consider it
524 ODR derived if return type or any of the parameters is.
525 We need to check all parameters because LTO streaming merges
526 common types (such as void) and they are not considered ODR then. */
527 if (TREE_CODE (t) == FUNCTION_TYPE)
529 if (TYPE_METHOD_BASETYPE (t))
530 t = TYPE_METHOD_BASETYPE (t);
531 else
533 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
534 return true;
535 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
536 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t))))
537 return true;
538 return false;
541 else
542 t = compound_type_base (t);
544 while (t);
545 return t;
548 /* Compare types T1 and T2 and return true if they are
549 equivalent. */
551 inline bool
552 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
554 tree t1 = o1->type;
556 gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
557 gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
558 if (t1 == t2)
559 return true;
560 if (!in_lto_p)
561 return false;
562 /* Check for anonymous namespaces. */
563 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
564 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
565 return false;
566 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
567 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
568 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
569 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
572 /* Compare types T1 and T2 and return true if they are
573 equivalent. */
575 inline bool
576 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
578 tree t1 = o1->type;
580 gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
581 gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
582 gcc_checking_assert (in_lto_p);
583 t1 = TYPE_MAIN_VARIANT (t1);
584 t2 = TYPE_MAIN_VARIANT (t2);
585 if (t1 == t2)
586 return true;
587 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
588 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
589 return (operand_equal_p (TREE_OPERAND (v1, 1),
590 TREE_OPERAND (v2, 1), 0)
591 && DECL_ASSEMBLER_NAME
592 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
593 == DECL_ASSEMBLER_NAME
594 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
597 /* Free ODR type V. */
599 inline void
600 odr_name_hasher::remove (odr_type_d *v)
602 v->bases.release ();
603 v->derived_types.release ();
604 if (v->types_set)
605 delete v->types_set;
606 ggc_free (v);
609 /* ODR type hash used to look up ODR type based on tree type node. */
611 typedef hash_table<odr_name_hasher> odr_hash_type;
612 static odr_hash_type *odr_hash;
613 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
614 static odr_vtable_hash_type *odr_vtable_hash;
616 /* ODR types are also stored into ODR_TYPE vector to allow consistent
617 walking. Bases appear before derived types. Vector is garbage collected
618 so we won't end up visiting empty types. */
620 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
621 #define odr_types (*odr_types_ptr)
623 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
624 void
625 set_type_binfo (tree type, tree binfo)
627 for (; type; type = TYPE_NEXT_VARIANT (type))
628 if (COMPLETE_TYPE_P (type))
629 TYPE_BINFO (type) = binfo;
630 else
631 gcc_assert (!TYPE_BINFO (type));
634 /* Return true if type variants match.
635 This assumes that we already verified that T1 and T2 are variants of the
636 same type. */
638 static bool
639 type_variants_equivalent_p (tree t1, tree t2, bool warn, bool *warned)
641 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
643 warn_odr (t1, t2, NULL, NULL, warn, warned,
644 G_("a type with different qualifiers is defined in another "
645 "translation unit"));
646 return false;
649 if (comp_type_attributes (t1, t2) != 1)
651 warn_odr (t1, t2, NULL, NULL, warn, warned,
652 G_("a type with different attributes "
653 "is defined in another translation unit"));
654 return false;
657 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
658 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
660 warn_odr (t1, t2, NULL, NULL, warn, warned,
661 G_("a type with different alignment "
662 "is defined in another translation unit"));
663 return false;
666 return true;
669 /* Compare T1 and T2 based on name or structure. */
671 static bool
672 odr_subtypes_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
673 hash_set<type_pair> *visited,
674 location_t loc1, location_t loc2)
677 /* This can happen in incomplete types that should be handled earlier. */
678 gcc_assert (t1 && t2);
680 if (t1 == t2)
681 return true;
683 /* Anonymous namespace types must match exactly. */
684 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
685 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
686 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
687 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
688 return false;
690 /* For ODR types be sure to compare their names.
691 To support -Wno-odr-type-merging we allow one type to be non-ODR
692 and other ODR even though it is a violation. */
693 if (types_odr_comparable (t1, t2))
695 if (!types_same_for_odr (t1, t2))
696 return false;
697 if (!type_variants_equivalent_p (t1, t2, warn, warned))
698 return false;
699 /* Limit recursion: If subtypes are ODR types and we know
700 that they are same, be happy. */
701 if (!odr_type_p (TYPE_MAIN_VARIANT (t1))
702 || !get_odr_type (TYPE_MAIN_VARIANT (t1), true)->odr_violated)
703 return true;
706 /* Component types, builtins and possibly violating ODR types
707 have to be compared structurally. */
708 if (TREE_CODE (t1) != TREE_CODE (t2))
709 return false;
710 if (AGGREGATE_TYPE_P (t1)
711 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
712 return false;
714 type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)};
715 if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2)))
717 pair.first = TYPE_MAIN_VARIANT (t2);
718 pair.second = TYPE_MAIN_VARIANT (t1);
720 if (visited->add (pair))
721 return true;
722 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
723 false, NULL, visited, loc1, loc2))
724 return false;
725 if (!type_variants_equivalent_p (t1, t2, warn, warned))
726 return false;
727 return true;
730 /* Return true if DECL1 and DECL2 are identical methods. Consider
731 name equivalent to name.localalias.xyz. */
733 static bool
734 methods_equal_p (tree decl1, tree decl2)
736 if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2))
737 return true;
738 const char sep = symbol_table::symbol_suffix_separator ();
740 const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1));
741 const char *ptr1 = strchr (name1, sep);
742 int len1 = ptr1 ? ptr1 - name1 : strlen (name1);
744 const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2));
745 const char *ptr2 = strchr (name2, sep);
746 int len2 = ptr2 ? ptr2 - name2 : strlen (name2);
748 if (len1 != len2)
749 return false;
750 return !strncmp (name1, name2, len1);
753 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
754 violation warnings. */
756 void
757 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
759 int n1, n2;
761 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
763 odr_violation_reported = true;
764 if (DECL_VIRTUAL_P (prevailing->decl))
766 varpool_node *tmp = prevailing;
767 prevailing = vtable;
768 vtable = tmp;
770 auto_diagnostic_group d;
771 if (warning_at (DECL_SOURCE_LOCATION
772 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
773 OPT_Wodr,
774 "virtual table of type %qD violates one definition rule",
775 DECL_CONTEXT (vtable->decl)))
776 inform (DECL_SOURCE_LOCATION (prevailing->decl),
777 "variable of same assembler name as the virtual table is "
778 "defined in another translation unit");
779 return;
781 if (!prevailing->definition || !vtable->definition)
782 return;
784 /* If we do not stream ODR type info, do not bother to do useful compare. */
785 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
786 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
787 return;
789 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
791 if (class_type->odr_violated)
792 return;
794 for (n1 = 0, n2 = 0; true; n1++, n2++)
796 struct ipa_ref *ref1, *ref2;
797 bool end1, end2;
799 end1 = !prevailing->iterate_reference (n1, ref1);
800 end2 = !vtable->iterate_reference (n2, ref2);
802 /* !DECL_VIRTUAL_P means RTTI entry;
803 We warn when RTTI is lost because non-RTTI previals; we silently
804 accept the other case. */
805 while (!end2
806 && (end1
807 || (methods_equal_p (ref1->referred->decl,
808 ref2->referred->decl)
809 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
810 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
812 if (!class_type->rtti_broken)
814 auto_diagnostic_group d;
815 if (warning_at (DECL_SOURCE_LOCATION
816 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
817 OPT_Wodr,
818 "virtual table of type %qD contains RTTI "
819 "information",
820 DECL_CONTEXT (vtable->decl)))
822 inform (DECL_SOURCE_LOCATION
823 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
824 "but is prevailed by one without from other"
825 " translation unit");
826 inform (DECL_SOURCE_LOCATION
827 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
828 "RTTI will not work on this type");
829 class_type->rtti_broken = true;
832 n2++;
833 end2 = !vtable->iterate_reference (n2, ref2);
835 while (!end1
836 && (end2
837 || (methods_equal_p (ref2->referred->decl, ref1->referred->decl)
838 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
839 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
841 n1++;
842 end1 = !prevailing->iterate_reference (n1, ref1);
845 /* Finished? */
846 if (end1 && end2)
848 /* Extra paranoia; compare the sizes. We do not have information
849 about virtual inheritance offsets, so just be sure that these
850 match.
851 Do this as very last check so the not very informative error
852 is not output too often. */
853 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
855 class_type->odr_violated = true;
856 auto_diagnostic_group d;
857 if (warning_at (DECL_SOURCE_LOCATION
858 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
859 OPT_Wodr,
860 "virtual table of type %qD violates "
861 "one definition rule ",
862 DECL_CONTEXT (vtable->decl)))
864 inform (DECL_SOURCE_LOCATION
865 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
866 "the conflicting type defined in another translation "
867 "unit has virtual table of different size");
870 return;
873 if (!end1 && !end2)
875 if (methods_equal_p (ref1->referred->decl, ref2->referred->decl))
876 continue;
878 class_type->odr_violated = true;
880 /* If the loops above stopped on non-virtual pointer, we have
881 mismatch in RTTI information mangling. */
882 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
883 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
885 auto_diagnostic_group d;
886 if (warning_at (DECL_SOURCE_LOCATION
887 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
888 OPT_Wodr,
889 "virtual table of type %qD violates "
890 "one definition rule ",
891 DECL_CONTEXT (vtable->decl)))
893 inform (DECL_SOURCE_LOCATION
894 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
895 "the conflicting type defined in another translation "
896 "unit with different RTTI information");
898 return;
900 /* At this point both REF1 and REF2 points either to virtual table
901 or virtual method. If one points to virtual table and other to
902 method we can complain the same way as if one table was shorter
903 than other pointing out the extra method. */
904 if (TREE_CODE (ref1->referred->decl)
905 != TREE_CODE (ref2->referred->decl))
907 if (VAR_P (ref1->referred->decl))
908 end1 = true;
909 else if (VAR_P (ref2->referred->decl))
910 end2 = true;
914 class_type->odr_violated = true;
916 /* Complain about size mismatch. Either we have too many virutal
917 functions or too many virtual table pointers. */
918 if (end1 || end2)
920 if (end1)
922 varpool_node *tmp = prevailing;
923 prevailing = vtable;
924 vtable = tmp;
925 ref1 = ref2;
927 auto_diagnostic_group d;
928 if (warning_at (DECL_SOURCE_LOCATION
929 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
930 OPT_Wodr,
931 "virtual table of type %qD violates "
932 "one definition rule",
933 DECL_CONTEXT (vtable->decl)))
935 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
937 inform (DECL_SOURCE_LOCATION
938 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
939 "the conflicting type defined in another translation "
940 "unit");
941 inform (DECL_SOURCE_LOCATION
942 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
943 "contains additional virtual method %qD",
944 ref1->referred->decl);
946 else
948 inform (DECL_SOURCE_LOCATION
949 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
950 "the conflicting type defined in another translation "
951 "unit has virtual table with more entries");
954 return;
957 /* And in the last case we have either mistmatch in between two virtual
958 methods or two virtual table pointers. */
959 auto_diagnostic_group d;
960 if (warning_at (DECL_SOURCE_LOCATION
961 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
962 "virtual table of type %qD violates "
963 "one definition rule ",
964 DECL_CONTEXT (vtable->decl)))
966 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
968 inform (DECL_SOURCE_LOCATION
969 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
970 "the conflicting type defined in another translation "
971 "unit");
972 gcc_assert (TREE_CODE (ref2->referred->decl)
973 == FUNCTION_DECL);
974 inform (DECL_SOURCE_LOCATION
975 (ref1->referred->ultimate_alias_target ()->decl),
976 "virtual method %qD",
977 ref1->referred->ultimate_alias_target ()->decl);
978 inform (DECL_SOURCE_LOCATION
979 (ref2->referred->ultimate_alias_target ()->decl),
980 "ought to match virtual method %qD but does not",
981 ref2->referred->ultimate_alias_target ()->decl);
983 else
984 inform (DECL_SOURCE_LOCATION
985 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
986 "the conflicting type defined in another translation "
987 "unit has virtual table with different contents");
988 return;
993 /* Output ODR violation warning about T1 and T2 with REASON.
994 Display location of ST1 and ST2 if REASON speaks about field or
995 method of the type.
996 If WARN is false, do nothing. Set WARNED if warning was indeed
997 output. */
999 static void
1000 warn_odr (tree t1, tree t2, tree st1, tree st2,
1001 bool warn, bool *warned, const char *reason)
1003 tree decl2 = TYPE_NAME (t2);
1004 if (warned)
1005 *warned = false;
1007 if (!warn || !TYPE_NAME(t1))
1008 return;
1010 /* ODR warnings are output druing LTO streaming; we must apply location
1011 cache for potential warnings to be output correctly. */
1012 if (lto_location_cache::current_cache)
1013 lto_location_cache::current_cache->apply_location_cache ();
1015 auto_diagnostic_group d;
1016 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
1017 "type %qT violates the C++ One Definition Rule",
1018 t1))
1019 return;
1020 if (!st1 && !st2)
1022 /* For FIELD_DECL support also case where one of fields is
1023 NULL - this is used when the structures have mismatching number of
1024 elements. */
1025 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
1027 inform (DECL_SOURCE_LOCATION (decl2),
1028 "a different type is defined in another translation unit");
1029 if (!st1)
1031 st1 = st2;
1032 st2 = NULL;
1034 inform (DECL_SOURCE_LOCATION (st1),
1035 "the first difference of corresponding definitions is field %qD",
1036 st1);
1037 if (st2)
1038 decl2 = st2;
1040 else if (TREE_CODE (st1) == FUNCTION_DECL)
1042 inform (DECL_SOURCE_LOCATION (decl2),
1043 "a different type is defined in another translation unit");
1044 inform (DECL_SOURCE_LOCATION (st1),
1045 "the first difference of corresponding definitions is method %qD",
1046 st1);
1047 decl2 = st2;
1049 else
1050 return;
1051 inform (DECL_SOURCE_LOCATION (decl2), reason);
1053 if (warned)
1054 *warned = true;
1057 /* Return ture if T1 and T2 are incompatible and we want to recusively
1058 dive into them from warn_type_mismatch to give sensible answer. */
1060 static bool
1061 type_mismatch_p (tree t1, tree t2)
1063 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1064 && !odr_types_equivalent_p (t1, t2))
1065 return true;
1066 return !types_compatible_p (t1, t2);
1070 /* Types T1 and T2 was found to be incompatible in a context they can't
1071 (either used to declare a symbol of same assembler name or unified by
1072 ODR rule). We already output warning about this, but if possible, output
1073 extra information on how the types mismatch.
1075 This is hard to do in general. We basically handle the common cases.
1077 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1078 themselves do no thave one.*/
1080 void
1081 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1083 /* Location of type is known only if it has TYPE_NAME and the name is
1084 TYPE_DECL. */
1085 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1086 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1087 : UNKNOWN_LOCATION;
1088 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1089 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1090 : UNKNOWN_LOCATION;
1091 bool loc_t2_useful = false;
1093 /* With LTO it is a common case that the location of both types match.
1094 See if T2 has a location that is different from T1. If so, we will
1095 inform user about the location.
1096 Do not consider the location passed to us in LOC1/LOC2 as those are
1097 already output. */
1098 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1100 if (loc_t1 <= BUILTINS_LOCATION)
1101 loc_t2_useful = true;
1102 else
1104 expanded_location xloc1 = expand_location (loc_t1);
1105 expanded_location xloc2 = expand_location (loc_t2);
1107 if (strcmp (xloc1.file, xloc2.file)
1108 || xloc1.line != xloc2.line
1109 || xloc1.column != xloc2.column)
1110 loc_t2_useful = true;
1114 if (loc_t1 <= BUILTINS_LOCATION)
1115 loc_t1 = loc1;
1116 if (loc_t2 <= BUILTINS_LOCATION)
1117 loc_t2 = loc2;
1119 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1121 /* It is a quite common bug to reference anonymous namespace type in
1122 non-anonymous namespace class. */
1123 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1124 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1125 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
1126 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
1128 if (type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1129 && !type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1131 std::swap (t1, t2);
1132 std::swap (loc_t1, loc_t2);
1134 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1135 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1136 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1137 tree n1 = TYPE_NAME (t1);
1138 tree n2 = TYPE_NAME (t2);
1139 if (TREE_CODE (n1) == TYPE_DECL)
1140 n1 = DECL_NAME (n1);
1141 if (TREE_CODE (n2) == TYPE_DECL)
1142 n2 = DECL_NAME (n2);
1143 /* Most of the time, the type names will match, do not be unnecesarily
1144 verbose. */
1145 if (IDENTIFIER_POINTER (n1) != IDENTIFIER_POINTER (n2))
1146 inform (loc_t1,
1147 "type %qT defined in anonymous namespace can not match "
1148 "type %qT across the translation unit boundary",
1149 t1, t2);
1150 else
1151 inform (loc_t1,
1152 "type %qT defined in anonymous namespace can not match "
1153 "across the translation unit boundary",
1154 t1);
1155 if (loc_t2_useful)
1156 inform (loc_t2,
1157 "the incompatible type defined in another translation unit");
1158 return;
1160 tree mt1 = TYPE_MAIN_VARIANT (t1);
1161 tree mt2 = TYPE_MAIN_VARIANT (t2);
1162 /* If types have mangled ODR names and they are different, it is most
1163 informative to output those.
1164 This also covers types defined in different namespaces. */
1165 if (TYPE_NAME (mt1) && TYPE_NAME (mt2)
1166 && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL
1167 && TREE_CODE (TYPE_NAME (mt2)) == TYPE_DECL
1168 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt1))
1169 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (mt2))
1170 && DECL_ASSEMBLER_NAME (TYPE_NAME (mt1))
1171 != DECL_ASSEMBLER_NAME (TYPE_NAME (mt2)))
1173 char *name1 = xstrdup (cplus_demangle
1174 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt1))),
1175 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1176 char *name2 = cplus_demangle
1177 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (mt2))),
1178 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1179 if (name1 && name2 && strcmp (name1, name2))
1181 inform (loc_t1,
1182 "type name %qs should match type name %qs",
1183 name1, name2);
1184 if (loc_t2_useful)
1185 inform (loc_t2,
1186 "the incompatible type is defined here");
1187 free (name1);
1188 return;
1190 free (name1);
1192 /* A tricky case are compound types. Often they appear the same in source
1193 code and the mismatch is dragged in by type they are build from.
1194 Look for those differences in subtypes and try to be informative. In other
1195 cases just output nothing because the source code is probably different
1196 and in this case we already output a all necessary info. */
1197 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1199 if (TREE_CODE (t1) == TREE_CODE (t2))
1201 if (TREE_CODE (t1) == ARRAY_TYPE
1202 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1204 tree i1 = TYPE_DOMAIN (t1);
1205 tree i2 = TYPE_DOMAIN (t2);
1207 if (i1 && i2
1208 && TYPE_MAX_VALUE (i1)
1209 && TYPE_MAX_VALUE (i2)
1210 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1211 TYPE_MAX_VALUE (i2), 0))
1213 inform (loc,
1214 "array types have different bounds");
1215 return;
1218 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1219 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1220 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1221 else if (TREE_CODE (t1) == METHOD_TYPE
1222 || TREE_CODE (t1) == FUNCTION_TYPE)
1224 tree parms1 = NULL, parms2 = NULL;
1225 int count = 1;
1227 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1229 inform (loc, "return value type mismatch");
1230 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1231 loc_t2);
1232 return;
1234 if (prototype_p (t1) && prototype_p (t2))
1235 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1236 parms1 && parms2;
1237 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1238 count++)
1240 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1242 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1243 inform (loc,
1244 "implicit this pointer type mismatch");
1245 else
1246 inform (loc,
1247 "type mismatch in parameter %i",
1248 count - (TREE_CODE (t1) == METHOD_TYPE));
1249 warn_types_mismatch (TREE_VALUE (parms1),
1250 TREE_VALUE (parms2),
1251 loc_t1, loc_t2);
1252 return;
1255 if (parms1 || parms2)
1257 inform (loc,
1258 "types have different parameter counts");
1259 return;
1263 return;
1266 if (types_odr_comparable (t1, t2)
1267 && types_same_for_odr (t1, t2))
1268 inform (loc_t1,
1269 "type %qT itself violates the C++ One Definition Rule", t1);
1270 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1271 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1272 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1273 return;
1274 else
1275 inform (loc_t1, "type %qT should match type %qT",
1276 t1, t2);
1277 if (loc_t2_useful)
1278 inform (loc_t2, "the incompatible type is defined here");
1281 /* Compare T1 and T2, report ODR violations if WARN is true and set
1282 WARNED to true if anything is reported. Return true if types match.
1283 If true is returned, the types are also compatible in the sense of
1284 gimple_canonical_types_compatible_p.
1285 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1286 about the type if the type itself do not have location. */
1288 static bool
1289 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1290 hash_set<type_pair> *visited,
1291 location_t loc1, location_t loc2)
1293 /* Check first for the obvious case of pointer identity. */
1294 if (t1 == t2)
1295 return true;
1297 /* Can't be the same type if the types don't have the same code. */
1298 if (TREE_CODE (t1) != TREE_CODE (t2))
1300 warn_odr (t1, t2, NULL, NULL, warn, warned,
1301 G_("a different type is defined in another translation unit"));
1302 return false;
1305 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1306 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1307 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
1308 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
1310 /* We can not trip this when comparing ODR types, only when trying to
1311 match different ODR derivations from different declarations.
1312 So WARN should be always false. */
1313 gcc_assert (!warn);
1314 return false;
1317 if (TREE_CODE (t1) == ENUMERAL_TYPE
1318 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1320 tree v1, v2;
1321 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1322 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1324 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1326 warn_odr (t1, t2, NULL, NULL, warn, warned,
1327 G_("an enum with different value name"
1328 " is defined in another translation unit"));
1329 return false;
1331 if (TREE_VALUE (v1) != TREE_VALUE (v2))
1333 warn_odr (t1, t2, NULL, NULL, warn, warned,
1334 G_("an enum with different values is defined"
1335 " in another translation unit"));
1336 return false;
1339 if (v1 || v2)
1341 warn_odr (t1, t2, NULL, NULL, warn, warned,
1342 G_("an enum with mismatching number of values "
1343 "is defined in another translation unit"));
1344 return false;
1348 /* Non-aggregate types can be handled cheaply. */
1349 if (INTEGRAL_TYPE_P (t1)
1350 || SCALAR_FLOAT_TYPE_P (t1)
1351 || FIXED_POINT_TYPE_P (t1)
1352 || TREE_CODE (t1) == VECTOR_TYPE
1353 || TREE_CODE (t1) == COMPLEX_TYPE
1354 || TREE_CODE (t1) == OFFSET_TYPE
1355 || POINTER_TYPE_P (t1))
1357 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1359 warn_odr (t1, t2, NULL, NULL, warn, warned,
1360 G_("a type with different precision is defined "
1361 "in another translation unit"));
1362 return false;
1364 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1366 warn_odr (t1, t2, NULL, NULL, warn, warned,
1367 G_("a type with different signedness is defined "
1368 "in another translation unit"));
1369 return false;
1372 if (TREE_CODE (t1) == INTEGER_TYPE
1373 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1375 /* char WRT uint_8? */
1376 warn_odr (t1, t2, NULL, NULL, warn, warned,
1377 G_("a different type is defined in another "
1378 "translation unit"));
1379 return false;
1382 /* For canonical type comparisons we do not want to build SCCs
1383 so we cannot compare pointed-to types. But we can, for now,
1384 require the same pointed-to type kind and match what
1385 useless_type_conversion_p would do. */
1386 if (POINTER_TYPE_P (t1))
1388 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1389 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1391 warn_odr (t1, t2, NULL, NULL, warn, warned,
1392 G_("it is defined as a pointer in different address "
1393 "space in another translation unit"));
1394 return false;
1397 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1398 warn, warned, visited, loc1, loc2))
1400 warn_odr (t1, t2, NULL, NULL, warn, warned,
1401 G_("it is defined as a pointer to different type "
1402 "in another translation unit"));
1403 if (warn && warned)
1404 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1405 loc1, loc2);
1406 return false;
1410 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1411 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1412 warn, warned,
1413 visited, loc1, loc2))
1415 /* Probably specific enough. */
1416 warn_odr (t1, t2, NULL, NULL, warn, warned,
1417 G_("a different type is defined "
1418 "in another translation unit"));
1419 if (warn && warned)
1420 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1421 return false;
1424 /* Do type-specific comparisons. */
1425 else switch (TREE_CODE (t1))
1427 case ARRAY_TYPE:
1429 /* Array types are the same if the element types are the same and
1430 the number of elements are the same. */
1431 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1432 warn, warned, visited, loc1, loc2))
1434 warn_odr (t1, t2, NULL, NULL, warn, warned,
1435 G_("a different type is defined in another "
1436 "translation unit"));
1437 if (warn && warned)
1438 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1440 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1441 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1442 == TYPE_NONALIASED_COMPONENT (t2));
1444 tree i1 = TYPE_DOMAIN (t1);
1445 tree i2 = TYPE_DOMAIN (t2);
1447 /* For an incomplete external array, the type domain can be
1448 NULL_TREE. Check this condition also. */
1449 if (i1 == NULL_TREE || i2 == NULL_TREE)
1450 return type_variants_equivalent_p (t1, t2, warn, warned);
1452 tree min1 = TYPE_MIN_VALUE (i1);
1453 tree min2 = TYPE_MIN_VALUE (i2);
1454 tree max1 = TYPE_MAX_VALUE (i1);
1455 tree max2 = TYPE_MAX_VALUE (i2);
1457 /* In C++, minimums should be always 0. */
1458 gcc_assert (min1 == min2);
1459 if (!operand_equal_p (max1, max2, 0))
1461 warn_odr (t1, t2, NULL, NULL, warn, warned,
1462 G_("an array of different size is defined "
1463 "in another translation unit"));
1464 return false;
1467 break;
1469 case METHOD_TYPE:
1470 case FUNCTION_TYPE:
1471 /* Function types are the same if the return type and arguments types
1472 are the same. */
1473 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1474 warn, warned, visited, loc1, loc2))
1476 warn_odr (t1, t2, NULL, NULL, warn, warned,
1477 G_("has different return value "
1478 "in another translation unit"));
1479 if (warn && warned)
1480 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1481 return false;
1484 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1485 || !prototype_p (t1) || !prototype_p (t2))
1486 return type_variants_equivalent_p (t1, t2, warn, warned);
1487 else
1489 tree parms1, parms2;
1491 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1492 parms1 && parms2;
1493 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1495 if (!odr_subtypes_equivalent_p
1496 (TREE_VALUE (parms1), TREE_VALUE (parms2), warn, warned,
1497 visited, loc1, loc2))
1499 warn_odr (t1, t2, NULL, NULL, warn, warned,
1500 G_("has different parameters in another "
1501 "translation unit"));
1502 if (warn && warned)
1503 warn_types_mismatch (TREE_VALUE (parms1),
1504 TREE_VALUE (parms2), loc1, loc2);
1505 return false;
1509 if (parms1 || parms2)
1511 warn_odr (t1, t2, NULL, NULL, warn, warned,
1512 G_("has different parameters "
1513 "in another translation unit"));
1514 return false;
1517 return type_variants_equivalent_p (t1, t2, warn, warned);
1520 case RECORD_TYPE:
1521 case UNION_TYPE:
1522 case QUAL_UNION_TYPE:
1524 tree f1, f2;
1526 /* For aggregate types, all the fields must be the same. */
1527 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1529 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1530 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1531 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1533 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1534 warn_odr (t1, t2, NULL, NULL, warn, warned,
1535 G_("a type defined in another translation unit "
1536 "is not polymorphic"));
1537 else
1538 warn_odr (t1, t2, NULL, NULL, warn, warned,
1539 G_("a type defined in another translation unit "
1540 "is polymorphic"));
1541 return false;
1543 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1544 f1 || f2;
1545 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1547 /* Skip non-fields. */
1548 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1549 f1 = TREE_CHAIN (f1);
1550 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1551 f2 = TREE_CHAIN (f2);
1552 if (!f1 || !f2)
1553 break;
1554 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1556 warn_odr (t1, t2, NULL, NULL, warn, warned,
1557 G_("a type with different virtual table pointers"
1558 " is defined in another translation unit"));
1559 return false;
1561 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1563 warn_odr (t1, t2, NULL, NULL, warn, warned,
1564 G_("a type with different bases is defined "
1565 "in another translation unit"));
1566 return false;
1568 if (DECL_NAME (f1) != DECL_NAME (f2)
1569 && !DECL_ARTIFICIAL (f1))
1571 warn_odr (t1, t2, f1, f2, warn, warned,
1572 G_("a field with different name is defined "
1573 "in another translation unit"));
1574 return false;
1576 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1577 TREE_TYPE (f2), warn, warned,
1578 visited, loc1, loc2))
1580 /* Do not warn about artificial fields and just go into
1581 generic field mismatch warning. */
1582 if (DECL_ARTIFICIAL (f1))
1583 break;
1585 warn_odr (t1, t2, f1, f2, warn, warned,
1586 G_("a field of same name but different type "
1587 "is defined in another translation unit"));
1588 if (warn && warned)
1589 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1590 return false;
1592 if (!gimple_compare_field_offset (f1, f2))
1594 /* Do not warn about artificial fields and just go into
1595 generic field mismatch warning. */
1596 if (DECL_ARTIFICIAL (f1))
1597 break;
1598 warn_odr (t1, t2, f1, f2, warn, warned,
1599 G_("fields have different layout "
1600 "in another translation unit"));
1601 return false;
1603 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
1605 warn_odr (t1, t2, f1, f2, warn, warned,
1606 G_("one field is bitfield while other is not"));
1607 return false;
1609 else
1610 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1611 == DECL_NONADDRESSABLE_P (f2));
1614 /* If one aggregate has more fields than the other, they
1615 are not the same. */
1616 if (f1 || f2)
1618 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1619 warn_odr (t1, t2, NULL, NULL, warn, warned,
1620 G_("a type with different virtual table pointers"
1621 " is defined in another translation unit"));
1622 else if ((f1 && DECL_ARTIFICIAL (f1))
1623 || (f2 && DECL_ARTIFICIAL (f2)))
1624 warn_odr (t1, t2, NULL, NULL, warn, warned,
1625 G_("a type with different bases is defined "
1626 "in another translation unit"));
1627 else
1628 warn_odr (t1, t2, f1, f2, warn, warned,
1629 G_("a type with different number of fields "
1630 "is defined in another translation unit"));
1632 return false;
1635 break;
1637 case VOID_TYPE:
1638 case NULLPTR_TYPE:
1639 break;
1641 default:
1642 debug_tree (t1);
1643 gcc_unreachable ();
1646 /* Those are better to come last as they are utterly uninformative. */
1647 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1648 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1650 warn_odr (t1, t2, NULL, NULL, warn, warned,
1651 G_("a type with different size "
1652 "is defined in another translation unit"));
1653 return false;
1656 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1657 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1658 TYPE_SIZE_UNIT (t2), 0));
1659 return type_variants_equivalent_p (t1, t2, warn, warned);
1662 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1664 bool
1665 odr_types_equivalent_p (tree type1, tree type2)
1667 gcc_checking_assert (odr_or_derived_type_p (type1)
1668 && odr_or_derived_type_p (type2));
1670 hash_set<type_pair> visited;
1671 return odr_types_equivalent_p (type1, type2, false, NULL,
1672 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1675 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1676 from VAL->type. This may happen in LTO where tree merging did not merge
1677 all variants of the same type or due to ODR violation.
1679 Analyze and report ODR violations and add type to duplicate list.
1680 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1681 this is first time we see definition of a class return true so the
1682 base types are analyzed. */
1684 static bool
1685 add_type_duplicate (odr_type val, tree type)
1687 bool build_bases = false;
1688 bool prevail = false;
1689 bool odr_must_violate = false;
1691 if (!val->types_set)
1692 val->types_set = new hash_set<tree>;
1694 /* Chose polymorphic type as leader (this happens only in case of ODR
1695 violations. */
1696 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1697 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1698 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1699 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1701 prevail = true;
1702 build_bases = true;
1704 /* Always prefer complete type to be the leader. */
1705 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1707 prevail = true;
1708 build_bases = TYPE_BINFO (type);
1710 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1712 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1713 && TREE_CODE (type) == ENUMERAL_TYPE
1714 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1715 prevail = true;
1716 else if (TREE_CODE (val->type) == RECORD_TYPE
1717 && TREE_CODE (type) == RECORD_TYPE
1718 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1720 gcc_assert (!val->bases.length ());
1721 build_bases = true;
1722 prevail = true;
1725 if (prevail)
1726 std::swap (val->type, type);
1728 val->types_set->add (type);
1730 /* If we now have a mangled name, be sure to record it to val->type
1731 so ODR hash can work. */
1733 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1734 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1735 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1737 bool merge = true;
1738 bool base_mismatch = false;
1739 unsigned int i;
1740 bool warned = false;
1741 hash_set<type_pair> visited;
1743 gcc_assert (in_lto_p);
1744 vec_safe_push (val->types, type);
1746 /* If both are class types, compare the bases. */
1747 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1748 && TREE_CODE (val->type) == RECORD_TYPE
1749 && TREE_CODE (type) == RECORD_TYPE
1750 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1752 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1753 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1755 if (!flag_ltrans && !warned && !val->odr_violated)
1757 tree extra_base;
1758 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1759 "a type with the same name but different "
1760 "number of polymorphic bases is "
1761 "defined in another translation unit");
1762 if (warned)
1764 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1765 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1766 extra_base = BINFO_BASE_BINFO
1767 (TYPE_BINFO (type),
1768 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1769 else
1770 extra_base = BINFO_BASE_BINFO
1771 (TYPE_BINFO (val->type),
1772 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1773 tree extra_base_type = BINFO_TYPE (extra_base);
1774 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1775 "the extra base is defined here");
1778 base_mismatch = true;
1780 else
1781 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1783 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1784 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1785 tree type1 = BINFO_TYPE (base1);
1786 tree type2 = BINFO_TYPE (base2);
1788 if (types_odr_comparable (type1, type2))
1790 if (!types_same_for_odr (type1, type2))
1791 base_mismatch = true;
1793 else
1794 if (!odr_types_equivalent_p (type1, type2))
1795 base_mismatch = true;
1796 if (base_mismatch)
1798 if (!warned && !val->odr_violated)
1800 warn_odr (type, val->type, NULL, NULL,
1801 !warned, &warned,
1802 "a type with the same name but different base "
1803 "type is defined in another translation unit");
1804 if (warned)
1805 warn_types_mismatch (type1, type2,
1806 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1808 break;
1810 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1812 base_mismatch = true;
1813 if (!warned && !val->odr_violated)
1814 warn_odr (type, val->type, NULL, NULL,
1815 !warned, &warned,
1816 "a type with the same name but different base "
1817 "layout is defined in another translation unit");
1818 break;
1820 /* One of bases is not of complete type. */
1821 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1823 /* If we have a polymorphic type info specified for TYPE1
1824 but not for TYPE2 we possibly missed a base when recording
1825 VAL->type earlier.
1826 Be sure this does not happen. */
1827 if (TYPE_BINFO (type1)
1828 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1829 && !build_bases)
1830 odr_must_violate = true;
1831 break;
1833 /* One base is polymorphic and the other not.
1834 This ought to be diagnosed earlier, but do not ICE in the
1835 checking bellow. */
1836 else if (TYPE_BINFO (type1)
1837 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1838 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1840 if (!warned && !val->odr_violated)
1841 warn_odr (type, val->type, NULL, NULL,
1842 !warned, &warned,
1843 "a base of the type is polymorphic only in one "
1844 "translation unit");
1845 base_mismatch = true;
1846 break;
1849 if (base_mismatch)
1851 merge = false;
1852 odr_violation_reported = true;
1853 val->odr_violated = true;
1855 if (symtab->dump_file)
1857 fprintf (symtab->dump_file, "ODR base violation\n");
1859 print_node (symtab->dump_file, "", val->type, 0);
1860 putc ('\n',symtab->dump_file);
1861 print_node (symtab->dump_file, "", type, 0);
1862 putc ('\n',symtab->dump_file);
1867 /* Next compare memory layout.
1868 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1869 We must apply the location cache to ensure that they are valid
1870 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1871 if (lto_location_cache::current_cache)
1872 lto_location_cache::current_cache->apply_location_cache ();
1873 /* As a special case we stream mangles names of integer types so we can see
1874 if they are believed to be same even though they have different
1875 representation. Avoid bogus warning on mismatches in these. */
1876 if (TREE_CODE (type) != INTEGER_TYPE
1877 && TREE_CODE (val->type) != INTEGER_TYPE
1878 && !odr_types_equivalent_p (val->type, type,
1879 !flag_ltrans && !val->odr_violated && !warned,
1880 &warned, &visited,
1881 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1882 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1884 merge = false;
1885 odr_violation_reported = true;
1886 val->odr_violated = true;
1888 gcc_assert (val->odr_violated || !odr_must_violate);
1889 /* Sanity check that all bases will be build same way again. */
1890 if (flag_checking
1891 && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1892 && TREE_CODE (val->type) == RECORD_TYPE
1893 && TREE_CODE (type) == RECORD_TYPE
1894 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1895 && !val->odr_violated
1896 && !base_mismatch && val->bases.length ())
1898 unsigned int num_poly_bases = 0;
1899 unsigned int j;
1901 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1902 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1903 (TYPE_BINFO (type), i)))
1904 num_poly_bases++;
1905 gcc_assert (num_poly_bases == val->bases.length ());
1906 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1907 i++)
1908 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1909 (TYPE_BINFO (type), i)))
1911 odr_type base = get_odr_type
1912 (BINFO_TYPE
1913 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1914 i)),
1915 true);
1916 gcc_assert (val->bases[j] == base);
1917 j++;
1922 /* Regularize things a little. During LTO same types may come with
1923 different BINFOs. Either because their virtual table was
1924 not merged by tree merging and only later at decl merging or
1925 because one type comes with external vtable, while other
1926 with internal. We want to merge equivalent binfos to conserve
1927 memory and streaming overhead.
1929 The external vtables are more harmful: they contain references
1930 to external declarations of methods that may be defined in the
1931 merged LTO unit. For this reason we absolutely need to remove
1932 them and replace by internal variants. Not doing so will lead
1933 to incomplete answers from possible_polymorphic_call_targets.
1935 FIXME: disable for now; because ODR types are now build during
1936 streaming in, the variants do not need to be linked to the type,
1937 yet. We need to do the merging in cleanup pass to be implemented
1938 soon. */
1939 if (!flag_ltrans && merge
1940 && 0
1941 && TREE_CODE (val->type) == RECORD_TYPE
1942 && TREE_CODE (type) == RECORD_TYPE
1943 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1944 && TYPE_MAIN_VARIANT (type) == type
1945 && TYPE_MAIN_VARIANT (val->type) == val->type
1946 && BINFO_VTABLE (TYPE_BINFO (val->type))
1947 && BINFO_VTABLE (TYPE_BINFO (type)))
1949 tree master_binfo = TYPE_BINFO (val->type);
1950 tree v1 = BINFO_VTABLE (master_binfo);
1951 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1953 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1955 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1956 && operand_equal_p (TREE_OPERAND (v1, 1),
1957 TREE_OPERAND (v2, 1), 0));
1958 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1959 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1961 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1962 == DECL_ASSEMBLER_NAME (v2));
1964 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1966 unsigned int i;
1968 set_type_binfo (val->type, TYPE_BINFO (type));
1969 for (i = 0; i < val->types->length (); i++)
1971 if (TYPE_BINFO ((*val->types)[i])
1972 == master_binfo)
1973 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1975 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1977 else
1978 set_type_binfo (type, master_binfo);
1980 return build_bases;
1983 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1984 possibly new entry. */
1986 odr_type
1987 get_odr_type (tree type, bool insert)
1989 odr_type_d **slot = NULL;
1990 odr_type_d **vtable_slot = NULL;
1991 odr_type val = NULL;
1992 hashval_t hash;
1993 bool build_bases = false;
1994 bool insert_to_odr_array = false;
1995 int base_id = -1;
1997 type = TYPE_MAIN_VARIANT (type);
1999 gcc_checking_assert (can_be_name_hashed_p (type)
2000 || can_be_vtable_hashed_p (type));
2002 /* Lookup entry, first try name hash, fallback to vtable hash. */
2003 if (can_be_name_hashed_p (type))
2005 hash = hash_odr_name (type);
2006 slot = odr_hash->find_slot_with_hash (type, hash,
2007 insert ? INSERT : NO_INSERT);
2009 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2011 hash = hash_odr_vtable (type);
2012 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2013 insert ? INSERT : NO_INSERT);
2016 if (!slot && !vtable_slot)
2017 return NULL;
2019 /* See if we already have entry for type. */
2020 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2022 if (slot && *slot)
2024 val = *slot;
2025 if (flag_checking
2026 && in_lto_p && can_be_vtable_hashed_p (type))
2028 hash = hash_odr_vtable (type);
2029 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2030 NO_INSERT);
2031 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2032 vtable_slot = NULL;
2035 else if (*vtable_slot)
2036 val = *vtable_slot;
2038 if (val->type != type
2039 && (!val->types_set || !val->types_set->add (type)))
2041 gcc_assert (insert);
2042 /* We have type duplicate, but it may introduce vtable name or
2043 mangled name; be sure to keep hashes in sync. */
2044 if (in_lto_p && can_be_vtable_hashed_p (type)
2045 && (!vtable_slot || !*vtable_slot))
2047 if (!vtable_slot)
2049 hash = hash_odr_vtable (type);
2050 vtable_slot = odr_vtable_hash->find_slot_with_hash
2051 (type, hash, INSERT);
2052 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2054 *vtable_slot = val;
2056 if (slot && !*slot)
2057 *slot = val;
2058 build_bases = add_type_duplicate (val, type);
2061 else
2063 val = ggc_cleared_alloc<odr_type_d> ();
2064 val->type = type;
2065 val->bases = vNULL;
2066 val->derived_types = vNULL;
2067 if (type_with_linkage_p (type))
2068 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2069 else
2070 val->anonymous_namespace = 0;
2071 build_bases = COMPLETE_TYPE_P (val->type);
2072 insert_to_odr_array = true;
2073 if (slot)
2074 *slot = val;
2075 if (vtable_slot)
2076 *vtable_slot = val;
2079 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2080 && type_with_linkage_p (type)
2081 && type == TYPE_MAIN_VARIANT (type))
2083 tree binfo = TYPE_BINFO (type);
2084 unsigned int i;
2086 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2088 val->all_derivations_known = type_all_derivations_known_p (type);
2089 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2090 /* For now record only polymorphic types. other are
2091 pointless for devirtualization and we can not precisely
2092 determine ODR equivalency of these during LTO. */
2093 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2095 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2096 odr_type base = get_odr_type (base_type, true);
2097 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2098 base->derived_types.safe_push (val);
2099 val->bases.safe_push (base);
2100 if (base->id > base_id)
2101 base_id = base->id;
2104 /* Ensure that type always appears after bases. */
2105 if (insert_to_odr_array)
2107 if (odr_types_ptr)
2108 val->id = odr_types.length ();
2109 vec_safe_push (odr_types_ptr, val);
2111 else if (base_id > val->id)
2113 odr_types[val->id] = 0;
2114 /* Be sure we did not recorded any derived types; these may need
2115 renumbering too. */
2116 gcc_assert (val->derived_types.length() == 0);
2117 val->id = odr_types.length ();
2118 vec_safe_push (odr_types_ptr, val);
2120 return val;
2123 /* Add TYPE od ODR type hash. */
2125 void
2126 register_odr_type (tree type)
2128 if (!odr_hash)
2130 odr_hash = new odr_hash_type (23);
2131 if (in_lto_p)
2132 odr_vtable_hash = new odr_vtable_hash_type (23);
2134 if (type == TYPE_MAIN_VARIANT (type))
2135 get_odr_type (type, true);
2138 /* Return true if type is known to have no derivations. */
2140 bool
2141 type_known_to_have_no_derivations_p (tree t)
2143 return (type_all_derivations_known_p (t)
2144 && (TYPE_FINAL_P (t)
2145 || (odr_hash
2146 && !get_odr_type (t, true)->derived_types.length())));
2149 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2150 recursive printing. */
2152 static void
2153 dump_odr_type (FILE *f, odr_type t, int indent=0)
2155 unsigned int i;
2156 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2157 print_generic_expr (f, t->type, TDF_SLIM);
2158 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2159 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2160 if (TYPE_NAME (t->type))
2162 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2163 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2164 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2165 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2166 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2167 IDENTIFIER_POINTER
2168 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2170 if (t->bases.length ())
2172 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2173 for (i = 0; i < t->bases.length (); i++)
2174 fprintf (f, " %i", t->bases[i]->id);
2175 fprintf (f, "\n");
2177 if (t->derived_types.length ())
2179 fprintf (f, "%*s derived types:\n", indent * 2, "");
2180 for (i = 0; i < t->derived_types.length (); i++)
2181 dump_odr_type (f, t->derived_types[i], indent + 1);
2183 fprintf (f, "\n");
2186 /* Dump the type inheritance graph. */
2188 static void
2189 dump_type_inheritance_graph (FILE *f)
2191 unsigned int i;
2192 unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0;
2193 if (!odr_types_ptr)
2194 return;
2195 fprintf (f, "\n\nType inheritance graph:\n");
2196 for (i = 0; i < odr_types.length (); i++)
2198 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2199 dump_odr_type (f, odr_types[i]);
2201 for (i = 0; i < odr_types.length (); i++)
2203 if (!odr_types[i])
2204 continue;
2206 num_all_types++;
2207 if (!odr_types[i]->types || !odr_types[i]->types->length ())
2208 continue;
2210 /* To aid ODR warnings we also mangle integer constants but do
2211 not consinder duplicates there. */
2212 if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE)
2213 continue;
2215 /* It is normal to have one duplicate and one normal variant. */
2216 if (odr_types[i]->types->length () == 1
2217 && COMPLETE_TYPE_P (odr_types[i]->type)
2218 && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0]))
2219 continue;
2221 num_types ++;
2223 unsigned int j;
2224 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2225 print_node (f, "", odr_types[i]->type, 0);
2226 print_node (f, "", TYPE_NAME (odr_types[i]->type), 0);
2227 putc ('\n',f);
2228 for (j = 0; j < odr_types[i]->types->length (); j++)
2230 tree t;
2231 num_duplicates ++;
2232 fprintf (f, "duplicate #%i\n", j);
2233 print_node (f, "", (*odr_types[i]->types)[j], 0);
2234 t = (*odr_types[i]->types)[j];
2235 while (TYPE_P (t) && TYPE_CONTEXT (t))
2237 t = TYPE_CONTEXT (t);
2238 print_node (f, "", t, 0);
2240 print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0);
2241 putc ('\n',f);
2244 fprintf (f, "Out of %i types there are %i types with duplicates; "
2245 "%i duplicates overall\n", num_all_types, num_types, num_duplicates);
2248 /* Save some WPA->ltrans streaming by freeing enum values. */
2250 static void
2251 free_enum_values ()
2253 static bool enum_values_freed = false;
2254 if (enum_values_freed || !flag_wpa || !odr_types_ptr)
2255 return;
2256 enum_values_freed = true;
2257 unsigned int i;
2258 for (i = 0; i < odr_types.length (); i++)
2259 if (odr_types[i] && TREE_CODE (odr_types[i]->type) == ENUMERAL_TYPE)
2261 TYPE_VALUES (odr_types[i]->type) = NULL;
2262 if (odr_types[i]->types)
2263 for (unsigned int j = 0; j < odr_types[i]->types->length (); j++)
2264 TYPE_VALUES ((*odr_types[i]->types)[j]) = NULL;
2266 enum_values_freed = true;
2269 /* Initialize IPA devirt and build inheritance tree graph. */
2271 void
2272 build_type_inheritance_graph (void)
2274 struct symtab_node *n;
2275 FILE *inheritance_dump_file;
2276 dump_flags_t flags;
2278 if (odr_hash)
2280 free_enum_values ();
2281 return;
2283 timevar_push (TV_IPA_INHERITANCE);
2284 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2285 odr_hash = new odr_hash_type (23);
2286 if (in_lto_p)
2287 odr_vtable_hash = new odr_vtable_hash_type (23);
2289 /* We reconstruct the graph starting of types of all methods seen in the
2290 unit. */
2291 FOR_EACH_SYMBOL (n)
2292 if (is_a <cgraph_node *> (n)
2293 && DECL_VIRTUAL_P (n->decl)
2294 && n->real_symbol_p ())
2295 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2297 /* Look also for virtual tables of types that do not define any methods.
2299 We need it in a case where class B has virtual base of class A
2300 re-defining its virtual method and there is class C with no virtual
2301 methods with B as virtual base.
2303 Here we output B's virtual method in two variant - for non-virtual
2304 and virtual inheritance. B's virtual table has non-virtual version,
2305 while C's has virtual.
2307 For this reason we need to know about C in order to include both
2308 variants of B. More correctly, record_target_from_binfo should
2309 add both variants of the method when walking B, but we have no
2310 link in between them.
2312 We rely on fact that either the method is exported and thus we
2313 assume it is called externally or C is in anonymous namespace and
2314 thus we will see the vtable. */
2316 else if (is_a <varpool_node *> (n)
2317 && DECL_VIRTUAL_P (n->decl)
2318 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2319 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2320 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2321 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2322 if (inheritance_dump_file)
2324 dump_type_inheritance_graph (inheritance_dump_file);
2325 dump_end (TDI_inheritance, inheritance_dump_file);
2327 free_enum_values ();
2328 timevar_pop (TV_IPA_INHERITANCE);
2331 /* Return true if N has reference from live virtual table
2332 (and thus can be a destination of polymorphic call).
2333 Be conservatively correct when callgraph is not built or
2334 if the method may be referred externally. */
2336 static bool
2337 referenced_from_vtable_p (struct cgraph_node *node)
2339 int i;
2340 struct ipa_ref *ref;
2341 bool found = false;
2343 if (node->externally_visible
2344 || DECL_EXTERNAL (node->decl)
2345 || node->used_from_other_partition)
2346 return true;
2348 /* Keep this test constant time.
2349 It is unlikely this can happen except for the case where speculative
2350 devirtualization introduced many speculative edges to this node.
2351 In this case the target is very likely alive anyway. */
2352 if (node->ref_list.referring.length () > 100)
2353 return true;
2355 /* We need references built. */
2356 if (symtab->state <= CONSTRUCTION)
2357 return true;
2359 for (i = 0; node->iterate_referring (i, ref); i++)
2360 if ((ref->use == IPA_REF_ALIAS
2361 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2362 || (ref->use == IPA_REF_ADDR
2363 && VAR_P (ref->referring->decl)
2364 && DECL_VIRTUAL_P (ref->referring->decl)))
2366 found = true;
2367 break;
2369 return found;
2372 /* Return if TARGET is cxa_pure_virtual. */
2374 static bool
2375 is_cxa_pure_virtual_p (tree target)
2377 return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
2378 && DECL_NAME (target)
2379 && id_equal (DECL_NAME (target),
2380 "__cxa_pure_virtual");
2383 /* If TARGET has associated node, record it in the NODES array.
2384 CAN_REFER specify if program can refer to the target directly.
2385 if TARGET is unknown (NULL) or it can not be inserted (for example because
2386 its body was already removed and there is no way to refer to it), clear
2387 COMPLETEP. */
2389 static void
2390 maybe_record_node (vec <cgraph_node *> &nodes,
2391 tree target, hash_set<tree> *inserted,
2392 bool can_refer,
2393 bool *completep)
2395 struct cgraph_node *target_node, *alias_target;
2396 enum availability avail;
2397 bool pure_virtual = is_cxa_pure_virtual_p (target);
2399 /* __builtin_unreachable do not need to be added into
2400 list of targets; the runtime effect of calling them is undefined.
2401 Only "real" virtual methods should be accounted. */
2402 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
2403 return;
2405 if (!can_refer)
2407 /* The only case when method of anonymous namespace becomes unreferable
2408 is when we completely optimized it out. */
2409 if (flag_ltrans
2410 || !target
2411 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2412 *completep = false;
2413 return;
2416 if (!target)
2417 return;
2419 target_node = cgraph_node::get (target);
2421 /* Prefer alias target over aliases, so we do not get confused by
2422 fake duplicates. */
2423 if (target_node)
2425 alias_target = target_node->ultimate_alias_target (&avail);
2426 if (target_node != alias_target
2427 && avail >= AVAIL_AVAILABLE
2428 && target_node->get_availability ())
2429 target_node = alias_target;
2432 /* Method can only be called by polymorphic call if any
2433 of vtables referring to it are alive.
2435 While this holds for non-anonymous functions, too, there are
2436 cases where we want to keep them in the list; for example
2437 inline functions with -fno-weak are static, but we still
2438 may devirtualize them when instance comes from other unit.
2439 The same holds for LTO.
2441 Currently we ignore these functions in speculative devirtualization.
2442 ??? Maybe it would make sense to be more aggressive for LTO even
2443 elsewhere. */
2444 if (!flag_ltrans
2445 && !pure_virtual
2446 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2447 && (!target_node
2448 || !referenced_from_vtable_p (target_node)))
2450 /* See if TARGET is useful function we can deal with. */
2451 else if (target_node != NULL
2452 && (TREE_PUBLIC (target)
2453 || DECL_EXTERNAL (target)
2454 || target_node->definition)
2455 && target_node->real_symbol_p ())
2457 gcc_assert (!target_node->global.inlined_to);
2458 gcc_assert (target_node->real_symbol_p ());
2459 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2460 by valid program. */
2461 if (flag_sanitize & SANITIZE_UNREACHABLE)
2463 /* Only add pure virtual if it is the only possible target. This way
2464 we will preserve the diagnostics about pure virtual called in many
2465 cases without disabling optimization in other. */
2466 else if (pure_virtual)
2468 if (nodes.length ())
2469 return;
2471 /* If we found a real target, take away cxa_pure_virtual. */
2472 else if (!pure_virtual && nodes.length () == 1
2473 && is_cxa_pure_virtual_p (nodes[0]->decl))
2474 nodes.pop ();
2475 if (pure_virtual && nodes.length ())
2476 return;
2477 if (!inserted->add (target))
2479 cached_polymorphic_call_targets->add (target_node);
2480 nodes.safe_push (target_node);
2483 else if (!completep)
2485 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2486 optimized out or partitioned to other unit) so we can not add it. When
2487 not sanitizing, there is nothing to do.
2488 Otherwise declare the list incomplete. */
2489 else if (pure_virtual)
2491 if (flag_sanitize & SANITIZE_UNREACHABLE)
2492 *completep = false;
2494 else if (flag_ltrans
2495 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2496 *completep = false;
2499 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2500 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2501 method in vtable and insert method to NODES array
2502 or BASES_TO_CONSIDER if this array is non-NULL.
2503 Otherwise recurse to base BINFOs.
2504 This matches what get_binfo_at_offset does, but with offset
2505 being unknown.
2507 TYPE_BINFOS is a stack of BINFOS of types with defined
2508 virtual table seen on way from class type to BINFO.
2510 MATCHED_VTABLES tracks virtual tables we already did lookup
2511 for virtual function in. INSERTED tracks nodes we already
2512 inserted.
2514 ANONYMOUS is true if BINFO is part of anonymous namespace.
2516 Clear COMPLETEP when we hit unreferable target.
2519 static void
2520 record_target_from_binfo (vec <cgraph_node *> &nodes,
2521 vec <tree> *bases_to_consider,
2522 tree binfo,
2523 tree otr_type,
2524 vec <tree> &type_binfos,
2525 HOST_WIDE_INT otr_token,
2526 tree outer_type,
2527 HOST_WIDE_INT offset,
2528 hash_set<tree> *inserted,
2529 hash_set<tree> *matched_vtables,
2530 bool anonymous,
2531 bool *completep)
2533 tree type = BINFO_TYPE (binfo);
2534 int i;
2535 tree base_binfo;
2538 if (BINFO_VTABLE (binfo))
2539 type_binfos.safe_push (binfo);
2540 if (types_same_for_odr (type, outer_type))
2542 int i;
2543 tree type_binfo = NULL;
2545 /* Look up BINFO with virtual table. For normal types it is always last
2546 binfo on stack. */
2547 for (i = type_binfos.length () - 1; i >= 0; i--)
2548 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2550 type_binfo = type_binfos[i];
2551 break;
2553 if (BINFO_VTABLE (binfo))
2554 type_binfos.pop ();
2555 /* If this is duplicated BINFO for base shared by virtual inheritance,
2556 we may not have its associated vtable. This is not a problem, since
2557 we will walk it on the other path. */
2558 if (!type_binfo)
2559 return;
2560 tree inner_binfo = get_binfo_at_offset (type_binfo,
2561 offset, otr_type);
2562 if (!inner_binfo)
2564 gcc_assert (odr_violation_reported);
2565 return;
2567 /* For types in anonymous namespace first check if the respective vtable
2568 is alive. If not, we know the type can't be called. */
2569 if (!flag_ltrans && anonymous)
2571 tree vtable = BINFO_VTABLE (inner_binfo);
2572 varpool_node *vnode;
2574 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2575 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2576 vnode = varpool_node::get (vtable);
2577 if (!vnode || !vnode->definition)
2578 return;
2580 gcc_assert (inner_binfo);
2581 if (bases_to_consider
2582 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2583 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2585 bool can_refer;
2586 tree target = gimple_get_virt_method_for_binfo (otr_token,
2587 inner_binfo,
2588 &can_refer);
2589 if (!bases_to_consider)
2590 maybe_record_node (nodes, target, inserted, can_refer, completep);
2591 /* Destructors are never called via construction vtables. */
2592 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2593 bases_to_consider->safe_push (target);
2595 return;
2598 /* Walk bases. */
2599 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2600 /* Walking bases that have no virtual method is pointless exercise. */
2601 if (polymorphic_type_binfo_p (base_binfo))
2602 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2603 type_binfos,
2604 otr_token, outer_type, offset, inserted,
2605 matched_vtables, anonymous, completep);
2606 if (BINFO_VTABLE (binfo))
2607 type_binfos.pop ();
2610 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2611 of TYPE, insert them to NODES, recurse into derived nodes.
2612 INSERTED is used to avoid duplicate insertions of methods into NODES.
2613 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2614 Clear COMPLETEP if unreferable target is found.
2616 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2617 all cases where BASE_SKIPPED is true (because the base is abstract
2618 class). */
2620 static void
2621 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2622 hash_set<tree> *inserted,
2623 hash_set<tree> *matched_vtables,
2624 tree otr_type,
2625 odr_type type,
2626 HOST_WIDE_INT otr_token,
2627 tree outer_type,
2628 HOST_WIDE_INT offset,
2629 bool *completep,
2630 vec <tree> &bases_to_consider,
2631 bool consider_construction)
2633 tree binfo = TYPE_BINFO (type->type);
2634 unsigned int i;
2635 auto_vec <tree, 8> type_binfos;
2636 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2638 /* We may need to consider types w/o instances because of possible derived
2639 types using their methods either directly or via construction vtables.
2640 We are safe to skip them when all derivations are known, since we will
2641 handle them later.
2642 This is done by recording them to BASES_TO_CONSIDER array. */
2643 if (possibly_instantiated || consider_construction)
2645 record_target_from_binfo (nodes,
2646 (!possibly_instantiated
2647 && type_all_derivations_known_p (type->type))
2648 ? &bases_to_consider : NULL,
2649 binfo, otr_type, type_binfos, otr_token,
2650 outer_type, offset,
2651 inserted, matched_vtables,
2652 type->anonymous_namespace, completep);
2654 for (i = 0; i < type->derived_types.length (); i++)
2655 possible_polymorphic_call_targets_1 (nodes, inserted,
2656 matched_vtables,
2657 otr_type,
2658 type->derived_types[i],
2659 otr_token, outer_type, offset, completep,
2660 bases_to_consider, consider_construction);
2663 /* Cache of queries for polymorphic call targets.
2665 Enumerating all call targets may get expensive when there are many
2666 polymorphic calls in the program, so we memoize all the previous
2667 queries and avoid duplicated work. */
2669 struct polymorphic_call_target_d
2671 HOST_WIDE_INT otr_token;
2672 ipa_polymorphic_call_context context;
2673 odr_type type;
2674 vec <cgraph_node *> targets;
2675 tree decl_warning;
2676 int type_warning;
2677 bool complete;
2678 bool speculative;
2681 /* Polymorphic call target cache helpers. */
2683 struct polymorphic_call_target_hasher
2684 : pointer_hash <polymorphic_call_target_d>
2686 static inline hashval_t hash (const polymorphic_call_target_d *);
2687 static inline bool equal (const polymorphic_call_target_d *,
2688 const polymorphic_call_target_d *);
2689 static inline void remove (polymorphic_call_target_d *);
2692 /* Return the computed hashcode for ODR_QUERY. */
2694 inline hashval_t
2695 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2697 inchash::hash hstate (odr_query->otr_token);
2699 hstate.add_hwi (odr_query->type->id);
2700 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2701 hstate.add_hwi (odr_query->context.offset);
2703 if (odr_query->context.speculative_outer_type)
2705 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2706 hstate.add_hwi (odr_query->context.speculative_offset);
2708 hstate.add_flag (odr_query->speculative);
2709 hstate.add_flag (odr_query->context.maybe_in_construction);
2710 hstate.add_flag (odr_query->context.maybe_derived_type);
2711 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2712 hstate.commit_flag ();
2713 return hstate.end ();
2716 /* Compare cache entries T1 and T2. */
2718 inline bool
2719 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2720 const polymorphic_call_target_d *t2)
2722 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2723 && t1->speculative == t2->speculative
2724 && t1->context.offset == t2->context.offset
2725 && t1->context.speculative_offset == t2->context.speculative_offset
2726 && t1->context.outer_type == t2->context.outer_type
2727 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2728 && t1->context.maybe_in_construction
2729 == t2->context.maybe_in_construction
2730 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2731 && (t1->context.speculative_maybe_derived_type
2732 == t2->context.speculative_maybe_derived_type));
2735 /* Remove entry in polymorphic call target cache hash. */
2737 inline void
2738 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2740 v->targets.release ();
2741 free (v);
2744 /* Polymorphic call target query cache. */
2746 typedef hash_table<polymorphic_call_target_hasher>
2747 polymorphic_call_target_hash_type;
2748 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2750 /* Destroy polymorphic call target query cache. */
2752 static void
2753 free_polymorphic_call_targets_hash ()
2755 if (cached_polymorphic_call_targets)
2757 delete polymorphic_call_target_hash;
2758 polymorphic_call_target_hash = NULL;
2759 delete cached_polymorphic_call_targets;
2760 cached_polymorphic_call_targets = NULL;
2764 /* Force rebuilding type inheritance graph from scratch.
2765 This is use to make sure that we do not keep references to types
2766 which was not visible to free_lang_data. */
2768 void
2769 rebuild_type_inheritance_graph ()
2771 if (!odr_hash)
2772 return;
2773 delete odr_hash;
2774 if (in_lto_p)
2775 delete odr_vtable_hash;
2776 odr_hash = NULL;
2777 odr_vtable_hash = NULL;
2778 odr_types_ptr = NULL;
2779 free_polymorphic_call_targets_hash ();
2782 /* When virtual function is removed, we may need to flush the cache. */
2784 static void
2785 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2787 if (cached_polymorphic_call_targets
2788 && cached_polymorphic_call_targets->contains (n))
2789 free_polymorphic_call_targets_hash ();
2792 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2794 tree
2795 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2796 tree vtable)
2798 tree v = BINFO_VTABLE (binfo);
2799 int i;
2800 tree base_binfo;
2801 unsigned HOST_WIDE_INT this_offset;
2803 if (v)
2805 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2806 gcc_unreachable ();
2808 if (offset == this_offset
2809 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2810 return binfo;
2813 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2814 if (polymorphic_type_binfo_p (base_binfo))
2816 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2817 if (base_binfo)
2818 return base_binfo;
2820 return NULL;
2823 /* T is known constant value of virtual table pointer.
2824 Store virtual table to V and its offset to OFFSET.
2825 Return false if T does not look like virtual table reference. */
2827 bool
2828 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2829 unsigned HOST_WIDE_INT *offset)
2831 /* We expect &MEM[(void *)&virtual_table + 16B].
2832 We obtain object's BINFO from the context of the virtual table.
2833 This one contains pointer to virtual table represented via
2834 POINTER_PLUS_EXPR. Verify that this pointer matches what
2835 we propagated through.
2837 In the case of virtual inheritance, the virtual tables may
2838 be nested, i.e. the offset may be different from 16 and we may
2839 need to dive into the type representation. */
2840 if (TREE_CODE (t) == ADDR_EXPR
2841 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2842 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2843 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2844 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2845 == VAR_DECL)
2846 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2847 (TREE_OPERAND (t, 0), 0), 0)))
2849 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2850 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2851 return true;
2854 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2855 We need to handle it when T comes from static variable initializer or
2856 BINFO. */
2857 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2859 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2860 t = TREE_OPERAND (t, 0);
2862 else
2863 *offset = 0;
2865 if (TREE_CODE (t) != ADDR_EXPR)
2866 return false;
2867 *v = TREE_OPERAND (t, 0);
2868 return true;
2871 /* T is known constant value of virtual table pointer. Return BINFO of the
2872 instance type. */
2874 tree
2875 vtable_pointer_value_to_binfo (const_tree t)
2877 tree vtable;
2878 unsigned HOST_WIDE_INT offset;
2880 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2881 return NULL_TREE;
2883 /* FIXME: for stores of construction vtables we return NULL,
2884 because we do not have BINFO for those. Eventually we should fix
2885 our representation to allow this case to be handled, too.
2886 In the case we see store of BINFO we however may assume
2887 that standard folding will be able to cope with it. */
2888 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2889 offset, vtable);
2892 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2893 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2894 and insert them in NODES.
2896 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2898 static void
2899 record_targets_from_bases (tree otr_type,
2900 HOST_WIDE_INT otr_token,
2901 tree outer_type,
2902 HOST_WIDE_INT offset,
2903 vec <cgraph_node *> &nodes,
2904 hash_set<tree> *inserted,
2905 hash_set<tree> *matched_vtables,
2906 bool *completep)
2908 while (true)
2910 HOST_WIDE_INT pos, size;
2911 tree base_binfo;
2912 tree fld;
2914 if (types_same_for_odr (outer_type, otr_type))
2915 return;
2917 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2919 if (TREE_CODE (fld) != FIELD_DECL)
2920 continue;
2922 pos = int_bit_position (fld);
2923 size = tree_to_shwi (DECL_SIZE (fld));
2924 if (pos <= offset && (pos + size) > offset
2925 /* Do not get confused by zero sized bases. */
2926 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2927 break;
2929 /* Within a class type we should always find corresponding fields. */
2930 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2932 /* Nonbase types should have been stripped by outer_class_type. */
2933 gcc_assert (DECL_ARTIFICIAL (fld));
2935 outer_type = TREE_TYPE (fld);
2936 offset -= pos;
2938 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2939 offset, otr_type);
2940 if (!base_binfo)
2942 gcc_assert (odr_violation_reported);
2943 return;
2945 gcc_assert (base_binfo);
2946 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2948 bool can_refer;
2949 tree target = gimple_get_virt_method_for_binfo (otr_token,
2950 base_binfo,
2951 &can_refer);
2952 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2953 maybe_record_node (nodes, target, inserted, can_refer, completep);
2954 matched_vtables->add (BINFO_VTABLE (base_binfo));
2959 /* When virtual table is removed, we may need to flush the cache. */
2961 static void
2962 devirt_variable_node_removal_hook (varpool_node *n,
2963 void *d ATTRIBUTE_UNUSED)
2965 if (cached_polymorphic_call_targets
2966 && DECL_VIRTUAL_P (n->decl)
2967 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2968 free_polymorphic_call_targets_hash ();
2971 /* Record about how many calls would benefit from given type to be final. */
2973 struct odr_type_warn_count
2975 tree type;
2976 int count;
2977 profile_count dyn_count;
2980 /* Record about how many calls would benefit from given method to be final. */
2982 struct decl_warn_count
2984 tree decl;
2985 int count;
2986 profile_count dyn_count;
2989 /* Information about type and decl warnings. */
2991 struct final_warning_record
2993 /* If needed grow type_warnings vector and initialize new decl_warn_count
2994 to have dyn_count set to profile_count::zero (). */
2995 void grow_type_warnings (unsigned newlen);
2997 profile_count dyn_count;
2998 auto_vec<odr_type_warn_count> type_warnings;
2999 hash_map<tree, decl_warn_count> decl_warnings;
3002 void
3003 final_warning_record::grow_type_warnings (unsigned newlen)
3005 unsigned len = type_warnings.length ();
3006 if (newlen > len)
3008 type_warnings.safe_grow_cleared (newlen);
3009 for (unsigned i = len; i < newlen; i++)
3010 type_warnings[i].dyn_count = profile_count::zero ();
3014 struct final_warning_record *final_warning_records;
3016 /* Return vector containing possible targets of polymorphic call of type
3017 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3018 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3019 OTR_TYPE and include their virtual method. This is useful for types
3020 possibly in construction or destruction where the virtual table may
3021 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3022 us to walk the inheritance graph for all derivations.
3024 If COMPLETEP is non-NULL, store true if the list is complete.
3025 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3026 in the target cache. If user needs to visit every target list
3027 just once, it can memoize them.
3029 If SPECULATIVE is set, the list will not contain targets that
3030 are not speculatively taken.
3032 Returned vector is placed into cache. It is NOT caller's responsibility
3033 to free it. The vector can be freed on cgraph_remove_node call if
3034 the particular node is a virtual function present in the cache. */
3036 vec <cgraph_node *>
3037 possible_polymorphic_call_targets (tree otr_type,
3038 HOST_WIDE_INT otr_token,
3039 ipa_polymorphic_call_context context,
3040 bool *completep,
3041 void **cache_token,
3042 bool speculative)
3044 static struct cgraph_node_hook_list *node_removal_hook_holder;
3045 vec <cgraph_node *> nodes = vNULL;
3046 auto_vec <tree, 8> bases_to_consider;
3047 odr_type type, outer_type;
3048 polymorphic_call_target_d key;
3049 polymorphic_call_target_d **slot;
3050 unsigned int i;
3051 tree binfo, target;
3052 bool complete;
3053 bool can_refer = false;
3054 bool skipped = false;
3056 otr_type = TYPE_MAIN_VARIANT (otr_type);
3058 /* If ODR is not initialized or the context is invalid, return empty
3059 incomplete list. */
3060 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3062 if (completep)
3063 *completep = context.invalid;
3064 if (cache_token)
3065 *cache_token = NULL;
3066 return nodes;
3069 /* Do not bother to compute speculative info when user do not asks for it. */
3070 if (!speculative || !context.speculative_outer_type)
3071 context.clear_speculation ();
3073 type = get_odr_type (otr_type, true);
3075 /* Recording type variants would waste results cache. */
3076 gcc_assert (!context.outer_type
3077 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3079 /* Look up the outer class type we want to walk.
3080 If we fail to do so, the context is invalid. */
3081 if ((context.outer_type || context.speculative_outer_type)
3082 && !context.restrict_to_inner_class (otr_type))
3084 if (completep)
3085 *completep = true;
3086 if (cache_token)
3087 *cache_token = NULL;
3088 return nodes;
3090 gcc_assert (!context.invalid);
3092 /* Check that restrict_to_inner_class kept the main variant. */
3093 gcc_assert (!context.outer_type
3094 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3096 /* We canonicalize our query, so we do not need extra hashtable entries. */
3098 /* Without outer type, we have no use for offset. Just do the
3099 basic search from inner type. */
3100 if (!context.outer_type)
3101 context.clear_outer_type (otr_type);
3102 /* We need to update our hierarchy if the type does not exist. */
3103 outer_type = get_odr_type (context.outer_type, true);
3104 /* If the type is complete, there are no derivations. */
3105 if (TYPE_FINAL_P (outer_type->type))
3106 context.maybe_derived_type = false;
3108 /* Initialize query cache. */
3109 if (!cached_polymorphic_call_targets)
3111 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3112 polymorphic_call_target_hash
3113 = new polymorphic_call_target_hash_type (23);
3114 if (!node_removal_hook_holder)
3116 node_removal_hook_holder =
3117 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3118 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3119 NULL);
3123 if (in_lto_p)
3125 if (context.outer_type != otr_type)
3126 context.outer_type
3127 = get_odr_type (context.outer_type, true)->type;
3128 if (context.speculative_outer_type)
3129 context.speculative_outer_type
3130 = get_odr_type (context.speculative_outer_type, true)->type;
3133 /* Look up cached answer. */
3134 key.type = type;
3135 key.otr_token = otr_token;
3136 key.speculative = speculative;
3137 key.context = context;
3138 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3139 if (cache_token)
3140 *cache_token = (void *)*slot;
3141 if (*slot)
3143 if (completep)
3144 *completep = (*slot)->complete;
3145 if ((*slot)->type_warning && final_warning_records)
3147 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3148 if (!final_warning_records->type_warnings
3149 [(*slot)->type_warning - 1].dyn_count.initialized_p ())
3150 final_warning_records->type_warnings
3151 [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
3152 if (final_warning_records->dyn_count > 0)
3153 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3154 = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3155 + final_warning_records->dyn_count;
3157 if (!speculative && (*slot)->decl_warning && final_warning_records)
3159 struct decl_warn_count *c =
3160 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3161 c->count++;
3162 if (final_warning_records->dyn_count > 0)
3163 c->dyn_count += final_warning_records->dyn_count;
3165 return (*slot)->targets;
3168 complete = true;
3170 /* Do actual search. */
3171 timevar_push (TV_IPA_VIRTUAL_CALL);
3172 *slot = XCNEW (polymorphic_call_target_d);
3173 if (cache_token)
3174 *cache_token = (void *)*slot;
3175 (*slot)->type = type;
3176 (*slot)->otr_token = otr_token;
3177 (*slot)->context = context;
3178 (*slot)->speculative = speculative;
3180 hash_set<tree> inserted;
3181 hash_set<tree> matched_vtables;
3183 /* First insert targets we speculatively identified as likely. */
3184 if (context.speculative_outer_type)
3186 odr_type speculative_outer_type;
3187 bool speculation_complete = true;
3189 /* First insert target from type itself and check if it may have
3190 derived types. */
3191 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3192 if (TYPE_FINAL_P (speculative_outer_type->type))
3193 context.speculative_maybe_derived_type = false;
3194 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3195 context.speculative_offset, otr_type);
3196 if (binfo)
3197 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3198 &can_refer);
3199 else
3200 target = NULL;
3202 /* In the case we get complete method, we don't need
3203 to walk derivations. */
3204 if (target && DECL_FINAL_P (target))
3205 context.speculative_maybe_derived_type = false;
3206 if (type_possibly_instantiated_p (speculative_outer_type->type))
3207 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3208 if (binfo)
3209 matched_vtables.add (BINFO_VTABLE (binfo));
3212 /* Next walk recursively all derived types. */
3213 if (context.speculative_maybe_derived_type)
3214 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3215 possible_polymorphic_call_targets_1 (nodes, &inserted,
3216 &matched_vtables,
3217 otr_type,
3218 speculative_outer_type->derived_types[i],
3219 otr_token, speculative_outer_type->type,
3220 context.speculative_offset,
3221 &speculation_complete,
3222 bases_to_consider,
3223 false);
3226 if (!speculative || !nodes.length ())
3228 /* First see virtual method of type itself. */
3229 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3230 context.offset, otr_type);
3231 if (binfo)
3232 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3233 &can_refer);
3234 else
3236 gcc_assert (odr_violation_reported);
3237 target = NULL;
3240 /* Destructors are never called through construction virtual tables,
3241 because the type is always known. */
3242 if (target && DECL_CXX_DESTRUCTOR_P (target))
3243 context.maybe_in_construction = false;
3245 if (target)
3247 /* In the case we get complete method, we don't need
3248 to walk derivations. */
3249 if (DECL_FINAL_P (target))
3250 context.maybe_derived_type = false;
3253 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3254 if (type_possibly_instantiated_p (outer_type->type))
3255 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3256 else
3257 skipped = true;
3259 if (binfo)
3260 matched_vtables.add (BINFO_VTABLE (binfo));
3262 /* Next walk recursively all derived types. */
3263 if (context.maybe_derived_type)
3265 for (i = 0; i < outer_type->derived_types.length(); i++)
3266 possible_polymorphic_call_targets_1 (nodes, &inserted,
3267 &matched_vtables,
3268 otr_type,
3269 outer_type->derived_types[i],
3270 otr_token, outer_type->type,
3271 context.offset, &complete,
3272 bases_to_consider,
3273 context.maybe_in_construction);
3275 if (!outer_type->all_derivations_known)
3277 if (!speculative && final_warning_records
3278 && nodes.length () == 1
3279 && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
3281 if (complete
3282 && warn_suggest_final_types
3283 && !outer_type->derived_types.length ())
3285 final_warning_records->grow_type_warnings
3286 (outer_type->id);
3287 final_warning_records->type_warnings[outer_type->id].count++;
3288 if (!final_warning_records->type_warnings
3289 [outer_type->id].dyn_count.initialized_p ())
3290 final_warning_records->type_warnings
3291 [outer_type->id].dyn_count = profile_count::zero ();
3292 final_warning_records->type_warnings[outer_type->id].dyn_count
3293 += final_warning_records->dyn_count;
3294 final_warning_records->type_warnings[outer_type->id].type
3295 = outer_type->type;
3296 (*slot)->type_warning = outer_type->id + 1;
3298 if (complete
3299 && warn_suggest_final_methods
3300 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3301 outer_type->type))
3303 bool existed;
3304 struct decl_warn_count &c =
3305 final_warning_records->decl_warnings.get_or_insert
3306 (nodes[0]->decl, &existed);
3308 if (existed)
3310 c.count++;
3311 c.dyn_count += final_warning_records->dyn_count;
3313 else
3315 c.count = 1;
3316 c.dyn_count = final_warning_records->dyn_count;
3317 c.decl = nodes[0]->decl;
3319 (*slot)->decl_warning = nodes[0]->decl;
3322 complete = false;
3326 if (!speculative)
3328 /* Destructors are never called through construction virtual tables,
3329 because the type is always known. One of entries may be
3330 cxa_pure_virtual so look to at least two of them. */
3331 if (context.maybe_in_construction)
3332 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3333 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3334 context.maybe_in_construction = false;
3335 if (context.maybe_in_construction)
3337 if (type != outer_type
3338 && (!skipped
3339 || (context.maybe_derived_type
3340 && !type_all_derivations_known_p (outer_type->type))))
3341 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3342 context.offset, nodes, &inserted,
3343 &matched_vtables, &complete);
3344 if (skipped)
3345 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3346 for (i = 0; i < bases_to_consider.length(); i++)
3347 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3352 (*slot)->targets = nodes;
3353 (*slot)->complete = complete;
3354 if (completep)
3355 *completep = complete;
3357 timevar_pop (TV_IPA_VIRTUAL_CALL);
3358 return nodes;
3361 bool
3362 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3363 vec<const decl_warn_count*> *vec)
3365 vec->safe_push (&value);
3366 return true;
3369 /* Dump target list TARGETS into FILE. */
3371 static void
3372 dump_targets (FILE *f, vec <cgraph_node *> targets)
3374 unsigned int i;
3376 for (i = 0; i < targets.length (); i++)
3378 char *name = NULL;
3379 if (in_lto_p)
3380 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3381 fprintf (f, " %s/%i", name ? name : targets[i]->name (),
3382 targets[i]->order);
3383 if (in_lto_p)
3384 free (name);
3385 if (!targets[i]->definition)
3386 fprintf (f, " (no definition%s)",
3387 DECL_DECLARED_INLINE_P (targets[i]->decl)
3388 ? " inline" : "");
3390 fprintf (f, "\n");
3393 /* Dump all possible targets of a polymorphic call. */
3395 void
3396 dump_possible_polymorphic_call_targets (FILE *f,
3397 tree otr_type,
3398 HOST_WIDE_INT otr_token,
3399 const ipa_polymorphic_call_context &ctx)
3401 vec <cgraph_node *> targets;
3402 bool final;
3403 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3404 unsigned int len;
3406 if (!type)
3407 return;
3408 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3409 ctx,
3410 &final, NULL, false);
3411 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3412 print_generic_expr (f, type->type, TDF_SLIM);
3413 fprintf (f, " token %i\n", (int)otr_token);
3415 ctx.dump (f);
3417 fprintf (f, " %s%s%s%s\n ",
3418 final ? "This is a complete list." :
3419 "This is partial list; extra targets may be defined in other units.",
3420 ctx.maybe_in_construction ? " (base types included)" : "",
3421 ctx.maybe_derived_type ? " (derived types included)" : "",
3422 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3423 len = targets.length ();
3424 dump_targets (f, targets);
3426 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3427 ctx,
3428 &final, NULL, true);
3429 if (targets.length () != len)
3431 fprintf (f, " Speculative targets:");
3432 dump_targets (f, targets);
3434 /* Ugly: during callgraph construction the target cache may get populated
3435 before all targets are found. While this is harmless (because all local
3436 types are discovered and only in those case we devirtualize fully and we
3437 don't do speculative devirtualization before IPA stage) it triggers
3438 assert here when dumping at that stage also populates the case with
3439 speculative targets. Quietly ignore this. */
3440 gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
3441 fprintf (f, "\n");
3445 /* Return true if N can be possibly target of a polymorphic call of
3446 OTR_TYPE/OTR_TOKEN. */
3448 bool
3449 possible_polymorphic_call_target_p (tree otr_type,
3450 HOST_WIDE_INT otr_token,
3451 const ipa_polymorphic_call_context &ctx,
3452 struct cgraph_node *n)
3454 vec <cgraph_node *> targets;
3455 unsigned int i;
3456 enum built_in_function fcode;
3457 bool final;
3459 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3460 && ((fcode = DECL_FUNCTION_CODE (n->decl)) == BUILT_IN_UNREACHABLE
3461 || fcode == BUILT_IN_TRAP))
3462 return true;
3464 if (is_cxa_pure_virtual_p (n->decl))
3465 return true;
3467 if (!odr_hash)
3468 return true;
3469 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3470 for (i = 0; i < targets.length (); i++)
3471 if (n->semantically_equivalent_p (targets[i]))
3472 return true;
3474 /* At a moment we allow middle end to dig out new external declarations
3475 as a targets of polymorphic calls. */
3476 if (!final && !n->definition)
3477 return true;
3478 return false;
3483 /* Return true if N can be possibly target of a polymorphic call of
3484 OBJ_TYPE_REF expression REF in STMT. */
3486 bool
3487 possible_polymorphic_call_target_p (tree ref,
3488 gimple *stmt,
3489 struct cgraph_node *n)
3491 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3492 tree call_fn = gimple_call_fn (stmt);
3494 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3495 tree_to_uhwi
3496 (OBJ_TYPE_REF_TOKEN (call_fn)),
3497 context,
3502 /* After callgraph construction new external nodes may appear.
3503 Add them into the graph. */
3505 void
3506 update_type_inheritance_graph (void)
3508 struct cgraph_node *n;
3510 if (!odr_hash)
3511 return;
3512 free_polymorphic_call_targets_hash ();
3513 timevar_push (TV_IPA_INHERITANCE);
3514 /* We reconstruct the graph starting from types of all methods seen in the
3515 unit. */
3516 FOR_EACH_FUNCTION (n)
3517 if (DECL_VIRTUAL_P (n->decl)
3518 && !n->definition
3519 && n->real_symbol_p ())
3520 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3521 timevar_pop (TV_IPA_INHERITANCE);
3525 /* Return true if N looks like likely target of a polymorphic call.
3526 Rule out cxa_pure_virtual, noreturns, function declared cold and
3527 other obvious cases. */
3529 bool
3530 likely_target_p (struct cgraph_node *n)
3532 int flags;
3533 /* cxa_pure_virtual and similar things are not likely. */
3534 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3535 return false;
3536 flags = flags_from_decl_or_type (n->decl);
3537 if (flags & ECF_NORETURN)
3538 return false;
3539 if (lookup_attribute ("cold",
3540 DECL_ATTRIBUTES (n->decl)))
3541 return false;
3542 if (n->frequency < NODE_FREQUENCY_NORMAL)
3543 return false;
3544 /* If there are no live virtual tables referring the target,
3545 the only way the target can be called is an instance coming from other
3546 compilation unit; speculative devirtualization is built around an
3547 assumption that won't happen. */
3548 if (!referenced_from_vtable_p (n))
3549 return false;
3550 return true;
3553 /* Compare type warning records P1 and P2 and choose one with larger count;
3554 helper for qsort. */
3557 type_warning_cmp (const void *p1, const void *p2)
3559 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3560 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3562 if (t1->dyn_count < t2->dyn_count)
3563 return 1;
3564 if (t1->dyn_count > t2->dyn_count)
3565 return -1;
3566 return t2->count - t1->count;
3569 /* Compare decl warning records P1 and P2 and choose one with larger count;
3570 helper for qsort. */
3573 decl_warning_cmp (const void *p1, const void *p2)
3575 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3576 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3578 if (t1->dyn_count < t2->dyn_count)
3579 return 1;
3580 if (t1->dyn_count > t2->dyn_count)
3581 return -1;
3582 return t2->count - t1->count;
3586 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3587 context CTX. */
3589 struct cgraph_node *
3590 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3591 ipa_polymorphic_call_context ctx)
3593 vec <cgraph_node *>targets
3594 = possible_polymorphic_call_targets
3595 (otr_type, otr_token, ctx, NULL, NULL, true);
3596 unsigned int i;
3597 struct cgraph_node *likely_target = NULL;
3599 for (i = 0; i < targets.length (); i++)
3600 if (likely_target_p (targets[i]))
3602 if (likely_target)
3603 return NULL;
3604 likely_target = targets[i];
3606 if (!likely_target
3607 ||!likely_target->definition
3608 || DECL_EXTERNAL (likely_target->decl))
3609 return NULL;
3611 /* Don't use an implicitly-declared destructor (c++/58678). */
3612 struct cgraph_node *non_thunk_target
3613 = likely_target->function_symbol ();
3614 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3615 return NULL;
3616 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3617 && likely_target->can_be_discarded_p ())
3618 return NULL;
3619 return likely_target;
3622 /* The ipa-devirt pass.
3623 When polymorphic call has only one likely target in the unit,
3624 turn it into a speculative call. */
3626 static unsigned int
3627 ipa_devirt (void)
3629 struct cgraph_node *n;
3630 hash_set<void *> bad_call_targets;
3631 struct cgraph_edge *e;
3633 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3634 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3635 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3636 int ndropped = 0;
3638 if (!odr_types_ptr)
3639 return 0;
3641 if (dump_file)
3642 dump_type_inheritance_graph (dump_file);
3644 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3645 This is implemented by setting up final_warning_records that are updated
3646 by get_polymorphic_call_targets.
3647 We need to clear cache in this case to trigger recomputation of all
3648 entries. */
3649 if (warn_suggest_final_methods || warn_suggest_final_types)
3651 final_warning_records = new (final_warning_record);
3652 final_warning_records->dyn_count = profile_count::zero ();
3653 final_warning_records->grow_type_warnings (odr_types.length ());
3654 free_polymorphic_call_targets_hash ();
3657 FOR_EACH_DEFINED_FUNCTION (n)
3659 bool update = false;
3660 if (!opt_for_fn (n->decl, flag_devirtualize))
3661 continue;
3662 if (dump_file && n->indirect_calls)
3663 fprintf (dump_file, "\n\nProcesing function %s\n",
3664 n->dump_name ());
3665 for (e = n->indirect_calls; e; e = e->next_callee)
3666 if (e->indirect_info->polymorphic)
3668 struct cgraph_node *likely_target = NULL;
3669 void *cache_token;
3670 bool final;
3672 if (final_warning_records)
3673 final_warning_records->dyn_count = e->count.ipa ();
3675 vec <cgraph_node *>targets
3676 = possible_polymorphic_call_targets
3677 (e, &final, &cache_token, true);
3678 unsigned int i;
3680 /* Trigger warnings by calculating non-speculative targets. */
3681 if (warn_suggest_final_methods || warn_suggest_final_types)
3682 possible_polymorphic_call_targets (e);
3684 if (dump_file)
3685 dump_possible_polymorphic_call_targets
3686 (dump_file, e);
3688 npolymorphic++;
3690 /* See if the call can be devirtualized by means of ipa-prop's
3691 polymorphic call context propagation. If not, we can just
3692 forget about this call being polymorphic and avoid some heavy
3693 lifting in remove_unreachable_nodes that will otherwise try to
3694 keep all possible targets alive until inlining and in the inliner
3695 itself.
3697 This may need to be revisited once we add further ways to use
3698 the may edges, but it is a resonable thing to do right now. */
3700 if ((e->indirect_info->param_index == -1
3701 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3702 && e->indirect_info->vptr_changed))
3703 && !flag_ltrans_devirtualize)
3705 e->indirect_info->polymorphic = false;
3706 ndropped++;
3707 if (dump_file)
3708 fprintf (dump_file, "Dropping polymorphic call info;"
3709 " it can not be used by ipa-prop\n");
3712 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3713 continue;
3715 if (!e->maybe_hot_p ())
3717 if (dump_file)
3718 fprintf (dump_file, "Call is cold\n\n");
3719 ncold++;
3720 continue;
3722 if (e->speculative)
3724 if (dump_file)
3725 fprintf (dump_file, "Call is already speculated\n\n");
3726 nspeculated++;
3728 /* When dumping see if we agree with speculation. */
3729 if (!dump_file)
3730 continue;
3732 if (bad_call_targets.contains (cache_token))
3734 if (dump_file)
3735 fprintf (dump_file, "Target list is known to be useless\n\n");
3736 nmultiple++;
3737 continue;
3739 for (i = 0; i < targets.length (); i++)
3740 if (likely_target_p (targets[i]))
3742 if (likely_target)
3744 likely_target = NULL;
3745 if (dump_file)
3746 fprintf (dump_file, "More than one likely target\n\n");
3747 nmultiple++;
3748 break;
3750 likely_target = targets[i];
3752 if (!likely_target)
3754 bad_call_targets.add (cache_token);
3755 continue;
3757 /* This is reached only when dumping; check if we agree or disagree
3758 with the speculation. */
3759 if (e->speculative)
3761 struct cgraph_edge *e2;
3762 struct ipa_ref *ref;
3763 e->speculative_call_info (e2, e, ref);
3764 if (e2->callee->ultimate_alias_target ()
3765 == likely_target->ultimate_alias_target ())
3767 fprintf (dump_file, "We agree with speculation\n\n");
3768 nok++;
3770 else
3772 fprintf (dump_file, "We disagree with speculation\n\n");
3773 nwrong++;
3775 continue;
3777 if (!likely_target->definition)
3779 if (dump_file)
3780 fprintf (dump_file, "Target is not a definition\n\n");
3781 nnotdefined++;
3782 continue;
3784 /* Do not introduce new references to external symbols. While we
3785 can handle these just well, it is common for programs to
3786 incorrectly with headers defining methods they are linked
3787 with. */
3788 if (DECL_EXTERNAL (likely_target->decl))
3790 if (dump_file)
3791 fprintf (dump_file, "Target is external\n\n");
3792 nexternal++;
3793 continue;
3795 /* Don't use an implicitly-declared destructor (c++/58678). */
3796 struct cgraph_node *non_thunk_target
3797 = likely_target->function_symbol ();
3798 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3800 if (dump_file)
3801 fprintf (dump_file, "Target is artificial\n\n");
3802 nartificial++;
3803 continue;
3805 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3806 && likely_target->can_be_discarded_p ())
3808 if (dump_file)
3809 fprintf (dump_file, "Target is overwritable\n\n");
3810 noverwritable++;
3811 continue;
3813 else if (dbg_cnt (devirt))
3815 if (dump_enabled_p ())
3817 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
3818 "speculatively devirtualizing call "
3819 "in %s to %s\n",
3820 n->dump_name (),
3821 likely_target->dump_name ());
3823 if (!likely_target->can_be_discarded_p ())
3825 cgraph_node *alias;
3826 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3827 if (alias)
3828 likely_target = alias;
3830 nconverted++;
3831 update = true;
3832 e->make_speculative
3833 (likely_target, e->count.apply_scale (8, 10));
3836 if (update)
3837 ipa_update_overall_fn_summary (n);
3839 if (warn_suggest_final_methods || warn_suggest_final_types)
3841 if (warn_suggest_final_types)
3843 final_warning_records->type_warnings.qsort (type_warning_cmp);
3844 for (unsigned int i = 0;
3845 i < final_warning_records->type_warnings.length (); i++)
3846 if (final_warning_records->type_warnings[i].count)
3848 tree type = final_warning_records->type_warnings[i].type;
3849 int count = final_warning_records->type_warnings[i].count;
3850 profile_count dyn_count
3851 = final_warning_records->type_warnings[i].dyn_count;
3853 if (!(dyn_count > 0))
3854 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3855 OPT_Wsuggest_final_types, count,
3856 "Declaring type %qD final "
3857 "would enable devirtualization of %i call",
3858 "Declaring type %qD final "
3859 "would enable devirtualization of %i calls",
3860 type,
3861 count);
3862 else
3863 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3864 OPT_Wsuggest_final_types, count,
3865 "Declaring type %qD final "
3866 "would enable devirtualization of %i call "
3867 "executed %lli times",
3868 "Declaring type %qD final "
3869 "would enable devirtualization of %i calls "
3870 "executed %lli times",
3871 type,
3872 count,
3873 (long long) dyn_count.to_gcov_type ());
3877 if (warn_suggest_final_methods)
3879 auto_vec<const decl_warn_count*> decl_warnings_vec;
3881 final_warning_records->decl_warnings.traverse
3882 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3883 decl_warnings_vec.qsort (decl_warning_cmp);
3884 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3886 tree decl = decl_warnings_vec[i]->decl;
3887 int count = decl_warnings_vec[i]->count;
3888 profile_count dyn_count
3889 = decl_warnings_vec[i]->dyn_count;
3891 if (!(dyn_count > 0))
3892 if (DECL_CXX_DESTRUCTOR_P (decl))
3893 warning_n (DECL_SOURCE_LOCATION (decl),
3894 OPT_Wsuggest_final_methods, count,
3895 "Declaring virtual destructor of %qD final "
3896 "would enable devirtualization of %i call",
3897 "Declaring virtual destructor of %qD final "
3898 "would enable devirtualization of %i calls",
3899 DECL_CONTEXT (decl), count);
3900 else
3901 warning_n (DECL_SOURCE_LOCATION (decl),
3902 OPT_Wsuggest_final_methods, count,
3903 "Declaring method %qD final "
3904 "would enable devirtualization of %i call",
3905 "Declaring method %qD final "
3906 "would enable devirtualization of %i calls",
3907 decl, count);
3908 else if (DECL_CXX_DESTRUCTOR_P (decl))
3909 warning_n (DECL_SOURCE_LOCATION (decl),
3910 OPT_Wsuggest_final_methods, count,
3911 "Declaring virtual destructor of %qD final "
3912 "would enable devirtualization of %i call "
3913 "executed %lli times",
3914 "Declaring virtual destructor of %qD final "
3915 "would enable devirtualization of %i calls "
3916 "executed %lli times",
3917 DECL_CONTEXT (decl), count,
3918 (long long)dyn_count.to_gcov_type ());
3919 else
3920 warning_n (DECL_SOURCE_LOCATION (decl),
3921 OPT_Wsuggest_final_methods, count,
3922 "Declaring method %qD final "
3923 "would enable devirtualization of %i call "
3924 "executed %lli times",
3925 "Declaring method %qD final "
3926 "would enable devirtualization of %i calls "
3927 "executed %lli times",
3928 decl, count,
3929 (long long)dyn_count.to_gcov_type ());
3933 delete (final_warning_records);
3934 final_warning_records = 0;
3937 if (dump_file)
3938 fprintf (dump_file,
3939 "%i polymorphic calls, %i devirtualized,"
3940 " %i speculatively devirtualized, %i cold\n"
3941 "%i have multiple targets, %i overwritable,"
3942 " %i already speculated (%i agree, %i disagree),"
3943 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3944 npolymorphic, ndevirtualized, nconverted, ncold,
3945 nmultiple, noverwritable, nspeculated, nok, nwrong,
3946 nexternal, nnotdefined, nartificial, ndropped);
3947 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3950 namespace {
3952 const pass_data pass_data_ipa_devirt =
3954 IPA_PASS, /* type */
3955 "devirt", /* name */
3956 OPTGROUP_NONE, /* optinfo_flags */
3957 TV_IPA_DEVIRT, /* tv_id */
3958 0, /* properties_required */
3959 0, /* properties_provided */
3960 0, /* properties_destroyed */
3961 0, /* todo_flags_start */
3962 ( TODO_dump_symtab ), /* todo_flags_finish */
3965 class pass_ipa_devirt : public ipa_opt_pass_d
3967 public:
3968 pass_ipa_devirt (gcc::context *ctxt)
3969 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3970 NULL, /* generate_summary */
3971 NULL, /* write_summary */
3972 NULL, /* read_summary */
3973 NULL, /* write_optimization_summary */
3974 NULL, /* read_optimization_summary */
3975 NULL, /* stmt_fixup */
3976 0, /* function_transform_todo_flags_start */
3977 NULL, /* function_transform */
3978 NULL) /* variable_transform */
3981 /* opt_pass methods: */
3982 virtual bool gate (function *)
3984 /* In LTO, always run the IPA passes and decide on function basis if the
3985 pass is enabled. */
3986 if (in_lto_p)
3987 return true;
3988 return (flag_devirtualize
3989 && (flag_devirtualize_speculatively
3990 || (warn_suggest_final_methods
3991 || warn_suggest_final_types))
3992 && optimize);
3995 virtual unsigned int execute (function *) { return ipa_devirt (); }
3997 }; // class pass_ipa_devirt
3999 } // anon namespace
4001 ipa_opt_pass_d *
4002 make_pass_ipa_devirt (gcc::context *ctxt)
4004 return new pass_ipa_devirt (ctxt);
4007 #include "gt-ipa-devirt.h"