2015-03-24 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ipa-devirt.c
blobcf8138a1be5f15eac1c0f57e0adb48a8a64f18b0
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 "hash-set.h"
113 #include "machmode.h"
114 #include "hash-map.h"
115 #include "vec.h"
116 #include "double-int.h"
117 #include "input.h"
118 #include "alias.h"
119 #include "symtab.h"
120 #include "wide-int.h"
121 #include "inchash.h"
122 #include "tree.h"
123 #include "fold-const.h"
124 #include "print-tree.h"
125 #include "calls.h"
126 #include "predict.h"
127 #include "basic-block.h"
128 #include "is-a.h"
129 #include "plugin-api.h"
130 #include "hard-reg-set.h"
131 #include "function.h"
132 #include "ipa-ref.h"
133 #include "cgraph.h"
134 #include "hashtab.h"
135 #include "rtl.h"
136 #include "flags.h"
137 #include "statistics.h"
138 #include "real.h"
139 #include "fixed-value.h"
140 #include "insn-config.h"
141 #include "expmed.h"
142 #include "dojump.h"
143 #include "explow.h"
144 #include "emit-rtl.h"
145 #include "varasm.h"
146 #include "stmt.h"
147 #include "expr.h"
148 #include "tree-pass.h"
149 #include "target.h"
150 #include "hash-table.h"
151 #include "tree-pretty-print.h"
152 #include "ipa-utils.h"
153 #include "tree-ssa-alias.h"
154 #include "internal-fn.h"
155 #include "gimple-fold.h"
156 #include "gimple-expr.h"
157 #include "gimple.h"
158 #include "alloc-pool.h"
159 #include "symbol-summary.h"
160 #include "ipa-prop.h"
161 #include "ipa-inline.h"
162 #include "diagnostic.h"
163 #include "tree-dfa.h"
164 #include "demangle.h"
165 #include "dbgcnt.h"
166 #include "gimple-pretty-print.h"
167 #include "stor-layout.h"
168 #include "intl.h"
170 /* Hash based set of pairs of types. */
171 typedef struct
173 tree first;
174 tree second;
175 } type_pair;
177 struct pair_traits : default_hashset_traits
179 static hashval_t
180 hash (type_pair p)
182 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
184 static bool
185 is_empty (type_pair p)
187 return p.first == NULL;
189 static bool
190 is_deleted (type_pair p ATTRIBUTE_UNUSED)
192 return false;
194 static bool
195 equal (const type_pair &a, const type_pair &b)
197 return a.first==b.first && a.second == b.second;
199 static void
200 mark_empty (type_pair &e)
202 e.first = NULL;
206 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
207 hash_set<type_pair,pair_traits> *);
209 static bool odr_violation_reported = false;
212 /* Pointer set of all call targets appearing in the cache. */
213 static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
215 /* The node of type inheritance graph. For each type unique in
216 One Definition Rule (ODR) sense, we produce one node linking all
217 main variants of types equivalent to it, bases and derived types. */
219 struct GTY(()) odr_type_d
221 /* leader type. */
222 tree type;
223 /* All bases; built only for main variants of types. */
224 vec<odr_type> GTY((skip)) bases;
225 /* All derived types with virtual methods seen in unit;
226 built only for main variants of types. */
227 vec<odr_type> GTY((skip)) derived_types;
229 /* All equivalent types, if more than one. */
230 vec<tree, va_gc> *types;
231 /* Set of all equivalent types, if NON-NULL. */
232 hash_set<tree> * GTY((skip)) types_set;
234 /* Unique ID indexing the type in odr_types array. */
235 int id;
236 /* Is it in anonymous namespace? */
237 bool anonymous_namespace;
238 /* Do we know about all derivations of given type? */
239 bool all_derivations_known;
240 /* Did we report ODR violation here? */
241 bool odr_violated;
242 /* Set when virtual table without RTTI previaled table with. */
243 bool rtti_broken;
246 /* Return TRUE if all derived types of T are known and thus
247 we may consider the walk of derived type complete.
249 This is typically true only for final anonymous namespace types and types
250 defined within functions (that may be COMDAT and thus shared across units,
251 but with the same set of derived types). */
253 bool
254 type_all_derivations_known_p (const_tree t)
256 if (TYPE_FINAL_P (t))
257 return true;
258 if (flag_ltrans)
259 return false;
260 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
261 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
262 return true;
263 if (type_in_anonymous_namespace_p (t))
264 return true;
265 return (decl_function_context (TYPE_NAME (t)) != NULL);
268 /* Return TRUE if type's constructors are all visible. */
270 static bool
271 type_all_ctors_visible_p (tree t)
273 return !flag_ltrans
274 && symtab->state >= CONSTRUCTION
275 /* We can not always use type_all_derivations_known_p.
276 For function local types we must assume case where
277 the function is COMDAT and shared in between units.
279 TODO: These cases are quite easy to get, but we need
280 to keep track of C++ privatizing via -Wno-weak
281 as well as the IPA privatizing. */
282 && type_in_anonymous_namespace_p (t);
285 /* Return TRUE if type may have instance. */
287 static bool
288 type_possibly_instantiated_p (tree t)
290 tree vtable;
291 varpool_node *vnode;
293 /* TODO: Add abstract types here. */
294 if (!type_all_ctors_visible_p (t))
295 return true;
297 vtable = BINFO_VTABLE (TYPE_BINFO (t));
298 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
299 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
300 vnode = varpool_node::get (vtable);
301 return vnode && vnode->definition;
304 /* Hash used to unify ODR types based on their mangled name and for anonymous
305 namespace types. */
307 struct odr_name_hasher
309 typedef odr_type_d value_type;
310 typedef union tree_node compare_type;
311 static inline hashval_t hash (const value_type *);
312 static inline bool equal (const value_type *, const compare_type *);
313 static inline void remove (value_type *);
316 /* Has used to unify ODR types based on their associated virtual table.
317 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
318 only polymorphic types. Types with mangled names are inserted to both. */
320 struct odr_vtable_hasher:odr_name_hasher
322 static inline hashval_t hash (const value_type *);
323 static inline bool equal (const value_type *, const compare_type *);
326 /* Return type that was declared with T's name so that T is an
327 qualified variant of it. */
329 static inline tree
330 main_odr_variant (const_tree t)
332 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
333 return TREE_TYPE (TYPE_NAME (t));
334 /* Unnamed types and non-C++ produced types can be compared by variants. */
335 else
336 return TYPE_MAIN_VARIANT (t);
339 static bool
340 can_be_name_hashed_p (tree t)
342 return (!in_lto_p || type_in_anonymous_namespace_p (t)
343 || (TYPE_NAME (t) && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t))));
346 /* Hash type by its ODR name. */
348 static hashval_t
349 hash_odr_name (const_tree t)
351 gcc_checking_assert (main_odr_variant (t) == t);
353 /* If not in LTO, all main variants are unique, so we can do
354 pointer hash. */
355 if (!in_lto_p)
356 return htab_hash_pointer (t);
358 /* Anonymous types are unique. */
359 if (type_in_anonymous_namespace_p (t))
360 return htab_hash_pointer (t);
362 gcc_checking_assert (TYPE_NAME (t)
363 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
364 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
367 /* Return the computed hashcode for ODR_TYPE. */
369 inline hashval_t
370 odr_name_hasher::hash (const value_type *odr_type)
372 return hash_odr_name (odr_type->type);
375 static bool
376 can_be_vtable_hashed_p (tree t)
378 /* vtable hashing can distinguish only main variants. */
379 if (TYPE_MAIN_VARIANT (t) != t)
380 return false;
381 /* Anonymous namespace types are always handled by name hash. */
382 if (type_in_anonymous_namespace_p (t))
383 return false;
384 return (TREE_CODE (t) == RECORD_TYPE
385 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
388 /* Hash type by assembler name of its vtable. */
390 static hashval_t
391 hash_odr_vtable (const_tree t)
393 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
394 inchash::hash hstate;
396 gcc_checking_assert (in_lto_p);
397 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
398 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
399 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
400 gcc_checking_assert (main_odr_variant (t) == t);
402 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
404 add_expr (TREE_OPERAND (v, 1), hstate);
405 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
408 hstate.add_wide_int (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
409 return hstate.end ();
412 /* Return the computed hashcode for ODR_TYPE. */
414 inline hashval_t
415 odr_vtable_hasher::hash (const value_type *odr_type)
417 return hash_odr_vtable (odr_type->type);
420 /* For languages with One Definition Rule, work out if
421 types are the same based on their name.
423 This is non-trivial for LTO where minor differences in
424 the type representation may have prevented type merging
425 to merge two copies of otherwise equivalent type.
427 Until we start streaming mangled type names, this function works
428 only for polymorphic types.
430 When STRICT is true, we compare types by their names for purposes of
431 ODR violation warnings. When strict is false, we consider variants
432 equivalent, becuase it is all that matters for devirtualization machinery.
435 bool
436 types_same_for_odr (const_tree type1, const_tree type2, bool strict)
438 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
440 type1 = main_odr_variant (type1);
441 type2 = main_odr_variant (type2);
442 if (!strict)
444 type1 = TYPE_MAIN_VARIANT (type1);
445 type2 = TYPE_MAIN_VARIANT (type2);
448 if (type1 == type2)
449 return true;
451 if (!in_lto_p)
452 return false;
454 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
455 on the corresponding TYPE_STUB_DECL. */
456 if (type_in_anonymous_namespace_p (type1)
457 || type_in_anonymous_namespace_p (type2))
458 return false;
461 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME.
463 Ideally we should never need types without ODR names here. It can however
464 happen in two cases:
466 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c
467 Here testing for equivalence is safe, since their MAIN_VARIANTs are
468 unique.
469 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
470 establish precise ODR equivalency, but for correctness we care only
471 about equivalency on complete polymorphic types. For these we can
472 compare assembler names of their virtual tables. */
473 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
474 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
476 /* See if types are obviously different (i.e. different codes
477 or polymorphic wrt non-polymorphic). This is not strictly correct
478 for ODR violating programs, but we can't do better without streaming
479 ODR names. */
480 if (TREE_CODE (type1) != TREE_CODE (type2))
481 return false;
482 if (TREE_CODE (type1) == RECORD_TYPE
483 && (TYPE_BINFO (type1) == NULL_TREE)
484 != (TYPE_BINFO (type1) == NULL_TREE))
485 return false;
486 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
487 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
488 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
489 return false;
491 /* At the moment we have no way to establish ODR equivalence at LTO
492 other than comparing virtual table pointers of polymorphic types.
493 Eventually we should start saving mangled names in TYPE_NAME.
494 Then this condition will become non-trivial. */
496 if (TREE_CODE (type1) == RECORD_TYPE
497 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
498 && BINFO_VTABLE (TYPE_BINFO (type1))
499 && BINFO_VTABLE (TYPE_BINFO (type2)))
501 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
502 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
503 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
504 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
505 return (operand_equal_p (TREE_OPERAND (v1, 1),
506 TREE_OPERAND (v2, 1), 0)
507 && DECL_ASSEMBLER_NAME
508 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
509 == DECL_ASSEMBLER_NAME
510 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
512 gcc_unreachable ();
514 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
515 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
518 /* Return true if we can decide on ODR equivalency.
520 In non-LTO it is always decide, in LTO however it depends in the type has
521 ODR info attached.
523 When STRICT is false, compare main variants. */
525 bool
526 types_odr_comparable (tree t1, tree t2, bool strict)
528 return (!in_lto_p
529 || (strict ? main_odr_variant (t1) == main_odr_variant (t2)
530 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
531 || (odr_type_p (t1) && odr_type_p (t2))
532 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
533 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
534 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
535 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
538 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
539 known, be conservative and return false. */
541 bool
542 types_must_be_same_for_odr (tree t1, tree t2)
544 if (types_odr_comparable (t1, t2))
545 return types_same_for_odr (t1, t2);
546 else
547 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
550 /* Compare types T1 and T2 and return true if they are
551 equivalent. */
553 inline bool
554 odr_name_hasher::equal (const value_type *o1, const compare_type *t2)
556 tree t1 = o1->type;
558 gcc_checking_assert (main_odr_variant (t2) == t2);
559 gcc_checking_assert (main_odr_variant (t1) == t1);
560 if (t1 == t2)
561 return true;
562 if (!in_lto_p)
563 return false;
564 /* Check for anonymous namespaces. Those have !TREE_PUBLIC
565 on the corresponding TYPE_STUB_DECL. */
566 if (type_in_anonymous_namespace_p (t1)
567 || type_in_anonymous_namespace_p (t2))
568 return false;
569 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
570 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
571 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
572 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
575 /* Compare types T1 and T2 and return true if they are
576 equivalent. */
578 inline bool
579 odr_vtable_hasher::equal (const value_type *o1, const compare_type *t2)
581 tree t1 = o1->type;
583 gcc_checking_assert (main_odr_variant (t2) == t2);
584 gcc_checking_assert (main_odr_variant (t1) == t1);
585 gcc_checking_assert (in_lto_p);
586 t1 = TYPE_MAIN_VARIANT (t1);
587 t2 = TYPE_MAIN_VARIANT (t2);
588 if (t1 == t2)
589 return true;
590 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
591 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
592 return (operand_equal_p (TREE_OPERAND (v1, 1),
593 TREE_OPERAND (v2, 1), 0)
594 && DECL_ASSEMBLER_NAME
595 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
596 == DECL_ASSEMBLER_NAME
597 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
600 /* Free ODR type V. */
602 inline void
603 odr_name_hasher::remove (value_type *v)
605 v->bases.release ();
606 v->derived_types.release ();
607 if (v->types_set)
608 delete v->types_set;
609 ggc_free (v);
612 /* ODR type hash used to look up ODR type based on tree type node. */
614 typedef hash_table<odr_name_hasher> odr_hash_type;
615 static odr_hash_type *odr_hash;
616 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
617 static odr_vtable_hash_type *odr_vtable_hash;
619 /* ODR types are also stored into ODR_TYPE vector to allow consistent
620 walking. Bases appear before derived types. Vector is garbage collected
621 so we won't end up visiting empty types. */
623 static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
624 #define odr_types (*odr_types_ptr)
626 /* Set TYPE_BINFO of TYPE and its variants to BINFO. */
627 void
628 set_type_binfo (tree type, tree binfo)
630 for (; type; type = TYPE_NEXT_VARIANT (type))
631 if (COMPLETE_TYPE_P (type))
632 TYPE_BINFO (type) = binfo;
633 else
634 gcc_assert (!TYPE_BINFO (type));
637 /* Compare T2 and T2 based on name or structure. */
639 static bool
640 odr_subtypes_equivalent_p (tree t1, tree t2,
641 hash_set<type_pair,pair_traits> *visited)
643 bool an1, an2;
645 /* This can happen in incomplete types that should be handled earlier. */
646 gcc_assert (t1 && t2);
648 t1 = main_odr_variant (t1);
649 t2 = main_odr_variant (t2);
650 if (t1 == t2)
651 return true;
653 /* Anonymous namespace types must match exactly. */
654 an1 = type_in_anonymous_namespace_p (t1);
655 an2 = type_in_anonymous_namespace_p (t2);
656 if (an1 != an2 || an1)
657 return false;
659 /* For ODR types be sure to compare their names.
660 To support -wno-odr-type-merging we allow one type to be non-ODR
661 and other ODR even though it is a violation. */
662 if (types_odr_comparable (t1, t2, true))
664 if (!types_same_for_odr (t1, t2, true))
665 return false;
666 /* Limit recursion: If subtypes are ODR types and we know
667 that they are same, be happy. */
668 if (!get_odr_type (t1, true)->odr_violated)
669 return true;
672 /* Component types, builtins and possibly violating ODR types
673 have to be compared structurally. */
674 if (TREE_CODE (t1) != TREE_CODE (t2))
675 return false;
676 if ((TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
677 return false;
679 type_pair pair={t1,t2};
680 if (TYPE_UID (t1) > TYPE_UID (t2))
682 pair.first = t2;
683 pair.second = t1;
685 if (visited->add (pair))
686 return true;
687 return odr_types_equivalent_p (t1, t2, false, NULL, visited);
690 /* Compare two virtual tables, PREVAILING and VTABLE and output ODR
691 violation warnings. */
693 void
694 compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
696 int n1, n2;
698 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
700 odr_violation_reported = true;
701 if (DECL_VIRTUAL_P (prevailing->decl))
703 varpool_node *tmp = prevailing;
704 prevailing = vtable;
705 vtable = tmp;
707 if (warning_at (DECL_SOURCE_LOCATION
708 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
709 OPT_Wodr,
710 "virtual table of type %qD violates one definition rule",
711 DECL_CONTEXT (vtable->decl)))
712 inform (DECL_SOURCE_LOCATION (prevailing->decl),
713 "variable of same assembler name as the virtual table is "
714 "defined in another translation unit");
715 return;
717 if (!prevailing->definition || !vtable->definition)
718 return;
720 /* If we do not stream ODR type info, do not bother to do useful compare. */
721 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
722 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
723 return;
725 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), true);
727 if (class_type->odr_violated)
728 return;
730 for (n1 = 0, n2 = 0; true; n1++, n2++)
732 struct ipa_ref *ref1, *ref2;
733 bool end1, end2;
735 end1 = !prevailing->iterate_reference (n1, ref1);
736 end2 = !vtable->iterate_reference (n2, ref2);
738 /* !DECL_VIRTUAL_P means RTTI entry;
739 We warn when RTTI is lost because non-RTTI previals; we silently
740 accept the other case. */
741 while (!end2
742 && (end1
743 || (DECL_ASSEMBLER_NAME (ref1->referred->decl)
744 != DECL_ASSEMBLER_NAME (ref2->referred->decl)
745 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
746 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
748 if (!class_type->rtti_broken
749 && warning_at (DECL_SOURCE_LOCATION
750 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
751 OPT_Wodr,
752 "virtual table of type %qD contains RTTI "
753 "information",
754 DECL_CONTEXT (vtable->decl)))
756 inform (DECL_SOURCE_LOCATION
757 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
758 "but is prevailed by one without from other translation "
759 "unit");
760 inform (DECL_SOURCE_LOCATION
761 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
762 "RTTI will not work on this type");
763 class_type->rtti_broken = true;
765 n2++;
766 end2 = !vtable->iterate_reference (n2, ref2);
768 while (!end1
769 && (end2
770 || (DECL_ASSEMBLER_NAME (ref2->referred->decl)
771 != DECL_ASSEMBLER_NAME (ref1->referred->decl)
772 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
773 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
775 n1++;
776 end1 = !prevailing->iterate_reference (n1, ref1);
779 /* Finished? */
780 if (end1 && end2)
782 /* Extra paranoia; compare the sizes. We do not have information
783 about virtual inheritance offsets, so just be sure that these
784 match.
785 Do this as very last check so the not very informative error
786 is not output too often. */
787 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
789 class_type->odr_violated = true;
790 if (warning_at (DECL_SOURCE_LOCATION
791 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
792 OPT_Wodr,
793 "virtual table of type %qD violates "
794 "one definition rule ",
795 DECL_CONTEXT (vtable->decl)))
797 inform (DECL_SOURCE_LOCATION
798 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
799 "the conflicting type defined in another translation "
800 "unit has virtual table of different size");
803 return;
806 if (!end1 && !end2)
808 if (DECL_ASSEMBLER_NAME (ref1->referred->decl)
809 == DECL_ASSEMBLER_NAME (ref2->referred->decl))
810 continue;
812 class_type->odr_violated = true;
814 /* If the loops above stopped on non-virtual pointer, we have
815 mismatch in RTTI information mangling. */
816 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
817 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
819 if (warning_at (DECL_SOURCE_LOCATION
820 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
821 OPT_Wodr,
822 "virtual table of type %qD violates "
823 "one definition rule ",
824 DECL_CONTEXT (vtable->decl)))
826 inform (DECL_SOURCE_LOCATION
827 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
828 "the conflicting type defined in another translation "
829 "unit with different RTTI information");
831 return;
833 /* At this point both REF1 and REF2 points either to virtual table
834 or virtual method. If one points to virtual table and other to
835 method we can complain the same way as if one table was shorter
836 than other pointing out the extra method. */
837 if (TREE_CODE (ref1->referred->decl)
838 != TREE_CODE (ref2->referred->decl))
840 if (TREE_CODE (ref1->referred->decl) == VAR_DECL)
841 end1 = true;
842 else if (TREE_CODE (ref2->referred->decl) == VAR_DECL)
843 end2 = true;
847 class_type->odr_violated = true;
849 /* Complain about size mismatch. Either we have too many virutal
850 functions or too many virtual table pointers. */
851 if (end1 || end2)
853 if (end1)
855 varpool_node *tmp = prevailing;
856 prevailing = vtable;
857 vtable = tmp;
858 ref1 = ref2;
860 if (warning_at (DECL_SOURCE_LOCATION
861 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
862 OPT_Wodr,
863 "virtual table of type %qD violates "
864 "one definition rule",
865 DECL_CONTEXT (vtable->decl)))
867 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
869 inform (DECL_SOURCE_LOCATION
870 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
871 "the conflicting type defined in another translation "
872 "unit");
873 inform (DECL_SOURCE_LOCATION
874 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
875 "contains additional virtual method %qD",
876 ref1->referred->decl);
878 else
880 inform (DECL_SOURCE_LOCATION
881 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
882 "the conflicting type defined in another translation "
883 "unit has virtual table table with more entries");
886 return;
889 /* And in the last case we have either mistmatch in between two virtual
890 methods or two virtual table pointers. */
891 if (warning_at (DECL_SOURCE_LOCATION
892 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
893 "virtual table of type %qD violates "
894 "one definition rule ",
895 DECL_CONTEXT (vtable->decl)))
897 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
899 inform (DECL_SOURCE_LOCATION
900 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
901 "the conflicting type defined in another translation "
902 "unit");
903 gcc_assert (TREE_CODE (ref2->referred->decl)
904 == FUNCTION_DECL);
905 inform (DECL_SOURCE_LOCATION (ref1->referred->decl),
906 "virtual method %qD", ref1->referred->decl);
907 inform (DECL_SOURCE_LOCATION (ref2->referred->decl),
908 "ought to match virtual method %qD but does not",
909 ref2->referred->decl);
911 else
912 inform (DECL_SOURCE_LOCATION
913 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
914 "the conflicting type defined in another translation "
915 "unit has virtual table table with different contents");
916 return;
921 /* Output ODR violation warning about T1 and T2 with REASON.
922 Display location of ST1 and ST2 if REASON speaks about field or
923 method of the type.
924 If WARN is false, do nothing. Set WARNED if warning was indeed
925 output. */
927 void
928 warn_odr (tree t1, tree t2, tree st1, tree st2,
929 bool warn, bool *warned, const char *reason)
931 tree decl2 = TYPE_NAME (t2);
932 if (warned)
933 *warned = false;
935 if (!warn || !TYPE_NAME(t1))
936 return;
938 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
939 "type %qT violates one definition rule",
940 t1))
941 return;
942 if (!st1 && !st2)
944 /* For FIELD_DECL support also case where one of fields is
945 NULL - this is used when the structures have mismatching number of
946 elements. */
947 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
949 inform (DECL_SOURCE_LOCATION (decl2),
950 "a different type is defined in another translation unit");
951 if (!st1)
953 st1 = st2;
954 st2 = NULL;
956 inform (DECL_SOURCE_LOCATION (st1),
957 "the first difference of corresponding definitions is field %qD",
958 st1);
959 if (st2)
960 decl2 = st2;
962 else if (TREE_CODE (st1) == FUNCTION_DECL)
964 inform (DECL_SOURCE_LOCATION (decl2),
965 "a different type is defined in another translation unit");
966 inform (DECL_SOURCE_LOCATION (st1),
967 "the first difference of corresponding definitions is method %qD",
968 st1);
969 decl2 = st2;
971 else
972 return;
973 inform (DECL_SOURCE_LOCATION (decl2), reason);
975 if (warned)
976 *warned = true;
979 /* We already warned about ODR mismatch. T1 and T2 ought to be equivalent
980 because they are used on same place in ODR matching types.
981 They are not; inform the user. */
983 void
984 warn_types_mismatch (tree t1, tree t2)
986 /* If types have names and they are different, it is most informative to
987 output those. */
988 if (TYPE_NAME (t1) && TYPE_NAME (t2)
989 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1))
990 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2))
991 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
992 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2)))
994 char *name1 = xstrdup (cplus_demangle
995 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
996 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
997 char *name2 = cplus_demangle
998 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
999 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1000 if (name1 && name2 && strcmp (name1, name2))
1002 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1003 "type name %<%s%> should match type name %<%s%>",
1004 name1, name2);
1005 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1006 "the incompatible type is defined here");
1007 free (name1);
1008 return;
1010 free (name1);
1012 /* It is a quite common bug to reference anonymous namespace type in
1013 non-anonymous namespace class. */
1014 if (type_in_anonymous_namespace_p (t1)
1015 || type_in_anonymous_namespace_p (t2))
1017 if (!type_in_anonymous_namespace_p (t1))
1019 tree tmp = t1;;
1020 t1 = t2;
1021 t2 = tmp;
1023 if (TYPE_NAME (t1) && TYPE_NAME (t2))
1025 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1026 "type %qT defined in anonymous namespace can not match "
1027 "type %qT",
1028 t1, t2);
1029 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1030 "the incompatible type defined in anonymous namespace in "
1031 "another translation unit");
1033 else
1034 inform (UNKNOWN_LOCATION,
1035 "types in anonymous namespace does not match across "
1036 "translation unit boundary");
1037 return;
1039 /* A tricky case are component types. Often they appear the same in source
1040 code and the mismatch is dragged in by type they are build from.
1041 Look for those differences in subtypes and try to be informative. In other
1042 cases just output nothing because the source code is probably different
1043 and in this case we already output a all necessary info. */
1044 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1046 if (TREE_CODE (t1) == TREE_CODE (t2))
1048 hash_set<type_pair,pair_traits> visited;
1049 if (TREE_CODE (t1) == ARRAY_TYPE
1050 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1052 tree i1 = TYPE_DOMAIN (t1);
1053 tree i2 = TYPE_DOMAIN (t2);
1055 if (i1 && i2
1056 && TYPE_MAX_VALUE (i1)
1057 && TYPE_MAX_VALUE (i2)
1058 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1059 TYPE_MAX_VALUE (i2), 0))
1061 inform (UNKNOWN_LOCATION,
1062 "array types have different bounds");
1063 return;
1066 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1067 && !odr_subtypes_equivalent_p (TREE_TYPE (t1),
1068 TREE_TYPE (t2),
1069 &visited))
1070 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1071 else if (TREE_CODE (t1) == METHOD_TYPE
1072 || TREE_CODE (t1) == FUNCTION_TYPE)
1074 tree parms1, parms2;
1075 int count = 1;
1077 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1078 &visited))
1080 inform (UNKNOWN_LOCATION, "return value type mismatch");
1081 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1082 return;
1084 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1085 parms1 && parms2;
1086 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1087 count++)
1089 if (!odr_subtypes_equivalent_p
1090 (TREE_VALUE (parms1), TREE_VALUE (parms2), &visited))
1092 inform (UNKNOWN_LOCATION,
1093 "type mismatch in parameter %i", count);
1094 warn_types_mismatch (TREE_VALUE (parms1),
1095 TREE_VALUE (parms2));
1096 return;
1099 if (parms1 || parms2)
1101 inform (UNKNOWN_LOCATION,
1102 "types have different parameter counts");
1103 return;
1107 return;
1109 /* This should not happen but if it does, the warning would not be helpful.
1110 TODO: turn it into assert next stage1. */
1111 if (TYPE_NAME (t1) == TYPE_NAME (t2))
1112 return;
1113 /* In Firefox it is a common bug to have same types but in
1114 different namespaces. Be a bit more informative on
1115 this. */
1116 if (TYPE_CONTEXT (t1) && TYPE_CONTEXT (t2)
1117 && (((TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL)
1118 != (TREE_CODE (TYPE_CONTEXT (t2)) == NAMESPACE_DECL))
1119 || (TREE_CODE (TYPE_CONTEXT (t1)) == NAMESPACE_DECL
1120 && (DECL_NAME (TYPE_CONTEXT (t1)) !=
1121 DECL_NAME (TYPE_CONTEXT (t2))))))
1122 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1123 "type %qT should match type %qT but is defined "
1124 "in different namespace ",
1125 t1, t2);
1126 else if (types_odr_comparable (t1, t2, true)
1127 && types_same_for_odr (t1, t2, true))
1128 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1129 "type %qT should match type %qT that itself violate "
1130 "one definition rule",
1131 t1, t2);
1132 else
1133 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t1)),
1134 "type %qT should match type %qT",
1135 t1, t2);
1136 if (DECL_SOURCE_LOCATION (TYPE_NAME (t2)) > BUILTINS_LOCATION)
1137 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t2)),
1138 "the incompatible type is defined here");
1141 /* Compare T1 and T2, report ODR violations if WARN is true and set
1142 WARNED to true if anything is reported. Return true if types match.
1143 If true is returned, the types are also compatible in the sense of
1144 gimple_canonical_types_compatible_p. */
1146 static bool
1147 odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1148 hash_set<type_pair,pair_traits> *visited)
1150 /* Check first for the obvious case of pointer identity. */
1151 if (t1 == t2)
1152 return true;
1153 gcc_assert (!type_in_anonymous_namespace_p (t1));
1154 gcc_assert (!type_in_anonymous_namespace_p (t2));
1156 /* Can't be the same type if the types don't have the same code. */
1157 if (TREE_CODE (t1) != TREE_CODE (t2))
1159 warn_odr (t1, t2, NULL, NULL, warn, warned,
1160 G_("a different type is defined in another translation unit"));
1161 return false;
1164 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1166 warn_odr (t1, t2, NULL, NULL, warn, warned,
1167 G_("a type with different qualifiers is defined in another "
1168 "translation unit"));
1169 return false;
1172 if (comp_type_attributes (t1, t2) != 1)
1174 warn_odr (t1, t2, NULL, NULL, warn, warned,
1175 G_("a type with attributes "
1176 "is defined in another translation unit"));
1177 return false;
1180 if (TREE_CODE (t1) == ENUMERAL_TYPE
1181 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1183 tree v1, v2;
1184 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
1185 v1 && v2 ; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
1187 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
1189 warn_odr (t1, t2, NULL, NULL, warn, warned,
1190 G_("an enum with different value name"
1191 " is defined in another translation unit"));
1192 return false;
1194 if (TREE_VALUE (v1) != TREE_VALUE (v2)
1195 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1196 DECL_INITIAL (TREE_VALUE (v2)), 0))
1198 warn_odr (t1, t2, NULL, NULL, warn, warned,
1199 G_("an enum with different values is defined"
1200 " in another translation unit"));
1201 return false;
1204 if (v1 || v2)
1206 warn_odr (t1, t2, NULL, NULL, warn, warned,
1207 G_("an enum with mismatching number of values "
1208 "is defined in another translation unit"));
1209 return false;
1213 /* Non-aggregate types can be handled cheaply. */
1214 if (INTEGRAL_TYPE_P (t1)
1215 || SCALAR_FLOAT_TYPE_P (t1)
1216 || FIXED_POINT_TYPE_P (t1)
1217 || TREE_CODE (t1) == VECTOR_TYPE
1218 || TREE_CODE (t1) == COMPLEX_TYPE
1219 || TREE_CODE (t1) == OFFSET_TYPE
1220 || POINTER_TYPE_P (t1))
1222 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1224 warn_odr (t1, t2, NULL, NULL, warn, warned,
1225 G_("a type with different precision is defined "
1226 "in another translation unit"));
1227 return false;
1229 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1231 warn_odr (t1, t2, NULL, NULL, warn, warned,
1232 G_("a type with different signedness is defined "
1233 "in another translation unit"));
1234 return false;
1237 if (TREE_CODE (t1) == INTEGER_TYPE
1238 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1240 /* char WRT uint_8? */
1241 warn_odr (t1, t2, NULL, NULL, warn, warned,
1242 G_("a different type is defined in another "
1243 "translation unit"));
1244 return false;
1247 /* For canonical type comparisons we do not want to build SCCs
1248 so we cannot compare pointed-to types. But we can, for now,
1249 require the same pointed-to type kind and match what
1250 useless_type_conversion_p would do. */
1251 if (POINTER_TYPE_P (t1))
1253 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1254 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1256 warn_odr (t1, t2, NULL, NULL, warn, warned,
1257 G_("it is defined as a pointer in different address "
1258 "space in another translation unit"));
1259 return false;
1262 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1264 warn_odr (t1, t2, NULL, NULL, warn, warned,
1265 G_("it is defined as a pointer to different type "
1266 "in another translation unit"));
1267 if (warn && warned)
1268 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1269 return false;
1273 if ((TREE_CODE (t1) == VECTOR_TYPE || TREE_CODE (t1) == COMPLEX_TYPE)
1274 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1276 /* Probably specific enough. */
1277 warn_odr (t1, t2, NULL, NULL, warn, warned,
1278 G_("a different type is defined "
1279 "in another translation unit"));
1280 if (warn && warned)
1281 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1282 return false;
1285 /* Do type-specific comparisons. */
1286 else switch (TREE_CODE (t1))
1288 case ARRAY_TYPE:
1290 /* Array types are the same if the element types are the same and
1291 the number of elements are the same. */
1292 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1294 warn_odr (t1, t2, NULL, NULL, warn, warned,
1295 G_("a different type is defined in another "
1296 "translation unit"));
1297 if (warn && warned)
1298 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1300 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1301 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1302 == TYPE_NONALIASED_COMPONENT (t2));
1304 tree i1 = TYPE_DOMAIN (t1);
1305 tree i2 = TYPE_DOMAIN (t2);
1307 /* For an incomplete external array, the type domain can be
1308 NULL_TREE. Check this condition also. */
1309 if (i1 == NULL_TREE || i2 == NULL_TREE)
1310 return true;
1312 tree min1 = TYPE_MIN_VALUE (i1);
1313 tree min2 = TYPE_MIN_VALUE (i2);
1314 tree max1 = TYPE_MAX_VALUE (i1);
1315 tree max2 = TYPE_MAX_VALUE (i2);
1317 /* In C++, minimums should be always 0. */
1318 gcc_assert (min1 == min2);
1319 if (!operand_equal_p (max1, max2, 0))
1321 warn_odr (t1, t2, NULL, NULL, warn, warned,
1322 G_("an array of different size is defined "
1323 "in another translation unit"));
1324 return false;
1327 break;
1329 case METHOD_TYPE:
1330 case FUNCTION_TYPE:
1331 /* Function types are the same if the return type and arguments types
1332 are the same. */
1333 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2), visited))
1335 warn_odr (t1, t2, NULL, NULL, warn, warned,
1336 G_("has different return value "
1337 "in another translation unit"));
1338 if (warn && warned)
1339 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2));
1340 return false;
1343 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
1344 return true;
1345 else
1347 tree parms1, parms2;
1349 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1350 parms1 && parms2;
1351 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1353 if (!odr_subtypes_equivalent_p
1354 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited))
1356 warn_odr (t1, t2, NULL, NULL, warn, warned,
1357 G_("has different parameters in another "
1358 "translation unit"));
1359 if (warn && warned)
1360 warn_types_mismatch (TREE_VALUE (parms1),
1361 TREE_VALUE (parms2));
1362 return false;
1366 if (parms1 || parms2)
1368 warn_odr (t1, t2, NULL, NULL, warn, warned,
1369 G_("has different parameters "
1370 "in another translation unit"));
1371 return false;
1374 return true;
1377 case RECORD_TYPE:
1378 case UNION_TYPE:
1379 case QUAL_UNION_TYPE:
1381 tree f1, f2;
1383 /* For aggregate types, all the fields must be the same. */
1384 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1386 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1387 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1388 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1390 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1391 warn_odr (t1, t2, NULL, NULL, warn, warned,
1392 G_("a type defined in another translation unit "
1393 "is not polymorphic"));
1394 else
1395 warn_odr (t1, t2, NULL, NULL, warn, warned,
1396 G_("a type defined in another translation unit "
1397 "is polymorphic"));
1398 return false;
1400 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1401 f1 || f2;
1402 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1404 /* Skip non-fields. */
1405 while (f1 && TREE_CODE (f1) != FIELD_DECL)
1406 f1 = TREE_CHAIN (f1);
1407 while (f2 && TREE_CODE (f2) != FIELD_DECL)
1408 f2 = TREE_CHAIN (f2);
1409 if (!f1 || !f2)
1410 break;
1411 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1413 warn_odr (t1, t2, NULL, NULL, warn, warned,
1414 G_("a type with different virtual table pointers"
1415 " is defined in another translation unit"));
1416 return false;
1418 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1420 warn_odr (t1, t2, NULL, NULL, warn, warned,
1421 G_("a type with different bases is defined "
1422 "in another translation unit"));
1423 return false;
1425 if (DECL_NAME (f1) != DECL_NAME (f2)
1426 && !DECL_ARTIFICIAL (f1))
1428 warn_odr (t1, t2, f1, f2, warn, warned,
1429 G_("a field with different name is defined "
1430 "in another translation unit"));
1431 return false;
1433 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1434 TREE_TYPE (f2), visited))
1436 /* Do not warn about artificial fields and just go into
1437 generic field mismatch warning. */
1438 if (DECL_ARTIFICIAL (f1))
1439 break;
1441 warn_odr (t1, t2, f1, f2, warn, warned,
1442 G_("a field of same name but different type "
1443 "is defined in another translation unit"));
1444 if (warn && warned)
1445 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2));
1446 return false;
1448 if (!gimple_compare_field_offset (f1, f2))
1450 /* Do not warn about artificial fields and just go into
1451 generic field mismatch warning. */
1452 if (DECL_ARTIFICIAL (f1))
1453 break;
1454 warn_odr (t1, t2, f1, f2, warn, warned,
1455 G_("fields has different layout "
1456 "in another translation unit"));
1457 return false;
1459 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1460 == DECL_NONADDRESSABLE_P (f2));
1463 /* If one aggregate has more fields than the other, they
1464 are not the same. */
1465 if (f1 || f2)
1467 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1468 warn_odr (t1, t2, NULL, NULL, warn, warned,
1469 G_("a type with different virtual table pointers"
1470 " is defined in another translation unit"));
1471 else if ((f1 && DECL_ARTIFICIAL (f1))
1472 || (f2 && DECL_ARTIFICIAL (f2)))
1473 warn_odr (t1, t2, NULL, NULL, warn, warned,
1474 G_("a type with different bases is defined "
1475 "in another translation unit"));
1476 else
1477 warn_odr (t1, t2, f1, f2, warn, warned,
1478 G_("a type with different number of fields "
1479 "is defined in another translation unit"));
1481 return false;
1483 if ((TYPE_MAIN_VARIANT (t1) == t1 || TYPE_MAIN_VARIANT (t2) == t2)
1484 && (TYPE_METHODS (TYPE_MAIN_VARIANT (t1))
1485 != TYPE_METHODS (TYPE_MAIN_VARIANT (t2))))
1487 for (f1 = TYPE_METHODS (TYPE_MAIN_VARIANT (t1)),
1488 f2 = TYPE_METHODS (TYPE_MAIN_VARIANT (t2));
1489 f1 && f2 ; f1 = DECL_CHAIN (f1), f2 = DECL_CHAIN (f2))
1491 if (DECL_ASSEMBLER_NAME (f1) != DECL_ASSEMBLER_NAME (f2))
1493 warn_odr (t1, t2, f1, f2, warn, warned,
1494 G_("a different method of same type "
1495 "is defined in another translation unit"));
1496 return false;
1498 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1500 warn_odr (t1, t2, f1, f2, warn, warned,
1501 G_("s definition that differs by virtual "
1502 "keyword in another translation unit"));
1503 return false;
1505 if (DECL_VINDEX (f1) != DECL_VINDEX (f2))
1507 warn_odr (t1, t2, f1, f2, warn, warned,
1508 G_("virtual table layout differs in another "
1509 "translation unit"));
1510 return false;
1512 if (odr_subtypes_equivalent_p (TREE_TYPE (f1), TREE_TYPE (f2), visited))
1514 warn_odr (t1, t2, f1, f2, warn, warned,
1515 G_("method with incompatible type is defined "
1516 "in another translation unit"));
1517 return false;
1520 if (f1 || f2)
1522 warn_odr (t1, t2, NULL, NULL, warn, warned,
1523 G_("a type with different number of methods "
1524 "is defined in another translation unit"));
1525 return false;
1529 break;
1531 case VOID_TYPE:
1532 break;
1534 default:
1535 debug_tree (t1);
1536 gcc_unreachable ();
1539 /* Those are better to come last as they are utterly uninformative. */
1540 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1541 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), 0))
1543 warn_odr (t1, t2, NULL, NULL, warn, warned,
1544 G_("a type with different size "
1545 "is defined in another translation unit"));
1546 return false;
1548 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
1549 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
1551 warn_odr (t1, t2, NULL, NULL, warn, warned,
1552 G_("a type with different alignment "
1553 "is defined in another translation unit"));
1554 return false;
1556 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1557 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1558 TYPE_SIZE_UNIT (t2), 0));
1559 return true;
1562 /* TYPE is equivalent to VAL by ODR, but its tree representation differs
1563 from VAL->type. This may happen in LTO where tree merging did not merge
1564 all variants of the same type or due to ODR violation.
1566 Analyze and report ODR violations and add type to duplicate list.
1567 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1568 this is first time we see definition of a class return true so the
1569 base types are analyzed. */
1571 static bool
1572 add_type_duplicate (odr_type val, tree type)
1574 bool build_bases = false;
1575 bool prevail = false;
1576 bool odr_must_violate = false;
1578 if (!val->types_set)
1579 val->types_set = new hash_set<tree>;
1581 /* Chose polymorphic type as leader (this happens only in case of ODR
1582 violations. */
1583 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1584 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1585 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1586 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1588 prevail = true;
1589 build_bases = true;
1591 /* Always prefer complete type to be the leader. */
1592 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1594 prevail = true;
1595 build_bases = TYPE_BINFO (type);
1597 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1599 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1600 && TREE_CODE (type) == ENUMERAL_TYPE
1601 && !TYPE_VALUES (val->type) && TYPE_VALUES (type))
1602 prevail = true;
1603 else if (TREE_CODE (val->type) == RECORD_TYPE
1604 && TREE_CODE (type) == RECORD_TYPE
1605 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1607 gcc_assert (!val->bases.length ());
1608 build_bases = true;
1609 prevail = true;
1612 if (prevail)
1614 tree tmp = type;
1616 type = val->type;
1617 val->type = tmp;
1620 val->types_set->add (type);
1622 /* If we now have a mangled name, be sure to record it to val->type
1623 so ODR hash can work. */
1625 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type))
1626 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type),
1627 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1629 bool merge = true;
1630 bool base_mismatch = false;
1631 unsigned int i;
1632 bool warned = false;
1633 hash_set<type_pair,pair_traits> visited;
1635 gcc_assert (in_lto_p);
1636 vec_safe_push (val->types, type);
1638 /* If both are class types, compare the bases. */
1639 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1640 && TREE_CODE (val->type) == RECORD_TYPE
1641 && TREE_CODE (type) == RECORD_TYPE
1642 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1644 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1645 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1647 if (!flag_ltrans && !warned && !val->odr_violated)
1649 tree extra_base;
1650 warn_odr (type, val->type, NULL, NULL, !warned, &warned,
1651 "a type with the same name but different "
1652 "number of polymorphic bases is "
1653 "defined in another translation unit");
1654 if (warned)
1656 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1657 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1658 extra_base = BINFO_BASE_BINFO
1659 (TYPE_BINFO (type),
1660 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1661 else
1662 extra_base = BINFO_BASE_BINFO
1663 (TYPE_BINFO (val->type),
1664 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1665 tree extra_base_type = BINFO_TYPE (extra_base);
1666 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1667 "the extra base is defined here");
1670 base_mismatch = true;
1672 else
1673 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1675 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1676 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1677 tree type1 = BINFO_TYPE (base1);
1678 tree type2 = BINFO_TYPE (base2);
1680 if (types_odr_comparable (type1, type2))
1682 if (!types_same_for_odr (type1, type2))
1683 base_mismatch = true;
1685 else
1687 hash_set<type_pair,pair_traits> visited;
1688 if (!odr_types_equivalent_p (type1, type2, false, NULL,
1689 &visited))
1690 base_mismatch = true;
1692 if (base_mismatch)
1694 if (!warned && !val->odr_violated)
1696 warn_odr (type, val->type, NULL, NULL,
1697 !warned, &warned,
1698 "a type with the same name but different base "
1699 "type is defined in another translation unit");
1700 if (warned)
1701 warn_types_mismatch (type1, type2);
1703 break;
1705 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1707 base_mismatch = true;
1708 if (!warned && !val->odr_violated)
1709 warn_odr (type, val->type, NULL, NULL,
1710 !warned, &warned,
1711 "a type with the same name but different base "
1712 "layout is defined in another translation unit");
1713 break;
1715 /* One of bases is not of complete type. */
1716 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1718 /* If we have a polymorphic type info specified for TYPE1
1719 but not for TYPE2 we possibly missed a base when recording
1720 VAL->type earlier.
1721 Be sure this does not happen. */
1722 if (TYPE_BINFO (type1)
1723 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1724 && !build_bases)
1725 odr_must_violate = true;
1726 break;
1728 /* One base is polymorphic and the other not.
1729 This ought to be diagnosed earlier, but do not ICE in the
1730 checking bellow. */
1731 else if (TYPE_BINFO (type1)
1732 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1733 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1735 if (!warned && !val->odr_violated)
1736 warn_odr (type, val->type, NULL, NULL,
1737 !warned, &warned,
1738 "a base of the type is polymorphic only in one "
1739 "translation unit");
1740 base_mismatch = true;
1741 break;
1744 if (base_mismatch)
1746 merge = false;
1747 odr_violation_reported = true;
1748 val->odr_violated = true;
1750 if (symtab->dump_file)
1752 fprintf (symtab->dump_file, "ODR base violation\n");
1754 print_node (symtab->dump_file, "", val->type, 0);
1755 putc ('\n',symtab->dump_file);
1756 print_node (symtab->dump_file, "", type, 0);
1757 putc ('\n',symtab->dump_file);
1762 /* Next compare memory layout. */
1763 if (!odr_types_equivalent_p (val->type, type,
1764 !flag_ltrans && !val->odr_violated && !warned,
1765 &warned, &visited))
1767 merge = false;
1768 odr_violation_reported = true;
1769 val->odr_violated = true;
1770 if (symtab->dump_file)
1772 fprintf (symtab->dump_file, "ODR violation\n");
1774 print_node (symtab->dump_file, "", val->type, 0);
1775 putc ('\n',symtab->dump_file);
1776 print_node (symtab->dump_file, "", type, 0);
1777 putc ('\n',symtab->dump_file);
1780 gcc_assert (val->odr_violated || !odr_must_violate);
1781 /* Sanity check that all bases will be build same way again. */
1782 #ifdef ENABLE_CHECKING
1783 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1784 && TREE_CODE (val->type) == RECORD_TYPE
1785 && TREE_CODE (type) == RECORD_TYPE
1786 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1787 && !val->odr_violated
1788 && !base_mismatch && val->bases.length ())
1790 unsigned int num_poly_bases = 0;
1791 unsigned int j;
1793 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1794 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1795 (TYPE_BINFO (type), i)))
1796 num_poly_bases++;
1797 gcc_assert (num_poly_bases == val->bases.length ());
1798 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1799 i++)
1800 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1801 (TYPE_BINFO (type), i)))
1803 odr_type base = get_odr_type
1804 (BINFO_TYPE
1805 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1806 i)),
1807 true);
1808 gcc_assert (val->bases[j] == base);
1809 j++;
1812 #endif
1815 /* Regularize things a little. During LTO same types may come with
1816 different BINFOs. Either because their virtual table was
1817 not merged by tree merging and only later at decl merging or
1818 because one type comes with external vtable, while other
1819 with internal. We want to merge equivalent binfos to conserve
1820 memory and streaming overhead.
1822 The external vtables are more harmful: they contain references
1823 to external declarations of methods that may be defined in the
1824 merged LTO unit. For this reason we absolutely need to remove
1825 them and replace by internal variants. Not doing so will lead
1826 to incomplete answers from possible_polymorphic_call_targets.
1828 FIXME: disable for now; because ODR types are now build during
1829 streaming in, the variants do not need to be linked to the type,
1830 yet. We need to do the merging in cleanup pass to be implemented
1831 soon. */
1832 if (!flag_ltrans && merge
1833 && 0
1834 && TREE_CODE (val->type) == RECORD_TYPE
1835 && TREE_CODE (type) == RECORD_TYPE
1836 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1837 && TYPE_MAIN_VARIANT (type) == type
1838 && TYPE_MAIN_VARIANT (val->type) == val->type
1839 && BINFO_VTABLE (TYPE_BINFO (val->type))
1840 && BINFO_VTABLE (TYPE_BINFO (type)))
1842 tree master_binfo = TYPE_BINFO (val->type);
1843 tree v1 = BINFO_VTABLE (master_binfo);
1844 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1846 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1848 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1849 && operand_equal_p (TREE_OPERAND (v1, 1),
1850 TREE_OPERAND (v2, 1), 0));
1851 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1852 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1854 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1855 == DECL_ASSEMBLER_NAME (v2));
1857 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1859 unsigned int i;
1861 set_type_binfo (val->type, TYPE_BINFO (type));
1862 for (i = 0; i < val->types->length (); i++)
1864 if (TYPE_BINFO ((*val->types)[i])
1865 == master_binfo)
1866 set_type_binfo ((*val->types)[i], TYPE_BINFO (type));
1868 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1870 else
1871 set_type_binfo (type, master_binfo);
1873 return build_bases;
1876 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1877 possibly new entry. */
1879 odr_type
1880 get_odr_type (tree type, bool insert)
1882 odr_type_d **slot = NULL;
1883 odr_type_d **vtable_slot = NULL;
1884 odr_type val = NULL;
1885 hashval_t hash;
1886 bool build_bases = false;
1887 bool insert_to_odr_array = false;
1888 int base_id = -1;
1890 type = main_odr_variant (type);
1892 gcc_checking_assert (can_be_name_hashed_p (type)
1893 || can_be_vtable_hashed_p (type));
1895 /* Lookup entry, first try name hash, fallback to vtable hash. */
1896 if (can_be_name_hashed_p (type))
1898 hash = hash_odr_name (type);
1899 slot = odr_hash->find_slot_with_hash (type, hash,
1900 insert ? INSERT : NO_INSERT);
1902 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
1904 hash = hash_odr_vtable (type);
1905 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
1906 insert ? INSERT : NO_INSERT);
1909 if (!slot && !vtable_slot)
1910 return NULL;
1912 /* See if we already have entry for type. */
1913 if ((slot && *slot) || (vtable_slot && *vtable_slot))
1915 if (slot && *slot)
1917 val = *slot;
1918 #ifdef ENABLE_CHECKING
1919 if (in_lto_p && can_be_vtable_hashed_p (type))
1921 hash = hash_odr_vtable (type);
1922 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
1923 NO_INSERT);
1924 gcc_assert (!vtable_slot || *vtable_slot == *slot);
1925 vtable_slot = NULL;
1927 #endif
1929 else if (*vtable_slot)
1930 val = *vtable_slot;
1932 if (val->type != type
1933 && (!val->types_set || !val->types_set->add (type)))
1935 gcc_assert (insert);
1936 /* We have type duplicate, but it may introduce vtable name or
1937 mangled name; be sure to keep hashes in sync. */
1938 if (in_lto_p && can_be_vtable_hashed_p (type)
1939 && (!vtable_slot || !*vtable_slot))
1941 if (!vtable_slot)
1943 hash = hash_odr_vtable (type);
1944 vtable_slot = odr_vtable_hash->find_slot_with_hash
1945 (type, hash, INSERT);
1946 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
1948 *vtable_slot = val;
1950 if (slot && !*slot)
1951 *slot = val;
1952 build_bases = add_type_duplicate (val, type);
1955 else
1957 val = ggc_cleared_alloc<odr_type_d> ();
1958 val->type = type;
1959 val->bases = vNULL;
1960 val->derived_types = vNULL;
1961 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
1962 build_bases = COMPLETE_TYPE_P (val->type);
1963 insert_to_odr_array = true;
1964 if (slot)
1965 *slot = val;
1966 if (vtable_slot)
1967 *vtable_slot = val;
1970 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1971 && type == TYPE_MAIN_VARIANT (type))
1973 tree binfo = TYPE_BINFO (type);
1974 unsigned int i;
1976 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
1978 val->all_derivations_known = type_all_derivations_known_p (type);
1979 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1980 /* For now record only polymorphic types. other are
1981 pointless for devirtualization and we can not precisely
1982 determine ODR equivalency of these during LTO. */
1983 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1985 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
1986 odr_type base = get_odr_type (base_type, true);
1987 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
1988 base->derived_types.safe_push (val);
1989 val->bases.safe_push (base);
1990 if (base->id > base_id)
1991 base_id = base->id;
1994 /* Ensure that type always appears after bases. */
1995 if (insert_to_odr_array)
1997 if (odr_types_ptr)
1998 val->id = odr_types.length ();
1999 vec_safe_push (odr_types_ptr, val);
2001 else if (base_id > val->id)
2003 odr_types[val->id] = 0;
2004 /* Be sure we did not recorded any derived types; these may need
2005 renumbering too. */
2006 gcc_assert (val->derived_types.length() == 0);
2007 if (odr_types_ptr)
2008 val->id = odr_types.length ();
2009 vec_safe_push (odr_types_ptr, val);
2011 return val;
2014 /* Add TYPE od ODR type hash. */
2016 void
2017 register_odr_type (tree type)
2019 if (!odr_hash)
2021 odr_hash = new odr_hash_type (23);
2022 if (in_lto_p)
2023 odr_vtable_hash = new odr_vtable_hash_type (23);
2025 /* Arrange things to be nicer and insert main variants first. */
2026 if (odr_type_p (TYPE_MAIN_VARIANT (type)))
2027 get_odr_type (TYPE_MAIN_VARIANT (type), true);
2028 if (TYPE_MAIN_VARIANT (type) != type)
2029 get_odr_type (type, true);
2032 /* Return true if type is known to have no derivations. */
2034 bool
2035 type_known_to_have_no_deriavations_p (tree t)
2037 return (type_all_derivations_known_p (t)
2038 && (TYPE_FINAL_P (t)
2039 || (odr_hash
2040 && !get_odr_type (t, true)->derived_types.length())));
2043 /* Dump ODR type T and all its derived types. INDENT specifies indentation for
2044 recursive printing. */
2046 static void
2047 dump_odr_type (FILE *f, odr_type t, int indent=0)
2049 unsigned int i;
2050 fprintf (f, "%*s type %i: ", indent * 2, "", t->id);
2051 print_generic_expr (f, t->type, TDF_SLIM);
2052 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2053 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2054 if (TYPE_NAME (t->type))
2056 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2057 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2058 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2059 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2060 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2061 IDENTIFIER_POINTER
2062 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2064 if (t->bases.length ())
2066 fprintf (f, "%*s base odr type ids: ", indent * 2, "");
2067 for (i = 0; i < t->bases.length (); i++)
2068 fprintf (f, " %i", t->bases[i]->id);
2069 fprintf (f, "\n");
2071 if (t->derived_types.length ())
2073 fprintf (f, "%*s derived types:\n", indent * 2, "");
2074 for (i = 0; i < t->derived_types.length (); i++)
2075 dump_odr_type (f, t->derived_types[i], indent + 1);
2077 fprintf (f, "\n");
2080 /* Dump the type inheritance graph. */
2082 static void
2083 dump_type_inheritance_graph (FILE *f)
2085 unsigned int i;
2086 if (!odr_types_ptr)
2087 return;
2088 fprintf (f, "\n\nType inheritance graph:\n");
2089 for (i = 0; i < odr_types.length (); i++)
2091 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2092 dump_odr_type (f, odr_types[i]);
2094 for (i = 0; i < odr_types.length (); i++)
2096 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ())
2098 unsigned int j;
2099 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2100 print_node (f, "", odr_types[i]->type, 0);
2101 for (j = 0; j < odr_types[i]->types->length (); j++)
2103 tree t;
2104 fprintf (f, "duplicate #%i\n", j);
2105 print_node (f, "", (*odr_types[i]->types)[j], 0);
2106 t = (*odr_types[i]->types)[j];
2107 while (TYPE_P (t) && TYPE_CONTEXT (t))
2109 t = TYPE_CONTEXT (t);
2110 print_node (f, "", t, 0);
2112 putc ('\n',f);
2118 /* Given method type T, return type of class it belongs to.
2119 Look up this pointer and get its type. */
2121 tree
2122 method_class_type (const_tree t)
2124 tree first_parm_type = TREE_VALUE (TYPE_ARG_TYPES (t));
2125 gcc_assert (TREE_CODE (t) == METHOD_TYPE);
2127 return TREE_TYPE (first_parm_type);
2130 /* Initialize IPA devirt and build inheritance tree graph. */
2132 void
2133 build_type_inheritance_graph (void)
2135 struct symtab_node *n;
2136 FILE *inheritance_dump_file;
2137 int flags;
2139 if (odr_hash)
2140 return;
2141 timevar_push (TV_IPA_INHERITANCE);
2142 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2143 odr_hash = new odr_hash_type (23);
2144 if (in_lto_p)
2145 odr_vtable_hash = new odr_vtable_hash_type (23);
2147 /* We reconstruct the graph starting of types of all methods seen in the
2148 the unit. */
2149 FOR_EACH_SYMBOL (n)
2150 if (is_a <cgraph_node *> (n)
2151 && DECL_VIRTUAL_P (n->decl)
2152 && n->real_symbol_p ())
2153 get_odr_type (TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (n->decl))),
2154 true);
2156 /* Look also for virtual tables of types that do not define any methods.
2158 We need it in a case where class B has virtual base of class A
2159 re-defining its virtual method and there is class C with no virtual
2160 methods with B as virtual base.
2162 Here we output B's virtual method in two variant - for non-virtual
2163 and virtual inheritance. B's virtual table has non-virtual version,
2164 while C's has virtual.
2166 For this reason we need to know about C in order to include both
2167 variants of B. More correctly, record_target_from_binfo should
2168 add both variants of the method when walking B, but we have no
2169 link in between them.
2171 We rely on fact that either the method is exported and thus we
2172 assume it is called externally or C is in anonymous namespace and
2173 thus we will see the vtable. */
2175 else if (is_a <varpool_node *> (n)
2176 && DECL_VIRTUAL_P (n->decl)
2177 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2178 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2179 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2180 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), true);
2181 if (inheritance_dump_file)
2183 dump_type_inheritance_graph (inheritance_dump_file);
2184 dump_end (TDI_inheritance, inheritance_dump_file);
2186 timevar_pop (TV_IPA_INHERITANCE);
2189 /* Return true if N has reference from live virtual table
2190 (and thus can be a destination of polymorphic call).
2191 Be conservatively correct when callgraph is not built or
2192 if the method may be referred externally. */
2194 static bool
2195 referenced_from_vtable_p (struct cgraph_node *node)
2197 int i;
2198 struct ipa_ref *ref;
2199 bool found = false;
2201 if (node->externally_visible
2202 || DECL_EXTERNAL (node->decl)
2203 || node->used_from_other_partition)
2204 return true;
2206 /* Keep this test constant time.
2207 It is unlikely this can happen except for the case where speculative
2208 devirtualization introduced many speculative edges to this node.
2209 In this case the target is very likely alive anyway. */
2210 if (node->ref_list.referring.length () > 100)
2211 return true;
2213 /* We need references built. */
2214 if (symtab->state <= CONSTRUCTION)
2215 return true;
2217 for (i = 0; node->iterate_referring (i, ref); i++)
2218 if ((ref->use == IPA_REF_ALIAS
2219 && referenced_from_vtable_p (dyn_cast<cgraph_node *> (ref->referring)))
2220 || (ref->use == IPA_REF_ADDR
2221 && TREE_CODE (ref->referring->decl) == VAR_DECL
2222 && DECL_VIRTUAL_P (ref->referring->decl)))
2224 found = true;
2225 break;
2227 return found;
2230 /* If TARGET has associated node, record it in the NODES array.
2231 CAN_REFER specify if program can refer to the target directly.
2232 if TARGET is unknown (NULL) or it can not be inserted (for example because
2233 its body was already removed and there is no way to refer to it), clear
2234 COMPLETEP. */
2236 static void
2237 maybe_record_node (vec <cgraph_node *> &nodes,
2238 tree target, hash_set<tree> *inserted,
2239 bool can_refer,
2240 bool *completep)
2242 struct cgraph_node *target_node, *alias_target;
2243 enum availability avail;
2245 /* cxa_pure_virtual and __builtin_unreachable do not need to be added into
2246 list of targets; the runtime effect of calling them is undefined.
2247 Only "real" virtual methods should be accounted. */
2248 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE)
2249 return;
2251 if (!can_refer)
2253 /* The only case when method of anonymous namespace becomes unreferable
2254 is when we completely optimized it out. */
2255 if (flag_ltrans
2256 || !target
2257 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2258 *completep = false;
2259 return;
2262 if (!target)
2263 return;
2265 target_node = cgraph_node::get (target);
2267 /* Prefer alias target over aliases, so we do not get confused by
2268 fake duplicates. */
2269 if (target_node)
2271 alias_target = target_node->ultimate_alias_target (&avail);
2272 if (target_node != alias_target
2273 && avail >= AVAIL_AVAILABLE
2274 && target_node->get_availability ())
2275 target_node = alias_target;
2278 /* Method can only be called by polymorphic call if any
2279 of vtables referring to it are alive.
2281 While this holds for non-anonymous functions, too, there are
2282 cases where we want to keep them in the list; for example
2283 inline functions with -fno-weak are static, but we still
2284 may devirtualize them when instance comes from other unit.
2285 The same holds for LTO.
2287 Currently we ignore these functions in speculative devirtualization.
2288 ??? Maybe it would make sense to be more aggressive for LTO even
2289 elsewhere. */
2290 if (!flag_ltrans
2291 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2292 && (!target_node
2293 || !referenced_from_vtable_p (target_node)))
2295 /* See if TARGET is useful function we can deal with. */
2296 else if (target_node != NULL
2297 && (TREE_PUBLIC (target)
2298 || DECL_EXTERNAL (target)
2299 || target_node->definition)
2300 && target_node->real_symbol_p ())
2302 gcc_assert (!target_node->global.inlined_to);
2303 gcc_assert (target_node->real_symbol_p ());
2304 if (!inserted->add (target))
2306 cached_polymorphic_call_targets->add (target_node);
2307 nodes.safe_push (target_node);
2310 else if (completep
2311 && (!type_in_anonymous_namespace_p
2312 (DECL_CONTEXT (target))
2313 || flag_ltrans))
2314 *completep = false;
2317 /* See if BINFO's type matches OUTER_TYPE. If so, look up
2318 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2319 method in vtable and insert method to NODES array
2320 or BASES_TO_CONSIDER if this array is non-NULL.
2321 Otherwise recurse to base BINFOs.
2322 This matches what get_binfo_at_offset does, but with offset
2323 being unknown.
2325 TYPE_BINFOS is a stack of BINFOS of types with defined
2326 virtual table seen on way from class type to BINFO.
2328 MATCHED_VTABLES tracks virtual tables we already did lookup
2329 for virtual function in. INSERTED tracks nodes we already
2330 inserted.
2332 ANONYMOUS is true if BINFO is part of anonymous namespace.
2334 Clear COMPLETEP when we hit unreferable target.
2337 static void
2338 record_target_from_binfo (vec <cgraph_node *> &nodes,
2339 vec <tree> *bases_to_consider,
2340 tree binfo,
2341 tree otr_type,
2342 vec <tree> &type_binfos,
2343 HOST_WIDE_INT otr_token,
2344 tree outer_type,
2345 HOST_WIDE_INT offset,
2346 hash_set<tree> *inserted,
2347 hash_set<tree> *matched_vtables,
2348 bool anonymous,
2349 bool *completep)
2351 tree type = BINFO_TYPE (binfo);
2352 int i;
2353 tree base_binfo;
2356 if (BINFO_VTABLE (binfo))
2357 type_binfos.safe_push (binfo);
2358 if (types_same_for_odr (type, outer_type))
2360 int i;
2361 tree type_binfo = NULL;
2363 /* Look up BINFO with virtual table. For normal types it is always last
2364 binfo on stack. */
2365 for (i = type_binfos.length () - 1; i >= 0; i--)
2366 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2368 type_binfo = type_binfos[i];
2369 break;
2371 if (BINFO_VTABLE (binfo))
2372 type_binfos.pop ();
2373 /* If this is duplicated BINFO for base shared by virtual inheritance,
2374 we may not have its associated vtable. This is not a problem, since
2375 we will walk it on the other path. */
2376 if (!type_binfo)
2377 return;
2378 tree inner_binfo = get_binfo_at_offset (type_binfo,
2379 offset, otr_type);
2380 if (!inner_binfo)
2382 gcc_assert (odr_violation_reported);
2383 return;
2385 /* For types in anonymous namespace first check if the respective vtable
2386 is alive. If not, we know the type can't be called. */
2387 if (!flag_ltrans && anonymous)
2389 tree vtable = BINFO_VTABLE (inner_binfo);
2390 varpool_node *vnode;
2392 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2393 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2394 vnode = varpool_node::get (vtable);
2395 if (!vnode || !vnode->definition)
2396 return;
2398 gcc_assert (inner_binfo);
2399 if (bases_to_consider
2400 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2401 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2403 bool can_refer;
2404 tree target = gimple_get_virt_method_for_binfo (otr_token,
2405 inner_binfo,
2406 &can_refer);
2407 if (!bases_to_consider)
2408 maybe_record_node (nodes, target, inserted, can_refer, completep);
2409 /* Destructors are never called via construction vtables. */
2410 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2411 bases_to_consider->safe_push (target);
2413 return;
2416 /* Walk bases. */
2417 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2418 /* Walking bases that have no virtual method is pointless exercise. */
2419 if (polymorphic_type_binfo_p (base_binfo))
2420 record_target_from_binfo (nodes, bases_to_consider, base_binfo, otr_type,
2421 type_binfos,
2422 otr_token, outer_type, offset, inserted,
2423 matched_vtables, anonymous, completep);
2424 if (BINFO_VTABLE (binfo))
2425 type_binfos.pop ();
2428 /* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2429 of TYPE, insert them to NODES, recurse into derived nodes.
2430 INSERTED is used to avoid duplicate insertions of methods into NODES.
2431 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2432 Clear COMPLETEP if unreferable target is found.
2434 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2435 all cases where BASE_SKIPPED is true (because the base is abstract
2436 class). */
2438 static void
2439 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2440 hash_set<tree> *inserted,
2441 hash_set<tree> *matched_vtables,
2442 tree otr_type,
2443 odr_type type,
2444 HOST_WIDE_INT otr_token,
2445 tree outer_type,
2446 HOST_WIDE_INT offset,
2447 bool *completep,
2448 vec <tree> &bases_to_consider,
2449 bool consider_construction)
2451 tree binfo = TYPE_BINFO (type->type);
2452 unsigned int i;
2453 auto_vec <tree, 8> type_binfos;
2454 bool possibly_instantiated = type_possibly_instantiated_p (type->type);
2456 /* We may need to consider types w/o instances because of possible derived
2457 types using their methods either directly or via construction vtables.
2458 We are safe to skip them when all derivations are known, since we will
2459 handle them later.
2460 This is done by recording them to BASES_TO_CONSIDER array. */
2461 if (possibly_instantiated || consider_construction)
2463 record_target_from_binfo (nodes,
2464 (!possibly_instantiated
2465 && type_all_derivations_known_p (type->type))
2466 ? &bases_to_consider : NULL,
2467 binfo, otr_type, type_binfos, otr_token,
2468 outer_type, offset,
2469 inserted, matched_vtables,
2470 type->anonymous_namespace, completep);
2472 for (i = 0; i < type->derived_types.length (); i++)
2473 possible_polymorphic_call_targets_1 (nodes, inserted,
2474 matched_vtables,
2475 otr_type,
2476 type->derived_types[i],
2477 otr_token, outer_type, offset, completep,
2478 bases_to_consider, consider_construction);
2481 /* Cache of queries for polymorphic call targets.
2483 Enumerating all call targets may get expensive when there are many
2484 polymorphic calls in the program, so we memoize all the previous
2485 queries and avoid duplicated work. */
2487 struct polymorphic_call_target_d
2489 HOST_WIDE_INT otr_token;
2490 ipa_polymorphic_call_context context;
2491 odr_type type;
2492 vec <cgraph_node *> targets;
2493 tree decl_warning;
2494 int type_warning;
2495 bool complete;
2496 bool speculative;
2499 /* Polymorphic call target cache helpers. */
2501 struct polymorphic_call_target_hasher
2503 typedef polymorphic_call_target_d value_type;
2504 typedef polymorphic_call_target_d compare_type;
2505 static inline hashval_t hash (const value_type *);
2506 static inline bool equal (const value_type *, const compare_type *);
2507 static inline void remove (value_type *);
2510 /* Return the computed hashcode for ODR_QUERY. */
2512 inline hashval_t
2513 polymorphic_call_target_hasher::hash (const value_type *odr_query)
2515 inchash::hash hstate (odr_query->otr_token);
2517 hstate.add_wide_int (odr_query->type->id);
2518 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2519 hstate.add_wide_int (odr_query->context.offset);
2521 if (odr_query->context.speculative_outer_type)
2523 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2524 hstate.add_wide_int (odr_query->context.speculative_offset);
2526 hstate.add_flag (odr_query->speculative);
2527 hstate.add_flag (odr_query->context.maybe_in_construction);
2528 hstate.add_flag (odr_query->context.maybe_derived_type);
2529 hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
2530 hstate.commit_flag ();
2531 return hstate.end ();
2534 /* Compare cache entries T1 and T2. */
2536 inline bool
2537 polymorphic_call_target_hasher::equal (const value_type *t1,
2538 const compare_type *t2)
2540 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2541 && t1->speculative == t2->speculative
2542 && t1->context.offset == t2->context.offset
2543 && t1->context.speculative_offset == t2->context.speculative_offset
2544 && t1->context.outer_type == t2->context.outer_type
2545 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2546 && t1->context.maybe_in_construction
2547 == t2->context.maybe_in_construction
2548 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2549 && (t1->context.speculative_maybe_derived_type
2550 == t2->context.speculative_maybe_derived_type));
2553 /* Remove entry in polymorphic call target cache hash. */
2555 inline void
2556 polymorphic_call_target_hasher::remove (value_type *v)
2558 v->targets.release ();
2559 free (v);
2562 /* Polymorphic call target query cache. */
2564 typedef hash_table<polymorphic_call_target_hasher>
2565 polymorphic_call_target_hash_type;
2566 static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2568 /* Destroy polymorphic call target query cache. */
2570 static void
2571 free_polymorphic_call_targets_hash ()
2573 if (cached_polymorphic_call_targets)
2575 delete polymorphic_call_target_hash;
2576 polymorphic_call_target_hash = NULL;
2577 delete cached_polymorphic_call_targets;
2578 cached_polymorphic_call_targets = NULL;
2582 /* When virtual function is removed, we may need to flush the cache. */
2584 static void
2585 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2587 if (cached_polymorphic_call_targets
2588 && cached_polymorphic_call_targets->contains (n))
2589 free_polymorphic_call_targets_hash ();
2592 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2594 tree
2595 subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2596 tree vtable)
2598 tree v = BINFO_VTABLE (binfo);
2599 int i;
2600 tree base_binfo;
2601 unsigned HOST_WIDE_INT this_offset;
2603 if (v)
2605 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2606 gcc_unreachable ();
2608 if (offset == this_offset
2609 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2610 return binfo;
2613 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2614 if (polymorphic_type_binfo_p (base_binfo))
2616 base_binfo = subbinfo_with_vtable_at_offset (base_binfo, offset, vtable);
2617 if (base_binfo)
2618 return base_binfo;
2620 return NULL;
2623 /* T is known constant value of virtual table pointer.
2624 Store virtual table to V and its offset to OFFSET.
2625 Return false if T does not look like virtual table reference. */
2627 bool
2628 vtable_pointer_value_to_vtable (const_tree t, tree *v,
2629 unsigned HOST_WIDE_INT *offset)
2631 /* We expect &MEM[(void *)&virtual_table + 16B].
2632 We obtain object's BINFO from the context of the virtual table.
2633 This one contains pointer to virtual table represented via
2634 POINTER_PLUS_EXPR. Verify that this pointer matches what
2635 we propagated through.
2637 In the case of virtual inheritance, the virtual tables may
2638 be nested, i.e. the offset may be different from 16 and we may
2639 need to dive into the type representation. */
2640 if (TREE_CODE (t) == ADDR_EXPR
2641 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2642 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2643 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2644 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2645 == VAR_DECL)
2646 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2647 (TREE_OPERAND (t, 0), 0), 0)))
2649 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2650 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2651 return true;
2654 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2655 We need to handle it when T comes from static variable initializer or
2656 BINFO. */
2657 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2659 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2660 t = TREE_OPERAND (t, 0);
2662 else
2663 *offset = 0;
2665 if (TREE_CODE (t) != ADDR_EXPR)
2666 return false;
2667 *v = TREE_OPERAND (t, 0);
2668 return true;
2671 /* T is known constant value of virtual table pointer. Return BINFO of the
2672 instance type. */
2674 tree
2675 vtable_pointer_value_to_binfo (const_tree t)
2677 tree vtable;
2678 unsigned HOST_WIDE_INT offset;
2680 if (!vtable_pointer_value_to_vtable (t, &vtable, &offset))
2681 return NULL_TREE;
2683 /* FIXME: for stores of construction vtables we return NULL,
2684 because we do not have BINFO for those. Eventually we should fix
2685 our representation to allow this case to be handled, too.
2686 In the case we see store of BINFO we however may assume
2687 that standard folding will be able to cope with it. */
2688 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2689 offset, vtable);
2692 /* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2693 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2694 and insert them in NODES.
2696 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2698 static void
2699 record_targets_from_bases (tree otr_type,
2700 HOST_WIDE_INT otr_token,
2701 tree outer_type,
2702 HOST_WIDE_INT offset,
2703 vec <cgraph_node *> &nodes,
2704 hash_set<tree> *inserted,
2705 hash_set<tree> *matched_vtables,
2706 bool *completep)
2708 while (true)
2710 HOST_WIDE_INT pos, size;
2711 tree base_binfo;
2712 tree fld;
2714 if (types_same_for_odr (outer_type, otr_type))
2715 return;
2717 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2719 if (TREE_CODE (fld) != FIELD_DECL)
2720 continue;
2722 pos = int_bit_position (fld);
2723 size = tree_to_shwi (DECL_SIZE (fld));
2724 if (pos <= offset && (pos + size) > offset
2725 /* Do not get confused by zero sized bases. */
2726 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2727 break;
2729 /* Within a class type we should always find corresponding fields. */
2730 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2732 /* Nonbase types should have been stripped by outer_class_type. */
2733 gcc_assert (DECL_ARTIFICIAL (fld));
2735 outer_type = TREE_TYPE (fld);
2736 offset -= pos;
2738 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2739 offset, otr_type);
2740 if (!base_binfo)
2742 gcc_assert (odr_violation_reported);
2743 return;
2745 gcc_assert (base_binfo);
2746 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2748 bool can_refer;
2749 tree target = gimple_get_virt_method_for_binfo (otr_token,
2750 base_binfo,
2751 &can_refer);
2752 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2753 maybe_record_node (nodes, target, inserted, can_refer, completep);
2754 matched_vtables->add (BINFO_VTABLE (base_binfo));
2759 /* When virtual table is removed, we may need to flush the cache. */
2761 static void
2762 devirt_variable_node_removal_hook (varpool_node *n,
2763 void *d ATTRIBUTE_UNUSED)
2765 if (cached_polymorphic_call_targets
2766 && DECL_VIRTUAL_P (n->decl)
2767 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2768 free_polymorphic_call_targets_hash ();
2771 /* Record about how many calls would benefit from given type to be final. */
2773 struct odr_type_warn_count
2775 tree type;
2776 int count;
2777 gcov_type dyn_count;
2780 /* Record about how many calls would benefit from given method to be final. */
2782 struct decl_warn_count
2784 tree decl;
2785 int count;
2786 gcov_type dyn_count;
2789 /* Information about type and decl warnings. */
2791 struct final_warning_record
2793 gcov_type dyn_count;
2794 vec<odr_type_warn_count> type_warnings;
2795 hash_map<tree, decl_warn_count> decl_warnings;
2797 struct final_warning_record *final_warning_records;
2799 /* Return vector containing possible targets of polymorphic call of type
2800 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2801 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2802 OTR_TYPE and include their virtual method. This is useful for types
2803 possibly in construction or destruction where the virtual table may
2804 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make
2805 us to walk the inheritance graph for all derivations.
2807 If COMPLETEP is non-NULL, store true if the list is complete.
2808 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2809 in the target cache. If user needs to visit every target list
2810 just once, it can memoize them.
2812 If SPECULATIVE is set, the list will not contain targets that
2813 are not speculatively taken.
2815 Returned vector is placed into cache. It is NOT caller's responsibility
2816 to free it. The vector can be freed on cgraph_remove_node call if
2817 the particular node is a virtual function present in the cache. */
2819 vec <cgraph_node *>
2820 possible_polymorphic_call_targets (tree otr_type,
2821 HOST_WIDE_INT otr_token,
2822 ipa_polymorphic_call_context context,
2823 bool *completep,
2824 void **cache_token,
2825 bool speculative)
2827 static struct cgraph_node_hook_list *node_removal_hook_holder;
2828 vec <cgraph_node *> nodes = vNULL;
2829 auto_vec <tree, 8> bases_to_consider;
2830 odr_type type, outer_type;
2831 polymorphic_call_target_d key;
2832 polymorphic_call_target_d **slot;
2833 unsigned int i;
2834 tree binfo, target;
2835 bool complete;
2836 bool can_refer = false;
2837 bool skipped = false;
2839 otr_type = TYPE_MAIN_VARIANT (otr_type);
2841 /* If ODR is not initialized or the context is invalid, return empty
2842 incomplete list. */
2843 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
2845 if (completep)
2846 *completep = context.invalid;
2847 if (cache_token)
2848 *cache_token = NULL;
2849 return nodes;
2852 /* Do not bother to compute speculative info when user do not asks for it. */
2853 if (!speculative || !context.speculative_outer_type)
2854 context.clear_speculation ();
2856 type = get_odr_type (otr_type, true);
2858 /* Recording type variants would waste results cache. */
2859 gcc_assert (!context.outer_type
2860 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2862 /* Look up the outer class type we want to walk.
2863 If we fail to do so, the context is invalid. */
2864 if ((context.outer_type || context.speculative_outer_type)
2865 && !context.restrict_to_inner_class (otr_type))
2867 if (completep)
2868 *completep = true;
2869 if (cache_token)
2870 *cache_token = NULL;
2871 return nodes;
2873 gcc_assert (!context.invalid);
2875 /* Check that restrict_to_inner_class kept the main variant. */
2876 gcc_assert (!context.outer_type
2877 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
2879 /* We canonicalize our query, so we do not need extra hashtable entries. */
2881 /* Without outer type, we have no use for offset. Just do the
2882 basic search from inner type. */
2883 if (!context.outer_type)
2884 context.clear_outer_type (otr_type);
2885 /* We need to update our hierarchy if the type does not exist. */
2886 outer_type = get_odr_type (context.outer_type, true);
2887 /* If the type is complete, there are no derivations. */
2888 if (TYPE_FINAL_P (outer_type->type))
2889 context.maybe_derived_type = false;
2891 /* Initialize query cache. */
2892 if (!cached_polymorphic_call_targets)
2894 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
2895 polymorphic_call_target_hash
2896 = new polymorphic_call_target_hash_type (23);
2897 if (!node_removal_hook_holder)
2899 node_removal_hook_holder =
2900 symtab->add_cgraph_removal_hook (&devirt_node_removal_hook, NULL);
2901 symtab->add_varpool_removal_hook (&devirt_variable_node_removal_hook,
2902 NULL);
2906 if (in_lto_p)
2908 if (context.outer_type != otr_type)
2909 context.outer_type
2910 = get_odr_type (context.outer_type, true)->type;
2911 if (context.speculative_outer_type)
2912 context.speculative_outer_type
2913 = get_odr_type (context.speculative_outer_type, true)->type;
2916 /* Look up cached answer. */
2917 key.type = type;
2918 key.otr_token = otr_token;
2919 key.speculative = speculative;
2920 key.context = context;
2921 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
2922 if (cache_token)
2923 *cache_token = (void *)*slot;
2924 if (*slot)
2926 if (completep)
2927 *completep = (*slot)->complete;
2928 if ((*slot)->type_warning && final_warning_records)
2930 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
2931 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
2932 += final_warning_records->dyn_count;
2934 if (!speculative && (*slot)->decl_warning && final_warning_records)
2936 struct decl_warn_count *c =
2937 final_warning_records->decl_warnings.get ((*slot)->decl_warning);
2938 c->count++;
2939 c->dyn_count += final_warning_records->dyn_count;
2941 return (*slot)->targets;
2944 complete = true;
2946 /* Do actual search. */
2947 timevar_push (TV_IPA_VIRTUAL_CALL);
2948 *slot = XCNEW (polymorphic_call_target_d);
2949 if (cache_token)
2950 *cache_token = (void *)*slot;
2951 (*slot)->type = type;
2952 (*slot)->otr_token = otr_token;
2953 (*slot)->context = context;
2954 (*slot)->speculative = speculative;
2956 hash_set<tree> inserted;
2957 hash_set<tree> matched_vtables;
2959 /* First insert targets we speculatively identified as likely. */
2960 if (context.speculative_outer_type)
2962 odr_type speculative_outer_type;
2963 bool speculation_complete = true;
2965 /* First insert target from type itself and check if it may have
2966 derived types. */
2967 speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
2968 if (TYPE_FINAL_P (speculative_outer_type->type))
2969 context.speculative_maybe_derived_type = false;
2970 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
2971 context.speculative_offset, otr_type);
2972 if (binfo)
2973 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
2974 &can_refer);
2975 else
2976 target = NULL;
2978 /* In the case we get complete method, we don't need
2979 to walk derivations. */
2980 if (target && DECL_FINAL_P (target))
2981 context.speculative_maybe_derived_type = false;
2982 if (type_possibly_instantiated_p (speculative_outer_type->type))
2983 maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete);
2984 if (binfo)
2985 matched_vtables.add (BINFO_VTABLE (binfo));
2988 /* Next walk recursively all derived types. */
2989 if (context.speculative_maybe_derived_type)
2990 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
2991 possible_polymorphic_call_targets_1 (nodes, &inserted,
2992 &matched_vtables,
2993 otr_type,
2994 speculative_outer_type->derived_types[i],
2995 otr_token, speculative_outer_type->type,
2996 context.speculative_offset,
2997 &speculation_complete,
2998 bases_to_consider,
2999 false);
3002 if (!speculative || !nodes.length ())
3004 /* First see virtual method of type itself. */
3005 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3006 context.offset, otr_type);
3007 if (binfo)
3008 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3009 &can_refer);
3010 else
3012 gcc_assert (odr_violation_reported);
3013 target = NULL;
3016 /* Destructors are never called through construction virtual tables,
3017 because the type is always known. */
3018 if (target && DECL_CXX_DESTRUCTOR_P (target))
3019 context.maybe_in_construction = false;
3021 if (target)
3023 /* In the case we get complete method, we don't need
3024 to walk derivations. */
3025 if (DECL_FINAL_P (target))
3026 context.maybe_derived_type = false;
3029 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3030 if (type_possibly_instantiated_p (outer_type->type))
3031 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3032 else
3033 skipped = true;
3035 if (binfo)
3036 matched_vtables.add (BINFO_VTABLE (binfo));
3038 /* Next walk recursively all derived types. */
3039 if (context.maybe_derived_type)
3041 for (i = 0; i < outer_type->derived_types.length(); i++)
3042 possible_polymorphic_call_targets_1 (nodes, &inserted,
3043 &matched_vtables,
3044 otr_type,
3045 outer_type->derived_types[i],
3046 otr_token, outer_type->type,
3047 context.offset, &complete,
3048 bases_to_consider,
3049 context.maybe_in_construction);
3051 if (!outer_type->all_derivations_known)
3053 if (!speculative && final_warning_records)
3055 if (complete
3056 && nodes.length () == 1
3057 && warn_suggest_final_types
3058 && !outer_type->derived_types.length ())
3060 if (outer_type->id >= (int)final_warning_records->type_warnings.length ())
3061 final_warning_records->type_warnings.safe_grow_cleared
3062 (odr_types.length ());
3063 final_warning_records->type_warnings[outer_type->id].count++;
3064 final_warning_records->type_warnings[outer_type->id].dyn_count
3065 += final_warning_records->dyn_count;
3066 final_warning_records->type_warnings[outer_type->id].type
3067 = outer_type->type;
3068 (*slot)->type_warning = outer_type->id + 1;
3070 if (complete
3071 && warn_suggest_final_methods
3072 && nodes.length () == 1
3073 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3074 outer_type->type))
3076 bool existed;
3077 struct decl_warn_count &c =
3078 final_warning_records->decl_warnings.get_or_insert
3079 (nodes[0]->decl, &existed);
3081 if (existed)
3083 c.count++;
3084 c.dyn_count += final_warning_records->dyn_count;
3086 else
3088 c.count = 1;
3089 c.dyn_count = final_warning_records->dyn_count;
3090 c.decl = nodes[0]->decl;
3092 (*slot)->decl_warning = nodes[0]->decl;
3095 complete = false;
3099 if (!speculative)
3101 /* Destructors are never called through construction virtual tables,
3102 because the type is always known. One of entries may be
3103 cxa_pure_virtual so look to at least two of them. */
3104 if (context.maybe_in_construction)
3105 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3106 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3107 context.maybe_in_construction = false;
3108 if (context.maybe_in_construction)
3110 if (type != outer_type
3111 && (!skipped
3112 || (context.maybe_derived_type
3113 && !type_all_derivations_known_p (outer_type->type))))
3114 record_targets_from_bases (otr_type, otr_token, outer_type->type,
3115 context.offset, nodes, &inserted,
3116 &matched_vtables, &complete);
3117 if (skipped)
3118 maybe_record_node (nodes, target, &inserted, can_refer, &complete);
3119 for (i = 0; i < bases_to_consider.length(); i++)
3120 maybe_record_node (nodes, bases_to_consider[i], &inserted, can_refer, &complete);
3125 (*slot)->targets = nodes;
3126 (*slot)->complete = complete;
3127 if (completep)
3128 *completep = complete;
3130 timevar_pop (TV_IPA_VIRTUAL_CALL);
3131 return nodes;
3134 bool
3135 add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3136 vec<const decl_warn_count*> *vec)
3138 vec->safe_push (&value);
3139 return true;
3142 /* Dump target list TARGETS into FILE. */
3144 static void
3145 dump_targets (FILE *f, vec <cgraph_node *> targets)
3147 unsigned int i;
3149 for (i = 0; i < targets.length (); i++)
3151 char *name = NULL;
3152 if (in_lto_p)
3153 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3154 fprintf (f, " %s/%i", name ? name : targets[i]->name (), targets[i]->order);
3155 if (in_lto_p)
3156 free (name);
3157 if (!targets[i]->definition)
3158 fprintf (f, " (no definition%s)",
3159 DECL_DECLARED_INLINE_P (targets[i]->decl)
3160 ? " inline" : "");
3162 fprintf (f, "\n");
3165 /* Dump all possible targets of a polymorphic call. */
3167 void
3168 dump_possible_polymorphic_call_targets (FILE *f,
3169 tree otr_type,
3170 HOST_WIDE_INT otr_token,
3171 const ipa_polymorphic_call_context &ctx)
3173 vec <cgraph_node *> targets;
3174 bool final;
3175 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3176 unsigned int len;
3178 if (!type)
3179 return;
3180 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3181 ctx,
3182 &final, NULL, false);
3183 fprintf (f, " Targets of polymorphic call of type %i:", type->id);
3184 print_generic_expr (f, type->type, TDF_SLIM);
3185 fprintf (f, " token %i\n", (int)otr_token);
3187 ctx.dump (f);
3189 fprintf (f, " %s%s%s%s\n ",
3190 final ? "This is a complete list." :
3191 "This is partial list; extra targets may be defined in other units.",
3192 ctx.maybe_in_construction ? " (base types included)" : "",
3193 ctx.maybe_derived_type ? " (derived types included)" : "",
3194 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3195 len = targets.length ();
3196 dump_targets (f, targets);
3198 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3199 ctx,
3200 &final, NULL, true);
3201 if (targets.length () != len)
3203 fprintf (f, " Speculative targets:");
3204 dump_targets (f, targets);
3206 gcc_assert (targets.length () <= len);
3207 fprintf (f, "\n");
3211 /* Return true if N can be possibly target of a polymorphic call of
3212 OTR_TYPE/OTR_TOKEN. */
3214 bool
3215 possible_polymorphic_call_target_p (tree otr_type,
3216 HOST_WIDE_INT otr_token,
3217 const ipa_polymorphic_call_context &ctx,
3218 struct cgraph_node *n)
3220 vec <cgraph_node *> targets;
3221 unsigned int i;
3222 enum built_in_function fcode;
3223 bool final;
3225 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE
3226 && ((fcode = DECL_FUNCTION_CODE (n->decl))
3227 == BUILT_IN_UNREACHABLE
3228 || fcode == BUILT_IN_TRAP))
3229 return true;
3231 if (!odr_hash)
3232 return true;
3233 targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
3234 for (i = 0; i < targets.length (); i++)
3235 if (n->semantically_equivalent_p (targets[i]))
3236 return true;
3238 /* At a moment we allow middle end to dig out new external declarations
3239 as a targets of polymorphic calls. */
3240 if (!final && !n->definition)
3241 return true;
3242 return false;
3247 /* Return true if N can be possibly target of a polymorphic call of
3248 OBJ_TYPE_REF expression REF in STMT. */
3250 bool
3251 possible_polymorphic_call_target_p (tree ref,
3252 gimple stmt,
3253 struct cgraph_node *n)
3255 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3256 tree call_fn = gimple_call_fn (stmt);
3258 return possible_polymorphic_call_target_p (obj_type_ref_class (call_fn),
3259 tree_to_uhwi
3260 (OBJ_TYPE_REF_TOKEN (call_fn)),
3261 context,
3266 /* After callgraph construction new external nodes may appear.
3267 Add them into the graph. */
3269 void
3270 update_type_inheritance_graph (void)
3272 struct cgraph_node *n;
3274 if (!odr_hash)
3275 return;
3276 free_polymorphic_call_targets_hash ();
3277 timevar_push (TV_IPA_INHERITANCE);
3278 /* We reconstruct the graph starting from types of all methods seen in the
3279 the unit. */
3280 FOR_EACH_FUNCTION (n)
3281 if (DECL_VIRTUAL_P (n->decl)
3282 && !n->definition
3283 && n->real_symbol_p ())
3284 get_odr_type (method_class_type (TYPE_MAIN_VARIANT (TREE_TYPE (n->decl))),
3285 true);
3286 timevar_pop (TV_IPA_INHERITANCE);
3290 /* Return true if N looks like likely target of a polymorphic call.
3291 Rule out cxa_pure_virtual, noreturns, function declared cold and
3292 other obvious cases. */
3294 bool
3295 likely_target_p (struct cgraph_node *n)
3297 int flags;
3298 /* cxa_pure_virtual and similar things are not likely. */
3299 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3300 return false;
3301 flags = flags_from_decl_or_type (n->decl);
3302 if (flags & ECF_NORETURN)
3303 return false;
3304 if (lookup_attribute ("cold",
3305 DECL_ATTRIBUTES (n->decl)))
3306 return false;
3307 if (n->frequency < NODE_FREQUENCY_NORMAL)
3308 return false;
3309 /* If there are no live virtual tables referring the target,
3310 the only way the target can be called is an instance coming from other
3311 compilation unit; speculative devirtualization is built around an
3312 assumption that won't happen. */
3313 if (!referenced_from_vtable_p (n))
3314 return false;
3315 return true;
3318 /* Compare type warning records P1 and P2 and choose one with larger count;
3319 helper for qsort. */
3322 type_warning_cmp (const void *p1, const void *p2)
3324 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3325 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3327 if (t1->dyn_count < t2->dyn_count)
3328 return 1;
3329 if (t1->dyn_count > t2->dyn_count)
3330 return -1;
3331 return t2->count - t1->count;
3334 /* Compare decl warning records P1 and P2 and choose one with larger count;
3335 helper for qsort. */
3338 decl_warning_cmp (const void *p1, const void *p2)
3340 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3341 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3343 if (t1->dyn_count < t2->dyn_count)
3344 return 1;
3345 if (t1->dyn_count > t2->dyn_count)
3346 return -1;
3347 return t2->count - t1->count;
3351 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3352 context CTX. */
3354 struct cgraph_node *
3355 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3356 ipa_polymorphic_call_context ctx)
3358 vec <cgraph_node *>targets
3359 = possible_polymorphic_call_targets
3360 (otr_type, otr_token, ctx, NULL, NULL, true);
3361 unsigned int i;
3362 struct cgraph_node *likely_target = NULL;
3364 for (i = 0; i < targets.length (); i++)
3365 if (likely_target_p (targets[i]))
3367 if (likely_target)
3368 return NULL;
3369 likely_target = targets[i];
3371 if (!likely_target
3372 ||!likely_target->definition
3373 || DECL_EXTERNAL (likely_target->decl))
3374 return NULL;
3376 /* Don't use an implicitly-declared destructor (c++/58678). */
3377 struct cgraph_node *non_thunk_target
3378 = likely_target->function_symbol ();
3379 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3380 return NULL;
3381 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3382 && likely_target->can_be_discarded_p ())
3383 return NULL;
3384 return likely_target;
3387 /* The ipa-devirt pass.
3388 When polymorphic call has only one likely target in the unit,
3389 turn it into a speculative call. */
3391 static unsigned int
3392 ipa_devirt (void)
3394 struct cgraph_node *n;
3395 hash_set<void *> bad_call_targets;
3396 struct cgraph_edge *e;
3398 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3399 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3400 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3401 int ndropped = 0;
3403 if (!odr_types_ptr)
3404 return 0;
3406 if (dump_file)
3407 dump_type_inheritance_graph (dump_file);
3409 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3410 This is implemented by setting up final_warning_records that are updated
3411 by get_polymorphic_call_targets.
3412 We need to clear cache in this case to trigger recomputation of all
3413 entries. */
3414 if (warn_suggest_final_methods || warn_suggest_final_types)
3416 final_warning_records = new (final_warning_record);
3417 final_warning_records->type_warnings = vNULL;
3418 final_warning_records->type_warnings.safe_grow_cleared (odr_types.length ());
3419 free_polymorphic_call_targets_hash ();
3422 FOR_EACH_DEFINED_FUNCTION (n)
3424 bool update = false;
3425 if (!opt_for_fn (n->decl, flag_devirtualize))
3426 continue;
3427 if (dump_file && n->indirect_calls)
3428 fprintf (dump_file, "\n\nProcesing function %s/%i\n",
3429 n->name (), n->order);
3430 for (e = n->indirect_calls; e; e = e->next_callee)
3431 if (e->indirect_info->polymorphic)
3433 struct cgraph_node *likely_target = NULL;
3434 void *cache_token;
3435 bool final;
3437 if (final_warning_records)
3438 final_warning_records->dyn_count = e->count;
3440 vec <cgraph_node *>targets
3441 = possible_polymorphic_call_targets
3442 (e, &final, &cache_token, true);
3443 unsigned int i;
3445 /* Trigger warnings by calculating non-speculative targets. */
3446 if (warn_suggest_final_methods || warn_suggest_final_types)
3447 possible_polymorphic_call_targets (e);
3449 if (dump_file)
3450 dump_possible_polymorphic_call_targets
3451 (dump_file, e);
3453 npolymorphic++;
3455 /* See if the call can be devirtualized by means of ipa-prop's
3456 polymorphic call context propagation. If not, we can just
3457 forget about this call being polymorphic and avoid some heavy
3458 lifting in remove_unreachable_nodes that will otherwise try to
3459 keep all possible targets alive until inlining and in the inliner
3460 itself.
3462 This may need to be revisited once we add further ways to use
3463 the may edges, but it is a resonable thing to do right now. */
3465 if ((e->indirect_info->param_index == -1
3466 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3467 && e->indirect_info->vptr_changed))
3468 && !flag_ltrans_devirtualize)
3470 e->indirect_info->polymorphic = false;
3471 ndropped++;
3472 if (dump_file)
3473 fprintf (dump_file, "Dropping polymorphic call info;"
3474 " it can not be used by ipa-prop\n");
3477 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3478 continue;
3480 if (!e->maybe_hot_p ())
3482 if (dump_file)
3483 fprintf (dump_file, "Call is cold\n\n");
3484 ncold++;
3485 continue;
3487 if (e->speculative)
3489 if (dump_file)
3490 fprintf (dump_file, "Call is already speculated\n\n");
3491 nspeculated++;
3493 /* When dumping see if we agree with speculation. */
3494 if (!dump_file)
3495 continue;
3497 if (bad_call_targets.contains (cache_token))
3499 if (dump_file)
3500 fprintf (dump_file, "Target list is known to be useless\n\n");
3501 nmultiple++;
3502 continue;
3504 for (i = 0; i < targets.length (); i++)
3505 if (likely_target_p (targets[i]))
3507 if (likely_target)
3509 likely_target = NULL;
3510 if (dump_file)
3511 fprintf (dump_file, "More than one likely target\n\n");
3512 nmultiple++;
3513 break;
3515 likely_target = targets[i];
3517 if (!likely_target)
3519 bad_call_targets.add (cache_token);
3520 continue;
3522 /* This is reached only when dumping; check if we agree or disagree
3523 with the speculation. */
3524 if (e->speculative)
3526 struct cgraph_edge *e2;
3527 struct ipa_ref *ref;
3528 e->speculative_call_info (e2, e, ref);
3529 if (e2->callee->ultimate_alias_target ()
3530 == likely_target->ultimate_alias_target ())
3532 fprintf (dump_file, "We agree with speculation\n\n");
3533 nok++;
3535 else
3537 fprintf (dump_file, "We disagree with speculation\n\n");
3538 nwrong++;
3540 continue;
3542 if (!likely_target->definition)
3544 if (dump_file)
3545 fprintf (dump_file, "Target is not a definition\n\n");
3546 nnotdefined++;
3547 continue;
3549 /* Do not introduce new references to external symbols. While we
3550 can handle these just well, it is common for programs to
3551 incorrectly with headers defining methods they are linked
3552 with. */
3553 if (DECL_EXTERNAL (likely_target->decl))
3555 if (dump_file)
3556 fprintf (dump_file, "Target is external\n\n");
3557 nexternal++;
3558 continue;
3560 /* Don't use an implicitly-declared destructor (c++/58678). */
3561 struct cgraph_node *non_thunk_target
3562 = likely_target->function_symbol ();
3563 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3565 if (dump_file)
3566 fprintf (dump_file, "Target is artificial\n\n");
3567 nartificial++;
3568 continue;
3570 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3571 && likely_target->can_be_discarded_p ())
3573 if (dump_file)
3574 fprintf (dump_file, "Target is overwritable\n\n");
3575 noverwritable++;
3576 continue;
3578 else if (dbg_cnt (devirt))
3580 if (dump_enabled_p ())
3582 location_t locus = gimple_location_safe (e->call_stmt);
3583 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3584 "speculatively devirtualizing call in %s/%i to %s/%i\n",
3585 n->name (), n->order,
3586 likely_target->name (),
3587 likely_target->order);
3589 if (!likely_target->can_be_discarded_p ())
3591 cgraph_node *alias;
3592 alias = dyn_cast<cgraph_node *> (likely_target->noninterposable_alias ());
3593 if (alias)
3594 likely_target = alias;
3596 nconverted++;
3597 update = true;
3598 e->make_speculative
3599 (likely_target, e->count * 8 / 10, e->frequency * 8 / 10);
3602 if (update)
3603 inline_update_overall_summary (n);
3605 if (warn_suggest_final_methods || warn_suggest_final_types)
3607 if (warn_suggest_final_types)
3609 final_warning_records->type_warnings.qsort (type_warning_cmp);
3610 for (unsigned int i = 0;
3611 i < final_warning_records->type_warnings.length (); i++)
3612 if (final_warning_records->type_warnings[i].count)
3614 tree type = final_warning_records->type_warnings[i].type;
3615 int count = final_warning_records->type_warnings[i].count;
3616 long long dyn_count
3617 = final_warning_records->type_warnings[i].dyn_count;
3619 if (!dyn_count)
3620 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3621 OPT_Wsuggest_final_types, count,
3622 "Declaring type %qD final "
3623 "would enable devirtualization of %i call",
3624 "Declaring type %qD final "
3625 "would enable devirtualization of %i calls",
3626 type,
3627 count);
3628 else
3629 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3630 OPT_Wsuggest_final_types, count,
3631 "Declaring type %qD final "
3632 "would enable devirtualization of %i call "
3633 "executed %lli times",
3634 "Declaring type %qD final "
3635 "would enable devirtualization of %i calls "
3636 "executed %lli times",
3637 type,
3638 count,
3639 dyn_count);
3643 if (warn_suggest_final_methods)
3645 vec<const decl_warn_count*> decl_warnings_vec = vNULL;
3647 final_warning_records->decl_warnings.traverse
3648 <vec<const decl_warn_count *> *, add_decl_warning> (&decl_warnings_vec);
3649 decl_warnings_vec.qsort (decl_warning_cmp);
3650 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3652 tree decl = decl_warnings_vec[i]->decl;
3653 int count = decl_warnings_vec[i]->count;
3654 long long dyn_count = decl_warnings_vec[i]->dyn_count;
3656 if (!dyn_count)
3657 if (DECL_CXX_DESTRUCTOR_P (decl))
3658 warning_n (DECL_SOURCE_LOCATION (decl),
3659 OPT_Wsuggest_final_methods, count,
3660 "Declaring virtual destructor of %qD final "
3661 "would enable devirtualization of %i call",
3662 "Declaring virtual destructor of %qD final "
3663 "would enable devirtualization of %i calls",
3664 DECL_CONTEXT (decl), count);
3665 else
3666 warning_n (DECL_SOURCE_LOCATION (decl),
3667 OPT_Wsuggest_final_methods, count,
3668 "Declaring method %qD final "
3669 "would enable devirtualization of %i call",
3670 "Declaring method %qD final "
3671 "would enable devirtualization of %i calls",
3672 decl, count);
3673 else if (DECL_CXX_DESTRUCTOR_P (decl))
3674 warning_n (DECL_SOURCE_LOCATION (decl),
3675 OPT_Wsuggest_final_methods, count,
3676 "Declaring virtual destructor of %qD final "
3677 "would enable devirtualization of %i call "
3678 "executed %lli times",
3679 "Declaring virtual destructor of %qD final "
3680 "would enable devirtualization of %i calls "
3681 "executed %lli times",
3682 DECL_CONTEXT (decl), count, dyn_count);
3683 else
3684 warning_n (DECL_SOURCE_LOCATION (decl),
3685 OPT_Wsuggest_final_methods, count,
3686 "Declaring method %qD final "
3687 "would enable devirtualization of %i call "
3688 "executed %lli times",
3689 "Declaring method %qD final "
3690 "would enable devirtualization of %i calls "
3691 "executed %lli times",
3692 decl, count, dyn_count);
3696 delete (final_warning_records);
3697 final_warning_records = 0;
3700 if (dump_file)
3701 fprintf (dump_file,
3702 "%i polymorphic calls, %i devirtualized,"
3703 " %i speculatively devirtualized, %i cold\n"
3704 "%i have multiple targets, %i overwritable,"
3705 " %i already speculated (%i agree, %i disagree),"
3706 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3707 npolymorphic, ndevirtualized, nconverted, ncold,
3708 nmultiple, noverwritable, nspeculated, nok, nwrong,
3709 nexternal, nnotdefined, nartificial, ndropped);
3710 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3713 namespace {
3715 const pass_data pass_data_ipa_devirt =
3717 IPA_PASS, /* type */
3718 "devirt", /* name */
3719 OPTGROUP_NONE, /* optinfo_flags */
3720 TV_IPA_DEVIRT, /* tv_id */
3721 0, /* properties_required */
3722 0, /* properties_provided */
3723 0, /* properties_destroyed */
3724 0, /* todo_flags_start */
3725 ( TODO_dump_symtab ), /* todo_flags_finish */
3728 class pass_ipa_devirt : public ipa_opt_pass_d
3730 public:
3731 pass_ipa_devirt (gcc::context *ctxt)
3732 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3733 NULL, /* generate_summary */
3734 NULL, /* write_summary */
3735 NULL, /* read_summary */
3736 NULL, /* write_optimization_summary */
3737 NULL, /* read_optimization_summary */
3738 NULL, /* stmt_fixup */
3739 0, /* function_transform_todo_flags_start */
3740 NULL, /* function_transform */
3741 NULL) /* variable_transform */
3744 /* opt_pass methods: */
3745 virtual bool gate (function *)
3747 /* In LTO, always run the IPA passes and decide on function basis if the
3748 pass is enabled. */
3749 if (in_lto_p)
3750 return true;
3751 return (flag_devirtualize
3752 && (flag_devirtualize_speculatively
3753 || (warn_suggest_final_methods
3754 || warn_suggest_final_types))
3755 && optimize);
3758 virtual unsigned int execute (function *) { return ipa_devirt (); }
3760 }; // class pass_ipa_devirt
3762 } // anon namespace
3764 ipa_opt_pass_d *
3765 make_pass_ipa_devirt (gcc::context *ctxt)
3767 return new pass_ipa_devirt (ctxt);
3770 #include "gt-ipa-devirt.h"