2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-devirt.c
blob1bf4b1acb6b401b468f41c344575a67bc06802b2
1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2015 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Brief vocabulary:
23 ODR = One Definition Rule
24 In short, the ODR states that:
25 1 In any translation unit, a template, type, function, or object can
26 have no more than one definition. Some of these can have any number
27 of declarations. A definition provides an instance.
28 2 In the entire program, an object or non-inline function cannot have
29 more than one definition; if an object or function is used, it must
30 have exactly one definition. You can declare an object or function
31 that is never used, in which case you don't have to provide
32 a definition. In no event can there be more than one definition.
33 3 Some things, like types, templates, and extern inline functions, can
34 be defined in more than one translation unit. For a given entity,
35 each definition must be the same. Non-extern objects and functions
36 in different translation units are different entities, even if their
37 names and types are the same.
39 OTR = OBJ_TYPE_REF
40 This is the Gimple representation of type information of a polymorphic call.
41 It contains two parameters:
42 otr_type is a type of class whose method is called.
43 otr_token is the index into virtual table where address is taken.
45 BINFO
46 This is the type inheritance information attached to each tree
47 RECORD_TYPE by the C++ frontend. It provides information about base
48 types and virtual tables.
50 BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
51 BINFO also links to its type by BINFO_TYPE and to the virtual table by
52 BINFO_VTABLE.
54 Base types of a given type are enumerated by BINFO_BASE_BINFO
55 vector. Members of this vectors are not BINFOs associated
56 with a base type. Rather they are new copies of BINFOs
57 (base BINFOs). Their virtual tables may differ from
58 virtual table of the base type. Also BINFO_OFFSET specifies
59 offset of the base within the type.
61 In the case of single inheritance, the virtual table is shared
62 and BINFO_VTABLE of base BINFO is NULL. In the case of multiple
63 inheritance the individual virtual tables are pointer to by
64 BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
65 binfo associated to the base type).
67 BINFO lookup for a given base type and offset can be done by
68 get_binfo_at_offset. It returns proper BINFO whose virtual table
69 can be used for lookup of virtual methods associated with the
70 base type.
72 token
73 This is an index of virtual method in virtual table associated
74 to the type defining it. Token can be looked up from OBJ_TYPE_REF
75 or from DECL_VINDEX of a given virtual table.
77 polymorphic (indirect) call
78 This is callgraph representation of virtual method call. Every
79 polymorphic call contains otr_type and otr_token taken from
80 original OBJ_TYPE_REF at callgraph construction time.
82 What we do here:
84 build_type_inheritance_graph triggers a construction of the type inheritance
85 graph.
87 We reconstruct it based on types of methods we see in the unit.
88 This means that the graph is not complete. Types with no methods are not
89 inserted into the graph. Also types without virtual methods are not
90 represented at all, though it may be easy to add this.
92 The inheritance graph is represented as follows:
94 Vertices are structures odr_type. Every odr_type may correspond
95 to one or more tree type nodes that are equivalent by ODR rule.
96 (the multiple type nodes appear only with linktime optimization)
98 Edges are represented by odr_type->base and odr_type->derived_types.
99 At the moment we do not track offsets of types for multiple inheritance.
100 Adding this is easy.
102 possible_polymorphic_call_targets returns, given an parameters found in
103 indirect polymorphic edge all possible polymorphic call targets of the call.
105 pass_ipa_devirt performs simple speculative devirtualization.
108 #include "config.h"
109 #include "system.h"
110 #include "coretypes.h"
111 #include "tm.h"
112 #include "alias.h"
113 #include "symtab.h"
114 #include "tree.h"
115 #include "fold-const.h"
116 #include "print-tree.h"
117 #include "calls.h"
118 #include "predict.h"
119 #include "basic-block.h"
120 #include "plugin-api.h"
121 #include "hard-reg-set.h"
122 #include "function.h"
123 #include "ipa-ref.h"
124 #include "cgraph.h"
125 #include "rtl.h"
126 #include "flags.h"
127 #include "insn-config.h"
128 #include "expmed.h"
129 #include "dojump.h"
130 #include "explow.h"
131 #include "emit-rtl.h"
132 #include "varasm.h"
133 #include "stmt.h"
134 #include "expr.h"
135 #include "tree-pass.h"
136 #include "target.h"
137 #include "tree-pretty-print.h"
138 #include "ipa-utils.h"
139 #include "tree-ssa-alias.h"
140 #include "internal-fn.h"
141 #include "gimple-fold.h"
142 #include "gimple-expr.h"
143 #include "gimple.h"
144 #include "alloc-pool.h"
145 #include "symbol-summary.h"
146 #include "ipa-prop.h"
147 #include "ipa-inline.h"
148 #include "diagnostic.h"
149 #include "tree-dfa.h"
150 #include "demangle.h"
151 #include "dbgcnt.h"
152 #include "gimple-pretty-print.h"
153 #include "stor-layout.h"
154 #include "intl.h"
155 #include "streamer-hooks.h"
156 #include "lto-streamer.h"
158 /* Hash based set of pairs of types. */
159 typedef struct
161 tree first;
162 tree second;
163 } type_pair;
165 struct pair_traits : default_hashset_traits
167 static hashval_t
168 hash (type_pair p)
170 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
172 static bool
173 is_empty (type_pair p)
175 return p.first == NULL;
177 static bool
178 is_deleted (type_pair p ATTRIBUTE_UNUSED)
180 return false;
182 static bool
183 equal (const type_pair &a, const type_pair &b)
185 return a.first==b.first && a.second == b.second;
187 static void
188 mark_empty (type_pair &e)
190 e.first = NULL;
194 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
195 hash_set<type_pair,pair_traits> *,
196 location_t, location_t);
198 static bool odr_violation_reported = false;
201 /* Pointer set of all call targets appearing in the cache. */
202 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
204 /* The node of type inheritance graph. For each type unique in
205 One Definition Rule (ODR) sense, we produce one node linking all
206 main variants of types equivalent to it, bases and derived types. */
208 struct GTY(()) odr_type_d
210 /* leader type. */
211 tree type;
212 /* All bases; built only for main variants of types. */
213 vec<odr_type> GTY((skip)) bases;
214 /* All derived types with virtual methods seen in unit;
215 built only for main variants of types. */
216 vec<odr_type> GTY((skip)) derived_types;
218 /* All equivalent types, if more than one. */
219 vec<tree, va_gc> *types;
220 /* Set of all equivalent types, if NON-NULL. */
221 hash_set<tree> * GTY((skip)) types_set;
223 /* Unique ID indexing the type in odr_types array. */
224 int id;
225 /* Is it in anonymous namespace? */
226 bool anonymous_namespace;
227 /* Do we know about all derivations of given type? */
228 bool all_derivations_known;
229 /* Did we report ODR violation here? */
230 bool odr_violated;
231 /* Set when virtual table without RTTI previaled table with. */
232 bool rtti_broken;
235 /* Return true if T is a type with linkage defined. */
237 bool
238 type_with_linkage_p (const_tree t)
240 /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL. */
241 if (!TYPE_CONTEXT (t)
242 || !TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL
243 || !TYPE_STUB_DECL (t))
244 return false;
246 /* In LTO do not get confused by non-C++ produced types or types built
247 with -fno-lto-odr-type-merigng. */
248 if (in_lto_p)
250 /* To support -fno-lto-odr-type-merigng recognize types with vtables
251 to have linkage. */
252 if (RECORD_OR_UNION_TYPE_P (t)
253 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
254 return true;
255 /* Do not accept any other types - we do not know if they were produced
256 by C++ FE. */
257 if (!DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
258 return false;
261 return (RECORD_OR_UNION_TYPE_P (t)
262 || TREE_CODE (t) == ENUMERAL_TYPE);
265 /* Return true if T is in anonymous namespace.
266 This works only on those C++ types with linkage defined. */
268 bool
269 type_in_anonymous_namespace_p (const_tree t)
271 gcc_assert (type_with_linkage_p (t));
273 /* Keep -fno-lto-odr-type-merging working by recognizing classes with vtables
274 properly into anonymous namespaces. */
275 if (RECORD_OR_UNION_TYPE_P (t)
276 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
277 return (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)));
279 if (TYPE_STUB_DECL (t) && !TREE_PUBLIC (TYPE_STUB_DECL (t)))
281 /* C++ FE uses magic <anon> as assembler names of anonymous types.
282 verify that this match with type_in_anonymous_namespace_p. */
283 #ifdef ENABLE_CHECKING
284 if (in_lto_p)
285 gcc_assert (!strcmp ("<anon>",
286 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t)))));
287 #endif
288 return true;
290 return false;
293 /* Return true of T is type with One Definition Rule info attached.
294 It means that either it is anonymous type or it has assembler name
295 set. */
297 bool
298 odr_type_p (const_tree t)
300 /* We do not have this information when not in LTO, but we do not need
301 to care, since it is used only for type merging. */
302 gcc_checking_assert (in_lto_p || flag_lto);
304 /* To support -fno-lto-odr-type-merging consider types with vtables ODR. */
305 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
306 return true;
308 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
309 && (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))))
311 #ifdef ENABLE_CHECKING
312 /* C++ FE uses magic <anon> as assembler names of anonymous types.
313 verify that this match with type_in_anonymous_namespace_p. */
314 gcc_assert (!type_with_linkage_p (t)
315 || strcmp ("<anon>",
316 IDENTIFIER_POINTER
317 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))))
318 || type_in_anonymous_namespace_p (t));
319 #endif
320 return true;
322 return false;
325 /* Return TRUE if all derived types of T are known and thus
326 we may consider the walk of derived type complete.
328 This is typically true only for final anonymous namespace types and types
329 defined within functions (that may be COMDAT and thus shared across units,
330 but with the same set of derived types). */
332 bool
333 type_all_derivations_known_p (const_tree t)
335 if (TYPE_FINAL_P (t))
336 return true;
337 if (flag_ltrans)
338 return false;
339 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
340 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
341 return true;
342 if (type_in_anonymous_namespace_p (t))
343 return true;
344 return (decl_function_context (TYPE_NAME (t)) != NULL);
347 /* Return TRUE if type's constructors are all visible. */
349 static bool
350 type_all_ctors_visible_p (tree t)
352 return !flag_ltrans
353 && symtab->state >= CONSTRUCTION
354 /* We can not always use type_all_derivations_known_p.
355 For function local types we must assume case where
356 the function is COMDAT and shared in between units.
358 TODO: These cases are quite easy to get, but we need
359 to keep track of C++ privatizing via -Wno-weak
360 as well as the IPA privatizing. */
361 && type_in_anonymous_namespace_p (t);
364 /* Return TRUE if type may have instance. */
366 static bool
367 type_possibly_instantiated_p (tree t)
369 tree vtable;
370 varpool_node *vnode;
372 /* TODO: Add abstract types here. */
373 if (!type_all_ctors_visible_p (t))
374 return true;
376 vtable = BINFO_VTABLE (TYPE_BINFO (t));
377 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
378 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
379 vnode = varpool_node::get (vtable);
380 return vnode && vnode->definition;
383 /* Hash used to unify ODR types based on their mangled name and for anonymous
384 namespace types. */
386 struct odr_name_hasher
388 typedef odr_type_d *value_type;
389 typedef union tree_node *compare_type;
390 static inline hashval_t hash (const odr_type_d *);
391 static inline bool equal (const odr_type_d *, const tree_node *);
392 static inline void remove (odr_type_d *);
395 /* Has used to unify ODR types based on their associated virtual table.
396 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
397 only polymorphic types. Types with mangled names are inserted to both. */
399 struct odr_vtable_hasher:odr_name_hasher
401 static inline hashval_t hash (const odr_type_d *);
402 static inline bool equal (const odr_type_d *, const tree_node *);
405 /* Return type that was declared with T's name so that T is an
406 qualified variant of it. */
408 static inline tree
409 main_odr_variant (const_tree t)
411 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
412 return TREE_TYPE (TYPE_NAME (t));
413 /* Unnamed types and non-C++ produced types can be compared by variants. */
414 else
415 return TYPE_MAIN_VARIANT (t);
418 static bool
419 can_be_name_hashed_p (tree t)
421 return (!in_lto_p || odr_type_p (t));
424 /* Hash type by its ODR name. */
426 static hashval_t
427 hash_odr_name (const_tree t)
429 gcc_checking_assert (main_odr_variant (t) == t);
431 /* If not in LTO, all main variants are unique, so we can do
432 pointer hash. */
433 if (!in_lto_p)
434 return htab_hash_pointer (t);
436 /* Anonymous types are unique. */
437 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
438 return htab_hash_pointer (t);
440 gcc_checking_assert (TYPE_NAME (t)
441 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
442 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
445 /* Return the computed hashcode for ODR_TYPE. */
447 inline hashval_t
448 odr_name_hasher::hash (const odr_type_d *odr_type)
450 return hash_odr_name (odr_type->type);
453 static bool
454 can_be_vtable_hashed_p (tree t)
456 /* vtable hashing can distinguish only main variants. */
457 if (TYPE_MAIN_VARIANT (t) != t)
458 return false;
459 /* Anonymous namespace types are always handled by name hash. */
460 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
461 return false;
462 return (TREE_CODE (t) == RECORD_TYPE
463 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
466 /* Hash type by assembler name of its vtable. */
468 static hashval_t
469 hash_odr_vtable (const_tree t)
471 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
472 inchash::hash hstate;
474 gcc_checking_assert (in_lto_p);
475 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
476 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
477 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
478 gcc_checking_assert (main_odr_variant (t) == t);
480 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
482 add_expr (TREE_OPERAND (v, 1), hstate);
483 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
486 hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
487 return hstate.end ();
490 /* Return the computed hashcode for ODR_TYPE. */
492 inline hashval_t
493 odr_vtable_hasher::hash (const odr_type_d *odr_type)
495 return hash_odr_vtable (odr_type->type);
498 /* For languages with One Definition Rule, work out if
499 types are the same based on their name.
501 This is non-trivial for LTO where minor differences in
502 the type representation may have prevented type merging
503 to merge two copies of otherwise equivalent type.
505 Until we start streaming mangled type names, this function works
506 only for polymorphic types.
508 When STRICT is true, we compare types by their names for purposes of
509 ODR violation warnings. When strict is false, we consider variants
510 equivalent, becuase it is all that matters for devirtualization machinery.
513 bool
514 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
516 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
518 type1 = main_odr_variant (type1);
519 type2 = main_odr_variant (type2);
520 if (!strict)
522 type1 = TYPE_MAIN_VARIANT (type1);
523 type2 = TYPE_MAIN_VARIANT (type2);
526 if (type1 == type2)
527 return true;
529 if (!in_lto_p)
530 return false;
532 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
533 on the corresponding TYPE_STUB_DECL. */
534 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
535 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
536 return false;
539 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
541 Ideally we should never need types without ODR names here. It can however
542 happen in two cases:
544 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
545 Here testing for equivalence is safe, since their MAIN_VARIANTs are
546 unique.
547 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
548 establish precise ODR equivalency, but for correctness we care only
549 about equivalency on complete polymorphic types. For these we can
550 compare assembler names of their virtual tables. */
551 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
552 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
554 /* See if types are obviously different (i.e. different codes
555 or polymorphic wrt non-polymorphic). This is not strictly correct
556 for ODR violating programs, but we can't do better without streaming
557 ODR names. */
558 if (TREE_CODE (type1) != TREE_CODE (type2))
559 return false;
560 if (TREE_CODE (type1) == RECORD_TYPE
561 && (TYPE_BINFO (type1) == NULL_TREE)
562 != (TYPE_BINFO (type1) == NULL_TREE))
563 return false;
564 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
565 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
566 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
567 return false;
569 /* At the moment we have no way to establish ODR equivalence at LTO
570 other than comparing virtual table pointers of polymorphic types.
571 Eventually we should start saving mangled names in TYPE_NAME.
572 Then this condition will become non-trivial. */
574 if (TREE_CODE (type1) == RECORD_TYPE
575 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
576 && BINFO_VTABLE (TYPE_BINFO (type1))
577 && BINFO_VTABLE (TYPE_BINFO (type2)))
579 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
580 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
581 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
582 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
583 return (operand_equal_p (TREE_OPERAND (v1, 1),
584 TREE_OPERAND (v2, 1), 0)
585 && DECL_ASSEMBLER_NAME
586 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
587 == DECL_ASSEMBLER_NAME
588 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
590 gcc_unreachable ();
592 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
593 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
596 /* Return true if we can decide on ODR equivalency.
598 In non-LTO it is always decide, in LTO however it depends in the type has
599 ODR info attached.
601 When STRICT is false, compare main variants. */
603 bool
604 types_odr_comparable (tree t1, tree t2, bool strict)
606 return (!in_lto_p
607 || (strict ? (main_odr_variant (t1) == main_odr_variant (t2)
608 && main_odr_variant (t1))
609 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
610 || (odr_type_p (t1) && odr_type_p (t2))
611 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
612 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
613 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
614 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
617 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
618 known, be conservative and return false. */
620 bool
621 types_must_be_same_for_odr (tree t1, tree t2)
623 if (types_odr_comparable (t1, t2))
624 return types_same_for_odr (t1, t2);
625 else
626 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
629 /* If T is compound type, return type it is based on. */
631 static tree
632 compound_type_base (const_tree t)
634 if (TREE_CODE (t) == ARRAY_TYPE
635 || POINTER_TYPE_P (t)
636 || TREE_CODE (t) == COMPLEX_TYPE
637 || VECTOR_TYPE_P (t))
638 return TREE_TYPE (t);
639 if (TREE_CODE (t) == METHOD_TYPE)
640 return TYPE_METHOD_BASETYPE (t);
641 if (TREE_CODE (t) == OFFSET_TYPE)
642 return TYPE_OFFSET_BASETYPE (t);
643 return NULL_TREE;
646 /* Return true if T is either ODR type or compound type based from it.
647 If the function return true, we know that T is a type originating from C++
648 source even at link-time. */
650 bool
651 odr_or_derived_type_p (const_tree t)
655 if (odr_type_p (t))
656 return true;
657 /* Function type is a tricky one. Basically we can consider it
658 ODR derived if return type or any of the parameters is.
659 We need to check all parameters because LTO streaming merges
660 common types (such as void) and they are not considered ODR then. */
661 if (TREE_CODE (t) == FUNCTION_TYPE)
663 if (TYPE_METHOD_BASETYPE (t))
664 t = TYPE_METHOD_BASETYPE (t);
665 else
667 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
668 return true;
669 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
670 if (odr_or_derived_type_p (TREE_VALUE (t)))
671 return true;
672 return false;
675 else
676 t = compound_type_base (t);
678 while (t);
679 return t;
682 /* Compare types T1 and T2 and return true if they are
683 equivalent. */
685 inline bool
686 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
688 tree t1 = o1->type;
690 gcc_checking_assert (main_odr_variant (t2) == t2);
691 gcc_checking_assert (main_odr_variant (t1) == t1);
692 if (t1 == t2)
693 return true;
694 if (!in_lto_p)
695 return false;
696 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
697 on the corresponding TYPE_STUB_DECL. */
698 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
699 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
700 return false;
701 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
702 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
703 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
704 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
707 /* Compare types T1 and T2 and return true if they are
708 equivalent. */
710 inline bool
711 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
713 tree t1 = o1->type;
715 gcc_checking_assert (main_odr_variant (t2) == t2);
716 gcc_checking_assert (main_odr_variant (t1) == t1);
717 gcc_checking_assert (in_lto_p);
718 t1 = TYPE_MAIN_VARIANT (t1);
719 t2 = TYPE_MAIN_VARIANT (t2);
720 if (t1 == t2)
721 return true;
722 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
723 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
724 return (operand_equal_p (TREE_OPERAND (v1, 1),
725 TREE_OPERAND (v2, 1), 0)
726 && DECL_ASSEMBLER_NAME
727 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
728 == DECL_ASSEMBLER_NAME
729 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
732 /* Free ODR type V. */
734 inline void
735 odr_name_hasher::remove (odr_type_d *v)
737 v->bases.release ();
738 v->derived_types.release ();
739 if (v->types_set)
740 delete v->types_set;
741 ggc_free (v);
744 /* ODR type hash used to look up ODR type based on tree type node. */
746 typedef hash_table<odr_name_hasher> odr_hash_type;
747 static odr_hash_type *odr_hash;
748 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
749 static odr_vtable_hash_type *odr_vtable_hash;
751 /* ODR types are also stored into ODR_TYPE vector to allow consistent
752 walking. Bases appear before derived types. Vector is garbage collected
753 so we won't end up visiting empty types. */
755 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
756 #define odr_types (*odr_types_ptr)
758 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
759 void
760 set_type_binfo (tree type, tree binfo)
762 for (; type; type = TYPE_NEXT_VARIANT (type))
763 if (COMPLETE_TYPE_P (type))
764 TYPE_BINFO (type) = binfo;
765 else
766 gcc_assert (!TYPE_BINFO (type));
769 /* Compare T2 and T2 based on name or structure. */
771 static bool
772 odr_subtypes_equivalent_p (tree t1, tree t2,
773 hash_set<type_pair,pair_traits> *visited,
774 location_t loc1, location_t loc2)
777 /* This can happen in incomplete types that should be handled earlier. */
778 gcc_assert (t1 && t2);
780 t1 = main_odr_variant (t1);
781 t2 = main_odr_variant (t2);
782 if (t1 == t2)
783 return true;
785 /* Anonymous namespace types must match exactly. */
786 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
787 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
788 return false;
790 /* For ODR types be sure to compare their names.
791 To support -wno-odr-type-merging we allow one type to be non-ODR
792 and other ODR even though it is a violation. */
793 if (types_odr_comparable (t1, t2, true))
795 if (!types_same_for_odr (t1, t2, true))
796 return false;
797 /* Limit recursion: If subtypes are ODR types and we know
798 that they are same, be happy. */
799 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated)
800 return true;
803 /* Component types, builtins and possibly violating ODR types
804 have to be compared structurally. */
805 if (TREE_CODE (t1) != TREE_CODE (t2))
806 return false;
807 if (AGGREGATE_TYPE_P (t1)
808 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
809 return false;
811 type_pair pair={t1,t2};
812 if (TYPE_UID (t1) > TYPE_UID (t2))
814 pair.first = t2;
815 pair.second = t1;
817 if (visited->add (pair))
818 return true;
819 return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2);
822 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
823 violation warnings. */
825 void
826 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
828 int n1, n2;
830 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
832 odr_violation_reported = true;
833 if (DECL_VIRTUAL_P (prevailing->decl))
835 varpool_node *tmp = prevailing;
836 prevailing = vtable;
837 vtable = tmp;
839 if (warning_at (DECL_SOURCE_LOCATION
840 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
841 OPT_Wodr,
842 "virtual table of type %qD violates one definition rule",
843 DECL_CONTEXT (vtable->decl)))
844 inform (DECL_SOURCE_LOCATION (prevailing->decl),
845 "variable of same assembler name as the virtual table is "
846 "defined in another translation unit");
847 return;
849 if (!prevailing->definition || !vtable->definition)
850 return;
852 /* If we do not stream ODR type info, do not bother to do useful compare. */
853 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
854 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
855 return;
857 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
859 if (class_type->odr_violated)
860 return;
862 for (n1 = 0, n2 = 0; true; n1++, n2++)
864 struct ipa_ref *ref1, *ref2;
865 bool end1, end2;
867 end1 = !prevailing->iterate_reference (n1, ref1);
868 end2 = !vtable->iterate_reference (n2, ref2);
870 /* !DECL_VIRTUAL_P means RTTI entry;
871 We warn when RTTI is lost because non-RTTI previals; we silently
872 accept the other case. */
873 while (!end2
874 && (end1
875 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
876 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
877 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
878 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
880 if (!class_type->rtti_broken
881 && warning_at (DECL_SOURCE_LOCATION
882 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
883 OPT_Wodr,
884 "virtual table of type %qD contains RTTI "
885 "information",
886 DECL_CONTEXT (vtable->decl)))
888 inform (DECL_SOURCE_LOCATION
889 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
890 "but is prevailed by one without from other translation "
891 "unit");
892 inform (DECL_SOURCE_LOCATION
893 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
894 "RTTI will not work on this type");
895 class_type->rtti_broken = true;
897 n2++;
898 end2 = !vtable->iterate_reference (n2, ref2);
900 while (!end1
901 && (end2
902 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
903 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
904 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
905 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
907 n1++;
908 end1 = !prevailing->iterate_reference (n1, ref1);
911 /* Finished? */
912 if (end1 && end2)
914 /* Extra paranoia; compare the sizes. We do not have information
915 about virtual inheritance offsets, so just be sure that these
916 match.
917 Do this as very last check so the not very informative error
918 is not output too often. */
919 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
921 class_type->odr_violated = true;
922 if (warning_at (DECL_SOURCE_LOCATION
923 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
924 OPT_Wodr,
925 "virtual table of type %qD violates "
926 "one definition rule ",
927 DECL_CONTEXT (vtable->decl)))
929 inform (DECL_SOURCE_LOCATION
930 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
931 "the conflicting type defined in another translation "
932 "unit has virtual table of different size");
935 return;
938 if (!end1 && !end2)
940 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
941 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
942 continue;
944 class_type->odr_violated = true;
946 /* If the loops above stopped on non-virtual pointer, we have
947 mismatch in RTTI information mangling. */
948 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
949 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
951 if (warning_at (DECL_SOURCE_LOCATION
952 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
953 OPT_Wodr,
954 "virtual table of type %qD violates "
955 "one definition rule ",
956 DECL_CONTEXT (vtable->decl)))
958 inform (DECL_SOURCE_LOCATION
959 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
960 "the conflicting type defined in another translation "
961 "unit with different RTTI information");
963 return;
965 /* At this point both REF1 and REF2 points either to virtual table
966 or virtual method. If one points to virtual table and other to
967 method we can complain the same way as if one table was shorter
968 than other pointing out the extra method. */
969 if (TREE_CODE (ref1->referred->decl)
970 != TREE_CODE (ref2->referred->decl))
972 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
973 end1 = true;
974 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
975 end2 = true;
979 class_type->odr_violated = true;
981 /* Complain about size mismatch. Either we have too many virutal
982 functions or too many virtual table pointers. */
983 if (end1 || end2)
985 if (end1)
987 varpool_node *tmp = prevailing;
988 prevailing = vtable;
989 vtable = tmp;
990 ref1 = ref2;
992 if (warning_at (DECL_SOURCE_LOCATION
993 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
994 OPT_Wodr,
995 "virtual table of type %qD violates "
996 "one definition rule",
997 DECL_CONTEXT (vtable->decl)))
999 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
1001 inform (DECL_SOURCE_LOCATION
1002 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1003 "the conflicting type defined in another translation "
1004 "unit");
1005 inform (DECL_SOURCE_LOCATION
1006 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
1007 "contains additional virtual method %qD",
1008 ref1->referred->decl);
1010 else
1012 inform (DECL_SOURCE_LOCATION
1013 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1014 "the conflicting type defined in another translation "
1015 "unit has virtual table table with more entries");
1018 return;
1021 /* And in the last case we have either mistmatch in between two virtual
1022 methods or two virtual table pointers. */
1023 if (warning_at (DECL_SOURCE_LOCATION
1024 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
1025 "virtual table of type %qD violates "
1026 "one definition rule ",
1027 DECL_CONTEXT (vtable->decl)))
1029 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
1031 inform (DECL_SOURCE_LOCATION
1032 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1033 "the conflicting type defined in another translation "
1034 "unit");
1035 gcc_assert (TREE_CODE (ref2->referred->decl)
1036 == FUNCTION_DECL);
1037 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
1038 "virtual method %qD", ref1->referred->decl);
1039 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
1040 "ought to match virtual method %qD but does not",
1041 ref2->referred->decl);
1043 else
1044 inform (DECL_SOURCE_LOCATION
1045 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
1046 "the conflicting type defined in another translation "
1047 "unit has virtual table table with different contents");
1048 return;
1053 /* Output ODR violation warning about T1 and T2 with REASON.
1054 Display location of ST1 and ST2 if REASON speaks about field or
1055 method of the type.
1056 If WARN is false, do nothing. Set WARNED if warning was indeed
1057 output. */
1059 void
1060 warn_odr (tree t1, tree t2, tree st1, tree st2,
1061 bool warn, bool *warned, const char *reason)
1063 tree decl2 = TYPE_NAME (t2);
1064 if (warned)
1065 *warned = false;
1067 if (!warn || !TYPE_NAME(t1))
1068 return;
1070 /* ODR warnings are output druing LTO streaming; we must apply location
1071 cache for potential warnings to be output correctly. */
1072 if (lto_location_cache::current_cache)
1073 lto_location_cache::current_cache->apply_location_cache ();
1075 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
1076 "type %qT violates the C++ One Definition Rule",
1077 t1))
1078 return;
1079 if (!st1 && !st2)
1081 /* For FIELD_DECL support also case where one of fields is
1082 NULL - this is used when the structures have mismatching number of
1083 elements. */
1084 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
1086 inform (DECL_SOURCE_LOCATION (decl2),
1087 "a different type is defined in another translation unit");
1088 if (!st1)
1090 st1 = st2;
1091 st2 = NULL;
1093 inform (DECL_SOURCE_LOCATION (st1),
1094 "the first difference of corresponding definitions is field %qD",
1095 st1);
1096 if (st2)
1097 decl2 = st2;
1099 else if (TREE_CODE (st1) == FUNCTION_DECL)
1101 inform (DECL_SOURCE_LOCATION (decl2),
1102 "a different type is defined in another translation unit");
1103 inform (DECL_SOURCE_LOCATION (st1),
1104 "the first difference of corresponding definitions is method %qD",
1105 st1);
1106 decl2 = st2;
1108 else
1109 return;
1110 inform (DECL_SOURCE_LOCATION (decl2), reason);
1112 if (warned)
1113 *warned = true;
1116 /* Return ture if T1 and T2 are incompatible and we want to recusively
1117 dive into them from warn_type_mismatch to give sensible answer. */
1119 static bool
1120 type_mismatch_p (tree t1, tree t2)
1122 if (odr_or_derived_type_p (t1) && odr_or_derived_type_p (t2)
1123 && !odr_types_equivalent_p (t1, t2))
1124 return true;
1125 return !types_compatible_p (t1, t2);
1129 /* Types T1 and T2 was found to be incompatible in a context they can't
1130 (either used to declare a symbol of same assembler name or unified by
1131 ODR rule). We already output warning about this, but if possible, output
1132 extra information on how the types mismatch.
1134 This is hard to do in general. We basically handle the common cases.
1136 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1137 themselves do no thave one.*/
1139 void
1140 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1142 /* Location of type is known only if it has TYPE_NAME and the name is
1143 TYPE_DECL. */
1144 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1145 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1146 : UNKNOWN_LOCATION;
1147 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1148 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1149 : UNKNOWN_LOCATION;
1150 bool loc_t2_useful = false;
1152 /* With LTO it is a common case that the location of both types match.
1153 See if T2 has a location that is different from T1. If so, we will
1154 inform user about the location.
1155 Do not consider the location passed to us in LOC1/LOC2 as those are
1156 already output. */
1157 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1159 if (loc_t1 <= BUILTINS_LOCATION)
1160 loc_t2_useful = true;
1161 else
1163 expanded_location xloc1 = expand_location (loc_t1);
1164 expanded_location xloc2 = expand_location (loc_t2);
1166 if (strcmp (xloc1.file, xloc2.file)
1167 || xloc1.line != xloc2.line
1168 || xloc1.column != xloc2.column)
1169 loc_t2_useful = true;
1173 if (loc_t1 <= BUILTINS_LOCATION)
1174 loc_t1 = loc1;
1175 if (loc_t2 <= BUILTINS_LOCATION)
1176 loc_t2 = loc2;
1178 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1180 /* It is a quite common bug to reference anonymous namespace type in
1181 non-anonymous namespace class. */
1182 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1183 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1185 if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1))
1187 std::swap (t1, t2);
1188 std::swap (loc_t1, loc_t2);
1190 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2)
1191 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1192 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL);
1193 /* Most of the time, the type names will match, do not be unnecesarily
1194 verbose. */
1195 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1)))
1196 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
1197 inform (loc_t1,
1198 "type %qT defined in anonymous namespace can not match "
1199 "type %qT across the translation unit boundary",
1200 t1, t2);
1201 else
1202 inform (loc_t1,
1203 "type %qT defined in anonymous namespace can not match "
1204 "across the translation unit boundary",
1205 t1);
1206 if (loc_t2_useful)
1207 inform (loc_t2,
1208 "the incompatible type defined in another translation unit");
1209 return;
1211 /* If types have mangled ODR names and they are different, it is most
1212 informative to output those.
1213 This also covers types defined in different namespaces. */
1214 if (TYPE_NAME (t1) && TYPE_NAME (t2)
1215 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1216 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1217 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
1218 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
1219 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
1220 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
1222 char *name1 = xstrdup (cplus_demangle
1223 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1224 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1225 char *name2 = cplus_demangle
1226 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1227 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1228 if (name1 && name2 && strcmp (name1, name2))
1230 inform (loc_t1,
1231 "type name %<%s%> should match type name %<%s%>",
1232 name1, name2);
1233 if (loc_t2_useful)
1234 inform (loc_t2,
1235 "the incompatible type is defined here");
1236 free (name1);
1237 return;
1239 free (name1);
1241 /* A tricky case are compound types. Often they appear the same in source
1242 code and the mismatch is dragged in by type they are build from.
1243 Look for those differences in subtypes and try to be informative. In other
1244 cases just output nothing because the source code is probably different
1245 and in this case we already output a all necessary info. */
1246 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1248 if (TREE_CODE (t1) == TREE_CODE (t2))
1250 if (TREE_CODE (t1) == ARRAY_TYPE
1251 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1253 tree i1 = TYPE_DOMAIN (t1);
1254 tree i2 = TYPE_DOMAIN (t2);
1256 if (i1 && i2
1257 && TYPE_MAX_VALUE (i1)
1258 && TYPE_MAX_VALUE (i2)
1259 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1260 TYPE_MAX_VALUE (i2), 0))
1262 inform (loc,
1263 "array types have different bounds");
1264 return;
1267 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1268 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1269 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1, loc_t2);
1270 else if (TREE_CODE (t1) == METHOD_TYPE
1271 || TREE_CODE (t1) == FUNCTION_TYPE)
1273 tree parms1 = NULL, parms2 = NULL;
1274 int count = 1;
1276 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1278 inform (loc, "return value type mismatch");
1279 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc_t1,
1280 loc_t2);
1281 return;
1283 if (prototype_p (t1) && prototype_p (t2))
1284 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1285 parms1 && parms2;
1286 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1287 count++)
1289 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1291 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1292 inform (loc,
1293 "implicit this pointer type mismatch");
1294 else
1295 inform (loc,
1296 "type mismatch in parameter %i",
1297 count - (TREE_CODE (t1) == METHOD_TYPE));
1298 warn_types_mismatch (TREE_VALUE (parms1),
1299 TREE_VALUE (parms2),
1300 loc_t1, loc_t2);
1301 return;
1304 if (parms1 || parms2)
1306 inform (loc,
1307 "types have different parameter counts");
1308 return;
1312 return;
1315 if (types_odr_comparable (t1, t2, true)
1316 && types_same_for_odr (t1, t2, true))
1317 inform (loc_t1,
1318 "type %qT itself violate the C++ One Definition Rule", t1);
1319 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1320 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1321 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1322 return;
1323 else
1324 inform (loc_t1, "type %qT should match type %qT",
1325 t1, t2);
1326 if (loc_t2_useful)
1327 inform (loc_t2, "the incompatible type is defined here");
1330 /* Compare T1 and T2, report ODR violations if WARN is true and set
1331 WARNED to true if anything is reported. Return true if types match.
1332 If true is returned, the types are also compatible in the sense of
1333 gimple_canonical_types_compatible_p.
1334 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1335 about the type if the type itself do not have location. */
1337 static bool
1338 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1339 hash_set<type_pair,pair_traits> *visited,
1340 location_t loc1, location_t loc2)
1342 /* Check first for the obvious case of pointer identity. */
1343 if (t1 == t2)
1344 return true;
1345 gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
1346 gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
1348 /* Can't be the same type if the types don't have the same code. */
1349 if (TREE_CODE (t1) != TREE_CODE (t2))
1351 warn_odr (t1, t2, NULL, NULL, warn, warned,
1352 G_("a different type is defined in another translation unit"));
1353 return false;
1356 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1358 warn_odr (t1, t2, NULL, NULL, warn, warned,
1359 G_("a type with different qualifiers is defined in another "
1360 "translation unit"));
1361 return false;
1364 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1365 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1367 /* We can not trip this when comparing ODR types, only when trying to
1368 match different ODR derivations from different declarations.
1369 So WARN should be always false. */
1370 gcc_assert (!warn);
1371 return false;
1374 if (comp_type_attributes (t1, t2) != 1)
1376 warn_odr (t1, t2, NULL, NULL, warn, warned,
1377 G_("a type with different attributes "
1378 "is defined in another translation unit"));
1379 return false;
1382 if (TREE_CODE (t1) == ENUMERAL_TYPE
1383 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1385 tree v1, v2;
1386 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1387 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1389 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1391 warn_odr (t1, t2, NULL, NULL, warn, warned,
1392 G_("an enum with different value name"
1393 " is defined in another translation unit"));
1394 return false;
1396 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1397 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1398 DECL_INITIAL (TREE_VALUE (v2)), 0))
1400 warn_odr (t1, t2, NULL, NULL, warn, warned,
1401 G_("an enum with different values is defined"
1402 " in another translation unit"));
1403 return false;
1406 if (v1 || v2)
1408 warn_odr (t1, t2, NULL, NULL, warn, warned,
1409 G_("an enum with mismatching number of values "
1410 "is defined in another translation unit"));
1411 return false;
1415 /* Non-aggregate types can be handled cheaply. */
1416 if (INTEGRAL_TYPE_P (t1)
1417 || SCALAR_FLOAT_TYPE_P (t1)
1418 || FIXED_POINT_TYPE_P (t1)
1419 || TREE_CODE (t1) == VECTOR_TYPE
1420 || TREE_CODE (t1) == COMPLEX_TYPE
1421 || TREE_CODE (t1) == OFFSET_TYPE
1422 || POINTER_TYPE_P (t1))
1424 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1426 warn_odr (t1, t2, NULL, NULL, warn, warned,
1427 G_("a type with different precision is defined "
1428 "in another translation unit"));
1429 return false;
1431 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1433 warn_odr (t1, t2, NULL, NULL, warn, warned,
1434 G_("a type with different signedness is defined "
1435 "in another translation unit"));
1436 return false;
1439 if (TREE_CODE (t1) == INTEGER_TYPE
1440 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1442 /* char WRT uint_8? */
1443 warn_odr (t1, t2, NULL, NULL, warn, warned,
1444 G_("a different type is defined in another "
1445 "translation unit"));
1446 return false;
1449 /* For canonical type comparisons we do not want to build SCCs
1450 so we cannot compare pointed-to types. But we can, for now,
1451 require the same pointed-to type kind and match what
1452 useless_type_conversion_p would do. */
1453 if (POINTER_TYPE_P (t1))
1455 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1456 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1458 warn_odr (t1, t2, NULL, NULL, warn, warned,
1459 G_("it is defined as a pointer in different address "
1460 "space in another translation unit"));
1461 return false;
1464 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1465 visited, loc1, loc2))
1467 warn_odr (t1, t2, NULL, NULL, warn, warned,
1468 G_("it is defined as a pointer to different type "
1469 "in another translation unit"));
1470 if (warn && warned)
1471 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1472 loc1, loc2);
1473 return false;
1477 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1478 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1479 visited, loc1, loc2))
1481 /* Probably specific enough. */
1482 warn_odr (t1, t2, NULL, NULL, warn, warned,
1483 G_("a different type is defined "
1484 "in another translation unit"));
1485 if (warn && warned)
1486 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1487 return false;
1490 /* Do type-specific comparisons. */
1491 else switch (TREE_CODE (t1))
1493 case ARRAY_TYPE:
1495 /* Array types are the same if the element types are the same and
1496 the number of elements are the same. */
1497 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1498 visited, loc1, loc2))
1500 warn_odr (t1, t2, NULL, NULL, warn, warned,
1501 G_("a different type is defined in another "
1502 "translation unit"));
1503 if (warn && warned)
1504 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1506 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1507 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1508 == TYPE_NONALIASED_COMPONENT (t2));
1510 tree i1 = TYPE_DOMAIN (t1);
1511 tree i2 = TYPE_DOMAIN (t2);
1513 /* For an incomplete external array, the type domain can be
1514 NULL_TREE. Check this condition also. */
1515 if (i1 == NULL_TREE || i2 == NULL_TREE)
1516 return true;
1518 tree min1 = TYPE_MIN_VALUE (i1);
1519 tree min2 = TYPE_MIN_VALUE (i2);
1520 tree max1 = TYPE_MAX_VALUE (i1);
1521 tree max2 = TYPE_MAX_VALUE (i2);
1523 /* In C++, minimums should be always 0. */
1524 gcc_assert (min1 == min2);
1525 if (!operand_equal_p (max1, max2, 0))
1527 warn_odr (t1, t2, NULL, NULL, warn, warned,
1528 G_("an array of different size is defined "
1529 "in another translation unit"));
1530 return false;
1533 break;
1535 case METHOD_TYPE:
1536 case FUNCTION_TYPE:
1537 /* Function types are the same if the return type and arguments types
1538 are the same. */
1539 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1540 visited, loc1, loc2))
1542 warn_odr (t1, t2, NULL, NULL, warn, warned,
1543 G_("has different return value "
1544 "in another translation unit"));
1545 if (warn && warned)
1546 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1547 return false;
1550 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1551 || !prototype_p (t1) || !prototype_p (t2))
1552 return true;
1553 else
1555 tree parms1, parms2;
1557 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1558 parms1 && parms2;
1559 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1561 if (!odr_subtypes_equivalent_p
1562 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited,
1563 loc1, loc2))
1565 warn_odr (t1, t2, NULL, NULL, warn, warned,
1566 G_("has different parameters in another "
1567 "translation unit"));
1568 if (warn && warned)
1569 warn_types_mismatch (TREE_VALUE (parms1),
1570 TREE_VALUE (parms2), loc1, loc2);
1571 return false;
1575 if (parms1 || parms2)
1577 warn_odr (t1, t2, NULL, NULL, warn, warned,
1578 G_("has different parameters "
1579 "in another translation unit"));
1580 return false;
1583 return true;
1586 case RECORD_TYPE:
1587 case UNION_TYPE:
1588 case QUAL_UNION_TYPE:
1590 tree f1, f2;
1592 /* For aggregate types, all the fields must be the same. */
1593 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1595 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1596 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1597 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1599 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1600 warn_odr (t1, t2, NULL, NULL, warn, warned,
1601 G_("a type defined in another translation unit "
1602 "is not polymorphic"));
1603 else
1604 warn_odr (t1, t2, NULL, NULL, warn, warned,
1605 G_("a type defined in another translation unit "
1606 "is polymorphic"));
1607 return false;
1609 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1610 f1 || f2;
1611 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1613 /* Skip non-fields. */
1614 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1615 f1 = TREE_CHAIN (f1);
1616 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1617 f2 = TREE_CHAIN (f2);
1618 if (!f1 || !f2)
1619 break;
1620 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1622 warn_odr (t1, t2, NULL, NULL, warn, warned,
1623 G_("a type with different virtual table pointers"
1624 " is defined in another translation unit"));
1625 return false;
1627 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1629 warn_odr (t1, t2, NULL, NULL, warn, warned,
1630 G_("a type with different bases is defined "
1631 "in another translation unit"));
1632 return false;
1634 if (DECL_NAME (f1) != DECL_NAME (f2)
1635 && !DECL_ARTIFICIAL (f1))
1637 warn_odr (t1, t2, f1, f2, warn, warned,
1638 G_("a field with different name is defined "
1639 "in another translation unit"));
1640 return false;
1642 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1643 TREE_TYPE (f2), visited,
1644 loc1, loc2))
1646 /* Do not warn about artificial fields and just go into
1647 generic field mismatch warning. */
1648 if (DECL_ARTIFICIAL (f1))
1649 break;
1651 warn_odr (t1, t2, f1, f2, warn, warned,
1652 G_("a field of same name but different type "
1653 "is defined in another translation unit"));
1654 if (warn && warned)
1655 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1656 return false;
1658 if (!gimple_compare_field_offset (f1, f2))
1660 /* Do not warn about artificial fields and just go into
1661 generic field mismatch warning. */
1662 if (DECL_ARTIFICIAL (f1))
1663 break;
1664 warn_odr (t1, t2, f1, f2, warn, warned,
1665 G_("fields has different layout "
1666 "in another translation unit"));
1667 return false;
1669 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1670 == DECL_NONADDRESSABLE_P (f2));
1673 /* If one aggregate has more fields than the other, they
1674 are not the same. */
1675 if (f1 || f2)
1677 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1678 warn_odr (t1, t2, NULL, NULL, warn, warned,
1679 G_("a type with different virtual table pointers"
1680 " is defined in another translation unit"));
1681 else if ((f1 && DECL_ARTIFICIAL (f1))
1682 || (f2 && DECL_ARTIFICIAL (f2)))
1683 warn_odr (t1, t2, NULL, NULL, warn, warned,
1684 G_("a type with different bases is defined "
1685 "in another translation unit"));
1686 else
1687 warn_odr (t1, t2, f1, f2, warn, warned,
1688 G_("a type with different number of fields "
1689 "is defined in another translation unit"));
1691 return false;
1693 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1694 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t1))
1695 && COMPLETE_TYPE_P (TYPE_MAIN_VARIANT (t2))
1696 && odr_type_p (TYPE_MAIN_VARIANT (t1))
1697 && odr_type_p (TYPE_MAIN_VARIANT (t2))
1698 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1699 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1701 /* Currently free_lang_data sets TYPE_METHODS to error_mark_node
1702 if it is non-NULL so this loop will never realy execute. */
1703 if (TYPE_METHODS (TYPE_MAIN_VARIANT (t1)) != error_mark_node
1704 && TYPE_METHODS (TYPE_MAIN_VARIANT (t2)) != error_mark_node)
1705 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1706 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1707 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1709 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1711 warn_odr (t1, t2, f1, f2, warn, warned,
1712 G_("a different method of same type "
1713 "is defined in another "
1714 "translation unit"));
1715 return false;
1717 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1719 warn_odr (t1, t2, f1, f2, warn, warned,
1720 G_("s definition that differs by virtual "
1721 "keyword in another translation unit"));
1722 return false;
1724 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1726 warn_odr (t1, t2, f1, f2, warn, warned,
1727 G_("virtual table layout differs "
1728 "in another translation unit"));
1729 return false;
1731 if (odr_subtypes_equivalent_p (TREE_TYPE (f1),
1732 TREE_TYPE (f2), visited,
1733 loc1, loc2))
1735 warn_odr (t1, t2, f1, f2, warn, warned,
1736 G_("method with incompatible type is "
1737 "defined in another translation unit"));
1738 return false;
1741 if ((f1 == NULL) != (f2 == NULL))
1743 warn_odr (t1, t2, NULL, NULL, warn, warned,
1744 G_("a type with different number of methods "
1745 "is defined in another translation unit"));
1746 return false;
1750 break;
1752 case VOID_TYPE:
1753 case NULLPTR_TYPE:
1754 break;
1756 default:
1757 debug_tree (t1);
1758 gcc_unreachable ();
1761 /* Those are better to come last as they are utterly uninformative. */
1762 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1763 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1765 warn_odr (t1, t2, NULL, NULL, warn, warned,
1766 G_("a type with different size "
1767 "is defined in another translation unit"));
1768 return false;
1770 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1771 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1773 warn_odr (t1, t2, NULL, NULL, warn, warned,
1774 G_("a type with different alignment "
1775 "is defined in another translation unit"));
1776 return false;
1778 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1779 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1780 TYPE_SIZE_UNIT (t2), 0));
1781 return true;
1784 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1786 bool
1787 odr_types_equivalent_p (tree type1, tree type2)
1789 hash_set<type_pair,pair_traits> visited;
1791 #ifdef ENABLE_CHECKING
1792 gcc_assert (odr_or_derived_type_p (type1) && odr_or_derived_type_p (type2));
1793 #endif
1794 return odr_types_equivalent_p (type1, type2, false, NULL,
1795 &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1798 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1799 from VAL->type. This may happen in LTO where tree merging did not merge
1800 all variants of the same type or due to ODR violation.
1802 Analyze and report ODR violations and add type to duplicate list.
1803 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1804 this is first time we see definition of a class return true so the
1805 base types are analyzed. */
1807 static bool
1808 add_type_duplicate (odr_type val, tree type)
1810 bool build_bases = false;
1811 bool prevail = false;
1812 bool odr_must_violate = false;
1814 if (!val->types_set)
1815 val->types_set = new hash_set<tree>;
1817 /* Chose polymorphic type as leader (this happens only in case of ODR
1818 violations. */
1819 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1820 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1821 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1822 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1824 prevail = true;
1825 build_bases = true;
1827 /* Always prefer complete type to be the leader. */
1828 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1830 prevail = true;
1831 build_bases = TYPE_BINFO (type);
1833 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1835 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1836 && TREE_CODE (type) == ENUMERAL_TYPE
1837 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1838 prevail = true;
1839 else if (TREE_CODE (val->type) == RECORD_TYPE
1840 && TREE_CODE (type) == RECORD_TYPE
1841 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1843 gcc_assert (!val->bases.length ());
1844 build_bases = true;
1845 prevail = true;
1848 if (prevail)
1849 std::swap (val->type, type);
1851 val->types_set->add (type);
1853 /* If we now have a mangled name, be sure to record it to val->type
1854 so ODR hash can work. */
1856 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1857 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1858 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1860 bool merge = true;
1861 bool base_mismatch = false;
1862 unsigned int i;
1863 bool warned = false;
1864 hash_set<type_pair,pair_traits> visited;
1866 gcc_assert (in_lto_p);
1867 vec_safe_push (val->types, type);
1869 /* If both are class types, compare the bases. */
1870 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1871 && TREE_CODE (val->type) == RECORD_TYPE
1872 && TREE_CODE (type) == RECORD_TYPE
1873 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1875 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1876 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1878 if (!flag_ltrans && !warned && !val->odr_violated)
1880 tree extra_base;
1881 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1882 "a type with the same name but different "
1883 "number of polymorphic bases is "
1884 "defined in another translation unit");
1885 if (warned)
1887 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1888 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1889 extra_base = BINFO_BASE_BINFO
1890 (TYPE_BINFO (type),
1891 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1892 else
1893 extra_base = BINFO_BASE_BINFO
1894 (TYPE_BINFO (val->type),
1895 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1896 tree extra_base_type = BINFO_TYPE (extra_base);
1897 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1898 "the extra base is defined here");
1901 base_mismatch = true;
1903 else
1904 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1906 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1907 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1908 tree type1 = BINFO_TYPE (base1);
1909 tree type2 = BINFO_TYPE (base2);
1911 if (types_odr_comparable (type1, type2))
1913 if (!types_same_for_odr (type1, type2))
1914 base_mismatch = true;
1916 else
1917 if (!odr_types_equivalent_p (type1, type2))
1918 base_mismatch = true;
1919 if (base_mismatch)
1921 if (!warned && !val->odr_violated)
1923 warn_odr (type, val->type, NULL, NULL,
1924 !warned, &warned,
1925 "a type with the same name but different base "
1926 "type is defined in another translation unit");
1927 if (warned)
1928 warn_types_mismatch (type1, type2,
1929 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1931 break;
1933 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1935 base_mismatch = true;
1936 if (!warned && !val->odr_violated)
1937 warn_odr (type, val->type, NULL, NULL,
1938 !warned, &warned,
1939 "a type with the same name but different base "
1940 "layout is defined in another translation unit");
1941 break;
1943 /* One of bases is not of complete type. */
1944 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1946 /* If we have a polymorphic type info specified for TYPE1
1947 but not for TYPE2 we possibly missed a base when recording
1948 VAL->type earlier.
1949 Be sure this does not happen. */
1950 if (TYPE_BINFO (type1)
1951 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1952 && !build_bases)
1953 odr_must_violate = true;
1954 break;
1956 /* One base is polymorphic and the other not.
1957 This ought to be diagnosed earlier, but do not ICE in the
1958 checking bellow. */
1959 else if (TYPE_BINFO (type1)
1960 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1961 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1963 if (!warned && !val->odr_violated)
1964 warn_odr (type, val->type, NULL, NULL,
1965 !warned, &warned,
1966 "a base of the type is polymorphic only in one "
1967 "translation unit");
1968 base_mismatch = true;
1969 break;
1972 if (base_mismatch)
1974 merge = false;
1975 odr_violation_reported = true;
1976 val->odr_violated = true;
1978 if (symtab->dump_file)
1980 fprintf (symtab->dump_file, "ODR base violation\n");
1982 print_node (symtab->dump_file, "", val->type, 0);
1983 putc ('\n',symtab->dump_file);
1984 print_node (symtab->dump_file, "", type, 0);
1985 putc ('\n',symtab->dump_file);
1990 /* Next compare memory layout. */
1991 if (!odr_types_equivalent_p (val->type, type,
1992 !flag_ltrans && !val->odr_violated && !warned,
1993 &warned, &visited,
1994 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1995 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1997 merge = false;
1998 odr_violation_reported = true;
1999 val->odr_violated = true;
2000 if (symtab->dump_file)
2002 fprintf (symtab->dump_file, "ODR violation\n");
2004 print_node (symtab->dump_file, "", val->type, 0);
2005 putc ('\n',symtab->dump_file);
2006 print_node (symtab->dump_file, "", type, 0);
2007 putc ('\n',symtab->dump_file);
2010 gcc_assert (val->odr_violated || !odr_must_violate);
2011 /* Sanity check that all bases will be build same way again. */
2012 #ifdef ENABLE_CHECKING
2013 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
2014 && TREE_CODE (val->type) == RECORD_TYPE
2015 && TREE_CODE (type) == RECORD_TYPE
2016 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
2017 && !val->odr_violated
2018 && !base_mismatch && val->bases.length ())
2020 unsigned int num_poly_bases = 0;
2021 unsigned int j;
2023 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
2024 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
2025 (TYPE_BINFO (type), i)))
2026 num_poly_bases++;
2027 gcc_assert (num_poly_bases == val->bases.length ());
2028 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
2029 i++)
2030 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
2031 (TYPE_BINFO (type), i)))
2033 odr_type base = get_odr_type
2034 (BINFO_TYPE
2035 (BINFO_BASE_BINFO (TYPE_BINFO (type),
2036 i)),
2037 true);
2038 gcc_assert (val->bases[j] == base);
2039 j++;
2042 #endif
2045 /* Regularize things a little. During LTO same types may come with
2046 different BINFOs. Either because their virtual table was
2047 not merged by tree merging and only later at decl merging or
2048 because one type comes with external vtable, while other
2049 with internal. We want to merge equivalent binfos to conserve
2050 memory and streaming overhead.
2052 The external vtables are more harmful: they contain references
2053 to external declarations of methods that may be defined in the
2054 merged LTO unit. For this reason we absolutely need to remove
2055 them and replace by internal variants. Not doing so will lead
2056 to incomplete answers from possible_polymorphic_call_targets.
2058 FIXME: disable for now; because ODR types are now build during
2059 streaming in, the variants do not need to be linked to the type,
2060 yet. We need to do the merging in cleanup pass to be implemented
2061 soon. */
2062 if (!flag_ltrans && merge
2063 && 0
2064 && TREE_CODE (val->type) == RECORD_TYPE
2065 && TREE_CODE (type) == RECORD_TYPE
2066 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
2067 && TYPE_MAIN_VARIANT (type) == type
2068 && TYPE_MAIN_VARIANT (val->type) == val->type
2069 && BINFO_VTABLE (TYPE_BINFO (val->type))
2070 && BINFO_VTABLE (TYPE_BINFO (type)))
2072 tree master_binfo = TYPE_BINFO (val->type);
2073 tree v1 = BINFO_VTABLE (master_binfo);
2074 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
2076 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
2078 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
2079 && operand_equal_p (TREE_OPERAND (v1, 1),
2080 TREE_OPERAND (v2, 1), 0));
2081 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
2082 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
2084 gcc_assert (DECL_ASSEMBLER_NAME (v1)
2085 == DECL_ASSEMBLER_NAME (v2));
2087 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
2089 unsigned int i;
2091 set_type_binfo (val->type, TYPE_BINFO (type));
2092 for (i = 0; i < val->types->length (); i++)
2094 if (TYPE_BINFO ((*val->types)[i])
2095 == master_binfo)
2096 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
2098 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
2100 else
2101 set_type_binfo (type, master_binfo);
2103 return build_bases;
2106 /* Get ODR type hash entry for TYPE. If INSERT is true, create
2107 possibly new entry. */
2109 odr_type
2110 get_odr_type (tree type, bool insert)
2112 odr_type_d **slot = NULL;
2113 odr_type_d **vtable_slot = NULL;
2114 odr_type val = NULL;
2115 hashval_t hash;
2116 bool build_bases = false;
2117 bool insert_to_odr_array = false;
2118 int base_id = -1;
2120 type = main_odr_variant (type);
2122 gcc_checking_assert (can_be_name_hashed_p (type)
2123 || can_be_vtable_hashed_p (type));
2125 /* Lookup entry, first try name hash, fallback to vtable hash. */
2126 if (can_be_name_hashed_p (type))
2128 hash = hash_odr_name (type);
2129 slot = odr_hash->find_slot_with_hash (type, hash,
2130 insert ? INSERT : NO_INSERT);
2132 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2134 hash = hash_odr_vtable (type);
2135 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2136 insert ? INSERT : NO_INSERT);
2139 if (!slot && !vtable_slot)
2140 return NULL;
2142 /* See if we already have entry for type. */
2143 if ((slot && *slot) || (vtable_slot && *vtable_slot))
2145 if (slot && *slot)
2147 val = *slot;
2148 #ifdef ENABLE_CHECKING
2149 if (in_lto_p && can_be_vtable_hashed_p (type))
2151 hash = hash_odr_vtable (type);
2152 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2153 NO_INSERT);
2154 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2155 vtable_slot = NULL;
2157 #endif
2159 else if (*vtable_slot)
2160 val = *vtable_slot;
2162 if (val->type != type
2163 && (!val->types_set || !val->types_set->add (type)))
2165 gcc_assert (insert);
2166 /* We have type duplicate, but it may introduce vtable name or
2167 mangled name; be sure to keep hashes in sync. */
2168 if (in_lto_p && can_be_vtable_hashed_p (type)
2169 && (!vtable_slot || !*vtable_slot))
2171 if (!vtable_slot)
2173 hash = hash_odr_vtable (type);
2174 vtable_slot = odr_vtable_hash->find_slot_with_hash
2175 (type, hash, INSERT);
2176 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2178 *vtable_slot = val;
2180 if (slot && !*slot)
2181 *slot = val;
2182 build_bases = add_type_duplicate (val, type);
2185 else
2187 val = ggc_cleared_alloc<odr_type_d> ();
2188 val->type = type;
2189 val->bases = vNULL;
2190 val->derived_types = vNULL;
2191 if (type_with_linkage_p (type))
2192 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2193 else
2194 val->anonymous_namespace = 0;
2195 build_bases = COMPLETE_TYPE_P (val->type);
2196 insert_to_odr_array = true;
2197 if (slot)
2198 *slot = val;
2199 if (vtable_slot)
2200 *vtable_slot = val;
2203 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2204 && type_with_linkage_p (type)
2205 && type == TYPE_MAIN_VARIANT (type))
2207 tree binfo = TYPE_BINFO (type);
2208 unsigned int i;
2210 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2212 val->all_derivations_known = type_all_derivations_known_p (type);
2213 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2214 /* For now record only polymorphic types. other are
2215 pointless for devirtualization and we can not precisely
2216 determine ODR equivalency of these during LTO. */
2217 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2219 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2220 odr_type base = get_odr_type (base_type, true);
2221 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2222 base->derived_types.safe_push (val);
2223 val->bases.safe_push (base);
2224 if (base->id > base_id)
2225 base_id = base->id;
2228 /* Ensure that type always appears after bases. */
2229 if (insert_to_odr_array)
2231 if (odr_types_ptr)
2232 val->id = odr_types.length ();
2233 vec_safe_push (odr_types_ptr, val);
2235 else if (base_id > val->id)
2237 odr_types[val->id] = 0;
2238 /* Be sure we did not recorded any derived types; these may need
2239 renumbering too. */
2240 gcc_assert (val->derived_types.length() == 0);
2241 if (odr_types_ptr)
2242 val->id = odr_types.length ();
2243 vec_safe_push (odr_types_ptr, val);
2245 return val;
2248 /* Add TYPE od ODR type hash. */
2250 void
2251 register_odr_type (tree type)
2253 if (!odr_hash)
2255 odr_hash = new odr_hash_type (23);
2256 if (in_lto_p)
2257 odr_vtable_hash = new odr_vtable_hash_type (23);
2259 /* Arrange things to be nicer and insert main variants first.
2260 ??? fundamental prerecorded types do not have mangled names; this
2261 makes it possible that non-ODR type is main_odr_variant of ODR type.
2262 Things may get smoother if LTO FE set mangled name of those types same
2263 way as C++ FE does. */
2264 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type)))
2265 && odr_type_p (TYPE_MAIN_VARIANT (type)))
2266 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2267 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type)))
2268 get_odr_type (type, true);
2271 /* Return true if type is known to have no derivations. */
2273 bool
2274 type_known_to_have_no_derivations_p (tree t)
2276 return (type_all_derivations_known_p (t)
2277 && (TYPE_FINAL_P (t)
2278 || (odr_hash
2279 && !get_odr_type (t, true)->derived_types.length())));
2282 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2283 recursive printing. */
2285 static void
2286 dump_odr_type (FILE *f, odr_type t, int indent=0)
2288 unsigned int i;
2289 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2290 print_generic_expr (f, t->type, TDF_SLIM);
2291 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2292 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2293 if (TYPE_NAME (t->type))
2295 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2296 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2297 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2298 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2299 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2300 IDENTIFIER_POINTER
2301 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2303 if (t->bases.length ())
2305 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2306 for (i = 0; i < t->bases.length (); i++)
2307 fprintf (f, " %i", t->bases[i]->id);
2308 fprintf (f, "\n");
2310 if (t->derived_types.length ())
2312 fprintf (f, "%*s derived types:\n", indent * 2, "");
2313 for (i = 0; i < t->derived_types.length (); i++)
2314 dump_odr_type (f, t->derived_types[i], indent + 1);
2316 fprintf (f, "\n");
2319 /* Dump the type inheritance graph. */
2321 static void
2322 dump_type_inheritance_graph (FILE *f)
2324 unsigned int i;
2325 if (!odr_types_ptr)
2326 return;
2327 fprintf (f, "\n\nType inheritance graph:\n");
2328 for (i = 0; i < odr_types.length (); i++)
2330 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2331 dump_odr_type (f, odr_types[i]);
2333 for (i = 0; i < odr_types.length (); i++)
2335 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2337 unsigned int j;
2338 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2339 print_node (f, "", odr_types[i]->type, 0);
2340 for (j = 0; j < odr_types[i]->types->length (); j++)
2342 tree t;
2343 fprintf (f, "duplicate #%i\n", j);
2344 print_node (f, "", (*odr_types[i]->types)[j], 0);
2345 t = (*odr_types[i]->types)[j];
2346 while (TYPE_P (t) && TYPE_CONTEXT (t))
2348 t = TYPE_CONTEXT (t);
2349 print_node (f, "", t, 0);
2351 putc ('\n',f);
2357 /* Initialize IPA devirt and build inheritance tree graph. */
2359 void
2360 build_type_inheritance_graph (void)
2362 struct symtab_node *n;
2363 FILE *inheritance_dump_file;
2364 int flags;
2366 if (odr_hash)
2367 return;
2368 timevar_push (TV_IPA_INHERITANCE);
2369 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2370 odr_hash = new odr_hash_type (23);
2371 if (in_lto_p)
2372 odr_vtable_hash = new odr_vtable_hash_type (23);
2374 /* We reconstruct the graph starting of types of all methods seen in the
2375 the unit. */
2376 FOR_EACH_SYMBOL (n)
2377 if (is_a <cgraph_node *> (n)
2378 && DECL_VIRTUAL_P (n->decl)
2379 && n->real_symbol_p ())
2380 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
2382 /* Look also for virtual tables of types that do not define any methods.
2384 We need it in a case where class B has virtual base of class A
2385 re-defining its virtual method and there is class C with no virtual
2386 methods with B as virtual base.
2388 Here we output B's virtual method in two variant - for non-virtual
2389 and virtual inheritance. B's virtual table has non-virtual version,
2390 while C's has virtual.
2392 For this reason we need to know about C in order to include both
2393 variants of B. More correctly, record_target_from_binfo should
2394 add both variants of the method when walking B, but we have no
2395 link in between them.
2397 We rely on fact that either the method is exported and thus we
2398 assume it is called externally or C is in anonymous namespace and
2399 thus we will see the vtable. */
2401 else if (is_a <varpool_node *> (n)
2402 && DECL_VIRTUAL_P (n->decl)
2403 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2404 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2405 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2406 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2407 if (inheritance_dump_file)
2409 dump_type_inheritance_graph (inheritance_dump_file);
2410 dump_end (TDI_inheritance, inheritance_dump_file);
2412 timevar_pop (TV_IPA_INHERITANCE);
2415 /* Return true if N has reference from live virtual table
2416 (and thus can be a destination of polymorphic call).
2417 Be conservatively correct when callgraph is not built or
2418 if the method may be referred externally. */
2420 static bool
2421 referenced_from_vtable_p (struct cgraph_node *node)
2423 int i;
2424 struct ipa_ref *ref;
2425 bool found = false;
2427 if (node->externally_visible
2428 || DECL_EXTERNAL (node->decl)
2429 || node->used_from_other_partition)
2430 return true;
2432 /* Keep this test constant time.
2433 It is unlikely this can happen except for the case where speculative
2434 devirtualization introduced many speculative edges to this node.
2435 In this case the target is very likely alive anyway. */
2436 if (node->ref_list.referring.length () > 100)
2437 return true;
2439 /* We need references built. */
2440 if (symtab->state <= CONSTRUCTION)
2441 return true;
2443 for (i = 0; node->iterate_referring (i, ref); i++)
2444 if ((ref->use == IPA_REF_ALIAS
2445 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2446 || (ref->use == IPA_REF_ADDR
2447 && TREE_CODE (ref->referring->decl) == VAR_DECL
2448 && DECL_VIRTUAL_P (ref->referring->decl)))
2450 found = true;
2451 break;
2453 return found;
2456 /* If TARGET has associated node, record it in the NODES array.
2457 CAN_REFER specify if program can refer to the target directly.
2458 if TARGET is unknown (NULL) or it can not be inserted (for example because
2459 its body was already removed and there is no way to refer to it), clear
2460 COMPLETEP. */
2462 static void
2463 maybe_record_node (vec <cgraph_node *> &nodes,
2464 tree target, hash_set<tree> *inserted,
2465 bool can_refer,
2466 bool *completep)
2468 struct cgraph_node *target_node, *alias_target;
2469 enum availability avail;
2471 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2472 list of targets; the runtime effect of calling them is undefined.
2473 Only "real" virtual methods should be accounted. */
2474 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
2475 return;
2477 if (!can_refer)
2479 /* The only case when method of anonymous namespace becomes unreferable
2480 is when we completely optimized it out. */
2481 if (flag_ltrans
2482 || !target
2483 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2484 *completep = false;
2485 return;
2488 if (!target)
2489 return;
2491 target_node = cgraph_node::get (target);
2493 /* Prefer alias target over aliases, so we do not get confused by
2494 fake duplicates. */
2495 if (target_node)
2497 alias_target = target_node->ultimate_alias_target (&avail);
2498 if (target_node != alias_target
2499 && avail >= AVAIL_AVAILABLE
2500 && target_node->get_availability ())
2501 target_node = alias_target;
2504 /* Method can only be called by polymorphic call if any
2505 of vtables referring to it are alive.
2507 While this holds for non-anonymous functions, too, there are
2508 cases where we want to keep them in the list; for example
2509 inline functions with -fno-weak are static, but we still
2510 may devirtualize them when instance comes from other unit.
2511 The same holds for LTO.
2513 Currently we ignore these functions in speculative devirtualization.
2514 ??? Maybe it would make sense to be more aggressive for LTO even
2515 elsewhere. */
2516 if (!flag_ltrans
2517 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2518 && (!target_node
2519 || !referenced_from_vtable_p (target_node)))
2521 /* See if TARGET is useful function we can deal with. */
2522 else if (target_node != NULL
2523 && (TREE_PUBLIC (target)
2524 || DECL_EXTERNAL (target)
2525 || target_node->definition)
2526 && target_node->real_symbol_p ())
2528 gcc_assert (!target_node->global.inlined_to);
2529 gcc_assert (target_node->real_symbol_p ());
2530 if (!inserted->add (target))
2532 cached_polymorphic_call_targets->add (target_node);
2533 nodes.safe_push (target_node);
2536 else if (completep
2537 && (!type_in_anonymous_namespace_p
2538 (DECL_CONTEXT (target))
2539 || flag_ltrans))
2540 *completep = false;
2543 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2544 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2545 method in vtable and insert method to NODES array
2546 or BASES_TO_CONSIDER if this array is non-NULL.
2547 Otherwise recurse to base BINFOs.
2548 This matches what get_binfo_at_offset does, but with offset
2549 being unknown.
2551 TYPE_BINFOS is a stack of BINFOS of types with defined
2552 virtual table seen on way from class type to BINFO.
2554 MATCHED_VTABLES tracks virtual tables we already did lookup
2555 for virtual function in. INSERTED tracks nodes we already
2556 inserted.
2558 ANONYMOUS is true if BINFO is part of anonymous namespace.
2560 Clear COMPLETEP when we hit unreferable target.
2563 static void
2564 record_target_from_binfo (vec <cgraph_node *> &nodes,
2565 vec <tree> *bases_to_consider,
2566 tree binfo,
2567 tree otr_type,
2568 vec <tree> &type_binfos,
2569 HOST_WIDE_INT otr_token,
2570 tree outer_type,
2571 HOST_WIDE_INT offset,
2572 hash_set<tree> *inserted,
2573 hash_set<tree> *matched_vtables,
2574 bool anonymous,
2575 bool *completep)
2577 tree type = BINFO_TYPE (binfo);
2578 int i;
2579 tree base_binfo;
2582 if (BINFO_VTABLE (binfo))
2583 type_binfos.safe_push (binfo);
2584 if (types_same_for_odr (type, outer_type))
2586 int i;
2587 tree type_binfo = NULL;
2589 /* Look up BINFO with virtual table. For normal types it is always last
2590 binfo on stack. */
2591 for (i = type_binfos.length () - 1; i >= 0; i--)
2592 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2594 type_binfo = type_binfos[i];
2595 break;
2597 if (BINFO_VTABLE (binfo))
2598 type_binfos.pop ();
2599 /* If this is duplicated BINFO for base shared by virtual inheritance,
2600 we may not have its associated vtable. This is not a problem, since
2601 we will walk it on the other path. */
2602 if (!type_binfo)
2603 return;
2604 tree inner_binfo = get_binfo_at_offset (type_binfo,
2605 offset, otr_type);
2606 if (!inner_binfo)
2608 gcc_assert (odr_violation_reported);
2609 return;
2611 /* For types in anonymous namespace first check if the respective vtable
2612 is alive. If not, we know the type can't be called. */
2613 if (!flag_ltrans && anonymous)
2615 tree vtable = BINFO_VTABLE (inner_binfo);
2616 varpool_node *vnode;
2618 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2619 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2620 vnode = varpool_node::get (vtable);
2621 if (!vnode || !vnode->definition)
2622 return;
2624 gcc_assert (inner_binfo);
2625 if (bases_to_consider
2626 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2627 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2629 bool can_refer;
2630 tree target = gimple_get_virt_method_for_binfo (otr_token,
2631 inner_binfo,
2632 &can_refer);
2633 if (!bases_to_consider)
2634 maybe_record_node (nodes, target, inserted, can_refer, completep);
2635 /* Destructors are never called via construction vtables. */
2636 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2637 bases_to_consider->safe_push (target);
2639 return;
2642 /* Walk bases. */
2643 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2644 /* Walking bases that have no virtual method is pointless exercise. */
2645 if (polymorphic_type_binfo_p (base_binfo))
2646 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2647 type_binfos,
2648 otr_token, outer_type, offset, inserted,
2649 matched_vtables, anonymous, completep);
2650 if (BINFO_VTABLE (binfo))
2651 type_binfos.pop ();
2654 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2655 of TYPE, insert them to NODES, recurse into derived nodes.
2656 INSERTED is used to avoid duplicate insertions of methods into NODES.
2657 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2658 Clear COMPLETEP if unreferable target is found.
2660 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2661 all cases where BASE_SKIPPED is true (because the base is abstract
2662 class). */
2664 static void
2665 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2666 hash_set<tree> *inserted,
2667 hash_set<tree> *matched_vtables,
2668 tree otr_type,
2669 odr_type type,
2670 HOST_WIDE_INT otr_token,
2671 tree outer_type,
2672 HOST_WIDE_INT offset,
2673 bool *completep,
2674 vec <tree> &bases_to_consider,
2675 bool consider_construction)
2677 tree binfo = TYPE_BINFO (type->type);
2678 unsigned int i;
2679 auto_vec <tree, 8> type_binfos;
2680 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2682 /* We may need to consider types w/o instances because of possible derived
2683 types using their methods either directly or via construction vtables.
2684 We are safe to skip them when all derivations are known, since we will
2685 handle them later.
2686 This is done by recording them to BASES_TO_CONSIDER array. */
2687 if (possibly_instantiated || consider_construction)
2689 record_target_from_binfo (nodes,
2690 (!possibly_instantiated
2691 && type_all_derivations_known_p (type->type))
2692 ? &bases_to_consider : NULL,
2693 binfo, otr_type, type_binfos, otr_token,
2694 outer_type, offset,
2695 inserted, matched_vtables,
2696 type->anonymous_namespace, completep);
2698 for (i = 0; i < type->derived_types.length (); i++)
2699 possible_polymorphic_call_targets_1 (nodes, inserted,
2700 matched_vtables,
2701 otr_type,
2702 type->derived_types[i],
2703 otr_token, outer_type, offset, completep,
2704 bases_to_consider, consider_construction);
2707 /* Cache of queries for polymorphic call targets.
2709 Enumerating all call targets may get expensive when there are many
2710 polymorphic calls in the program, so we memoize all the previous
2711 queries and avoid duplicated work. */
2713 struct polymorphic_call_target_d
2715 HOST_WIDE_INT otr_token;
2716 ipa_polymorphic_call_context context;
2717 odr_type type;
2718 vec <cgraph_node *> targets;
2719 tree decl_warning;
2720 int type_warning;
2721 bool complete;
2722 bool speculative;
2725 /* Polymorphic call target cache helpers. */
2727 struct polymorphic_call_target_hasher
2729 typedef polymorphic_call_target_d *value_type;
2730 typedef polymorphic_call_target_d *compare_type;
2731 static inline hashval_t hash (const polymorphic_call_target_d *);
2732 static inline bool equal (const polymorphic_call_target_d *,
2733 const polymorphic_call_target_d *);
2734 static inline void remove (polymorphic_call_target_d *);
2737 /* Return the computed hashcode for ODR_QUERY. */
2739 inline hashval_t
2740 polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2742 inchash::hash hstate (odr_query->otr_token);
2744 hstate.add_wide_int (odr_query->type->id);
2745 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2746 hstate.add_wide_int (odr_query->context.offset);
2748 if (odr_query->context.speculative_outer_type)
2750 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2751 hstate.add_wide_int (odr_query->context.speculative_offset);
2753 hstate.add_flag (odr_query->speculative);
2754 hstate.add_flag (odr_query->context.maybe_in_construction);
2755 hstate.add_flag (odr_query->context.maybe_derived_type);
2756 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2757 hstate.commit_flag ();
2758 return hstate.end ();
2761 /* Compare cache entries T1 and T2. */
2763 inline bool
2764 polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2765 const polymorphic_call_target_d *t2)
2767 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2768 && t1->speculative == t2->speculative
2769 && t1->context.offset == t2->context.offset
2770 && t1->context.speculative_offset == t2->context.speculative_offset
2771 && t1->context.outer_type == t2->context.outer_type
2772 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2773 && t1->context.maybe_in_construction
2774 == t2->context.maybe_in_construction
2775 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2776 && (t1->context.speculative_maybe_derived_type
2777 == t2->context.speculative_maybe_derived_type));
2780 /* Remove entry in polymorphic call target cache hash. */
2782 inline void
2783 polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2785 v->targets.release ();
2786 free (v);
2789 /* Polymorphic call target query cache. */
2791 typedef hash_table<polymorphic_call_target_hasher>
2792 polymorphic_call_target_hash_type;
2793 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2795 /* Destroy polymorphic call target query cache. */
2797 static void
2798 free_polymorphic_call_targets_hash ()
2800 if (cached_polymorphic_call_targets)
2802 delete polymorphic_call_target_hash;
2803 polymorphic_call_target_hash = NULL;
2804 delete cached_polymorphic_call_targets;
2805 cached_polymorphic_call_targets = NULL;
2809 /* When virtual function is removed, we may need to flush the cache. */
2811 static void
2812 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2814 if (cached_polymorphic_call_targets
2815 && cached_polymorphic_call_targets->contains (n))
2816 free_polymorphic_call_targets_hash ();
2819 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2821 tree
2822 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2823 tree vtable)
2825 tree v = BINFO_VTABLE (binfo);
2826 int i;
2827 tree base_binfo;
2828 unsigned HOST_WIDE_INT this_offset;
2830 if (v)
2832 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2833 gcc_unreachable ();
2835 if (offset == this_offset
2836 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2837 return binfo;
2840 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2841 if (polymorphic_type_binfo_p (base_binfo))
2843 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2844 if (base_binfo)
2845 return base_binfo;
2847 return NULL;
2850 /* T is known constant value of virtual table pointer.
2851 Store virtual table to V and its offset to OFFSET.
2852 Return false if T does not look like virtual table reference. */
2854 bool
2855 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2856 unsigned HOST_WIDE_INT *offset)
2858 /* We expect &MEM[(void *)&virtual_table + 16B].
2859 We obtain object's BINFO from the context of the virtual table.
2860 This one contains pointer to virtual table represented via
2861 POINTER_PLUS_EXPR. Verify that this pointer matches what
2862 we propagated through.
2864 In the case of virtual inheritance, the virtual tables may
2865 be nested, i.e. the offset may be different from 16 and we may
2866 need to dive into the type representation. */
2867 if (TREE_CODE (t) == ADDR_EXPR
2868 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2869 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2870 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2871 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2872 == VAR_DECL)
2873 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2874 (TREE_OPERAND (t, 0), 0), 0)))
2876 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2877 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2878 return true;
2881 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2882 We need to handle it when T comes from static variable initializer or
2883 BINFO. */
2884 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2886 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2887 t = TREE_OPERAND (t, 0);
2889 else
2890 *offset = 0;
2892 if (TREE_CODE (t) != ADDR_EXPR)
2893 return false;
2894 *v = TREE_OPERAND (t, 0);
2895 return true;
2898 /* T is known constant value of virtual table pointer. Return BINFO of the
2899 instance type. */
2901 tree
2902 vtable_pointer_value_to_binfo (const_tree t)
2904 tree vtable;
2905 unsigned HOST_WIDE_INT offset;
2907 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2908 return NULL_TREE;
2910 /* FIXME: for stores of construction vtables we return NULL,
2911 because we do not have BINFO for those. Eventually we should fix
2912 our representation to allow this case to be handled, too.
2913 In the case we see store of BINFO we however may assume
2914 that standard folding will be able to cope with it. */
2915 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2916 offset, vtable);
2919 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2920 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2921 and insert them in NODES.
2923 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2925 static void
2926 record_targets_from_bases (tree otr_type,
2927 HOST_WIDE_INT otr_token,
2928 tree outer_type,
2929 HOST_WIDE_INT offset,
2930 vec <cgraph_node *> &nodes,
2931 hash_set<tree> *inserted,
2932 hash_set<tree> *matched_vtables,
2933 bool *completep)
2935 while (true)
2937 HOST_WIDE_INT pos, size;
2938 tree base_binfo;
2939 tree fld;
2941 if (types_same_for_odr (outer_type, otr_type))
2942 return;
2944 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2946 if (TREE_CODE (fld) != FIELD_DECL)
2947 continue;
2949 pos = int_bit_position (fld);
2950 size = tree_to_shwi (DECL_SIZE (fld));
2951 if (pos <= offset && (pos + size) > offset
2952 /* Do not get confused by zero sized bases. */
2953 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2954 break;
2956 /* Within a class type we should always find corresponding fields. */
2957 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2959 /* Nonbase types should have been stripped by outer_class_type. */
2960 gcc_assert (DECL_ARTIFICIAL (fld));
2962 outer_type = TREE_TYPE (fld);
2963 offset -= pos;
2965 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2966 offset, otr_type);
2967 if (!base_binfo)
2969 gcc_assert (odr_violation_reported);
2970 return;
2972 gcc_assert (base_binfo);
2973 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2975 bool can_refer;
2976 tree target = gimple_get_virt_method_for_binfo (otr_token,
2977 base_binfo,
2978 &can_refer);
2979 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2980 maybe_record_node (nodes, target, inserted, can_refer, completep);
2981 matched_vtables->add (BINFO_VTABLE (base_binfo));
2986 /* When virtual table is removed, we may need to flush the cache. */
2988 static void
2989 devirt_variable_node_removal_hook (varpool_node *n,
2990 void *d ATTRIBUTE_UNUSED)
2992 if (cached_polymorphic_call_targets
2993 && DECL_VIRTUAL_P (n->decl)
2994 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2995 free_polymorphic_call_targets_hash ();
2998 /* Record about how many calls would benefit from given type to be final. */
3000 struct odr_type_warn_count
3002 tree type;
3003 int count;
3004 gcov_type dyn_count;
3007 /* Record about how many calls would benefit from given method to be final. */
3009 struct decl_warn_count
3011 tree decl;
3012 int count;
3013 gcov_type dyn_count;
3016 /* Information about type and decl warnings. */
3018 struct final_warning_record
3020 gcov_type dyn_count;
3021 vec<odr_type_warn_count> type_warnings;
3022 hash_map<tree, decl_warn_count> decl_warnings;
3024 struct final_warning_record *final_warning_records;
3026 /* Return vector containing possible targets of polymorphic call of type
3027 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3028 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3029 OTR_TYPE and include their virtual method. This is useful for types
3030 possibly in construction or destruction where the virtual table may
3031 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
3032 us to walk the inheritance graph for all derivations.
3034 If COMPLETEP is non-NULL, store true if the list is complete.
3035 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3036 in the target cache. If user needs to visit every target list
3037 just once, it can memoize them.
3039 If SPECULATIVE is set, the list will not contain targets that
3040 are not speculatively taken.
3042 Returned vector is placed into cache. It is NOT caller's responsibility
3043 to free it. The vector can be freed on cgraph_remove_node call if
3044 the particular node is a virtual function present in the cache. */
3046 vec <cgraph_node *>
3047 possible_polymorphic_call_targets (tree otr_type,
3048 HOST_WIDE_INT otr_token,
3049 ipa_polymorphic_call_context context,
3050 bool *completep,
3051 void **cache_token,
3052 bool speculative)
3054 static struct cgraph_node_hook_list *node_removal_hook_holder;
3055 vec <cgraph_node *> nodes = vNULL;
3056 auto_vec <tree, 8> bases_to_consider;
3057 odr_type type, outer_type;
3058 polymorphic_call_target_d key;
3059 polymorphic_call_target_d **slot;
3060 unsigned int i;
3061 tree binfo, target;
3062 bool complete;
3063 bool can_refer = false;
3064 bool skipped = false;
3066 otr_type = TYPE_MAIN_VARIANT (otr_type);
3068 /* If ODR is not initialized or the context is invalid, return empty
3069 incomplete list. */
3070 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3072 if (completep)
3073 *completep = context.invalid;
3074 if (cache_token)
3075 *cache_token = NULL;
3076 return nodes;
3079 /* Do not bother to compute speculative info when user do not asks for it. */
3080 if (!speculative || !context.speculative_outer_type)
3081 context.clear_speculation ();
3083 type = get_odr_type (otr_type, true);
3085 /* Recording type variants would waste results cache. */
3086 gcc_assert (!context.outer_type
3087 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3089 /* Look up the outer class type we want to walk.
3090 If we fail to do so, the context is invalid. */
3091 if ((context.outer_type || context.speculative_outer_type)
3092 && !context.restrict_to_inner_class (otr_type))
3094 if (completep)
3095 *completep = true;
3096 if (cache_token)
3097 *cache_token = NULL;
3098 return nodes;
3100 gcc_assert (!context.invalid);
3102 /* Check that restrict_to_inner_class kept the main variant. */
3103 gcc_assert (!context.outer_type
3104 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3106 /* We canonicalize our query, so we do not need extra hashtable entries. */
3108 /* Without outer type, we have no use for offset. Just do the
3109 basic search from inner type. */
3110 if (!context.outer_type)
3111 context.clear_outer_type (otr_type);
3112 /* We need to update our hierarchy if the type does not exist. */
3113 outer_type = get_odr_type (context.outer_type, true);
3114 /* If the type is complete, there are no derivations. */
3115 if (TYPE_FINAL_P (outer_type->type))
3116 context.maybe_derived_type = false;
3118 /* Initialize query cache. */
3119 if (!cached_polymorphic_call_targets)
3121 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3122 polymorphic_call_target_hash
3123 = new polymorphic_call_target_hash_type (23);
3124 if (!node_removal_hook_holder)
3126 node_removal_hook_holder =
3127 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
3128 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
3129 NULL);
3133 if (in_lto_p)
3135 if (context.outer_type != otr_type)
3136 context.outer_type
3137 = get_odr_type (context.outer_type, true)->type;
3138 if (context.speculative_outer_type)
3139 context.speculative_outer_type
3140 = get_odr_type (context.speculative_outer_type, true)->type;
3143 /* Look up cached answer. */
3144 key.type = type;
3145 key.otr_token = otr_token;
3146 key.speculative = speculative;
3147 key.context = context;
3148 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3149 if (cache_token)
3150 *cache_token = (void *)*slot;
3151 if (*slot)
3153 if (completep)
3154 *completep = (*slot)->complete;
3155 if ((*slot)->type_warning && final_warning_records)
3157 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3158 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3159 += final_warning_records->dyn_count;
3161 if (!speculative && (*slot)->decl_warning && final_warning_records)
3163 struct decl_warn_count *c =
3164 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
3165 c->count++;
3166 c->dyn_count += final_warning_records->dyn_count;
3168 return (*slot)->targets;
3171 complete = true;
3173 /* Do actual search. */
3174 timevar_push (TV_IPA_VIRTUAL_CALL);
3175 *slot = XCNEW (polymorphic_call_target_d);
3176 if (cache_token)
3177 *cache_token = (void *)*slot;
3178 (*slot)->type = type;
3179 (*slot)->otr_token = otr_token;
3180 (*slot)->context = context;
3181 (*slot)->speculative = speculative;
3183 hash_set<tree> inserted;
3184 hash_set<tree> matched_vtables;
3186 /* First insert targets we speculatively identified as likely. */
3187 if (context.speculative_outer_type)
3189 odr_type speculative_outer_type;
3190 bool speculation_complete = true;
3192 /* First insert target from type itself and check if it may have
3193 derived types. */
3194 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
3195 if (TYPE_FINAL_P (speculative_outer_type->type))
3196 context.speculative_maybe_derived_type = false;
3197 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3198 context.speculative_offset, otr_type);
3199 if (binfo)
3200 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3201 &can_refer);
3202 else
3203 target = NULL;
3205 /* In the case we get complete method, we don't need
3206 to walk derivations. */
3207 if (target && DECL_FINAL_P (target))
3208 context.speculative_maybe_derived_type = false;
3209 if (type_possibly_instantiated_p (speculative_outer_type->type))
3210 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
3211 if (binfo)
3212 matched_vtables.add (BINFO_VTABLE (binfo));
3215 /* Next walk recursively all derived types. */
3216 if (context.speculative_maybe_derived_type)
3217 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3218 possible_polymorphic_call_targets_1 (nodes, &inserted,
3219 &matched_vtables,
3220 otr_type,
3221 speculative_outer_type->derived_types[i],
3222 otr_token, speculative_outer_type->type,
3223 context.speculative_offset,
3224 &speculation_complete,
3225 bases_to_consider,
3226 false);
3229 if (!speculative || !nodes.length ())
3231 /* First see virtual method of type itself. */
3232 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3233 context.offset, otr_type);
3234 if (binfo)
3235 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3236 &can_refer);
3237 else
3239 gcc_assert (odr_violation_reported);
3240 target = NULL;
3243 /* Destructors are never called through construction virtual tables,
3244 because the type is always known. */
3245 if (target && DECL_CXX_DESTRUCTOR_P (target))
3246 context.maybe_in_construction = false;
3248 if (target)
3250 /* In the case we get complete method, we don't need
3251 to walk derivations. */
3252 if (DECL_FINAL_P (target))
3253 context.maybe_derived_type = false;
3256 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3257 if (type_possibly_instantiated_p (outer_type->type))
3258 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3259 else
3260 skipped = true;
3262 if (binfo)
3263 matched_vtables.add (BINFO_VTABLE (binfo));
3265 /* Next walk recursively all derived types. */
3266 if (context.maybe_derived_type)
3268 for (i = 0; i < outer_type->derived_types.length(); i++)
3269 possible_polymorphic_call_targets_1 (nodes, &inserted,
3270 &matched_vtables,
3271 otr_type,
3272 outer_type->derived_types[i],
3273 otr_token, outer_type->type,
3274 context.offset, &complete,
3275 bases_to_consider,
3276 context.maybe_in_construction);
3278 if (!outer_type->all_derivations_known)
3280 if (!speculative && final_warning_records)
3282 if (complete
3283 && nodes.length () == 1
3284 && warn_suggest_final_types
3285 && !outer_type->derived_types.length ())
3287 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3288 final_warning_records->type_warnings.safe_grow_cleared
3289 (odr_types.length ());
3290 final_warning_records->type_warnings[outer_type->id].count++;
3291 final_warning_records->type_warnings[outer_type->id].dyn_count
3292 += final_warning_records->dyn_count;
3293 final_warning_records->type_warnings[outer_type->id].type
3294 = outer_type->type;
3295 (*slot)->type_warning = outer_type->id + 1;
3297 if (complete
3298 && warn_suggest_final_methods
3299 && nodes.length () == 1
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 (), targets[i]->order);
3382 if (in_lto_p)
3383 free (name);
3384 if (!targets[i]->definition)
3385 fprintf (f, " (no definition%s)",
3386 DECL_DECLARED_INLINE_P (targets[i]->decl)
3387 ? " inline" : "");
3389 fprintf (f, "\n");
3392 /* Dump all possible targets of a polymorphic call. */
3394 void
3395 dump_possible_polymorphic_call_targets (FILE *f,
3396 tree otr_type,
3397 HOST_WIDE_INT otr_token,
3398 const ipa_polymorphic_call_context &ctx)
3400 vec <cgraph_node *> targets;
3401 bool final;
3402 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3403 unsigned int len;
3405 if (!type)
3406 return;
3407 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3408 ctx,
3409 &final, NULL, false);
3410 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3411 print_generic_expr (f, type->type, TDF_SLIM);
3412 fprintf (f, " token %i\n", (int)otr_token);
3414 ctx.dump (f);
3416 fprintf (f, " %s%s%s%s\n ",
3417 final ? "This is a complete list." :
3418 "This is partial list; extra targets may be defined in other units.",
3419 ctx.maybe_in_construction ? " (base types included)" : "",
3420 ctx.maybe_derived_type ? " (derived types included)" : "",
3421 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3422 len = targets.length ();
3423 dump_targets (f, targets);
3425 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3426 ctx,
3427 &final, NULL, true);
3428 if (targets.length () != len)
3430 fprintf (f, " Speculative targets:");
3431 dump_targets (f, targets);
3433 gcc_assert (targets.length () <= len);
3434 fprintf (f, "\n");
3438 /* Return true if N can be possibly target of a polymorphic call of
3439 OTR_TYPE/OTR_TOKEN. */
3441 bool
3442 possible_polymorphic_call_target_p (tree otr_type,
3443 HOST_WIDE_INT otr_token,
3444 const ipa_polymorphic_call_context &ctx,
3445 struct cgraph_node *n)
3447 vec <cgraph_node *> targets;
3448 unsigned int i;
3449 enum built_in_function fcode;
3450 bool final;
3452 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3453 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3454 == BUILT_IN_UNREACHABLE
3455 || fcode == BUILT_IN_TRAP))
3456 return true;
3458 if (!odr_hash)
3459 return true;
3460 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3461 for (i = 0; i < targets.length (); i++)
3462 if (n->semantically_equivalent_p (targets[i]))
3463 return true;
3465 /* At a moment we allow middle end to dig out new external declarations
3466 as a targets of polymorphic calls. */
3467 if (!final && !n->definition)
3468 return true;
3469 return false;
3474 /* Return true if N can be possibly target of a polymorphic call of
3475 OBJ_TYPE_REF expression REF in STMT. */
3477 bool
3478 possible_polymorphic_call_target_p (tree ref,
3479 gimple stmt,
3480 struct cgraph_node *n)
3482 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3483 tree call_fn = gimple_call_fn (stmt);
3485 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3486 tree_to_uhwi
3487 (OBJ_TYPE_REF_TOKEN (call_fn)),
3488 context,
3493 /* After callgraph construction new external nodes may appear.
3494 Add them into the graph. */
3496 void
3497 update_type_inheritance_graph (void)
3499 struct cgraph_node *n;
3501 if (!odr_hash)
3502 return;
3503 free_polymorphic_call_targets_hash ();
3504 timevar_push (TV_IPA_INHERITANCE);
3505 /* We reconstruct the graph starting from types of all methods seen in the
3506 the unit. */
3507 FOR_EACH_FUNCTION (n)
3508 if (DECL_VIRTUAL_P (n->decl)
3509 && !n->definition
3510 && n->real_symbol_p ())
3511 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), true);
3512 timevar_pop (TV_IPA_INHERITANCE);
3516 /* Return true if N looks like likely target of a polymorphic call.
3517 Rule out cxa_pure_virtual, noreturns, function declared cold and
3518 other obvious cases. */
3520 bool
3521 likely_target_p (struct cgraph_node *n)
3523 int flags;
3524 /* cxa_pure_virtual and similar things are not likely. */
3525 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3526 return false;
3527 flags = flags_from_decl_or_type (n->decl);
3528 if (flags & ECF_NORETURN)
3529 return false;
3530 if (lookup_attribute ("cold",
3531 DECL_ATTRIBUTES (n->decl)))
3532 return false;
3533 if (n->frequency < NODE_FREQUENCY_NORMAL)
3534 return false;
3535 /* If there are no live virtual tables referring the target,
3536 the only way the target can be called is an instance coming from other
3537 compilation unit; speculative devirtualization is built around an
3538 assumption that won't happen. */
3539 if (!referenced_from_vtable_p (n))
3540 return false;
3541 return true;
3544 /* Compare type warning records P1 and P2 and choose one with larger count;
3545 helper for qsort. */
3548 type_warning_cmp (const void *p1, const void *p2)
3550 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3551 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3553 if (t1->dyn_count < t2->dyn_count)
3554 return 1;
3555 if (t1->dyn_count > t2->dyn_count)
3556 return -1;
3557 return t2->count - t1->count;
3560 /* Compare decl warning records P1 and P2 and choose one with larger count;
3561 helper for qsort. */
3564 decl_warning_cmp (const void *p1, const void *p2)
3566 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3567 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3569 if (t1->dyn_count < t2->dyn_count)
3570 return 1;
3571 if (t1->dyn_count > t2->dyn_count)
3572 return -1;
3573 return t2->count - t1->count;
3577 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3578 context CTX. */
3580 struct cgraph_node *
3581 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3582 ipa_polymorphic_call_context ctx)
3584 vec <cgraph_node *>targets
3585 = possible_polymorphic_call_targets
3586 (otr_type, otr_token, ctx, NULL, NULL, true);
3587 unsigned int i;
3588 struct cgraph_node *likely_target = NULL;
3590 for (i = 0; i < targets.length (); i++)
3591 if (likely_target_p (targets[i]))
3593 if (likely_target)
3594 return NULL;
3595 likely_target = targets[i];
3597 if (!likely_target
3598 ||!likely_target->definition
3599 || DECL_EXTERNAL (likely_target->decl))
3600 return NULL;
3602 /* Don't use an implicitly-declared destructor (c++/58678). */
3603 struct cgraph_node *non_thunk_target
3604 = likely_target->function_symbol ();
3605 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3606 return NULL;
3607 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3608 && likely_target->can_be_discarded_p ())
3609 return NULL;
3610 return likely_target;
3613 /* The ipa-devirt pass.
3614 When polymorphic call has only one likely target in the unit,
3615 turn it into a speculative call. */
3617 static unsigned int
3618 ipa_devirt (void)
3620 struct cgraph_node *n;
3621 hash_set<void *> bad_call_targets;
3622 struct cgraph_edge *e;
3624 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3625 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3626 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3627 int ndropped = 0;
3629 if (!odr_types_ptr)
3630 return 0;
3632 if (dump_file)
3633 dump_type_inheritance_graph (dump_file);
3635 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3636 This is implemented by setting up final_warning_records that are updated
3637 by get_polymorphic_call_targets.
3638 We need to clear cache in this case to trigger recomputation of all
3639 entries. */
3640 if (warn_suggest_final_methods || warn_suggest_final_types)
3642 final_warning_records = new (final_warning_record);
3643 final_warning_records->type_warnings = vNULL;
3644 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3645 free_polymorphic_call_targets_hash ();
3648 FOR_EACH_DEFINED_FUNCTION (n)
3650 bool update = false;
3651 if (!opt_for_fn (n->decl, flag_devirtualize))
3652 continue;
3653 if (dump_file && n->indirect_calls)
3654 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3655 n->name (), n->order);
3656 for (e = n->indirect_calls; e; e = e->next_callee)
3657 if (e->indirect_info->polymorphic)
3659 struct cgraph_node *likely_target = NULL;
3660 void *cache_token;
3661 bool final;
3663 if (final_warning_records)
3664 final_warning_records->dyn_count = e->count;
3666 vec <cgraph_node *>targets
3667 = possible_polymorphic_call_targets
3668 (e, &final, &cache_token, true);
3669 unsigned int i;
3671 /* Trigger warnings by calculating non-speculative targets. */
3672 if (warn_suggest_final_methods || warn_suggest_final_types)
3673 possible_polymorphic_call_targets (e);
3675 if (dump_file)
3676 dump_possible_polymorphic_call_targets
3677 (dump_file, e);
3679 npolymorphic++;
3681 /* See if the call can be devirtualized by means of ipa-prop's
3682 polymorphic call context propagation. If not, we can just
3683 forget about this call being polymorphic and avoid some heavy
3684 lifting in remove_unreachable_nodes that will otherwise try to
3685 keep all possible targets alive until inlining and in the inliner
3686 itself.
3688 This may need to be revisited once we add further ways to use
3689 the may edges, but it is a resonable thing to do right now. */
3691 if ((e->indirect_info->param_index == -1
3692 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3693 && e->indirect_info->vptr_changed))
3694 && !flag_ltrans_devirtualize)
3696 e->indirect_info->polymorphic = false;
3697 ndropped++;
3698 if (dump_file)
3699 fprintf (dump_file, "Dropping polymorphic call info;"
3700 " it can not be used by ipa-prop\n");
3703 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3704 continue;
3706 if (!e->maybe_hot_p ())
3708 if (dump_file)
3709 fprintf (dump_file, "Call is cold\n\n");
3710 ncold++;
3711 continue;
3713 if (e->speculative)
3715 if (dump_file)
3716 fprintf (dump_file, "Call is already speculated\n\n");
3717 nspeculated++;
3719 /* When dumping see if we agree with speculation. */
3720 if (!dump_file)
3721 continue;
3723 if (bad_call_targets.contains (cache_token))
3725 if (dump_file)
3726 fprintf (dump_file, "Target list is known to be useless\n\n");
3727 nmultiple++;
3728 continue;
3730 for (i = 0; i < targets.length (); i++)
3731 if (likely_target_p (targets[i]))
3733 if (likely_target)
3735 likely_target = NULL;
3736 if (dump_file)
3737 fprintf (dump_file, "More than one likely target\n\n");
3738 nmultiple++;
3739 break;
3741 likely_target = targets[i];
3743 if (!likely_target)
3745 bad_call_targets.add (cache_token);
3746 continue;
3748 /* This is reached only when dumping; check if we agree or disagree
3749 with the speculation. */
3750 if (e->speculative)
3752 struct cgraph_edge *e2;
3753 struct ipa_ref *ref;
3754 e->speculative_call_info (e2, e, ref);
3755 if (e2->callee->ultimate_alias_target ()
3756 == likely_target->ultimate_alias_target ())
3758 fprintf (dump_file, "We agree with speculation\n\n");
3759 nok++;
3761 else
3763 fprintf (dump_file, "We disagree with speculation\n\n");
3764 nwrong++;
3766 continue;
3768 if (!likely_target->definition)
3770 if (dump_file)
3771 fprintf (dump_file, "Target is not a definition\n\n");
3772 nnotdefined++;
3773 continue;
3775 /* Do not introduce new references to external symbols. While we
3776 can handle these just well, it is common for programs to
3777 incorrectly with headers defining methods they are linked
3778 with. */
3779 if (DECL_EXTERNAL (likely_target->decl))
3781 if (dump_file)
3782 fprintf (dump_file, "Target is external\n\n");
3783 nexternal++;
3784 continue;
3786 /* Don't use an implicitly-declared destructor (c++/58678). */
3787 struct cgraph_node *non_thunk_target
3788 = likely_target->function_symbol ();
3789 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3791 if (dump_file)
3792 fprintf (dump_file, "Target is artificial\n\n");
3793 nartificial++;
3794 continue;
3796 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3797 && likely_target->can_be_discarded_p ())
3799 if (dump_file)
3800 fprintf (dump_file, "Target is overwritable\n\n");
3801 noverwritable++;
3802 continue;
3804 else if (dbg_cnt (devirt))
3806 if (dump_enabled_p ())
3808 location_t locus = gimple_location_safe (e->call_stmt);
3809 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3810 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3811 n->name (), n->order,
3812 likely_target->name (),
3813 likely_target->order);
3815 if (!likely_target->can_be_discarded_p ())
3817 cgraph_node *alias;
3818 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3819 if (alias)
3820 likely_target = alias;
3822 nconverted++;
3823 update = true;
3824 e->make_speculative
3825 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3828 if (update)
3829 inline_update_overall_summary (n);
3831 if (warn_suggest_final_methods || warn_suggest_final_types)
3833 if (warn_suggest_final_types)
3835 final_warning_records->type_warnings.qsort (type_warning_cmp);
3836 for (unsigned int i = 0;
3837 i < final_warning_records->type_warnings.length (); i++)
3838 if (final_warning_records->type_warnings[i].count)
3840 tree type = final_warning_records->type_warnings[i].type;
3841 int count = final_warning_records->type_warnings[i].count;
3842 long long dyn_count
3843 = final_warning_records->type_warnings[i].dyn_count;
3845 if (!dyn_count)
3846 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3847 OPT_Wsuggest_final_types, count,
3848 "Declaring type %qD final "
3849 "would enable devirtualization of %i call",
3850 "Declaring type %qD final "
3851 "would enable devirtualization of %i calls",
3852 type,
3853 count);
3854 else
3855 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3856 OPT_Wsuggest_final_types, count,
3857 "Declaring type %qD final "
3858 "would enable devirtualization of %i call "
3859 "executed %lli times",
3860 "Declaring type %qD final "
3861 "would enable devirtualization of %i calls "
3862 "executed %lli times",
3863 type,
3864 count,
3865 dyn_count);
3869 if (warn_suggest_final_methods)
3871 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
3873 final_warning_records->decl_warnings.traverse
3874 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3875 decl_warnings_vec.qsort (decl_warning_cmp);
3876 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3878 tree decl = decl_warnings_vec[i]->decl;
3879 int count = decl_warnings_vec[i]->count;
3880 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3882 if (!dyn_count)
3883 if (DECL_CXX_DESTRUCTOR_P (decl))
3884 warning_n (DECL_SOURCE_LOCATION (decl),
3885 OPT_Wsuggest_final_methods, count,
3886 "Declaring virtual destructor of %qD final "
3887 "would enable devirtualization of %i call",
3888 "Declaring virtual destructor of %qD final "
3889 "would enable devirtualization of %i calls",
3890 DECL_CONTEXT (decl), count);
3891 else
3892 warning_n (DECL_SOURCE_LOCATION (decl),
3893 OPT_Wsuggest_final_methods, count,
3894 "Declaring method %qD final "
3895 "would enable devirtualization of %i call",
3896 "Declaring method %qD final "
3897 "would enable devirtualization of %i calls",
3898 decl, count);
3899 else if (DECL_CXX_DESTRUCTOR_P (decl))
3900 warning_n (DECL_SOURCE_LOCATION (decl),
3901 OPT_Wsuggest_final_methods, count,
3902 "Declaring virtual destructor of %qD final "
3903 "would enable devirtualization of %i call "
3904 "executed %lli times",
3905 "Declaring virtual destructor of %qD final "
3906 "would enable devirtualization of %i calls "
3907 "executed %lli times",
3908 DECL_CONTEXT (decl), count, dyn_count);
3909 else
3910 warning_n (DECL_SOURCE_LOCATION (decl),
3911 OPT_Wsuggest_final_methods, count,
3912 "Declaring method %qD final "
3913 "would enable devirtualization of %i call "
3914 "executed %lli times",
3915 "Declaring method %qD final "
3916 "would enable devirtualization of %i calls "
3917 "executed %lli times",
3918 decl, count, dyn_count);
3922 delete (final_warning_records);
3923 final_warning_records = 0;
3926 if (dump_file)
3927 fprintf (dump_file,
3928 "%i polymorphic calls, %i devirtualized,"
3929 " %i speculatively devirtualized, %i cold\n"
3930 "%i have multiple targets, %i overwritable,"
3931 " %i already speculated (%i agree, %i disagree),"
3932 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3933 npolymorphic, ndevirtualized, nconverted, ncold,
3934 nmultiple, noverwritable, nspeculated, nok, nwrong,
3935 nexternal, nnotdefined, nartificial, ndropped);
3936 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3939 namespace {
3941 const pass_data pass_data_ipa_devirt =
3943 IPA_PASS, /* type */
3944 "devirt", /* name */
3945 OPTGROUP_NONE, /* optinfo_flags */
3946 TV_IPA_DEVIRT, /* tv_id */
3947 0, /* properties_required */
3948 0, /* properties_provided */
3949 0, /* properties_destroyed */
3950 0, /* todo_flags_start */
3951 ( TODO_dump_symtab ), /* todo_flags_finish */
3954 class pass_ipa_devirt : public ipa_opt_pass_d
3956 public:
3957 pass_ipa_devirt (gcc::context *ctxt)
3958 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3959 NULL, /* generate_summary */
3960 NULL, /* write_summary */
3961 NULL, /* read_summary */
3962 NULL, /* write_optimization_summary */
3963 NULL, /* read_optimization_summary */
3964 NULL, /* stmt_fixup */
3965 0, /* function_transform_todo_flags_start */
3966 NULL, /* function_transform */
3967 NULL) /* variable_transform */
3970 /* opt_pass methods: */
3971 virtual bool gate (function *)
3973 /* In LTO, always run the IPA passes and decide on function basis if the
3974 pass is enabled. */
3975 if (in_lto_p)
3976 return true;
3977 return (flag_devirtualize
3978 && (flag_devirtualize_speculatively
3979 || (warn_suggest_final_methods
3980 || warn_suggest_final_types))
3981 && optimize);
3984 virtual unsigned int execute (function *) { return ipa_devirt (); }
3986 }; // class pass_ipa_devirt
3988 } // anon namespace
3990 ipa_opt_pass_d *
3991 make_pass_ipa_devirt (gcc::context *ctxt)
3993 return new pass_ipa_devirt (ctxt);
3996 #include "gt-ipa-devirt.h"